xref: /linux/drivers/scsi/scsi_transport_sas.c (revision 858259cf7d1c443c836a2022b78cb281f0a9b95e)
1 /*
2  * Copyright (C) 2005 Dell Inc.
3  *	Released under GPL v2.
4  *
5  * Serial Attached SCSI (SAS) transport class.
6  *
7  * The SAS transport class contains common code to deal with SAS HBAs,
8  * an aproximated representation of SAS topologies in the driver model,
9  * and various sysfs attributes to expose these topologies and managment
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/err.h>
29 
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_sas.h>
34 
35 
36 #define SAS_HOST_ATTRS		0
37 #define SAS_PORT_ATTRS		17
38 #define SAS_RPORT_ATTRS		5
39 
40 struct sas_internal {
41 	struct scsi_transport_template t;
42 	struct sas_function_template *f;
43 
44 	struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
45 	struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
46 	struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
47 
48 	struct transport_container phy_attr_cont;
49 	struct transport_container rphy_attr_cont;
50 
51 	/*
52 	 * The array of null terminated pointers to attributes
53 	 * needed by scsi_sysfs.c
54 	 */
55 	struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
56 	struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
57 	struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
58 };
59 #define to_sas_internal(tmpl)	container_of(tmpl, struct sas_internal, t)
60 
61 struct sas_host_attrs {
62 	struct list_head rphy_list;
63 	spinlock_t lock;
64 	u32 next_target_id;
65 };
66 #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
67 
68 
69 /*
70  * Hack to allow attributes of the same name in different objects.
71  */
72 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
73 	struct class_device_attribute class_device_attr_##_prefix##_##_name = \
74 	__ATTR(_name,_mode,_show,_store)
75 
76 
77 /*
78  * Pretty printing helpers
79  */
80 
81 #define sas_bitfield_name_match(title, table)			\
82 static ssize_t							\
83 get_sas_##title##_names(u32 table_key, char *buf)		\
84 {								\
85 	char *prefix = "";					\
86 	ssize_t len = 0;					\
87 	int i;							\
88 								\
89 	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {	\
90 		if (table[i].value & table_key) {		\
91 			len += sprintf(buf + len, "%s%s",	\
92 				prefix, table[i].name);		\
93 			prefix = ", ";				\
94 		}						\
95 	}							\
96 	len += sprintf(buf + len, "\n");			\
97 	return len;						\
98 }
99 
100 #define sas_bitfield_name_search(title, table)			\
101 static ssize_t							\
102 get_sas_##title##_names(u32 table_key, char *buf)		\
103 {								\
104 	ssize_t len = 0;					\
105 	int i;							\
106 								\
107 	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {	\
108 		if (table[i].value == table_key) {		\
109 			len += sprintf(buf + len, "%s",		\
110 				table[i].name);			\
111 			break;					\
112 		}						\
113 	}							\
114 	len += sprintf(buf + len, "\n");			\
115 	return len;						\
116 }
117 
118 static struct {
119 	u32		value;
120 	char		*name;
121 } sas_device_type_names[] = {
122 	{ SAS_PHY_UNUSED,		"unused" },
123 	{ SAS_END_DEVICE,		"end device" },
124 	{ SAS_EDGE_EXPANDER_DEVICE,	"edge expander" },
125 	{ SAS_FANOUT_EXPANDER_DEVICE,	"fanout expander" },
126 };
127 sas_bitfield_name_search(device_type, sas_device_type_names)
128 
129 
130 static struct {
131 	u32		value;
132 	char		*name;
133 } sas_protocol_names[] = {
134 	{ SAS_PROTOCOL_SATA,		"sata" },
135 	{ SAS_PROTOCOL_SMP,		"smp" },
136 	{ SAS_PROTOCOL_STP,		"stp" },
137 	{ SAS_PROTOCOL_SSP,		"ssp" },
138 };
139 sas_bitfield_name_match(protocol, sas_protocol_names)
140 
141 static struct {
142 	u32		value;
143 	char		*name;
144 } sas_linkspeed_names[] = {
145 	{ SAS_LINK_RATE_UNKNOWN,	"Unknown" },
146 	{ SAS_PHY_DISABLED,		"Phy disabled" },
147 	{ SAS_LINK_RATE_FAILED,		"Link Rate failed" },
148 	{ SAS_SATA_SPINUP_HOLD,		"Spin-up hold" },
149 	{ SAS_LINK_RATE_1_5_GBPS,	"1.5 Gbit" },
150 	{ SAS_LINK_RATE_3_0_GBPS,	"3.0 Gbit" },
151 };
152 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
153 
154 
155 /*
156  * SAS host attributes
157  */
158 
159 static int sas_host_setup(struct transport_container *tc, struct device *dev,
160 			  struct class_device *cdev)
161 {
162 	struct Scsi_Host *shost = dev_to_shost(dev);
163 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
164 
165 	INIT_LIST_HEAD(&sas_host->rphy_list);
166 	spin_lock_init(&sas_host->lock);
167 	sas_host->next_target_id = 0;
168 	return 0;
169 }
170 
171 static DECLARE_TRANSPORT_CLASS(sas_host_class,
172 		"sas_host", sas_host_setup, NULL, NULL);
173 
174 static int sas_host_match(struct attribute_container *cont,
175 			    struct device *dev)
176 {
177 	struct Scsi_Host *shost;
178 	struct sas_internal *i;
179 
180 	if (!scsi_is_host_device(dev))
181 		return 0;
182 	shost = dev_to_shost(dev);
183 
184 	if (!shost->transportt)
185 		return 0;
186 	if (shost->transportt->host_attrs.ac.class !=
187 			&sas_host_class.class)
188 		return 0;
189 
190 	i = to_sas_internal(shost->transportt);
191 	return &i->t.host_attrs.ac == cont;
192 }
193 
194 static int do_sas_phy_delete(struct device *dev, void *data)
195 {
196 	if (scsi_is_sas_phy(dev))
197 		sas_phy_delete(dev_to_phy(dev));
198 	return 0;
199 }
200 
201 /**
202  * sas_remove_host  --  tear down a Scsi_Host's SAS data structures
203  * @shost:	Scsi Host that is torn down
204  *
205  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
206  * Must be called just before scsi_remove_host for SAS HBAs.
207  */
208 void sas_remove_host(struct Scsi_Host *shost)
209 {
210 	device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
211 }
212 EXPORT_SYMBOL(sas_remove_host);
213 
214 
215 /*
216  * SAS Port attributes
217  */
218 
219 #define sas_phy_show_simple(field, name, format_string, cast)		\
220 static ssize_t								\
221 show_sas_phy_##name(struct class_device *cdev, char *buf)		\
222 {									\
223 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
224 									\
225 	return snprintf(buf, 20, format_string, cast phy->field);	\
226 }
227 
228 #define sas_phy_simple_attr(field, name, format_string, type)		\
229 	sas_phy_show_simple(field, name, format_string, (type))	\
230 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
231 
232 #define sas_phy_show_protocol(field, name)				\
233 static ssize_t								\
234 show_sas_phy_##name(struct class_device *cdev, char *buf)		\
235 {									\
236 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
237 									\
238 	if (!phy->field)						\
239 		return snprintf(buf, 20, "none\n");			\
240 	return get_sas_protocol_names(phy->field, buf);		\
241 }
242 
243 #define sas_phy_protocol_attr(field, name)				\
244 	sas_phy_show_protocol(field, name)				\
245 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
246 
247 #define sas_phy_show_linkspeed(field)					\
248 static ssize_t								\
249 show_sas_phy_##field(struct class_device *cdev, char *buf)		\
250 {									\
251 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
252 									\
253 	return get_sas_linkspeed_names(phy->field, buf);		\
254 }
255 
256 #define sas_phy_linkspeed_attr(field)					\
257 	sas_phy_show_linkspeed(field)					\
258 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
259 
260 #define sas_phy_show_linkerror(field)					\
261 static ssize_t								\
262 show_sas_phy_##field(struct class_device *cdev, char *buf)		\
263 {									\
264 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
265 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
266 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
267 	int error;							\
268 									\
269 	if (!phy->local_attached)					\
270 		return -EINVAL;						\
271 									\
272 	error = i->f->get_linkerrors(phy);				\
273 	if (error)							\
274 		return error;						\
275 	return snprintf(buf, 20, "%u\n", phy->field);			\
276 }
277 
278 #define sas_phy_linkerror_attr(field)					\
279 	sas_phy_show_linkerror(field)					\
280 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
281 
282 
283 static ssize_t
284 show_sas_device_type(struct class_device *cdev, char *buf)
285 {
286 	struct sas_phy *phy = transport_class_to_phy(cdev);
287 
288 	if (!phy->identify.device_type)
289 		return snprintf(buf, 20, "none\n");
290 	return get_sas_device_type_names(phy->identify.device_type, buf);
291 }
292 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
293 
294 static ssize_t do_sas_phy_reset(struct class_device *cdev,
295 		size_t count, int hard_reset)
296 {
297 	struct sas_phy *phy = transport_class_to_phy(cdev);
298 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
299 	struct sas_internal *i = to_sas_internal(shost->transportt);
300 	int error;
301 
302 	if (!phy->local_attached)
303 		return -EINVAL;
304 
305 	error = i->f->phy_reset(phy, hard_reset);
306 	if (error)
307 		return error;
308 	return count;
309 };
310 
311 static ssize_t store_sas_link_reset(struct class_device *cdev,
312 		const char *buf, size_t count)
313 {
314 	return do_sas_phy_reset(cdev, count, 0);
315 }
316 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
317 
318 static ssize_t store_sas_hard_reset(struct class_device *cdev,
319 		const char *buf, size_t count)
320 {
321 	return do_sas_phy_reset(cdev, count, 1);
322 }
323 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
324 
325 sas_phy_protocol_attr(identify.initiator_port_protocols,
326 		initiator_port_protocols);
327 sas_phy_protocol_attr(identify.target_port_protocols,
328 		target_port_protocols);
329 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
330 		unsigned long long);
331 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
332 sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
333 sas_phy_linkspeed_attr(negotiated_linkrate);
334 sas_phy_linkspeed_attr(minimum_linkrate_hw);
335 sas_phy_linkspeed_attr(minimum_linkrate);
336 sas_phy_linkspeed_attr(maximum_linkrate_hw);
337 sas_phy_linkspeed_attr(maximum_linkrate);
338 sas_phy_linkerror_attr(invalid_dword_count);
339 sas_phy_linkerror_attr(running_disparity_error_count);
340 sas_phy_linkerror_attr(loss_of_dword_sync_count);
341 sas_phy_linkerror_attr(phy_reset_problem_count);
342 
343 
344 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
345 		"sas_phy", NULL, NULL, NULL);
346 
347 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
348 {
349 	struct Scsi_Host *shost;
350 	struct sas_internal *i;
351 
352 	if (!scsi_is_sas_phy(dev))
353 		return 0;
354 	shost = dev_to_shost(dev->parent);
355 
356 	if (!shost->transportt)
357 		return 0;
358 	if (shost->transportt->host_attrs.ac.class !=
359 			&sas_host_class.class)
360 		return 0;
361 
362 	i = to_sas_internal(shost->transportt);
363 	return &i->phy_attr_cont.ac == cont;
364 }
365 
366 static void sas_phy_release(struct device *dev)
367 {
368 	struct sas_phy *phy = dev_to_phy(dev);
369 
370 	put_device(dev->parent);
371 	kfree(phy);
372 }
373 
374 /**
375  * sas_phy_alloc  --  allocates and initialize a SAS PHY structure
376  * @parent:	Parent device
377  * @number:	Port number
378  *
379  * Allocates an SAS PHY structure.  It will be added in the device tree
380  * below the device specified by @parent, which has to be either a Scsi_Host
381  * or sas_rphy.
382  *
383  * Returns:
384  *	SAS PHY allocated or %NULL if the allocation failed.
385  */
386 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
387 {
388 	struct Scsi_Host *shost = dev_to_shost(parent);
389 	struct sas_phy *phy;
390 
391 	phy = kmalloc(sizeof(*phy), GFP_KERNEL);
392 	if (!phy)
393 		return NULL;
394 	memset(phy, 0, sizeof(*phy));
395 
396 	get_device(parent);
397 
398 	phy->number = number;
399 
400 	device_initialize(&phy->dev);
401 	phy->dev.parent = get_device(parent);
402 	phy->dev.release = sas_phy_release;
403 	sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
404 
405 	transport_setup_device(&phy->dev);
406 
407 	return phy;
408 }
409 EXPORT_SYMBOL(sas_phy_alloc);
410 
411 /**
412  * sas_phy_add  --  add a SAS PHY to the device hierachy
413  * @phy:	The PHY to be added
414  *
415  * Publishes a SAS PHY to the rest of the system.
416  */
417 int sas_phy_add(struct sas_phy *phy)
418 {
419 	int error;
420 
421 	error = device_add(&phy->dev);
422 	if (!error) {
423 		transport_add_device(&phy->dev);
424 		transport_configure_device(&phy->dev);
425 	}
426 
427 	return error;
428 }
429 EXPORT_SYMBOL(sas_phy_add);
430 
431 /**
432  * sas_phy_free  --  free a SAS PHY
433  * @phy:	SAS PHY to free
434  *
435  * Frees the specified SAS PHY.
436  *
437  * Note:
438  *   This function must only be called on a PHY that has not
439  *   sucessfully been added using sas_phy_add().
440  */
441 void sas_phy_free(struct sas_phy *phy)
442 {
443 	transport_destroy_device(&phy->dev);
444 	put_device(phy->dev.parent);
445 	put_device(phy->dev.parent);
446 	put_device(phy->dev.parent);
447 	kfree(phy);
448 }
449 EXPORT_SYMBOL(sas_phy_free);
450 
451 /**
452  * sas_phy_delete  --  remove SAS PHY
453  * @phy:	SAS PHY to remove
454  *
455  * Removes the specified SAS PHY.  If the SAS PHY has an
456  * associated remote PHY it is removed before.
457  */
458 void
459 sas_phy_delete(struct sas_phy *phy)
460 {
461 	struct device *dev = &phy->dev;
462 
463 	if (phy->rphy)
464 		sas_rphy_delete(phy->rphy);
465 
466 	transport_remove_device(dev);
467 	device_del(dev);
468 	transport_destroy_device(dev);
469 	put_device(dev->parent);
470 }
471 EXPORT_SYMBOL(sas_phy_delete);
472 
473 /**
474  * scsi_is_sas_phy  --  check if a struct device represents a SAS PHY
475  * @dev:	device to check
476  *
477  * Returns:
478  *	%1 if the device represents a SAS PHY, %0 else
479  */
480 int scsi_is_sas_phy(const struct device *dev)
481 {
482 	return dev->release == sas_phy_release;
483 }
484 EXPORT_SYMBOL(scsi_is_sas_phy);
485 
486 /*
487  * SAS remote PHY attributes.
488  */
489 
490 #define sas_rphy_show_simple(field, name, format_string, cast)		\
491 static ssize_t								\
492 show_sas_rphy_##name(struct class_device *cdev, char *buf)		\
493 {									\
494 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);	\
495 									\
496 	return snprintf(buf, 20, format_string, cast rphy->field);	\
497 }
498 
499 #define sas_rphy_simple_attr(field, name, format_string, type)		\
500 	sas_rphy_show_simple(field, name, format_string, (type))	\
501 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
502 		show_sas_rphy_##name, NULL)
503 
504 #define sas_rphy_show_protocol(field, name)				\
505 static ssize_t								\
506 show_sas_rphy_##name(struct class_device *cdev, char *buf)		\
507 {									\
508 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);	\
509 									\
510 	if (!rphy->field)					\
511 		return snprintf(buf, 20, "none\n");			\
512 	return get_sas_protocol_names(rphy->field, buf);	\
513 }
514 
515 #define sas_rphy_protocol_attr(field, name)				\
516 	sas_rphy_show_protocol(field, name)				\
517 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
518 		show_sas_rphy_##name, NULL)
519 
520 static ssize_t
521 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
522 {
523 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);
524 
525 	if (!rphy->identify.device_type)
526 		return snprintf(buf, 20, "none\n");
527 	return get_sas_device_type_names(
528 			rphy->identify.device_type, buf);
529 }
530 
531 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
532 		show_sas_rphy_device_type, NULL);
533 
534 sas_rphy_protocol_attr(identify.initiator_port_protocols,
535 		initiator_port_protocols);
536 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
537 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
538 		unsigned long long);
539 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
540 
541 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
542 		"sas_rphy", NULL, NULL, NULL);
543 
544 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
545 {
546 	struct Scsi_Host *shost;
547 	struct sas_internal *i;
548 
549 	if (!scsi_is_sas_rphy(dev))
550 		return 0;
551 	shost = dev_to_shost(dev->parent->parent);
552 
553 	if (!shost->transportt)
554 		return 0;
555 	if (shost->transportt->host_attrs.ac.class !=
556 			&sas_host_class.class)
557 		return 0;
558 
559 	i = to_sas_internal(shost->transportt);
560 	return &i->rphy_attr_cont.ac == cont;
561 }
562 
563 static void sas_rphy_release(struct device *dev)
564 {
565 	struct sas_rphy *rphy = dev_to_rphy(dev);
566 
567 	put_device(dev->parent);
568 	kfree(rphy);
569 }
570 
571 /**
572  * sas_rphy_alloc  --  allocates and initialize a SAS remote PHY structure
573  * @parent:		SAS PHY this remote PHY is conneted to
574  *
575  * Allocates an SAS remote PHY structure, connected to @parent.
576  *
577  * Returns:
578  *	SAS PHY allocated or %NULL if the allocation failed.
579  */
580 struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
581 {
582 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
583 	struct sas_rphy *rphy;
584 
585 	rphy = kmalloc(sizeof(*rphy), GFP_KERNEL);
586 	if (!rphy) {
587 		put_device(&parent->dev);
588 		return NULL;
589 	}
590 	memset(rphy, 0, sizeof(*rphy));
591 
592 	device_initialize(&rphy->dev);
593 	rphy->dev.parent = get_device(&parent->dev);
594 	rphy->dev.release = sas_rphy_release;
595 	sprintf(rphy->dev.bus_id, "rphy-%d:%d",
596 		shost->host_no, parent->number);
597 	transport_setup_device(&rphy->dev);
598 
599 	return rphy;
600 }
601 EXPORT_SYMBOL(sas_rphy_alloc);
602 
603 /**
604  * sas_rphy_add  --  add a SAS remote PHY to the device hierachy
605  * @rphy:	The remote PHY to be added
606  *
607  * Publishes a SAS remote PHY to the rest of the system.
608  */
609 int sas_rphy_add(struct sas_rphy *rphy)
610 {
611 	struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
612 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
613 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
614 	struct sas_identify *identify = &rphy->identify;
615 	int error;
616 
617 	if (parent->rphy)
618 		return -ENXIO;
619 	parent->rphy = rphy;
620 
621 	error = device_add(&rphy->dev);
622 	if (error)
623 		return error;
624 	transport_add_device(&rphy->dev);
625 	transport_configure_device(&rphy->dev);
626 
627 	spin_lock(&sas_host->lock);
628 	list_add_tail(&rphy->list, &sas_host->rphy_list);
629 	if (identify->device_type == SAS_END_DEVICE &&
630 	    (identify->target_port_protocols &
631 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
632 		rphy->scsi_target_id = sas_host->next_target_id++;
633 	else
634 		rphy->scsi_target_id = -1;
635 	spin_unlock(&sas_host->lock);
636 
637 	if (rphy->scsi_target_id != -1) {
638 		scsi_scan_target(&rphy->dev, parent->number,
639 				rphy->scsi_target_id, ~0, 0);
640 	}
641 
642 	return 0;
643 }
644 EXPORT_SYMBOL(sas_rphy_add);
645 
646 /**
647  * sas_rphy_free  --  free a SAS remote PHY
648  * @rphy	SAS remote PHY to free
649  *
650  * Frees the specified SAS remote PHY.
651  *
652  * Note:
653  *   This function must only be called on a remote
654  *   PHY that has not sucessfully been added using
655  *   sas_rphy_add().
656  */
657 void sas_rphy_free(struct sas_rphy *rphy)
658 {
659 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
660 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
661 
662 	spin_lock(&sas_host->lock);
663 	list_del(&rphy->list);
664 	spin_unlock(&sas_host->lock);
665 
666 	transport_destroy_device(&rphy->dev);
667 	put_device(rphy->dev.parent);
668 	put_device(rphy->dev.parent);
669 	put_device(rphy->dev.parent);
670 	kfree(rphy);
671 }
672 EXPORT_SYMBOL(sas_rphy_free);
673 
674 /**
675  * sas_rphy_delete  --  remove SAS remote PHY
676  * @rphy:	SAS remote PHY to remove
677  *
678  * Removes the specified SAS remote PHY.
679  */
680 void
681 sas_rphy_delete(struct sas_rphy *rphy)
682 {
683 	struct device *dev = &rphy->dev;
684 	struct sas_phy *parent = dev_to_phy(dev->parent);
685 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
686 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
687 
688 	scsi_remove_target(dev);
689 
690 	transport_remove_device(dev);
691 	device_del(dev);
692 	transport_destroy_device(dev);
693 
694 	spin_lock(&sas_host->lock);
695 	list_del(&rphy->list);
696 	spin_unlock(&sas_host->lock);
697 
698 	put_device(&parent->dev);
699 }
700 EXPORT_SYMBOL(sas_rphy_delete);
701 
702 /**
703  * scsi_is_sas_rphy  --  check if a struct device represents a SAS remote PHY
704  * @dev:	device to check
705  *
706  * Returns:
707  *	%1 if the device represents a SAS remote PHY, %0 else
708  */
709 int scsi_is_sas_rphy(const struct device *dev)
710 {
711 	return dev->release == sas_rphy_release;
712 }
713 EXPORT_SYMBOL(scsi_is_sas_rphy);
714 
715 
716 /*
717  * SCSI scan helper
718  */
719 
720 static struct device *sas_target_parent(struct Scsi_Host *shost,
721 					int channel, uint id)
722 {
723 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
724 	struct sas_rphy *rphy;
725 	struct device *dev = NULL;
726 
727 	spin_lock(&sas_host->lock);
728 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
729 		struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
730 		if (parent->number == channel &&
731 		    rphy->scsi_target_id == id)
732 			dev = &rphy->dev;
733 	}
734 	spin_unlock(&sas_host->lock);
735 
736 	return dev;
737 }
738 
739 
740 /*
741  * Setup / Teardown code
742  */
743 
744 #define SETUP_RPORT_ATTRIBUTE(field)					\
745 	i->private_rphy_attrs[count] = class_device_attr_##field;	\
746 	i->private_rphy_attrs[count].attr.mode = S_IRUGO;		\
747 	i->private_rphy_attrs[count].store = NULL;			\
748 	i->rphy_attrs[count] = &i->private_rphy_attrs[count];	\
749 	count++
750 
751 #define SETUP_PORT_ATTRIBUTE(field)					\
752 	i->private_phy_attrs[count] = class_device_attr_##field;	\
753         i->private_phy_attrs[count].attr.mode = S_IRUGO;		\
754         i->private_phy_attrs[count].store = NULL;			\
755         i->phy_attrs[count] = &i->private_phy_attrs[count];		\
756 	count++
757 
758 #define SETUP_PORT_ATTRIBUTE_WRONLY(field)				\
759 	i->private_phy_attrs[count] = class_device_attr_##field;	\
760 	i->private_phy_attrs[count].attr.mode = S_IWUGO;		\
761 	i->private_phy_attrs[count].show = NULL;			\
762 	i->phy_attrs[count] = &i->private_phy_attrs[count];		\
763 	count++
764 
765 
766 /**
767  * sas_attach_transport  --  instantiate SAS transport template
768  * @ft:		SAS transport class function template
769  */
770 struct scsi_transport_template *
771 sas_attach_transport(struct sas_function_template *ft)
772 {
773 	struct sas_internal *i;
774 	int count;
775 
776 	i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL);
777 	if (!i)
778 		return NULL;
779 	memset(i, 0, sizeof(struct sas_internal));
780 
781 	i->t.target_parent = sas_target_parent;
782 
783 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
784 	i->t.host_attrs.ac.class = &sas_host_class.class;
785 	i->t.host_attrs.ac.match = sas_host_match;
786 	transport_container_register(&i->t.host_attrs);
787 	i->t.host_size = sizeof(struct sas_host_attrs);
788 
789 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
790 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
791 	i->phy_attr_cont.ac.match = sas_phy_match;
792 	transport_container_register(&i->phy_attr_cont);
793 
794 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
795 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
796 	i->rphy_attr_cont.ac.match = sas_rphy_match;
797 	transport_container_register(&i->rphy_attr_cont);
798 
799 	i->f = ft;
800 
801 	count = 0;
802 	i->host_attrs[count] = NULL;
803 
804 	count = 0;
805 	SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
806 	SETUP_PORT_ATTRIBUTE(target_port_protocols);
807 	SETUP_PORT_ATTRIBUTE(device_type);
808 	SETUP_PORT_ATTRIBUTE(sas_address);
809 	SETUP_PORT_ATTRIBUTE(phy_identifier);
810 	SETUP_PORT_ATTRIBUTE(port_identifier);
811 	SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
812 	SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
813 	SETUP_PORT_ATTRIBUTE(minimum_linkrate);
814 	SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
815 	SETUP_PORT_ATTRIBUTE(maximum_linkrate);
816 
817 	SETUP_PORT_ATTRIBUTE(invalid_dword_count);
818 	SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
819 	SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
820 	SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
821 	SETUP_PORT_ATTRIBUTE_WRONLY(link_reset);
822 	SETUP_PORT_ATTRIBUTE_WRONLY(hard_reset);
823 	i->phy_attrs[count] = NULL;
824 
825 	count = 0;
826 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
827 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
828 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
829 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
830 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
831 	i->rphy_attrs[count] = NULL;
832 
833 	return &i->t;
834 }
835 EXPORT_SYMBOL(sas_attach_transport);
836 
837 /**
838  * sas_release_transport  --  release SAS transport template instance
839  * @t:		transport template instance
840  */
841 void sas_release_transport(struct scsi_transport_template *t)
842 {
843 	struct sas_internal *i = to_sas_internal(t);
844 
845 	transport_container_unregister(&i->t.host_attrs);
846 	transport_container_unregister(&i->phy_attr_cont);
847 	transport_container_unregister(&i->rphy_attr_cont);
848 
849 	kfree(i);
850 }
851 EXPORT_SYMBOL(sas_release_transport);
852 
853 static __init int sas_transport_init(void)
854 {
855 	int error;
856 
857 	error = transport_class_register(&sas_host_class);
858 	if (error)
859 		goto out;
860 	error = transport_class_register(&sas_phy_class);
861 	if (error)
862 		goto out_unregister_transport;
863 	error = transport_class_register(&sas_rphy_class);
864 	if (error)
865 		goto out_unregister_phy;
866 
867 	return 0;
868 
869  out_unregister_phy:
870 	transport_class_unregister(&sas_phy_class);
871  out_unregister_transport:
872 	transport_class_unregister(&sas_host_class);
873  out:
874 	return error;
875 
876 }
877 
878 static void __exit sas_transport_exit(void)
879 {
880 	transport_class_unregister(&sas_host_class);
881 	transport_class_unregister(&sas_phy_class);
882 	transport_class_unregister(&sas_rphy_class);
883 }
884 
885 MODULE_AUTHOR("Christoph Hellwig");
886 MODULE_DESCRIPTION("SAS Transphy Attributes");
887 MODULE_LICENSE("GPL");
888 
889 module_init(sas_transport_init);
890 module_exit(sas_transport_exit);
891