xref: /linux/drivers/scsi/scsi_transport_sas.c (revision be8fcd4a8217a916344c88a4b1b84f5736dda17e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005-2006 Dell Inc.
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/log2.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/blkdev.h>
34 #include <linux/bsg.h>
35 
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.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 	{ SAS_LINK_RATE_22_5_GBPS,	"22.5 Gbit" },
158 };
159 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
160 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
161 
162 static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
163 {
164 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
165 	struct sas_end_device *rdev;
166 
167 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
168 
169 	rdev = rphy_to_end_device(rphy);
170 	return rdev;
171 }
172 
173 static int sas_smp_dispatch(struct bsg_job *job)
174 {
175 	struct Scsi_Host *shost = dev_to_shost(job->dev);
176 	struct sas_rphy *rphy = NULL;
177 
178 	if (!scsi_is_host_device(job->dev))
179 		rphy = dev_to_rphy(job->dev);
180 
181 	if (!job->reply_payload.payload_len) {
182 		dev_warn(job->dev, "space for a smp response is missing\n");
183 		bsg_job_done(job, -EINVAL, 0);
184 		return 0;
185 	}
186 
187 	to_sas_internal(shost->transportt)->f->smp_handler(job, shost, rphy);
188 	return 0;
189 }
190 
191 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
192 {
193 	struct request_queue *q;
194 
195 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
196 		printk("%s can't handle SMP requests\n", shost->hostt->name);
197 		return 0;
198 	}
199 
200 	if (rphy) {
201 		q = bsg_setup_queue(&rphy->dev, dev_name(&rphy->dev), NULL,
202 				sas_smp_dispatch, NULL, 0);
203 		if (IS_ERR(q))
204 			return PTR_ERR(q);
205 		rphy->q = q;
206 	} else {
207 		char name[20];
208 
209 		snprintf(name, sizeof(name), "sas_host%d", shost->host_no);
210 		q = bsg_setup_queue(&shost->shost_gendev, name, NULL,
211 				sas_smp_dispatch, NULL, 0);
212 		if (IS_ERR(q))
213 			return PTR_ERR(q);
214 		to_sas_host_attrs(shost)->q = q;
215 	}
216 
217 	return 0;
218 }
219 
220 /*
221  * SAS host attributes
222  */
223 
224 /*
225  * Set shost->opt_sectors from the DMA optimal mapping size, but only
226  * when dma_opt_mapping_size() is strictly less than dma_max_mapping_size(),
227  * indicating a genuine optimization hint from an IOMMU or DMA backend.
228  * When the two are equal (e.g. IOMMU disabled / passthrough), no real
229  * hint exists, so leave opt_sectors at 0 to avoid bogus optimal_io_size
230  * values that break filesystem geometry (e.g. mkfs.xfs stripe alignment).
231  */
232 static void sas_dma_setup_opt_sectors(struct Scsi_Host *shost)
233 {
234 	struct device *dma_dev = shost->dma_dev;
235 	size_t opt = dma_opt_mapping_size(dma_dev);
236 	size_t max = dma_max_mapping_size(dma_dev);
237 	unsigned int opt_sectors;
238 
239 	/* opt >= max means no real hint was provided by the DMA layer */
240 	if (opt >= max)
241 		return;
242 
243 	/* Clamp to max_sectors to avoid overflow in sector arithmetic */
244 	opt_sectors = min_t(unsigned int, opt >> SECTOR_SHIFT,
245 			    shost->max_sectors);
246 
247 	/* Guard against zero before rounddown_pow_of_two() */
248 	if (!opt_sectors)
249 		return;
250 
251 	/*
252 	 * Round down to power-of-two so filesystem geometry calculations
253 	 * (e.g. XFS stripe width/unit) always produce clean divisors.
254 	 */
255 	shost->opt_sectors = rounddown_pow_of_two(opt_sectors);
256 }
257 
258 static int sas_host_setup(struct transport_container *tc, struct device *dev,
259 			  struct device *cdev)
260 {
261 	struct Scsi_Host *shost = dev_to_shost(dev);
262 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
263 
264 	INIT_LIST_HEAD(&sas_host->rphy_list);
265 	mutex_init(&sas_host->lock);
266 	sas_host->next_target_id = 0;
267 	sas_host->next_expander_id = 0;
268 	sas_host->next_port_id = 0;
269 
270 	if (sas_bsg_initialize(shost, NULL))
271 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
272 			   shost->host_no);
273 
274 	sas_dma_setup_opt_sectors(shost);
275 
276 	return 0;
277 }
278 
279 static int sas_host_remove(struct transport_container *tc, struct device *dev,
280 			   struct device *cdev)
281 {
282 	struct Scsi_Host *shost = dev_to_shost(dev);
283 	struct request_queue *q = to_sas_host_attrs(shost)->q;
284 
285 	bsg_remove_queue(q);
286 	return 0;
287 }
288 
289 static DECLARE_TRANSPORT_CLASS(sas_host_class,
290 		"sas_host", sas_host_setup, sas_host_remove, NULL);
291 
292 static int sas_host_match(struct attribute_container *cont,
293 			    struct device *dev)
294 {
295 	struct Scsi_Host *shost;
296 	struct sas_internal *i;
297 
298 	if (!scsi_is_host_device(dev))
299 		return 0;
300 	shost = dev_to_shost(dev);
301 
302 	if (!shost->transportt)
303 		return 0;
304 	if (shost->transportt->host_attrs.ac.class !=
305 			&sas_host_class.class)
306 		return 0;
307 
308 	i = to_sas_internal(shost->transportt);
309 	return &i->t.host_attrs.ac == cont;
310 }
311 
312 static int do_sas_phy_delete(struct device *dev, void *data)
313 {
314 	int pass = (int)(unsigned long)data;
315 
316 	if (pass == 0 && scsi_is_sas_port(dev))
317 		sas_port_delete(dev_to_sas_port(dev));
318 	else if (pass == 1 && scsi_is_sas_phy(dev))
319 		sas_phy_delete(dev_to_phy(dev));
320 	return 0;
321 }
322 
323 /**
324  * sas_remove_children  -  tear down a devices SAS data structures
325  * @dev:	device belonging to the sas object
326  *
327  * Removes all SAS PHYs and remote PHYs for a given object
328  */
329 void sas_remove_children(struct device *dev)
330 {
331 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
332 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
333 }
334 EXPORT_SYMBOL(sas_remove_children);
335 
336 /**
337  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
338  * @shost:	Scsi Host that is torn down
339  *
340  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
341  * Scsi_Host as well.
342  *
343  * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
344  * already removed.
345  */
346 void sas_remove_host(struct Scsi_Host *shost)
347 {
348 	sas_remove_children(&shost->shost_gendev);
349 	scsi_remove_host(shost);
350 }
351 EXPORT_SYMBOL(sas_remove_host);
352 
353 /**
354  * sas_get_address - return the SAS address of the device
355  * @sdev: scsi device
356  *
357  * Returns the SAS address of the scsi device
358  */
359 u64 sas_get_address(struct scsi_device *sdev)
360 {
361 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
362 
363 	return rdev->rphy.identify.sas_address;
364 }
365 EXPORT_SYMBOL(sas_get_address);
366 
367 /**
368  * sas_tlr_supported - checking TLR bit in vpd 0x90
369  * @sdev: scsi device struct
370  *
371  * Check Transport Layer Retries are supported or not.
372  * If vpd page 0x90 is present, TRL is supported.
373  *
374  */
375 unsigned int
376 sas_tlr_supported(struct scsi_device *sdev)
377 {
378 	const int vpd_len = 32;
379 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
380 	char *buffer = kzalloc(vpd_len, GFP_KERNEL);
381 	int ret = 0;
382 
383 	if (!buffer)
384 		goto out;
385 
386 	if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
387 		goto out;
388 
389 	/*
390 	 * Magic numbers: the VPD Protocol page (0x90)
391 	 * has a 4 byte header and then one entry per device port
392 	 * the TLR bit is at offset 8 on each port entry
393 	 * if we take the first port, that's at total offset 12
394 	 */
395 	ret = buffer[12] & 0x01;
396 
397  out:
398 	kfree(buffer);
399 	rdev->tlr_supported = ret;
400 	return ret;
401 
402 }
403 EXPORT_SYMBOL_GPL(sas_tlr_supported);
404 
405 /**
406  * sas_disable_tlr - setting TLR flags
407  * @sdev: scsi device struct
408  *
409  * Seting tlr_enabled flag to 0.
410  *
411  */
412 void
413 sas_disable_tlr(struct scsi_device *sdev)
414 {
415 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
416 
417 	rdev->tlr_enabled = 0;
418 }
419 EXPORT_SYMBOL_GPL(sas_disable_tlr);
420 
421 /**
422  * sas_enable_tlr - setting TLR flags
423  * @sdev: scsi device struct
424  *
425  * Seting tlr_enabled flag 1.
426  *
427  */
428 void sas_enable_tlr(struct scsi_device *sdev)
429 {
430 	unsigned int tlr_supported = 0;
431 	tlr_supported  = sas_tlr_supported(sdev);
432 
433 	if (tlr_supported) {
434 		struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
435 
436 		rdev->tlr_enabled = 1;
437 	}
438 
439 	return;
440 }
441 EXPORT_SYMBOL_GPL(sas_enable_tlr);
442 
443 unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
444 {
445 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
446 	return rdev->tlr_enabled;
447 }
448 EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
449 
450 /**
451  * sas_ata_ncq_prio_supported - Check for ATA NCQ command priority support
452  * @sdev: SCSI device
453  *
454  * Check if an ATA device supports NCQ priority using VPD page 89h (ATA
455  * Information). Since this VPD page is implemented only for ATA devices,
456  * this function always returns false for SCSI devices.
457  */
458 bool sas_ata_ncq_prio_supported(struct scsi_device *sdev)
459 {
460 	struct scsi_vpd *vpd;
461 	bool ncq_prio_supported = false;
462 
463 	rcu_read_lock();
464 	vpd = rcu_dereference(sdev->vpd_pg89);
465 	if (vpd && vpd->len >= 214)
466 		ncq_prio_supported = (vpd->data[213] >> 4) & 1;
467 	rcu_read_unlock();
468 
469 	return ncq_prio_supported;
470 }
471 EXPORT_SYMBOL_GPL(sas_ata_ncq_prio_supported);
472 
473 /*
474  * SAS Phy attributes
475  */
476 
477 #define sas_phy_show_simple(field, name, format_string, cast)		\
478 static ssize_t								\
479 show_sas_phy_##name(struct device *dev, 				\
480 		    struct device_attribute *attr, char *buf)		\
481 {									\
482 	struct sas_phy *phy = transport_class_to_phy(dev);		\
483 									\
484 	return snprintf(buf, 20, format_string, cast phy->field);	\
485 }
486 
487 #define sas_phy_simple_attr(field, name, format_string, type)		\
488 	sas_phy_show_simple(field, name, format_string, (type))	\
489 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
490 
491 #define sas_phy_show_protocol(field, name)				\
492 static ssize_t								\
493 show_sas_phy_##name(struct device *dev, 				\
494 		    struct device_attribute *attr, char *buf)		\
495 {									\
496 	struct sas_phy *phy = transport_class_to_phy(dev);		\
497 									\
498 	if (!phy->field)						\
499 		return snprintf(buf, 20, "none\n");			\
500 	return get_sas_protocol_names(phy->field, buf);		\
501 }
502 
503 #define sas_phy_protocol_attr(field, name)				\
504 	sas_phy_show_protocol(field, name)				\
505 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
506 
507 #define sas_phy_show_linkspeed(field)					\
508 static ssize_t								\
509 show_sas_phy_##field(struct device *dev, 				\
510 		     struct device_attribute *attr, char *buf)		\
511 {									\
512 	struct sas_phy *phy = transport_class_to_phy(dev);		\
513 									\
514 	return get_sas_linkspeed_names(phy->field, buf);		\
515 }
516 
517 /* Fudge to tell if we're minimum or maximum */
518 #define sas_phy_store_linkspeed(field)					\
519 static ssize_t								\
520 store_sas_phy_##field(struct device *dev, 				\
521 		      struct device_attribute *attr, 			\
522 		      const char *buf,	size_t count)			\
523 {									\
524 	struct sas_phy *phy = transport_class_to_phy(dev);		\
525 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
526 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
527 	u32 value;							\
528 	struct sas_phy_linkrates rates = {0};				\
529 	int error;							\
530 									\
531 	error = set_sas_linkspeed_names(&value, buf);			\
532 	if (error)							\
533 		return error;						\
534 	rates.field = value;						\
535 	error = i->f->set_phy_speed(phy, &rates);			\
536 									\
537 	return error ? error : count;					\
538 }
539 
540 #define sas_phy_linkspeed_rw_attr(field)				\
541 	sas_phy_show_linkspeed(field)					\
542 	sas_phy_store_linkspeed(field)					\
543 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
544 	store_sas_phy_##field)
545 
546 #define sas_phy_linkspeed_attr(field)					\
547 	sas_phy_show_linkspeed(field)					\
548 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
549 
550 
551 #define sas_phy_show_linkerror(field)					\
552 static ssize_t								\
553 show_sas_phy_##field(struct device *dev, 				\
554 		     struct device_attribute *attr, char *buf)		\
555 {									\
556 	struct sas_phy *phy = transport_class_to_phy(dev);		\
557 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
558 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
559 	int error;							\
560 									\
561 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
562 	if (error)							\
563 		return error;						\
564 	return snprintf(buf, 20, "%u\n", phy->field);			\
565 }
566 
567 #define sas_phy_linkerror_attr(field)					\
568 	sas_phy_show_linkerror(field)					\
569 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
570 
571 
572 static ssize_t
573 show_sas_device_type(struct device *dev,
574 		     struct device_attribute *attr, char *buf)
575 {
576 	struct sas_phy *phy = transport_class_to_phy(dev);
577 
578 	if (!phy->identify.device_type)
579 		return snprintf(buf, 20, "none\n");
580 	return get_sas_device_type_names(phy->identify.device_type, buf);
581 }
582 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
583 
584 static ssize_t do_sas_phy_enable(struct device *dev,
585 		size_t count, int enable)
586 {
587 	struct sas_phy *phy = transport_class_to_phy(dev);
588 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
589 	struct sas_internal *i = to_sas_internal(shost->transportt);
590 	int error;
591 
592 	error = i->f->phy_enable(phy, enable);
593 	if (error)
594 		return error;
595 	phy->enabled = enable;
596 	return count;
597 };
598 
599 static ssize_t
600 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
601 		     const char *buf, size_t count)
602 {
603 	if (count < 1)
604 		return -EINVAL;
605 
606 	switch (buf[0]) {
607 	case '0':
608 		do_sas_phy_enable(dev, count, 0);
609 		break;
610 	case '1':
611 		do_sas_phy_enable(dev, count, 1);
612 		break;
613 	default:
614 		return -EINVAL;
615 	}
616 
617 	return count;
618 }
619 
620 static ssize_t
621 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
622 		    char *buf)
623 {
624 	struct sas_phy *phy = transport_class_to_phy(dev);
625 
626 	return snprintf(buf, 20, "%d\n", phy->enabled);
627 }
628 
629 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
630 			 store_sas_phy_enable);
631 
632 static ssize_t
633 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
634 {
635 	struct sas_phy *phy = transport_class_to_phy(dev);
636 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
637 	struct sas_internal *i = to_sas_internal(shost->transportt);
638 	int error;
639 
640 	error = i->f->phy_reset(phy, hard_reset);
641 	if (error)
642 		return error;
643 	phy->enabled = 1;
644 	return count;
645 };
646 
647 static ssize_t
648 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
649 		     const char *buf, size_t count)
650 {
651 	return do_sas_phy_reset(dev, count, 0);
652 }
653 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
654 
655 static ssize_t
656 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
657 		     const char *buf, size_t count)
658 {
659 	return do_sas_phy_reset(dev, count, 1);
660 }
661 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
662 
663 sas_phy_protocol_attr(identify.initiator_port_protocols,
664 		initiator_port_protocols);
665 sas_phy_protocol_attr(identify.target_port_protocols,
666 		target_port_protocols);
667 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
668 		unsigned long long);
669 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
670 sas_phy_linkspeed_attr(negotiated_linkrate);
671 sas_phy_linkspeed_attr(minimum_linkrate_hw);
672 sas_phy_linkspeed_rw_attr(minimum_linkrate);
673 sas_phy_linkspeed_attr(maximum_linkrate_hw);
674 sas_phy_linkspeed_rw_attr(maximum_linkrate);
675 sas_phy_linkerror_attr(invalid_dword_count);
676 sas_phy_linkerror_attr(running_disparity_error_count);
677 sas_phy_linkerror_attr(loss_of_dword_sync_count);
678 sas_phy_linkerror_attr(phy_reset_problem_count);
679 
680 static int sas_phy_setup(struct transport_container *tc, struct device *dev,
681 			 struct device *cdev)
682 {
683 	struct sas_phy *phy = dev_to_phy(dev);
684 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
685 	struct sas_internal *i = to_sas_internal(shost->transportt);
686 
687 	if (i->f->phy_setup)
688 		i->f->phy_setup(phy);
689 
690 	return 0;
691 }
692 
693 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
694 		"sas_phy", sas_phy_setup, NULL, NULL);
695 
696 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
697 {
698 	struct Scsi_Host *shost;
699 	struct sas_internal *i;
700 
701 	if (!scsi_is_sas_phy(dev))
702 		return 0;
703 	shost = dev_to_shost(dev->parent);
704 
705 	if (!shost->transportt)
706 		return 0;
707 	if (shost->transportt->host_attrs.ac.class !=
708 			&sas_host_class.class)
709 		return 0;
710 
711 	i = to_sas_internal(shost->transportt);
712 	return &i->phy_attr_cont.ac == cont;
713 }
714 
715 static void sas_phy_release(struct device *dev)
716 {
717 	struct sas_phy *phy = dev_to_phy(dev);
718 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
719 	struct sas_internal *i = to_sas_internal(shost->transportt);
720 
721 	if (i->f->phy_release)
722 		i->f->phy_release(phy);
723 	put_device(dev->parent);
724 	kfree(phy);
725 }
726 
727 /**
728  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
729  * @parent:	Parent device
730  * @number:	Phy index
731  *
732  * Allocates an SAS PHY structure.  It will be added in the device tree
733  * below the device specified by @parent, which has to be either a Scsi_Host
734  * or sas_rphy.
735  *
736  * Returns:
737  *	SAS PHY allocated or %NULL if the allocation failed.
738  */
739 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
740 {
741 	struct Scsi_Host *shost = dev_to_shost(parent);
742 	struct sas_phy *phy;
743 
744 	phy = kzalloc_obj(*phy);
745 	if (!phy)
746 		return NULL;
747 
748 	phy->number = number;
749 	phy->enabled = 1;
750 
751 	device_initialize(&phy->dev);
752 	phy->dev.parent = get_device(parent);
753 	phy->dev.release = sas_phy_release;
754 	INIT_LIST_HEAD(&phy->port_siblings);
755 	if (scsi_is_sas_expander_device(parent)) {
756 		struct sas_rphy *rphy = dev_to_rphy(parent);
757 		dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
758 			rphy->scsi_target_id, number);
759 	} else
760 		dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
761 
762 	transport_setup_device(&phy->dev);
763 
764 	return phy;
765 }
766 EXPORT_SYMBOL(sas_phy_alloc);
767 
768 /**
769  * sas_phy_add  -  add a SAS PHY to the device hierarchy
770  * @phy:	The PHY to be added
771  *
772  * Publishes a SAS PHY to the rest of the system.
773  */
774 int sas_phy_add(struct sas_phy *phy)
775 {
776 	int error;
777 
778 	error = device_add(&phy->dev);
779 	if (error)
780 		return error;
781 
782 	error = transport_add_device(&phy->dev);
783 	if (error) {
784 		device_del(&phy->dev);
785 		return error;
786 	}
787 	transport_configure_device(&phy->dev);
788 
789 	return 0;
790 }
791 EXPORT_SYMBOL(sas_phy_add);
792 
793 /**
794  * sas_phy_free  -  free a SAS PHY
795  * @phy:	SAS PHY to free
796  *
797  * Frees the specified SAS PHY.
798  *
799  * Note:
800  *   This function must only be called on a PHY that has not
801  *   successfully been added using sas_phy_add().
802  */
803 void sas_phy_free(struct sas_phy *phy)
804 {
805 	transport_destroy_device(&phy->dev);
806 	put_device(&phy->dev);
807 }
808 EXPORT_SYMBOL(sas_phy_free);
809 
810 /**
811  * sas_phy_delete  -  remove SAS PHY
812  * @phy:	SAS PHY to remove
813  *
814  * Removes the specified SAS PHY.  If the SAS PHY has an
815  * associated remote PHY it is removed before.
816  */
817 void
818 sas_phy_delete(struct sas_phy *phy)
819 {
820 	struct device *dev = &phy->dev;
821 
822 	/* this happens if the phy is still part of a port when deleted */
823 	BUG_ON(!list_empty(&phy->port_siblings));
824 
825 	transport_remove_device(dev);
826 	device_del(dev);
827 	transport_destroy_device(dev);
828 	put_device(dev);
829 }
830 EXPORT_SYMBOL(sas_phy_delete);
831 
832 /**
833  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
834  * @dev:	device to check
835  *
836  * Returns:
837  *	%1 if the device represents a SAS PHY, %0 else
838  */
839 int scsi_is_sas_phy(const struct device *dev)
840 {
841 	return dev->release == sas_phy_release;
842 }
843 EXPORT_SYMBOL(scsi_is_sas_phy);
844 
845 /*
846  * SAS Port attributes
847  */
848 #define sas_port_show_simple(field, name, format_string, cast)		\
849 static ssize_t								\
850 show_sas_port_##name(struct device *dev, 				\
851 		     struct device_attribute *attr, char *buf)		\
852 {									\
853 	struct sas_port *port = transport_class_to_sas_port(dev);	\
854 									\
855 	return snprintf(buf, 20, format_string, cast port->field);	\
856 }
857 
858 #define sas_port_simple_attr(field, name, format_string, type)		\
859 	sas_port_show_simple(field, name, format_string, (type))	\
860 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
861 
862 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
863 
864 static DECLARE_TRANSPORT_CLASS(sas_port_class,
865 			       "sas_port", NULL, NULL, NULL);
866 
867 static int sas_port_match(struct attribute_container *cont, struct device *dev)
868 {
869 	struct Scsi_Host *shost;
870 	struct sas_internal *i;
871 
872 	if (!scsi_is_sas_port(dev))
873 		return 0;
874 	shost = dev_to_shost(dev->parent);
875 
876 	if (!shost->transportt)
877 		return 0;
878 	if (shost->transportt->host_attrs.ac.class !=
879 			&sas_host_class.class)
880 		return 0;
881 
882 	i = to_sas_internal(shost->transportt);
883 	return &i->port_attr_cont.ac == cont;
884 }
885 
886 
887 static void sas_port_release(struct device *dev)
888 {
889 	struct sas_port *port = dev_to_sas_port(dev);
890 
891 	BUG_ON(!list_empty(&port->phy_list));
892 
893 	put_device(dev->parent);
894 	kfree(port);
895 }
896 
897 static void sas_port_create_link(struct sas_port *port,
898 				 struct sas_phy *phy)
899 {
900 	int res;
901 
902 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
903 				dev_name(&phy->dev));
904 	if (res)
905 		goto err;
906 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
907 	if (res)
908 		goto err;
909 	return;
910 err:
911 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
912 	       __func__, res);
913 }
914 
915 static void sas_port_delete_link(struct sas_port *port,
916 				 struct sas_phy *phy)
917 {
918 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
919 	sysfs_remove_link(&phy->dev.kobj, "port");
920 }
921 
922 /**
923  * sas_port_alloc - allocate and initialize a SAS port structure
924  *
925  * @parent:	parent device
926  * @port_id:	port number
927  *
928  * Allocates a SAS port structure.  It will be added to the device tree
929  * below the device specified by @parent which must be either a Scsi_Host
930  * or a sas_expander_device.
931  *
932  * Returns: %NULL on error
933  */
934 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
935 {
936 	struct Scsi_Host *shost = dev_to_shost(parent);
937 	struct sas_port *port;
938 
939 	port = kzalloc_obj(*port);
940 	if (!port)
941 		return NULL;
942 
943 	port->port_identifier = port_id;
944 
945 	device_initialize(&port->dev);
946 
947 	port->dev.parent = get_device(parent);
948 	port->dev.release = sas_port_release;
949 
950 	mutex_init(&port->phy_list_mutex);
951 	INIT_LIST_HEAD(&port->phy_list);
952 
953 	if (scsi_is_sas_expander_device(parent)) {
954 		struct sas_rphy *rphy = dev_to_rphy(parent);
955 		dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
956 			     rphy->scsi_target_id, port->port_identifier);
957 	} else
958 		dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
959 			     port->port_identifier);
960 
961 	transport_setup_device(&port->dev);
962 
963 	return port;
964 }
965 EXPORT_SYMBOL(sas_port_alloc);
966 
967 /**
968  * sas_port_alloc_num - allocate and initialize a SAS port structure
969  *
970  * @parent:	parent device
971  *
972  * Allocates a SAS port structure and a number to go with it.  This
973  * interface is really for adapters where the port number has no
974  * meansing, so the sas class should manage them.  It will be added to
975  * the device tree below the device specified by @parent which must be
976  * either a Scsi_Host or a sas_expander_device.
977  *
978  * Returns: %NULL on error
979  */
980 struct sas_port *sas_port_alloc_num(struct device *parent)
981 {
982 	int index;
983 	struct Scsi_Host *shost = dev_to_shost(parent);
984 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
985 
986 	/* FIXME: use idr for this eventually */
987 	mutex_lock(&sas_host->lock);
988 	if (scsi_is_sas_expander_device(parent)) {
989 		struct sas_rphy *rphy = dev_to_rphy(parent);
990 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
991 
992 		index = exp->next_port_id++;
993 	} else
994 		index = sas_host->next_port_id++;
995 	mutex_unlock(&sas_host->lock);
996 	return sas_port_alloc(parent, index);
997 }
998 EXPORT_SYMBOL(sas_port_alloc_num);
999 
1000 /**
1001  * sas_port_add - add a SAS port to the device hierarchy
1002  * @port:	port to be added
1003  *
1004  * publishes a port to the rest of the system
1005  */
1006 int sas_port_add(struct sas_port *port)
1007 {
1008 	int error;
1009 
1010 	/* No phys should be added until this is made visible */
1011 	BUG_ON(!list_empty(&port->phy_list));
1012 
1013 	error = device_add(&port->dev);
1014 
1015 	if (error)
1016 		return error;
1017 
1018 	transport_add_device(&port->dev);
1019 	transport_configure_device(&port->dev);
1020 
1021 	return 0;
1022 }
1023 EXPORT_SYMBOL(sas_port_add);
1024 
1025 /**
1026  * sas_port_free  -  free a SAS PORT
1027  * @port:	SAS PORT to free
1028  *
1029  * Frees the specified SAS PORT.
1030  *
1031  * Note:
1032  *   This function must only be called on a PORT that has not
1033  *   successfully been added using sas_port_add().
1034  */
1035 void sas_port_free(struct sas_port *port)
1036 {
1037 	transport_destroy_device(&port->dev);
1038 	put_device(&port->dev);
1039 }
1040 EXPORT_SYMBOL(sas_port_free);
1041 
1042 /**
1043  * sas_port_delete  -  remove SAS PORT
1044  * @port:	SAS PORT to remove
1045  *
1046  * Removes the specified SAS PORT.  If the SAS PORT has an
1047  * associated phys, unlink them from the port as well.
1048  */
1049 void sas_port_delete(struct sas_port *port)
1050 {
1051 	struct device *dev = &port->dev;
1052 	struct sas_phy *phy, *tmp_phy;
1053 
1054 	if (port->rphy) {
1055 		sas_rphy_delete(port->rphy);
1056 		port->rphy = NULL;
1057 	}
1058 
1059 	mutex_lock(&port->phy_list_mutex);
1060 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1061 				 port_siblings) {
1062 		sas_port_delete_link(port, phy);
1063 		list_del_init(&phy->port_siblings);
1064 	}
1065 	mutex_unlock(&port->phy_list_mutex);
1066 
1067 	if (port->is_backlink) {
1068 		struct device *parent = port->dev.parent;
1069 
1070 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1071 		port->is_backlink = 0;
1072 	}
1073 
1074 	transport_remove_device(dev);
1075 	device_del(dev);
1076 	transport_destroy_device(dev);
1077 	put_device(dev);
1078 }
1079 EXPORT_SYMBOL(sas_port_delete);
1080 
1081 /**
1082  * scsi_is_sas_port -  check if a struct device represents a SAS port
1083  * @dev:	device to check
1084  *
1085  * Returns:
1086  *	%1 if the device represents a SAS Port, %0 else
1087  */
1088 int scsi_is_sas_port(const struct device *dev)
1089 {
1090 	return dev->release == sas_port_release;
1091 }
1092 EXPORT_SYMBOL(scsi_is_sas_port);
1093 
1094 /**
1095  * sas_port_get_phy - try to take a reference on a port member
1096  * @port: port to check
1097  */
1098 struct sas_phy *sas_port_get_phy(struct sas_port *port)
1099 {
1100 	struct sas_phy *phy;
1101 
1102 	mutex_lock(&port->phy_list_mutex);
1103 	if (list_empty(&port->phy_list))
1104 		phy = NULL;
1105 	else {
1106 		struct list_head *ent = port->phy_list.next;
1107 
1108 		phy = list_entry(ent, typeof(*phy), port_siblings);
1109 		get_device(&phy->dev);
1110 	}
1111 	mutex_unlock(&port->phy_list_mutex);
1112 
1113 	return phy;
1114 }
1115 EXPORT_SYMBOL(sas_port_get_phy);
1116 
1117 /**
1118  * sas_port_add_phy - add another phy to a port to form a wide port
1119  * @port:	port to add the phy to
1120  * @phy:	phy to add
1121  *
1122  * When a port is initially created, it is empty (has no phys).  All
1123  * ports must have at least one phy to operated, and all wide ports
1124  * must have at least two.  The current code makes no difference
1125  * between ports and wide ports, but the only object that can be
1126  * connected to a remote device is a port, so ports must be formed on
1127  * all devices with phys if they're connected to anything.
1128  */
1129 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1130 {
1131 	mutex_lock(&port->phy_list_mutex);
1132 	if (unlikely(!list_empty(&phy->port_siblings))) {
1133 		/* make sure we're already on this port */
1134 		struct sas_phy *tmp;
1135 
1136 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
1137 			if (tmp == phy)
1138 				break;
1139 		/* If this trips, you added a phy that was already
1140 		 * part of a different port */
1141 		if (unlikely(tmp != phy)) {
1142 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1143 				   dev_name(&phy->dev));
1144 			BUG();
1145 		}
1146 	} else {
1147 		sas_port_create_link(port, phy);
1148 		list_add_tail(&phy->port_siblings, &port->phy_list);
1149 		port->num_phys++;
1150 	}
1151 	mutex_unlock(&port->phy_list_mutex);
1152 }
1153 EXPORT_SYMBOL(sas_port_add_phy);
1154 
1155 /**
1156  * sas_port_delete_phy - remove a phy from a port or wide port
1157  * @port:	port to remove the phy from
1158  * @phy:	phy to remove
1159  *
1160  * This operation is used for tearing down ports again.  It must be
1161  * done to every port or wide port before calling sas_port_delete.
1162  */
1163 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1164 {
1165 	mutex_lock(&port->phy_list_mutex);
1166 	sas_port_delete_link(port, phy);
1167 	list_del_init(&phy->port_siblings);
1168 	port->num_phys--;
1169 	mutex_unlock(&port->phy_list_mutex);
1170 }
1171 EXPORT_SYMBOL(sas_port_delete_phy);
1172 
1173 void sas_port_mark_backlink(struct sas_port *port)
1174 {
1175 	int res;
1176 	struct device *parent = port->dev.parent->parent->parent;
1177 
1178 	if (port->is_backlink)
1179 		return;
1180 	port->is_backlink = 1;
1181 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1182 				dev_name(parent));
1183 	if (res)
1184 		goto err;
1185 	return;
1186 err:
1187 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1188 	       __func__, res);
1189 
1190 }
1191 EXPORT_SYMBOL(sas_port_mark_backlink);
1192 
1193 /*
1194  * SAS remote PHY attributes.
1195  */
1196 
1197 #define sas_rphy_show_simple(field, name, format_string, cast)		\
1198 static ssize_t								\
1199 show_sas_rphy_##name(struct device *dev, 				\
1200 		     struct device_attribute *attr, char *buf)		\
1201 {									\
1202 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1203 									\
1204 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1205 }
1206 
1207 #define sas_rphy_simple_attr(field, name, format_string, type)		\
1208 	sas_rphy_show_simple(field, name, format_string, (type))	\
1209 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1210 		show_sas_rphy_##name, NULL)
1211 
1212 #define sas_rphy_show_protocol(field, name)				\
1213 static ssize_t								\
1214 show_sas_rphy_##name(struct device *dev, 				\
1215 		     struct device_attribute *attr, char *buf)		\
1216 {									\
1217 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1218 									\
1219 	if (!rphy->field)					\
1220 		return snprintf(buf, 20, "none\n");			\
1221 	return get_sas_protocol_names(rphy->field, buf);	\
1222 }
1223 
1224 #define sas_rphy_protocol_attr(field, name)				\
1225 	sas_rphy_show_protocol(field, name)				\
1226 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1227 		show_sas_rphy_##name, NULL)
1228 
1229 static ssize_t
1230 show_sas_rphy_device_type(struct device *dev,
1231 			  struct device_attribute *attr, char *buf)
1232 {
1233 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1234 
1235 	if (!rphy->identify.device_type)
1236 		return snprintf(buf, 20, "none\n");
1237 	return get_sas_device_type_names(
1238 			rphy->identify.device_type, buf);
1239 }
1240 
1241 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1242 		show_sas_rphy_device_type, NULL);
1243 
1244 static ssize_t
1245 show_sas_rphy_enclosure_identifier(struct device *dev,
1246 				   struct device_attribute *attr, char *buf)
1247 {
1248 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1249 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1250 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1251 	struct sas_internal *i = to_sas_internal(shost->transportt);
1252 	u64 identifier;
1253 	int error;
1254 
1255 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1256 	if (error)
1257 		return error;
1258 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1259 }
1260 
1261 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1262 		show_sas_rphy_enclosure_identifier, NULL);
1263 
1264 static ssize_t
1265 show_sas_rphy_bay_identifier(struct device *dev,
1266 			     struct device_attribute *attr, char *buf)
1267 {
1268 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1269 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1270 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1271 	struct sas_internal *i = to_sas_internal(shost->transportt);
1272 	int val;
1273 
1274 	val = i->f->get_bay_identifier(rphy);
1275 	if (val < 0)
1276 		return val;
1277 	return sprintf(buf, "%d\n", val);
1278 }
1279 
1280 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1281 		show_sas_rphy_bay_identifier, NULL);
1282 
1283 sas_rphy_protocol_attr(identify.initiator_port_protocols,
1284 		initiator_port_protocols);
1285 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1286 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1287 		unsigned long long);
1288 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1289 sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
1290 
1291 /* only need 8 bytes of data plus header (4 or 8) */
1292 #define BUF_SIZE 64
1293 
1294 int sas_read_port_mode_page(struct scsi_device *sdev)
1295 {
1296 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1297 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
1298 	struct scsi_mode_data mode_data;
1299 	int error;
1300 
1301 	if (!buffer)
1302 		return -ENOMEM;
1303 
1304 	error = scsi_mode_sense(sdev, 1, 0x19, 0, buffer, BUF_SIZE, 30*HZ, 3,
1305 				&mode_data, NULL);
1306 
1307 	if (error)
1308 		goto out;
1309 
1310 	msdata = buffer +  mode_data.header_length +
1311 		mode_data.block_descriptor_length;
1312 
1313 	if (msdata - buffer > BUF_SIZE - 8)
1314 		goto out;
1315 
1316 	error = 0;
1317 
1318 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1319 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1320 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1321 
1322  out:
1323 	kfree(buffer);
1324 	return error;
1325 }
1326 EXPORT_SYMBOL(sas_read_port_mode_page);
1327 
1328 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1329 			       "sas_end_device", NULL, NULL, NULL);
1330 
1331 #define sas_end_dev_show_simple(field, name, format_string, cast)	\
1332 static ssize_t								\
1333 show_sas_end_dev_##name(struct device *dev, 				\
1334 			struct device_attribute *attr, char *buf)	\
1335 {									\
1336 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1337 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
1338 									\
1339 	return snprintf(buf, 20, format_string, cast rdev->field);	\
1340 }
1341 
1342 #define sas_end_dev_simple_attr(field, name, format_string, type)	\
1343 	sas_end_dev_show_simple(field, name, format_string, (type))	\
1344 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
1345 		show_sas_end_dev_##name, NULL)
1346 
1347 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1348 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1349 			"%d\n", int);
1350 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1351 			"%d\n", int);
1352 sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1353 			"%d\n", int);
1354 sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1355 			"%d\n", int);
1356 
1357 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1358 			       "sas_expander", NULL, NULL, NULL);
1359 
1360 #define sas_expander_show_simple(field, name, format_string, cast)	\
1361 static ssize_t								\
1362 show_sas_expander_##name(struct device *dev, 				\
1363 			 struct device_attribute *attr, char *buf)	\
1364 {									\
1365 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1366 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1367 									\
1368 	return snprintf(buf, 20, format_string, cast edev->field);	\
1369 }
1370 
1371 #define sas_expander_simple_attr(field, name, format_string, type)	\
1372 	sas_expander_show_simple(field, name, format_string, (type))	\
1373 static SAS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
1374 		show_sas_expander_##name, NULL)
1375 
1376 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1377 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1378 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1379 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1380 			 "%s\n", char *);
1381 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1382 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1383 			 unsigned int);
1384 sas_expander_simple_attr(level, level, "%d\n", int);
1385 
1386 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1387 		"sas_device", NULL, NULL, NULL);
1388 
1389 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1390 {
1391 	struct Scsi_Host *shost;
1392 	struct sas_internal *i;
1393 
1394 	if (!scsi_is_sas_rphy(dev))
1395 		return 0;
1396 	shost = dev_to_shost(dev->parent->parent);
1397 
1398 	if (!shost->transportt)
1399 		return 0;
1400 	if (shost->transportt->host_attrs.ac.class !=
1401 			&sas_host_class.class)
1402 		return 0;
1403 
1404 	i = to_sas_internal(shost->transportt);
1405 	return &i->rphy_attr_cont.ac == cont;
1406 }
1407 
1408 static int sas_end_dev_match(struct attribute_container *cont,
1409 			     struct device *dev)
1410 {
1411 	struct Scsi_Host *shost;
1412 	struct sas_internal *i;
1413 	struct sas_rphy *rphy;
1414 
1415 	if (!scsi_is_sas_rphy(dev))
1416 		return 0;
1417 	shost = dev_to_shost(dev->parent->parent);
1418 	rphy = dev_to_rphy(dev);
1419 
1420 	if (!shost->transportt)
1421 		return 0;
1422 	if (shost->transportt->host_attrs.ac.class !=
1423 			&sas_host_class.class)
1424 		return 0;
1425 
1426 	i = to_sas_internal(shost->transportt);
1427 	return &i->end_dev_attr_cont.ac == cont &&
1428 		rphy->identify.device_type == SAS_END_DEVICE;
1429 }
1430 
1431 static int sas_expander_match(struct attribute_container *cont,
1432 			      struct device *dev)
1433 {
1434 	struct Scsi_Host *shost;
1435 	struct sas_internal *i;
1436 	struct sas_rphy *rphy;
1437 
1438 	if (!scsi_is_sas_rphy(dev))
1439 		return 0;
1440 	shost = dev_to_shost(dev->parent->parent);
1441 	rphy = dev_to_rphy(dev);
1442 
1443 	if (!shost->transportt)
1444 		return 0;
1445 	if (shost->transportt->host_attrs.ac.class !=
1446 			&sas_host_class.class)
1447 		return 0;
1448 
1449 	i = to_sas_internal(shost->transportt);
1450 	return &i->expander_attr_cont.ac == cont &&
1451 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1452 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1453 }
1454 
1455 static void sas_expander_release(struct device *dev)
1456 {
1457 	struct sas_rphy *rphy = dev_to_rphy(dev);
1458 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1459 
1460 	put_device(dev->parent);
1461 	kfree(edev);
1462 }
1463 
1464 static void sas_end_device_release(struct device *dev)
1465 {
1466 	struct sas_rphy *rphy = dev_to_rphy(dev);
1467 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1468 
1469 	put_device(dev->parent);
1470 	kfree(edev);
1471 }
1472 
1473 /**
1474  * sas_rphy_initialize - common rphy initialization
1475  * @rphy:	rphy to initialise
1476  *
1477  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1478  * initialise the common rphy component of each.
1479  */
1480 static void sas_rphy_initialize(struct sas_rphy *rphy)
1481 {
1482 	INIT_LIST_HEAD(&rphy->list);
1483 }
1484 
1485 /**
1486  * sas_end_device_alloc - allocate an rphy for an end device
1487  * @parent: which port
1488  *
1489  * Allocates an SAS remote PHY structure, connected to @parent.
1490  *
1491  * Returns:
1492  *	SAS PHY allocated or %NULL if the allocation failed.
1493  */
1494 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1495 {
1496 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1497 	struct sas_end_device *rdev;
1498 
1499 	rdev = kzalloc_obj(*rdev);
1500 	if (!rdev) {
1501 		return NULL;
1502 	}
1503 
1504 	device_initialize(&rdev->rphy.dev);
1505 	rdev->rphy.dev.parent = get_device(&parent->dev);
1506 	rdev->rphy.dev.release = sas_end_device_release;
1507 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
1508 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1509 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1510 			     shost->host_no, rphy->scsi_target_id,
1511 			     parent->port_identifier);
1512 	} else
1513 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1514 			     shost->host_no, parent->port_identifier);
1515 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1516 	sas_rphy_initialize(&rdev->rphy);
1517 	transport_setup_device(&rdev->rphy.dev);
1518 
1519 	return &rdev->rphy;
1520 }
1521 EXPORT_SYMBOL(sas_end_device_alloc);
1522 
1523 /**
1524  * sas_expander_alloc - allocate an rphy for an end device
1525  * @parent: which port
1526  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
1527  *
1528  * Allocates an SAS remote PHY structure, connected to @parent.
1529  *
1530  * Returns:
1531  *	SAS PHY allocated or %NULL if the allocation failed.
1532  */
1533 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1534 				    enum sas_device_type type)
1535 {
1536 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1537 	struct sas_expander_device *rdev;
1538 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1539 
1540 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1541 	       type != SAS_FANOUT_EXPANDER_DEVICE);
1542 
1543 	rdev = kzalloc_obj(*rdev);
1544 	if (!rdev) {
1545 		return NULL;
1546 	}
1547 
1548 	device_initialize(&rdev->rphy.dev);
1549 	rdev->rphy.dev.parent = get_device(&parent->dev);
1550 	rdev->rphy.dev.release = sas_expander_release;
1551 	mutex_lock(&sas_host->lock);
1552 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1553 	mutex_unlock(&sas_host->lock);
1554 	dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1555 		     shost->host_no, rdev->rphy.scsi_target_id);
1556 	rdev->rphy.identify.device_type = type;
1557 	sas_rphy_initialize(&rdev->rphy);
1558 	transport_setup_device(&rdev->rphy.dev);
1559 
1560 	return &rdev->rphy;
1561 }
1562 EXPORT_SYMBOL(sas_expander_alloc);
1563 
1564 /**
1565  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1566  * @rphy:	The remote PHY to be added
1567  *
1568  * Publishes a SAS remote PHY to the rest of the system.
1569  */
1570 int sas_rphy_add(struct sas_rphy *rphy)
1571 {
1572 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1573 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1574 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1575 	struct sas_identify *identify = &rphy->identify;
1576 	int error;
1577 
1578 	if (parent->rphy)
1579 		return -ENXIO;
1580 	parent->rphy = rphy;
1581 
1582 	error = device_add(&rphy->dev);
1583 	if (error)
1584 		return error;
1585 	transport_add_device(&rphy->dev);
1586 	transport_configure_device(&rphy->dev);
1587 	if (sas_bsg_initialize(shost, rphy))
1588 		printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
1589 
1590 
1591 	mutex_lock(&sas_host->lock);
1592 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1593 	if (identify->device_type == SAS_END_DEVICE &&
1594 	    (identify->target_port_protocols &
1595 	     (SAS_PROTOCOL_SSP | SAS_PROTOCOL_STP | SAS_PROTOCOL_SATA)))
1596 		rphy->scsi_target_id = sas_host->next_target_id++;
1597 	else if (identify->device_type == SAS_END_DEVICE)
1598 		rphy->scsi_target_id = -1;
1599 	mutex_unlock(&sas_host->lock);
1600 
1601 	if (identify->device_type == SAS_END_DEVICE &&
1602 	    rphy->scsi_target_id != -1) {
1603 		int lun;
1604 
1605 		if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
1606 			lun = SCAN_WILD_CARD;
1607 		else
1608 			lun = 0;
1609 
1610 		scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
1611 				 SCSI_SCAN_INITIAL);
1612 	}
1613 
1614 	return 0;
1615 }
1616 EXPORT_SYMBOL(sas_rphy_add);
1617 
1618 /**
1619  * sas_rphy_free  -  free a SAS remote PHY
1620  * @rphy: SAS remote PHY to free
1621  *
1622  * Frees the specified SAS remote PHY.
1623  *
1624  * Note:
1625  *   This function must only be called on a remote
1626  *   PHY that has not successfully been added using
1627  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1628  */
1629 void sas_rphy_free(struct sas_rphy *rphy)
1630 {
1631 	struct device *dev = &rphy->dev;
1632 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1633 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1634 
1635 	mutex_lock(&sas_host->lock);
1636 	list_del(&rphy->list);
1637 	mutex_unlock(&sas_host->lock);
1638 
1639 	transport_destroy_device(dev);
1640 
1641 	put_device(dev);
1642 }
1643 EXPORT_SYMBOL(sas_rphy_free);
1644 
1645 /**
1646  * sas_rphy_delete  -  remove and free SAS remote PHY
1647  * @rphy:	SAS remote PHY to remove and free
1648  *
1649  * Removes the specified SAS remote PHY and frees it.
1650  */
1651 void
1652 sas_rphy_delete(struct sas_rphy *rphy)
1653 {
1654 	sas_rphy_remove(rphy);
1655 	sas_rphy_free(rphy);
1656 }
1657 EXPORT_SYMBOL(sas_rphy_delete);
1658 
1659 /**
1660  * sas_rphy_unlink  -  unlink SAS remote PHY
1661  * @rphy:	SAS remote phy to unlink from its parent port
1662  *
1663  * Removes port reference to an rphy
1664  */
1665 void sas_rphy_unlink(struct sas_rphy *rphy)
1666 {
1667 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1668 
1669 	parent->rphy = NULL;
1670 }
1671 EXPORT_SYMBOL(sas_rphy_unlink);
1672 
1673 /**
1674  * sas_rphy_remove  -  remove SAS remote PHY
1675  * @rphy:	SAS remote phy to remove
1676  *
1677  * Removes the specified SAS remote PHY.
1678  */
1679 void
1680 sas_rphy_remove(struct sas_rphy *rphy)
1681 {
1682 	struct device *dev = &rphy->dev;
1683 
1684 	switch (rphy->identify.device_type) {
1685 	case SAS_END_DEVICE:
1686 		scsi_remove_target(dev);
1687 		break;
1688 	case SAS_EDGE_EXPANDER_DEVICE:
1689 	case SAS_FANOUT_EXPANDER_DEVICE:
1690 		sas_remove_children(dev);
1691 		break;
1692 	default:
1693 		break;
1694 	}
1695 
1696 	sas_rphy_unlink(rphy);
1697 	bsg_remove_queue(rphy->q);
1698 	transport_remove_device(dev);
1699 	device_del(dev);
1700 }
1701 EXPORT_SYMBOL(sas_rphy_remove);
1702 
1703 /**
1704  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1705  * @dev:	device to check
1706  *
1707  * Returns:
1708  *	%1 if the device represents a SAS remote PHY, %0 else
1709  */
1710 int scsi_is_sas_rphy(const struct device *dev)
1711 {
1712 	return dev->release == sas_end_device_release ||
1713 		dev->release == sas_expander_release;
1714 }
1715 EXPORT_SYMBOL(scsi_is_sas_rphy);
1716 
1717 
1718 /*
1719  * SCSI scan helper
1720  */
1721 
1722 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1723 		uint id, u64 lun)
1724 {
1725 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1726 	struct sas_rphy *rphy;
1727 
1728 	mutex_lock(&sas_host->lock);
1729 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1730 		if (rphy->identify.device_type != SAS_END_DEVICE ||
1731 		    rphy->scsi_target_id == -1)
1732 			continue;
1733 
1734 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1735 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1736 			scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
1737 					 lun, SCSI_SCAN_MANUAL);
1738 		}
1739 	}
1740 	mutex_unlock(&sas_host->lock);
1741 
1742 	return 0;
1743 }
1744 
1745 
1746 /*
1747  * Setup / Teardown code
1748  */
1749 
1750 #define SETUP_TEMPLATE(attrb, field, perm, test)			\
1751 	i->private_##attrb[count] = dev_attr_##field;		\
1752 	i->private_##attrb[count].attr.mode = perm;			\
1753 	i->attrb[count] = &i->private_##attrb[count];			\
1754 	if (test)							\
1755 		count++
1756 
1757 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1758 	i->private_##attrb[count] = dev_attr_##field;		\
1759 	i->private_##attrb[count].attr.mode = perm;			\
1760 	if (ro_test) {							\
1761 		i->private_##attrb[count].attr.mode = ro_perm;		\
1762 		i->private_##attrb[count].store = NULL;			\
1763 	}								\
1764 	i->attrb[count] = &i->private_##attrb[count];			\
1765 	if (test)							\
1766 		count++
1767 
1768 #define SETUP_RPORT_ATTRIBUTE(field) 					\
1769 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1770 
1771 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
1772 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1773 
1774 #define SETUP_PHY_ATTRIBUTE(field)					\
1775 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1776 
1777 #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1778 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1779 			!i->f->set_phy_speed, S_IRUGO)
1780 
1781 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1782 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1783 			  !i->f->func, S_IRUGO)
1784 
1785 #define SETUP_PORT_ATTRIBUTE(field)					\
1786 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1787 
1788 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
1789 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1790 
1791 #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1792 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1793 
1794 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1795 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1796 
1797 #define SETUP_END_DEV_ATTRIBUTE(field)					\
1798 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1799 
1800 #define SETUP_EXPANDER_ATTRIBUTE(field)					\
1801 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1802 
1803 /**
1804  * sas_attach_transport  -  instantiate SAS transport template
1805  * @ft:		SAS transport class function template
1806  */
1807 struct scsi_transport_template *
1808 sas_attach_transport(struct sas_function_template *ft)
1809 {
1810 	struct sas_internal *i;
1811 	int count;
1812 
1813 	i = kzalloc_obj(struct sas_internal);
1814 	if (!i)
1815 		return NULL;
1816 
1817 	i->t.user_scan = sas_user_scan;
1818 
1819 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1820 	i->t.host_attrs.ac.class = &sas_host_class.class;
1821 	i->t.host_attrs.ac.match = sas_host_match;
1822 	transport_container_register(&i->t.host_attrs);
1823 	i->t.host_size = sizeof(struct sas_host_attrs);
1824 
1825 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1826 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1827 	i->phy_attr_cont.ac.match = sas_phy_match;
1828 	transport_container_register(&i->phy_attr_cont);
1829 
1830 	i->port_attr_cont.ac.class = &sas_port_class.class;
1831 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1832 	i->port_attr_cont.ac.match = sas_port_match;
1833 	transport_container_register(&i->port_attr_cont);
1834 
1835 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1836 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1837 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1838 	transport_container_register(&i->rphy_attr_cont);
1839 
1840 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1841 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1842 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1843 	transport_container_register(&i->end_dev_attr_cont);
1844 
1845 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
1846 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1847 	i->expander_attr_cont.ac.match = sas_expander_match;
1848 	transport_container_register(&i->expander_attr_cont);
1849 
1850 	i->f = ft;
1851 
1852 	count = 0;
1853 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1854 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
1855 	SETUP_PHY_ATTRIBUTE(device_type);
1856 	SETUP_PHY_ATTRIBUTE(sas_address);
1857 	SETUP_PHY_ATTRIBUTE(phy_identifier);
1858 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1859 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1860 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
1861 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1862 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1863 
1864 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1865 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1866 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1867 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1868 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1869 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1870 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1871 	i->phy_attrs[count] = NULL;
1872 
1873 	count = 0;
1874 	SETUP_PORT_ATTRIBUTE(num_phys);
1875 	i->port_attrs[count] = NULL;
1876 
1877 	count = 0;
1878 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1879 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1880 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1881 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1882 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1883 	SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
1884 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1885 				       get_enclosure_identifier);
1886 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1887 				       get_bay_identifier);
1888 	i->rphy_attrs[count] = NULL;
1889 
1890 	count = 0;
1891 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1892 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1893 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1894 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1895 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
1896 	i->end_dev_attrs[count] = NULL;
1897 
1898 	count = 0;
1899 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1900 	SETUP_EXPANDER_ATTRIBUTE(product_id);
1901 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
1902 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1903 	SETUP_EXPANDER_ATTRIBUTE(component_id);
1904 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1905 	SETUP_EXPANDER_ATTRIBUTE(level);
1906 	i->expander_attrs[count] = NULL;
1907 
1908 	return &i->t;
1909 }
1910 EXPORT_SYMBOL(sas_attach_transport);
1911 
1912 /**
1913  * sas_release_transport  -  release SAS transport template instance
1914  * @t:		transport template instance
1915  */
1916 void sas_release_transport(struct scsi_transport_template *t)
1917 {
1918 	struct sas_internal *i = to_sas_internal(t);
1919 
1920 	transport_container_unregister(&i->t.host_attrs);
1921 	transport_container_unregister(&i->phy_attr_cont);
1922 	transport_container_unregister(&i->port_attr_cont);
1923 	transport_container_unregister(&i->rphy_attr_cont);
1924 	transport_container_unregister(&i->end_dev_attr_cont);
1925 	transport_container_unregister(&i->expander_attr_cont);
1926 
1927 	kfree(i);
1928 }
1929 EXPORT_SYMBOL(sas_release_transport);
1930 
1931 static __init int sas_transport_init(void)
1932 {
1933 	int error;
1934 
1935 	error = transport_class_register(&sas_host_class);
1936 	if (error)
1937 		goto out;
1938 	error = transport_class_register(&sas_phy_class);
1939 	if (error)
1940 		goto out_unregister_transport;
1941 	error = transport_class_register(&sas_port_class);
1942 	if (error)
1943 		goto out_unregister_phy;
1944 	error = transport_class_register(&sas_rphy_class);
1945 	if (error)
1946 		goto out_unregister_port;
1947 	error = transport_class_register(&sas_end_dev_class);
1948 	if (error)
1949 		goto out_unregister_rphy;
1950 	error = transport_class_register(&sas_expander_class);
1951 	if (error)
1952 		goto out_unregister_end_dev;
1953 
1954 	return 0;
1955 
1956  out_unregister_end_dev:
1957 	transport_class_unregister(&sas_end_dev_class);
1958  out_unregister_rphy:
1959 	transport_class_unregister(&sas_rphy_class);
1960  out_unregister_port:
1961 	transport_class_unregister(&sas_port_class);
1962  out_unregister_phy:
1963 	transport_class_unregister(&sas_phy_class);
1964  out_unregister_transport:
1965 	transport_class_unregister(&sas_host_class);
1966  out:
1967 	return error;
1968 
1969 }
1970 
1971 static void __exit sas_transport_exit(void)
1972 {
1973 	transport_class_unregister(&sas_host_class);
1974 	transport_class_unregister(&sas_phy_class);
1975 	transport_class_unregister(&sas_port_class);
1976 	transport_class_unregister(&sas_rphy_class);
1977 	transport_class_unregister(&sas_end_dev_class);
1978 	transport_class_unregister(&sas_expander_class);
1979 }
1980 
1981 MODULE_AUTHOR("Christoph Hellwig");
1982 MODULE_DESCRIPTION("SAS Transport Attributes");
1983 MODULE_LICENSE("GPL");
1984 
1985 module_init(sas_transport_init);
1986 module_exit(sas_transport_exit);
1987