1c7ebbbceSChristoph Hellwig /*
2a0125641SChristoph Hellwig  * Copyright (C) 2005-2006 Dell Inc.
3c7ebbbceSChristoph Hellwig  *	Released under GPL v2.
4c7ebbbceSChristoph Hellwig  *
5c7ebbbceSChristoph Hellwig  * Serial Attached SCSI (SAS) transport class.
6c7ebbbceSChristoph Hellwig  *
7c7ebbbceSChristoph Hellwig  * The SAS transport class contains common code to deal with SAS HBAs,
8c7ebbbceSChristoph Hellwig  * an aproximated representation of SAS topologies in the driver model,
9b1c11812SJoe Perches  * and various sysfs attributes to expose these topologies and management
10c7ebbbceSChristoph Hellwig  * interfaces to userspace.
11c7ebbbceSChristoph Hellwig  *
12c7ebbbceSChristoph Hellwig  * In addition to the basic SCSI core objects this transport class
13c7ebbbceSChristoph Hellwig  * introduces two additional intermediate objects:  The SAS PHY
14c7ebbbceSChristoph Hellwig  * as represented by struct sas_phy defines an "outgoing" PHY on
15c7ebbbceSChristoph Hellwig  * a SAS HBA or Expander, and the SAS remote PHY represented by
16c7ebbbceSChristoph Hellwig  * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17c7ebbbceSChristoph Hellwig  * end device.  Note that this is purely a software concept, the
18c7ebbbceSChristoph Hellwig  * underlying hardware for a PHY and a remote PHY is the exactly
19c7ebbbceSChristoph Hellwig  * the same.
20c7ebbbceSChristoph Hellwig  *
21c7ebbbceSChristoph Hellwig  * There is no concept of a SAS port in this code, users can see
22c7ebbbceSChristoph Hellwig  * what PHYs form a wide port based on the port_identifier attribute,
23c7ebbbceSChristoph Hellwig  * which is the same for all PHYs in a port.
24c7ebbbceSChristoph Hellwig  */
25c7ebbbceSChristoph Hellwig 
26c7ebbbceSChristoph Hellwig #include <linux/init.h>
27c7ebbbceSChristoph Hellwig #include <linux/module.h>
28f6a57033SAl Viro #include <linux/jiffies.h>
29c7ebbbceSChristoph Hellwig #include <linux/err.h>
308c65b4a6STim Schmielau #include <linux/slab.h>
318c65b4a6STim Schmielau #include <linux/string.h>
327aa68e80SFUJITA Tomonori #include <linux/blkdev.h>
337aa68e80SFUJITA Tomonori #include <linux/bsg.h>
34c7ebbbceSChristoph Hellwig 
35e02f3f59SChristoph Hellwig #include <scsi/scsi.h>
36ca18d6f7SBart Van Assche #include <scsi/scsi_cmnd.h>
3782ed4db4SChristoph Hellwig #include <scsi/scsi_request.h>
38c7ebbbceSChristoph Hellwig #include <scsi/scsi_device.h>
39c7ebbbceSChristoph Hellwig #include <scsi/scsi_host.h>
40c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport.h>
41c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport_sas.h>
42c7ebbbceSChristoph Hellwig 
43d6159c17SJames Bottomley #include "scsi_sas_internal.h"
44c7ebbbceSChristoph Hellwig struct sas_host_attrs {
45c7ebbbceSChristoph Hellwig 	struct list_head rphy_list;
46e02f3f59SChristoph Hellwig 	struct mutex lock;
47b6aff669SJames Bottomley 	struct request_queue *q;
48c7ebbbceSChristoph Hellwig 	u32 next_target_id;
4979cb1819SJames Bottomley 	u32 next_expander_id;
50c9fefeb2SJames Bottomley 	int next_port_id;
51c7ebbbceSChristoph Hellwig };
52c7ebbbceSChristoph Hellwig #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
53c7ebbbceSChristoph Hellwig 
54c7ebbbceSChristoph Hellwig 
55c7ebbbceSChristoph Hellwig /*
56c7ebbbceSChristoph Hellwig  * Hack to allow attributes of the same name in different objects.
57c7ebbbceSChristoph Hellwig  */
58ee959b00STony Jones #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59ee959b00STony Jones 	struct device_attribute dev_attr_##_prefix##_##_name = \
60c7ebbbceSChristoph Hellwig 	__ATTR(_name,_mode,_show,_store)
61c7ebbbceSChristoph Hellwig 
62c7ebbbceSChristoph Hellwig 
63c7ebbbceSChristoph Hellwig /*
64c7ebbbceSChristoph Hellwig  * Pretty printing helpers
65c7ebbbceSChristoph Hellwig  */
66c7ebbbceSChristoph Hellwig 
67c7ebbbceSChristoph Hellwig #define sas_bitfield_name_match(title, table)			\
68c7ebbbceSChristoph Hellwig static ssize_t							\
69c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
70c7ebbbceSChristoph Hellwig {								\
71c7ebbbceSChristoph Hellwig 	char *prefix = "";					\
72c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
73c7ebbbceSChristoph Hellwig 	int i;							\
74c7ebbbceSChristoph Hellwig 								\
756391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
76c7ebbbceSChristoph Hellwig 		if (table[i].value & table_key) {		\
77c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s%s",	\
78c7ebbbceSChristoph Hellwig 				prefix, table[i].name);		\
79c7ebbbceSChristoph Hellwig 			prefix = ", ";				\
80c7ebbbceSChristoph Hellwig 		}						\
81c7ebbbceSChristoph Hellwig 	}							\
82c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
83c7ebbbceSChristoph Hellwig 	return len;						\
84c7ebbbceSChristoph Hellwig }
85c7ebbbceSChristoph Hellwig 
86d24e1eebSJames Bottomley #define sas_bitfield_name_set(title, table)			\
87d24e1eebSJames Bottomley static ssize_t							\
88d24e1eebSJames Bottomley set_sas_##title##_names(u32 *table_key, const char *buf)	\
89d24e1eebSJames Bottomley {								\
90d24e1eebSJames Bottomley 	ssize_t len = 0;					\
91d24e1eebSJames Bottomley 	int i;							\
92d24e1eebSJames Bottomley 								\
93d24e1eebSJames Bottomley 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
94d24e1eebSJames Bottomley 		len = strlen(table[i].name);			\
95d24e1eebSJames Bottomley 		if (strncmp(buf, table[i].name, len) == 0 &&	\
96d24e1eebSJames Bottomley 		    (buf[len] == '\n' || buf[len] == '\0')) {	\
97d24e1eebSJames Bottomley 			*table_key = table[i].value;		\
98d24e1eebSJames Bottomley 			return 0;				\
99d24e1eebSJames Bottomley 		}						\
100d24e1eebSJames Bottomley 	}							\
101d24e1eebSJames Bottomley 	return -EINVAL;						\
102d24e1eebSJames Bottomley }
103d24e1eebSJames Bottomley 
104c7ebbbceSChristoph Hellwig #define sas_bitfield_name_search(title, table)			\
105c7ebbbceSChristoph Hellwig static ssize_t							\
106c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
107c7ebbbceSChristoph Hellwig {								\
108c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
109c7ebbbceSChristoph Hellwig 	int i;							\
110c7ebbbceSChristoph Hellwig 								\
1116391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
112c7ebbbceSChristoph Hellwig 		if (table[i].value == table_key) {		\
113c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s",		\
114c7ebbbceSChristoph Hellwig 				table[i].name);			\
115c7ebbbceSChristoph Hellwig 			break;					\
116c7ebbbceSChristoph Hellwig 		}						\
117c7ebbbceSChristoph Hellwig 	}							\
118c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
119c7ebbbceSChristoph Hellwig 	return len;						\
120c7ebbbceSChristoph Hellwig }
121c7ebbbceSChristoph Hellwig 
122c7ebbbceSChristoph Hellwig static struct {
123c7ebbbceSChristoph Hellwig 	u32		value;
124c7ebbbceSChristoph Hellwig 	char		*name;
125c7ebbbceSChristoph Hellwig } sas_device_type_names[] = {
126c7ebbbceSChristoph Hellwig 	{ SAS_PHY_UNUSED,		"unused" },
127c7ebbbceSChristoph Hellwig 	{ SAS_END_DEVICE,		"end device" },
128c7ebbbceSChristoph Hellwig 	{ SAS_EDGE_EXPANDER_DEVICE,	"edge expander" },
129c7ebbbceSChristoph Hellwig 	{ SAS_FANOUT_EXPANDER_DEVICE,	"fanout expander" },
130c7ebbbceSChristoph Hellwig };
131c7ebbbceSChristoph Hellwig sas_bitfield_name_search(device_type, sas_device_type_names)
132c7ebbbceSChristoph Hellwig 
133c7ebbbceSChristoph Hellwig 
134c7ebbbceSChristoph Hellwig static struct {
135c7ebbbceSChristoph Hellwig 	u32		value;
136c7ebbbceSChristoph Hellwig 	char		*name;
137c7ebbbceSChristoph Hellwig } sas_protocol_names[] = {
138c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SATA,		"sata" },
139c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SMP,		"smp" },
140c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_STP,		"stp" },
141c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SSP,		"ssp" },
142c7ebbbceSChristoph Hellwig };
143c7ebbbceSChristoph Hellwig sas_bitfield_name_match(protocol, sas_protocol_names)
144c7ebbbceSChristoph Hellwig 
145c7ebbbceSChristoph Hellwig static struct {
146c7ebbbceSChristoph Hellwig 	u32		value;
147c7ebbbceSChristoph Hellwig 	char		*name;
148c7ebbbceSChristoph Hellwig } sas_linkspeed_names[] = {
149c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_UNKNOWN,	"Unknown" },
150c7ebbbceSChristoph Hellwig 	{ SAS_PHY_DISABLED,		"Phy disabled" },
151c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_FAILED,		"Link Rate failed" },
152c7ebbbceSChristoph Hellwig 	{ SAS_SATA_SPINUP_HOLD,		"Spin-up hold" },
153c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_1_5_GBPS,	"1.5 Gbit" },
154c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_3_0_GBPS,	"3.0 Gbit" },
1557e6dff62SJames Bottomley 	{ SAS_LINK_RATE_6_0_GBPS,	"6.0 Gbit" },
156d84fd392SSreekanth Reddy 	{ SAS_LINK_RATE_12_0_GBPS,	"12.0 Gbit" },
157c7ebbbceSChristoph Hellwig };
158c7ebbbceSChristoph Hellwig sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
159d24e1eebSJames Bottomley sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
160c7ebbbceSChristoph Hellwig 
1610f88009dSJames Bottomley static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
1620f88009dSJames Bottomley {
1630f88009dSJames Bottomley 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
1640f88009dSJames Bottomley 	struct sas_end_device *rdev;
1650f88009dSJames Bottomley 
1660f88009dSJames Bottomley 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
1670f88009dSJames Bottomley 
1680f88009dSJames Bottomley 	rdev = rphy_to_end_device(rphy);
1690f88009dSJames Bottomley 	return rdev;
1700f88009dSJames Bottomley }
1710f88009dSJames Bottomley 
1727aa68e80SFUJITA Tomonori static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
1737aa68e80SFUJITA Tomonori 			    struct sas_rphy *rphy)
1747aa68e80SFUJITA Tomonori {
1757aa68e80SFUJITA Tomonori 	struct request *req;
1762a842acaSChristoph Hellwig 	blk_status_t ret;
1777aa68e80SFUJITA Tomonori 	int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
1787aa68e80SFUJITA Tomonori 
1797eaceaccSJens Axboe 	while ((req = blk_fetch_request(q)) != NULL) {
1807aa68e80SFUJITA Tomonori 		spin_unlock_irq(q->queue_lock);
1817aa68e80SFUJITA Tomonori 
18282ed4db4SChristoph Hellwig 		scsi_req(req)->resid_len = blk_rq_bytes(req);
18382ed4db4SChristoph Hellwig 		if (req->next_rq)
18482ed4db4SChristoph Hellwig 			scsi_req(req->next_rq)->resid_len =
18582ed4db4SChristoph Hellwig 				blk_rq_bytes(req->next_rq);
1867aa68e80SFUJITA Tomonori 		handler = to_sas_internal(shost->transportt)->f->smp_handler;
1877aa68e80SFUJITA Tomonori 		ret = handler(shost, rphy, req);
18817d5363bSChristoph Hellwig 		scsi_req(req)->result = ret;
1897aa68e80SFUJITA Tomonori 
190d188b90cSChristoph Hellwig 		blk_end_request_all(req, 0);
1917aa68e80SFUJITA Tomonori 
19293bdcba5SFUJITA Tomonori 		spin_lock_irq(q->queue_lock);
1937aa68e80SFUJITA Tomonori 	}
1947aa68e80SFUJITA Tomonori }
1957aa68e80SFUJITA Tomonori 
1967aa68e80SFUJITA Tomonori static void sas_host_smp_request(struct request_queue *q)
1977aa68e80SFUJITA Tomonori {
1987aa68e80SFUJITA Tomonori 	sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
1997aa68e80SFUJITA Tomonori }
2007aa68e80SFUJITA Tomonori 
2017aa68e80SFUJITA Tomonori static void sas_non_host_smp_request(struct request_queue *q)
2027aa68e80SFUJITA Tomonori {
2037aa68e80SFUJITA Tomonori 	struct sas_rphy *rphy = q->queuedata;
2047aa68e80SFUJITA Tomonori 	sas_smp_request(q, rphy_to_shost(rphy), rphy);
2057aa68e80SFUJITA Tomonori }
2067aa68e80SFUJITA Tomonori 
20793c20a59SFUJITA Tomonori static void sas_host_release(struct device *dev)
20893c20a59SFUJITA Tomonori {
20993c20a59SFUJITA Tomonori 	struct Scsi_Host *shost = dev_to_shost(dev);
21093c20a59SFUJITA Tomonori 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
21193c20a59SFUJITA Tomonori 	struct request_queue *q = sas_host->q;
21293c20a59SFUJITA Tomonori 
21393c20a59SFUJITA Tomonori 	if (q)
21493c20a59SFUJITA Tomonori 		blk_cleanup_queue(q);
21593c20a59SFUJITA Tomonori }
21693c20a59SFUJITA Tomonori 
21739dca558SJames Bottomley static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
2187aa68e80SFUJITA Tomonori {
2197aa68e80SFUJITA Tomonori 	struct request_queue *q;
2207aa68e80SFUJITA Tomonori 	int error;
22139dca558SJames Bottomley 	struct device *dev;
22271610f55SKay Sievers 	char namebuf[20];
22339dca558SJames Bottomley 	const char *name;
22493c20a59SFUJITA Tomonori 	void (*release)(struct device *);
2257aa68e80SFUJITA Tomonori 
2267aa68e80SFUJITA Tomonori 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
2277aa68e80SFUJITA Tomonori 		printk("%s can't handle SMP requests\n", shost->hostt->name);
2287aa68e80SFUJITA Tomonori 		return 0;
2297aa68e80SFUJITA Tomonori 	}
2307aa68e80SFUJITA Tomonori 
231bd1599d9SOmar Sandoval 	q = blk_alloc_queue(GFP_KERNEL);
232bd1599d9SOmar Sandoval 	if (!q)
233bd1599d9SOmar Sandoval 		return -ENOMEM;
234ca18d6f7SBart Van Assche 	q->initialize_rq_fn = scsi_initialize_rq;
235bd1599d9SOmar Sandoval 	q->cmd_size = sizeof(struct scsi_request);
236bd1599d9SOmar Sandoval 
23739dca558SJames Bottomley 	if (rphy) {
238bd1599d9SOmar Sandoval 		q->request_fn = sas_non_host_smp_request;
23939dca558SJames Bottomley 		dev = &rphy->dev;
24071610f55SKay Sievers 		name = dev_name(dev);
24193c20a59SFUJITA Tomonori 		release = NULL;
24239dca558SJames Bottomley 	} else {
243bd1599d9SOmar Sandoval 		q->request_fn = sas_host_smp_request;
24439dca558SJames Bottomley 		dev = &shost->shost_gendev;
24539dca558SJames Bottomley 		snprintf(namebuf, sizeof(namebuf),
24639dca558SJames Bottomley 			 "sas_host%d", shost->host_no);
24739dca558SJames Bottomley 		name = namebuf;
24893c20a59SFUJITA Tomonori 		release = sas_host_release;
24939dca558SJames Bottomley 	}
250bd1599d9SOmar Sandoval 	error = blk_init_allocated_queue(q);
251bd1599d9SOmar Sandoval 	if (error)
252bd1599d9SOmar Sandoval 		goto out_cleanup_queue;
2537aa68e80SFUJITA Tomonori 
2540bf6595eSChristoph Hellwig 	/*
2550bf6595eSChristoph Hellwig 	 * by default assume old behaviour and bounce for any highmem page
2560bf6595eSChristoph Hellwig 	 */
2570bf6595eSChristoph Hellwig 	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
2580bf6595eSChristoph Hellwig 
25993c20a59SFUJITA Tomonori 	error = bsg_register_queue(q, dev, name, release);
260bd1599d9SOmar Sandoval 	if (error)
261bd1599d9SOmar Sandoval 		goto out_cleanup_queue;
2627aa68e80SFUJITA Tomonori 
2637aa68e80SFUJITA Tomonori 	if (rphy)
264b6aff669SJames Bottomley 		rphy->q = q;
265b6aff669SJames Bottomley 	else
266b6aff669SJames Bottomley 		to_sas_host_attrs(shost)->q = q;
267b6aff669SJames Bottomley 
268b6aff669SJames Bottomley 	if (rphy)
2697aa68e80SFUJITA Tomonori 		q->queuedata = rphy;
2707aa68e80SFUJITA Tomonori 	else
2717aa68e80SFUJITA Tomonori 		q->queuedata = shost;
2727aa68e80SFUJITA Tomonori 
27375ad23bcSNick Piggin 	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
2749efc160fSBart Van Assche 	queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
2757aa68e80SFUJITA Tomonori 	return 0;
276bd1599d9SOmar Sandoval 
277bd1599d9SOmar Sandoval out_cleanup_queue:
278bd1599d9SOmar Sandoval 	blk_cleanup_queue(q);
279bd1599d9SOmar Sandoval 	return error;
2807aa68e80SFUJITA Tomonori }
2817aa68e80SFUJITA Tomonori 
282b6aff669SJames Bottomley static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
283b6aff669SJames Bottomley {
284b6aff669SJames Bottomley 	struct request_queue *q;
285b6aff669SJames Bottomley 
286b6aff669SJames Bottomley 	if (rphy)
287b6aff669SJames Bottomley 		q = rphy->q;
288b6aff669SJames Bottomley 	else
289b6aff669SJames Bottomley 		q = to_sas_host_attrs(shost)->q;
290b6aff669SJames Bottomley 
291b6aff669SJames Bottomley 	if (!q)
292b6aff669SJames Bottomley 		return;
293b6aff669SJames Bottomley 
294b6aff669SJames Bottomley 	bsg_unregister_queue(q);
295b6aff669SJames Bottomley }
296b6aff669SJames Bottomley 
297c7ebbbceSChristoph Hellwig /*
298c7ebbbceSChristoph Hellwig  * SAS host attributes
299c7ebbbceSChristoph Hellwig  */
300c7ebbbceSChristoph Hellwig 
30137be6eebSJames Bottomley static int sas_host_setup(struct transport_container *tc, struct device *dev,
302ee959b00STony Jones 			  struct device *cdev)
303c7ebbbceSChristoph Hellwig {
304c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(dev);
305c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
306c7ebbbceSChristoph Hellwig 
307c7ebbbceSChristoph Hellwig 	INIT_LIST_HEAD(&sas_host->rphy_list);
308e02f3f59SChristoph Hellwig 	mutex_init(&sas_host->lock);
309c7ebbbceSChristoph Hellwig 	sas_host->next_target_id = 0;
31079cb1819SJames Bottomley 	sas_host->next_expander_id = 0;
311c9fefeb2SJames Bottomley 	sas_host->next_port_id = 0;
3127aa68e80SFUJITA Tomonori 
31339dca558SJames Bottomley 	if (sas_bsg_initialize(shost, NULL))
3147aa68e80SFUJITA Tomonori 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
3157aa68e80SFUJITA Tomonori 			   shost->host_no);
3167aa68e80SFUJITA Tomonori 
317c7ebbbceSChristoph Hellwig 	return 0;
318c7ebbbceSChristoph Hellwig }
319c7ebbbceSChristoph Hellwig 
320b6aff669SJames Bottomley static int sas_host_remove(struct transport_container *tc, struct device *dev,
321ee959b00STony Jones 			   struct device *cdev)
322b6aff669SJames Bottomley {
323b6aff669SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev);
324b6aff669SJames Bottomley 
325b6aff669SJames Bottomley 	sas_bsg_remove(shost, NULL);
326b6aff669SJames Bottomley 
327b6aff669SJames Bottomley 	return 0;
328b6aff669SJames Bottomley }
329b6aff669SJames Bottomley 
330c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_host_class,
331b6aff669SJames Bottomley 		"sas_host", sas_host_setup, sas_host_remove, NULL);
332c7ebbbceSChristoph Hellwig 
333c7ebbbceSChristoph Hellwig static int sas_host_match(struct attribute_container *cont,
334c7ebbbceSChristoph Hellwig 			    struct device *dev)
335c7ebbbceSChristoph Hellwig {
336c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
337c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
338c7ebbbceSChristoph Hellwig 
339c7ebbbceSChristoph Hellwig 	if (!scsi_is_host_device(dev))
340c7ebbbceSChristoph Hellwig 		return 0;
341c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev);
342c7ebbbceSChristoph Hellwig 
343c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
344c7ebbbceSChristoph Hellwig 		return 0;
345c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
346c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
347c7ebbbceSChristoph Hellwig 		return 0;
348c7ebbbceSChristoph Hellwig 
349c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
350c7ebbbceSChristoph Hellwig 	return &i->t.host_attrs.ac == cont;
351c7ebbbceSChristoph Hellwig }
352c7ebbbceSChristoph Hellwig 
353c7ebbbceSChristoph Hellwig static int do_sas_phy_delete(struct device *dev, void *data)
354c7ebbbceSChristoph Hellwig {
35565c92b09SJames Bottomley 	int pass = (int)(unsigned long)data;
35665c92b09SJames Bottomley 
35765c92b09SJames Bottomley 	if (pass == 0 && scsi_is_sas_port(dev))
35865c92b09SJames Bottomley 		sas_port_delete(dev_to_sas_port(dev));
35965c92b09SJames Bottomley 	else if (pass == 1 && scsi_is_sas_phy(dev))
360c7ebbbceSChristoph Hellwig 		sas_phy_delete(dev_to_phy(dev));
361c7ebbbceSChristoph Hellwig 	return 0;
362c7ebbbceSChristoph Hellwig }
363c7ebbbceSChristoph Hellwig 
364c7ebbbceSChristoph Hellwig /**
365eb44820cSRob Landley  * sas_remove_children  -  tear down a devices SAS data structures
36665c92b09SJames Bottomley  * @dev:	device belonging to the sas object
36765c92b09SJames Bottomley  *
36865c92b09SJames Bottomley  * Removes all SAS PHYs and remote PHYs for a given object
36965c92b09SJames Bottomley  */
37065c92b09SJames Bottomley void sas_remove_children(struct device *dev)
37165c92b09SJames Bottomley {
37265c92b09SJames Bottomley 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
37365c92b09SJames Bottomley 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
37465c92b09SJames Bottomley }
37565c92b09SJames Bottomley EXPORT_SYMBOL(sas_remove_children);
37665c92b09SJames Bottomley 
37765c92b09SJames Bottomley /**
378eb44820cSRob Landley  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
379c7ebbbceSChristoph Hellwig  * @shost:	Scsi Host that is torn down
380c7ebbbceSChristoph Hellwig  *
381c5ce0abeSJohannes Thumshirn  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
382c5ce0abeSJohannes Thumshirn  * Scsi_Host as well.
383c5ce0abeSJohannes Thumshirn  *
384c5ce0abeSJohannes Thumshirn  * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
385c5ce0abeSJohannes Thumshirn  * already removed.
386c7ebbbceSChristoph Hellwig  */
387c7ebbbceSChristoph Hellwig void sas_remove_host(struct Scsi_Host *shost)
388c7ebbbceSChristoph Hellwig {
38965c92b09SJames Bottomley 	sas_remove_children(&shost->shost_gendev);
390c5ce0abeSJohannes Thumshirn 	scsi_remove_host(shost);
391c7ebbbceSChristoph Hellwig }
392c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_remove_host);
393c7ebbbceSChristoph Hellwig 
3940f88009dSJames Bottomley /**
395bcf508c1SJames Bottomley  * sas_get_address - return the SAS address of the device
396bcf508c1SJames Bottomley  * @sdev: scsi device
397bcf508c1SJames Bottomley  *
398bcf508c1SJames Bottomley  * Returns the SAS address of the scsi device
399bcf508c1SJames Bottomley  */
400bcf508c1SJames Bottomley u64 sas_get_address(struct scsi_device *sdev)
401bcf508c1SJames Bottomley {
402bcf508c1SJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
403bcf508c1SJames Bottomley 
404bcf508c1SJames Bottomley 	return rdev->rphy.identify.sas_address;
405bcf508c1SJames Bottomley }
406bcf508c1SJames Bottomley EXPORT_SYMBOL(sas_get_address);
407bcf508c1SJames Bottomley 
408bcf508c1SJames Bottomley /**
4090f88009dSJames Bottomley  * sas_tlr_supported - checking TLR bit in vpd 0x90
4100f88009dSJames Bottomley  * @sdev: scsi device struct
4110f88009dSJames Bottomley  *
4120f88009dSJames Bottomley  * Check Transport Layer Retries are supported or not.
4130f88009dSJames Bottomley  * If vpd page 0x90 is present, TRL is supported.
4140f88009dSJames Bottomley  *
4150f88009dSJames Bottomley  */
4160f88009dSJames Bottomley unsigned int
4170f88009dSJames Bottomley sas_tlr_supported(struct scsi_device *sdev)
4180f88009dSJames Bottomley {
4190f88009dSJames Bottomley 	const int vpd_len = 32;
4200f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4210f88009dSJames Bottomley 	char *buffer = kzalloc(vpd_len, GFP_KERNEL);
4220f88009dSJames Bottomley 	int ret = 0;
4230f88009dSJames Bottomley 
4240f88009dSJames Bottomley 	if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
4250f88009dSJames Bottomley 		goto out;
4260f88009dSJames Bottomley 
4270f88009dSJames Bottomley 	/*
4280f88009dSJames Bottomley 	 * Magic numbers: the VPD Protocol page (0x90)
4290f88009dSJames Bottomley 	 * has a 4 byte header and then one entry per device port
4300f88009dSJames Bottomley 	 * the TLR bit is at offset 8 on each port entry
4310f88009dSJames Bottomley 	 * if we take the first port, that's at total offset 12
4320f88009dSJames Bottomley 	 */
4330f88009dSJames Bottomley 	ret = buffer[12] & 0x01;
4340f88009dSJames Bottomley 
4350f88009dSJames Bottomley  out:
4360f88009dSJames Bottomley 	kfree(buffer);
4370f88009dSJames Bottomley 	rdev->tlr_supported = ret;
4380f88009dSJames Bottomley 	return ret;
4390f88009dSJames Bottomley 
4400f88009dSJames Bottomley }
4410f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_tlr_supported);
4420f88009dSJames Bottomley 
4430f88009dSJames Bottomley /**
4440f88009dSJames Bottomley  * sas_disable_tlr - setting TLR flags
4450f88009dSJames Bottomley  * @sdev: scsi device struct
4460f88009dSJames Bottomley  *
4470f88009dSJames Bottomley  * Seting tlr_enabled flag to 0.
4480f88009dSJames Bottomley  *
4490f88009dSJames Bottomley  */
4500f88009dSJames Bottomley void
4510f88009dSJames Bottomley sas_disable_tlr(struct scsi_device *sdev)
4520f88009dSJames Bottomley {
4530f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4540f88009dSJames Bottomley 
4550f88009dSJames Bottomley 	rdev->tlr_enabled = 0;
4560f88009dSJames Bottomley }
4570f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_disable_tlr);
4580f88009dSJames Bottomley 
4590f88009dSJames Bottomley /**
4600f88009dSJames Bottomley  * sas_enable_tlr - setting TLR flags
4610f88009dSJames Bottomley  * @sdev: scsi device struct
4620f88009dSJames Bottomley  *
4630f88009dSJames Bottomley  * Seting tlr_enabled flag 1.
4640f88009dSJames Bottomley  *
4650f88009dSJames Bottomley  */
4660f88009dSJames Bottomley void sas_enable_tlr(struct scsi_device *sdev)
4670f88009dSJames Bottomley {
4680f88009dSJames Bottomley 	unsigned int tlr_supported = 0;
4690f88009dSJames Bottomley 	tlr_supported  = sas_tlr_supported(sdev);
4700f88009dSJames Bottomley 
4710f88009dSJames Bottomley 	if (tlr_supported) {
4720f88009dSJames Bottomley 		struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4730f88009dSJames Bottomley 
4740f88009dSJames Bottomley 		rdev->tlr_enabled = 1;
4750f88009dSJames Bottomley 	}
4760f88009dSJames Bottomley 
4770f88009dSJames Bottomley 	return;
4780f88009dSJames Bottomley }
4790f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_enable_tlr);
4800f88009dSJames Bottomley 
4810f88009dSJames Bottomley unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
4820f88009dSJames Bottomley {
4830f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4840f88009dSJames Bottomley 	return rdev->tlr_enabled;
4850f88009dSJames Bottomley }
4860f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
487c7ebbbceSChristoph Hellwig 
488c7ebbbceSChristoph Hellwig /*
48965c92b09SJames Bottomley  * SAS Phy attributes
490c7ebbbceSChristoph Hellwig  */
491c7ebbbceSChristoph Hellwig 
492c7ebbbceSChristoph Hellwig #define sas_phy_show_simple(field, name, format_string, cast)		\
493c7ebbbceSChristoph Hellwig static ssize_t								\
494ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
495ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
496c7ebbbceSChristoph Hellwig {									\
497ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
498c7ebbbceSChristoph Hellwig 									\
499c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast phy->field);	\
500c7ebbbceSChristoph Hellwig }
501c7ebbbceSChristoph Hellwig 
502c7ebbbceSChristoph Hellwig #define sas_phy_simple_attr(field, name, format_string, type)		\
503c7ebbbceSChristoph Hellwig 	sas_phy_show_simple(field, name, format_string, (type))	\
504ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
505c7ebbbceSChristoph Hellwig 
506c7ebbbceSChristoph Hellwig #define sas_phy_show_protocol(field, name)				\
507c7ebbbceSChristoph Hellwig static ssize_t								\
508ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
509ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
510c7ebbbceSChristoph Hellwig {									\
511ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
512c7ebbbceSChristoph Hellwig 									\
513c7ebbbceSChristoph Hellwig 	if (!phy->field)						\
514c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
515c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(phy->field, buf);		\
516c7ebbbceSChristoph Hellwig }
517c7ebbbceSChristoph Hellwig 
518c7ebbbceSChristoph Hellwig #define sas_phy_protocol_attr(field, name)				\
519c7ebbbceSChristoph Hellwig 	sas_phy_show_protocol(field, name)				\
520ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
521c7ebbbceSChristoph Hellwig 
522c7ebbbceSChristoph Hellwig #define sas_phy_show_linkspeed(field)					\
523c7ebbbceSChristoph Hellwig static ssize_t								\
524ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
525ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
526c7ebbbceSChristoph Hellwig {									\
527ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
528c7ebbbceSChristoph Hellwig 									\
529c7ebbbceSChristoph Hellwig 	return get_sas_linkspeed_names(phy->field, buf);		\
530c7ebbbceSChristoph Hellwig }
531c7ebbbceSChristoph Hellwig 
532d24e1eebSJames Bottomley /* Fudge to tell if we're minimum or maximum */
533d24e1eebSJames Bottomley #define sas_phy_store_linkspeed(field)					\
534d24e1eebSJames Bottomley static ssize_t								\
535ee959b00STony Jones store_sas_phy_##field(struct device *dev, 				\
536ee959b00STony Jones 		      struct device_attribute *attr, 			\
537ee959b00STony Jones 		      const char *buf,	size_t count)			\
538d24e1eebSJames Bottomley {									\
539ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
540d24e1eebSJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
541d24e1eebSJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
542d24e1eebSJames Bottomley 	u32 value;							\
543d24e1eebSJames Bottomley 	struct sas_phy_linkrates rates = {0};				\
544d24e1eebSJames Bottomley 	int error;							\
545d24e1eebSJames Bottomley 									\
546d24e1eebSJames Bottomley 	error = set_sas_linkspeed_names(&value, buf);			\
547d24e1eebSJames Bottomley 	if (error)							\
548d24e1eebSJames Bottomley 		return error;						\
549d24e1eebSJames Bottomley 	rates.field = value;						\
550d24e1eebSJames Bottomley 	error = i->f->set_phy_speed(phy, &rates);			\
551d24e1eebSJames Bottomley 									\
552d24e1eebSJames Bottomley 	return error ? error : count;					\
553d24e1eebSJames Bottomley }
554d24e1eebSJames Bottomley 
555d24e1eebSJames Bottomley #define sas_phy_linkspeed_rw_attr(field)				\
556d24e1eebSJames Bottomley 	sas_phy_show_linkspeed(field)					\
557d24e1eebSJames Bottomley 	sas_phy_store_linkspeed(field)					\
558ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
559d24e1eebSJames Bottomley 	store_sas_phy_##field)
560d24e1eebSJames Bottomley 
561c7ebbbceSChristoph Hellwig #define sas_phy_linkspeed_attr(field)					\
562c7ebbbceSChristoph Hellwig 	sas_phy_show_linkspeed(field)					\
563ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
564c7ebbbceSChristoph Hellwig 
565d24e1eebSJames Bottomley 
566c3ee74c4SChristoph Hellwig #define sas_phy_show_linkerror(field)					\
567c3ee74c4SChristoph Hellwig static ssize_t								\
568ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
569ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
570c3ee74c4SChristoph Hellwig {									\
571ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
572c3ee74c4SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
573c3ee74c4SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
574c3ee74c4SChristoph Hellwig 	int error;							\
575c3ee74c4SChristoph Hellwig 									\
576dd9fbb52SJames Bottomley 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
577c3ee74c4SChristoph Hellwig 	if (error)							\
578c3ee74c4SChristoph Hellwig 		return error;						\
579c3ee74c4SChristoph Hellwig 	return snprintf(buf, 20, "%u\n", phy->field);			\
580c3ee74c4SChristoph Hellwig }
581c3ee74c4SChristoph Hellwig 
582c3ee74c4SChristoph Hellwig #define sas_phy_linkerror_attr(field)					\
583c3ee74c4SChristoph Hellwig 	sas_phy_show_linkerror(field)					\
584ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
585c3ee74c4SChristoph Hellwig 
586c3ee74c4SChristoph Hellwig 
587c7ebbbceSChristoph Hellwig static ssize_t
588ee959b00STony Jones show_sas_device_type(struct device *dev,
589ee959b00STony Jones 		     struct device_attribute *attr, char *buf)
590c7ebbbceSChristoph Hellwig {
591ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
592c7ebbbceSChristoph Hellwig 
593c7ebbbceSChristoph Hellwig 	if (!phy->identify.device_type)
594c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
595c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(phy->identify.device_type, buf);
596c7ebbbceSChristoph Hellwig }
597ee959b00STony Jones static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
598c7ebbbceSChristoph Hellwig 
599ee959b00STony Jones static ssize_t do_sas_phy_enable(struct device *dev,
600acbf167dSDarrick J. Wong 		size_t count, int enable)
601acbf167dSDarrick J. Wong {
602ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
603acbf167dSDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
604acbf167dSDarrick J. Wong 	struct sas_internal *i = to_sas_internal(shost->transportt);
605acbf167dSDarrick J. Wong 	int error;
606acbf167dSDarrick J. Wong 
607acbf167dSDarrick J. Wong 	error = i->f->phy_enable(phy, enable);
608acbf167dSDarrick J. Wong 	if (error)
609acbf167dSDarrick J. Wong 		return error;
610acbf167dSDarrick J. Wong 	phy->enabled = enable;
611acbf167dSDarrick J. Wong 	return count;
612acbf167dSDarrick J. Wong };
613acbf167dSDarrick J. Wong 
614ee959b00STony Jones static ssize_t
615ee959b00STony Jones store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
616acbf167dSDarrick J. Wong 		     const char *buf, size_t count)
617acbf167dSDarrick J. Wong {
618acbf167dSDarrick J. Wong 	if (count < 1)
619acbf167dSDarrick J. Wong 		return -EINVAL;
620acbf167dSDarrick J. Wong 
621acbf167dSDarrick J. Wong 	switch (buf[0]) {
622acbf167dSDarrick J. Wong 	case '0':
623ee959b00STony Jones 		do_sas_phy_enable(dev, count, 0);
624acbf167dSDarrick J. Wong 		break;
625acbf167dSDarrick J. Wong 	case '1':
626ee959b00STony Jones 		do_sas_phy_enable(dev, count, 1);
627acbf167dSDarrick J. Wong 		break;
628acbf167dSDarrick J. Wong 	default:
629acbf167dSDarrick J. Wong 		return -EINVAL;
630acbf167dSDarrick J. Wong 	}
631acbf167dSDarrick J. Wong 
632acbf167dSDarrick J. Wong 	return count;
633acbf167dSDarrick J. Wong }
634acbf167dSDarrick J. Wong 
635ee959b00STony Jones static ssize_t
636ee959b00STony Jones show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
637ee959b00STony Jones 		    char *buf)
638acbf167dSDarrick J. Wong {
639ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
640acbf167dSDarrick J. Wong 
641acbf167dSDarrick J. Wong 	return snprintf(buf, 20, "%d", phy->enabled);
642acbf167dSDarrick J. Wong }
643acbf167dSDarrick J. Wong 
644ee959b00STony Jones static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
645acbf167dSDarrick J. Wong 			 store_sas_phy_enable);
646acbf167dSDarrick J. Wong 
647ee959b00STony Jones static ssize_t
648ee959b00STony Jones do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
64907ba3a95SChristoph Hellwig {
650ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
65107ba3a95SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
65207ba3a95SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
65307ba3a95SChristoph Hellwig 	int error;
65407ba3a95SChristoph Hellwig 
65507ba3a95SChristoph Hellwig 	error = i->f->phy_reset(phy, hard_reset);
65607ba3a95SChristoph Hellwig 	if (error)
65707ba3a95SChristoph Hellwig 		return error;
65816d3db1bSDan Williams 	phy->enabled = 1;
65907ba3a95SChristoph Hellwig 	return count;
66007ba3a95SChristoph Hellwig };
66107ba3a95SChristoph Hellwig 
662ee959b00STony Jones static ssize_t
663ee959b00STony Jones store_sas_link_reset(struct device *dev, struct device_attribute *attr,
66407ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
66507ba3a95SChristoph Hellwig {
666ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 0);
66707ba3a95SChristoph Hellwig }
668ee959b00STony Jones static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
66907ba3a95SChristoph Hellwig 
670ee959b00STony Jones static ssize_t
671ee959b00STony Jones store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
67207ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
67307ba3a95SChristoph Hellwig {
674ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 1);
67507ba3a95SChristoph Hellwig }
676ee959b00STony Jones static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
67707ba3a95SChristoph Hellwig 
678c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.initiator_port_protocols,
679c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
680c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.target_port_protocols,
681c7ebbbceSChristoph Hellwig 		target_port_protocols);
682c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
683c7ebbbceSChristoph Hellwig 		unsigned long long);
684c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
685c9fefeb2SJames Bottomley //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
686c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(negotiated_linkrate);
687c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(minimum_linkrate_hw);
688d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(minimum_linkrate);
689c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(maximum_linkrate_hw);
690d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(maximum_linkrate);
691c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(invalid_dword_count);
692c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(running_disparity_error_count);
693c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(loss_of_dword_sync_count);
694c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(phy_reset_problem_count);
695c7ebbbceSChristoph Hellwig 
6960b3e09daSDan Williams static int sas_phy_setup(struct transport_container *tc, struct device *dev,
6970b3e09daSDan Williams 			 struct device *cdev)
6980b3e09daSDan Williams {
6990b3e09daSDan Williams 	struct sas_phy *phy = dev_to_phy(dev);
7000b3e09daSDan Williams 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
7010b3e09daSDan Williams 	struct sas_internal *i = to_sas_internal(shost->transportt);
7020b3e09daSDan Williams 
7030b3e09daSDan Williams 	if (i->f->phy_setup)
7040b3e09daSDan Williams 		i->f->phy_setup(phy);
7050b3e09daSDan Williams 
7060b3e09daSDan Williams 	return 0;
7070b3e09daSDan Williams }
708c7ebbbceSChristoph Hellwig 
709c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_phy_class,
7100b3e09daSDan Williams 		"sas_phy", sas_phy_setup, NULL, NULL);
711c7ebbbceSChristoph Hellwig 
712c7ebbbceSChristoph Hellwig static int sas_phy_match(struct attribute_container *cont, struct device *dev)
713c7ebbbceSChristoph Hellwig {
714c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
715c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
716c7ebbbceSChristoph Hellwig 
717c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_phy(dev))
718c7ebbbceSChristoph Hellwig 		return 0;
719c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent);
720c7ebbbceSChristoph Hellwig 
721c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
722c7ebbbceSChristoph Hellwig 		return 0;
723c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
724c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
725c7ebbbceSChristoph Hellwig 		return 0;
726c7ebbbceSChristoph Hellwig 
727c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
728c7ebbbceSChristoph Hellwig 	return &i->phy_attr_cont.ac == cont;
729c7ebbbceSChristoph Hellwig }
730c7ebbbceSChristoph Hellwig 
731c7ebbbceSChristoph Hellwig static void sas_phy_release(struct device *dev)
732c7ebbbceSChristoph Hellwig {
733c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(dev);
7340b3e09daSDan Williams 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
7350b3e09daSDan Williams 	struct sas_internal *i = to_sas_internal(shost->transportt);
736c7ebbbceSChristoph Hellwig 
7370b3e09daSDan Williams 	if (i->f->phy_release)
7380b3e09daSDan Williams 		i->f->phy_release(phy);
739c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
740c7ebbbceSChristoph Hellwig 	kfree(phy);
741c7ebbbceSChristoph Hellwig }
742c7ebbbceSChristoph Hellwig 
743c7ebbbceSChristoph Hellwig /**
744eb44820cSRob Landley  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
745c7ebbbceSChristoph Hellwig  * @parent:	Parent device
746d99ca418SMoore, Eric  * @number:	Phy index
747c7ebbbceSChristoph Hellwig  *
748c7ebbbceSChristoph Hellwig  * Allocates an SAS PHY structure.  It will be added in the device tree
749c7ebbbceSChristoph Hellwig  * below the device specified by @parent, which has to be either a Scsi_Host
750c7ebbbceSChristoph Hellwig  * or sas_rphy.
751c7ebbbceSChristoph Hellwig  *
752c7ebbbceSChristoph Hellwig  * Returns:
753c7ebbbceSChristoph Hellwig  *	SAS PHY allocated or %NULL if the allocation failed.
754c7ebbbceSChristoph Hellwig  */
755c7ebbbceSChristoph Hellwig struct sas_phy *sas_phy_alloc(struct device *parent, int number)
756c7ebbbceSChristoph Hellwig {
757c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent);
758c7ebbbceSChristoph Hellwig 	struct sas_phy *phy;
759c7ebbbceSChristoph Hellwig 
76024669f75SJes Sorensen 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
761c7ebbbceSChristoph Hellwig 	if (!phy)
762c7ebbbceSChristoph Hellwig 		return NULL;
763c7ebbbceSChristoph Hellwig 
764c7ebbbceSChristoph Hellwig 	phy->number = number;
765acbf167dSDarrick J. Wong 	phy->enabled = 1;
766c7ebbbceSChristoph Hellwig 
767c7ebbbceSChristoph Hellwig 	device_initialize(&phy->dev);
768c7ebbbceSChristoph Hellwig 	phy->dev.parent = get_device(parent);
769c7ebbbceSChristoph Hellwig 	phy->dev.release = sas_phy_release;
77065c92b09SJames Bottomley 	INIT_LIST_HEAD(&phy->port_siblings);
77179cb1819SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
77279cb1819SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
77371610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
77479cb1819SJames Bottomley 			rphy->scsi_target_id, number);
77579cb1819SJames Bottomley 	} else
77671610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
777c7ebbbceSChristoph Hellwig 
778c7ebbbceSChristoph Hellwig 	transport_setup_device(&phy->dev);
779c7ebbbceSChristoph Hellwig 
780c7ebbbceSChristoph Hellwig 	return phy;
781c7ebbbceSChristoph Hellwig }
782c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_alloc);
783c7ebbbceSChristoph Hellwig 
784c7ebbbceSChristoph Hellwig /**
785eb44820cSRob Landley  * sas_phy_add  -  add a SAS PHY to the device hierarchy
786c7ebbbceSChristoph Hellwig  * @phy:	The PHY to be added
787c7ebbbceSChristoph Hellwig  *
788c7ebbbceSChristoph Hellwig  * Publishes a SAS PHY to the rest of the system.
789c7ebbbceSChristoph Hellwig  */
790c7ebbbceSChristoph Hellwig int sas_phy_add(struct sas_phy *phy)
791c7ebbbceSChristoph Hellwig {
792c7ebbbceSChristoph Hellwig 	int error;
793c7ebbbceSChristoph Hellwig 
794c7ebbbceSChristoph Hellwig 	error = device_add(&phy->dev);
795c7ebbbceSChristoph Hellwig 	if (!error) {
796c7ebbbceSChristoph Hellwig 		transport_add_device(&phy->dev);
797c7ebbbceSChristoph Hellwig 		transport_configure_device(&phy->dev);
798c7ebbbceSChristoph Hellwig 	}
799c7ebbbceSChristoph Hellwig 
800c7ebbbceSChristoph Hellwig 	return error;
801c7ebbbceSChristoph Hellwig }
802c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_add);
803c7ebbbceSChristoph Hellwig 
804c7ebbbceSChristoph Hellwig /**
805eb44820cSRob Landley  * sas_phy_free  -  free a SAS PHY
806c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to free
807c7ebbbceSChristoph Hellwig  *
808c7ebbbceSChristoph Hellwig  * Frees the specified SAS PHY.
809c7ebbbceSChristoph Hellwig  *
810c7ebbbceSChristoph Hellwig  * Note:
811c7ebbbceSChristoph Hellwig  *   This function must only be called on a PHY that has not
812af901ca1SAndré Goddard Rosa  *   successfully been added using sas_phy_add().
813c7ebbbceSChristoph Hellwig  */
814c7ebbbceSChristoph Hellwig void sas_phy_free(struct sas_phy *phy)
815c7ebbbceSChristoph Hellwig {
816c7ebbbceSChristoph Hellwig 	transport_destroy_device(&phy->dev);
81792aab646SMike Anderson 	put_device(&phy->dev);
818c7ebbbceSChristoph Hellwig }
819c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_free);
820c7ebbbceSChristoph Hellwig 
821c7ebbbceSChristoph Hellwig /**
822eb44820cSRob Landley  * sas_phy_delete  -  remove SAS PHY
823c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to remove
824c7ebbbceSChristoph Hellwig  *
825c7ebbbceSChristoph Hellwig  * Removes the specified SAS PHY.  If the SAS PHY has an
826c7ebbbceSChristoph Hellwig  * associated remote PHY it is removed before.
827c7ebbbceSChristoph Hellwig  */
828c7ebbbceSChristoph Hellwig void
829c7ebbbceSChristoph Hellwig sas_phy_delete(struct sas_phy *phy)
830c7ebbbceSChristoph Hellwig {
831c7ebbbceSChristoph Hellwig 	struct device *dev = &phy->dev;
832c7ebbbceSChristoph Hellwig 
83365c92b09SJames Bottomley 	/* this happens if the phy is still part of a port when deleted */
83465c92b09SJames Bottomley 	BUG_ON(!list_empty(&phy->port_siblings));
835c7ebbbceSChristoph Hellwig 
836c7ebbbceSChristoph Hellwig 	transport_remove_device(dev);
837c7ebbbceSChristoph Hellwig 	device_del(dev);
838c7ebbbceSChristoph Hellwig 	transport_destroy_device(dev);
83992aab646SMike Anderson 	put_device(dev);
840c7ebbbceSChristoph Hellwig }
841c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_delete);
842c7ebbbceSChristoph Hellwig 
843c7ebbbceSChristoph Hellwig /**
844eb44820cSRob Landley  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
845c7ebbbceSChristoph Hellwig  * @dev:	device to check
846c7ebbbceSChristoph Hellwig  *
847c7ebbbceSChristoph Hellwig  * Returns:
848c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS PHY, %0 else
849c7ebbbceSChristoph Hellwig  */
850c7ebbbceSChristoph Hellwig int scsi_is_sas_phy(const struct device *dev)
851c7ebbbceSChristoph Hellwig {
852c7ebbbceSChristoph Hellwig 	return dev->release == sas_phy_release;
853c7ebbbceSChristoph Hellwig }
854c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_phy);
855c7ebbbceSChristoph Hellwig 
856c7ebbbceSChristoph Hellwig /*
85765c92b09SJames Bottomley  * SAS Port attributes
85865c92b09SJames Bottomley  */
85965c92b09SJames Bottomley #define sas_port_show_simple(field, name, format_string, cast)		\
86065c92b09SJames Bottomley static ssize_t								\
861ee959b00STony Jones show_sas_port_##name(struct device *dev, 				\
862ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
86365c92b09SJames Bottomley {									\
864ee959b00STony Jones 	struct sas_port *port = transport_class_to_sas_port(dev);	\
86565c92b09SJames Bottomley 									\
86665c92b09SJames Bottomley 	return snprintf(buf, 20, format_string, cast port->field);	\
86765c92b09SJames Bottomley }
86865c92b09SJames Bottomley 
86965c92b09SJames Bottomley #define sas_port_simple_attr(field, name, format_string, type)		\
87065c92b09SJames Bottomley 	sas_port_show_simple(field, name, format_string, (type))	\
871ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
87265c92b09SJames Bottomley 
87365c92b09SJames Bottomley sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
87465c92b09SJames Bottomley 
87565c92b09SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_port_class,
87665c92b09SJames Bottomley 			       "sas_port", NULL, NULL, NULL);
87765c92b09SJames Bottomley 
87865c92b09SJames Bottomley static int sas_port_match(struct attribute_container *cont, struct device *dev)
87965c92b09SJames Bottomley {
88065c92b09SJames Bottomley 	struct Scsi_Host *shost;
88165c92b09SJames Bottomley 	struct sas_internal *i;
88265c92b09SJames Bottomley 
88365c92b09SJames Bottomley 	if (!scsi_is_sas_port(dev))
88465c92b09SJames Bottomley 		return 0;
88565c92b09SJames Bottomley 	shost = dev_to_shost(dev->parent);
88665c92b09SJames Bottomley 
88765c92b09SJames Bottomley 	if (!shost->transportt)
88865c92b09SJames Bottomley 		return 0;
88965c92b09SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
89065c92b09SJames Bottomley 			&sas_host_class.class)
89165c92b09SJames Bottomley 		return 0;
89265c92b09SJames Bottomley 
89365c92b09SJames Bottomley 	i = to_sas_internal(shost->transportt);
89465c92b09SJames Bottomley 	return &i->port_attr_cont.ac == cont;
89565c92b09SJames Bottomley }
89665c92b09SJames Bottomley 
89765c92b09SJames Bottomley 
89865c92b09SJames Bottomley static void sas_port_release(struct device *dev)
89965c92b09SJames Bottomley {
90065c92b09SJames Bottomley 	struct sas_port *port = dev_to_sas_port(dev);
90165c92b09SJames Bottomley 
90265c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
90365c92b09SJames Bottomley 
90465c92b09SJames Bottomley 	put_device(dev->parent);
90565c92b09SJames Bottomley 	kfree(port);
90665c92b09SJames Bottomley }
90765c92b09SJames Bottomley 
90865c92b09SJames Bottomley static void sas_port_create_link(struct sas_port *port,
90965c92b09SJames Bottomley 				 struct sas_phy *phy)
91065c92b09SJames Bottomley {
91121434966SDarrick J. Wong 	int res;
91221434966SDarrick J. Wong 
91321434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
91471610f55SKay Sievers 				dev_name(&phy->dev));
91521434966SDarrick J. Wong 	if (res)
91621434966SDarrick J. Wong 		goto err;
91721434966SDarrick J. Wong 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
91821434966SDarrick J. Wong 	if (res)
91921434966SDarrick J. Wong 		goto err;
92021434966SDarrick J. Wong 	return;
92121434966SDarrick J. Wong err:
92221434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
923cadbd4a5SHarvey Harrison 	       __func__, res);
92465c92b09SJames Bottomley }
92565c92b09SJames Bottomley 
92665c92b09SJames Bottomley static void sas_port_delete_link(struct sas_port *port,
92765c92b09SJames Bottomley 				 struct sas_phy *phy)
92865c92b09SJames Bottomley {
92971610f55SKay Sievers 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
93065c92b09SJames Bottomley 	sysfs_remove_link(&phy->dev.kobj, "port");
93165c92b09SJames Bottomley }
93265c92b09SJames Bottomley 
93365c92b09SJames Bottomley /** sas_port_alloc - allocate and initialize a SAS port structure
93465c92b09SJames Bottomley  *
93565c92b09SJames Bottomley  * @parent:	parent device
93665c92b09SJames Bottomley  * @port_id:	port number
93765c92b09SJames Bottomley  *
93865c92b09SJames Bottomley  * Allocates a SAS port structure.  It will be added to the device tree
93965c92b09SJames Bottomley  * below the device specified by @parent which must be either a Scsi_Host
94065c92b09SJames Bottomley  * or a sas_expander_device.
94165c92b09SJames Bottomley  *
94265c92b09SJames Bottomley  * Returns %NULL on error
94365c92b09SJames Bottomley  */
94465c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *parent, int port_id)
94565c92b09SJames Bottomley {
94665c92b09SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
94765c92b09SJames Bottomley 	struct sas_port *port;
94865c92b09SJames Bottomley 
94965c92b09SJames Bottomley 	port = kzalloc(sizeof(*port), GFP_KERNEL);
95065c92b09SJames Bottomley 	if (!port)
95165c92b09SJames Bottomley 		return NULL;
95265c92b09SJames Bottomley 
95365c92b09SJames Bottomley 	port->port_identifier = port_id;
95465c92b09SJames Bottomley 
95565c92b09SJames Bottomley 	device_initialize(&port->dev);
95665c92b09SJames Bottomley 
95765c92b09SJames Bottomley 	port->dev.parent = get_device(parent);
95865c92b09SJames Bottomley 	port->dev.release = sas_port_release;
95965c92b09SJames Bottomley 
96065c92b09SJames Bottomley 	mutex_init(&port->phy_list_mutex);
96165c92b09SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
96265c92b09SJames Bottomley 
96365c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
96465c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
96571610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
96665c92b09SJames Bottomley 			     rphy->scsi_target_id, port->port_identifier);
96765c92b09SJames Bottomley 	} else
96871610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
96965c92b09SJames Bottomley 			     port->port_identifier);
97065c92b09SJames Bottomley 
97165c92b09SJames Bottomley 	transport_setup_device(&port->dev);
97265c92b09SJames Bottomley 
97365c92b09SJames Bottomley 	return port;
97465c92b09SJames Bottomley }
97565c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_alloc);
97665c92b09SJames Bottomley 
977c9fefeb2SJames Bottomley /** sas_port_alloc_num - allocate and initialize a SAS port structure
978c9fefeb2SJames Bottomley  *
979c9fefeb2SJames Bottomley  * @parent:	parent device
980c9fefeb2SJames Bottomley  *
981c9fefeb2SJames Bottomley  * Allocates a SAS port structure and a number to go with it.  This
982c9fefeb2SJames Bottomley  * interface is really for adapters where the port number has no
983c9fefeb2SJames Bottomley  * meansing, so the sas class should manage them.  It will be added to
984c9fefeb2SJames Bottomley  * the device tree below the device specified by @parent which must be
985c9fefeb2SJames Bottomley  * either a Scsi_Host or a sas_expander_device.
986c9fefeb2SJames Bottomley  *
987c9fefeb2SJames Bottomley  * Returns %NULL on error
988c9fefeb2SJames Bottomley  */
989c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *parent)
990c9fefeb2SJames Bottomley {
991c9fefeb2SJames Bottomley 	int index;
992c9fefeb2SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
993c9fefeb2SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
994c9fefeb2SJames Bottomley 
995c9fefeb2SJames Bottomley 	/* FIXME: use idr for this eventually */
996c9fefeb2SJames Bottomley 	mutex_lock(&sas_host->lock);
997c9fefeb2SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
998c9fefeb2SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
999c9fefeb2SJames Bottomley 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
1000c9fefeb2SJames Bottomley 
1001c9fefeb2SJames Bottomley 		index = exp->next_port_id++;
1002c9fefeb2SJames Bottomley 	} else
1003c9fefeb2SJames Bottomley 		index = sas_host->next_port_id++;
1004c9fefeb2SJames Bottomley 	mutex_unlock(&sas_host->lock);
1005c9fefeb2SJames Bottomley 	return sas_port_alloc(parent, index);
1006c9fefeb2SJames Bottomley }
1007c9fefeb2SJames Bottomley EXPORT_SYMBOL(sas_port_alloc_num);
1008c9fefeb2SJames Bottomley 
100965c92b09SJames Bottomley /**
101065c92b09SJames Bottomley  * sas_port_add - add a SAS port to the device hierarchy
101165c92b09SJames Bottomley  * @port:	port to be added
101265c92b09SJames Bottomley  *
101365c92b09SJames Bottomley  * publishes a port to the rest of the system
101465c92b09SJames Bottomley  */
101565c92b09SJames Bottomley int sas_port_add(struct sas_port *port)
101665c92b09SJames Bottomley {
101765c92b09SJames Bottomley 	int error;
101865c92b09SJames Bottomley 
101965c92b09SJames Bottomley 	/* No phys should be added until this is made visible */
102065c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
102165c92b09SJames Bottomley 
102265c92b09SJames Bottomley 	error = device_add(&port->dev);
102365c92b09SJames Bottomley 
102465c92b09SJames Bottomley 	if (error)
102565c92b09SJames Bottomley 		return error;
102665c92b09SJames Bottomley 
102765c92b09SJames Bottomley 	transport_add_device(&port->dev);
102865c92b09SJames Bottomley 	transport_configure_device(&port->dev);
102965c92b09SJames Bottomley 
103065c92b09SJames Bottomley 	return 0;
103165c92b09SJames Bottomley }
103265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add);
103365c92b09SJames Bottomley 
103465c92b09SJames Bottomley /**
1035eb44820cSRob Landley  * sas_port_free  -  free a SAS PORT
103665c92b09SJames Bottomley  * @port:	SAS PORT to free
103765c92b09SJames Bottomley  *
103865c92b09SJames Bottomley  * Frees the specified SAS PORT.
103965c92b09SJames Bottomley  *
104065c92b09SJames Bottomley  * Note:
104165c92b09SJames Bottomley  *   This function must only be called on a PORT that has not
1042af901ca1SAndré Goddard Rosa  *   successfully been added using sas_port_add().
104365c92b09SJames Bottomley  */
104465c92b09SJames Bottomley void sas_port_free(struct sas_port *port)
104565c92b09SJames Bottomley {
104665c92b09SJames Bottomley 	transport_destroy_device(&port->dev);
104765c92b09SJames Bottomley 	put_device(&port->dev);
104865c92b09SJames Bottomley }
104965c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_free);
105065c92b09SJames Bottomley 
105165c92b09SJames Bottomley /**
1052eb44820cSRob Landley  * sas_port_delete  -  remove SAS PORT
105365c92b09SJames Bottomley  * @port:	SAS PORT to remove
105465c92b09SJames Bottomley  *
105565c92b09SJames Bottomley  * Removes the specified SAS PORT.  If the SAS PORT has an
105665c92b09SJames Bottomley  * associated phys, unlink them from the port as well.
105765c92b09SJames Bottomley  */
105865c92b09SJames Bottomley void sas_port_delete(struct sas_port *port)
105965c92b09SJames Bottomley {
106065c92b09SJames Bottomley 	struct device *dev = &port->dev;
106165c92b09SJames Bottomley 	struct sas_phy *phy, *tmp_phy;
106265c92b09SJames Bottomley 
106365c92b09SJames Bottomley 	if (port->rphy) {
106465c92b09SJames Bottomley 		sas_rphy_delete(port->rphy);
106565c92b09SJames Bottomley 		port->rphy = NULL;
106665c92b09SJames Bottomley 	}
106765c92b09SJames Bottomley 
106865c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
106965c92b09SJames Bottomley 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
107065c92b09SJames Bottomley 				 port_siblings) {
107165c92b09SJames Bottomley 		sas_port_delete_link(port, phy);
107265c92b09SJames Bottomley 		list_del_init(&phy->port_siblings);
107365c92b09SJames Bottomley 	}
107465c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
107565c92b09SJames Bottomley 
1076a0e1b6efSJames Bottomley 	if (port->is_backlink) {
1077a0e1b6efSJames Bottomley 		struct device *parent = port->dev.parent;
1078a0e1b6efSJames Bottomley 
107971610f55SKay Sievers 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1080a0e1b6efSJames Bottomley 		port->is_backlink = 0;
1081a0e1b6efSJames Bottomley 	}
1082a0e1b6efSJames Bottomley 
108365c92b09SJames Bottomley 	transport_remove_device(dev);
108465c92b09SJames Bottomley 	device_del(dev);
108565c92b09SJames Bottomley 	transport_destroy_device(dev);
108665c92b09SJames Bottomley 	put_device(dev);
108765c92b09SJames Bottomley }
108865c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete);
108965c92b09SJames Bottomley 
109065c92b09SJames Bottomley /**
1091eb44820cSRob Landley  * scsi_is_sas_port -  check if a struct device represents a SAS port
109265c92b09SJames Bottomley  * @dev:	device to check
109365c92b09SJames Bottomley  *
109465c92b09SJames Bottomley  * Returns:
109565c92b09SJames Bottomley  *	%1 if the device represents a SAS Port, %0 else
109665c92b09SJames Bottomley  */
109765c92b09SJames Bottomley int scsi_is_sas_port(const struct device *dev)
109865c92b09SJames Bottomley {
109965c92b09SJames Bottomley 	return dev->release == sas_port_release;
110065c92b09SJames Bottomley }
110165c92b09SJames Bottomley EXPORT_SYMBOL(scsi_is_sas_port);
110265c92b09SJames Bottomley 
110365c92b09SJames Bottomley /**
1104f41a0c44SDan Williams  * sas_port_get_phy - try to take a reference on a port member
1105f41a0c44SDan Williams  * @port: port to check
1106f41a0c44SDan Williams  */
1107f41a0c44SDan Williams struct sas_phy *sas_port_get_phy(struct sas_port *port)
1108f41a0c44SDan Williams {
1109f41a0c44SDan Williams 	struct sas_phy *phy;
1110f41a0c44SDan Williams 
1111f41a0c44SDan Williams 	mutex_lock(&port->phy_list_mutex);
1112f41a0c44SDan Williams 	if (list_empty(&port->phy_list))
1113f41a0c44SDan Williams 		phy = NULL;
1114f41a0c44SDan Williams 	else {
1115f41a0c44SDan Williams 		struct list_head *ent = port->phy_list.next;
1116f41a0c44SDan Williams 
1117f41a0c44SDan Williams 		phy = list_entry(ent, typeof(*phy), port_siblings);
1118f41a0c44SDan Williams 		get_device(&phy->dev);
1119f41a0c44SDan Williams 	}
1120f41a0c44SDan Williams 	mutex_unlock(&port->phy_list_mutex);
1121f41a0c44SDan Williams 
1122f41a0c44SDan Williams 	return phy;
1123f41a0c44SDan Williams }
1124f41a0c44SDan Williams EXPORT_SYMBOL(sas_port_get_phy);
1125f41a0c44SDan Williams 
1126f41a0c44SDan Williams /**
112765c92b09SJames Bottomley  * sas_port_add_phy - add another phy to a port to form a wide port
112865c92b09SJames Bottomley  * @port:	port to add the phy to
112965c92b09SJames Bottomley  * @phy:	phy to add
113065c92b09SJames Bottomley  *
113165c92b09SJames Bottomley  * When a port is initially created, it is empty (has no phys).  All
113265c92b09SJames Bottomley  * ports must have at least one phy to operated, and all wide ports
113365c92b09SJames Bottomley  * must have at least two.  The current code makes no difference
113465c92b09SJames Bottomley  * between ports and wide ports, but the only object that can be
113565c92b09SJames Bottomley  * connected to a remote device is a port, so ports must be formed on
113665c92b09SJames Bottomley  * all devices with phys if they're connected to anything.
113765c92b09SJames Bottomley  */
113865c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
113965c92b09SJames Bottomley {
114065c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
114165c92b09SJames Bottomley 	if (unlikely(!list_empty(&phy->port_siblings))) {
114265c92b09SJames Bottomley 		/* make sure we're already on this port */
114365c92b09SJames Bottomley 		struct sas_phy *tmp;
114465c92b09SJames Bottomley 
114565c92b09SJames Bottomley 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
114665c92b09SJames Bottomley 			if (tmp == phy)
114765c92b09SJames Bottomley 				break;
114865c92b09SJames Bottomley 		/* If this trips, you added a phy that was already
114965c92b09SJames Bottomley 		 * part of a different port */
115065c92b09SJames Bottomley 		if (unlikely(tmp != phy)) {
115171610f55SKay Sievers 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
115271610f55SKay Sievers 				   dev_name(&phy->dev));
115365c92b09SJames Bottomley 			BUG();
115465c92b09SJames Bottomley 		}
115565c92b09SJames Bottomley 	} else {
115665c92b09SJames Bottomley 		sas_port_create_link(port, phy);
115765c92b09SJames Bottomley 		list_add_tail(&phy->port_siblings, &port->phy_list);
115865c92b09SJames Bottomley 		port->num_phys++;
115965c92b09SJames Bottomley 	}
116065c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
116165c92b09SJames Bottomley }
116265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add_phy);
116365c92b09SJames Bottomley 
116465c92b09SJames Bottomley /**
116565c92b09SJames Bottomley  * sas_port_delete_phy - remove a phy from a port or wide port
116665c92b09SJames Bottomley  * @port:	port to remove the phy from
116765c92b09SJames Bottomley  * @phy:	phy to remove
116865c92b09SJames Bottomley  *
116965c92b09SJames Bottomley  * This operation is used for tearing down ports again.  It must be
117065c92b09SJames Bottomley  * done to every port or wide port before calling sas_port_delete.
117165c92b09SJames Bottomley  */
117265c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
117365c92b09SJames Bottomley {
117465c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
117565c92b09SJames Bottomley 	sas_port_delete_link(port, phy);
117665c92b09SJames Bottomley 	list_del_init(&phy->port_siblings);
117765c92b09SJames Bottomley 	port->num_phys--;
117865c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
117965c92b09SJames Bottomley }
118065c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete_phy);
118165c92b09SJames Bottomley 
1182a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *port)
1183a0e1b6efSJames Bottomley {
118421434966SDarrick J. Wong 	int res;
1185a0e1b6efSJames Bottomley 	struct device *parent = port->dev.parent->parent->parent;
1186a0e1b6efSJames Bottomley 
1187a0e1b6efSJames Bottomley 	if (port->is_backlink)
1188a0e1b6efSJames Bottomley 		return;
1189a0e1b6efSJames Bottomley 	port->is_backlink = 1;
119021434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
119171610f55SKay Sievers 				dev_name(parent));
119221434966SDarrick J. Wong 	if (res)
119321434966SDarrick J. Wong 		goto err;
119421434966SDarrick J. Wong 	return;
119521434966SDarrick J. Wong err:
119621434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1197cadbd4a5SHarvey Harrison 	       __func__, res);
1198a0e1b6efSJames Bottomley 
1199a0e1b6efSJames Bottomley }
1200a0e1b6efSJames Bottomley EXPORT_SYMBOL(sas_port_mark_backlink);
1201a0e1b6efSJames Bottomley 
120265c92b09SJames Bottomley /*
1203c7ebbbceSChristoph Hellwig  * SAS remote PHY attributes.
1204c7ebbbceSChristoph Hellwig  */
1205c7ebbbceSChristoph Hellwig 
1206c7ebbbceSChristoph Hellwig #define sas_rphy_show_simple(field, name, format_string, cast)		\
1207c7ebbbceSChristoph Hellwig static ssize_t								\
1208ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1209ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1210c7ebbbceSChristoph Hellwig {									\
1211ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1212c7ebbbceSChristoph Hellwig 									\
1213c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1214c7ebbbceSChristoph Hellwig }
1215c7ebbbceSChristoph Hellwig 
1216c7ebbbceSChristoph Hellwig #define sas_rphy_simple_attr(field, name, format_string, type)		\
1217c7ebbbceSChristoph Hellwig 	sas_rphy_show_simple(field, name, format_string, (type))	\
1218ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1219c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1220c7ebbbceSChristoph Hellwig 
1221c7ebbbceSChristoph Hellwig #define sas_rphy_show_protocol(field, name)				\
1222c7ebbbceSChristoph Hellwig static ssize_t								\
1223ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1224ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1225c7ebbbceSChristoph Hellwig {									\
1226ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1227c7ebbbceSChristoph Hellwig 									\
1228c7ebbbceSChristoph Hellwig 	if (!rphy->field)					\
1229c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
1230c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(rphy->field, buf);	\
1231c7ebbbceSChristoph Hellwig }
1232c7ebbbceSChristoph Hellwig 
1233c7ebbbceSChristoph Hellwig #define sas_rphy_protocol_attr(field, name)				\
1234c7ebbbceSChristoph Hellwig 	sas_rphy_show_protocol(field, name)				\
1235ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1236c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1237c7ebbbceSChristoph Hellwig 
1238c7ebbbceSChristoph Hellwig static ssize_t
1239ee959b00STony Jones show_sas_rphy_device_type(struct device *dev,
1240ee959b00STony Jones 			  struct device_attribute *attr, char *buf)
1241c7ebbbceSChristoph Hellwig {
1242ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1243c7ebbbceSChristoph Hellwig 
1244c7ebbbceSChristoph Hellwig 	if (!rphy->identify.device_type)
1245c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
1246c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(
1247c7ebbbceSChristoph Hellwig 			rphy->identify.device_type, buf);
1248c7ebbbceSChristoph Hellwig }
1249c7ebbbceSChristoph Hellwig 
1250ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1251c7ebbbceSChristoph Hellwig 		show_sas_rphy_device_type, NULL);
1252c7ebbbceSChristoph Hellwig 
1253a0125641SChristoph Hellwig static ssize_t
1254ee959b00STony Jones show_sas_rphy_enclosure_identifier(struct device *dev,
1255ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
1256a0125641SChristoph Hellwig {
1257ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1258a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1259a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1260a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1261a0125641SChristoph Hellwig 	u64 identifier;
1262a0125641SChristoph Hellwig 	int error;
1263a0125641SChristoph Hellwig 
1264a0125641SChristoph Hellwig 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1265a0125641SChristoph Hellwig 	if (error)
1266a0125641SChristoph Hellwig 		return error;
1267a0125641SChristoph Hellwig 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1268a0125641SChristoph Hellwig }
1269a0125641SChristoph Hellwig 
1270ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1271a0125641SChristoph Hellwig 		show_sas_rphy_enclosure_identifier, NULL);
1272a0125641SChristoph Hellwig 
1273a0125641SChristoph Hellwig static ssize_t
1274ee959b00STony Jones show_sas_rphy_bay_identifier(struct device *dev,
1275ee959b00STony Jones 			     struct device_attribute *attr, char *buf)
1276a0125641SChristoph Hellwig {
1277ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1278a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1279a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1280a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1281a0125641SChristoph Hellwig 	int val;
1282a0125641SChristoph Hellwig 
1283a0125641SChristoph Hellwig 	val = i->f->get_bay_identifier(rphy);
1284a0125641SChristoph Hellwig 	if (val < 0)
1285a0125641SChristoph Hellwig 		return val;
1286a0125641SChristoph Hellwig 	return sprintf(buf, "%d\n", val);
1287a0125641SChristoph Hellwig }
1288a0125641SChristoph Hellwig 
1289ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1290a0125641SChristoph Hellwig 		show_sas_rphy_bay_identifier, NULL);
1291a0125641SChristoph Hellwig 
1292c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.initiator_port_protocols,
1293c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
1294c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1295c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1296c7ebbbceSChristoph Hellwig 		unsigned long long);
1297c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1298cdc43ae3SHannes Reinecke sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
1299c7ebbbceSChristoph Hellwig 
130042ab0360SJames Bottomley /* only need 8 bytes of data plus header (4 or 8) */
130142ab0360SJames Bottomley #define BUF_SIZE 64
130242ab0360SJames Bottomley 
130342ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *sdev)
130442ab0360SJames Bottomley {
130542ab0360SJames Bottomley 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
13060f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
130742ab0360SJames Bottomley 	struct scsi_mode_data mode_data;
130842ab0360SJames Bottomley 	int res, error;
130942ab0360SJames Bottomley 
131042ab0360SJames Bottomley 	if (!buffer)
131142ab0360SJames Bottomley 		return -ENOMEM;
131242ab0360SJames Bottomley 
131342ab0360SJames Bottomley 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
131442ab0360SJames Bottomley 			      &mode_data, NULL);
131542ab0360SJames Bottomley 
131642ab0360SJames Bottomley 	error = -EINVAL;
131742ab0360SJames Bottomley 	if (!scsi_status_is_good(res))
131842ab0360SJames Bottomley 		goto out;
131942ab0360SJames Bottomley 
132042ab0360SJames Bottomley 	msdata = buffer +  mode_data.header_length +
132142ab0360SJames Bottomley 		mode_data.block_descriptor_length;
132242ab0360SJames Bottomley 
132342ab0360SJames Bottomley 	if (msdata - buffer > BUF_SIZE - 8)
132442ab0360SJames Bottomley 		goto out;
132542ab0360SJames Bottomley 
132642ab0360SJames Bottomley 	error = 0;
132742ab0360SJames Bottomley 
132842ab0360SJames Bottomley 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
132942ab0360SJames Bottomley 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
133042ab0360SJames Bottomley 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
133142ab0360SJames Bottomley 
133242ab0360SJames Bottomley  out:
133342ab0360SJames Bottomley 	kfree(buffer);
133442ab0360SJames Bottomley 	return error;
133542ab0360SJames Bottomley }
133642ab0360SJames Bottomley EXPORT_SYMBOL(sas_read_port_mode_page);
133742ab0360SJames Bottomley 
133879cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
133979cb1819SJames Bottomley 			       "sas_end_device", NULL, NULL, NULL);
134079cb1819SJames Bottomley 
134142ab0360SJames Bottomley #define sas_end_dev_show_simple(field, name, format_string, cast)	\
134242ab0360SJames Bottomley static ssize_t								\
1343ee959b00STony Jones show_sas_end_dev_##name(struct device *dev, 				\
1344ee959b00STony Jones 			struct device_attribute *attr, char *buf)	\
134542ab0360SJames Bottomley {									\
1346ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
134742ab0360SJames Bottomley 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
134842ab0360SJames Bottomley 									\
134942ab0360SJames Bottomley 	return snprintf(buf, 20, format_string, cast rdev->field);	\
135042ab0360SJames Bottomley }
135142ab0360SJames Bottomley 
135242ab0360SJames Bottomley #define sas_end_dev_simple_attr(field, name, format_string, type)	\
135342ab0360SJames Bottomley 	sas_end_dev_show_simple(field, name, format_string, (type))	\
1354ee959b00STony Jones static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
135542ab0360SJames Bottomley 		show_sas_end_dev_##name, NULL)
135642ab0360SJames Bottomley 
135742ab0360SJames Bottomley sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
135842ab0360SJames Bottomley sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
135942ab0360SJames Bottomley 			"%d\n", int);
136042ab0360SJames Bottomley sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
136142ab0360SJames Bottomley 			"%d\n", int);
13620f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_supported, tlr_supported,
13630f88009dSJames Bottomley 			"%d\n", int);
13640f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
13650f88009dSJames Bottomley 			"%d\n", int);
136642ab0360SJames Bottomley 
136779cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_expander_class,
136879cb1819SJames Bottomley 			       "sas_expander", NULL, NULL, NULL);
136979cb1819SJames Bottomley 
137079cb1819SJames Bottomley #define sas_expander_show_simple(field, name, format_string, cast)	\
137179cb1819SJames Bottomley static ssize_t								\
1372ee959b00STony Jones show_sas_expander_##name(struct device *dev, 				\
1373ee959b00STony Jones 			 struct device_attribute *attr, char *buf)	\
137479cb1819SJames Bottomley {									\
1375ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
137679cb1819SJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
137779cb1819SJames Bottomley 									\
137879cb1819SJames Bottomley 	return snprintf(buf, 20, format_string, cast edev->field);	\
137979cb1819SJames Bottomley }
138079cb1819SJames Bottomley 
138179cb1819SJames Bottomley #define sas_expander_simple_attr(field, name, format_string, type)	\
138279cb1819SJames Bottomley 	sas_expander_show_simple(field, name, format_string, (type))	\
1383ee959b00STony Jones static SAS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
138479cb1819SJames Bottomley 		show_sas_expander_##name, NULL)
138579cb1819SJames Bottomley 
138679cb1819SJames Bottomley sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
138779cb1819SJames Bottomley sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
138879cb1819SJames Bottomley sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
138979cb1819SJames Bottomley sas_expander_simple_attr(component_vendor_id, component_vendor_id,
139079cb1819SJames Bottomley 			 "%s\n", char *);
139179cb1819SJames Bottomley sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
139279cb1819SJames Bottomley sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
139379cb1819SJames Bottomley 			 unsigned int);
139479cb1819SJames Bottomley sas_expander_simple_attr(level, level, "%d\n", int);
139542ab0360SJames Bottomley 
1396c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
13972f8600dfSJames Bottomley 		"sas_device", NULL, NULL, NULL);
1398c7ebbbceSChristoph Hellwig 
1399c7ebbbceSChristoph Hellwig static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1400c7ebbbceSChristoph Hellwig {
1401c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
1402c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1403c7ebbbceSChristoph Hellwig 
1404c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_rphy(dev))
1405c7ebbbceSChristoph Hellwig 		return 0;
1406c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent->parent);
1407c7ebbbceSChristoph Hellwig 
1408c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
1409c7ebbbceSChristoph Hellwig 		return 0;
1410c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
1411c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
1412c7ebbbceSChristoph Hellwig 		return 0;
1413c7ebbbceSChristoph Hellwig 
1414c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
1415c7ebbbceSChristoph Hellwig 	return &i->rphy_attr_cont.ac == cont;
1416c7ebbbceSChristoph Hellwig }
1417c7ebbbceSChristoph Hellwig 
141842ab0360SJames Bottomley static int sas_end_dev_match(struct attribute_container *cont,
141942ab0360SJames Bottomley 			     struct device *dev)
142042ab0360SJames Bottomley {
142142ab0360SJames Bottomley 	struct Scsi_Host *shost;
142242ab0360SJames Bottomley 	struct sas_internal *i;
142342ab0360SJames Bottomley 	struct sas_rphy *rphy;
142442ab0360SJames Bottomley 
142542ab0360SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
142642ab0360SJames Bottomley 		return 0;
142742ab0360SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
142842ab0360SJames Bottomley 	rphy = dev_to_rphy(dev);
142942ab0360SJames Bottomley 
143042ab0360SJames Bottomley 	if (!shost->transportt)
143142ab0360SJames Bottomley 		return 0;
143242ab0360SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
143342ab0360SJames Bottomley 			&sas_host_class.class)
143442ab0360SJames Bottomley 		return 0;
143542ab0360SJames Bottomley 
143642ab0360SJames Bottomley 	i = to_sas_internal(shost->transportt);
143742ab0360SJames Bottomley 	return &i->end_dev_attr_cont.ac == cont &&
14382f8600dfSJames Bottomley 		rphy->identify.device_type == SAS_END_DEVICE;
143942ab0360SJames Bottomley }
144042ab0360SJames Bottomley 
144179cb1819SJames Bottomley static int sas_expander_match(struct attribute_container *cont,
144279cb1819SJames Bottomley 			      struct device *dev)
144379cb1819SJames Bottomley {
144479cb1819SJames Bottomley 	struct Scsi_Host *shost;
144579cb1819SJames Bottomley 	struct sas_internal *i;
144679cb1819SJames Bottomley 	struct sas_rphy *rphy;
144779cb1819SJames Bottomley 
144879cb1819SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
144979cb1819SJames Bottomley 		return 0;
145079cb1819SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
145179cb1819SJames Bottomley 	rphy = dev_to_rphy(dev);
145279cb1819SJames Bottomley 
145379cb1819SJames Bottomley 	if (!shost->transportt)
145479cb1819SJames Bottomley 		return 0;
145579cb1819SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
145679cb1819SJames Bottomley 			&sas_host_class.class)
145779cb1819SJames Bottomley 		return 0;
145879cb1819SJames Bottomley 
145979cb1819SJames Bottomley 	i = to_sas_internal(shost->transportt);
146079cb1819SJames Bottomley 	return &i->expander_attr_cont.ac == cont &&
146179cb1819SJames Bottomley 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
14622f8600dfSJames Bottomley 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
146379cb1819SJames Bottomley }
146479cb1819SJames Bottomley 
14652f8600dfSJames Bottomley static void sas_expander_release(struct device *dev)
1466c7ebbbceSChristoph Hellwig {
1467c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = dev_to_rphy(dev);
14682f8600dfSJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1469c7ebbbceSChristoph Hellwig 
147093c20a59SFUJITA Tomonori 	if (rphy->q)
147193c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
147293c20a59SFUJITA Tomonori 
1473c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
14742f8600dfSJames Bottomley 	kfree(edev);
1475c7ebbbceSChristoph Hellwig }
1476c7ebbbceSChristoph Hellwig 
14772f8600dfSJames Bottomley static void sas_end_device_release(struct device *dev)
1478c7ebbbceSChristoph Hellwig {
14792f8600dfSJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(dev);
14802f8600dfSJames Bottomley 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1481c7ebbbceSChristoph Hellwig 
148293c20a59SFUJITA Tomonori 	if (rphy->q)
148393c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
148493c20a59SFUJITA Tomonori 
14852f8600dfSJames Bottomley 	put_device(dev->parent);
14862f8600dfSJames Bottomley 	kfree(edev);
1487c7ebbbceSChristoph Hellwig }
1488c7ebbbceSChristoph Hellwig 
1489c7ebbbceSChristoph Hellwig /**
1490183b8021SMasahiro Yamada  * sas_rphy_initialize - common rphy initialization
1491c5943d36SJames Bottomley  * @rphy:	rphy to initialise
1492c5943d36SJames Bottomley  *
1493c5943d36SJames Bottomley  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1494c5943d36SJames Bottomley  * initialise the common rphy component of each.
1495c5943d36SJames Bottomley  */
1496c5943d36SJames Bottomley static void sas_rphy_initialize(struct sas_rphy *rphy)
1497c5943d36SJames Bottomley {
1498c5943d36SJames Bottomley 	INIT_LIST_HEAD(&rphy->list);
1499c5943d36SJames Bottomley }
1500c5943d36SJames Bottomley 
1501c5943d36SJames Bottomley /**
150242ab0360SJames Bottomley  * sas_end_device_alloc - allocate an rphy for an end device
1503eb44820cSRob Landley  * @parent: which port
150442ab0360SJames Bottomley  *
150542ab0360SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
150642ab0360SJames Bottomley  *
150742ab0360SJames Bottomley  * Returns:
150842ab0360SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
150942ab0360SJames Bottomley  */
151065c92b09SJames Bottomley struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
151142ab0360SJames Bottomley {
151242ab0360SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
151342ab0360SJames Bottomley 	struct sas_end_device *rdev;
151442ab0360SJames Bottomley 
151542ab0360SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
151642ab0360SJames Bottomley 	if (!rdev) {
151742ab0360SJames Bottomley 		return NULL;
151842ab0360SJames Bottomley 	}
151942ab0360SJames Bottomley 
152042ab0360SJames Bottomley 	device_initialize(&rdev->rphy.dev);
152142ab0360SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
15222f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_end_device_release;
152365c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
152465c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
152571610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
152671610f55SKay Sievers 			     shost->host_no, rphy->scsi_target_id,
152771610f55SKay Sievers 			     parent->port_identifier);
152865c92b09SJames Bottomley 	} else
152971610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
153065c92b09SJames Bottomley 			     shost->host_no, parent->port_identifier);
153142ab0360SJames Bottomley 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1532c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
153342ab0360SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
153442ab0360SJames Bottomley 
153542ab0360SJames Bottomley 	return &rdev->rphy;
153642ab0360SJames Bottomley }
153742ab0360SJames Bottomley EXPORT_SYMBOL(sas_end_device_alloc);
153842ab0360SJames Bottomley 
153979cb1819SJames Bottomley /**
154079cb1819SJames Bottomley  * sas_expander_alloc - allocate an rphy for an end device
1541eb44820cSRob Landley  * @parent: which port
1542eb44820cSRob Landley  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
154379cb1819SJames Bottomley  *
154479cb1819SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
154579cb1819SJames Bottomley  *
154679cb1819SJames Bottomley  * Returns:
154779cb1819SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
154879cb1819SJames Bottomley  */
154965c92b09SJames Bottomley struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
155079cb1819SJames Bottomley 				    enum sas_device_type type)
155179cb1819SJames Bottomley {
155279cb1819SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
155379cb1819SJames Bottomley 	struct sas_expander_device *rdev;
155479cb1819SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
155579cb1819SJames Bottomley 
155679cb1819SJames Bottomley 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
155779cb1819SJames Bottomley 	       type != SAS_FANOUT_EXPANDER_DEVICE);
155879cb1819SJames Bottomley 
155979cb1819SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
156079cb1819SJames Bottomley 	if (!rdev) {
156179cb1819SJames Bottomley 		return NULL;
156279cb1819SJames Bottomley 	}
156379cb1819SJames Bottomley 
156479cb1819SJames Bottomley 	device_initialize(&rdev->rphy.dev);
156579cb1819SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
15662f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_expander_release;
156779cb1819SJames Bottomley 	mutex_lock(&sas_host->lock);
156879cb1819SJames Bottomley 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
156979cb1819SJames Bottomley 	mutex_unlock(&sas_host->lock);
157071610f55SKay Sievers 	dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
157179cb1819SJames Bottomley 		     shost->host_no, rdev->rphy.scsi_target_id);
157279cb1819SJames Bottomley 	rdev->rphy.identify.device_type = type;
1573c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
157479cb1819SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
157579cb1819SJames Bottomley 
157679cb1819SJames Bottomley 	return &rdev->rphy;
157779cb1819SJames Bottomley }
157879cb1819SJames Bottomley EXPORT_SYMBOL(sas_expander_alloc);
157942ab0360SJames Bottomley 
158042ab0360SJames Bottomley /**
1581eb44820cSRob Landley  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1582c7ebbbceSChristoph Hellwig  * @rphy:	The remote PHY to be added
1583c7ebbbceSChristoph Hellwig  *
1584c7ebbbceSChristoph Hellwig  * Publishes a SAS remote PHY to the rest of the system.
1585c7ebbbceSChristoph Hellwig  */
1586c7ebbbceSChristoph Hellwig int sas_rphy_add(struct sas_rphy *rphy)
1587c7ebbbceSChristoph Hellwig {
158865c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1589c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1590c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1591c7ebbbceSChristoph Hellwig 	struct sas_identify *identify = &rphy->identify;
1592c7ebbbceSChristoph Hellwig 	int error;
1593c7ebbbceSChristoph Hellwig 
1594c7ebbbceSChristoph Hellwig 	if (parent->rphy)
1595c7ebbbceSChristoph Hellwig 		return -ENXIO;
1596c7ebbbceSChristoph Hellwig 	parent->rphy = rphy;
1597c7ebbbceSChristoph Hellwig 
1598c7ebbbceSChristoph Hellwig 	error = device_add(&rphy->dev);
1599c7ebbbceSChristoph Hellwig 	if (error)
1600c7ebbbceSChristoph Hellwig 		return error;
1601c7ebbbceSChristoph Hellwig 	transport_add_device(&rphy->dev);
1602c7ebbbceSChristoph Hellwig 	transport_configure_device(&rphy->dev);
160339dca558SJames Bottomley 	if (sas_bsg_initialize(shost, rphy))
160471610f55SKay Sievers 		printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
160539dca558SJames Bottomley 
1606c7ebbbceSChristoph Hellwig 
1607e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1608c7ebbbceSChristoph Hellwig 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1609c7ebbbceSChristoph Hellwig 	if (identify->device_type == SAS_END_DEVICE &&
1610c7ebbbceSChristoph Hellwig 	    (identify->target_port_protocols &
1611c7ebbbceSChristoph Hellwig 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1612c7ebbbceSChristoph Hellwig 		rphy->scsi_target_id = sas_host->next_target_id++;
16137676f83aSJames Bottomley 	else if (identify->device_type == SAS_END_DEVICE)
16147676f83aSJames Bottomley 		rphy->scsi_target_id = -1;
1615e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1616c7ebbbceSChristoph Hellwig 
161779cb1819SJames Bottomley 	if (identify->device_type == SAS_END_DEVICE &&
161879cb1819SJames Bottomley 	    rphy->scsi_target_id != -1) {
16192fc62e2aSDan Williams 		int lun;
16202fc62e2aSDan Williams 
16212fc62e2aSDan Williams 		if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
16222fc62e2aSDan Williams 			lun = SCAN_WILD_CARD;
16232fc62e2aSDan Williams 		else
16242fc62e2aSDan Williams 			lun = 0;
16252fc62e2aSDan Williams 
16261d645088SHannes Reinecke 		scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
16271d645088SHannes Reinecke 				 SCSI_SCAN_INITIAL);
1628c7ebbbceSChristoph Hellwig 	}
1629c7ebbbceSChristoph Hellwig 
1630c7ebbbceSChristoph Hellwig 	return 0;
1631c7ebbbceSChristoph Hellwig }
1632c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_add);
1633c7ebbbceSChristoph Hellwig 
1634c7ebbbceSChristoph Hellwig /**
1635eb44820cSRob Landley  * sas_rphy_free  -  free a SAS remote PHY
1636eb44820cSRob Landley  * @rphy: SAS remote PHY to free
1637c7ebbbceSChristoph Hellwig  *
1638c7ebbbceSChristoph Hellwig  * Frees the specified SAS remote PHY.
1639c7ebbbceSChristoph Hellwig  *
1640c7ebbbceSChristoph Hellwig  * Note:
1641c7ebbbceSChristoph Hellwig  *   This function must only be called on a remote
1642af901ca1SAndré Goddard Rosa  *   PHY that has not successfully been added using
16436f63caaeSDarrick J. Wong  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1644c7ebbbceSChristoph Hellwig  */
1645c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *rphy)
1646c7ebbbceSChristoph Hellwig {
164792aab646SMike Anderson 	struct device *dev = &rphy->dev;
1648c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1649c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1650c7ebbbceSChristoph Hellwig 
1651e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1652c7ebbbceSChristoph Hellwig 	list_del(&rphy->list);
1653e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1654c7ebbbceSChristoph Hellwig 
165592aab646SMike Anderson 	transport_destroy_device(dev);
16562f8600dfSJames Bottomley 
165792aab646SMike Anderson 	put_device(dev);
1658c7ebbbceSChristoph Hellwig }
1659c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_free);
1660c7ebbbceSChristoph Hellwig 
1661c7ebbbceSChristoph Hellwig /**
1662eb44820cSRob Landley  * sas_rphy_delete  -  remove and free SAS remote PHY
16636f63caaeSDarrick J. Wong  * @rphy:	SAS remote PHY to remove and free
1664c7ebbbceSChristoph Hellwig  *
16656f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY and frees it.
1666c7ebbbceSChristoph Hellwig  */
1667c7ebbbceSChristoph Hellwig void
1668c7ebbbceSChristoph Hellwig sas_rphy_delete(struct sas_rphy *rphy)
1669c7ebbbceSChristoph Hellwig {
16706f63caaeSDarrick J. Wong 	sas_rphy_remove(rphy);
16716f63caaeSDarrick J. Wong 	sas_rphy_free(rphy);
16726f63caaeSDarrick J. Wong }
16736f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_delete);
16746f63caaeSDarrick J. Wong 
16756f63caaeSDarrick J. Wong /**
167687c8331fSDan Williams  * sas_rphy_unlink  -  unlink SAS remote PHY
167787c8331fSDan Williams  * @rphy:	SAS remote phy to unlink from its parent port
167887c8331fSDan Williams  *
167987c8331fSDan Williams  * Removes port reference to an rphy
168087c8331fSDan Williams  */
168187c8331fSDan Williams void sas_rphy_unlink(struct sas_rphy *rphy)
168287c8331fSDan Williams {
168387c8331fSDan Williams 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
168487c8331fSDan Williams 
168587c8331fSDan Williams 	parent->rphy = NULL;
168687c8331fSDan Williams }
168787c8331fSDan Williams EXPORT_SYMBOL(sas_rphy_unlink);
168887c8331fSDan Williams 
168987c8331fSDan Williams /**
1690eb44820cSRob Landley  * sas_rphy_remove  -  remove SAS remote PHY
16916f63caaeSDarrick J. Wong  * @rphy:	SAS remote phy to remove
16926f63caaeSDarrick J. Wong  *
16936f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY.
16946f63caaeSDarrick J. Wong  */
16956f63caaeSDarrick J. Wong void
16966f63caaeSDarrick J. Wong sas_rphy_remove(struct sas_rphy *rphy)
16976f63caaeSDarrick J. Wong {
1698c7ebbbceSChristoph Hellwig 	struct device *dev = &rphy->dev;
1699c7ebbbceSChristoph Hellwig 
1700d4054239SChristoph Hellwig 	switch (rphy->identify.device_type) {
1701d4054239SChristoph Hellwig 	case SAS_END_DEVICE:
1702fe8b2304SChristoph Hellwig 		scsi_remove_target(dev);
1703d4054239SChristoph Hellwig 		break;
1704d4054239SChristoph Hellwig 	case SAS_EDGE_EXPANDER_DEVICE:
1705d4054239SChristoph Hellwig 	case SAS_FANOUT_EXPANDER_DEVICE:
170665c92b09SJames Bottomley 		sas_remove_children(dev);
1707d4054239SChristoph Hellwig 		break;
1708d4054239SChristoph Hellwig 	default:
1709d4054239SChristoph Hellwig 		break;
1710d4054239SChristoph Hellwig 	}
1711c7ebbbceSChristoph Hellwig 
171287c8331fSDan Williams 	sas_rphy_unlink(rphy);
17136aa6caffSJoe Lawrence 	sas_bsg_remove(NULL, rphy);
1714fe8b2304SChristoph Hellwig 	transport_remove_device(dev);
1715fe8b2304SChristoph Hellwig 	device_del(dev);
1716c7ebbbceSChristoph Hellwig }
17176f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_remove);
1718c7ebbbceSChristoph Hellwig 
1719c7ebbbceSChristoph Hellwig /**
1720eb44820cSRob Landley  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1721c7ebbbceSChristoph Hellwig  * @dev:	device to check
1722c7ebbbceSChristoph Hellwig  *
1723c7ebbbceSChristoph Hellwig  * Returns:
1724c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS remote PHY, %0 else
1725c7ebbbceSChristoph Hellwig  */
1726c7ebbbceSChristoph Hellwig int scsi_is_sas_rphy(const struct device *dev)
1727c7ebbbceSChristoph Hellwig {
17282f8600dfSJames Bottomley 	return dev->release == sas_end_device_release ||
17292f8600dfSJames Bottomley 		dev->release == sas_expander_release;
1730c7ebbbceSChristoph Hellwig }
1731c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_rphy);
1732c7ebbbceSChristoph Hellwig 
1733c7ebbbceSChristoph Hellwig 
1734c7ebbbceSChristoph Hellwig /*
1735c7ebbbceSChristoph Hellwig  * SCSI scan helper
1736c7ebbbceSChristoph Hellwig  */
1737c7ebbbceSChristoph Hellwig 
1738e02f3f59SChristoph Hellwig static int sas_user_scan(struct Scsi_Host *shost, uint channel,
17399cb78c16SHannes Reinecke 		uint id, u64 lun)
1740c7ebbbceSChristoph Hellwig {
1741c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1742c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy;
1743c7ebbbceSChristoph Hellwig 
1744e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1745c7ebbbceSChristoph Hellwig 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
17466d99a3f3SJames Bottomley 		if (rphy->identify.device_type != SAS_END_DEVICE ||
17476d99a3f3SJames Bottomley 		    rphy->scsi_target_id == -1)
1748e02f3f59SChristoph Hellwig 			continue;
1749e02f3f59SChristoph Hellwig 
1750e8bf3941SJames Bottomley 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1751e02f3f59SChristoph Hellwig 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
17521d645088SHannes Reinecke 			scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
17531d645088SHannes Reinecke 					 lun, SCSI_SCAN_MANUAL);
1754e02f3f59SChristoph Hellwig 		}
1755e02f3f59SChristoph Hellwig 	}
1756e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1757e02f3f59SChristoph Hellwig 
1758e02f3f59SChristoph Hellwig 	return 0;
1759c7ebbbceSChristoph Hellwig }
1760c7ebbbceSChristoph Hellwig 
1761c7ebbbceSChristoph Hellwig 
1762c7ebbbceSChristoph Hellwig /*
1763c7ebbbceSChristoph Hellwig  * Setup / Teardown code
1764c7ebbbceSChristoph Hellwig  */
1765c7ebbbceSChristoph Hellwig 
176642ab0360SJames Bottomley #define SETUP_TEMPLATE(attrb, field, perm, test)			\
1767ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
176842ab0360SJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
176942ab0360SJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
177042ab0360SJames Bottomley 	if (test)							\
1771c7ebbbceSChristoph Hellwig 		count++
1772c7ebbbceSChristoph Hellwig 
1773d24e1eebSJames Bottomley #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1774ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
1775d24e1eebSJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
1776d24e1eebSJames Bottomley 	if (ro_test) {							\
1777d24e1eebSJames Bottomley 		i->private_##attrb[count].attr.mode = ro_perm;		\
1778d24e1eebSJames Bottomley 		i->private_##attrb[count].store = NULL;			\
1779d24e1eebSJames Bottomley 	}								\
1780d24e1eebSJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
1781d24e1eebSJames Bottomley 	if (test)							\
1782d24e1eebSJames Bottomley 		count++
178342ab0360SJames Bottomley 
178442ab0360SJames Bottomley #define SETUP_RPORT_ATTRIBUTE(field) 					\
178542ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
178642ab0360SJames Bottomley 
1787dd9fbb52SJames Bottomley #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
178842ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1789dd9fbb52SJames Bottomley 
179065c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE(field)					\
179142ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1792c7ebbbceSChristoph Hellwig 
1793d24e1eebSJames Bottomley #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1794d24e1eebSJames Bottomley 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1795d24e1eebSJames Bottomley 			!i->f->set_phy_speed, S_IRUGO)
1796d24e1eebSJames Bottomley 
1797acbf167dSDarrick J. Wong #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1798acbf167dSDarrick J. Wong 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1799acbf167dSDarrick J. Wong 			  !i->f->func, S_IRUGO)
1800acbf167dSDarrick J. Wong 
180165c92b09SJames Bottomley #define SETUP_PORT_ATTRIBUTE(field)					\
180265c92b09SJames Bottomley 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
180365c92b09SJames Bottomley 
180465c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
180542ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1806dd9fbb52SJames Bottomley 
180765c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1808fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
180907ba3a95SChristoph Hellwig 
181065c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1811fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1812dd9fbb52SJames Bottomley 
181342ab0360SJames Bottomley #define SETUP_END_DEV_ATTRIBUTE(field)					\
181442ab0360SJames Bottomley 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1815c7ebbbceSChristoph Hellwig 
181679cb1819SJames Bottomley #define SETUP_EXPANDER_ATTRIBUTE(field)					\
181779cb1819SJames Bottomley 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
181879cb1819SJames Bottomley 
1819c7ebbbceSChristoph Hellwig /**
1820eb44820cSRob Landley  * sas_attach_transport  -  instantiate SAS transport template
1821c7ebbbceSChristoph Hellwig  * @ft:		SAS transport class function template
1822c7ebbbceSChristoph Hellwig  */
1823c7ebbbceSChristoph Hellwig struct scsi_transport_template *
1824c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *ft)
1825c7ebbbceSChristoph Hellwig {
1826c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1827c7ebbbceSChristoph Hellwig 	int count;
1828c7ebbbceSChristoph Hellwig 
182924669f75SJes Sorensen 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1830c7ebbbceSChristoph Hellwig 	if (!i)
1831c7ebbbceSChristoph Hellwig 		return NULL;
1832c7ebbbceSChristoph Hellwig 
1833e02f3f59SChristoph Hellwig 	i->t.user_scan = sas_user_scan;
1834c7ebbbceSChristoph Hellwig 
1835c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1836c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.class = &sas_host_class.class;
1837c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.match = sas_host_match;
1838c7ebbbceSChristoph Hellwig 	transport_container_register(&i->t.host_attrs);
1839c7ebbbceSChristoph Hellwig 	i->t.host_size = sizeof(struct sas_host_attrs);
1840c7ebbbceSChristoph Hellwig 
1841c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1842c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1843c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.match = sas_phy_match;
1844c7ebbbceSChristoph Hellwig 	transport_container_register(&i->phy_attr_cont);
1845c7ebbbceSChristoph Hellwig 
184665c92b09SJames Bottomley 	i->port_attr_cont.ac.class = &sas_port_class.class;
184765c92b09SJames Bottomley 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
184865c92b09SJames Bottomley 	i->port_attr_cont.ac.match = sas_port_match;
184965c92b09SJames Bottomley 	transport_container_register(&i->port_attr_cont);
185065c92b09SJames Bottomley 
1851c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1852c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1853c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1854c7ebbbceSChristoph Hellwig 	transport_container_register(&i->rphy_attr_cont);
1855c7ebbbceSChristoph Hellwig 
185642ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
185742ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
185842ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
185942ab0360SJames Bottomley 	transport_container_register(&i->end_dev_attr_cont);
186042ab0360SJames Bottomley 
186179cb1819SJames Bottomley 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
186279cb1819SJames Bottomley 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
186379cb1819SJames Bottomley 	i->expander_attr_cont.ac.match = sas_expander_match;
186479cb1819SJames Bottomley 	transport_container_register(&i->expander_attr_cont);
186579cb1819SJames Bottomley 
1866c7ebbbceSChristoph Hellwig 	i->f = ft;
1867c7ebbbceSChristoph Hellwig 
1868c7ebbbceSChristoph Hellwig 	count = 0;
186965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
187065c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
187165c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(device_type);
187265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(sas_address);
187365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_identifier);
187465c92b09SJames Bottomley 	//SETUP_PHY_ATTRIBUTE(port_identifier);
187565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
187665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1877d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
187865c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1879d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1880c3ee74c4SChristoph Hellwig 
188165c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
188265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
188365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
188465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
188565c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
188665c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1887acbf167dSDarrick J. Wong 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1888c7ebbbceSChristoph Hellwig 	i->phy_attrs[count] = NULL;
1889c7ebbbceSChristoph Hellwig 
1890c7ebbbceSChristoph Hellwig 	count = 0;
189165c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
189265c92b09SJames Bottomley 	i->port_attrs[count] = NULL;
189365c92b09SJames Bottomley 
189465c92b09SJames Bottomley 	count = 0;
1895c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1896c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1897c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1898c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1899c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1900cdc43ae3SHannes Reinecke 	SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
1901dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1902dd9fbb52SJames Bottomley 				       get_enclosure_identifier);
1903dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1904dd9fbb52SJames Bottomley 				       get_bay_identifier);
1905c7ebbbceSChristoph Hellwig 	i->rphy_attrs[count] = NULL;
1906c7ebbbceSChristoph Hellwig 
190742ab0360SJames Bottomley 	count = 0;
190842ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
190942ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
191042ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
19110f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
19120f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
191342ab0360SJames Bottomley 	i->end_dev_attrs[count] = NULL;
191442ab0360SJames Bottomley 
191579cb1819SJames Bottomley 	count = 0;
191679cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
191779cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_id);
191879cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
191979cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
192079cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_id);
192179cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
192279cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(level);
192379cb1819SJames Bottomley 	i->expander_attrs[count] = NULL;
192479cb1819SJames Bottomley 
1925c7ebbbceSChristoph Hellwig 	return &i->t;
1926c7ebbbceSChristoph Hellwig }
1927c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_attach_transport);
1928c7ebbbceSChristoph Hellwig 
1929c7ebbbceSChristoph Hellwig /**
1930eb44820cSRob Landley  * sas_release_transport  -  release SAS transport template instance
1931c7ebbbceSChristoph Hellwig  * @t:		transport template instance
1932c7ebbbceSChristoph Hellwig  */
1933c7ebbbceSChristoph Hellwig void sas_release_transport(struct scsi_transport_template *t)
1934c7ebbbceSChristoph Hellwig {
1935c7ebbbceSChristoph Hellwig 	struct sas_internal *i = to_sas_internal(t);
1936c7ebbbceSChristoph Hellwig 
1937c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->t.host_attrs);
1938c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->phy_attr_cont);
193965c92b09SJames Bottomley 	transport_container_unregister(&i->port_attr_cont);
1940c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->rphy_attr_cont);
1941db82f841SJames Bottomley 	transport_container_unregister(&i->end_dev_attr_cont);
194279cb1819SJames Bottomley 	transport_container_unregister(&i->expander_attr_cont);
1943c7ebbbceSChristoph Hellwig 
1944c7ebbbceSChristoph Hellwig 	kfree(i);
1945c7ebbbceSChristoph Hellwig }
1946c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_release_transport);
1947c7ebbbceSChristoph Hellwig 
1948c7ebbbceSChristoph Hellwig static __init int sas_transport_init(void)
1949c7ebbbceSChristoph Hellwig {
1950c7ebbbceSChristoph Hellwig 	int error;
1951c7ebbbceSChristoph Hellwig 
1952c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_host_class);
1953c7ebbbceSChristoph Hellwig 	if (error)
1954c7ebbbceSChristoph Hellwig 		goto out;
1955c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_phy_class);
1956c7ebbbceSChristoph Hellwig 	if (error)
1957c7ebbbceSChristoph Hellwig 		goto out_unregister_transport;
195865c92b09SJames Bottomley 	error = transport_class_register(&sas_port_class);
1959c7ebbbceSChristoph Hellwig 	if (error)
1960c7ebbbceSChristoph Hellwig 		goto out_unregister_phy;
196165c92b09SJames Bottomley 	error = transport_class_register(&sas_rphy_class);
196265c92b09SJames Bottomley 	if (error)
196365c92b09SJames Bottomley 		goto out_unregister_port;
196442ab0360SJames Bottomley 	error = transport_class_register(&sas_end_dev_class);
196542ab0360SJames Bottomley 	if (error)
196642ab0360SJames Bottomley 		goto out_unregister_rphy;
196779cb1819SJames Bottomley 	error = transport_class_register(&sas_expander_class);
196879cb1819SJames Bottomley 	if (error)
196979cb1819SJames Bottomley 		goto out_unregister_end_dev;
1970c7ebbbceSChristoph Hellwig 
1971c7ebbbceSChristoph Hellwig 	return 0;
1972c7ebbbceSChristoph Hellwig 
197379cb1819SJames Bottomley  out_unregister_end_dev:
197479cb1819SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
197542ab0360SJames Bottomley  out_unregister_rphy:
197642ab0360SJames Bottomley 	transport_class_unregister(&sas_rphy_class);
197765c92b09SJames Bottomley  out_unregister_port:
197865c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1979c7ebbbceSChristoph Hellwig  out_unregister_phy:
1980c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
1981c7ebbbceSChristoph Hellwig  out_unregister_transport:
1982c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1983c7ebbbceSChristoph Hellwig  out:
1984c7ebbbceSChristoph Hellwig 	return error;
1985c7ebbbceSChristoph Hellwig 
1986c7ebbbceSChristoph Hellwig }
1987c7ebbbceSChristoph Hellwig 
1988c7ebbbceSChristoph Hellwig static void __exit sas_transport_exit(void)
1989c7ebbbceSChristoph Hellwig {
1990c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1991c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
199265c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1993c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_rphy_class);
199442ab0360SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
199579cb1819SJames Bottomley 	transport_class_unregister(&sas_expander_class);
1996c7ebbbceSChristoph Hellwig }
1997c7ebbbceSChristoph Hellwig 
1998c7ebbbceSChristoph Hellwig MODULE_AUTHOR("Christoph Hellwig");
199986b9c4c1SAlexis Bruemmer MODULE_DESCRIPTION("SAS Transport Attributes");
2000c7ebbbceSChristoph Hellwig MODULE_LICENSE("GPL");
2001c7ebbbceSChristoph Hellwig 
2002c7ebbbceSChristoph Hellwig module_init(sas_transport_init);
2003c7ebbbceSChristoph Hellwig module_exit(sas_transport_exit);
2004