ALSA: ymfpci: use enum control info helper
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_transport_sas.c
blob927e99cb72250c61154639ea8ca1eae2dd32b198
1 /*
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 management
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
19 * the same.
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;
44 struct mutex lock;
45 struct request_queue *q;
46 u32 next_target_id;
47 u32 next_expander_id;
48 int next_port_id;
50 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
54 * Hack to allow attributes of the same name in different objects.
56 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct device_attribute dev_attr_##_prefix##_##_name = \
58 __ATTR(_name,_mode,_show,_store)
62 * Pretty printing helpers
65 #define sas_bitfield_name_match(title, table) \
66 static ssize_t \
67 get_sas_##title##_names(u32 table_key, char *buf) \
68 { \
69 char *prefix = ""; \
70 ssize_t len = 0; \
71 int i; \
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); \
77 prefix = ", "; \
78 } \
79 } \
80 len += sprintf(buf + len, "\n"); \
81 return len; \
84 #define sas_bitfield_name_set(title, table) \
85 static ssize_t \
86 set_sas_##title##_names(u32 *table_key, const char *buf) \
87 { \
88 ssize_t len = 0; \
89 int i; \
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; \
96 return 0; \
97 } \
98 } \
99 return -EINVAL; \
102 #define sas_bitfield_name_search(title, table) \
103 static ssize_t \
104 get_sas_##title##_names(u32 table_key, char *buf) \
106 ssize_t len = 0; \
107 int i; \
109 for (i = 0; i < ARRAY_SIZE(table); i++) { \
110 if (table[i].value == table_key) { \
111 len += sprintf(buf + len, "%s", \
112 table[i].name); \
113 break; \
116 len += sprintf(buf + len, "\n"); \
117 return len; \
120 static struct {
121 u32 value;
122 char *name;
123 } sas_device_type_names[] = {
124 { SAS_PHY_UNUSED, "unused" },
125 { SAS_END_DEVICE, "end device" },
126 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
127 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
129 sas_bitfield_name_search(device_type, sas_device_type_names)
132 static struct {
133 u32 value;
134 char *name;
135 } sas_protocol_names[] = {
136 { SAS_PROTOCOL_SATA, "sata" },
137 { SAS_PROTOCOL_SMP, "smp" },
138 { SAS_PROTOCOL_STP, "stp" },
139 { SAS_PROTOCOL_SSP, "ssp" },
141 sas_bitfield_name_match(protocol, sas_protocol_names)
143 static struct {
144 u32 value;
145 char *name;
146 } sas_linkspeed_names[] = {
147 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
148 { SAS_PHY_DISABLED, "Phy disabled" },
149 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
150 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
151 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
152 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
153 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
155 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
156 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
158 static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
160 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
161 struct sas_end_device *rdev;
163 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
165 rdev = rphy_to_end_device(rphy);
166 return rdev;
169 static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
170 struct sas_rphy *rphy)
172 struct request *req;
173 int ret;
174 int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
176 while (!blk_queue_plugged(q)) {
177 req = blk_fetch_request(q);
178 if (!req)
179 break;
181 spin_unlock_irq(q->queue_lock);
183 handler = to_sas_internal(shost->transportt)->f->smp_handler;
184 ret = handler(shost, rphy, req);
185 req->errors = ret;
187 blk_end_request_all(req, ret);
189 spin_lock_irq(q->queue_lock);
193 static void sas_host_smp_request(struct request_queue *q)
195 sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
198 static void sas_non_host_smp_request(struct request_queue *q)
200 struct sas_rphy *rphy = q->queuedata;
201 sas_smp_request(q, rphy_to_shost(rphy), rphy);
204 static void sas_host_release(struct device *dev)
206 struct Scsi_Host *shost = dev_to_shost(dev);
207 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
208 struct request_queue *q = sas_host->q;
210 if (q)
211 blk_cleanup_queue(q);
214 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
216 struct request_queue *q;
217 int error;
218 struct device *dev;
219 char namebuf[20];
220 const char *name;
221 void (*release)(struct device *);
223 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
224 printk("%s can't handle SMP requests\n", shost->hostt->name);
225 return 0;
228 if (rphy) {
229 q = blk_init_queue(sas_non_host_smp_request, NULL);
230 dev = &rphy->dev;
231 name = dev_name(dev);
232 release = NULL;
233 } else {
234 q = blk_init_queue(sas_host_smp_request, NULL);
235 dev = &shost->shost_gendev;
236 snprintf(namebuf, sizeof(namebuf),
237 "sas_host%d", shost->host_no);
238 name = namebuf;
239 release = sas_host_release;
241 if (!q)
242 return -ENOMEM;
244 error = bsg_register_queue(q, dev, name, release);
245 if (error) {
246 blk_cleanup_queue(q);
247 return -ENOMEM;
250 if (rphy)
251 rphy->q = q;
252 else
253 to_sas_host_attrs(shost)->q = q;
255 if (rphy)
256 q->queuedata = rphy;
257 else
258 q->queuedata = shost;
260 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
261 return 0;
264 static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
266 struct request_queue *q;
268 if (rphy)
269 q = rphy->q;
270 else
271 q = to_sas_host_attrs(shost)->q;
273 if (!q)
274 return;
276 bsg_unregister_queue(q);
280 * SAS host attributes
283 static int sas_host_setup(struct transport_container *tc, struct device *dev,
284 struct device *cdev)
286 struct Scsi_Host *shost = dev_to_shost(dev);
287 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
289 INIT_LIST_HEAD(&sas_host->rphy_list);
290 mutex_init(&sas_host->lock);
291 sas_host->next_target_id = 0;
292 sas_host->next_expander_id = 0;
293 sas_host->next_port_id = 0;
295 if (sas_bsg_initialize(shost, NULL))
296 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
297 shost->host_no);
299 return 0;
302 static int sas_host_remove(struct transport_container *tc, struct device *dev,
303 struct device *cdev)
305 struct Scsi_Host *shost = dev_to_shost(dev);
307 sas_bsg_remove(shost, NULL);
309 return 0;
312 static DECLARE_TRANSPORT_CLASS(sas_host_class,
313 "sas_host", sas_host_setup, sas_host_remove, NULL);
315 static int sas_host_match(struct attribute_container *cont,
316 struct device *dev)
318 struct Scsi_Host *shost;
319 struct sas_internal *i;
321 if (!scsi_is_host_device(dev))
322 return 0;
323 shost = dev_to_shost(dev);
325 if (!shost->transportt)
326 return 0;
327 if (shost->transportt->host_attrs.ac.class !=
328 &sas_host_class.class)
329 return 0;
331 i = to_sas_internal(shost->transportt);
332 return &i->t.host_attrs.ac == cont;
335 static int do_sas_phy_delete(struct device *dev, void *data)
337 int pass = (int)(unsigned long)data;
339 if (pass == 0 && scsi_is_sas_port(dev))
340 sas_port_delete(dev_to_sas_port(dev));
341 else if (pass == 1 && scsi_is_sas_phy(dev))
342 sas_phy_delete(dev_to_phy(dev));
343 return 0;
347 * sas_remove_children - tear down a devices SAS data structures
348 * @dev: device belonging to the sas object
350 * Removes all SAS PHYs and remote PHYs for a given object
352 void sas_remove_children(struct device *dev)
354 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
355 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
357 EXPORT_SYMBOL(sas_remove_children);
360 * sas_remove_host - tear down a Scsi_Host's SAS data structures
361 * @shost: Scsi Host that is torn down
363 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
364 * Must be called just before scsi_remove_host for SAS HBAs.
366 void sas_remove_host(struct Scsi_Host *shost)
368 sas_remove_children(&shost->shost_gendev);
370 EXPORT_SYMBOL(sas_remove_host);
373 * sas_tlr_supported - checking TLR bit in vpd 0x90
374 * @sdev: scsi device struct
376 * Check Transport Layer Retries are supported or not.
377 * If vpd page 0x90 is present, TRL is supported.
380 unsigned int
381 sas_tlr_supported(struct scsi_device *sdev)
383 const int vpd_len = 32;
384 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
385 char *buffer = kzalloc(vpd_len, GFP_KERNEL);
386 int ret = 0;
388 if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
389 goto out;
392 * Magic numbers: the VPD Protocol page (0x90)
393 * has a 4 byte header and then one entry per device port
394 * the TLR bit is at offset 8 on each port entry
395 * if we take the first port, that's at total offset 12
397 ret = buffer[12] & 0x01;
399 out:
400 kfree(buffer);
401 rdev->tlr_supported = ret;
402 return ret;
405 EXPORT_SYMBOL_GPL(sas_tlr_supported);
408 * sas_disable_tlr - setting TLR flags
409 * @sdev: scsi device struct
411 * Seting tlr_enabled flag to 0.
414 void
415 sas_disable_tlr(struct scsi_device *sdev)
417 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
419 rdev->tlr_enabled = 0;
421 EXPORT_SYMBOL_GPL(sas_disable_tlr);
424 * sas_enable_tlr - setting TLR flags
425 * @sdev: scsi device struct
427 * Seting tlr_enabled flag 1.
430 void sas_enable_tlr(struct scsi_device *sdev)
432 unsigned int tlr_supported = 0;
433 tlr_supported = sas_tlr_supported(sdev);
435 if (tlr_supported) {
436 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
438 rdev->tlr_enabled = 1;
441 return;
443 EXPORT_SYMBOL_GPL(sas_enable_tlr);
445 unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
447 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
448 return rdev->tlr_enabled;
450 EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
453 * SAS Phy attributes
456 #define sas_phy_show_simple(field, name, format_string, cast) \
457 static ssize_t \
458 show_sas_phy_##name(struct device *dev, \
459 struct device_attribute *attr, char *buf) \
461 struct sas_phy *phy = transport_class_to_phy(dev); \
463 return snprintf(buf, 20, format_string, cast phy->field); \
466 #define sas_phy_simple_attr(field, name, format_string, type) \
467 sas_phy_show_simple(field, name, format_string, (type)) \
468 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
470 #define sas_phy_show_protocol(field, name) \
471 static ssize_t \
472 show_sas_phy_##name(struct device *dev, \
473 struct device_attribute *attr, char *buf) \
475 struct sas_phy *phy = transport_class_to_phy(dev); \
477 if (!phy->field) \
478 return snprintf(buf, 20, "none\n"); \
479 return get_sas_protocol_names(phy->field, buf); \
482 #define sas_phy_protocol_attr(field, name) \
483 sas_phy_show_protocol(field, name) \
484 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
486 #define sas_phy_show_linkspeed(field) \
487 static ssize_t \
488 show_sas_phy_##field(struct device *dev, \
489 struct device_attribute *attr, char *buf) \
491 struct sas_phy *phy = transport_class_to_phy(dev); \
493 return get_sas_linkspeed_names(phy->field, buf); \
496 /* Fudge to tell if we're minimum or maximum */
497 #define sas_phy_store_linkspeed(field) \
498 static ssize_t \
499 store_sas_phy_##field(struct device *dev, \
500 struct device_attribute *attr, \
501 const char *buf, size_t count) \
503 struct sas_phy *phy = transport_class_to_phy(dev); \
504 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
505 struct sas_internal *i = to_sas_internal(shost->transportt); \
506 u32 value; \
507 struct sas_phy_linkrates rates = {0}; \
508 int error; \
510 error = set_sas_linkspeed_names(&value, buf); \
511 if (error) \
512 return error; \
513 rates.field = value; \
514 error = i->f->set_phy_speed(phy, &rates); \
516 return error ? error : count; \
519 #define sas_phy_linkspeed_rw_attr(field) \
520 sas_phy_show_linkspeed(field) \
521 sas_phy_store_linkspeed(field) \
522 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
523 store_sas_phy_##field)
525 #define sas_phy_linkspeed_attr(field) \
526 sas_phy_show_linkspeed(field) \
527 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
530 #define sas_phy_show_linkerror(field) \
531 static ssize_t \
532 show_sas_phy_##field(struct device *dev, \
533 struct device_attribute *attr, char *buf) \
535 struct sas_phy *phy = transport_class_to_phy(dev); \
536 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
537 struct sas_internal *i = to_sas_internal(shost->transportt); \
538 int error; \
540 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
541 if (error) \
542 return error; \
543 return snprintf(buf, 20, "%u\n", phy->field); \
546 #define sas_phy_linkerror_attr(field) \
547 sas_phy_show_linkerror(field) \
548 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
551 static ssize_t
552 show_sas_device_type(struct device *dev,
553 struct device_attribute *attr, char *buf)
555 struct sas_phy *phy = transport_class_to_phy(dev);
557 if (!phy->identify.device_type)
558 return snprintf(buf, 20, "none\n");
559 return get_sas_device_type_names(phy->identify.device_type, buf);
561 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
563 static ssize_t do_sas_phy_enable(struct device *dev,
564 size_t count, int enable)
566 struct sas_phy *phy = transport_class_to_phy(dev);
567 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
568 struct sas_internal *i = to_sas_internal(shost->transportt);
569 int error;
571 error = i->f->phy_enable(phy, enable);
572 if (error)
573 return error;
574 phy->enabled = enable;
575 return count;
578 static ssize_t
579 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
582 if (count < 1)
583 return -EINVAL;
585 switch (buf[0]) {
586 case '0':
587 do_sas_phy_enable(dev, count, 0);
588 break;
589 case '1':
590 do_sas_phy_enable(dev, count, 1);
591 break;
592 default:
593 return -EINVAL;
596 return count;
599 static ssize_t
600 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
601 char *buf)
603 struct sas_phy *phy = transport_class_to_phy(dev);
605 return snprintf(buf, 20, "%d", phy->enabled);
608 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
609 store_sas_phy_enable);
611 static ssize_t
612 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
614 struct sas_phy *phy = transport_class_to_phy(dev);
615 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
616 struct sas_internal *i = to_sas_internal(shost->transportt);
617 int error;
619 error = i->f->phy_reset(phy, hard_reset);
620 if (error)
621 return error;
622 return count;
625 static ssize_t
626 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
629 return do_sas_phy_reset(dev, count, 0);
631 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
633 static ssize_t
634 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
637 return do_sas_phy_reset(dev, count, 1);
639 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
641 sas_phy_protocol_attr(identify.initiator_port_protocols,
642 initiator_port_protocols);
643 sas_phy_protocol_attr(identify.target_port_protocols,
644 target_port_protocols);
645 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
646 unsigned long long);
647 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
648 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
649 sas_phy_linkspeed_attr(negotiated_linkrate);
650 sas_phy_linkspeed_attr(minimum_linkrate_hw);
651 sas_phy_linkspeed_rw_attr(minimum_linkrate);
652 sas_phy_linkspeed_attr(maximum_linkrate_hw);
653 sas_phy_linkspeed_rw_attr(maximum_linkrate);
654 sas_phy_linkerror_attr(invalid_dword_count);
655 sas_phy_linkerror_attr(running_disparity_error_count);
656 sas_phy_linkerror_attr(loss_of_dword_sync_count);
657 sas_phy_linkerror_attr(phy_reset_problem_count);
660 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
661 "sas_phy", NULL, NULL, NULL);
663 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
665 struct Scsi_Host *shost;
666 struct sas_internal *i;
668 if (!scsi_is_sas_phy(dev))
669 return 0;
670 shost = dev_to_shost(dev->parent);
672 if (!shost->transportt)
673 return 0;
674 if (shost->transportt->host_attrs.ac.class !=
675 &sas_host_class.class)
676 return 0;
678 i = to_sas_internal(shost->transportt);
679 return &i->phy_attr_cont.ac == cont;
682 static void sas_phy_release(struct device *dev)
684 struct sas_phy *phy = dev_to_phy(dev);
686 put_device(dev->parent);
687 kfree(phy);
691 * sas_phy_alloc - allocates and initialize a SAS PHY structure
692 * @parent: Parent device
693 * @number: Phy index
695 * Allocates an SAS PHY structure. It will be added in the device tree
696 * below the device specified by @parent, which has to be either a Scsi_Host
697 * or sas_rphy.
699 * Returns:
700 * SAS PHY allocated or %NULL if the allocation failed.
702 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
704 struct Scsi_Host *shost = dev_to_shost(parent);
705 struct sas_phy *phy;
707 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
708 if (!phy)
709 return NULL;
711 phy->number = number;
712 phy->enabled = 1;
714 device_initialize(&phy->dev);
715 phy->dev.parent = get_device(parent);
716 phy->dev.release = sas_phy_release;
717 INIT_LIST_HEAD(&phy->port_siblings);
718 if (scsi_is_sas_expander_device(parent)) {
719 struct sas_rphy *rphy = dev_to_rphy(parent);
720 dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
721 rphy->scsi_target_id, number);
722 } else
723 dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
725 transport_setup_device(&phy->dev);
727 return phy;
729 EXPORT_SYMBOL(sas_phy_alloc);
732 * sas_phy_add - add a SAS PHY to the device hierarchy
733 * @phy: The PHY to be added
735 * Publishes a SAS PHY to the rest of the system.
737 int sas_phy_add(struct sas_phy *phy)
739 int error;
741 error = device_add(&phy->dev);
742 if (!error) {
743 transport_add_device(&phy->dev);
744 transport_configure_device(&phy->dev);
747 return error;
749 EXPORT_SYMBOL(sas_phy_add);
752 * sas_phy_free - free a SAS PHY
753 * @phy: SAS PHY to free
755 * Frees the specified SAS PHY.
757 * Note:
758 * This function must only be called on a PHY that has not
759 * successfully been added using sas_phy_add().
761 void sas_phy_free(struct sas_phy *phy)
763 transport_destroy_device(&phy->dev);
764 put_device(&phy->dev);
766 EXPORT_SYMBOL(sas_phy_free);
769 * sas_phy_delete - remove SAS PHY
770 * @phy: SAS PHY to remove
772 * Removes the specified SAS PHY. If the SAS PHY has an
773 * associated remote PHY it is removed before.
775 void
776 sas_phy_delete(struct sas_phy *phy)
778 struct device *dev = &phy->dev;
780 /* this happens if the phy is still part of a port when deleted */
781 BUG_ON(!list_empty(&phy->port_siblings));
783 transport_remove_device(dev);
784 device_del(dev);
785 transport_destroy_device(dev);
786 put_device(dev);
788 EXPORT_SYMBOL(sas_phy_delete);
791 * scsi_is_sas_phy - check if a struct device represents a SAS PHY
792 * @dev: device to check
794 * Returns:
795 * %1 if the device represents a SAS PHY, %0 else
797 int scsi_is_sas_phy(const struct device *dev)
799 return dev->release == sas_phy_release;
801 EXPORT_SYMBOL(scsi_is_sas_phy);
804 * SAS Port attributes
806 #define sas_port_show_simple(field, name, format_string, cast) \
807 static ssize_t \
808 show_sas_port_##name(struct device *dev, \
809 struct device_attribute *attr, char *buf) \
811 struct sas_port *port = transport_class_to_sas_port(dev); \
813 return snprintf(buf, 20, format_string, cast port->field); \
816 #define sas_port_simple_attr(field, name, format_string, type) \
817 sas_port_show_simple(field, name, format_string, (type)) \
818 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
820 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
822 static DECLARE_TRANSPORT_CLASS(sas_port_class,
823 "sas_port", NULL, NULL, NULL);
825 static int sas_port_match(struct attribute_container *cont, struct device *dev)
827 struct Scsi_Host *shost;
828 struct sas_internal *i;
830 if (!scsi_is_sas_port(dev))
831 return 0;
832 shost = dev_to_shost(dev->parent);
834 if (!shost->transportt)
835 return 0;
836 if (shost->transportt->host_attrs.ac.class !=
837 &sas_host_class.class)
838 return 0;
840 i = to_sas_internal(shost->transportt);
841 return &i->port_attr_cont.ac == cont;
845 static void sas_port_release(struct device *dev)
847 struct sas_port *port = dev_to_sas_port(dev);
849 BUG_ON(!list_empty(&port->phy_list));
851 put_device(dev->parent);
852 kfree(port);
855 static void sas_port_create_link(struct sas_port *port,
856 struct sas_phy *phy)
858 int res;
860 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
861 dev_name(&phy->dev));
862 if (res)
863 goto err;
864 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
865 if (res)
866 goto err;
867 return;
868 err:
869 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
870 __func__, res);
873 static void sas_port_delete_link(struct sas_port *port,
874 struct sas_phy *phy)
876 sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
877 sysfs_remove_link(&phy->dev.kobj, "port");
880 /** sas_port_alloc - allocate and initialize a SAS port structure
882 * @parent: parent device
883 * @port_id: port number
885 * Allocates a SAS port structure. It will be added to the device tree
886 * below the device specified by @parent which must be either a Scsi_Host
887 * or a sas_expander_device.
889 * Returns %NULL on error
891 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
893 struct Scsi_Host *shost = dev_to_shost(parent);
894 struct sas_port *port;
896 port = kzalloc(sizeof(*port), GFP_KERNEL);
897 if (!port)
898 return NULL;
900 port->port_identifier = port_id;
902 device_initialize(&port->dev);
904 port->dev.parent = get_device(parent);
905 port->dev.release = sas_port_release;
907 mutex_init(&port->phy_list_mutex);
908 INIT_LIST_HEAD(&port->phy_list);
910 if (scsi_is_sas_expander_device(parent)) {
911 struct sas_rphy *rphy = dev_to_rphy(parent);
912 dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
913 rphy->scsi_target_id, port->port_identifier);
914 } else
915 dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
916 port->port_identifier);
918 transport_setup_device(&port->dev);
920 return port;
922 EXPORT_SYMBOL(sas_port_alloc);
924 /** sas_port_alloc_num - allocate and initialize a SAS port structure
926 * @parent: parent device
928 * Allocates a SAS port structure and a number to go with it. This
929 * interface is really for adapters where the port number has no
930 * meansing, so the sas class should manage them. It will be added to
931 * the device tree below the device specified by @parent which must be
932 * either a Scsi_Host or a sas_expander_device.
934 * Returns %NULL on error
936 struct sas_port *sas_port_alloc_num(struct device *parent)
938 int index;
939 struct Scsi_Host *shost = dev_to_shost(parent);
940 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
942 /* FIXME: use idr for this eventually */
943 mutex_lock(&sas_host->lock);
944 if (scsi_is_sas_expander_device(parent)) {
945 struct sas_rphy *rphy = dev_to_rphy(parent);
946 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
948 index = exp->next_port_id++;
949 } else
950 index = sas_host->next_port_id++;
951 mutex_unlock(&sas_host->lock);
952 return sas_port_alloc(parent, index);
954 EXPORT_SYMBOL(sas_port_alloc_num);
957 * sas_port_add - add a SAS port to the device hierarchy
958 * @port: port to be added
960 * publishes a port to the rest of the system
962 int sas_port_add(struct sas_port *port)
964 int error;
966 /* No phys should be added until this is made visible */
967 BUG_ON(!list_empty(&port->phy_list));
969 error = device_add(&port->dev);
971 if (error)
972 return error;
974 transport_add_device(&port->dev);
975 transport_configure_device(&port->dev);
977 return 0;
979 EXPORT_SYMBOL(sas_port_add);
982 * sas_port_free - free a SAS PORT
983 * @port: SAS PORT to free
985 * Frees the specified SAS PORT.
987 * Note:
988 * This function must only be called on a PORT that has not
989 * successfully been added using sas_port_add().
991 void sas_port_free(struct sas_port *port)
993 transport_destroy_device(&port->dev);
994 put_device(&port->dev);
996 EXPORT_SYMBOL(sas_port_free);
999 * sas_port_delete - remove SAS PORT
1000 * @port: SAS PORT to remove
1002 * Removes the specified SAS PORT. If the SAS PORT has an
1003 * associated phys, unlink them from the port as well.
1005 void sas_port_delete(struct sas_port *port)
1007 struct device *dev = &port->dev;
1008 struct sas_phy *phy, *tmp_phy;
1010 if (port->rphy) {
1011 sas_rphy_delete(port->rphy);
1012 port->rphy = NULL;
1015 mutex_lock(&port->phy_list_mutex);
1016 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1017 port_siblings) {
1018 sas_port_delete_link(port, phy);
1019 list_del_init(&phy->port_siblings);
1021 mutex_unlock(&port->phy_list_mutex);
1023 if (port->is_backlink) {
1024 struct device *parent = port->dev.parent;
1026 sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1027 port->is_backlink = 0;
1030 transport_remove_device(dev);
1031 device_del(dev);
1032 transport_destroy_device(dev);
1033 put_device(dev);
1035 EXPORT_SYMBOL(sas_port_delete);
1038 * scsi_is_sas_port - check if a struct device represents a SAS port
1039 * @dev: device to check
1041 * Returns:
1042 * %1 if the device represents a SAS Port, %0 else
1044 int scsi_is_sas_port(const struct device *dev)
1046 return dev->release == sas_port_release;
1048 EXPORT_SYMBOL(scsi_is_sas_port);
1051 * sas_port_add_phy - add another phy to a port to form a wide port
1052 * @port: port to add the phy to
1053 * @phy: phy to add
1055 * When a port is initially created, it is empty (has no phys). All
1056 * ports must have at least one phy to operated, and all wide ports
1057 * must have at least two. The current code makes no difference
1058 * between ports and wide ports, but the only object that can be
1059 * connected to a remote device is a port, so ports must be formed on
1060 * all devices with phys if they're connected to anything.
1062 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1064 mutex_lock(&port->phy_list_mutex);
1065 if (unlikely(!list_empty(&phy->port_siblings))) {
1066 /* make sure we're already on this port */
1067 struct sas_phy *tmp;
1069 list_for_each_entry(tmp, &port->phy_list, port_siblings)
1070 if (tmp == phy)
1071 break;
1072 /* If this trips, you added a phy that was already
1073 * part of a different port */
1074 if (unlikely(tmp != phy)) {
1075 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1076 dev_name(&phy->dev));
1077 BUG();
1079 } else {
1080 sas_port_create_link(port, phy);
1081 list_add_tail(&phy->port_siblings, &port->phy_list);
1082 port->num_phys++;
1084 mutex_unlock(&port->phy_list_mutex);
1086 EXPORT_SYMBOL(sas_port_add_phy);
1089 * sas_port_delete_phy - remove a phy from a port or wide port
1090 * @port: port to remove the phy from
1091 * @phy: phy to remove
1093 * This operation is used for tearing down ports again. It must be
1094 * done to every port or wide port before calling sas_port_delete.
1096 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1098 mutex_lock(&port->phy_list_mutex);
1099 sas_port_delete_link(port, phy);
1100 list_del_init(&phy->port_siblings);
1101 port->num_phys--;
1102 mutex_unlock(&port->phy_list_mutex);
1104 EXPORT_SYMBOL(sas_port_delete_phy);
1106 void sas_port_mark_backlink(struct sas_port *port)
1108 int res;
1109 struct device *parent = port->dev.parent->parent->parent;
1111 if (port->is_backlink)
1112 return;
1113 port->is_backlink = 1;
1114 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1115 dev_name(parent));
1116 if (res)
1117 goto err;
1118 return;
1119 err:
1120 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1121 __func__, res);
1124 EXPORT_SYMBOL(sas_port_mark_backlink);
1127 * SAS remote PHY attributes.
1130 #define sas_rphy_show_simple(field, name, format_string, cast) \
1131 static ssize_t \
1132 show_sas_rphy_##name(struct device *dev, \
1133 struct device_attribute *attr, char *buf) \
1135 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1137 return snprintf(buf, 20, format_string, cast rphy->field); \
1140 #define sas_rphy_simple_attr(field, name, format_string, type) \
1141 sas_rphy_show_simple(field, name, format_string, (type)) \
1142 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1143 show_sas_rphy_##name, NULL)
1145 #define sas_rphy_show_protocol(field, name) \
1146 static ssize_t \
1147 show_sas_rphy_##name(struct device *dev, \
1148 struct device_attribute *attr, char *buf) \
1150 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1152 if (!rphy->field) \
1153 return snprintf(buf, 20, "none\n"); \
1154 return get_sas_protocol_names(rphy->field, buf); \
1157 #define sas_rphy_protocol_attr(field, name) \
1158 sas_rphy_show_protocol(field, name) \
1159 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1160 show_sas_rphy_##name, NULL)
1162 static ssize_t
1163 show_sas_rphy_device_type(struct device *dev,
1164 struct device_attribute *attr, char *buf)
1166 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1168 if (!rphy->identify.device_type)
1169 return snprintf(buf, 20, "none\n");
1170 return get_sas_device_type_names(
1171 rphy->identify.device_type, buf);
1174 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1175 show_sas_rphy_device_type, NULL);
1177 static ssize_t
1178 show_sas_rphy_enclosure_identifier(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1181 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1182 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1183 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1184 struct sas_internal *i = to_sas_internal(shost->transportt);
1185 u64 identifier;
1186 int error;
1189 * Only devices behind an expander are supported, because the
1190 * enclosure identifier is a SMP feature.
1192 if (scsi_is_sas_phy_local(phy))
1193 return -EINVAL;
1195 error = i->f->get_enclosure_identifier(rphy, &identifier);
1196 if (error)
1197 return error;
1198 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1201 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1202 show_sas_rphy_enclosure_identifier, NULL);
1204 static ssize_t
1205 show_sas_rphy_bay_identifier(struct device *dev,
1206 struct device_attribute *attr, char *buf)
1208 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1209 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1210 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1211 struct sas_internal *i = to_sas_internal(shost->transportt);
1212 int val;
1214 if (scsi_is_sas_phy_local(phy))
1215 return -EINVAL;
1217 val = i->f->get_bay_identifier(rphy);
1218 if (val < 0)
1219 return val;
1220 return sprintf(buf, "%d\n", val);
1223 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1224 show_sas_rphy_bay_identifier, NULL);
1226 sas_rphy_protocol_attr(identify.initiator_port_protocols,
1227 initiator_port_protocols);
1228 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1229 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1230 unsigned long long);
1231 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1233 /* only need 8 bytes of data plus header (4 or 8) */
1234 #define BUF_SIZE 64
1236 int sas_read_port_mode_page(struct scsi_device *sdev)
1238 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1239 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
1240 struct scsi_mode_data mode_data;
1241 int res, error;
1243 if (!buffer)
1244 return -ENOMEM;
1246 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1247 &mode_data, NULL);
1249 error = -EINVAL;
1250 if (!scsi_status_is_good(res))
1251 goto out;
1253 msdata = buffer + mode_data.header_length +
1254 mode_data.block_descriptor_length;
1256 if (msdata - buffer > BUF_SIZE - 8)
1257 goto out;
1259 error = 0;
1261 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1262 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1263 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1265 out:
1266 kfree(buffer);
1267 return error;
1269 EXPORT_SYMBOL(sas_read_port_mode_page);
1271 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1272 "sas_end_device", NULL, NULL, NULL);
1274 #define sas_end_dev_show_simple(field, name, format_string, cast) \
1275 static ssize_t \
1276 show_sas_end_dev_##name(struct device *dev, \
1277 struct device_attribute *attr, char *buf) \
1279 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1280 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1282 return snprintf(buf, 20, format_string, cast rdev->field); \
1285 #define sas_end_dev_simple_attr(field, name, format_string, type) \
1286 sas_end_dev_show_simple(field, name, format_string, (type)) \
1287 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1288 show_sas_end_dev_##name, NULL)
1290 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1291 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1292 "%d\n", int);
1293 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1294 "%d\n", int);
1295 sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1296 "%d\n", int);
1297 sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1298 "%d\n", int);
1300 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1301 "sas_expander", NULL, NULL, NULL);
1303 #define sas_expander_show_simple(field, name, format_string, cast) \
1304 static ssize_t \
1305 show_sas_expander_##name(struct device *dev, \
1306 struct device_attribute *attr, char *buf) \
1308 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1309 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1311 return snprintf(buf, 20, format_string, cast edev->field); \
1314 #define sas_expander_simple_attr(field, name, format_string, type) \
1315 sas_expander_show_simple(field, name, format_string, (type)) \
1316 static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
1317 show_sas_expander_##name, NULL)
1319 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1320 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1321 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1322 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1323 "%s\n", char *);
1324 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1325 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1326 unsigned int);
1327 sas_expander_simple_attr(level, level, "%d\n", int);
1329 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1330 "sas_device", NULL, NULL, NULL);
1332 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1334 struct Scsi_Host *shost;
1335 struct sas_internal *i;
1337 if (!scsi_is_sas_rphy(dev))
1338 return 0;
1339 shost = dev_to_shost(dev->parent->parent);
1341 if (!shost->transportt)
1342 return 0;
1343 if (shost->transportt->host_attrs.ac.class !=
1344 &sas_host_class.class)
1345 return 0;
1347 i = to_sas_internal(shost->transportt);
1348 return &i->rphy_attr_cont.ac == cont;
1351 static int sas_end_dev_match(struct attribute_container *cont,
1352 struct device *dev)
1354 struct Scsi_Host *shost;
1355 struct sas_internal *i;
1356 struct sas_rphy *rphy;
1358 if (!scsi_is_sas_rphy(dev))
1359 return 0;
1360 shost = dev_to_shost(dev->parent->parent);
1361 rphy = dev_to_rphy(dev);
1363 if (!shost->transportt)
1364 return 0;
1365 if (shost->transportt->host_attrs.ac.class !=
1366 &sas_host_class.class)
1367 return 0;
1369 i = to_sas_internal(shost->transportt);
1370 return &i->end_dev_attr_cont.ac == cont &&
1371 rphy->identify.device_type == SAS_END_DEVICE;
1374 static int sas_expander_match(struct attribute_container *cont,
1375 struct device *dev)
1377 struct Scsi_Host *shost;
1378 struct sas_internal *i;
1379 struct sas_rphy *rphy;
1381 if (!scsi_is_sas_rphy(dev))
1382 return 0;
1383 shost = dev_to_shost(dev->parent->parent);
1384 rphy = dev_to_rphy(dev);
1386 if (!shost->transportt)
1387 return 0;
1388 if (shost->transportt->host_attrs.ac.class !=
1389 &sas_host_class.class)
1390 return 0;
1392 i = to_sas_internal(shost->transportt);
1393 return &i->expander_attr_cont.ac == cont &&
1394 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1395 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1398 static void sas_expander_release(struct device *dev)
1400 struct sas_rphy *rphy = dev_to_rphy(dev);
1401 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1403 if (rphy->q)
1404 blk_cleanup_queue(rphy->q);
1406 put_device(dev->parent);
1407 kfree(edev);
1410 static void sas_end_device_release(struct device *dev)
1412 struct sas_rphy *rphy = dev_to_rphy(dev);
1413 struct sas_end_device *edev = rphy_to_end_device(rphy);
1415 if (rphy->q)
1416 blk_cleanup_queue(rphy->q);
1418 put_device(dev->parent);
1419 kfree(edev);
1423 * sas_rphy_initialize - common rphy intialization
1424 * @rphy: rphy to initialise
1426 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1427 * initialise the common rphy component of each.
1429 static void sas_rphy_initialize(struct sas_rphy *rphy)
1431 INIT_LIST_HEAD(&rphy->list);
1435 * sas_end_device_alloc - allocate an rphy for an end device
1436 * @parent: which port
1438 * Allocates an SAS remote PHY structure, connected to @parent.
1440 * Returns:
1441 * SAS PHY allocated or %NULL if the allocation failed.
1443 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1445 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1446 struct sas_end_device *rdev;
1448 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1449 if (!rdev) {
1450 return NULL;
1453 device_initialize(&rdev->rphy.dev);
1454 rdev->rphy.dev.parent = get_device(&parent->dev);
1455 rdev->rphy.dev.release = sas_end_device_release;
1456 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1457 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1458 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1459 shost->host_no, rphy->scsi_target_id,
1460 parent->port_identifier);
1461 } else
1462 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1463 shost->host_no, parent->port_identifier);
1464 rdev->rphy.identify.device_type = SAS_END_DEVICE;
1465 sas_rphy_initialize(&rdev->rphy);
1466 transport_setup_device(&rdev->rphy.dev);
1468 return &rdev->rphy;
1470 EXPORT_SYMBOL(sas_end_device_alloc);
1473 * sas_expander_alloc - allocate an rphy for an end device
1474 * @parent: which port
1475 * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
1477 * Allocates an SAS remote PHY structure, connected to @parent.
1479 * Returns:
1480 * SAS PHY allocated or %NULL if the allocation failed.
1482 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1483 enum sas_device_type type)
1485 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1486 struct sas_expander_device *rdev;
1487 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1489 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1490 type != SAS_FANOUT_EXPANDER_DEVICE);
1492 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1493 if (!rdev) {
1494 return NULL;
1497 device_initialize(&rdev->rphy.dev);
1498 rdev->rphy.dev.parent = get_device(&parent->dev);
1499 rdev->rphy.dev.release = sas_expander_release;
1500 mutex_lock(&sas_host->lock);
1501 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1502 mutex_unlock(&sas_host->lock);
1503 dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1504 shost->host_no, rdev->rphy.scsi_target_id);
1505 rdev->rphy.identify.device_type = type;
1506 sas_rphy_initialize(&rdev->rphy);
1507 transport_setup_device(&rdev->rphy.dev);
1509 return &rdev->rphy;
1511 EXPORT_SYMBOL(sas_expander_alloc);
1514 * sas_rphy_add - add a SAS remote PHY to the device hierarchy
1515 * @rphy: The remote PHY to be added
1517 * Publishes a SAS remote PHY to the rest of the system.
1519 int sas_rphy_add(struct sas_rphy *rphy)
1521 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1522 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1523 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1524 struct sas_identify *identify = &rphy->identify;
1525 int error;
1527 if (parent->rphy)
1528 return -ENXIO;
1529 parent->rphy = rphy;
1531 error = device_add(&rphy->dev);
1532 if (error)
1533 return error;
1534 transport_add_device(&rphy->dev);
1535 transport_configure_device(&rphy->dev);
1536 if (sas_bsg_initialize(shost, rphy))
1537 printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
1540 mutex_lock(&sas_host->lock);
1541 list_add_tail(&rphy->list, &sas_host->rphy_list);
1542 if (identify->device_type == SAS_END_DEVICE &&
1543 (identify->target_port_protocols &
1544 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1545 rphy->scsi_target_id = sas_host->next_target_id++;
1546 else if (identify->device_type == SAS_END_DEVICE)
1547 rphy->scsi_target_id = -1;
1548 mutex_unlock(&sas_host->lock);
1550 if (identify->device_type == SAS_END_DEVICE &&
1551 rphy->scsi_target_id != -1) {
1552 scsi_scan_target(&rphy->dev, 0,
1553 rphy->scsi_target_id, SCAN_WILD_CARD, 0);
1556 return 0;
1558 EXPORT_SYMBOL(sas_rphy_add);
1561 * sas_rphy_free - free a SAS remote PHY
1562 * @rphy: SAS remote PHY to free
1564 * Frees the specified SAS remote PHY.
1566 * Note:
1567 * This function must only be called on a remote
1568 * PHY that has not successfully been added using
1569 * sas_rphy_add() (or has been sas_rphy_remove()'d)
1571 void sas_rphy_free(struct sas_rphy *rphy)
1573 struct device *dev = &rphy->dev;
1574 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1575 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1577 mutex_lock(&sas_host->lock);
1578 list_del(&rphy->list);
1579 mutex_unlock(&sas_host->lock);
1581 sas_bsg_remove(shost, rphy);
1583 transport_destroy_device(dev);
1585 put_device(dev);
1587 EXPORT_SYMBOL(sas_rphy_free);
1590 * sas_rphy_delete - remove and free SAS remote PHY
1591 * @rphy: SAS remote PHY to remove and free
1593 * Removes the specified SAS remote PHY and frees it.
1595 void
1596 sas_rphy_delete(struct sas_rphy *rphy)
1598 sas_rphy_remove(rphy);
1599 sas_rphy_free(rphy);
1601 EXPORT_SYMBOL(sas_rphy_delete);
1604 * sas_rphy_remove - remove SAS remote PHY
1605 * @rphy: SAS remote phy to remove
1607 * Removes the specified SAS remote PHY.
1609 void
1610 sas_rphy_remove(struct sas_rphy *rphy)
1612 struct device *dev = &rphy->dev;
1613 struct sas_port *parent = dev_to_sas_port(dev->parent);
1615 switch (rphy->identify.device_type) {
1616 case SAS_END_DEVICE:
1617 scsi_remove_target(dev);
1618 break;
1619 case SAS_EDGE_EXPANDER_DEVICE:
1620 case SAS_FANOUT_EXPANDER_DEVICE:
1621 sas_remove_children(dev);
1622 break;
1623 default:
1624 break;
1627 transport_remove_device(dev);
1628 device_del(dev);
1630 parent->rphy = NULL;
1632 EXPORT_SYMBOL(sas_rphy_remove);
1635 * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
1636 * @dev: device to check
1638 * Returns:
1639 * %1 if the device represents a SAS remote PHY, %0 else
1641 int scsi_is_sas_rphy(const struct device *dev)
1643 return dev->release == sas_end_device_release ||
1644 dev->release == sas_expander_release;
1646 EXPORT_SYMBOL(scsi_is_sas_rphy);
1650 * SCSI scan helper
1653 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1654 uint id, uint lun)
1656 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1657 struct sas_rphy *rphy;
1659 mutex_lock(&sas_host->lock);
1660 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1661 if (rphy->identify.device_type != SAS_END_DEVICE ||
1662 rphy->scsi_target_id == -1)
1663 continue;
1665 if ((channel == SCAN_WILD_CARD || channel == 0) &&
1666 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1667 scsi_scan_target(&rphy->dev, 0,
1668 rphy->scsi_target_id, lun, 1);
1671 mutex_unlock(&sas_host->lock);
1673 return 0;
1678 * Setup / Teardown code
1681 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1682 i->private_##attrb[count] = dev_attr_##field; \
1683 i->private_##attrb[count].attr.mode = perm; \
1684 i->attrb[count] = &i->private_##attrb[count]; \
1685 if (test) \
1686 count++
1688 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1689 i->private_##attrb[count] = dev_attr_##field; \
1690 i->private_##attrb[count].attr.mode = perm; \
1691 if (ro_test) { \
1692 i->private_##attrb[count].attr.mode = ro_perm; \
1693 i->private_##attrb[count].store = NULL; \
1695 i->attrb[count] = &i->private_##attrb[count]; \
1696 if (test) \
1697 count++
1699 #define SETUP_RPORT_ATTRIBUTE(field) \
1700 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1702 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1703 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1705 #define SETUP_PHY_ATTRIBUTE(field) \
1706 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1708 #define SETUP_PHY_ATTRIBUTE_RW(field) \
1709 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1710 !i->f->set_phy_speed, S_IRUGO)
1712 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1713 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1714 !i->f->func, S_IRUGO)
1716 #define SETUP_PORT_ATTRIBUTE(field) \
1717 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1719 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1720 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1722 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1723 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1725 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1726 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1728 #define SETUP_END_DEV_ATTRIBUTE(field) \
1729 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1731 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1732 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1735 * sas_attach_transport - instantiate SAS transport template
1736 * @ft: SAS transport class function template
1738 struct scsi_transport_template *
1739 sas_attach_transport(struct sas_function_template *ft)
1741 struct sas_internal *i;
1742 int count;
1744 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1745 if (!i)
1746 return NULL;
1748 i->t.user_scan = sas_user_scan;
1750 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1751 i->t.host_attrs.ac.class = &sas_host_class.class;
1752 i->t.host_attrs.ac.match = sas_host_match;
1753 transport_container_register(&i->t.host_attrs);
1754 i->t.host_size = sizeof(struct sas_host_attrs);
1756 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1757 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1758 i->phy_attr_cont.ac.match = sas_phy_match;
1759 transport_container_register(&i->phy_attr_cont);
1761 i->port_attr_cont.ac.class = &sas_port_class.class;
1762 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1763 i->port_attr_cont.ac.match = sas_port_match;
1764 transport_container_register(&i->port_attr_cont);
1766 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1767 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1768 i->rphy_attr_cont.ac.match = sas_rphy_match;
1769 transport_container_register(&i->rphy_attr_cont);
1771 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1772 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1773 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1774 transport_container_register(&i->end_dev_attr_cont);
1776 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1777 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1778 i->expander_attr_cont.ac.match = sas_expander_match;
1779 transport_container_register(&i->expander_attr_cont);
1781 i->f = ft;
1783 count = 0;
1784 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1785 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1786 SETUP_PHY_ATTRIBUTE(device_type);
1787 SETUP_PHY_ATTRIBUTE(sas_address);
1788 SETUP_PHY_ATTRIBUTE(phy_identifier);
1789 //SETUP_PHY_ATTRIBUTE(port_identifier);
1790 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1791 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1792 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
1793 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1794 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1796 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1797 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1798 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1799 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1800 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1801 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1802 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1803 i->phy_attrs[count] = NULL;
1805 count = 0;
1806 SETUP_PORT_ATTRIBUTE(num_phys);
1807 i->port_attrs[count] = NULL;
1809 count = 0;
1810 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1811 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1812 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1813 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1814 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1815 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1816 get_enclosure_identifier);
1817 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1818 get_bay_identifier);
1819 i->rphy_attrs[count] = NULL;
1821 count = 0;
1822 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1823 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1824 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1825 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1826 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
1827 i->end_dev_attrs[count] = NULL;
1829 count = 0;
1830 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1831 SETUP_EXPANDER_ATTRIBUTE(product_id);
1832 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1833 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1834 SETUP_EXPANDER_ATTRIBUTE(component_id);
1835 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1836 SETUP_EXPANDER_ATTRIBUTE(level);
1837 i->expander_attrs[count] = NULL;
1839 return &i->t;
1841 EXPORT_SYMBOL(sas_attach_transport);
1844 * sas_release_transport - release SAS transport template instance
1845 * @t: transport template instance
1847 void sas_release_transport(struct scsi_transport_template *t)
1849 struct sas_internal *i = to_sas_internal(t);
1851 transport_container_unregister(&i->t.host_attrs);
1852 transport_container_unregister(&i->phy_attr_cont);
1853 transport_container_unregister(&i->port_attr_cont);
1854 transport_container_unregister(&i->rphy_attr_cont);
1855 transport_container_unregister(&i->end_dev_attr_cont);
1856 transport_container_unregister(&i->expander_attr_cont);
1858 kfree(i);
1860 EXPORT_SYMBOL(sas_release_transport);
1862 static __init int sas_transport_init(void)
1864 int error;
1866 error = transport_class_register(&sas_host_class);
1867 if (error)
1868 goto out;
1869 error = transport_class_register(&sas_phy_class);
1870 if (error)
1871 goto out_unregister_transport;
1872 error = transport_class_register(&sas_port_class);
1873 if (error)
1874 goto out_unregister_phy;
1875 error = transport_class_register(&sas_rphy_class);
1876 if (error)
1877 goto out_unregister_port;
1878 error = transport_class_register(&sas_end_dev_class);
1879 if (error)
1880 goto out_unregister_rphy;
1881 error = transport_class_register(&sas_expander_class);
1882 if (error)
1883 goto out_unregister_end_dev;
1885 return 0;
1887 out_unregister_end_dev:
1888 transport_class_unregister(&sas_end_dev_class);
1889 out_unregister_rphy:
1890 transport_class_unregister(&sas_rphy_class);
1891 out_unregister_port:
1892 transport_class_unregister(&sas_port_class);
1893 out_unregister_phy:
1894 transport_class_unregister(&sas_phy_class);
1895 out_unregister_transport:
1896 transport_class_unregister(&sas_host_class);
1897 out:
1898 return error;
1902 static void __exit sas_transport_exit(void)
1904 transport_class_unregister(&sas_host_class);
1905 transport_class_unregister(&sas_phy_class);
1906 transport_class_unregister(&sas_port_class);
1907 transport_class_unregister(&sas_rphy_class);
1908 transport_class_unregister(&sas_end_dev_class);
1909 transport_class_unregister(&sas_expander_class);
1912 MODULE_AUTHOR("Christoph Hellwig");
1913 MODULE_DESCRIPTION("SAS Transport Attributes");
1914 MODULE_LICENSE("GPL");
1916 module_init(sas_transport_init);
1917 module_exit(sas_transport_exit);