1 /*
2  * Copyright (C) 2005-2006 Dell Inc.
3  *	Released under GPL v2.
4  *
5  * Serial Attached SCSI (SAS) transport class.
6  *
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.
11  *
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.
20  *
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.
24  */
25 
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>
34 
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>
42 
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;
51 };
52 #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
53 
54 
55 /*
56  * Hack to allow attributes of the same name in different objects.
57  */
58 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59 	struct device_attribute dev_attr_##_prefix##_##_name = \
60 	__ATTR(_name,_mode,_show,_store)
61 
62 
63 /*
64  * Pretty printing helpers
65  */
66 
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;							\
74 								\
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;						\
84 }
85 
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;							\
92 								\
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 		}						\
100 	}							\
101 	return -EINVAL;						\
102 }
103 
104 #define sas_bitfield_name_search(title, table)			\
105 static ssize_t							\
106 get_sas_##title##_names(u32 table_key, char *buf)		\
107 {								\
108 	ssize_t len = 0;					\
109 	int i;							\
110 								\
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;					\
116 		}						\
117 	}							\
118 	len += sprintf(buf + len, "\n");			\
119 	return len;						\
120 }
121 
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" },
130 };
131 sas_bitfield_name_search(device_type, sas_device_type_names)
132 
133 
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" },
142 };
143 sas_bitfield_name_match(protocol, sas_protocol_names)
144 
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" },
157 };
158 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
159 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
160 
161 static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
162 {
163 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
164 	struct sas_end_device *rdev;
165 
166 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
167 
168 	rdev = rphy_to_end_device(rphy);
169 	return rdev;
170 }
171 
172 static int sas_smp_dispatch(struct bsg_job *job)
173 {
174 	struct Scsi_Host *shost = dev_to_shost(job->dev);
175 	struct sas_rphy *rphy = NULL;
176 
177 	if (!scsi_is_host_device(job->dev))
178 		rphy = dev_to_rphy(job->dev);
179 
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;
184 	}
185 
186 	to_sas_internal(shost->transportt)->f->smp_handler(job, shost, rphy);
187 	return 0;
188 }
189 
190 static void sas_host_release(struct device *dev)
191 {
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;
195 
196 	if (q)
197 		blk_cleanup_queue(q);
198 }
199 
200 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
201 {
202 	struct request_queue *q;
203 
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;
207 	}
208 
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];
217 
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;
224 	}
225 
226 	/*
227 	 * by default assume old behaviour and bounce for any highmem page
228 	 */
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;
233 }
234 
235 /*
236  * SAS host attributes
237  */
238 
239 static int sas_host_setup(struct transport_container *tc, struct device *dev,
240 			  struct device *cdev)
241 {
242 	struct Scsi_Host *shost = dev_to_shost(dev);
243 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
244 
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;
250 
251 	if (sas_bsg_initialize(shost, NULL))
252 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
253 			   shost->host_no);
254 
255 	return 0;
256 }
257 
258 static int sas_host_remove(struct transport_container *tc, struct device *dev,
259 			   struct device *cdev)
260 {
261 	struct Scsi_Host *shost = dev_to_shost(dev);
262 	struct request_queue *q = to_sas_host_attrs(shost)->q;
263 
264 	if (q)
265 		bsg_unregister_queue(q);
266 	return 0;
267 }
268 
269 static DECLARE_TRANSPORT_CLASS(sas_host_class,
270 		"sas_host", sas_host_setup, sas_host_remove, NULL);
271 
272 static int sas_host_match(struct attribute_container *cont,
273 			    struct device *dev)
274 {
275 	struct Scsi_Host *shost;
276 	struct sas_internal *i;
277 
278 	if (!scsi_is_host_device(dev))
279 		return 0;
280 	shost = dev_to_shost(dev);
281 
282 	if (!shost->transportt)
283 		return 0;
284 	if (shost->transportt->host_attrs.ac.class !=
285 			&sas_host_class.class)
286 		return 0;
287 
288 	i = to_sas_internal(shost->transportt);
289 	return &i->t.host_attrs.ac == cont;
290 }
291 
292 static int do_sas_phy_delete(struct device *dev, void *data)
293 {
294 	int pass = (int)(unsigned long)data;
295 
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;
301 }
302 
303 /**
304  * sas_remove_children  -  tear down a devices SAS data structures
305  * @dev:	device belonging to the sas object
306  *
307  * Removes all SAS PHYs and remote PHYs for a given object
308  */
309 void sas_remove_children(struct device *dev)
310 {
311 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
312 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
313 }
314 EXPORT_SYMBOL(sas_remove_children);
315 
316 /**
317  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
318  * @shost:	Scsi Host that is torn down
319  *
320  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
321  * Scsi_Host as well.
322  *
323  * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
324  * already removed.
325  */
326 void sas_remove_host(struct Scsi_Host *shost)
327 {
328 	sas_remove_children(&shost->shost_gendev);
329 	scsi_remove_host(shost);
330 }
331 EXPORT_SYMBOL(sas_remove_host);
332 
333 /**
334  * sas_get_address - return the SAS address of the device
335  * @sdev: scsi device
336  *
337  * Returns the SAS address of the scsi device
338  */
339 u64 sas_get_address(struct scsi_device *sdev)
340 {
341 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
342 
343 	return rdev->rphy.identify.sas_address;
344 }
345 EXPORT_SYMBOL(sas_get_address);
346 
347 /**
348  * sas_tlr_supported - checking TLR bit in vpd 0x90
349  * @sdev: scsi device struct
350  *
351  * Check Transport Layer Retries are supported or not.
352  * If vpd page 0x90 is present, TRL is supported.
353  *
354  */
355 unsigned int
356 sas_tlr_supported(struct scsi_device *sdev)
357 {
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;
362 
363 	if (!buffer)
364 		goto out;
365 
366 	if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
367 		goto out;
368 
369 	/*
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
374 	 */
375 	ret = buffer[12] & 0x01;
376 
377  out:
378 	kfree(buffer);
379 	rdev->tlr_supported = ret;
380 	return ret;
381 
382 }
383 EXPORT_SYMBOL_GPL(sas_tlr_supported);
384 
385 /**
386  * sas_disable_tlr - setting TLR flags
387  * @sdev: scsi device struct
388  *
389  * Seting tlr_enabled flag to 0.
390  *
391  */
392 void
393 sas_disable_tlr(struct scsi_device *sdev)
394 {
395 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
396 
397 	rdev->tlr_enabled = 0;
398 }
399 EXPORT_SYMBOL_GPL(sas_disable_tlr);
400 
401 /**
402  * sas_enable_tlr - setting TLR flags
403  * @sdev: scsi device struct
404  *
405  * Seting tlr_enabled flag 1.
406  *
407  */
408 void sas_enable_tlr(struct scsi_device *sdev)
409 {
410 	unsigned int tlr_supported = 0;
411 	tlr_supported  = sas_tlr_supported(sdev);
412 
413 	if (tlr_supported) {
414 		struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
415 
416 		rdev->tlr_enabled = 1;
417 	}
418 
419 	return;
420 }
421 EXPORT_SYMBOL_GPL(sas_enable_tlr);
422 
423 unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
424 {
425 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
426 	return rdev->tlr_enabled;
427 }
428 EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
429 
430 /*
431  * SAS Phy attributes
432  */
433 
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)		\
438 {									\
439 	struct sas_phy *phy = transport_class_to_phy(dev);		\
440 									\
441 	return snprintf(buf, 20, format_string, cast phy->field);	\
442 }
443 
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)
447 
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)		\
452 {									\
453 	struct sas_phy *phy = transport_class_to_phy(dev);		\
454 									\
455 	if (!phy->field)						\
456 		return snprintf(buf, 20, "none\n");			\
457 	return get_sas_protocol_names(phy->field, buf);		\
458 }
459 
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)
463 
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)		\
468 {									\
469 	struct sas_phy *phy = transport_class_to_phy(dev);		\
470 									\
471 	return get_sas_linkspeed_names(phy->field, buf);		\
472 }
473 
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)			\
480 {									\
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;							\
487 									\
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);			\
493 									\
494 	return error ? error : count;					\
495 }
496 
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)
502 
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)
506 
507 
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)		\
512 {									\
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;							\
517 									\
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);			\
522 }
523 
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)
527 
528 
529 static ssize_t
530 show_sas_device_type(struct device *dev,
531 		     struct device_attribute *attr, char *buf)
532 {
533 	struct sas_phy *phy = transport_class_to_phy(dev);
534 
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);
538 }
539 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
540 
541 static ssize_t do_sas_phy_enable(struct device *dev,
542 		size_t count, int enable)
543 {
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;
548 
549 	error = i->f->phy_enable(phy, enable);
550 	if (error)
551 		return error;
552 	phy->enabled = enable;
553 	return count;
554 };
555 
556 static ssize_t
557 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
558 		     const char *buf, size_t count)
559 {
560 	if (count < 1)
561 		return -EINVAL;
562 
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;
572 	}
573 
574 	return count;
575 }
576 
577 static ssize_t
578 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
579 		    char *buf)
580 {
581 	struct sas_phy *phy = transport_class_to_phy(dev);
582 
583 	return snprintf(buf, 20, "%d", phy->enabled);
584 }
585 
586 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
587 			 store_sas_phy_enable);
588 
589 static ssize_t
590 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
591 {
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;
596 
597 	error = i->f->phy_reset(phy, hard_reset);
598 	if (error)
599 		return error;
600 	phy->enabled = 1;
601 	return count;
602 };
603 
604 static ssize_t
605 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
606 		     const char *buf, size_t count)
607 {
608 	return do_sas_phy_reset(dev, count, 0);
609 }
610 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
611 
612 static ssize_t
613 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
614 		     const char *buf, size_t count)
615 {
616 	return do_sas_phy_reset(dev, count, 1);
617 }
618 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
619 
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);
637 
638 static int sas_phy_setup(struct transport_container *tc, struct device *dev,
639 			 struct device *cdev)
640 {
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);
644 
645 	if (i->f->phy_setup)
646 		i->f->phy_setup(phy);
647 
648 	return 0;
649 }
650 
651 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
652 		"sas_phy", sas_phy_setup, NULL, NULL);
653 
654 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
655 {
656 	struct Scsi_Host *shost;
657 	struct sas_internal *i;
658 
659 	if (!scsi_is_sas_phy(dev))
660 		return 0;
661 	shost = dev_to_shost(dev->parent);
662 
663 	if (!shost->transportt)
664 		return 0;
665 	if (shost->transportt->host_attrs.ac.class !=
666 			&sas_host_class.class)
667 		return 0;
668 
669 	i = to_sas_internal(shost->transportt);
670 	return &i->phy_attr_cont.ac == cont;
671 }
672 
673 static void sas_phy_release(struct device *dev)
674 {
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);
678 
679 	if (i->f->phy_release)
680 		i->f->phy_release(phy);
681 	put_device(dev->parent);
682 	kfree(phy);
683 }
684 
685 /**
686  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
687  * @parent:	Parent device
688  * @number:	Phy index
689  *
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.
693  *
694  * Returns:
695  *	SAS PHY allocated or %NULL if the allocation failed.
696  */
697 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
698 {
699 	struct Scsi_Host *shost = dev_to_shost(parent);
700 	struct sas_phy *phy;
701 
702 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
703 	if (!phy)
704 		return NULL;
705 
706 	phy->number = number;
707 	phy->enabled = 1;
708 
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);
719 
720 	transport_setup_device(&phy->dev);
721 
722 	return phy;
723 }
724 EXPORT_SYMBOL(sas_phy_alloc);
725 
726 /**
727  * sas_phy_add  -  add a SAS PHY to the device hierarchy
728  * @phy:	The PHY to be added
729  *
730  * Publishes a SAS PHY to the rest of the system.
731  */
732 int sas_phy_add(struct sas_phy *phy)
733 {
734 	int error;
735 
736 	error = device_add(&phy->dev);
737 	if (!error) {
738 		transport_add_device(&phy->dev);
739 		transport_configure_device(&phy->dev);
740 	}
741 
742 	return error;
743 }
744 EXPORT_SYMBOL(sas_phy_add);
745 
746 /**
747  * sas_phy_free  -  free a SAS PHY
748  * @phy:	SAS PHY to free
749  *
750  * Frees the specified SAS PHY.
751  *
752  * Note:
753  *   This function must only be called on a PHY that has not
754  *   successfully been added using sas_phy_add().
755  */
756 void sas_phy_free(struct sas_phy *phy)
757 {
758 	transport_destroy_device(&phy->dev);
759 	put_device(&phy->dev);
760 }
761 EXPORT_SYMBOL(sas_phy_free);
762 
763 /**
764  * sas_phy_delete  -  remove SAS PHY
765  * @phy:	SAS PHY to remove
766  *
767  * Removes the specified SAS PHY.  If the SAS PHY has an
768  * associated remote PHY it is removed before.
769  */
770 void
771 sas_phy_delete(struct sas_phy *phy)
772 {
773 	struct device *dev = &phy->dev;
774 
775 	/* this happens if the phy is still part of a port when deleted */
776 	BUG_ON(!list_empty(&phy->port_siblings));
777 
778 	transport_remove_device(dev);
779 	device_del(dev);
780 	transport_destroy_device(dev);
781 	put_device(dev);
782 }
783 EXPORT_SYMBOL(sas_phy_delete);
784 
785 /**
786  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
787  * @dev:	device to check
788  *
789  * Returns:
790  *	%1 if the device represents a SAS PHY, %0 else
791  */
792 int scsi_is_sas_phy(const struct device *dev)
793 {
794 	return dev->release == sas_phy_release;
795 }
796 EXPORT_SYMBOL(scsi_is_sas_phy);
797 
798 /*
799  * SAS Port attributes
800  */
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)		\
805 {									\
806 	struct sas_port *port = transport_class_to_sas_port(dev);	\
807 									\
808 	return snprintf(buf, 20, format_string, cast port->field);	\
809 }
810 
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)
814 
815 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
816 
817 static DECLARE_TRANSPORT_CLASS(sas_port_class,
818 			       "sas_port", NULL, NULL, NULL);
819 
820 static int sas_port_match(struct attribute_container *cont, struct device *dev)
821 {
822 	struct Scsi_Host *shost;
823 	struct sas_internal *i;
824 
825 	if (!scsi_is_sas_port(dev))
826 		return 0;
827 	shost = dev_to_shost(dev->parent);
828 
829 	if (!shost->transportt)
830 		return 0;
831 	if (shost->transportt->host_attrs.ac.class !=
832 			&sas_host_class.class)
833 		return 0;
834 
835 	i = to_sas_internal(shost->transportt);
836 	return &i->port_attr_cont.ac == cont;
837 }
838 
839 
840 static void sas_port_release(struct device *dev)
841 {
842 	struct sas_port *port = dev_to_sas_port(dev);
843 
844 	BUG_ON(!list_empty(&port->phy_list));
845 
846 	put_device(dev->parent);
847 	kfree(port);
848 }
849 
850 static void sas_port_create_link(struct sas_port *port,
851 				 struct sas_phy *phy)
852 {
853 	int res;
854 
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);
866 }
867 
868 static void sas_port_delete_link(struct sas_port *port,
869 				 struct sas_phy *phy)
870 {
871 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
872 	sysfs_remove_link(&phy->dev.kobj, "port");
873 }
874 
875 /** sas_port_alloc - allocate and initialize a SAS port structure
876  *
877  * @parent:	parent device
878  * @port_id:	port number
879  *
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.
883  *
884  * Returns %NULL on error
885  */
886 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
887 {
888 	struct Scsi_Host *shost = dev_to_shost(parent);
889 	struct sas_port *port;
890 
891 	port = kzalloc(sizeof(*port), GFP_KERNEL);
892 	if (!port)
893 		return NULL;
894 
895 	port->port_identifier = port_id;
896 
897 	device_initialize(&port->dev);
898 
899 	port->dev.parent = get_device(parent);
900 	port->dev.release = sas_port_release;
901 
902 	mutex_init(&port->phy_list_mutex);
903 	INIT_LIST_HEAD(&port->phy_list);
904 
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);
912 
913 	transport_setup_device(&port->dev);
914 
915 	return port;
916 }
917 EXPORT_SYMBOL(sas_port_alloc);
918 
919 /** sas_port_alloc_num - allocate and initialize a SAS port structure
920  *
921  * @parent:	parent device
922  *
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.
928  *
929  * Returns %NULL on error
930  */
931 struct sas_port *sas_port_alloc_num(struct device *parent)
932 {
933 	int index;
934 	struct Scsi_Host *shost = dev_to_shost(parent);
935 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
936 
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);
942 
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);
948 }
949 EXPORT_SYMBOL(sas_port_alloc_num);
950 
951 /**
952  * sas_port_add - add a SAS port to the device hierarchy
953  * @port:	port to be added
954  *
955  * publishes a port to the rest of the system
956  */
957 int sas_port_add(struct sas_port *port)
958 {
959 	int error;
960 
961 	/* No phys should be added until this is made visible */
962 	BUG_ON(!list_empty(&port->phy_list));
963 
964 	error = device_add(&port->dev);
965 
966 	if (error)
967 		return error;
968 
969 	transport_add_device(&port->dev);
970 	transport_configure_device(&port->dev);
971 
972 	return 0;
973 }
974 EXPORT_SYMBOL(sas_port_add);
975 
976 /**
977  * sas_port_free  -  free a SAS PORT
978  * @port:	SAS PORT to free
979  *
980  * Frees the specified SAS PORT.
981  *
982  * Note:
983  *   This function must only be called on a PORT that has not
984  *   successfully been added using sas_port_add().
985  */
986 void sas_port_free(struct sas_port *port)
987 {
988 	transport_destroy_device(&port->dev);
989 	put_device(&port->dev);
990 }
991 EXPORT_SYMBOL(sas_port_free);
992 
993 /**
994  * sas_port_delete  -  remove SAS PORT
995  * @port:	SAS PORT to remove
996  *
997  * Removes the specified SAS PORT.  If the SAS PORT has an
998  * associated phys, unlink them from the port as well.
999  */
1000 void sas_port_delete(struct sas_port *port)
1001 {
1002 	struct device *dev = &port->dev;
1003 	struct sas_phy *phy, *tmp_phy;
1004 
1005 	if (port->rphy) {
1006 		sas_rphy_delete(port->rphy);
1007 		port->rphy = NULL;
1008 	}
1009 
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);
1015 	}
1016 	mutex_unlock(&port->phy_list_mutex);
1017 
1018 	if (port->is_backlink) {
1019 		struct device *parent = port->dev.parent;
1020 
1021 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1022 		port->is_backlink = 0;
1023 	}
1024 
1025 	transport_remove_device(dev);
1026 	device_del(dev);
1027 	transport_destroy_device(dev);
1028 	put_device(dev);
1029 }
1030 EXPORT_SYMBOL(sas_port_delete);
1031 
1032 /**
1033  * scsi_is_sas_port -  check if a struct device represents a SAS port
1034  * @dev:	device to check
1035  *
1036  * Returns:
1037  *	%1 if the device represents a SAS Port, %0 else
1038  */
1039 int scsi_is_sas_port(const struct device *dev)
1040 {
1041 	return dev->release == sas_port_release;
1042 }
1043 EXPORT_SYMBOL(scsi_is_sas_port);
1044 
1045 /**
1046  * sas_port_get_phy - try to take a reference on a port member
1047  * @port: port to check
1048  */
1049 struct sas_phy *sas_port_get_phy(struct sas_port *port)
1050 {
1051 	struct sas_phy *phy;
1052 
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;
1058 
1059 		phy = list_entry(ent, typeof(*phy), port_siblings);
1060 		get_device(&phy->dev);
1061 	}
1062 	mutex_unlock(&port->phy_list_mutex);
1063 
1064 	return phy;
1065 }
1066 EXPORT_SYMBOL(sas_port_get_phy);
1067 
1068 /**
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
1072  *
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.
1079  */
1080 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1081 {
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;
1086 
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();
1096 		}
1097 	} else {
1098 		sas_port_create_link(port, phy);
1099 		list_add_tail(&phy->port_siblings, &port->phy_list);
1100 		port->num_phys++;
1101 	}
1102 	mutex_unlock(&port->phy_list_mutex);
1103 }
1104 EXPORT_SYMBOL(sas_port_add_phy);
1105 
1106 /**
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
1110  *
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.
1113  */
1114 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1115 {
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);
1121 }
1122 EXPORT_SYMBOL(sas_port_delete_phy);
1123 
1124 void sas_port_mark_backlink(struct sas_port *port)
1125 {
1126 	int res;
1127 	struct device *parent = port->dev.parent->parent->parent;
1128 
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);
1140 
1141 }
1142 EXPORT_SYMBOL(sas_port_mark_backlink);
1143 
1144 /*
1145  * SAS remote PHY attributes.
1146  */
1147 
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)		\
1152 {									\
1153 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1154 									\
1155 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1156 }
1157 
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)
1162 
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)		\
1167 {									\
1168 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1169 									\
1170 	if (!rphy->field)					\
1171 		return snprintf(buf, 20, "none\n");			\
1172 	return get_sas_protocol_names(rphy->field, buf);	\
1173 }
1174 
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)
1179 
1180 static ssize_t
1181 show_sas_rphy_device_type(struct device *dev,
1182 			  struct device_attribute *attr, char *buf)
1183 {
1184 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1185 
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);
1190 }
1191 
1192 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1193 		show_sas_rphy_device_type, NULL);
1194 
1195 static ssize_t
1196 show_sas_rphy_enclosure_identifier(struct device *dev,
1197 				   struct device_attribute *attr, char *buf)
1198 {
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;
1205 
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);
1210 }
1211 
1212 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1213 		show_sas_rphy_enclosure_identifier, NULL);
1214 
1215 static ssize_t
1216 show_sas_rphy_bay_identifier(struct device *dev,
1217 			     struct device_attribute *attr, char *buf)
1218 {
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;
1224 
1225 	val = i->f->get_bay_identifier(rphy);
1226 	if (val < 0)
1227 		return val;
1228 	return sprintf(buf, "%d\n", val);
1229 }
1230 
1231 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1232 		show_sas_rphy_bay_identifier, NULL);
1233 
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);
1241 
1242 /* only need 8 bytes of data plus header (4 or 8) */
1243 #define BUF_SIZE 64
1244 
1245 int sas_read_port_mode_page(struct scsi_device *sdev)
1246 {
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;
1251 
1252 	if (!buffer)
1253 		return -ENOMEM;
1254 
1255 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1256 			      &mode_data, NULL);
1257 
1258 	error = -EINVAL;
1259 	if (!scsi_status_is_good(res))
1260 		goto out;
1261 
1262 	msdata = buffer +  mode_data.header_length +
1263 		mode_data.block_descriptor_length;
1264 
1265 	if (msdata - buffer > BUF_SIZE - 8)
1266 		goto out;
1267 
1268 	error = 0;
1269 
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];
1273 
1274  out:
1275 	kfree(buffer);
1276 	return error;
1277 }
1278 EXPORT_SYMBOL(sas_read_port_mode_page);
1279 
1280 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1281 			       "sas_end_device", NULL, NULL, NULL);
1282 
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)	\
1287 {									\
1288 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1289 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
1290 									\
1291 	return snprintf(buf, 20, format_string, cast rdev->field);	\
1292 }
1293 
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)
1298 
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);
1308 
1309 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1310 			       "sas_expander", NULL, NULL, NULL);
1311 
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)	\
1316 {									\
1317 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1318 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1319 									\
1320 	return snprintf(buf, 20, format_string, cast edev->field);	\
1321 }
1322 
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)
1327 
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);
1337 
1338 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1339 		"sas_device", NULL, NULL, NULL);
1340 
1341 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1342 {
1343 	struct Scsi_Host *shost;
1344 	struct sas_internal *i;
1345 
1346 	if (!scsi_is_sas_rphy(dev))
1347 		return 0;
1348 	shost = dev_to_shost(dev->parent->parent);
1349 
1350 	if (!shost->transportt)
1351 		return 0;
1352 	if (shost->transportt->host_attrs.ac.class !=
1353 			&sas_host_class.class)
1354 		return 0;
1355 
1356 	i = to_sas_internal(shost->transportt);
1357 	return &i->rphy_attr_cont.ac == cont;
1358 }
1359 
1360 static int sas_end_dev_match(struct attribute_container *cont,
1361 			     struct device *dev)
1362 {
1363 	struct Scsi_Host *shost;
1364 	struct sas_internal *i;
1365 	struct sas_rphy *rphy;
1366 
1367 	if (!scsi_is_sas_rphy(dev))
1368 		return 0;
1369 	shost = dev_to_shost(dev->parent->parent);
1370 	rphy = dev_to_rphy(dev);
1371 
1372 	if (!shost->transportt)
1373 		return 0;
1374 	if (shost->transportt->host_attrs.ac.class !=
1375 			&sas_host_class.class)
1376 		return 0;
1377 
1378 	i = to_sas_internal(shost->transportt);
1379 	return &i->end_dev_attr_cont.ac == cont &&
1380 		rphy->identify.device_type == SAS_END_DEVICE;
1381 }
1382 
1383 static int sas_expander_match(struct attribute_container *cont,
1384 			      struct device *dev)
1385 {
1386 	struct Scsi_Host *shost;
1387 	struct sas_internal *i;
1388 	struct sas_rphy *rphy;
1389 
1390 	if (!scsi_is_sas_rphy(dev))
1391 		return 0;
1392 	shost = dev_to_shost(dev->parent->parent);
1393 	rphy = dev_to_rphy(dev);
1394 
1395 	if (!shost->transportt)
1396 		return 0;
1397 	if (shost->transportt->host_attrs.ac.class !=
1398 			&sas_host_class.class)
1399 		return 0;
1400 
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);
1405 }
1406 
1407 static void sas_expander_release(struct device *dev)
1408 {
1409 	struct sas_rphy *rphy = dev_to_rphy(dev);
1410 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1411 
1412 	if (rphy->q)
1413 		blk_cleanup_queue(rphy->q);
1414 
1415 	put_device(dev->parent);
1416 	kfree(edev);
1417 }
1418 
1419 static void sas_end_device_release(struct device *dev)
1420 {
1421 	struct sas_rphy *rphy = dev_to_rphy(dev);
1422 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1423 
1424 	if (rphy->q)
1425 		blk_cleanup_queue(rphy->q);
1426 
1427 	put_device(dev->parent);
1428 	kfree(edev);
1429 }
1430 
1431 /**
1432  * sas_rphy_initialize - common rphy initialization
1433  * @rphy:	rphy to initialise
1434  *
1435  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1436  * initialise the common rphy component of each.
1437  */
1438 static void sas_rphy_initialize(struct sas_rphy *rphy)
1439 {
1440 	INIT_LIST_HEAD(&rphy->list);
1441 }
1442 
1443 /**
1444  * sas_end_device_alloc - allocate an rphy for an end device
1445  * @parent: which port
1446  *
1447  * Allocates an SAS remote PHY structure, connected to @parent.
1448  *
1449  * Returns:
1450  *	SAS PHY allocated or %NULL if the allocation failed.
1451  */
1452 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1453 {
1454 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1455 	struct sas_end_device *rdev;
1456 
1457 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1458 	if (!rdev) {
1459 		return NULL;
1460 	}
1461 
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);
1476 
1477 	return &rdev->rphy;
1478 }
1479 EXPORT_SYMBOL(sas_end_device_alloc);
1480 
1481 /**
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
1485  *
1486  * Allocates an SAS remote PHY structure, connected to @parent.
1487  *
1488  * Returns:
1489  *	SAS PHY allocated or %NULL if the allocation failed.
1490  */
1491 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1492 				    enum sas_device_type type)
1493 {
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);
1497 
1498 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1499 	       type != SAS_FANOUT_EXPANDER_DEVICE);
1500 
1501 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1502 	if (!rdev) {
1503 		return NULL;
1504 	}
1505 
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);
1517 
1518 	return &rdev->rphy;
1519 }
1520 EXPORT_SYMBOL(sas_expander_alloc);
1521 
1522 /**
1523  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1524  * @rphy:	The remote PHY to be added
1525  *
1526  * Publishes a SAS remote PHY to the rest of the system.
1527  */
1528 int sas_rphy_add(struct sas_rphy *rphy)
1529 {
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;
1535 
1536 	if (parent->rphy)
1537 		return -ENXIO;
1538 	parent->rphy = rphy;
1539 
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));
1547 
1548 
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);
1558 
1559 	if (identify->device_type == SAS_END_DEVICE &&
1560 	    rphy->scsi_target_id != -1) {
1561 		int lun;
1562 
1563 		if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
1564 			lun = SCAN_WILD_CARD;
1565 		else
1566 			lun = 0;
1567 
1568 		scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
1569 				 SCSI_SCAN_INITIAL);
1570 	}
1571 
1572 	return 0;
1573 }
1574 EXPORT_SYMBOL(sas_rphy_add);
1575 
1576 /**
1577  * sas_rphy_free  -  free a SAS remote PHY
1578  * @rphy: SAS remote PHY to free
1579  *
1580  * Frees the specified SAS remote PHY.
1581  *
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)
1586  */
1587 void sas_rphy_free(struct sas_rphy *rphy)
1588 {
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);
1592 
1593 	mutex_lock(&sas_host->lock);
1594 	list_del(&rphy->list);
1595 	mutex_unlock(&sas_host->lock);
1596 
1597 	transport_destroy_device(dev);
1598 
1599 	put_device(dev);
1600 }
1601 EXPORT_SYMBOL(sas_rphy_free);
1602 
1603 /**
1604  * sas_rphy_delete  -  remove and free SAS remote PHY
1605  * @rphy:	SAS remote PHY to remove and free
1606  *
1607  * Removes the specified SAS remote PHY and frees it.
1608  */
1609 void
1610 sas_rphy_delete(struct sas_rphy *rphy)
1611 {
1612 	sas_rphy_remove(rphy);
1613 	sas_rphy_free(rphy);
1614 }
1615 EXPORT_SYMBOL(sas_rphy_delete);
1616 
1617 /**
1618  * sas_rphy_unlink  -  unlink SAS remote PHY
1619  * @rphy:	SAS remote phy to unlink from its parent port
1620  *
1621  * Removes port reference to an rphy
1622  */
1623 void sas_rphy_unlink(struct sas_rphy *rphy)
1624 {
1625 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1626 
1627 	parent->rphy = NULL;
1628 }
1629 EXPORT_SYMBOL(sas_rphy_unlink);
1630 
1631 /**
1632  * sas_rphy_remove  -  remove SAS remote PHY
1633  * @rphy:	SAS remote phy to remove
1634  *
1635  * Removes the specified SAS remote PHY.
1636  */
1637 void
1638 sas_rphy_remove(struct sas_rphy *rphy)
1639 {
1640 	struct device *dev = &rphy->dev;
1641 
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;
1652 	}
1653 
1654 	sas_rphy_unlink(rphy);
1655 	if (rphy->q)
1656 		bsg_unregister_queue(rphy->q);
1657 	transport_remove_device(dev);
1658 	device_del(dev);
1659 }
1660 EXPORT_SYMBOL(sas_rphy_remove);
1661 
1662 /**
1663  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1664  * @dev:	device to check
1665  *
1666  * Returns:
1667  *	%1 if the device represents a SAS remote PHY, %0 else
1668  */
1669 int scsi_is_sas_rphy(const struct device *dev)
1670 {
1671 	return dev->release == sas_end_device_release ||
1672 		dev->release == sas_expander_release;
1673 }
1674 EXPORT_SYMBOL(scsi_is_sas_rphy);
1675 
1676 
1677 /*
1678  * SCSI scan helper
1679  */
1680 
1681 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1682 		uint id, u64 lun)
1683 {
1684 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1685 	struct sas_rphy *rphy;
1686 
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;
1692 
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);
1697 		}
1698 	}
1699 	mutex_unlock(&sas_host->lock);
1700 
1701 	return 0;
1702 }
1703 
1704 
1705 /*
1706  * Setup / Teardown code
1707  */
1708 
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++
1715 
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;			\
1722 	}								\
1723 	i->attrb[count] = &i->private_##attrb[count];			\
1724 	if (test)							\
1725 		count++
1726 
1727 #define SETUP_RPORT_ATTRIBUTE(field) 					\
1728 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1729 
1730 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
1731 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1732 
1733 #define SETUP_PHY_ATTRIBUTE(field)					\
1734 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1735 
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)
1739 
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)
1743 
1744 #define SETUP_PORT_ATTRIBUTE(field)					\
1745 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1746 
1747 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
1748 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1749 
1750 #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1751 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1752 
1753 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1754 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1755 
1756 #define SETUP_END_DEV_ATTRIBUTE(field)					\
1757 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1758 
1759 #define SETUP_EXPANDER_ATTRIBUTE(field)					\
1760 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1761 
1762 /**
1763  * sas_attach_transport  -  instantiate SAS transport template
1764  * @ft:		SAS transport class function template
1765  */
1766 struct scsi_transport_template *
1767 sas_attach_transport(struct sas_function_template *ft)
1768 {
1769 	struct sas_internal *i;
1770 	int count;
1771 
1772 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1773 	if (!i)
1774 		return NULL;
1775 
1776 	i->t.user_scan = sas_user_scan;
1777 
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);
1783 
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);
1788 
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);
1793 
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);
1798 
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);
1803 
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);
1808 
1809 	i->f = ft;
1810 
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);
1823 
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;
1832 
1833 	count = 0;
1834 	SETUP_PORT_ATTRIBUTE(num_phys);
1835 	i->port_attrs[count] = NULL;
1836 
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;
1849 
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;
1857 
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;
1867 
1868 	return &i->t;
1869 }
1870 EXPORT_SYMBOL(sas_attach_transport);
1871 
1872 /**
1873  * sas_release_transport  -  release SAS transport template instance
1874  * @t:		transport template instance
1875  */
1876 void sas_release_transport(struct scsi_transport_template *t)
1877 {
1878 	struct sas_internal *i = to_sas_internal(t);
1879 
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);
1886 
1887 	kfree(i);
1888 }
1889 EXPORT_SYMBOL(sas_release_transport);
1890 
1891 static __init int sas_transport_init(void)
1892 {
1893 	int error;
1894 
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;
1913 
1914 	return 0;
1915 
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;
1928 
1929 }
1930 
1931 static void __exit sas_transport_exit(void)
1932 {
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);
1939 }
1940 
1941 MODULE_AUTHOR("Christoph Hellwig");
1942 MODULE_DESCRIPTION("SAS Transport Attributes");
1943 MODULE_LICENSE("GPL");
1944 
1945 module_init(sas_transport_init);
1946 module_exit(sas_transport_exit);
1947