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 
1587aa68e80SFUJITA Tomonori static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
1597aa68e80SFUJITA Tomonori 			    struct sas_rphy *rphy)
1607aa68e80SFUJITA Tomonori {
1617aa68e80SFUJITA Tomonori 	struct request *req;
1627aa68e80SFUJITA Tomonori 	int ret;
1637aa68e80SFUJITA Tomonori 	int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
1647aa68e80SFUJITA Tomonori 
1657aa68e80SFUJITA Tomonori 	while (!blk_queue_plugged(q)) {
1669934c8c0STejun Heo 		req = blk_fetch_request(q);
1677aa68e80SFUJITA Tomonori 		if (!req)
1687aa68e80SFUJITA Tomonori 			break;
1697aa68e80SFUJITA Tomonori 
1707aa68e80SFUJITA Tomonori 		spin_unlock_irq(q->queue_lock);
1717aa68e80SFUJITA Tomonori 
1727aa68e80SFUJITA Tomonori 		handler = to_sas_internal(shost->transportt)->f->smp_handler;
1737aa68e80SFUJITA Tomonori 		ret = handler(shost, rphy, req);
1742d507a01SJames Bottomley 		req->errors = ret;
1757aa68e80SFUJITA Tomonori 
17693bdcba5SFUJITA Tomonori 		blk_end_request_all(req, ret);
1777aa68e80SFUJITA Tomonori 
17893bdcba5SFUJITA Tomonori 		spin_lock_irq(q->queue_lock);
1797aa68e80SFUJITA Tomonori 	}
1807aa68e80SFUJITA Tomonori }
1817aa68e80SFUJITA Tomonori 
1827aa68e80SFUJITA Tomonori static void sas_host_smp_request(struct request_queue *q)
1837aa68e80SFUJITA Tomonori {
1847aa68e80SFUJITA Tomonori 	sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
1857aa68e80SFUJITA Tomonori }
1867aa68e80SFUJITA Tomonori 
1877aa68e80SFUJITA Tomonori static void sas_non_host_smp_request(struct request_queue *q)
1887aa68e80SFUJITA Tomonori {
1897aa68e80SFUJITA Tomonori 	struct sas_rphy *rphy = q->queuedata;
1907aa68e80SFUJITA Tomonori 	sas_smp_request(q, rphy_to_shost(rphy), rphy);
1917aa68e80SFUJITA Tomonori }
1927aa68e80SFUJITA Tomonori 
19393c20a59SFUJITA Tomonori static void sas_host_release(struct device *dev)
19493c20a59SFUJITA Tomonori {
19593c20a59SFUJITA Tomonori 	struct Scsi_Host *shost = dev_to_shost(dev);
19693c20a59SFUJITA Tomonori 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
19793c20a59SFUJITA Tomonori 	struct request_queue *q = sas_host->q;
19893c20a59SFUJITA Tomonori 
19993c20a59SFUJITA Tomonori 	if (q)
20093c20a59SFUJITA Tomonori 		blk_cleanup_queue(q);
20193c20a59SFUJITA Tomonori }
20293c20a59SFUJITA Tomonori 
20339dca558SJames Bottomley static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
2047aa68e80SFUJITA Tomonori {
2057aa68e80SFUJITA Tomonori 	struct request_queue *q;
2067aa68e80SFUJITA Tomonori 	int error;
20739dca558SJames Bottomley 	struct device *dev;
20871610f55SKay Sievers 	char namebuf[20];
20939dca558SJames Bottomley 	const char *name;
21093c20a59SFUJITA Tomonori 	void (*release)(struct device *);
2117aa68e80SFUJITA Tomonori 
2127aa68e80SFUJITA Tomonori 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
2137aa68e80SFUJITA Tomonori 		printk("%s can't handle SMP requests\n", shost->hostt->name);
2147aa68e80SFUJITA Tomonori 		return 0;
2157aa68e80SFUJITA Tomonori 	}
2167aa68e80SFUJITA Tomonori 
21739dca558SJames Bottomley 	if (rphy) {
2187aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_non_host_smp_request, NULL);
21939dca558SJames Bottomley 		dev = &rphy->dev;
22071610f55SKay Sievers 		name = dev_name(dev);
22193c20a59SFUJITA Tomonori 		release = NULL;
22239dca558SJames Bottomley 	} else {
2237aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_host_smp_request, NULL);
22439dca558SJames Bottomley 		dev = &shost->shost_gendev;
22539dca558SJames Bottomley 		snprintf(namebuf, sizeof(namebuf),
22639dca558SJames Bottomley 			 "sas_host%d", shost->host_no);
22739dca558SJames Bottomley 		name = namebuf;
22893c20a59SFUJITA Tomonori 		release = sas_host_release;
22939dca558SJames Bottomley 	}
2307aa68e80SFUJITA Tomonori 	if (!q)
2317aa68e80SFUJITA Tomonori 		return -ENOMEM;
2327aa68e80SFUJITA Tomonori 
23393c20a59SFUJITA Tomonori 	error = bsg_register_queue(q, dev, name, release);
2347aa68e80SFUJITA Tomonori 	if (error) {
2357aa68e80SFUJITA Tomonori 		blk_cleanup_queue(q);
2367aa68e80SFUJITA Tomonori 		return -ENOMEM;
2377aa68e80SFUJITA Tomonori 	}
2387aa68e80SFUJITA Tomonori 
2397aa68e80SFUJITA Tomonori 	if (rphy)
240b6aff669SJames Bottomley 		rphy->q = q;
241b6aff669SJames Bottomley 	else
242b6aff669SJames Bottomley 		to_sas_host_attrs(shost)->q = q;
243b6aff669SJames Bottomley 
244b6aff669SJames Bottomley 	if (rphy)
2457aa68e80SFUJITA Tomonori 		q->queuedata = rphy;
2467aa68e80SFUJITA Tomonori 	else
2477aa68e80SFUJITA Tomonori 		q->queuedata = shost;
2487aa68e80SFUJITA Tomonori 
24975ad23bcSNick Piggin 	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
2507aa68e80SFUJITA Tomonori 	return 0;
2517aa68e80SFUJITA Tomonori }
2527aa68e80SFUJITA Tomonori 
253b6aff669SJames Bottomley static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
254b6aff669SJames Bottomley {
255b6aff669SJames Bottomley 	struct request_queue *q;
256b6aff669SJames Bottomley 
257b6aff669SJames Bottomley 	if (rphy)
258b6aff669SJames Bottomley 		q = rphy->q;
259b6aff669SJames Bottomley 	else
260b6aff669SJames Bottomley 		q = to_sas_host_attrs(shost)->q;
261b6aff669SJames Bottomley 
262b6aff669SJames Bottomley 	if (!q)
263b6aff669SJames Bottomley 		return;
264b6aff669SJames Bottomley 
265b6aff669SJames Bottomley 	bsg_unregister_queue(q);
266b6aff669SJames Bottomley }
267b6aff669SJames Bottomley 
268c7ebbbceSChristoph Hellwig /*
269c7ebbbceSChristoph Hellwig  * SAS host attributes
270c7ebbbceSChristoph Hellwig  */
271c7ebbbceSChristoph Hellwig 
27237be6eebSJames Bottomley static int sas_host_setup(struct transport_container *tc, struct device *dev,
273ee959b00STony Jones 			  struct device *cdev)
274c7ebbbceSChristoph Hellwig {
275c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(dev);
276c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
277c7ebbbceSChristoph Hellwig 
278c7ebbbceSChristoph Hellwig 	INIT_LIST_HEAD(&sas_host->rphy_list);
279e02f3f59SChristoph Hellwig 	mutex_init(&sas_host->lock);
280c7ebbbceSChristoph Hellwig 	sas_host->next_target_id = 0;
28179cb1819SJames Bottomley 	sas_host->next_expander_id = 0;
282c9fefeb2SJames Bottomley 	sas_host->next_port_id = 0;
2837aa68e80SFUJITA Tomonori 
28439dca558SJames Bottomley 	if (sas_bsg_initialize(shost, NULL))
2857aa68e80SFUJITA Tomonori 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
2867aa68e80SFUJITA Tomonori 			   shost->host_no);
2877aa68e80SFUJITA Tomonori 
288c7ebbbceSChristoph Hellwig 	return 0;
289c7ebbbceSChristoph Hellwig }
290c7ebbbceSChristoph Hellwig 
291b6aff669SJames Bottomley static int sas_host_remove(struct transport_container *tc, struct device *dev,
292ee959b00STony Jones 			   struct device *cdev)
293b6aff669SJames Bottomley {
294b6aff669SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev);
295b6aff669SJames Bottomley 
296b6aff669SJames Bottomley 	sas_bsg_remove(shost, NULL);
297b6aff669SJames Bottomley 
298b6aff669SJames Bottomley 	return 0;
299b6aff669SJames Bottomley }
300b6aff669SJames Bottomley 
301c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_host_class,
302b6aff669SJames Bottomley 		"sas_host", sas_host_setup, sas_host_remove, NULL);
303c7ebbbceSChristoph Hellwig 
304c7ebbbceSChristoph Hellwig static int sas_host_match(struct attribute_container *cont,
305c7ebbbceSChristoph Hellwig 			    struct device *dev)
306c7ebbbceSChristoph Hellwig {
307c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
308c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
309c7ebbbceSChristoph Hellwig 
310c7ebbbceSChristoph Hellwig 	if (!scsi_is_host_device(dev))
311c7ebbbceSChristoph Hellwig 		return 0;
312c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev);
313c7ebbbceSChristoph Hellwig 
314c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
315c7ebbbceSChristoph Hellwig 		return 0;
316c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
317c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
318c7ebbbceSChristoph Hellwig 		return 0;
319c7ebbbceSChristoph Hellwig 
320c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
321c7ebbbceSChristoph Hellwig 	return &i->t.host_attrs.ac == cont;
322c7ebbbceSChristoph Hellwig }
323c7ebbbceSChristoph Hellwig 
324c7ebbbceSChristoph Hellwig static int do_sas_phy_delete(struct device *dev, void *data)
325c7ebbbceSChristoph Hellwig {
32665c92b09SJames Bottomley 	int pass = (int)(unsigned long)data;
32765c92b09SJames Bottomley 
32865c92b09SJames Bottomley 	if (pass == 0 && scsi_is_sas_port(dev))
32965c92b09SJames Bottomley 		sas_port_delete(dev_to_sas_port(dev));
33065c92b09SJames Bottomley 	else if (pass == 1 && scsi_is_sas_phy(dev))
331c7ebbbceSChristoph Hellwig 		sas_phy_delete(dev_to_phy(dev));
332c7ebbbceSChristoph Hellwig 	return 0;
333c7ebbbceSChristoph Hellwig }
334c7ebbbceSChristoph Hellwig 
335c7ebbbceSChristoph Hellwig /**
336eb44820cSRob Landley  * sas_remove_children  -  tear down a devices SAS data structures
33765c92b09SJames Bottomley  * @dev:	device belonging to the sas object
33865c92b09SJames Bottomley  *
33965c92b09SJames Bottomley  * Removes all SAS PHYs and remote PHYs for a given object
34065c92b09SJames Bottomley  */
34165c92b09SJames Bottomley void sas_remove_children(struct device *dev)
34265c92b09SJames Bottomley {
34365c92b09SJames Bottomley 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
34465c92b09SJames Bottomley 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
34565c92b09SJames Bottomley }
34665c92b09SJames Bottomley EXPORT_SYMBOL(sas_remove_children);
34765c92b09SJames Bottomley 
34865c92b09SJames Bottomley /**
349eb44820cSRob Landley  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
350c7ebbbceSChristoph Hellwig  * @shost:	Scsi Host that is torn down
351c7ebbbceSChristoph Hellwig  *
352c7ebbbceSChristoph Hellwig  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
353c7ebbbceSChristoph Hellwig  * Must be called just before scsi_remove_host for SAS HBAs.
354c7ebbbceSChristoph Hellwig  */
355c7ebbbceSChristoph Hellwig void sas_remove_host(struct Scsi_Host *shost)
356c7ebbbceSChristoph Hellwig {
35765c92b09SJames Bottomley 	sas_remove_children(&shost->shost_gendev);
358c7ebbbceSChristoph Hellwig }
359c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_remove_host);
360c7ebbbceSChristoph Hellwig 
361c7ebbbceSChristoph Hellwig 
362c7ebbbceSChristoph Hellwig /*
36365c92b09SJames Bottomley  * SAS Phy attributes
364c7ebbbceSChristoph Hellwig  */
365c7ebbbceSChristoph Hellwig 
366c7ebbbceSChristoph Hellwig #define sas_phy_show_simple(field, name, format_string, cast)		\
367c7ebbbceSChristoph Hellwig static ssize_t								\
368ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
369ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
370c7ebbbceSChristoph Hellwig {									\
371ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
372c7ebbbceSChristoph Hellwig 									\
373c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast phy->field);	\
374c7ebbbceSChristoph Hellwig }
375c7ebbbceSChristoph Hellwig 
376c7ebbbceSChristoph Hellwig #define sas_phy_simple_attr(field, name, format_string, type)		\
377c7ebbbceSChristoph Hellwig 	sas_phy_show_simple(field, name, format_string, (type))	\
378ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
379c7ebbbceSChristoph Hellwig 
380c7ebbbceSChristoph Hellwig #define sas_phy_show_protocol(field, name)				\
381c7ebbbceSChristoph Hellwig static ssize_t								\
382ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
383ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
384c7ebbbceSChristoph Hellwig {									\
385ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
386c7ebbbceSChristoph Hellwig 									\
387c7ebbbceSChristoph Hellwig 	if (!phy->field)						\
388c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
389c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(phy->field, buf);		\
390c7ebbbceSChristoph Hellwig }
391c7ebbbceSChristoph Hellwig 
392c7ebbbceSChristoph Hellwig #define sas_phy_protocol_attr(field, name)				\
393c7ebbbceSChristoph Hellwig 	sas_phy_show_protocol(field, name)				\
394ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
395c7ebbbceSChristoph Hellwig 
396c7ebbbceSChristoph Hellwig #define sas_phy_show_linkspeed(field)					\
397c7ebbbceSChristoph Hellwig static ssize_t								\
398ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
399ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
400c7ebbbceSChristoph Hellwig {									\
401ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
402c7ebbbceSChristoph Hellwig 									\
403c7ebbbceSChristoph Hellwig 	return get_sas_linkspeed_names(phy->field, buf);		\
404c7ebbbceSChristoph Hellwig }
405c7ebbbceSChristoph Hellwig 
406d24e1eebSJames Bottomley /* Fudge to tell if we're minimum or maximum */
407d24e1eebSJames Bottomley #define sas_phy_store_linkspeed(field)					\
408d24e1eebSJames Bottomley static ssize_t								\
409ee959b00STony Jones store_sas_phy_##field(struct device *dev, 				\
410ee959b00STony Jones 		      struct device_attribute *attr, 			\
411ee959b00STony Jones 		      const char *buf,	size_t count)			\
412d24e1eebSJames Bottomley {									\
413ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
414d24e1eebSJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
415d24e1eebSJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
416d24e1eebSJames Bottomley 	u32 value;							\
417d24e1eebSJames Bottomley 	struct sas_phy_linkrates rates = {0};				\
418d24e1eebSJames Bottomley 	int error;							\
419d24e1eebSJames Bottomley 									\
420d24e1eebSJames Bottomley 	error = set_sas_linkspeed_names(&value, buf);			\
421d24e1eebSJames Bottomley 	if (error)							\
422d24e1eebSJames Bottomley 		return error;						\
423d24e1eebSJames Bottomley 	rates.field = value;						\
424d24e1eebSJames Bottomley 	error = i->f->set_phy_speed(phy, &rates);			\
425d24e1eebSJames Bottomley 									\
426d24e1eebSJames Bottomley 	return error ? error : count;					\
427d24e1eebSJames Bottomley }
428d24e1eebSJames Bottomley 
429d24e1eebSJames Bottomley #define sas_phy_linkspeed_rw_attr(field)				\
430d24e1eebSJames Bottomley 	sas_phy_show_linkspeed(field)					\
431d24e1eebSJames Bottomley 	sas_phy_store_linkspeed(field)					\
432ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
433d24e1eebSJames Bottomley 	store_sas_phy_##field)
434d24e1eebSJames Bottomley 
435c7ebbbceSChristoph Hellwig #define sas_phy_linkspeed_attr(field)					\
436c7ebbbceSChristoph Hellwig 	sas_phy_show_linkspeed(field)					\
437ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
438c7ebbbceSChristoph Hellwig 
439d24e1eebSJames Bottomley 
440c3ee74c4SChristoph Hellwig #define sas_phy_show_linkerror(field)					\
441c3ee74c4SChristoph Hellwig static ssize_t								\
442ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
443ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
444c3ee74c4SChristoph Hellwig {									\
445ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
446c3ee74c4SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
447c3ee74c4SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
448c3ee74c4SChristoph Hellwig 	int error;							\
449c3ee74c4SChristoph Hellwig 									\
450dd9fbb52SJames Bottomley 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
451c3ee74c4SChristoph Hellwig 	if (error)							\
452c3ee74c4SChristoph Hellwig 		return error;						\
453c3ee74c4SChristoph Hellwig 	return snprintf(buf, 20, "%u\n", phy->field);			\
454c3ee74c4SChristoph Hellwig }
455c3ee74c4SChristoph Hellwig 
456c3ee74c4SChristoph Hellwig #define sas_phy_linkerror_attr(field)					\
457c3ee74c4SChristoph Hellwig 	sas_phy_show_linkerror(field)					\
458ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
459c3ee74c4SChristoph Hellwig 
460c3ee74c4SChristoph Hellwig 
461c7ebbbceSChristoph Hellwig static ssize_t
462ee959b00STony Jones show_sas_device_type(struct device *dev,
463ee959b00STony Jones 		     struct device_attribute *attr, char *buf)
464c7ebbbceSChristoph Hellwig {
465ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
466c7ebbbceSChristoph Hellwig 
467c7ebbbceSChristoph Hellwig 	if (!phy->identify.device_type)
468c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
469c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(phy->identify.device_type, buf);
470c7ebbbceSChristoph Hellwig }
471ee959b00STony Jones static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
472c7ebbbceSChristoph Hellwig 
473ee959b00STony Jones static ssize_t do_sas_phy_enable(struct device *dev,
474acbf167dSDarrick J. Wong 		size_t count, int enable)
475acbf167dSDarrick J. Wong {
476ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
477acbf167dSDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
478acbf167dSDarrick J. Wong 	struct sas_internal *i = to_sas_internal(shost->transportt);
479acbf167dSDarrick J. Wong 	int error;
480acbf167dSDarrick J. Wong 
481acbf167dSDarrick J. Wong 	error = i->f->phy_enable(phy, enable);
482acbf167dSDarrick J. Wong 	if (error)
483acbf167dSDarrick J. Wong 		return error;
484acbf167dSDarrick J. Wong 	phy->enabled = enable;
485acbf167dSDarrick J. Wong 	return count;
486acbf167dSDarrick J. Wong };
487acbf167dSDarrick J. Wong 
488ee959b00STony Jones static ssize_t
489ee959b00STony Jones store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
490acbf167dSDarrick J. Wong 		     const char *buf, size_t count)
491acbf167dSDarrick J. Wong {
492acbf167dSDarrick J. Wong 	if (count < 1)
493acbf167dSDarrick J. Wong 		return -EINVAL;
494acbf167dSDarrick J. Wong 
495acbf167dSDarrick J. Wong 	switch (buf[0]) {
496acbf167dSDarrick J. Wong 	case '0':
497ee959b00STony Jones 		do_sas_phy_enable(dev, count, 0);
498acbf167dSDarrick J. Wong 		break;
499acbf167dSDarrick J. Wong 	case '1':
500ee959b00STony Jones 		do_sas_phy_enable(dev, count, 1);
501acbf167dSDarrick J. Wong 		break;
502acbf167dSDarrick J. Wong 	default:
503acbf167dSDarrick J. Wong 		return -EINVAL;
504acbf167dSDarrick J. Wong 	}
505acbf167dSDarrick J. Wong 
506acbf167dSDarrick J. Wong 	return count;
507acbf167dSDarrick J. Wong }
508acbf167dSDarrick J. Wong 
509ee959b00STony Jones static ssize_t
510ee959b00STony Jones show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
511ee959b00STony Jones 		    char *buf)
512acbf167dSDarrick J. Wong {
513ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
514acbf167dSDarrick J. Wong 
515acbf167dSDarrick J. Wong 	return snprintf(buf, 20, "%d", phy->enabled);
516acbf167dSDarrick J. Wong }
517acbf167dSDarrick J. Wong 
518ee959b00STony Jones static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
519acbf167dSDarrick J. Wong 			 store_sas_phy_enable);
520acbf167dSDarrick J. Wong 
521ee959b00STony Jones static ssize_t
522ee959b00STony Jones do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
52307ba3a95SChristoph Hellwig {
524ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
52507ba3a95SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
52607ba3a95SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
52707ba3a95SChristoph Hellwig 	int error;
52807ba3a95SChristoph Hellwig 
52907ba3a95SChristoph Hellwig 	error = i->f->phy_reset(phy, hard_reset);
53007ba3a95SChristoph Hellwig 	if (error)
53107ba3a95SChristoph Hellwig 		return error;
53207ba3a95SChristoph Hellwig 	return count;
53307ba3a95SChristoph Hellwig };
53407ba3a95SChristoph Hellwig 
535ee959b00STony Jones static ssize_t
536ee959b00STony Jones store_sas_link_reset(struct device *dev, struct device_attribute *attr,
53707ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
53807ba3a95SChristoph Hellwig {
539ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 0);
54007ba3a95SChristoph Hellwig }
541ee959b00STony Jones static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
54207ba3a95SChristoph Hellwig 
543ee959b00STony Jones static ssize_t
544ee959b00STony Jones store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
54507ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
54607ba3a95SChristoph Hellwig {
547ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 1);
54807ba3a95SChristoph Hellwig }
549ee959b00STony Jones static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
55007ba3a95SChristoph Hellwig 
551c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.initiator_port_protocols,
552c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
553c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.target_port_protocols,
554c7ebbbceSChristoph Hellwig 		target_port_protocols);
555c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
556c7ebbbceSChristoph Hellwig 		unsigned long long);
557c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
558c9fefeb2SJames Bottomley //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
559c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(negotiated_linkrate);
560c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(minimum_linkrate_hw);
561d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(minimum_linkrate);
562c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(maximum_linkrate_hw);
563d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(maximum_linkrate);
564c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(invalid_dword_count);
565c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(running_disparity_error_count);
566c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(loss_of_dword_sync_count);
567c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(phy_reset_problem_count);
568c7ebbbceSChristoph Hellwig 
569c7ebbbceSChristoph Hellwig 
570c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_phy_class,
571c7ebbbceSChristoph Hellwig 		"sas_phy", NULL, NULL, NULL);
572c7ebbbceSChristoph Hellwig 
573c7ebbbceSChristoph Hellwig static int sas_phy_match(struct attribute_container *cont, struct device *dev)
574c7ebbbceSChristoph Hellwig {
575c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
576c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
577c7ebbbceSChristoph Hellwig 
578c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_phy(dev))
579c7ebbbceSChristoph Hellwig 		return 0;
580c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent);
581c7ebbbceSChristoph Hellwig 
582c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
583c7ebbbceSChristoph Hellwig 		return 0;
584c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
585c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
586c7ebbbceSChristoph Hellwig 		return 0;
587c7ebbbceSChristoph Hellwig 
588c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
589c7ebbbceSChristoph Hellwig 	return &i->phy_attr_cont.ac == cont;
590c7ebbbceSChristoph Hellwig }
591c7ebbbceSChristoph Hellwig 
592c7ebbbceSChristoph Hellwig static void sas_phy_release(struct device *dev)
593c7ebbbceSChristoph Hellwig {
594c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(dev);
595c7ebbbceSChristoph Hellwig 
596c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
597c7ebbbceSChristoph Hellwig 	kfree(phy);
598c7ebbbceSChristoph Hellwig }
599c7ebbbceSChristoph Hellwig 
600c7ebbbceSChristoph Hellwig /**
601eb44820cSRob Landley  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
602c7ebbbceSChristoph Hellwig  * @parent:	Parent device
603d99ca418SMoore, Eric  * @number:	Phy index
604c7ebbbceSChristoph Hellwig  *
605c7ebbbceSChristoph Hellwig  * Allocates an SAS PHY structure.  It will be added in the device tree
606c7ebbbceSChristoph Hellwig  * below the device specified by @parent, which has to be either a Scsi_Host
607c7ebbbceSChristoph Hellwig  * or sas_rphy.
608c7ebbbceSChristoph Hellwig  *
609c7ebbbceSChristoph Hellwig  * Returns:
610c7ebbbceSChristoph Hellwig  *	SAS PHY allocated or %NULL if the allocation failed.
611c7ebbbceSChristoph Hellwig  */
612c7ebbbceSChristoph Hellwig struct sas_phy *sas_phy_alloc(struct device *parent, int number)
613c7ebbbceSChristoph Hellwig {
614c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent);
615c7ebbbceSChristoph Hellwig 	struct sas_phy *phy;
616c7ebbbceSChristoph Hellwig 
61724669f75SJes Sorensen 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
618c7ebbbceSChristoph Hellwig 	if (!phy)
619c7ebbbceSChristoph Hellwig 		return NULL;
620c7ebbbceSChristoph Hellwig 
621c7ebbbceSChristoph Hellwig 	phy->number = number;
622acbf167dSDarrick J. Wong 	phy->enabled = 1;
623c7ebbbceSChristoph Hellwig 
624c7ebbbceSChristoph Hellwig 	device_initialize(&phy->dev);
625c7ebbbceSChristoph Hellwig 	phy->dev.parent = get_device(parent);
626c7ebbbceSChristoph Hellwig 	phy->dev.release = sas_phy_release;
62765c92b09SJames Bottomley 	INIT_LIST_HEAD(&phy->port_siblings);
62879cb1819SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
62979cb1819SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
63071610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
63179cb1819SJames Bottomley 			rphy->scsi_target_id, number);
63279cb1819SJames Bottomley 	} else
63371610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
634c7ebbbceSChristoph Hellwig 
635c7ebbbceSChristoph Hellwig 	transport_setup_device(&phy->dev);
636c7ebbbceSChristoph Hellwig 
637c7ebbbceSChristoph Hellwig 	return phy;
638c7ebbbceSChristoph Hellwig }
639c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_alloc);
640c7ebbbceSChristoph Hellwig 
641c7ebbbceSChristoph Hellwig /**
642eb44820cSRob Landley  * sas_phy_add  -  add a SAS PHY to the device hierarchy
643c7ebbbceSChristoph Hellwig  * @phy:	The PHY to be added
644c7ebbbceSChristoph Hellwig  *
645c7ebbbceSChristoph Hellwig  * Publishes a SAS PHY to the rest of the system.
646c7ebbbceSChristoph Hellwig  */
647c7ebbbceSChristoph Hellwig int sas_phy_add(struct sas_phy *phy)
648c7ebbbceSChristoph Hellwig {
649c7ebbbceSChristoph Hellwig 	int error;
650c7ebbbceSChristoph Hellwig 
651c7ebbbceSChristoph Hellwig 	error = device_add(&phy->dev);
652c7ebbbceSChristoph Hellwig 	if (!error) {
653c7ebbbceSChristoph Hellwig 		transport_add_device(&phy->dev);
654c7ebbbceSChristoph Hellwig 		transport_configure_device(&phy->dev);
655c7ebbbceSChristoph Hellwig 	}
656c7ebbbceSChristoph Hellwig 
657c7ebbbceSChristoph Hellwig 	return error;
658c7ebbbceSChristoph Hellwig }
659c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_add);
660c7ebbbceSChristoph Hellwig 
661c7ebbbceSChristoph Hellwig /**
662eb44820cSRob Landley  * sas_phy_free  -  free a SAS PHY
663c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to free
664c7ebbbceSChristoph Hellwig  *
665c7ebbbceSChristoph Hellwig  * Frees the specified SAS PHY.
666c7ebbbceSChristoph Hellwig  *
667c7ebbbceSChristoph Hellwig  * Note:
668c7ebbbceSChristoph Hellwig  *   This function must only be called on a PHY that has not
669af901ca1SAndré Goddard Rosa  *   successfully been added using sas_phy_add().
670c7ebbbceSChristoph Hellwig  */
671c7ebbbceSChristoph Hellwig void sas_phy_free(struct sas_phy *phy)
672c7ebbbceSChristoph Hellwig {
673c7ebbbceSChristoph Hellwig 	transport_destroy_device(&phy->dev);
67492aab646SMike Anderson 	put_device(&phy->dev);
675c7ebbbceSChristoph Hellwig }
676c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_free);
677c7ebbbceSChristoph Hellwig 
678c7ebbbceSChristoph Hellwig /**
679eb44820cSRob Landley  * sas_phy_delete  -  remove SAS PHY
680c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to remove
681c7ebbbceSChristoph Hellwig  *
682c7ebbbceSChristoph Hellwig  * Removes the specified SAS PHY.  If the SAS PHY has an
683c7ebbbceSChristoph Hellwig  * associated remote PHY it is removed before.
684c7ebbbceSChristoph Hellwig  */
685c7ebbbceSChristoph Hellwig void
686c7ebbbceSChristoph Hellwig sas_phy_delete(struct sas_phy *phy)
687c7ebbbceSChristoph Hellwig {
688c7ebbbceSChristoph Hellwig 	struct device *dev = &phy->dev;
689c7ebbbceSChristoph Hellwig 
69065c92b09SJames Bottomley 	/* this happens if the phy is still part of a port when deleted */
69165c92b09SJames Bottomley 	BUG_ON(!list_empty(&phy->port_siblings));
692c7ebbbceSChristoph Hellwig 
693c7ebbbceSChristoph Hellwig 	transport_remove_device(dev);
694c7ebbbceSChristoph Hellwig 	device_del(dev);
695c7ebbbceSChristoph Hellwig 	transport_destroy_device(dev);
69692aab646SMike Anderson 	put_device(dev);
697c7ebbbceSChristoph Hellwig }
698c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_delete);
699c7ebbbceSChristoph Hellwig 
700c7ebbbceSChristoph Hellwig /**
701eb44820cSRob Landley  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
702c7ebbbceSChristoph Hellwig  * @dev:	device to check
703c7ebbbceSChristoph Hellwig  *
704c7ebbbceSChristoph Hellwig  * Returns:
705c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS PHY, %0 else
706c7ebbbceSChristoph Hellwig  */
707c7ebbbceSChristoph Hellwig int scsi_is_sas_phy(const struct device *dev)
708c7ebbbceSChristoph Hellwig {
709c7ebbbceSChristoph Hellwig 	return dev->release == sas_phy_release;
710c7ebbbceSChristoph Hellwig }
711c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_phy);
712c7ebbbceSChristoph Hellwig 
713c7ebbbceSChristoph Hellwig /*
71465c92b09SJames Bottomley  * SAS Port attributes
71565c92b09SJames Bottomley  */
71665c92b09SJames Bottomley #define sas_port_show_simple(field, name, format_string, cast)		\
71765c92b09SJames Bottomley static ssize_t								\
718ee959b00STony Jones show_sas_port_##name(struct device *dev, 				\
719ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
72065c92b09SJames Bottomley {									\
721ee959b00STony Jones 	struct sas_port *port = transport_class_to_sas_port(dev);	\
72265c92b09SJames Bottomley 									\
72365c92b09SJames Bottomley 	return snprintf(buf, 20, format_string, cast port->field);	\
72465c92b09SJames Bottomley }
72565c92b09SJames Bottomley 
72665c92b09SJames Bottomley #define sas_port_simple_attr(field, name, format_string, type)		\
72765c92b09SJames Bottomley 	sas_port_show_simple(field, name, format_string, (type))	\
728ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
72965c92b09SJames Bottomley 
73065c92b09SJames Bottomley sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
73165c92b09SJames Bottomley 
73265c92b09SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_port_class,
73365c92b09SJames Bottomley 			       "sas_port", NULL, NULL, NULL);
73465c92b09SJames Bottomley 
73565c92b09SJames Bottomley static int sas_port_match(struct attribute_container *cont, struct device *dev)
73665c92b09SJames Bottomley {
73765c92b09SJames Bottomley 	struct Scsi_Host *shost;
73865c92b09SJames Bottomley 	struct sas_internal *i;
73965c92b09SJames Bottomley 
74065c92b09SJames Bottomley 	if (!scsi_is_sas_port(dev))
74165c92b09SJames Bottomley 		return 0;
74265c92b09SJames Bottomley 	shost = dev_to_shost(dev->parent);
74365c92b09SJames Bottomley 
74465c92b09SJames Bottomley 	if (!shost->transportt)
74565c92b09SJames Bottomley 		return 0;
74665c92b09SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
74765c92b09SJames Bottomley 			&sas_host_class.class)
74865c92b09SJames Bottomley 		return 0;
74965c92b09SJames Bottomley 
75065c92b09SJames Bottomley 	i = to_sas_internal(shost->transportt);
75165c92b09SJames Bottomley 	return &i->port_attr_cont.ac == cont;
75265c92b09SJames Bottomley }
75365c92b09SJames Bottomley 
75465c92b09SJames Bottomley 
75565c92b09SJames Bottomley static void sas_port_release(struct device *dev)
75665c92b09SJames Bottomley {
75765c92b09SJames Bottomley 	struct sas_port *port = dev_to_sas_port(dev);
75865c92b09SJames Bottomley 
75965c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
76065c92b09SJames Bottomley 
76165c92b09SJames Bottomley 	put_device(dev->parent);
76265c92b09SJames Bottomley 	kfree(port);
76365c92b09SJames Bottomley }
76465c92b09SJames Bottomley 
76565c92b09SJames Bottomley static void sas_port_create_link(struct sas_port *port,
76665c92b09SJames Bottomley 				 struct sas_phy *phy)
76765c92b09SJames Bottomley {
76821434966SDarrick J. Wong 	int res;
76921434966SDarrick J. Wong 
77021434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
77171610f55SKay Sievers 				dev_name(&phy->dev));
77221434966SDarrick J. Wong 	if (res)
77321434966SDarrick J. Wong 		goto err;
77421434966SDarrick J. Wong 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
77521434966SDarrick J. Wong 	if (res)
77621434966SDarrick J. Wong 		goto err;
77721434966SDarrick J. Wong 	return;
77821434966SDarrick J. Wong err:
77921434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
780cadbd4a5SHarvey Harrison 	       __func__, res);
78165c92b09SJames Bottomley }
78265c92b09SJames Bottomley 
78365c92b09SJames Bottomley static void sas_port_delete_link(struct sas_port *port,
78465c92b09SJames Bottomley 				 struct sas_phy *phy)
78565c92b09SJames Bottomley {
78671610f55SKay Sievers 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
78765c92b09SJames Bottomley 	sysfs_remove_link(&phy->dev.kobj, "port");
78865c92b09SJames Bottomley }
78965c92b09SJames Bottomley 
79065c92b09SJames Bottomley /** sas_port_alloc - allocate and initialize a SAS port structure
79165c92b09SJames Bottomley  *
79265c92b09SJames Bottomley  * @parent:	parent device
79365c92b09SJames Bottomley  * @port_id:	port number
79465c92b09SJames Bottomley  *
79565c92b09SJames Bottomley  * Allocates a SAS port structure.  It will be added to the device tree
79665c92b09SJames Bottomley  * below the device specified by @parent which must be either a Scsi_Host
79765c92b09SJames Bottomley  * or a sas_expander_device.
79865c92b09SJames Bottomley  *
79965c92b09SJames Bottomley  * Returns %NULL on error
80065c92b09SJames Bottomley  */
80165c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *parent, int port_id)
80265c92b09SJames Bottomley {
80365c92b09SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
80465c92b09SJames Bottomley 	struct sas_port *port;
80565c92b09SJames Bottomley 
80665c92b09SJames Bottomley 	port = kzalloc(sizeof(*port), GFP_KERNEL);
80765c92b09SJames Bottomley 	if (!port)
80865c92b09SJames Bottomley 		return NULL;
80965c92b09SJames Bottomley 
81065c92b09SJames Bottomley 	port->port_identifier = port_id;
81165c92b09SJames Bottomley 
81265c92b09SJames Bottomley 	device_initialize(&port->dev);
81365c92b09SJames Bottomley 
81465c92b09SJames Bottomley 	port->dev.parent = get_device(parent);
81565c92b09SJames Bottomley 	port->dev.release = sas_port_release;
81665c92b09SJames Bottomley 
81765c92b09SJames Bottomley 	mutex_init(&port->phy_list_mutex);
81865c92b09SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
81965c92b09SJames Bottomley 
82065c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
82165c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
82271610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
82365c92b09SJames Bottomley 			     rphy->scsi_target_id, port->port_identifier);
82465c92b09SJames Bottomley 	} else
82571610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
82665c92b09SJames Bottomley 			     port->port_identifier);
82765c92b09SJames Bottomley 
82865c92b09SJames Bottomley 	transport_setup_device(&port->dev);
82965c92b09SJames Bottomley 
83065c92b09SJames Bottomley 	return port;
83165c92b09SJames Bottomley }
83265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_alloc);
83365c92b09SJames Bottomley 
834c9fefeb2SJames Bottomley /** sas_port_alloc_num - allocate and initialize a SAS port structure
835c9fefeb2SJames Bottomley  *
836c9fefeb2SJames Bottomley  * @parent:	parent device
837c9fefeb2SJames Bottomley  *
838c9fefeb2SJames Bottomley  * Allocates a SAS port structure and a number to go with it.  This
839c9fefeb2SJames Bottomley  * interface is really for adapters where the port number has no
840c9fefeb2SJames Bottomley  * meansing, so the sas class should manage them.  It will be added to
841c9fefeb2SJames Bottomley  * the device tree below the device specified by @parent which must be
842c9fefeb2SJames Bottomley  * either a Scsi_Host or a sas_expander_device.
843c9fefeb2SJames Bottomley  *
844c9fefeb2SJames Bottomley  * Returns %NULL on error
845c9fefeb2SJames Bottomley  */
846c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *parent)
847c9fefeb2SJames Bottomley {
848c9fefeb2SJames Bottomley 	int index;
849c9fefeb2SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
850c9fefeb2SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
851c9fefeb2SJames Bottomley 
852c9fefeb2SJames Bottomley 	/* FIXME: use idr for this eventually */
853c9fefeb2SJames Bottomley 	mutex_lock(&sas_host->lock);
854c9fefeb2SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
855c9fefeb2SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
856c9fefeb2SJames Bottomley 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
857c9fefeb2SJames Bottomley 
858c9fefeb2SJames Bottomley 		index = exp->next_port_id++;
859c9fefeb2SJames Bottomley 	} else
860c9fefeb2SJames Bottomley 		index = sas_host->next_port_id++;
861c9fefeb2SJames Bottomley 	mutex_unlock(&sas_host->lock);
862c9fefeb2SJames Bottomley 	return sas_port_alloc(parent, index);
863c9fefeb2SJames Bottomley }
864c9fefeb2SJames Bottomley EXPORT_SYMBOL(sas_port_alloc_num);
865c9fefeb2SJames Bottomley 
86665c92b09SJames Bottomley /**
86765c92b09SJames Bottomley  * sas_port_add - add a SAS port to the device hierarchy
86865c92b09SJames Bottomley  * @port:	port to be added
86965c92b09SJames Bottomley  *
87065c92b09SJames Bottomley  * publishes a port to the rest of the system
87165c92b09SJames Bottomley  */
87265c92b09SJames Bottomley int sas_port_add(struct sas_port *port)
87365c92b09SJames Bottomley {
87465c92b09SJames Bottomley 	int error;
87565c92b09SJames Bottomley 
87665c92b09SJames Bottomley 	/* No phys should be added until this is made visible */
87765c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
87865c92b09SJames Bottomley 
87965c92b09SJames Bottomley 	error = device_add(&port->dev);
88065c92b09SJames Bottomley 
88165c92b09SJames Bottomley 	if (error)
88265c92b09SJames Bottomley 		return error;
88365c92b09SJames Bottomley 
88465c92b09SJames Bottomley 	transport_add_device(&port->dev);
88565c92b09SJames Bottomley 	transport_configure_device(&port->dev);
88665c92b09SJames Bottomley 
88765c92b09SJames Bottomley 	return 0;
88865c92b09SJames Bottomley }
88965c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add);
89065c92b09SJames Bottomley 
89165c92b09SJames Bottomley /**
892eb44820cSRob Landley  * sas_port_free  -  free a SAS PORT
89365c92b09SJames Bottomley  * @port:	SAS PORT to free
89465c92b09SJames Bottomley  *
89565c92b09SJames Bottomley  * Frees the specified SAS PORT.
89665c92b09SJames Bottomley  *
89765c92b09SJames Bottomley  * Note:
89865c92b09SJames Bottomley  *   This function must only be called on a PORT that has not
899af901ca1SAndré Goddard Rosa  *   successfully been added using sas_port_add().
90065c92b09SJames Bottomley  */
90165c92b09SJames Bottomley void sas_port_free(struct sas_port *port)
90265c92b09SJames Bottomley {
90365c92b09SJames Bottomley 	transport_destroy_device(&port->dev);
90465c92b09SJames Bottomley 	put_device(&port->dev);
90565c92b09SJames Bottomley }
90665c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_free);
90765c92b09SJames Bottomley 
90865c92b09SJames Bottomley /**
909eb44820cSRob Landley  * sas_port_delete  -  remove SAS PORT
91065c92b09SJames Bottomley  * @port:	SAS PORT to remove
91165c92b09SJames Bottomley  *
91265c92b09SJames Bottomley  * Removes the specified SAS PORT.  If the SAS PORT has an
91365c92b09SJames Bottomley  * associated phys, unlink them from the port as well.
91465c92b09SJames Bottomley  */
91565c92b09SJames Bottomley void sas_port_delete(struct sas_port *port)
91665c92b09SJames Bottomley {
91765c92b09SJames Bottomley 	struct device *dev = &port->dev;
91865c92b09SJames Bottomley 	struct sas_phy *phy, *tmp_phy;
91965c92b09SJames Bottomley 
92065c92b09SJames Bottomley 	if (port->rphy) {
92165c92b09SJames Bottomley 		sas_rphy_delete(port->rphy);
92265c92b09SJames Bottomley 		port->rphy = NULL;
92365c92b09SJames Bottomley 	}
92465c92b09SJames Bottomley 
92565c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
92665c92b09SJames Bottomley 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
92765c92b09SJames Bottomley 				 port_siblings) {
92865c92b09SJames Bottomley 		sas_port_delete_link(port, phy);
92965c92b09SJames Bottomley 		list_del_init(&phy->port_siblings);
93065c92b09SJames Bottomley 	}
93165c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
93265c92b09SJames Bottomley 
933a0e1b6efSJames Bottomley 	if (port->is_backlink) {
934a0e1b6efSJames Bottomley 		struct device *parent = port->dev.parent;
935a0e1b6efSJames Bottomley 
93671610f55SKay Sievers 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
937a0e1b6efSJames Bottomley 		port->is_backlink = 0;
938a0e1b6efSJames Bottomley 	}
939a0e1b6efSJames Bottomley 
94065c92b09SJames Bottomley 	transport_remove_device(dev);
94165c92b09SJames Bottomley 	device_del(dev);
94265c92b09SJames Bottomley 	transport_destroy_device(dev);
94365c92b09SJames Bottomley 	put_device(dev);
94465c92b09SJames Bottomley }
94565c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete);
94665c92b09SJames Bottomley 
94765c92b09SJames Bottomley /**
948eb44820cSRob Landley  * scsi_is_sas_port -  check if a struct device represents a SAS port
94965c92b09SJames Bottomley  * @dev:	device to check
95065c92b09SJames Bottomley  *
95165c92b09SJames Bottomley  * Returns:
95265c92b09SJames Bottomley  *	%1 if the device represents a SAS Port, %0 else
95365c92b09SJames Bottomley  */
95465c92b09SJames Bottomley int scsi_is_sas_port(const struct device *dev)
95565c92b09SJames Bottomley {
95665c92b09SJames Bottomley 	return dev->release == sas_port_release;
95765c92b09SJames Bottomley }
95865c92b09SJames Bottomley EXPORT_SYMBOL(scsi_is_sas_port);
95965c92b09SJames Bottomley 
96065c92b09SJames Bottomley /**
96165c92b09SJames Bottomley  * sas_port_add_phy - add another phy to a port to form a wide port
96265c92b09SJames Bottomley  * @port:	port to add the phy to
96365c92b09SJames Bottomley  * @phy:	phy to add
96465c92b09SJames Bottomley  *
96565c92b09SJames Bottomley  * When a port is initially created, it is empty (has no phys).  All
96665c92b09SJames Bottomley  * ports must have at least one phy to operated, and all wide ports
96765c92b09SJames Bottomley  * must have at least two.  The current code makes no difference
96865c92b09SJames Bottomley  * between ports and wide ports, but the only object that can be
96965c92b09SJames Bottomley  * connected to a remote device is a port, so ports must be formed on
97065c92b09SJames Bottomley  * all devices with phys if they're connected to anything.
97165c92b09SJames Bottomley  */
97265c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
97365c92b09SJames Bottomley {
97465c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
97565c92b09SJames Bottomley 	if (unlikely(!list_empty(&phy->port_siblings))) {
97665c92b09SJames Bottomley 		/* make sure we're already on this port */
97765c92b09SJames Bottomley 		struct sas_phy *tmp;
97865c92b09SJames Bottomley 
97965c92b09SJames Bottomley 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
98065c92b09SJames Bottomley 			if (tmp == phy)
98165c92b09SJames Bottomley 				break;
98265c92b09SJames Bottomley 		/* If this trips, you added a phy that was already
98365c92b09SJames Bottomley 		 * part of a different port */
98465c92b09SJames Bottomley 		if (unlikely(tmp != phy)) {
98571610f55SKay Sievers 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
98671610f55SKay Sievers 				   dev_name(&phy->dev));
98765c92b09SJames Bottomley 			BUG();
98865c92b09SJames Bottomley 		}
98965c92b09SJames Bottomley 	} else {
99065c92b09SJames Bottomley 		sas_port_create_link(port, phy);
99165c92b09SJames Bottomley 		list_add_tail(&phy->port_siblings, &port->phy_list);
99265c92b09SJames Bottomley 		port->num_phys++;
99365c92b09SJames Bottomley 	}
99465c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
99565c92b09SJames Bottomley }
99665c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add_phy);
99765c92b09SJames Bottomley 
99865c92b09SJames Bottomley /**
99965c92b09SJames Bottomley  * sas_port_delete_phy - remove a phy from a port or wide port
100065c92b09SJames Bottomley  * @port:	port to remove the phy from
100165c92b09SJames Bottomley  * @phy:	phy to remove
100265c92b09SJames Bottomley  *
100365c92b09SJames Bottomley  * This operation is used for tearing down ports again.  It must be
100465c92b09SJames Bottomley  * done to every port or wide port before calling sas_port_delete.
100565c92b09SJames Bottomley  */
100665c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
100765c92b09SJames Bottomley {
100865c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
100965c92b09SJames Bottomley 	sas_port_delete_link(port, phy);
101065c92b09SJames Bottomley 	list_del_init(&phy->port_siblings);
101165c92b09SJames Bottomley 	port->num_phys--;
101265c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
101365c92b09SJames Bottomley }
101465c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete_phy);
101565c92b09SJames Bottomley 
1016a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *port)
1017a0e1b6efSJames Bottomley {
101821434966SDarrick J. Wong 	int res;
1019a0e1b6efSJames Bottomley 	struct device *parent = port->dev.parent->parent->parent;
1020a0e1b6efSJames Bottomley 
1021a0e1b6efSJames Bottomley 	if (port->is_backlink)
1022a0e1b6efSJames Bottomley 		return;
1023a0e1b6efSJames Bottomley 	port->is_backlink = 1;
102421434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
102571610f55SKay Sievers 				dev_name(parent));
102621434966SDarrick J. Wong 	if (res)
102721434966SDarrick J. Wong 		goto err;
102821434966SDarrick J. Wong 	return;
102921434966SDarrick J. Wong err:
103021434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1031cadbd4a5SHarvey Harrison 	       __func__, res);
1032a0e1b6efSJames Bottomley 
1033a0e1b6efSJames Bottomley }
1034a0e1b6efSJames Bottomley EXPORT_SYMBOL(sas_port_mark_backlink);
1035a0e1b6efSJames Bottomley 
103665c92b09SJames Bottomley /*
1037c7ebbbceSChristoph Hellwig  * SAS remote PHY attributes.
1038c7ebbbceSChristoph Hellwig  */
1039c7ebbbceSChristoph Hellwig 
1040c7ebbbceSChristoph Hellwig #define sas_rphy_show_simple(field, name, format_string, cast)		\
1041c7ebbbceSChristoph Hellwig static ssize_t								\
1042ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1043ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1044c7ebbbceSChristoph Hellwig {									\
1045ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1046c7ebbbceSChristoph Hellwig 									\
1047c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1048c7ebbbceSChristoph Hellwig }
1049c7ebbbceSChristoph Hellwig 
1050c7ebbbceSChristoph Hellwig #define sas_rphy_simple_attr(field, name, format_string, type)		\
1051c7ebbbceSChristoph Hellwig 	sas_rphy_show_simple(field, name, format_string, (type))	\
1052ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1053c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1054c7ebbbceSChristoph Hellwig 
1055c7ebbbceSChristoph Hellwig #define sas_rphy_show_protocol(field, name)				\
1056c7ebbbceSChristoph Hellwig static ssize_t								\
1057ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1058ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1059c7ebbbceSChristoph Hellwig {									\
1060ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1061c7ebbbceSChristoph Hellwig 									\
1062c7ebbbceSChristoph Hellwig 	if (!rphy->field)					\
1063c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
1064c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(rphy->field, buf);	\
1065c7ebbbceSChristoph Hellwig }
1066c7ebbbceSChristoph Hellwig 
1067c7ebbbceSChristoph Hellwig #define sas_rphy_protocol_attr(field, name)				\
1068c7ebbbceSChristoph Hellwig 	sas_rphy_show_protocol(field, name)				\
1069ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1070c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1071c7ebbbceSChristoph Hellwig 
1072c7ebbbceSChristoph Hellwig static ssize_t
1073ee959b00STony Jones show_sas_rphy_device_type(struct device *dev,
1074ee959b00STony Jones 			  struct device_attribute *attr, char *buf)
1075c7ebbbceSChristoph Hellwig {
1076ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1077c7ebbbceSChristoph Hellwig 
1078c7ebbbceSChristoph Hellwig 	if (!rphy->identify.device_type)
1079c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
1080c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(
1081c7ebbbceSChristoph Hellwig 			rphy->identify.device_type, buf);
1082c7ebbbceSChristoph Hellwig }
1083c7ebbbceSChristoph Hellwig 
1084ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1085c7ebbbceSChristoph Hellwig 		show_sas_rphy_device_type, NULL);
1086c7ebbbceSChristoph Hellwig 
1087a0125641SChristoph Hellwig static ssize_t
1088ee959b00STony Jones show_sas_rphy_enclosure_identifier(struct device *dev,
1089ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
1090a0125641SChristoph Hellwig {
1091ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1092a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1093a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1094a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1095a0125641SChristoph Hellwig 	u64 identifier;
1096a0125641SChristoph Hellwig 	int error;
1097a0125641SChristoph Hellwig 
1098a0125641SChristoph Hellwig 	/*
1099a0125641SChristoph Hellwig 	 * Only devices behind an expander are supported, because the
1100a0125641SChristoph Hellwig 	 * enclosure identifier is a SMP feature.
1101a0125641SChristoph Hellwig 	 */
1102f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1103a0125641SChristoph Hellwig 		return -EINVAL;
1104a0125641SChristoph Hellwig 
1105a0125641SChristoph Hellwig 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1106a0125641SChristoph Hellwig 	if (error)
1107a0125641SChristoph Hellwig 		return error;
1108a0125641SChristoph Hellwig 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1109a0125641SChristoph Hellwig }
1110a0125641SChristoph Hellwig 
1111ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1112a0125641SChristoph Hellwig 		show_sas_rphy_enclosure_identifier, NULL);
1113a0125641SChristoph Hellwig 
1114a0125641SChristoph Hellwig static ssize_t
1115ee959b00STony Jones show_sas_rphy_bay_identifier(struct device *dev,
1116ee959b00STony Jones 			     struct device_attribute *attr, char *buf)
1117a0125641SChristoph Hellwig {
1118ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1119a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1120a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1121a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1122a0125641SChristoph Hellwig 	int val;
1123a0125641SChristoph Hellwig 
1124f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1125a0125641SChristoph Hellwig 		return -EINVAL;
1126a0125641SChristoph Hellwig 
1127a0125641SChristoph Hellwig 	val = i->f->get_bay_identifier(rphy);
1128a0125641SChristoph Hellwig 	if (val < 0)
1129a0125641SChristoph Hellwig 		return val;
1130a0125641SChristoph Hellwig 	return sprintf(buf, "%d\n", val);
1131a0125641SChristoph Hellwig }
1132a0125641SChristoph Hellwig 
1133ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1134a0125641SChristoph Hellwig 		show_sas_rphy_bay_identifier, NULL);
1135a0125641SChristoph Hellwig 
1136c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.initiator_port_protocols,
1137c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
1138c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1139c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1140c7ebbbceSChristoph Hellwig 		unsigned long long);
1141c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1142c7ebbbceSChristoph Hellwig 
114342ab0360SJames Bottomley /* only need 8 bytes of data plus header (4 or 8) */
114442ab0360SJames Bottomley #define BUF_SIZE 64
114542ab0360SJames Bottomley 
114642ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *sdev)
114742ab0360SJames Bottomley {
114842ab0360SJames Bottomley 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
114942ab0360SJames Bottomley 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
115042ab0360SJames Bottomley 	struct sas_end_device *rdev;
115142ab0360SJames Bottomley 	struct scsi_mode_data mode_data;
115242ab0360SJames Bottomley 	int res, error;
115342ab0360SJames Bottomley 
115442ab0360SJames Bottomley 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
115542ab0360SJames Bottomley 
115642ab0360SJames Bottomley 	rdev = rphy_to_end_device(rphy);
115742ab0360SJames Bottomley 
115842ab0360SJames Bottomley 	if (!buffer)
115942ab0360SJames Bottomley 		return -ENOMEM;
116042ab0360SJames Bottomley 
116142ab0360SJames Bottomley 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
116242ab0360SJames Bottomley 			      &mode_data, NULL);
116342ab0360SJames Bottomley 
116442ab0360SJames Bottomley 	error = -EINVAL;
116542ab0360SJames Bottomley 	if (!scsi_status_is_good(res))
116642ab0360SJames Bottomley 		goto out;
116742ab0360SJames Bottomley 
116842ab0360SJames Bottomley 	msdata = buffer +  mode_data.header_length +
116942ab0360SJames Bottomley 		mode_data.block_descriptor_length;
117042ab0360SJames Bottomley 
117142ab0360SJames Bottomley 	if (msdata - buffer > BUF_SIZE - 8)
117242ab0360SJames Bottomley 		goto out;
117342ab0360SJames Bottomley 
117442ab0360SJames Bottomley 	error = 0;
117542ab0360SJames Bottomley 
117642ab0360SJames Bottomley 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
117742ab0360SJames Bottomley 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
117842ab0360SJames Bottomley 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
117942ab0360SJames Bottomley 
118042ab0360SJames Bottomley  out:
118142ab0360SJames Bottomley 	kfree(buffer);
118242ab0360SJames Bottomley 	return error;
118342ab0360SJames Bottomley }
118442ab0360SJames Bottomley EXPORT_SYMBOL(sas_read_port_mode_page);
118542ab0360SJames Bottomley 
118679cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
118779cb1819SJames Bottomley 			       "sas_end_device", NULL, NULL, NULL);
118879cb1819SJames Bottomley 
118942ab0360SJames Bottomley #define sas_end_dev_show_simple(field, name, format_string, cast)	\
119042ab0360SJames Bottomley static ssize_t								\
1191ee959b00STony Jones show_sas_end_dev_##name(struct device *dev, 				\
1192ee959b00STony Jones 			struct device_attribute *attr, char *buf)	\
119342ab0360SJames Bottomley {									\
1194ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
119542ab0360SJames Bottomley 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
119642ab0360SJames Bottomley 									\
119742ab0360SJames Bottomley 	return snprintf(buf, 20, format_string, cast rdev->field);	\
119842ab0360SJames Bottomley }
119942ab0360SJames Bottomley 
120042ab0360SJames Bottomley #define sas_end_dev_simple_attr(field, name, format_string, type)	\
120142ab0360SJames Bottomley 	sas_end_dev_show_simple(field, name, format_string, (type))	\
1202ee959b00STony Jones static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
120342ab0360SJames Bottomley 		show_sas_end_dev_##name, NULL)
120442ab0360SJames Bottomley 
120542ab0360SJames Bottomley sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
120642ab0360SJames Bottomley sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
120742ab0360SJames Bottomley 			"%d\n", int);
120842ab0360SJames Bottomley sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
120942ab0360SJames Bottomley 			"%d\n", int);
121042ab0360SJames Bottomley 
121179cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_expander_class,
121279cb1819SJames Bottomley 			       "sas_expander", NULL, NULL, NULL);
121379cb1819SJames Bottomley 
121479cb1819SJames Bottomley #define sas_expander_show_simple(field, name, format_string, cast)	\
121579cb1819SJames Bottomley static ssize_t								\
1216ee959b00STony Jones show_sas_expander_##name(struct device *dev, 				\
1217ee959b00STony Jones 			 struct device_attribute *attr, char *buf)	\
121879cb1819SJames Bottomley {									\
1219ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
122079cb1819SJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
122179cb1819SJames Bottomley 									\
122279cb1819SJames Bottomley 	return snprintf(buf, 20, format_string, cast edev->field);	\
122379cb1819SJames Bottomley }
122479cb1819SJames Bottomley 
122579cb1819SJames Bottomley #define sas_expander_simple_attr(field, name, format_string, type)	\
122679cb1819SJames Bottomley 	sas_expander_show_simple(field, name, format_string, (type))	\
1227ee959b00STony Jones static SAS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
122879cb1819SJames Bottomley 		show_sas_expander_##name, NULL)
122979cb1819SJames Bottomley 
123079cb1819SJames Bottomley sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
123179cb1819SJames Bottomley sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
123279cb1819SJames Bottomley sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
123379cb1819SJames Bottomley sas_expander_simple_attr(component_vendor_id, component_vendor_id,
123479cb1819SJames Bottomley 			 "%s\n", char *);
123579cb1819SJames Bottomley sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
123679cb1819SJames Bottomley sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
123779cb1819SJames Bottomley 			 unsigned int);
123879cb1819SJames Bottomley sas_expander_simple_attr(level, level, "%d\n", int);
123942ab0360SJames Bottomley 
1240c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
12412f8600dfSJames Bottomley 		"sas_device", NULL, NULL, NULL);
1242c7ebbbceSChristoph Hellwig 
1243c7ebbbceSChristoph Hellwig static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1244c7ebbbceSChristoph Hellwig {
1245c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
1246c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1247c7ebbbceSChristoph Hellwig 
1248c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_rphy(dev))
1249c7ebbbceSChristoph Hellwig 		return 0;
1250c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent->parent);
1251c7ebbbceSChristoph Hellwig 
1252c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
1253c7ebbbceSChristoph Hellwig 		return 0;
1254c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
1255c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
1256c7ebbbceSChristoph Hellwig 		return 0;
1257c7ebbbceSChristoph Hellwig 
1258c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
1259c7ebbbceSChristoph Hellwig 	return &i->rphy_attr_cont.ac == cont;
1260c7ebbbceSChristoph Hellwig }
1261c7ebbbceSChristoph Hellwig 
126242ab0360SJames Bottomley static int sas_end_dev_match(struct attribute_container *cont,
126342ab0360SJames Bottomley 			     struct device *dev)
126442ab0360SJames Bottomley {
126542ab0360SJames Bottomley 	struct Scsi_Host *shost;
126642ab0360SJames Bottomley 	struct sas_internal *i;
126742ab0360SJames Bottomley 	struct sas_rphy *rphy;
126842ab0360SJames Bottomley 
126942ab0360SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
127042ab0360SJames Bottomley 		return 0;
127142ab0360SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
127242ab0360SJames Bottomley 	rphy = dev_to_rphy(dev);
127342ab0360SJames Bottomley 
127442ab0360SJames Bottomley 	if (!shost->transportt)
127542ab0360SJames Bottomley 		return 0;
127642ab0360SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
127742ab0360SJames Bottomley 			&sas_host_class.class)
127842ab0360SJames Bottomley 		return 0;
127942ab0360SJames Bottomley 
128042ab0360SJames Bottomley 	i = to_sas_internal(shost->transportt);
128142ab0360SJames Bottomley 	return &i->end_dev_attr_cont.ac == cont &&
12822f8600dfSJames Bottomley 		rphy->identify.device_type == SAS_END_DEVICE;
128342ab0360SJames Bottomley }
128442ab0360SJames Bottomley 
128579cb1819SJames Bottomley static int sas_expander_match(struct attribute_container *cont,
128679cb1819SJames Bottomley 			      struct device *dev)
128779cb1819SJames Bottomley {
128879cb1819SJames Bottomley 	struct Scsi_Host *shost;
128979cb1819SJames Bottomley 	struct sas_internal *i;
129079cb1819SJames Bottomley 	struct sas_rphy *rphy;
129179cb1819SJames Bottomley 
129279cb1819SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
129379cb1819SJames Bottomley 		return 0;
129479cb1819SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
129579cb1819SJames Bottomley 	rphy = dev_to_rphy(dev);
129679cb1819SJames Bottomley 
129779cb1819SJames Bottomley 	if (!shost->transportt)
129879cb1819SJames Bottomley 		return 0;
129979cb1819SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
130079cb1819SJames Bottomley 			&sas_host_class.class)
130179cb1819SJames Bottomley 		return 0;
130279cb1819SJames Bottomley 
130379cb1819SJames Bottomley 	i = to_sas_internal(shost->transportt);
130479cb1819SJames Bottomley 	return &i->expander_attr_cont.ac == cont &&
130579cb1819SJames Bottomley 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
13062f8600dfSJames Bottomley 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
130779cb1819SJames Bottomley }
130879cb1819SJames Bottomley 
13092f8600dfSJames Bottomley static void sas_expander_release(struct device *dev)
1310c7ebbbceSChristoph Hellwig {
1311c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = dev_to_rphy(dev);
13122f8600dfSJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1313c7ebbbceSChristoph Hellwig 
131493c20a59SFUJITA Tomonori 	if (rphy->q)
131593c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
131693c20a59SFUJITA Tomonori 
1317c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
13182f8600dfSJames Bottomley 	kfree(edev);
1319c7ebbbceSChristoph Hellwig }
1320c7ebbbceSChristoph Hellwig 
13212f8600dfSJames Bottomley static void sas_end_device_release(struct device *dev)
1322c7ebbbceSChristoph Hellwig {
13232f8600dfSJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(dev);
13242f8600dfSJames Bottomley 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1325c7ebbbceSChristoph Hellwig 
132693c20a59SFUJITA Tomonori 	if (rphy->q)
132793c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
132893c20a59SFUJITA Tomonori 
13292f8600dfSJames Bottomley 	put_device(dev->parent);
13302f8600dfSJames Bottomley 	kfree(edev);
1331c7ebbbceSChristoph Hellwig }
1332c7ebbbceSChristoph Hellwig 
1333c7ebbbceSChristoph Hellwig /**
1334c5943d36SJames Bottomley  * sas_rphy_initialize - common rphy intialization
1335c5943d36SJames Bottomley  * @rphy:	rphy to initialise
1336c5943d36SJames Bottomley  *
1337c5943d36SJames Bottomley  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1338c5943d36SJames Bottomley  * initialise the common rphy component of each.
1339c5943d36SJames Bottomley  */
1340c5943d36SJames Bottomley static void sas_rphy_initialize(struct sas_rphy *rphy)
1341c5943d36SJames Bottomley {
1342c5943d36SJames Bottomley 	INIT_LIST_HEAD(&rphy->list);
1343c5943d36SJames Bottomley }
1344c5943d36SJames Bottomley 
1345c5943d36SJames Bottomley /**
134642ab0360SJames Bottomley  * sas_end_device_alloc - allocate an rphy for an end device
1347eb44820cSRob Landley  * @parent: which port
134842ab0360SJames Bottomley  *
134942ab0360SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
135042ab0360SJames Bottomley  *
135142ab0360SJames Bottomley  * Returns:
135242ab0360SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
135342ab0360SJames Bottomley  */
135465c92b09SJames Bottomley struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
135542ab0360SJames Bottomley {
135642ab0360SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
135742ab0360SJames Bottomley 	struct sas_end_device *rdev;
135842ab0360SJames Bottomley 
135942ab0360SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
136042ab0360SJames Bottomley 	if (!rdev) {
136142ab0360SJames Bottomley 		return NULL;
136242ab0360SJames Bottomley 	}
136342ab0360SJames Bottomley 
136442ab0360SJames Bottomley 	device_initialize(&rdev->rphy.dev);
136542ab0360SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
13662f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_end_device_release;
136765c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
136865c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
136971610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
137071610f55SKay Sievers 			     shost->host_no, rphy->scsi_target_id,
137171610f55SKay Sievers 			     parent->port_identifier);
137265c92b09SJames Bottomley 	} else
137371610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
137465c92b09SJames Bottomley 			     shost->host_no, parent->port_identifier);
137542ab0360SJames Bottomley 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1376c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
137742ab0360SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
137842ab0360SJames Bottomley 
137942ab0360SJames Bottomley 	return &rdev->rphy;
138042ab0360SJames Bottomley }
138142ab0360SJames Bottomley EXPORT_SYMBOL(sas_end_device_alloc);
138242ab0360SJames Bottomley 
138379cb1819SJames Bottomley /**
138479cb1819SJames Bottomley  * sas_expander_alloc - allocate an rphy for an end device
1385eb44820cSRob Landley  * @parent: which port
1386eb44820cSRob Landley  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
138779cb1819SJames Bottomley  *
138879cb1819SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
138979cb1819SJames Bottomley  *
139079cb1819SJames Bottomley  * Returns:
139179cb1819SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
139279cb1819SJames Bottomley  */
139365c92b09SJames Bottomley struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
139479cb1819SJames Bottomley 				    enum sas_device_type type)
139579cb1819SJames Bottomley {
139679cb1819SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
139779cb1819SJames Bottomley 	struct sas_expander_device *rdev;
139879cb1819SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
139979cb1819SJames Bottomley 
140079cb1819SJames Bottomley 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
140179cb1819SJames Bottomley 	       type != SAS_FANOUT_EXPANDER_DEVICE);
140279cb1819SJames Bottomley 
140379cb1819SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
140479cb1819SJames Bottomley 	if (!rdev) {
140579cb1819SJames Bottomley 		return NULL;
140679cb1819SJames Bottomley 	}
140779cb1819SJames Bottomley 
140879cb1819SJames Bottomley 	device_initialize(&rdev->rphy.dev);
140979cb1819SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
14102f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_expander_release;
141179cb1819SJames Bottomley 	mutex_lock(&sas_host->lock);
141279cb1819SJames Bottomley 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
141379cb1819SJames Bottomley 	mutex_unlock(&sas_host->lock);
141471610f55SKay Sievers 	dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
141579cb1819SJames Bottomley 		     shost->host_no, rdev->rphy.scsi_target_id);
141679cb1819SJames Bottomley 	rdev->rphy.identify.device_type = type;
1417c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
141879cb1819SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
141979cb1819SJames Bottomley 
142079cb1819SJames Bottomley 	return &rdev->rphy;
142179cb1819SJames Bottomley }
142279cb1819SJames Bottomley EXPORT_SYMBOL(sas_expander_alloc);
142342ab0360SJames Bottomley 
142442ab0360SJames Bottomley /**
1425eb44820cSRob Landley  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1426c7ebbbceSChristoph Hellwig  * @rphy:	The remote PHY to be added
1427c7ebbbceSChristoph Hellwig  *
1428c7ebbbceSChristoph Hellwig  * Publishes a SAS remote PHY to the rest of the system.
1429c7ebbbceSChristoph Hellwig  */
1430c7ebbbceSChristoph Hellwig int sas_rphy_add(struct sas_rphy *rphy)
1431c7ebbbceSChristoph Hellwig {
143265c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1433c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1434c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1435c7ebbbceSChristoph Hellwig 	struct sas_identify *identify = &rphy->identify;
1436c7ebbbceSChristoph Hellwig 	int error;
1437c7ebbbceSChristoph Hellwig 
1438c7ebbbceSChristoph Hellwig 	if (parent->rphy)
1439c7ebbbceSChristoph Hellwig 		return -ENXIO;
1440c7ebbbceSChristoph Hellwig 	parent->rphy = rphy;
1441c7ebbbceSChristoph Hellwig 
1442c7ebbbceSChristoph Hellwig 	error = device_add(&rphy->dev);
1443c7ebbbceSChristoph Hellwig 	if (error)
1444c7ebbbceSChristoph Hellwig 		return error;
1445c7ebbbceSChristoph Hellwig 	transport_add_device(&rphy->dev);
1446c7ebbbceSChristoph Hellwig 	transport_configure_device(&rphy->dev);
144739dca558SJames Bottomley 	if (sas_bsg_initialize(shost, rphy))
144871610f55SKay Sievers 		printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
144939dca558SJames Bottomley 
1450c7ebbbceSChristoph Hellwig 
1451e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1452c7ebbbceSChristoph Hellwig 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1453c7ebbbceSChristoph Hellwig 	if (identify->device_type == SAS_END_DEVICE &&
1454c7ebbbceSChristoph Hellwig 	    (identify->target_port_protocols &
1455c7ebbbceSChristoph Hellwig 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1456c7ebbbceSChristoph Hellwig 		rphy->scsi_target_id = sas_host->next_target_id++;
14577676f83aSJames Bottomley 	else if (identify->device_type == SAS_END_DEVICE)
14587676f83aSJames Bottomley 		rphy->scsi_target_id = -1;
1459e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1460c7ebbbceSChristoph Hellwig 
146179cb1819SJames Bottomley 	if (identify->device_type == SAS_END_DEVICE &&
146279cb1819SJames Bottomley 	    rphy->scsi_target_id != -1) {
1463e8bf3941SJames Bottomley 		scsi_scan_target(&rphy->dev, 0,
1464c8490f3aSDarrick J. Wong 				rphy->scsi_target_id, SCAN_WILD_CARD, 0);
1465c7ebbbceSChristoph Hellwig 	}
1466c7ebbbceSChristoph Hellwig 
1467c7ebbbceSChristoph Hellwig 	return 0;
1468c7ebbbceSChristoph Hellwig }
1469c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_add);
1470c7ebbbceSChristoph Hellwig 
1471c7ebbbceSChristoph Hellwig /**
1472eb44820cSRob Landley  * sas_rphy_free  -  free a SAS remote PHY
1473eb44820cSRob Landley  * @rphy: SAS remote PHY to free
1474c7ebbbceSChristoph Hellwig  *
1475c7ebbbceSChristoph Hellwig  * Frees the specified SAS remote PHY.
1476c7ebbbceSChristoph Hellwig  *
1477c7ebbbceSChristoph Hellwig  * Note:
1478c7ebbbceSChristoph Hellwig  *   This function must only be called on a remote
1479af901ca1SAndré Goddard Rosa  *   PHY that has not successfully been added using
14806f63caaeSDarrick J. Wong  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1481c7ebbbceSChristoph Hellwig  */
1482c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *rphy)
1483c7ebbbceSChristoph Hellwig {
148492aab646SMike Anderson 	struct device *dev = &rphy->dev;
1485c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1486c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1487c7ebbbceSChristoph Hellwig 
1488e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1489c7ebbbceSChristoph Hellwig 	list_del(&rphy->list);
1490e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1491c7ebbbceSChristoph Hellwig 
1492b6aff669SJames Bottomley 	sas_bsg_remove(shost, rphy);
1493b6aff669SJames Bottomley 
149492aab646SMike Anderson 	transport_destroy_device(dev);
14952f8600dfSJames Bottomley 
149692aab646SMike Anderson 	put_device(dev);
1497c7ebbbceSChristoph Hellwig }
1498c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_free);
1499c7ebbbceSChristoph Hellwig 
1500c7ebbbceSChristoph Hellwig /**
1501eb44820cSRob Landley  * sas_rphy_delete  -  remove and free SAS remote PHY
15026f63caaeSDarrick J. Wong  * @rphy:	SAS remote PHY to remove and free
1503c7ebbbceSChristoph Hellwig  *
15046f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY and frees it.
1505c7ebbbceSChristoph Hellwig  */
1506c7ebbbceSChristoph Hellwig void
1507c7ebbbceSChristoph Hellwig sas_rphy_delete(struct sas_rphy *rphy)
1508c7ebbbceSChristoph Hellwig {
15096f63caaeSDarrick J. Wong 	sas_rphy_remove(rphy);
15106f63caaeSDarrick J. Wong 	sas_rphy_free(rphy);
15116f63caaeSDarrick J. Wong }
15126f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_delete);
15136f63caaeSDarrick J. Wong 
15146f63caaeSDarrick J. Wong /**
1515eb44820cSRob Landley  * sas_rphy_remove  -  remove SAS remote PHY
15166f63caaeSDarrick J. Wong  * @rphy:	SAS remote phy to remove
15176f63caaeSDarrick J. Wong  *
15186f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY.
15196f63caaeSDarrick J. Wong  */
15206f63caaeSDarrick J. Wong void
15216f63caaeSDarrick J. Wong sas_rphy_remove(struct sas_rphy *rphy)
15226f63caaeSDarrick J. Wong {
1523c7ebbbceSChristoph Hellwig 	struct device *dev = &rphy->dev;
152465c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(dev->parent);
1525c7ebbbceSChristoph Hellwig 
1526d4054239SChristoph Hellwig 	switch (rphy->identify.device_type) {
1527d4054239SChristoph Hellwig 	case SAS_END_DEVICE:
1528fe8b2304SChristoph Hellwig 		scsi_remove_target(dev);
1529d4054239SChristoph Hellwig 		break;
1530d4054239SChristoph Hellwig 	case SAS_EDGE_EXPANDER_DEVICE:
1531d4054239SChristoph Hellwig 	case SAS_FANOUT_EXPANDER_DEVICE:
153265c92b09SJames Bottomley 		sas_remove_children(dev);
1533d4054239SChristoph Hellwig 		break;
1534d4054239SChristoph Hellwig 	default:
1535d4054239SChristoph Hellwig 		break;
1536d4054239SChristoph Hellwig 	}
1537c7ebbbceSChristoph Hellwig 
1538fe8b2304SChristoph Hellwig 	transport_remove_device(dev);
1539fe8b2304SChristoph Hellwig 	device_del(dev);
1540c7ebbbceSChristoph Hellwig 
154133b114e9SChristoph Hellwig 	parent->rphy = NULL;
1542c7ebbbceSChristoph Hellwig }
15436f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_remove);
1544c7ebbbceSChristoph Hellwig 
1545c7ebbbceSChristoph Hellwig /**
1546eb44820cSRob Landley  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1547c7ebbbceSChristoph Hellwig  * @dev:	device to check
1548c7ebbbceSChristoph Hellwig  *
1549c7ebbbceSChristoph Hellwig  * Returns:
1550c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS remote PHY, %0 else
1551c7ebbbceSChristoph Hellwig  */
1552c7ebbbceSChristoph Hellwig int scsi_is_sas_rphy(const struct device *dev)
1553c7ebbbceSChristoph Hellwig {
15542f8600dfSJames Bottomley 	return dev->release == sas_end_device_release ||
15552f8600dfSJames Bottomley 		dev->release == sas_expander_release;
1556c7ebbbceSChristoph Hellwig }
1557c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_rphy);
1558c7ebbbceSChristoph Hellwig 
1559c7ebbbceSChristoph Hellwig 
1560c7ebbbceSChristoph Hellwig /*
1561c7ebbbceSChristoph Hellwig  * SCSI scan helper
1562c7ebbbceSChristoph Hellwig  */
1563c7ebbbceSChristoph Hellwig 
1564e02f3f59SChristoph Hellwig static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1565e02f3f59SChristoph Hellwig 		uint id, uint lun)
1566c7ebbbceSChristoph Hellwig {
1567c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1568c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy;
1569c7ebbbceSChristoph Hellwig 
1570e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1571c7ebbbceSChristoph Hellwig 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
15726d99a3f3SJames Bottomley 		if (rphy->identify.device_type != SAS_END_DEVICE ||
15736d99a3f3SJames Bottomley 		    rphy->scsi_target_id == -1)
1574e02f3f59SChristoph Hellwig 			continue;
1575e02f3f59SChristoph Hellwig 
1576e8bf3941SJames Bottomley 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1577e02f3f59SChristoph Hellwig 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1578e8bf3941SJames Bottomley 			scsi_scan_target(&rphy->dev, 0,
1579e02f3f59SChristoph Hellwig 					 rphy->scsi_target_id, lun, 1);
1580e02f3f59SChristoph Hellwig 		}
1581e02f3f59SChristoph Hellwig 	}
1582e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1583e02f3f59SChristoph Hellwig 
1584e02f3f59SChristoph Hellwig 	return 0;
1585c7ebbbceSChristoph Hellwig }
1586c7ebbbceSChristoph Hellwig 
1587c7ebbbceSChristoph Hellwig 
1588c7ebbbceSChristoph Hellwig /*
1589c7ebbbceSChristoph Hellwig  * Setup / Teardown code
1590c7ebbbceSChristoph Hellwig  */
1591c7ebbbceSChristoph Hellwig 
159242ab0360SJames Bottomley #define SETUP_TEMPLATE(attrb, field, perm, test)			\
1593ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
159442ab0360SJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
159542ab0360SJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
159642ab0360SJames Bottomley 	if (test)							\
1597c7ebbbceSChristoph Hellwig 		count++
1598c7ebbbceSChristoph Hellwig 
1599d24e1eebSJames Bottomley #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1600ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
1601d24e1eebSJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
1602d24e1eebSJames Bottomley 	if (ro_test) {							\
1603d24e1eebSJames Bottomley 		i->private_##attrb[count].attr.mode = ro_perm;		\
1604d24e1eebSJames Bottomley 		i->private_##attrb[count].store = NULL;			\
1605d24e1eebSJames Bottomley 	}								\
1606d24e1eebSJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
1607d24e1eebSJames Bottomley 	if (test)							\
1608d24e1eebSJames Bottomley 		count++
160942ab0360SJames Bottomley 
161042ab0360SJames Bottomley #define SETUP_RPORT_ATTRIBUTE(field) 					\
161142ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
161242ab0360SJames Bottomley 
1613dd9fbb52SJames Bottomley #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
161442ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1615dd9fbb52SJames Bottomley 
161665c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE(field)					\
161742ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1618c7ebbbceSChristoph Hellwig 
1619d24e1eebSJames Bottomley #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1620d24e1eebSJames Bottomley 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1621d24e1eebSJames Bottomley 			!i->f->set_phy_speed, S_IRUGO)
1622d24e1eebSJames Bottomley 
1623acbf167dSDarrick J. Wong #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1624acbf167dSDarrick J. Wong 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1625acbf167dSDarrick J. Wong 			  !i->f->func, S_IRUGO)
1626acbf167dSDarrick J. Wong 
162765c92b09SJames Bottomley #define SETUP_PORT_ATTRIBUTE(field)					\
162865c92b09SJames Bottomley 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
162965c92b09SJames Bottomley 
163065c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
163142ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1632dd9fbb52SJames Bottomley 
163365c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1634fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
163507ba3a95SChristoph Hellwig 
163665c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1637fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1638dd9fbb52SJames Bottomley 
163942ab0360SJames Bottomley #define SETUP_END_DEV_ATTRIBUTE(field)					\
164042ab0360SJames Bottomley 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1641c7ebbbceSChristoph Hellwig 
164279cb1819SJames Bottomley #define SETUP_EXPANDER_ATTRIBUTE(field)					\
164379cb1819SJames Bottomley 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
164479cb1819SJames Bottomley 
1645c7ebbbceSChristoph Hellwig /**
1646eb44820cSRob Landley  * sas_attach_transport  -  instantiate SAS transport template
1647c7ebbbceSChristoph Hellwig  * @ft:		SAS transport class function template
1648c7ebbbceSChristoph Hellwig  */
1649c7ebbbceSChristoph Hellwig struct scsi_transport_template *
1650c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *ft)
1651c7ebbbceSChristoph Hellwig {
1652c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1653c7ebbbceSChristoph Hellwig 	int count;
1654c7ebbbceSChristoph Hellwig 
165524669f75SJes Sorensen 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1656c7ebbbceSChristoph Hellwig 	if (!i)
1657c7ebbbceSChristoph Hellwig 		return NULL;
1658c7ebbbceSChristoph Hellwig 
1659e02f3f59SChristoph Hellwig 	i->t.user_scan = sas_user_scan;
1660c7ebbbceSChristoph Hellwig 
1661c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1662c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.class = &sas_host_class.class;
1663c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.match = sas_host_match;
1664c7ebbbceSChristoph Hellwig 	transport_container_register(&i->t.host_attrs);
1665c7ebbbceSChristoph Hellwig 	i->t.host_size = sizeof(struct sas_host_attrs);
1666c7ebbbceSChristoph Hellwig 
1667c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1668c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1669c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.match = sas_phy_match;
1670c7ebbbceSChristoph Hellwig 	transport_container_register(&i->phy_attr_cont);
1671c7ebbbceSChristoph Hellwig 
167265c92b09SJames Bottomley 	i->port_attr_cont.ac.class = &sas_port_class.class;
167365c92b09SJames Bottomley 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
167465c92b09SJames Bottomley 	i->port_attr_cont.ac.match = sas_port_match;
167565c92b09SJames Bottomley 	transport_container_register(&i->port_attr_cont);
167665c92b09SJames Bottomley 
1677c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1678c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1679c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1680c7ebbbceSChristoph Hellwig 	transport_container_register(&i->rphy_attr_cont);
1681c7ebbbceSChristoph Hellwig 
168242ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
168342ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
168442ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
168542ab0360SJames Bottomley 	transport_container_register(&i->end_dev_attr_cont);
168642ab0360SJames Bottomley 
168779cb1819SJames Bottomley 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
168879cb1819SJames Bottomley 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
168979cb1819SJames Bottomley 	i->expander_attr_cont.ac.match = sas_expander_match;
169079cb1819SJames Bottomley 	transport_container_register(&i->expander_attr_cont);
169179cb1819SJames Bottomley 
1692c7ebbbceSChristoph Hellwig 	i->f = ft;
1693c7ebbbceSChristoph Hellwig 
1694c7ebbbceSChristoph Hellwig 	count = 0;
169565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
169665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
169765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(device_type);
169865c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(sas_address);
169965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_identifier);
170065c92b09SJames Bottomley 	//SETUP_PHY_ATTRIBUTE(port_identifier);
170165c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
170265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1703d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
170465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1705d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1706c3ee74c4SChristoph Hellwig 
170765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
170865c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
170965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
171065c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
171165c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
171265c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1713acbf167dSDarrick J. Wong 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1714c7ebbbceSChristoph Hellwig 	i->phy_attrs[count] = NULL;
1715c7ebbbceSChristoph Hellwig 
1716c7ebbbceSChristoph Hellwig 	count = 0;
171765c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
171865c92b09SJames Bottomley 	i->port_attrs[count] = NULL;
171965c92b09SJames Bottomley 
172065c92b09SJames Bottomley 	count = 0;
1721c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1722c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1723c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1724c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1725c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1726dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1727dd9fbb52SJames Bottomley 				       get_enclosure_identifier);
1728dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1729dd9fbb52SJames Bottomley 				       get_bay_identifier);
1730c7ebbbceSChristoph Hellwig 	i->rphy_attrs[count] = NULL;
1731c7ebbbceSChristoph Hellwig 
173242ab0360SJames Bottomley 	count = 0;
173342ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
173442ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
173542ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
173642ab0360SJames Bottomley 	i->end_dev_attrs[count] = NULL;
173742ab0360SJames Bottomley 
173879cb1819SJames Bottomley 	count = 0;
173979cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
174079cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_id);
174179cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
174279cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
174379cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_id);
174479cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
174579cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(level);
174679cb1819SJames Bottomley 	i->expander_attrs[count] = NULL;
174779cb1819SJames Bottomley 
1748c7ebbbceSChristoph Hellwig 	return &i->t;
1749c7ebbbceSChristoph Hellwig }
1750c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_attach_transport);
1751c7ebbbceSChristoph Hellwig 
1752c7ebbbceSChristoph Hellwig /**
1753eb44820cSRob Landley  * sas_release_transport  -  release SAS transport template instance
1754c7ebbbceSChristoph Hellwig  * @t:		transport template instance
1755c7ebbbceSChristoph Hellwig  */
1756c7ebbbceSChristoph Hellwig void sas_release_transport(struct scsi_transport_template *t)
1757c7ebbbceSChristoph Hellwig {
1758c7ebbbceSChristoph Hellwig 	struct sas_internal *i = to_sas_internal(t);
1759c7ebbbceSChristoph Hellwig 
1760c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->t.host_attrs);
1761c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->phy_attr_cont);
176265c92b09SJames Bottomley 	transport_container_unregister(&i->port_attr_cont);
1763c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->rphy_attr_cont);
1764db82f841SJames Bottomley 	transport_container_unregister(&i->end_dev_attr_cont);
176579cb1819SJames Bottomley 	transport_container_unregister(&i->expander_attr_cont);
1766c7ebbbceSChristoph Hellwig 
1767c7ebbbceSChristoph Hellwig 	kfree(i);
1768c7ebbbceSChristoph Hellwig }
1769c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_release_transport);
1770c7ebbbceSChristoph Hellwig 
1771c7ebbbceSChristoph Hellwig static __init int sas_transport_init(void)
1772c7ebbbceSChristoph Hellwig {
1773c7ebbbceSChristoph Hellwig 	int error;
1774c7ebbbceSChristoph Hellwig 
1775c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_host_class);
1776c7ebbbceSChristoph Hellwig 	if (error)
1777c7ebbbceSChristoph Hellwig 		goto out;
1778c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_phy_class);
1779c7ebbbceSChristoph Hellwig 	if (error)
1780c7ebbbceSChristoph Hellwig 		goto out_unregister_transport;
178165c92b09SJames Bottomley 	error = transport_class_register(&sas_port_class);
1782c7ebbbceSChristoph Hellwig 	if (error)
1783c7ebbbceSChristoph Hellwig 		goto out_unregister_phy;
178465c92b09SJames Bottomley 	error = transport_class_register(&sas_rphy_class);
178565c92b09SJames Bottomley 	if (error)
178665c92b09SJames Bottomley 		goto out_unregister_port;
178742ab0360SJames Bottomley 	error = transport_class_register(&sas_end_dev_class);
178842ab0360SJames Bottomley 	if (error)
178942ab0360SJames Bottomley 		goto out_unregister_rphy;
179079cb1819SJames Bottomley 	error = transport_class_register(&sas_expander_class);
179179cb1819SJames Bottomley 	if (error)
179279cb1819SJames Bottomley 		goto out_unregister_end_dev;
1793c7ebbbceSChristoph Hellwig 
1794c7ebbbceSChristoph Hellwig 	return 0;
1795c7ebbbceSChristoph Hellwig 
179679cb1819SJames Bottomley  out_unregister_end_dev:
179779cb1819SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
179842ab0360SJames Bottomley  out_unregister_rphy:
179942ab0360SJames Bottomley 	transport_class_unregister(&sas_rphy_class);
180065c92b09SJames Bottomley  out_unregister_port:
180165c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1802c7ebbbceSChristoph Hellwig  out_unregister_phy:
1803c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
1804c7ebbbceSChristoph Hellwig  out_unregister_transport:
1805c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1806c7ebbbceSChristoph Hellwig  out:
1807c7ebbbceSChristoph Hellwig 	return error;
1808c7ebbbceSChristoph Hellwig 
1809c7ebbbceSChristoph Hellwig }
1810c7ebbbceSChristoph Hellwig 
1811c7ebbbceSChristoph Hellwig static void __exit sas_transport_exit(void)
1812c7ebbbceSChristoph Hellwig {
1813c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1814c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
181565c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1816c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_rphy_class);
181742ab0360SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
181879cb1819SJames Bottomley 	transport_class_unregister(&sas_expander_class);
1819c7ebbbceSChristoph Hellwig }
1820c7ebbbceSChristoph Hellwig 
1821c7ebbbceSChristoph Hellwig MODULE_AUTHOR("Christoph Hellwig");
182286b9c4c1SAlexis Bruemmer MODULE_DESCRIPTION("SAS Transport Attributes");
1823c7ebbbceSChristoph Hellwig MODULE_LICENSE("GPL");
1824c7ebbbceSChristoph Hellwig 
1825c7ebbbceSChristoph Hellwig module_init(sas_transport_init);
1826c7ebbbceSChristoph Hellwig module_exit(sas_transport_exit);
1827