Merge gregkh@master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / scsi / scsi_transport_sas.c
1 /*
2  * Copyright (C) 2005-2006 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 #include <linux/slab.h>
30 #include <linux/string.h>
31
32 #include <scsi/scsi.h>
33 #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
38 #include "scsi_sas_internal.h"
39 struct sas_host_attrs {
40         struct list_head rphy_list;
41         struct mutex lock;
42         u32 next_target_id;
43         u32 next_expander_id;
44         int next_port_id;
45 };
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)                   \
62 static ssize_t                                                  \
63 get_sas_##title##_names(u32 table_key, char *buf)               \
64 {                                                               \
65         char *prefix = "";                                      \
66         ssize_t len = 0;                                        \
67         int i;                                                  \
68                                                                 \
69         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
70                 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
80 #define sas_bitfield_name_search(title, table)                  \
81 static ssize_t                                                  \
82 get_sas_##title##_names(u32 table_key, char *buf)               \
83 {                                                               \
84         ssize_t len = 0;                                        \
85         int i;                                                  \
86                                                                 \
87         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
88                 if (table[i].value == table_key) {              \
89                         len += sprintf(buf + len, "%s",         \
90                                 table[i].name);                 \
91                         break;                                  \
92                 }                                               \
93         }                                                       \
94         len += sprintf(buf + len, "\n");                        \
95         return len;                                             \
96 }
97
98 static struct {
99         u32             value;
100         char            *name;
101 } sas_device_type_names[] = {
102         { SAS_PHY_UNUSED,               "unused" },
103         { SAS_END_DEVICE,               "end device" },
104         { SAS_EDGE_EXPANDER_DEVICE,     "edge expander" },
105         { SAS_FANOUT_EXPANDER_DEVICE,   "fanout expander" },
106 };
107 sas_bitfield_name_search(device_type, sas_device_type_names)
108
109
110 static struct {
111         u32             value;
112         char            *name;
113 } sas_protocol_names[] = {
114         { SAS_PROTOCOL_SATA,            "sata" },
115         { SAS_PROTOCOL_SMP,             "smp" },
116         { SAS_PROTOCOL_STP,             "stp" },
117         { SAS_PROTOCOL_SSP,             "ssp" },
118 };
119 sas_bitfield_name_match(protocol, sas_protocol_names)
120
121 static struct {
122         u32             value;
123         char            *name;
124 } sas_linkspeed_names[] = {
125         { SAS_LINK_RATE_UNKNOWN,        "Unknown" },
126         { SAS_PHY_DISABLED,             "Phy disabled" },
127         { SAS_LINK_RATE_FAILED,         "Link Rate failed" },
128         { SAS_SATA_SPINUP_HOLD,         "Spin-up hold" },
129         { SAS_LINK_RATE_1_5_GBPS,       "1.5 Gbit" },
130         { SAS_LINK_RATE_3_0_GBPS,       "3.0 Gbit" },
131         { SAS_LINK_RATE_6_0_GBPS,       "6.0 Gbit" },
132 };
133 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
134
135
136 /*
137  * SAS host attributes
138  */
139
140 static int sas_host_setup(struct transport_container *tc, struct device *dev,
141                           struct class_device *cdev)
142 {
143         struct Scsi_Host *shost = dev_to_shost(dev);
144         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
145
146         INIT_LIST_HEAD(&sas_host->rphy_list);
147         mutex_init(&sas_host->lock);
148         sas_host->next_target_id = 0;
149         sas_host->next_expander_id = 0;
150         sas_host->next_port_id = 0;
151         return 0;
152 }
153
154 static DECLARE_TRANSPORT_CLASS(sas_host_class,
155                 "sas_host", sas_host_setup, NULL, NULL);
156
157 static int sas_host_match(struct attribute_container *cont,
158                             struct device *dev)
159 {
160         struct Scsi_Host *shost;
161         struct sas_internal *i;
162
163         if (!scsi_is_host_device(dev))
164                 return 0;
165         shost = dev_to_shost(dev);
166
167         if (!shost->transportt)
168                 return 0;
169         if (shost->transportt->host_attrs.ac.class !=
170                         &sas_host_class.class)
171                 return 0;
172
173         i = to_sas_internal(shost->transportt);
174         return &i->t.host_attrs.ac == cont;
175 }
176
177 static int do_sas_phy_delete(struct device *dev, void *data)
178 {
179         int pass = (int)(unsigned long)data;
180
181         if (pass == 0 && scsi_is_sas_port(dev))
182                 sas_port_delete(dev_to_sas_port(dev));
183         else if (pass == 1 && scsi_is_sas_phy(dev))
184                 sas_phy_delete(dev_to_phy(dev));
185         return 0;
186 }
187
188 /**
189  * sas_remove_children  --  tear down a devices SAS data structures
190  * @dev:        device belonging to the sas object
191  *
192  * Removes all SAS PHYs and remote PHYs for a given object
193  */
194 void sas_remove_children(struct device *dev)
195 {
196         device_for_each_child(dev, (void *)0, do_sas_phy_delete);
197         device_for_each_child(dev, (void *)1, do_sas_phy_delete);
198 }
199 EXPORT_SYMBOL(sas_remove_children);
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         sas_remove_children(&shost->shost_gendev);
211 }
212 EXPORT_SYMBOL(sas_remove_host);
213
214
215 /*
216  * SAS Phy 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 #define sas_phy_show_linkerror(field)                                   \
261 static ssize_t                                                          \
262 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
263 {                                                                       \
264         struct sas_phy *phy = transport_class_to_phy(cdev);             \
265         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
266         struct sas_internal *i = to_sas_internal(shost->transportt);    \
267         int error;                                                      \
268                                                                         \
269         if (!phy->local_attached)                                       \
270                 return -EINVAL;                                         \
271                                                                         \
272         error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;   \
273         if (error)                                                      \
274                 return error;                                           \
275         return snprintf(buf, 20, "%u\n", phy->field);                   \
276 }
277
278 #define sas_phy_linkerror_attr(field)                                   \
279         sas_phy_show_linkerror(field)                                   \
280 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
281
282
283 static ssize_t
284 show_sas_device_type(struct class_device *cdev, char *buf)
285 {
286         struct sas_phy *phy = transport_class_to_phy(cdev);
287
288         if (!phy->identify.device_type)
289                 return snprintf(buf, 20, "none\n");
290         return get_sas_device_type_names(phy->identify.device_type, buf);
291 }
292 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
293
294 static ssize_t do_sas_phy_reset(struct class_device *cdev,
295                 size_t count, int hard_reset)
296 {
297         struct sas_phy *phy = transport_class_to_phy(cdev);
298         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
299         struct sas_internal *i = to_sas_internal(shost->transportt);
300         int error;
301
302         if (!phy->local_attached)
303                 return -EINVAL;
304
305         error = i->f->phy_reset(phy, hard_reset);
306         if (error)
307                 return error;
308         return count;
309 };
310
311 static ssize_t store_sas_link_reset(struct class_device *cdev,
312                 const char *buf, size_t count)
313 {
314         return do_sas_phy_reset(cdev, count, 0);
315 }
316 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
317
318 static ssize_t store_sas_hard_reset(struct class_device *cdev,
319                 const char *buf, size_t count)
320 {
321         return do_sas_phy_reset(cdev, count, 1);
322 }
323 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
324
325 sas_phy_protocol_attr(identify.initiator_port_protocols,
326                 initiator_port_protocols);
327 sas_phy_protocol_attr(identify.target_port_protocols,
328                 target_port_protocols);
329 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
330                 unsigned long long);
331 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
332 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
333 sas_phy_linkspeed_attr(negotiated_linkrate);
334 sas_phy_linkspeed_attr(minimum_linkrate_hw);
335 sas_phy_linkspeed_attr(minimum_linkrate);
336 sas_phy_linkspeed_attr(maximum_linkrate_hw);
337 sas_phy_linkspeed_attr(maximum_linkrate);
338 sas_phy_linkerror_attr(invalid_dword_count);
339 sas_phy_linkerror_attr(running_disparity_error_count);
340 sas_phy_linkerror_attr(loss_of_dword_sync_count);
341 sas_phy_linkerror_attr(phy_reset_problem_count);
342
343
344 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
345                 "sas_phy", NULL, NULL, NULL);
346
347 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
348 {
349         struct Scsi_Host *shost;
350         struct sas_internal *i;
351
352         if (!scsi_is_sas_phy(dev))
353                 return 0;
354         shost = dev_to_shost(dev->parent);
355
356         if (!shost->transportt)
357                 return 0;
358         if (shost->transportt->host_attrs.ac.class !=
359                         &sas_host_class.class)
360                 return 0;
361
362         i = to_sas_internal(shost->transportt);
363         return &i->phy_attr_cont.ac == cont;
364 }
365
366 static void sas_phy_release(struct device *dev)
367 {
368         struct sas_phy *phy = dev_to_phy(dev);
369
370         put_device(dev->parent);
371         kfree(phy);
372 }
373
374 /**
375  * sas_phy_alloc  --  allocates and initialize a SAS PHY structure
376  * @parent:     Parent device
377  * @number:     Phy index
378  *
379  * Allocates an SAS PHY structure.  It will be added in the device tree
380  * below the device specified by @parent, which has to be either a Scsi_Host
381  * or sas_rphy.
382  *
383  * Returns:
384  *      SAS PHY allocated or %NULL if the allocation failed.
385  */
386 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
387 {
388         struct Scsi_Host *shost = dev_to_shost(parent);
389         struct sas_phy *phy;
390
391         phy = kzalloc(sizeof(*phy), GFP_KERNEL);
392         if (!phy)
393                 return NULL;
394
395         phy->number = number;
396
397         device_initialize(&phy->dev);
398         phy->dev.parent = get_device(parent);
399         phy->dev.release = sas_phy_release;
400         INIT_LIST_HEAD(&phy->port_siblings);
401         if (scsi_is_sas_expander_device(parent)) {
402                 struct sas_rphy *rphy = dev_to_rphy(parent);
403                 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
404                         rphy->scsi_target_id, number);
405         } else
406                 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
407
408         transport_setup_device(&phy->dev);
409
410         return phy;
411 }
412 EXPORT_SYMBOL(sas_phy_alloc);
413
414 /**
415  * sas_phy_add  --  add a SAS PHY to the device hierachy
416  * @phy:        The PHY to be added
417  *
418  * Publishes a SAS PHY to the rest of the system.
419  */
420 int sas_phy_add(struct sas_phy *phy)
421 {
422         int error;
423
424         error = device_add(&phy->dev);
425         if (!error) {
426                 transport_add_device(&phy->dev);
427                 transport_configure_device(&phy->dev);
428         }
429
430         return error;
431 }
432 EXPORT_SYMBOL(sas_phy_add);
433
434 /**
435  * sas_phy_free  --  free a SAS PHY
436  * @phy:        SAS PHY to free
437  *
438  * Frees the specified SAS PHY.
439  *
440  * Note:
441  *   This function must only be called on a PHY that has not
442  *   sucessfully been added using sas_phy_add().
443  */
444 void sas_phy_free(struct sas_phy *phy)
445 {
446         transport_destroy_device(&phy->dev);
447         put_device(&phy->dev);
448 }
449 EXPORT_SYMBOL(sas_phy_free);
450
451 /**
452  * sas_phy_delete  --  remove SAS PHY
453  * @phy:        SAS PHY to remove
454  *
455  * Removes the specified SAS PHY.  If the SAS PHY has an
456  * associated remote PHY it is removed before.
457  */
458 void
459 sas_phy_delete(struct sas_phy *phy)
460 {
461         struct device *dev = &phy->dev;
462
463         /* this happens if the phy is still part of a port when deleted */
464         BUG_ON(!list_empty(&phy->port_siblings));
465
466         transport_remove_device(dev);
467         device_del(dev);
468         transport_destroy_device(dev);
469         put_device(dev);
470 }
471 EXPORT_SYMBOL(sas_phy_delete);
472
473 /**
474  * scsi_is_sas_phy  --  check if a struct device represents a SAS PHY
475  * @dev:        device to check
476  *
477  * Returns:
478  *      %1 if the device represents a SAS PHY, %0 else
479  */
480 int scsi_is_sas_phy(const struct device *dev)
481 {
482         return dev->release == sas_phy_release;
483 }
484 EXPORT_SYMBOL(scsi_is_sas_phy);
485
486 /*
487  * SAS Port attributes
488  */
489 #define sas_port_show_simple(field, name, format_string, cast)          \
490 static ssize_t                                                          \
491 show_sas_port_##name(struct class_device *cdev, char *buf)              \
492 {                                                                       \
493         struct sas_port *port = transport_class_to_sas_port(cdev);      \
494                                                                         \
495         return snprintf(buf, 20, format_string, cast port->field);      \
496 }
497
498 #define sas_port_simple_attr(field, name, format_string, type)          \
499         sas_port_show_simple(field, name, format_string, (type))        \
500 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
501
502 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
503
504 static DECLARE_TRANSPORT_CLASS(sas_port_class,
505                                "sas_port", NULL, NULL, NULL);
506
507 static int sas_port_match(struct attribute_container *cont, struct device *dev)
508 {
509         struct Scsi_Host *shost;
510         struct sas_internal *i;
511
512         if (!scsi_is_sas_port(dev))
513                 return 0;
514         shost = dev_to_shost(dev->parent);
515
516         if (!shost->transportt)
517                 return 0;
518         if (shost->transportt->host_attrs.ac.class !=
519                         &sas_host_class.class)
520                 return 0;
521
522         i = to_sas_internal(shost->transportt);
523         return &i->port_attr_cont.ac == cont;
524 }
525
526
527 static void sas_port_release(struct device *dev)
528 {
529         struct sas_port *port = dev_to_sas_port(dev);
530
531         BUG_ON(!list_empty(&port->phy_list));
532
533         put_device(dev->parent);
534         kfree(port);
535 }
536
537 static void sas_port_create_link(struct sas_port *port,
538                                  struct sas_phy *phy)
539 {
540         sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
541         sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
542 }
543
544 static void sas_port_delete_link(struct sas_port *port,
545                                  struct sas_phy *phy)
546 {
547         sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
548         sysfs_remove_link(&phy->dev.kobj, "port");
549 }
550
551 /** sas_port_alloc - allocate and initialize a SAS port structure
552  *
553  * @parent:     parent device
554  * @port_id:    port number
555  *
556  * Allocates a SAS port structure.  It will be added to the device tree
557  * below the device specified by @parent which must be either a Scsi_Host
558  * or a sas_expander_device.
559  *
560  * Returns %NULL on error
561  */
562 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
563 {
564         struct Scsi_Host *shost = dev_to_shost(parent);
565         struct sas_port *port;
566
567         port = kzalloc(sizeof(*port), GFP_KERNEL);
568         if (!port)
569                 return NULL;
570
571         port->port_identifier = port_id;
572
573         device_initialize(&port->dev);
574
575         port->dev.parent = get_device(parent);
576         port->dev.release = sas_port_release;
577
578         mutex_init(&port->phy_list_mutex);
579         INIT_LIST_HEAD(&port->phy_list);
580
581         if (scsi_is_sas_expander_device(parent)) {
582                 struct sas_rphy *rphy = dev_to_rphy(parent);
583                 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
584                         rphy->scsi_target_id, port->port_identifier);
585         } else
586                 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
587                         port->port_identifier);
588
589         transport_setup_device(&port->dev);
590
591         return port;
592 }
593 EXPORT_SYMBOL(sas_port_alloc);
594
595 /** sas_port_alloc_num - allocate and initialize a SAS port structure
596  *
597  * @parent:     parent device
598  *
599  * Allocates a SAS port structure and a number to go with it.  This
600  * interface is really for adapters where the port number has no
601  * meansing, so the sas class should manage them.  It will be added to
602  * the device tree below the device specified by @parent which must be
603  * either a Scsi_Host or a sas_expander_device.
604  *
605  * Returns %NULL on error
606  */
607 struct sas_port *sas_port_alloc_num(struct device *parent)
608 {
609         int index;
610         struct Scsi_Host *shost = dev_to_shost(parent);
611         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
612
613         /* FIXME: use idr for this eventually */
614         mutex_lock(&sas_host->lock);
615         if (scsi_is_sas_expander_device(parent)) {
616                 struct sas_rphy *rphy = dev_to_rphy(parent);
617                 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
618
619                 index = exp->next_port_id++;
620         } else
621                 index = sas_host->next_port_id++;
622         mutex_unlock(&sas_host->lock);
623         return sas_port_alloc(parent, index);
624 }
625 EXPORT_SYMBOL(sas_port_alloc_num);
626
627 /**
628  * sas_port_add - add a SAS port to the device hierarchy
629  *
630  * @port:       port to be added
631  *
632  * publishes a port to the rest of the system
633  */
634 int sas_port_add(struct sas_port *port)
635 {
636         int error;
637
638         /* No phys should be added until this is made visible */
639         BUG_ON(!list_empty(&port->phy_list));
640
641         error = device_add(&port->dev);
642
643         if (error)
644                 return error;
645
646         transport_add_device(&port->dev);
647         transport_configure_device(&port->dev);
648
649         return 0;
650 }
651 EXPORT_SYMBOL(sas_port_add);
652
653 /**
654  * sas_port_free  --  free a SAS PORT
655  * @port:       SAS PORT to free
656  *
657  * Frees the specified SAS PORT.
658  *
659  * Note:
660  *   This function must only be called on a PORT that has not
661  *   sucessfully been added using sas_port_add().
662  */
663 void sas_port_free(struct sas_port *port)
664 {
665         transport_destroy_device(&port->dev);
666         put_device(&port->dev);
667 }
668 EXPORT_SYMBOL(sas_port_free);
669
670 /**
671  * sas_port_delete  --  remove SAS PORT
672  * @port:       SAS PORT to remove
673  *
674  * Removes the specified SAS PORT.  If the SAS PORT has an
675  * associated phys, unlink them from the port as well.
676  */
677 void sas_port_delete(struct sas_port *port)
678 {
679         struct device *dev = &port->dev;
680         struct sas_phy *phy, *tmp_phy;
681
682         if (port->rphy) {
683                 sas_rphy_delete(port->rphy);
684                 port->rphy = NULL;
685         }
686
687         mutex_lock(&port->phy_list_mutex);
688         list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
689                                  port_siblings) {
690                 sas_port_delete_link(port, phy);
691                 list_del_init(&phy->port_siblings);
692         }
693         mutex_unlock(&port->phy_list_mutex);
694
695         if (port->is_backlink) {
696                 struct device *parent = port->dev.parent;
697
698                 sysfs_remove_link(&port->dev.kobj, parent->bus_id);
699                 port->is_backlink = 0;
700         }
701
702         transport_remove_device(dev);
703         device_del(dev);
704         transport_destroy_device(dev);
705         put_device(dev);
706 }
707 EXPORT_SYMBOL(sas_port_delete);
708
709 /**
710  * scsi_is_sas_port --  check if a struct device represents a SAS port
711  * @dev:        device to check
712  *
713  * Returns:
714  *      %1 if the device represents a SAS Port, %0 else
715  */
716 int scsi_is_sas_port(const struct device *dev)
717 {
718         return dev->release == sas_port_release;
719 }
720 EXPORT_SYMBOL(scsi_is_sas_port);
721
722 /**
723  * sas_port_add_phy - add another phy to a port to form a wide port
724  * @port:       port to add the phy to
725  * @phy:        phy to add
726  *
727  * When a port is initially created, it is empty (has no phys).  All
728  * ports must have at least one phy to operated, and all wide ports
729  * must have at least two.  The current code makes no difference
730  * between ports and wide ports, but the only object that can be
731  * connected to a remote device is a port, so ports must be formed on
732  * all devices with phys if they're connected to anything.
733  */
734 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
735 {
736         mutex_lock(&port->phy_list_mutex);
737         if (unlikely(!list_empty(&phy->port_siblings))) {
738                 /* make sure we're already on this port */
739                 struct sas_phy *tmp;
740
741                 list_for_each_entry(tmp, &port->phy_list, port_siblings)
742                         if (tmp == phy)
743                                 break;
744                 /* If this trips, you added a phy that was already
745                  * part of a different port */
746                 if (unlikely(tmp != phy)) {
747                         dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
748                         BUG();
749                 }
750         } else {
751                 sas_port_create_link(port, phy);
752                 list_add_tail(&phy->port_siblings, &port->phy_list);
753                 port->num_phys++;
754         }
755         mutex_unlock(&port->phy_list_mutex);
756 }
757 EXPORT_SYMBOL(sas_port_add_phy);
758
759 /**
760  * sas_port_delete_phy - remove a phy from a port or wide port
761  * @port:       port to remove the phy from
762  * @phy:        phy to remove
763  *
764  * This operation is used for tearing down ports again.  It must be
765  * done to every port or wide port before calling sas_port_delete.
766  */
767 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
768 {
769         mutex_lock(&port->phy_list_mutex);
770         sas_port_delete_link(port, phy);
771         list_del_init(&phy->port_siblings);
772         port->num_phys--;
773         mutex_unlock(&port->phy_list_mutex);
774 }
775 EXPORT_SYMBOL(sas_port_delete_phy);
776
777 void sas_port_mark_backlink(struct sas_port *port)
778 {
779         struct device *parent = port->dev.parent->parent->parent;
780
781         if (port->is_backlink)
782                 return;
783         port->is_backlink = 1;
784         sysfs_create_link(&port->dev.kobj, &parent->kobj,
785                           parent->bus_id);
786
787 }
788 EXPORT_SYMBOL(sas_port_mark_backlink);
789
790 /*
791  * SAS remote PHY attributes.
792  */
793
794 #define sas_rphy_show_simple(field, name, format_string, cast)          \
795 static ssize_t                                                          \
796 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
797 {                                                                       \
798         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
799                                                                         \
800         return snprintf(buf, 20, format_string, cast rphy->field);      \
801 }
802
803 #define sas_rphy_simple_attr(field, name, format_string, type)          \
804         sas_rphy_show_simple(field, name, format_string, (type))        \
805 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
806                 show_sas_rphy_##name, NULL)
807
808 #define sas_rphy_show_protocol(field, name)                             \
809 static ssize_t                                                          \
810 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
811 {                                                                       \
812         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
813                                                                         \
814         if (!rphy->field)                                       \
815                 return snprintf(buf, 20, "none\n");                     \
816         return get_sas_protocol_names(rphy->field, buf);        \
817 }
818
819 #define sas_rphy_protocol_attr(field, name)                             \
820         sas_rphy_show_protocol(field, name)                             \
821 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
822                 show_sas_rphy_##name, NULL)
823
824 static ssize_t
825 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
826 {
827         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
828
829         if (!rphy->identify.device_type)
830                 return snprintf(buf, 20, "none\n");
831         return get_sas_device_type_names(
832                         rphy->identify.device_type, buf);
833 }
834
835 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
836                 show_sas_rphy_device_type, NULL);
837
838 static ssize_t
839 show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
840 {
841         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
842         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
843         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
844         struct sas_internal *i = to_sas_internal(shost->transportt);
845         u64 identifier;
846         int error;
847
848         /*
849          * Only devices behind an expander are supported, because the
850          * enclosure identifier is a SMP feature.
851          */
852         if (phy->local_attached)
853                 return -EINVAL;
854
855         error = i->f->get_enclosure_identifier(rphy, &identifier);
856         if (error)
857                 return error;
858         return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
859 }
860
861 static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
862                 show_sas_rphy_enclosure_identifier, NULL);
863
864 static ssize_t
865 show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
866 {
867         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
868         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
869         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
870         struct sas_internal *i = to_sas_internal(shost->transportt);
871         int val;
872
873         if (phy->local_attached)
874                 return -EINVAL;
875
876         val = i->f->get_bay_identifier(rphy);
877         if (val < 0)
878                 return val;
879         return sprintf(buf, "%d\n", val);
880 }
881
882 static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
883                 show_sas_rphy_bay_identifier, NULL);
884
885 sas_rphy_protocol_attr(identify.initiator_port_protocols,
886                 initiator_port_protocols);
887 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
888 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
889                 unsigned long long);
890 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
891
892 /* only need 8 bytes of data plus header (4 or 8) */
893 #define BUF_SIZE 64
894
895 int sas_read_port_mode_page(struct scsi_device *sdev)
896 {
897         char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
898         struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
899         struct sas_end_device *rdev;
900         struct scsi_mode_data mode_data;
901         int res, error;
902
903         BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
904
905         rdev = rphy_to_end_device(rphy);
906
907         if (!buffer)
908                 return -ENOMEM;
909
910         res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
911                               &mode_data, NULL);
912
913         error = -EINVAL;
914         if (!scsi_status_is_good(res))
915                 goto out;
916
917         msdata = buffer +  mode_data.header_length +
918                 mode_data.block_descriptor_length;
919
920         if (msdata - buffer > BUF_SIZE - 8)
921                 goto out;
922
923         error = 0;
924
925         rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
926         rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
927         rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
928
929  out:
930         kfree(buffer);
931         return error;
932 }
933 EXPORT_SYMBOL(sas_read_port_mode_page);
934
935 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
936                                "sas_end_device", NULL, NULL, NULL);
937
938 #define sas_end_dev_show_simple(field, name, format_string, cast)       \
939 static ssize_t                                                          \
940 show_sas_end_dev_##name(struct class_device *cdev, char *buf)           \
941 {                                                                       \
942         struct sas_rphy *rphy = transport_class_to_rphy(cdev);          \
943         struct sas_end_device *rdev = rphy_to_end_device(rphy);         \
944                                                                         \
945         return snprintf(buf, 20, format_string, cast rdev->field);      \
946 }
947
948 #define sas_end_dev_simple_attr(field, name, format_string, type)       \
949         sas_end_dev_show_simple(field, name, format_string, (type))     \
950 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO,                    \
951                 show_sas_end_dev_##name, NULL)
952
953 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
954 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
955                         "%d\n", int);
956 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
957                         "%d\n", int);
958
959 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
960                                "sas_expander", NULL, NULL, NULL);
961
962 #define sas_expander_show_simple(field, name, format_string, cast)      \
963 static ssize_t                                                          \
964 show_sas_expander_##name(struct class_device *cdev, char *buf)          \
965 {                                                                       \
966         struct sas_rphy *rphy = transport_class_to_rphy(cdev);          \
967         struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
968                                                                         \
969         return snprintf(buf, 20, format_string, cast edev->field);      \
970 }
971
972 #define sas_expander_simple_attr(field, name, format_string, type)      \
973         sas_expander_show_simple(field, name, format_string, (type))    \
974 static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO,                   \
975                 show_sas_expander_##name, NULL)
976
977 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
978 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
979 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
980 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
981                          "%s\n", char *);
982 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
983 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
984                          unsigned int);
985 sas_expander_simple_attr(level, level, "%d\n", int);
986
987 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
988                 "sas_device", NULL, NULL, NULL);
989
990 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
991 {
992         struct Scsi_Host *shost;
993         struct sas_internal *i;
994
995         if (!scsi_is_sas_rphy(dev))
996                 return 0;
997         shost = dev_to_shost(dev->parent->parent);
998
999         if (!shost->transportt)
1000                 return 0;
1001         if (shost->transportt->host_attrs.ac.class !=
1002                         &sas_host_class.class)
1003                 return 0;
1004
1005         i = to_sas_internal(shost->transportt);
1006         return &i->rphy_attr_cont.ac == cont;
1007 }
1008
1009 static int sas_end_dev_match(struct attribute_container *cont,
1010                              struct device *dev)
1011 {
1012         struct Scsi_Host *shost;
1013         struct sas_internal *i;
1014         struct sas_rphy *rphy;
1015
1016         if (!scsi_is_sas_rphy(dev))
1017                 return 0;
1018         shost = dev_to_shost(dev->parent->parent);
1019         rphy = dev_to_rphy(dev);
1020
1021         if (!shost->transportt)
1022                 return 0;
1023         if (shost->transportt->host_attrs.ac.class !=
1024                         &sas_host_class.class)
1025                 return 0;
1026
1027         i = to_sas_internal(shost->transportt);
1028         return &i->end_dev_attr_cont.ac == cont &&
1029                 rphy->identify.device_type == SAS_END_DEVICE;
1030 }
1031
1032 static int sas_expander_match(struct attribute_container *cont,
1033                               struct device *dev)
1034 {
1035         struct Scsi_Host *shost;
1036         struct sas_internal *i;
1037         struct sas_rphy *rphy;
1038
1039         if (!scsi_is_sas_rphy(dev))
1040                 return 0;
1041         shost = dev_to_shost(dev->parent->parent);
1042         rphy = dev_to_rphy(dev);
1043
1044         if (!shost->transportt)
1045                 return 0;
1046         if (shost->transportt->host_attrs.ac.class !=
1047                         &sas_host_class.class)
1048                 return 0;
1049
1050         i = to_sas_internal(shost->transportt);
1051         return &i->expander_attr_cont.ac == cont &&
1052                 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1053                  rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1054 }
1055
1056 static void sas_expander_release(struct device *dev)
1057 {
1058         struct sas_rphy *rphy = dev_to_rphy(dev);
1059         struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1060
1061         put_device(dev->parent);
1062         kfree(edev);
1063 }
1064
1065 static void sas_end_device_release(struct device *dev)
1066 {
1067         struct sas_rphy *rphy = dev_to_rphy(dev);
1068         struct sas_end_device *edev = rphy_to_end_device(rphy);
1069
1070         put_device(dev->parent);
1071         kfree(edev);
1072 }
1073
1074 /**
1075  * sas_rphy_initialize - common rphy intialization
1076  * @rphy:       rphy to initialise
1077  *
1078  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1079  * initialise the common rphy component of each.
1080  */
1081 static void sas_rphy_initialize(struct sas_rphy *rphy)
1082 {
1083         INIT_LIST_HEAD(&rphy->list);
1084 }
1085
1086 /**
1087  * sas_end_device_alloc - allocate an rphy for an end device
1088  *
1089  * Allocates an SAS remote PHY structure, connected to @parent.
1090  *
1091  * Returns:
1092  *      SAS PHY allocated or %NULL if the allocation failed.
1093  */
1094 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1095 {
1096         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1097         struct sas_end_device *rdev;
1098
1099         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1100         if (!rdev) {
1101                 return NULL;
1102         }
1103
1104         device_initialize(&rdev->rphy.dev);
1105         rdev->rphy.dev.parent = get_device(&parent->dev);
1106         rdev->rphy.dev.release = sas_end_device_release;
1107         if (scsi_is_sas_expander_device(parent->dev.parent)) {
1108                 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1109                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
1110                         shost->host_no, rphy->scsi_target_id, parent->port_identifier);
1111         } else
1112                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
1113                         shost->host_no, parent->port_identifier);
1114         rdev->rphy.identify.device_type = SAS_END_DEVICE;
1115         sas_rphy_initialize(&rdev->rphy);
1116         transport_setup_device(&rdev->rphy.dev);
1117
1118         return &rdev->rphy;
1119 }
1120 EXPORT_SYMBOL(sas_end_device_alloc);
1121
1122 /**
1123  * sas_expander_alloc - allocate an rphy for an end device
1124  *
1125  * Allocates an SAS remote PHY structure, connected to @parent.
1126  *
1127  * Returns:
1128  *      SAS PHY allocated or %NULL if the allocation failed.
1129  */
1130 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1131                                     enum sas_device_type type)
1132 {
1133         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1134         struct sas_expander_device *rdev;
1135         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1136
1137         BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1138                type != SAS_FANOUT_EXPANDER_DEVICE);
1139
1140         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1141         if (!rdev) {
1142                 return NULL;
1143         }
1144
1145         device_initialize(&rdev->rphy.dev);
1146         rdev->rphy.dev.parent = get_device(&parent->dev);
1147         rdev->rphy.dev.release = sas_expander_release;
1148         mutex_lock(&sas_host->lock);
1149         rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1150         mutex_unlock(&sas_host->lock);
1151         sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
1152                 shost->host_no, rdev->rphy.scsi_target_id);
1153         rdev->rphy.identify.device_type = type;
1154         sas_rphy_initialize(&rdev->rphy);
1155         transport_setup_device(&rdev->rphy.dev);
1156
1157         return &rdev->rphy;
1158 }
1159 EXPORT_SYMBOL(sas_expander_alloc);
1160
1161 /**
1162  * sas_rphy_add  --  add a SAS remote PHY to the device hierachy
1163  * @rphy:       The remote PHY to be added
1164  *
1165  * Publishes a SAS remote PHY to the rest of the system.
1166  */
1167 int sas_rphy_add(struct sas_rphy *rphy)
1168 {
1169         struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1170         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1171         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1172         struct sas_identify *identify = &rphy->identify;
1173         int error;
1174
1175         if (parent->rphy)
1176                 return -ENXIO;
1177         parent->rphy = rphy;
1178
1179         error = device_add(&rphy->dev);
1180         if (error)
1181                 return error;
1182         transport_add_device(&rphy->dev);
1183         transport_configure_device(&rphy->dev);
1184
1185         mutex_lock(&sas_host->lock);
1186         list_add_tail(&rphy->list, &sas_host->rphy_list);
1187         if (identify->device_type == SAS_END_DEVICE &&
1188             (identify->target_port_protocols &
1189              (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1190                 rphy->scsi_target_id = sas_host->next_target_id++;
1191         else if (identify->device_type == SAS_END_DEVICE)
1192                 rphy->scsi_target_id = -1;
1193         mutex_unlock(&sas_host->lock);
1194
1195         if (identify->device_type == SAS_END_DEVICE &&
1196             rphy->scsi_target_id != -1) {
1197                 scsi_scan_target(&rphy->dev, 0,
1198                                 rphy->scsi_target_id, ~0, 0);
1199         }
1200
1201         return 0;
1202 }
1203 EXPORT_SYMBOL(sas_rphy_add);
1204
1205 /**
1206  * sas_rphy_free  --  free a SAS remote PHY
1207  * @rphy        SAS remote PHY to free
1208  *
1209  * Frees the specified SAS remote PHY.
1210  *
1211  * Note:
1212  *   This function must only be called on a remote
1213  *   PHY that has not sucessfully been added using
1214  *   sas_rphy_add().
1215  */
1216 void sas_rphy_free(struct sas_rphy *rphy)
1217 {
1218         struct device *dev = &rphy->dev;
1219         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1220         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1221
1222         mutex_lock(&sas_host->lock);
1223         list_del(&rphy->list);
1224         mutex_unlock(&sas_host->lock);
1225
1226         transport_destroy_device(dev);
1227
1228         put_device(dev);
1229 }
1230 EXPORT_SYMBOL(sas_rphy_free);
1231
1232 /**
1233  * sas_rphy_delete  --  remove SAS remote PHY
1234  * @rphy:       SAS remote PHY to remove
1235  *
1236  * Removes the specified SAS remote PHY.
1237  */
1238 void
1239 sas_rphy_delete(struct sas_rphy *rphy)
1240 {
1241         struct device *dev = &rphy->dev;
1242         struct sas_port *parent = dev_to_sas_port(dev->parent);
1243         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1244         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1245
1246         switch (rphy->identify.device_type) {
1247         case SAS_END_DEVICE:
1248                 scsi_remove_target(dev);
1249                 break;
1250         case SAS_EDGE_EXPANDER_DEVICE:
1251         case SAS_FANOUT_EXPANDER_DEVICE:
1252                 sas_remove_children(dev);
1253                 break;
1254         default:
1255                 break;
1256         }
1257
1258         transport_remove_device(dev);
1259         device_del(dev);
1260         transport_destroy_device(dev);
1261
1262         mutex_lock(&sas_host->lock);
1263         list_del(&rphy->list);
1264         mutex_unlock(&sas_host->lock);
1265
1266         parent->rphy = NULL;
1267
1268         put_device(dev);
1269 }
1270 EXPORT_SYMBOL(sas_rphy_delete);
1271
1272 /**
1273  * scsi_is_sas_rphy  --  check if a struct device represents a SAS remote PHY
1274  * @dev:        device to check
1275  *
1276  * Returns:
1277  *      %1 if the device represents a SAS remote PHY, %0 else
1278  */
1279 int scsi_is_sas_rphy(const struct device *dev)
1280 {
1281         return dev->release == sas_end_device_release ||
1282                 dev->release == sas_expander_release;
1283 }
1284 EXPORT_SYMBOL(scsi_is_sas_rphy);
1285
1286
1287 /*
1288  * SCSI scan helper
1289  */
1290
1291 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1292                 uint id, uint lun)
1293 {
1294         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1295         struct sas_rphy *rphy;
1296
1297         mutex_lock(&sas_host->lock);
1298         list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1299                 if (rphy->identify.device_type != SAS_END_DEVICE ||
1300                     rphy->scsi_target_id == -1)
1301                         continue;
1302
1303                 if ((channel == SCAN_WILD_CARD || channel == 0) &&
1304                     (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1305                         scsi_scan_target(&rphy->dev, 0,
1306                                          rphy->scsi_target_id, lun, 1);
1307                 }
1308         }
1309         mutex_unlock(&sas_host->lock);
1310
1311         return 0;
1312 }
1313
1314
1315 /*
1316  * Setup / Teardown code
1317  */
1318
1319 #define SETUP_TEMPLATE(attrb, field, perm, test)                                \
1320         i->private_##attrb[count] = class_device_attr_##field;          \
1321         i->private_##attrb[count].attr.mode = perm;                     \
1322         i->attrb[count] = &i->private_##attrb[count];                   \
1323         if (test)                                                       \
1324                 count++
1325
1326
1327 #define SETUP_RPORT_ATTRIBUTE(field)                                    \
1328         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1329
1330 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)                     \
1331         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1332
1333 #define SETUP_PHY_ATTRIBUTE(field)                                      \
1334         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1335
1336 #define SETUP_PORT_ATTRIBUTE(field)                                     \
1337         SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1338
1339 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)                       \
1340         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1341
1342 #define SETUP_PHY_ATTRIBUTE_WRONLY(field)                               \
1343         SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
1344
1345 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)                \
1346         SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
1347
1348 #define SETUP_END_DEV_ATTRIBUTE(field)                                  \
1349         SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1350
1351 #define SETUP_EXPANDER_ATTRIBUTE(field)                                 \
1352         SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1353
1354 /**
1355  * sas_attach_transport  --  instantiate SAS transport template
1356  * @ft:         SAS transport class function template
1357  */
1358 struct scsi_transport_template *
1359 sas_attach_transport(struct sas_function_template *ft)
1360 {
1361         struct sas_internal *i;
1362         int count;
1363
1364         i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1365         if (!i)
1366                 return NULL;
1367
1368         i->t.user_scan = sas_user_scan;
1369
1370         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1371         i->t.host_attrs.ac.class = &sas_host_class.class;
1372         i->t.host_attrs.ac.match = sas_host_match;
1373         transport_container_register(&i->t.host_attrs);
1374         i->t.host_size = sizeof(struct sas_host_attrs);
1375
1376         i->phy_attr_cont.ac.class = &sas_phy_class.class;
1377         i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1378         i->phy_attr_cont.ac.match = sas_phy_match;
1379         transport_container_register(&i->phy_attr_cont);
1380
1381         i->port_attr_cont.ac.class = &sas_port_class.class;
1382         i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1383         i->port_attr_cont.ac.match = sas_port_match;
1384         transport_container_register(&i->port_attr_cont);
1385
1386         i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1387         i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1388         i->rphy_attr_cont.ac.match = sas_rphy_match;
1389         transport_container_register(&i->rphy_attr_cont);
1390
1391         i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1392         i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1393         i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1394         transport_container_register(&i->end_dev_attr_cont);
1395
1396         i->expander_attr_cont.ac.class = &sas_expander_class.class;
1397         i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1398         i->expander_attr_cont.ac.match = sas_expander_match;
1399         transport_container_register(&i->expander_attr_cont);
1400
1401         i->f = ft;
1402
1403         count = 0;
1404         SETUP_PORT_ATTRIBUTE(num_phys);
1405         i->host_attrs[count] = NULL;
1406
1407         count = 0;
1408         SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1409         SETUP_PHY_ATTRIBUTE(target_port_protocols);
1410         SETUP_PHY_ATTRIBUTE(device_type);
1411         SETUP_PHY_ATTRIBUTE(sas_address);
1412         SETUP_PHY_ATTRIBUTE(phy_identifier);
1413         //SETUP_PHY_ATTRIBUTE(port_identifier);
1414         SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1415         SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1416         SETUP_PHY_ATTRIBUTE(minimum_linkrate);
1417         SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1418         SETUP_PHY_ATTRIBUTE(maximum_linkrate);
1419
1420         SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1421         SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1422         SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1423         SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1424         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1425         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1426         i->phy_attrs[count] = NULL;
1427
1428         count = 0;
1429         SETUP_PORT_ATTRIBUTE(num_phys);
1430         i->port_attrs[count] = NULL;
1431
1432         count = 0;
1433         SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1434         SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1435         SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1436         SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1437         SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1438         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1439                                        get_enclosure_identifier);
1440         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1441                                        get_bay_identifier);
1442         i->rphy_attrs[count] = NULL;
1443
1444         count = 0;
1445         SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1446         SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1447         SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1448         i->end_dev_attrs[count] = NULL;
1449
1450         count = 0;
1451         SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1452         SETUP_EXPANDER_ATTRIBUTE(product_id);
1453         SETUP_EXPANDER_ATTRIBUTE(product_rev);
1454         SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1455         SETUP_EXPANDER_ATTRIBUTE(component_id);
1456         SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1457         SETUP_EXPANDER_ATTRIBUTE(level);
1458         i->expander_attrs[count] = NULL;
1459
1460         return &i->t;
1461 }
1462 EXPORT_SYMBOL(sas_attach_transport);
1463
1464 /**
1465  * sas_release_transport  --  release SAS transport template instance
1466  * @t:          transport template instance
1467  */
1468 void sas_release_transport(struct scsi_transport_template *t)
1469 {
1470         struct sas_internal *i = to_sas_internal(t);
1471
1472         transport_container_unregister(&i->t.host_attrs);
1473         transport_container_unregister(&i->phy_attr_cont);
1474         transport_container_unregister(&i->port_attr_cont);
1475         transport_container_unregister(&i->rphy_attr_cont);
1476         transport_container_unregister(&i->end_dev_attr_cont);
1477         transport_container_unregister(&i->expander_attr_cont);
1478
1479         kfree(i);
1480 }
1481 EXPORT_SYMBOL(sas_release_transport);
1482
1483 static __init int sas_transport_init(void)
1484 {
1485         int error;
1486
1487         error = transport_class_register(&sas_host_class);
1488         if (error)
1489                 goto out;
1490         error = transport_class_register(&sas_phy_class);
1491         if (error)
1492                 goto out_unregister_transport;
1493         error = transport_class_register(&sas_port_class);
1494         if (error)
1495                 goto out_unregister_phy;
1496         error = transport_class_register(&sas_rphy_class);
1497         if (error)
1498                 goto out_unregister_port;
1499         error = transport_class_register(&sas_end_dev_class);
1500         if (error)
1501                 goto out_unregister_rphy;
1502         error = transport_class_register(&sas_expander_class);
1503         if (error)
1504                 goto out_unregister_end_dev;
1505
1506         return 0;
1507
1508  out_unregister_end_dev:
1509         transport_class_unregister(&sas_end_dev_class);
1510  out_unregister_rphy:
1511         transport_class_unregister(&sas_rphy_class);
1512  out_unregister_port:
1513         transport_class_unregister(&sas_port_class);
1514  out_unregister_phy:
1515         transport_class_unregister(&sas_phy_class);
1516  out_unregister_transport:
1517         transport_class_unregister(&sas_host_class);
1518  out:
1519         return error;
1520
1521 }
1522
1523 static void __exit sas_transport_exit(void)
1524 {
1525         transport_class_unregister(&sas_host_class);
1526         transport_class_unregister(&sas_phy_class);
1527         transport_class_unregister(&sas_port_class);
1528         transport_class_unregister(&sas_rphy_class);
1529         transport_class_unregister(&sas_end_dev_class);
1530         transport_class_unregister(&sas_expander_class);
1531 }
1532
1533 MODULE_AUTHOR("Christoph Hellwig");
1534 MODULE_DESCRIPTION("SAS Transphy Attributes");
1535 MODULE_LICENSE("GPL");
1536
1537 module_init(sas_transport_init);
1538 module_exit(sas_transport_exit);