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