blob: b5b0c2cba96b4757c05d5a6261069bb49f577202 [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;
James Bottomleyc9fefeb2006-07-02 11:10:18 -050044 int next_port_id;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020045};
46#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
47
48
49/*
50 * Hack to allow attributes of the same name in different objects.
51 */
52#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
53 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
54 __ATTR(_name,_mode,_show,_store)
55
56
57/*
58 * Pretty printing helpers
59 */
60
61#define sas_bitfield_name_match(title, table) \
62static ssize_t \
63get_sas_##title##_names(u32 table_key, char *buf) \
64{ \
65 char *prefix = ""; \
66 ssize_t len = 0; \
67 int i; \
68 \
Tobias Klauser6391a112006-06-08 22:23:48 -070069 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020070 if (table[i].value & table_key) { \
71 len += sprintf(buf + len, "%s%s", \
72 prefix, table[i].name); \
73 prefix = ", "; \
74 } \
75 } \
76 len += sprintf(buf + len, "\n"); \
77 return len; \
78}
79
James Bottomleyd24e1ee2006-09-06 19:25:22 -050080#define sas_bitfield_name_set(title, table) \
81static ssize_t \
82set_sas_##title##_names(u32 *table_key, const char *buf) \
83{ \
84 ssize_t len = 0; \
85 int i; \
86 \
87 for (i = 0; i < ARRAY_SIZE(table); i++) { \
88 len = strlen(table[i].name); \
89 if (strncmp(buf, table[i].name, len) == 0 && \
90 (buf[len] == '\n' || buf[len] == '\0')) { \
91 *table_key = table[i].value; \
92 return 0; \
93 } \
94 } \
95 return -EINVAL; \
96}
97
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020098#define sas_bitfield_name_search(title, table) \
99static ssize_t \
100get_sas_##title##_names(u32 table_key, char *buf) \
101{ \
102 ssize_t len = 0; \
103 int i; \
104 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700105 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200106 if (table[i].value == table_key) { \
107 len += sprintf(buf + len, "%s", \
108 table[i].name); \
109 break; \
110 } \
111 } \
112 len += sprintf(buf + len, "\n"); \
113 return len; \
114}
115
116static struct {
117 u32 value;
118 char *name;
119} sas_device_type_names[] = {
120 { SAS_PHY_UNUSED, "unused" },
121 { SAS_END_DEVICE, "end device" },
122 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
123 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
124};
125sas_bitfield_name_search(device_type, sas_device_type_names)
126
127
128static struct {
129 u32 value;
130 char *name;
131} sas_protocol_names[] = {
132 { SAS_PROTOCOL_SATA, "sata" },
133 { SAS_PROTOCOL_SMP, "smp" },
134 { SAS_PROTOCOL_STP, "stp" },
135 { SAS_PROTOCOL_SSP, "ssp" },
136};
137sas_bitfield_name_match(protocol, sas_protocol_names)
138
139static struct {
140 u32 value;
141 char *name;
142} sas_linkspeed_names[] = {
143 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
144 { SAS_PHY_DISABLED, "Phy disabled" },
145 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
146 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
147 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
148 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
James Bottomley7e6dff62006-03-02 14:12:56 -0600149 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200150};
151sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500152sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200153
154/*
155 * SAS host attributes
156 */
157
James Bottomley37be6ee2005-09-09 18:38:27 -0500158static int sas_host_setup(struct transport_container *tc, struct device *dev,
159 struct class_device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200160{
161 struct Scsi_Host *shost = dev_to_shost(dev);
162 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
163
164 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100165 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200166 sas_host->next_target_id = 0;
James Bottomley79cb1812006-03-13 13:50:04 -0600167 sas_host->next_expander_id = 0;
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500168 sas_host->next_port_id = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200169 return 0;
170}
171
172static DECLARE_TRANSPORT_CLASS(sas_host_class,
173 "sas_host", sas_host_setup, NULL, NULL);
174
175static int sas_host_match(struct attribute_container *cont,
176 struct device *dev)
177{
178 struct Scsi_Host *shost;
179 struct sas_internal *i;
180
181 if (!scsi_is_host_device(dev))
182 return 0;
183 shost = dev_to_shost(dev);
184
185 if (!shost->transportt)
186 return 0;
187 if (shost->transportt->host_attrs.ac.class !=
188 &sas_host_class.class)
189 return 0;
190
191 i = to_sas_internal(shost->transportt);
192 return &i->t.host_attrs.ac == cont;
193}
194
195static int do_sas_phy_delete(struct device *dev, void *data)
196{
James Bottomley65c92b02006-06-28 12:22:50 -0400197 int pass = (int)(unsigned long)data;
198
199 if (pass == 0 && scsi_is_sas_port(dev))
200 sas_port_delete(dev_to_sas_port(dev));
201 else if (pass == 1 && scsi_is_sas_phy(dev))
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200202 sas_phy_delete(dev_to_phy(dev));
203 return 0;
204}
205
206/**
James Bottomley65c92b02006-06-28 12:22:50 -0400207 * sas_remove_children -- tear down a devices SAS data structures
208 * @dev: device belonging to the sas object
209 *
210 * Removes all SAS PHYs and remote PHYs for a given object
211 */
212void sas_remove_children(struct device *dev)
213{
214 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
215 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
216}
217EXPORT_SYMBOL(sas_remove_children);
218
219/**
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200220 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
221 * @shost: Scsi Host that is torn down
222 *
223 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
224 * Must be called just before scsi_remove_host for SAS HBAs.
225 */
226void sas_remove_host(struct Scsi_Host *shost)
227{
James Bottomley65c92b02006-06-28 12:22:50 -0400228 sas_remove_children(&shost->shost_gendev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200229}
230EXPORT_SYMBOL(sas_remove_host);
231
232
233/*
James Bottomley65c92b02006-06-28 12:22:50 -0400234 * SAS Phy attributes
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200235 */
236
237#define sas_phy_show_simple(field, name, format_string, cast) \
238static ssize_t \
239show_sas_phy_##name(struct class_device *cdev, char *buf) \
240{ \
241 struct sas_phy *phy = transport_class_to_phy(cdev); \
242 \
243 return snprintf(buf, 20, format_string, cast phy->field); \
244}
245
246#define sas_phy_simple_attr(field, name, format_string, type) \
247 sas_phy_show_simple(field, name, format_string, (type)) \
248static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
249
250#define sas_phy_show_protocol(field, name) \
251static ssize_t \
252show_sas_phy_##name(struct class_device *cdev, char *buf) \
253{ \
254 struct sas_phy *phy = transport_class_to_phy(cdev); \
255 \
256 if (!phy->field) \
257 return snprintf(buf, 20, "none\n"); \
258 return get_sas_protocol_names(phy->field, buf); \
259}
260
261#define sas_phy_protocol_attr(field, name) \
262 sas_phy_show_protocol(field, name) \
263static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
264
265#define sas_phy_show_linkspeed(field) \
266static ssize_t \
267show_sas_phy_##field(struct class_device *cdev, char *buf) \
268{ \
269 struct sas_phy *phy = transport_class_to_phy(cdev); \
270 \
271 return get_sas_linkspeed_names(phy->field, buf); \
272}
273
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500274/* Fudge to tell if we're minimum or maximum */
275#define sas_phy_store_linkspeed(field) \
276static ssize_t \
277store_sas_phy_##field(struct class_device *cdev, const char *buf, \
278 size_t count) \
279{ \
280 struct sas_phy *phy = transport_class_to_phy(cdev); \
281 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
282 struct sas_internal *i = to_sas_internal(shost->transportt); \
283 u32 value; \
284 struct sas_phy_linkrates rates = {0}; \
285 int error; \
286 \
287 error = set_sas_linkspeed_names(&value, buf); \
288 if (error) \
289 return error; \
290 rates.field = value; \
291 error = i->f->set_phy_speed(phy, &rates); \
292 \
293 return error ? error : count; \
294}
295
296#define sas_phy_linkspeed_rw_attr(field) \
297 sas_phy_show_linkspeed(field) \
298 sas_phy_store_linkspeed(field) \
299static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
300 store_sas_phy_##field)
301
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200302#define sas_phy_linkspeed_attr(field) \
303 sas_phy_show_linkspeed(field) \
304static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
305
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500306
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200307#define sas_phy_show_linkerror(field) \
308static ssize_t \
309show_sas_phy_##field(struct class_device *cdev, char *buf) \
310{ \
311 struct sas_phy *phy = transport_class_to_phy(cdev); \
312 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
313 struct sas_internal *i = to_sas_internal(shost->transportt); \
314 int error; \
315 \
James Bottomleydd9fbb522006-03-02 16:01:31 -0600316 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200317 if (error) \
318 return error; \
319 return snprintf(buf, 20, "%u\n", phy->field); \
320}
321
322#define sas_phy_linkerror_attr(field) \
323 sas_phy_show_linkerror(field) \
324static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
325
326
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200327static ssize_t
328show_sas_device_type(struct class_device *cdev, char *buf)
329{
330 struct sas_phy *phy = transport_class_to_phy(cdev);
331
332 if (!phy->identify.device_type)
333 return snprintf(buf, 20, "none\n");
334 return get_sas_device_type_names(phy->identify.device_type, buf);
335}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200336static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
337
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200338static ssize_t do_sas_phy_reset(struct class_device *cdev,
339 size_t count, int hard_reset)
340{
341 struct sas_phy *phy = transport_class_to_phy(cdev);
342 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
343 struct sas_internal *i = to_sas_internal(shost->transportt);
344 int error;
345
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200346 error = i->f->phy_reset(phy, hard_reset);
347 if (error)
348 return error;
349 return count;
350};
351
352static ssize_t store_sas_link_reset(struct class_device *cdev,
353 const char *buf, size_t count)
354{
355 return do_sas_phy_reset(cdev, count, 0);
356}
357static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
358
359static ssize_t store_sas_hard_reset(struct class_device *cdev,
360 const char *buf, size_t count)
361{
362 return do_sas_phy_reset(cdev, count, 1);
363}
364static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
365
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200366sas_phy_protocol_attr(identify.initiator_port_protocols,
367 initiator_port_protocols);
368sas_phy_protocol_attr(identify.target_port_protocols,
369 target_port_protocols);
370sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
371 unsigned long long);
372sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500373//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200374sas_phy_linkspeed_attr(negotiated_linkrate);
375sas_phy_linkspeed_attr(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500376sas_phy_linkspeed_rw_attr(minimum_linkrate);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200377sas_phy_linkspeed_attr(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500378sas_phy_linkspeed_rw_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200379sas_phy_linkerror_attr(invalid_dword_count);
380sas_phy_linkerror_attr(running_disparity_error_count);
381sas_phy_linkerror_attr(loss_of_dword_sync_count);
382sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200383
384
385static DECLARE_TRANSPORT_CLASS(sas_phy_class,
386 "sas_phy", NULL, NULL, NULL);
387
388static int sas_phy_match(struct attribute_container *cont, struct device *dev)
389{
390 struct Scsi_Host *shost;
391 struct sas_internal *i;
392
393 if (!scsi_is_sas_phy(dev))
394 return 0;
395 shost = dev_to_shost(dev->parent);
396
397 if (!shost->transportt)
398 return 0;
399 if (shost->transportt->host_attrs.ac.class !=
400 &sas_host_class.class)
401 return 0;
402
403 i = to_sas_internal(shost->transportt);
404 return &i->phy_attr_cont.ac == cont;
405}
406
407static void sas_phy_release(struct device *dev)
408{
409 struct sas_phy *phy = dev_to_phy(dev);
410
411 put_device(dev->parent);
412 kfree(phy);
413}
414
415/**
416 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
417 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700418 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200419 *
420 * Allocates an SAS PHY structure. It will be added in the device tree
421 * below the device specified by @parent, which has to be either a Scsi_Host
422 * or sas_rphy.
423 *
424 * Returns:
425 * SAS PHY allocated or %NULL if the allocation failed.
426 */
427struct sas_phy *sas_phy_alloc(struct device *parent, int number)
428{
429 struct Scsi_Host *shost = dev_to_shost(parent);
430 struct sas_phy *phy;
431
Jes Sorensen24669f752006-01-16 10:31:18 -0500432 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200433 if (!phy)
434 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200435
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200436 phy->number = number;
437
438 device_initialize(&phy->dev);
439 phy->dev.parent = get_device(parent);
440 phy->dev.release = sas_phy_release;
James Bottomley65c92b02006-06-28 12:22:50 -0400441 INIT_LIST_HEAD(&phy->port_siblings);
James Bottomley79cb1812006-03-13 13:50:04 -0600442 if (scsi_is_sas_expander_device(parent)) {
443 struct sas_rphy *rphy = dev_to_rphy(parent);
James Bottomley65c92b02006-06-28 12:22:50 -0400444 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
James Bottomley79cb1812006-03-13 13:50:04 -0600445 rphy->scsi_target_id, number);
446 } else
447 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200448
449 transport_setup_device(&phy->dev);
450
451 return phy;
452}
453EXPORT_SYMBOL(sas_phy_alloc);
454
455/**
456 * sas_phy_add -- add a SAS PHY to the device hierachy
457 * @phy: The PHY to be added
458 *
459 * Publishes a SAS PHY to the rest of the system.
460 */
461int sas_phy_add(struct sas_phy *phy)
462{
463 int error;
464
465 error = device_add(&phy->dev);
466 if (!error) {
467 transport_add_device(&phy->dev);
468 transport_configure_device(&phy->dev);
469 }
470
471 return error;
472}
473EXPORT_SYMBOL(sas_phy_add);
474
475/**
476 * sas_phy_free -- free a SAS PHY
477 * @phy: SAS PHY to free
478 *
479 * Frees the specified SAS PHY.
480 *
481 * Note:
482 * This function must only be called on a PHY that has not
483 * sucessfully been added using sas_phy_add().
484 */
485void sas_phy_free(struct sas_phy *phy)
486{
487 transport_destroy_device(&phy->dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800488 put_device(&phy->dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200489}
490EXPORT_SYMBOL(sas_phy_free);
491
492/**
493 * sas_phy_delete -- remove SAS PHY
494 * @phy: SAS PHY to remove
495 *
496 * Removes the specified SAS PHY. If the SAS PHY has an
497 * associated remote PHY it is removed before.
498 */
499void
500sas_phy_delete(struct sas_phy *phy)
501{
502 struct device *dev = &phy->dev;
503
James Bottomley65c92b02006-06-28 12:22:50 -0400504 /* this happens if the phy is still part of a port when deleted */
505 BUG_ON(!list_empty(&phy->port_siblings));
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200506
507 transport_remove_device(dev);
508 device_del(dev);
509 transport_destroy_device(dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800510 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200511}
512EXPORT_SYMBOL(sas_phy_delete);
513
514/**
515 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
516 * @dev: device to check
517 *
518 * Returns:
519 * %1 if the device represents a SAS PHY, %0 else
520 */
521int scsi_is_sas_phy(const struct device *dev)
522{
523 return dev->release == sas_phy_release;
524}
525EXPORT_SYMBOL(scsi_is_sas_phy);
526
527/*
James Bottomley65c92b02006-06-28 12:22:50 -0400528 * SAS Port attributes
529 */
530#define sas_port_show_simple(field, name, format_string, cast) \
531static ssize_t \
532show_sas_port_##name(struct class_device *cdev, char *buf) \
533{ \
534 struct sas_port *port = transport_class_to_sas_port(cdev); \
535 \
536 return snprintf(buf, 20, format_string, cast port->field); \
537}
538
539#define sas_port_simple_attr(field, name, format_string, type) \
540 sas_port_show_simple(field, name, format_string, (type)) \
541static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
542
543sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
544
545static DECLARE_TRANSPORT_CLASS(sas_port_class,
546 "sas_port", NULL, NULL, NULL);
547
548static int sas_port_match(struct attribute_container *cont, struct device *dev)
549{
550 struct Scsi_Host *shost;
551 struct sas_internal *i;
552
553 if (!scsi_is_sas_port(dev))
554 return 0;
555 shost = dev_to_shost(dev->parent);
556
557 if (!shost->transportt)
558 return 0;
559 if (shost->transportt->host_attrs.ac.class !=
560 &sas_host_class.class)
561 return 0;
562
563 i = to_sas_internal(shost->transportt);
564 return &i->port_attr_cont.ac == cont;
565}
566
567
568static void sas_port_release(struct device *dev)
569{
570 struct sas_port *port = dev_to_sas_port(dev);
571
572 BUG_ON(!list_empty(&port->phy_list));
573
574 put_device(dev->parent);
575 kfree(port);
576}
577
578static void sas_port_create_link(struct sas_port *port,
579 struct sas_phy *phy)
580{
581 sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
582 sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
583}
584
585static void sas_port_delete_link(struct sas_port *port,
586 struct sas_phy *phy)
587{
588 sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
589 sysfs_remove_link(&phy->dev.kobj, "port");
590}
591
592/** sas_port_alloc - allocate and initialize a SAS port structure
593 *
594 * @parent: parent device
595 * @port_id: port number
596 *
597 * Allocates a SAS port structure. It will be added to the device tree
598 * below the device specified by @parent which must be either a Scsi_Host
599 * or a sas_expander_device.
600 *
601 * Returns %NULL on error
602 */
603struct sas_port *sas_port_alloc(struct device *parent, int port_id)
604{
605 struct Scsi_Host *shost = dev_to_shost(parent);
606 struct sas_port *port;
607
608 port = kzalloc(sizeof(*port), GFP_KERNEL);
609 if (!port)
610 return NULL;
611
612 port->port_identifier = port_id;
613
614 device_initialize(&port->dev);
615
616 port->dev.parent = get_device(parent);
617 port->dev.release = sas_port_release;
618
619 mutex_init(&port->phy_list_mutex);
620 INIT_LIST_HEAD(&port->phy_list);
621
622 if (scsi_is_sas_expander_device(parent)) {
623 struct sas_rphy *rphy = dev_to_rphy(parent);
624 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
625 rphy->scsi_target_id, port->port_identifier);
626 } else
627 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
628 port->port_identifier);
629
630 transport_setup_device(&port->dev);
631
632 return port;
633}
634EXPORT_SYMBOL(sas_port_alloc);
635
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500636/** sas_port_alloc_num - allocate and initialize a SAS port structure
637 *
638 * @parent: parent device
639 *
640 * Allocates a SAS port structure and a number to go with it. This
641 * interface is really for adapters where the port number has no
642 * meansing, so the sas class should manage them. It will be added to
643 * the device tree below the device specified by @parent which must be
644 * either a Scsi_Host or a sas_expander_device.
645 *
646 * Returns %NULL on error
647 */
648struct sas_port *sas_port_alloc_num(struct device *parent)
649{
650 int index;
651 struct Scsi_Host *shost = dev_to_shost(parent);
652 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
653
654 /* FIXME: use idr for this eventually */
655 mutex_lock(&sas_host->lock);
656 if (scsi_is_sas_expander_device(parent)) {
657 struct sas_rphy *rphy = dev_to_rphy(parent);
658 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
659
660 index = exp->next_port_id++;
661 } else
662 index = sas_host->next_port_id++;
663 mutex_unlock(&sas_host->lock);
664 return sas_port_alloc(parent, index);
665}
666EXPORT_SYMBOL(sas_port_alloc_num);
667
James Bottomley65c92b02006-06-28 12:22:50 -0400668/**
669 * sas_port_add - add a SAS port to the device hierarchy
670 *
671 * @port: port to be added
672 *
673 * publishes a port to the rest of the system
674 */
675int sas_port_add(struct sas_port *port)
676{
677 int error;
678
679 /* No phys should be added until this is made visible */
680 BUG_ON(!list_empty(&port->phy_list));
681
682 error = device_add(&port->dev);
683
684 if (error)
685 return error;
686
687 transport_add_device(&port->dev);
688 transport_configure_device(&port->dev);
689
690 return 0;
691}
692EXPORT_SYMBOL(sas_port_add);
693
694/**
695 * sas_port_free -- free a SAS PORT
696 * @port: SAS PORT to free
697 *
698 * Frees the specified SAS PORT.
699 *
700 * Note:
701 * This function must only be called on a PORT that has not
702 * sucessfully been added using sas_port_add().
703 */
704void sas_port_free(struct sas_port *port)
705{
706 transport_destroy_device(&port->dev);
707 put_device(&port->dev);
708}
709EXPORT_SYMBOL(sas_port_free);
710
711/**
712 * sas_port_delete -- remove SAS PORT
713 * @port: SAS PORT to remove
714 *
715 * Removes the specified SAS PORT. If the SAS PORT has an
716 * associated phys, unlink them from the port as well.
717 */
718void sas_port_delete(struct sas_port *port)
719{
720 struct device *dev = &port->dev;
721 struct sas_phy *phy, *tmp_phy;
722
723 if (port->rphy) {
724 sas_rphy_delete(port->rphy);
725 port->rphy = NULL;
726 }
727
728 mutex_lock(&port->phy_list_mutex);
729 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
730 port_siblings) {
731 sas_port_delete_link(port, phy);
732 list_del_init(&phy->port_siblings);
733 }
734 mutex_unlock(&port->phy_list_mutex);
735
James Bottomleya0e1b6e2006-07-09 12:38:19 -0500736 if (port->is_backlink) {
737 struct device *parent = port->dev.parent;
738
739 sysfs_remove_link(&port->dev.kobj, parent->bus_id);
740 port->is_backlink = 0;
741 }
742
James Bottomley65c92b02006-06-28 12:22:50 -0400743 transport_remove_device(dev);
744 device_del(dev);
745 transport_destroy_device(dev);
746 put_device(dev);
747}
748EXPORT_SYMBOL(sas_port_delete);
749
750/**
751 * scsi_is_sas_port -- check if a struct device represents a SAS port
752 * @dev: device to check
753 *
754 * Returns:
755 * %1 if the device represents a SAS Port, %0 else
756 */
757int scsi_is_sas_port(const struct device *dev)
758{
759 return dev->release == sas_port_release;
760}
761EXPORT_SYMBOL(scsi_is_sas_port);
762
763/**
764 * sas_port_add_phy - add another phy to a port to form a wide port
765 * @port: port to add the phy to
766 * @phy: phy to add
767 *
768 * When a port is initially created, it is empty (has no phys). All
769 * ports must have at least one phy to operated, and all wide ports
770 * must have at least two. The current code makes no difference
771 * between ports and wide ports, but the only object that can be
772 * connected to a remote device is a port, so ports must be formed on
773 * all devices with phys if they're connected to anything.
774 */
775void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
776{
777 mutex_lock(&port->phy_list_mutex);
778 if (unlikely(!list_empty(&phy->port_siblings))) {
779 /* make sure we're already on this port */
780 struct sas_phy *tmp;
781
782 list_for_each_entry(tmp, &port->phy_list, port_siblings)
783 if (tmp == phy)
784 break;
785 /* If this trips, you added a phy that was already
786 * part of a different port */
787 if (unlikely(tmp != phy)) {
788 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
789 BUG();
790 }
791 } else {
792 sas_port_create_link(port, phy);
793 list_add_tail(&phy->port_siblings, &port->phy_list);
794 port->num_phys++;
795 }
796 mutex_unlock(&port->phy_list_mutex);
797}
798EXPORT_SYMBOL(sas_port_add_phy);
799
800/**
801 * sas_port_delete_phy - remove a phy from a port or wide port
802 * @port: port to remove the phy from
803 * @phy: phy to remove
804 *
805 * This operation is used for tearing down ports again. It must be
806 * done to every port or wide port before calling sas_port_delete.
807 */
808void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
809{
810 mutex_lock(&port->phy_list_mutex);
811 sas_port_delete_link(port, phy);
812 list_del_init(&phy->port_siblings);
813 port->num_phys--;
814 mutex_unlock(&port->phy_list_mutex);
815}
816EXPORT_SYMBOL(sas_port_delete_phy);
817
James Bottomleya0e1b6e2006-07-09 12:38:19 -0500818void sas_port_mark_backlink(struct sas_port *port)
819{
820 struct device *parent = port->dev.parent->parent->parent;
821
822 if (port->is_backlink)
823 return;
824 port->is_backlink = 1;
825 sysfs_create_link(&port->dev.kobj, &parent->kobj,
826 parent->bus_id);
827
828}
829EXPORT_SYMBOL(sas_port_mark_backlink);
830
James Bottomley65c92b02006-06-28 12:22:50 -0400831/*
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200832 * SAS remote PHY attributes.
833 */
834
835#define sas_rphy_show_simple(field, name, format_string, cast) \
836static ssize_t \
837show_sas_rphy_##name(struct class_device *cdev, char *buf) \
838{ \
839 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
840 \
841 return snprintf(buf, 20, format_string, cast rphy->field); \
842}
843
844#define sas_rphy_simple_attr(field, name, format_string, type) \
845 sas_rphy_show_simple(field, name, format_string, (type)) \
846static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
847 show_sas_rphy_##name, NULL)
848
849#define sas_rphy_show_protocol(field, name) \
850static ssize_t \
851show_sas_rphy_##name(struct class_device *cdev, char *buf) \
852{ \
853 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
854 \
855 if (!rphy->field) \
856 return snprintf(buf, 20, "none\n"); \
857 return get_sas_protocol_names(rphy->field, buf); \
858}
859
860#define sas_rphy_protocol_attr(field, name) \
861 sas_rphy_show_protocol(field, name) \
862static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
863 show_sas_rphy_##name, NULL)
864
865static ssize_t
866show_sas_rphy_device_type(struct class_device *cdev, char *buf)
867{
868 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
869
870 if (!rphy->identify.device_type)
871 return snprintf(buf, 20, "none\n");
872 return get_sas_device_type_names(
873 rphy->identify.device_type, buf);
874}
875
876static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
877 show_sas_rphy_device_type, NULL);
878
Christoph Hellwiga0125642006-02-16 13:31:47 +0100879static ssize_t
880show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
881{
882 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
883 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
884 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
885 struct sas_internal *i = to_sas_internal(shost->transportt);
886 u64 identifier;
887 int error;
888
889 /*
890 * Only devices behind an expander are supported, because the
891 * enclosure identifier is a SMP feature.
892 */
James Bottomleyf4ad7b52006-08-25 13:48:18 -0500893 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +0100894 return -EINVAL;
895
896 error = i->f->get_enclosure_identifier(rphy, &identifier);
897 if (error)
898 return error;
899 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
900}
901
902static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
903 show_sas_rphy_enclosure_identifier, NULL);
904
905static ssize_t
906show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
907{
908 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
909 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
910 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
911 struct sas_internal *i = to_sas_internal(shost->transportt);
912 int val;
913
James Bottomleyf4ad7b52006-08-25 13:48:18 -0500914 if (scsi_is_sas_phy_local(phy))
Christoph Hellwiga0125642006-02-16 13:31:47 +0100915 return -EINVAL;
916
917 val = i->f->get_bay_identifier(rphy);
918 if (val < 0)
919 return val;
920 return sprintf(buf, "%d\n", val);
921}
922
923static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
924 show_sas_rphy_bay_identifier, NULL);
925
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200926sas_rphy_protocol_attr(identify.initiator_port_protocols,
927 initiator_port_protocols);
928sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
929sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
930 unsigned long long);
931sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
932
James Bottomley42ab0362006-03-04 09:10:18 -0600933/* only need 8 bytes of data plus header (4 or 8) */
934#define BUF_SIZE 64
935
936int sas_read_port_mode_page(struct scsi_device *sdev)
937{
938 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
939 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
940 struct sas_end_device *rdev;
941 struct scsi_mode_data mode_data;
942 int res, error;
943
944 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
945
946 rdev = rphy_to_end_device(rphy);
947
948 if (!buffer)
949 return -ENOMEM;
950
951 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
952 &mode_data, NULL);
953
954 error = -EINVAL;
955 if (!scsi_status_is_good(res))
956 goto out;
957
958 msdata = buffer + mode_data.header_length +
959 mode_data.block_descriptor_length;
960
961 if (msdata - buffer > BUF_SIZE - 8)
962 goto out;
963
964 error = 0;
965
966 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
967 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
968 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
969
970 out:
971 kfree(buffer);
972 return error;
973}
974EXPORT_SYMBOL(sas_read_port_mode_page);
975
James Bottomley79cb1812006-03-13 13:50:04 -0600976static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
977 "sas_end_device", NULL, NULL, NULL);
978
James Bottomley42ab0362006-03-04 09:10:18 -0600979#define sas_end_dev_show_simple(field, name, format_string, cast) \
980static ssize_t \
981show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
982{ \
983 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
984 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
985 \
986 return snprintf(buf, 20, format_string, cast rdev->field); \
987}
988
989#define sas_end_dev_simple_attr(field, name, format_string, type) \
990 sas_end_dev_show_simple(field, name, format_string, (type)) \
991static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
992 show_sas_end_dev_##name, NULL)
993
994sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
995sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
996 "%d\n", int);
997sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
998 "%d\n", int);
999
James Bottomley79cb1812006-03-13 13:50:04 -06001000static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1001 "sas_expander", NULL, NULL, NULL);
1002
1003#define sas_expander_show_simple(field, name, format_string, cast) \
1004static ssize_t \
1005show_sas_expander_##name(struct class_device *cdev, char *buf) \
1006{ \
1007 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1008 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1009 \
1010 return snprintf(buf, 20, format_string, cast edev->field); \
1011}
1012
1013#define sas_expander_simple_attr(field, name, format_string, type) \
1014 sas_expander_show_simple(field, name, format_string, (type)) \
1015static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
1016 show_sas_expander_##name, NULL)
1017
1018sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1019sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1020sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1021sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1022 "%s\n", char *);
1023sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1024sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1025 unsigned int);
1026sas_expander_simple_attr(level, level, "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001027
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001028static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
James Bottomley2f8600d2006-03-18 15:00:50 -06001029 "sas_device", NULL, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001030
1031static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1032{
1033 struct Scsi_Host *shost;
1034 struct sas_internal *i;
1035
1036 if (!scsi_is_sas_rphy(dev))
1037 return 0;
1038 shost = dev_to_shost(dev->parent->parent);
1039
1040 if (!shost->transportt)
1041 return 0;
1042 if (shost->transportt->host_attrs.ac.class !=
1043 &sas_host_class.class)
1044 return 0;
1045
1046 i = to_sas_internal(shost->transportt);
1047 return &i->rphy_attr_cont.ac == cont;
1048}
1049
James Bottomley42ab0362006-03-04 09:10:18 -06001050static int sas_end_dev_match(struct attribute_container *cont,
1051 struct device *dev)
1052{
1053 struct Scsi_Host *shost;
1054 struct sas_internal *i;
1055 struct sas_rphy *rphy;
1056
1057 if (!scsi_is_sas_rphy(dev))
1058 return 0;
1059 shost = dev_to_shost(dev->parent->parent);
1060 rphy = dev_to_rphy(dev);
1061
1062 if (!shost->transportt)
1063 return 0;
1064 if (shost->transportt->host_attrs.ac.class !=
1065 &sas_host_class.class)
1066 return 0;
1067
1068 i = to_sas_internal(shost->transportt);
1069 return &i->end_dev_attr_cont.ac == cont &&
James Bottomley2f8600d2006-03-18 15:00:50 -06001070 rphy->identify.device_type == SAS_END_DEVICE;
James Bottomley42ab0362006-03-04 09:10:18 -06001071}
1072
James Bottomley79cb1812006-03-13 13:50:04 -06001073static int sas_expander_match(struct attribute_container *cont,
1074 struct device *dev)
1075{
1076 struct Scsi_Host *shost;
1077 struct sas_internal *i;
1078 struct sas_rphy *rphy;
1079
1080 if (!scsi_is_sas_rphy(dev))
1081 return 0;
1082 shost = dev_to_shost(dev->parent->parent);
1083 rphy = dev_to_rphy(dev);
1084
1085 if (!shost->transportt)
1086 return 0;
1087 if (shost->transportt->host_attrs.ac.class !=
1088 &sas_host_class.class)
1089 return 0;
1090
1091 i = to_sas_internal(shost->transportt);
1092 return &i->expander_attr_cont.ac == cont &&
1093 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
James Bottomley2f8600d2006-03-18 15:00:50 -06001094 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
James Bottomley79cb1812006-03-13 13:50:04 -06001095}
1096
James Bottomley2f8600d2006-03-18 15:00:50 -06001097static void sas_expander_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001098{
1099 struct sas_rphy *rphy = dev_to_rphy(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001100 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001101
1102 put_device(dev->parent);
James Bottomley2f8600d2006-03-18 15:00:50 -06001103 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001104}
1105
James Bottomley2f8600d2006-03-18 15:00:50 -06001106static void sas_end_device_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001107{
James Bottomley2f8600d2006-03-18 15:00:50 -06001108 struct sas_rphy *rphy = dev_to_rphy(dev);
1109 struct sas_end_device *edev = rphy_to_end_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001110
James Bottomley2f8600d2006-03-18 15:00:50 -06001111 put_device(dev->parent);
1112 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001113}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001114
1115/**
James Bottomleyc5943d32006-06-12 09:09:18 -05001116 * sas_rphy_initialize - common rphy intialization
1117 * @rphy: rphy to initialise
1118 *
1119 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1120 * initialise the common rphy component of each.
1121 */
1122static void sas_rphy_initialize(struct sas_rphy *rphy)
1123{
1124 INIT_LIST_HEAD(&rphy->list);
1125}
1126
1127/**
James Bottomley42ab0362006-03-04 09:10:18 -06001128 * sas_end_device_alloc - allocate an rphy for an end device
1129 *
1130 * Allocates an SAS remote PHY structure, connected to @parent.
1131 *
1132 * Returns:
1133 * SAS PHY allocated or %NULL if the allocation failed.
1134 */
James Bottomley65c92b02006-06-28 12:22:50 -04001135struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
James Bottomley42ab0362006-03-04 09:10:18 -06001136{
1137 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1138 struct sas_end_device *rdev;
1139
1140 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1141 if (!rdev) {
James Bottomley42ab0362006-03-04 09:10:18 -06001142 return NULL;
1143 }
1144
1145 device_initialize(&rdev->rphy.dev);
1146 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001147 rdev->rphy.dev.release = sas_end_device_release;
James Bottomley65c92b02006-06-28 12:22:50 -04001148 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1149 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1150 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
1151 shost->host_no, rphy->scsi_target_id, parent->port_identifier);
1152 } else
1153 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
1154 shost->host_no, parent->port_identifier);
James Bottomley42ab0362006-03-04 09:10:18 -06001155 rdev->rphy.identify.device_type = SAS_END_DEVICE;
James Bottomleyc5943d32006-06-12 09:09:18 -05001156 sas_rphy_initialize(&rdev->rphy);
James Bottomley42ab0362006-03-04 09:10:18 -06001157 transport_setup_device(&rdev->rphy.dev);
1158
1159 return &rdev->rphy;
1160}
1161EXPORT_SYMBOL(sas_end_device_alloc);
1162
James Bottomley79cb1812006-03-13 13:50:04 -06001163/**
1164 * sas_expander_alloc - allocate an rphy for an end device
1165 *
1166 * Allocates an SAS remote PHY structure, connected to @parent.
1167 *
1168 * Returns:
1169 * SAS PHY allocated or %NULL if the allocation failed.
1170 */
James Bottomley65c92b02006-06-28 12:22:50 -04001171struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
James Bottomley79cb1812006-03-13 13:50:04 -06001172 enum sas_device_type type)
1173{
1174 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1175 struct sas_expander_device *rdev;
1176 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1177
1178 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1179 type != SAS_FANOUT_EXPANDER_DEVICE);
1180
1181 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1182 if (!rdev) {
James Bottomley79cb1812006-03-13 13:50:04 -06001183 return NULL;
1184 }
1185
1186 device_initialize(&rdev->rphy.dev);
1187 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001188 rdev->rphy.dev.release = sas_expander_release;
James Bottomley79cb1812006-03-13 13:50:04 -06001189 mutex_lock(&sas_host->lock);
1190 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1191 mutex_unlock(&sas_host->lock);
1192 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
1193 shost->host_no, rdev->rphy.scsi_target_id);
1194 rdev->rphy.identify.device_type = type;
James Bottomleyc5943d32006-06-12 09:09:18 -05001195 sas_rphy_initialize(&rdev->rphy);
James Bottomley79cb1812006-03-13 13:50:04 -06001196 transport_setup_device(&rdev->rphy.dev);
1197
1198 return &rdev->rphy;
1199}
1200EXPORT_SYMBOL(sas_expander_alloc);
James Bottomley42ab0362006-03-04 09:10:18 -06001201
1202/**
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001203 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
1204 * @rphy: The remote PHY to be added
1205 *
1206 * Publishes a SAS remote PHY to the rest of the system.
1207 */
1208int sas_rphy_add(struct sas_rphy *rphy)
1209{
James Bottomley65c92b02006-06-28 12:22:50 -04001210 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001211 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1212 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1213 struct sas_identify *identify = &rphy->identify;
1214 int error;
1215
1216 if (parent->rphy)
1217 return -ENXIO;
1218 parent->rphy = rphy;
1219
1220 error = device_add(&rphy->dev);
1221 if (error)
1222 return error;
1223 transport_add_device(&rphy->dev);
1224 transport_configure_device(&rphy->dev);
1225
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001226 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001227 list_add_tail(&rphy->list, &sas_host->rphy_list);
1228 if (identify->device_type == SAS_END_DEVICE &&
1229 (identify->target_port_protocols &
1230 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1231 rphy->scsi_target_id = sas_host->next_target_id++;
James Bottomley7676f832006-04-14 09:47:59 -05001232 else if (identify->device_type == SAS_END_DEVICE)
1233 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001234 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001235
James Bottomley79cb1812006-03-13 13:50:04 -06001236 if (identify->device_type == SAS_END_DEVICE &&
1237 rphy->scsi_target_id != -1) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001238 scsi_scan_target(&rphy->dev, 0,
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001239 rphy->scsi_target_id, ~0, 0);
1240 }
1241
1242 return 0;
1243}
1244EXPORT_SYMBOL(sas_rphy_add);
1245
1246/**
1247 * sas_rphy_free -- free a SAS remote PHY
1248 * @rphy SAS remote PHY to free
1249 *
1250 * Frees the specified SAS remote PHY.
1251 *
1252 * Note:
1253 * This function must only be called on a remote
1254 * PHY that has not sucessfully been added using
1255 * sas_rphy_add().
1256 */
1257void sas_rphy_free(struct sas_rphy *rphy)
1258{
Mike Anderson92aab642006-03-27 09:37:28 -08001259 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001260 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1261 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1262
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001263 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001264 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001265 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001266
Mike Anderson92aab642006-03-27 09:37:28 -08001267 transport_destroy_device(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001268
Mike Anderson92aab642006-03-27 09:37:28 -08001269 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001270}
1271EXPORT_SYMBOL(sas_rphy_free);
1272
1273/**
1274 * sas_rphy_delete -- remove SAS remote PHY
1275 * @rphy: SAS remote PHY to remove
1276 *
1277 * Removes the specified SAS remote PHY.
1278 */
1279void
1280sas_rphy_delete(struct sas_rphy *rphy)
1281{
1282 struct device *dev = &rphy->dev;
James Bottomley65c92b02006-06-28 12:22:50 -04001283 struct sas_port *parent = dev_to_sas_port(dev->parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001284 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1285 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1286
Christoph Hellwigd4054232006-01-04 13:45:20 +01001287 switch (rphy->identify.device_type) {
1288 case SAS_END_DEVICE:
1289 scsi_remove_target(dev);
1290 break;
1291 case SAS_EDGE_EXPANDER_DEVICE:
1292 case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley65c92b02006-06-28 12:22:50 -04001293 sas_remove_children(dev);
Christoph Hellwigd4054232006-01-04 13:45:20 +01001294 break;
1295 default:
1296 break;
1297 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001298
Christoph Hellwigfe8b2302005-09-25 23:10:33 +02001299 transport_remove_device(dev);
1300 device_del(dev);
1301 transport_destroy_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001302
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001303 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001304 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001305 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001306
Christoph Hellwig33b114e2006-01-11 14:20:43 +01001307 parent->rphy = NULL;
1308
Mike Anderson92aab642006-03-27 09:37:28 -08001309 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001310}
1311EXPORT_SYMBOL(sas_rphy_delete);
1312
1313/**
1314 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
1315 * @dev: device to check
1316 *
1317 * Returns:
1318 * %1 if the device represents a SAS remote PHY, %0 else
1319 */
1320int scsi_is_sas_rphy(const struct device *dev)
1321{
James Bottomley2f8600d2006-03-18 15:00:50 -06001322 return dev->release == sas_end_device_release ||
1323 dev->release == sas_expander_release;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001324}
1325EXPORT_SYMBOL(scsi_is_sas_rphy);
1326
1327
1328/*
1329 * SCSI scan helper
1330 */
1331
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001332static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1333 uint id, uint lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001334{
1335 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1336 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001337
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001338 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001339 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
James Bottomley6d99a3f2006-05-19 10:49:37 -05001340 if (rphy->identify.device_type != SAS_END_DEVICE ||
1341 rphy->scsi_target_id == -1)
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001342 continue;
1343
James Bottomleye8bf3942006-07-11 17:49:34 -04001344 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001345 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
James Bottomleye8bf3942006-07-11 17:49:34 -04001346 scsi_scan_target(&rphy->dev, 0,
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001347 rphy->scsi_target_id, lun, 1);
1348 }
1349 }
1350 mutex_unlock(&sas_host->lock);
1351
1352 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001353}
1354
1355
1356/*
1357 * Setup / Teardown code
1358 */
1359
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001360#define SETUP_TEMPLATE(attrb, field, perm, test) \
James Bottomley42ab0362006-03-04 09:10:18 -06001361 i->private_##attrb[count] = class_device_attr_##field; \
1362 i->private_##attrb[count].attr.mode = perm; \
James Bottomley42ab0362006-03-04 09:10:18 -06001363 i->attrb[count] = &i->private_##attrb[count]; \
1364 if (test) \
1365 count++
1366
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001367#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1368 i->private_##attrb[count] = class_device_attr_##field; \
1369 i->private_##attrb[count].attr.mode = perm; \
1370 if (ro_test) { \
1371 i->private_##attrb[count].attr.mode = ro_perm; \
1372 i->private_##attrb[count].store = NULL; \
1373 } \
1374 i->attrb[count] = &i->private_##attrb[count]; \
1375 if (test) \
1376 count++
James Bottomley42ab0362006-03-04 09:10:18 -06001377
1378#define SETUP_RPORT_ATTRIBUTE(field) \
1379 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001380
James Bottomleydd9fbb522006-03-02 16:01:31 -06001381#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001382 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001383
James Bottomley65c92b02006-06-28 12:22:50 -04001384#define SETUP_PHY_ATTRIBUTE(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001385 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001386
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001387#define SETUP_PHY_ATTRIBUTE_RW(field) \
1388 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1389 !i->f->set_phy_speed, S_IRUGO)
1390
James Bottomley65c92b02006-06-28 12:22:50 -04001391#define SETUP_PORT_ATTRIBUTE(field) \
1392 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1393
1394#define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001395 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001396
James Bottomley65c92b02006-06-28 12:22:50 -04001397#define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001398 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001399
James Bottomley65c92b02006-06-28 12:22:50 -04001400#define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001401 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001402
James Bottomley42ab0362006-03-04 09:10:18 -06001403#define SETUP_END_DEV_ATTRIBUTE(field) \
1404 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001405
James Bottomley79cb1812006-03-13 13:50:04 -06001406#define SETUP_EXPANDER_ATTRIBUTE(field) \
1407 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1408
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001409/**
1410 * sas_attach_transport -- instantiate SAS transport template
1411 * @ft: SAS transport class function template
1412 */
1413struct scsi_transport_template *
1414sas_attach_transport(struct sas_function_template *ft)
1415{
1416 struct sas_internal *i;
1417 int count;
1418
Jes Sorensen24669f752006-01-16 10:31:18 -05001419 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001420 if (!i)
1421 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001422
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001423 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001424
1425 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1426 i->t.host_attrs.ac.class = &sas_host_class.class;
1427 i->t.host_attrs.ac.match = sas_host_match;
1428 transport_container_register(&i->t.host_attrs);
1429 i->t.host_size = sizeof(struct sas_host_attrs);
1430
1431 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1432 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1433 i->phy_attr_cont.ac.match = sas_phy_match;
1434 transport_container_register(&i->phy_attr_cont);
1435
James Bottomley65c92b02006-06-28 12:22:50 -04001436 i->port_attr_cont.ac.class = &sas_port_class.class;
1437 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1438 i->port_attr_cont.ac.match = sas_port_match;
1439 transport_container_register(&i->port_attr_cont);
1440
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001441 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1442 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1443 i->rphy_attr_cont.ac.match = sas_rphy_match;
1444 transport_container_register(&i->rphy_attr_cont);
1445
James Bottomley42ab0362006-03-04 09:10:18 -06001446 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1447 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1448 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1449 transport_container_register(&i->end_dev_attr_cont);
1450
James Bottomley79cb1812006-03-13 13:50:04 -06001451 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1452 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1453 i->expander_attr_cont.ac.match = sas_expander_match;
1454 transport_container_register(&i->expander_attr_cont);
1455
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001456 i->f = ft;
1457
1458 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001459 SETUP_PORT_ATTRIBUTE(num_phys);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001460 i->host_attrs[count] = NULL;
1461
1462 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001463 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1464 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1465 SETUP_PHY_ATTRIBUTE(device_type);
1466 SETUP_PHY_ATTRIBUTE(sas_address);
1467 SETUP_PHY_ATTRIBUTE(phy_identifier);
1468 //SETUP_PHY_ATTRIBUTE(port_identifier);
1469 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1470 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001471 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
James Bottomley65c92b02006-06-28 12:22:50 -04001472 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001473 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +02001474
James Bottomley65c92b02006-06-28 12:22:50 -04001475 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1476 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1477 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1478 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1479 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1480 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001481 i->phy_attrs[count] = NULL;
1482
1483 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001484 SETUP_PORT_ATTRIBUTE(num_phys);
1485 i->port_attrs[count] = NULL;
1486
1487 count = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001488 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1489 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1490 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1491 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1492 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001493 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1494 get_enclosure_identifier);
1495 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1496 get_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001497 i->rphy_attrs[count] = NULL;
1498
James Bottomley42ab0362006-03-04 09:10:18 -06001499 count = 0;
1500 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1501 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1502 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1503 i->end_dev_attrs[count] = NULL;
1504
James Bottomley79cb1812006-03-13 13:50:04 -06001505 count = 0;
1506 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1507 SETUP_EXPANDER_ATTRIBUTE(product_id);
1508 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1509 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1510 SETUP_EXPANDER_ATTRIBUTE(component_id);
1511 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1512 SETUP_EXPANDER_ATTRIBUTE(level);
1513 i->expander_attrs[count] = NULL;
1514
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001515 return &i->t;
1516}
1517EXPORT_SYMBOL(sas_attach_transport);
1518
1519/**
1520 * sas_release_transport -- release SAS transport template instance
1521 * @t: transport template instance
1522 */
1523void sas_release_transport(struct scsi_transport_template *t)
1524{
1525 struct sas_internal *i = to_sas_internal(t);
1526
1527 transport_container_unregister(&i->t.host_attrs);
1528 transport_container_unregister(&i->phy_attr_cont);
James Bottomley65c92b02006-06-28 12:22:50 -04001529 transport_container_unregister(&i->port_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001530 transport_container_unregister(&i->rphy_attr_cont);
James Bottomleydb82f842006-03-09 22:06:36 -05001531 transport_container_unregister(&i->end_dev_attr_cont);
James Bottomley79cb1812006-03-13 13:50:04 -06001532 transport_container_unregister(&i->expander_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001533
1534 kfree(i);
1535}
1536EXPORT_SYMBOL(sas_release_transport);
1537
1538static __init int sas_transport_init(void)
1539{
1540 int error;
1541
1542 error = transport_class_register(&sas_host_class);
1543 if (error)
1544 goto out;
1545 error = transport_class_register(&sas_phy_class);
1546 if (error)
1547 goto out_unregister_transport;
James Bottomley65c92b02006-06-28 12:22:50 -04001548 error = transport_class_register(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001549 if (error)
1550 goto out_unregister_phy;
James Bottomley65c92b02006-06-28 12:22:50 -04001551 error = transport_class_register(&sas_rphy_class);
1552 if (error)
1553 goto out_unregister_port;
James Bottomley42ab0362006-03-04 09:10:18 -06001554 error = transport_class_register(&sas_end_dev_class);
1555 if (error)
1556 goto out_unregister_rphy;
James Bottomley79cb1812006-03-13 13:50:04 -06001557 error = transport_class_register(&sas_expander_class);
1558 if (error)
1559 goto out_unregister_end_dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001560
1561 return 0;
1562
James Bottomley79cb1812006-03-13 13:50:04 -06001563 out_unregister_end_dev:
1564 transport_class_unregister(&sas_end_dev_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001565 out_unregister_rphy:
1566 transport_class_unregister(&sas_rphy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001567 out_unregister_port:
1568 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001569 out_unregister_phy:
1570 transport_class_unregister(&sas_phy_class);
1571 out_unregister_transport:
1572 transport_class_unregister(&sas_host_class);
1573 out:
1574 return error;
1575
1576}
1577
1578static void __exit sas_transport_exit(void)
1579{
1580 transport_class_unregister(&sas_host_class);
1581 transport_class_unregister(&sas_phy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001582 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001583 transport_class_unregister(&sas_rphy_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001584 transport_class_unregister(&sas_end_dev_class);
James Bottomley79cb1812006-03-13 13:50:04 -06001585 transport_class_unregister(&sas_expander_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001586}
1587
1588MODULE_AUTHOR("Christoph Hellwig");
1589MODULE_DESCRIPTION("SAS Transphy Attributes");
1590MODULE_LICENSE("GPL");
1591
1592module_init(sas_transport_init);
1593module_exit(sas_transport_exit);