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