blob: f2149d0bb9997a4caff68d9900eeb6da8de8832a [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,
9 * and various sysfs attributes to expose these topologies and managment
10 * 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 */
56#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
58 __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
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900158static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
159 struct sas_rphy *rphy)
160{
161 struct request *req;
162 int ret;
163 int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
164
165 while (!blk_queue_plugged(q)) {
166 req = elv_next_request(q);
167 if (!req)
168 break;
169
170 blkdev_dequeue_request(req);
171
172 spin_unlock_irq(q->queue_lock);
173
174 handler = to_sas_internal(shost->transportt)->f->smp_handler;
175 ret = handler(shost, rphy, req);
James Bottomley2d507a02007-12-29 10:59:53 -0600176 req->errors = ret;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900177
178 spin_lock_irq(q->queue_lock);
179
180 req->end_io(req, ret);
181 }
182}
183
184static void sas_host_smp_request(struct request_queue *q)
185{
186 sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
187}
188
189static void sas_non_host_smp_request(struct request_queue *q)
190{
191 struct sas_rphy *rphy = q->queuedata;
192 sas_smp_request(q, rphy_to_shost(rphy), rphy);
193}
194
James Bottomley39dca552007-07-20 18:22:17 -0500195static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900196{
197 struct request_queue *q;
198 int error;
James Bottomley39dca552007-07-20 18:22:17 -0500199 struct device *dev;
200 char namebuf[BUS_ID_SIZE];
201 const char *name;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900202
203 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
204 printk("%s can't handle SMP requests\n", shost->hostt->name);
205 return 0;
206 }
207
James Bottomley39dca552007-07-20 18:22:17 -0500208 if (rphy) {
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900209 q = blk_init_queue(sas_non_host_smp_request, NULL);
James Bottomley39dca552007-07-20 18:22:17 -0500210 dev = &rphy->dev;
211 name = dev->bus_id;
212 } else {
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900213 q = blk_init_queue(sas_host_smp_request, NULL);
James Bottomley39dca552007-07-20 18:22:17 -0500214 dev = &shost->shost_gendev;
215 snprintf(namebuf, sizeof(namebuf),
216 "sas_host%d", shost->host_no);
217 name = namebuf;
218 }
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900219 if (!q)
220 return -ENOMEM;
221
James Bottomley39dca552007-07-20 18:22:17 -0500222 error = bsg_register_queue(q, dev, name);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900223 if (error) {
224 blk_cleanup_queue(q);
225 return -ENOMEM;
226 }
227
228 if (rphy)
James Bottomleyb6aff662007-07-20 11:10:05 -0500229 rphy->q = q;
230 else
231 to_sas_host_attrs(shost)->q = q;
232
233 if (rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900234 q->queuedata = rphy;
235 else
236 q->queuedata = shost;
237
238 set_bit(QUEUE_FLAG_BIDI, &q->queue_flags);
239
240 return 0;
241}
242
James Bottomleyb6aff662007-07-20 11:10:05 -0500243static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
244{
245 struct request_queue *q;
246
247 if (rphy)
248 q = rphy->q;
249 else
250 q = to_sas_host_attrs(shost)->q;
251
252 if (!q)
253 return;
254
255 bsg_unregister_queue(q);
256 blk_cleanup_queue(q);
257}
258
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200259/*
260 * SAS host attributes
261 */
262
James Bottomley37be6ee2005-09-09 18:38:27 -0500263static int sas_host_setup(struct transport_container *tc, struct device *dev,
264 struct class_device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200265{
266 struct Scsi_Host *shost = dev_to_shost(dev);
267 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
268
269 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100270 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200271 sas_host->next_target_id = 0;
James Bottomley79cb1812006-03-13 13:50:04 -0600272 sas_host->next_expander_id = 0;
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500273 sas_host->next_port_id = 0;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900274
James Bottomley39dca552007-07-20 18:22:17 -0500275 if (sas_bsg_initialize(shost, NULL))
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900276 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
277 shost->host_no);
278
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200279 return 0;
280}
281
James Bottomleyb6aff662007-07-20 11:10:05 -0500282static int sas_host_remove(struct transport_container *tc, struct device *dev,
283 struct class_device *cdev)
284{
285 struct Scsi_Host *shost = dev_to_shost(dev);
286
287 sas_bsg_remove(shost, NULL);
288
289 return 0;
290}
291
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200292static DECLARE_TRANSPORT_CLASS(sas_host_class,
James Bottomleyb6aff662007-07-20 11:10:05 -0500293 "sas_host", sas_host_setup, sas_host_remove, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200294
295static int sas_host_match(struct attribute_container *cont,
296 struct device *dev)
297{
298 struct Scsi_Host *shost;
299 struct sas_internal *i;
300
301 if (!scsi_is_host_device(dev))
302 return 0;
303 shost = dev_to_shost(dev);
304
305 if (!shost->transportt)
306 return 0;
307 if (shost->transportt->host_attrs.ac.class !=
308 &sas_host_class.class)
309 return 0;
310
311 i = to_sas_internal(shost->transportt);
312 return &i->t.host_attrs.ac == cont;
313}
314
315static int do_sas_phy_delete(struct device *dev, void *data)
316{
James Bottomley65c92b02006-06-28 12:22:50 -0400317 int pass = (int)(unsigned long)data;
318
319 if (pass == 0 && scsi_is_sas_port(dev))
320 sas_port_delete(dev_to_sas_port(dev));
321 else if (pass == 1 && scsi_is_sas_phy(dev))
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200322 sas_phy_delete(dev_to_phy(dev));
323 return 0;
324}
325
326/**
Rob Landleyeb448202007-11-03 13:30:39 -0500327 * sas_remove_children - tear down a devices SAS data structures
James Bottomley65c92b02006-06-28 12:22:50 -0400328 * @dev: device belonging to the sas object
329 *
330 * Removes all SAS PHYs and remote PHYs for a given object
331 */
332void sas_remove_children(struct device *dev)
333{
334 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
335 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
336}
337EXPORT_SYMBOL(sas_remove_children);
338
339/**
Rob Landleyeb448202007-11-03 13:30:39 -0500340 * sas_remove_host - tear down a Scsi_Host's SAS data structures
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200341 * @shost: Scsi Host that is torn down
342 *
343 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
344 * Must be called just before scsi_remove_host for SAS HBAs.
345 */
346void sas_remove_host(struct Scsi_Host *shost)
347{
James Bottomley65c92b02006-06-28 12:22:50 -0400348 sas_remove_children(&shost->shost_gendev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200349}
350EXPORT_SYMBOL(sas_remove_host);
351
352
353/*
James Bottomley65c92b02006-06-28 12:22:50 -0400354 * SAS Phy attributes
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200355 */
356
357#define sas_phy_show_simple(field, name, format_string, cast) \
358static ssize_t \
359show_sas_phy_##name(struct class_device *cdev, char *buf) \
360{ \
361 struct sas_phy *phy = transport_class_to_phy(cdev); \
362 \
363 return snprintf(buf, 20, format_string, cast phy->field); \
364}
365
366#define sas_phy_simple_attr(field, name, format_string, type) \
367 sas_phy_show_simple(field, name, format_string, (type)) \
368static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
369
370#define sas_phy_show_protocol(field, name) \
371static ssize_t \
372show_sas_phy_##name(struct class_device *cdev, char *buf) \
373{ \
374 struct sas_phy *phy = transport_class_to_phy(cdev); \
375 \
376 if (!phy->field) \
377 return snprintf(buf, 20, "none\n"); \
378 return get_sas_protocol_names(phy->field, buf); \
379}
380
381#define sas_phy_protocol_attr(field, name) \
382 sas_phy_show_protocol(field, name) \
383static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
384
385#define sas_phy_show_linkspeed(field) \
386static ssize_t \
387show_sas_phy_##field(struct class_device *cdev, char *buf) \
388{ \
389 struct sas_phy *phy = transport_class_to_phy(cdev); \
390 \
391 return get_sas_linkspeed_names(phy->field, buf); \
392}
393
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500394/* Fudge to tell if we're minimum or maximum */
395#define sas_phy_store_linkspeed(field) \
396static ssize_t \
397store_sas_phy_##field(struct class_device *cdev, const char *buf, \
398 size_t count) \
399{ \
400 struct sas_phy *phy = transport_class_to_phy(cdev); \
401 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
402 struct sas_internal *i = to_sas_internal(shost->transportt); \
403 u32 value; \
404 struct sas_phy_linkrates rates = {0}; \
405 int error; \
406 \
407 error = set_sas_linkspeed_names(&value, buf); \
408 if (error) \
409 return error; \
410 rates.field = value; \
411 error = i->f->set_phy_speed(phy, &rates); \
412 \
413 return error ? error : count; \
414}
415
416#define sas_phy_linkspeed_rw_attr(field) \
417 sas_phy_show_linkspeed(field) \
418 sas_phy_store_linkspeed(field) \
419static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
420 store_sas_phy_##field)
421
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200422#define sas_phy_linkspeed_attr(field) \
423 sas_phy_show_linkspeed(field) \
424static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
425
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500426
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200427#define sas_phy_show_linkerror(field) \
428static ssize_t \
429show_sas_phy_##field(struct class_device *cdev, char *buf) \
430{ \
431 struct sas_phy *phy = transport_class_to_phy(cdev); \
432 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
433 struct sas_internal *i = to_sas_internal(shost->transportt); \
434 int error; \
435 \
James Bottomleydd9fbb522006-03-02 16:01:31 -0600436 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200437 if (error) \
438 return error; \
439 return snprintf(buf, 20, "%u\n", phy->field); \
440}
441
442#define sas_phy_linkerror_attr(field) \
443 sas_phy_show_linkerror(field) \
444static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
445
446
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200447static ssize_t
448show_sas_device_type(struct class_device *cdev, char *buf)
449{
450 struct sas_phy *phy = transport_class_to_phy(cdev);
451
452 if (!phy->identify.device_type)
453 return snprintf(buf, 20, "none\n");
454 return get_sas_device_type_names(phy->identify.device_type, buf);
455}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200456static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
457
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800458static ssize_t do_sas_phy_enable(struct class_device *cdev,
459 size_t count, int enable)
460{
461 struct sas_phy *phy = transport_class_to_phy(cdev);
462 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
463 struct sas_internal *i = to_sas_internal(shost->transportt);
464 int error;
465
466 error = i->f->phy_enable(phy, enable);
467 if (error)
468 return error;
469 phy->enabled = enable;
470 return count;
471};
472
473static ssize_t store_sas_phy_enable(struct class_device *cdev,
474 const char *buf, size_t count)
475{
476 if (count < 1)
477 return -EINVAL;
478
479 switch (buf[0]) {
480 case '0':
481 do_sas_phy_enable(cdev, count, 0);
482 break;
483 case '1':
484 do_sas_phy_enable(cdev, count, 1);
485 break;
486 default:
487 return -EINVAL;
488 }
489
490 return count;
491}
492
493static ssize_t show_sas_phy_enable(struct class_device *cdev, char *buf)
494{
495 struct sas_phy *phy = transport_class_to_phy(cdev);
496
497 return snprintf(buf, 20, "%d", phy->enabled);
498}
499
500static CLASS_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
501 store_sas_phy_enable);
502
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200503static ssize_t do_sas_phy_reset(struct class_device *cdev,
504 size_t count, int hard_reset)
505{
506 struct sas_phy *phy = transport_class_to_phy(cdev);
507 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
508 struct sas_internal *i = to_sas_internal(shost->transportt);
509 int error;
510
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200511 error = i->f->phy_reset(phy, hard_reset);
512 if (error)
513 return error;
514 return count;
515};
516
517static ssize_t store_sas_link_reset(struct class_device *cdev,
518 const char *buf, size_t count)
519{
520 return do_sas_phy_reset(cdev, count, 0);
521}
522static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
523
524static ssize_t store_sas_hard_reset(struct class_device *cdev,
525 const char *buf, size_t count)
526{
527 return do_sas_phy_reset(cdev, count, 1);
528}
529static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
530
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200531sas_phy_protocol_attr(identify.initiator_port_protocols,
532 initiator_port_protocols);
533sas_phy_protocol_attr(identify.target_port_protocols,
534 target_port_protocols);
535sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
536 unsigned long long);
537sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500538//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200539sas_phy_linkspeed_attr(negotiated_linkrate);
540sas_phy_linkspeed_attr(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500541sas_phy_linkspeed_rw_attr(minimum_linkrate);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200542sas_phy_linkspeed_attr(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500543sas_phy_linkspeed_rw_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200544sas_phy_linkerror_attr(invalid_dword_count);
545sas_phy_linkerror_attr(running_disparity_error_count);
546sas_phy_linkerror_attr(loss_of_dword_sync_count);
547sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200548
549
550static DECLARE_TRANSPORT_CLASS(sas_phy_class,
551 "sas_phy", NULL, NULL, NULL);
552
553static int sas_phy_match(struct attribute_container *cont, struct device *dev)
554{
555 struct Scsi_Host *shost;
556 struct sas_internal *i;
557
558 if (!scsi_is_sas_phy(dev))
559 return 0;
560 shost = dev_to_shost(dev->parent);
561
562 if (!shost->transportt)
563 return 0;
564 if (shost->transportt->host_attrs.ac.class !=
565 &sas_host_class.class)
566 return 0;
567
568 i = to_sas_internal(shost->transportt);
569 return &i->phy_attr_cont.ac == cont;
570}
571
572static void sas_phy_release(struct device *dev)
573{
574 struct sas_phy *phy = dev_to_phy(dev);
575
576 put_device(dev->parent);
577 kfree(phy);
578}
579
580/**
Rob Landleyeb448202007-11-03 13:30:39 -0500581 * sas_phy_alloc - allocates and initialize a SAS PHY structure
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200582 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700583 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200584 *
585 * Allocates an SAS PHY structure. It will be added in the device tree
586 * below the device specified by @parent, which has to be either a Scsi_Host
587 * or sas_rphy.
588 *
589 * Returns:
590 * SAS PHY allocated or %NULL if the allocation failed.
591 */
592struct sas_phy *sas_phy_alloc(struct device *parent, int number)
593{
594 struct Scsi_Host *shost = dev_to_shost(parent);
595 struct sas_phy *phy;
596
Jes Sorensen24669f752006-01-16 10:31:18 -0500597 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200598 if (!phy)
599 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200600
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200601 phy->number = number;
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800602 phy->enabled = 1;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200603
604 device_initialize(&phy->dev);
605 phy->dev.parent = get_device(parent);
606 phy->dev.release = sas_phy_release;
James Bottomley65c92b02006-06-28 12:22:50 -0400607 INIT_LIST_HEAD(&phy->port_siblings);
James Bottomley79cb1812006-03-13 13:50:04 -0600608 if (scsi_is_sas_expander_device(parent)) {
609 struct sas_rphy *rphy = dev_to_rphy(parent);
James Bottomley65c92b02006-06-28 12:22:50 -0400610 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
James Bottomley79cb1812006-03-13 13:50:04 -0600611 rphy->scsi_target_id, number);
612 } else
613 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200614
615 transport_setup_device(&phy->dev);
616
617 return phy;
618}
619EXPORT_SYMBOL(sas_phy_alloc);
620
621/**
Rob Landleyeb448202007-11-03 13:30:39 -0500622 * sas_phy_add - add a SAS PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200623 * @phy: The PHY to be added
624 *
625 * Publishes a SAS PHY to the rest of the system.
626 */
627int sas_phy_add(struct sas_phy *phy)
628{
629 int error;
630
631 error = device_add(&phy->dev);
632 if (!error) {
633 transport_add_device(&phy->dev);
634 transport_configure_device(&phy->dev);
635 }
636
637 return error;
638}
639EXPORT_SYMBOL(sas_phy_add);
640
641/**
Rob Landleyeb448202007-11-03 13:30:39 -0500642 * sas_phy_free - free a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200643 * @phy: SAS PHY to free
644 *
645 * Frees the specified SAS PHY.
646 *
647 * Note:
648 * This function must only be called on a PHY that has not
649 * sucessfully been added using sas_phy_add().
650 */
651void sas_phy_free(struct sas_phy *phy)
652{
653 transport_destroy_device(&phy->dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800654 put_device(&phy->dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200655}
656EXPORT_SYMBOL(sas_phy_free);
657
658/**
Rob Landleyeb448202007-11-03 13:30:39 -0500659 * sas_phy_delete - remove SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200660 * @phy: SAS PHY to remove
661 *
662 * Removes the specified SAS PHY. If the SAS PHY has an
663 * associated remote PHY it is removed before.
664 */
665void
666sas_phy_delete(struct sas_phy *phy)
667{
668 struct device *dev = &phy->dev;
669
James Bottomley65c92b02006-06-28 12:22:50 -0400670 /* this happens if the phy is still part of a port when deleted */
671 BUG_ON(!list_empty(&phy->port_siblings));
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200672
673 transport_remove_device(dev);
674 device_del(dev);
675 transport_destroy_device(dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800676 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200677}
678EXPORT_SYMBOL(sas_phy_delete);
679
680/**
Rob Landleyeb448202007-11-03 13:30:39 -0500681 * scsi_is_sas_phy - check if a struct device represents a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200682 * @dev: device to check
683 *
684 * Returns:
685 * %1 if the device represents a SAS PHY, %0 else
686 */
687int scsi_is_sas_phy(const struct device *dev)
688{
689 return dev->release == sas_phy_release;
690}
691EXPORT_SYMBOL(scsi_is_sas_phy);
692
693/*
James Bottomley65c92b02006-06-28 12:22:50 -0400694 * SAS Port attributes
695 */
696#define sas_port_show_simple(field, name, format_string, cast) \
697static ssize_t \
698show_sas_port_##name(struct class_device *cdev, char *buf) \
699{ \
700 struct sas_port *port = transport_class_to_sas_port(cdev); \
701 \
702 return snprintf(buf, 20, format_string, cast port->field); \
703}
704
705#define sas_port_simple_attr(field, name, format_string, type) \
706 sas_port_show_simple(field, name, format_string, (type)) \
707static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
708
709sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
710
711static DECLARE_TRANSPORT_CLASS(sas_port_class,
712 "sas_port", NULL, NULL, NULL);
713
714static int sas_port_match(struct attribute_container *cont, struct device *dev)
715{
716 struct Scsi_Host *shost;
717 struct sas_internal *i;
718
719 if (!scsi_is_sas_port(dev))
720 return 0;
721 shost = dev_to_shost(dev->parent);
722
723 if (!shost->transportt)
724 return 0;
725 if (shost->transportt->host_attrs.ac.class !=
726 &sas_host_class.class)
727 return 0;
728
729 i = to_sas_internal(shost->transportt);
730 return &i->port_attr_cont.ac == cont;
731}
732
733
734static void sas_port_release(struct device *dev)
735{
736 struct sas_port *port = dev_to_sas_port(dev);
737
738 BUG_ON(!list_empty(&port->phy_list));
739
740 put_device(dev->parent);
741 kfree(port);
742}
743
744static void sas_port_create_link(struct sas_port *port,
745 struct sas_phy *phy)
746{
Darrick J. Wong21434962007-01-26 14:08:46 -0800747 int res;
748
749 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
750 phy->dev.bus_id);
751 if (res)
752 goto err;
753 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
754 if (res)
755 goto err;
756 return;
757err:
758 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
759 __FUNCTION__, res);
James Bottomley65c92b02006-06-28 12:22:50 -0400760}
761
762static void sas_port_delete_link(struct sas_port *port,
763 struct sas_phy *phy)
764{
765 sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
766 sysfs_remove_link(&phy->dev.kobj, "port");
767}
768
769/** sas_port_alloc - allocate and initialize a SAS port structure
770 *
771 * @parent: parent device
772 * @port_id: port number
773 *
774 * Allocates a SAS port structure. It will be added to the device tree
775 * below the device specified by @parent which must be either a Scsi_Host
776 * or a sas_expander_device.
777 *
778 * Returns %NULL on error
779 */
780struct sas_port *sas_port_alloc(struct device *parent, int port_id)
781{
782 struct Scsi_Host *shost = dev_to_shost(parent);
783 struct sas_port *port;
784
785 port = kzalloc(sizeof(*port), GFP_KERNEL);
786 if (!port)
787 return NULL;
788
789 port->port_identifier = port_id;
790
791 device_initialize(&port->dev);
792
793 port->dev.parent = get_device(parent);
794 port->dev.release = sas_port_release;
795
796 mutex_init(&port->phy_list_mutex);
797 INIT_LIST_HEAD(&port->phy_list);
798
799 if (scsi_is_sas_expander_device(parent)) {
800 struct sas_rphy *rphy = dev_to_rphy(parent);
801 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
802 rphy->scsi_target_id, port->port_identifier);
803 } else
804 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
805 port->port_identifier);
806
807 transport_setup_device(&port->dev);
808
809 return port;
810}
811EXPORT_SYMBOL(sas_port_alloc);
812
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500813/** sas_port_alloc_num - allocate and initialize a SAS port structure
814 *
815 * @parent: parent device
816 *
817 * Allocates a SAS port structure and a number to go with it. This
818 * interface is really for adapters where the port number has no
819 * meansing, so the sas class should manage them. It will be added to
820 * the device tree below the device specified by @parent which must be
821 * either a Scsi_Host or a sas_expander_device.
822 *
823 * Returns %NULL on error
824 */
825struct sas_port *sas_port_alloc_num(struct device *parent)
826{
827 int index;
828 struct Scsi_Host *shost = dev_to_shost(parent);
829 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
830
831 /* FIXME: use idr for this eventually */
832 mutex_lock(&sas_host->lock);
833 if (scsi_is_sas_expander_device(parent)) {
834 struct sas_rphy *rphy = dev_to_rphy(parent);
835 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
836
837 index = exp->next_port_id++;
838 } else
839 index = sas_host->next_port_id++;
840 mutex_unlock(&sas_host->lock);
841 return sas_port_alloc(parent, index);
842}
843EXPORT_SYMBOL(sas_port_alloc_num);
844
James Bottomley65c92b02006-06-28 12:22:50 -0400845/**
846 * sas_port_add - add a SAS port to the device hierarchy
James Bottomley65c92b02006-06-28 12:22:50 -0400847 * @port: port to be added
848 *
849 * publishes a port to the rest of the system
850 */
851int sas_port_add(struct sas_port *port)
852{
853 int error;
854
855 /* No phys should be added until this is made visible */
856 BUG_ON(!list_empty(&port->phy_list));
857
858 error = device_add(&port->dev);
859
860 if (error)
861 return error;
862
863 transport_add_device(&port->dev);
864 transport_configure_device(&port->dev);
865
866 return 0;
867}
868EXPORT_SYMBOL(sas_port_add);
869
870/**
Rob Landleyeb448202007-11-03 13:30:39 -0500871 * sas_port_free - free a SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -0400872 * @port: SAS PORT to free
873 *
874 * Frees the specified SAS PORT.
875 *
876 * Note:
877 * This function must only be called on a PORT that has not
878 * sucessfully been added using sas_port_add().
879 */
880void sas_port_free(struct sas_port *port)
881{
882 transport_destroy_device(&port->dev);
883 put_device(&port->dev);
884}
885EXPORT_SYMBOL(sas_port_free);
886
887/**
Rob Landleyeb448202007-11-03 13:30:39 -0500888 * sas_port_delete - remove SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -0400889 * @port: SAS PORT to remove
890 *
891 * Removes the specified SAS PORT. If the SAS PORT has an
892 * associated phys, unlink them from the port as well.
893 */
894void sas_port_delete(struct sas_port *port)
895{
896 struct device *dev = &port->dev;
897 struct sas_phy *phy, *tmp_phy;
898
899 if (port->rphy) {
900 sas_rphy_delete(port->rphy);
901 port->rphy = NULL;
902 }
903
904 mutex_lock(&port->phy_list_mutex);
905 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
906 port_siblings) {
907 sas_port_delete_link(port, phy);
908 list_del_init(&phy->port_siblings);
909 }
910 mutex_unlock(&port->phy_list_mutex);
911
James Bottomleya0e1b6e2006-07-09 12:38:19 -0500912 if (port->is_backlink) {
913 struct device *parent = port->dev.parent;
914
915 sysfs_remove_link(&port->dev.kobj, parent->bus_id);
916 port->is_backlink = 0;
917 }
918
James Bottomley65c92b02006-06-28 12:22:50 -0400919 transport_remove_device(dev);
920 device_del(dev);
921 transport_destroy_device(dev);
922 put_device(dev);
923}
924EXPORT_SYMBOL(sas_port_delete);
925
926/**
Rob Landleyeb448202007-11-03 13:30:39 -0500927 * scsi_is_sas_port - check if a struct device represents a SAS port
James Bottomley65c92b02006-06-28 12:22:50 -0400928 * @dev: device to check
929 *
930 * Returns:
931 * %1 if the device represents a SAS Port, %0 else
932 */
933int scsi_is_sas_port(const struct device *dev)
934{
935 return dev->release == sas_port_release;
936}
937EXPORT_SYMBOL(scsi_is_sas_port);
938
939/**
940 * sas_port_add_phy - add another phy to a port to form a wide port
941 * @port: port to add the phy to
942 * @phy: phy to add
943 *
944 * When a port is initially created, it is empty (has no phys). All
945 * ports must have at least one phy to operated, and all wide ports
946 * must have at least two. The current code makes no difference
947 * between ports and wide ports, but the only object that can be
948 * connected to a remote device is a port, so ports must be formed on
949 * all devices with phys if they're connected to anything.
950 */
951void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
952{
953 mutex_lock(&port->phy_list_mutex);
954 if (unlikely(!list_empty(&phy->port_siblings))) {
955 /* make sure we're already on this port */
956 struct sas_phy *tmp;
957
958 list_for_each_entry(tmp, &port->phy_list, port_siblings)
959 if (tmp == phy)
960 break;
961 /* If this trips, you added a phy that was already
962 * part of a different port */
963 if (unlikely(tmp != phy)) {
964 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
965 BUG();
966 }
967 } else {
968 sas_port_create_link(port, phy);
969 list_add_tail(&phy->port_siblings, &port->phy_list);
970 port->num_phys++;
971 }
972 mutex_unlock(&port->phy_list_mutex);
973}
974EXPORT_SYMBOL(sas_port_add_phy);
975
976/**
977 * sas_port_delete_phy - remove a phy from a port or wide port
978 * @port: port to remove the phy from
979 * @phy: phy to remove
980 *
981 * This operation is used for tearing down ports again. It must be
982 * done to every port or wide port before calling sas_port_delete.
983 */
984void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
985{
986 mutex_lock(&port->phy_list_mutex);
987 sas_port_delete_link(port, phy);
988 list_del_init(&phy->port_siblings);
989 port->num_phys--;
990 mutex_unlock(&port->phy_list_mutex);
991}
992EXPORT_SYMBOL(sas_port_delete_phy);
993
James Bottomleya0e1b6e2006-07-09 12:38:19 -0500994void sas_port_mark_backlink(struct sas_port *port)
995{
Darrick J. Wong21434962007-01-26 14:08:46 -0800996 int res;
James Bottomleya0e1b6e2006-07-09 12:38:19 -0500997 struct device *parent = port->dev.parent->parent->parent;
998
999 if (port->is_backlink)
1000 return;
1001 port->is_backlink = 1;
Darrick J. Wong21434962007-01-26 14:08:46 -08001002 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1003 parent->bus_id);
1004 if (res)
1005 goto err;
1006 return;
1007err:
1008 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1009 __FUNCTION__, res);
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001010
1011}
1012EXPORT_SYMBOL(sas_port_mark_backlink);
1013
James Bottomley65c92b02006-06-28 12:22:50 -04001014/*
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001015 * SAS remote PHY attributes.
1016 */
1017
1018#define sas_rphy_show_simple(field, name, format_string, cast) \
1019static ssize_t \
1020show_sas_rphy_##name(struct class_device *cdev, char *buf) \
1021{ \
1022 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1023 \
1024 return snprintf(buf, 20, format_string, cast rphy->field); \
1025}
1026
1027#define sas_rphy_simple_attr(field, name, format_string, type) \
1028 sas_rphy_show_simple(field, name, format_string, (type)) \
1029static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1030 show_sas_rphy_##name, NULL)
1031
1032#define sas_rphy_show_protocol(field, name) \
1033static ssize_t \
1034show_sas_rphy_##name(struct class_device *cdev, char *buf) \
1035{ \
1036 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1037 \
1038 if (!rphy->field) \
1039 return snprintf(buf, 20, "none\n"); \
1040 return get_sas_protocol_names(rphy->field, buf); \
1041}
1042
1043#define sas_rphy_protocol_attr(field, name) \
1044 sas_rphy_show_protocol(field, name) \
1045static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1046 show_sas_rphy_##name, NULL)
1047
1048static ssize_t
1049show_sas_rphy_device_type(struct class_device *cdev, char *buf)
1050{
1051 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1052
1053 if (!rphy->identify.device_type)
1054 return snprintf(buf, 20, "none\n");
1055 return get_sas_device_type_names(
1056 rphy->identify.device_type, buf);
1057}
1058
1059static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1060 show_sas_rphy_device_type, NULL);
1061
Christoph Hellwiga0125642006-02-16 13:31:47 +01001062static ssize_t
1063show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
1064{
1065 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1066 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1067 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1068 struct sas_internal *i = to_sas_internal(shost->transportt);
1069 u64 identifier;
1070 int error;
1071
1072 /*
1073 * Only devices behind an expander are supported, because the
1074 * enclosure identifier is a SMP feature.
1075 */
James Bottomleyf4ad7b52006-08-25 13:48:18 -05001076 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +01001077 return -EINVAL;
1078
1079 error = i->f->get_enclosure_identifier(rphy, &identifier);
1080 if (error)
1081 return error;
1082 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1083}
1084
1085static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1086 show_sas_rphy_enclosure_identifier, NULL);
1087
1088static ssize_t
1089show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
1090{
1091 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
1092 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1093 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1094 struct sas_internal *i = to_sas_internal(shost->transportt);
1095 int val;
1096
James Bottomleyf4ad7b52006-08-25 13:48:18 -05001097 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +01001098 return -EINVAL;
1099
1100 val = i->f->get_bay_identifier(rphy);
1101 if (val < 0)
1102 return val;
1103 return sprintf(buf, "%d\n", val);
1104}
1105
1106static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1107 show_sas_rphy_bay_identifier, NULL);
1108
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001109sas_rphy_protocol_attr(identify.initiator_port_protocols,
1110 initiator_port_protocols);
1111sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1112sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1113 unsigned long long);
1114sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1115
James Bottomley42ab0362006-03-04 09:10:18 -06001116/* only need 8 bytes of data plus header (4 or 8) */
1117#define BUF_SIZE 64
1118
1119int sas_read_port_mode_page(struct scsi_device *sdev)
1120{
1121 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1122 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
1123 struct sas_end_device *rdev;
1124 struct scsi_mode_data mode_data;
1125 int res, error;
1126
1127 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
1128
1129 rdev = rphy_to_end_device(rphy);
1130
1131 if (!buffer)
1132 return -ENOMEM;
1133
1134 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1135 &mode_data, NULL);
1136
1137 error = -EINVAL;
1138 if (!scsi_status_is_good(res))
1139 goto out;
1140
1141 msdata = buffer + mode_data.header_length +
1142 mode_data.block_descriptor_length;
1143
1144 if (msdata - buffer > BUF_SIZE - 8)
1145 goto out;
1146
1147 error = 0;
1148
1149 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1150 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1151 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1152
1153 out:
1154 kfree(buffer);
1155 return error;
1156}
1157EXPORT_SYMBOL(sas_read_port_mode_page);
1158
James Bottomley79cb1812006-03-13 13:50:04 -06001159static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1160 "sas_end_device", NULL, NULL, NULL);
1161
James Bottomley42ab0362006-03-04 09:10:18 -06001162#define sas_end_dev_show_simple(field, name, format_string, cast) \
1163static ssize_t \
1164show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
1165{ \
1166 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1167 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1168 \
1169 return snprintf(buf, 20, format_string, cast rdev->field); \
1170}
1171
1172#define sas_end_dev_simple_attr(field, name, format_string, type) \
1173 sas_end_dev_show_simple(field, name, format_string, (type)) \
1174static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1175 show_sas_end_dev_##name, NULL)
1176
1177sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1178sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1179 "%d\n", int);
1180sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1181 "%d\n", int);
1182
James Bottomley79cb1812006-03-13 13:50:04 -06001183static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1184 "sas_expander", NULL, NULL, NULL);
1185
1186#define sas_expander_show_simple(field, name, format_string, cast) \
1187static ssize_t \
1188show_sas_expander_##name(struct class_device *cdev, char *buf) \
1189{ \
1190 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1191 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1192 \
1193 return snprintf(buf, 20, format_string, cast edev->field); \
1194}
1195
1196#define sas_expander_simple_attr(field, name, format_string, type) \
1197 sas_expander_show_simple(field, name, format_string, (type)) \
1198static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
1199 show_sas_expander_##name, NULL)
1200
1201sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1202sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1203sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1204sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1205 "%s\n", char *);
1206sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1207sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1208 unsigned int);
1209sas_expander_simple_attr(level, level, "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001210
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001211static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
James Bottomley2f8600d2006-03-18 15:00:50 -06001212 "sas_device", NULL, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001213
1214static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1215{
1216 struct Scsi_Host *shost;
1217 struct sas_internal *i;
1218
1219 if (!scsi_is_sas_rphy(dev))
1220 return 0;
1221 shost = dev_to_shost(dev->parent->parent);
1222
1223 if (!shost->transportt)
1224 return 0;
1225 if (shost->transportt->host_attrs.ac.class !=
1226 &sas_host_class.class)
1227 return 0;
1228
1229 i = to_sas_internal(shost->transportt);
1230 return &i->rphy_attr_cont.ac == cont;
1231}
1232
James Bottomley42ab0362006-03-04 09:10:18 -06001233static int sas_end_dev_match(struct attribute_container *cont,
1234 struct device *dev)
1235{
1236 struct Scsi_Host *shost;
1237 struct sas_internal *i;
1238 struct sas_rphy *rphy;
1239
1240 if (!scsi_is_sas_rphy(dev))
1241 return 0;
1242 shost = dev_to_shost(dev->parent->parent);
1243 rphy = dev_to_rphy(dev);
1244
1245 if (!shost->transportt)
1246 return 0;
1247 if (shost->transportt->host_attrs.ac.class !=
1248 &sas_host_class.class)
1249 return 0;
1250
1251 i = to_sas_internal(shost->transportt);
1252 return &i->end_dev_attr_cont.ac == cont &&
James Bottomley2f8600d2006-03-18 15:00:50 -06001253 rphy->identify.device_type == SAS_END_DEVICE;
James Bottomley42ab0362006-03-04 09:10:18 -06001254}
1255
James Bottomley79cb1812006-03-13 13:50:04 -06001256static int sas_expander_match(struct attribute_container *cont,
1257 struct device *dev)
1258{
1259 struct Scsi_Host *shost;
1260 struct sas_internal *i;
1261 struct sas_rphy *rphy;
1262
1263 if (!scsi_is_sas_rphy(dev))
1264 return 0;
1265 shost = dev_to_shost(dev->parent->parent);
1266 rphy = dev_to_rphy(dev);
1267
1268 if (!shost->transportt)
1269 return 0;
1270 if (shost->transportt->host_attrs.ac.class !=
1271 &sas_host_class.class)
1272 return 0;
1273
1274 i = to_sas_internal(shost->transportt);
1275 return &i->expander_attr_cont.ac == cont &&
1276 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
James Bottomley2f8600d2006-03-18 15:00:50 -06001277 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
James Bottomley79cb1812006-03-13 13:50:04 -06001278}
1279
James Bottomley2f8600d2006-03-18 15:00:50 -06001280static void sas_expander_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001281{
1282 struct sas_rphy *rphy = dev_to_rphy(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001283 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001284
1285 put_device(dev->parent);
James Bottomley2f8600d2006-03-18 15:00:50 -06001286 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001287}
1288
James Bottomley2f8600d2006-03-18 15:00:50 -06001289static void sas_end_device_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001290{
James Bottomley2f8600d2006-03-18 15:00:50 -06001291 struct sas_rphy *rphy = dev_to_rphy(dev);
1292 struct sas_end_device *edev = rphy_to_end_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001293
James Bottomley2f8600d2006-03-18 15:00:50 -06001294 put_device(dev->parent);
1295 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001296}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001297
1298/**
James Bottomleyc5943d32006-06-12 09:09:18 -05001299 * sas_rphy_initialize - common rphy intialization
1300 * @rphy: rphy to initialise
1301 *
1302 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1303 * initialise the common rphy component of each.
1304 */
1305static void sas_rphy_initialize(struct sas_rphy *rphy)
1306{
1307 INIT_LIST_HEAD(&rphy->list);
1308}
1309
1310/**
James Bottomley42ab0362006-03-04 09:10:18 -06001311 * sas_end_device_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001312 * @parent: which port
James Bottomley42ab0362006-03-04 09:10:18 -06001313 *
1314 * Allocates an SAS remote PHY structure, connected to @parent.
1315 *
1316 * Returns:
1317 * SAS PHY allocated or %NULL if the allocation failed.
1318 */
James Bottomley65c92b02006-06-28 12:22:50 -04001319struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
James Bottomley42ab0362006-03-04 09:10:18 -06001320{
1321 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1322 struct sas_end_device *rdev;
1323
1324 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1325 if (!rdev) {
James Bottomley42ab0362006-03-04 09:10:18 -06001326 return NULL;
1327 }
1328
1329 device_initialize(&rdev->rphy.dev);
1330 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001331 rdev->rphy.dev.release = sas_end_device_release;
James Bottomley65c92b02006-06-28 12:22:50 -04001332 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1333 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1334 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
1335 shost->host_no, rphy->scsi_target_id, parent->port_identifier);
1336 } else
1337 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
1338 shost->host_no, parent->port_identifier);
James Bottomley42ab0362006-03-04 09:10:18 -06001339 rdev->rphy.identify.device_type = SAS_END_DEVICE;
James Bottomleyc5943d32006-06-12 09:09:18 -05001340 sas_rphy_initialize(&rdev->rphy);
James Bottomley42ab0362006-03-04 09:10:18 -06001341 transport_setup_device(&rdev->rphy.dev);
1342
1343 return &rdev->rphy;
1344}
1345EXPORT_SYMBOL(sas_end_device_alloc);
1346
James Bottomley79cb1812006-03-13 13:50:04 -06001347/**
1348 * sas_expander_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001349 * @parent: which port
1350 * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
James Bottomley79cb1812006-03-13 13:50:04 -06001351 *
1352 * Allocates an SAS remote PHY structure, connected to @parent.
1353 *
1354 * Returns:
1355 * SAS PHY allocated or %NULL if the allocation failed.
1356 */
James Bottomley65c92b02006-06-28 12:22:50 -04001357struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
James Bottomley79cb1812006-03-13 13:50:04 -06001358 enum sas_device_type type)
1359{
1360 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1361 struct sas_expander_device *rdev;
1362 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1363
1364 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1365 type != SAS_FANOUT_EXPANDER_DEVICE);
1366
1367 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1368 if (!rdev) {
James Bottomley79cb1812006-03-13 13:50:04 -06001369 return NULL;
1370 }
1371
1372 device_initialize(&rdev->rphy.dev);
1373 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001374 rdev->rphy.dev.release = sas_expander_release;
James Bottomley79cb1812006-03-13 13:50:04 -06001375 mutex_lock(&sas_host->lock);
1376 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1377 mutex_unlock(&sas_host->lock);
1378 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
1379 shost->host_no, rdev->rphy.scsi_target_id);
1380 rdev->rphy.identify.device_type = type;
James Bottomleyc5943d32006-06-12 09:09:18 -05001381 sas_rphy_initialize(&rdev->rphy);
James Bottomley79cb1812006-03-13 13:50:04 -06001382 transport_setup_device(&rdev->rphy.dev);
1383
1384 return &rdev->rphy;
1385}
1386EXPORT_SYMBOL(sas_expander_alloc);
James Bottomley42ab0362006-03-04 09:10:18 -06001387
1388/**
Rob Landleyeb448202007-11-03 13:30:39 -05001389 * sas_rphy_add - add a SAS remote PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001390 * @rphy: The remote PHY to be added
1391 *
1392 * Publishes a SAS remote PHY to the rest of the system.
1393 */
1394int sas_rphy_add(struct sas_rphy *rphy)
1395{
James Bottomley65c92b02006-06-28 12:22:50 -04001396 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001397 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1398 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1399 struct sas_identify *identify = &rphy->identify;
1400 int error;
1401
1402 if (parent->rphy)
1403 return -ENXIO;
1404 parent->rphy = rphy;
1405
1406 error = device_add(&rphy->dev);
1407 if (error)
1408 return error;
1409 transport_add_device(&rphy->dev);
1410 transport_configure_device(&rphy->dev);
James Bottomley39dca552007-07-20 18:22:17 -05001411 if (sas_bsg_initialize(shost, rphy))
1412 printk("fail to a bsg device %s\n", rphy->dev.bus_id);
1413
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001414
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001415 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001416 list_add_tail(&rphy->list, &sas_host->rphy_list);
1417 if (identify->device_type == SAS_END_DEVICE &&
1418 (identify->target_port_protocols &
1419 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1420 rphy->scsi_target_id = sas_host->next_target_id++;
James Bottomley7676f832006-04-14 09:47:59 -05001421 else if (identify->device_type == SAS_END_DEVICE)
1422 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001423 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001424
James Bottomley79cb1812006-03-13 13:50:04 -06001425 if (identify->device_type == SAS_END_DEVICE &&
1426 rphy->scsi_target_id != -1) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001427 scsi_scan_target(&rphy->dev, 0,
Darrick J. Wongc8490f32007-01-11 14:15:09 -08001428 rphy->scsi_target_id, SCAN_WILD_CARD, 0);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001429 }
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL(sas_rphy_add);
1434
1435/**
Rob Landleyeb448202007-11-03 13:30:39 -05001436 * sas_rphy_free - free a SAS remote PHY
1437 * @rphy: SAS remote PHY to free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001438 *
1439 * Frees the specified SAS remote PHY.
1440 *
1441 * Note:
1442 * This function must only be called on a remote
1443 * PHY that has not sucessfully been added using
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001444 * sas_rphy_add() (or has been sas_rphy_remove()'d)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001445 */
1446void sas_rphy_free(struct sas_rphy *rphy)
1447{
Mike Anderson92aab642006-03-27 09:37:28 -08001448 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001449 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1450 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1451
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001452 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001453 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001454 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001455
James Bottomleyb6aff662007-07-20 11:10:05 -05001456 sas_bsg_remove(shost, rphy);
1457
Mike Anderson92aab642006-03-27 09:37:28 -08001458 transport_destroy_device(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001459
Mike Anderson92aab642006-03-27 09:37:28 -08001460 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001461}
1462EXPORT_SYMBOL(sas_rphy_free);
1463
1464/**
Rob Landleyeb448202007-11-03 13:30:39 -05001465 * sas_rphy_delete - remove and free SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001466 * @rphy: SAS remote PHY to remove and free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001467 *
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001468 * Removes the specified SAS remote PHY and frees it.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001469 */
1470void
1471sas_rphy_delete(struct sas_rphy *rphy)
1472{
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001473 sas_rphy_remove(rphy);
1474 sas_rphy_free(rphy);
1475}
1476EXPORT_SYMBOL(sas_rphy_delete);
1477
1478/**
Rob Landleyeb448202007-11-03 13:30:39 -05001479 * sas_rphy_remove - remove SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001480 * @rphy: SAS remote phy to remove
1481 *
1482 * Removes the specified SAS remote PHY.
1483 */
1484void
1485sas_rphy_remove(struct sas_rphy *rphy)
1486{
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001487 struct device *dev = &rphy->dev;
James Bottomley65c92b02006-06-28 12:22:50 -04001488 struct sas_port *parent = dev_to_sas_port(dev->parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001489
Christoph Hellwigd4054232006-01-04 13:45:20 +01001490 switch (rphy->identify.device_type) {
1491 case SAS_END_DEVICE:
1492 scsi_remove_target(dev);
1493 break;
1494 case SAS_EDGE_EXPANDER_DEVICE:
1495 case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley65c92b02006-06-28 12:22:50 -04001496 sas_remove_children(dev);
Christoph Hellwigd4054232006-01-04 13:45:20 +01001497 break;
1498 default:
1499 break;
1500 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001501
Christoph Hellwigfe8b2302005-09-25 23:10:33 +02001502 transport_remove_device(dev);
1503 device_del(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001504
Christoph Hellwig33b114e2006-01-11 14:20:43 +01001505 parent->rphy = NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001506}
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001507EXPORT_SYMBOL(sas_rphy_remove);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001508
1509/**
Rob Landleyeb448202007-11-03 13:30:39 -05001510 * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001511 * @dev: device to check
1512 *
1513 * Returns:
1514 * %1 if the device represents a SAS remote PHY, %0 else
1515 */
1516int scsi_is_sas_rphy(const struct device *dev)
1517{
James Bottomley2f8600d2006-03-18 15:00:50 -06001518 return dev->release == sas_end_device_release ||
1519 dev->release == sas_expander_release;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001520}
1521EXPORT_SYMBOL(scsi_is_sas_rphy);
1522
1523
1524/*
1525 * SCSI scan helper
1526 */
1527
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001528static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1529 uint id, uint lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001530{
1531 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1532 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001533
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001534 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001535 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
James Bottomley6d99a3f2006-05-19 10:49:37 -05001536 if (rphy->identify.device_type != SAS_END_DEVICE ||
1537 rphy->scsi_target_id == -1)
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001538 continue;
1539
James Bottomleye8bf3942006-07-11 17:49:34 -04001540 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001541 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001542 scsi_scan_target(&rphy->dev, 0,
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001543 rphy->scsi_target_id, lun, 1);
1544 }
1545 }
1546 mutex_unlock(&sas_host->lock);
1547
1548 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001549}
1550
1551
1552/*
1553 * Setup / Teardown code
1554 */
1555
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001556#define SETUP_TEMPLATE(attrb, field, perm, test) \
James Bottomley42ab0362006-03-04 09:10:18 -06001557 i->private_##attrb[count] = class_device_attr_##field; \
1558 i->private_##attrb[count].attr.mode = perm; \
James Bottomley42ab0362006-03-04 09:10:18 -06001559 i->attrb[count] = &i->private_##attrb[count]; \
1560 if (test) \
1561 count++
1562
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001563#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1564 i->private_##attrb[count] = class_device_attr_##field; \
1565 i->private_##attrb[count].attr.mode = perm; \
1566 if (ro_test) { \
1567 i->private_##attrb[count].attr.mode = ro_perm; \
1568 i->private_##attrb[count].store = NULL; \
1569 } \
1570 i->attrb[count] = &i->private_##attrb[count]; \
1571 if (test) \
1572 count++
James Bottomley42ab0362006-03-04 09:10:18 -06001573
1574#define SETUP_RPORT_ATTRIBUTE(field) \
1575 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001576
James Bottomleydd9fbb522006-03-02 16:01:31 -06001577#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001578 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001579
James Bottomley65c92b02006-06-28 12:22:50 -04001580#define SETUP_PHY_ATTRIBUTE(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001581 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001582
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001583#define SETUP_PHY_ATTRIBUTE_RW(field) \
1584 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1585 !i->f->set_phy_speed, S_IRUGO)
1586
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001587#define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1588 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1589 !i->f->func, S_IRUGO)
1590
James Bottomley65c92b02006-06-28 12:22:50 -04001591#define SETUP_PORT_ATTRIBUTE(field) \
1592 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1593
1594#define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001595 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001596
James Bottomley65c92b02006-06-28 12:22:50 -04001597#define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001598 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001599
James Bottomley65c92b02006-06-28 12:22:50 -04001600#define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001601 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001602
James Bottomley42ab0362006-03-04 09:10:18 -06001603#define SETUP_END_DEV_ATTRIBUTE(field) \
1604 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001605
James Bottomley79cb1812006-03-13 13:50:04 -06001606#define SETUP_EXPANDER_ATTRIBUTE(field) \
1607 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1608
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001609/**
Rob Landleyeb448202007-11-03 13:30:39 -05001610 * sas_attach_transport - instantiate SAS transport template
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001611 * @ft: SAS transport class function template
1612 */
1613struct scsi_transport_template *
1614sas_attach_transport(struct sas_function_template *ft)
1615{
1616 struct sas_internal *i;
1617 int count;
1618
Jes Sorensen24669f752006-01-16 10:31:18 -05001619 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001620 if (!i)
1621 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001622
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001623 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001624
1625 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1626 i->t.host_attrs.ac.class = &sas_host_class.class;
1627 i->t.host_attrs.ac.match = sas_host_match;
1628 transport_container_register(&i->t.host_attrs);
1629 i->t.host_size = sizeof(struct sas_host_attrs);
1630
1631 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1632 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1633 i->phy_attr_cont.ac.match = sas_phy_match;
1634 transport_container_register(&i->phy_attr_cont);
1635
James Bottomley65c92b02006-06-28 12:22:50 -04001636 i->port_attr_cont.ac.class = &sas_port_class.class;
1637 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1638 i->port_attr_cont.ac.match = sas_port_match;
1639 transport_container_register(&i->port_attr_cont);
1640
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001641 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1642 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1643 i->rphy_attr_cont.ac.match = sas_rphy_match;
1644 transport_container_register(&i->rphy_attr_cont);
1645
James Bottomley42ab0362006-03-04 09:10:18 -06001646 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1647 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1648 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1649 transport_container_register(&i->end_dev_attr_cont);
1650
James Bottomley79cb1812006-03-13 13:50:04 -06001651 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1652 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1653 i->expander_attr_cont.ac.match = sas_expander_match;
1654 transport_container_register(&i->expander_attr_cont);
1655
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001656 i->f = ft;
1657
1658 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001659 SETUP_PORT_ATTRIBUTE(num_phys);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001660 i->host_attrs[count] = NULL;
1661
1662 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001663 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1664 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1665 SETUP_PHY_ATTRIBUTE(device_type);
1666 SETUP_PHY_ATTRIBUTE(sas_address);
1667 SETUP_PHY_ATTRIBUTE(phy_identifier);
1668 //SETUP_PHY_ATTRIBUTE(port_identifier);
1669 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1670 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001671 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
James Bottomley65c92b02006-06-28 12:22:50 -04001672 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001673 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +02001674
James Bottomley65c92b02006-06-28 12:22:50 -04001675 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1676 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1677 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1678 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1679 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1680 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001681 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001682 i->phy_attrs[count] = NULL;
1683
1684 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001685 SETUP_PORT_ATTRIBUTE(num_phys);
1686 i->port_attrs[count] = NULL;
1687
1688 count = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001689 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1690 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1691 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1692 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1693 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001694 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1695 get_enclosure_identifier);
1696 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1697 get_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001698 i->rphy_attrs[count] = NULL;
1699
James Bottomley42ab0362006-03-04 09:10:18 -06001700 count = 0;
1701 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1702 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1703 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1704 i->end_dev_attrs[count] = NULL;
1705
James Bottomley79cb1812006-03-13 13:50:04 -06001706 count = 0;
1707 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1708 SETUP_EXPANDER_ATTRIBUTE(product_id);
1709 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1710 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1711 SETUP_EXPANDER_ATTRIBUTE(component_id);
1712 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1713 SETUP_EXPANDER_ATTRIBUTE(level);
1714 i->expander_attrs[count] = NULL;
1715
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001716 return &i->t;
1717}
1718EXPORT_SYMBOL(sas_attach_transport);
1719
1720/**
Rob Landleyeb448202007-11-03 13:30:39 -05001721 * sas_release_transport - release SAS transport template instance
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001722 * @t: transport template instance
1723 */
1724void sas_release_transport(struct scsi_transport_template *t)
1725{
1726 struct sas_internal *i = to_sas_internal(t);
1727
1728 transport_container_unregister(&i->t.host_attrs);
1729 transport_container_unregister(&i->phy_attr_cont);
James Bottomley65c92b02006-06-28 12:22:50 -04001730 transport_container_unregister(&i->port_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001731 transport_container_unregister(&i->rphy_attr_cont);
James Bottomleydb82f842006-03-09 22:06:36 -05001732 transport_container_unregister(&i->end_dev_attr_cont);
James Bottomley79cb1812006-03-13 13:50:04 -06001733 transport_container_unregister(&i->expander_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001734
1735 kfree(i);
1736}
1737EXPORT_SYMBOL(sas_release_transport);
1738
1739static __init int sas_transport_init(void)
1740{
1741 int error;
1742
1743 error = transport_class_register(&sas_host_class);
1744 if (error)
1745 goto out;
1746 error = transport_class_register(&sas_phy_class);
1747 if (error)
1748 goto out_unregister_transport;
James Bottomley65c92b02006-06-28 12:22:50 -04001749 error = transport_class_register(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001750 if (error)
1751 goto out_unregister_phy;
James Bottomley65c92b02006-06-28 12:22:50 -04001752 error = transport_class_register(&sas_rphy_class);
1753 if (error)
1754 goto out_unregister_port;
James Bottomley42ab0362006-03-04 09:10:18 -06001755 error = transport_class_register(&sas_end_dev_class);
1756 if (error)
1757 goto out_unregister_rphy;
James Bottomley79cb1812006-03-13 13:50:04 -06001758 error = transport_class_register(&sas_expander_class);
1759 if (error)
1760 goto out_unregister_end_dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001761
1762 return 0;
1763
James Bottomley79cb1812006-03-13 13:50:04 -06001764 out_unregister_end_dev:
1765 transport_class_unregister(&sas_end_dev_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001766 out_unregister_rphy:
1767 transport_class_unregister(&sas_rphy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001768 out_unregister_port:
1769 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001770 out_unregister_phy:
1771 transport_class_unregister(&sas_phy_class);
1772 out_unregister_transport:
1773 transport_class_unregister(&sas_host_class);
1774 out:
1775 return error;
1776
1777}
1778
1779static void __exit sas_transport_exit(void)
1780{
1781 transport_class_unregister(&sas_host_class);
1782 transport_class_unregister(&sas_phy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001783 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001784 transport_class_unregister(&sas_rphy_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001785 transport_class_unregister(&sas_end_dev_class);
James Bottomley79cb1812006-03-13 13:50:04 -06001786 transport_class_unregister(&sas_expander_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001787}
1788
1789MODULE_AUTHOR("Christoph Hellwig");
Alexis Bruemmer86b9c4c2007-01-16 15:36:12 -08001790MODULE_DESCRIPTION("SAS Transport Attributes");
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001791MODULE_LICENSE("GPL");
1792
1793module_init(sas_transport_init);
1794module_exit(sas_transport_exit);