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>
36c7ebbbceSChristoph Hellwig #include <scsi/scsi_device.h>
37c7ebbbceSChristoph Hellwig #include <scsi/scsi_host.h>
38c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport.h>
39c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport_sas.h>
40c7ebbbceSChristoph Hellwig 
41d6159c17SJames Bottomley #include "scsi_sas_internal.h"
42c7ebbbceSChristoph Hellwig struct sas_host_attrs {
43c7ebbbceSChristoph Hellwig 	struct list_head rphy_list;
44e02f3f59SChristoph Hellwig 	struct mutex lock;
45b6aff669SJames Bottomley 	struct request_queue *q;
46c7ebbbceSChristoph Hellwig 	u32 next_target_id;
4779cb1819SJames Bottomley 	u32 next_expander_id;
48c9fefeb2SJames Bottomley 	int next_port_id;
49c7ebbbceSChristoph Hellwig };
50c7ebbbceSChristoph Hellwig #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
51c7ebbbceSChristoph Hellwig 
52c7ebbbceSChristoph Hellwig 
53c7ebbbceSChristoph Hellwig /*
54c7ebbbceSChristoph Hellwig  * Hack to allow attributes of the same name in different objects.
55c7ebbbceSChristoph Hellwig  */
56ee959b00STony Jones #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57ee959b00STony Jones 	struct device_attribute dev_attr_##_prefix##_##_name = \
58c7ebbbceSChristoph Hellwig 	__ATTR(_name,_mode,_show,_store)
59c7ebbbceSChristoph Hellwig 
60c7ebbbceSChristoph Hellwig 
61c7ebbbceSChristoph Hellwig /*
62c7ebbbceSChristoph Hellwig  * Pretty printing helpers
63c7ebbbceSChristoph Hellwig  */
64c7ebbbceSChristoph Hellwig 
65c7ebbbceSChristoph Hellwig #define sas_bitfield_name_match(title, table)			\
66c7ebbbceSChristoph Hellwig static ssize_t							\
67c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
68c7ebbbceSChristoph Hellwig {								\
69c7ebbbceSChristoph Hellwig 	char *prefix = "";					\
70c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
71c7ebbbceSChristoph Hellwig 	int i;							\
72c7ebbbceSChristoph Hellwig 								\
736391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
74c7ebbbceSChristoph Hellwig 		if (table[i].value & table_key) {		\
75c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s%s",	\
76c7ebbbceSChristoph Hellwig 				prefix, table[i].name);		\
77c7ebbbceSChristoph Hellwig 			prefix = ", ";				\
78c7ebbbceSChristoph Hellwig 		}						\
79c7ebbbceSChristoph Hellwig 	}							\
80c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
81c7ebbbceSChristoph Hellwig 	return len;						\
82c7ebbbceSChristoph Hellwig }
83c7ebbbceSChristoph Hellwig 
84d24e1eebSJames Bottomley #define sas_bitfield_name_set(title, table)			\
85d24e1eebSJames Bottomley static ssize_t							\
86d24e1eebSJames Bottomley set_sas_##title##_names(u32 *table_key, const char *buf)	\
87d24e1eebSJames Bottomley {								\
88d24e1eebSJames Bottomley 	ssize_t len = 0;					\
89d24e1eebSJames Bottomley 	int i;							\
90d24e1eebSJames Bottomley 								\
91d24e1eebSJames Bottomley 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
92d24e1eebSJames Bottomley 		len = strlen(table[i].name);			\
93d24e1eebSJames Bottomley 		if (strncmp(buf, table[i].name, len) == 0 &&	\
94d24e1eebSJames Bottomley 		    (buf[len] == '\n' || buf[len] == '\0')) {	\
95d24e1eebSJames Bottomley 			*table_key = table[i].value;		\
96d24e1eebSJames Bottomley 			return 0;				\
97d24e1eebSJames Bottomley 		}						\
98d24e1eebSJames Bottomley 	}							\
99d24e1eebSJames Bottomley 	return -EINVAL;						\
100d24e1eebSJames Bottomley }
101d24e1eebSJames Bottomley 
102c7ebbbceSChristoph Hellwig #define sas_bitfield_name_search(title, table)			\
103c7ebbbceSChristoph Hellwig static ssize_t							\
104c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
105c7ebbbceSChristoph Hellwig {								\
106c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
107c7ebbbceSChristoph Hellwig 	int i;							\
108c7ebbbceSChristoph Hellwig 								\
1096391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
110c7ebbbceSChristoph Hellwig 		if (table[i].value == table_key) {		\
111c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s",		\
112c7ebbbceSChristoph Hellwig 				table[i].name);			\
113c7ebbbceSChristoph Hellwig 			break;					\
114c7ebbbceSChristoph Hellwig 		}						\
115c7ebbbceSChristoph Hellwig 	}							\
116c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
117c7ebbbceSChristoph Hellwig 	return len;						\
118c7ebbbceSChristoph Hellwig }
119c7ebbbceSChristoph Hellwig 
120c7ebbbceSChristoph Hellwig static struct {
121c7ebbbceSChristoph Hellwig 	u32		value;
122c7ebbbceSChristoph Hellwig 	char		*name;
123c7ebbbceSChristoph Hellwig } sas_device_type_names[] = {
124c7ebbbceSChristoph Hellwig 	{ SAS_PHY_UNUSED,		"unused" },
125c7ebbbceSChristoph Hellwig 	{ SAS_END_DEVICE,		"end device" },
126c7ebbbceSChristoph Hellwig 	{ SAS_EDGE_EXPANDER_DEVICE,	"edge expander" },
127c7ebbbceSChristoph Hellwig 	{ SAS_FANOUT_EXPANDER_DEVICE,	"fanout expander" },
128c7ebbbceSChristoph Hellwig };
129c7ebbbceSChristoph Hellwig sas_bitfield_name_search(device_type, sas_device_type_names)
130c7ebbbceSChristoph Hellwig 
131c7ebbbceSChristoph Hellwig 
132c7ebbbceSChristoph Hellwig static struct {
133c7ebbbceSChristoph Hellwig 	u32		value;
134c7ebbbceSChristoph Hellwig 	char		*name;
135c7ebbbceSChristoph Hellwig } sas_protocol_names[] = {
136c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SATA,		"sata" },
137c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SMP,		"smp" },
138c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_STP,		"stp" },
139c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SSP,		"ssp" },
140c7ebbbceSChristoph Hellwig };
141c7ebbbceSChristoph Hellwig sas_bitfield_name_match(protocol, sas_protocol_names)
142c7ebbbceSChristoph Hellwig 
143c7ebbbceSChristoph Hellwig static struct {
144c7ebbbceSChristoph Hellwig 	u32		value;
145c7ebbbceSChristoph Hellwig 	char		*name;
146c7ebbbceSChristoph Hellwig } sas_linkspeed_names[] = {
147c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_UNKNOWN,	"Unknown" },
148c7ebbbceSChristoph Hellwig 	{ SAS_PHY_DISABLED,		"Phy disabled" },
149c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_FAILED,		"Link Rate failed" },
150c7ebbbceSChristoph Hellwig 	{ SAS_SATA_SPINUP_HOLD,		"Spin-up hold" },
151c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_1_5_GBPS,	"1.5 Gbit" },
152c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_3_0_GBPS,	"3.0 Gbit" },
1537e6dff62SJames Bottomley 	{ SAS_LINK_RATE_6_0_GBPS,	"6.0 Gbit" },
154c7ebbbceSChristoph Hellwig };
155c7ebbbceSChristoph Hellwig sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
156d24e1eebSJames Bottomley sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
157c7ebbbceSChristoph Hellwig 
1580f88009dSJames Bottomley static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
1590f88009dSJames Bottomley {
1600f88009dSJames Bottomley 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
1610f88009dSJames Bottomley 	struct sas_end_device *rdev;
1620f88009dSJames Bottomley 
1630f88009dSJames Bottomley 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
1640f88009dSJames Bottomley 
1650f88009dSJames Bottomley 	rdev = rphy_to_end_device(rphy);
1660f88009dSJames Bottomley 	return rdev;
1670f88009dSJames Bottomley }
1680f88009dSJames Bottomley 
1697aa68e80SFUJITA Tomonori static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
1707aa68e80SFUJITA Tomonori 			    struct sas_rphy *rphy)
1717aa68e80SFUJITA Tomonori {
1727aa68e80SFUJITA Tomonori 	struct request *req;
1737aa68e80SFUJITA Tomonori 	int ret;
1747aa68e80SFUJITA Tomonori 	int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
1757aa68e80SFUJITA Tomonori 
1767eaceaccSJens Axboe 	while ((req = blk_fetch_request(q)) != NULL) {
1777aa68e80SFUJITA Tomonori 		spin_unlock_irq(q->queue_lock);
1787aa68e80SFUJITA Tomonori 
1797aa68e80SFUJITA Tomonori 		handler = to_sas_internal(shost->transportt)->f->smp_handler;
1807aa68e80SFUJITA Tomonori 		ret = handler(shost, rphy, req);
1812d507a01SJames Bottomley 		req->errors = ret;
1827aa68e80SFUJITA Tomonori 
18393bdcba5SFUJITA Tomonori 		blk_end_request_all(req, ret);
1847aa68e80SFUJITA Tomonori 
18593bdcba5SFUJITA Tomonori 		spin_lock_irq(q->queue_lock);
1867aa68e80SFUJITA Tomonori 	}
1877aa68e80SFUJITA Tomonori }
1887aa68e80SFUJITA Tomonori 
1897aa68e80SFUJITA Tomonori static void sas_host_smp_request(struct request_queue *q)
1907aa68e80SFUJITA Tomonori {
1917aa68e80SFUJITA Tomonori 	sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
1927aa68e80SFUJITA Tomonori }
1937aa68e80SFUJITA Tomonori 
1947aa68e80SFUJITA Tomonori static void sas_non_host_smp_request(struct request_queue *q)
1957aa68e80SFUJITA Tomonori {
1967aa68e80SFUJITA Tomonori 	struct sas_rphy *rphy = q->queuedata;
1977aa68e80SFUJITA Tomonori 	sas_smp_request(q, rphy_to_shost(rphy), rphy);
1987aa68e80SFUJITA Tomonori }
1997aa68e80SFUJITA Tomonori 
20093c20a59SFUJITA Tomonori static void sas_host_release(struct device *dev)
20193c20a59SFUJITA Tomonori {
20293c20a59SFUJITA Tomonori 	struct Scsi_Host *shost = dev_to_shost(dev);
20393c20a59SFUJITA Tomonori 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
20493c20a59SFUJITA Tomonori 	struct request_queue *q = sas_host->q;
20593c20a59SFUJITA Tomonori 
20693c20a59SFUJITA Tomonori 	if (q)
20793c20a59SFUJITA Tomonori 		blk_cleanup_queue(q);
20893c20a59SFUJITA Tomonori }
20993c20a59SFUJITA Tomonori 
21039dca558SJames Bottomley static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
2117aa68e80SFUJITA Tomonori {
2127aa68e80SFUJITA Tomonori 	struct request_queue *q;
2137aa68e80SFUJITA Tomonori 	int error;
21439dca558SJames Bottomley 	struct device *dev;
21571610f55SKay Sievers 	char namebuf[20];
21639dca558SJames Bottomley 	const char *name;
21793c20a59SFUJITA Tomonori 	void (*release)(struct device *);
2187aa68e80SFUJITA Tomonori 
2197aa68e80SFUJITA Tomonori 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
2207aa68e80SFUJITA Tomonori 		printk("%s can't handle SMP requests\n", shost->hostt->name);
2217aa68e80SFUJITA Tomonori 		return 0;
2227aa68e80SFUJITA Tomonori 	}
2237aa68e80SFUJITA Tomonori 
22439dca558SJames Bottomley 	if (rphy) {
2257aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_non_host_smp_request, NULL);
22639dca558SJames Bottomley 		dev = &rphy->dev;
22771610f55SKay Sievers 		name = dev_name(dev);
22893c20a59SFUJITA Tomonori 		release = NULL;
22939dca558SJames Bottomley 	} else {
2307aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_host_smp_request, NULL);
23139dca558SJames Bottomley 		dev = &shost->shost_gendev;
23239dca558SJames Bottomley 		snprintf(namebuf, sizeof(namebuf),
23339dca558SJames Bottomley 			 "sas_host%d", shost->host_no);
23439dca558SJames Bottomley 		name = namebuf;
23593c20a59SFUJITA Tomonori 		release = sas_host_release;
23639dca558SJames Bottomley 	}
2377aa68e80SFUJITA Tomonori 	if (!q)
2387aa68e80SFUJITA Tomonori 		return -ENOMEM;
2397aa68e80SFUJITA Tomonori 
24093c20a59SFUJITA Tomonori 	error = bsg_register_queue(q, dev, name, release);
2417aa68e80SFUJITA Tomonori 	if (error) {
2427aa68e80SFUJITA Tomonori 		blk_cleanup_queue(q);
2437aa68e80SFUJITA Tomonori 		return -ENOMEM;
2447aa68e80SFUJITA Tomonori 	}
2457aa68e80SFUJITA Tomonori 
2467aa68e80SFUJITA Tomonori 	if (rphy)
247b6aff669SJames Bottomley 		rphy->q = q;
248b6aff669SJames Bottomley 	else
249b6aff669SJames Bottomley 		to_sas_host_attrs(shost)->q = q;
250b6aff669SJames Bottomley 
251b6aff669SJames Bottomley 	if (rphy)
2527aa68e80SFUJITA Tomonori 		q->queuedata = rphy;
2537aa68e80SFUJITA Tomonori 	else
2547aa68e80SFUJITA Tomonori 		q->queuedata = shost;
2557aa68e80SFUJITA Tomonori 
25675ad23bcSNick Piggin 	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
2577aa68e80SFUJITA Tomonori 	return 0;
2587aa68e80SFUJITA Tomonori }
2597aa68e80SFUJITA Tomonori 
260b6aff669SJames Bottomley static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
261b6aff669SJames Bottomley {
262b6aff669SJames Bottomley 	struct request_queue *q;
263b6aff669SJames Bottomley 
264b6aff669SJames Bottomley 	if (rphy)
265b6aff669SJames Bottomley 		q = rphy->q;
266b6aff669SJames Bottomley 	else
267b6aff669SJames Bottomley 		q = to_sas_host_attrs(shost)->q;
268b6aff669SJames Bottomley 
269b6aff669SJames Bottomley 	if (!q)
270b6aff669SJames Bottomley 		return;
271b6aff669SJames Bottomley 
272b6aff669SJames Bottomley 	bsg_unregister_queue(q);
273b6aff669SJames Bottomley }
274b6aff669SJames Bottomley 
275c7ebbbceSChristoph Hellwig /*
276c7ebbbceSChristoph Hellwig  * SAS host attributes
277c7ebbbceSChristoph Hellwig  */
278c7ebbbceSChristoph Hellwig 
27937be6eebSJames Bottomley static int sas_host_setup(struct transport_container *tc, struct device *dev,
280ee959b00STony Jones 			  struct device *cdev)
281c7ebbbceSChristoph Hellwig {
282c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(dev);
283c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
284c7ebbbceSChristoph Hellwig 
285c7ebbbceSChristoph Hellwig 	INIT_LIST_HEAD(&sas_host->rphy_list);
286e02f3f59SChristoph Hellwig 	mutex_init(&sas_host->lock);
287c7ebbbceSChristoph Hellwig 	sas_host->next_target_id = 0;
28879cb1819SJames Bottomley 	sas_host->next_expander_id = 0;
289c9fefeb2SJames Bottomley 	sas_host->next_port_id = 0;
2907aa68e80SFUJITA Tomonori 
29139dca558SJames Bottomley 	if (sas_bsg_initialize(shost, NULL))
2927aa68e80SFUJITA Tomonori 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
2937aa68e80SFUJITA Tomonori 			   shost->host_no);
2947aa68e80SFUJITA Tomonori 
295c7ebbbceSChristoph Hellwig 	return 0;
296c7ebbbceSChristoph Hellwig }
297c7ebbbceSChristoph Hellwig 
298b6aff669SJames Bottomley static int sas_host_remove(struct transport_container *tc, struct device *dev,
299ee959b00STony Jones 			   struct device *cdev)
300b6aff669SJames Bottomley {
301b6aff669SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev);
302b6aff669SJames Bottomley 
303b6aff669SJames Bottomley 	sas_bsg_remove(shost, NULL);
304b6aff669SJames Bottomley 
305b6aff669SJames Bottomley 	return 0;
306b6aff669SJames Bottomley }
307b6aff669SJames Bottomley 
308c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_host_class,
309b6aff669SJames Bottomley 		"sas_host", sas_host_setup, sas_host_remove, NULL);
310c7ebbbceSChristoph Hellwig 
311c7ebbbceSChristoph Hellwig static int sas_host_match(struct attribute_container *cont,
312c7ebbbceSChristoph Hellwig 			    struct device *dev)
313c7ebbbceSChristoph Hellwig {
314c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
315c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
316c7ebbbceSChristoph Hellwig 
317c7ebbbceSChristoph Hellwig 	if (!scsi_is_host_device(dev))
318c7ebbbceSChristoph Hellwig 		return 0;
319c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev);
320c7ebbbceSChristoph Hellwig 
321c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
322c7ebbbceSChristoph Hellwig 		return 0;
323c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
324c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
325c7ebbbceSChristoph Hellwig 		return 0;
326c7ebbbceSChristoph Hellwig 
327c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
328c7ebbbceSChristoph Hellwig 	return &i->t.host_attrs.ac == cont;
329c7ebbbceSChristoph Hellwig }
330c7ebbbceSChristoph Hellwig 
331c7ebbbceSChristoph Hellwig static int do_sas_phy_delete(struct device *dev, void *data)
332c7ebbbceSChristoph Hellwig {
33365c92b09SJames Bottomley 	int pass = (int)(unsigned long)data;
33465c92b09SJames Bottomley 
33565c92b09SJames Bottomley 	if (pass == 0 && scsi_is_sas_port(dev))
33665c92b09SJames Bottomley 		sas_port_delete(dev_to_sas_port(dev));
33765c92b09SJames Bottomley 	else if (pass == 1 && scsi_is_sas_phy(dev))
338c7ebbbceSChristoph Hellwig 		sas_phy_delete(dev_to_phy(dev));
339c7ebbbceSChristoph Hellwig 	return 0;
340c7ebbbceSChristoph Hellwig }
341c7ebbbceSChristoph Hellwig 
342c7ebbbceSChristoph Hellwig /**
343eb44820cSRob Landley  * sas_remove_children  -  tear down a devices SAS data structures
34465c92b09SJames Bottomley  * @dev:	device belonging to the sas object
34565c92b09SJames Bottomley  *
34665c92b09SJames Bottomley  * Removes all SAS PHYs and remote PHYs for a given object
34765c92b09SJames Bottomley  */
34865c92b09SJames Bottomley void sas_remove_children(struct device *dev)
34965c92b09SJames Bottomley {
35065c92b09SJames Bottomley 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
35165c92b09SJames Bottomley 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
35265c92b09SJames Bottomley }
35365c92b09SJames Bottomley EXPORT_SYMBOL(sas_remove_children);
35465c92b09SJames Bottomley 
35565c92b09SJames Bottomley /**
356eb44820cSRob Landley  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
357c7ebbbceSChristoph Hellwig  * @shost:	Scsi Host that is torn down
358c7ebbbceSChristoph Hellwig  *
359c7ebbbceSChristoph Hellwig  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
360c7ebbbceSChristoph Hellwig  * Must be called just before scsi_remove_host for SAS HBAs.
361c7ebbbceSChristoph Hellwig  */
362c7ebbbceSChristoph Hellwig void sas_remove_host(struct Scsi_Host *shost)
363c7ebbbceSChristoph Hellwig {
36465c92b09SJames Bottomley 	sas_remove_children(&shost->shost_gendev);
365c7ebbbceSChristoph Hellwig }
366c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_remove_host);
367c7ebbbceSChristoph Hellwig 
3680f88009dSJames Bottomley /**
3690f88009dSJames Bottomley  * sas_tlr_supported - checking TLR bit in vpd 0x90
3700f88009dSJames Bottomley  * @sdev: scsi device struct
3710f88009dSJames Bottomley  *
3720f88009dSJames Bottomley  * Check Transport Layer Retries are supported or not.
3730f88009dSJames Bottomley  * If vpd page 0x90 is present, TRL is supported.
3740f88009dSJames Bottomley  *
3750f88009dSJames Bottomley  */
3760f88009dSJames Bottomley unsigned int
3770f88009dSJames Bottomley sas_tlr_supported(struct scsi_device *sdev)
3780f88009dSJames Bottomley {
3790f88009dSJames Bottomley 	const int vpd_len = 32;
3800f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
3810f88009dSJames Bottomley 	char *buffer = kzalloc(vpd_len, GFP_KERNEL);
3820f88009dSJames Bottomley 	int ret = 0;
3830f88009dSJames Bottomley 
3840f88009dSJames Bottomley 	if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
3850f88009dSJames Bottomley 		goto out;
3860f88009dSJames Bottomley 
3870f88009dSJames Bottomley 	/*
3880f88009dSJames Bottomley 	 * Magic numbers: the VPD Protocol page (0x90)
3890f88009dSJames Bottomley 	 * has a 4 byte header and then one entry per device port
3900f88009dSJames Bottomley 	 * the TLR bit is at offset 8 on each port entry
3910f88009dSJames Bottomley 	 * if we take the first port, that's at total offset 12
3920f88009dSJames Bottomley 	 */
3930f88009dSJames Bottomley 	ret = buffer[12] & 0x01;
3940f88009dSJames Bottomley 
3950f88009dSJames Bottomley  out:
3960f88009dSJames Bottomley 	kfree(buffer);
3970f88009dSJames Bottomley 	rdev->tlr_supported = ret;
3980f88009dSJames Bottomley 	return ret;
3990f88009dSJames Bottomley 
4000f88009dSJames Bottomley }
4010f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_tlr_supported);
4020f88009dSJames Bottomley 
4030f88009dSJames Bottomley /**
4040f88009dSJames Bottomley  * sas_disable_tlr - setting TLR flags
4050f88009dSJames Bottomley  * @sdev: scsi device struct
4060f88009dSJames Bottomley  *
4070f88009dSJames Bottomley  * Seting tlr_enabled flag to 0.
4080f88009dSJames Bottomley  *
4090f88009dSJames Bottomley  */
4100f88009dSJames Bottomley void
4110f88009dSJames Bottomley sas_disable_tlr(struct scsi_device *sdev)
4120f88009dSJames Bottomley {
4130f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4140f88009dSJames Bottomley 
4150f88009dSJames Bottomley 	rdev->tlr_enabled = 0;
4160f88009dSJames Bottomley }
4170f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_disable_tlr);
4180f88009dSJames Bottomley 
4190f88009dSJames Bottomley /**
4200f88009dSJames Bottomley  * sas_enable_tlr - setting TLR flags
4210f88009dSJames Bottomley  * @sdev: scsi device struct
4220f88009dSJames Bottomley  *
4230f88009dSJames Bottomley  * Seting tlr_enabled flag 1.
4240f88009dSJames Bottomley  *
4250f88009dSJames Bottomley  */
4260f88009dSJames Bottomley void sas_enable_tlr(struct scsi_device *sdev)
4270f88009dSJames Bottomley {
4280f88009dSJames Bottomley 	unsigned int tlr_supported = 0;
4290f88009dSJames Bottomley 	tlr_supported  = sas_tlr_supported(sdev);
4300f88009dSJames Bottomley 
4310f88009dSJames Bottomley 	if (tlr_supported) {
4320f88009dSJames Bottomley 		struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4330f88009dSJames Bottomley 
4340f88009dSJames Bottomley 		rdev->tlr_enabled = 1;
4350f88009dSJames Bottomley 	}
4360f88009dSJames Bottomley 
4370f88009dSJames Bottomley 	return;
4380f88009dSJames Bottomley }
4390f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_enable_tlr);
4400f88009dSJames Bottomley 
4410f88009dSJames Bottomley unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
4420f88009dSJames Bottomley {
4430f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4440f88009dSJames Bottomley 	return rdev->tlr_enabled;
4450f88009dSJames Bottomley }
4460f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
447c7ebbbceSChristoph Hellwig 
448c7ebbbceSChristoph Hellwig /*
44965c92b09SJames Bottomley  * SAS Phy attributes
450c7ebbbceSChristoph Hellwig  */
451c7ebbbceSChristoph Hellwig 
452c7ebbbceSChristoph Hellwig #define sas_phy_show_simple(field, name, format_string, cast)		\
453c7ebbbceSChristoph Hellwig static ssize_t								\
454ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
455ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
456c7ebbbceSChristoph Hellwig {									\
457ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
458c7ebbbceSChristoph Hellwig 									\
459c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast phy->field);	\
460c7ebbbceSChristoph Hellwig }
461c7ebbbceSChristoph Hellwig 
462c7ebbbceSChristoph Hellwig #define sas_phy_simple_attr(field, name, format_string, type)		\
463c7ebbbceSChristoph Hellwig 	sas_phy_show_simple(field, name, format_string, (type))	\
464ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
465c7ebbbceSChristoph Hellwig 
466c7ebbbceSChristoph Hellwig #define sas_phy_show_protocol(field, name)				\
467c7ebbbceSChristoph Hellwig static ssize_t								\
468ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
469ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
470c7ebbbceSChristoph Hellwig {									\
471ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
472c7ebbbceSChristoph Hellwig 									\
473c7ebbbceSChristoph Hellwig 	if (!phy->field)						\
474c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
475c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(phy->field, buf);		\
476c7ebbbceSChristoph Hellwig }
477c7ebbbceSChristoph Hellwig 
478c7ebbbceSChristoph Hellwig #define sas_phy_protocol_attr(field, name)				\
479c7ebbbceSChristoph Hellwig 	sas_phy_show_protocol(field, name)				\
480ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
481c7ebbbceSChristoph Hellwig 
482c7ebbbceSChristoph Hellwig #define sas_phy_show_linkspeed(field)					\
483c7ebbbceSChristoph Hellwig static ssize_t								\
484ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
485ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
486c7ebbbceSChristoph Hellwig {									\
487ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
488c7ebbbceSChristoph Hellwig 									\
489c7ebbbceSChristoph Hellwig 	return get_sas_linkspeed_names(phy->field, buf);		\
490c7ebbbceSChristoph Hellwig }
491c7ebbbceSChristoph Hellwig 
492d24e1eebSJames Bottomley /* Fudge to tell if we're minimum or maximum */
493d24e1eebSJames Bottomley #define sas_phy_store_linkspeed(field)					\
494d24e1eebSJames Bottomley static ssize_t								\
495ee959b00STony Jones store_sas_phy_##field(struct device *dev, 				\
496ee959b00STony Jones 		      struct device_attribute *attr, 			\
497ee959b00STony Jones 		      const char *buf,	size_t count)			\
498d24e1eebSJames Bottomley {									\
499ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
500d24e1eebSJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
501d24e1eebSJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
502d24e1eebSJames Bottomley 	u32 value;							\
503d24e1eebSJames Bottomley 	struct sas_phy_linkrates rates = {0};				\
504d24e1eebSJames Bottomley 	int error;							\
505d24e1eebSJames Bottomley 									\
506d24e1eebSJames Bottomley 	error = set_sas_linkspeed_names(&value, buf);			\
507d24e1eebSJames Bottomley 	if (error)							\
508d24e1eebSJames Bottomley 		return error;						\
509d24e1eebSJames Bottomley 	rates.field = value;						\
510d24e1eebSJames Bottomley 	error = i->f->set_phy_speed(phy, &rates);			\
511d24e1eebSJames Bottomley 									\
512d24e1eebSJames Bottomley 	return error ? error : count;					\
513d24e1eebSJames Bottomley }
514d24e1eebSJames Bottomley 
515d24e1eebSJames Bottomley #define sas_phy_linkspeed_rw_attr(field)				\
516d24e1eebSJames Bottomley 	sas_phy_show_linkspeed(field)					\
517d24e1eebSJames Bottomley 	sas_phy_store_linkspeed(field)					\
518ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
519d24e1eebSJames Bottomley 	store_sas_phy_##field)
520d24e1eebSJames Bottomley 
521c7ebbbceSChristoph Hellwig #define sas_phy_linkspeed_attr(field)					\
522c7ebbbceSChristoph Hellwig 	sas_phy_show_linkspeed(field)					\
523ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
524c7ebbbceSChristoph Hellwig 
525d24e1eebSJames Bottomley 
526c3ee74c4SChristoph Hellwig #define sas_phy_show_linkerror(field)					\
527c3ee74c4SChristoph Hellwig static ssize_t								\
528ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
529ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
530c3ee74c4SChristoph Hellwig {									\
531ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
532c3ee74c4SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
533c3ee74c4SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
534c3ee74c4SChristoph Hellwig 	int error;							\
535c3ee74c4SChristoph Hellwig 									\
536dd9fbb52SJames Bottomley 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
537c3ee74c4SChristoph Hellwig 	if (error)							\
538c3ee74c4SChristoph Hellwig 		return error;						\
539c3ee74c4SChristoph Hellwig 	return snprintf(buf, 20, "%u\n", phy->field);			\
540c3ee74c4SChristoph Hellwig }
541c3ee74c4SChristoph Hellwig 
542c3ee74c4SChristoph Hellwig #define sas_phy_linkerror_attr(field)					\
543c3ee74c4SChristoph Hellwig 	sas_phy_show_linkerror(field)					\
544ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
545c3ee74c4SChristoph Hellwig 
546c3ee74c4SChristoph Hellwig 
547c7ebbbceSChristoph Hellwig static ssize_t
548ee959b00STony Jones show_sas_device_type(struct device *dev,
549ee959b00STony Jones 		     struct device_attribute *attr, char *buf)
550c7ebbbceSChristoph Hellwig {
551ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
552c7ebbbceSChristoph Hellwig 
553c7ebbbceSChristoph Hellwig 	if (!phy->identify.device_type)
554c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
555c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(phy->identify.device_type, buf);
556c7ebbbceSChristoph Hellwig }
557ee959b00STony Jones static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
558c7ebbbceSChristoph Hellwig 
559ee959b00STony Jones static ssize_t do_sas_phy_enable(struct device *dev,
560acbf167dSDarrick J. Wong 		size_t count, int enable)
561acbf167dSDarrick J. Wong {
562ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
563acbf167dSDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
564acbf167dSDarrick J. Wong 	struct sas_internal *i = to_sas_internal(shost->transportt);
565acbf167dSDarrick J. Wong 	int error;
566acbf167dSDarrick J. Wong 
567acbf167dSDarrick J. Wong 	error = i->f->phy_enable(phy, enable);
568acbf167dSDarrick J. Wong 	if (error)
569acbf167dSDarrick J. Wong 		return error;
570acbf167dSDarrick J. Wong 	phy->enabled = enable;
571acbf167dSDarrick J. Wong 	return count;
572acbf167dSDarrick J. Wong };
573acbf167dSDarrick J. Wong 
574ee959b00STony Jones static ssize_t
575ee959b00STony Jones store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
576acbf167dSDarrick J. Wong 		     const char *buf, size_t count)
577acbf167dSDarrick J. Wong {
578acbf167dSDarrick J. Wong 	if (count < 1)
579acbf167dSDarrick J. Wong 		return -EINVAL;
580acbf167dSDarrick J. Wong 
581acbf167dSDarrick J. Wong 	switch (buf[0]) {
582acbf167dSDarrick J. Wong 	case '0':
583ee959b00STony Jones 		do_sas_phy_enable(dev, count, 0);
584acbf167dSDarrick J. Wong 		break;
585acbf167dSDarrick J. Wong 	case '1':
586ee959b00STony Jones 		do_sas_phy_enable(dev, count, 1);
587acbf167dSDarrick J. Wong 		break;
588acbf167dSDarrick J. Wong 	default:
589acbf167dSDarrick J. Wong 		return -EINVAL;
590acbf167dSDarrick J. Wong 	}
591acbf167dSDarrick J. Wong 
592acbf167dSDarrick J. Wong 	return count;
593acbf167dSDarrick J. Wong }
594acbf167dSDarrick J. Wong 
595ee959b00STony Jones static ssize_t
596ee959b00STony Jones show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
597ee959b00STony Jones 		    char *buf)
598acbf167dSDarrick J. Wong {
599ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
600acbf167dSDarrick J. Wong 
601acbf167dSDarrick J. Wong 	return snprintf(buf, 20, "%d", phy->enabled);
602acbf167dSDarrick J. Wong }
603acbf167dSDarrick J. Wong 
604ee959b00STony Jones static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
605acbf167dSDarrick J. Wong 			 store_sas_phy_enable);
606acbf167dSDarrick J. Wong 
607ee959b00STony Jones static ssize_t
608ee959b00STony Jones do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
60907ba3a95SChristoph Hellwig {
610ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
61107ba3a95SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
61207ba3a95SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
61307ba3a95SChristoph Hellwig 	int error;
61407ba3a95SChristoph Hellwig 
61507ba3a95SChristoph Hellwig 	error = i->f->phy_reset(phy, hard_reset);
61607ba3a95SChristoph Hellwig 	if (error)
61707ba3a95SChristoph Hellwig 		return error;
61807ba3a95SChristoph Hellwig 	return count;
61907ba3a95SChristoph Hellwig };
62007ba3a95SChristoph Hellwig 
621ee959b00STony Jones static ssize_t
622ee959b00STony Jones store_sas_link_reset(struct device *dev, struct device_attribute *attr,
62307ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
62407ba3a95SChristoph Hellwig {
625ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 0);
62607ba3a95SChristoph Hellwig }
627ee959b00STony Jones static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
62807ba3a95SChristoph Hellwig 
629ee959b00STony Jones static ssize_t
630ee959b00STony Jones store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
63107ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
63207ba3a95SChristoph Hellwig {
633ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 1);
63407ba3a95SChristoph Hellwig }
635ee959b00STony Jones static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
63607ba3a95SChristoph Hellwig 
637c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.initiator_port_protocols,
638c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
639c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.target_port_protocols,
640c7ebbbceSChristoph Hellwig 		target_port_protocols);
641c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
642c7ebbbceSChristoph Hellwig 		unsigned long long);
643c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
644c9fefeb2SJames Bottomley //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
645c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(negotiated_linkrate);
646c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(minimum_linkrate_hw);
647d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(minimum_linkrate);
648c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(maximum_linkrate_hw);
649d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(maximum_linkrate);
650c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(invalid_dword_count);
651c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(running_disparity_error_count);
652c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(loss_of_dword_sync_count);
653c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(phy_reset_problem_count);
654c7ebbbceSChristoph Hellwig 
655c7ebbbceSChristoph Hellwig 
656c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_phy_class,
657c7ebbbceSChristoph Hellwig 		"sas_phy", NULL, NULL, NULL);
658c7ebbbceSChristoph Hellwig 
659c7ebbbceSChristoph Hellwig static int sas_phy_match(struct attribute_container *cont, struct device *dev)
660c7ebbbceSChristoph Hellwig {
661c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
662c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
663c7ebbbceSChristoph Hellwig 
664c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_phy(dev))
665c7ebbbceSChristoph Hellwig 		return 0;
666c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent);
667c7ebbbceSChristoph Hellwig 
668c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
669c7ebbbceSChristoph Hellwig 		return 0;
670c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
671c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
672c7ebbbceSChristoph Hellwig 		return 0;
673c7ebbbceSChristoph Hellwig 
674c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
675c7ebbbceSChristoph Hellwig 	return &i->phy_attr_cont.ac == cont;
676c7ebbbceSChristoph Hellwig }
677c7ebbbceSChristoph Hellwig 
678c7ebbbceSChristoph Hellwig static void sas_phy_release(struct device *dev)
679c7ebbbceSChristoph Hellwig {
680c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(dev);
681c7ebbbceSChristoph Hellwig 
682c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
683c7ebbbceSChristoph Hellwig 	kfree(phy);
684c7ebbbceSChristoph Hellwig }
685c7ebbbceSChristoph Hellwig 
686c7ebbbceSChristoph Hellwig /**
687eb44820cSRob Landley  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
688c7ebbbceSChristoph Hellwig  * @parent:	Parent device
689d99ca418SMoore, Eric  * @number:	Phy index
690c7ebbbceSChristoph Hellwig  *
691c7ebbbceSChristoph Hellwig  * Allocates an SAS PHY structure.  It will be added in the device tree
692c7ebbbceSChristoph Hellwig  * below the device specified by @parent, which has to be either a Scsi_Host
693c7ebbbceSChristoph Hellwig  * or sas_rphy.
694c7ebbbceSChristoph Hellwig  *
695c7ebbbceSChristoph Hellwig  * Returns:
696c7ebbbceSChristoph Hellwig  *	SAS PHY allocated or %NULL if the allocation failed.
697c7ebbbceSChristoph Hellwig  */
698c7ebbbceSChristoph Hellwig struct sas_phy *sas_phy_alloc(struct device *parent, int number)
699c7ebbbceSChristoph Hellwig {
700c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent);
701c7ebbbceSChristoph Hellwig 	struct sas_phy *phy;
702c7ebbbceSChristoph Hellwig 
70324669f75SJes Sorensen 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
704c7ebbbceSChristoph Hellwig 	if (!phy)
705c7ebbbceSChristoph Hellwig 		return NULL;
706c7ebbbceSChristoph Hellwig 
707c7ebbbceSChristoph Hellwig 	phy->number = number;
708acbf167dSDarrick J. Wong 	phy->enabled = 1;
709c7ebbbceSChristoph Hellwig 
710c7ebbbceSChristoph Hellwig 	device_initialize(&phy->dev);
711c7ebbbceSChristoph Hellwig 	phy->dev.parent = get_device(parent);
712c7ebbbceSChristoph Hellwig 	phy->dev.release = sas_phy_release;
71365c92b09SJames Bottomley 	INIT_LIST_HEAD(&phy->port_siblings);
71479cb1819SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
71579cb1819SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
71671610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
71779cb1819SJames Bottomley 			rphy->scsi_target_id, number);
71879cb1819SJames Bottomley 	} else
71971610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
720c7ebbbceSChristoph Hellwig 
721c7ebbbceSChristoph Hellwig 	transport_setup_device(&phy->dev);
722c7ebbbceSChristoph Hellwig 
723c7ebbbceSChristoph Hellwig 	return phy;
724c7ebbbceSChristoph Hellwig }
725c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_alloc);
726c7ebbbceSChristoph Hellwig 
727c7ebbbceSChristoph Hellwig /**
728eb44820cSRob Landley  * sas_phy_add  -  add a SAS PHY to the device hierarchy
729c7ebbbceSChristoph Hellwig  * @phy:	The PHY to be added
730c7ebbbceSChristoph Hellwig  *
731c7ebbbceSChristoph Hellwig  * Publishes a SAS PHY to the rest of the system.
732c7ebbbceSChristoph Hellwig  */
733c7ebbbceSChristoph Hellwig int sas_phy_add(struct sas_phy *phy)
734c7ebbbceSChristoph Hellwig {
735c7ebbbceSChristoph Hellwig 	int error;
736c7ebbbceSChristoph Hellwig 
737c7ebbbceSChristoph Hellwig 	error = device_add(&phy->dev);
738c7ebbbceSChristoph Hellwig 	if (!error) {
739c7ebbbceSChristoph Hellwig 		transport_add_device(&phy->dev);
740c7ebbbceSChristoph Hellwig 		transport_configure_device(&phy->dev);
741c7ebbbceSChristoph Hellwig 	}
742c7ebbbceSChristoph Hellwig 
743c7ebbbceSChristoph Hellwig 	return error;
744c7ebbbceSChristoph Hellwig }
745c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_add);
746c7ebbbceSChristoph Hellwig 
747c7ebbbceSChristoph Hellwig /**
748eb44820cSRob Landley  * sas_phy_free  -  free a SAS PHY
749c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to free
750c7ebbbceSChristoph Hellwig  *
751c7ebbbceSChristoph Hellwig  * Frees the specified SAS PHY.
752c7ebbbceSChristoph Hellwig  *
753c7ebbbceSChristoph Hellwig  * Note:
754c7ebbbceSChristoph Hellwig  *   This function must only be called on a PHY that has not
755af901ca1SAndré Goddard Rosa  *   successfully been added using sas_phy_add().
756c7ebbbceSChristoph Hellwig  */
757c7ebbbceSChristoph Hellwig void sas_phy_free(struct sas_phy *phy)
758c7ebbbceSChristoph Hellwig {
759c7ebbbceSChristoph Hellwig 	transport_destroy_device(&phy->dev);
76092aab646SMike Anderson 	put_device(&phy->dev);
761c7ebbbceSChristoph Hellwig }
762c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_free);
763c7ebbbceSChristoph Hellwig 
764c7ebbbceSChristoph Hellwig /**
765eb44820cSRob Landley  * sas_phy_delete  -  remove SAS PHY
766c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to remove
767c7ebbbceSChristoph Hellwig  *
768c7ebbbceSChristoph Hellwig  * Removes the specified SAS PHY.  If the SAS PHY has an
769c7ebbbceSChristoph Hellwig  * associated remote PHY it is removed before.
770c7ebbbceSChristoph Hellwig  */
771c7ebbbceSChristoph Hellwig void
772c7ebbbceSChristoph Hellwig sas_phy_delete(struct sas_phy *phy)
773c7ebbbceSChristoph Hellwig {
774c7ebbbceSChristoph Hellwig 	struct device *dev = &phy->dev;
775c7ebbbceSChristoph Hellwig 
77665c92b09SJames Bottomley 	/* this happens if the phy is still part of a port when deleted */
77765c92b09SJames Bottomley 	BUG_ON(!list_empty(&phy->port_siblings));
778c7ebbbceSChristoph Hellwig 
779c7ebbbceSChristoph Hellwig 	transport_remove_device(dev);
780c7ebbbceSChristoph Hellwig 	device_del(dev);
781c7ebbbceSChristoph Hellwig 	transport_destroy_device(dev);
78292aab646SMike Anderson 	put_device(dev);
783c7ebbbceSChristoph Hellwig }
784c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_delete);
785c7ebbbceSChristoph Hellwig 
786c7ebbbceSChristoph Hellwig /**
787eb44820cSRob Landley  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
788c7ebbbceSChristoph Hellwig  * @dev:	device to check
789c7ebbbceSChristoph Hellwig  *
790c7ebbbceSChristoph Hellwig  * Returns:
791c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS PHY, %0 else
792c7ebbbceSChristoph Hellwig  */
793c7ebbbceSChristoph Hellwig int scsi_is_sas_phy(const struct device *dev)
794c7ebbbceSChristoph Hellwig {
795c7ebbbceSChristoph Hellwig 	return dev->release == sas_phy_release;
796c7ebbbceSChristoph Hellwig }
797c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_phy);
798c7ebbbceSChristoph Hellwig 
799c7ebbbceSChristoph Hellwig /*
80065c92b09SJames Bottomley  * SAS Port attributes
80165c92b09SJames Bottomley  */
80265c92b09SJames Bottomley #define sas_port_show_simple(field, name, format_string, cast)		\
80365c92b09SJames Bottomley static ssize_t								\
804ee959b00STony Jones show_sas_port_##name(struct device *dev, 				\
805ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
80665c92b09SJames Bottomley {									\
807ee959b00STony Jones 	struct sas_port *port = transport_class_to_sas_port(dev);	\
80865c92b09SJames Bottomley 									\
80965c92b09SJames Bottomley 	return snprintf(buf, 20, format_string, cast port->field);	\
81065c92b09SJames Bottomley }
81165c92b09SJames Bottomley 
81265c92b09SJames Bottomley #define sas_port_simple_attr(field, name, format_string, type)		\
81365c92b09SJames Bottomley 	sas_port_show_simple(field, name, format_string, (type))	\
814ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
81565c92b09SJames Bottomley 
81665c92b09SJames Bottomley sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
81765c92b09SJames Bottomley 
81865c92b09SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_port_class,
81965c92b09SJames Bottomley 			       "sas_port", NULL, NULL, NULL);
82065c92b09SJames Bottomley 
82165c92b09SJames Bottomley static int sas_port_match(struct attribute_container *cont, struct device *dev)
82265c92b09SJames Bottomley {
82365c92b09SJames Bottomley 	struct Scsi_Host *shost;
82465c92b09SJames Bottomley 	struct sas_internal *i;
82565c92b09SJames Bottomley 
82665c92b09SJames Bottomley 	if (!scsi_is_sas_port(dev))
82765c92b09SJames Bottomley 		return 0;
82865c92b09SJames Bottomley 	shost = dev_to_shost(dev->parent);
82965c92b09SJames Bottomley 
83065c92b09SJames Bottomley 	if (!shost->transportt)
83165c92b09SJames Bottomley 		return 0;
83265c92b09SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
83365c92b09SJames Bottomley 			&sas_host_class.class)
83465c92b09SJames Bottomley 		return 0;
83565c92b09SJames Bottomley 
83665c92b09SJames Bottomley 	i = to_sas_internal(shost->transportt);
83765c92b09SJames Bottomley 	return &i->port_attr_cont.ac == cont;
83865c92b09SJames Bottomley }
83965c92b09SJames Bottomley 
84065c92b09SJames Bottomley 
84165c92b09SJames Bottomley static void sas_port_release(struct device *dev)
84265c92b09SJames Bottomley {
84365c92b09SJames Bottomley 	struct sas_port *port = dev_to_sas_port(dev);
84465c92b09SJames Bottomley 
84565c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
84665c92b09SJames Bottomley 
84765c92b09SJames Bottomley 	put_device(dev->parent);
84865c92b09SJames Bottomley 	kfree(port);
84965c92b09SJames Bottomley }
85065c92b09SJames Bottomley 
85165c92b09SJames Bottomley static void sas_port_create_link(struct sas_port *port,
85265c92b09SJames Bottomley 				 struct sas_phy *phy)
85365c92b09SJames Bottomley {
85421434966SDarrick J. Wong 	int res;
85521434966SDarrick J. Wong 
85621434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
85771610f55SKay Sievers 				dev_name(&phy->dev));
85821434966SDarrick J. Wong 	if (res)
85921434966SDarrick J. Wong 		goto err;
86021434966SDarrick J. Wong 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
86121434966SDarrick J. Wong 	if (res)
86221434966SDarrick J. Wong 		goto err;
86321434966SDarrick J. Wong 	return;
86421434966SDarrick J. Wong err:
86521434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
866cadbd4a5SHarvey Harrison 	       __func__, res);
86765c92b09SJames Bottomley }
86865c92b09SJames Bottomley 
86965c92b09SJames Bottomley static void sas_port_delete_link(struct sas_port *port,
87065c92b09SJames Bottomley 				 struct sas_phy *phy)
87165c92b09SJames Bottomley {
87271610f55SKay Sievers 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
87365c92b09SJames Bottomley 	sysfs_remove_link(&phy->dev.kobj, "port");
87465c92b09SJames Bottomley }
87565c92b09SJames Bottomley 
87665c92b09SJames Bottomley /** sas_port_alloc - allocate and initialize a SAS port structure
87765c92b09SJames Bottomley  *
87865c92b09SJames Bottomley  * @parent:	parent device
87965c92b09SJames Bottomley  * @port_id:	port number
88065c92b09SJames Bottomley  *
88165c92b09SJames Bottomley  * Allocates a SAS port structure.  It will be added to the device tree
88265c92b09SJames Bottomley  * below the device specified by @parent which must be either a Scsi_Host
88365c92b09SJames Bottomley  * or a sas_expander_device.
88465c92b09SJames Bottomley  *
88565c92b09SJames Bottomley  * Returns %NULL on error
88665c92b09SJames Bottomley  */
88765c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *parent, int port_id)
88865c92b09SJames Bottomley {
88965c92b09SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
89065c92b09SJames Bottomley 	struct sas_port *port;
89165c92b09SJames Bottomley 
89265c92b09SJames Bottomley 	port = kzalloc(sizeof(*port), GFP_KERNEL);
89365c92b09SJames Bottomley 	if (!port)
89465c92b09SJames Bottomley 		return NULL;
89565c92b09SJames Bottomley 
89665c92b09SJames Bottomley 	port->port_identifier = port_id;
89765c92b09SJames Bottomley 
89865c92b09SJames Bottomley 	device_initialize(&port->dev);
89965c92b09SJames Bottomley 
90065c92b09SJames Bottomley 	port->dev.parent = get_device(parent);
90165c92b09SJames Bottomley 	port->dev.release = sas_port_release;
90265c92b09SJames Bottomley 
90365c92b09SJames Bottomley 	mutex_init(&port->phy_list_mutex);
90465c92b09SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
90565c92b09SJames Bottomley 
90665c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
90765c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
90871610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
90965c92b09SJames Bottomley 			     rphy->scsi_target_id, port->port_identifier);
91065c92b09SJames Bottomley 	} else
91171610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
91265c92b09SJames Bottomley 			     port->port_identifier);
91365c92b09SJames Bottomley 
91465c92b09SJames Bottomley 	transport_setup_device(&port->dev);
91565c92b09SJames Bottomley 
91665c92b09SJames Bottomley 	return port;
91765c92b09SJames Bottomley }
91865c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_alloc);
91965c92b09SJames Bottomley 
920c9fefeb2SJames Bottomley /** sas_port_alloc_num - allocate and initialize a SAS port structure
921c9fefeb2SJames Bottomley  *
922c9fefeb2SJames Bottomley  * @parent:	parent device
923c9fefeb2SJames Bottomley  *
924c9fefeb2SJames Bottomley  * Allocates a SAS port structure and a number to go with it.  This
925c9fefeb2SJames Bottomley  * interface is really for adapters where the port number has no
926c9fefeb2SJames Bottomley  * meansing, so the sas class should manage them.  It will be added to
927c9fefeb2SJames Bottomley  * the device tree below the device specified by @parent which must be
928c9fefeb2SJames Bottomley  * either a Scsi_Host or a sas_expander_device.
929c9fefeb2SJames Bottomley  *
930c9fefeb2SJames Bottomley  * Returns %NULL on error
931c9fefeb2SJames Bottomley  */
932c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *parent)
933c9fefeb2SJames Bottomley {
934c9fefeb2SJames Bottomley 	int index;
935c9fefeb2SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
936c9fefeb2SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
937c9fefeb2SJames Bottomley 
938c9fefeb2SJames Bottomley 	/* FIXME: use idr for this eventually */
939c9fefeb2SJames Bottomley 	mutex_lock(&sas_host->lock);
940c9fefeb2SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
941c9fefeb2SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
942c9fefeb2SJames Bottomley 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
943c9fefeb2SJames Bottomley 
944c9fefeb2SJames Bottomley 		index = exp->next_port_id++;
945c9fefeb2SJames Bottomley 	} else
946c9fefeb2SJames Bottomley 		index = sas_host->next_port_id++;
947c9fefeb2SJames Bottomley 	mutex_unlock(&sas_host->lock);
948c9fefeb2SJames Bottomley 	return sas_port_alloc(parent, index);
949c9fefeb2SJames Bottomley }
950c9fefeb2SJames Bottomley EXPORT_SYMBOL(sas_port_alloc_num);
951c9fefeb2SJames Bottomley 
95265c92b09SJames Bottomley /**
95365c92b09SJames Bottomley  * sas_port_add - add a SAS port to the device hierarchy
95465c92b09SJames Bottomley  * @port:	port to be added
95565c92b09SJames Bottomley  *
95665c92b09SJames Bottomley  * publishes a port to the rest of the system
95765c92b09SJames Bottomley  */
95865c92b09SJames Bottomley int sas_port_add(struct sas_port *port)
95965c92b09SJames Bottomley {
96065c92b09SJames Bottomley 	int error;
96165c92b09SJames Bottomley 
96265c92b09SJames Bottomley 	/* No phys should be added until this is made visible */
96365c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
96465c92b09SJames Bottomley 
96565c92b09SJames Bottomley 	error = device_add(&port->dev);
96665c92b09SJames Bottomley 
96765c92b09SJames Bottomley 	if (error)
96865c92b09SJames Bottomley 		return error;
96965c92b09SJames Bottomley 
97065c92b09SJames Bottomley 	transport_add_device(&port->dev);
97165c92b09SJames Bottomley 	transport_configure_device(&port->dev);
97265c92b09SJames Bottomley 
97365c92b09SJames Bottomley 	return 0;
97465c92b09SJames Bottomley }
97565c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add);
97665c92b09SJames Bottomley 
97765c92b09SJames Bottomley /**
978eb44820cSRob Landley  * sas_port_free  -  free a SAS PORT
97965c92b09SJames Bottomley  * @port:	SAS PORT to free
98065c92b09SJames Bottomley  *
98165c92b09SJames Bottomley  * Frees the specified SAS PORT.
98265c92b09SJames Bottomley  *
98365c92b09SJames Bottomley  * Note:
98465c92b09SJames Bottomley  *   This function must only be called on a PORT that has not
985af901ca1SAndré Goddard Rosa  *   successfully been added using sas_port_add().
98665c92b09SJames Bottomley  */
98765c92b09SJames Bottomley void sas_port_free(struct sas_port *port)
98865c92b09SJames Bottomley {
98965c92b09SJames Bottomley 	transport_destroy_device(&port->dev);
99065c92b09SJames Bottomley 	put_device(&port->dev);
99165c92b09SJames Bottomley }
99265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_free);
99365c92b09SJames Bottomley 
99465c92b09SJames Bottomley /**
995eb44820cSRob Landley  * sas_port_delete  -  remove SAS PORT
99665c92b09SJames Bottomley  * @port:	SAS PORT to remove
99765c92b09SJames Bottomley  *
99865c92b09SJames Bottomley  * Removes the specified SAS PORT.  If the SAS PORT has an
99965c92b09SJames Bottomley  * associated phys, unlink them from the port as well.
100065c92b09SJames Bottomley  */
100165c92b09SJames Bottomley void sas_port_delete(struct sas_port *port)
100265c92b09SJames Bottomley {
100365c92b09SJames Bottomley 	struct device *dev = &port->dev;
100465c92b09SJames Bottomley 	struct sas_phy *phy, *tmp_phy;
100565c92b09SJames Bottomley 
100665c92b09SJames Bottomley 	if (port->rphy) {
100765c92b09SJames Bottomley 		sas_rphy_delete(port->rphy);
100865c92b09SJames Bottomley 		port->rphy = NULL;
100965c92b09SJames Bottomley 	}
101065c92b09SJames Bottomley 
101165c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
101265c92b09SJames Bottomley 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
101365c92b09SJames Bottomley 				 port_siblings) {
101465c92b09SJames Bottomley 		sas_port_delete_link(port, phy);
101565c92b09SJames Bottomley 		list_del_init(&phy->port_siblings);
101665c92b09SJames Bottomley 	}
101765c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
101865c92b09SJames Bottomley 
1019a0e1b6efSJames Bottomley 	if (port->is_backlink) {
1020a0e1b6efSJames Bottomley 		struct device *parent = port->dev.parent;
1021a0e1b6efSJames Bottomley 
102271610f55SKay Sievers 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1023a0e1b6efSJames Bottomley 		port->is_backlink = 0;
1024a0e1b6efSJames Bottomley 	}
1025a0e1b6efSJames Bottomley 
102665c92b09SJames Bottomley 	transport_remove_device(dev);
102765c92b09SJames Bottomley 	device_del(dev);
102865c92b09SJames Bottomley 	transport_destroy_device(dev);
102965c92b09SJames Bottomley 	put_device(dev);
103065c92b09SJames Bottomley }
103165c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete);
103265c92b09SJames Bottomley 
103365c92b09SJames Bottomley /**
1034eb44820cSRob Landley  * scsi_is_sas_port -  check if a struct device represents a SAS port
103565c92b09SJames Bottomley  * @dev:	device to check
103665c92b09SJames Bottomley  *
103765c92b09SJames Bottomley  * Returns:
103865c92b09SJames Bottomley  *	%1 if the device represents a SAS Port, %0 else
103965c92b09SJames Bottomley  */
104065c92b09SJames Bottomley int scsi_is_sas_port(const struct device *dev)
104165c92b09SJames Bottomley {
104265c92b09SJames Bottomley 	return dev->release == sas_port_release;
104365c92b09SJames Bottomley }
104465c92b09SJames Bottomley EXPORT_SYMBOL(scsi_is_sas_port);
104565c92b09SJames Bottomley 
104665c92b09SJames Bottomley /**
104765c92b09SJames Bottomley  * sas_port_add_phy - add another phy to a port to form a wide port
104865c92b09SJames Bottomley  * @port:	port to add the phy to
104965c92b09SJames Bottomley  * @phy:	phy to add
105065c92b09SJames Bottomley  *
105165c92b09SJames Bottomley  * When a port is initially created, it is empty (has no phys).  All
105265c92b09SJames Bottomley  * ports must have at least one phy to operated, and all wide ports
105365c92b09SJames Bottomley  * must have at least two.  The current code makes no difference
105465c92b09SJames Bottomley  * between ports and wide ports, but the only object that can be
105565c92b09SJames Bottomley  * connected to a remote device is a port, so ports must be formed on
105665c92b09SJames Bottomley  * all devices with phys if they're connected to anything.
105765c92b09SJames Bottomley  */
105865c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
105965c92b09SJames Bottomley {
106065c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
106165c92b09SJames Bottomley 	if (unlikely(!list_empty(&phy->port_siblings))) {
106265c92b09SJames Bottomley 		/* make sure we're already on this port */
106365c92b09SJames Bottomley 		struct sas_phy *tmp;
106465c92b09SJames Bottomley 
106565c92b09SJames Bottomley 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
106665c92b09SJames Bottomley 			if (tmp == phy)
106765c92b09SJames Bottomley 				break;
106865c92b09SJames Bottomley 		/* If this trips, you added a phy that was already
106965c92b09SJames Bottomley 		 * part of a different port */
107065c92b09SJames Bottomley 		if (unlikely(tmp != phy)) {
107171610f55SKay Sievers 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
107271610f55SKay Sievers 				   dev_name(&phy->dev));
107365c92b09SJames Bottomley 			BUG();
107465c92b09SJames Bottomley 		}
107565c92b09SJames Bottomley 	} else {
107665c92b09SJames Bottomley 		sas_port_create_link(port, phy);
107765c92b09SJames Bottomley 		list_add_tail(&phy->port_siblings, &port->phy_list);
107865c92b09SJames Bottomley 		port->num_phys++;
107965c92b09SJames Bottomley 	}
108065c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
108165c92b09SJames Bottomley }
108265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add_phy);
108365c92b09SJames Bottomley 
108465c92b09SJames Bottomley /**
108565c92b09SJames Bottomley  * sas_port_delete_phy - remove a phy from a port or wide port
108665c92b09SJames Bottomley  * @port:	port to remove the phy from
108765c92b09SJames Bottomley  * @phy:	phy to remove
108865c92b09SJames Bottomley  *
108965c92b09SJames Bottomley  * This operation is used for tearing down ports again.  It must be
109065c92b09SJames Bottomley  * done to every port or wide port before calling sas_port_delete.
109165c92b09SJames Bottomley  */
109265c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
109365c92b09SJames Bottomley {
109465c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
109565c92b09SJames Bottomley 	sas_port_delete_link(port, phy);
109665c92b09SJames Bottomley 	list_del_init(&phy->port_siblings);
109765c92b09SJames Bottomley 	port->num_phys--;
109865c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
109965c92b09SJames Bottomley }
110065c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete_phy);
110165c92b09SJames Bottomley 
1102a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *port)
1103a0e1b6efSJames Bottomley {
110421434966SDarrick J. Wong 	int res;
1105a0e1b6efSJames Bottomley 	struct device *parent = port->dev.parent->parent->parent;
1106a0e1b6efSJames Bottomley 
1107a0e1b6efSJames Bottomley 	if (port->is_backlink)
1108a0e1b6efSJames Bottomley 		return;
1109a0e1b6efSJames Bottomley 	port->is_backlink = 1;
111021434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
111171610f55SKay Sievers 				dev_name(parent));
111221434966SDarrick J. Wong 	if (res)
111321434966SDarrick J. Wong 		goto err;
111421434966SDarrick J. Wong 	return;
111521434966SDarrick J. Wong err:
111621434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1117cadbd4a5SHarvey Harrison 	       __func__, res);
1118a0e1b6efSJames Bottomley 
1119a0e1b6efSJames Bottomley }
1120a0e1b6efSJames Bottomley EXPORT_SYMBOL(sas_port_mark_backlink);
1121a0e1b6efSJames Bottomley 
112265c92b09SJames Bottomley /*
1123c7ebbbceSChristoph Hellwig  * SAS remote PHY attributes.
1124c7ebbbceSChristoph Hellwig  */
1125c7ebbbceSChristoph Hellwig 
1126c7ebbbceSChristoph Hellwig #define sas_rphy_show_simple(field, name, format_string, cast)		\
1127c7ebbbceSChristoph Hellwig static ssize_t								\
1128ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1129ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1130c7ebbbceSChristoph Hellwig {									\
1131ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1132c7ebbbceSChristoph Hellwig 									\
1133c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1134c7ebbbceSChristoph Hellwig }
1135c7ebbbceSChristoph Hellwig 
1136c7ebbbceSChristoph Hellwig #define sas_rphy_simple_attr(field, name, format_string, type)		\
1137c7ebbbceSChristoph Hellwig 	sas_rphy_show_simple(field, name, format_string, (type))	\
1138ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1139c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1140c7ebbbceSChristoph Hellwig 
1141c7ebbbceSChristoph Hellwig #define sas_rphy_show_protocol(field, name)				\
1142c7ebbbceSChristoph Hellwig static ssize_t								\
1143ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1144ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1145c7ebbbceSChristoph Hellwig {									\
1146ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1147c7ebbbceSChristoph Hellwig 									\
1148c7ebbbceSChristoph Hellwig 	if (!rphy->field)					\
1149c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
1150c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(rphy->field, buf);	\
1151c7ebbbceSChristoph Hellwig }
1152c7ebbbceSChristoph Hellwig 
1153c7ebbbceSChristoph Hellwig #define sas_rphy_protocol_attr(field, name)				\
1154c7ebbbceSChristoph Hellwig 	sas_rphy_show_protocol(field, name)				\
1155ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1156c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1157c7ebbbceSChristoph Hellwig 
1158c7ebbbceSChristoph Hellwig static ssize_t
1159ee959b00STony Jones show_sas_rphy_device_type(struct device *dev,
1160ee959b00STony Jones 			  struct device_attribute *attr, char *buf)
1161c7ebbbceSChristoph Hellwig {
1162ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1163c7ebbbceSChristoph Hellwig 
1164c7ebbbceSChristoph Hellwig 	if (!rphy->identify.device_type)
1165c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
1166c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(
1167c7ebbbceSChristoph Hellwig 			rphy->identify.device_type, buf);
1168c7ebbbceSChristoph Hellwig }
1169c7ebbbceSChristoph Hellwig 
1170ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1171c7ebbbceSChristoph Hellwig 		show_sas_rphy_device_type, NULL);
1172c7ebbbceSChristoph Hellwig 
1173a0125641SChristoph Hellwig static ssize_t
1174ee959b00STony Jones show_sas_rphy_enclosure_identifier(struct device *dev,
1175ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
1176a0125641SChristoph Hellwig {
1177ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1178a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1179a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1180a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1181a0125641SChristoph Hellwig 	u64 identifier;
1182a0125641SChristoph Hellwig 	int error;
1183a0125641SChristoph Hellwig 
1184a0125641SChristoph Hellwig 	/*
1185a0125641SChristoph Hellwig 	 * Only devices behind an expander are supported, because the
1186a0125641SChristoph Hellwig 	 * enclosure identifier is a SMP feature.
1187a0125641SChristoph Hellwig 	 */
1188f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1189a0125641SChristoph Hellwig 		return -EINVAL;
1190a0125641SChristoph Hellwig 
1191a0125641SChristoph Hellwig 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1192a0125641SChristoph Hellwig 	if (error)
1193a0125641SChristoph Hellwig 		return error;
1194a0125641SChristoph Hellwig 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1195a0125641SChristoph Hellwig }
1196a0125641SChristoph Hellwig 
1197ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1198a0125641SChristoph Hellwig 		show_sas_rphy_enclosure_identifier, NULL);
1199a0125641SChristoph Hellwig 
1200a0125641SChristoph Hellwig static ssize_t
1201ee959b00STony Jones show_sas_rphy_bay_identifier(struct device *dev,
1202ee959b00STony Jones 			     struct device_attribute *attr, char *buf)
1203a0125641SChristoph Hellwig {
1204ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1205a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1206a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1207a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1208a0125641SChristoph Hellwig 	int val;
1209a0125641SChristoph Hellwig 
1210f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1211a0125641SChristoph Hellwig 		return -EINVAL;
1212a0125641SChristoph Hellwig 
1213a0125641SChristoph Hellwig 	val = i->f->get_bay_identifier(rphy);
1214a0125641SChristoph Hellwig 	if (val < 0)
1215a0125641SChristoph Hellwig 		return val;
1216a0125641SChristoph Hellwig 	return sprintf(buf, "%d\n", val);
1217a0125641SChristoph Hellwig }
1218a0125641SChristoph Hellwig 
1219ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1220a0125641SChristoph Hellwig 		show_sas_rphy_bay_identifier, NULL);
1221a0125641SChristoph Hellwig 
1222c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.initiator_port_protocols,
1223c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
1224c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1225c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1226c7ebbbceSChristoph Hellwig 		unsigned long long);
1227c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1228c7ebbbceSChristoph Hellwig 
122942ab0360SJames Bottomley /* only need 8 bytes of data plus header (4 or 8) */
123042ab0360SJames Bottomley #define BUF_SIZE 64
123142ab0360SJames Bottomley 
123242ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *sdev)
123342ab0360SJames Bottomley {
123442ab0360SJames Bottomley 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
12350f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
123642ab0360SJames Bottomley 	struct scsi_mode_data mode_data;
123742ab0360SJames Bottomley 	int res, error;
123842ab0360SJames Bottomley 
123942ab0360SJames Bottomley 	if (!buffer)
124042ab0360SJames Bottomley 		return -ENOMEM;
124142ab0360SJames Bottomley 
124242ab0360SJames Bottomley 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
124342ab0360SJames Bottomley 			      &mode_data, NULL);
124442ab0360SJames Bottomley 
124542ab0360SJames Bottomley 	error = -EINVAL;
124642ab0360SJames Bottomley 	if (!scsi_status_is_good(res))
124742ab0360SJames Bottomley 		goto out;
124842ab0360SJames Bottomley 
124942ab0360SJames Bottomley 	msdata = buffer +  mode_data.header_length +
125042ab0360SJames Bottomley 		mode_data.block_descriptor_length;
125142ab0360SJames Bottomley 
125242ab0360SJames Bottomley 	if (msdata - buffer > BUF_SIZE - 8)
125342ab0360SJames Bottomley 		goto out;
125442ab0360SJames Bottomley 
125542ab0360SJames Bottomley 	error = 0;
125642ab0360SJames Bottomley 
125742ab0360SJames Bottomley 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
125842ab0360SJames Bottomley 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
125942ab0360SJames Bottomley 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
126042ab0360SJames Bottomley 
126142ab0360SJames Bottomley  out:
126242ab0360SJames Bottomley 	kfree(buffer);
126342ab0360SJames Bottomley 	return error;
126442ab0360SJames Bottomley }
126542ab0360SJames Bottomley EXPORT_SYMBOL(sas_read_port_mode_page);
126642ab0360SJames Bottomley 
126779cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
126879cb1819SJames Bottomley 			       "sas_end_device", NULL, NULL, NULL);
126979cb1819SJames Bottomley 
127042ab0360SJames Bottomley #define sas_end_dev_show_simple(field, name, format_string, cast)	\
127142ab0360SJames Bottomley static ssize_t								\
1272ee959b00STony Jones show_sas_end_dev_##name(struct device *dev, 				\
1273ee959b00STony Jones 			struct device_attribute *attr, char *buf)	\
127442ab0360SJames Bottomley {									\
1275ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
127642ab0360SJames Bottomley 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
127742ab0360SJames Bottomley 									\
127842ab0360SJames Bottomley 	return snprintf(buf, 20, format_string, cast rdev->field);	\
127942ab0360SJames Bottomley }
128042ab0360SJames Bottomley 
128142ab0360SJames Bottomley #define sas_end_dev_simple_attr(field, name, format_string, type)	\
128242ab0360SJames Bottomley 	sas_end_dev_show_simple(field, name, format_string, (type))	\
1283ee959b00STony Jones static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
128442ab0360SJames Bottomley 		show_sas_end_dev_##name, NULL)
128542ab0360SJames Bottomley 
128642ab0360SJames Bottomley sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
128742ab0360SJames Bottomley sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
128842ab0360SJames Bottomley 			"%d\n", int);
128942ab0360SJames Bottomley sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
129042ab0360SJames Bottomley 			"%d\n", int);
12910f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_supported, tlr_supported,
12920f88009dSJames Bottomley 			"%d\n", int);
12930f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
12940f88009dSJames Bottomley 			"%d\n", int);
129542ab0360SJames Bottomley 
129679cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_expander_class,
129779cb1819SJames Bottomley 			       "sas_expander", NULL, NULL, NULL);
129879cb1819SJames Bottomley 
129979cb1819SJames Bottomley #define sas_expander_show_simple(field, name, format_string, cast)	\
130079cb1819SJames Bottomley static ssize_t								\
1301ee959b00STony Jones show_sas_expander_##name(struct device *dev, 				\
1302ee959b00STony Jones 			 struct device_attribute *attr, char *buf)	\
130379cb1819SJames Bottomley {									\
1304ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
130579cb1819SJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
130679cb1819SJames Bottomley 									\
130779cb1819SJames Bottomley 	return snprintf(buf, 20, format_string, cast edev->field);	\
130879cb1819SJames Bottomley }
130979cb1819SJames Bottomley 
131079cb1819SJames Bottomley #define sas_expander_simple_attr(field, name, format_string, type)	\
131179cb1819SJames Bottomley 	sas_expander_show_simple(field, name, format_string, (type))	\
1312ee959b00STony Jones static SAS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
131379cb1819SJames Bottomley 		show_sas_expander_##name, NULL)
131479cb1819SJames Bottomley 
131579cb1819SJames Bottomley sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
131679cb1819SJames Bottomley sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
131779cb1819SJames Bottomley sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
131879cb1819SJames Bottomley sas_expander_simple_attr(component_vendor_id, component_vendor_id,
131979cb1819SJames Bottomley 			 "%s\n", char *);
132079cb1819SJames Bottomley sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
132179cb1819SJames Bottomley sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
132279cb1819SJames Bottomley 			 unsigned int);
132379cb1819SJames Bottomley sas_expander_simple_attr(level, level, "%d\n", int);
132442ab0360SJames Bottomley 
1325c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
13262f8600dfSJames Bottomley 		"sas_device", NULL, NULL, NULL);
1327c7ebbbceSChristoph Hellwig 
1328c7ebbbceSChristoph Hellwig static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1329c7ebbbceSChristoph Hellwig {
1330c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
1331c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1332c7ebbbceSChristoph Hellwig 
1333c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_rphy(dev))
1334c7ebbbceSChristoph Hellwig 		return 0;
1335c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent->parent);
1336c7ebbbceSChristoph Hellwig 
1337c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
1338c7ebbbceSChristoph Hellwig 		return 0;
1339c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
1340c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
1341c7ebbbceSChristoph Hellwig 		return 0;
1342c7ebbbceSChristoph Hellwig 
1343c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
1344c7ebbbceSChristoph Hellwig 	return &i->rphy_attr_cont.ac == cont;
1345c7ebbbceSChristoph Hellwig }
1346c7ebbbceSChristoph Hellwig 
134742ab0360SJames Bottomley static int sas_end_dev_match(struct attribute_container *cont,
134842ab0360SJames Bottomley 			     struct device *dev)
134942ab0360SJames Bottomley {
135042ab0360SJames Bottomley 	struct Scsi_Host *shost;
135142ab0360SJames Bottomley 	struct sas_internal *i;
135242ab0360SJames Bottomley 	struct sas_rphy *rphy;
135342ab0360SJames Bottomley 
135442ab0360SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
135542ab0360SJames Bottomley 		return 0;
135642ab0360SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
135742ab0360SJames Bottomley 	rphy = dev_to_rphy(dev);
135842ab0360SJames Bottomley 
135942ab0360SJames Bottomley 	if (!shost->transportt)
136042ab0360SJames Bottomley 		return 0;
136142ab0360SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
136242ab0360SJames Bottomley 			&sas_host_class.class)
136342ab0360SJames Bottomley 		return 0;
136442ab0360SJames Bottomley 
136542ab0360SJames Bottomley 	i = to_sas_internal(shost->transportt);
136642ab0360SJames Bottomley 	return &i->end_dev_attr_cont.ac == cont &&
13672f8600dfSJames Bottomley 		rphy->identify.device_type == SAS_END_DEVICE;
136842ab0360SJames Bottomley }
136942ab0360SJames Bottomley 
137079cb1819SJames Bottomley static int sas_expander_match(struct attribute_container *cont,
137179cb1819SJames Bottomley 			      struct device *dev)
137279cb1819SJames Bottomley {
137379cb1819SJames Bottomley 	struct Scsi_Host *shost;
137479cb1819SJames Bottomley 	struct sas_internal *i;
137579cb1819SJames Bottomley 	struct sas_rphy *rphy;
137679cb1819SJames Bottomley 
137779cb1819SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
137879cb1819SJames Bottomley 		return 0;
137979cb1819SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
138079cb1819SJames Bottomley 	rphy = dev_to_rphy(dev);
138179cb1819SJames Bottomley 
138279cb1819SJames Bottomley 	if (!shost->transportt)
138379cb1819SJames Bottomley 		return 0;
138479cb1819SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
138579cb1819SJames Bottomley 			&sas_host_class.class)
138679cb1819SJames Bottomley 		return 0;
138779cb1819SJames Bottomley 
138879cb1819SJames Bottomley 	i = to_sas_internal(shost->transportt);
138979cb1819SJames Bottomley 	return &i->expander_attr_cont.ac == cont &&
139079cb1819SJames Bottomley 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
13912f8600dfSJames Bottomley 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
139279cb1819SJames Bottomley }
139379cb1819SJames Bottomley 
13942f8600dfSJames Bottomley static void sas_expander_release(struct device *dev)
1395c7ebbbceSChristoph Hellwig {
1396c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = dev_to_rphy(dev);
13972f8600dfSJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1398c7ebbbceSChristoph Hellwig 
139993c20a59SFUJITA Tomonori 	if (rphy->q)
140093c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
140193c20a59SFUJITA Tomonori 
1402c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
14032f8600dfSJames Bottomley 	kfree(edev);
1404c7ebbbceSChristoph Hellwig }
1405c7ebbbceSChristoph Hellwig 
14062f8600dfSJames Bottomley static void sas_end_device_release(struct device *dev)
1407c7ebbbceSChristoph Hellwig {
14082f8600dfSJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(dev);
14092f8600dfSJames Bottomley 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1410c7ebbbceSChristoph Hellwig 
141193c20a59SFUJITA Tomonori 	if (rphy->q)
141293c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
141393c20a59SFUJITA Tomonori 
14142f8600dfSJames Bottomley 	put_device(dev->parent);
14152f8600dfSJames Bottomley 	kfree(edev);
1416c7ebbbceSChristoph Hellwig }
1417c7ebbbceSChristoph Hellwig 
1418c7ebbbceSChristoph Hellwig /**
1419c5943d36SJames Bottomley  * sas_rphy_initialize - common rphy intialization
1420c5943d36SJames Bottomley  * @rphy:	rphy to initialise
1421c5943d36SJames Bottomley  *
1422c5943d36SJames Bottomley  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1423c5943d36SJames Bottomley  * initialise the common rphy component of each.
1424c5943d36SJames Bottomley  */
1425c5943d36SJames Bottomley static void sas_rphy_initialize(struct sas_rphy *rphy)
1426c5943d36SJames Bottomley {
1427c5943d36SJames Bottomley 	INIT_LIST_HEAD(&rphy->list);
1428c5943d36SJames Bottomley }
1429c5943d36SJames Bottomley 
1430c5943d36SJames Bottomley /**
143142ab0360SJames Bottomley  * sas_end_device_alloc - allocate an rphy for an end device
1432eb44820cSRob Landley  * @parent: which port
143342ab0360SJames Bottomley  *
143442ab0360SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
143542ab0360SJames Bottomley  *
143642ab0360SJames Bottomley  * Returns:
143742ab0360SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
143842ab0360SJames Bottomley  */
143965c92b09SJames Bottomley struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
144042ab0360SJames Bottomley {
144142ab0360SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
144242ab0360SJames Bottomley 	struct sas_end_device *rdev;
144342ab0360SJames Bottomley 
144442ab0360SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
144542ab0360SJames Bottomley 	if (!rdev) {
144642ab0360SJames Bottomley 		return NULL;
144742ab0360SJames Bottomley 	}
144842ab0360SJames Bottomley 
144942ab0360SJames Bottomley 	device_initialize(&rdev->rphy.dev);
145042ab0360SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
14512f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_end_device_release;
145265c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
145365c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
145471610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
145571610f55SKay Sievers 			     shost->host_no, rphy->scsi_target_id,
145671610f55SKay Sievers 			     parent->port_identifier);
145765c92b09SJames Bottomley 	} else
145871610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
145965c92b09SJames Bottomley 			     shost->host_no, parent->port_identifier);
146042ab0360SJames Bottomley 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1461c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
146242ab0360SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
146342ab0360SJames Bottomley 
146442ab0360SJames Bottomley 	return &rdev->rphy;
146542ab0360SJames Bottomley }
146642ab0360SJames Bottomley EXPORT_SYMBOL(sas_end_device_alloc);
146742ab0360SJames Bottomley 
146879cb1819SJames Bottomley /**
146979cb1819SJames Bottomley  * sas_expander_alloc - allocate an rphy for an end device
1470eb44820cSRob Landley  * @parent: which port
1471eb44820cSRob Landley  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
147279cb1819SJames Bottomley  *
147379cb1819SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
147479cb1819SJames Bottomley  *
147579cb1819SJames Bottomley  * Returns:
147679cb1819SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
147779cb1819SJames Bottomley  */
147865c92b09SJames Bottomley struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
147979cb1819SJames Bottomley 				    enum sas_device_type type)
148079cb1819SJames Bottomley {
148179cb1819SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
148279cb1819SJames Bottomley 	struct sas_expander_device *rdev;
148379cb1819SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
148479cb1819SJames Bottomley 
148579cb1819SJames Bottomley 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
148679cb1819SJames Bottomley 	       type != SAS_FANOUT_EXPANDER_DEVICE);
148779cb1819SJames Bottomley 
148879cb1819SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
148979cb1819SJames Bottomley 	if (!rdev) {
149079cb1819SJames Bottomley 		return NULL;
149179cb1819SJames Bottomley 	}
149279cb1819SJames Bottomley 
149379cb1819SJames Bottomley 	device_initialize(&rdev->rphy.dev);
149479cb1819SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
14952f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_expander_release;
149679cb1819SJames Bottomley 	mutex_lock(&sas_host->lock);
149779cb1819SJames Bottomley 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
149879cb1819SJames Bottomley 	mutex_unlock(&sas_host->lock);
149971610f55SKay Sievers 	dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
150079cb1819SJames Bottomley 		     shost->host_no, rdev->rphy.scsi_target_id);
150179cb1819SJames Bottomley 	rdev->rphy.identify.device_type = type;
1502c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
150379cb1819SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
150479cb1819SJames Bottomley 
150579cb1819SJames Bottomley 	return &rdev->rphy;
150679cb1819SJames Bottomley }
150779cb1819SJames Bottomley EXPORT_SYMBOL(sas_expander_alloc);
150842ab0360SJames Bottomley 
150942ab0360SJames Bottomley /**
1510eb44820cSRob Landley  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1511c7ebbbceSChristoph Hellwig  * @rphy:	The remote PHY to be added
1512c7ebbbceSChristoph Hellwig  *
1513c7ebbbceSChristoph Hellwig  * Publishes a SAS remote PHY to the rest of the system.
1514c7ebbbceSChristoph Hellwig  */
1515c7ebbbceSChristoph Hellwig int sas_rphy_add(struct sas_rphy *rphy)
1516c7ebbbceSChristoph Hellwig {
151765c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1518c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1519c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1520c7ebbbceSChristoph Hellwig 	struct sas_identify *identify = &rphy->identify;
1521c7ebbbceSChristoph Hellwig 	int error;
1522c7ebbbceSChristoph Hellwig 
1523c7ebbbceSChristoph Hellwig 	if (parent->rphy)
1524c7ebbbceSChristoph Hellwig 		return -ENXIO;
1525c7ebbbceSChristoph Hellwig 	parent->rphy = rphy;
1526c7ebbbceSChristoph Hellwig 
1527c7ebbbceSChristoph Hellwig 	error = device_add(&rphy->dev);
1528c7ebbbceSChristoph Hellwig 	if (error)
1529c7ebbbceSChristoph Hellwig 		return error;
1530c7ebbbceSChristoph Hellwig 	transport_add_device(&rphy->dev);
1531c7ebbbceSChristoph Hellwig 	transport_configure_device(&rphy->dev);
153239dca558SJames Bottomley 	if (sas_bsg_initialize(shost, rphy))
153371610f55SKay Sievers 		printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
153439dca558SJames Bottomley 
1535c7ebbbceSChristoph Hellwig 
1536e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1537c7ebbbceSChristoph Hellwig 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1538c7ebbbceSChristoph Hellwig 	if (identify->device_type == SAS_END_DEVICE &&
1539c7ebbbceSChristoph Hellwig 	    (identify->target_port_protocols &
1540c7ebbbceSChristoph Hellwig 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1541c7ebbbceSChristoph Hellwig 		rphy->scsi_target_id = sas_host->next_target_id++;
15427676f83aSJames Bottomley 	else if (identify->device_type == SAS_END_DEVICE)
15437676f83aSJames Bottomley 		rphy->scsi_target_id = -1;
1544e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1545c7ebbbceSChristoph Hellwig 
154679cb1819SJames Bottomley 	if (identify->device_type == SAS_END_DEVICE &&
154779cb1819SJames Bottomley 	    rphy->scsi_target_id != -1) {
1548e8bf3941SJames Bottomley 		scsi_scan_target(&rphy->dev, 0,
1549c8490f3aSDarrick J. Wong 				rphy->scsi_target_id, SCAN_WILD_CARD, 0);
1550c7ebbbceSChristoph Hellwig 	}
1551c7ebbbceSChristoph Hellwig 
1552c7ebbbceSChristoph Hellwig 	return 0;
1553c7ebbbceSChristoph Hellwig }
1554c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_add);
1555c7ebbbceSChristoph Hellwig 
1556c7ebbbceSChristoph Hellwig /**
1557eb44820cSRob Landley  * sas_rphy_free  -  free a SAS remote PHY
1558eb44820cSRob Landley  * @rphy: SAS remote PHY to free
1559c7ebbbceSChristoph Hellwig  *
1560c7ebbbceSChristoph Hellwig  * Frees the specified SAS remote PHY.
1561c7ebbbceSChristoph Hellwig  *
1562c7ebbbceSChristoph Hellwig  * Note:
1563c7ebbbceSChristoph Hellwig  *   This function must only be called on a remote
1564af901ca1SAndré Goddard Rosa  *   PHY that has not successfully been added using
15656f63caaeSDarrick J. Wong  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1566c7ebbbceSChristoph Hellwig  */
1567c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *rphy)
1568c7ebbbceSChristoph Hellwig {
156992aab646SMike Anderson 	struct device *dev = &rphy->dev;
1570c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1571c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1572c7ebbbceSChristoph Hellwig 
1573e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1574c7ebbbceSChristoph Hellwig 	list_del(&rphy->list);
1575e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1576c7ebbbceSChristoph Hellwig 
1577b6aff669SJames Bottomley 	sas_bsg_remove(shost, rphy);
1578b6aff669SJames Bottomley 
157992aab646SMike Anderson 	transport_destroy_device(dev);
15802f8600dfSJames Bottomley 
158192aab646SMike Anderson 	put_device(dev);
1582c7ebbbceSChristoph Hellwig }
1583c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_free);
1584c7ebbbceSChristoph Hellwig 
1585c7ebbbceSChristoph Hellwig /**
1586eb44820cSRob Landley  * sas_rphy_delete  -  remove and free SAS remote PHY
15876f63caaeSDarrick J. Wong  * @rphy:	SAS remote PHY to remove and free
1588c7ebbbceSChristoph Hellwig  *
15896f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY and frees it.
1590c7ebbbceSChristoph Hellwig  */
1591c7ebbbceSChristoph Hellwig void
1592c7ebbbceSChristoph Hellwig sas_rphy_delete(struct sas_rphy *rphy)
1593c7ebbbceSChristoph Hellwig {
15946f63caaeSDarrick J. Wong 	sas_rphy_remove(rphy);
15956f63caaeSDarrick J. Wong 	sas_rphy_free(rphy);
15966f63caaeSDarrick J. Wong }
15976f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_delete);
15986f63caaeSDarrick J. Wong 
15996f63caaeSDarrick J. Wong /**
1600eb44820cSRob Landley  * sas_rphy_remove  -  remove SAS remote PHY
16016f63caaeSDarrick J. Wong  * @rphy:	SAS remote phy to remove
16026f63caaeSDarrick J. Wong  *
16036f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY.
16046f63caaeSDarrick J. Wong  */
16056f63caaeSDarrick J. Wong void
16066f63caaeSDarrick J. Wong sas_rphy_remove(struct sas_rphy *rphy)
16076f63caaeSDarrick J. Wong {
1608c7ebbbceSChristoph Hellwig 	struct device *dev = &rphy->dev;
160965c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(dev->parent);
1610c7ebbbceSChristoph Hellwig 
1611d4054239SChristoph Hellwig 	switch (rphy->identify.device_type) {
1612d4054239SChristoph Hellwig 	case SAS_END_DEVICE:
1613fe8b2304SChristoph Hellwig 		scsi_remove_target(dev);
1614d4054239SChristoph Hellwig 		break;
1615d4054239SChristoph Hellwig 	case SAS_EDGE_EXPANDER_DEVICE:
1616d4054239SChristoph Hellwig 	case SAS_FANOUT_EXPANDER_DEVICE:
161765c92b09SJames Bottomley 		sas_remove_children(dev);
1618d4054239SChristoph Hellwig 		break;
1619d4054239SChristoph Hellwig 	default:
1620d4054239SChristoph Hellwig 		break;
1621d4054239SChristoph Hellwig 	}
1622c7ebbbceSChristoph Hellwig 
1623fe8b2304SChristoph Hellwig 	transport_remove_device(dev);
1624fe8b2304SChristoph Hellwig 	device_del(dev);
1625c7ebbbceSChristoph Hellwig 
162633b114e9SChristoph Hellwig 	parent->rphy = NULL;
1627c7ebbbceSChristoph Hellwig }
16286f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_remove);
1629c7ebbbceSChristoph Hellwig 
1630c7ebbbceSChristoph Hellwig /**
1631eb44820cSRob Landley  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1632c7ebbbceSChristoph Hellwig  * @dev:	device to check
1633c7ebbbceSChristoph Hellwig  *
1634c7ebbbceSChristoph Hellwig  * Returns:
1635c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS remote PHY, %0 else
1636c7ebbbceSChristoph Hellwig  */
1637c7ebbbceSChristoph Hellwig int scsi_is_sas_rphy(const struct device *dev)
1638c7ebbbceSChristoph Hellwig {
16392f8600dfSJames Bottomley 	return dev->release == sas_end_device_release ||
16402f8600dfSJames Bottomley 		dev->release == sas_expander_release;
1641c7ebbbceSChristoph Hellwig }
1642c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_rphy);
1643c7ebbbceSChristoph Hellwig 
1644c7ebbbceSChristoph Hellwig 
1645c7ebbbceSChristoph Hellwig /*
1646c7ebbbceSChristoph Hellwig  * SCSI scan helper
1647c7ebbbceSChristoph Hellwig  */
1648c7ebbbceSChristoph Hellwig 
1649e02f3f59SChristoph Hellwig static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1650e02f3f59SChristoph Hellwig 		uint id, uint lun)
1651c7ebbbceSChristoph Hellwig {
1652c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1653c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy;
1654c7ebbbceSChristoph Hellwig 
1655e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1656c7ebbbceSChristoph Hellwig 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
16576d99a3f3SJames Bottomley 		if (rphy->identify.device_type != SAS_END_DEVICE ||
16586d99a3f3SJames Bottomley 		    rphy->scsi_target_id == -1)
1659e02f3f59SChristoph Hellwig 			continue;
1660e02f3f59SChristoph Hellwig 
1661e8bf3941SJames Bottomley 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1662e02f3f59SChristoph Hellwig 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1663e8bf3941SJames Bottomley 			scsi_scan_target(&rphy->dev, 0,
1664e02f3f59SChristoph Hellwig 					 rphy->scsi_target_id, lun, 1);
1665e02f3f59SChristoph Hellwig 		}
1666e02f3f59SChristoph Hellwig 	}
1667e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1668e02f3f59SChristoph Hellwig 
1669e02f3f59SChristoph Hellwig 	return 0;
1670c7ebbbceSChristoph Hellwig }
1671c7ebbbceSChristoph Hellwig 
1672c7ebbbceSChristoph Hellwig 
1673c7ebbbceSChristoph Hellwig /*
1674c7ebbbceSChristoph Hellwig  * Setup / Teardown code
1675c7ebbbceSChristoph Hellwig  */
1676c7ebbbceSChristoph Hellwig 
167742ab0360SJames Bottomley #define SETUP_TEMPLATE(attrb, field, perm, test)			\
1678ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
167942ab0360SJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
168042ab0360SJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
168142ab0360SJames Bottomley 	if (test)							\
1682c7ebbbceSChristoph Hellwig 		count++
1683c7ebbbceSChristoph Hellwig 
1684d24e1eebSJames Bottomley #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1685ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
1686d24e1eebSJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
1687d24e1eebSJames Bottomley 	if (ro_test) {							\
1688d24e1eebSJames Bottomley 		i->private_##attrb[count].attr.mode = ro_perm;		\
1689d24e1eebSJames Bottomley 		i->private_##attrb[count].store = NULL;			\
1690d24e1eebSJames Bottomley 	}								\
1691d24e1eebSJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
1692d24e1eebSJames Bottomley 	if (test)							\
1693d24e1eebSJames Bottomley 		count++
169442ab0360SJames Bottomley 
169542ab0360SJames Bottomley #define SETUP_RPORT_ATTRIBUTE(field) 					\
169642ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
169742ab0360SJames Bottomley 
1698dd9fbb52SJames Bottomley #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
169942ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1700dd9fbb52SJames Bottomley 
170165c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE(field)					\
170242ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1703c7ebbbceSChristoph Hellwig 
1704d24e1eebSJames Bottomley #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1705d24e1eebSJames Bottomley 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1706d24e1eebSJames Bottomley 			!i->f->set_phy_speed, S_IRUGO)
1707d24e1eebSJames Bottomley 
1708acbf167dSDarrick J. Wong #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1709acbf167dSDarrick J. Wong 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1710acbf167dSDarrick J. Wong 			  !i->f->func, S_IRUGO)
1711acbf167dSDarrick J. Wong 
171265c92b09SJames Bottomley #define SETUP_PORT_ATTRIBUTE(field)					\
171365c92b09SJames Bottomley 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
171465c92b09SJames Bottomley 
171565c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
171642ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1717dd9fbb52SJames Bottomley 
171865c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1719fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
172007ba3a95SChristoph Hellwig 
172165c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1722fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1723dd9fbb52SJames Bottomley 
172442ab0360SJames Bottomley #define SETUP_END_DEV_ATTRIBUTE(field)					\
172542ab0360SJames Bottomley 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1726c7ebbbceSChristoph Hellwig 
172779cb1819SJames Bottomley #define SETUP_EXPANDER_ATTRIBUTE(field)					\
172879cb1819SJames Bottomley 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
172979cb1819SJames Bottomley 
1730c7ebbbceSChristoph Hellwig /**
1731eb44820cSRob Landley  * sas_attach_transport  -  instantiate SAS transport template
1732c7ebbbceSChristoph Hellwig  * @ft:		SAS transport class function template
1733c7ebbbceSChristoph Hellwig  */
1734c7ebbbceSChristoph Hellwig struct scsi_transport_template *
1735c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *ft)
1736c7ebbbceSChristoph Hellwig {
1737c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1738c7ebbbceSChristoph Hellwig 	int count;
1739c7ebbbceSChristoph Hellwig 
174024669f75SJes Sorensen 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1741c7ebbbceSChristoph Hellwig 	if (!i)
1742c7ebbbceSChristoph Hellwig 		return NULL;
1743c7ebbbceSChristoph Hellwig 
1744e02f3f59SChristoph Hellwig 	i->t.user_scan = sas_user_scan;
1745c7ebbbceSChristoph Hellwig 
1746c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1747c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.class = &sas_host_class.class;
1748c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.match = sas_host_match;
1749c7ebbbceSChristoph Hellwig 	transport_container_register(&i->t.host_attrs);
1750c7ebbbceSChristoph Hellwig 	i->t.host_size = sizeof(struct sas_host_attrs);
1751c7ebbbceSChristoph Hellwig 
1752c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1753c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1754c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.match = sas_phy_match;
1755c7ebbbceSChristoph Hellwig 	transport_container_register(&i->phy_attr_cont);
1756c7ebbbceSChristoph Hellwig 
175765c92b09SJames Bottomley 	i->port_attr_cont.ac.class = &sas_port_class.class;
175865c92b09SJames Bottomley 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
175965c92b09SJames Bottomley 	i->port_attr_cont.ac.match = sas_port_match;
176065c92b09SJames Bottomley 	transport_container_register(&i->port_attr_cont);
176165c92b09SJames Bottomley 
1762c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1763c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1764c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1765c7ebbbceSChristoph Hellwig 	transport_container_register(&i->rphy_attr_cont);
1766c7ebbbceSChristoph Hellwig 
176742ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
176842ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
176942ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
177042ab0360SJames Bottomley 	transport_container_register(&i->end_dev_attr_cont);
177142ab0360SJames Bottomley 
177279cb1819SJames Bottomley 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
177379cb1819SJames Bottomley 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
177479cb1819SJames Bottomley 	i->expander_attr_cont.ac.match = sas_expander_match;
177579cb1819SJames Bottomley 	transport_container_register(&i->expander_attr_cont);
177679cb1819SJames Bottomley 
1777c7ebbbceSChristoph Hellwig 	i->f = ft;
1778c7ebbbceSChristoph Hellwig 
1779c7ebbbceSChristoph Hellwig 	count = 0;
178065c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
178165c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
178265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(device_type);
178365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(sas_address);
178465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_identifier);
178565c92b09SJames Bottomley 	//SETUP_PHY_ATTRIBUTE(port_identifier);
178665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
178765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1788d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
178965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1790d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1791c3ee74c4SChristoph Hellwig 
179265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
179365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
179465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
179565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
179665c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
179765c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1798acbf167dSDarrick J. Wong 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1799c7ebbbceSChristoph Hellwig 	i->phy_attrs[count] = NULL;
1800c7ebbbceSChristoph Hellwig 
1801c7ebbbceSChristoph Hellwig 	count = 0;
180265c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
180365c92b09SJames Bottomley 	i->port_attrs[count] = NULL;
180465c92b09SJames Bottomley 
180565c92b09SJames Bottomley 	count = 0;
1806c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1807c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1808c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1809c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1810c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1811dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1812dd9fbb52SJames Bottomley 				       get_enclosure_identifier);
1813dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1814dd9fbb52SJames Bottomley 				       get_bay_identifier);
1815c7ebbbceSChristoph Hellwig 	i->rphy_attrs[count] = NULL;
1816c7ebbbceSChristoph Hellwig 
181742ab0360SJames Bottomley 	count = 0;
181842ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
181942ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
182042ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
18210f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
18220f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
182342ab0360SJames Bottomley 	i->end_dev_attrs[count] = NULL;
182442ab0360SJames Bottomley 
182579cb1819SJames Bottomley 	count = 0;
182679cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
182779cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_id);
182879cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
182979cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
183079cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_id);
183179cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
183279cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(level);
183379cb1819SJames Bottomley 	i->expander_attrs[count] = NULL;
183479cb1819SJames Bottomley 
1835c7ebbbceSChristoph Hellwig 	return &i->t;
1836c7ebbbceSChristoph Hellwig }
1837c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_attach_transport);
1838c7ebbbceSChristoph Hellwig 
1839c7ebbbceSChristoph Hellwig /**
1840eb44820cSRob Landley  * sas_release_transport  -  release SAS transport template instance
1841c7ebbbceSChristoph Hellwig  * @t:		transport template instance
1842c7ebbbceSChristoph Hellwig  */
1843c7ebbbceSChristoph Hellwig void sas_release_transport(struct scsi_transport_template *t)
1844c7ebbbceSChristoph Hellwig {
1845c7ebbbceSChristoph Hellwig 	struct sas_internal *i = to_sas_internal(t);
1846c7ebbbceSChristoph Hellwig 
1847c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->t.host_attrs);
1848c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->phy_attr_cont);
184965c92b09SJames Bottomley 	transport_container_unregister(&i->port_attr_cont);
1850c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->rphy_attr_cont);
1851db82f841SJames Bottomley 	transport_container_unregister(&i->end_dev_attr_cont);
185279cb1819SJames Bottomley 	transport_container_unregister(&i->expander_attr_cont);
1853c7ebbbceSChristoph Hellwig 
1854c7ebbbceSChristoph Hellwig 	kfree(i);
1855c7ebbbceSChristoph Hellwig }
1856c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_release_transport);
1857c7ebbbceSChristoph Hellwig 
1858c7ebbbceSChristoph Hellwig static __init int sas_transport_init(void)
1859c7ebbbceSChristoph Hellwig {
1860c7ebbbceSChristoph Hellwig 	int error;
1861c7ebbbceSChristoph Hellwig 
1862c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_host_class);
1863c7ebbbceSChristoph Hellwig 	if (error)
1864c7ebbbceSChristoph Hellwig 		goto out;
1865c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_phy_class);
1866c7ebbbceSChristoph Hellwig 	if (error)
1867c7ebbbceSChristoph Hellwig 		goto out_unregister_transport;
186865c92b09SJames Bottomley 	error = transport_class_register(&sas_port_class);
1869c7ebbbceSChristoph Hellwig 	if (error)
1870c7ebbbceSChristoph Hellwig 		goto out_unregister_phy;
187165c92b09SJames Bottomley 	error = transport_class_register(&sas_rphy_class);
187265c92b09SJames Bottomley 	if (error)
187365c92b09SJames Bottomley 		goto out_unregister_port;
187442ab0360SJames Bottomley 	error = transport_class_register(&sas_end_dev_class);
187542ab0360SJames Bottomley 	if (error)
187642ab0360SJames Bottomley 		goto out_unregister_rphy;
187779cb1819SJames Bottomley 	error = transport_class_register(&sas_expander_class);
187879cb1819SJames Bottomley 	if (error)
187979cb1819SJames Bottomley 		goto out_unregister_end_dev;
1880c7ebbbceSChristoph Hellwig 
1881c7ebbbceSChristoph Hellwig 	return 0;
1882c7ebbbceSChristoph Hellwig 
188379cb1819SJames Bottomley  out_unregister_end_dev:
188479cb1819SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
188542ab0360SJames Bottomley  out_unregister_rphy:
188642ab0360SJames Bottomley 	transport_class_unregister(&sas_rphy_class);
188765c92b09SJames Bottomley  out_unregister_port:
188865c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1889c7ebbbceSChristoph Hellwig  out_unregister_phy:
1890c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
1891c7ebbbceSChristoph Hellwig  out_unregister_transport:
1892c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1893c7ebbbceSChristoph Hellwig  out:
1894c7ebbbceSChristoph Hellwig 	return error;
1895c7ebbbceSChristoph Hellwig 
1896c7ebbbceSChristoph Hellwig }
1897c7ebbbceSChristoph Hellwig 
1898c7ebbbceSChristoph Hellwig static void __exit sas_transport_exit(void)
1899c7ebbbceSChristoph Hellwig {
1900c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1901c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
190265c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1903c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_rphy_class);
190442ab0360SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
190579cb1819SJames Bottomley 	transport_class_unregister(&sas_expander_class);
1906c7ebbbceSChristoph Hellwig }
1907c7ebbbceSChristoph Hellwig 
1908c7ebbbceSChristoph Hellwig MODULE_AUTHOR("Christoph Hellwig");
190986b9c4c1SAlexis Bruemmer MODULE_DESCRIPTION("SAS Transport Attributes");
1910c7ebbbceSChristoph Hellwig MODULE_LICENSE("GPL");
1911c7ebbbceSChristoph Hellwig 
1912c7ebbbceSChristoph Hellwig module_init(sas_transport_init);
1913c7ebbbceSChristoph Hellwig module_exit(sas_transport_exit);
1914