blob: 927e99cb72250c61154639ea8ca1eae2dd32b198 [file] [log] [blame]
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001/*
Christoph Hellwiga0125642006-02-16 13:31:47 +01002 * Copyright (C) 2005-2006 Dell Inc.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02003 * Released under GPL v2.
4 *
5 * Serial Attached SCSI (SAS) transport class.
6 *
7 * The SAS transport class contains common code to deal with SAS HBAs,
8 * an aproximated representation of SAS topologies in the driver model,
Joe Perchesb1c11812008-02-03 17:28:22 +02009 * and various sysfs attributes to expose these topologies and management
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020010 * interfaces to userspace.
11 *
12 * In addition to the basic SCSI core objects this transport class
13 * introduces two additional intermediate objects: The SAS PHY
14 * as represented by struct sas_phy defines an "outgoing" PHY on
15 * a SAS HBA or Expander, and the SAS remote PHY represented by
16 * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17 * end device. Note that this is purely a software concept, the
18 * underlying hardware for a PHY and a remote PHY is the exactly
19 * the same.
20 *
21 * There is no concept of a SAS port in this code, users can see
22 * what PHYs form a wide port based on the port_identifier attribute,
23 * which is the same for all PHYs in a port.
24 */
25
26#include <linux/init.h>
27#include <linux/module.h>
Al Virof6a57032006-10-18 01:47:25 -040028#include <linux/jiffies.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020029#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080030#include <linux/slab.h>
31#include <linux/string.h>
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +090032#include <linux/blkdev.h>
33#include <linux/bsg.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020034
Christoph Hellwige02f3f52006-01-13 19:04:00 +010035#include <scsi/scsi.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020036#include <scsi/scsi_device.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_sas.h>
40
James Bottomleyd6159c12006-03-27 16:45:34 -060041#include "scsi_sas_internal.h"
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020042struct sas_host_attrs {
43 struct list_head rphy_list;
Christoph Hellwige02f3f52006-01-13 19:04:00 +010044 struct mutex lock;
James Bottomleyb6aff662007-07-20 11:10:05 -050045 struct request_queue *q;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020046 u32 next_target_id;
James Bottomley79cb1812006-03-13 13:50:04 -060047 u32 next_expander_id;
James Bottomleyc9fefeb2006-07-02 11:10:18 -050048 int next_port_id;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020049};
50#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
51
52
53/*
54 * Hack to allow attributes of the same name in different objects.
55 */
Tony Jonesee959b02008-02-22 00:13:36 +010056#define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct device_attribute dev_attr_##_prefix##_##_name = \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020058 __ATTR(_name,_mode,_show,_store)
59
60
61/*
62 * Pretty printing helpers
63 */
64
65#define sas_bitfield_name_match(title, table) \
66static ssize_t \
67get_sas_##title##_names(u32 table_key, char *buf) \
68{ \
69 char *prefix = ""; \
70 ssize_t len = 0; \
71 int i; \
72 \
Tobias Klauser6391a112006-06-08 22:23:48 -070073 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020074 if (table[i].value & table_key) { \
75 len += sprintf(buf + len, "%s%s", \
76 prefix, table[i].name); \
77 prefix = ", "; \
78 } \
79 } \
80 len += sprintf(buf + len, "\n"); \
81 return len; \
82}
83
James Bottomleyd24e1ee2006-09-06 19:25:22 -050084#define sas_bitfield_name_set(title, table) \
85static ssize_t \
86set_sas_##title##_names(u32 *table_key, const char *buf) \
87{ \
88 ssize_t len = 0; \
89 int i; \
90 \
91 for (i = 0; i < ARRAY_SIZE(table); i++) { \
92 len = strlen(table[i].name); \
93 if (strncmp(buf, table[i].name, len) == 0 && \
94 (buf[len] == '\n' || buf[len] == '\0')) { \
95 *table_key = table[i].value; \
96 return 0; \
97 } \
98 } \
99 return -EINVAL; \
100}
101
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200102#define sas_bitfield_name_search(title, table) \
103static ssize_t \
104get_sas_##title##_names(u32 table_key, char *buf) \
105{ \
106 ssize_t len = 0; \
107 int i; \
108 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700109 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200110 if (table[i].value == table_key) { \
111 len += sprintf(buf + len, "%s", \
112 table[i].name); \
113 break; \
114 } \
115 } \
116 len += sprintf(buf + len, "\n"); \
117 return len; \
118}
119
120static struct {
121 u32 value;
122 char *name;
123} sas_device_type_names[] = {
124 { SAS_PHY_UNUSED, "unused" },
125 { SAS_END_DEVICE, "end device" },
126 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
127 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
128};
129sas_bitfield_name_search(device_type, sas_device_type_names)
130
131
132static struct {
133 u32 value;
134 char *name;
135} sas_protocol_names[] = {
136 { SAS_PROTOCOL_SATA, "sata" },
137 { SAS_PROTOCOL_SMP, "smp" },
138 { SAS_PROTOCOL_STP, "stp" },
139 { SAS_PROTOCOL_SSP, "ssp" },
140};
141sas_bitfield_name_match(protocol, sas_protocol_names)
142
143static struct {
144 u32 value;
145 char *name;
146} sas_linkspeed_names[] = {
147 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
148 { SAS_PHY_DISABLED, "Phy disabled" },
149 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
150 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
151 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
152 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
James Bottomley7e6dff62006-03-02 14:12:56 -0600153 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200154};
155sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500156sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200157
James Bottomley0f880092010-01-18 10:14:51 -0600158static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
159{
160 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
161 struct sas_end_device *rdev;
162
163 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
164
165 rdev = rphy_to_end_device(rphy);
166 return rdev;
167}
168
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900169static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
170 struct sas_rphy *rphy)
171{
172 struct request *req;
173 int ret;
174 int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
175
176 while (!blk_queue_plugged(q)) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900177 req = blk_fetch_request(q);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900178 if (!req)
179 break;
180
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900181 spin_unlock_irq(q->queue_lock);
182
183 handler = to_sas_internal(shost->transportt)->f->smp_handler;
184 ret = handler(shost, rphy, req);
James Bottomley2d507a02007-12-29 10:59:53 -0600185 req->errors = ret;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900186
FUJITA Tomonori93bdcba2009-06-17 15:10:10 +0900187 blk_end_request_all(req, ret);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900188
FUJITA Tomonori93bdcba2009-06-17 15:10:10 +0900189 spin_lock_irq(q->queue_lock);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900190 }
191}
192
193static void sas_host_smp_request(struct request_queue *q)
194{
195 sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
196}
197
198static void sas_non_host_smp_request(struct request_queue *q)
199{
200 struct sas_rphy *rphy = q->queuedata;
201 sas_smp_request(q, rphy_to_shost(rphy), rphy);
202}
203
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900204static void sas_host_release(struct device *dev)
205{
206 struct Scsi_Host *shost = dev_to_shost(dev);
207 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
208 struct request_queue *q = sas_host->q;
209
210 if (q)
211 blk_cleanup_queue(q);
212}
213
James Bottomley39dca552007-07-20 18:22:17 -0500214static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900215{
216 struct request_queue *q;
217 int error;
James Bottomley39dca552007-07-20 18:22:17 -0500218 struct device *dev;
Kay Sievers71610f52008-12-03 22:41:36 +0100219 char namebuf[20];
James Bottomley39dca552007-07-20 18:22:17 -0500220 const char *name;
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900221 void (*release)(struct device *);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900222
223 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
224 printk("%s can't handle SMP requests\n", shost->hostt->name);
225 return 0;
226 }
227
James Bottomley39dca552007-07-20 18:22:17 -0500228 if (rphy) {
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900229 q = blk_init_queue(sas_non_host_smp_request, NULL);
James Bottomley39dca552007-07-20 18:22:17 -0500230 dev = &rphy->dev;
Kay Sievers71610f52008-12-03 22:41:36 +0100231 name = dev_name(dev);
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900232 release = NULL;
James Bottomley39dca552007-07-20 18:22:17 -0500233 } else {
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900234 q = blk_init_queue(sas_host_smp_request, NULL);
James Bottomley39dca552007-07-20 18:22:17 -0500235 dev = &shost->shost_gendev;
236 snprintf(namebuf, sizeof(namebuf),
237 "sas_host%d", shost->host_no);
238 name = namebuf;
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900239 release = sas_host_release;
James Bottomley39dca552007-07-20 18:22:17 -0500240 }
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900241 if (!q)
242 return -ENOMEM;
243
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900244 error = bsg_register_queue(q, dev, name, release);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900245 if (error) {
246 blk_cleanup_queue(q);
247 return -ENOMEM;
248 }
249
250 if (rphy)
James Bottomleyb6aff662007-07-20 11:10:05 -0500251 rphy->q = q;
252 else
253 to_sas_host_attrs(shost)->q = q;
254
255 if (rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900256 q->queuedata = rphy;
257 else
258 q->queuedata = shost;
259
Nick Piggin75ad23b2008-04-29 14:48:33 +0200260 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900261 return 0;
262}
263
James Bottomleyb6aff662007-07-20 11:10:05 -0500264static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
265{
266 struct request_queue *q;
267
268 if (rphy)
269 q = rphy->q;
270 else
271 q = to_sas_host_attrs(shost)->q;
272
273 if (!q)
274 return;
275
276 bsg_unregister_queue(q);
James Bottomleyb6aff662007-07-20 11:10:05 -0500277}
278
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200279/*
280 * SAS host attributes
281 */
282
James Bottomley37be6ee2005-09-09 18:38:27 -0500283static int sas_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100284 struct device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200285{
286 struct Scsi_Host *shost = dev_to_shost(dev);
287 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
288
289 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100290 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200291 sas_host->next_target_id = 0;
James Bottomley79cb1812006-03-13 13:50:04 -0600292 sas_host->next_expander_id = 0;
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500293 sas_host->next_port_id = 0;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900294
James Bottomley39dca552007-07-20 18:22:17 -0500295 if (sas_bsg_initialize(shost, NULL))
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900296 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
297 shost->host_no);
298
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200299 return 0;
300}
301
James Bottomleyb6aff662007-07-20 11:10:05 -0500302static int sas_host_remove(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100303 struct device *cdev)
James Bottomleyb6aff662007-07-20 11:10:05 -0500304{
305 struct Scsi_Host *shost = dev_to_shost(dev);
306
307 sas_bsg_remove(shost, NULL);
308
309 return 0;
310}
311
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200312static DECLARE_TRANSPORT_CLASS(sas_host_class,
James Bottomleyb6aff662007-07-20 11:10:05 -0500313 "sas_host", sas_host_setup, sas_host_remove, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200314
315static int sas_host_match(struct attribute_container *cont,
316 struct device *dev)
317{
318 struct Scsi_Host *shost;
319 struct sas_internal *i;
320
321 if (!scsi_is_host_device(dev))
322 return 0;
323 shost = dev_to_shost(dev);
324
325 if (!shost->transportt)
326 return 0;
327 if (shost->transportt->host_attrs.ac.class !=
328 &sas_host_class.class)
329 return 0;
330
331 i = to_sas_internal(shost->transportt);
332 return &i->t.host_attrs.ac == cont;
333}
334
335static int do_sas_phy_delete(struct device *dev, void *data)
336{
James Bottomley65c92b02006-06-28 12:22:50 -0400337 int pass = (int)(unsigned long)data;
338
339 if (pass == 0 && scsi_is_sas_port(dev))
340 sas_port_delete(dev_to_sas_port(dev));
341 else if (pass == 1 && scsi_is_sas_phy(dev))
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200342 sas_phy_delete(dev_to_phy(dev));
343 return 0;
344}
345
346/**
Rob Landleyeb448202007-11-03 13:30:39 -0500347 * sas_remove_children - tear down a devices SAS data structures
James Bottomley65c92b02006-06-28 12:22:50 -0400348 * @dev: device belonging to the sas object
349 *
350 * Removes all SAS PHYs and remote PHYs for a given object
351 */
352void sas_remove_children(struct device *dev)
353{
354 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
355 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
356}
357EXPORT_SYMBOL(sas_remove_children);
358
359/**
Rob Landleyeb448202007-11-03 13:30:39 -0500360 * sas_remove_host - tear down a Scsi_Host's SAS data structures
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200361 * @shost: Scsi Host that is torn down
362 *
363 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
364 * Must be called just before scsi_remove_host for SAS HBAs.
365 */
366void sas_remove_host(struct Scsi_Host *shost)
367{
James Bottomley65c92b02006-06-28 12:22:50 -0400368 sas_remove_children(&shost->shost_gendev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200369}
370EXPORT_SYMBOL(sas_remove_host);
371
James Bottomley0f880092010-01-18 10:14:51 -0600372/**
373 * sas_tlr_supported - checking TLR bit in vpd 0x90
374 * @sdev: scsi device struct
375 *
376 * Check Transport Layer Retries are supported or not.
377 * If vpd page 0x90 is present, TRL is supported.
378 *
379 */
380unsigned int
381sas_tlr_supported(struct scsi_device *sdev)
382{
383 const int vpd_len = 32;
384 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
385 char *buffer = kzalloc(vpd_len, GFP_KERNEL);
386 int ret = 0;
387
388 if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
389 goto out;
390
391 /*
392 * Magic numbers: the VPD Protocol page (0x90)
393 * has a 4 byte header and then one entry per device port
394 * the TLR bit is at offset 8 on each port entry
395 * if we take the first port, that's at total offset 12
396 */
397 ret = buffer[12] & 0x01;
398
399 out:
400 kfree(buffer);
401 rdev->tlr_supported = ret;
402 return ret;
403
404}
405EXPORT_SYMBOL_GPL(sas_tlr_supported);
406
407/**
408 * sas_disable_tlr - setting TLR flags
409 * @sdev: scsi device struct
410 *
411 * Seting tlr_enabled flag to 0.
412 *
413 */
414void
415sas_disable_tlr(struct scsi_device *sdev)
416{
417 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
418
419 rdev->tlr_enabled = 0;
420}
421EXPORT_SYMBOL_GPL(sas_disable_tlr);
422
423/**
424 * sas_enable_tlr - setting TLR flags
425 * @sdev: scsi device struct
426 *
427 * Seting tlr_enabled flag 1.
428 *
429 */
430void sas_enable_tlr(struct scsi_device *sdev)
431{
432 unsigned int tlr_supported = 0;
433 tlr_supported = sas_tlr_supported(sdev);
434
435 if (tlr_supported) {
436 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
437
438 rdev->tlr_enabled = 1;
439 }
440
441 return;
442}
443EXPORT_SYMBOL_GPL(sas_enable_tlr);
444
445unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
446{
447 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
448 return rdev->tlr_enabled;
449}
450EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200451
452/*
James Bottomley65c92b02006-06-28 12:22:50 -0400453 * SAS Phy attributes
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200454 */
455
456#define sas_phy_show_simple(field, name, format_string, cast) \
457static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100458show_sas_phy_##name(struct device *dev, \
459 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200460{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100461 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200462 \
463 return snprintf(buf, 20, format_string, cast phy->field); \
464}
465
466#define sas_phy_simple_attr(field, name, format_string, type) \
467 sas_phy_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100468static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200469
470#define sas_phy_show_protocol(field, name) \
471static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100472show_sas_phy_##name(struct device *dev, \
473 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200474{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100475 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200476 \
477 if (!phy->field) \
478 return snprintf(buf, 20, "none\n"); \
479 return get_sas_protocol_names(phy->field, buf); \
480}
481
482#define sas_phy_protocol_attr(field, name) \
483 sas_phy_show_protocol(field, name) \
Tony Jonesee959b02008-02-22 00:13:36 +0100484static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200485
486#define sas_phy_show_linkspeed(field) \
487static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100488show_sas_phy_##field(struct device *dev, \
489 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200490{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100491 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200492 \
493 return get_sas_linkspeed_names(phy->field, buf); \
494}
495
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500496/* Fudge to tell if we're minimum or maximum */
497#define sas_phy_store_linkspeed(field) \
498static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100499store_sas_phy_##field(struct device *dev, \
500 struct device_attribute *attr, \
501 const char *buf, size_t count) \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500502{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100503 struct sas_phy *phy = transport_class_to_phy(dev); \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500504 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
505 struct sas_internal *i = to_sas_internal(shost->transportt); \
506 u32 value; \
507 struct sas_phy_linkrates rates = {0}; \
508 int error; \
509 \
510 error = set_sas_linkspeed_names(&value, buf); \
511 if (error) \
512 return error; \
513 rates.field = value; \
514 error = i->f->set_phy_speed(phy, &rates); \
515 \
516 return error ? error : count; \
517}
518
519#define sas_phy_linkspeed_rw_attr(field) \
520 sas_phy_show_linkspeed(field) \
521 sas_phy_store_linkspeed(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100522static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500523 store_sas_phy_##field)
524
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200525#define sas_phy_linkspeed_attr(field) \
526 sas_phy_show_linkspeed(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100527static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200528
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500529
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200530#define sas_phy_show_linkerror(field) \
531static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100532show_sas_phy_##field(struct device *dev, \
533 struct device_attribute *attr, char *buf) \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200534{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100535 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200536 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
537 struct sas_internal *i = to_sas_internal(shost->transportt); \
538 int error; \
539 \
James Bottomleydd9fbb522006-03-02 16:01:31 -0600540 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200541 if (error) \
542 return error; \
543 return snprintf(buf, 20, "%u\n", phy->field); \
544}
545
546#define sas_phy_linkerror_attr(field) \
547 sas_phy_show_linkerror(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100548static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200549
550
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200551static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100552show_sas_device_type(struct device *dev,
553 struct device_attribute *attr, char *buf)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200554{
Tony Jonesee959b02008-02-22 00:13:36 +0100555 struct sas_phy *phy = transport_class_to_phy(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200556
557 if (!phy->identify.device_type)
558 return snprintf(buf, 20, "none\n");
559 return get_sas_device_type_names(phy->identify.device_type, buf);
560}
Tony Jonesee959b02008-02-22 00:13:36 +0100561static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200562
Tony Jonesee959b02008-02-22 00:13:36 +0100563static ssize_t do_sas_phy_enable(struct device *dev,
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800564 size_t count, int enable)
565{
Tony Jonesee959b02008-02-22 00:13:36 +0100566 struct sas_phy *phy = transport_class_to_phy(dev);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800567 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
568 struct sas_internal *i = to_sas_internal(shost->transportt);
569 int error;
570
571 error = i->f->phy_enable(phy, enable);
572 if (error)
573 return error;
574 phy->enabled = enable;
575 return count;
576};
577
Tony Jonesee959b02008-02-22 00:13:36 +0100578static ssize_t
579store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800581{
582 if (count < 1)
583 return -EINVAL;
584
585 switch (buf[0]) {
586 case '0':
Tony Jonesee959b02008-02-22 00:13:36 +0100587 do_sas_phy_enable(dev, count, 0);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800588 break;
589 case '1':
Tony Jonesee959b02008-02-22 00:13:36 +0100590 do_sas_phy_enable(dev, count, 1);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800591 break;
592 default:
593 return -EINVAL;
594 }
595
596 return count;
597}
598
Tony Jonesee959b02008-02-22 00:13:36 +0100599static ssize_t
600show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
601 char *buf)
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800602{
Tony Jonesee959b02008-02-22 00:13:36 +0100603 struct sas_phy *phy = transport_class_to_phy(dev);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800604
605 return snprintf(buf, 20, "%d", phy->enabled);
606}
607
Tony Jonesee959b02008-02-22 00:13:36 +0100608static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800609 store_sas_phy_enable);
610
Tony Jonesee959b02008-02-22 00:13:36 +0100611static ssize_t
612do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200613{
Tony Jonesee959b02008-02-22 00:13:36 +0100614 struct sas_phy *phy = transport_class_to_phy(dev);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200615 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
616 struct sas_internal *i = to_sas_internal(shost->transportt);
617 int error;
618
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200619 error = i->f->phy_reset(phy, hard_reset);
620 if (error)
621 return error;
622 return count;
623};
624
Tony Jonesee959b02008-02-22 00:13:36 +0100625static ssize_t
626store_sas_link_reset(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200628{
Tony Jonesee959b02008-02-22 00:13:36 +0100629 return do_sas_phy_reset(dev, count, 0);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200630}
Tony Jonesee959b02008-02-22 00:13:36 +0100631static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200632
Tony Jonesee959b02008-02-22 00:13:36 +0100633static ssize_t
634store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200636{
Tony Jonesee959b02008-02-22 00:13:36 +0100637 return do_sas_phy_reset(dev, count, 1);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200638}
Tony Jonesee959b02008-02-22 00:13:36 +0100639static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200640
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200641sas_phy_protocol_attr(identify.initiator_port_protocols,
642 initiator_port_protocols);
643sas_phy_protocol_attr(identify.target_port_protocols,
644 target_port_protocols);
645sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
646 unsigned long long);
647sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500648//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200649sas_phy_linkspeed_attr(negotiated_linkrate);
650sas_phy_linkspeed_attr(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500651sas_phy_linkspeed_rw_attr(minimum_linkrate);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200652sas_phy_linkspeed_attr(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500653sas_phy_linkspeed_rw_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200654sas_phy_linkerror_attr(invalid_dword_count);
655sas_phy_linkerror_attr(running_disparity_error_count);
656sas_phy_linkerror_attr(loss_of_dword_sync_count);
657sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200658
659
660static DECLARE_TRANSPORT_CLASS(sas_phy_class,
661 "sas_phy", NULL, NULL, NULL);
662
663static int sas_phy_match(struct attribute_container *cont, struct device *dev)
664{
665 struct Scsi_Host *shost;
666 struct sas_internal *i;
667
668 if (!scsi_is_sas_phy(dev))
669 return 0;
670 shost = dev_to_shost(dev->parent);
671
672 if (!shost->transportt)
673 return 0;
674 if (shost->transportt->host_attrs.ac.class !=
675 &sas_host_class.class)
676 return 0;
677
678 i = to_sas_internal(shost->transportt);
679 return &i->phy_attr_cont.ac == cont;
680}
681
682static void sas_phy_release(struct device *dev)
683{
684 struct sas_phy *phy = dev_to_phy(dev);
685
686 put_device(dev->parent);
687 kfree(phy);
688}
689
690/**
Rob Landleyeb448202007-11-03 13:30:39 -0500691 * sas_phy_alloc - allocates and initialize a SAS PHY structure
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200692 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700693 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200694 *
695 * Allocates an SAS PHY structure. It will be added in the device tree
696 * below the device specified by @parent, which has to be either a Scsi_Host
697 * or sas_rphy.
698 *
699 * Returns:
700 * SAS PHY allocated or %NULL if the allocation failed.
701 */
702struct sas_phy *sas_phy_alloc(struct device *parent, int number)
703{
704 struct Scsi_Host *shost = dev_to_shost(parent);
705 struct sas_phy *phy;
706
Jes Sorensen24669f752006-01-16 10:31:18 -0500707 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200708 if (!phy)
709 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200710
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200711 phy->number = number;
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800712 phy->enabled = 1;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200713
714 device_initialize(&phy->dev);
715 phy->dev.parent = get_device(parent);
716 phy->dev.release = sas_phy_release;
James Bottomley65c92b02006-06-28 12:22:50 -0400717 INIT_LIST_HEAD(&phy->port_siblings);
James Bottomley79cb1812006-03-13 13:50:04 -0600718 if (scsi_is_sas_expander_device(parent)) {
719 struct sas_rphy *rphy = dev_to_rphy(parent);
Kay Sievers71610f52008-12-03 22:41:36 +0100720 dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
James Bottomley79cb1812006-03-13 13:50:04 -0600721 rphy->scsi_target_id, number);
722 } else
Kay Sievers71610f52008-12-03 22:41:36 +0100723 dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200724
725 transport_setup_device(&phy->dev);
726
727 return phy;
728}
729EXPORT_SYMBOL(sas_phy_alloc);
730
731/**
Rob Landleyeb448202007-11-03 13:30:39 -0500732 * sas_phy_add - add a SAS PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200733 * @phy: The PHY to be added
734 *
735 * Publishes a SAS PHY to the rest of the system.
736 */
737int sas_phy_add(struct sas_phy *phy)
738{
739 int error;
740
741 error = device_add(&phy->dev);
742 if (!error) {
743 transport_add_device(&phy->dev);
744 transport_configure_device(&phy->dev);
745 }
746
747 return error;
748}
749EXPORT_SYMBOL(sas_phy_add);
750
751/**
Rob Landleyeb448202007-11-03 13:30:39 -0500752 * sas_phy_free - free a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200753 * @phy: SAS PHY to free
754 *
755 * Frees the specified SAS PHY.
756 *
757 * Note:
758 * This function must only be called on a PHY that has not
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200759 * successfully been added using sas_phy_add().
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200760 */
761void sas_phy_free(struct sas_phy *phy)
762{
763 transport_destroy_device(&phy->dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800764 put_device(&phy->dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200765}
766EXPORT_SYMBOL(sas_phy_free);
767
768/**
Rob Landleyeb448202007-11-03 13:30:39 -0500769 * sas_phy_delete - remove SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200770 * @phy: SAS PHY to remove
771 *
772 * Removes the specified SAS PHY. If the SAS PHY has an
773 * associated remote PHY it is removed before.
774 */
775void
776sas_phy_delete(struct sas_phy *phy)
777{
778 struct device *dev = &phy->dev;
779
James Bottomley65c92b02006-06-28 12:22:50 -0400780 /* this happens if the phy is still part of a port when deleted */
781 BUG_ON(!list_empty(&phy->port_siblings));
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200782
783 transport_remove_device(dev);
784 device_del(dev);
785 transport_destroy_device(dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800786 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200787}
788EXPORT_SYMBOL(sas_phy_delete);
789
790/**
Rob Landleyeb448202007-11-03 13:30:39 -0500791 * scsi_is_sas_phy - check if a struct device represents a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200792 * @dev: device to check
793 *
794 * Returns:
795 * %1 if the device represents a SAS PHY, %0 else
796 */
797int scsi_is_sas_phy(const struct device *dev)
798{
799 return dev->release == sas_phy_release;
800}
801EXPORT_SYMBOL(scsi_is_sas_phy);
802
803/*
James Bottomley65c92b02006-06-28 12:22:50 -0400804 * SAS Port attributes
805 */
806#define sas_port_show_simple(field, name, format_string, cast) \
807static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100808show_sas_port_##name(struct device *dev, \
809 struct device_attribute *attr, char *buf) \
James Bottomley65c92b02006-06-28 12:22:50 -0400810{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100811 struct sas_port *port = transport_class_to_sas_port(dev); \
James Bottomley65c92b02006-06-28 12:22:50 -0400812 \
813 return snprintf(buf, 20, format_string, cast port->field); \
814}
815
816#define sas_port_simple_attr(field, name, format_string, type) \
817 sas_port_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100818static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
James Bottomley65c92b02006-06-28 12:22:50 -0400819
820sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
821
822static DECLARE_TRANSPORT_CLASS(sas_port_class,
823 "sas_port", NULL, NULL, NULL);
824
825static int sas_port_match(struct attribute_container *cont, struct device *dev)
826{
827 struct Scsi_Host *shost;
828 struct sas_internal *i;
829
830 if (!scsi_is_sas_port(dev))
831 return 0;
832 shost = dev_to_shost(dev->parent);
833
834 if (!shost->transportt)
835 return 0;
836 if (shost->transportt->host_attrs.ac.class !=
837 &sas_host_class.class)
838 return 0;
839
840 i = to_sas_internal(shost->transportt);
841 return &i->port_attr_cont.ac == cont;
842}
843
844
845static void sas_port_release(struct device *dev)
846{
847 struct sas_port *port = dev_to_sas_port(dev);
848
849 BUG_ON(!list_empty(&port->phy_list));
850
851 put_device(dev->parent);
852 kfree(port);
853}
854
855static void sas_port_create_link(struct sas_port *port,
856 struct sas_phy *phy)
857{
Darrick J. Wong21434962007-01-26 14:08:46 -0800858 int res;
859
860 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +0100861 dev_name(&phy->dev));
Darrick J. Wong21434962007-01-26 14:08:46 -0800862 if (res)
863 goto err;
864 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
865 if (res)
866 goto err;
867 return;
868err:
869 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700870 __func__, res);
James Bottomley65c92b02006-06-28 12:22:50 -0400871}
872
873static void sas_port_delete_link(struct sas_port *port,
874 struct sas_phy *phy)
875{
Kay Sievers71610f52008-12-03 22:41:36 +0100876 sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
James Bottomley65c92b02006-06-28 12:22:50 -0400877 sysfs_remove_link(&phy->dev.kobj, "port");
878}
879
880/** sas_port_alloc - allocate and initialize a SAS port structure
881 *
882 * @parent: parent device
883 * @port_id: port number
884 *
885 * Allocates a SAS port structure. It will be added to the device tree
886 * below the device specified by @parent which must be either a Scsi_Host
887 * or a sas_expander_device.
888 *
889 * Returns %NULL on error
890 */
891struct sas_port *sas_port_alloc(struct device *parent, int port_id)
892{
893 struct Scsi_Host *shost = dev_to_shost(parent);
894 struct sas_port *port;
895
896 port = kzalloc(sizeof(*port), GFP_KERNEL);
897 if (!port)
898 return NULL;
899
900 port->port_identifier = port_id;
901
902 device_initialize(&port->dev);
903
904 port->dev.parent = get_device(parent);
905 port->dev.release = sas_port_release;
906
907 mutex_init(&port->phy_list_mutex);
908 INIT_LIST_HEAD(&port->phy_list);
909
910 if (scsi_is_sas_expander_device(parent)) {
911 struct sas_rphy *rphy = dev_to_rphy(parent);
Kay Sievers71610f52008-12-03 22:41:36 +0100912 dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
913 rphy->scsi_target_id, port->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -0400914 } else
Kay Sievers71610f52008-12-03 22:41:36 +0100915 dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
916 port->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -0400917
918 transport_setup_device(&port->dev);
919
920 return port;
921}
922EXPORT_SYMBOL(sas_port_alloc);
923
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500924/** sas_port_alloc_num - allocate and initialize a SAS port structure
925 *
926 * @parent: parent device
927 *
928 * Allocates a SAS port structure and a number to go with it. This
929 * interface is really for adapters where the port number has no
930 * meansing, so the sas class should manage them. It will be added to
931 * the device tree below the device specified by @parent which must be
932 * either a Scsi_Host or a sas_expander_device.
933 *
934 * Returns %NULL on error
935 */
936struct sas_port *sas_port_alloc_num(struct device *parent)
937{
938 int index;
939 struct Scsi_Host *shost = dev_to_shost(parent);
940 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
941
942 /* FIXME: use idr for this eventually */
943 mutex_lock(&sas_host->lock);
944 if (scsi_is_sas_expander_device(parent)) {
945 struct sas_rphy *rphy = dev_to_rphy(parent);
946 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
947
948 index = exp->next_port_id++;
949 } else
950 index = sas_host->next_port_id++;
951 mutex_unlock(&sas_host->lock);
952 return sas_port_alloc(parent, index);
953}
954EXPORT_SYMBOL(sas_port_alloc_num);
955
James Bottomley65c92b02006-06-28 12:22:50 -0400956/**
957 * sas_port_add - add a SAS port to the device hierarchy
James Bottomley65c92b02006-06-28 12:22:50 -0400958 * @port: port to be added
959 *
960 * publishes a port to the rest of the system
961 */
962int sas_port_add(struct sas_port *port)
963{
964 int error;
965
966 /* No phys should be added until this is made visible */
967 BUG_ON(!list_empty(&port->phy_list));
968
969 error = device_add(&port->dev);
970
971 if (error)
972 return error;
973
974 transport_add_device(&port->dev);
975 transport_configure_device(&port->dev);
976
977 return 0;
978}
979EXPORT_SYMBOL(sas_port_add);
980
981/**
Rob Landleyeb448202007-11-03 13:30:39 -0500982 * sas_port_free - free a SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -0400983 * @port: SAS PORT to free
984 *
985 * Frees the specified SAS PORT.
986 *
987 * Note:
988 * This function must only be called on a PORT that has not
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200989 * successfully been added using sas_port_add().
James Bottomley65c92b02006-06-28 12:22:50 -0400990 */
991void sas_port_free(struct sas_port *port)
992{
993 transport_destroy_device(&port->dev);
994 put_device(&port->dev);
995}
996EXPORT_SYMBOL(sas_port_free);
997
998/**
Rob Landleyeb448202007-11-03 13:30:39 -0500999 * sas_port_delete - remove SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -04001000 * @port: SAS PORT to remove
1001 *
1002 * Removes the specified SAS PORT. If the SAS PORT has an
1003 * associated phys, unlink them from the port as well.
1004 */
1005void sas_port_delete(struct sas_port *port)
1006{
1007 struct device *dev = &port->dev;
1008 struct sas_phy *phy, *tmp_phy;
1009
1010 if (port->rphy) {
1011 sas_rphy_delete(port->rphy);
1012 port->rphy = NULL;
1013 }
1014
1015 mutex_lock(&port->phy_list_mutex);
1016 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1017 port_siblings) {
1018 sas_port_delete_link(port, phy);
1019 list_del_init(&phy->port_siblings);
1020 }
1021 mutex_unlock(&port->phy_list_mutex);
1022
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001023 if (port->is_backlink) {
1024 struct device *parent = port->dev.parent;
1025
Kay Sievers71610f52008-12-03 22:41:36 +01001026 sysfs_remove_link(&port->dev.kobj, dev_name(parent));
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001027 port->is_backlink = 0;
1028 }
1029
James Bottomley65c92b02006-06-28 12:22:50 -04001030 transport_remove_device(dev);
1031 device_del(dev);
1032 transport_destroy_device(dev);
1033 put_device(dev);
1034}
1035EXPORT_SYMBOL(sas_port_delete);
1036
1037/**
Rob Landleyeb448202007-11-03 13:30:39 -05001038 * scsi_is_sas_port - check if a struct device represents a SAS port
James Bottomley65c92b02006-06-28 12:22:50 -04001039 * @dev: device to check
1040 *
1041 * Returns:
1042 * %1 if the device represents a SAS Port, %0 else
1043 */
1044int scsi_is_sas_port(const struct device *dev)
1045{
1046 return dev->release == sas_port_release;
1047}
1048EXPORT_SYMBOL(scsi_is_sas_port);
1049
1050/**
1051 * sas_port_add_phy - add another phy to a port to form a wide port
1052 * @port: port to add the phy to
1053 * @phy: phy to add
1054 *
1055 * When a port is initially created, it is empty (has no phys). All
1056 * ports must have at least one phy to operated, and all wide ports
1057 * must have at least two. The current code makes no difference
1058 * between ports and wide ports, but the only object that can be
1059 * connected to a remote device is a port, so ports must be formed on
1060 * all devices with phys if they're connected to anything.
1061 */
1062void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1063{
1064 mutex_lock(&port->phy_list_mutex);
1065 if (unlikely(!list_empty(&phy->port_siblings))) {
1066 /* make sure we're already on this port */
1067 struct sas_phy *tmp;
1068
1069 list_for_each_entry(tmp, &port->phy_list, port_siblings)
1070 if (tmp == phy)
1071 break;
1072 /* If this trips, you added a phy that was already
1073 * part of a different port */
1074 if (unlikely(tmp != phy)) {
Kay Sievers71610f52008-12-03 22:41:36 +01001075 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1076 dev_name(&phy->dev));
James Bottomley65c92b02006-06-28 12:22:50 -04001077 BUG();
1078 }
1079 } else {
1080 sas_port_create_link(port, phy);
1081 list_add_tail(&phy->port_siblings, &port->phy_list);
1082 port->num_phys++;
1083 }
1084 mutex_unlock(&port->phy_list_mutex);
1085}
1086EXPORT_SYMBOL(sas_port_add_phy);
1087
1088/**
1089 * sas_port_delete_phy - remove a phy from a port or wide port
1090 * @port: port to remove the phy from
1091 * @phy: phy to remove
1092 *
1093 * This operation is used for tearing down ports again. It must be
1094 * done to every port or wide port before calling sas_port_delete.
1095 */
1096void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1097{
1098 mutex_lock(&port->phy_list_mutex);
1099 sas_port_delete_link(port, phy);
1100 list_del_init(&phy->port_siblings);
1101 port->num_phys--;
1102 mutex_unlock(&port->phy_list_mutex);
1103}
1104EXPORT_SYMBOL(sas_port_delete_phy);
1105
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001106void sas_port_mark_backlink(struct sas_port *port)
1107{
Darrick J. Wong21434962007-01-26 14:08:46 -08001108 int res;
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001109 struct device *parent = port->dev.parent->parent->parent;
1110
1111 if (port->is_backlink)
1112 return;
1113 port->is_backlink = 1;
Darrick J. Wong21434962007-01-26 14:08:46 -08001114 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01001115 dev_name(parent));
Darrick J. Wong21434962007-01-26 14:08:46 -08001116 if (res)
1117 goto err;
1118 return;
1119err:
1120 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001121 __func__, res);
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001122
1123}
1124EXPORT_SYMBOL(sas_port_mark_backlink);
1125
James Bottomley65c92b02006-06-28 12:22:50 -04001126/*
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001127 * SAS remote PHY attributes.
1128 */
1129
1130#define sas_rphy_show_simple(field, name, format_string, cast) \
1131static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001132show_sas_rphy_##name(struct device *dev, \
1133 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001134{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001135 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001136 \
1137 return snprintf(buf, 20, format_string, cast rphy->field); \
1138}
1139
1140#define sas_rphy_simple_attr(field, name, format_string, type) \
1141 sas_rphy_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001142static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001143 show_sas_rphy_##name, NULL)
1144
1145#define sas_rphy_show_protocol(field, name) \
1146static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001147show_sas_rphy_##name(struct device *dev, \
1148 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001149{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001150 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001151 \
1152 if (!rphy->field) \
1153 return snprintf(buf, 20, "none\n"); \
1154 return get_sas_protocol_names(rphy->field, buf); \
1155}
1156
1157#define sas_rphy_protocol_attr(field, name) \
1158 sas_rphy_show_protocol(field, name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001159static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001160 show_sas_rphy_##name, NULL)
1161
1162static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001163show_sas_rphy_device_type(struct device *dev,
1164 struct device_attribute *attr, char *buf)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001165{
Tony Jonesee959b02008-02-22 00:13:36 +01001166 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001167
1168 if (!rphy->identify.device_type)
1169 return snprintf(buf, 20, "none\n");
1170 return get_sas_device_type_names(
1171 rphy->identify.device_type, buf);
1172}
1173
Tony Jonesee959b02008-02-22 00:13:36 +01001174static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001175 show_sas_rphy_device_type, NULL);
1176
Christoph Hellwiga0125642006-02-16 13:31:47 +01001177static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001178show_sas_rphy_enclosure_identifier(struct device *dev,
1179 struct device_attribute *attr, char *buf)
Christoph Hellwiga0125642006-02-16 13:31:47 +01001180{
Tony Jonesee959b02008-02-22 00:13:36 +01001181 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwiga0125642006-02-16 13:31:47 +01001182 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1183 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1184 struct sas_internal *i = to_sas_internal(shost->transportt);
1185 u64 identifier;
1186 int error;
1187
1188 /*
1189 * Only devices behind an expander are supported, because the
1190 * enclosure identifier is a SMP feature.
1191 */
James Bottomleyf4ad7b52006-08-25 13:48:18 -05001192 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +01001193 return -EINVAL;
1194
1195 error = i->f->get_enclosure_identifier(rphy, &identifier);
1196 if (error)
1197 return error;
1198 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1199}
1200
Tony Jonesee959b02008-02-22 00:13:36 +01001201static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
Christoph Hellwiga0125642006-02-16 13:31:47 +01001202 show_sas_rphy_enclosure_identifier, NULL);
1203
1204static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001205show_sas_rphy_bay_identifier(struct device *dev,
1206 struct device_attribute *attr, char *buf)
Christoph Hellwiga0125642006-02-16 13:31:47 +01001207{
Tony Jonesee959b02008-02-22 00:13:36 +01001208 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwiga0125642006-02-16 13:31:47 +01001209 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1210 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1211 struct sas_internal *i = to_sas_internal(shost->transportt);
1212 int val;
1213
James Bottomleyf4ad7b52006-08-25 13:48:18 -05001214 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +01001215 return -EINVAL;
1216
1217 val = i->f->get_bay_identifier(rphy);
1218 if (val < 0)
1219 return val;
1220 return sprintf(buf, "%d\n", val);
1221}
1222
Tony Jonesee959b02008-02-22 00:13:36 +01001223static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
Christoph Hellwiga0125642006-02-16 13:31:47 +01001224 show_sas_rphy_bay_identifier, NULL);
1225
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001226sas_rphy_protocol_attr(identify.initiator_port_protocols,
1227 initiator_port_protocols);
1228sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1229sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1230 unsigned long long);
1231sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1232
James Bottomley42ab0362006-03-04 09:10:18 -06001233/* only need 8 bytes of data plus header (4 or 8) */
1234#define BUF_SIZE 64
1235
1236int sas_read_port_mode_page(struct scsi_device *sdev)
1237{
1238 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
James Bottomley0f880092010-01-18 10:14:51 -06001239 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
James Bottomley42ab0362006-03-04 09:10:18 -06001240 struct scsi_mode_data mode_data;
1241 int res, error;
1242
James Bottomley42ab0362006-03-04 09:10:18 -06001243 if (!buffer)
1244 return -ENOMEM;
1245
1246 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1247 &mode_data, NULL);
1248
1249 error = -EINVAL;
1250 if (!scsi_status_is_good(res))
1251 goto out;
1252
1253 msdata = buffer + mode_data.header_length +
1254 mode_data.block_descriptor_length;
1255
1256 if (msdata - buffer > BUF_SIZE - 8)
1257 goto out;
1258
1259 error = 0;
1260
1261 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1262 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1263 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1264
1265 out:
1266 kfree(buffer);
1267 return error;
1268}
1269EXPORT_SYMBOL(sas_read_port_mode_page);
1270
James Bottomley79cb1812006-03-13 13:50:04 -06001271static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1272 "sas_end_device", NULL, NULL, NULL);
1273
James Bottomley42ab0362006-03-04 09:10:18 -06001274#define sas_end_dev_show_simple(field, name, format_string, cast) \
1275static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001276show_sas_end_dev_##name(struct device *dev, \
1277 struct device_attribute *attr, char *buf) \
James Bottomley42ab0362006-03-04 09:10:18 -06001278{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001279 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
James Bottomley42ab0362006-03-04 09:10:18 -06001280 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1281 \
1282 return snprintf(buf, 20, format_string, cast rdev->field); \
1283}
1284
1285#define sas_end_dev_simple_attr(field, name, format_string, type) \
1286 sas_end_dev_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001287static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
James Bottomley42ab0362006-03-04 09:10:18 -06001288 show_sas_end_dev_##name, NULL)
1289
1290sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1291sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1292 "%d\n", int);
1293sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1294 "%d\n", int);
James Bottomley0f880092010-01-18 10:14:51 -06001295sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1296 "%d\n", int);
1297sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1298 "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001299
James Bottomley79cb1812006-03-13 13:50:04 -06001300static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1301 "sas_expander", NULL, NULL, NULL);
1302
1303#define sas_expander_show_simple(field, name, format_string, cast) \
1304static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001305show_sas_expander_##name(struct device *dev, \
1306 struct device_attribute *attr, char *buf) \
James Bottomley79cb1812006-03-13 13:50:04 -06001307{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001308 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
James Bottomley79cb1812006-03-13 13:50:04 -06001309 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1310 \
1311 return snprintf(buf, 20, format_string, cast edev->field); \
1312}
1313
1314#define sas_expander_simple_attr(field, name, format_string, type) \
1315 sas_expander_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001316static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
James Bottomley79cb1812006-03-13 13:50:04 -06001317 show_sas_expander_##name, NULL)
1318
1319sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1320sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1321sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1322sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1323 "%s\n", char *);
1324sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1325sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1326 unsigned int);
1327sas_expander_simple_attr(level, level, "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001328
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001329static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
James Bottomley2f8600d2006-03-18 15:00:50 -06001330 "sas_device", NULL, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001331
1332static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1333{
1334 struct Scsi_Host *shost;
1335 struct sas_internal *i;
1336
1337 if (!scsi_is_sas_rphy(dev))
1338 return 0;
1339 shost = dev_to_shost(dev->parent->parent);
1340
1341 if (!shost->transportt)
1342 return 0;
1343 if (shost->transportt->host_attrs.ac.class !=
1344 &sas_host_class.class)
1345 return 0;
1346
1347 i = to_sas_internal(shost->transportt);
1348 return &i->rphy_attr_cont.ac == cont;
1349}
1350
James Bottomley42ab0362006-03-04 09:10:18 -06001351static int sas_end_dev_match(struct attribute_container *cont,
1352 struct device *dev)
1353{
1354 struct Scsi_Host *shost;
1355 struct sas_internal *i;
1356 struct sas_rphy *rphy;
1357
1358 if (!scsi_is_sas_rphy(dev))
1359 return 0;
1360 shost = dev_to_shost(dev->parent->parent);
1361 rphy = dev_to_rphy(dev);
1362
1363 if (!shost->transportt)
1364 return 0;
1365 if (shost->transportt->host_attrs.ac.class !=
1366 &sas_host_class.class)
1367 return 0;
1368
1369 i = to_sas_internal(shost->transportt);
1370 return &i->end_dev_attr_cont.ac == cont &&
James Bottomley2f8600d2006-03-18 15:00:50 -06001371 rphy->identify.device_type == SAS_END_DEVICE;
James Bottomley42ab0362006-03-04 09:10:18 -06001372}
1373
James Bottomley79cb1812006-03-13 13:50:04 -06001374static int sas_expander_match(struct attribute_container *cont,
1375 struct device *dev)
1376{
1377 struct Scsi_Host *shost;
1378 struct sas_internal *i;
1379 struct sas_rphy *rphy;
1380
1381 if (!scsi_is_sas_rphy(dev))
1382 return 0;
1383 shost = dev_to_shost(dev->parent->parent);
1384 rphy = dev_to_rphy(dev);
1385
1386 if (!shost->transportt)
1387 return 0;
1388 if (shost->transportt->host_attrs.ac.class !=
1389 &sas_host_class.class)
1390 return 0;
1391
1392 i = to_sas_internal(shost->transportt);
1393 return &i->expander_attr_cont.ac == cont &&
1394 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
James Bottomley2f8600d2006-03-18 15:00:50 -06001395 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
James Bottomley79cb1812006-03-13 13:50:04 -06001396}
1397
James Bottomley2f8600d2006-03-18 15:00:50 -06001398static void sas_expander_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001399{
1400 struct sas_rphy *rphy = dev_to_rphy(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001401 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001402
FUJITA Tomonori93c20a52008-04-19 00:43:15 +09001403 if (rphy->q)
1404 blk_cleanup_queue(rphy->q);
1405
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001406 put_device(dev->parent);
James Bottomley2f8600d2006-03-18 15:00:50 -06001407 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001408}
1409
James Bottomley2f8600d2006-03-18 15:00:50 -06001410static void sas_end_device_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001411{
James Bottomley2f8600d2006-03-18 15:00:50 -06001412 struct sas_rphy *rphy = dev_to_rphy(dev);
1413 struct sas_end_device *edev = rphy_to_end_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001414
FUJITA Tomonori93c20a52008-04-19 00:43:15 +09001415 if (rphy->q)
1416 blk_cleanup_queue(rphy->q);
1417
James Bottomley2f8600d2006-03-18 15:00:50 -06001418 put_device(dev->parent);
1419 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001420}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001421
1422/**
James Bottomleyc5943d32006-06-12 09:09:18 -05001423 * sas_rphy_initialize - common rphy intialization
1424 * @rphy: rphy to initialise
1425 *
1426 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1427 * initialise the common rphy component of each.
1428 */
1429static void sas_rphy_initialize(struct sas_rphy *rphy)
1430{
1431 INIT_LIST_HEAD(&rphy->list);
1432}
1433
1434/**
James Bottomley42ab0362006-03-04 09:10:18 -06001435 * sas_end_device_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001436 * @parent: which port
James Bottomley42ab0362006-03-04 09:10:18 -06001437 *
1438 * Allocates an SAS remote PHY structure, connected to @parent.
1439 *
1440 * Returns:
1441 * SAS PHY allocated or %NULL if the allocation failed.
1442 */
James Bottomley65c92b02006-06-28 12:22:50 -04001443struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
James Bottomley42ab0362006-03-04 09:10:18 -06001444{
1445 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1446 struct sas_end_device *rdev;
1447
1448 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1449 if (!rdev) {
James Bottomley42ab0362006-03-04 09:10:18 -06001450 return NULL;
1451 }
1452
1453 device_initialize(&rdev->rphy.dev);
1454 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001455 rdev->rphy.dev.release = sas_end_device_release;
James Bottomley65c92b02006-06-28 12:22:50 -04001456 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1457 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
Kay Sievers71610f52008-12-03 22:41:36 +01001458 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1459 shost->host_no, rphy->scsi_target_id,
1460 parent->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -04001461 } else
Kay Sievers71610f52008-12-03 22:41:36 +01001462 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1463 shost->host_no, parent->port_identifier);
James Bottomley42ab0362006-03-04 09:10:18 -06001464 rdev->rphy.identify.device_type = SAS_END_DEVICE;
James Bottomleyc5943d32006-06-12 09:09:18 -05001465 sas_rphy_initialize(&rdev->rphy);
James Bottomley42ab0362006-03-04 09:10:18 -06001466 transport_setup_device(&rdev->rphy.dev);
1467
1468 return &rdev->rphy;
1469}
1470EXPORT_SYMBOL(sas_end_device_alloc);
1471
James Bottomley79cb1812006-03-13 13:50:04 -06001472/**
1473 * sas_expander_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001474 * @parent: which port
1475 * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
James Bottomley79cb1812006-03-13 13:50:04 -06001476 *
1477 * Allocates an SAS remote PHY structure, connected to @parent.
1478 *
1479 * Returns:
1480 * SAS PHY allocated or %NULL if the allocation failed.
1481 */
James Bottomley65c92b02006-06-28 12:22:50 -04001482struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
James Bottomley79cb1812006-03-13 13:50:04 -06001483 enum sas_device_type type)
1484{
1485 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1486 struct sas_expander_device *rdev;
1487 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1488
1489 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1490 type != SAS_FANOUT_EXPANDER_DEVICE);
1491
1492 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1493 if (!rdev) {
James Bottomley79cb1812006-03-13 13:50:04 -06001494 return NULL;
1495 }
1496
1497 device_initialize(&rdev->rphy.dev);
1498 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001499 rdev->rphy.dev.release = sas_expander_release;
James Bottomley79cb1812006-03-13 13:50:04 -06001500 mutex_lock(&sas_host->lock);
1501 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1502 mutex_unlock(&sas_host->lock);
Kay Sievers71610f52008-12-03 22:41:36 +01001503 dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1504 shost->host_no, rdev->rphy.scsi_target_id);
James Bottomley79cb1812006-03-13 13:50:04 -06001505 rdev->rphy.identify.device_type = type;
James Bottomleyc5943d32006-06-12 09:09:18 -05001506 sas_rphy_initialize(&rdev->rphy);
James Bottomley79cb1812006-03-13 13:50:04 -06001507 transport_setup_device(&rdev->rphy.dev);
1508
1509 return &rdev->rphy;
1510}
1511EXPORT_SYMBOL(sas_expander_alloc);
James Bottomley42ab0362006-03-04 09:10:18 -06001512
1513/**
Rob Landleyeb448202007-11-03 13:30:39 -05001514 * sas_rphy_add - add a SAS remote PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001515 * @rphy: The remote PHY to be added
1516 *
1517 * Publishes a SAS remote PHY to the rest of the system.
1518 */
1519int sas_rphy_add(struct sas_rphy *rphy)
1520{
James Bottomley65c92b02006-06-28 12:22:50 -04001521 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001522 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1523 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1524 struct sas_identify *identify = &rphy->identify;
1525 int error;
1526
1527 if (parent->rphy)
1528 return -ENXIO;
1529 parent->rphy = rphy;
1530
1531 error = device_add(&rphy->dev);
1532 if (error)
1533 return error;
1534 transport_add_device(&rphy->dev);
1535 transport_configure_device(&rphy->dev);
James Bottomley39dca552007-07-20 18:22:17 -05001536 if (sas_bsg_initialize(shost, rphy))
Kay Sievers71610f52008-12-03 22:41:36 +01001537 printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
James Bottomley39dca552007-07-20 18:22:17 -05001538
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001539
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001540 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001541 list_add_tail(&rphy->list, &sas_host->rphy_list);
1542 if (identify->device_type == SAS_END_DEVICE &&
1543 (identify->target_port_protocols &
1544 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1545 rphy->scsi_target_id = sas_host->next_target_id++;
James Bottomley7676f832006-04-14 09:47:59 -05001546 else if (identify->device_type == SAS_END_DEVICE)
1547 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001548 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001549
James Bottomley79cb1812006-03-13 13:50:04 -06001550 if (identify->device_type == SAS_END_DEVICE &&
1551 rphy->scsi_target_id != -1) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001552 scsi_scan_target(&rphy->dev, 0,
Darrick J. Wongc8490f32007-01-11 14:15:09 -08001553 rphy->scsi_target_id, SCAN_WILD_CARD, 0);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001554 }
1555
1556 return 0;
1557}
1558EXPORT_SYMBOL(sas_rphy_add);
1559
1560/**
Rob Landleyeb448202007-11-03 13:30:39 -05001561 * sas_rphy_free - free a SAS remote PHY
1562 * @rphy: SAS remote PHY to free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001563 *
1564 * Frees the specified SAS remote PHY.
1565 *
1566 * Note:
1567 * This function must only be called on a remote
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001568 * PHY that has not successfully been added using
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001569 * sas_rphy_add() (or has been sas_rphy_remove()'d)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001570 */
1571void sas_rphy_free(struct sas_rphy *rphy)
1572{
Mike Anderson92aab642006-03-27 09:37:28 -08001573 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001574 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1575 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1576
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001577 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001578 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001579 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001580
James Bottomleyb6aff662007-07-20 11:10:05 -05001581 sas_bsg_remove(shost, rphy);
1582
Mike Anderson92aab642006-03-27 09:37:28 -08001583 transport_destroy_device(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001584
Mike Anderson92aab642006-03-27 09:37:28 -08001585 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001586}
1587EXPORT_SYMBOL(sas_rphy_free);
1588
1589/**
Rob Landleyeb448202007-11-03 13:30:39 -05001590 * sas_rphy_delete - remove and free SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001591 * @rphy: SAS remote PHY to remove and free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001592 *
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001593 * Removes the specified SAS remote PHY and frees it.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001594 */
1595void
1596sas_rphy_delete(struct sas_rphy *rphy)
1597{
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001598 sas_rphy_remove(rphy);
1599 sas_rphy_free(rphy);
1600}
1601EXPORT_SYMBOL(sas_rphy_delete);
1602
1603/**
Rob Landleyeb448202007-11-03 13:30:39 -05001604 * sas_rphy_remove - remove SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001605 * @rphy: SAS remote phy to remove
1606 *
1607 * Removes the specified SAS remote PHY.
1608 */
1609void
1610sas_rphy_remove(struct sas_rphy *rphy)
1611{
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001612 struct device *dev = &rphy->dev;
James Bottomley65c92b02006-06-28 12:22:50 -04001613 struct sas_port *parent = dev_to_sas_port(dev->parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001614
Christoph Hellwigd4054232006-01-04 13:45:20 +01001615 switch (rphy->identify.device_type) {
1616 case SAS_END_DEVICE:
1617 scsi_remove_target(dev);
1618 break;
1619 case SAS_EDGE_EXPANDER_DEVICE:
1620 case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley65c92b02006-06-28 12:22:50 -04001621 sas_remove_children(dev);
Christoph Hellwigd4054232006-01-04 13:45:20 +01001622 break;
1623 default:
1624 break;
1625 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001626
Christoph Hellwigfe8b2302005-09-25 23:10:33 +02001627 transport_remove_device(dev);
1628 device_del(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001629
Christoph Hellwig33b114e2006-01-11 14:20:43 +01001630 parent->rphy = NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001631}
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001632EXPORT_SYMBOL(sas_rphy_remove);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001633
1634/**
Rob Landleyeb448202007-11-03 13:30:39 -05001635 * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001636 * @dev: device to check
1637 *
1638 * Returns:
1639 * %1 if the device represents a SAS remote PHY, %0 else
1640 */
1641int scsi_is_sas_rphy(const struct device *dev)
1642{
James Bottomley2f8600d2006-03-18 15:00:50 -06001643 return dev->release == sas_end_device_release ||
1644 dev->release == sas_expander_release;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001645}
1646EXPORT_SYMBOL(scsi_is_sas_rphy);
1647
1648
1649/*
1650 * SCSI scan helper
1651 */
1652
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001653static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1654 uint id, uint lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001655{
1656 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1657 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001658
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001659 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001660 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
James Bottomley6d99a3f2006-05-19 10:49:37 -05001661 if (rphy->identify.device_type != SAS_END_DEVICE ||
1662 rphy->scsi_target_id == -1)
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001663 continue;
1664
James Bottomleye8bf3942006-07-11 17:49:34 -04001665 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001666 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001667 scsi_scan_target(&rphy->dev, 0,
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001668 rphy->scsi_target_id, lun, 1);
1669 }
1670 }
1671 mutex_unlock(&sas_host->lock);
1672
1673 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001674}
1675
1676
1677/*
1678 * Setup / Teardown code
1679 */
1680
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001681#define SETUP_TEMPLATE(attrb, field, perm, test) \
Tony Jonesee959b02008-02-22 00:13:36 +01001682 i->private_##attrb[count] = dev_attr_##field; \
James Bottomley42ab0362006-03-04 09:10:18 -06001683 i->private_##attrb[count].attr.mode = perm; \
James Bottomley42ab0362006-03-04 09:10:18 -06001684 i->attrb[count] = &i->private_##attrb[count]; \
1685 if (test) \
1686 count++
1687
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001688#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
Tony Jonesee959b02008-02-22 00:13:36 +01001689 i->private_##attrb[count] = dev_attr_##field; \
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001690 i->private_##attrb[count].attr.mode = perm; \
1691 if (ro_test) { \
1692 i->private_##attrb[count].attr.mode = ro_perm; \
1693 i->private_##attrb[count].store = NULL; \
1694 } \
1695 i->attrb[count] = &i->private_##attrb[count]; \
1696 if (test) \
1697 count++
James Bottomley42ab0362006-03-04 09:10:18 -06001698
1699#define SETUP_RPORT_ATTRIBUTE(field) \
1700 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001701
James Bottomleydd9fbb522006-03-02 16:01:31 -06001702#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001703 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001704
James Bottomley65c92b02006-06-28 12:22:50 -04001705#define SETUP_PHY_ATTRIBUTE(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001706 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001707
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001708#define SETUP_PHY_ATTRIBUTE_RW(field) \
1709 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1710 !i->f->set_phy_speed, S_IRUGO)
1711
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001712#define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1713 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1714 !i->f->func, S_IRUGO)
1715
James Bottomley65c92b02006-06-28 12:22:50 -04001716#define SETUP_PORT_ATTRIBUTE(field) \
1717 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1718
1719#define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001720 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001721
James Bottomley65c92b02006-06-28 12:22:50 -04001722#define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001723 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001724
James Bottomley65c92b02006-06-28 12:22:50 -04001725#define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001726 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001727
James Bottomley42ab0362006-03-04 09:10:18 -06001728#define SETUP_END_DEV_ATTRIBUTE(field) \
1729 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001730
James Bottomley79cb1812006-03-13 13:50:04 -06001731#define SETUP_EXPANDER_ATTRIBUTE(field) \
1732 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1733
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001734/**
Rob Landleyeb448202007-11-03 13:30:39 -05001735 * sas_attach_transport - instantiate SAS transport template
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001736 * @ft: SAS transport class function template
1737 */
1738struct scsi_transport_template *
1739sas_attach_transport(struct sas_function_template *ft)
1740{
1741 struct sas_internal *i;
1742 int count;
1743
Jes Sorensen24669f752006-01-16 10:31:18 -05001744 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001745 if (!i)
1746 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001747
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001748 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001749
1750 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1751 i->t.host_attrs.ac.class = &sas_host_class.class;
1752 i->t.host_attrs.ac.match = sas_host_match;
1753 transport_container_register(&i->t.host_attrs);
1754 i->t.host_size = sizeof(struct sas_host_attrs);
1755
1756 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1757 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1758 i->phy_attr_cont.ac.match = sas_phy_match;
1759 transport_container_register(&i->phy_attr_cont);
1760
James Bottomley65c92b02006-06-28 12:22:50 -04001761 i->port_attr_cont.ac.class = &sas_port_class.class;
1762 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1763 i->port_attr_cont.ac.match = sas_port_match;
1764 transport_container_register(&i->port_attr_cont);
1765
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001766 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1767 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1768 i->rphy_attr_cont.ac.match = sas_rphy_match;
1769 transport_container_register(&i->rphy_attr_cont);
1770
James Bottomley42ab0362006-03-04 09:10:18 -06001771 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1772 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1773 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1774 transport_container_register(&i->end_dev_attr_cont);
1775
James Bottomley79cb1812006-03-13 13:50:04 -06001776 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1777 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1778 i->expander_attr_cont.ac.match = sas_expander_match;
1779 transport_container_register(&i->expander_attr_cont);
1780
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001781 i->f = ft;
1782
1783 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001784 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1785 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1786 SETUP_PHY_ATTRIBUTE(device_type);
1787 SETUP_PHY_ATTRIBUTE(sas_address);
1788 SETUP_PHY_ATTRIBUTE(phy_identifier);
1789 //SETUP_PHY_ATTRIBUTE(port_identifier);
1790 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1791 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001792 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
James Bottomley65c92b02006-06-28 12:22:50 -04001793 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001794 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +02001795
James Bottomley65c92b02006-06-28 12:22:50 -04001796 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1797 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1798 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1799 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1800 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1801 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001802 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001803 i->phy_attrs[count] = NULL;
1804
1805 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001806 SETUP_PORT_ATTRIBUTE(num_phys);
1807 i->port_attrs[count] = NULL;
1808
1809 count = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001810 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1811 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1812 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1813 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1814 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001815 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1816 get_enclosure_identifier);
1817 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1818 get_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001819 i->rphy_attrs[count] = NULL;
1820
James Bottomley42ab0362006-03-04 09:10:18 -06001821 count = 0;
1822 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1823 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1824 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
James Bottomley0f880092010-01-18 10:14:51 -06001825 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1826 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
James Bottomley42ab0362006-03-04 09:10:18 -06001827 i->end_dev_attrs[count] = NULL;
1828
James Bottomley79cb1812006-03-13 13:50:04 -06001829 count = 0;
1830 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1831 SETUP_EXPANDER_ATTRIBUTE(product_id);
1832 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1833 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1834 SETUP_EXPANDER_ATTRIBUTE(component_id);
1835 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1836 SETUP_EXPANDER_ATTRIBUTE(level);
1837 i->expander_attrs[count] = NULL;
1838
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001839 return &i->t;
1840}
1841EXPORT_SYMBOL(sas_attach_transport);
1842
1843/**
Rob Landleyeb448202007-11-03 13:30:39 -05001844 * sas_release_transport - release SAS transport template instance
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001845 * @t: transport template instance
1846 */
1847void sas_release_transport(struct scsi_transport_template *t)
1848{
1849 struct sas_internal *i = to_sas_internal(t);
1850
1851 transport_container_unregister(&i->t.host_attrs);
1852 transport_container_unregister(&i->phy_attr_cont);
James Bottomley65c92b02006-06-28 12:22:50 -04001853 transport_container_unregister(&i->port_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001854 transport_container_unregister(&i->rphy_attr_cont);
James Bottomleydb82f842006-03-09 22:06:36 -05001855 transport_container_unregister(&i->end_dev_attr_cont);
James Bottomley79cb1812006-03-13 13:50:04 -06001856 transport_container_unregister(&i->expander_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001857
1858 kfree(i);
1859}
1860EXPORT_SYMBOL(sas_release_transport);
1861
1862static __init int sas_transport_init(void)
1863{
1864 int error;
1865
1866 error = transport_class_register(&sas_host_class);
1867 if (error)
1868 goto out;
1869 error = transport_class_register(&sas_phy_class);
1870 if (error)
1871 goto out_unregister_transport;
James Bottomley65c92b02006-06-28 12:22:50 -04001872 error = transport_class_register(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001873 if (error)
1874 goto out_unregister_phy;
James Bottomley65c92b02006-06-28 12:22:50 -04001875 error = transport_class_register(&sas_rphy_class);
1876 if (error)
1877 goto out_unregister_port;
James Bottomley42ab0362006-03-04 09:10:18 -06001878 error = transport_class_register(&sas_end_dev_class);
1879 if (error)
1880 goto out_unregister_rphy;
James Bottomley79cb1812006-03-13 13:50:04 -06001881 error = transport_class_register(&sas_expander_class);
1882 if (error)
1883 goto out_unregister_end_dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001884
1885 return 0;
1886
James Bottomley79cb1812006-03-13 13:50:04 -06001887 out_unregister_end_dev:
1888 transport_class_unregister(&sas_end_dev_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001889 out_unregister_rphy:
1890 transport_class_unregister(&sas_rphy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001891 out_unregister_port:
1892 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001893 out_unregister_phy:
1894 transport_class_unregister(&sas_phy_class);
1895 out_unregister_transport:
1896 transport_class_unregister(&sas_host_class);
1897 out:
1898 return error;
1899
1900}
1901
1902static void __exit sas_transport_exit(void)
1903{
1904 transport_class_unregister(&sas_host_class);
1905 transport_class_unregister(&sas_phy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001906 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001907 transport_class_unregister(&sas_rphy_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001908 transport_class_unregister(&sas_end_dev_class);
James Bottomley79cb1812006-03-13 13:50:04 -06001909 transport_class_unregister(&sas_expander_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001910}
1911
1912MODULE_AUTHOR("Christoph Hellwig");
Alexis Bruemmer86b9c4c2007-01-16 15:36:12 -08001913MODULE_DESCRIPTION("SAS Transport Attributes");
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001914MODULE_LICENSE("GPL");
1915
1916module_init(sas_transport_init);
1917module_exit(sas_transport_exit);