powerpc/mm/slice: create header files dedicated to slices
[linux-2.6/btrfs-unstable.git] / drivers / scsi / scsi_transport_sas.c
blob736a1f4f96761c84bf2be7104f82cd5d767178a5
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_cmnd.h>
37 #include <scsi/scsi_request.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_transport.h>
41 #include <scsi/scsi_transport_sas.h>
43 #include "scsi_sas_internal.h"
44 struct sas_host_attrs {
45 struct list_head rphy_list;
46 struct mutex lock;
47 struct request_queue *q;
48 u32 next_target_id;
49 u32 next_expander_id;
50 int next_port_id;
52 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
56 * Hack to allow attributes of the same name in different objects.
58 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59 struct device_attribute dev_attr_##_prefix##_##_name = \
60 __ATTR(_name,_mode,_show,_store)
64 * Pretty printing helpers
67 #define sas_bitfield_name_match(title, table) \
68 static ssize_t \
69 get_sas_##title##_names(u32 table_key, char *buf) \
70 { \
71 char *prefix = ""; \
72 ssize_t len = 0; \
73 int i; \
75 for (i = 0; i < ARRAY_SIZE(table); i++) { \
76 if (table[i].value & table_key) { \
77 len += sprintf(buf + len, "%s%s", \
78 prefix, table[i].name); \
79 prefix = ", "; \
80 } \
81 } \
82 len += sprintf(buf + len, "\n"); \
83 return len; \
86 #define sas_bitfield_name_set(title, table) \
87 static ssize_t \
88 set_sas_##title##_names(u32 *table_key, const char *buf) \
89 { \
90 ssize_t len = 0; \
91 int i; \
93 for (i = 0; i < ARRAY_SIZE(table); i++) { \
94 len = strlen(table[i].name); \
95 if (strncmp(buf, table[i].name, len) == 0 && \
96 (buf[len] == '\n' || buf[len] == '\0')) { \
97 *table_key = table[i].value; \
98 return 0; \
99 } \
101 return -EINVAL; \
104 #define sas_bitfield_name_search(title, table) \
105 static ssize_t \
106 get_sas_##title##_names(u32 table_key, char *buf) \
108 ssize_t len = 0; \
109 int i; \
111 for (i = 0; i < ARRAY_SIZE(table); i++) { \
112 if (table[i].value == table_key) { \
113 len += sprintf(buf + len, "%s", \
114 table[i].name); \
115 break; \
118 len += sprintf(buf + len, "\n"); \
119 return len; \
122 static struct {
123 u32 value;
124 char *name;
125 } sas_device_type_names[] = {
126 { SAS_PHY_UNUSED, "unused" },
127 { SAS_END_DEVICE, "end device" },
128 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
129 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
131 sas_bitfield_name_search(device_type, sas_device_type_names)
134 static struct {
135 u32 value;
136 char *name;
137 } sas_protocol_names[] = {
138 { SAS_PROTOCOL_SATA, "sata" },
139 { SAS_PROTOCOL_SMP, "smp" },
140 { SAS_PROTOCOL_STP, "stp" },
141 { SAS_PROTOCOL_SSP, "ssp" },
143 sas_bitfield_name_match(protocol, sas_protocol_names)
145 static struct {
146 u32 value;
147 char *name;
148 } sas_linkspeed_names[] = {
149 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
150 { SAS_PHY_DISABLED, "Phy disabled" },
151 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
152 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
153 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
154 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
155 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
156 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
158 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
159 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
161 static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
163 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
164 struct sas_end_device *rdev;
166 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
168 rdev = rphy_to_end_device(rphy);
169 return rdev;
172 static int sas_smp_dispatch(struct bsg_job *job)
174 struct Scsi_Host *shost = dev_to_shost(job->dev);
175 struct sas_rphy *rphy = NULL;
177 if (!scsi_is_host_device(job->dev))
178 rphy = dev_to_rphy(job->dev);
180 if (!job->reply_payload.payload_len) {
181 dev_warn(job->dev, "space for a smp response is missing\n");
182 bsg_job_done(job, -EINVAL, 0);
183 return 0;
186 to_sas_internal(shost->transportt)->f->smp_handler(job, shost, rphy);
187 return 0;
190 static void sas_host_release(struct device *dev)
192 struct Scsi_Host *shost = dev_to_shost(dev);
193 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
194 struct request_queue *q = sas_host->q;
196 if (q)
197 blk_cleanup_queue(q);
200 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
202 struct request_queue *q;
204 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
205 printk("%s can't handle SMP requests\n", shost->hostt->name);
206 return 0;
209 if (rphy) {
210 q = bsg_setup_queue(&rphy->dev, dev_name(&rphy->dev),
211 sas_smp_dispatch, 0, NULL);
212 if (IS_ERR(q))
213 return PTR_ERR(q);
214 rphy->q = q;
215 } else {
216 char name[20];
218 snprintf(name, sizeof(name), "sas_host%d", shost->host_no);
219 q = bsg_setup_queue(&shost->shost_gendev, name,
220 sas_smp_dispatch, 0, sas_host_release);
221 if (IS_ERR(q))
222 return PTR_ERR(q);
223 to_sas_host_attrs(shost)->q = q;
227 * by default assume old behaviour and bounce for any highmem page
229 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
230 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
231 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
232 return 0;
236 * SAS host attributes
239 static int sas_host_setup(struct transport_container *tc, struct device *dev,
240 struct device *cdev)
242 struct Scsi_Host *shost = dev_to_shost(dev);
243 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
245 INIT_LIST_HEAD(&sas_host->rphy_list);
246 mutex_init(&sas_host->lock);
247 sas_host->next_target_id = 0;
248 sas_host->next_expander_id = 0;
249 sas_host->next_port_id = 0;
251 if (sas_bsg_initialize(shost, NULL))
252 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
253 shost->host_no);
255 return 0;
258 static int sas_host_remove(struct transport_container *tc, struct device *dev,
259 struct device *cdev)
261 struct Scsi_Host *shost = dev_to_shost(dev);
262 struct request_queue *q = to_sas_host_attrs(shost)->q;
264 if (q)
265 bsg_unregister_queue(q);
266 return 0;
269 static DECLARE_TRANSPORT_CLASS(sas_host_class,
270 "sas_host", sas_host_setup, sas_host_remove, NULL);
272 static int sas_host_match(struct attribute_container *cont,
273 struct device *dev)
275 struct Scsi_Host *shost;
276 struct sas_internal *i;
278 if (!scsi_is_host_device(dev))
279 return 0;
280 shost = dev_to_shost(dev);
282 if (!shost->transportt)
283 return 0;
284 if (shost->transportt->host_attrs.ac.class !=
285 &sas_host_class.class)
286 return 0;
288 i = to_sas_internal(shost->transportt);
289 return &i->t.host_attrs.ac == cont;
292 static int do_sas_phy_delete(struct device *dev, void *data)
294 int pass = (int)(unsigned long)data;
296 if (pass == 0 && scsi_is_sas_port(dev))
297 sas_port_delete(dev_to_sas_port(dev));
298 else if (pass == 1 && scsi_is_sas_phy(dev))
299 sas_phy_delete(dev_to_phy(dev));
300 return 0;
304 * sas_remove_children - tear down a devices SAS data structures
305 * @dev: device belonging to the sas object
307 * Removes all SAS PHYs and remote PHYs for a given object
309 void sas_remove_children(struct device *dev)
311 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
312 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
314 EXPORT_SYMBOL(sas_remove_children);
317 * sas_remove_host - tear down a Scsi_Host's SAS data structures
318 * @shost: Scsi Host that is torn down
320 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
321 * Scsi_Host as well.
323 * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
324 * already removed.
326 void sas_remove_host(struct Scsi_Host *shost)
328 sas_remove_children(&shost->shost_gendev);
329 scsi_remove_host(shost);
331 EXPORT_SYMBOL(sas_remove_host);
334 * sas_get_address - return the SAS address of the device
335 * @sdev: scsi device
337 * Returns the SAS address of the scsi device
339 u64 sas_get_address(struct scsi_device *sdev)
341 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
343 return rdev->rphy.identify.sas_address;
345 EXPORT_SYMBOL(sas_get_address);
348 * sas_tlr_supported - checking TLR bit in vpd 0x90
349 * @sdev: scsi device struct
351 * Check Transport Layer Retries are supported or not.
352 * If vpd page 0x90 is present, TRL is supported.
355 unsigned int
356 sas_tlr_supported(struct scsi_device *sdev)
358 const int vpd_len = 32;
359 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
360 char *buffer = kzalloc(vpd_len, GFP_KERNEL);
361 int ret = 0;
363 if (!buffer)
364 goto out;
366 if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
367 goto out;
370 * Magic numbers: the VPD Protocol page (0x90)
371 * has a 4 byte header and then one entry per device port
372 * the TLR bit is at offset 8 on each port entry
373 * if we take the first port, that's at total offset 12
375 ret = buffer[12] & 0x01;
377 out:
378 kfree(buffer);
379 rdev->tlr_supported = ret;
380 return ret;
383 EXPORT_SYMBOL_GPL(sas_tlr_supported);
386 * sas_disable_tlr - setting TLR flags
387 * @sdev: scsi device struct
389 * Seting tlr_enabled flag to 0.
392 void
393 sas_disable_tlr(struct scsi_device *sdev)
395 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
397 rdev->tlr_enabled = 0;
399 EXPORT_SYMBOL_GPL(sas_disable_tlr);
402 * sas_enable_tlr - setting TLR flags
403 * @sdev: scsi device struct
405 * Seting tlr_enabled flag 1.
408 void sas_enable_tlr(struct scsi_device *sdev)
410 unsigned int tlr_supported = 0;
411 tlr_supported = sas_tlr_supported(sdev);
413 if (tlr_supported) {
414 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
416 rdev->tlr_enabled = 1;
419 return;
421 EXPORT_SYMBOL_GPL(sas_enable_tlr);
423 unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
425 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
426 return rdev->tlr_enabled;
428 EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
431 * SAS Phy attributes
434 #define sas_phy_show_simple(field, name, format_string, cast) \
435 static ssize_t \
436 show_sas_phy_##name(struct device *dev, \
437 struct device_attribute *attr, char *buf) \
439 struct sas_phy *phy = transport_class_to_phy(dev); \
441 return snprintf(buf, 20, format_string, cast phy->field); \
444 #define sas_phy_simple_attr(field, name, format_string, type) \
445 sas_phy_show_simple(field, name, format_string, (type)) \
446 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
448 #define sas_phy_show_protocol(field, name) \
449 static ssize_t \
450 show_sas_phy_##name(struct device *dev, \
451 struct device_attribute *attr, char *buf) \
453 struct sas_phy *phy = transport_class_to_phy(dev); \
455 if (!phy->field) \
456 return snprintf(buf, 20, "none\n"); \
457 return get_sas_protocol_names(phy->field, buf); \
460 #define sas_phy_protocol_attr(field, name) \
461 sas_phy_show_protocol(field, name) \
462 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
464 #define sas_phy_show_linkspeed(field) \
465 static ssize_t \
466 show_sas_phy_##field(struct device *dev, \
467 struct device_attribute *attr, char *buf) \
469 struct sas_phy *phy = transport_class_to_phy(dev); \
471 return get_sas_linkspeed_names(phy->field, buf); \
474 /* Fudge to tell if we're minimum or maximum */
475 #define sas_phy_store_linkspeed(field) \
476 static ssize_t \
477 store_sas_phy_##field(struct device *dev, \
478 struct device_attribute *attr, \
479 const char *buf, size_t count) \
481 struct sas_phy *phy = transport_class_to_phy(dev); \
482 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
483 struct sas_internal *i = to_sas_internal(shost->transportt); \
484 u32 value; \
485 struct sas_phy_linkrates rates = {0}; \
486 int error; \
488 error = set_sas_linkspeed_names(&value, buf); \
489 if (error) \
490 return error; \
491 rates.field = value; \
492 error = i->f->set_phy_speed(phy, &rates); \
494 return error ? error : count; \
497 #define sas_phy_linkspeed_rw_attr(field) \
498 sas_phy_show_linkspeed(field) \
499 sas_phy_store_linkspeed(field) \
500 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
501 store_sas_phy_##field)
503 #define sas_phy_linkspeed_attr(field) \
504 sas_phy_show_linkspeed(field) \
505 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
508 #define sas_phy_show_linkerror(field) \
509 static ssize_t \
510 show_sas_phy_##field(struct device *dev, \
511 struct device_attribute *attr, char *buf) \
513 struct sas_phy *phy = transport_class_to_phy(dev); \
514 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
515 struct sas_internal *i = to_sas_internal(shost->transportt); \
516 int error; \
518 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
519 if (error) \
520 return error; \
521 return snprintf(buf, 20, "%u\n", phy->field); \
524 #define sas_phy_linkerror_attr(field) \
525 sas_phy_show_linkerror(field) \
526 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
529 static ssize_t
530 show_sas_device_type(struct device *dev,
531 struct device_attribute *attr, char *buf)
533 struct sas_phy *phy = transport_class_to_phy(dev);
535 if (!phy->identify.device_type)
536 return snprintf(buf, 20, "none\n");
537 return get_sas_device_type_names(phy->identify.device_type, buf);
539 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
541 static ssize_t do_sas_phy_enable(struct device *dev,
542 size_t count, int enable)
544 struct sas_phy *phy = transport_class_to_phy(dev);
545 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
546 struct sas_internal *i = to_sas_internal(shost->transportt);
547 int error;
549 error = i->f->phy_enable(phy, enable);
550 if (error)
551 return error;
552 phy->enabled = enable;
553 return count;
556 static ssize_t
557 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
560 if (count < 1)
561 return -EINVAL;
563 switch (buf[0]) {
564 case '0':
565 do_sas_phy_enable(dev, count, 0);
566 break;
567 case '1':
568 do_sas_phy_enable(dev, count, 1);
569 break;
570 default:
571 return -EINVAL;
574 return count;
577 static ssize_t
578 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
579 char *buf)
581 struct sas_phy *phy = transport_class_to_phy(dev);
583 return snprintf(buf, 20, "%d", phy->enabled);
586 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
587 store_sas_phy_enable);
589 static ssize_t
590 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
592 struct sas_phy *phy = transport_class_to_phy(dev);
593 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
594 struct sas_internal *i = to_sas_internal(shost->transportt);
595 int error;
597 error = i->f->phy_reset(phy, hard_reset);
598 if (error)
599 return error;
600 phy->enabled = 1;
601 return count;
604 static ssize_t
605 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
608 return do_sas_phy_reset(dev, count, 0);
610 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
612 static ssize_t
613 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
616 return do_sas_phy_reset(dev, count, 1);
618 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
620 sas_phy_protocol_attr(identify.initiator_port_protocols,
621 initiator_port_protocols);
622 sas_phy_protocol_attr(identify.target_port_protocols,
623 target_port_protocols);
624 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
625 unsigned long long);
626 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
627 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
628 sas_phy_linkspeed_attr(negotiated_linkrate);
629 sas_phy_linkspeed_attr(minimum_linkrate_hw);
630 sas_phy_linkspeed_rw_attr(minimum_linkrate);
631 sas_phy_linkspeed_attr(maximum_linkrate_hw);
632 sas_phy_linkspeed_rw_attr(maximum_linkrate);
633 sas_phy_linkerror_attr(invalid_dword_count);
634 sas_phy_linkerror_attr(running_disparity_error_count);
635 sas_phy_linkerror_attr(loss_of_dword_sync_count);
636 sas_phy_linkerror_attr(phy_reset_problem_count);
638 static int sas_phy_setup(struct transport_container *tc, struct device *dev,
639 struct device *cdev)
641 struct sas_phy *phy = dev_to_phy(dev);
642 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
643 struct sas_internal *i = to_sas_internal(shost->transportt);
645 if (i->f->phy_setup)
646 i->f->phy_setup(phy);
648 return 0;
651 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
652 "sas_phy", sas_phy_setup, NULL, NULL);
654 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
656 struct Scsi_Host *shost;
657 struct sas_internal *i;
659 if (!scsi_is_sas_phy(dev))
660 return 0;
661 shost = dev_to_shost(dev->parent);
663 if (!shost->transportt)
664 return 0;
665 if (shost->transportt->host_attrs.ac.class !=
666 &sas_host_class.class)
667 return 0;
669 i = to_sas_internal(shost->transportt);
670 return &i->phy_attr_cont.ac == cont;
673 static void sas_phy_release(struct device *dev)
675 struct sas_phy *phy = dev_to_phy(dev);
676 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
677 struct sas_internal *i = to_sas_internal(shost->transportt);
679 if (i->f->phy_release)
680 i->f->phy_release(phy);
681 put_device(dev->parent);
682 kfree(phy);
686 * sas_phy_alloc - allocates and initialize a SAS PHY structure
687 * @parent: Parent device
688 * @number: Phy index
690 * Allocates an SAS PHY structure. It will be added in the device tree
691 * below the device specified by @parent, which has to be either a Scsi_Host
692 * or sas_rphy.
694 * Returns:
695 * SAS PHY allocated or %NULL if the allocation failed.
697 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
699 struct Scsi_Host *shost = dev_to_shost(parent);
700 struct sas_phy *phy;
702 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
703 if (!phy)
704 return NULL;
706 phy->number = number;
707 phy->enabled = 1;
709 device_initialize(&phy->dev);
710 phy->dev.parent = get_device(parent);
711 phy->dev.release = sas_phy_release;
712 INIT_LIST_HEAD(&phy->port_siblings);
713 if (scsi_is_sas_expander_device(parent)) {
714 struct sas_rphy *rphy = dev_to_rphy(parent);
715 dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
716 rphy->scsi_target_id, number);
717 } else
718 dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
720 transport_setup_device(&phy->dev);
722 return phy;
724 EXPORT_SYMBOL(sas_phy_alloc);
727 * sas_phy_add - add a SAS PHY to the device hierarchy
728 * @phy: The PHY to be added
730 * Publishes a SAS PHY to the rest of the system.
732 int sas_phy_add(struct sas_phy *phy)
734 int error;
736 error = device_add(&phy->dev);
737 if (!error) {
738 transport_add_device(&phy->dev);
739 transport_configure_device(&phy->dev);
742 return error;
744 EXPORT_SYMBOL(sas_phy_add);
747 * sas_phy_free - free a SAS PHY
748 * @phy: SAS PHY to free
750 * Frees the specified SAS PHY.
752 * Note:
753 * This function must only be called on a PHY that has not
754 * successfully been added using sas_phy_add().
756 void sas_phy_free(struct sas_phy *phy)
758 transport_destroy_device(&phy->dev);
759 put_device(&phy->dev);
761 EXPORT_SYMBOL(sas_phy_free);
764 * sas_phy_delete - remove SAS PHY
765 * @phy: SAS PHY to remove
767 * Removes the specified SAS PHY. If the SAS PHY has an
768 * associated remote PHY it is removed before.
770 void
771 sas_phy_delete(struct sas_phy *phy)
773 struct device *dev = &phy->dev;
775 /* this happens if the phy is still part of a port when deleted */
776 BUG_ON(!list_empty(&phy->port_siblings));
778 transport_remove_device(dev);
779 device_del(dev);
780 transport_destroy_device(dev);
781 put_device(dev);
783 EXPORT_SYMBOL(sas_phy_delete);
786 * scsi_is_sas_phy - check if a struct device represents a SAS PHY
787 * @dev: device to check
789 * Returns:
790 * %1 if the device represents a SAS PHY, %0 else
792 int scsi_is_sas_phy(const struct device *dev)
794 return dev->release == sas_phy_release;
796 EXPORT_SYMBOL(scsi_is_sas_phy);
799 * SAS Port attributes
801 #define sas_port_show_simple(field, name, format_string, cast) \
802 static ssize_t \
803 show_sas_port_##name(struct device *dev, \
804 struct device_attribute *attr, char *buf) \
806 struct sas_port *port = transport_class_to_sas_port(dev); \
808 return snprintf(buf, 20, format_string, cast port->field); \
811 #define sas_port_simple_attr(field, name, format_string, type) \
812 sas_port_show_simple(field, name, format_string, (type)) \
813 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
815 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
817 static DECLARE_TRANSPORT_CLASS(sas_port_class,
818 "sas_port", NULL, NULL, NULL);
820 static int sas_port_match(struct attribute_container *cont, struct device *dev)
822 struct Scsi_Host *shost;
823 struct sas_internal *i;
825 if (!scsi_is_sas_port(dev))
826 return 0;
827 shost = dev_to_shost(dev->parent);
829 if (!shost->transportt)
830 return 0;
831 if (shost->transportt->host_attrs.ac.class !=
832 &sas_host_class.class)
833 return 0;
835 i = to_sas_internal(shost->transportt);
836 return &i->port_attr_cont.ac == cont;
840 static void sas_port_release(struct device *dev)
842 struct sas_port *port = dev_to_sas_port(dev);
844 BUG_ON(!list_empty(&port->phy_list));
846 put_device(dev->parent);
847 kfree(port);
850 static void sas_port_create_link(struct sas_port *port,
851 struct sas_phy *phy)
853 int res;
855 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
856 dev_name(&phy->dev));
857 if (res)
858 goto err;
859 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
860 if (res)
861 goto err;
862 return;
863 err:
864 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
865 __func__, res);
868 static void sas_port_delete_link(struct sas_port *port,
869 struct sas_phy *phy)
871 sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
872 sysfs_remove_link(&phy->dev.kobj, "port");
875 /** sas_port_alloc - allocate and initialize a SAS port structure
877 * @parent: parent device
878 * @port_id: port number
880 * Allocates a SAS port structure. It will be added to the device tree
881 * below the device specified by @parent which must be either a Scsi_Host
882 * or a sas_expander_device.
884 * Returns %NULL on error
886 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
888 struct Scsi_Host *shost = dev_to_shost(parent);
889 struct sas_port *port;
891 port = kzalloc(sizeof(*port), GFP_KERNEL);
892 if (!port)
893 return NULL;
895 port->port_identifier = port_id;
897 device_initialize(&port->dev);
899 port->dev.parent = get_device(parent);
900 port->dev.release = sas_port_release;
902 mutex_init(&port->phy_list_mutex);
903 INIT_LIST_HEAD(&port->phy_list);
905 if (scsi_is_sas_expander_device(parent)) {
906 struct sas_rphy *rphy = dev_to_rphy(parent);
907 dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
908 rphy->scsi_target_id, port->port_identifier);
909 } else
910 dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
911 port->port_identifier);
913 transport_setup_device(&port->dev);
915 return port;
917 EXPORT_SYMBOL(sas_port_alloc);
919 /** sas_port_alloc_num - allocate and initialize a SAS port structure
921 * @parent: parent device
923 * Allocates a SAS port structure and a number to go with it. This
924 * interface is really for adapters where the port number has no
925 * meansing, so the sas class should manage them. It will be added to
926 * the device tree below the device specified by @parent which must be
927 * either a Scsi_Host or a sas_expander_device.
929 * Returns %NULL on error
931 struct sas_port *sas_port_alloc_num(struct device *parent)
933 int index;
934 struct Scsi_Host *shost = dev_to_shost(parent);
935 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
937 /* FIXME: use idr for this eventually */
938 mutex_lock(&sas_host->lock);
939 if (scsi_is_sas_expander_device(parent)) {
940 struct sas_rphy *rphy = dev_to_rphy(parent);
941 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
943 index = exp->next_port_id++;
944 } else
945 index = sas_host->next_port_id++;
946 mutex_unlock(&sas_host->lock);
947 return sas_port_alloc(parent, index);
949 EXPORT_SYMBOL(sas_port_alloc_num);
952 * sas_port_add - add a SAS port to the device hierarchy
953 * @port: port to be added
955 * publishes a port to the rest of the system
957 int sas_port_add(struct sas_port *port)
959 int error;
961 /* No phys should be added until this is made visible */
962 BUG_ON(!list_empty(&port->phy_list));
964 error = device_add(&port->dev);
966 if (error)
967 return error;
969 transport_add_device(&port->dev);
970 transport_configure_device(&port->dev);
972 return 0;
974 EXPORT_SYMBOL(sas_port_add);
977 * sas_port_free - free a SAS PORT
978 * @port: SAS PORT to free
980 * Frees the specified SAS PORT.
982 * Note:
983 * This function must only be called on a PORT that has not
984 * successfully been added using sas_port_add().
986 void sas_port_free(struct sas_port *port)
988 transport_destroy_device(&port->dev);
989 put_device(&port->dev);
991 EXPORT_SYMBOL(sas_port_free);
994 * sas_port_delete - remove SAS PORT
995 * @port: SAS PORT to remove
997 * Removes the specified SAS PORT. If the SAS PORT has an
998 * associated phys, unlink them from the port as well.
1000 void sas_port_delete(struct sas_port *port)
1002 struct device *dev = &port->dev;
1003 struct sas_phy *phy, *tmp_phy;
1005 if (port->rphy) {
1006 sas_rphy_delete(port->rphy);
1007 port->rphy = NULL;
1010 mutex_lock(&port->phy_list_mutex);
1011 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1012 port_siblings) {
1013 sas_port_delete_link(port, phy);
1014 list_del_init(&phy->port_siblings);
1016 mutex_unlock(&port->phy_list_mutex);
1018 if (port->is_backlink) {
1019 struct device *parent = port->dev.parent;
1021 sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1022 port->is_backlink = 0;
1025 transport_remove_device(dev);
1026 device_del(dev);
1027 transport_destroy_device(dev);
1028 put_device(dev);
1030 EXPORT_SYMBOL(sas_port_delete);
1033 * scsi_is_sas_port - check if a struct device represents a SAS port
1034 * @dev: device to check
1036 * Returns:
1037 * %1 if the device represents a SAS Port, %0 else
1039 int scsi_is_sas_port(const struct device *dev)
1041 return dev->release == sas_port_release;
1043 EXPORT_SYMBOL(scsi_is_sas_port);
1046 * sas_port_get_phy - try to take a reference on a port member
1047 * @port: port to check
1049 struct sas_phy *sas_port_get_phy(struct sas_port *port)
1051 struct sas_phy *phy;
1053 mutex_lock(&port->phy_list_mutex);
1054 if (list_empty(&port->phy_list))
1055 phy = NULL;
1056 else {
1057 struct list_head *ent = port->phy_list.next;
1059 phy = list_entry(ent, typeof(*phy), port_siblings);
1060 get_device(&phy->dev);
1062 mutex_unlock(&port->phy_list_mutex);
1064 return phy;
1066 EXPORT_SYMBOL(sas_port_get_phy);
1069 * sas_port_add_phy - add another phy to a port to form a wide port
1070 * @port: port to add the phy to
1071 * @phy: phy to add
1073 * When a port is initially created, it is empty (has no phys). All
1074 * ports must have at least one phy to operated, and all wide ports
1075 * must have at least two. The current code makes no difference
1076 * between ports and wide ports, but the only object that can be
1077 * connected to a remote device is a port, so ports must be formed on
1078 * all devices with phys if they're connected to anything.
1080 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1082 mutex_lock(&port->phy_list_mutex);
1083 if (unlikely(!list_empty(&phy->port_siblings))) {
1084 /* make sure we're already on this port */
1085 struct sas_phy *tmp;
1087 list_for_each_entry(tmp, &port->phy_list, port_siblings)
1088 if (tmp == phy)
1089 break;
1090 /* If this trips, you added a phy that was already
1091 * part of a different port */
1092 if (unlikely(tmp != phy)) {
1093 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1094 dev_name(&phy->dev));
1095 BUG();
1097 } else {
1098 sas_port_create_link(port, phy);
1099 list_add_tail(&phy->port_siblings, &port->phy_list);
1100 port->num_phys++;
1102 mutex_unlock(&port->phy_list_mutex);
1104 EXPORT_SYMBOL(sas_port_add_phy);
1107 * sas_port_delete_phy - remove a phy from a port or wide port
1108 * @port: port to remove the phy from
1109 * @phy: phy to remove
1111 * This operation is used for tearing down ports again. It must be
1112 * done to every port or wide port before calling sas_port_delete.
1114 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1116 mutex_lock(&port->phy_list_mutex);
1117 sas_port_delete_link(port, phy);
1118 list_del_init(&phy->port_siblings);
1119 port->num_phys--;
1120 mutex_unlock(&port->phy_list_mutex);
1122 EXPORT_SYMBOL(sas_port_delete_phy);
1124 void sas_port_mark_backlink(struct sas_port *port)
1126 int res;
1127 struct device *parent = port->dev.parent->parent->parent;
1129 if (port->is_backlink)
1130 return;
1131 port->is_backlink = 1;
1132 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1133 dev_name(parent));
1134 if (res)
1135 goto err;
1136 return;
1137 err:
1138 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1139 __func__, res);
1142 EXPORT_SYMBOL(sas_port_mark_backlink);
1145 * SAS remote PHY attributes.
1148 #define sas_rphy_show_simple(field, name, format_string, cast) \
1149 static ssize_t \
1150 show_sas_rphy_##name(struct device *dev, \
1151 struct device_attribute *attr, char *buf) \
1153 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1155 return snprintf(buf, 20, format_string, cast rphy->field); \
1158 #define sas_rphy_simple_attr(field, name, format_string, type) \
1159 sas_rphy_show_simple(field, name, format_string, (type)) \
1160 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1161 show_sas_rphy_##name, NULL)
1163 #define sas_rphy_show_protocol(field, name) \
1164 static ssize_t \
1165 show_sas_rphy_##name(struct device *dev, \
1166 struct device_attribute *attr, char *buf) \
1168 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1170 if (!rphy->field) \
1171 return snprintf(buf, 20, "none\n"); \
1172 return get_sas_protocol_names(rphy->field, buf); \
1175 #define sas_rphy_protocol_attr(field, name) \
1176 sas_rphy_show_protocol(field, name) \
1177 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1178 show_sas_rphy_##name, NULL)
1180 static ssize_t
1181 show_sas_rphy_device_type(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1184 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1186 if (!rphy->identify.device_type)
1187 return snprintf(buf, 20, "none\n");
1188 return get_sas_device_type_names(
1189 rphy->identify.device_type, buf);
1192 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1193 show_sas_rphy_device_type, NULL);
1195 static ssize_t
1196 show_sas_rphy_enclosure_identifier(struct device *dev,
1197 struct device_attribute *attr, char *buf)
1199 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1200 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1201 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1202 struct sas_internal *i = to_sas_internal(shost->transportt);
1203 u64 identifier;
1204 int error;
1206 error = i->f->get_enclosure_identifier(rphy, &identifier);
1207 if (error)
1208 return error;
1209 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1212 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1213 show_sas_rphy_enclosure_identifier, NULL);
1215 static ssize_t
1216 show_sas_rphy_bay_identifier(struct device *dev,
1217 struct device_attribute *attr, char *buf)
1219 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1220 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1221 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1222 struct sas_internal *i = to_sas_internal(shost->transportt);
1223 int val;
1225 val = i->f->get_bay_identifier(rphy);
1226 if (val < 0)
1227 return val;
1228 return sprintf(buf, "%d\n", val);
1231 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1232 show_sas_rphy_bay_identifier, NULL);
1234 sas_rphy_protocol_attr(identify.initiator_port_protocols,
1235 initiator_port_protocols);
1236 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1237 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1238 unsigned long long);
1239 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1240 sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
1242 /* only need 8 bytes of data plus header (4 or 8) */
1243 #define BUF_SIZE 64
1245 int sas_read_port_mode_page(struct scsi_device *sdev)
1247 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1248 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
1249 struct scsi_mode_data mode_data;
1250 int res, error;
1252 if (!buffer)
1253 return -ENOMEM;
1255 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1256 &mode_data, NULL);
1258 error = -EINVAL;
1259 if (!scsi_status_is_good(res))
1260 goto out;
1262 msdata = buffer + mode_data.header_length +
1263 mode_data.block_descriptor_length;
1265 if (msdata - buffer > BUF_SIZE - 8)
1266 goto out;
1268 error = 0;
1270 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1271 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1272 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1274 out:
1275 kfree(buffer);
1276 return error;
1278 EXPORT_SYMBOL(sas_read_port_mode_page);
1280 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1281 "sas_end_device", NULL, NULL, NULL);
1283 #define sas_end_dev_show_simple(field, name, format_string, cast) \
1284 static ssize_t \
1285 show_sas_end_dev_##name(struct device *dev, \
1286 struct device_attribute *attr, char *buf) \
1288 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1289 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1291 return snprintf(buf, 20, format_string, cast rdev->field); \
1294 #define sas_end_dev_simple_attr(field, name, format_string, type) \
1295 sas_end_dev_show_simple(field, name, format_string, (type)) \
1296 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1297 show_sas_end_dev_##name, NULL)
1299 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1300 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1301 "%d\n", int);
1302 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1303 "%d\n", int);
1304 sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1305 "%d\n", int);
1306 sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1307 "%d\n", int);
1309 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1310 "sas_expander", NULL, NULL, NULL);
1312 #define sas_expander_show_simple(field, name, format_string, cast) \
1313 static ssize_t \
1314 show_sas_expander_##name(struct device *dev, \
1315 struct device_attribute *attr, char *buf) \
1317 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1318 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1320 return snprintf(buf, 20, format_string, cast edev->field); \
1323 #define sas_expander_simple_attr(field, name, format_string, type) \
1324 sas_expander_show_simple(field, name, format_string, (type)) \
1325 static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
1326 show_sas_expander_##name, NULL)
1328 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1329 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1330 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1331 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1332 "%s\n", char *);
1333 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1334 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1335 unsigned int);
1336 sas_expander_simple_attr(level, level, "%d\n", int);
1338 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1339 "sas_device", NULL, NULL, NULL);
1341 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1343 struct Scsi_Host *shost;
1344 struct sas_internal *i;
1346 if (!scsi_is_sas_rphy(dev))
1347 return 0;
1348 shost = dev_to_shost(dev->parent->parent);
1350 if (!shost->transportt)
1351 return 0;
1352 if (shost->transportt->host_attrs.ac.class !=
1353 &sas_host_class.class)
1354 return 0;
1356 i = to_sas_internal(shost->transportt);
1357 return &i->rphy_attr_cont.ac == cont;
1360 static int sas_end_dev_match(struct attribute_container *cont,
1361 struct device *dev)
1363 struct Scsi_Host *shost;
1364 struct sas_internal *i;
1365 struct sas_rphy *rphy;
1367 if (!scsi_is_sas_rphy(dev))
1368 return 0;
1369 shost = dev_to_shost(dev->parent->parent);
1370 rphy = dev_to_rphy(dev);
1372 if (!shost->transportt)
1373 return 0;
1374 if (shost->transportt->host_attrs.ac.class !=
1375 &sas_host_class.class)
1376 return 0;
1378 i = to_sas_internal(shost->transportt);
1379 return &i->end_dev_attr_cont.ac == cont &&
1380 rphy->identify.device_type == SAS_END_DEVICE;
1383 static int sas_expander_match(struct attribute_container *cont,
1384 struct device *dev)
1386 struct Scsi_Host *shost;
1387 struct sas_internal *i;
1388 struct sas_rphy *rphy;
1390 if (!scsi_is_sas_rphy(dev))
1391 return 0;
1392 shost = dev_to_shost(dev->parent->parent);
1393 rphy = dev_to_rphy(dev);
1395 if (!shost->transportt)
1396 return 0;
1397 if (shost->transportt->host_attrs.ac.class !=
1398 &sas_host_class.class)
1399 return 0;
1401 i = to_sas_internal(shost->transportt);
1402 return &i->expander_attr_cont.ac == cont &&
1403 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1404 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1407 static void sas_expander_release(struct device *dev)
1409 struct sas_rphy *rphy = dev_to_rphy(dev);
1410 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1412 if (rphy->q)
1413 blk_cleanup_queue(rphy->q);
1415 put_device(dev->parent);
1416 kfree(edev);
1419 static void sas_end_device_release(struct device *dev)
1421 struct sas_rphy *rphy = dev_to_rphy(dev);
1422 struct sas_end_device *edev = rphy_to_end_device(rphy);
1424 if (rphy->q)
1425 blk_cleanup_queue(rphy->q);
1427 put_device(dev->parent);
1428 kfree(edev);
1432 * sas_rphy_initialize - common rphy initialization
1433 * @rphy: rphy to initialise
1435 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1436 * initialise the common rphy component of each.
1438 static void sas_rphy_initialize(struct sas_rphy *rphy)
1440 INIT_LIST_HEAD(&rphy->list);
1444 * sas_end_device_alloc - allocate an rphy for an end device
1445 * @parent: which port
1447 * Allocates an SAS remote PHY structure, connected to @parent.
1449 * Returns:
1450 * SAS PHY allocated or %NULL if the allocation failed.
1452 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1454 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1455 struct sas_end_device *rdev;
1457 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1458 if (!rdev) {
1459 return NULL;
1462 device_initialize(&rdev->rphy.dev);
1463 rdev->rphy.dev.parent = get_device(&parent->dev);
1464 rdev->rphy.dev.release = sas_end_device_release;
1465 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1466 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1467 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1468 shost->host_no, rphy->scsi_target_id,
1469 parent->port_identifier);
1470 } else
1471 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1472 shost->host_no, parent->port_identifier);
1473 rdev->rphy.identify.device_type = SAS_END_DEVICE;
1474 sas_rphy_initialize(&rdev->rphy);
1475 transport_setup_device(&rdev->rphy.dev);
1477 return &rdev->rphy;
1479 EXPORT_SYMBOL(sas_end_device_alloc);
1482 * sas_expander_alloc - allocate an rphy for an end device
1483 * @parent: which port
1484 * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
1486 * Allocates an SAS remote PHY structure, connected to @parent.
1488 * Returns:
1489 * SAS PHY allocated or %NULL if the allocation failed.
1491 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1492 enum sas_device_type type)
1494 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1495 struct sas_expander_device *rdev;
1496 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1498 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1499 type != SAS_FANOUT_EXPANDER_DEVICE);
1501 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1502 if (!rdev) {
1503 return NULL;
1506 device_initialize(&rdev->rphy.dev);
1507 rdev->rphy.dev.parent = get_device(&parent->dev);
1508 rdev->rphy.dev.release = sas_expander_release;
1509 mutex_lock(&sas_host->lock);
1510 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1511 mutex_unlock(&sas_host->lock);
1512 dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1513 shost->host_no, rdev->rphy.scsi_target_id);
1514 rdev->rphy.identify.device_type = type;
1515 sas_rphy_initialize(&rdev->rphy);
1516 transport_setup_device(&rdev->rphy.dev);
1518 return &rdev->rphy;
1520 EXPORT_SYMBOL(sas_expander_alloc);
1523 * sas_rphy_add - add a SAS remote PHY to the device hierarchy
1524 * @rphy: The remote PHY to be added
1526 * Publishes a SAS remote PHY to the rest of the system.
1528 int sas_rphy_add(struct sas_rphy *rphy)
1530 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1531 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1532 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1533 struct sas_identify *identify = &rphy->identify;
1534 int error;
1536 if (parent->rphy)
1537 return -ENXIO;
1538 parent->rphy = rphy;
1540 error = device_add(&rphy->dev);
1541 if (error)
1542 return error;
1543 transport_add_device(&rphy->dev);
1544 transport_configure_device(&rphy->dev);
1545 if (sas_bsg_initialize(shost, rphy))
1546 printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
1549 mutex_lock(&sas_host->lock);
1550 list_add_tail(&rphy->list, &sas_host->rphy_list);
1551 if (identify->device_type == SAS_END_DEVICE &&
1552 (identify->target_port_protocols &
1553 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1554 rphy->scsi_target_id = sas_host->next_target_id++;
1555 else if (identify->device_type == SAS_END_DEVICE)
1556 rphy->scsi_target_id = -1;
1557 mutex_unlock(&sas_host->lock);
1559 if (identify->device_type == SAS_END_DEVICE &&
1560 rphy->scsi_target_id != -1) {
1561 int lun;
1563 if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
1564 lun = SCAN_WILD_CARD;
1565 else
1566 lun = 0;
1568 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
1569 SCSI_SCAN_INITIAL);
1572 return 0;
1574 EXPORT_SYMBOL(sas_rphy_add);
1577 * sas_rphy_free - free a SAS remote PHY
1578 * @rphy: SAS remote PHY to free
1580 * Frees the specified SAS remote PHY.
1582 * Note:
1583 * This function must only be called on a remote
1584 * PHY that has not successfully been added using
1585 * sas_rphy_add() (or has been sas_rphy_remove()'d)
1587 void sas_rphy_free(struct sas_rphy *rphy)
1589 struct device *dev = &rphy->dev;
1590 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1591 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1593 mutex_lock(&sas_host->lock);
1594 list_del(&rphy->list);
1595 mutex_unlock(&sas_host->lock);
1597 transport_destroy_device(dev);
1599 put_device(dev);
1601 EXPORT_SYMBOL(sas_rphy_free);
1604 * sas_rphy_delete - remove and free SAS remote PHY
1605 * @rphy: SAS remote PHY to remove and free
1607 * Removes the specified SAS remote PHY and frees it.
1609 void
1610 sas_rphy_delete(struct sas_rphy *rphy)
1612 sas_rphy_remove(rphy);
1613 sas_rphy_free(rphy);
1615 EXPORT_SYMBOL(sas_rphy_delete);
1618 * sas_rphy_unlink - unlink SAS remote PHY
1619 * @rphy: SAS remote phy to unlink from its parent port
1621 * Removes port reference to an rphy
1623 void sas_rphy_unlink(struct sas_rphy *rphy)
1625 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1627 parent->rphy = NULL;
1629 EXPORT_SYMBOL(sas_rphy_unlink);
1632 * sas_rphy_remove - remove SAS remote PHY
1633 * @rphy: SAS remote phy to remove
1635 * Removes the specified SAS remote PHY.
1637 void
1638 sas_rphy_remove(struct sas_rphy *rphy)
1640 struct device *dev = &rphy->dev;
1642 switch (rphy->identify.device_type) {
1643 case SAS_END_DEVICE:
1644 scsi_remove_target(dev);
1645 break;
1646 case SAS_EDGE_EXPANDER_DEVICE:
1647 case SAS_FANOUT_EXPANDER_DEVICE:
1648 sas_remove_children(dev);
1649 break;
1650 default:
1651 break;
1654 sas_rphy_unlink(rphy);
1655 if (rphy->q)
1656 bsg_unregister_queue(rphy->q);
1657 transport_remove_device(dev);
1658 device_del(dev);
1660 EXPORT_SYMBOL(sas_rphy_remove);
1663 * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
1664 * @dev: device to check
1666 * Returns:
1667 * %1 if the device represents a SAS remote PHY, %0 else
1669 int scsi_is_sas_rphy(const struct device *dev)
1671 return dev->release == sas_end_device_release ||
1672 dev->release == sas_expander_release;
1674 EXPORT_SYMBOL(scsi_is_sas_rphy);
1678 * SCSI scan helper
1681 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1682 uint id, u64 lun)
1684 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1685 struct sas_rphy *rphy;
1687 mutex_lock(&sas_host->lock);
1688 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1689 if (rphy->identify.device_type != SAS_END_DEVICE ||
1690 rphy->scsi_target_id == -1)
1691 continue;
1693 if ((channel == SCAN_WILD_CARD || channel == 0) &&
1694 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1695 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
1696 lun, SCSI_SCAN_MANUAL);
1699 mutex_unlock(&sas_host->lock);
1701 return 0;
1706 * Setup / Teardown code
1709 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1710 i->private_##attrb[count] = dev_attr_##field; \
1711 i->private_##attrb[count].attr.mode = perm; \
1712 i->attrb[count] = &i->private_##attrb[count]; \
1713 if (test) \
1714 count++
1716 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1717 i->private_##attrb[count] = dev_attr_##field; \
1718 i->private_##attrb[count].attr.mode = perm; \
1719 if (ro_test) { \
1720 i->private_##attrb[count].attr.mode = ro_perm; \
1721 i->private_##attrb[count].store = NULL; \
1723 i->attrb[count] = &i->private_##attrb[count]; \
1724 if (test) \
1725 count++
1727 #define SETUP_RPORT_ATTRIBUTE(field) \
1728 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1730 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1731 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1733 #define SETUP_PHY_ATTRIBUTE(field) \
1734 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1736 #define SETUP_PHY_ATTRIBUTE_RW(field) \
1737 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1738 !i->f->set_phy_speed, S_IRUGO)
1740 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1741 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1742 !i->f->func, S_IRUGO)
1744 #define SETUP_PORT_ATTRIBUTE(field) \
1745 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1747 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1748 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1750 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1751 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1753 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1754 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1756 #define SETUP_END_DEV_ATTRIBUTE(field) \
1757 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1759 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1760 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1763 * sas_attach_transport - instantiate SAS transport template
1764 * @ft: SAS transport class function template
1766 struct scsi_transport_template *
1767 sas_attach_transport(struct sas_function_template *ft)
1769 struct sas_internal *i;
1770 int count;
1772 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1773 if (!i)
1774 return NULL;
1776 i->t.user_scan = sas_user_scan;
1778 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1779 i->t.host_attrs.ac.class = &sas_host_class.class;
1780 i->t.host_attrs.ac.match = sas_host_match;
1781 transport_container_register(&i->t.host_attrs);
1782 i->t.host_size = sizeof(struct sas_host_attrs);
1784 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1785 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1786 i->phy_attr_cont.ac.match = sas_phy_match;
1787 transport_container_register(&i->phy_attr_cont);
1789 i->port_attr_cont.ac.class = &sas_port_class.class;
1790 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1791 i->port_attr_cont.ac.match = sas_port_match;
1792 transport_container_register(&i->port_attr_cont);
1794 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1795 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1796 i->rphy_attr_cont.ac.match = sas_rphy_match;
1797 transport_container_register(&i->rphy_attr_cont);
1799 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1800 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1801 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1802 transport_container_register(&i->end_dev_attr_cont);
1804 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1805 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1806 i->expander_attr_cont.ac.match = sas_expander_match;
1807 transport_container_register(&i->expander_attr_cont);
1809 i->f = ft;
1811 count = 0;
1812 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1813 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1814 SETUP_PHY_ATTRIBUTE(device_type);
1815 SETUP_PHY_ATTRIBUTE(sas_address);
1816 SETUP_PHY_ATTRIBUTE(phy_identifier);
1817 //SETUP_PHY_ATTRIBUTE(port_identifier);
1818 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1819 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1820 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
1821 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1822 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1824 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1825 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1826 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1827 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1828 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1829 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1830 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1831 i->phy_attrs[count] = NULL;
1833 count = 0;
1834 SETUP_PORT_ATTRIBUTE(num_phys);
1835 i->port_attrs[count] = NULL;
1837 count = 0;
1838 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1839 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1840 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1841 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1842 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1843 SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
1844 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1845 get_enclosure_identifier);
1846 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1847 get_bay_identifier);
1848 i->rphy_attrs[count] = NULL;
1850 count = 0;
1851 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1852 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1853 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1854 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1855 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
1856 i->end_dev_attrs[count] = NULL;
1858 count = 0;
1859 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1860 SETUP_EXPANDER_ATTRIBUTE(product_id);
1861 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1862 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1863 SETUP_EXPANDER_ATTRIBUTE(component_id);
1864 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1865 SETUP_EXPANDER_ATTRIBUTE(level);
1866 i->expander_attrs[count] = NULL;
1868 return &i->t;
1870 EXPORT_SYMBOL(sas_attach_transport);
1873 * sas_release_transport - release SAS transport template instance
1874 * @t: transport template instance
1876 void sas_release_transport(struct scsi_transport_template *t)
1878 struct sas_internal *i = to_sas_internal(t);
1880 transport_container_unregister(&i->t.host_attrs);
1881 transport_container_unregister(&i->phy_attr_cont);
1882 transport_container_unregister(&i->port_attr_cont);
1883 transport_container_unregister(&i->rphy_attr_cont);
1884 transport_container_unregister(&i->end_dev_attr_cont);
1885 transport_container_unregister(&i->expander_attr_cont);
1887 kfree(i);
1889 EXPORT_SYMBOL(sas_release_transport);
1891 static __init int sas_transport_init(void)
1893 int error;
1895 error = transport_class_register(&sas_host_class);
1896 if (error)
1897 goto out;
1898 error = transport_class_register(&sas_phy_class);
1899 if (error)
1900 goto out_unregister_transport;
1901 error = transport_class_register(&sas_port_class);
1902 if (error)
1903 goto out_unregister_phy;
1904 error = transport_class_register(&sas_rphy_class);
1905 if (error)
1906 goto out_unregister_port;
1907 error = transport_class_register(&sas_end_dev_class);
1908 if (error)
1909 goto out_unregister_rphy;
1910 error = transport_class_register(&sas_expander_class);
1911 if (error)
1912 goto out_unregister_end_dev;
1914 return 0;
1916 out_unregister_end_dev:
1917 transport_class_unregister(&sas_end_dev_class);
1918 out_unregister_rphy:
1919 transport_class_unregister(&sas_rphy_class);
1920 out_unregister_port:
1921 transport_class_unregister(&sas_port_class);
1922 out_unregister_phy:
1923 transport_class_unregister(&sas_phy_class);
1924 out_unregister_transport:
1925 transport_class_unregister(&sas_host_class);
1926 out:
1927 return error;
1931 static void __exit sas_transport_exit(void)
1933 transport_class_unregister(&sas_host_class);
1934 transport_class_unregister(&sas_phy_class);
1935 transport_class_unregister(&sas_port_class);
1936 transport_class_unregister(&sas_rphy_class);
1937 transport_class_unregister(&sas_end_dev_class);
1938 transport_class_unregister(&sas_expander_class);
1941 MODULE_AUTHOR("Christoph Hellwig");
1942 MODULE_DESCRIPTION("SAS Transport Attributes");
1943 MODULE_LICENSE("GPL");
1945 module_init(sas_transport_init);
1946 module_exit(sas_transport_exit);