1c7ebbbceSChristoph Hellwig /*
2a0125641SChristoph Hellwig  * Copyright (C) 2005-2006 Dell Inc.
3c7ebbbceSChristoph Hellwig  *	Released under GPL v2.
4c7ebbbceSChristoph Hellwig  *
5c7ebbbceSChristoph Hellwig  * Serial Attached SCSI (SAS) transport class.
6c7ebbbceSChristoph Hellwig  *
7c7ebbbceSChristoph Hellwig  * The SAS transport class contains common code to deal with SAS HBAs,
8c7ebbbceSChristoph Hellwig  * an aproximated representation of SAS topologies in the driver model,
9b1c11812SJoe Perches  * and various sysfs attributes to expose these topologies and management
10c7ebbbceSChristoph Hellwig  * interfaces to userspace.
11c7ebbbceSChristoph Hellwig  *
12c7ebbbceSChristoph Hellwig  * In addition to the basic SCSI core objects this transport class
13c7ebbbceSChristoph Hellwig  * introduces two additional intermediate objects:  The SAS PHY
14c7ebbbceSChristoph Hellwig  * as represented by struct sas_phy defines an "outgoing" PHY on
15c7ebbbceSChristoph Hellwig  * a SAS HBA or Expander, and the SAS remote PHY represented by
16c7ebbbceSChristoph Hellwig  * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17c7ebbbceSChristoph Hellwig  * end device.  Note that this is purely a software concept, the
18c7ebbbceSChristoph Hellwig  * underlying hardware for a PHY and a remote PHY is the exactly
19c7ebbbceSChristoph Hellwig  * the same.
20c7ebbbceSChristoph Hellwig  *
21c7ebbbceSChristoph Hellwig  * There is no concept of a SAS port in this code, users can see
22c7ebbbceSChristoph Hellwig  * what PHYs form a wide port based on the port_identifier attribute,
23c7ebbbceSChristoph Hellwig  * which is the same for all PHYs in a port.
24c7ebbbceSChristoph Hellwig  */
25c7ebbbceSChristoph Hellwig 
26c7ebbbceSChristoph Hellwig #include <linux/init.h>
27c7ebbbceSChristoph Hellwig #include <linux/module.h>
28f6a57033SAl Viro #include <linux/jiffies.h>
29c7ebbbceSChristoph Hellwig #include <linux/err.h>
308c65b4a6STim Schmielau #include <linux/slab.h>
318c65b4a6STim Schmielau #include <linux/string.h>
327aa68e80SFUJITA Tomonori #include <linux/blkdev.h>
337aa68e80SFUJITA Tomonori #include <linux/bsg.h>
34c7ebbbceSChristoph Hellwig 
35e02f3f59SChristoph Hellwig #include <scsi/scsi.h>
36ca18d6f7SBart Van Assche #include <scsi/scsi_cmnd.h>
3782ed4db4SChristoph Hellwig #include <scsi/scsi_request.h>
38c7ebbbceSChristoph Hellwig #include <scsi/scsi_device.h>
39c7ebbbceSChristoph Hellwig #include <scsi/scsi_host.h>
40c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport.h>
41c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport_sas.h>
42c7ebbbceSChristoph Hellwig 
43d6159c17SJames Bottomley #include "scsi_sas_internal.h"
44c7ebbbceSChristoph Hellwig struct sas_host_attrs {
45c7ebbbceSChristoph Hellwig 	struct list_head rphy_list;
46e02f3f59SChristoph Hellwig 	struct mutex lock;
47b6aff669SJames Bottomley 	struct request_queue *q;
48c7ebbbceSChristoph Hellwig 	u32 next_target_id;
4979cb1819SJames Bottomley 	u32 next_expander_id;
50c9fefeb2SJames Bottomley 	int next_port_id;
51c7ebbbceSChristoph Hellwig };
52c7ebbbceSChristoph Hellwig #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
53c7ebbbceSChristoph Hellwig 
54c7ebbbceSChristoph Hellwig 
55c7ebbbceSChristoph Hellwig /*
56c7ebbbceSChristoph Hellwig  * Hack to allow attributes of the same name in different objects.
57c7ebbbceSChristoph Hellwig  */
58ee959b00STony Jones #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59ee959b00STony Jones 	struct device_attribute dev_attr_##_prefix##_##_name = \
60c7ebbbceSChristoph Hellwig 	__ATTR(_name,_mode,_show,_store)
61c7ebbbceSChristoph Hellwig 
62c7ebbbceSChristoph Hellwig 
63c7ebbbceSChristoph Hellwig /*
64c7ebbbceSChristoph Hellwig  * Pretty printing helpers
65c7ebbbceSChristoph Hellwig  */
66c7ebbbceSChristoph Hellwig 
67c7ebbbceSChristoph Hellwig #define sas_bitfield_name_match(title, table)			\
68c7ebbbceSChristoph Hellwig static ssize_t							\
69c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
70c7ebbbceSChristoph Hellwig {								\
71c7ebbbceSChristoph Hellwig 	char *prefix = "";					\
72c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
73c7ebbbceSChristoph Hellwig 	int i;							\
74c7ebbbceSChristoph Hellwig 								\
756391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
76c7ebbbceSChristoph Hellwig 		if (table[i].value & table_key) {		\
77c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s%s",	\
78c7ebbbceSChristoph Hellwig 				prefix, table[i].name);		\
79c7ebbbceSChristoph Hellwig 			prefix = ", ";				\
80c7ebbbceSChristoph Hellwig 		}						\
81c7ebbbceSChristoph Hellwig 	}							\
82c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
83c7ebbbceSChristoph Hellwig 	return len;						\
84c7ebbbceSChristoph Hellwig }
85c7ebbbceSChristoph Hellwig 
86d24e1eebSJames Bottomley #define sas_bitfield_name_set(title, table)			\
87d24e1eebSJames Bottomley static ssize_t							\
88d24e1eebSJames Bottomley set_sas_##title##_names(u32 *table_key, const char *buf)	\
89d24e1eebSJames Bottomley {								\
90d24e1eebSJames Bottomley 	ssize_t len = 0;					\
91d24e1eebSJames Bottomley 	int i;							\
92d24e1eebSJames Bottomley 								\
93d24e1eebSJames Bottomley 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
94d24e1eebSJames Bottomley 		len = strlen(table[i].name);			\
95d24e1eebSJames Bottomley 		if (strncmp(buf, table[i].name, len) == 0 &&	\
96d24e1eebSJames Bottomley 		    (buf[len] == '\n' || buf[len] == '\0')) {	\
97d24e1eebSJames Bottomley 			*table_key = table[i].value;		\
98d24e1eebSJames Bottomley 			return 0;				\
99d24e1eebSJames Bottomley 		}						\
100d24e1eebSJames Bottomley 	}							\
101d24e1eebSJames Bottomley 	return -EINVAL;						\
102d24e1eebSJames Bottomley }
103d24e1eebSJames Bottomley 
104c7ebbbceSChristoph Hellwig #define sas_bitfield_name_search(title, table)			\
105c7ebbbceSChristoph Hellwig static ssize_t							\
106c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
107c7ebbbceSChristoph Hellwig {								\
108c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
109c7ebbbceSChristoph Hellwig 	int i;							\
110c7ebbbceSChristoph Hellwig 								\
1116391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
112c7ebbbceSChristoph Hellwig 		if (table[i].value == table_key) {		\
113c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s",		\
114c7ebbbceSChristoph Hellwig 				table[i].name);			\
115c7ebbbceSChristoph Hellwig 			break;					\
116c7ebbbceSChristoph Hellwig 		}						\
117c7ebbbceSChristoph Hellwig 	}							\
118c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
119c7ebbbceSChristoph Hellwig 	return len;						\
120c7ebbbceSChristoph Hellwig }
121c7ebbbceSChristoph Hellwig 
122c7ebbbceSChristoph Hellwig static struct {
123c7ebbbceSChristoph Hellwig 	u32		value;
124c7ebbbceSChristoph Hellwig 	char		*name;
125c7ebbbceSChristoph Hellwig } sas_device_type_names[] = {
126c7ebbbceSChristoph Hellwig 	{ SAS_PHY_UNUSED,		"unused" },
127c7ebbbceSChristoph Hellwig 	{ SAS_END_DEVICE,		"end device" },
128c7ebbbceSChristoph Hellwig 	{ SAS_EDGE_EXPANDER_DEVICE,	"edge expander" },
129c7ebbbceSChristoph Hellwig 	{ SAS_FANOUT_EXPANDER_DEVICE,	"fanout expander" },
130c7ebbbceSChristoph Hellwig };
131c7ebbbceSChristoph Hellwig sas_bitfield_name_search(device_type, sas_device_type_names)
132c7ebbbceSChristoph Hellwig 
133c7ebbbceSChristoph Hellwig 
134c7ebbbceSChristoph Hellwig static struct {
135c7ebbbceSChristoph Hellwig 	u32		value;
136c7ebbbceSChristoph Hellwig 	char		*name;
137c7ebbbceSChristoph Hellwig } sas_protocol_names[] = {
138c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SATA,		"sata" },
139c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SMP,		"smp" },
140c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_STP,		"stp" },
141c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SSP,		"ssp" },
142c7ebbbceSChristoph Hellwig };
143c7ebbbceSChristoph Hellwig sas_bitfield_name_match(protocol, sas_protocol_names)
144c7ebbbceSChristoph Hellwig 
145c7ebbbceSChristoph Hellwig static struct {
146c7ebbbceSChristoph Hellwig 	u32		value;
147c7ebbbceSChristoph Hellwig 	char		*name;
148c7ebbbceSChristoph Hellwig } sas_linkspeed_names[] = {
149c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_UNKNOWN,	"Unknown" },
150c7ebbbceSChristoph Hellwig 	{ SAS_PHY_DISABLED,		"Phy disabled" },
151c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_FAILED,		"Link Rate failed" },
152c7ebbbceSChristoph Hellwig 	{ SAS_SATA_SPINUP_HOLD,		"Spin-up hold" },
153c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_1_5_GBPS,	"1.5 Gbit" },
154c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_3_0_GBPS,	"3.0 Gbit" },
1557e6dff62SJames Bottomley 	{ SAS_LINK_RATE_6_0_GBPS,	"6.0 Gbit" },
156d84fd392SSreekanth Reddy 	{ SAS_LINK_RATE_12_0_GBPS,	"12.0 Gbit" },
157c7ebbbceSChristoph Hellwig };
158c7ebbbceSChristoph Hellwig sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
159d24e1eebSJames Bottomley sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
160c7ebbbceSChristoph Hellwig 
1610f88009dSJames Bottomley static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
1620f88009dSJames Bottomley {
1630f88009dSJames Bottomley 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
1640f88009dSJames Bottomley 	struct sas_end_device *rdev;
1650f88009dSJames Bottomley 
1660f88009dSJames Bottomley 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
1670f88009dSJames Bottomley 
1680f88009dSJames Bottomley 	rdev = rphy_to_end_device(rphy);
1690f88009dSJames Bottomley 	return rdev;
1700f88009dSJames Bottomley }
1710f88009dSJames Bottomley 
172651a0136SChristoph Hellwig static int sas_smp_dispatch(struct bsg_job *job)
1737aa68e80SFUJITA Tomonori {
174651a0136SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(job->dev);
175651a0136SChristoph Hellwig 	struct sas_rphy *rphy = NULL;
1767aa68e80SFUJITA Tomonori 
177651a0136SChristoph Hellwig 	if (!scsi_is_host_device(job->dev))
178651a0136SChristoph Hellwig 		rphy = dev_to_rphy(job->dev);
1797aa68e80SFUJITA Tomonori 
18007f5d563SChristoph Hellwig 	if (!job->reply_payload.payload_len) {
181651a0136SChristoph Hellwig 		dev_warn(job->dev, "space for a smp response is missing\n");
182651a0136SChristoph Hellwig 		bsg_job_done(job, -EINVAL, 0);
183651a0136SChristoph Hellwig 		return 0;
1847aa68e80SFUJITA Tomonori 	}
1857aa68e80SFUJITA Tomonori 
186651a0136SChristoph Hellwig 	to_sas_internal(shost->transportt)->f->smp_handler(job, shost, rphy);
187651a0136SChristoph Hellwig 	return 0;
1887aa68e80SFUJITA Tomonori }
1897aa68e80SFUJITA Tomonori 
19093c20a59SFUJITA Tomonori static void sas_host_release(struct device *dev)
19193c20a59SFUJITA Tomonori {
19293c20a59SFUJITA Tomonori 	struct Scsi_Host *shost = dev_to_shost(dev);
19393c20a59SFUJITA Tomonori 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
19493c20a59SFUJITA Tomonori 	struct request_queue *q = sas_host->q;
19593c20a59SFUJITA Tomonori 
19693c20a59SFUJITA Tomonori 	if (q)
19793c20a59SFUJITA Tomonori 		blk_cleanup_queue(q);
19893c20a59SFUJITA Tomonori }
19993c20a59SFUJITA Tomonori 
20039dca558SJames Bottomley static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
2017aa68e80SFUJITA Tomonori {
2027aa68e80SFUJITA Tomonori 	struct request_queue *q;
2037aa68e80SFUJITA Tomonori 
2047aa68e80SFUJITA Tomonori 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
2057aa68e80SFUJITA Tomonori 		printk("%s can't handle SMP requests\n", shost->hostt->name);
2067aa68e80SFUJITA Tomonori 		return 0;
2077aa68e80SFUJITA Tomonori 	}
2087aa68e80SFUJITA Tomonori 
20939dca558SJames Bottomley 	if (rphy) {
210651a0136SChristoph Hellwig 		q = bsg_setup_queue(&rphy->dev, dev_name(&rphy->dev),
211651a0136SChristoph Hellwig 				sas_smp_dispatch, 0, NULL);
212651a0136SChristoph Hellwig 		if (IS_ERR(q))
213651a0136SChristoph Hellwig 			return PTR_ERR(q);
214651a0136SChristoph Hellwig 		rphy->q = q;
21539dca558SJames Bottomley 	} else {
216651a0136SChristoph Hellwig 		char name[20];
217651a0136SChristoph Hellwig 
218651a0136SChristoph Hellwig 		snprintf(name, sizeof(name), "sas_host%d", shost->host_no);
219651a0136SChristoph Hellwig 		q = bsg_setup_queue(&shost->shost_gendev, name,
220651a0136SChristoph Hellwig 				sas_smp_dispatch, 0, sas_host_release);
221651a0136SChristoph Hellwig 		if (IS_ERR(q))
222651a0136SChristoph Hellwig 			return PTR_ERR(q);
223651a0136SChristoph Hellwig 		to_sas_host_attrs(shost)->q = q;
22439dca558SJames Bottomley 	}
2257aa68e80SFUJITA Tomonori 
2260bf6595eSChristoph Hellwig 	/*
2270bf6595eSChristoph Hellwig 	 * by default assume old behaviour and bounce for any highmem page
2280bf6595eSChristoph Hellwig 	 */
2290bf6595eSChristoph Hellwig 	blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
23075ad23bcSNick Piggin 	queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
2319efc160fSBart Van Assche 	queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
2327aa68e80SFUJITA Tomonori 	return 0;
233b6aff669SJames Bottomley }
234b6aff669SJames Bottomley 
235c7ebbbceSChristoph Hellwig /*
236c7ebbbceSChristoph Hellwig  * SAS host attributes
237c7ebbbceSChristoph Hellwig  */
238c7ebbbceSChristoph Hellwig 
23937be6eebSJames Bottomley static int sas_host_setup(struct transport_container *tc, struct device *dev,
240ee959b00STony Jones 			  struct device *cdev)
241c7ebbbceSChristoph Hellwig {
242c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(dev);
243c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
244c7ebbbceSChristoph Hellwig 
245c7ebbbceSChristoph Hellwig 	INIT_LIST_HEAD(&sas_host->rphy_list);
246e02f3f59SChristoph Hellwig 	mutex_init(&sas_host->lock);
247c7ebbbceSChristoph Hellwig 	sas_host->next_target_id = 0;
24879cb1819SJames Bottomley 	sas_host->next_expander_id = 0;
249c9fefeb2SJames Bottomley 	sas_host->next_port_id = 0;
2507aa68e80SFUJITA Tomonori 
25139dca558SJames Bottomley 	if (sas_bsg_initialize(shost, NULL))
2527aa68e80SFUJITA Tomonori 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
2537aa68e80SFUJITA Tomonori 			   shost->host_no);
2547aa68e80SFUJITA Tomonori 
255c7ebbbceSChristoph Hellwig 	return 0;
256c7ebbbceSChristoph Hellwig }
257c7ebbbceSChristoph Hellwig 
258b6aff669SJames Bottomley static int sas_host_remove(struct transport_container *tc, struct device *dev,
259ee959b00STony Jones 			   struct device *cdev)
260b6aff669SJames Bottomley {
261b6aff669SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev);
262651a0136SChristoph Hellwig 	struct request_queue *q = to_sas_host_attrs(shost)->q;
263b6aff669SJames Bottomley 
264651a0136SChristoph Hellwig 	if (q)
265651a0136SChristoph Hellwig 		bsg_unregister_queue(q);
266b6aff669SJames Bottomley 	return 0;
267b6aff669SJames Bottomley }
268b6aff669SJames Bottomley 
269c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_host_class,
270b6aff669SJames Bottomley 		"sas_host", sas_host_setup, sas_host_remove, NULL);
271c7ebbbceSChristoph Hellwig 
272c7ebbbceSChristoph Hellwig static int sas_host_match(struct attribute_container *cont,
273c7ebbbceSChristoph Hellwig 			    struct device *dev)
274c7ebbbceSChristoph Hellwig {
275c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
276c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
277c7ebbbceSChristoph Hellwig 
278c7ebbbceSChristoph Hellwig 	if (!scsi_is_host_device(dev))
279c7ebbbceSChristoph Hellwig 		return 0;
280c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev);
281c7ebbbceSChristoph Hellwig 
282c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
283c7ebbbceSChristoph Hellwig 		return 0;
284c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
285c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
286c7ebbbceSChristoph Hellwig 		return 0;
287c7ebbbceSChristoph Hellwig 
288c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
289c7ebbbceSChristoph Hellwig 	return &i->t.host_attrs.ac == cont;
290c7ebbbceSChristoph Hellwig }
291c7ebbbceSChristoph Hellwig 
292c7ebbbceSChristoph Hellwig static int do_sas_phy_delete(struct device *dev, void *data)
293c7ebbbceSChristoph Hellwig {
29465c92b09SJames Bottomley 	int pass = (int)(unsigned long)data;
29565c92b09SJames Bottomley 
29665c92b09SJames Bottomley 	if (pass == 0 && scsi_is_sas_port(dev))
29765c92b09SJames Bottomley 		sas_port_delete(dev_to_sas_port(dev));
29865c92b09SJames Bottomley 	else if (pass == 1 && scsi_is_sas_phy(dev))
299c7ebbbceSChristoph Hellwig 		sas_phy_delete(dev_to_phy(dev));
300c7ebbbceSChristoph Hellwig 	return 0;
301c7ebbbceSChristoph Hellwig }
302c7ebbbceSChristoph Hellwig 
303c7ebbbceSChristoph Hellwig /**
304eb44820cSRob Landley  * sas_remove_children  -  tear down a devices SAS data structures
30565c92b09SJames Bottomley  * @dev:	device belonging to the sas object
30665c92b09SJames Bottomley  *
30765c92b09SJames Bottomley  * Removes all SAS PHYs and remote PHYs for a given object
30865c92b09SJames Bottomley  */
30965c92b09SJames Bottomley void sas_remove_children(struct device *dev)
31065c92b09SJames Bottomley {
31165c92b09SJames Bottomley 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
31265c92b09SJames Bottomley 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
31365c92b09SJames Bottomley }
31465c92b09SJames Bottomley EXPORT_SYMBOL(sas_remove_children);
31565c92b09SJames Bottomley 
31665c92b09SJames Bottomley /**
317eb44820cSRob Landley  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
318c7ebbbceSChristoph Hellwig  * @shost:	Scsi Host that is torn down
319c7ebbbceSChristoph Hellwig  *
320c5ce0abeSJohannes Thumshirn  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
321c5ce0abeSJohannes Thumshirn  * Scsi_Host as well.
322c5ce0abeSJohannes Thumshirn  *
323c5ce0abeSJohannes Thumshirn  * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
324c5ce0abeSJohannes Thumshirn  * already removed.
325c7ebbbceSChristoph Hellwig  */
326c7ebbbceSChristoph Hellwig void sas_remove_host(struct Scsi_Host *shost)
327c7ebbbceSChristoph Hellwig {
32865c92b09SJames Bottomley 	sas_remove_children(&shost->shost_gendev);
329c5ce0abeSJohannes Thumshirn 	scsi_remove_host(shost);
330c7ebbbceSChristoph Hellwig }
331c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_remove_host);
332c7ebbbceSChristoph Hellwig 
3330f88009dSJames Bottomley /**
334bcf508c1SJames Bottomley  * sas_get_address - return the SAS address of the device
335bcf508c1SJames Bottomley  * @sdev: scsi device
336bcf508c1SJames Bottomley  *
337bcf508c1SJames Bottomley  * Returns the SAS address of the scsi device
338bcf508c1SJames Bottomley  */
339bcf508c1SJames Bottomley u64 sas_get_address(struct scsi_device *sdev)
340bcf508c1SJames Bottomley {
341bcf508c1SJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
342bcf508c1SJames Bottomley 
343bcf508c1SJames Bottomley 	return rdev->rphy.identify.sas_address;
344bcf508c1SJames Bottomley }
345bcf508c1SJames Bottomley EXPORT_SYMBOL(sas_get_address);
346bcf508c1SJames Bottomley 
347bcf508c1SJames Bottomley /**
3480f88009dSJames Bottomley  * sas_tlr_supported - checking TLR bit in vpd 0x90
3490f88009dSJames Bottomley  * @sdev: scsi device struct
3500f88009dSJames Bottomley  *
3510f88009dSJames Bottomley  * Check Transport Layer Retries are supported or not.
3520f88009dSJames Bottomley  * If vpd page 0x90 is present, TRL is supported.
3530f88009dSJames Bottomley  *
3540f88009dSJames Bottomley  */
3550f88009dSJames Bottomley unsigned int
3560f88009dSJames Bottomley sas_tlr_supported(struct scsi_device *sdev)
3570f88009dSJames Bottomley {
3580f88009dSJames Bottomley 	const int vpd_len = 32;
3590f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
3600f88009dSJames Bottomley 	char *buffer = kzalloc(vpd_len, GFP_KERNEL);
3610f88009dSJames Bottomley 	int ret = 0;
3620f88009dSJames Bottomley 
363e1779b4fSBart Van Assche 	if (!buffer)
364e1779b4fSBart Van Assche 		goto out;
365e1779b4fSBart Van Assche 
3660f88009dSJames Bottomley 	if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
3670f88009dSJames Bottomley 		goto out;
3680f88009dSJames Bottomley 
3690f88009dSJames Bottomley 	/*
3700f88009dSJames Bottomley 	 * Magic numbers: the VPD Protocol page (0x90)
3710f88009dSJames Bottomley 	 * has a 4 byte header and then one entry per device port
3720f88009dSJames Bottomley 	 * the TLR bit is at offset 8 on each port entry
3730f88009dSJames Bottomley 	 * if we take the first port, that's at total offset 12
3740f88009dSJames Bottomley 	 */
3750f88009dSJames Bottomley 	ret = buffer[12] & 0x01;
3760f88009dSJames Bottomley 
3770f88009dSJames Bottomley  out:
3780f88009dSJames Bottomley 	kfree(buffer);
3790f88009dSJames Bottomley 	rdev->tlr_supported = ret;
3800f88009dSJames Bottomley 	return ret;
3810f88009dSJames Bottomley 
3820f88009dSJames Bottomley }
3830f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_tlr_supported);
3840f88009dSJames Bottomley 
3850f88009dSJames Bottomley /**
3860f88009dSJames Bottomley  * sas_disable_tlr - setting TLR flags
3870f88009dSJames Bottomley  * @sdev: scsi device struct
3880f88009dSJames Bottomley  *
3890f88009dSJames Bottomley  * Seting tlr_enabled flag to 0.
3900f88009dSJames Bottomley  *
3910f88009dSJames Bottomley  */
3920f88009dSJames Bottomley void
3930f88009dSJames Bottomley sas_disable_tlr(struct scsi_device *sdev)
3940f88009dSJames Bottomley {
3950f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
3960f88009dSJames Bottomley 
3970f88009dSJames Bottomley 	rdev->tlr_enabled = 0;
3980f88009dSJames Bottomley }
3990f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_disable_tlr);
4000f88009dSJames Bottomley 
4010f88009dSJames Bottomley /**
4020f88009dSJames Bottomley  * sas_enable_tlr - setting TLR flags
4030f88009dSJames Bottomley  * @sdev: scsi device struct
4040f88009dSJames Bottomley  *
4050f88009dSJames Bottomley  * Seting tlr_enabled flag 1.
4060f88009dSJames Bottomley  *
4070f88009dSJames Bottomley  */
4080f88009dSJames Bottomley void sas_enable_tlr(struct scsi_device *sdev)
4090f88009dSJames Bottomley {
4100f88009dSJames Bottomley 	unsigned int tlr_supported = 0;
4110f88009dSJames Bottomley 	tlr_supported  = sas_tlr_supported(sdev);
4120f88009dSJames Bottomley 
4130f88009dSJames Bottomley 	if (tlr_supported) {
4140f88009dSJames Bottomley 		struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4150f88009dSJames Bottomley 
4160f88009dSJames Bottomley 		rdev->tlr_enabled = 1;
4170f88009dSJames Bottomley 	}
4180f88009dSJames Bottomley 
4190f88009dSJames Bottomley 	return;
4200f88009dSJames Bottomley }
4210f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_enable_tlr);
4220f88009dSJames Bottomley 
4230f88009dSJames Bottomley unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
4240f88009dSJames Bottomley {
4250f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
4260f88009dSJames Bottomley 	return rdev->tlr_enabled;
4270f88009dSJames Bottomley }
4280f88009dSJames Bottomley EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
429c7ebbbceSChristoph Hellwig 
430c7ebbbceSChristoph Hellwig /*
43165c92b09SJames Bottomley  * SAS Phy attributes
432c7ebbbceSChristoph Hellwig  */
433c7ebbbceSChristoph Hellwig 
434c7ebbbceSChristoph Hellwig #define sas_phy_show_simple(field, name, format_string, cast)		\
435c7ebbbceSChristoph Hellwig static ssize_t								\
436ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
437ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
438c7ebbbceSChristoph Hellwig {									\
439ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
440c7ebbbceSChristoph Hellwig 									\
441c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast phy->field);	\
442c7ebbbceSChristoph Hellwig }
443c7ebbbceSChristoph Hellwig 
444c7ebbbceSChristoph Hellwig #define sas_phy_simple_attr(field, name, format_string, type)		\
445c7ebbbceSChristoph Hellwig 	sas_phy_show_simple(field, name, format_string, (type))	\
446ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
447c7ebbbceSChristoph Hellwig 
448c7ebbbceSChristoph Hellwig #define sas_phy_show_protocol(field, name)				\
449c7ebbbceSChristoph Hellwig static ssize_t								\
450ee959b00STony Jones show_sas_phy_##name(struct device *dev, 				\
451ee959b00STony Jones 		    struct device_attribute *attr, char *buf)		\
452c7ebbbceSChristoph Hellwig {									\
453ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
454c7ebbbceSChristoph Hellwig 									\
455c7ebbbceSChristoph Hellwig 	if (!phy->field)						\
456c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
457c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(phy->field, buf);		\
458c7ebbbceSChristoph Hellwig }
459c7ebbbceSChristoph Hellwig 
460c7ebbbceSChristoph Hellwig #define sas_phy_protocol_attr(field, name)				\
461c7ebbbceSChristoph Hellwig 	sas_phy_show_protocol(field, name)				\
462ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
463c7ebbbceSChristoph Hellwig 
464c7ebbbceSChristoph Hellwig #define sas_phy_show_linkspeed(field)					\
465c7ebbbceSChristoph Hellwig static ssize_t								\
466ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
467ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
468c7ebbbceSChristoph Hellwig {									\
469ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
470c7ebbbceSChristoph Hellwig 									\
471c7ebbbceSChristoph Hellwig 	return get_sas_linkspeed_names(phy->field, buf);		\
472c7ebbbceSChristoph Hellwig }
473c7ebbbceSChristoph Hellwig 
474d24e1eebSJames Bottomley /* Fudge to tell if we're minimum or maximum */
475d24e1eebSJames Bottomley #define sas_phy_store_linkspeed(field)					\
476d24e1eebSJames Bottomley static ssize_t								\
477ee959b00STony Jones store_sas_phy_##field(struct device *dev, 				\
478ee959b00STony Jones 		      struct device_attribute *attr, 			\
479ee959b00STony Jones 		      const char *buf,	size_t count)			\
480d24e1eebSJames Bottomley {									\
481ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
482d24e1eebSJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
483d24e1eebSJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
484d24e1eebSJames Bottomley 	u32 value;							\
485d24e1eebSJames Bottomley 	struct sas_phy_linkrates rates = {0};				\
486d24e1eebSJames Bottomley 	int error;							\
487d24e1eebSJames Bottomley 									\
488d24e1eebSJames Bottomley 	error = set_sas_linkspeed_names(&value, buf);			\
489d24e1eebSJames Bottomley 	if (error)							\
490d24e1eebSJames Bottomley 		return error;						\
491d24e1eebSJames Bottomley 	rates.field = value;						\
492d24e1eebSJames Bottomley 	error = i->f->set_phy_speed(phy, &rates);			\
493d24e1eebSJames Bottomley 									\
494d24e1eebSJames Bottomley 	return error ? error : count;					\
495d24e1eebSJames Bottomley }
496d24e1eebSJames Bottomley 
497d24e1eebSJames Bottomley #define sas_phy_linkspeed_rw_attr(field)				\
498d24e1eebSJames Bottomley 	sas_phy_show_linkspeed(field)					\
499d24e1eebSJames Bottomley 	sas_phy_store_linkspeed(field)					\
500ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
501d24e1eebSJames Bottomley 	store_sas_phy_##field)
502d24e1eebSJames Bottomley 
503c7ebbbceSChristoph Hellwig #define sas_phy_linkspeed_attr(field)					\
504c7ebbbceSChristoph Hellwig 	sas_phy_show_linkspeed(field)					\
505ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
506c7ebbbceSChristoph Hellwig 
507d24e1eebSJames Bottomley 
508c3ee74c4SChristoph Hellwig #define sas_phy_show_linkerror(field)					\
509c3ee74c4SChristoph Hellwig static ssize_t								\
510ee959b00STony Jones show_sas_phy_##field(struct device *dev, 				\
511ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
512c3ee74c4SChristoph Hellwig {									\
513ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);		\
514c3ee74c4SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
515c3ee74c4SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
516c3ee74c4SChristoph Hellwig 	int error;							\
517c3ee74c4SChristoph Hellwig 									\
518dd9fbb52SJames Bottomley 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
519c3ee74c4SChristoph Hellwig 	if (error)							\
520c3ee74c4SChristoph Hellwig 		return error;						\
521c3ee74c4SChristoph Hellwig 	return snprintf(buf, 20, "%u\n", phy->field);			\
522c3ee74c4SChristoph Hellwig }
523c3ee74c4SChristoph Hellwig 
524c3ee74c4SChristoph Hellwig #define sas_phy_linkerror_attr(field)					\
525c3ee74c4SChristoph Hellwig 	sas_phy_show_linkerror(field)					\
526ee959b00STony Jones static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
527c3ee74c4SChristoph Hellwig 
528c3ee74c4SChristoph Hellwig 
529c7ebbbceSChristoph Hellwig static ssize_t
530ee959b00STony Jones show_sas_device_type(struct device *dev,
531ee959b00STony Jones 		     struct device_attribute *attr, char *buf)
532c7ebbbceSChristoph Hellwig {
533ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
534c7ebbbceSChristoph Hellwig 
535c7ebbbceSChristoph Hellwig 	if (!phy->identify.device_type)
536c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
537c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(phy->identify.device_type, buf);
538c7ebbbceSChristoph Hellwig }
539ee959b00STony Jones static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
540c7ebbbceSChristoph Hellwig 
541ee959b00STony Jones static ssize_t do_sas_phy_enable(struct device *dev,
542acbf167dSDarrick J. Wong 		size_t count, int enable)
543acbf167dSDarrick J. Wong {
544ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
545acbf167dSDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
546acbf167dSDarrick J. Wong 	struct sas_internal *i = to_sas_internal(shost->transportt);
547acbf167dSDarrick J. Wong 	int error;
548acbf167dSDarrick J. Wong 
549acbf167dSDarrick J. Wong 	error = i->f->phy_enable(phy, enable);
550acbf167dSDarrick J. Wong 	if (error)
551acbf167dSDarrick J. Wong 		return error;
552acbf167dSDarrick J. Wong 	phy->enabled = enable;
553acbf167dSDarrick J. Wong 	return count;
554acbf167dSDarrick J. Wong };
555acbf167dSDarrick J. Wong 
556ee959b00STony Jones static ssize_t
557ee959b00STony Jones store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
558acbf167dSDarrick J. Wong 		     const char *buf, size_t count)
559acbf167dSDarrick J. Wong {
560acbf167dSDarrick J. Wong 	if (count < 1)
561acbf167dSDarrick J. Wong 		return -EINVAL;
562acbf167dSDarrick J. Wong 
563acbf167dSDarrick J. Wong 	switch (buf[0]) {
564acbf167dSDarrick J. Wong 	case '0':
565ee959b00STony Jones 		do_sas_phy_enable(dev, count, 0);
566acbf167dSDarrick J. Wong 		break;
567acbf167dSDarrick J. Wong 	case '1':
568ee959b00STony Jones 		do_sas_phy_enable(dev, count, 1);
569acbf167dSDarrick J. Wong 		break;
570acbf167dSDarrick J. Wong 	default:
571acbf167dSDarrick J. Wong 		return -EINVAL;
572acbf167dSDarrick J. Wong 	}
573acbf167dSDarrick J. Wong 
574acbf167dSDarrick J. Wong 	return count;
575acbf167dSDarrick J. Wong }
576acbf167dSDarrick J. Wong 
577ee959b00STony Jones static ssize_t
578ee959b00STony Jones show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
579ee959b00STony Jones 		    char *buf)
580acbf167dSDarrick J. Wong {
581ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
582acbf167dSDarrick J. Wong 
583acbf167dSDarrick J. Wong 	return snprintf(buf, 20, "%d", phy->enabled);
584acbf167dSDarrick J. Wong }
585acbf167dSDarrick J. Wong 
586ee959b00STony Jones static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
587acbf167dSDarrick J. Wong 			 store_sas_phy_enable);
588acbf167dSDarrick J. Wong 
589ee959b00STony Jones static ssize_t
590ee959b00STony Jones do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
59107ba3a95SChristoph Hellwig {
592ee959b00STony Jones 	struct sas_phy *phy = transport_class_to_phy(dev);
59307ba3a95SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
59407ba3a95SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
59507ba3a95SChristoph Hellwig 	int error;
59607ba3a95SChristoph Hellwig 
59707ba3a95SChristoph Hellwig 	error = i->f->phy_reset(phy, hard_reset);
59807ba3a95SChristoph Hellwig 	if (error)
59907ba3a95SChristoph Hellwig 		return error;
60016d3db1bSDan Williams 	phy->enabled = 1;
60107ba3a95SChristoph Hellwig 	return count;
60207ba3a95SChristoph Hellwig };
60307ba3a95SChristoph Hellwig 
604ee959b00STony Jones static ssize_t
605ee959b00STony Jones store_sas_link_reset(struct device *dev, struct device_attribute *attr,
60607ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
60707ba3a95SChristoph Hellwig {
608ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 0);
60907ba3a95SChristoph Hellwig }
610ee959b00STony Jones static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
61107ba3a95SChristoph Hellwig 
612ee959b00STony Jones static ssize_t
613ee959b00STony Jones store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
61407ba3a95SChristoph Hellwig 		     const char *buf, size_t count)
61507ba3a95SChristoph Hellwig {
616ee959b00STony Jones 	return do_sas_phy_reset(dev, count, 1);
61707ba3a95SChristoph Hellwig }
618ee959b00STony Jones static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
61907ba3a95SChristoph Hellwig 
620c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.initiator_port_protocols,
621c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
622c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.target_port_protocols,
623c7ebbbceSChristoph Hellwig 		target_port_protocols);
624c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
625c7ebbbceSChristoph Hellwig 		unsigned long long);
626c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
627c9fefeb2SJames Bottomley //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
628c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(negotiated_linkrate);
629c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(minimum_linkrate_hw);
630d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(minimum_linkrate);
631c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(maximum_linkrate_hw);
632d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(maximum_linkrate);
633c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(invalid_dword_count);
634c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(running_disparity_error_count);
635c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(loss_of_dword_sync_count);
636c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(phy_reset_problem_count);
637c7ebbbceSChristoph Hellwig 
6380b3e09daSDan Williams static int sas_phy_setup(struct transport_container *tc, struct device *dev,
6390b3e09daSDan Williams 			 struct device *cdev)
6400b3e09daSDan Williams {
6410b3e09daSDan Williams 	struct sas_phy *phy = dev_to_phy(dev);
6420b3e09daSDan Williams 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
6430b3e09daSDan Williams 	struct sas_internal *i = to_sas_internal(shost->transportt);
6440b3e09daSDan Williams 
6450b3e09daSDan Williams 	if (i->f->phy_setup)
6460b3e09daSDan Williams 		i->f->phy_setup(phy);
6470b3e09daSDan Williams 
6480b3e09daSDan Williams 	return 0;
6490b3e09daSDan Williams }
650c7ebbbceSChristoph Hellwig 
651c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_phy_class,
6520b3e09daSDan Williams 		"sas_phy", sas_phy_setup, NULL, NULL);
653c7ebbbceSChristoph Hellwig 
654c7ebbbceSChristoph Hellwig static int sas_phy_match(struct attribute_container *cont, struct device *dev)
655c7ebbbceSChristoph Hellwig {
656c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
657c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
658c7ebbbceSChristoph Hellwig 
659c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_phy(dev))
660c7ebbbceSChristoph Hellwig 		return 0;
661c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent);
662c7ebbbceSChristoph Hellwig 
663c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
664c7ebbbceSChristoph Hellwig 		return 0;
665c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
666c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
667c7ebbbceSChristoph Hellwig 		return 0;
668c7ebbbceSChristoph Hellwig 
669c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
670c7ebbbceSChristoph Hellwig 	return &i->phy_attr_cont.ac == cont;
671c7ebbbceSChristoph Hellwig }
672c7ebbbceSChristoph Hellwig 
673c7ebbbceSChristoph Hellwig static void sas_phy_release(struct device *dev)
674c7ebbbceSChristoph Hellwig {
675c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(dev);
6760b3e09daSDan Williams 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
6770b3e09daSDan Williams 	struct sas_internal *i = to_sas_internal(shost->transportt);
678c7ebbbceSChristoph Hellwig 
6790b3e09daSDan Williams 	if (i->f->phy_release)
6800b3e09daSDan Williams 		i->f->phy_release(phy);
681c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
682c7ebbbceSChristoph Hellwig 	kfree(phy);
683c7ebbbceSChristoph Hellwig }
684c7ebbbceSChristoph Hellwig 
685c7ebbbceSChristoph Hellwig /**
686eb44820cSRob Landley  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
687c7ebbbceSChristoph Hellwig  * @parent:	Parent device
688d99ca418SMoore, Eric  * @number:	Phy index
689c7ebbbceSChristoph Hellwig  *
690c7ebbbceSChristoph Hellwig  * Allocates an SAS PHY structure.  It will be added in the device tree
691c7ebbbceSChristoph Hellwig  * below the device specified by @parent, which has to be either a Scsi_Host
692c7ebbbceSChristoph Hellwig  * or sas_rphy.
693c7ebbbceSChristoph Hellwig  *
694c7ebbbceSChristoph Hellwig  * Returns:
695c7ebbbceSChristoph Hellwig  *	SAS PHY allocated or %NULL if the allocation failed.
696c7ebbbceSChristoph Hellwig  */
697c7ebbbceSChristoph Hellwig struct sas_phy *sas_phy_alloc(struct device *parent, int number)
698c7ebbbceSChristoph Hellwig {
699c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent);
700c7ebbbceSChristoph Hellwig 	struct sas_phy *phy;
701c7ebbbceSChristoph Hellwig 
70224669f75SJes Sorensen 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
703c7ebbbceSChristoph Hellwig 	if (!phy)
704c7ebbbceSChristoph Hellwig 		return NULL;
705c7ebbbceSChristoph Hellwig 
706c7ebbbceSChristoph Hellwig 	phy->number = number;
707acbf167dSDarrick J. Wong 	phy->enabled = 1;
708c7ebbbceSChristoph Hellwig 
709c7ebbbceSChristoph Hellwig 	device_initialize(&phy->dev);
710c7ebbbceSChristoph Hellwig 	phy->dev.parent = get_device(parent);
711c7ebbbceSChristoph Hellwig 	phy->dev.release = sas_phy_release;
71265c92b09SJames Bottomley 	INIT_LIST_HEAD(&phy->port_siblings);
71379cb1819SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
71479cb1819SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
71571610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
71679cb1819SJames Bottomley 			rphy->scsi_target_id, number);
71779cb1819SJames Bottomley 	} else
71871610f55SKay Sievers 		dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
719c7ebbbceSChristoph Hellwig 
720c7ebbbceSChristoph Hellwig 	transport_setup_device(&phy->dev);
721c7ebbbceSChristoph Hellwig 
722c7ebbbceSChristoph Hellwig 	return phy;
723c7ebbbceSChristoph Hellwig }
724c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_alloc);
725c7ebbbceSChristoph Hellwig 
726c7ebbbceSChristoph Hellwig /**
727eb44820cSRob Landley  * sas_phy_add  -  add a SAS PHY to the device hierarchy
728c7ebbbceSChristoph Hellwig  * @phy:	The PHY to be added
729c7ebbbceSChristoph Hellwig  *
730c7ebbbceSChristoph Hellwig  * Publishes a SAS PHY to the rest of the system.
731c7ebbbceSChristoph Hellwig  */
732c7ebbbceSChristoph Hellwig int sas_phy_add(struct sas_phy *phy)
733c7ebbbceSChristoph Hellwig {
734c7ebbbceSChristoph Hellwig 	int error;
735c7ebbbceSChristoph Hellwig 
736c7ebbbceSChristoph Hellwig 	error = device_add(&phy->dev);
737c7ebbbceSChristoph Hellwig 	if (!error) {
738c7ebbbceSChristoph Hellwig 		transport_add_device(&phy->dev);
739c7ebbbceSChristoph Hellwig 		transport_configure_device(&phy->dev);
740c7ebbbceSChristoph Hellwig 	}
741c7ebbbceSChristoph Hellwig 
742c7ebbbceSChristoph Hellwig 	return error;
743c7ebbbceSChristoph Hellwig }
744c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_add);
745c7ebbbceSChristoph Hellwig 
746c7ebbbceSChristoph Hellwig /**
747eb44820cSRob Landley  * sas_phy_free  -  free a SAS PHY
748c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to free
749c7ebbbceSChristoph Hellwig  *
750c7ebbbceSChristoph Hellwig  * Frees the specified SAS PHY.
751c7ebbbceSChristoph Hellwig  *
752c7ebbbceSChristoph Hellwig  * Note:
753c7ebbbceSChristoph Hellwig  *   This function must only be called on a PHY that has not
754af901ca1SAndré Goddard Rosa  *   successfully been added using sas_phy_add().
755c7ebbbceSChristoph Hellwig  */
756c7ebbbceSChristoph Hellwig void sas_phy_free(struct sas_phy *phy)
757c7ebbbceSChristoph Hellwig {
758c7ebbbceSChristoph Hellwig 	transport_destroy_device(&phy->dev);
75992aab646SMike Anderson 	put_device(&phy->dev);
760c7ebbbceSChristoph Hellwig }
761c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_free);
762c7ebbbceSChristoph Hellwig 
763c7ebbbceSChristoph Hellwig /**
764eb44820cSRob Landley  * sas_phy_delete  -  remove SAS PHY
765c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to remove
766c7ebbbceSChristoph Hellwig  *
767c7ebbbceSChristoph Hellwig  * Removes the specified SAS PHY.  If the SAS PHY has an
768c7ebbbceSChristoph Hellwig  * associated remote PHY it is removed before.
769c7ebbbceSChristoph Hellwig  */
770c7ebbbceSChristoph Hellwig void
771c7ebbbceSChristoph Hellwig sas_phy_delete(struct sas_phy *phy)
772c7ebbbceSChristoph Hellwig {
773c7ebbbceSChristoph Hellwig 	struct device *dev = &phy->dev;
774c7ebbbceSChristoph Hellwig 
77565c92b09SJames Bottomley 	/* this happens if the phy is still part of a port when deleted */
77665c92b09SJames Bottomley 	BUG_ON(!list_empty(&phy->port_siblings));
777c7ebbbceSChristoph Hellwig 
778c7ebbbceSChristoph Hellwig 	transport_remove_device(dev);
779c7ebbbceSChristoph Hellwig 	device_del(dev);
780c7ebbbceSChristoph Hellwig 	transport_destroy_device(dev);
78192aab646SMike Anderson 	put_device(dev);
782c7ebbbceSChristoph Hellwig }
783c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_delete);
784c7ebbbceSChristoph Hellwig 
785c7ebbbceSChristoph Hellwig /**
786eb44820cSRob Landley  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
787c7ebbbceSChristoph Hellwig  * @dev:	device to check
788c7ebbbceSChristoph Hellwig  *
789c7ebbbceSChristoph Hellwig  * Returns:
790c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS PHY, %0 else
791c7ebbbceSChristoph Hellwig  */
792c7ebbbceSChristoph Hellwig int scsi_is_sas_phy(const struct device *dev)
793c7ebbbceSChristoph Hellwig {
794c7ebbbceSChristoph Hellwig 	return dev->release == sas_phy_release;
795c7ebbbceSChristoph Hellwig }
796c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_phy);
797c7ebbbceSChristoph Hellwig 
798c7ebbbceSChristoph Hellwig /*
79965c92b09SJames Bottomley  * SAS Port attributes
80065c92b09SJames Bottomley  */
80165c92b09SJames Bottomley #define sas_port_show_simple(field, name, format_string, cast)		\
80265c92b09SJames Bottomley static ssize_t								\
803ee959b00STony Jones show_sas_port_##name(struct device *dev, 				\
804ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
80565c92b09SJames Bottomley {									\
806ee959b00STony Jones 	struct sas_port *port = transport_class_to_sas_port(dev);	\
80765c92b09SJames Bottomley 									\
80865c92b09SJames Bottomley 	return snprintf(buf, 20, format_string, cast port->field);	\
80965c92b09SJames Bottomley }
81065c92b09SJames Bottomley 
81165c92b09SJames Bottomley #define sas_port_simple_attr(field, name, format_string, type)		\
81265c92b09SJames Bottomley 	sas_port_show_simple(field, name, format_string, (type))	\
813ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
81465c92b09SJames Bottomley 
81565c92b09SJames Bottomley sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
81665c92b09SJames Bottomley 
81765c92b09SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_port_class,
81865c92b09SJames Bottomley 			       "sas_port", NULL, NULL, NULL);
81965c92b09SJames Bottomley 
82065c92b09SJames Bottomley static int sas_port_match(struct attribute_container *cont, struct device *dev)
82165c92b09SJames Bottomley {
82265c92b09SJames Bottomley 	struct Scsi_Host *shost;
82365c92b09SJames Bottomley 	struct sas_internal *i;
82465c92b09SJames Bottomley 
82565c92b09SJames Bottomley 	if (!scsi_is_sas_port(dev))
82665c92b09SJames Bottomley 		return 0;
82765c92b09SJames Bottomley 	shost = dev_to_shost(dev->parent);
82865c92b09SJames Bottomley 
82965c92b09SJames Bottomley 	if (!shost->transportt)
83065c92b09SJames Bottomley 		return 0;
83165c92b09SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
83265c92b09SJames Bottomley 			&sas_host_class.class)
83365c92b09SJames Bottomley 		return 0;
83465c92b09SJames Bottomley 
83565c92b09SJames Bottomley 	i = to_sas_internal(shost->transportt);
83665c92b09SJames Bottomley 	return &i->port_attr_cont.ac == cont;
83765c92b09SJames Bottomley }
83865c92b09SJames Bottomley 
83965c92b09SJames Bottomley 
84065c92b09SJames Bottomley static void sas_port_release(struct device *dev)
84165c92b09SJames Bottomley {
84265c92b09SJames Bottomley 	struct sas_port *port = dev_to_sas_port(dev);
84365c92b09SJames Bottomley 
84465c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
84565c92b09SJames Bottomley 
84665c92b09SJames Bottomley 	put_device(dev->parent);
84765c92b09SJames Bottomley 	kfree(port);
84865c92b09SJames Bottomley }
84965c92b09SJames Bottomley 
85065c92b09SJames Bottomley static void sas_port_create_link(struct sas_port *port,
85165c92b09SJames Bottomley 				 struct sas_phy *phy)
85265c92b09SJames Bottomley {
85321434966SDarrick J. Wong 	int res;
85421434966SDarrick J. Wong 
85521434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
85671610f55SKay Sievers 				dev_name(&phy->dev));
85721434966SDarrick J. Wong 	if (res)
85821434966SDarrick J. Wong 		goto err;
85921434966SDarrick J. Wong 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
86021434966SDarrick J. Wong 	if (res)
86121434966SDarrick J. Wong 		goto err;
86221434966SDarrick J. Wong 	return;
86321434966SDarrick J. Wong err:
86421434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
865cadbd4a5SHarvey Harrison 	       __func__, res);
86665c92b09SJames Bottomley }
86765c92b09SJames Bottomley 
86865c92b09SJames Bottomley static void sas_port_delete_link(struct sas_port *port,
86965c92b09SJames Bottomley 				 struct sas_phy *phy)
87065c92b09SJames Bottomley {
87171610f55SKay Sievers 	sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
87265c92b09SJames Bottomley 	sysfs_remove_link(&phy->dev.kobj, "port");
87365c92b09SJames Bottomley }
87465c92b09SJames Bottomley 
87565c92b09SJames Bottomley /** sas_port_alloc - allocate and initialize a SAS port structure
87665c92b09SJames Bottomley  *
87765c92b09SJames Bottomley  * @parent:	parent device
87865c92b09SJames Bottomley  * @port_id:	port number
87965c92b09SJames Bottomley  *
88065c92b09SJames Bottomley  * Allocates a SAS port structure.  It will be added to the device tree
88165c92b09SJames Bottomley  * below the device specified by @parent which must be either a Scsi_Host
88265c92b09SJames Bottomley  * or a sas_expander_device.
88365c92b09SJames Bottomley  *
88465c92b09SJames Bottomley  * Returns %NULL on error
88565c92b09SJames Bottomley  */
88665c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *parent, int port_id)
88765c92b09SJames Bottomley {
88865c92b09SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
88965c92b09SJames Bottomley 	struct sas_port *port;
89065c92b09SJames Bottomley 
89165c92b09SJames Bottomley 	port = kzalloc(sizeof(*port), GFP_KERNEL);
89265c92b09SJames Bottomley 	if (!port)
89365c92b09SJames Bottomley 		return NULL;
89465c92b09SJames Bottomley 
89565c92b09SJames Bottomley 	port->port_identifier = port_id;
89665c92b09SJames Bottomley 
89765c92b09SJames Bottomley 	device_initialize(&port->dev);
89865c92b09SJames Bottomley 
89965c92b09SJames Bottomley 	port->dev.parent = get_device(parent);
90065c92b09SJames Bottomley 	port->dev.release = sas_port_release;
90165c92b09SJames Bottomley 
90265c92b09SJames Bottomley 	mutex_init(&port->phy_list_mutex);
90365c92b09SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
90465c92b09SJames Bottomley 
90565c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
90665c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
90771610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
90865c92b09SJames Bottomley 			     rphy->scsi_target_id, port->port_identifier);
90965c92b09SJames Bottomley 	} else
91071610f55SKay Sievers 		dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
91165c92b09SJames Bottomley 			     port->port_identifier);
91265c92b09SJames Bottomley 
91365c92b09SJames Bottomley 	transport_setup_device(&port->dev);
91465c92b09SJames Bottomley 
91565c92b09SJames Bottomley 	return port;
91665c92b09SJames Bottomley }
91765c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_alloc);
91865c92b09SJames Bottomley 
919c9fefeb2SJames Bottomley /** sas_port_alloc_num - allocate and initialize a SAS port structure
920c9fefeb2SJames Bottomley  *
921c9fefeb2SJames Bottomley  * @parent:	parent device
922c9fefeb2SJames Bottomley  *
923c9fefeb2SJames Bottomley  * Allocates a SAS port structure and a number to go with it.  This
924c9fefeb2SJames Bottomley  * interface is really for adapters where the port number has no
925c9fefeb2SJames Bottomley  * meansing, so the sas class should manage them.  It will be added to
926c9fefeb2SJames Bottomley  * the device tree below the device specified by @parent which must be
927c9fefeb2SJames Bottomley  * either a Scsi_Host or a sas_expander_device.
928c9fefeb2SJames Bottomley  *
929c9fefeb2SJames Bottomley  * Returns %NULL on error
930c9fefeb2SJames Bottomley  */
931c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *parent)
932c9fefeb2SJames Bottomley {
933c9fefeb2SJames Bottomley 	int index;
934c9fefeb2SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
935c9fefeb2SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
936c9fefeb2SJames Bottomley 
937c9fefeb2SJames Bottomley 	/* FIXME: use idr for this eventually */
938c9fefeb2SJames Bottomley 	mutex_lock(&sas_host->lock);
939c9fefeb2SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
940c9fefeb2SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
941c9fefeb2SJames Bottomley 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
942c9fefeb2SJames Bottomley 
943c9fefeb2SJames Bottomley 		index = exp->next_port_id++;
944c9fefeb2SJames Bottomley 	} else
945c9fefeb2SJames Bottomley 		index = sas_host->next_port_id++;
946c9fefeb2SJames Bottomley 	mutex_unlock(&sas_host->lock);
947c9fefeb2SJames Bottomley 	return sas_port_alloc(parent, index);
948c9fefeb2SJames Bottomley }
949c9fefeb2SJames Bottomley EXPORT_SYMBOL(sas_port_alloc_num);
950c9fefeb2SJames Bottomley 
95165c92b09SJames Bottomley /**
95265c92b09SJames Bottomley  * sas_port_add - add a SAS port to the device hierarchy
95365c92b09SJames Bottomley  * @port:	port to be added
95465c92b09SJames Bottomley  *
95565c92b09SJames Bottomley  * publishes a port to the rest of the system
95665c92b09SJames Bottomley  */
95765c92b09SJames Bottomley int sas_port_add(struct sas_port *port)
95865c92b09SJames Bottomley {
95965c92b09SJames Bottomley 	int error;
96065c92b09SJames Bottomley 
96165c92b09SJames Bottomley 	/* No phys should be added until this is made visible */
96265c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
96365c92b09SJames Bottomley 
96465c92b09SJames Bottomley 	error = device_add(&port->dev);
96565c92b09SJames Bottomley 
96665c92b09SJames Bottomley 	if (error)
96765c92b09SJames Bottomley 		return error;
96865c92b09SJames Bottomley 
96965c92b09SJames Bottomley 	transport_add_device(&port->dev);
97065c92b09SJames Bottomley 	transport_configure_device(&port->dev);
97165c92b09SJames Bottomley 
97265c92b09SJames Bottomley 	return 0;
97365c92b09SJames Bottomley }
97465c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add);
97565c92b09SJames Bottomley 
97665c92b09SJames Bottomley /**
977eb44820cSRob Landley  * sas_port_free  -  free a SAS PORT
97865c92b09SJames Bottomley  * @port:	SAS PORT to free
97965c92b09SJames Bottomley  *
98065c92b09SJames Bottomley  * Frees the specified SAS PORT.
98165c92b09SJames Bottomley  *
98265c92b09SJames Bottomley  * Note:
98365c92b09SJames Bottomley  *   This function must only be called on a PORT that has not
984af901ca1SAndré Goddard Rosa  *   successfully been added using sas_port_add().
98565c92b09SJames Bottomley  */
98665c92b09SJames Bottomley void sas_port_free(struct sas_port *port)
98765c92b09SJames Bottomley {
98865c92b09SJames Bottomley 	transport_destroy_device(&port->dev);
98965c92b09SJames Bottomley 	put_device(&port->dev);
99065c92b09SJames Bottomley }
99165c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_free);
99265c92b09SJames Bottomley 
99365c92b09SJames Bottomley /**
994eb44820cSRob Landley  * sas_port_delete  -  remove SAS PORT
99565c92b09SJames Bottomley  * @port:	SAS PORT to remove
99665c92b09SJames Bottomley  *
99765c92b09SJames Bottomley  * Removes the specified SAS PORT.  If the SAS PORT has an
99865c92b09SJames Bottomley  * associated phys, unlink them from the port as well.
99965c92b09SJames Bottomley  */
100065c92b09SJames Bottomley void sas_port_delete(struct sas_port *port)
100165c92b09SJames Bottomley {
100265c92b09SJames Bottomley 	struct device *dev = &port->dev;
100365c92b09SJames Bottomley 	struct sas_phy *phy, *tmp_phy;
100465c92b09SJames Bottomley 
100565c92b09SJames Bottomley 	if (port->rphy) {
100665c92b09SJames Bottomley 		sas_rphy_delete(port->rphy);
100765c92b09SJames Bottomley 		port->rphy = NULL;
100865c92b09SJames Bottomley 	}
100965c92b09SJames Bottomley 
101065c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
101165c92b09SJames Bottomley 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
101265c92b09SJames Bottomley 				 port_siblings) {
101365c92b09SJames Bottomley 		sas_port_delete_link(port, phy);
101465c92b09SJames Bottomley 		list_del_init(&phy->port_siblings);
101565c92b09SJames Bottomley 	}
101665c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
101765c92b09SJames Bottomley 
1018a0e1b6efSJames Bottomley 	if (port->is_backlink) {
1019a0e1b6efSJames Bottomley 		struct device *parent = port->dev.parent;
1020a0e1b6efSJames Bottomley 
102171610f55SKay Sievers 		sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1022a0e1b6efSJames Bottomley 		port->is_backlink = 0;
1023a0e1b6efSJames Bottomley 	}
1024a0e1b6efSJames Bottomley 
102565c92b09SJames Bottomley 	transport_remove_device(dev);
102665c92b09SJames Bottomley 	device_del(dev);
102765c92b09SJames Bottomley 	transport_destroy_device(dev);
102865c92b09SJames Bottomley 	put_device(dev);
102965c92b09SJames Bottomley }
103065c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete);
103165c92b09SJames Bottomley 
103265c92b09SJames Bottomley /**
1033eb44820cSRob Landley  * scsi_is_sas_port -  check if a struct device represents a SAS port
103465c92b09SJames Bottomley  * @dev:	device to check
103565c92b09SJames Bottomley  *
103665c92b09SJames Bottomley  * Returns:
103765c92b09SJames Bottomley  *	%1 if the device represents a SAS Port, %0 else
103865c92b09SJames Bottomley  */
103965c92b09SJames Bottomley int scsi_is_sas_port(const struct device *dev)
104065c92b09SJames Bottomley {
104165c92b09SJames Bottomley 	return dev->release == sas_port_release;
104265c92b09SJames Bottomley }
104365c92b09SJames Bottomley EXPORT_SYMBOL(scsi_is_sas_port);
104465c92b09SJames Bottomley 
104565c92b09SJames Bottomley /**
1046f41a0c44SDan Williams  * sas_port_get_phy - try to take a reference on a port member
1047f41a0c44SDan Williams  * @port: port to check
1048f41a0c44SDan Williams  */
1049f41a0c44SDan Williams struct sas_phy *sas_port_get_phy(struct sas_port *port)
1050f41a0c44SDan Williams {
1051f41a0c44SDan Williams 	struct sas_phy *phy;
1052f41a0c44SDan Williams 
1053f41a0c44SDan Williams 	mutex_lock(&port->phy_list_mutex);
1054f41a0c44SDan Williams 	if (list_empty(&port->phy_list))
1055f41a0c44SDan Williams 		phy = NULL;
1056f41a0c44SDan Williams 	else {
1057f41a0c44SDan Williams 		struct list_head *ent = port->phy_list.next;
1058f41a0c44SDan Williams 
1059f41a0c44SDan Williams 		phy = list_entry(ent, typeof(*phy), port_siblings);
1060f41a0c44SDan Williams 		get_device(&phy->dev);
1061f41a0c44SDan Williams 	}
1062f41a0c44SDan Williams 	mutex_unlock(&port->phy_list_mutex);
1063f41a0c44SDan Williams 
1064f41a0c44SDan Williams 	return phy;
1065f41a0c44SDan Williams }
1066f41a0c44SDan Williams EXPORT_SYMBOL(sas_port_get_phy);
1067f41a0c44SDan Williams 
1068f41a0c44SDan Williams /**
106965c92b09SJames Bottomley  * sas_port_add_phy - add another phy to a port to form a wide port
107065c92b09SJames Bottomley  * @port:	port to add the phy to
107165c92b09SJames Bottomley  * @phy:	phy to add
107265c92b09SJames Bottomley  *
107365c92b09SJames Bottomley  * When a port is initially created, it is empty (has no phys).  All
107465c92b09SJames Bottomley  * ports must have at least one phy to operated, and all wide ports
107565c92b09SJames Bottomley  * must have at least two.  The current code makes no difference
107665c92b09SJames Bottomley  * between ports and wide ports, but the only object that can be
107765c92b09SJames Bottomley  * connected to a remote device is a port, so ports must be formed on
107865c92b09SJames Bottomley  * all devices with phys if they're connected to anything.
107965c92b09SJames Bottomley  */
108065c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
108165c92b09SJames Bottomley {
108265c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
108365c92b09SJames Bottomley 	if (unlikely(!list_empty(&phy->port_siblings))) {
108465c92b09SJames Bottomley 		/* make sure we're already on this port */
108565c92b09SJames Bottomley 		struct sas_phy *tmp;
108665c92b09SJames Bottomley 
108765c92b09SJames Bottomley 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
108865c92b09SJames Bottomley 			if (tmp == phy)
108965c92b09SJames Bottomley 				break;
109065c92b09SJames Bottomley 		/* If this trips, you added a phy that was already
109165c92b09SJames Bottomley 		 * part of a different port */
109265c92b09SJames Bottomley 		if (unlikely(tmp != phy)) {
109371610f55SKay Sievers 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
109471610f55SKay Sievers 				   dev_name(&phy->dev));
109565c92b09SJames Bottomley 			BUG();
109665c92b09SJames Bottomley 		}
109765c92b09SJames Bottomley 	} else {
109865c92b09SJames Bottomley 		sas_port_create_link(port, phy);
109965c92b09SJames Bottomley 		list_add_tail(&phy->port_siblings, &port->phy_list);
110065c92b09SJames Bottomley 		port->num_phys++;
110165c92b09SJames Bottomley 	}
110265c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
110365c92b09SJames Bottomley }
110465c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add_phy);
110565c92b09SJames Bottomley 
110665c92b09SJames Bottomley /**
110765c92b09SJames Bottomley  * sas_port_delete_phy - remove a phy from a port or wide port
110865c92b09SJames Bottomley  * @port:	port to remove the phy from
110965c92b09SJames Bottomley  * @phy:	phy to remove
111065c92b09SJames Bottomley  *
111165c92b09SJames Bottomley  * This operation is used for tearing down ports again.  It must be
111265c92b09SJames Bottomley  * done to every port or wide port before calling sas_port_delete.
111365c92b09SJames Bottomley  */
111465c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
111565c92b09SJames Bottomley {
111665c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
111765c92b09SJames Bottomley 	sas_port_delete_link(port, phy);
111865c92b09SJames Bottomley 	list_del_init(&phy->port_siblings);
111965c92b09SJames Bottomley 	port->num_phys--;
112065c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
112165c92b09SJames Bottomley }
112265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete_phy);
112365c92b09SJames Bottomley 
1124a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *port)
1125a0e1b6efSJames Bottomley {
112621434966SDarrick J. Wong 	int res;
1127a0e1b6efSJames Bottomley 	struct device *parent = port->dev.parent->parent->parent;
1128a0e1b6efSJames Bottomley 
1129a0e1b6efSJames Bottomley 	if (port->is_backlink)
1130a0e1b6efSJames Bottomley 		return;
1131a0e1b6efSJames Bottomley 	port->is_backlink = 1;
113221434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
113371610f55SKay Sievers 				dev_name(parent));
113421434966SDarrick J. Wong 	if (res)
113521434966SDarrick J. Wong 		goto err;
113621434966SDarrick J. Wong 	return;
113721434966SDarrick J. Wong err:
113821434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1139cadbd4a5SHarvey Harrison 	       __func__, res);
1140a0e1b6efSJames Bottomley 
1141a0e1b6efSJames Bottomley }
1142a0e1b6efSJames Bottomley EXPORT_SYMBOL(sas_port_mark_backlink);
1143a0e1b6efSJames Bottomley 
114465c92b09SJames Bottomley /*
1145c7ebbbceSChristoph Hellwig  * SAS remote PHY attributes.
1146c7ebbbceSChristoph Hellwig  */
1147c7ebbbceSChristoph Hellwig 
1148c7ebbbceSChristoph Hellwig #define sas_rphy_show_simple(field, name, format_string, cast)		\
1149c7ebbbceSChristoph Hellwig static ssize_t								\
1150ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1151ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1152c7ebbbceSChristoph Hellwig {									\
1153ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1154c7ebbbceSChristoph Hellwig 									\
1155c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1156c7ebbbceSChristoph Hellwig }
1157c7ebbbceSChristoph Hellwig 
1158c7ebbbceSChristoph Hellwig #define sas_rphy_simple_attr(field, name, format_string, type)		\
1159c7ebbbceSChristoph Hellwig 	sas_rphy_show_simple(field, name, format_string, (type))	\
1160ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1161c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1162c7ebbbceSChristoph Hellwig 
1163c7ebbbceSChristoph Hellwig #define sas_rphy_show_protocol(field, name)				\
1164c7ebbbceSChristoph Hellwig static ssize_t								\
1165ee959b00STony Jones show_sas_rphy_##name(struct device *dev, 				\
1166ee959b00STony Jones 		     struct device_attribute *attr, char *buf)		\
1167c7ebbbceSChristoph Hellwig {									\
1168ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
1169c7ebbbceSChristoph Hellwig 									\
1170c7ebbbceSChristoph Hellwig 	if (!rphy->field)					\
1171c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
1172c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(rphy->field, buf);	\
1173c7ebbbceSChristoph Hellwig }
1174c7ebbbceSChristoph Hellwig 
1175c7ebbbceSChristoph Hellwig #define sas_rphy_protocol_attr(field, name)				\
1176c7ebbbceSChristoph Hellwig 	sas_rphy_show_protocol(field, name)				\
1177ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1178c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1179c7ebbbceSChristoph Hellwig 
1180c7ebbbceSChristoph Hellwig static ssize_t
1181ee959b00STony Jones show_sas_rphy_device_type(struct device *dev,
1182ee959b00STony Jones 			  struct device_attribute *attr, char *buf)
1183c7ebbbceSChristoph Hellwig {
1184ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1185c7ebbbceSChristoph Hellwig 
1186c7ebbbceSChristoph Hellwig 	if (!rphy->identify.device_type)
1187c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
1188c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(
1189c7ebbbceSChristoph Hellwig 			rphy->identify.device_type, buf);
1190c7ebbbceSChristoph Hellwig }
1191c7ebbbceSChristoph Hellwig 
1192ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1193c7ebbbceSChristoph Hellwig 		show_sas_rphy_device_type, NULL);
1194c7ebbbceSChristoph Hellwig 
1195a0125641SChristoph Hellwig static ssize_t
1196ee959b00STony Jones show_sas_rphy_enclosure_identifier(struct device *dev,
1197ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
1198a0125641SChristoph Hellwig {
1199ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1200a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1201a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1202a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1203a0125641SChristoph Hellwig 	u64 identifier;
1204a0125641SChristoph Hellwig 	int error;
1205a0125641SChristoph Hellwig 
1206a0125641SChristoph Hellwig 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1207a0125641SChristoph Hellwig 	if (error)
1208a0125641SChristoph Hellwig 		return error;
1209a0125641SChristoph Hellwig 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1210a0125641SChristoph Hellwig }
1211a0125641SChristoph Hellwig 
1212ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1213a0125641SChristoph Hellwig 		show_sas_rphy_enclosure_identifier, NULL);
1214a0125641SChristoph Hellwig 
1215a0125641SChristoph Hellwig static ssize_t
1216ee959b00STony Jones show_sas_rphy_bay_identifier(struct device *dev,
1217ee959b00STony Jones 			     struct device_attribute *attr, char *buf)
1218a0125641SChristoph Hellwig {
1219ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);
1220a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1221a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1222a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1223a0125641SChristoph Hellwig 	int val;
1224a0125641SChristoph Hellwig 
1225a0125641SChristoph Hellwig 	val = i->f->get_bay_identifier(rphy);
1226a0125641SChristoph Hellwig 	if (val < 0)
1227a0125641SChristoph Hellwig 		return val;
1228a0125641SChristoph Hellwig 	return sprintf(buf, "%d\n", val);
1229a0125641SChristoph Hellwig }
1230a0125641SChristoph Hellwig 
1231ee959b00STony Jones static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1232a0125641SChristoph Hellwig 		show_sas_rphy_bay_identifier, NULL);
1233a0125641SChristoph Hellwig 
1234c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.initiator_port_protocols,
1235c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
1236c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1237c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1238c7ebbbceSChristoph Hellwig 		unsigned long long);
1239c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1240cdc43ae3SHannes Reinecke sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
1241c7ebbbceSChristoph Hellwig 
124242ab0360SJames Bottomley /* only need 8 bytes of data plus header (4 or 8) */
124342ab0360SJames Bottomley #define BUF_SIZE 64
124442ab0360SJames Bottomley 
124542ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *sdev)
124642ab0360SJames Bottomley {
124742ab0360SJames Bottomley 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
12480f88009dSJames Bottomley 	struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
124942ab0360SJames Bottomley 	struct scsi_mode_data mode_data;
125042ab0360SJames Bottomley 	int res, error;
125142ab0360SJames Bottomley 
125242ab0360SJames Bottomley 	if (!buffer)
125342ab0360SJames Bottomley 		return -ENOMEM;
125442ab0360SJames Bottomley 
125542ab0360SJames Bottomley 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
125642ab0360SJames Bottomley 			      &mode_data, NULL);
125742ab0360SJames Bottomley 
125842ab0360SJames Bottomley 	error = -EINVAL;
125942ab0360SJames Bottomley 	if (!scsi_status_is_good(res))
126042ab0360SJames Bottomley 		goto out;
126142ab0360SJames Bottomley 
126242ab0360SJames Bottomley 	msdata = buffer +  mode_data.header_length +
126342ab0360SJames Bottomley 		mode_data.block_descriptor_length;
126442ab0360SJames Bottomley 
126542ab0360SJames Bottomley 	if (msdata - buffer > BUF_SIZE - 8)
126642ab0360SJames Bottomley 		goto out;
126742ab0360SJames Bottomley 
126842ab0360SJames Bottomley 	error = 0;
126942ab0360SJames Bottomley 
127042ab0360SJames Bottomley 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
127142ab0360SJames Bottomley 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
127242ab0360SJames Bottomley 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
127342ab0360SJames Bottomley 
127442ab0360SJames Bottomley  out:
127542ab0360SJames Bottomley 	kfree(buffer);
127642ab0360SJames Bottomley 	return error;
127742ab0360SJames Bottomley }
127842ab0360SJames Bottomley EXPORT_SYMBOL(sas_read_port_mode_page);
127942ab0360SJames Bottomley 
128079cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
128179cb1819SJames Bottomley 			       "sas_end_device", NULL, NULL, NULL);
128279cb1819SJames Bottomley 
128342ab0360SJames Bottomley #define sas_end_dev_show_simple(field, name, format_string, cast)	\
128442ab0360SJames Bottomley static ssize_t								\
1285ee959b00STony Jones show_sas_end_dev_##name(struct device *dev, 				\
1286ee959b00STony Jones 			struct device_attribute *attr, char *buf)	\
128742ab0360SJames Bottomley {									\
1288ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
128942ab0360SJames Bottomley 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
129042ab0360SJames Bottomley 									\
129142ab0360SJames Bottomley 	return snprintf(buf, 20, format_string, cast rdev->field);	\
129242ab0360SJames Bottomley }
129342ab0360SJames Bottomley 
129442ab0360SJames Bottomley #define sas_end_dev_simple_attr(field, name, format_string, type)	\
129542ab0360SJames Bottomley 	sas_end_dev_show_simple(field, name, format_string, (type))	\
1296ee959b00STony Jones static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
129742ab0360SJames Bottomley 		show_sas_end_dev_##name, NULL)
129842ab0360SJames Bottomley 
129942ab0360SJames Bottomley sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
130042ab0360SJames Bottomley sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
130142ab0360SJames Bottomley 			"%d\n", int);
130242ab0360SJames Bottomley sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
130342ab0360SJames Bottomley 			"%d\n", int);
13040f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_supported, tlr_supported,
13050f88009dSJames Bottomley 			"%d\n", int);
13060f88009dSJames Bottomley sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
13070f88009dSJames Bottomley 			"%d\n", int);
130842ab0360SJames Bottomley 
130979cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_expander_class,
131079cb1819SJames Bottomley 			       "sas_expander", NULL, NULL, NULL);
131179cb1819SJames Bottomley 
131279cb1819SJames Bottomley #define sas_expander_show_simple(field, name, format_string, cast)	\
131379cb1819SJames Bottomley static ssize_t								\
1314ee959b00STony Jones show_sas_expander_##name(struct device *dev, 				\
1315ee959b00STony Jones 			 struct device_attribute *attr, char *buf)	\
131679cb1819SJames Bottomley {									\
1317ee959b00STony Jones 	struct sas_rphy *rphy = transport_class_to_rphy(dev);		\
131879cb1819SJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
131979cb1819SJames Bottomley 									\
132079cb1819SJames Bottomley 	return snprintf(buf, 20, format_string, cast edev->field);	\
132179cb1819SJames Bottomley }
132279cb1819SJames Bottomley 
132379cb1819SJames Bottomley #define sas_expander_simple_attr(field, name, format_string, type)	\
132479cb1819SJames Bottomley 	sas_expander_show_simple(field, name, format_string, (type))	\
1325ee959b00STony Jones static SAS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
132679cb1819SJames Bottomley 		show_sas_expander_##name, NULL)
132779cb1819SJames Bottomley 
132879cb1819SJames Bottomley sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
132979cb1819SJames Bottomley sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
133079cb1819SJames Bottomley sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
133179cb1819SJames Bottomley sas_expander_simple_attr(component_vendor_id, component_vendor_id,
133279cb1819SJames Bottomley 			 "%s\n", char *);
133379cb1819SJames Bottomley sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
133479cb1819SJames Bottomley sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
133579cb1819SJames Bottomley 			 unsigned int);
133679cb1819SJames Bottomley sas_expander_simple_attr(level, level, "%d\n", int);
133742ab0360SJames Bottomley 
1338c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
13392f8600dfSJames Bottomley 		"sas_device", NULL, NULL, NULL);
1340c7ebbbceSChristoph Hellwig 
1341c7ebbbceSChristoph Hellwig static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1342c7ebbbceSChristoph Hellwig {
1343c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
1344c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1345c7ebbbceSChristoph Hellwig 
1346c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_rphy(dev))
1347c7ebbbceSChristoph Hellwig 		return 0;
1348c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent->parent);
1349c7ebbbceSChristoph Hellwig 
1350c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
1351c7ebbbceSChristoph Hellwig 		return 0;
1352c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
1353c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
1354c7ebbbceSChristoph Hellwig 		return 0;
1355c7ebbbceSChristoph Hellwig 
1356c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
1357c7ebbbceSChristoph Hellwig 	return &i->rphy_attr_cont.ac == cont;
1358c7ebbbceSChristoph Hellwig }
1359c7ebbbceSChristoph Hellwig 
136042ab0360SJames Bottomley static int sas_end_dev_match(struct attribute_container *cont,
136142ab0360SJames Bottomley 			     struct device *dev)
136242ab0360SJames Bottomley {
136342ab0360SJames Bottomley 	struct Scsi_Host *shost;
136442ab0360SJames Bottomley 	struct sas_internal *i;
136542ab0360SJames Bottomley 	struct sas_rphy *rphy;
136642ab0360SJames Bottomley 
136742ab0360SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
136842ab0360SJames Bottomley 		return 0;
136942ab0360SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
137042ab0360SJames Bottomley 	rphy = dev_to_rphy(dev);
137142ab0360SJames Bottomley 
137242ab0360SJames Bottomley 	if (!shost->transportt)
137342ab0360SJames Bottomley 		return 0;
137442ab0360SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
137542ab0360SJames Bottomley 			&sas_host_class.class)
137642ab0360SJames Bottomley 		return 0;
137742ab0360SJames Bottomley 
137842ab0360SJames Bottomley 	i = to_sas_internal(shost->transportt);
137942ab0360SJames Bottomley 	return &i->end_dev_attr_cont.ac == cont &&
13802f8600dfSJames Bottomley 		rphy->identify.device_type == SAS_END_DEVICE;
138142ab0360SJames Bottomley }
138242ab0360SJames Bottomley 
138379cb1819SJames Bottomley static int sas_expander_match(struct attribute_container *cont,
138479cb1819SJames Bottomley 			      struct device *dev)
138579cb1819SJames Bottomley {
138679cb1819SJames Bottomley 	struct Scsi_Host *shost;
138779cb1819SJames Bottomley 	struct sas_internal *i;
138879cb1819SJames Bottomley 	struct sas_rphy *rphy;
138979cb1819SJames Bottomley 
139079cb1819SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
139179cb1819SJames Bottomley 		return 0;
139279cb1819SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
139379cb1819SJames Bottomley 	rphy = dev_to_rphy(dev);
139479cb1819SJames Bottomley 
139579cb1819SJames Bottomley 	if (!shost->transportt)
139679cb1819SJames Bottomley 		return 0;
139779cb1819SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
139879cb1819SJames Bottomley 			&sas_host_class.class)
139979cb1819SJames Bottomley 		return 0;
140079cb1819SJames Bottomley 
140179cb1819SJames Bottomley 	i = to_sas_internal(shost->transportt);
140279cb1819SJames Bottomley 	return &i->expander_attr_cont.ac == cont &&
140379cb1819SJames Bottomley 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
14042f8600dfSJames Bottomley 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
140579cb1819SJames Bottomley }
140679cb1819SJames Bottomley 
14072f8600dfSJames Bottomley static void sas_expander_release(struct device *dev)
1408c7ebbbceSChristoph Hellwig {
1409c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = dev_to_rphy(dev);
14102f8600dfSJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1411c7ebbbceSChristoph Hellwig 
141293c20a59SFUJITA Tomonori 	if (rphy->q)
141393c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
141493c20a59SFUJITA Tomonori 
1415c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
14162f8600dfSJames Bottomley 	kfree(edev);
1417c7ebbbceSChristoph Hellwig }
1418c7ebbbceSChristoph Hellwig 
14192f8600dfSJames Bottomley static void sas_end_device_release(struct device *dev)
1420c7ebbbceSChristoph Hellwig {
14212f8600dfSJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(dev);
14222f8600dfSJames Bottomley 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1423c7ebbbceSChristoph Hellwig 
142493c20a59SFUJITA Tomonori 	if (rphy->q)
142593c20a59SFUJITA Tomonori 		blk_cleanup_queue(rphy->q);
142693c20a59SFUJITA Tomonori 
14272f8600dfSJames Bottomley 	put_device(dev->parent);
14282f8600dfSJames Bottomley 	kfree(edev);
1429c7ebbbceSChristoph Hellwig }
1430c7ebbbceSChristoph Hellwig 
1431c7ebbbceSChristoph Hellwig /**
1432183b8021SMasahiro Yamada  * sas_rphy_initialize - common rphy initialization
1433c5943d36SJames Bottomley  * @rphy:	rphy to initialise
1434c5943d36SJames Bottomley  *
1435c5943d36SJames Bottomley  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1436c5943d36SJames Bottomley  * initialise the common rphy component of each.
1437c5943d36SJames Bottomley  */
1438c5943d36SJames Bottomley static void sas_rphy_initialize(struct sas_rphy *rphy)
1439c5943d36SJames Bottomley {
1440c5943d36SJames Bottomley 	INIT_LIST_HEAD(&rphy->list);
1441c5943d36SJames Bottomley }
1442c5943d36SJames Bottomley 
1443c5943d36SJames Bottomley /**
144442ab0360SJames Bottomley  * sas_end_device_alloc - allocate an rphy for an end device
1445eb44820cSRob Landley  * @parent: which port
144642ab0360SJames Bottomley  *
144742ab0360SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
144842ab0360SJames Bottomley  *
144942ab0360SJames Bottomley  * Returns:
145042ab0360SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
145142ab0360SJames Bottomley  */
145265c92b09SJames Bottomley struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
145342ab0360SJames Bottomley {
145442ab0360SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
145542ab0360SJames Bottomley 	struct sas_end_device *rdev;
145642ab0360SJames Bottomley 
145742ab0360SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
145842ab0360SJames Bottomley 	if (!rdev) {
145942ab0360SJames Bottomley 		return NULL;
146042ab0360SJames Bottomley 	}
146142ab0360SJames Bottomley 
146242ab0360SJames Bottomley 	device_initialize(&rdev->rphy.dev);
146342ab0360SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
14642f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_end_device_release;
146565c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
146665c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
146771610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
146871610f55SKay Sievers 			     shost->host_no, rphy->scsi_target_id,
146971610f55SKay Sievers 			     parent->port_identifier);
147065c92b09SJames Bottomley 	} else
147171610f55SKay Sievers 		dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
147265c92b09SJames Bottomley 			     shost->host_no, parent->port_identifier);
147342ab0360SJames Bottomley 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1474c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
147542ab0360SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
147642ab0360SJames Bottomley 
147742ab0360SJames Bottomley 	return &rdev->rphy;
147842ab0360SJames Bottomley }
147942ab0360SJames Bottomley EXPORT_SYMBOL(sas_end_device_alloc);
148042ab0360SJames Bottomley 
148179cb1819SJames Bottomley /**
148279cb1819SJames Bottomley  * sas_expander_alloc - allocate an rphy for an end device
1483eb44820cSRob Landley  * @parent: which port
1484eb44820cSRob Landley  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
148579cb1819SJames Bottomley  *
148679cb1819SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
148779cb1819SJames Bottomley  *
148879cb1819SJames Bottomley  * Returns:
148979cb1819SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
149079cb1819SJames Bottomley  */
149165c92b09SJames Bottomley struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
149279cb1819SJames Bottomley 				    enum sas_device_type type)
149379cb1819SJames Bottomley {
149479cb1819SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
149579cb1819SJames Bottomley 	struct sas_expander_device *rdev;
149679cb1819SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
149779cb1819SJames Bottomley 
149879cb1819SJames Bottomley 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
149979cb1819SJames Bottomley 	       type != SAS_FANOUT_EXPANDER_DEVICE);
150079cb1819SJames Bottomley 
150179cb1819SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
150279cb1819SJames Bottomley 	if (!rdev) {
150379cb1819SJames Bottomley 		return NULL;
150479cb1819SJames Bottomley 	}
150579cb1819SJames Bottomley 
150679cb1819SJames Bottomley 	device_initialize(&rdev->rphy.dev);
150779cb1819SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
15082f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_expander_release;
150979cb1819SJames Bottomley 	mutex_lock(&sas_host->lock);
151079cb1819SJames Bottomley 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
151179cb1819SJames Bottomley 	mutex_unlock(&sas_host->lock);
151271610f55SKay Sievers 	dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
151379cb1819SJames Bottomley 		     shost->host_no, rdev->rphy.scsi_target_id);
151479cb1819SJames Bottomley 	rdev->rphy.identify.device_type = type;
1515c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
151679cb1819SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
151779cb1819SJames Bottomley 
151879cb1819SJames Bottomley 	return &rdev->rphy;
151979cb1819SJames Bottomley }
152079cb1819SJames Bottomley EXPORT_SYMBOL(sas_expander_alloc);
152142ab0360SJames Bottomley 
152242ab0360SJames Bottomley /**
1523eb44820cSRob Landley  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1524c7ebbbceSChristoph Hellwig  * @rphy:	The remote PHY to be added
1525c7ebbbceSChristoph Hellwig  *
1526c7ebbbceSChristoph Hellwig  * Publishes a SAS remote PHY to the rest of the system.
1527c7ebbbceSChristoph Hellwig  */
1528c7ebbbceSChristoph Hellwig int sas_rphy_add(struct sas_rphy *rphy)
1529c7ebbbceSChristoph Hellwig {
153065c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1531c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1532c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1533c7ebbbceSChristoph Hellwig 	struct sas_identify *identify = &rphy->identify;
1534c7ebbbceSChristoph Hellwig 	int error;
1535c7ebbbceSChristoph Hellwig 
1536c7ebbbceSChristoph Hellwig 	if (parent->rphy)
1537c7ebbbceSChristoph Hellwig 		return -ENXIO;
1538c7ebbbceSChristoph Hellwig 	parent->rphy = rphy;
1539c7ebbbceSChristoph Hellwig 
1540c7ebbbceSChristoph Hellwig 	error = device_add(&rphy->dev);
1541c7ebbbceSChristoph Hellwig 	if (error)
1542c7ebbbceSChristoph Hellwig 		return error;
1543c7ebbbceSChristoph Hellwig 	transport_add_device(&rphy->dev);
1544c7ebbbceSChristoph Hellwig 	transport_configure_device(&rphy->dev);
154539dca558SJames Bottomley 	if (sas_bsg_initialize(shost, rphy))
154671610f55SKay Sievers 		printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
154739dca558SJames Bottomley 
1548c7ebbbceSChristoph Hellwig 
1549e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1550c7ebbbceSChristoph Hellwig 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1551c7ebbbceSChristoph Hellwig 	if (identify->device_type == SAS_END_DEVICE &&
1552c7ebbbceSChristoph Hellwig 	    (identify->target_port_protocols &
1553c7ebbbceSChristoph Hellwig 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1554c7ebbbceSChristoph Hellwig 		rphy->scsi_target_id = sas_host->next_target_id++;
15557676f83aSJames Bottomley 	else if (identify->device_type == SAS_END_DEVICE)
15567676f83aSJames Bottomley 		rphy->scsi_target_id = -1;
1557e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1558c7ebbbceSChristoph Hellwig 
155979cb1819SJames Bottomley 	if (identify->device_type == SAS_END_DEVICE &&
156079cb1819SJames Bottomley 	    rphy->scsi_target_id != -1) {
15612fc62e2aSDan Williams 		int lun;
15622fc62e2aSDan Williams 
15632fc62e2aSDan Williams 		if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
15642fc62e2aSDan Williams 			lun = SCAN_WILD_CARD;
15652fc62e2aSDan Williams 		else
15662fc62e2aSDan Williams 			lun = 0;
15672fc62e2aSDan Williams 
15681d645088SHannes Reinecke 		scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
15691d645088SHannes Reinecke 				 SCSI_SCAN_INITIAL);
1570c7ebbbceSChristoph Hellwig 	}
1571c7ebbbceSChristoph Hellwig 
1572c7ebbbceSChristoph Hellwig 	return 0;
1573c7ebbbceSChristoph Hellwig }
1574c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_add);
1575c7ebbbceSChristoph Hellwig 
1576c7ebbbceSChristoph Hellwig /**
1577eb44820cSRob Landley  * sas_rphy_free  -  free a SAS remote PHY
1578eb44820cSRob Landley  * @rphy: SAS remote PHY to free
1579c7ebbbceSChristoph Hellwig  *
1580c7ebbbceSChristoph Hellwig  * Frees the specified SAS remote PHY.
1581c7ebbbceSChristoph Hellwig  *
1582c7ebbbceSChristoph Hellwig  * Note:
1583c7ebbbceSChristoph Hellwig  *   This function must only be called on a remote
1584af901ca1SAndré Goddard Rosa  *   PHY that has not successfully been added using
15856f63caaeSDarrick J. Wong  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1586c7ebbbceSChristoph Hellwig  */
1587c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *rphy)
1588c7ebbbceSChristoph Hellwig {
158992aab646SMike Anderson 	struct device *dev = &rphy->dev;
1590c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1591c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1592c7ebbbceSChristoph Hellwig 
1593e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1594c7ebbbceSChristoph Hellwig 	list_del(&rphy->list);
1595e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1596c7ebbbceSChristoph Hellwig 
159792aab646SMike Anderson 	transport_destroy_device(dev);
15982f8600dfSJames Bottomley 
159992aab646SMike Anderson 	put_device(dev);
1600c7ebbbceSChristoph Hellwig }
1601c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_free);
1602c7ebbbceSChristoph Hellwig 
1603c7ebbbceSChristoph Hellwig /**
1604eb44820cSRob Landley  * sas_rphy_delete  -  remove and free SAS remote PHY
16056f63caaeSDarrick J. Wong  * @rphy:	SAS remote PHY to remove and free
1606c7ebbbceSChristoph Hellwig  *
16076f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY and frees it.
1608c7ebbbceSChristoph Hellwig  */
1609c7ebbbceSChristoph Hellwig void
1610c7ebbbceSChristoph Hellwig sas_rphy_delete(struct sas_rphy *rphy)
1611c7ebbbceSChristoph Hellwig {
16126f63caaeSDarrick J. Wong 	sas_rphy_remove(rphy);
16136f63caaeSDarrick J. Wong 	sas_rphy_free(rphy);
16146f63caaeSDarrick J. Wong }
16156f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_delete);
16166f63caaeSDarrick J. Wong 
16176f63caaeSDarrick J. Wong /**
161887c8331fSDan Williams  * sas_rphy_unlink  -  unlink SAS remote PHY
161987c8331fSDan Williams  * @rphy:	SAS remote phy to unlink from its parent port
162087c8331fSDan Williams  *
162187c8331fSDan Williams  * Removes port reference to an rphy
162287c8331fSDan Williams  */
162387c8331fSDan Williams void sas_rphy_unlink(struct sas_rphy *rphy)
162487c8331fSDan Williams {
162587c8331fSDan Williams 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
162687c8331fSDan Williams 
162787c8331fSDan Williams 	parent->rphy = NULL;
162887c8331fSDan Williams }
162987c8331fSDan Williams EXPORT_SYMBOL(sas_rphy_unlink);
163087c8331fSDan Williams 
163187c8331fSDan Williams /**
1632eb44820cSRob Landley  * sas_rphy_remove  -  remove SAS remote PHY
16336f63caaeSDarrick J. Wong  * @rphy:	SAS remote phy to remove
16346f63caaeSDarrick J. Wong  *
16356f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY.
16366f63caaeSDarrick J. Wong  */
16376f63caaeSDarrick J. Wong void
16386f63caaeSDarrick J. Wong sas_rphy_remove(struct sas_rphy *rphy)
16396f63caaeSDarrick J. Wong {
1640c7ebbbceSChristoph Hellwig 	struct device *dev = &rphy->dev;
1641c7ebbbceSChristoph Hellwig 
1642d4054239SChristoph Hellwig 	switch (rphy->identify.device_type) {
1643d4054239SChristoph Hellwig 	case SAS_END_DEVICE:
1644fe8b2304SChristoph Hellwig 		scsi_remove_target(dev);
1645d4054239SChristoph Hellwig 		break;
1646d4054239SChristoph Hellwig 	case SAS_EDGE_EXPANDER_DEVICE:
1647d4054239SChristoph Hellwig 	case SAS_FANOUT_EXPANDER_DEVICE:
164865c92b09SJames Bottomley 		sas_remove_children(dev);
1649d4054239SChristoph Hellwig 		break;
1650d4054239SChristoph Hellwig 	default:
1651d4054239SChristoph Hellwig 		break;
1652d4054239SChristoph Hellwig 	}
1653c7ebbbceSChristoph Hellwig 
165487c8331fSDan Williams 	sas_rphy_unlink(rphy);
1655651a0136SChristoph Hellwig 	if (rphy->q)
1656651a0136SChristoph Hellwig 		bsg_unregister_queue(rphy->q);
1657fe8b2304SChristoph Hellwig 	transport_remove_device(dev);
1658fe8b2304SChristoph Hellwig 	device_del(dev);
1659c7ebbbceSChristoph Hellwig }
16606f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_remove);
1661c7ebbbceSChristoph Hellwig 
1662c7ebbbceSChristoph Hellwig /**
1663eb44820cSRob Landley  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1664c7ebbbceSChristoph Hellwig  * @dev:	device to check
1665c7ebbbceSChristoph Hellwig  *
1666c7ebbbceSChristoph Hellwig  * Returns:
1667c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS remote PHY, %0 else
1668c7ebbbceSChristoph Hellwig  */
1669c7ebbbceSChristoph Hellwig int scsi_is_sas_rphy(const struct device *dev)
1670c7ebbbceSChristoph Hellwig {
16712f8600dfSJames Bottomley 	return dev->release == sas_end_device_release ||
16722f8600dfSJames Bottomley 		dev->release == sas_expander_release;
1673c7ebbbceSChristoph Hellwig }
1674c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_rphy);
1675c7ebbbceSChristoph Hellwig 
1676c7ebbbceSChristoph Hellwig 
1677c7ebbbceSChristoph Hellwig /*
1678c7ebbbceSChristoph Hellwig  * SCSI scan helper
1679c7ebbbceSChristoph Hellwig  */
1680c7ebbbceSChristoph Hellwig 
1681e02f3f59SChristoph Hellwig static int sas_user_scan(struct Scsi_Host *shost, uint channel,
16829cb78c16SHannes Reinecke 		uint id, u64 lun)
1683c7ebbbceSChristoph Hellwig {
1684c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1685c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy;
1686c7ebbbceSChristoph Hellwig 
1687e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1688c7ebbbceSChristoph Hellwig 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
16896d99a3f3SJames Bottomley 		if (rphy->identify.device_type != SAS_END_DEVICE ||
16906d99a3f3SJames Bottomley 		    rphy->scsi_target_id == -1)
1691e02f3f59SChristoph Hellwig 			continue;
1692e02f3f59SChristoph Hellwig 
1693e8bf3941SJames Bottomley 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1694e02f3f59SChristoph Hellwig 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
16951d645088SHannes Reinecke 			scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
16961d645088SHannes Reinecke 					 lun, SCSI_SCAN_MANUAL);
1697e02f3f59SChristoph Hellwig 		}
1698e02f3f59SChristoph Hellwig 	}
1699e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1700e02f3f59SChristoph Hellwig 
1701e02f3f59SChristoph Hellwig 	return 0;
1702c7ebbbceSChristoph Hellwig }
1703c7ebbbceSChristoph Hellwig 
1704c7ebbbceSChristoph Hellwig 
1705c7ebbbceSChristoph Hellwig /*
1706c7ebbbceSChristoph Hellwig  * Setup / Teardown code
1707c7ebbbceSChristoph Hellwig  */
1708c7ebbbceSChristoph Hellwig 
170942ab0360SJames Bottomley #define SETUP_TEMPLATE(attrb, field, perm, test)			\
1710ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
171142ab0360SJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
171242ab0360SJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
171342ab0360SJames Bottomley 	if (test)							\
1714c7ebbbceSChristoph Hellwig 		count++
1715c7ebbbceSChristoph Hellwig 
1716d24e1eebSJames Bottomley #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1717ee959b00STony Jones 	i->private_##attrb[count] = dev_attr_##field;		\
1718d24e1eebSJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
1719d24e1eebSJames Bottomley 	if (ro_test) {							\
1720d24e1eebSJames Bottomley 		i->private_##attrb[count].attr.mode = ro_perm;		\
1721d24e1eebSJames Bottomley 		i->private_##attrb[count].store = NULL;			\
1722d24e1eebSJames Bottomley 	}								\
1723d24e1eebSJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
1724d24e1eebSJames Bottomley 	if (test)							\
1725d24e1eebSJames Bottomley 		count++
172642ab0360SJames Bottomley 
172742ab0360SJames Bottomley #define SETUP_RPORT_ATTRIBUTE(field) 					\
172842ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
172942ab0360SJames Bottomley 
1730dd9fbb52SJames Bottomley #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
173142ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1732dd9fbb52SJames Bottomley 
173365c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE(field)					\
173442ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1735c7ebbbceSChristoph Hellwig 
1736d24e1eebSJames Bottomley #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1737d24e1eebSJames Bottomley 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1738d24e1eebSJames Bottomley 			!i->f->set_phy_speed, S_IRUGO)
1739d24e1eebSJames Bottomley 
1740acbf167dSDarrick J. Wong #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1741acbf167dSDarrick J. Wong 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1742acbf167dSDarrick J. Wong 			  !i->f->func, S_IRUGO)
1743acbf167dSDarrick J. Wong 
174465c92b09SJames Bottomley #define SETUP_PORT_ATTRIBUTE(field)					\
174565c92b09SJames Bottomley 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
174665c92b09SJames Bottomley 
174765c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
174842ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1749dd9fbb52SJames Bottomley 
175065c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1751fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
175207ba3a95SChristoph Hellwig 
175365c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1754fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1755dd9fbb52SJames Bottomley 
175642ab0360SJames Bottomley #define SETUP_END_DEV_ATTRIBUTE(field)					\
175742ab0360SJames Bottomley 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1758c7ebbbceSChristoph Hellwig 
175979cb1819SJames Bottomley #define SETUP_EXPANDER_ATTRIBUTE(field)					\
176079cb1819SJames Bottomley 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
176179cb1819SJames Bottomley 
1762c7ebbbceSChristoph Hellwig /**
1763eb44820cSRob Landley  * sas_attach_transport  -  instantiate SAS transport template
1764c7ebbbceSChristoph Hellwig  * @ft:		SAS transport class function template
1765c7ebbbceSChristoph Hellwig  */
1766c7ebbbceSChristoph Hellwig struct scsi_transport_template *
1767c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *ft)
1768c7ebbbceSChristoph Hellwig {
1769c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1770c7ebbbceSChristoph Hellwig 	int count;
1771c7ebbbceSChristoph Hellwig 
177224669f75SJes Sorensen 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1773c7ebbbceSChristoph Hellwig 	if (!i)
1774c7ebbbceSChristoph Hellwig 		return NULL;
1775c7ebbbceSChristoph Hellwig 
1776e02f3f59SChristoph Hellwig 	i->t.user_scan = sas_user_scan;
1777c7ebbbceSChristoph Hellwig 
1778c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1779c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.class = &sas_host_class.class;
1780c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.match = sas_host_match;
1781c7ebbbceSChristoph Hellwig 	transport_container_register(&i->t.host_attrs);
1782c7ebbbceSChristoph Hellwig 	i->t.host_size = sizeof(struct sas_host_attrs);
1783c7ebbbceSChristoph Hellwig 
1784c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1785c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1786c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.match = sas_phy_match;
1787c7ebbbceSChristoph Hellwig 	transport_container_register(&i->phy_attr_cont);
1788c7ebbbceSChristoph Hellwig 
178965c92b09SJames Bottomley 	i->port_attr_cont.ac.class = &sas_port_class.class;
179065c92b09SJames Bottomley 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
179165c92b09SJames Bottomley 	i->port_attr_cont.ac.match = sas_port_match;
179265c92b09SJames Bottomley 	transport_container_register(&i->port_attr_cont);
179365c92b09SJames Bottomley 
1794c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1795c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1796c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1797c7ebbbceSChristoph Hellwig 	transport_container_register(&i->rphy_attr_cont);
1798c7ebbbceSChristoph Hellwig 
179942ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
180042ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
180142ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
180242ab0360SJames Bottomley 	transport_container_register(&i->end_dev_attr_cont);
180342ab0360SJames Bottomley 
180479cb1819SJames Bottomley 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
180579cb1819SJames Bottomley 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
180679cb1819SJames Bottomley 	i->expander_attr_cont.ac.match = sas_expander_match;
180779cb1819SJames Bottomley 	transport_container_register(&i->expander_attr_cont);
180879cb1819SJames Bottomley 
1809c7ebbbceSChristoph Hellwig 	i->f = ft;
1810c7ebbbceSChristoph Hellwig 
1811c7ebbbceSChristoph Hellwig 	count = 0;
181265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
181365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
181465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(device_type);
181565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(sas_address);
181665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_identifier);
181765c92b09SJames Bottomley 	//SETUP_PHY_ATTRIBUTE(port_identifier);
181865c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
181965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1820d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
182165c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1822d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1823c3ee74c4SChristoph Hellwig 
182465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
182565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
182665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
182765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
182865c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
182965c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1830acbf167dSDarrick J. Wong 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1831c7ebbbceSChristoph Hellwig 	i->phy_attrs[count] = NULL;
1832c7ebbbceSChristoph Hellwig 
1833c7ebbbceSChristoph Hellwig 	count = 0;
183465c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
183565c92b09SJames Bottomley 	i->port_attrs[count] = NULL;
183665c92b09SJames Bottomley 
183765c92b09SJames Bottomley 	count = 0;
1838c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1839c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1840c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1841c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1842c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1843cdc43ae3SHannes Reinecke 	SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
1844dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1845dd9fbb52SJames Bottomley 				       get_enclosure_identifier);
1846dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1847dd9fbb52SJames Bottomley 				       get_bay_identifier);
1848c7ebbbceSChristoph Hellwig 	i->rphy_attrs[count] = NULL;
1849c7ebbbceSChristoph Hellwig 
185042ab0360SJames Bottomley 	count = 0;
185142ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
185242ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
185342ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
18540f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
18550f88009dSJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
185642ab0360SJames Bottomley 	i->end_dev_attrs[count] = NULL;
185742ab0360SJames Bottomley 
185879cb1819SJames Bottomley 	count = 0;
185979cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
186079cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_id);
186179cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
186279cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
186379cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_id);
186479cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
186579cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(level);
186679cb1819SJames Bottomley 	i->expander_attrs[count] = NULL;
186779cb1819SJames Bottomley 
1868c7ebbbceSChristoph Hellwig 	return &i->t;
1869c7ebbbceSChristoph Hellwig }
1870c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_attach_transport);
1871c7ebbbceSChristoph Hellwig 
1872c7ebbbceSChristoph Hellwig /**
1873eb44820cSRob Landley  * sas_release_transport  -  release SAS transport template instance
1874c7ebbbceSChristoph Hellwig  * @t:		transport template instance
1875c7ebbbceSChristoph Hellwig  */
1876c7ebbbceSChristoph Hellwig void sas_release_transport(struct scsi_transport_template *t)
1877c7ebbbceSChristoph Hellwig {
1878c7ebbbceSChristoph Hellwig 	struct sas_internal *i = to_sas_internal(t);
1879c7ebbbceSChristoph Hellwig 
1880c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->t.host_attrs);
1881c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->phy_attr_cont);
188265c92b09SJames Bottomley 	transport_container_unregister(&i->port_attr_cont);
1883c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->rphy_attr_cont);
1884db82f841SJames Bottomley 	transport_container_unregister(&i->end_dev_attr_cont);
188579cb1819SJames Bottomley 	transport_container_unregister(&i->expander_attr_cont);
1886c7ebbbceSChristoph Hellwig 
1887c7ebbbceSChristoph Hellwig 	kfree(i);
1888c7ebbbceSChristoph Hellwig }
1889c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_release_transport);
1890c7ebbbceSChristoph Hellwig 
1891c7ebbbceSChristoph Hellwig static __init int sas_transport_init(void)
1892c7ebbbceSChristoph Hellwig {
1893c7ebbbceSChristoph Hellwig 	int error;
1894c7ebbbceSChristoph Hellwig 
1895c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_host_class);
1896c7ebbbceSChristoph Hellwig 	if (error)
1897c7ebbbceSChristoph Hellwig 		goto out;
1898c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_phy_class);
1899c7ebbbceSChristoph Hellwig 	if (error)
1900c7ebbbceSChristoph Hellwig 		goto out_unregister_transport;
190165c92b09SJames Bottomley 	error = transport_class_register(&sas_port_class);
1902c7ebbbceSChristoph Hellwig 	if (error)
1903c7ebbbceSChristoph Hellwig 		goto out_unregister_phy;
190465c92b09SJames Bottomley 	error = transport_class_register(&sas_rphy_class);
190565c92b09SJames Bottomley 	if (error)
190665c92b09SJames Bottomley 		goto out_unregister_port;
190742ab0360SJames Bottomley 	error = transport_class_register(&sas_end_dev_class);
190842ab0360SJames Bottomley 	if (error)
190942ab0360SJames Bottomley 		goto out_unregister_rphy;
191079cb1819SJames Bottomley 	error = transport_class_register(&sas_expander_class);
191179cb1819SJames Bottomley 	if (error)
191279cb1819SJames Bottomley 		goto out_unregister_end_dev;
1913c7ebbbceSChristoph Hellwig 
1914c7ebbbceSChristoph Hellwig 	return 0;
1915c7ebbbceSChristoph Hellwig 
191679cb1819SJames Bottomley  out_unregister_end_dev:
191779cb1819SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
191842ab0360SJames Bottomley  out_unregister_rphy:
191942ab0360SJames Bottomley 	transport_class_unregister(&sas_rphy_class);
192065c92b09SJames Bottomley  out_unregister_port:
192165c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1922c7ebbbceSChristoph Hellwig  out_unregister_phy:
1923c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
1924c7ebbbceSChristoph Hellwig  out_unregister_transport:
1925c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1926c7ebbbceSChristoph Hellwig  out:
1927c7ebbbceSChristoph Hellwig 	return error;
1928c7ebbbceSChristoph Hellwig 
1929c7ebbbceSChristoph Hellwig }
1930c7ebbbceSChristoph Hellwig 
1931c7ebbbceSChristoph Hellwig static void __exit sas_transport_exit(void)
1932c7ebbbceSChristoph Hellwig {
1933c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1934c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
193565c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1936c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_rphy_class);
193742ab0360SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
193879cb1819SJames Bottomley 	transport_class_unregister(&sas_expander_class);
1939c7ebbbceSChristoph Hellwig }
1940c7ebbbceSChristoph Hellwig 
1941c7ebbbceSChristoph Hellwig MODULE_AUTHOR("Christoph Hellwig");
194286b9c4c1SAlexis Bruemmer MODULE_DESCRIPTION("SAS Transport Attributes");
1943c7ebbbceSChristoph Hellwig MODULE_LICENSE("GPL");
1944c7ebbbceSChristoph Hellwig 
1945c7ebbbceSChristoph Hellwig module_init(sas_transport_init);
1946c7ebbbceSChristoph Hellwig module_exit(sas_transport_exit);
1947