blob: 1fe6b2d01853288adbe0e9b01121a7bc25b888dd [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>
28#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080029#include <linux/slab.h>
30#include <linux/string.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020031
Christoph Hellwige02f3f52006-01-13 19:04:00 +010032#include <scsi/scsi.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_sas.h>
37
James Bottomleyd6159c12006-03-27 16:45:34 -060038#include "scsi_sas_internal.h"
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020039struct sas_host_attrs {
40 struct list_head rphy_list;
Christoph Hellwige02f3f52006-01-13 19:04:00 +010041 struct mutex lock;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020042 u32 next_target_id;
James Bottomley79cb1812006-03-13 13:50:04 -060043 u32 next_expander_id;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020044};
45#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
46
47
48/*
49 * Hack to allow attributes of the same name in different objects.
50 */
51#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
52 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
53 __ATTR(_name,_mode,_show,_store)
54
55
56/*
57 * Pretty printing helpers
58 */
59
60#define sas_bitfield_name_match(title, table) \
61static ssize_t \
62get_sas_##title##_names(u32 table_key, char *buf) \
63{ \
64 char *prefix = ""; \
65 ssize_t len = 0; \
66 int i; \
67 \
Tobias Klauser6391a112006-06-08 22:23:48 -070068 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020069 if (table[i].value & table_key) { \
70 len += sprintf(buf + len, "%s%s", \
71 prefix, table[i].name); \
72 prefix = ", "; \
73 } \
74 } \
75 len += sprintf(buf + len, "\n"); \
76 return len; \
77}
78
79#define sas_bitfield_name_search(title, table) \
80static ssize_t \
81get_sas_##title##_names(u32 table_key, char *buf) \
82{ \
83 ssize_t len = 0; \
84 int i; \
85 \
Tobias Klauser6391a112006-06-08 22:23:48 -070086 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020087 if (table[i].value == table_key) { \
88 len += sprintf(buf + len, "%s", \
89 table[i].name); \
90 break; \
91 } \
92 } \
93 len += sprintf(buf + len, "\n"); \
94 return len; \
95}
96
97static struct {
98 u32 value;
99 char *name;
100} sas_device_type_names[] = {
101 { SAS_PHY_UNUSED, "unused" },
102 { SAS_END_DEVICE, "end device" },
103 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
104 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
105};
106sas_bitfield_name_search(device_type, sas_device_type_names)
107
108
109static struct {
110 u32 value;
111 char *name;
112} sas_protocol_names[] = {
113 { SAS_PROTOCOL_SATA, "sata" },
114 { SAS_PROTOCOL_SMP, "smp" },
115 { SAS_PROTOCOL_STP, "stp" },
116 { SAS_PROTOCOL_SSP, "ssp" },
117};
118sas_bitfield_name_match(protocol, sas_protocol_names)
119
120static struct {
121 u32 value;
122 char *name;
123} sas_linkspeed_names[] = {
124 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
125 { SAS_PHY_DISABLED, "Phy disabled" },
126 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
127 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
128 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
129 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
James Bottomley7e6dff62006-03-02 14:12:56 -0600130 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200131};
132sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
133
134
135/*
136 * SAS host attributes
137 */
138
James Bottomley37be6ee2005-09-09 18:38:27 -0500139static int sas_host_setup(struct transport_container *tc, struct device *dev,
140 struct class_device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200141{
142 struct Scsi_Host *shost = dev_to_shost(dev);
143 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
144
145 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100146 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200147 sas_host->next_target_id = 0;
James Bottomley79cb1812006-03-13 13:50:04 -0600148 sas_host->next_expander_id = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200149 return 0;
150}
151
152static DECLARE_TRANSPORT_CLASS(sas_host_class,
153 "sas_host", sas_host_setup, NULL, NULL);
154
155static int sas_host_match(struct attribute_container *cont,
156 struct device *dev)
157{
158 struct Scsi_Host *shost;
159 struct sas_internal *i;
160
161 if (!scsi_is_host_device(dev))
162 return 0;
163 shost = dev_to_shost(dev);
164
165 if (!shost->transportt)
166 return 0;
167 if (shost->transportt->host_attrs.ac.class !=
168 &sas_host_class.class)
169 return 0;
170
171 i = to_sas_internal(shost->transportt);
172 return &i->t.host_attrs.ac == cont;
173}
174
175static int do_sas_phy_delete(struct device *dev, void *data)
176{
177 if (scsi_is_sas_phy(dev))
178 sas_phy_delete(dev_to_phy(dev));
179 return 0;
180}
181
182/**
183 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
184 * @shost: Scsi Host that is torn down
185 *
186 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
187 * Must be called just before scsi_remove_host for SAS HBAs.
188 */
189void sas_remove_host(struct Scsi_Host *shost)
190{
191 device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
192}
193EXPORT_SYMBOL(sas_remove_host);
194
195
196/*
197 * SAS Port attributes
198 */
199
200#define sas_phy_show_simple(field, name, format_string, cast) \
201static ssize_t \
202show_sas_phy_##name(struct class_device *cdev, char *buf) \
203{ \
204 struct sas_phy *phy = transport_class_to_phy(cdev); \
205 \
206 return snprintf(buf, 20, format_string, cast phy->field); \
207}
208
209#define sas_phy_simple_attr(field, name, format_string, type) \
210 sas_phy_show_simple(field, name, format_string, (type)) \
211static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
212
213#define sas_phy_show_protocol(field, name) \
214static ssize_t \
215show_sas_phy_##name(struct class_device *cdev, char *buf) \
216{ \
217 struct sas_phy *phy = transport_class_to_phy(cdev); \
218 \
219 if (!phy->field) \
220 return snprintf(buf, 20, "none\n"); \
221 return get_sas_protocol_names(phy->field, buf); \
222}
223
224#define sas_phy_protocol_attr(field, name) \
225 sas_phy_show_protocol(field, name) \
226static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
227
228#define sas_phy_show_linkspeed(field) \
229static ssize_t \
230show_sas_phy_##field(struct class_device *cdev, char *buf) \
231{ \
232 struct sas_phy *phy = transport_class_to_phy(cdev); \
233 \
234 return get_sas_linkspeed_names(phy->field, buf); \
235}
236
237#define sas_phy_linkspeed_attr(field) \
238 sas_phy_show_linkspeed(field) \
239static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
240
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200241#define sas_phy_show_linkerror(field) \
242static ssize_t \
243show_sas_phy_##field(struct class_device *cdev, char *buf) \
244{ \
245 struct sas_phy *phy = transport_class_to_phy(cdev); \
246 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
247 struct sas_internal *i = to_sas_internal(shost->transportt); \
248 int error; \
249 \
Christoph Hellwigac01bbb2005-10-19 20:01:17 +0200250 if (!phy->local_attached) \
251 return -EINVAL; \
252 \
James Bottomleydd9fbb522006-03-02 16:01:31 -0600253 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200254 if (error) \
255 return error; \
256 return snprintf(buf, 20, "%u\n", phy->field); \
257}
258
259#define sas_phy_linkerror_attr(field) \
260 sas_phy_show_linkerror(field) \
261static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
262
263
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200264static ssize_t
265show_sas_device_type(struct class_device *cdev, char *buf)
266{
267 struct sas_phy *phy = transport_class_to_phy(cdev);
268
269 if (!phy->identify.device_type)
270 return snprintf(buf, 20, "none\n");
271 return get_sas_device_type_names(phy->identify.device_type, buf);
272}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200273static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
274
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200275static ssize_t do_sas_phy_reset(struct class_device *cdev,
276 size_t count, int hard_reset)
277{
278 struct sas_phy *phy = transport_class_to_phy(cdev);
279 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
280 struct sas_internal *i = to_sas_internal(shost->transportt);
281 int error;
282
283 if (!phy->local_attached)
284 return -EINVAL;
285
286 error = i->f->phy_reset(phy, hard_reset);
287 if (error)
288 return error;
289 return count;
290};
291
292static ssize_t store_sas_link_reset(struct class_device *cdev,
293 const char *buf, size_t count)
294{
295 return do_sas_phy_reset(cdev, count, 0);
296}
297static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
298
299static ssize_t store_sas_hard_reset(struct class_device *cdev,
300 const char *buf, size_t count)
301{
302 return do_sas_phy_reset(cdev, count, 1);
303}
304static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
305
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200306sas_phy_protocol_attr(identify.initiator_port_protocols,
307 initiator_port_protocols);
308sas_phy_protocol_attr(identify.target_port_protocols,
309 target_port_protocols);
310sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
311 unsigned long long);
312sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
313sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
314sas_phy_linkspeed_attr(negotiated_linkrate);
315sas_phy_linkspeed_attr(minimum_linkrate_hw);
316sas_phy_linkspeed_attr(minimum_linkrate);
317sas_phy_linkspeed_attr(maximum_linkrate_hw);
318sas_phy_linkspeed_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200319sas_phy_linkerror_attr(invalid_dword_count);
320sas_phy_linkerror_attr(running_disparity_error_count);
321sas_phy_linkerror_attr(loss_of_dword_sync_count);
322sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200323
324
325static DECLARE_TRANSPORT_CLASS(sas_phy_class,
326 "sas_phy", NULL, NULL, NULL);
327
328static int sas_phy_match(struct attribute_container *cont, struct device *dev)
329{
330 struct Scsi_Host *shost;
331 struct sas_internal *i;
332
333 if (!scsi_is_sas_phy(dev))
334 return 0;
335 shost = dev_to_shost(dev->parent);
336
337 if (!shost->transportt)
338 return 0;
339 if (shost->transportt->host_attrs.ac.class !=
340 &sas_host_class.class)
341 return 0;
342
343 i = to_sas_internal(shost->transportt);
344 return &i->phy_attr_cont.ac == cont;
345}
346
347static void sas_phy_release(struct device *dev)
348{
349 struct sas_phy *phy = dev_to_phy(dev);
350
351 put_device(dev->parent);
352 kfree(phy);
353}
354
355/**
356 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
357 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700358 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200359 *
360 * Allocates an SAS PHY structure. It will be added in the device tree
361 * below the device specified by @parent, which has to be either a Scsi_Host
362 * or sas_rphy.
363 *
364 * Returns:
365 * SAS PHY allocated or %NULL if the allocation failed.
366 */
367struct sas_phy *sas_phy_alloc(struct device *parent, int number)
368{
369 struct Scsi_Host *shost = dev_to_shost(parent);
370 struct sas_phy *phy;
371
Jes Sorensen24669f752006-01-16 10:31:18 -0500372 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200373 if (!phy)
374 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200375
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200376 phy->number = number;
377
378 device_initialize(&phy->dev);
379 phy->dev.parent = get_device(parent);
380 phy->dev.release = sas_phy_release;
James Bottomley79cb1812006-03-13 13:50:04 -0600381 if (scsi_is_sas_expander_device(parent)) {
382 struct sas_rphy *rphy = dev_to_rphy(parent);
383 sprintf(phy->dev.bus_id, "phy-%d-%d:%d", shost->host_no,
384 rphy->scsi_target_id, number);
385 } else
386 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200387
388 transport_setup_device(&phy->dev);
389
390 return phy;
391}
392EXPORT_SYMBOL(sas_phy_alloc);
393
394/**
395 * sas_phy_add -- add a SAS PHY to the device hierachy
396 * @phy: The PHY to be added
397 *
398 * Publishes a SAS PHY to the rest of the system.
399 */
400int sas_phy_add(struct sas_phy *phy)
401{
402 int error;
403
404 error = device_add(&phy->dev);
405 if (!error) {
406 transport_add_device(&phy->dev);
407 transport_configure_device(&phy->dev);
408 }
409
410 return error;
411}
412EXPORT_SYMBOL(sas_phy_add);
413
414/**
415 * sas_phy_free -- free a SAS PHY
416 * @phy: SAS PHY to free
417 *
418 * Frees the specified SAS PHY.
419 *
420 * Note:
421 * This function must only be called on a PHY that has not
422 * sucessfully been added using sas_phy_add().
423 */
424void sas_phy_free(struct sas_phy *phy)
425{
426 transport_destroy_device(&phy->dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800427 put_device(&phy->dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200428}
429EXPORT_SYMBOL(sas_phy_free);
430
431/**
432 * sas_phy_delete -- remove SAS PHY
433 * @phy: SAS PHY to remove
434 *
435 * Removes the specified SAS PHY. If the SAS PHY has an
436 * associated remote PHY it is removed before.
437 */
438void
439sas_phy_delete(struct sas_phy *phy)
440{
441 struct device *dev = &phy->dev;
442
443 if (phy->rphy)
444 sas_rphy_delete(phy->rphy);
445
446 transport_remove_device(dev);
447 device_del(dev);
448 transport_destroy_device(dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800449 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200450}
451EXPORT_SYMBOL(sas_phy_delete);
452
453/**
454 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
455 * @dev: device to check
456 *
457 * Returns:
458 * %1 if the device represents a SAS PHY, %0 else
459 */
460int scsi_is_sas_phy(const struct device *dev)
461{
462 return dev->release == sas_phy_release;
463}
464EXPORT_SYMBOL(scsi_is_sas_phy);
465
466/*
467 * SAS remote PHY attributes.
468 */
469
470#define sas_rphy_show_simple(field, name, format_string, cast) \
471static ssize_t \
472show_sas_rphy_##name(struct class_device *cdev, char *buf) \
473{ \
474 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
475 \
476 return snprintf(buf, 20, format_string, cast rphy->field); \
477}
478
479#define sas_rphy_simple_attr(field, name, format_string, type) \
480 sas_rphy_show_simple(field, name, format_string, (type)) \
481static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
482 show_sas_rphy_##name, NULL)
483
484#define sas_rphy_show_protocol(field, name) \
485static ssize_t \
486show_sas_rphy_##name(struct class_device *cdev, char *buf) \
487{ \
488 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
489 \
490 if (!rphy->field) \
491 return snprintf(buf, 20, "none\n"); \
492 return get_sas_protocol_names(rphy->field, buf); \
493}
494
495#define sas_rphy_protocol_attr(field, name) \
496 sas_rphy_show_protocol(field, name) \
497static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
498 show_sas_rphy_##name, NULL)
499
500static ssize_t
501show_sas_rphy_device_type(struct class_device *cdev, char *buf)
502{
503 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
504
505 if (!rphy->identify.device_type)
506 return snprintf(buf, 20, "none\n");
507 return get_sas_device_type_names(
508 rphy->identify.device_type, buf);
509}
510
511static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
512 show_sas_rphy_device_type, NULL);
513
Christoph Hellwiga0125642006-02-16 13:31:47 +0100514static ssize_t
515show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
516{
517 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
518 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
519 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
520 struct sas_internal *i = to_sas_internal(shost->transportt);
521 u64 identifier;
522 int error;
523
524 /*
525 * Only devices behind an expander are supported, because the
526 * enclosure identifier is a SMP feature.
527 */
528 if (phy->local_attached)
529 return -EINVAL;
530
531 error = i->f->get_enclosure_identifier(rphy, &identifier);
532 if (error)
533 return error;
534 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
535}
536
537static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
538 show_sas_rphy_enclosure_identifier, NULL);
539
540static ssize_t
541show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
542{
543 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
544 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
545 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
546 struct sas_internal *i = to_sas_internal(shost->transportt);
547 int val;
548
549 if (phy->local_attached)
550 return -EINVAL;
551
552 val = i->f->get_bay_identifier(rphy);
553 if (val < 0)
554 return val;
555 return sprintf(buf, "%d\n", val);
556}
557
558static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
559 show_sas_rphy_bay_identifier, NULL);
560
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200561sas_rphy_protocol_attr(identify.initiator_port_protocols,
562 initiator_port_protocols);
563sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
564sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
565 unsigned long long);
566sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
567
James Bottomley42ab0362006-03-04 09:10:18 -0600568/* only need 8 bytes of data plus header (4 or 8) */
569#define BUF_SIZE 64
570
571int sas_read_port_mode_page(struct scsi_device *sdev)
572{
573 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
574 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
575 struct sas_end_device *rdev;
576 struct scsi_mode_data mode_data;
577 int res, error;
578
579 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
580
581 rdev = rphy_to_end_device(rphy);
582
583 if (!buffer)
584 return -ENOMEM;
585
586 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
587 &mode_data, NULL);
588
589 error = -EINVAL;
590 if (!scsi_status_is_good(res))
591 goto out;
592
593 msdata = buffer + mode_data.header_length +
594 mode_data.block_descriptor_length;
595
596 if (msdata - buffer > BUF_SIZE - 8)
597 goto out;
598
599 error = 0;
600
601 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
602 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
603 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
604
605 out:
606 kfree(buffer);
607 return error;
608}
609EXPORT_SYMBOL(sas_read_port_mode_page);
610
James Bottomley79cb1812006-03-13 13:50:04 -0600611static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
612 "sas_end_device", NULL, NULL, NULL);
613
James Bottomley42ab0362006-03-04 09:10:18 -0600614#define sas_end_dev_show_simple(field, name, format_string, cast) \
615static ssize_t \
616show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
617{ \
618 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
619 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
620 \
621 return snprintf(buf, 20, format_string, cast rdev->field); \
622}
623
624#define sas_end_dev_simple_attr(field, name, format_string, type) \
625 sas_end_dev_show_simple(field, name, format_string, (type)) \
626static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
627 show_sas_end_dev_##name, NULL)
628
629sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
630sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
631 "%d\n", int);
632sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
633 "%d\n", int);
634
James Bottomley79cb1812006-03-13 13:50:04 -0600635static DECLARE_TRANSPORT_CLASS(sas_expander_class,
636 "sas_expander", NULL, NULL, NULL);
637
638#define sas_expander_show_simple(field, name, format_string, cast) \
639static ssize_t \
640show_sas_expander_##name(struct class_device *cdev, char *buf) \
641{ \
642 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
643 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
644 \
645 return snprintf(buf, 20, format_string, cast edev->field); \
646}
647
648#define sas_expander_simple_attr(field, name, format_string, type) \
649 sas_expander_show_simple(field, name, format_string, (type)) \
650static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
651 show_sas_expander_##name, NULL)
652
653sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
654sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
655sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
656sas_expander_simple_attr(component_vendor_id, component_vendor_id,
657 "%s\n", char *);
658sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
659sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
660 unsigned int);
661sas_expander_simple_attr(level, level, "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -0600662
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200663static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
James Bottomley2f8600d2006-03-18 15:00:50 -0600664 "sas_device", NULL, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200665
666static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
667{
668 struct Scsi_Host *shost;
669 struct sas_internal *i;
670
671 if (!scsi_is_sas_rphy(dev))
672 return 0;
673 shost = dev_to_shost(dev->parent->parent);
674
675 if (!shost->transportt)
676 return 0;
677 if (shost->transportt->host_attrs.ac.class !=
678 &sas_host_class.class)
679 return 0;
680
681 i = to_sas_internal(shost->transportt);
682 return &i->rphy_attr_cont.ac == cont;
683}
684
James Bottomley42ab0362006-03-04 09:10:18 -0600685static int sas_end_dev_match(struct attribute_container *cont,
686 struct device *dev)
687{
688 struct Scsi_Host *shost;
689 struct sas_internal *i;
690 struct sas_rphy *rphy;
691
692 if (!scsi_is_sas_rphy(dev))
693 return 0;
694 shost = dev_to_shost(dev->parent->parent);
695 rphy = dev_to_rphy(dev);
696
697 if (!shost->transportt)
698 return 0;
699 if (shost->transportt->host_attrs.ac.class !=
700 &sas_host_class.class)
701 return 0;
702
703 i = to_sas_internal(shost->transportt);
704 return &i->end_dev_attr_cont.ac == cont &&
James Bottomley2f8600d2006-03-18 15:00:50 -0600705 rphy->identify.device_type == SAS_END_DEVICE;
James Bottomley42ab0362006-03-04 09:10:18 -0600706}
707
James Bottomley79cb1812006-03-13 13:50:04 -0600708static int sas_expander_match(struct attribute_container *cont,
709 struct device *dev)
710{
711 struct Scsi_Host *shost;
712 struct sas_internal *i;
713 struct sas_rphy *rphy;
714
715 if (!scsi_is_sas_rphy(dev))
716 return 0;
717 shost = dev_to_shost(dev->parent->parent);
718 rphy = dev_to_rphy(dev);
719
720 if (!shost->transportt)
721 return 0;
722 if (shost->transportt->host_attrs.ac.class !=
723 &sas_host_class.class)
724 return 0;
725
726 i = to_sas_internal(shost->transportt);
727 return &i->expander_attr_cont.ac == cont &&
728 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
James Bottomley2f8600d2006-03-18 15:00:50 -0600729 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
James Bottomley79cb1812006-03-13 13:50:04 -0600730}
731
James Bottomley2f8600d2006-03-18 15:00:50 -0600732static void sas_expander_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200733{
734 struct sas_rphy *rphy = dev_to_rphy(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -0600735 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200736
737 put_device(dev->parent);
James Bottomley2f8600d2006-03-18 15:00:50 -0600738 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200739}
740
James Bottomley2f8600d2006-03-18 15:00:50 -0600741static void sas_end_device_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200742{
James Bottomley2f8600d2006-03-18 15:00:50 -0600743 struct sas_rphy *rphy = dev_to_rphy(dev);
744 struct sas_end_device *edev = rphy_to_end_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200745
James Bottomley2f8600d2006-03-18 15:00:50 -0600746 put_device(dev->parent);
747 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200748}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200749
750/**
James Bottomleyc5943d32006-06-12 09:09:18 -0500751 * sas_rphy_initialize - common rphy intialization
752 * @rphy: rphy to initialise
753 *
754 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
755 * initialise the common rphy component of each.
756 */
757static void sas_rphy_initialize(struct sas_rphy *rphy)
758{
759 INIT_LIST_HEAD(&rphy->list);
760}
761
762/**
James Bottomley42ab0362006-03-04 09:10:18 -0600763 * sas_end_device_alloc - allocate an rphy for an end device
764 *
765 * Allocates an SAS remote PHY structure, connected to @parent.
766 *
767 * Returns:
768 * SAS PHY allocated or %NULL if the allocation failed.
769 */
770struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
771{
772 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
773 struct sas_end_device *rdev;
774
775 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
776 if (!rdev) {
James Bottomley42ab0362006-03-04 09:10:18 -0600777 return NULL;
778 }
779
780 device_initialize(&rdev->rphy.dev);
781 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -0600782 rdev->rphy.dev.release = sas_end_device_release;
783 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d-%d",
James Bottomley42ab0362006-03-04 09:10:18 -0600784 shost->host_no, parent->port_identifier, parent->number);
785 rdev->rphy.identify.device_type = SAS_END_DEVICE;
James Bottomleyc5943d32006-06-12 09:09:18 -0500786 sas_rphy_initialize(&rdev->rphy);
James Bottomley42ab0362006-03-04 09:10:18 -0600787 transport_setup_device(&rdev->rphy.dev);
788
789 return &rdev->rphy;
790}
791EXPORT_SYMBOL(sas_end_device_alloc);
792
James Bottomley79cb1812006-03-13 13:50:04 -0600793/**
794 * sas_expander_alloc - allocate an rphy for an end device
795 *
796 * Allocates an SAS remote PHY structure, connected to @parent.
797 *
798 * Returns:
799 * SAS PHY allocated or %NULL if the allocation failed.
800 */
801struct sas_rphy *sas_expander_alloc(struct sas_phy *parent,
802 enum sas_device_type type)
803{
804 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
805 struct sas_expander_device *rdev;
806 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
807
808 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
809 type != SAS_FANOUT_EXPANDER_DEVICE);
810
811 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
812 if (!rdev) {
James Bottomley79cb1812006-03-13 13:50:04 -0600813 return NULL;
814 }
815
816 device_initialize(&rdev->rphy.dev);
817 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -0600818 rdev->rphy.dev.release = sas_expander_release;
James Bottomley79cb1812006-03-13 13:50:04 -0600819 mutex_lock(&sas_host->lock);
820 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
821 mutex_unlock(&sas_host->lock);
822 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
823 shost->host_no, rdev->rphy.scsi_target_id);
824 rdev->rphy.identify.device_type = type;
James Bottomleyc5943d32006-06-12 09:09:18 -0500825 sas_rphy_initialize(&rdev->rphy);
James Bottomley79cb1812006-03-13 13:50:04 -0600826 transport_setup_device(&rdev->rphy.dev);
827
828 return &rdev->rphy;
829}
830EXPORT_SYMBOL(sas_expander_alloc);
James Bottomley42ab0362006-03-04 09:10:18 -0600831
832/**
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200833 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
834 * @rphy: The remote PHY to be added
835 *
836 * Publishes a SAS remote PHY to the rest of the system.
837 */
838int sas_rphy_add(struct sas_rphy *rphy)
839{
840 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
841 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
842 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
843 struct sas_identify *identify = &rphy->identify;
844 int error;
845
846 if (parent->rphy)
847 return -ENXIO;
848 parent->rphy = rphy;
849
850 error = device_add(&rphy->dev);
851 if (error)
852 return error;
853 transport_add_device(&rphy->dev);
854 transport_configure_device(&rphy->dev);
855
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100856 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200857 list_add_tail(&rphy->list, &sas_host->rphy_list);
858 if (identify->device_type == SAS_END_DEVICE &&
859 (identify->target_port_protocols &
860 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
861 rphy->scsi_target_id = sas_host->next_target_id++;
James Bottomley7676f832006-04-14 09:47:59 -0500862 else if (identify->device_type == SAS_END_DEVICE)
863 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100864 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200865
James Bottomley79cb1812006-03-13 13:50:04 -0600866 if (identify->device_type == SAS_END_DEVICE &&
867 rphy->scsi_target_id != -1) {
Moore, Erice6bc8632006-01-13 16:33:59 -0700868 scsi_scan_target(&rphy->dev, parent->port_identifier,
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200869 rphy->scsi_target_id, ~0, 0);
870 }
871
872 return 0;
873}
874EXPORT_SYMBOL(sas_rphy_add);
875
876/**
877 * sas_rphy_free -- free a SAS remote PHY
878 * @rphy SAS remote PHY to free
879 *
880 * Frees the specified SAS remote PHY.
881 *
882 * Note:
883 * This function must only be called on a remote
884 * PHY that has not sucessfully been added using
885 * sas_rphy_add().
886 */
887void sas_rphy_free(struct sas_rphy *rphy)
888{
Mike Anderson92aab642006-03-27 09:37:28 -0800889 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200890 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
891 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
892
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100893 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200894 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100895 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200896
Mike Anderson92aab642006-03-27 09:37:28 -0800897 transport_destroy_device(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -0600898
Mike Anderson92aab642006-03-27 09:37:28 -0800899 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200900}
901EXPORT_SYMBOL(sas_rphy_free);
902
903/**
904 * sas_rphy_delete -- remove SAS remote PHY
905 * @rphy: SAS remote PHY to remove
906 *
907 * Removes the specified SAS remote PHY.
908 */
909void
910sas_rphy_delete(struct sas_rphy *rphy)
911{
912 struct device *dev = &rphy->dev;
913 struct sas_phy *parent = dev_to_phy(dev->parent);
914 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
915 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
916
Christoph Hellwigd4054232006-01-04 13:45:20 +0100917 switch (rphy->identify.device_type) {
918 case SAS_END_DEVICE:
919 scsi_remove_target(dev);
920 break;
921 case SAS_EDGE_EXPANDER_DEVICE:
922 case SAS_FANOUT_EXPANDER_DEVICE:
923 device_for_each_child(dev, NULL, do_sas_phy_delete);
924 break;
925 default:
926 break;
927 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200928
Christoph Hellwigfe8b2302005-09-25 23:10:33 +0200929 transport_remove_device(dev);
930 device_del(dev);
931 transport_destroy_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200932
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100933 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200934 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100935 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200936
Christoph Hellwig33b114e2006-01-11 14:20:43 +0100937 parent->rphy = NULL;
938
Mike Anderson92aab642006-03-27 09:37:28 -0800939 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200940}
941EXPORT_SYMBOL(sas_rphy_delete);
942
943/**
944 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
945 * @dev: device to check
946 *
947 * Returns:
948 * %1 if the device represents a SAS remote PHY, %0 else
949 */
950int scsi_is_sas_rphy(const struct device *dev)
951{
James Bottomley2f8600d2006-03-18 15:00:50 -0600952 return dev->release == sas_end_device_release ||
953 dev->release == sas_expander_release;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200954}
955EXPORT_SYMBOL(scsi_is_sas_rphy);
956
957
958/*
959 * SCSI scan helper
960 */
961
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100962static int sas_user_scan(struct Scsi_Host *shost, uint channel,
963 uint id, uint lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200964{
965 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
966 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200967
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100968 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200969 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
970 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200971
James Bottomley6d99a3f2006-05-19 10:49:37 -0500972 if (rphy->identify.device_type != SAS_END_DEVICE ||
973 rphy->scsi_target_id == -1)
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100974 continue;
975
Moore, Erice6bc8632006-01-13 16:33:59 -0700976 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100977 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
Moore, Erice6bc8632006-01-13 16:33:59 -0700978 scsi_scan_target(&rphy->dev, parent->port_identifier,
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100979 rphy->scsi_target_id, lun, 1);
980 }
981 }
982 mutex_unlock(&sas_host->lock);
983
984 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200985}
986
987
988/*
989 * Setup / Teardown code
990 */
991
James Bottomley42ab0362006-03-04 09:10:18 -0600992#define SETUP_TEMPLATE(attrb, field, perm, test) \
993 i->private_##attrb[count] = class_device_attr_##field; \
994 i->private_##attrb[count].attr.mode = perm; \
James Bottomley42ab0362006-03-04 09:10:18 -0600995 i->attrb[count] = &i->private_##attrb[count]; \
996 if (test) \
997 count++
998
999
1000#define SETUP_RPORT_ATTRIBUTE(field) \
1001 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001002
James Bottomleydd9fbb522006-03-02 16:01:31 -06001003#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001004 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001005
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001006#define SETUP_PORT_ATTRIBUTE(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001007 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001008
James Bottomleydd9fbb522006-03-02 16:01:31 -06001009#define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001010 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001011
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001012#define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001013 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001014
James Bottomleydd9fbb522006-03-02 16:01:31 -06001015#define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001016 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001017
James Bottomley42ab0362006-03-04 09:10:18 -06001018#define SETUP_END_DEV_ATTRIBUTE(field) \
1019 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001020
James Bottomley79cb1812006-03-13 13:50:04 -06001021#define SETUP_EXPANDER_ATTRIBUTE(field) \
1022 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1023
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001024/**
1025 * sas_attach_transport -- instantiate SAS transport template
1026 * @ft: SAS transport class function template
1027 */
1028struct scsi_transport_template *
1029sas_attach_transport(struct sas_function_template *ft)
1030{
1031 struct sas_internal *i;
1032 int count;
1033
Jes Sorensen24669f752006-01-16 10:31:18 -05001034 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001035 if (!i)
1036 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001037
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001038 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001039
1040 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1041 i->t.host_attrs.ac.class = &sas_host_class.class;
1042 i->t.host_attrs.ac.match = sas_host_match;
1043 transport_container_register(&i->t.host_attrs);
1044 i->t.host_size = sizeof(struct sas_host_attrs);
1045
1046 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1047 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1048 i->phy_attr_cont.ac.match = sas_phy_match;
1049 transport_container_register(&i->phy_attr_cont);
1050
1051 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1052 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1053 i->rphy_attr_cont.ac.match = sas_rphy_match;
1054 transport_container_register(&i->rphy_attr_cont);
1055
James Bottomley42ab0362006-03-04 09:10:18 -06001056 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1057 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1058 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1059 transport_container_register(&i->end_dev_attr_cont);
1060
James Bottomley79cb1812006-03-13 13:50:04 -06001061 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1062 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1063 i->expander_attr_cont.ac.match = sas_expander_match;
1064 transport_container_register(&i->expander_attr_cont);
1065
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001066 i->f = ft;
1067
1068 count = 0;
1069 i->host_attrs[count] = NULL;
1070
1071 count = 0;
1072 SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
1073 SETUP_PORT_ATTRIBUTE(target_port_protocols);
1074 SETUP_PORT_ATTRIBUTE(device_type);
1075 SETUP_PORT_ATTRIBUTE(sas_address);
1076 SETUP_PORT_ATTRIBUTE(phy_identifier);
1077 SETUP_PORT_ATTRIBUTE(port_identifier);
1078 SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
1079 SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
1080 SETUP_PORT_ATTRIBUTE(minimum_linkrate);
1081 SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
1082 SETUP_PORT_ATTRIBUTE(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +02001083
1084 SETUP_PORT_ATTRIBUTE(invalid_dword_count);
1085 SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
1086 SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
1087 SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001088 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1089 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001090 i->phy_attrs[count] = NULL;
1091
1092 count = 0;
1093 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1094 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1095 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1096 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1097 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001098 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1099 get_enclosure_identifier);
1100 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1101 get_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001102 i->rphy_attrs[count] = NULL;
1103
James Bottomley42ab0362006-03-04 09:10:18 -06001104 count = 0;
1105 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1106 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1107 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1108 i->end_dev_attrs[count] = NULL;
1109
James Bottomley79cb1812006-03-13 13:50:04 -06001110 count = 0;
1111 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1112 SETUP_EXPANDER_ATTRIBUTE(product_id);
1113 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1114 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1115 SETUP_EXPANDER_ATTRIBUTE(component_id);
1116 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1117 SETUP_EXPANDER_ATTRIBUTE(level);
1118 i->expander_attrs[count] = NULL;
1119
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001120 return &i->t;
1121}
1122EXPORT_SYMBOL(sas_attach_transport);
1123
1124/**
1125 * sas_release_transport -- release SAS transport template instance
1126 * @t: transport template instance
1127 */
1128void sas_release_transport(struct scsi_transport_template *t)
1129{
1130 struct sas_internal *i = to_sas_internal(t);
1131
1132 transport_container_unregister(&i->t.host_attrs);
1133 transport_container_unregister(&i->phy_attr_cont);
1134 transport_container_unregister(&i->rphy_attr_cont);
James Bottomleydb82f842006-03-09 22:06:36 -05001135 transport_container_unregister(&i->end_dev_attr_cont);
James Bottomley79cb1812006-03-13 13:50:04 -06001136 transport_container_unregister(&i->expander_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001137
1138 kfree(i);
1139}
1140EXPORT_SYMBOL(sas_release_transport);
1141
1142static __init int sas_transport_init(void)
1143{
1144 int error;
1145
1146 error = transport_class_register(&sas_host_class);
1147 if (error)
1148 goto out;
1149 error = transport_class_register(&sas_phy_class);
1150 if (error)
1151 goto out_unregister_transport;
1152 error = transport_class_register(&sas_rphy_class);
1153 if (error)
1154 goto out_unregister_phy;
James Bottomley42ab0362006-03-04 09:10:18 -06001155 error = transport_class_register(&sas_end_dev_class);
1156 if (error)
1157 goto out_unregister_rphy;
James Bottomley79cb1812006-03-13 13:50:04 -06001158 error = transport_class_register(&sas_expander_class);
1159 if (error)
1160 goto out_unregister_end_dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001161
1162 return 0;
1163
James Bottomley79cb1812006-03-13 13:50:04 -06001164 out_unregister_end_dev:
1165 transport_class_unregister(&sas_end_dev_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001166 out_unregister_rphy:
1167 transport_class_unregister(&sas_rphy_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001168 out_unregister_phy:
1169 transport_class_unregister(&sas_phy_class);
1170 out_unregister_transport:
1171 transport_class_unregister(&sas_host_class);
1172 out:
1173 return error;
1174
1175}
1176
1177static void __exit sas_transport_exit(void)
1178{
1179 transport_class_unregister(&sas_host_class);
1180 transport_class_unregister(&sas_phy_class);
1181 transport_class_unregister(&sas_rphy_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001182 transport_class_unregister(&sas_end_dev_class);
James Bottomley79cb1812006-03-13 13:50:04 -06001183 transport_class_unregister(&sas_expander_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001184}
1185
1186MODULE_AUTHOR("Christoph Hellwig");
1187MODULE_DESCRIPTION("SAS Transphy Attributes");
1188MODULE_LICENSE("GPL");
1189
1190module_init(sas_transport_init);
1191module_exit(sas_transport_exit);