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,
9c7ebbbceSChristoph Hellwig  * and various sysfs attributes to expose these topologies and managment
10c7ebbbceSChristoph Hellwig  * interfaces to userspace.
11c7ebbbceSChristoph Hellwig  *
12c7ebbbceSChristoph Hellwig  * In addition to the basic SCSI core objects this transport class
13c7ebbbceSChristoph Hellwig  * introduces two additional intermediate objects:  The SAS PHY
14c7ebbbceSChristoph Hellwig  * as represented by struct sas_phy defines an "outgoing" PHY on
15c7ebbbceSChristoph Hellwig  * a SAS HBA or Expander, and the SAS remote PHY represented by
16c7ebbbceSChristoph Hellwig  * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17c7ebbbceSChristoph Hellwig  * end device.  Note that this is purely a software concept, the
18c7ebbbceSChristoph Hellwig  * underlying hardware for a PHY and a remote PHY is the exactly
19c7ebbbceSChristoph Hellwig  * the same.
20c7ebbbceSChristoph Hellwig  *
21c7ebbbceSChristoph Hellwig  * There is no concept of a SAS port in this code, users can see
22c7ebbbceSChristoph Hellwig  * what PHYs form a wide port based on the port_identifier attribute,
23c7ebbbceSChristoph Hellwig  * which is the same for all PHYs in a port.
24c7ebbbceSChristoph Hellwig  */
25c7ebbbceSChristoph Hellwig 
26c7ebbbceSChristoph Hellwig #include <linux/init.h>
27c7ebbbceSChristoph Hellwig #include <linux/module.h>
28f6a57033SAl Viro #include <linux/jiffies.h>
29c7ebbbceSChristoph Hellwig #include <linux/err.h>
308c65b4a6STim Schmielau #include <linux/slab.h>
318c65b4a6STim Schmielau #include <linux/string.h>
327aa68e80SFUJITA Tomonori #include <linux/blkdev.h>
337aa68e80SFUJITA Tomonori #include <linux/bsg.h>
34c7ebbbceSChristoph Hellwig 
35e02f3f59SChristoph Hellwig #include <scsi/scsi.h>
36c7ebbbceSChristoph Hellwig #include <scsi/scsi_device.h>
37c7ebbbceSChristoph Hellwig #include <scsi/scsi_host.h>
38c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport.h>
39c7ebbbceSChristoph Hellwig #include <scsi/scsi_transport_sas.h>
40c7ebbbceSChristoph Hellwig 
41d6159c17SJames Bottomley #include "scsi_sas_internal.h"
42c7ebbbceSChristoph Hellwig struct sas_host_attrs {
43c7ebbbceSChristoph Hellwig 	struct list_head rphy_list;
44e02f3f59SChristoph Hellwig 	struct mutex lock;
45b6aff669SJames Bottomley 	struct request_queue *q;
46c7ebbbceSChristoph Hellwig 	u32 next_target_id;
4779cb1819SJames Bottomley 	u32 next_expander_id;
48c9fefeb2SJames Bottomley 	int next_port_id;
49c7ebbbceSChristoph Hellwig };
50c7ebbbceSChristoph Hellwig #define to_sas_host_attrs(host)	((struct sas_host_attrs *)(host)->shost_data)
51c7ebbbceSChristoph Hellwig 
52c7ebbbceSChristoph Hellwig 
53c7ebbbceSChristoph Hellwig /*
54c7ebbbceSChristoph Hellwig  * Hack to allow attributes of the same name in different objects.
55c7ebbbceSChristoph Hellwig  */
56c7ebbbceSChristoph Hellwig #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57c7ebbbceSChristoph Hellwig 	struct class_device_attribute class_device_attr_##_prefix##_##_name = \
58c7ebbbceSChristoph Hellwig 	__ATTR(_name,_mode,_show,_store)
59c7ebbbceSChristoph Hellwig 
60c7ebbbceSChristoph Hellwig 
61c7ebbbceSChristoph Hellwig /*
62c7ebbbceSChristoph Hellwig  * Pretty printing helpers
63c7ebbbceSChristoph Hellwig  */
64c7ebbbceSChristoph Hellwig 
65c7ebbbceSChristoph Hellwig #define sas_bitfield_name_match(title, table)			\
66c7ebbbceSChristoph Hellwig static ssize_t							\
67c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
68c7ebbbceSChristoph Hellwig {								\
69c7ebbbceSChristoph Hellwig 	char *prefix = "";					\
70c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
71c7ebbbceSChristoph Hellwig 	int i;							\
72c7ebbbceSChristoph Hellwig 								\
736391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
74c7ebbbceSChristoph Hellwig 		if (table[i].value & table_key) {		\
75c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s%s",	\
76c7ebbbceSChristoph Hellwig 				prefix, table[i].name);		\
77c7ebbbceSChristoph Hellwig 			prefix = ", ";				\
78c7ebbbceSChristoph Hellwig 		}						\
79c7ebbbceSChristoph Hellwig 	}							\
80c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
81c7ebbbceSChristoph Hellwig 	return len;						\
82c7ebbbceSChristoph Hellwig }
83c7ebbbceSChristoph Hellwig 
84d24e1eebSJames Bottomley #define sas_bitfield_name_set(title, table)			\
85d24e1eebSJames Bottomley static ssize_t							\
86d24e1eebSJames Bottomley set_sas_##title##_names(u32 *table_key, const char *buf)	\
87d24e1eebSJames Bottomley {								\
88d24e1eebSJames Bottomley 	ssize_t len = 0;					\
89d24e1eebSJames Bottomley 	int i;							\
90d24e1eebSJames Bottomley 								\
91d24e1eebSJames Bottomley 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
92d24e1eebSJames Bottomley 		len = strlen(table[i].name);			\
93d24e1eebSJames Bottomley 		if (strncmp(buf, table[i].name, len) == 0 &&	\
94d24e1eebSJames Bottomley 		    (buf[len] == '\n' || buf[len] == '\0')) {	\
95d24e1eebSJames Bottomley 			*table_key = table[i].value;		\
96d24e1eebSJames Bottomley 			return 0;				\
97d24e1eebSJames Bottomley 		}						\
98d24e1eebSJames Bottomley 	}							\
99d24e1eebSJames Bottomley 	return -EINVAL;						\
100d24e1eebSJames Bottomley }
101d24e1eebSJames Bottomley 
102c7ebbbceSChristoph Hellwig #define sas_bitfield_name_search(title, table)			\
103c7ebbbceSChristoph Hellwig static ssize_t							\
104c7ebbbceSChristoph Hellwig get_sas_##title##_names(u32 table_key, char *buf)		\
105c7ebbbceSChristoph Hellwig {								\
106c7ebbbceSChristoph Hellwig 	ssize_t len = 0;					\
107c7ebbbceSChristoph Hellwig 	int i;							\
108c7ebbbceSChristoph Hellwig 								\
1096391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
110c7ebbbceSChristoph Hellwig 		if (table[i].value == table_key) {		\
111c7ebbbceSChristoph Hellwig 			len += sprintf(buf + len, "%s",		\
112c7ebbbceSChristoph Hellwig 				table[i].name);			\
113c7ebbbceSChristoph Hellwig 			break;					\
114c7ebbbceSChristoph Hellwig 		}						\
115c7ebbbceSChristoph Hellwig 	}							\
116c7ebbbceSChristoph Hellwig 	len += sprintf(buf + len, "\n");			\
117c7ebbbceSChristoph Hellwig 	return len;						\
118c7ebbbceSChristoph Hellwig }
119c7ebbbceSChristoph Hellwig 
120c7ebbbceSChristoph Hellwig static struct {
121c7ebbbceSChristoph Hellwig 	u32		value;
122c7ebbbceSChristoph Hellwig 	char		*name;
123c7ebbbceSChristoph Hellwig } sas_device_type_names[] = {
124c7ebbbceSChristoph Hellwig 	{ SAS_PHY_UNUSED,		"unused" },
125c7ebbbceSChristoph Hellwig 	{ SAS_END_DEVICE,		"end device" },
126c7ebbbceSChristoph Hellwig 	{ SAS_EDGE_EXPANDER_DEVICE,	"edge expander" },
127c7ebbbceSChristoph Hellwig 	{ SAS_FANOUT_EXPANDER_DEVICE,	"fanout expander" },
128c7ebbbceSChristoph Hellwig };
129c7ebbbceSChristoph Hellwig sas_bitfield_name_search(device_type, sas_device_type_names)
130c7ebbbceSChristoph Hellwig 
131c7ebbbceSChristoph Hellwig 
132c7ebbbceSChristoph Hellwig static struct {
133c7ebbbceSChristoph Hellwig 	u32		value;
134c7ebbbceSChristoph Hellwig 	char		*name;
135c7ebbbceSChristoph Hellwig } sas_protocol_names[] = {
136c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SATA,		"sata" },
137c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SMP,		"smp" },
138c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_STP,		"stp" },
139c7ebbbceSChristoph Hellwig 	{ SAS_PROTOCOL_SSP,		"ssp" },
140c7ebbbceSChristoph Hellwig };
141c7ebbbceSChristoph Hellwig sas_bitfield_name_match(protocol, sas_protocol_names)
142c7ebbbceSChristoph Hellwig 
143c7ebbbceSChristoph Hellwig static struct {
144c7ebbbceSChristoph Hellwig 	u32		value;
145c7ebbbceSChristoph Hellwig 	char		*name;
146c7ebbbceSChristoph Hellwig } sas_linkspeed_names[] = {
147c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_UNKNOWN,	"Unknown" },
148c7ebbbceSChristoph Hellwig 	{ SAS_PHY_DISABLED,		"Phy disabled" },
149c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_FAILED,		"Link Rate failed" },
150c7ebbbceSChristoph Hellwig 	{ SAS_SATA_SPINUP_HOLD,		"Spin-up hold" },
151c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_1_5_GBPS,	"1.5 Gbit" },
152c7ebbbceSChristoph Hellwig 	{ SAS_LINK_RATE_3_0_GBPS,	"3.0 Gbit" },
1537e6dff62SJames Bottomley 	{ SAS_LINK_RATE_6_0_GBPS,	"6.0 Gbit" },
154c7ebbbceSChristoph Hellwig };
155c7ebbbceSChristoph Hellwig sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
156d24e1eebSJames Bottomley sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
157c7ebbbceSChristoph Hellwig 
1587aa68e80SFUJITA Tomonori static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
1597aa68e80SFUJITA Tomonori 			    struct sas_rphy *rphy)
1607aa68e80SFUJITA Tomonori {
1617aa68e80SFUJITA Tomonori 	struct request *req;
1627aa68e80SFUJITA Tomonori 	int ret;
1637aa68e80SFUJITA Tomonori 	int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
1647aa68e80SFUJITA Tomonori 
1657aa68e80SFUJITA Tomonori 	while (!blk_queue_plugged(q)) {
1667aa68e80SFUJITA Tomonori 		req = elv_next_request(q);
1677aa68e80SFUJITA Tomonori 		if (!req)
1687aa68e80SFUJITA Tomonori 			break;
1697aa68e80SFUJITA Tomonori 
1707aa68e80SFUJITA Tomonori 		blkdev_dequeue_request(req);
1717aa68e80SFUJITA Tomonori 
1727aa68e80SFUJITA Tomonori 		spin_unlock_irq(q->queue_lock);
1737aa68e80SFUJITA Tomonori 
1747aa68e80SFUJITA Tomonori 		handler = to_sas_internal(shost->transportt)->f->smp_handler;
1757aa68e80SFUJITA Tomonori 		ret = handler(shost, rphy, req);
1762d507a01SJames Bottomley 		req->errors = ret;
1777aa68e80SFUJITA Tomonori 
1787aa68e80SFUJITA Tomonori 		spin_lock_irq(q->queue_lock);
1797aa68e80SFUJITA Tomonori 
1807aa68e80SFUJITA Tomonori 		req->end_io(req, ret);
1817aa68e80SFUJITA Tomonori 	}
1827aa68e80SFUJITA Tomonori }
1837aa68e80SFUJITA Tomonori 
1847aa68e80SFUJITA Tomonori static void sas_host_smp_request(struct request_queue *q)
1857aa68e80SFUJITA Tomonori {
1867aa68e80SFUJITA Tomonori 	sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
1877aa68e80SFUJITA Tomonori }
1887aa68e80SFUJITA Tomonori 
1897aa68e80SFUJITA Tomonori static void sas_non_host_smp_request(struct request_queue *q)
1907aa68e80SFUJITA Tomonori {
1917aa68e80SFUJITA Tomonori 	struct sas_rphy *rphy = q->queuedata;
1927aa68e80SFUJITA Tomonori 	sas_smp_request(q, rphy_to_shost(rphy), rphy);
1937aa68e80SFUJITA Tomonori }
1947aa68e80SFUJITA Tomonori 
19539dca558SJames Bottomley static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
1967aa68e80SFUJITA Tomonori {
1977aa68e80SFUJITA Tomonori 	struct request_queue *q;
1987aa68e80SFUJITA Tomonori 	int error;
19939dca558SJames Bottomley 	struct device *dev;
20039dca558SJames Bottomley 	char namebuf[BUS_ID_SIZE];
20139dca558SJames Bottomley 	const char *name;
2027aa68e80SFUJITA Tomonori 
2037aa68e80SFUJITA Tomonori 	if (!to_sas_internal(shost->transportt)->f->smp_handler) {
2047aa68e80SFUJITA Tomonori 		printk("%s can't handle SMP requests\n", shost->hostt->name);
2057aa68e80SFUJITA Tomonori 		return 0;
2067aa68e80SFUJITA Tomonori 	}
2077aa68e80SFUJITA Tomonori 
20839dca558SJames Bottomley 	if (rphy) {
2097aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_non_host_smp_request, NULL);
21039dca558SJames Bottomley 		dev = &rphy->dev;
21139dca558SJames Bottomley 		name = dev->bus_id;
21239dca558SJames Bottomley 	} else {
2137aa68e80SFUJITA Tomonori 		q = blk_init_queue(sas_host_smp_request, NULL);
21439dca558SJames Bottomley 		dev = &shost->shost_gendev;
21539dca558SJames Bottomley 		snprintf(namebuf, sizeof(namebuf),
21639dca558SJames Bottomley 			 "sas_host%d", shost->host_no);
21739dca558SJames Bottomley 		name = namebuf;
21839dca558SJames Bottomley 	}
2197aa68e80SFUJITA Tomonori 	if (!q)
2207aa68e80SFUJITA Tomonori 		return -ENOMEM;
2217aa68e80SFUJITA Tomonori 
22239dca558SJames Bottomley 	error = bsg_register_queue(q, dev, name);
2237aa68e80SFUJITA Tomonori 	if (error) {
2247aa68e80SFUJITA Tomonori 		blk_cleanup_queue(q);
2257aa68e80SFUJITA Tomonori 		return -ENOMEM;
2267aa68e80SFUJITA Tomonori 	}
2277aa68e80SFUJITA Tomonori 
2287aa68e80SFUJITA Tomonori 	if (rphy)
229b6aff669SJames Bottomley 		rphy->q = q;
230b6aff669SJames Bottomley 	else
231b6aff669SJames Bottomley 		to_sas_host_attrs(shost)->q = q;
232b6aff669SJames Bottomley 
233b6aff669SJames Bottomley 	if (rphy)
2347aa68e80SFUJITA Tomonori 		q->queuedata = rphy;
2357aa68e80SFUJITA Tomonori 	else
2367aa68e80SFUJITA Tomonori 		q->queuedata = shost;
2377aa68e80SFUJITA Tomonori 
2387aa68e80SFUJITA Tomonori 	set_bit(QUEUE_FLAG_BIDI, &q->queue_flags);
2397aa68e80SFUJITA Tomonori 
2407aa68e80SFUJITA Tomonori 	return 0;
2417aa68e80SFUJITA Tomonori }
2427aa68e80SFUJITA Tomonori 
243b6aff669SJames Bottomley static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
244b6aff669SJames Bottomley {
245b6aff669SJames Bottomley 	struct request_queue *q;
246b6aff669SJames Bottomley 
247b6aff669SJames Bottomley 	if (rphy)
248b6aff669SJames Bottomley 		q = rphy->q;
249b6aff669SJames Bottomley 	else
250b6aff669SJames Bottomley 		q = to_sas_host_attrs(shost)->q;
251b6aff669SJames Bottomley 
252b6aff669SJames Bottomley 	if (!q)
253b6aff669SJames Bottomley 		return;
254b6aff669SJames Bottomley 
255b6aff669SJames Bottomley 	bsg_unregister_queue(q);
256b6aff669SJames Bottomley 	blk_cleanup_queue(q);
257b6aff669SJames Bottomley }
258b6aff669SJames Bottomley 
259c7ebbbceSChristoph Hellwig /*
260c7ebbbceSChristoph Hellwig  * SAS host attributes
261c7ebbbceSChristoph Hellwig  */
262c7ebbbceSChristoph Hellwig 
26337be6eebSJames Bottomley static int sas_host_setup(struct transport_container *tc, struct device *dev,
26437be6eebSJames Bottomley 			  struct class_device *cdev)
265c7ebbbceSChristoph Hellwig {
266c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(dev);
267c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
268c7ebbbceSChristoph Hellwig 
269c7ebbbceSChristoph Hellwig 	INIT_LIST_HEAD(&sas_host->rphy_list);
270e02f3f59SChristoph Hellwig 	mutex_init(&sas_host->lock);
271c7ebbbceSChristoph Hellwig 	sas_host->next_target_id = 0;
27279cb1819SJames Bottomley 	sas_host->next_expander_id = 0;
273c9fefeb2SJames Bottomley 	sas_host->next_port_id = 0;
2747aa68e80SFUJITA Tomonori 
27539dca558SJames Bottomley 	if (sas_bsg_initialize(shost, NULL))
2767aa68e80SFUJITA Tomonori 		dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
2777aa68e80SFUJITA Tomonori 			   shost->host_no);
2787aa68e80SFUJITA Tomonori 
279c7ebbbceSChristoph Hellwig 	return 0;
280c7ebbbceSChristoph Hellwig }
281c7ebbbceSChristoph Hellwig 
282b6aff669SJames Bottomley static int sas_host_remove(struct transport_container *tc, struct device *dev,
283b6aff669SJames Bottomley 			   struct class_device *cdev)
284b6aff669SJames Bottomley {
285b6aff669SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev);
286b6aff669SJames Bottomley 
287b6aff669SJames Bottomley 	sas_bsg_remove(shost, NULL);
288b6aff669SJames Bottomley 
289b6aff669SJames Bottomley 	return 0;
290b6aff669SJames Bottomley }
291b6aff669SJames Bottomley 
292c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_host_class,
293b6aff669SJames Bottomley 		"sas_host", sas_host_setup, sas_host_remove, NULL);
294c7ebbbceSChristoph Hellwig 
295c7ebbbceSChristoph Hellwig static int sas_host_match(struct attribute_container *cont,
296c7ebbbceSChristoph Hellwig 			    struct device *dev)
297c7ebbbceSChristoph Hellwig {
298c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
299c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
300c7ebbbceSChristoph Hellwig 
301c7ebbbceSChristoph Hellwig 	if (!scsi_is_host_device(dev))
302c7ebbbceSChristoph Hellwig 		return 0;
303c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev);
304c7ebbbceSChristoph Hellwig 
305c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
306c7ebbbceSChristoph Hellwig 		return 0;
307c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
308c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
309c7ebbbceSChristoph Hellwig 		return 0;
310c7ebbbceSChristoph Hellwig 
311c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
312c7ebbbceSChristoph Hellwig 	return &i->t.host_attrs.ac == cont;
313c7ebbbceSChristoph Hellwig }
314c7ebbbceSChristoph Hellwig 
315c7ebbbceSChristoph Hellwig static int do_sas_phy_delete(struct device *dev, void *data)
316c7ebbbceSChristoph Hellwig {
31765c92b09SJames Bottomley 	int pass = (int)(unsigned long)data;
31865c92b09SJames Bottomley 
31965c92b09SJames Bottomley 	if (pass == 0 && scsi_is_sas_port(dev))
32065c92b09SJames Bottomley 		sas_port_delete(dev_to_sas_port(dev));
32165c92b09SJames Bottomley 	else if (pass == 1 && scsi_is_sas_phy(dev))
322c7ebbbceSChristoph Hellwig 		sas_phy_delete(dev_to_phy(dev));
323c7ebbbceSChristoph Hellwig 	return 0;
324c7ebbbceSChristoph Hellwig }
325c7ebbbceSChristoph Hellwig 
326c7ebbbceSChristoph Hellwig /**
327eb44820cSRob Landley  * sas_remove_children  -  tear down a devices SAS data structures
32865c92b09SJames Bottomley  * @dev:	device belonging to the sas object
32965c92b09SJames Bottomley  *
33065c92b09SJames Bottomley  * Removes all SAS PHYs and remote PHYs for a given object
33165c92b09SJames Bottomley  */
33265c92b09SJames Bottomley void sas_remove_children(struct device *dev)
33365c92b09SJames Bottomley {
33465c92b09SJames Bottomley 	device_for_each_child(dev, (void *)0, do_sas_phy_delete);
33565c92b09SJames Bottomley 	device_for_each_child(dev, (void *)1, do_sas_phy_delete);
33665c92b09SJames Bottomley }
33765c92b09SJames Bottomley EXPORT_SYMBOL(sas_remove_children);
33865c92b09SJames Bottomley 
33965c92b09SJames Bottomley /**
340eb44820cSRob Landley  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
341c7ebbbceSChristoph Hellwig  * @shost:	Scsi Host that is torn down
342c7ebbbceSChristoph Hellwig  *
343c7ebbbceSChristoph Hellwig  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
344c7ebbbceSChristoph Hellwig  * Must be called just before scsi_remove_host for SAS HBAs.
345c7ebbbceSChristoph Hellwig  */
346c7ebbbceSChristoph Hellwig void sas_remove_host(struct Scsi_Host *shost)
347c7ebbbceSChristoph Hellwig {
34865c92b09SJames Bottomley 	sas_remove_children(&shost->shost_gendev);
349c7ebbbceSChristoph Hellwig }
350c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_remove_host);
351c7ebbbceSChristoph Hellwig 
352c7ebbbceSChristoph Hellwig 
353c7ebbbceSChristoph Hellwig /*
35465c92b09SJames Bottomley  * SAS Phy attributes
355c7ebbbceSChristoph Hellwig  */
356c7ebbbceSChristoph Hellwig 
357c7ebbbceSChristoph Hellwig #define sas_phy_show_simple(field, name, format_string, cast)		\
358c7ebbbceSChristoph Hellwig static ssize_t								\
359c7ebbbceSChristoph Hellwig show_sas_phy_##name(struct class_device *cdev, char *buf)		\
360c7ebbbceSChristoph Hellwig {									\
361c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
362c7ebbbceSChristoph Hellwig 									\
363c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast phy->field);	\
364c7ebbbceSChristoph Hellwig }
365c7ebbbceSChristoph Hellwig 
366c7ebbbceSChristoph Hellwig #define sas_phy_simple_attr(field, name, format_string, type)		\
367c7ebbbceSChristoph Hellwig 	sas_phy_show_simple(field, name, format_string, (type))	\
368c7ebbbceSChristoph Hellwig static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
369c7ebbbceSChristoph Hellwig 
370c7ebbbceSChristoph Hellwig #define sas_phy_show_protocol(field, name)				\
371c7ebbbceSChristoph Hellwig static ssize_t								\
372c7ebbbceSChristoph Hellwig show_sas_phy_##name(struct class_device *cdev, char *buf)		\
373c7ebbbceSChristoph Hellwig {									\
374c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
375c7ebbbceSChristoph Hellwig 									\
376c7ebbbceSChristoph Hellwig 	if (!phy->field)						\
377c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
378c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(phy->field, buf);		\
379c7ebbbceSChristoph Hellwig }
380c7ebbbceSChristoph Hellwig 
381c7ebbbceSChristoph Hellwig #define sas_phy_protocol_attr(field, name)				\
382c7ebbbceSChristoph Hellwig 	sas_phy_show_protocol(field, name)				\
383c7ebbbceSChristoph Hellwig static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
384c7ebbbceSChristoph Hellwig 
385c7ebbbceSChristoph Hellwig #define sas_phy_show_linkspeed(field)					\
386c7ebbbceSChristoph Hellwig static ssize_t								\
387c7ebbbceSChristoph Hellwig show_sas_phy_##field(struct class_device *cdev, char *buf)		\
388c7ebbbceSChristoph Hellwig {									\
389c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
390c7ebbbceSChristoph Hellwig 									\
391c7ebbbceSChristoph Hellwig 	return get_sas_linkspeed_names(phy->field, buf);		\
392c7ebbbceSChristoph Hellwig }
393c7ebbbceSChristoph Hellwig 
394d24e1eebSJames Bottomley /* Fudge to tell if we're minimum or maximum */
395d24e1eebSJames Bottomley #define sas_phy_store_linkspeed(field)					\
396d24e1eebSJames Bottomley static ssize_t								\
397d24e1eebSJames Bottomley store_sas_phy_##field(struct class_device *cdev, const char *buf,	\
398d24e1eebSJames Bottomley 		      size_t count)					\
399d24e1eebSJames Bottomley {									\
400d24e1eebSJames Bottomley 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
401d24e1eebSJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
402d24e1eebSJames Bottomley 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
403d24e1eebSJames Bottomley 	u32 value;							\
404d24e1eebSJames Bottomley 	struct sas_phy_linkrates rates = {0};				\
405d24e1eebSJames Bottomley 	int error;							\
406d24e1eebSJames Bottomley 									\
407d24e1eebSJames Bottomley 	error = set_sas_linkspeed_names(&value, buf);			\
408d24e1eebSJames Bottomley 	if (error)							\
409d24e1eebSJames Bottomley 		return error;						\
410d24e1eebSJames Bottomley 	rates.field = value;						\
411d24e1eebSJames Bottomley 	error = i->f->set_phy_speed(phy, &rates);			\
412d24e1eebSJames Bottomley 									\
413d24e1eebSJames Bottomley 	return error ? error : count;					\
414d24e1eebSJames Bottomley }
415d24e1eebSJames Bottomley 
416d24e1eebSJames Bottomley #define sas_phy_linkspeed_rw_attr(field)				\
417d24e1eebSJames Bottomley 	sas_phy_show_linkspeed(field)					\
418d24e1eebSJames Bottomley 	sas_phy_store_linkspeed(field)					\
419d24e1eebSJames Bottomley static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,		\
420d24e1eebSJames Bottomley 	store_sas_phy_##field)
421d24e1eebSJames Bottomley 
422c7ebbbceSChristoph Hellwig #define sas_phy_linkspeed_attr(field)					\
423c7ebbbceSChristoph Hellwig 	sas_phy_show_linkspeed(field)					\
424c7ebbbceSChristoph Hellwig static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
425c7ebbbceSChristoph Hellwig 
426d24e1eebSJames Bottomley 
427c3ee74c4SChristoph Hellwig #define sas_phy_show_linkerror(field)					\
428c3ee74c4SChristoph Hellwig static ssize_t								\
429c3ee74c4SChristoph Hellwig show_sas_phy_##field(struct class_device *cdev, char *buf)		\
430c3ee74c4SChristoph Hellwig {									\
431c3ee74c4SChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);		\
432c3ee74c4SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);	\
433c3ee74c4SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);	\
434c3ee74c4SChristoph Hellwig 	int error;							\
435c3ee74c4SChristoph Hellwig 									\
436dd9fbb52SJames Bottomley 	error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;	\
437c3ee74c4SChristoph Hellwig 	if (error)							\
438c3ee74c4SChristoph Hellwig 		return error;						\
439c3ee74c4SChristoph Hellwig 	return snprintf(buf, 20, "%u\n", phy->field);			\
440c3ee74c4SChristoph Hellwig }
441c3ee74c4SChristoph Hellwig 
442c3ee74c4SChristoph Hellwig #define sas_phy_linkerror_attr(field)					\
443c3ee74c4SChristoph Hellwig 	sas_phy_show_linkerror(field)					\
444c3ee74c4SChristoph Hellwig static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
445c3ee74c4SChristoph Hellwig 
446c3ee74c4SChristoph Hellwig 
447c7ebbbceSChristoph Hellwig static ssize_t
448c7ebbbceSChristoph Hellwig show_sas_device_type(struct class_device *cdev, char *buf)
449c7ebbbceSChristoph Hellwig {
450c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);
451c7ebbbceSChristoph Hellwig 
452c7ebbbceSChristoph Hellwig 	if (!phy->identify.device_type)
453c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
454c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(phy->identify.device_type, buf);
455c7ebbbceSChristoph Hellwig }
456c7ebbbceSChristoph Hellwig static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
457c7ebbbceSChristoph Hellwig 
458acbf167dSDarrick J. Wong static ssize_t do_sas_phy_enable(struct class_device *cdev,
459acbf167dSDarrick J. Wong 		size_t count, int enable)
460acbf167dSDarrick J. Wong {
461acbf167dSDarrick J. Wong 	struct sas_phy *phy = transport_class_to_phy(cdev);
462acbf167dSDarrick J. Wong 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
463acbf167dSDarrick J. Wong 	struct sas_internal *i = to_sas_internal(shost->transportt);
464acbf167dSDarrick J. Wong 	int error;
465acbf167dSDarrick J. Wong 
466acbf167dSDarrick J. Wong 	error = i->f->phy_enable(phy, enable);
467acbf167dSDarrick J. Wong 	if (error)
468acbf167dSDarrick J. Wong 		return error;
469acbf167dSDarrick J. Wong 	phy->enabled = enable;
470acbf167dSDarrick J. Wong 	return count;
471acbf167dSDarrick J. Wong };
472acbf167dSDarrick J. Wong 
473acbf167dSDarrick J. Wong static ssize_t store_sas_phy_enable(struct class_device *cdev,
474acbf167dSDarrick J. Wong 		const char *buf, size_t count)
475acbf167dSDarrick J. Wong {
476acbf167dSDarrick J. Wong 	if (count < 1)
477acbf167dSDarrick J. Wong 		return -EINVAL;
478acbf167dSDarrick J. Wong 
479acbf167dSDarrick J. Wong 	switch (buf[0]) {
480acbf167dSDarrick J. Wong 	case '0':
481acbf167dSDarrick J. Wong 		do_sas_phy_enable(cdev, count, 0);
482acbf167dSDarrick J. Wong 		break;
483acbf167dSDarrick J. Wong 	case '1':
484acbf167dSDarrick J. Wong 		do_sas_phy_enable(cdev, count, 1);
485acbf167dSDarrick J. Wong 		break;
486acbf167dSDarrick J. Wong 	default:
487acbf167dSDarrick J. Wong 		return -EINVAL;
488acbf167dSDarrick J. Wong 	}
489acbf167dSDarrick J. Wong 
490acbf167dSDarrick J. Wong 	return count;
491acbf167dSDarrick J. Wong }
492acbf167dSDarrick J. Wong 
493acbf167dSDarrick J. Wong static ssize_t show_sas_phy_enable(struct class_device *cdev, char *buf)
494acbf167dSDarrick J. Wong {
495acbf167dSDarrick J. Wong 	struct sas_phy *phy = transport_class_to_phy(cdev);
496acbf167dSDarrick J. Wong 
497acbf167dSDarrick J. Wong 	return snprintf(buf, 20, "%d", phy->enabled);
498acbf167dSDarrick J. Wong }
499acbf167dSDarrick J. Wong 
500acbf167dSDarrick J. Wong static CLASS_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
501acbf167dSDarrick J. Wong 			 store_sas_phy_enable);
502acbf167dSDarrick J. Wong 
50307ba3a95SChristoph Hellwig static ssize_t do_sas_phy_reset(struct class_device *cdev,
50407ba3a95SChristoph Hellwig 		size_t count, int hard_reset)
50507ba3a95SChristoph Hellwig {
50607ba3a95SChristoph Hellwig 	struct sas_phy *phy = transport_class_to_phy(cdev);
50707ba3a95SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
50807ba3a95SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
50907ba3a95SChristoph Hellwig 	int error;
51007ba3a95SChristoph Hellwig 
51107ba3a95SChristoph Hellwig 	error = i->f->phy_reset(phy, hard_reset);
51207ba3a95SChristoph Hellwig 	if (error)
51307ba3a95SChristoph Hellwig 		return error;
51407ba3a95SChristoph Hellwig 	return count;
51507ba3a95SChristoph Hellwig };
51607ba3a95SChristoph Hellwig 
51707ba3a95SChristoph Hellwig static ssize_t store_sas_link_reset(struct class_device *cdev,
51807ba3a95SChristoph Hellwig 		const char *buf, size_t count)
51907ba3a95SChristoph Hellwig {
52007ba3a95SChristoph Hellwig 	return do_sas_phy_reset(cdev, count, 0);
52107ba3a95SChristoph Hellwig }
52207ba3a95SChristoph Hellwig static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
52307ba3a95SChristoph Hellwig 
52407ba3a95SChristoph Hellwig static ssize_t store_sas_hard_reset(struct class_device *cdev,
52507ba3a95SChristoph Hellwig 		const char *buf, size_t count)
52607ba3a95SChristoph Hellwig {
52707ba3a95SChristoph Hellwig 	return do_sas_phy_reset(cdev, count, 1);
52807ba3a95SChristoph Hellwig }
52907ba3a95SChristoph Hellwig static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
53007ba3a95SChristoph Hellwig 
531c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.initiator_port_protocols,
532c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
533c7ebbbceSChristoph Hellwig sas_phy_protocol_attr(identify.target_port_protocols,
534c7ebbbceSChristoph Hellwig 		target_port_protocols);
535c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
536c7ebbbceSChristoph Hellwig 		unsigned long long);
537c7ebbbceSChristoph Hellwig sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
538c9fefeb2SJames Bottomley //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
539c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(negotiated_linkrate);
540c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(minimum_linkrate_hw);
541d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(minimum_linkrate);
542c7ebbbceSChristoph Hellwig sas_phy_linkspeed_attr(maximum_linkrate_hw);
543d24e1eebSJames Bottomley sas_phy_linkspeed_rw_attr(maximum_linkrate);
544c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(invalid_dword_count);
545c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(running_disparity_error_count);
546c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(loss_of_dword_sync_count);
547c3ee74c4SChristoph Hellwig sas_phy_linkerror_attr(phy_reset_problem_count);
548c7ebbbceSChristoph Hellwig 
549c7ebbbceSChristoph Hellwig 
550c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_phy_class,
551c7ebbbceSChristoph Hellwig 		"sas_phy", NULL, NULL, NULL);
552c7ebbbceSChristoph Hellwig 
553c7ebbbceSChristoph Hellwig static int sas_phy_match(struct attribute_container *cont, struct device *dev)
554c7ebbbceSChristoph Hellwig {
555c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
556c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
557c7ebbbceSChristoph Hellwig 
558c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_phy(dev))
559c7ebbbceSChristoph Hellwig 		return 0;
560c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent);
561c7ebbbceSChristoph Hellwig 
562c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
563c7ebbbceSChristoph Hellwig 		return 0;
564c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
565c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
566c7ebbbceSChristoph Hellwig 		return 0;
567c7ebbbceSChristoph Hellwig 
568c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
569c7ebbbceSChristoph Hellwig 	return &i->phy_attr_cont.ac == cont;
570c7ebbbceSChristoph Hellwig }
571c7ebbbceSChristoph Hellwig 
572c7ebbbceSChristoph Hellwig static void sas_phy_release(struct device *dev)
573c7ebbbceSChristoph Hellwig {
574c7ebbbceSChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(dev);
575c7ebbbceSChristoph Hellwig 
576c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
577c7ebbbceSChristoph Hellwig 	kfree(phy);
578c7ebbbceSChristoph Hellwig }
579c7ebbbceSChristoph Hellwig 
580c7ebbbceSChristoph Hellwig /**
581eb44820cSRob Landley  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
582c7ebbbceSChristoph Hellwig  * @parent:	Parent device
583d99ca418SMoore, Eric  * @number:	Phy index
584c7ebbbceSChristoph Hellwig  *
585c7ebbbceSChristoph Hellwig  * Allocates an SAS PHY structure.  It will be added in the device tree
586c7ebbbceSChristoph Hellwig  * below the device specified by @parent, which has to be either a Scsi_Host
587c7ebbbceSChristoph Hellwig  * or sas_rphy.
588c7ebbbceSChristoph Hellwig  *
589c7ebbbceSChristoph Hellwig  * Returns:
590c7ebbbceSChristoph Hellwig  *	SAS PHY allocated or %NULL if the allocation failed.
591c7ebbbceSChristoph Hellwig  */
592c7ebbbceSChristoph Hellwig struct sas_phy *sas_phy_alloc(struct device *parent, int number)
593c7ebbbceSChristoph Hellwig {
594c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent);
595c7ebbbceSChristoph Hellwig 	struct sas_phy *phy;
596c7ebbbceSChristoph Hellwig 
59724669f75SJes Sorensen 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
598c7ebbbceSChristoph Hellwig 	if (!phy)
599c7ebbbceSChristoph Hellwig 		return NULL;
600c7ebbbceSChristoph Hellwig 
601c7ebbbceSChristoph Hellwig 	phy->number = number;
602acbf167dSDarrick J. Wong 	phy->enabled = 1;
603c7ebbbceSChristoph Hellwig 
604c7ebbbceSChristoph Hellwig 	device_initialize(&phy->dev);
605c7ebbbceSChristoph Hellwig 	phy->dev.parent = get_device(parent);
606c7ebbbceSChristoph Hellwig 	phy->dev.release = sas_phy_release;
60765c92b09SJames Bottomley 	INIT_LIST_HEAD(&phy->port_siblings);
60879cb1819SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
60979cb1819SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
61065c92b09SJames Bottomley 		sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
61179cb1819SJames Bottomley 			rphy->scsi_target_id, number);
61279cb1819SJames Bottomley 	} else
613c7ebbbceSChristoph Hellwig 		sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
614c7ebbbceSChristoph Hellwig 
615c7ebbbceSChristoph Hellwig 	transport_setup_device(&phy->dev);
616c7ebbbceSChristoph Hellwig 
617c7ebbbceSChristoph Hellwig 	return phy;
618c7ebbbceSChristoph Hellwig }
619c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_alloc);
620c7ebbbceSChristoph Hellwig 
621c7ebbbceSChristoph Hellwig /**
622eb44820cSRob Landley  * sas_phy_add  -  add a SAS PHY to the device hierarchy
623c7ebbbceSChristoph Hellwig  * @phy:	The PHY to be added
624c7ebbbceSChristoph Hellwig  *
625c7ebbbceSChristoph Hellwig  * Publishes a SAS PHY to the rest of the system.
626c7ebbbceSChristoph Hellwig  */
627c7ebbbceSChristoph Hellwig int sas_phy_add(struct sas_phy *phy)
628c7ebbbceSChristoph Hellwig {
629c7ebbbceSChristoph Hellwig 	int error;
630c7ebbbceSChristoph Hellwig 
631c7ebbbceSChristoph Hellwig 	error = device_add(&phy->dev);
632c7ebbbceSChristoph Hellwig 	if (!error) {
633c7ebbbceSChristoph Hellwig 		transport_add_device(&phy->dev);
634c7ebbbceSChristoph Hellwig 		transport_configure_device(&phy->dev);
635c7ebbbceSChristoph Hellwig 	}
636c7ebbbceSChristoph Hellwig 
637c7ebbbceSChristoph Hellwig 	return error;
638c7ebbbceSChristoph Hellwig }
639c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_add);
640c7ebbbceSChristoph Hellwig 
641c7ebbbceSChristoph Hellwig /**
642eb44820cSRob Landley  * sas_phy_free  -  free a SAS PHY
643c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to free
644c7ebbbceSChristoph Hellwig  *
645c7ebbbceSChristoph Hellwig  * Frees the specified SAS PHY.
646c7ebbbceSChristoph Hellwig  *
647c7ebbbceSChristoph Hellwig  * Note:
648c7ebbbceSChristoph Hellwig  *   This function must only be called on a PHY that has not
649c7ebbbceSChristoph Hellwig  *   sucessfully been added using sas_phy_add().
650c7ebbbceSChristoph Hellwig  */
651c7ebbbceSChristoph Hellwig void sas_phy_free(struct sas_phy *phy)
652c7ebbbceSChristoph Hellwig {
653c7ebbbceSChristoph Hellwig 	transport_destroy_device(&phy->dev);
65492aab646SMike Anderson 	put_device(&phy->dev);
655c7ebbbceSChristoph Hellwig }
656c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_free);
657c7ebbbceSChristoph Hellwig 
658c7ebbbceSChristoph Hellwig /**
659eb44820cSRob Landley  * sas_phy_delete  -  remove SAS PHY
660c7ebbbceSChristoph Hellwig  * @phy:	SAS PHY to remove
661c7ebbbceSChristoph Hellwig  *
662c7ebbbceSChristoph Hellwig  * Removes the specified SAS PHY.  If the SAS PHY has an
663c7ebbbceSChristoph Hellwig  * associated remote PHY it is removed before.
664c7ebbbceSChristoph Hellwig  */
665c7ebbbceSChristoph Hellwig void
666c7ebbbceSChristoph Hellwig sas_phy_delete(struct sas_phy *phy)
667c7ebbbceSChristoph Hellwig {
668c7ebbbceSChristoph Hellwig 	struct device *dev = &phy->dev;
669c7ebbbceSChristoph Hellwig 
67065c92b09SJames Bottomley 	/* this happens if the phy is still part of a port when deleted */
67165c92b09SJames Bottomley 	BUG_ON(!list_empty(&phy->port_siblings));
672c7ebbbceSChristoph Hellwig 
673c7ebbbceSChristoph Hellwig 	transport_remove_device(dev);
674c7ebbbceSChristoph Hellwig 	device_del(dev);
675c7ebbbceSChristoph Hellwig 	transport_destroy_device(dev);
67692aab646SMike Anderson 	put_device(dev);
677c7ebbbceSChristoph Hellwig }
678c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_phy_delete);
679c7ebbbceSChristoph Hellwig 
680c7ebbbceSChristoph Hellwig /**
681eb44820cSRob Landley  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
682c7ebbbceSChristoph Hellwig  * @dev:	device to check
683c7ebbbceSChristoph Hellwig  *
684c7ebbbceSChristoph Hellwig  * Returns:
685c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS PHY, %0 else
686c7ebbbceSChristoph Hellwig  */
687c7ebbbceSChristoph Hellwig int scsi_is_sas_phy(const struct device *dev)
688c7ebbbceSChristoph Hellwig {
689c7ebbbceSChristoph Hellwig 	return dev->release == sas_phy_release;
690c7ebbbceSChristoph Hellwig }
691c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_phy);
692c7ebbbceSChristoph Hellwig 
693c7ebbbceSChristoph Hellwig /*
69465c92b09SJames Bottomley  * SAS Port attributes
69565c92b09SJames Bottomley  */
69665c92b09SJames Bottomley #define sas_port_show_simple(field, name, format_string, cast)		\
69765c92b09SJames Bottomley static ssize_t								\
69865c92b09SJames Bottomley show_sas_port_##name(struct class_device *cdev, char *buf)		\
69965c92b09SJames Bottomley {									\
70065c92b09SJames Bottomley 	struct sas_port *port = transport_class_to_sas_port(cdev);	\
70165c92b09SJames Bottomley 									\
70265c92b09SJames Bottomley 	return snprintf(buf, 20, format_string, cast port->field);	\
70365c92b09SJames Bottomley }
70465c92b09SJames Bottomley 
70565c92b09SJames Bottomley #define sas_port_simple_attr(field, name, format_string, type)		\
70665c92b09SJames Bottomley 	sas_port_show_simple(field, name, format_string, (type))	\
70765c92b09SJames Bottomley static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
70865c92b09SJames Bottomley 
70965c92b09SJames Bottomley sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
71065c92b09SJames Bottomley 
71165c92b09SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_port_class,
71265c92b09SJames Bottomley 			       "sas_port", NULL, NULL, NULL);
71365c92b09SJames Bottomley 
71465c92b09SJames Bottomley static int sas_port_match(struct attribute_container *cont, struct device *dev)
71565c92b09SJames Bottomley {
71665c92b09SJames Bottomley 	struct Scsi_Host *shost;
71765c92b09SJames Bottomley 	struct sas_internal *i;
71865c92b09SJames Bottomley 
71965c92b09SJames Bottomley 	if (!scsi_is_sas_port(dev))
72065c92b09SJames Bottomley 		return 0;
72165c92b09SJames Bottomley 	shost = dev_to_shost(dev->parent);
72265c92b09SJames Bottomley 
72365c92b09SJames Bottomley 	if (!shost->transportt)
72465c92b09SJames Bottomley 		return 0;
72565c92b09SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
72665c92b09SJames Bottomley 			&sas_host_class.class)
72765c92b09SJames Bottomley 		return 0;
72865c92b09SJames Bottomley 
72965c92b09SJames Bottomley 	i = to_sas_internal(shost->transportt);
73065c92b09SJames Bottomley 	return &i->port_attr_cont.ac == cont;
73165c92b09SJames Bottomley }
73265c92b09SJames Bottomley 
73365c92b09SJames Bottomley 
73465c92b09SJames Bottomley static void sas_port_release(struct device *dev)
73565c92b09SJames Bottomley {
73665c92b09SJames Bottomley 	struct sas_port *port = dev_to_sas_port(dev);
73765c92b09SJames Bottomley 
73865c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
73965c92b09SJames Bottomley 
74065c92b09SJames Bottomley 	put_device(dev->parent);
74165c92b09SJames Bottomley 	kfree(port);
74265c92b09SJames Bottomley }
74365c92b09SJames Bottomley 
74465c92b09SJames Bottomley static void sas_port_create_link(struct sas_port *port,
74565c92b09SJames Bottomley 				 struct sas_phy *phy)
74665c92b09SJames Bottomley {
74721434966SDarrick J. Wong 	int res;
74821434966SDarrick J. Wong 
74921434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
75021434966SDarrick J. Wong 				phy->dev.bus_id);
75121434966SDarrick J. Wong 	if (res)
75221434966SDarrick J. Wong 		goto err;
75321434966SDarrick J. Wong 	res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
75421434966SDarrick J. Wong 	if (res)
75521434966SDarrick J. Wong 		goto err;
75621434966SDarrick J. Wong 	return;
75721434966SDarrick J. Wong err:
75821434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
75921434966SDarrick J. Wong 	       __FUNCTION__, res);
76065c92b09SJames Bottomley }
76165c92b09SJames Bottomley 
76265c92b09SJames Bottomley static void sas_port_delete_link(struct sas_port *port,
76365c92b09SJames Bottomley 				 struct sas_phy *phy)
76465c92b09SJames Bottomley {
76565c92b09SJames Bottomley 	sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
76665c92b09SJames Bottomley 	sysfs_remove_link(&phy->dev.kobj, "port");
76765c92b09SJames Bottomley }
76865c92b09SJames Bottomley 
76965c92b09SJames Bottomley /** sas_port_alloc - allocate and initialize a SAS port structure
77065c92b09SJames Bottomley  *
77165c92b09SJames Bottomley  * @parent:	parent device
77265c92b09SJames Bottomley  * @port_id:	port number
77365c92b09SJames Bottomley  *
77465c92b09SJames Bottomley  * Allocates a SAS port structure.  It will be added to the device tree
77565c92b09SJames Bottomley  * below the device specified by @parent which must be either a Scsi_Host
77665c92b09SJames Bottomley  * or a sas_expander_device.
77765c92b09SJames Bottomley  *
77865c92b09SJames Bottomley  * Returns %NULL on error
77965c92b09SJames Bottomley  */
78065c92b09SJames Bottomley struct sas_port *sas_port_alloc(struct device *parent, int port_id)
78165c92b09SJames Bottomley {
78265c92b09SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
78365c92b09SJames Bottomley 	struct sas_port *port;
78465c92b09SJames Bottomley 
78565c92b09SJames Bottomley 	port = kzalloc(sizeof(*port), GFP_KERNEL);
78665c92b09SJames Bottomley 	if (!port)
78765c92b09SJames Bottomley 		return NULL;
78865c92b09SJames Bottomley 
78965c92b09SJames Bottomley 	port->port_identifier = port_id;
79065c92b09SJames Bottomley 
79165c92b09SJames Bottomley 	device_initialize(&port->dev);
79265c92b09SJames Bottomley 
79365c92b09SJames Bottomley 	port->dev.parent = get_device(parent);
79465c92b09SJames Bottomley 	port->dev.release = sas_port_release;
79565c92b09SJames Bottomley 
79665c92b09SJames Bottomley 	mutex_init(&port->phy_list_mutex);
79765c92b09SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
79865c92b09SJames Bottomley 
79965c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
80065c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
80165c92b09SJames Bottomley 		sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
80265c92b09SJames Bottomley 			rphy->scsi_target_id, port->port_identifier);
80365c92b09SJames Bottomley 	} else
80465c92b09SJames Bottomley 		sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
80565c92b09SJames Bottomley 			port->port_identifier);
80665c92b09SJames Bottomley 
80765c92b09SJames Bottomley 	transport_setup_device(&port->dev);
80865c92b09SJames Bottomley 
80965c92b09SJames Bottomley 	return port;
81065c92b09SJames Bottomley }
81165c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_alloc);
81265c92b09SJames Bottomley 
813c9fefeb2SJames Bottomley /** sas_port_alloc_num - allocate and initialize a SAS port structure
814c9fefeb2SJames Bottomley  *
815c9fefeb2SJames Bottomley  * @parent:	parent device
816c9fefeb2SJames Bottomley  *
817c9fefeb2SJames Bottomley  * Allocates a SAS port structure and a number to go with it.  This
818c9fefeb2SJames Bottomley  * interface is really for adapters where the port number has no
819c9fefeb2SJames Bottomley  * meansing, so the sas class should manage them.  It will be added to
820c9fefeb2SJames Bottomley  * the device tree below the device specified by @parent which must be
821c9fefeb2SJames Bottomley  * either a Scsi_Host or a sas_expander_device.
822c9fefeb2SJames Bottomley  *
823c9fefeb2SJames Bottomley  * Returns %NULL on error
824c9fefeb2SJames Bottomley  */
825c9fefeb2SJames Bottomley struct sas_port *sas_port_alloc_num(struct device *parent)
826c9fefeb2SJames Bottomley {
827c9fefeb2SJames Bottomley 	int index;
828c9fefeb2SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(parent);
829c9fefeb2SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
830c9fefeb2SJames Bottomley 
831c9fefeb2SJames Bottomley 	/* FIXME: use idr for this eventually */
832c9fefeb2SJames Bottomley 	mutex_lock(&sas_host->lock);
833c9fefeb2SJames Bottomley 	if (scsi_is_sas_expander_device(parent)) {
834c9fefeb2SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent);
835c9fefeb2SJames Bottomley 		struct sas_expander_device *exp = rphy_to_expander_device(rphy);
836c9fefeb2SJames Bottomley 
837c9fefeb2SJames Bottomley 		index = exp->next_port_id++;
838c9fefeb2SJames Bottomley 	} else
839c9fefeb2SJames Bottomley 		index = sas_host->next_port_id++;
840c9fefeb2SJames Bottomley 	mutex_unlock(&sas_host->lock);
841c9fefeb2SJames Bottomley 	return sas_port_alloc(parent, index);
842c9fefeb2SJames Bottomley }
843c9fefeb2SJames Bottomley EXPORT_SYMBOL(sas_port_alloc_num);
844c9fefeb2SJames Bottomley 
84565c92b09SJames Bottomley /**
84665c92b09SJames Bottomley  * sas_port_add - add a SAS port to the device hierarchy
84765c92b09SJames Bottomley  * @port:	port to be added
84865c92b09SJames Bottomley  *
84965c92b09SJames Bottomley  * publishes a port to the rest of the system
85065c92b09SJames Bottomley  */
85165c92b09SJames Bottomley int sas_port_add(struct sas_port *port)
85265c92b09SJames Bottomley {
85365c92b09SJames Bottomley 	int error;
85465c92b09SJames Bottomley 
85565c92b09SJames Bottomley 	/* No phys should be added until this is made visible */
85665c92b09SJames Bottomley 	BUG_ON(!list_empty(&port->phy_list));
85765c92b09SJames Bottomley 
85865c92b09SJames Bottomley 	error = device_add(&port->dev);
85965c92b09SJames Bottomley 
86065c92b09SJames Bottomley 	if (error)
86165c92b09SJames Bottomley 		return error;
86265c92b09SJames Bottomley 
86365c92b09SJames Bottomley 	transport_add_device(&port->dev);
86465c92b09SJames Bottomley 	transport_configure_device(&port->dev);
86565c92b09SJames Bottomley 
86665c92b09SJames Bottomley 	return 0;
86765c92b09SJames Bottomley }
86865c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add);
86965c92b09SJames Bottomley 
87065c92b09SJames Bottomley /**
871eb44820cSRob Landley  * sas_port_free  -  free a SAS PORT
87265c92b09SJames Bottomley  * @port:	SAS PORT to free
87365c92b09SJames Bottomley  *
87465c92b09SJames Bottomley  * Frees the specified SAS PORT.
87565c92b09SJames Bottomley  *
87665c92b09SJames Bottomley  * Note:
87765c92b09SJames Bottomley  *   This function must only be called on a PORT that has not
87865c92b09SJames Bottomley  *   sucessfully been added using sas_port_add().
87965c92b09SJames Bottomley  */
88065c92b09SJames Bottomley void sas_port_free(struct sas_port *port)
88165c92b09SJames Bottomley {
88265c92b09SJames Bottomley 	transport_destroy_device(&port->dev);
88365c92b09SJames Bottomley 	put_device(&port->dev);
88465c92b09SJames Bottomley }
88565c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_free);
88665c92b09SJames Bottomley 
88765c92b09SJames Bottomley /**
888eb44820cSRob Landley  * sas_port_delete  -  remove SAS PORT
88965c92b09SJames Bottomley  * @port:	SAS PORT to remove
89065c92b09SJames Bottomley  *
89165c92b09SJames Bottomley  * Removes the specified SAS PORT.  If the SAS PORT has an
89265c92b09SJames Bottomley  * associated phys, unlink them from the port as well.
89365c92b09SJames Bottomley  */
89465c92b09SJames Bottomley void sas_port_delete(struct sas_port *port)
89565c92b09SJames Bottomley {
89665c92b09SJames Bottomley 	struct device *dev = &port->dev;
89765c92b09SJames Bottomley 	struct sas_phy *phy, *tmp_phy;
89865c92b09SJames Bottomley 
89965c92b09SJames Bottomley 	if (port->rphy) {
90065c92b09SJames Bottomley 		sas_rphy_delete(port->rphy);
90165c92b09SJames Bottomley 		port->rphy = NULL;
90265c92b09SJames Bottomley 	}
90365c92b09SJames Bottomley 
90465c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
90565c92b09SJames Bottomley 	list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
90665c92b09SJames Bottomley 				 port_siblings) {
90765c92b09SJames Bottomley 		sas_port_delete_link(port, phy);
90865c92b09SJames Bottomley 		list_del_init(&phy->port_siblings);
90965c92b09SJames Bottomley 	}
91065c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
91165c92b09SJames Bottomley 
912a0e1b6efSJames Bottomley 	if (port->is_backlink) {
913a0e1b6efSJames Bottomley 		struct device *parent = port->dev.parent;
914a0e1b6efSJames Bottomley 
915a0e1b6efSJames Bottomley 		sysfs_remove_link(&port->dev.kobj, parent->bus_id);
916a0e1b6efSJames Bottomley 		port->is_backlink = 0;
917a0e1b6efSJames Bottomley 	}
918a0e1b6efSJames Bottomley 
91965c92b09SJames Bottomley 	transport_remove_device(dev);
92065c92b09SJames Bottomley 	device_del(dev);
92165c92b09SJames Bottomley 	transport_destroy_device(dev);
92265c92b09SJames Bottomley 	put_device(dev);
92365c92b09SJames Bottomley }
92465c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete);
92565c92b09SJames Bottomley 
92665c92b09SJames Bottomley /**
927eb44820cSRob Landley  * scsi_is_sas_port -  check if a struct device represents a SAS port
92865c92b09SJames Bottomley  * @dev:	device to check
92965c92b09SJames Bottomley  *
93065c92b09SJames Bottomley  * Returns:
93165c92b09SJames Bottomley  *	%1 if the device represents a SAS Port, %0 else
93265c92b09SJames Bottomley  */
93365c92b09SJames Bottomley int scsi_is_sas_port(const struct device *dev)
93465c92b09SJames Bottomley {
93565c92b09SJames Bottomley 	return dev->release == sas_port_release;
93665c92b09SJames Bottomley }
93765c92b09SJames Bottomley EXPORT_SYMBOL(scsi_is_sas_port);
93865c92b09SJames Bottomley 
93965c92b09SJames Bottomley /**
94065c92b09SJames Bottomley  * sas_port_add_phy - add another phy to a port to form a wide port
94165c92b09SJames Bottomley  * @port:	port to add the phy to
94265c92b09SJames Bottomley  * @phy:	phy to add
94365c92b09SJames Bottomley  *
94465c92b09SJames Bottomley  * When a port is initially created, it is empty (has no phys).  All
94565c92b09SJames Bottomley  * ports must have at least one phy to operated, and all wide ports
94665c92b09SJames Bottomley  * must have at least two.  The current code makes no difference
94765c92b09SJames Bottomley  * between ports and wide ports, but the only object that can be
94865c92b09SJames Bottomley  * connected to a remote device is a port, so ports must be formed on
94965c92b09SJames Bottomley  * all devices with phys if they're connected to anything.
95065c92b09SJames Bottomley  */
95165c92b09SJames Bottomley void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
95265c92b09SJames Bottomley {
95365c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
95465c92b09SJames Bottomley 	if (unlikely(!list_empty(&phy->port_siblings))) {
95565c92b09SJames Bottomley 		/* make sure we're already on this port */
95665c92b09SJames Bottomley 		struct sas_phy *tmp;
95765c92b09SJames Bottomley 
95865c92b09SJames Bottomley 		list_for_each_entry(tmp, &port->phy_list, port_siblings)
95965c92b09SJames Bottomley 			if (tmp == phy)
96065c92b09SJames Bottomley 				break;
96165c92b09SJames Bottomley 		/* If this trips, you added a phy that was already
96265c92b09SJames Bottomley 		 * part of a different port */
96365c92b09SJames Bottomley 		if (unlikely(tmp != phy)) {
96465c92b09SJames Bottomley 			dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
96565c92b09SJames Bottomley 			BUG();
96665c92b09SJames Bottomley 		}
96765c92b09SJames Bottomley 	} else {
96865c92b09SJames Bottomley 		sas_port_create_link(port, phy);
96965c92b09SJames Bottomley 		list_add_tail(&phy->port_siblings, &port->phy_list);
97065c92b09SJames Bottomley 		port->num_phys++;
97165c92b09SJames Bottomley 	}
97265c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
97365c92b09SJames Bottomley }
97465c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_add_phy);
97565c92b09SJames Bottomley 
97665c92b09SJames Bottomley /**
97765c92b09SJames Bottomley  * sas_port_delete_phy - remove a phy from a port or wide port
97865c92b09SJames Bottomley  * @port:	port to remove the phy from
97965c92b09SJames Bottomley  * @phy:	phy to remove
98065c92b09SJames Bottomley  *
98165c92b09SJames Bottomley  * This operation is used for tearing down ports again.  It must be
98265c92b09SJames Bottomley  * done to every port or wide port before calling sas_port_delete.
98365c92b09SJames Bottomley  */
98465c92b09SJames Bottomley void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
98565c92b09SJames Bottomley {
98665c92b09SJames Bottomley 	mutex_lock(&port->phy_list_mutex);
98765c92b09SJames Bottomley 	sas_port_delete_link(port, phy);
98865c92b09SJames Bottomley 	list_del_init(&phy->port_siblings);
98965c92b09SJames Bottomley 	port->num_phys--;
99065c92b09SJames Bottomley 	mutex_unlock(&port->phy_list_mutex);
99165c92b09SJames Bottomley }
99265c92b09SJames Bottomley EXPORT_SYMBOL(sas_port_delete_phy);
99365c92b09SJames Bottomley 
994a0e1b6efSJames Bottomley void sas_port_mark_backlink(struct sas_port *port)
995a0e1b6efSJames Bottomley {
99621434966SDarrick J. Wong 	int res;
997a0e1b6efSJames Bottomley 	struct device *parent = port->dev.parent->parent->parent;
998a0e1b6efSJames Bottomley 
999a0e1b6efSJames Bottomley 	if (port->is_backlink)
1000a0e1b6efSJames Bottomley 		return;
1001a0e1b6efSJames Bottomley 	port->is_backlink = 1;
100221434966SDarrick J. Wong 	res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1003a0e1b6efSJames Bottomley 				parent->bus_id);
100421434966SDarrick J. Wong 	if (res)
100521434966SDarrick J. Wong 		goto err;
100621434966SDarrick J. Wong 	return;
100721434966SDarrick J. Wong err:
100821434966SDarrick J. Wong 	printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
100921434966SDarrick J. Wong 	       __FUNCTION__, res);
1010a0e1b6efSJames Bottomley 
1011a0e1b6efSJames Bottomley }
1012a0e1b6efSJames Bottomley EXPORT_SYMBOL(sas_port_mark_backlink);
1013a0e1b6efSJames Bottomley 
101465c92b09SJames Bottomley /*
1015c7ebbbceSChristoph Hellwig  * SAS remote PHY attributes.
1016c7ebbbceSChristoph Hellwig  */
1017c7ebbbceSChristoph Hellwig 
1018c7ebbbceSChristoph Hellwig #define sas_rphy_show_simple(field, name, format_string, cast)		\
1019c7ebbbceSChristoph Hellwig static ssize_t								\
1020c7ebbbceSChristoph Hellwig show_sas_rphy_##name(struct class_device *cdev, char *buf)		\
1021c7ebbbceSChristoph Hellwig {									\
1022c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);	\
1023c7ebbbceSChristoph Hellwig 									\
1024c7ebbbceSChristoph Hellwig 	return snprintf(buf, 20, format_string, cast rphy->field);	\
1025c7ebbbceSChristoph Hellwig }
1026c7ebbbceSChristoph Hellwig 
1027c7ebbbceSChristoph Hellwig #define sas_rphy_simple_attr(field, name, format_string, type)		\
1028c7ebbbceSChristoph Hellwig 	sas_rphy_show_simple(field, name, format_string, (type))	\
1029c7ebbbceSChristoph Hellwig static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, 			\
1030c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1031c7ebbbceSChristoph Hellwig 
1032c7ebbbceSChristoph Hellwig #define sas_rphy_show_protocol(field, name)				\
1033c7ebbbceSChristoph Hellwig static ssize_t								\
1034c7ebbbceSChristoph Hellwig show_sas_rphy_##name(struct class_device *cdev, char *buf)		\
1035c7ebbbceSChristoph Hellwig {									\
1036c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);	\
1037c7ebbbceSChristoph Hellwig 									\
1038c7ebbbceSChristoph Hellwig 	if (!rphy->field)					\
1039c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");			\
1040c7ebbbceSChristoph Hellwig 	return get_sas_protocol_names(rphy->field, buf);	\
1041c7ebbbceSChristoph Hellwig }
1042c7ebbbceSChristoph Hellwig 
1043c7ebbbceSChristoph Hellwig #define sas_rphy_protocol_attr(field, name)				\
1044c7ebbbceSChristoph Hellwig 	sas_rphy_show_protocol(field, name)				\
1045c7ebbbceSChristoph Hellwig static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,			\
1046c7ebbbceSChristoph Hellwig 		show_sas_rphy_##name, NULL)
1047c7ebbbceSChristoph Hellwig 
1048c7ebbbceSChristoph Hellwig static ssize_t
1049c7ebbbceSChristoph Hellwig show_sas_rphy_device_type(struct class_device *cdev, char *buf)
1050c7ebbbceSChristoph Hellwig {
1051c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1052c7ebbbceSChristoph Hellwig 
1053c7ebbbceSChristoph Hellwig 	if (!rphy->identify.device_type)
1054c7ebbbceSChristoph Hellwig 		return snprintf(buf, 20, "none\n");
1055c7ebbbceSChristoph Hellwig 	return get_sas_device_type_names(
1056c7ebbbceSChristoph Hellwig 			rphy->identify.device_type, buf);
1057c7ebbbceSChristoph Hellwig }
1058c7ebbbceSChristoph Hellwig 
1059c7ebbbceSChristoph Hellwig static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1060c7ebbbceSChristoph Hellwig 		show_sas_rphy_device_type, NULL);
1061c7ebbbceSChristoph Hellwig 
1062a0125641SChristoph Hellwig static ssize_t
1063a0125641SChristoph Hellwig show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
1064a0125641SChristoph Hellwig {
1065a0125641SChristoph Hellwig 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1066a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1067a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1068a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1069a0125641SChristoph Hellwig 	u64 identifier;
1070a0125641SChristoph Hellwig 	int error;
1071a0125641SChristoph Hellwig 
1072a0125641SChristoph Hellwig 	/*
1073a0125641SChristoph Hellwig 	 * Only devices behind an expander are supported, because the
1074a0125641SChristoph Hellwig 	 * enclosure identifier is a SMP feature.
1075a0125641SChristoph Hellwig 	 */
1076f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1077a0125641SChristoph Hellwig 		return -EINVAL;
1078a0125641SChristoph Hellwig 
1079a0125641SChristoph Hellwig 	error = i->f->get_enclosure_identifier(rphy, &identifier);
1080a0125641SChristoph Hellwig 	if (error)
1081a0125641SChristoph Hellwig 		return error;
1082a0125641SChristoph Hellwig 	return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1083a0125641SChristoph Hellwig }
1084a0125641SChristoph Hellwig 
1085a0125641SChristoph Hellwig static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1086a0125641SChristoph Hellwig 		show_sas_rphy_enclosure_identifier, NULL);
1087a0125641SChristoph Hellwig 
1088a0125641SChristoph Hellwig static ssize_t
1089a0125641SChristoph Hellwig show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
1090a0125641SChristoph Hellwig {
1091a0125641SChristoph Hellwig 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1092a0125641SChristoph Hellwig 	struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1093a0125641SChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1094a0125641SChristoph Hellwig 	struct sas_internal *i = to_sas_internal(shost->transportt);
1095a0125641SChristoph Hellwig 	int val;
1096a0125641SChristoph Hellwig 
1097f4ad7b58SJames Bottomley 	if (scsi_is_sas_phy_local(phy))
1098a0125641SChristoph Hellwig 		return -EINVAL;
1099a0125641SChristoph Hellwig 
1100a0125641SChristoph Hellwig 	val = i->f->get_bay_identifier(rphy);
1101a0125641SChristoph Hellwig 	if (val < 0)
1102a0125641SChristoph Hellwig 		return val;
1103a0125641SChristoph Hellwig 	return sprintf(buf, "%d\n", val);
1104a0125641SChristoph Hellwig }
1105a0125641SChristoph Hellwig 
1106a0125641SChristoph Hellwig static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1107a0125641SChristoph Hellwig 		show_sas_rphy_bay_identifier, NULL);
1108a0125641SChristoph Hellwig 
1109c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.initiator_port_protocols,
1110c7ebbbceSChristoph Hellwig 		initiator_port_protocols);
1111c7ebbbceSChristoph Hellwig sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1112c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1113c7ebbbceSChristoph Hellwig 		unsigned long long);
1114c7ebbbceSChristoph Hellwig sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1115c7ebbbceSChristoph Hellwig 
111642ab0360SJames Bottomley /* only need 8 bytes of data plus header (4 or 8) */
111742ab0360SJames Bottomley #define BUF_SIZE 64
111842ab0360SJames Bottomley 
111942ab0360SJames Bottomley int sas_read_port_mode_page(struct scsi_device *sdev)
112042ab0360SJames Bottomley {
112142ab0360SJames Bottomley 	char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
112242ab0360SJames Bottomley 	struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
112342ab0360SJames Bottomley 	struct sas_end_device *rdev;
112442ab0360SJames Bottomley 	struct scsi_mode_data mode_data;
112542ab0360SJames Bottomley 	int res, error;
112642ab0360SJames Bottomley 
112742ab0360SJames Bottomley 	BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
112842ab0360SJames Bottomley 
112942ab0360SJames Bottomley 	rdev = rphy_to_end_device(rphy);
113042ab0360SJames Bottomley 
113142ab0360SJames Bottomley 	if (!buffer)
113242ab0360SJames Bottomley 		return -ENOMEM;
113342ab0360SJames Bottomley 
113442ab0360SJames Bottomley 	res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
113542ab0360SJames Bottomley 			      &mode_data, NULL);
113642ab0360SJames Bottomley 
113742ab0360SJames Bottomley 	error = -EINVAL;
113842ab0360SJames Bottomley 	if (!scsi_status_is_good(res))
113942ab0360SJames Bottomley 		goto out;
114042ab0360SJames Bottomley 
114142ab0360SJames Bottomley 	msdata = buffer +  mode_data.header_length +
114242ab0360SJames Bottomley 		mode_data.block_descriptor_length;
114342ab0360SJames Bottomley 
114442ab0360SJames Bottomley 	if (msdata - buffer > BUF_SIZE - 8)
114542ab0360SJames Bottomley 		goto out;
114642ab0360SJames Bottomley 
114742ab0360SJames Bottomley 	error = 0;
114842ab0360SJames Bottomley 
114942ab0360SJames Bottomley 	rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
115042ab0360SJames Bottomley 	rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
115142ab0360SJames Bottomley 	rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
115242ab0360SJames Bottomley 
115342ab0360SJames Bottomley  out:
115442ab0360SJames Bottomley 	kfree(buffer);
115542ab0360SJames Bottomley 	return error;
115642ab0360SJames Bottomley }
115742ab0360SJames Bottomley EXPORT_SYMBOL(sas_read_port_mode_page);
115842ab0360SJames Bottomley 
115979cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
116079cb1819SJames Bottomley 			       "sas_end_device", NULL, NULL, NULL);
116179cb1819SJames Bottomley 
116242ab0360SJames Bottomley #define sas_end_dev_show_simple(field, name, format_string, cast)	\
116342ab0360SJames Bottomley static ssize_t								\
116442ab0360SJames Bottomley show_sas_end_dev_##name(struct class_device *cdev, char *buf)		\
116542ab0360SJames Bottomley {									\
116642ab0360SJames Bottomley 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);		\
116742ab0360SJames Bottomley 	struct sas_end_device *rdev = rphy_to_end_device(rphy);		\
116842ab0360SJames Bottomley 									\
116942ab0360SJames Bottomley 	return snprintf(buf, 20, format_string, cast rdev->field);	\
117042ab0360SJames Bottomley }
117142ab0360SJames Bottomley 
117242ab0360SJames Bottomley #define sas_end_dev_simple_attr(field, name, format_string, type)	\
117342ab0360SJames Bottomley 	sas_end_dev_show_simple(field, name, format_string, (type))	\
117442ab0360SJames Bottomley static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, 			\
117542ab0360SJames Bottomley 		show_sas_end_dev_##name, NULL)
117642ab0360SJames Bottomley 
117742ab0360SJames Bottomley sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
117842ab0360SJames Bottomley sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
117942ab0360SJames Bottomley 			"%d\n", int);
118042ab0360SJames Bottomley sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
118142ab0360SJames Bottomley 			"%d\n", int);
118242ab0360SJames Bottomley 
118379cb1819SJames Bottomley static DECLARE_TRANSPORT_CLASS(sas_expander_class,
118479cb1819SJames Bottomley 			       "sas_expander", NULL, NULL, NULL);
118579cb1819SJames Bottomley 
118679cb1819SJames Bottomley #define sas_expander_show_simple(field, name, format_string, cast)	\
118779cb1819SJames Bottomley static ssize_t								\
118879cb1819SJames Bottomley show_sas_expander_##name(struct class_device *cdev, char *buf)		\
118979cb1819SJames Bottomley {									\
119079cb1819SJames Bottomley 	struct sas_rphy *rphy = transport_class_to_rphy(cdev);		\
119179cb1819SJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
119279cb1819SJames Bottomley 									\
119379cb1819SJames Bottomley 	return snprintf(buf, 20, format_string, cast edev->field);	\
119479cb1819SJames Bottomley }
119579cb1819SJames Bottomley 
119679cb1819SJames Bottomley #define sas_expander_simple_attr(field, name, format_string, type)	\
119779cb1819SJames Bottomley 	sas_expander_show_simple(field, name, format_string, (type))	\
119879cb1819SJames Bottomley static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, 			\
119979cb1819SJames Bottomley 		show_sas_expander_##name, NULL)
120079cb1819SJames Bottomley 
120179cb1819SJames Bottomley sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
120279cb1819SJames Bottomley sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
120379cb1819SJames Bottomley sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
120479cb1819SJames Bottomley sas_expander_simple_attr(component_vendor_id, component_vendor_id,
120579cb1819SJames Bottomley 			 "%s\n", char *);
120679cb1819SJames Bottomley sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
120779cb1819SJames Bottomley sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
120879cb1819SJames Bottomley 			 unsigned int);
120979cb1819SJames Bottomley sas_expander_simple_attr(level, level, "%d\n", int);
121042ab0360SJames Bottomley 
1211c7ebbbceSChristoph Hellwig static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
12122f8600dfSJames Bottomley 		"sas_device", NULL, NULL, NULL);
1213c7ebbbceSChristoph Hellwig 
1214c7ebbbceSChristoph Hellwig static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1215c7ebbbceSChristoph Hellwig {
1216c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost;
1217c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1218c7ebbbceSChristoph Hellwig 
1219c7ebbbceSChristoph Hellwig 	if (!scsi_is_sas_rphy(dev))
1220c7ebbbceSChristoph Hellwig 		return 0;
1221c7ebbbceSChristoph Hellwig 	shost = dev_to_shost(dev->parent->parent);
1222c7ebbbceSChristoph Hellwig 
1223c7ebbbceSChristoph Hellwig 	if (!shost->transportt)
1224c7ebbbceSChristoph Hellwig 		return 0;
1225c7ebbbceSChristoph Hellwig 	if (shost->transportt->host_attrs.ac.class !=
1226c7ebbbceSChristoph Hellwig 			&sas_host_class.class)
1227c7ebbbceSChristoph Hellwig 		return 0;
1228c7ebbbceSChristoph Hellwig 
1229c7ebbbceSChristoph Hellwig 	i = to_sas_internal(shost->transportt);
1230c7ebbbceSChristoph Hellwig 	return &i->rphy_attr_cont.ac == cont;
1231c7ebbbceSChristoph Hellwig }
1232c7ebbbceSChristoph Hellwig 
123342ab0360SJames Bottomley static int sas_end_dev_match(struct attribute_container *cont,
123442ab0360SJames Bottomley 			     struct device *dev)
123542ab0360SJames Bottomley {
123642ab0360SJames Bottomley 	struct Scsi_Host *shost;
123742ab0360SJames Bottomley 	struct sas_internal *i;
123842ab0360SJames Bottomley 	struct sas_rphy *rphy;
123942ab0360SJames Bottomley 
124042ab0360SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
124142ab0360SJames Bottomley 		return 0;
124242ab0360SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
124342ab0360SJames Bottomley 	rphy = dev_to_rphy(dev);
124442ab0360SJames Bottomley 
124542ab0360SJames Bottomley 	if (!shost->transportt)
124642ab0360SJames Bottomley 		return 0;
124742ab0360SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
124842ab0360SJames Bottomley 			&sas_host_class.class)
124942ab0360SJames Bottomley 		return 0;
125042ab0360SJames Bottomley 
125142ab0360SJames Bottomley 	i = to_sas_internal(shost->transportt);
125242ab0360SJames Bottomley 	return &i->end_dev_attr_cont.ac == cont &&
12532f8600dfSJames Bottomley 		rphy->identify.device_type == SAS_END_DEVICE;
125442ab0360SJames Bottomley }
125542ab0360SJames Bottomley 
125679cb1819SJames Bottomley static int sas_expander_match(struct attribute_container *cont,
125779cb1819SJames Bottomley 			      struct device *dev)
125879cb1819SJames Bottomley {
125979cb1819SJames Bottomley 	struct Scsi_Host *shost;
126079cb1819SJames Bottomley 	struct sas_internal *i;
126179cb1819SJames Bottomley 	struct sas_rphy *rphy;
126279cb1819SJames Bottomley 
126379cb1819SJames Bottomley 	if (!scsi_is_sas_rphy(dev))
126479cb1819SJames Bottomley 		return 0;
126579cb1819SJames Bottomley 	shost = dev_to_shost(dev->parent->parent);
126679cb1819SJames Bottomley 	rphy = dev_to_rphy(dev);
126779cb1819SJames Bottomley 
126879cb1819SJames Bottomley 	if (!shost->transportt)
126979cb1819SJames Bottomley 		return 0;
127079cb1819SJames Bottomley 	if (shost->transportt->host_attrs.ac.class !=
127179cb1819SJames Bottomley 			&sas_host_class.class)
127279cb1819SJames Bottomley 		return 0;
127379cb1819SJames Bottomley 
127479cb1819SJames Bottomley 	i = to_sas_internal(shost->transportt);
127579cb1819SJames Bottomley 	return &i->expander_attr_cont.ac == cont &&
127679cb1819SJames Bottomley 		(rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
12772f8600dfSJames Bottomley 		 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
127879cb1819SJames Bottomley }
127979cb1819SJames Bottomley 
12802f8600dfSJames Bottomley static void sas_expander_release(struct device *dev)
1281c7ebbbceSChristoph Hellwig {
1282c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy = dev_to_rphy(dev);
12832f8600dfSJames Bottomley 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1284c7ebbbceSChristoph Hellwig 
1285c7ebbbceSChristoph Hellwig 	put_device(dev->parent);
12862f8600dfSJames Bottomley 	kfree(edev);
1287c7ebbbceSChristoph Hellwig }
1288c7ebbbceSChristoph Hellwig 
12892f8600dfSJames Bottomley static void sas_end_device_release(struct device *dev)
1290c7ebbbceSChristoph Hellwig {
12912f8600dfSJames Bottomley 	struct sas_rphy *rphy = dev_to_rphy(dev);
12922f8600dfSJames Bottomley 	struct sas_end_device *edev = rphy_to_end_device(rphy);
1293c7ebbbceSChristoph Hellwig 
12942f8600dfSJames Bottomley 	put_device(dev->parent);
12952f8600dfSJames Bottomley 	kfree(edev);
1296c7ebbbceSChristoph Hellwig }
1297c7ebbbceSChristoph Hellwig 
1298c7ebbbceSChristoph Hellwig /**
1299c5943d36SJames Bottomley  * sas_rphy_initialize - common rphy intialization
1300c5943d36SJames Bottomley  * @rphy:	rphy to initialise
1301c5943d36SJames Bottomley  *
1302c5943d36SJames Bottomley  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1303c5943d36SJames Bottomley  * initialise the common rphy component of each.
1304c5943d36SJames Bottomley  */
1305c5943d36SJames Bottomley static void sas_rphy_initialize(struct sas_rphy *rphy)
1306c5943d36SJames Bottomley {
1307c5943d36SJames Bottomley 	INIT_LIST_HEAD(&rphy->list);
1308c5943d36SJames Bottomley }
1309c5943d36SJames Bottomley 
1310c5943d36SJames Bottomley /**
131142ab0360SJames Bottomley  * sas_end_device_alloc - allocate an rphy for an end device
1312eb44820cSRob Landley  * @parent: which port
131342ab0360SJames Bottomley  *
131442ab0360SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
131542ab0360SJames Bottomley  *
131642ab0360SJames Bottomley  * Returns:
131742ab0360SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
131842ab0360SJames Bottomley  */
131965c92b09SJames Bottomley struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
132042ab0360SJames Bottomley {
132142ab0360SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
132242ab0360SJames Bottomley 	struct sas_end_device *rdev;
132342ab0360SJames Bottomley 
132442ab0360SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
132542ab0360SJames Bottomley 	if (!rdev) {
132642ab0360SJames Bottomley 		return NULL;
132742ab0360SJames Bottomley 	}
132842ab0360SJames Bottomley 
132942ab0360SJames Bottomley 	device_initialize(&rdev->rphy.dev);
133042ab0360SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
13312f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_end_device_release;
133265c92b09SJames Bottomley 	if (scsi_is_sas_expander_device(parent->dev.parent)) {
133365c92b09SJames Bottomley 		struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
133465c92b09SJames Bottomley 		sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
133565c92b09SJames Bottomley 			shost->host_no, rphy->scsi_target_id, parent->port_identifier);
133665c92b09SJames Bottomley 	} else
133765c92b09SJames Bottomley 		sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
133865c92b09SJames Bottomley 			shost->host_no, parent->port_identifier);
133942ab0360SJames Bottomley 	rdev->rphy.identify.device_type = SAS_END_DEVICE;
1340c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
134142ab0360SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
134242ab0360SJames Bottomley 
134342ab0360SJames Bottomley 	return &rdev->rphy;
134442ab0360SJames Bottomley }
134542ab0360SJames Bottomley EXPORT_SYMBOL(sas_end_device_alloc);
134642ab0360SJames Bottomley 
134779cb1819SJames Bottomley /**
134879cb1819SJames Bottomley  * sas_expander_alloc - allocate an rphy for an end device
1349eb44820cSRob Landley  * @parent: which port
1350eb44820cSRob Landley  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
135179cb1819SJames Bottomley  *
135279cb1819SJames Bottomley  * Allocates an SAS remote PHY structure, connected to @parent.
135379cb1819SJames Bottomley  *
135479cb1819SJames Bottomley  * Returns:
135579cb1819SJames Bottomley  *	SAS PHY allocated or %NULL if the allocation failed.
135679cb1819SJames Bottomley  */
135765c92b09SJames Bottomley struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
135879cb1819SJames Bottomley 				    enum sas_device_type type)
135979cb1819SJames Bottomley {
136079cb1819SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&parent->dev);
136179cb1819SJames Bottomley 	struct sas_expander_device *rdev;
136279cb1819SJames Bottomley 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
136379cb1819SJames Bottomley 
136479cb1819SJames Bottomley 	BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
136579cb1819SJames Bottomley 	       type != SAS_FANOUT_EXPANDER_DEVICE);
136679cb1819SJames Bottomley 
136779cb1819SJames Bottomley 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
136879cb1819SJames Bottomley 	if (!rdev) {
136979cb1819SJames Bottomley 		return NULL;
137079cb1819SJames Bottomley 	}
137179cb1819SJames Bottomley 
137279cb1819SJames Bottomley 	device_initialize(&rdev->rphy.dev);
137379cb1819SJames Bottomley 	rdev->rphy.dev.parent = get_device(&parent->dev);
13742f8600dfSJames Bottomley 	rdev->rphy.dev.release = sas_expander_release;
137579cb1819SJames Bottomley 	mutex_lock(&sas_host->lock);
137679cb1819SJames Bottomley 	rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
137779cb1819SJames Bottomley 	mutex_unlock(&sas_host->lock);
137879cb1819SJames Bottomley 	sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
137979cb1819SJames Bottomley 		shost->host_no, rdev->rphy.scsi_target_id);
138079cb1819SJames Bottomley 	rdev->rphy.identify.device_type = type;
1381c5943d36SJames Bottomley 	sas_rphy_initialize(&rdev->rphy);
138279cb1819SJames Bottomley 	transport_setup_device(&rdev->rphy.dev);
138379cb1819SJames Bottomley 
138479cb1819SJames Bottomley 	return &rdev->rphy;
138579cb1819SJames Bottomley }
138679cb1819SJames Bottomley EXPORT_SYMBOL(sas_expander_alloc);
138742ab0360SJames Bottomley 
138842ab0360SJames Bottomley /**
1389eb44820cSRob Landley  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1390c7ebbbceSChristoph Hellwig  * @rphy:	The remote PHY to be added
1391c7ebbbceSChristoph Hellwig  *
1392c7ebbbceSChristoph Hellwig  * Publishes a SAS remote PHY to the rest of the system.
1393c7ebbbceSChristoph Hellwig  */
1394c7ebbbceSChristoph Hellwig int sas_rphy_add(struct sas_rphy *rphy)
1395c7ebbbceSChristoph Hellwig {
139665c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1397c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1398c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1399c7ebbbceSChristoph Hellwig 	struct sas_identify *identify = &rphy->identify;
1400c7ebbbceSChristoph Hellwig 	int error;
1401c7ebbbceSChristoph Hellwig 
1402c7ebbbceSChristoph Hellwig 	if (parent->rphy)
1403c7ebbbceSChristoph Hellwig 		return -ENXIO;
1404c7ebbbceSChristoph Hellwig 	parent->rphy = rphy;
1405c7ebbbceSChristoph Hellwig 
1406c7ebbbceSChristoph Hellwig 	error = device_add(&rphy->dev);
1407c7ebbbceSChristoph Hellwig 	if (error)
1408c7ebbbceSChristoph Hellwig 		return error;
1409c7ebbbceSChristoph Hellwig 	transport_add_device(&rphy->dev);
1410c7ebbbceSChristoph Hellwig 	transport_configure_device(&rphy->dev);
141139dca558SJames Bottomley 	if (sas_bsg_initialize(shost, rphy))
141239dca558SJames Bottomley 		printk("fail to a bsg device %s\n", rphy->dev.bus_id);
141339dca558SJames Bottomley 
1414c7ebbbceSChristoph Hellwig 
1415e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1416c7ebbbceSChristoph Hellwig 	list_add_tail(&rphy->list, &sas_host->rphy_list);
1417c7ebbbceSChristoph Hellwig 	if (identify->device_type == SAS_END_DEVICE &&
1418c7ebbbceSChristoph Hellwig 	    (identify->target_port_protocols &
1419c7ebbbceSChristoph Hellwig 	     (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1420c7ebbbceSChristoph Hellwig 		rphy->scsi_target_id = sas_host->next_target_id++;
14217676f83aSJames Bottomley 	else if (identify->device_type == SAS_END_DEVICE)
14227676f83aSJames Bottomley 		rphy->scsi_target_id = -1;
1423e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1424c7ebbbceSChristoph Hellwig 
142579cb1819SJames Bottomley 	if (identify->device_type == SAS_END_DEVICE &&
142679cb1819SJames Bottomley 	    rphy->scsi_target_id != -1) {
1427e8bf3941SJames Bottomley 		scsi_scan_target(&rphy->dev, 0,
1428c8490f3aSDarrick J. Wong 				rphy->scsi_target_id, SCAN_WILD_CARD, 0);
1429c7ebbbceSChristoph Hellwig 	}
1430c7ebbbceSChristoph Hellwig 
1431c7ebbbceSChristoph Hellwig 	return 0;
1432c7ebbbceSChristoph Hellwig }
1433c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_add);
1434c7ebbbceSChristoph Hellwig 
1435c7ebbbceSChristoph Hellwig /**
1436eb44820cSRob Landley  * sas_rphy_free  -  free a SAS remote PHY
1437eb44820cSRob Landley  * @rphy: SAS remote PHY to free
1438c7ebbbceSChristoph Hellwig  *
1439c7ebbbceSChristoph Hellwig  * Frees the specified SAS remote PHY.
1440c7ebbbceSChristoph Hellwig  *
1441c7ebbbceSChristoph Hellwig  * Note:
1442c7ebbbceSChristoph Hellwig  *   This function must only be called on a remote
1443c7ebbbceSChristoph Hellwig  *   PHY that has not sucessfully been added using
14446f63caaeSDarrick J. Wong  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1445c7ebbbceSChristoph Hellwig  */
1446c7ebbbceSChristoph Hellwig void sas_rphy_free(struct sas_rphy *rphy)
1447c7ebbbceSChristoph Hellwig {
144892aab646SMike Anderson 	struct device *dev = &rphy->dev;
1449c7ebbbceSChristoph Hellwig 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1450c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1451c7ebbbceSChristoph Hellwig 
1452e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1453c7ebbbceSChristoph Hellwig 	list_del(&rphy->list);
1454e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1455c7ebbbceSChristoph Hellwig 
1456b6aff669SJames Bottomley 	sas_bsg_remove(shost, rphy);
1457b6aff669SJames Bottomley 
145892aab646SMike Anderson 	transport_destroy_device(dev);
14592f8600dfSJames Bottomley 
146092aab646SMike Anderson 	put_device(dev);
1461c7ebbbceSChristoph Hellwig }
1462c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_rphy_free);
1463c7ebbbceSChristoph Hellwig 
1464c7ebbbceSChristoph Hellwig /**
1465eb44820cSRob Landley  * sas_rphy_delete  -  remove and free SAS remote PHY
14666f63caaeSDarrick J. Wong  * @rphy:	SAS remote PHY to remove and free
1467c7ebbbceSChristoph Hellwig  *
14686f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY and frees it.
1469c7ebbbceSChristoph Hellwig  */
1470c7ebbbceSChristoph Hellwig void
1471c7ebbbceSChristoph Hellwig sas_rphy_delete(struct sas_rphy *rphy)
1472c7ebbbceSChristoph Hellwig {
14736f63caaeSDarrick J. Wong 	sas_rphy_remove(rphy);
14746f63caaeSDarrick J. Wong 	sas_rphy_free(rphy);
14756f63caaeSDarrick J. Wong }
14766f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_delete);
14776f63caaeSDarrick J. Wong 
14786f63caaeSDarrick J. Wong /**
1479eb44820cSRob Landley  * sas_rphy_remove  -  remove SAS remote PHY
14806f63caaeSDarrick J. Wong  * @rphy:	SAS remote phy to remove
14816f63caaeSDarrick J. Wong  *
14826f63caaeSDarrick J. Wong  * Removes the specified SAS remote PHY.
14836f63caaeSDarrick J. Wong  */
14846f63caaeSDarrick J. Wong void
14856f63caaeSDarrick J. Wong sas_rphy_remove(struct sas_rphy *rphy)
14866f63caaeSDarrick J. Wong {
1487c7ebbbceSChristoph Hellwig 	struct device *dev = &rphy->dev;
148865c92b09SJames Bottomley 	struct sas_port *parent = dev_to_sas_port(dev->parent);
1489c7ebbbceSChristoph Hellwig 
1490d4054239SChristoph Hellwig 	switch (rphy->identify.device_type) {
1491d4054239SChristoph Hellwig 	case SAS_END_DEVICE:
1492fe8b2304SChristoph Hellwig 		scsi_remove_target(dev);
1493d4054239SChristoph Hellwig 		break;
1494d4054239SChristoph Hellwig 	case SAS_EDGE_EXPANDER_DEVICE:
1495d4054239SChristoph Hellwig 	case SAS_FANOUT_EXPANDER_DEVICE:
149665c92b09SJames Bottomley 		sas_remove_children(dev);
1497d4054239SChristoph Hellwig 		break;
1498d4054239SChristoph Hellwig 	default:
1499d4054239SChristoph Hellwig 		break;
1500d4054239SChristoph Hellwig 	}
1501c7ebbbceSChristoph Hellwig 
1502fe8b2304SChristoph Hellwig 	transport_remove_device(dev);
1503fe8b2304SChristoph Hellwig 	device_del(dev);
1504c7ebbbceSChristoph Hellwig 
150533b114e9SChristoph Hellwig 	parent->rphy = NULL;
1506c7ebbbceSChristoph Hellwig }
15076f63caaeSDarrick J. Wong EXPORT_SYMBOL(sas_rphy_remove);
1508c7ebbbceSChristoph Hellwig 
1509c7ebbbceSChristoph Hellwig /**
1510eb44820cSRob Landley  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1511c7ebbbceSChristoph Hellwig  * @dev:	device to check
1512c7ebbbceSChristoph Hellwig  *
1513c7ebbbceSChristoph Hellwig  * Returns:
1514c7ebbbceSChristoph Hellwig  *	%1 if the device represents a SAS remote PHY, %0 else
1515c7ebbbceSChristoph Hellwig  */
1516c7ebbbceSChristoph Hellwig int scsi_is_sas_rphy(const struct device *dev)
1517c7ebbbceSChristoph Hellwig {
15182f8600dfSJames Bottomley 	return dev->release == sas_end_device_release ||
15192f8600dfSJames Bottomley 		dev->release == sas_expander_release;
1520c7ebbbceSChristoph Hellwig }
1521c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(scsi_is_sas_rphy);
1522c7ebbbceSChristoph Hellwig 
1523c7ebbbceSChristoph Hellwig 
1524c7ebbbceSChristoph Hellwig /*
1525c7ebbbceSChristoph Hellwig  * SCSI scan helper
1526c7ebbbceSChristoph Hellwig  */
1527c7ebbbceSChristoph Hellwig 
1528e02f3f59SChristoph Hellwig static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1529e02f3f59SChristoph Hellwig 		uint id, uint lun)
1530c7ebbbceSChristoph Hellwig {
1531c7ebbbceSChristoph Hellwig 	struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1532c7ebbbceSChristoph Hellwig 	struct sas_rphy *rphy;
1533c7ebbbceSChristoph Hellwig 
1534e02f3f59SChristoph Hellwig 	mutex_lock(&sas_host->lock);
1535c7ebbbceSChristoph Hellwig 	list_for_each_entry(rphy, &sas_host->rphy_list, list) {
15366d99a3f3SJames Bottomley 		if (rphy->identify.device_type != SAS_END_DEVICE ||
15376d99a3f3SJames Bottomley 		    rphy->scsi_target_id == -1)
1538e02f3f59SChristoph Hellwig 			continue;
1539e02f3f59SChristoph Hellwig 
1540e8bf3941SJames Bottomley 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
1541e02f3f59SChristoph Hellwig 		    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1542e8bf3941SJames Bottomley 			scsi_scan_target(&rphy->dev, 0,
1543e02f3f59SChristoph Hellwig 					 rphy->scsi_target_id, lun, 1);
1544e02f3f59SChristoph Hellwig 		}
1545e02f3f59SChristoph Hellwig 	}
1546e02f3f59SChristoph Hellwig 	mutex_unlock(&sas_host->lock);
1547e02f3f59SChristoph Hellwig 
1548e02f3f59SChristoph Hellwig 	return 0;
1549c7ebbbceSChristoph Hellwig }
1550c7ebbbceSChristoph Hellwig 
1551c7ebbbceSChristoph Hellwig 
1552c7ebbbceSChristoph Hellwig /*
1553c7ebbbceSChristoph Hellwig  * Setup / Teardown code
1554c7ebbbceSChristoph Hellwig  */
1555c7ebbbceSChristoph Hellwig 
155642ab0360SJames Bottomley #define SETUP_TEMPLATE(attrb, field, perm, test)			\
155742ab0360SJames Bottomley 	i->private_##attrb[count] = class_device_attr_##field;		\
155842ab0360SJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
155942ab0360SJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
156042ab0360SJames Bottomley 	if (test)							\
1561c7ebbbceSChristoph Hellwig 		count++
1562c7ebbbceSChristoph Hellwig 
1563d24e1eebSJames Bottomley #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)	\
1564d24e1eebSJames Bottomley 	i->private_##attrb[count] = class_device_attr_##field;		\
1565d24e1eebSJames Bottomley 	i->private_##attrb[count].attr.mode = perm;			\
1566d24e1eebSJames Bottomley 	if (ro_test) {							\
1567d24e1eebSJames Bottomley 		i->private_##attrb[count].attr.mode = ro_perm;		\
1568d24e1eebSJames Bottomley 		i->private_##attrb[count].store = NULL;			\
1569d24e1eebSJames Bottomley 	}								\
1570d24e1eebSJames Bottomley 	i->attrb[count] = &i->private_##attrb[count];			\
1571d24e1eebSJames Bottomley 	if (test)							\
1572d24e1eebSJames Bottomley 		count++
157342ab0360SJames Bottomley 
157442ab0360SJames Bottomley #define SETUP_RPORT_ATTRIBUTE(field) 					\
157542ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
157642ab0360SJames Bottomley 
1577dd9fbb52SJames Bottomley #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)			\
157842ab0360SJames Bottomley 	SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1579dd9fbb52SJames Bottomley 
158065c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE(field)					\
158142ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1582c7ebbbceSChristoph Hellwig 
1583d24e1eebSJames Bottomley #define SETUP_PHY_ATTRIBUTE_RW(field)					\
1584d24e1eebSJames Bottomley 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1585d24e1eebSJames Bottomley 			!i->f->set_phy_speed, S_IRUGO)
1586d24e1eebSJames Bottomley 
1587acbf167dSDarrick J. Wong #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)			\
1588acbf167dSDarrick J. Wong 	SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,	\
1589acbf167dSDarrick J. Wong 			  !i->f->func, S_IRUGO)
1590acbf167dSDarrick J. Wong 
159165c92b09SJames Bottomley #define SETUP_PORT_ATTRIBUTE(field)					\
159265c92b09SJames Bottomley 	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
159365c92b09SJames Bottomley 
159465c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)			\
159542ab0360SJames Bottomley 	SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1596dd9fbb52SJames Bottomley 
159765c92b09SJames Bottomley #define SETUP_PHY_ATTRIBUTE_WRONLY(field)				\
1598fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
159907ba3a95SChristoph Hellwig 
160065c92b09SJames Bottomley #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)		\
1601fe3b5bfeSDarrick J. Wong 	SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1602dd9fbb52SJames Bottomley 
160342ab0360SJames Bottomley #define SETUP_END_DEV_ATTRIBUTE(field)					\
160442ab0360SJames Bottomley 	SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1605c7ebbbceSChristoph Hellwig 
160679cb1819SJames Bottomley #define SETUP_EXPANDER_ATTRIBUTE(field)					\
160779cb1819SJames Bottomley 	SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
160879cb1819SJames Bottomley 
1609c7ebbbceSChristoph Hellwig /**
1610eb44820cSRob Landley  * sas_attach_transport  -  instantiate SAS transport template
1611c7ebbbceSChristoph Hellwig  * @ft:		SAS transport class function template
1612c7ebbbceSChristoph Hellwig  */
1613c7ebbbceSChristoph Hellwig struct scsi_transport_template *
1614c7ebbbceSChristoph Hellwig sas_attach_transport(struct sas_function_template *ft)
1615c7ebbbceSChristoph Hellwig {
1616c7ebbbceSChristoph Hellwig 	struct sas_internal *i;
1617c7ebbbceSChristoph Hellwig 	int count;
1618c7ebbbceSChristoph Hellwig 
161924669f75SJes Sorensen 	i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1620c7ebbbceSChristoph Hellwig 	if (!i)
1621c7ebbbceSChristoph Hellwig 		return NULL;
1622c7ebbbceSChristoph Hellwig 
1623e02f3f59SChristoph Hellwig 	i->t.user_scan = sas_user_scan;
1624c7ebbbceSChristoph Hellwig 
1625c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1626c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.class = &sas_host_class.class;
1627c7ebbbceSChristoph Hellwig 	i->t.host_attrs.ac.match = sas_host_match;
1628c7ebbbceSChristoph Hellwig 	transport_container_register(&i->t.host_attrs);
1629c7ebbbceSChristoph Hellwig 	i->t.host_size = sizeof(struct sas_host_attrs);
1630c7ebbbceSChristoph Hellwig 
1631c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.class = &sas_phy_class.class;
1632c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1633c7ebbbceSChristoph Hellwig 	i->phy_attr_cont.ac.match = sas_phy_match;
1634c7ebbbceSChristoph Hellwig 	transport_container_register(&i->phy_attr_cont);
1635c7ebbbceSChristoph Hellwig 
163665c92b09SJames Bottomley 	i->port_attr_cont.ac.class = &sas_port_class.class;
163765c92b09SJames Bottomley 	i->port_attr_cont.ac.attrs = &i->port_attrs[0];
163865c92b09SJames Bottomley 	i->port_attr_cont.ac.match = sas_port_match;
163965c92b09SJames Bottomley 	transport_container_register(&i->port_attr_cont);
164065c92b09SJames Bottomley 
1641c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1642c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1643c7ebbbceSChristoph Hellwig 	i->rphy_attr_cont.ac.match = sas_rphy_match;
1644c7ebbbceSChristoph Hellwig 	transport_container_register(&i->rphy_attr_cont);
1645c7ebbbceSChristoph Hellwig 
164642ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
164742ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
164842ab0360SJames Bottomley 	i->end_dev_attr_cont.ac.match = sas_end_dev_match;
164942ab0360SJames Bottomley 	transport_container_register(&i->end_dev_attr_cont);
165042ab0360SJames Bottomley 
165179cb1819SJames Bottomley 	i->expander_attr_cont.ac.class = &sas_expander_class.class;
165279cb1819SJames Bottomley 	i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
165379cb1819SJames Bottomley 	i->expander_attr_cont.ac.match = sas_expander_match;
165479cb1819SJames Bottomley 	transport_container_register(&i->expander_attr_cont);
165579cb1819SJames Bottomley 
1656c7ebbbceSChristoph Hellwig 	i->f = ft;
1657c7ebbbceSChristoph Hellwig 
1658c7ebbbceSChristoph Hellwig 	count = 0;
165965c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
1660c7ebbbceSChristoph Hellwig 	i->host_attrs[count] = NULL;
1661c7ebbbceSChristoph Hellwig 
1662c7ebbbceSChristoph Hellwig 	count = 0;
166365c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
166465c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(target_port_protocols);
166565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(device_type);
166665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(sas_address);
166765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_identifier);
166865c92b09SJames Bottomley 	//SETUP_PHY_ATTRIBUTE(port_identifier);
166965c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
167065c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1671d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
167265c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1673d24e1eebSJames Bottomley 	SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1674c3ee74c4SChristoph Hellwig 
167565c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(invalid_dword_count);
167665c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
167765c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
167865c92b09SJames Bottomley 	SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
167965c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
168065c92b09SJames Bottomley 	SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1681acbf167dSDarrick J. Wong 	SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1682c7ebbbceSChristoph Hellwig 	i->phy_attrs[count] = NULL;
1683c7ebbbceSChristoph Hellwig 
1684c7ebbbceSChristoph Hellwig 	count = 0;
168565c92b09SJames Bottomley 	SETUP_PORT_ATTRIBUTE(num_phys);
168665c92b09SJames Bottomley 	i->port_attrs[count] = NULL;
168765c92b09SJames Bottomley 
168865c92b09SJames Bottomley 	count = 0;
1689c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1690c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1691c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1692c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1693c7ebbbceSChristoph Hellwig 	SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1694dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1695dd9fbb52SJames Bottomley 				       get_enclosure_identifier);
1696dd9fbb52SJames Bottomley 	SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1697dd9fbb52SJames Bottomley 				       get_bay_identifier);
1698c7ebbbceSChristoph Hellwig 	i->rphy_attrs[count] = NULL;
1699c7ebbbceSChristoph Hellwig 
170042ab0360SJames Bottomley 	count = 0;
170142ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
170242ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
170342ab0360SJames Bottomley 	SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
170442ab0360SJames Bottomley 	i->end_dev_attrs[count] = NULL;
170542ab0360SJames Bottomley 
170679cb1819SJames Bottomley 	count = 0;
170779cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(vendor_id);
170879cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_id);
170979cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(product_rev);
171079cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
171179cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_id);
171279cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
171379cb1819SJames Bottomley 	SETUP_EXPANDER_ATTRIBUTE(level);
171479cb1819SJames Bottomley 	i->expander_attrs[count] = NULL;
171579cb1819SJames Bottomley 
1716c7ebbbceSChristoph Hellwig 	return &i->t;
1717c7ebbbceSChristoph Hellwig }
1718c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_attach_transport);
1719c7ebbbceSChristoph Hellwig 
1720c7ebbbceSChristoph Hellwig /**
1721eb44820cSRob Landley  * sas_release_transport  -  release SAS transport template instance
1722c7ebbbceSChristoph Hellwig  * @t:		transport template instance
1723c7ebbbceSChristoph Hellwig  */
1724c7ebbbceSChristoph Hellwig void sas_release_transport(struct scsi_transport_template *t)
1725c7ebbbceSChristoph Hellwig {
1726c7ebbbceSChristoph Hellwig 	struct sas_internal *i = to_sas_internal(t);
1727c7ebbbceSChristoph Hellwig 
1728c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->t.host_attrs);
1729c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->phy_attr_cont);
173065c92b09SJames Bottomley 	transport_container_unregister(&i->port_attr_cont);
1731c7ebbbceSChristoph Hellwig 	transport_container_unregister(&i->rphy_attr_cont);
1732db82f841SJames Bottomley 	transport_container_unregister(&i->end_dev_attr_cont);
173379cb1819SJames Bottomley 	transport_container_unregister(&i->expander_attr_cont);
1734c7ebbbceSChristoph Hellwig 
1735c7ebbbceSChristoph Hellwig 	kfree(i);
1736c7ebbbceSChristoph Hellwig }
1737c7ebbbceSChristoph Hellwig EXPORT_SYMBOL(sas_release_transport);
1738c7ebbbceSChristoph Hellwig 
1739c7ebbbceSChristoph Hellwig static __init int sas_transport_init(void)
1740c7ebbbceSChristoph Hellwig {
1741c7ebbbceSChristoph Hellwig 	int error;
1742c7ebbbceSChristoph Hellwig 
1743c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_host_class);
1744c7ebbbceSChristoph Hellwig 	if (error)
1745c7ebbbceSChristoph Hellwig 		goto out;
1746c7ebbbceSChristoph Hellwig 	error = transport_class_register(&sas_phy_class);
1747c7ebbbceSChristoph Hellwig 	if (error)
1748c7ebbbceSChristoph Hellwig 		goto out_unregister_transport;
174965c92b09SJames Bottomley 	error = transport_class_register(&sas_port_class);
1750c7ebbbceSChristoph Hellwig 	if (error)
1751c7ebbbceSChristoph Hellwig 		goto out_unregister_phy;
175265c92b09SJames Bottomley 	error = transport_class_register(&sas_rphy_class);
175365c92b09SJames Bottomley 	if (error)
175465c92b09SJames Bottomley 		goto out_unregister_port;
175542ab0360SJames Bottomley 	error = transport_class_register(&sas_end_dev_class);
175642ab0360SJames Bottomley 	if (error)
175742ab0360SJames Bottomley 		goto out_unregister_rphy;
175879cb1819SJames Bottomley 	error = transport_class_register(&sas_expander_class);
175979cb1819SJames Bottomley 	if (error)
176079cb1819SJames Bottomley 		goto out_unregister_end_dev;
1761c7ebbbceSChristoph Hellwig 
1762c7ebbbceSChristoph Hellwig 	return 0;
1763c7ebbbceSChristoph Hellwig 
176479cb1819SJames Bottomley  out_unregister_end_dev:
176579cb1819SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
176642ab0360SJames Bottomley  out_unregister_rphy:
176742ab0360SJames Bottomley 	transport_class_unregister(&sas_rphy_class);
176865c92b09SJames Bottomley  out_unregister_port:
176965c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1770c7ebbbceSChristoph Hellwig  out_unregister_phy:
1771c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
1772c7ebbbceSChristoph Hellwig  out_unregister_transport:
1773c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1774c7ebbbceSChristoph Hellwig  out:
1775c7ebbbceSChristoph Hellwig 	return error;
1776c7ebbbceSChristoph Hellwig 
1777c7ebbbceSChristoph Hellwig }
1778c7ebbbceSChristoph Hellwig 
1779c7ebbbceSChristoph Hellwig static void __exit sas_transport_exit(void)
1780c7ebbbceSChristoph Hellwig {
1781c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_host_class);
1782c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_phy_class);
178365c92b09SJames Bottomley 	transport_class_unregister(&sas_port_class);
1784c7ebbbceSChristoph Hellwig 	transport_class_unregister(&sas_rphy_class);
178542ab0360SJames Bottomley 	transport_class_unregister(&sas_end_dev_class);
178679cb1819SJames Bottomley 	transport_class_unregister(&sas_expander_class);
1787c7ebbbceSChristoph Hellwig }
1788c7ebbbceSChristoph Hellwig 
1789c7ebbbceSChristoph Hellwig MODULE_AUTHOR("Christoph Hellwig");
179086b9c4c1SAlexis Bruemmer MODULE_DESCRIPTION("SAS Transport Attributes");
1791c7ebbbceSChristoph Hellwig MODULE_LICENSE("GPL");
1792c7ebbbceSChristoph Hellwig 
1793c7ebbbceSChristoph Hellwig module_init(sas_transport_init);
1794c7ebbbceSChristoph Hellwig module_exit(sas_transport_exit);
1795