[SCSI] Alter the scsi_add_device() API to conform to what users expect
[linux-2.6] / drivers / scsi / scsi_transport_sas.c
1 /*
2  * Copyright (C) 2005 Dell Inc.
3  *      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>
29
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_sas.h>
34
35
36 #define SAS_HOST_ATTRS          0
37 #define SAS_PORT_ATTRS          11
38 #define SAS_RPORT_ATTRS         5
39
40 struct sas_internal {
41         struct scsi_transport_template t;
42         struct sas_function_template *f;
43
44         struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
45         struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
46         struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
47
48         struct transport_container phy_attr_cont;
49         struct transport_container rphy_attr_cont;
50
51         /*
52          * The array of null terminated pointers to attributes
53          * needed by scsi_sysfs.c
54          */
55         struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
56         struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
57         struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
58 };
59 #define to_sas_internal(tmpl)   container_of(tmpl, struct sas_internal, t)
60
61 struct sas_host_attrs {
62         struct list_head rphy_list;
63         spinlock_t lock;
64         u32 next_target_id;
65 };
66 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
67
68
69 /*
70  * Hack to allow attributes of the same name in different objects.
71  */
72 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
73         struct class_device_attribute class_device_attr_##_prefix##_##_name = \
74         __ATTR(_name,_mode,_show,_store)
75
76
77 /*
78  * Pretty printing helpers
79  */
80
81 #define sas_bitfield_name_match(title, table)                   \
82 static ssize_t                                                  \
83 get_sas_##title##_names(u32 table_key, char *buf)               \
84 {                                                               \
85         char *prefix = "";                                      \
86         ssize_t len = 0;                                        \
87         int i;                                                  \
88                                                                 \
89         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
90                 if (table[i].value & table_key) {               \
91                         len += sprintf(buf + len, "%s%s",       \
92                                 prefix, table[i].name);         \
93                         prefix = ", ";                          \
94                 }                                               \
95         }                                                       \
96         len += sprintf(buf + len, "\n");                        \
97         return len;                                             \
98 }
99
100 #define sas_bitfield_name_search(title, table)                  \
101 static ssize_t                                                  \
102 get_sas_##title##_names(u32 table_key, char *buf)               \
103 {                                                               \
104         ssize_t len = 0;                                        \
105         int i;                                                  \
106                                                                 \
107         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
108                 if (table[i].value == table_key) {              \
109                         len += sprintf(buf + len, "%s",         \
110                                 table[i].name);                 \
111                         break;                                  \
112                 }                                               \
113         }                                                       \
114         len += sprintf(buf + len, "\n");                        \
115         return len;                                             \
116 }
117
118 static struct {
119         u32             value;
120         char            *name;
121 } sas_device_type_names[] = {
122         { SAS_PHY_UNUSED,               "unused" },
123         { SAS_END_DEVICE,               "end device" },
124         { SAS_EDGE_EXPANDER_DEVICE,     "edge expander" },
125         { SAS_FANOUT_EXPANDER_DEVICE,   "fanout expander" },
126 };
127 sas_bitfield_name_search(device_type, sas_device_type_names)
128
129
130 static struct {
131         u32             value;
132         char            *name;
133 } sas_protocol_names[] = {
134         { SAS_PROTOCOL_SATA,            "sata" },
135         { SAS_PROTOCOL_SMP,             "smp" },
136         { SAS_PROTOCOL_STP,             "stp" },
137         { SAS_PROTOCOL_SSP,             "ssp" },
138 };
139 sas_bitfield_name_match(protocol, sas_protocol_names)
140
141 static struct {
142         u32             value;
143         char            *name;
144 } sas_linkspeed_names[] = {
145         { SAS_LINK_RATE_UNKNOWN,        "Unknown" },
146         { SAS_PHY_DISABLED,             "Phy disabled" },
147         { SAS_LINK_RATE_FAILED,         "Link Rate failed" },
148         { SAS_SATA_SPINUP_HOLD,         "Spin-up hold" },
149         { SAS_LINK_RATE_1_5_GBPS,       "1.5 Gbit" },
150         { SAS_LINK_RATE_3_0_GBPS,       "3.0 Gbit" },
151 };
152 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
153
154
155 /*
156  * SAS host attributes
157  */
158
159 static int sas_host_setup(struct transport_container *tc, struct device *dev,
160                           struct class_device *cdev)
161 {
162         struct Scsi_Host *shost = dev_to_shost(dev);
163         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
164
165         INIT_LIST_HEAD(&sas_host->rphy_list);
166         spin_lock_init(&sas_host->lock);
167         sas_host->next_target_id = 0;
168         return 0;
169 }
170
171 static DECLARE_TRANSPORT_CLASS(sas_host_class,
172                 "sas_host", sas_host_setup, NULL, NULL);
173
174 static int sas_host_match(struct attribute_container *cont,
175                             struct device *dev)
176 {
177         struct Scsi_Host *shost;
178         struct sas_internal *i;
179
180         if (!scsi_is_host_device(dev))
181                 return 0;
182         shost = dev_to_shost(dev);
183
184         if (!shost->transportt)
185                 return 0;
186         if (shost->transportt->host_attrs.ac.class !=
187                         &sas_host_class.class)
188                 return 0;
189
190         i = to_sas_internal(shost->transportt);
191         return &i->t.host_attrs.ac == cont;
192 }
193
194 static int do_sas_phy_delete(struct device *dev, void *data)
195 {
196         if (scsi_is_sas_phy(dev))
197                 sas_phy_delete(dev_to_phy(dev));
198         return 0;
199 }
200
201 /**
202  * sas_remove_host  --  tear down a Scsi_Host's SAS data structures
203  * @shost:      Scsi Host that is torn down
204  *
205  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
206  * Must be called just before scsi_remove_host for SAS HBAs.
207  */
208 void sas_remove_host(struct Scsi_Host *shost)
209 {
210         device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
211 }
212 EXPORT_SYMBOL(sas_remove_host);
213
214
215 /*
216  * SAS Port attributes
217  */
218
219 #define sas_phy_show_simple(field, name, format_string, cast)           \
220 static ssize_t                                                          \
221 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
222 {                                                                       \
223         struct sas_phy *phy = transport_class_to_phy(cdev);             \
224                                                                         \
225         return snprintf(buf, 20, format_string, cast phy->field);       \
226 }
227
228 #define sas_phy_simple_attr(field, name, format_string, type)           \
229         sas_phy_show_simple(field, name, format_string, (type)) \
230 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
231
232 #define sas_phy_show_protocol(field, name)                              \
233 static ssize_t                                                          \
234 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
235 {                                                                       \
236         struct sas_phy *phy = transport_class_to_phy(cdev);             \
237                                                                         \
238         if (!phy->field)                                                \
239                 return snprintf(buf, 20, "none\n");                     \
240         return get_sas_protocol_names(phy->field, buf);         \
241 }
242
243 #define sas_phy_protocol_attr(field, name)                              \
244         sas_phy_show_protocol(field, name)                              \
245 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
246
247 #define sas_phy_show_linkspeed(field)                                   \
248 static ssize_t                                                          \
249 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
250 {                                                                       \
251         struct sas_phy *phy = transport_class_to_phy(cdev);             \
252                                                                         \
253         return get_sas_linkspeed_names(phy->field, buf);                \
254 }
255
256 #define sas_phy_linkspeed_attr(field)                                   \
257         sas_phy_show_linkspeed(field)                                   \
258 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
259
260 static ssize_t
261 show_sas_device_type(struct class_device *cdev, char *buf)
262 {
263         struct sas_phy *phy = transport_class_to_phy(cdev);
264
265         if (!phy->identify.device_type)
266                 return snprintf(buf, 20, "none\n");
267         return get_sas_device_type_names(phy->identify.device_type, buf);
268 }
269
270 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
271
272 sas_phy_protocol_attr(identify.initiator_port_protocols,
273                 initiator_port_protocols);
274 sas_phy_protocol_attr(identify.target_port_protocols,
275                 target_port_protocols);
276 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
277                 unsigned long long);
278 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
279 sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
280 sas_phy_linkspeed_attr(negotiated_linkrate);
281 sas_phy_linkspeed_attr(minimum_linkrate_hw);
282 sas_phy_linkspeed_attr(minimum_linkrate);
283 sas_phy_linkspeed_attr(maximum_linkrate_hw);
284 sas_phy_linkspeed_attr(maximum_linkrate);
285
286
287 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
288                 "sas_phy", NULL, NULL, NULL);
289
290 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
291 {
292         struct Scsi_Host *shost;
293         struct sas_internal *i;
294
295         if (!scsi_is_sas_phy(dev))
296                 return 0;
297         shost = dev_to_shost(dev->parent);
298
299         if (!shost->transportt)
300                 return 0;
301         if (shost->transportt->host_attrs.ac.class !=
302                         &sas_host_class.class)
303                 return 0;
304
305         i = to_sas_internal(shost->transportt);
306         return &i->phy_attr_cont.ac == cont;
307 }
308
309 static void sas_phy_release(struct device *dev)
310 {
311         struct sas_phy *phy = dev_to_phy(dev);
312
313         put_device(dev->parent);
314         kfree(phy);
315 }
316
317 /**
318  * sas_phy_alloc  --  allocates and initialize a SAS PHY structure
319  * @parent:     Parent device
320  * @number:     Port number
321  *
322  * Allocates an SAS PHY structure.  It will be added in the device tree
323  * below the device specified by @parent, which has to be either a Scsi_Host
324  * or sas_rphy.
325  *
326  * Returns:
327  *      SAS PHY allocated or %NULL if the allocation failed.
328  */
329 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
330 {
331         struct Scsi_Host *shost = dev_to_shost(parent);
332         struct sas_phy *phy;
333
334         phy = kmalloc(sizeof(*phy), GFP_KERNEL);
335         if (!phy)
336                 return NULL;
337         memset(phy, 0, sizeof(*phy));
338
339         get_device(parent);
340
341         phy->number = number;
342
343         device_initialize(&phy->dev);
344         phy->dev.parent = get_device(parent);
345         phy->dev.release = sas_phy_release;
346         sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
347
348         transport_setup_device(&phy->dev);
349
350         return phy;
351 }
352 EXPORT_SYMBOL(sas_phy_alloc);
353
354 /**
355  * sas_phy_add  --  add a SAS PHY to the device hierachy
356  * @phy:        The PHY to be added
357  *
358  * Publishes a SAS PHY to the rest of the system.
359  */
360 int sas_phy_add(struct sas_phy *phy)
361 {
362         int error;
363
364         error = device_add(&phy->dev);
365         if (!error) {
366                 transport_add_device(&phy->dev);
367                 transport_configure_device(&phy->dev);
368         }
369
370         return error;
371 }
372 EXPORT_SYMBOL(sas_phy_add);
373
374 /**
375  * sas_phy_free  --  free a SAS PHY
376  * @phy:        SAS PHY to free
377  *
378  * Frees the specified SAS PHY.
379  *
380  * Note:
381  *   This function must only be called on a PHY that has not
382  *   sucessfully been added using sas_phy_add().
383  */
384 void sas_phy_free(struct sas_phy *phy)
385 {
386         transport_destroy_device(&phy->dev);
387         put_device(phy->dev.parent);
388         put_device(phy->dev.parent);
389         put_device(phy->dev.parent);
390         kfree(phy);
391 }
392 EXPORT_SYMBOL(sas_phy_free);
393
394 /**
395  * sas_phy_delete  --  remove SAS PHY
396  * @phy:        SAS PHY to remove
397  *
398  * Removes the specified SAS PHY.  If the SAS PHY has an
399  * associated remote PHY it is removed before.
400  */
401 void
402 sas_phy_delete(struct sas_phy *phy)
403 {
404         struct device *dev = &phy->dev;
405
406         if (phy->rphy)
407                 sas_rphy_delete(phy->rphy);
408
409         transport_remove_device(dev);
410         device_del(dev);
411         transport_destroy_device(dev);
412         put_device(dev->parent);
413 }
414 EXPORT_SYMBOL(sas_phy_delete);
415
416 /**
417  * scsi_is_sas_phy  --  check if a struct device represents a SAS PHY
418  * @dev:        device to check
419  *
420  * Returns:
421  *      %1 if the device represents a SAS PHY, %0 else
422  */
423 int scsi_is_sas_phy(const struct device *dev)
424 {
425         return dev->release == sas_phy_release;
426 }
427 EXPORT_SYMBOL(scsi_is_sas_phy);
428
429 /*
430  * SAS remote PHY attributes.
431  */
432
433 #define sas_rphy_show_simple(field, name, format_string, cast)          \
434 static ssize_t                                                          \
435 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
436 {                                                                       \
437         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
438                                                                         \
439         return snprintf(buf, 20, format_string, cast rphy->field);      \
440 }
441
442 #define sas_rphy_simple_attr(field, name, format_string, type)          \
443         sas_rphy_show_simple(field, name, format_string, (type))        \
444 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
445                 show_sas_rphy_##name, NULL)
446
447 #define sas_rphy_show_protocol(field, name)                             \
448 static ssize_t                                                          \
449 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
450 {                                                                       \
451         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
452                                                                         \
453         if (!rphy->field)                                       \
454                 return snprintf(buf, 20, "none\n");                     \
455         return get_sas_protocol_names(rphy->field, buf);        \
456 }
457
458 #define sas_rphy_protocol_attr(field, name)                             \
459         sas_rphy_show_protocol(field, name)                             \
460 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
461                 show_sas_rphy_##name, NULL)
462
463 static ssize_t
464 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
465 {
466         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
467
468         if (!rphy->identify.device_type)
469                 return snprintf(buf, 20, "none\n");
470         return get_sas_device_type_names(
471                         rphy->identify.device_type, buf);
472 }
473
474 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
475                 show_sas_rphy_device_type, NULL);
476
477 sas_rphy_protocol_attr(identify.initiator_port_protocols,
478                 initiator_port_protocols);
479 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
480 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
481                 unsigned long long);
482 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
483
484 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
485                 "sas_rphy", NULL, NULL, NULL);
486
487 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
488 {
489         struct Scsi_Host *shost;
490         struct sas_internal *i;
491
492         if (!scsi_is_sas_rphy(dev))
493                 return 0;
494         shost = dev_to_shost(dev->parent->parent);
495
496         if (!shost->transportt)
497                 return 0;
498         if (shost->transportt->host_attrs.ac.class !=
499                         &sas_host_class.class)
500                 return 0;
501
502         i = to_sas_internal(shost->transportt);
503         return &i->rphy_attr_cont.ac == cont;
504 }
505
506 static void sas_rphy_release(struct device *dev)
507 {
508         struct sas_rphy *rphy = dev_to_rphy(dev);
509
510         put_device(dev->parent);
511         kfree(rphy);
512 }
513
514 /**
515  * sas_rphy_alloc  --  allocates and initialize a SAS remote PHY structure
516  * @parent:             SAS PHY this remote PHY is conneted to
517  *
518  * Allocates an SAS remote PHY structure, connected to @parent.
519  *
520  * Returns:
521  *      SAS PHY allocated or %NULL if the allocation failed.
522  */
523 struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
524 {
525         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
526         struct sas_rphy *rphy;
527
528         rphy = kmalloc(sizeof(*rphy), GFP_KERNEL);
529         if (!rphy) {
530                 put_device(&parent->dev);
531                 return NULL;
532         }
533         memset(rphy, 0, sizeof(*rphy));
534
535         device_initialize(&rphy->dev);
536         rphy->dev.parent = get_device(&parent->dev);
537         rphy->dev.release = sas_rphy_release;
538         sprintf(rphy->dev.bus_id, "rphy-%d:%d",
539                 shost->host_no, parent->number);
540         transport_setup_device(&rphy->dev);
541
542         return rphy;
543 }
544 EXPORT_SYMBOL(sas_rphy_alloc);
545
546 /**
547  * sas_rphy_add  --  add a SAS remote PHY to the device hierachy
548  * @rphy:       The remote PHY to be added
549  *
550  * Publishes a SAS remote PHY to the rest of the system.
551  */
552 int sas_rphy_add(struct sas_rphy *rphy)
553 {
554         struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
555         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
556         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
557         struct sas_identify *identify = &rphy->identify;
558         int error;
559
560         if (parent->rphy)
561                 return -ENXIO;
562         parent->rphy = rphy;
563
564         error = device_add(&rphy->dev);
565         if (error)
566                 return error;
567         transport_add_device(&rphy->dev);
568         transport_configure_device(&rphy->dev);
569
570         spin_lock(&sas_host->lock);
571         list_add_tail(&rphy->list, &sas_host->rphy_list);
572         if (identify->device_type == SAS_END_DEVICE &&
573             (identify->target_port_protocols &
574              (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
575                 rphy->scsi_target_id = sas_host->next_target_id++;
576         else
577                 rphy->scsi_target_id = -1;
578         spin_unlock(&sas_host->lock);
579
580         if (rphy->scsi_target_id != -1) {
581                 scsi_scan_target(&rphy->dev, parent->number,
582                                 rphy->scsi_target_id, ~0, 0);
583         }
584
585         return 0;
586 }
587 EXPORT_SYMBOL(sas_rphy_add);
588
589 /**
590  * sas_rphy_free  --  free a SAS remote PHY
591  * @rphy        SAS remote PHY to free
592  *
593  * Frees the specified SAS remote PHY.
594  *
595  * Note:
596  *   This function must only be called on a remote
597  *   PHY that has not sucessfully been added using
598  *   sas_rphy_add().
599  */
600 void sas_rphy_free(struct sas_rphy *rphy)
601 {
602         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
603         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
604
605         spin_lock(&sas_host->lock);
606         list_del(&rphy->list);
607         spin_unlock(&sas_host->lock);
608
609         transport_destroy_device(&rphy->dev);
610         put_device(rphy->dev.parent);
611         put_device(rphy->dev.parent);
612         put_device(rphy->dev.parent);
613         kfree(rphy);
614 }
615 EXPORT_SYMBOL(sas_rphy_free);
616
617 /**
618  * sas_rphy_delete  --  remove SAS remote PHY
619  * @rphy:       SAS remote PHY to remove
620  *
621  * Removes the specified SAS remote PHY.
622  */
623 void
624 sas_rphy_delete(struct sas_rphy *rphy)
625 {
626         struct device *dev = &rphy->dev;
627         struct sas_phy *parent = dev_to_phy(dev->parent);
628         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
629         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
630
631         transport_destroy_device(&rphy->dev);
632
633         scsi_remove_target(&rphy->dev);
634
635         spin_lock(&sas_host->lock);
636         list_del(&rphy->list);
637         spin_unlock(&sas_host->lock);
638
639         transport_remove_device(dev);
640         device_del(dev);
641         transport_destroy_device(dev);
642         put_device(&parent->dev);
643 }
644 EXPORT_SYMBOL(sas_rphy_delete);
645
646 /**
647  * scsi_is_sas_rphy  --  check if a struct device represents a SAS remote PHY
648  * @dev:        device to check
649  *
650  * Returns:
651  *      %1 if the device represents a SAS remote PHY, %0 else
652  */
653 int scsi_is_sas_rphy(const struct device *dev)
654 {
655         return dev->release == sas_rphy_release;
656 }
657 EXPORT_SYMBOL(scsi_is_sas_rphy);
658
659
660 /*
661  * SCSI scan helper
662  */
663
664 static struct device *sas_target_parent(struct Scsi_Host *shost,
665                                         int channel, uint id)
666 {
667         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
668         struct sas_rphy *rphy;
669         struct device *dev = NULL;
670
671         spin_lock(&sas_host->lock);
672         list_for_each_entry(rphy, &sas_host->rphy_list, list) {
673                 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
674                 if (parent->number == channel &&
675                     rphy->scsi_target_id == id)
676                         dev = &rphy->dev;
677         }
678         spin_unlock(&sas_host->lock);
679
680         return dev;
681 }
682
683
684 /*
685  * Setup / Teardown code
686  */
687
688 #define SETUP_RPORT_ATTRIBUTE(field)                                    \
689         i->private_rphy_attrs[count] = class_device_attr_##field;       \
690         i->private_rphy_attrs[count].attr.mode = S_IRUGO;               \
691         i->private_rphy_attrs[count].store = NULL;                      \
692         i->rphy_attrs[count] = &i->private_rphy_attrs[count];   \
693         count++
694
695 #define SETUP_PORT_ATTRIBUTE(field)                                     \
696         i->private_phy_attrs[count] = class_device_attr_##field;        \
697         i->private_phy_attrs[count].attr.mode = S_IRUGO;                \
698         i->private_phy_attrs[count].store = NULL;                       \
699         i->phy_attrs[count] = &i->private_phy_attrs[count];             \
700         count++
701
702
703 /**
704  * sas_attach_transport  --  instantiate SAS transport template
705  * @ft:         SAS transport class function template
706  */
707 struct scsi_transport_template *
708 sas_attach_transport(struct sas_function_template *ft)
709 {
710         struct sas_internal *i;
711         int count;
712
713         i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL);
714         if (!i)
715                 return NULL;
716         memset(i, 0, sizeof(struct sas_internal));
717
718         i->t.target_parent = sas_target_parent;
719
720         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
721         i->t.host_attrs.ac.class = &sas_host_class.class;
722         i->t.host_attrs.ac.match = sas_host_match;
723         transport_container_register(&i->t.host_attrs);
724         i->t.host_size = sizeof(struct sas_host_attrs);
725
726         i->phy_attr_cont.ac.class = &sas_phy_class.class;
727         i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
728         i->phy_attr_cont.ac.match = sas_phy_match;
729         transport_container_register(&i->phy_attr_cont);
730
731         i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
732         i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
733         i->rphy_attr_cont.ac.match = sas_rphy_match;
734         transport_container_register(&i->rphy_attr_cont);
735
736         i->f = ft;
737
738         count = 0;
739         i->host_attrs[count] = NULL;
740
741         count = 0;
742         SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
743         SETUP_PORT_ATTRIBUTE(target_port_protocols);
744         SETUP_PORT_ATTRIBUTE(device_type);
745         SETUP_PORT_ATTRIBUTE(sas_address);
746         SETUP_PORT_ATTRIBUTE(phy_identifier);
747         SETUP_PORT_ATTRIBUTE(port_identifier);
748         SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
749         SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
750         SETUP_PORT_ATTRIBUTE(minimum_linkrate);
751         SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
752         SETUP_PORT_ATTRIBUTE(maximum_linkrate);
753         i->phy_attrs[count] = NULL;
754
755         count = 0;
756         SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
757         SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
758         SETUP_RPORT_ATTRIBUTE(rphy_device_type);
759         SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
760         SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
761         i->rphy_attrs[count] = NULL;
762
763         return &i->t;
764 }
765 EXPORT_SYMBOL(sas_attach_transport);
766
767 /**
768  * sas_release_transport  --  release SAS transport template instance
769  * @t:          transport template instance
770  */
771 void sas_release_transport(struct scsi_transport_template *t)
772 {
773         struct sas_internal *i = to_sas_internal(t);
774
775         transport_container_unregister(&i->t.host_attrs);
776         transport_container_unregister(&i->phy_attr_cont);
777         transport_container_unregister(&i->rphy_attr_cont);
778
779         kfree(i);
780 }
781 EXPORT_SYMBOL(sas_release_transport);
782
783 static __init int sas_transport_init(void)
784 {
785         int error;
786
787         error = transport_class_register(&sas_host_class);
788         if (error)
789                 goto out;
790         error = transport_class_register(&sas_phy_class);
791         if (error)
792                 goto out_unregister_transport;
793         error = transport_class_register(&sas_rphy_class);
794         if (error)
795                 goto out_unregister_phy;
796
797         return 0;
798
799  out_unregister_phy:
800         transport_class_unregister(&sas_phy_class);
801  out_unregister_transport:
802         transport_class_unregister(&sas_host_class);
803  out:
804         return error;
805
806 }
807
808 static void __exit sas_transport_exit(void)
809 {
810         transport_class_unregister(&sas_host_class);
811         transport_class_unregister(&sas_phy_class);
812         transport_class_unregister(&sas_rphy_class);
813 }
814
815 MODULE_AUTHOR("Christoph Hellwig");
816 MODULE_DESCRIPTION("SAS Transphy Attributes");
817 MODULE_LICENSE("GPL");
818
819 module_init(sas_transport_init);
820 module_exit(sas_transport_exit);