]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/scsi_transport_sas.c
[PATCH] convert aic94xx over to using the sas transport end device
[linux-2.6-omap-h63xx.git] / 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
39 #define SAS_HOST_ATTRS          0
40 #define SAS_PORT_ATTRS          17
41 #define SAS_RPORT_ATTRS         7
42 #define SAS_END_DEV_ATTRS       3
43
44 struct sas_internal {
45         struct scsi_transport_template t;
46         struct sas_function_template *f;
47
48         struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
49         struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
50         struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
51         struct class_device_attribute private_end_dev_attrs[SAS_END_DEV_ATTRS];
52
53         struct transport_container phy_attr_cont;
54         struct transport_container rphy_attr_cont;
55         struct transport_container end_dev_attr_cont;
56
57         /*
58          * The array of null terminated pointers to attributes
59          * needed by scsi_sysfs.c
60          */
61         struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
62         struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
63         struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
64         struct class_device_attribute *end_dev_attrs[SAS_END_DEV_ATTRS + 1];
65 };
66 #define to_sas_internal(tmpl)   container_of(tmpl, struct sas_internal, t)
67
68 struct sas_host_attrs {
69         struct list_head rphy_list;
70         struct mutex lock;
71         u32 next_target_id;
72 };
73 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
74
75
76 /*
77  * Hack to allow attributes of the same name in different objects.
78  */
79 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
80         struct class_device_attribute class_device_attr_##_prefix##_##_name = \
81         __ATTR(_name,_mode,_show,_store)
82
83
84 /*
85  * Pretty printing helpers
86  */
87
88 #define sas_bitfield_name_match(title, table)                   \
89 static ssize_t                                                  \
90 get_sas_##title##_names(u32 table_key, char *buf)               \
91 {                                                               \
92         char *prefix = "";                                      \
93         ssize_t len = 0;                                        \
94         int i;                                                  \
95                                                                 \
96         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
97                 if (table[i].value & table_key) {               \
98                         len += sprintf(buf + len, "%s%s",       \
99                                 prefix, table[i].name);         \
100                         prefix = ", ";                          \
101                 }                                               \
102         }                                                       \
103         len += sprintf(buf + len, "\n");                        \
104         return len;                                             \
105 }
106
107 #define sas_bitfield_name_search(title, table)                  \
108 static ssize_t                                                  \
109 get_sas_##title##_names(u32 table_key, char *buf)               \
110 {                                                               \
111         ssize_t len = 0;                                        \
112         int i;                                                  \
113                                                                 \
114         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
115                 if (table[i].value == table_key) {              \
116                         len += sprintf(buf + len, "%s",         \
117                                 table[i].name);                 \
118                         break;                                  \
119                 }                                               \
120         }                                                       \
121         len += sprintf(buf + len, "\n");                        \
122         return len;                                             \
123 }
124
125 static struct {
126         u32             value;
127         char            *name;
128 } sas_device_type_names[] = {
129         { SAS_PHY_UNUSED,               "unused" },
130         { SAS_END_DEVICE,               "end device" },
131         { SAS_EDGE_EXPANDER_DEVICE,     "edge expander" },
132         { SAS_FANOUT_EXPANDER_DEVICE,   "fanout expander" },
133 };
134 sas_bitfield_name_search(device_type, sas_device_type_names)
135
136
137 static struct {
138         u32             value;
139         char            *name;
140 } sas_protocol_names[] = {
141         { SAS_PROTOCOL_SATA,            "sata" },
142         { SAS_PROTOCOL_SMP,             "smp" },
143         { SAS_PROTOCOL_STP,             "stp" },
144         { SAS_PROTOCOL_SSP,             "ssp" },
145 };
146 sas_bitfield_name_match(protocol, sas_protocol_names)
147
148 static struct {
149         u32             value;
150         char            *name;
151 } sas_linkspeed_names[] = {
152         { SAS_LINK_RATE_UNKNOWN,        "Unknown" },
153         { SAS_PHY_DISABLED,             "Phy disabled" },
154         { SAS_LINK_RATE_FAILED,         "Link Rate failed" },
155         { SAS_SATA_SPINUP_HOLD,         "Spin-up hold" },
156         { SAS_LINK_RATE_1_5_GBPS,       "1.5 Gbit" },
157         { SAS_LINK_RATE_3_0_GBPS,       "3.0 Gbit" },
158         { SAS_LINK_RATE_6_0_GBPS,       "6.0 Gbit" },
159 };
160 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
161
162
163 /*
164  * SAS host attributes
165  */
166
167 static int sas_host_setup(struct transport_container *tc, struct device *dev,
168                           struct class_device *cdev)
169 {
170         struct Scsi_Host *shost = dev_to_shost(dev);
171         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
172
173         INIT_LIST_HEAD(&sas_host->rphy_list);
174         mutex_init(&sas_host->lock);
175         sas_host->next_target_id = 0;
176         return 0;
177 }
178
179 static DECLARE_TRANSPORT_CLASS(sas_host_class,
180                 "sas_host", sas_host_setup, NULL, NULL);
181
182 static int sas_host_match(struct attribute_container *cont,
183                             struct device *dev)
184 {
185         struct Scsi_Host *shost;
186         struct sas_internal *i;
187
188         if (!scsi_is_host_device(dev))
189                 return 0;
190         shost = dev_to_shost(dev);
191
192         if (!shost->transportt)
193                 return 0;
194         if (shost->transportt->host_attrs.ac.class !=
195                         &sas_host_class.class)
196                 return 0;
197
198         i = to_sas_internal(shost->transportt);
199         return &i->t.host_attrs.ac == cont;
200 }
201
202 static int do_sas_phy_delete(struct device *dev, void *data)
203 {
204         if (scsi_is_sas_phy(dev))
205                 sas_phy_delete(dev_to_phy(dev));
206         return 0;
207 }
208
209 /**
210  * sas_remove_host  --  tear down a Scsi_Host's SAS data structures
211  * @shost:      Scsi Host that is torn down
212  *
213  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
214  * Must be called just before scsi_remove_host for SAS HBAs.
215  */
216 void sas_remove_host(struct Scsi_Host *shost)
217 {
218         device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
219 }
220 EXPORT_SYMBOL(sas_remove_host);
221
222
223 /*
224  * SAS Port attributes
225  */
226
227 #define sas_phy_show_simple(field, name, format_string, cast)           \
228 static ssize_t                                                          \
229 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
230 {                                                                       \
231         struct sas_phy *phy = transport_class_to_phy(cdev);             \
232                                                                         \
233         return snprintf(buf, 20, format_string, cast phy->field);       \
234 }
235
236 #define sas_phy_simple_attr(field, name, format_string, type)           \
237         sas_phy_show_simple(field, name, format_string, (type)) \
238 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
239
240 #define sas_phy_show_protocol(field, name)                              \
241 static ssize_t                                                          \
242 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
243 {                                                                       \
244         struct sas_phy *phy = transport_class_to_phy(cdev);             \
245                                                                         \
246         if (!phy->field)                                                \
247                 return snprintf(buf, 20, "none\n");                     \
248         return get_sas_protocol_names(phy->field, buf);         \
249 }
250
251 #define sas_phy_protocol_attr(field, name)                              \
252         sas_phy_show_protocol(field, name)                              \
253 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
254
255 #define sas_phy_show_linkspeed(field)                                   \
256 static ssize_t                                                          \
257 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
258 {                                                                       \
259         struct sas_phy *phy = transport_class_to_phy(cdev);             \
260                                                                         \
261         return get_sas_linkspeed_names(phy->field, buf);                \
262 }
263
264 #define sas_phy_linkspeed_attr(field)                                   \
265         sas_phy_show_linkspeed(field)                                   \
266 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
267
268 #define sas_phy_show_linkerror(field)                                   \
269 static ssize_t                                                          \
270 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
271 {                                                                       \
272         struct sas_phy *phy = transport_class_to_phy(cdev);             \
273         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
274         struct sas_internal *i = to_sas_internal(shost->transportt);    \
275         int error;                                                      \
276                                                                         \
277         if (!phy->local_attached)                                       \
278                 return -EINVAL;                                         \
279                                                                         \
280         error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;   \
281         if (error)                                                      \
282                 return error;                                           \
283         return snprintf(buf, 20, "%u\n", phy->field);                   \
284 }
285
286 #define sas_phy_linkerror_attr(field)                                   \
287         sas_phy_show_linkerror(field)                                   \
288 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
289
290
291 static ssize_t
292 show_sas_device_type(struct class_device *cdev, char *buf)
293 {
294         struct sas_phy *phy = transport_class_to_phy(cdev);
295
296         if (!phy->identify.device_type)
297                 return snprintf(buf, 20, "none\n");
298         return get_sas_device_type_names(phy->identify.device_type, buf);
299 }
300 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
301
302 static ssize_t do_sas_phy_reset(struct class_device *cdev,
303                 size_t count, int hard_reset)
304 {
305         struct sas_phy *phy = transport_class_to_phy(cdev);
306         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
307         struct sas_internal *i = to_sas_internal(shost->transportt);
308         int error;
309
310         if (!phy->local_attached)
311                 return -EINVAL;
312
313         error = i->f->phy_reset(phy, hard_reset);
314         if (error)
315                 return error;
316         return count;
317 };
318
319 static ssize_t store_sas_link_reset(struct class_device *cdev,
320                 const char *buf, size_t count)
321 {
322         return do_sas_phy_reset(cdev, count, 0);
323 }
324 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
325
326 static ssize_t store_sas_hard_reset(struct class_device *cdev,
327                 const char *buf, size_t count)
328 {
329         return do_sas_phy_reset(cdev, count, 1);
330 }
331 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
332
333 sas_phy_protocol_attr(identify.initiator_port_protocols,
334                 initiator_port_protocols);
335 sas_phy_protocol_attr(identify.target_port_protocols,
336                 target_port_protocols);
337 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
338                 unsigned long long);
339 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
340 sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
341 sas_phy_linkspeed_attr(negotiated_linkrate);
342 sas_phy_linkspeed_attr(minimum_linkrate_hw);
343 sas_phy_linkspeed_attr(minimum_linkrate);
344 sas_phy_linkspeed_attr(maximum_linkrate_hw);
345 sas_phy_linkspeed_attr(maximum_linkrate);
346 sas_phy_linkerror_attr(invalid_dword_count);
347 sas_phy_linkerror_attr(running_disparity_error_count);
348 sas_phy_linkerror_attr(loss_of_dword_sync_count);
349 sas_phy_linkerror_attr(phy_reset_problem_count);
350
351
352 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
353                 "sas_phy", NULL, NULL, NULL);
354
355 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
356 {
357         struct Scsi_Host *shost;
358         struct sas_internal *i;
359
360         if (!scsi_is_sas_phy(dev))
361                 return 0;
362         shost = dev_to_shost(dev->parent);
363
364         if (!shost->transportt)
365                 return 0;
366         if (shost->transportt->host_attrs.ac.class !=
367                         &sas_host_class.class)
368                 return 0;
369
370         i = to_sas_internal(shost->transportt);
371         return &i->phy_attr_cont.ac == cont;
372 }
373
374 static void sas_phy_release(struct device *dev)
375 {
376         struct sas_phy *phy = dev_to_phy(dev);
377
378         put_device(dev->parent);
379         kfree(phy);
380 }
381
382 /**
383  * sas_phy_alloc  --  allocates and initialize a SAS PHY structure
384  * @parent:     Parent device
385  * @number:     Phy index
386  *
387  * Allocates an SAS PHY structure.  It will be added in the device tree
388  * below the device specified by @parent, which has to be either a Scsi_Host
389  * or sas_rphy.
390  *
391  * Returns:
392  *      SAS PHY allocated or %NULL if the allocation failed.
393  */
394 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
395 {
396         struct Scsi_Host *shost = dev_to_shost(parent);
397         struct sas_phy *phy;
398
399         phy = kzalloc(sizeof(*phy), GFP_KERNEL);
400         if (!phy)
401                 return NULL;
402
403         get_device(parent);
404
405         phy->number = number;
406
407         device_initialize(&phy->dev);
408         phy->dev.parent = get_device(parent);
409         phy->dev.release = sas_phy_release;
410         sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
411
412         transport_setup_device(&phy->dev);
413
414         return phy;
415 }
416 EXPORT_SYMBOL(sas_phy_alloc);
417
418 /**
419  * sas_phy_add  --  add a SAS PHY to the device hierachy
420  * @phy:        The PHY to be added
421  *
422  * Publishes a SAS PHY to the rest of the system.
423  */
424 int sas_phy_add(struct sas_phy *phy)
425 {
426         int error;
427
428         error = device_add(&phy->dev);
429         if (!error) {
430                 transport_add_device(&phy->dev);
431                 transport_configure_device(&phy->dev);
432         }
433
434         return error;
435 }
436 EXPORT_SYMBOL(sas_phy_add);
437
438 /**
439  * sas_phy_free  --  free a SAS PHY
440  * @phy:        SAS PHY to free
441  *
442  * Frees the specified SAS PHY.
443  *
444  * Note:
445  *   This function must only be called on a PHY that has not
446  *   sucessfully been added using sas_phy_add().
447  */
448 void sas_phy_free(struct sas_phy *phy)
449 {
450         transport_destroy_device(&phy->dev);
451         put_device(phy->dev.parent);
452         put_device(phy->dev.parent);
453         put_device(phy->dev.parent);
454         kfree(phy);
455 }
456 EXPORT_SYMBOL(sas_phy_free);
457
458 /**
459  * sas_phy_delete  --  remove SAS PHY
460  * @phy:        SAS PHY to remove
461  *
462  * Removes the specified SAS PHY.  If the SAS PHY has an
463  * associated remote PHY it is removed before.
464  */
465 void
466 sas_phy_delete(struct sas_phy *phy)
467 {
468         struct device *dev = &phy->dev;
469
470         if (phy->rphy)
471                 sas_rphy_delete(phy->rphy);
472
473         transport_remove_device(dev);
474         device_del(dev);
475         transport_destroy_device(dev);
476         put_device(dev->parent);
477 }
478 EXPORT_SYMBOL(sas_phy_delete);
479
480 /**
481  * scsi_is_sas_phy  --  check if a struct device represents a SAS PHY
482  * @dev:        device to check
483  *
484  * Returns:
485  *      %1 if the device represents a SAS PHY, %0 else
486  */
487 int scsi_is_sas_phy(const struct device *dev)
488 {
489         return dev->release == sas_phy_release;
490 }
491 EXPORT_SYMBOL(scsi_is_sas_phy);
492
493 /*
494  * SAS remote PHY attributes.
495  */
496
497 #define sas_rphy_show_simple(field, name, format_string, cast)          \
498 static ssize_t                                                          \
499 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
500 {                                                                       \
501         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
502                                                                         \
503         return snprintf(buf, 20, format_string, cast rphy->field);      \
504 }
505
506 #define sas_rphy_simple_attr(field, name, format_string, type)          \
507         sas_rphy_show_simple(field, name, format_string, (type))        \
508 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
509                 show_sas_rphy_##name, NULL)
510
511 #define sas_rphy_show_protocol(field, name)                             \
512 static ssize_t                                                          \
513 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
514 {                                                                       \
515         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
516                                                                         \
517         if (!rphy->field)                                       \
518                 return snprintf(buf, 20, "none\n");                     \
519         return get_sas_protocol_names(rphy->field, buf);        \
520 }
521
522 #define sas_rphy_protocol_attr(field, name)                             \
523         sas_rphy_show_protocol(field, name)                             \
524 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
525                 show_sas_rphy_##name, NULL)
526
527 static ssize_t
528 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
529 {
530         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
531
532         if (!rphy->identify.device_type)
533                 return snprintf(buf, 20, "none\n");
534         return get_sas_device_type_names(
535                         rphy->identify.device_type, buf);
536 }
537
538 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
539                 show_sas_rphy_device_type, NULL);
540
541 static ssize_t
542 show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
543 {
544         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
545         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
546         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
547         struct sas_internal *i = to_sas_internal(shost->transportt);
548         u64 identifier;
549         int error;
550
551         /*
552          * Only devices behind an expander are supported, because the
553          * enclosure identifier is a SMP feature.
554          */
555         if (phy->local_attached)
556                 return -EINVAL;
557
558         error = i->f->get_enclosure_identifier(rphy, &identifier);
559         if (error)
560                 return error;
561         return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
562 }
563
564 static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
565                 show_sas_rphy_enclosure_identifier, NULL);
566
567 static ssize_t
568 show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
569 {
570         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
571         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
572         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
573         struct sas_internal *i = to_sas_internal(shost->transportt);
574         int val;
575
576         if (phy->local_attached)
577                 return -EINVAL;
578
579         val = i->f->get_bay_identifier(rphy);
580         if (val < 0)
581                 return val;
582         return sprintf(buf, "%d\n", val);
583 }
584
585 static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
586                 show_sas_rphy_bay_identifier, NULL);
587
588 sas_rphy_protocol_attr(identify.initiator_port_protocols,
589                 initiator_port_protocols);
590 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
591 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
592                 unsigned long long);
593 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
594
595 /* only need 8 bytes of data plus header (4 or 8) */
596 #define BUF_SIZE 64
597
598 int sas_read_port_mode_page(struct scsi_device *sdev)
599 {
600         char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
601         struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
602         struct sas_end_device *rdev;
603         struct scsi_mode_data mode_data;
604         int res, error;
605
606         BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
607
608         rdev = rphy_to_end_device(rphy);
609
610         if (!buffer)
611                 return -ENOMEM;
612
613         res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
614                               &mode_data, NULL);
615
616         error = -EINVAL;
617         if (!scsi_status_is_good(res))
618                 goto out;
619
620         msdata = buffer +  mode_data.header_length +
621                 mode_data.block_descriptor_length;
622
623         if (msdata - buffer > BUF_SIZE - 8)
624                 goto out;
625
626         error = 0;
627
628         rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
629         rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
630         rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
631
632  out:
633         kfree(buffer);
634         return error;
635 }
636 EXPORT_SYMBOL(sas_read_port_mode_page);
637
638 #define sas_end_dev_show_simple(field, name, format_string, cast)       \
639 static ssize_t                                                          \
640 show_sas_end_dev_##name(struct class_device *cdev, char *buf)           \
641 {                                                                       \
642         struct sas_rphy *rphy = transport_class_to_rphy(cdev);          \
643         struct sas_end_device *rdev = rphy_to_end_device(rphy);         \
644                                                                         \
645         return snprintf(buf, 20, format_string, cast rdev->field);      \
646 }
647
648 #define sas_end_dev_simple_attr(field, name, format_string, type)       \
649         sas_end_dev_show_simple(field, name, format_string, (type))     \
650 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO,                    \
651                 show_sas_end_dev_##name, NULL)
652
653 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
654 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
655                         "%d\n", int);
656 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
657                         "%d\n", int);
658
659 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
660                                "sas_end_device", NULL, NULL, NULL);
661
662 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
663                 "sas_rphy", NULL, NULL, NULL);
664
665 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
666 {
667         struct Scsi_Host *shost;
668         struct sas_internal *i;
669
670         if (!scsi_is_sas_rphy(dev))
671                 return 0;
672         shost = dev_to_shost(dev->parent->parent);
673
674         if (!shost->transportt)
675                 return 0;
676         if (shost->transportt->host_attrs.ac.class !=
677                         &sas_host_class.class)
678                 return 0;
679
680         i = to_sas_internal(shost->transportt);
681         return &i->rphy_attr_cont.ac == cont;
682 }
683
684 static int sas_end_dev_match(struct attribute_container *cont,
685                              struct device *dev)
686 {
687         struct Scsi_Host *shost;
688         struct sas_internal *i;
689         struct sas_rphy *rphy;
690
691         if (!scsi_is_sas_rphy(dev))
692                 return 0;
693         shost = dev_to_shost(dev->parent->parent);
694         rphy = dev_to_rphy(dev);
695
696         if (!shost->transportt)
697                 return 0;
698         if (shost->transportt->host_attrs.ac.class !=
699                         &sas_host_class.class)
700                 return 0;
701
702         i = to_sas_internal(shost->transportt);
703         return &i->end_dev_attr_cont.ac == cont &&
704                 rphy->identify.device_type == SAS_END_DEVICE &&
705                 /* FIXME: remove contained eventually */
706                 rphy->contained;
707 }
708
709 static void sas_rphy_release(struct device *dev)
710 {
711         struct sas_rphy *rphy = dev_to_rphy(dev);
712
713         put_device(dev->parent);
714         kfree(rphy);
715 }
716
717 /**
718  * sas_rphy_alloc  --  allocates and initialize a SAS remote PHY structure
719  * @parent:             SAS PHY this remote PHY is conneted to
720  *
721  * Allocates an SAS remote PHY structure, connected to @parent.
722  *
723  * Returns:
724  *      SAS PHY allocated or %NULL if the allocation failed.
725  */
726 struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
727 {
728         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
729         struct sas_rphy *rphy;
730
731         rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
732         if (!rphy) {
733                 put_device(&parent->dev);
734                 return NULL;
735         }
736
737         device_initialize(&rphy->dev);
738         rphy->dev.parent = get_device(&parent->dev);
739         rphy->dev.release = sas_rphy_release;
740         sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
741                 shost->host_no, parent->port_identifier, parent->number);
742         transport_setup_device(&rphy->dev);
743
744         return rphy;
745 }
746 EXPORT_SYMBOL(sas_rphy_alloc);
747
748 /**
749  * sas_end_device_alloc - allocate an rphy for an end device
750  *
751  * Allocates an SAS remote PHY structure, connected to @parent.
752  *
753  * Returns:
754  *      SAS PHY allocated or %NULL if the allocation failed.
755  */
756 struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
757 {
758         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
759         struct sas_end_device *rdev;
760
761         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
762         if (!rdev) {
763                 put_device(&parent->dev);
764                 return NULL;
765         }
766
767         device_initialize(&rdev->rphy.dev);
768         rdev->rphy.dev.parent = get_device(&parent->dev);
769         rdev->rphy.dev.release = sas_rphy_release;
770         sprintf(rdev->rphy.dev.bus_id, "rphy-%d:%d-%d",
771                 shost->host_no, parent->port_identifier, parent->number);
772         rdev->rphy.identify.device_type = SAS_END_DEVICE;
773         /* FIXME: mark the rphy as being contained in a larger structure */
774         rdev->rphy.contained = 1;
775         transport_setup_device(&rdev->rphy.dev);
776
777         return &rdev->rphy;
778 }
779 EXPORT_SYMBOL(sas_end_device_alloc);
780
781
782 /**
783  * sas_rphy_add  --  add a SAS remote PHY to the device hierachy
784  * @rphy:       The remote PHY to be added
785  *
786  * Publishes a SAS remote PHY to the rest of the system.
787  */
788 int sas_rphy_add(struct sas_rphy *rphy)
789 {
790         struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
791         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
792         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
793         struct sas_identify *identify = &rphy->identify;
794         int error;
795
796         if (parent->rphy)
797                 return -ENXIO;
798         parent->rphy = rphy;
799
800         error = device_add(&rphy->dev);
801         if (error)
802                 return error;
803         transport_add_device(&rphy->dev);
804         transport_configure_device(&rphy->dev);
805
806         mutex_lock(&sas_host->lock);
807         list_add_tail(&rphy->list, &sas_host->rphy_list);
808         if (identify->device_type == SAS_END_DEVICE &&
809             (identify->target_port_protocols &
810              (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
811                 rphy->scsi_target_id = sas_host->next_target_id++;
812         else
813                 rphy->scsi_target_id = -1;
814         mutex_unlock(&sas_host->lock);
815
816         if (rphy->scsi_target_id != -1) {
817                 scsi_scan_target(&rphy->dev, parent->port_identifier,
818                                 rphy->scsi_target_id, ~0, 0);
819         }
820
821         return 0;
822 }
823 EXPORT_SYMBOL(sas_rphy_add);
824
825 /**
826  * sas_rphy_free  --  free a SAS remote PHY
827  * @rphy        SAS remote PHY to free
828  *
829  * Frees the specified SAS remote PHY.
830  *
831  * Note:
832  *   This function must only be called on a remote
833  *   PHY that has not sucessfully been added using
834  *   sas_rphy_add().
835  */
836 void sas_rphy_free(struct sas_rphy *rphy)
837 {
838         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
839         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
840
841         mutex_lock(&sas_host->lock);
842         list_del(&rphy->list);
843         mutex_unlock(&sas_host->lock);
844
845         transport_destroy_device(&rphy->dev);
846         put_device(rphy->dev.parent);
847         put_device(rphy->dev.parent);
848         put_device(rphy->dev.parent);
849         kfree(rphy);
850 }
851 EXPORT_SYMBOL(sas_rphy_free);
852
853 /**
854  * sas_rphy_delete  --  remove SAS remote PHY
855  * @rphy:       SAS remote PHY to remove
856  *
857  * Removes the specified SAS remote PHY.
858  */
859 void
860 sas_rphy_delete(struct sas_rphy *rphy)
861 {
862         struct device *dev = &rphy->dev;
863         struct sas_phy *parent = dev_to_phy(dev->parent);
864         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
865         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
866
867         switch (rphy->identify.device_type) {
868         case SAS_END_DEVICE:
869                 scsi_remove_target(dev);
870                 break;
871         case SAS_EDGE_EXPANDER_DEVICE:
872         case SAS_FANOUT_EXPANDER_DEVICE:
873                 device_for_each_child(dev, NULL, do_sas_phy_delete);
874                 break;
875         default:
876                 break;
877         }
878
879         transport_remove_device(dev);
880         device_del(dev);
881         transport_destroy_device(dev);
882
883         mutex_lock(&sas_host->lock);
884         list_del(&rphy->list);
885         mutex_unlock(&sas_host->lock);
886
887         parent->rphy = NULL;
888
889         put_device(&parent->dev);
890 }
891 EXPORT_SYMBOL(sas_rphy_delete);
892
893 /**
894  * scsi_is_sas_rphy  --  check if a struct device represents a SAS remote PHY
895  * @dev:        device to check
896  *
897  * Returns:
898  *      %1 if the device represents a SAS remote PHY, %0 else
899  */
900 int scsi_is_sas_rphy(const struct device *dev)
901 {
902         return dev->release == sas_rphy_release;
903 }
904 EXPORT_SYMBOL(scsi_is_sas_rphy);
905
906
907 /*
908  * SCSI scan helper
909  */
910
911 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
912                 uint id, uint lun)
913 {
914         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
915         struct sas_rphy *rphy;
916
917         mutex_lock(&sas_host->lock);
918         list_for_each_entry(rphy, &sas_host->rphy_list, list) {
919                 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
920
921                 if (rphy->scsi_target_id == -1)
922                         continue;
923
924                 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
925                     (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
926                         scsi_scan_target(&rphy->dev, parent->port_identifier,
927                                          rphy->scsi_target_id, lun, 1);
928                 }
929         }
930         mutex_unlock(&sas_host->lock);
931
932         return 0;
933 }
934
935
936 /*
937  * Setup / Teardown code
938  */
939
940 #define SETUP_TEMPLATE(attrb, field, perm, test)                                \
941         i->private_##attrb[count] = class_device_attr_##field;          \
942         i->private_##attrb[count].attr.mode = perm;                     \
943         i->private_##attrb[count].store = NULL;                         \
944         i->attrb[count] = &i->private_##attrb[count];                   \
945         if (test)                                                       \
946                 count++
947
948
949 #define SETUP_RPORT_ATTRIBUTE(field)                                    \
950         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
951
952 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)                     \
953         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
954
955 #define SETUP_PORT_ATTRIBUTE(field)                                     \
956         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
957
958 #define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func)                      \
959         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
960
961 #define SETUP_PORT_ATTRIBUTE_WRONLY(field)                              \
962         SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
963
964 #define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func)               \
965         SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
966
967 #define SETUP_END_DEV_ATTRIBUTE(field)                                  \
968         SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
969
970 /**
971  * sas_attach_transport  --  instantiate SAS transport template
972  * @ft:         SAS transport class function template
973  */
974 struct scsi_transport_template *
975 sas_attach_transport(struct sas_function_template *ft)
976 {
977         struct sas_internal *i;
978         int count;
979
980         i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
981         if (!i)
982                 return NULL;
983
984         i->t.user_scan = sas_user_scan;
985
986         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
987         i->t.host_attrs.ac.class = &sas_host_class.class;
988         i->t.host_attrs.ac.match = sas_host_match;
989         transport_container_register(&i->t.host_attrs);
990         i->t.host_size = sizeof(struct sas_host_attrs);
991
992         i->phy_attr_cont.ac.class = &sas_phy_class.class;
993         i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
994         i->phy_attr_cont.ac.match = sas_phy_match;
995         transport_container_register(&i->phy_attr_cont);
996
997         i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
998         i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
999         i->rphy_attr_cont.ac.match = sas_rphy_match;
1000         transport_container_register(&i->rphy_attr_cont);
1001
1002         i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1003         i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1004         i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1005         transport_container_register(&i->end_dev_attr_cont);
1006
1007         i->f = ft;
1008
1009         count = 0;
1010         i->host_attrs[count] = NULL;
1011
1012         count = 0;
1013         SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
1014         SETUP_PORT_ATTRIBUTE(target_port_protocols);
1015         SETUP_PORT_ATTRIBUTE(device_type);
1016         SETUP_PORT_ATTRIBUTE(sas_address);
1017         SETUP_PORT_ATTRIBUTE(phy_identifier);
1018         SETUP_PORT_ATTRIBUTE(port_identifier);
1019         SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
1020         SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
1021         SETUP_PORT_ATTRIBUTE(minimum_linkrate);
1022         SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
1023         SETUP_PORT_ATTRIBUTE(maximum_linkrate);
1024
1025         SETUP_PORT_ATTRIBUTE(invalid_dword_count);
1026         SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
1027         SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
1028         SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
1029         SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1030         SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1031         i->phy_attrs[count] = NULL;
1032
1033         count = 0;
1034         SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1035         SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1036         SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1037         SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1038         SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1039         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1040                                        get_enclosure_identifier);
1041         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1042                                        get_bay_identifier);
1043         i->rphy_attrs[count] = NULL;
1044
1045         count = 0;
1046         SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1047         SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1048         SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1049         i->end_dev_attrs[count] = NULL;
1050
1051         return &i->t;
1052 }
1053 EXPORT_SYMBOL(sas_attach_transport);
1054
1055 /**
1056  * sas_release_transport  --  release SAS transport template instance
1057  * @t:          transport template instance
1058  */
1059 void sas_release_transport(struct scsi_transport_template *t)
1060 {
1061         struct sas_internal *i = to_sas_internal(t);
1062
1063         transport_container_unregister(&i->t.host_attrs);
1064         transport_container_unregister(&i->phy_attr_cont);
1065         transport_container_unregister(&i->rphy_attr_cont);
1066
1067         kfree(i);
1068 }
1069 EXPORT_SYMBOL(sas_release_transport);
1070
1071 static __init int sas_transport_init(void)
1072 {
1073         int error;
1074
1075         error = transport_class_register(&sas_host_class);
1076         if (error)
1077                 goto out;
1078         error = transport_class_register(&sas_phy_class);
1079         if (error)
1080                 goto out_unregister_transport;
1081         error = transport_class_register(&sas_rphy_class);
1082         if (error)
1083                 goto out_unregister_phy;
1084         error = transport_class_register(&sas_end_dev_class);
1085         if (error)
1086                 goto out_unregister_rphy;
1087
1088         return 0;
1089
1090  out_unregister_rphy:
1091         transport_class_unregister(&sas_rphy_class);
1092  out_unregister_phy:
1093         transport_class_unregister(&sas_phy_class);
1094  out_unregister_transport:
1095         transport_class_unregister(&sas_host_class);
1096  out:
1097         return error;
1098
1099 }
1100
1101 static void __exit sas_transport_exit(void)
1102 {
1103         transport_class_unregister(&sas_host_class);
1104         transport_class_unregister(&sas_phy_class);
1105         transport_class_unregister(&sas_rphy_class);
1106         transport_class_unregister(&sas_end_dev_class);
1107 }
1108
1109 MODULE_AUTHOR("Christoph Hellwig");
1110 MODULE_DESCRIPTION("SAS Transphy Attributes");
1111 MODULE_LICENSE("GPL");
1112
1113 module_init(sas_transport_init);
1114 module_exit(sas_transport_exit);