[PATCH] convert aic94xx over to using the sas transport end device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_transport_sas.c
blob5de29b941c6b2bdc7d17d24a87ea6d264622718c
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 managment
10 * interfaces to userspace.
12 * In addition to the basic SCSI core objects this transport class
13 * introduces two additional intermediate objects: The SAS PHY
14 * as represented by struct sas_phy defines an "outgoing" PHY on
15 * a SAS HBA or Expander, and the SAS remote PHY represented by
16 * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17 * end device. Note that this is purely a software concept, the
18 * underlying hardware for a PHY and a remote PHY is the exactly
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/err.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
39 #define SAS_HOST_ATTRS 0
40 #define SAS_PORT_ATTRS 17
41 #define SAS_RPORT_ATTRS 7
42 #define SAS_END_DEV_ATTRS 3
44 struct sas_internal {
45 struct scsi_transport_template t;
46 struct sas_function_template *f;
48 struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
49 struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
50 struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
51 struct class_device_attribute private_end_dev_attrs[SAS_END_DEV_ATTRS];
53 struct transport_container phy_attr_cont;
54 struct transport_container rphy_attr_cont;
55 struct transport_container end_dev_attr_cont;
58 * The array of null terminated pointers to attributes
59 * needed by scsi_sysfs.c
61 struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
62 struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
63 struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
64 struct class_device_attribute *end_dev_attrs[SAS_END_DEV_ATTRS + 1];
66 #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
68 struct sas_host_attrs {
69 struct list_head rphy_list;
70 struct mutex lock;
71 u32 next_target_id;
73 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
77 * Hack to allow attributes of the same name in different objects.
79 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
80 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
81 __ATTR(_name,_mode,_show,_store)
85 * Pretty printing helpers
88 #define sas_bitfield_name_match(title, table) \
89 static ssize_t \
90 get_sas_##title##_names(u32 table_key, char *buf) \
91 { \
92 char *prefix = ""; \
93 ssize_t len = 0; \
94 int i; \
96 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
97 if (table[i].value & table_key) { \
98 len += sprintf(buf + len, "%s%s", \
99 prefix, table[i].name); \
100 prefix = ", "; \
103 len += sprintf(buf + len, "\n"); \
104 return len; \
107 #define sas_bitfield_name_search(title, table) \
108 static ssize_t \
109 get_sas_##title##_names(u32 table_key, char *buf) \
111 ssize_t len = 0; \
112 int i; \
114 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
115 if (table[i].value == table_key) { \
116 len += sprintf(buf + len, "%s", \
117 table[i].name); \
118 break; \
121 len += sprintf(buf + len, "\n"); \
122 return len; \
125 static struct {
126 u32 value;
127 char *name;
128 } sas_device_type_names[] = {
129 { SAS_PHY_UNUSED, "unused" },
130 { SAS_END_DEVICE, "end device" },
131 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
132 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
134 sas_bitfield_name_search(device_type, sas_device_type_names)
137 static struct {
138 u32 value;
139 char *name;
140 } sas_protocol_names[] = {
141 { SAS_PROTOCOL_SATA, "sata" },
142 { SAS_PROTOCOL_SMP, "smp" },
143 { SAS_PROTOCOL_STP, "stp" },
144 { SAS_PROTOCOL_SSP, "ssp" },
146 sas_bitfield_name_match(protocol, sas_protocol_names)
148 static struct {
149 u32 value;
150 char *name;
151 } sas_linkspeed_names[] = {
152 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
153 { SAS_PHY_DISABLED, "Phy disabled" },
154 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
155 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
156 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
157 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
158 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
160 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
164 * SAS host attributes
167 static int sas_host_setup(struct transport_container *tc, struct device *dev,
168 struct class_device *cdev)
170 struct Scsi_Host *shost = dev_to_shost(dev);
171 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
173 INIT_LIST_HEAD(&sas_host->rphy_list);
174 mutex_init(&sas_host->lock);
175 sas_host->next_target_id = 0;
176 return 0;
179 static DECLARE_TRANSPORT_CLASS(sas_host_class,
180 "sas_host", sas_host_setup, NULL, NULL);
182 static int sas_host_match(struct attribute_container *cont,
183 struct device *dev)
185 struct Scsi_Host *shost;
186 struct sas_internal *i;
188 if (!scsi_is_host_device(dev))
189 return 0;
190 shost = dev_to_shost(dev);
192 if (!shost->transportt)
193 return 0;
194 if (shost->transportt->host_attrs.ac.class !=
195 &sas_host_class.class)
196 return 0;
198 i = to_sas_internal(shost->transportt);
199 return &i->t.host_attrs.ac == cont;
202 static int do_sas_phy_delete(struct device *dev, void *data)
204 if (scsi_is_sas_phy(dev))
205 sas_phy_delete(dev_to_phy(dev));
206 return 0;
210 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
211 * @shost: Scsi Host that is torn down
213 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
214 * Must be called just before scsi_remove_host for SAS HBAs.
216 void sas_remove_host(struct Scsi_Host *shost)
218 device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
220 EXPORT_SYMBOL(sas_remove_host);
224 * SAS Port attributes
227 #define sas_phy_show_simple(field, name, format_string, cast) \
228 static ssize_t \
229 show_sas_phy_##name(struct class_device *cdev, char *buf) \
231 struct sas_phy *phy = transport_class_to_phy(cdev); \
233 return snprintf(buf, 20, format_string, cast phy->field); \
236 #define sas_phy_simple_attr(field, name, format_string, type) \
237 sas_phy_show_simple(field, name, format_string, (type)) \
238 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
240 #define sas_phy_show_protocol(field, name) \
241 static ssize_t \
242 show_sas_phy_##name(struct class_device *cdev, char *buf) \
244 struct sas_phy *phy = transport_class_to_phy(cdev); \
246 if (!phy->field) \
247 return snprintf(buf, 20, "none\n"); \
248 return get_sas_protocol_names(phy->field, buf); \
251 #define sas_phy_protocol_attr(field, name) \
252 sas_phy_show_protocol(field, name) \
253 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
255 #define sas_phy_show_linkspeed(field) \
256 static ssize_t \
257 show_sas_phy_##field(struct class_device *cdev, char *buf) \
259 struct sas_phy *phy = transport_class_to_phy(cdev); \
261 return get_sas_linkspeed_names(phy->field, buf); \
264 #define sas_phy_linkspeed_attr(field) \
265 sas_phy_show_linkspeed(field) \
266 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
268 #define sas_phy_show_linkerror(field) \
269 static ssize_t \
270 show_sas_phy_##field(struct class_device *cdev, char *buf) \
272 struct sas_phy *phy = transport_class_to_phy(cdev); \
273 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
274 struct sas_internal *i = to_sas_internal(shost->transportt); \
275 int error; \
277 if (!phy->local_attached) \
278 return -EINVAL; \
280 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
281 if (error) \
282 return error; \
283 return snprintf(buf, 20, "%u\n", phy->field); \
286 #define sas_phy_linkerror_attr(field) \
287 sas_phy_show_linkerror(field) \
288 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
291 static ssize_t
292 show_sas_device_type(struct class_device *cdev, char *buf)
294 struct sas_phy *phy = transport_class_to_phy(cdev);
296 if (!phy->identify.device_type)
297 return snprintf(buf, 20, "none\n");
298 return get_sas_device_type_names(phy->identify.device_type, buf);
300 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
302 static ssize_t do_sas_phy_reset(struct class_device *cdev,
303 size_t count, int hard_reset)
305 struct sas_phy *phy = transport_class_to_phy(cdev);
306 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
307 struct sas_internal *i = to_sas_internal(shost->transportt);
308 int error;
310 if (!phy->local_attached)
311 return -EINVAL;
313 error = i->f->phy_reset(phy, hard_reset);
314 if (error)
315 return error;
316 return count;
319 static ssize_t store_sas_link_reset(struct class_device *cdev,
320 const char *buf, size_t count)
322 return do_sas_phy_reset(cdev, count, 0);
324 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
326 static ssize_t store_sas_hard_reset(struct class_device *cdev,
327 const char *buf, size_t count)
329 return do_sas_phy_reset(cdev, count, 1);
331 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
333 sas_phy_protocol_attr(identify.initiator_port_protocols,
334 initiator_port_protocols);
335 sas_phy_protocol_attr(identify.target_port_protocols,
336 target_port_protocols);
337 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
338 unsigned long long);
339 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
340 sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
341 sas_phy_linkspeed_attr(negotiated_linkrate);
342 sas_phy_linkspeed_attr(minimum_linkrate_hw);
343 sas_phy_linkspeed_attr(minimum_linkrate);
344 sas_phy_linkspeed_attr(maximum_linkrate_hw);
345 sas_phy_linkspeed_attr(maximum_linkrate);
346 sas_phy_linkerror_attr(invalid_dword_count);
347 sas_phy_linkerror_attr(running_disparity_error_count);
348 sas_phy_linkerror_attr(loss_of_dword_sync_count);
349 sas_phy_linkerror_attr(phy_reset_problem_count);
352 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
353 "sas_phy", NULL, NULL, NULL);
355 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
357 struct Scsi_Host *shost;
358 struct sas_internal *i;
360 if (!scsi_is_sas_phy(dev))
361 return 0;
362 shost = dev_to_shost(dev->parent);
364 if (!shost->transportt)
365 return 0;
366 if (shost->transportt->host_attrs.ac.class !=
367 &sas_host_class.class)
368 return 0;
370 i = to_sas_internal(shost->transportt);
371 return &i->phy_attr_cont.ac == cont;
374 static void sas_phy_release(struct device *dev)
376 struct sas_phy *phy = dev_to_phy(dev);
378 put_device(dev->parent);
379 kfree(phy);
383 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
384 * @parent: Parent device
385 * @number: Phy index
387 * Allocates an SAS PHY structure. It will be added in the device tree
388 * below the device specified by @parent, which has to be either a Scsi_Host
389 * or sas_rphy.
391 * Returns:
392 * SAS PHY allocated or %NULL if the allocation failed.
394 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
396 struct Scsi_Host *shost = dev_to_shost(parent);
397 struct sas_phy *phy;
399 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
400 if (!phy)
401 return NULL;
403 get_device(parent);
405 phy->number = number;
407 device_initialize(&phy->dev);
408 phy->dev.parent = get_device(parent);
409 phy->dev.release = sas_phy_release;
410 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
412 transport_setup_device(&phy->dev);
414 return phy;
416 EXPORT_SYMBOL(sas_phy_alloc);
419 * sas_phy_add -- add a SAS PHY to the device hierachy
420 * @phy: The PHY to be added
422 * Publishes a SAS PHY to the rest of the system.
424 int sas_phy_add(struct sas_phy *phy)
426 int error;
428 error = device_add(&phy->dev);
429 if (!error) {
430 transport_add_device(&phy->dev);
431 transport_configure_device(&phy->dev);
434 return error;
436 EXPORT_SYMBOL(sas_phy_add);
439 * sas_phy_free -- free a SAS PHY
440 * @phy: SAS PHY to free
442 * Frees the specified SAS PHY.
444 * Note:
445 * This function must only be called on a PHY that has not
446 * sucessfully been added using sas_phy_add().
448 void sas_phy_free(struct sas_phy *phy)
450 transport_destroy_device(&phy->dev);
451 put_device(phy->dev.parent);
452 put_device(phy->dev.parent);
453 put_device(phy->dev.parent);
454 kfree(phy);
456 EXPORT_SYMBOL(sas_phy_free);
459 * sas_phy_delete -- remove SAS PHY
460 * @phy: SAS PHY to remove
462 * Removes the specified SAS PHY. If the SAS PHY has an
463 * associated remote PHY it is removed before.
465 void
466 sas_phy_delete(struct sas_phy *phy)
468 struct device *dev = &phy->dev;
470 if (phy->rphy)
471 sas_rphy_delete(phy->rphy);
473 transport_remove_device(dev);
474 device_del(dev);
475 transport_destroy_device(dev);
476 put_device(dev->parent);
478 EXPORT_SYMBOL(sas_phy_delete);
481 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
482 * @dev: device to check
484 * Returns:
485 * %1 if the device represents a SAS PHY, %0 else
487 int scsi_is_sas_phy(const struct device *dev)
489 return dev->release == sas_phy_release;
491 EXPORT_SYMBOL(scsi_is_sas_phy);
494 * SAS remote PHY attributes.
497 #define sas_rphy_show_simple(field, name, format_string, cast) \
498 static ssize_t \
499 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
501 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
503 return snprintf(buf, 20, format_string, cast rphy->field); \
506 #define sas_rphy_simple_attr(field, name, format_string, type) \
507 sas_rphy_show_simple(field, name, format_string, (type)) \
508 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
509 show_sas_rphy_##name, NULL)
511 #define sas_rphy_show_protocol(field, name) \
512 static ssize_t \
513 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
515 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
517 if (!rphy->field) \
518 return snprintf(buf, 20, "none\n"); \
519 return get_sas_protocol_names(rphy->field, buf); \
522 #define sas_rphy_protocol_attr(field, name) \
523 sas_rphy_show_protocol(field, name) \
524 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
525 show_sas_rphy_##name, NULL)
527 static ssize_t
528 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
530 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
532 if (!rphy->identify.device_type)
533 return snprintf(buf, 20, "none\n");
534 return get_sas_device_type_names(
535 rphy->identify.device_type, buf);
538 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
539 show_sas_rphy_device_type, NULL);
541 static ssize_t
542 show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
544 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
545 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
546 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
547 struct sas_internal *i = to_sas_internal(shost->transportt);
548 u64 identifier;
549 int error;
552 * Only devices behind an expander are supported, because the
553 * enclosure identifier is a SMP feature.
555 if (phy->local_attached)
556 return -EINVAL;
558 error = i->f->get_enclosure_identifier(rphy, &identifier);
559 if (error)
560 return error;
561 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
564 static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
565 show_sas_rphy_enclosure_identifier, NULL);
567 static ssize_t
568 show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
570 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
571 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
572 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
573 struct sas_internal *i = to_sas_internal(shost->transportt);
574 int val;
576 if (phy->local_attached)
577 return -EINVAL;
579 val = i->f->get_bay_identifier(rphy);
580 if (val < 0)
581 return val;
582 return sprintf(buf, "%d\n", val);
585 static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
586 show_sas_rphy_bay_identifier, NULL);
588 sas_rphy_protocol_attr(identify.initiator_port_protocols,
589 initiator_port_protocols);
590 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
591 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
592 unsigned long long);
593 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
595 /* only need 8 bytes of data plus header (4 or 8) */
596 #define BUF_SIZE 64
598 int sas_read_port_mode_page(struct scsi_device *sdev)
600 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
601 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
602 struct sas_end_device *rdev;
603 struct scsi_mode_data mode_data;
604 int res, error;
606 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
608 rdev = rphy_to_end_device(rphy);
610 if (!buffer)
611 return -ENOMEM;
613 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
614 &mode_data, NULL);
616 error = -EINVAL;
617 if (!scsi_status_is_good(res))
618 goto out;
620 msdata = buffer + mode_data.header_length +
621 mode_data.block_descriptor_length;
623 if (msdata - buffer > BUF_SIZE - 8)
624 goto out;
626 error = 0;
628 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
629 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
630 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
632 out:
633 kfree(buffer);
634 return error;
636 EXPORT_SYMBOL(sas_read_port_mode_page);
638 #define sas_end_dev_show_simple(field, name, format_string, cast) \
639 static ssize_t \
640 show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
642 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
643 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
645 return snprintf(buf, 20, format_string, cast rdev->field); \
648 #define sas_end_dev_simple_attr(field, name, format_string, type) \
649 sas_end_dev_show_simple(field, name, format_string, (type)) \
650 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
651 show_sas_end_dev_##name, NULL)
653 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
654 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
655 "%d\n", int);
656 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
657 "%d\n", int);
659 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
660 "sas_end_device", NULL, NULL, NULL);
662 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
663 "sas_rphy", NULL, NULL, NULL);
665 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
667 struct Scsi_Host *shost;
668 struct sas_internal *i;
670 if (!scsi_is_sas_rphy(dev))
671 return 0;
672 shost = dev_to_shost(dev->parent->parent);
674 if (!shost->transportt)
675 return 0;
676 if (shost->transportt->host_attrs.ac.class !=
677 &sas_host_class.class)
678 return 0;
680 i = to_sas_internal(shost->transportt);
681 return &i->rphy_attr_cont.ac == cont;
684 static int sas_end_dev_match(struct attribute_container *cont,
685 struct device *dev)
687 struct Scsi_Host *shost;
688 struct sas_internal *i;
689 struct sas_rphy *rphy;
691 if (!scsi_is_sas_rphy(dev))
692 return 0;
693 shost = dev_to_shost(dev->parent->parent);
694 rphy = dev_to_rphy(dev);
696 if (!shost->transportt)
697 return 0;
698 if (shost->transportt->host_attrs.ac.class !=
699 &sas_host_class.class)
700 return 0;
702 i = to_sas_internal(shost->transportt);
703 return &i->end_dev_attr_cont.ac == cont &&
704 rphy->identify.device_type == SAS_END_DEVICE &&
705 /* FIXME: remove contained eventually */
706 rphy->contained;
709 static void sas_rphy_release(struct device *dev)
711 struct sas_rphy *rphy = dev_to_rphy(dev);
713 put_device(dev->parent);
714 kfree(rphy);
718 * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
719 * @parent: SAS PHY this remote PHY is conneted to
721 * Allocates an SAS remote PHY structure, connected to @parent.
723 * Returns:
724 * SAS PHY allocated or %NULL if the allocation failed.
726 struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
728 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
729 struct sas_rphy *rphy;
731 rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
732 if (!rphy) {
733 put_device(&parent->dev);
734 return NULL;
737 device_initialize(&rphy->dev);
738 rphy->dev.parent = get_device(&parent->dev);
739 rphy->dev.release = sas_rphy_release;
740 sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
741 shost->host_no, parent->port_identifier, parent->number);
742 transport_setup_device(&rphy->dev);
744 return rphy;
746 EXPORT_SYMBOL(sas_rphy_alloc);
749 * sas_end_device_alloc - allocate an rphy for an end device
751 * Allocates an SAS remote PHY structure, connected to @parent.
753 * Returns:
754 * SAS PHY allocated or %NULL if the allocation failed.
756 struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
758 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
759 struct sas_end_device *rdev;
761 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
762 if (!rdev) {
763 put_device(&parent->dev);
764 return NULL;
767 device_initialize(&rdev->rphy.dev);
768 rdev->rphy.dev.parent = get_device(&parent->dev);
769 rdev->rphy.dev.release = sas_rphy_release;
770 sprintf(rdev->rphy.dev.bus_id, "rphy-%d:%d-%d",
771 shost->host_no, parent->port_identifier, parent->number);
772 rdev->rphy.identify.device_type = SAS_END_DEVICE;
773 /* FIXME: mark the rphy as being contained in a larger structure */
774 rdev->rphy.contained = 1;
775 transport_setup_device(&rdev->rphy.dev);
777 return &rdev->rphy;
779 EXPORT_SYMBOL(sas_end_device_alloc);
783 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
784 * @rphy: The remote PHY to be added
786 * Publishes a SAS remote PHY to the rest of the system.
788 int sas_rphy_add(struct sas_rphy *rphy)
790 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
791 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
792 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
793 struct sas_identify *identify = &rphy->identify;
794 int error;
796 if (parent->rphy)
797 return -ENXIO;
798 parent->rphy = rphy;
800 error = device_add(&rphy->dev);
801 if (error)
802 return error;
803 transport_add_device(&rphy->dev);
804 transport_configure_device(&rphy->dev);
806 mutex_lock(&sas_host->lock);
807 list_add_tail(&rphy->list, &sas_host->rphy_list);
808 if (identify->device_type == SAS_END_DEVICE &&
809 (identify->target_port_protocols &
810 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
811 rphy->scsi_target_id = sas_host->next_target_id++;
812 else
813 rphy->scsi_target_id = -1;
814 mutex_unlock(&sas_host->lock);
816 if (rphy->scsi_target_id != -1) {
817 scsi_scan_target(&rphy->dev, parent->port_identifier,
818 rphy->scsi_target_id, ~0, 0);
821 return 0;
823 EXPORT_SYMBOL(sas_rphy_add);
826 * sas_rphy_free -- free a SAS remote PHY
827 * @rphy SAS remote PHY to free
829 * Frees the specified SAS remote PHY.
831 * Note:
832 * This function must only be called on a remote
833 * PHY that has not sucessfully been added using
834 * sas_rphy_add().
836 void sas_rphy_free(struct sas_rphy *rphy)
838 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
839 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
841 mutex_lock(&sas_host->lock);
842 list_del(&rphy->list);
843 mutex_unlock(&sas_host->lock);
845 transport_destroy_device(&rphy->dev);
846 put_device(rphy->dev.parent);
847 put_device(rphy->dev.parent);
848 put_device(rphy->dev.parent);
849 kfree(rphy);
851 EXPORT_SYMBOL(sas_rphy_free);
854 * sas_rphy_delete -- remove SAS remote PHY
855 * @rphy: SAS remote PHY to remove
857 * Removes the specified SAS remote PHY.
859 void
860 sas_rphy_delete(struct sas_rphy *rphy)
862 struct device *dev = &rphy->dev;
863 struct sas_phy *parent = dev_to_phy(dev->parent);
864 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
865 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
867 switch (rphy->identify.device_type) {
868 case SAS_END_DEVICE:
869 scsi_remove_target(dev);
870 break;
871 case SAS_EDGE_EXPANDER_DEVICE:
872 case SAS_FANOUT_EXPANDER_DEVICE:
873 device_for_each_child(dev, NULL, do_sas_phy_delete);
874 break;
875 default:
876 break;
879 transport_remove_device(dev);
880 device_del(dev);
881 transport_destroy_device(dev);
883 mutex_lock(&sas_host->lock);
884 list_del(&rphy->list);
885 mutex_unlock(&sas_host->lock);
887 parent->rphy = NULL;
889 put_device(&parent->dev);
891 EXPORT_SYMBOL(sas_rphy_delete);
894 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
895 * @dev: device to check
897 * Returns:
898 * %1 if the device represents a SAS remote PHY, %0 else
900 int scsi_is_sas_rphy(const struct device *dev)
902 return dev->release == sas_rphy_release;
904 EXPORT_SYMBOL(scsi_is_sas_rphy);
908 * SCSI scan helper
911 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
912 uint id, uint lun)
914 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
915 struct sas_rphy *rphy;
917 mutex_lock(&sas_host->lock);
918 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
919 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
921 if (rphy->scsi_target_id == -1)
922 continue;
924 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
925 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
926 scsi_scan_target(&rphy->dev, parent->port_identifier,
927 rphy->scsi_target_id, lun, 1);
930 mutex_unlock(&sas_host->lock);
932 return 0;
937 * Setup / Teardown code
940 #define SETUP_TEMPLATE(attrb, field, perm, test) \
941 i->private_##attrb[count] = class_device_attr_##field; \
942 i->private_##attrb[count].attr.mode = perm; \
943 i->private_##attrb[count].store = NULL; \
944 i->attrb[count] = &i->private_##attrb[count]; \
945 if (test) \
946 count++
949 #define SETUP_RPORT_ATTRIBUTE(field) \
950 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
952 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
953 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
955 #define SETUP_PORT_ATTRIBUTE(field) \
956 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
958 #define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
959 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
961 #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
962 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
964 #define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
965 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
967 #define SETUP_END_DEV_ATTRIBUTE(field) \
968 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
971 * sas_attach_transport -- instantiate SAS transport template
972 * @ft: SAS transport class function template
974 struct scsi_transport_template *
975 sas_attach_transport(struct sas_function_template *ft)
977 struct sas_internal *i;
978 int count;
980 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
981 if (!i)
982 return NULL;
984 i->t.user_scan = sas_user_scan;
986 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
987 i->t.host_attrs.ac.class = &sas_host_class.class;
988 i->t.host_attrs.ac.match = sas_host_match;
989 transport_container_register(&i->t.host_attrs);
990 i->t.host_size = sizeof(struct sas_host_attrs);
992 i->phy_attr_cont.ac.class = &sas_phy_class.class;
993 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
994 i->phy_attr_cont.ac.match = sas_phy_match;
995 transport_container_register(&i->phy_attr_cont);
997 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
998 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
999 i->rphy_attr_cont.ac.match = sas_rphy_match;
1000 transport_container_register(&i->rphy_attr_cont);
1002 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1003 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1004 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1005 transport_container_register(&i->end_dev_attr_cont);
1007 i->f = ft;
1009 count = 0;
1010 i->host_attrs[count] = NULL;
1012 count = 0;
1013 SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
1014 SETUP_PORT_ATTRIBUTE(target_port_protocols);
1015 SETUP_PORT_ATTRIBUTE(device_type);
1016 SETUP_PORT_ATTRIBUTE(sas_address);
1017 SETUP_PORT_ATTRIBUTE(phy_identifier);
1018 SETUP_PORT_ATTRIBUTE(port_identifier);
1019 SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
1020 SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
1021 SETUP_PORT_ATTRIBUTE(minimum_linkrate);
1022 SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
1023 SETUP_PORT_ATTRIBUTE(maximum_linkrate);
1025 SETUP_PORT_ATTRIBUTE(invalid_dword_count);
1026 SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
1027 SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
1028 SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
1029 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1030 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1031 i->phy_attrs[count] = NULL;
1033 count = 0;
1034 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1035 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1036 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1037 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1038 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1039 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1040 get_enclosure_identifier);
1041 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1042 get_bay_identifier);
1043 i->rphy_attrs[count] = NULL;
1045 count = 0;
1046 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1047 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1048 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1049 i->end_dev_attrs[count] = NULL;
1051 return &i->t;
1053 EXPORT_SYMBOL(sas_attach_transport);
1056 * sas_release_transport -- release SAS transport template instance
1057 * @t: transport template instance
1059 void sas_release_transport(struct scsi_transport_template *t)
1061 struct sas_internal *i = to_sas_internal(t);
1063 transport_container_unregister(&i->t.host_attrs);
1064 transport_container_unregister(&i->phy_attr_cont);
1065 transport_container_unregister(&i->rphy_attr_cont);
1067 kfree(i);
1069 EXPORT_SYMBOL(sas_release_transport);
1071 static __init int sas_transport_init(void)
1073 int error;
1075 error = transport_class_register(&sas_host_class);
1076 if (error)
1077 goto out;
1078 error = transport_class_register(&sas_phy_class);
1079 if (error)
1080 goto out_unregister_transport;
1081 error = transport_class_register(&sas_rphy_class);
1082 if (error)
1083 goto out_unregister_phy;
1084 error = transport_class_register(&sas_end_dev_class);
1085 if (error)
1086 goto out_unregister_rphy;
1088 return 0;
1090 out_unregister_rphy:
1091 transport_class_unregister(&sas_rphy_class);
1092 out_unregister_phy:
1093 transport_class_unregister(&sas_phy_class);
1094 out_unregister_transport:
1095 transport_class_unregister(&sas_host_class);
1096 out:
1097 return error;
1101 static void __exit sas_transport_exit(void)
1103 transport_class_unregister(&sas_host_class);
1104 transport_class_unregister(&sas_phy_class);
1105 transport_class_unregister(&sas_rphy_class);
1106 transport_class_unregister(&sas_end_dev_class);
1109 MODULE_AUTHOR("Christoph Hellwig");
1110 MODULE_DESCRIPTION("SAS Transphy Attributes");
1111 MODULE_LICENSE("GPL");
1113 module_init(sas_transport_init);
1114 module_exit(sas_transport_exit);