- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / scsi_transport_fc.c
1 /* 
2  *  FiberChannel transport specific attributes exported to sysfs.
3  *
4  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_transport.h>
25 #include <scsi/scsi_transport_fc.h>
26 #include "scsi_priv.h"
27
28 #define FC_PRINTK(x, l, f, a...)        printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
29
30 /*
31  * Redefine so that we can have same named attributes in the
32  * sdev/starget/host objects.
33  */
34 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)          \
35 struct class_device_attribute class_device_attr_##_prefix##_##_name =   \
36         __ATTR(_name,_mode,_show,_store)
37
38 #define fc_enum_name_search(title, table_type, table)                   \
39 static const char *get_fc_##title##_name(enum table_type table_key)     \
40 {                                                                       \
41         int i;                                                          \
42         char *name = NULL;                                              \
43                                                                         \
44         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {          \
45                 if (table[i].value == table_key) {                      \
46                         name = table[i].name;                           \
47                         break;                                          \
48                 }                                                       \
49         }                                                               \
50         return name;                                                    \
51 }
52
53 #define fc_enum_name_match(title, table_type, table)                    \
54 static int get_fc_##title##_match(const char *table_key,                \
55                 enum table_type *value)                                 \
56 {                                                                       \
57         int i;                                                          \
58                                                                         \
59         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {          \
60                 if (strncmp(table_key, table[i].name,                   \
61                                 table[i].matchlen) == 0) {              \
62                         *value = table[i].value;                        \
63                         return 0; /* success */                         \
64                 }                                                       \
65         }                                                               \
66         return 1; /* failure */                                         \
67 }
68
69
70 /* Convert fc_port_type values to ascii string name */
71 static struct {
72         enum fc_port_type       value;
73         char                    *name;
74 } fc_port_type_names[] = {
75         { FC_PORTTYPE_UNKNOWN,          "Unknown" },
76         { FC_PORTTYPE_OTHER,            "Other" },
77         { FC_PORTTYPE_NOTPRESENT,       "Not Present" },
78         { FC_PORTTYPE_NPORT,    "NPort (fabric via point-to-point)" },
79         { FC_PORTTYPE_NLPORT,   "NLPort (fabric via loop)" },
80         { FC_PORTTYPE_LPORT,    "LPort (private loop)" },
81         { FC_PORTTYPE_PTP,      "Point-To-Point (direct nport connection" },
82 };
83 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
84 #define FC_PORTTYPE_MAX_NAMELEN         50
85
86
87 /* Convert fc_port_state values to ascii string name */
88 static struct {
89         enum fc_port_state      value;
90         char                    *name;
91 } fc_port_state_names[] = {
92         { FC_PORTSTATE_UNKNOWN,         "Unknown" },
93         { FC_PORTSTATE_ONLINE,          "Online" },
94         { FC_PORTSTATE_OFFLINE,         "Offline" },
95         { FC_PORTSTATE_BYPASSED,        "Bypassed" },
96         { FC_PORTSTATE_DIAGNOSTICS,     "Diagnostics" },
97         { FC_PORTSTATE_LINKDOWN,        "Linkdown" },
98         { FC_PORTSTATE_ERROR,           "Error" },
99         { FC_PORTSTATE_LOOPBACK,        "Loopback" },
100 };
101 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
102 #define FC_PORTSTATE_MAX_NAMELEN        20
103
104
105 /* Convert fc_tgtid_binding_type values to ascii string name */
106 static struct {
107         enum fc_tgtid_binding_type      value;
108         char                            *name;
109         int                             matchlen;
110 } fc_tgtid_binding_type_names[] = {
111         { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
112         { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
113         { FC_TGTID_BIND_BY_ID, "fcportid (FC Address)", 8 },
114 };
115 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
116                 fc_tgtid_binding_type_names)
117 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
118                 fc_tgtid_binding_type_names)
119 #define FC_BINDTYPE_MAX_NAMELEN 30
120
121
122 #define fc_bitfield_name_search(title, table)                   \
123 static ssize_t                                                  \
124 get_fc_##title##_names(u32 table_key, char *buf)                \
125 {                                                               \
126         char *prefix = "";                                      \
127         ssize_t len = 0;                                        \
128         int i;                                                  \
129                                                                 \
130         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
131                 if (table[i].value & table_key) {               \
132                         len += sprintf(buf + len, "%s%s",       \
133                                 prefix, table[i].name);         \
134                         prefix = ", ";                          \
135                 }                                               \
136         }                                                       \
137         len += sprintf(buf + len, "\n");                        \
138         return len;                                             \
139 }
140
141
142 /* Convert fc_cos bit values to ascii string name */
143 static struct {
144         u32                     value;
145         char                    *name;
146 } fc_cos_names[] = {
147         { FC_COS_CLASS1,        "Class 1" },
148         { FC_COS_CLASS2,        "Class 2" },
149         { FC_COS_CLASS3,        "Class 3" },
150         { FC_COS_CLASS4,        "Class 4" },
151         { FC_COS_CLASS6,        "Class 6" },
152 };
153 fc_bitfield_name_search(cos, fc_cos_names)
154
155
156 /* Convert fc_port_speed bit values to ascii string name */
157 static struct {
158         u32                     value;
159         char                    *name;
160 } fc_port_speed_names[] = {
161         { FC_PORTSPEED_1GBIT,           "1 Gbit" },
162         { FC_PORTSPEED_2GBIT,           "2 Gbit" },
163         { FC_PORTSPEED_4GBIT,           "4 Gbit" },
164         { FC_PORTSPEED_10GBIT,          "10 Gbit" },
165         { FC_PORTSPEED_NOT_NEGOTIATED,  "Not Negotiated" },
166 };
167 fc_bitfield_name_search(port_speed, fc_port_speed_names)
168
169
170 static int
171 show_fc_fc4s (char *buf, u8 *fc4_list)
172 {
173         int i, len=0;
174
175         for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
176                 len += sprintf(buf + len , "0x%02x ", *fc4_list);
177         len += sprintf(buf + len, "\n");
178         return len;
179 }
180
181
182
183 static void transport_class_release(struct class_device *class_dev);
184 static void host_class_release(struct class_device *class_dev);
185 static void fc_timeout_blocked_host(void *data);
186 static void fc_timeout_blocked_tgt(void *data);
187
188 #define FC_STARGET_NUM_ATTRS    4       /* increase this if you add attributes */
189 #define FC_STARGET_OTHER_ATTRS  0       /* increase this if you add "always on"
190                                          * attributes */
191 #define FC_HOST_NUM_ATTRS       15
192
193 struct fc_internal {
194         struct scsi_transport_template t;
195         struct fc_function_template *f;
196         /* The actual attributes */
197         struct class_device_attribute private_starget_attrs[
198                                                 FC_STARGET_NUM_ATTRS];
199         /* The array of null terminated pointers to attributes
200          * needed by scsi_sysfs.c */
201         struct class_device_attribute *starget_attrs[
202                         FC_STARGET_NUM_ATTRS + FC_STARGET_OTHER_ATTRS + 1];
203
204         struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
205         struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
206 };
207
208 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
209
210 struct class fc_transport_class = {
211         .name = "fc_transport",
212         .release = transport_class_release,
213 };
214
215 struct class fc_host_class = {
216         .name = "fc_host",
217         .release = host_class_release,
218 };
219
220 static __init int fc_transport_init(void)
221 {
222         int error = class_register(&fc_host_class);
223         if (error)
224                 return error;
225         return class_register(&fc_transport_class);
226 }
227
228 static void __exit fc_transport_exit(void)
229 {
230         class_unregister(&fc_transport_class);
231         class_unregister(&fc_host_class);
232 }
233
234 static int fc_setup_starget_transport_attrs(struct scsi_target *starget)
235 {
236         /* 
237          * Set default values easily detected by the midlayer as
238          * failure cases.  The scsi lldd is responsible for initializing
239          * all transport attributes to valid values per target.
240          */
241         fc_starget_node_name(starget) = -1;
242         fc_starget_port_name(starget) = -1;
243         fc_starget_port_id(starget) = -1;
244         fc_starget_dev_loss_tmo(starget) = -1;
245         INIT_WORK(&fc_starget_dev_loss_work(starget),
246                   fc_timeout_blocked_tgt, starget);
247         return 0;
248 }
249
250 static void fc_destroy_starget(struct scsi_target *starget)
251 {
252         /* Stop the target timer */
253         if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
254                 flush_scheduled_work();
255 }
256
257 static int fc_setup_host_transport_attrs(struct Scsi_Host *shost)
258 {
259         /* 
260          * Set default values easily detected by the midlayer as
261          * failure cases.  The scsi lldd is responsible for initializing
262          * all transport attributes to valid values per host.
263          */
264         fc_host_node_name(shost) = -1;
265         fc_host_port_name(shost) = -1;
266         fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
267         memset(fc_host_supported_fc4s(shost), 0,
268                 sizeof(fc_host_supported_fc4s(shost)));
269         memset(fc_host_symbolic_name(shost), 0,
270                 sizeof(fc_host_symbolic_name(shost)));
271         fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
272         fc_host_maxframe_size(shost) = -1;
273         memset(fc_host_hardware_version(shost), 0,
274                 sizeof(fc_host_hardware_version(shost)));
275         memset(fc_host_firmware_version(shost), 0,
276                 sizeof(fc_host_firmware_version(shost)));
277         memset(fc_host_serial_number(shost), 0,
278                 sizeof(fc_host_serial_number(shost)));
279         memset(fc_host_opt_rom_version(shost), 0,
280                 sizeof(fc_host_opt_rom_version(shost)));
281         memset(fc_host_driver_version(shost), 0,
282                 sizeof(fc_host_driver_version(shost)));
283
284         fc_host_port_id(shost) = -1;
285         fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
286         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
287         memset(fc_host_active_fc4s(shost), 0,
288                 sizeof(fc_host_active_fc4s(shost)));
289         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
290         fc_host_fabric_name(shost) = -1;
291         fc_host_link_down_tmo(shost) = -1;
292
293         fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
294
295         INIT_WORK(&fc_host_link_down_work(shost),
296                   fc_timeout_blocked_host, shost);
297         return 0;
298 }
299
300 static void fc_destroy_host(struct Scsi_Host *shost)
301 {
302         /* Stop the host timer */
303         if (cancel_delayed_work(&fc_host_link_down_work(shost)))
304                 flush_scheduled_work();
305 }
306
307 static void transport_class_release(struct class_device *class_dev)
308 {
309         struct scsi_target *starget = transport_class_to_starget(class_dev);
310         put_device(&starget->dev);
311 }
312
313 static void host_class_release(struct class_device *class_dev)
314 {
315         struct Scsi_Host *shost = transport_class_to_shost(class_dev);
316         put_device(&shost->shost_gendev);
317 }
318
319
320 /*
321  * Remote Port (Target) Attribute Management
322  */
323
324 #define fc_starget_show_function(field, format_string, cast)            \
325 static ssize_t                                                          \
326 show_fc_starget_##field (struct class_device *cdev, char *buf)          \
327 {                                                                       \
328         struct scsi_target *starget = transport_class_to_starget(cdev); \
329         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
330         struct fc_internal *i = to_fc_internal(shost->transportt);      \
331         if (i->f->get_starget_##field)                                  \
332                 i->f->get_starget_##field(starget);                     \
333         return snprintf(buf, 20, format_string,                         \
334                 cast fc_starget_##field(starget));                      \
335 }
336
337 #define fc_starget_store_function(field, format_string)                 \
338 static ssize_t                                                          \
339 store_fc_starget_##field(struct class_device *cdev, const char *buf,    \
340                            size_t count)                                \
341 {                                                                       \
342         int val;                                                        \
343         struct scsi_target *starget = transport_class_to_starget(cdev); \
344         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
345         struct fc_internal *i = to_fc_internal(shost->transportt);      \
346                                                                         \
347         val = simple_strtoul(buf, NULL, 0);                             \
348         i->f->set_starget_##field(starget, val);                        \
349         return count;                                                   \
350 }
351
352 #define fc_starget_rd_attr(field, format_string)                        \
353         fc_starget_show_function(field, format_string, )                \
354 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
355                          show_fc_starget_##field, NULL)
356
357 #define fc_starget_rd_attr_cast(field, format_string, cast)             \
358         fc_starget_show_function(field, format_string, (cast))          \
359 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
360                           show_fc_starget_##field, NULL)
361
362 #define fc_starget_rw_attr(field, format_string)                        \
363         fc_starget_show_function(field, format_string, )                \
364         fc_starget_store_function(field, format_string)                 \
365 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO | S_IWUSR,          \
366                         show_fc_starget_##field,                        \
367                         store_fc_starget_##field)
368
369 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
370         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
371         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
372         i->private_starget_attrs[count].store = NULL;                   \
373         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
374         if (i->f->show_starget_##field)                                 \
375                 count++
376
377 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
378         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
379         if (!i->f->set_starget_##field) {                               \
380                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
381                 i->private_starget_attrs[count].store = NULL;           \
382         }                                                               \
383         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
384         if (i->f->show_starget_##field)                                 \
385                 count++
386
387 /* The FC Tranport Remote Port (Target) Attributes: */
388 fc_starget_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
389 fc_starget_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
390 fc_starget_rd_attr(port_id, "0x%06x\n");
391 fc_starget_rw_attr(dev_loss_tmo, "%d\n");
392
393
394
395 /*
396  * Host Attribute Management
397  */
398
399 #define fc_host_show_function(field, format_string, sz, cast)           \
400 static ssize_t                                                          \
401 show_fc_host_##field (struct class_device *cdev, char *buf)             \
402 {                                                                       \
403         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
404         struct fc_internal *i = to_fc_internal(shost->transportt);      \
405         if (i->f->get_host_##field)                                     \
406                 i->f->get_host_##field(shost);                          \
407         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
408 }
409
410 #define fc_host_store_function(field, format_string)                    \
411 static ssize_t                                                          \
412 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
413                            size_t count)                                \
414 {                                                                       \
415         int val;                                                        \
416         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
417         struct fc_internal *i = to_fc_internal(shost->transportt);      \
418                                                                         \
419         val = simple_strtoul(buf, NULL, 0);                             \
420         i->f->set_host_##field(shost, val);                             \
421         return count;                                                   \
422 }
423
424 #define fc_host_rd_attr(field, format_string, sz)                       \
425         fc_host_show_function(field, format_string, sz, )               \
426 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
427                          show_fc_host_##field, NULL)
428
429 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
430         fc_host_show_function(field, format_string, sz, (cast))         \
431 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
432                           show_fc_host_##field, NULL)
433
434 #define fc_host_rw_attr(field, format_string, sz)                       \
435         fc_host_show_function(field, format_string, sz, )               \
436         fc_host_store_function(field, format_string)                    \
437 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,             \
438                         show_fc_host_##field,                           \
439                         store_fc_host_##field)
440
441 #define fc_host_rd_enum_attr(title, maxlen)                             \
442 static ssize_t                                                          \
443 show_fc_host_##title (struct class_device *cdev, char *buf)             \
444 {                                                                       \
445         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
446         struct fc_internal *i = to_fc_internal(shost->transportt);      \
447         const char *name;                                               \
448         if (i->f->get_host_##title)                                     \
449                 i->f->get_host_##title(shost);                          \
450         name = get_fc_##title##_name(fc_host_##title(shost));           \
451         if (!name)                                                      \
452                 return -EINVAL;                                         \
453         return snprintf(buf, maxlen, "%s\n", name);                     \
454 }                                                                       \
455 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
456
457 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
458         i->private_host_attrs[count] = class_device_attr_host_##field;  \
459         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
460         i->private_host_attrs[count].store = NULL;                      \
461         i->host_attrs[count] = &i->private_host_attrs[count];           \
462         if (i->f->show_host_##field)                                    \
463                 count++
464
465 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
466         i->private_host_attrs[count] = class_device_attr_host_##field;  \
467         if (!i->f->set_host_##field) {                                  \
468                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
469                 i->private_host_attrs[count].store = NULL;              \
470         }                                                               \
471         i->host_attrs[count] = &i->private_host_attrs[count];           \
472         if (i->f->show_host_##field)                                    \
473                 count++
474
475
476 #define fc_private_host_show_function(field, format_string, sz, cast)   \
477 static ssize_t                                                          \
478 show_fc_host_##field (struct class_device *cdev, char *buf)             \
479 {                                                                       \
480         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
481         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
482 }
483
484 #define fc_private_host_rd_attr(field, format_string, sz)               \
485         fc_private_host_show_function(field, format_string, sz, )       \
486 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
487                          show_fc_host_##field, NULL)
488
489 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
490         fc_private_host_show_function(field, format_string, sz, (cast)) \
491 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
492                           show_fc_host_##field, NULL)
493
494 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
495         i->private_host_attrs[count] = class_device_attr_host_##field;  \
496         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
497         i->private_host_attrs[count].store = NULL;                      \
498         i->host_attrs[count] = &i->private_host_attrs[count];           \
499         count++
500
501 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
502         i->private_host_attrs[count] = class_device_attr_host_##field;  \
503         i->host_attrs[count] = &i->private_host_attrs[count];           \
504         count++
505
506
507 /* Fixed Host Attributes */
508
509 static ssize_t
510 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
511 {
512         struct Scsi_Host *shost = transport_class_to_shost(cdev);
513
514         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
515                 return snprintf(buf, 20, "unspecified\n");
516
517         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
518 }
519 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
520                 show_fc_host_supported_classes, NULL);
521
522 static ssize_t
523 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
524 {
525         struct Scsi_Host *shost = transport_class_to_shost(cdev);
526         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
527 }
528 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
529                 show_fc_host_supported_fc4s, NULL);
530
531 static ssize_t
532 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
533 {
534         struct Scsi_Host *shost = transport_class_to_shost(cdev);
535
536         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
537                 return snprintf(buf, 20, "unknown\n");
538
539         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
540 }
541 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
542                 show_fc_host_supported_speeds, NULL);
543
544
545 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
546 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
547 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
548 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
549 fc_private_host_rd_attr(hardware_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
550 fc_private_host_rd_attr(firmware_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
551 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
552 fc_private_host_rd_attr(opt_rom_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
553 fc_private_host_rd_attr(driver_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
554
555
556 /* Dynamic Host Attributes */
557
558 static ssize_t
559 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
560 {
561         struct Scsi_Host *shost = transport_class_to_shost(cdev);
562         struct fc_internal *i = to_fc_internal(shost->transportt);
563
564         if (i->f->get_host_active_fc4s)
565                 i->f->get_host_active_fc4s(shost);
566
567         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
568 }
569 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
570                 show_fc_host_active_fc4s, NULL);
571
572 static ssize_t
573 show_fc_host_speed (struct class_device *cdev, char *buf)
574 {
575         struct Scsi_Host *shost = transport_class_to_shost(cdev);
576         struct fc_internal *i = to_fc_internal(shost->transportt);
577
578         if (i->f->get_host_speed)
579                 i->f->get_host_speed(shost);
580
581         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
582                 return snprintf(buf, 20, "unknown\n");
583
584         return get_fc_port_speed_names(fc_host_speed(shost), buf);
585 }
586 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
587                 show_fc_host_speed, NULL);
588
589
590 fc_host_rd_attr(port_id, "0x%06x\n", 20);
591 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
592 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
593 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
594 fc_host_rw_attr(link_down_tmo, "%d\n", 20);
595
596
597 /* Private Host Attributes */
598
599 static ssize_t
600 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
601 {
602         struct Scsi_Host *shost = transport_class_to_shost(cdev);
603         const char *name;
604
605         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
606         if (!name)
607                 return -EINVAL;
608         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
609 }
610
611 static ssize_t
612 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
613         const char *buf, size_t count)
614 {
615         struct Scsi_Host *shost = transport_class_to_shost(cdev);
616         enum fc_tgtid_binding_type val;
617
618         if (get_fc_tgtid_bind_type_match(buf, &val))
619                 return -EINVAL;
620         fc_host_tgtid_bind_type(shost) = val;
621         return count;
622 }
623
624 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
625                         show_fc_private_host_tgtid_bind_type,
626                         store_fc_private_host_tgtid_bind_type);
627
628 /*
629  * Host Statistics Management
630  */
631
632 /* Show a given an attribute in the statistics group */
633 static ssize_t
634 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
635 {
636         struct Scsi_Host *shost = transport_class_to_shost(cdev);
637         struct fc_internal *i = to_fc_internal(shost->transportt);
638         struct fc_host_statistics *stats;
639         ssize_t ret = -ENOENT;
640
641         if (offset > sizeof(struct fc_host_statistics) ||
642             offset % sizeof(u64) != 0)
643                 WARN_ON(1);
644
645         if (i->f->get_fc_host_stats) {
646                 stats = (i->f->get_fc_host_stats)(shost);
647                 if (stats)
648                         ret = snprintf(buf, 20, "0x%llx\n",
649                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
650         }
651         return ret;
652 }
653
654
655 /* generate a read-only statistics attribute */
656 #define fc_host_statistic(name)                                         \
657 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf)   \
658 {                                                                       \
659         return fc_stat_show(cd, buf,                                    \
660                             offsetof(struct fc_host_statistics, name)); \
661 }                                                                       \
662 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
663
664 fc_host_statistic(seconds_since_last_reset);
665 fc_host_statistic(tx_frames);
666 fc_host_statistic(tx_words);
667 fc_host_statistic(rx_frames);
668 fc_host_statistic(rx_words);
669 fc_host_statistic(lip_count);
670 fc_host_statistic(nos_count);
671 fc_host_statistic(error_frames);
672 fc_host_statistic(dumped_frames);
673 fc_host_statistic(link_failure_count);
674 fc_host_statistic(loss_of_sync_count);
675 fc_host_statistic(loss_of_signal_count);
676 fc_host_statistic(prim_seq_protocol_err_count);
677 fc_host_statistic(invalid_tx_word_count);
678 fc_host_statistic(invalid_crc_count);
679 fc_host_statistic(fcp_input_requests);
680 fc_host_statistic(fcp_output_requests);
681 fc_host_statistic(fcp_control_requests);
682 fc_host_statistic(fcp_input_megabytes);
683 fc_host_statistic(fcp_output_megabytes);
684
685 static ssize_t
686 fc_reset_statistics(struct class_device *cdev, const char *buf,
687                            size_t count)
688 {
689         struct Scsi_Host *shost = transport_class_to_shost(cdev);
690         struct fc_internal *i = to_fc_internal(shost->transportt);
691
692         /* ignore any data value written to the attribute */
693         if (i->f->reset_fc_host_stats) {
694                 i->f->reset_fc_host_stats(shost);
695                 return count;
696         }
697
698         return -ENOENT;
699 }
700 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
701                                 fc_reset_statistics);
702
703
704 static struct attribute *fc_statistics_attrs[] = {
705         &class_device_attr_host_seconds_since_last_reset.attr,
706         &class_device_attr_host_tx_frames.attr,
707         &class_device_attr_host_tx_words.attr,
708         &class_device_attr_host_rx_frames.attr,
709         &class_device_attr_host_rx_words.attr,
710         &class_device_attr_host_lip_count.attr,
711         &class_device_attr_host_nos_count.attr,
712         &class_device_attr_host_error_frames.attr,
713         &class_device_attr_host_dumped_frames.attr,
714         &class_device_attr_host_link_failure_count.attr,
715         &class_device_attr_host_loss_of_sync_count.attr,
716         &class_device_attr_host_loss_of_signal_count.attr,
717         &class_device_attr_host_prim_seq_protocol_err_count.attr,
718         &class_device_attr_host_invalid_tx_word_count.attr,
719         &class_device_attr_host_invalid_crc_count.attr,
720         &class_device_attr_host_fcp_input_requests.attr,
721         &class_device_attr_host_fcp_output_requests.attr,
722         &class_device_attr_host_fcp_control_requests.attr,
723         &class_device_attr_host_fcp_input_megabytes.attr,
724         &class_device_attr_host_fcp_output_megabytes.attr,
725         &class_device_attr_host_reset_statistics.attr,
726         NULL
727 };
728
729 static struct attribute_group fc_statistics_group = {
730         .name = "statistics",
731         .attrs = fc_statistics_attrs,
732 };
733
734
735
736 struct scsi_transport_template *
737 fc_attach_transport(struct fc_function_template *ft)
738 {
739         struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
740                                         GFP_KERNEL);
741         int count = 0;
742
743         if (unlikely(!i))
744                 return NULL;
745
746         memset(i, 0, sizeof(struct fc_internal));
747
748         i->t.target_attrs = &i->starget_attrs[0];
749         i->t.target_class = &fc_transport_class;
750         i->t.target_setup = &fc_setup_starget_transport_attrs;
751         i->t.target_destroy = &fc_destroy_starget;
752         i->t.target_size = sizeof(struct fc_starget_attrs);
753
754         i->t.host_attrs = &i->host_attrs[0];
755         i->t.host_class = &fc_host_class;
756         i->t.host_setup = &fc_setup_host_transport_attrs;
757         i->t.host_destroy = &fc_destroy_host;
758         i->t.host_size = sizeof(struct fc_host_attrs);
759
760         if (ft->get_fc_host_stats)
761                 i->t.host_statistics = &fc_statistics_group;
762
763         i->f = ft;
764
765         
766         /*
767          * setup remote port (target) attributes
768          */
769         SETUP_STARGET_ATTRIBUTE_RD(port_id);
770         SETUP_STARGET_ATTRIBUTE_RD(port_name);
771         SETUP_STARGET_ATTRIBUTE_RD(node_name);
772         SETUP_STARGET_ATTRIBUTE_RW(dev_loss_tmo);
773
774         BUG_ON(count > FC_STARGET_NUM_ATTRS);
775
776         /* Setup the always-on attributes here */
777
778         i->starget_attrs[count] = NULL;
779
780
781         /* setup host attributes */
782         count=0;
783         SETUP_HOST_ATTRIBUTE_RD(node_name);
784         SETUP_HOST_ATTRIBUTE_RD(port_name);
785         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
786         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
787         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
788         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
789         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
790         SETUP_HOST_ATTRIBUTE_RD(hardware_version);
791         SETUP_HOST_ATTRIBUTE_RD(firmware_version);
792         SETUP_HOST_ATTRIBUTE_RD(serial_number);
793         SETUP_HOST_ATTRIBUTE_RD(opt_rom_version);
794         SETUP_HOST_ATTRIBUTE_RD(driver_version);
795
796         SETUP_HOST_ATTRIBUTE_RD(port_id);
797         SETUP_HOST_ATTRIBUTE_RD(port_type);
798         SETUP_HOST_ATTRIBUTE_RD(port_state);
799         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
800         SETUP_HOST_ATTRIBUTE_RD(speed);
801         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
802         SETUP_HOST_ATTRIBUTE_RW(link_down_tmo);
803
804         /* Transport-managed attributes */
805         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
806
807         BUG_ON(count > FC_HOST_NUM_ATTRS);
808
809         i->host_attrs[count] = NULL;
810
811
812         return &i->t;
813 }
814 EXPORT_SYMBOL(fc_attach_transport);
815
816 void fc_release_transport(struct scsi_transport_template *t)
817 {
818         struct fc_internal *i = to_fc_internal(t);
819
820         kfree(i);
821 }
822 EXPORT_SYMBOL(fc_release_transport);
823
824
825
826 /**
827  * fc_device_block - called by target functions to block a scsi device
828  * @dev:        scsi device
829  * @data:       unused
830  **/
831 static void fc_device_block(struct scsi_device *sdev, void *data)
832 {
833         scsi_internal_device_block(sdev);
834 }
835
836 /**
837  * fc_device_unblock - called by target functions to unblock a scsi device
838  * @dev:        scsi device
839  * @data:       unused
840  **/
841 static void fc_device_unblock(struct scsi_device *sdev, void *data)
842 {
843         scsi_internal_device_unblock(sdev);
844 }
845
846 /**
847  * fc_timeout_blocked_tgt - Timeout handler for blocked scsi targets
848  *                       that fail to recover in the alloted time.
849  * @data:       scsi target that failed to reappear in the alloted time.
850  **/
851 static void fc_timeout_blocked_tgt(void  *data)
852 {
853         struct scsi_target *starget = (struct scsi_target *)data;
854
855         dev_printk(KERN_ERR, &starget->dev, 
856                 "blocked target time out: target resuming\n");
857
858         /* 
859          * set the device going again ... if the scsi lld didn't
860          * unblock this device, then IO errors will probably
861          * result if the host still isn't ready.
862          */
863         starget_for_each_device(starget, NULL, fc_device_unblock);
864 }
865
866 /**
867  * fc_target_block - block a target by temporarily putting all its scsi devices
868  *              into the SDEV_BLOCK state.
869  * @starget:    scsi target managed by this fc scsi lldd.
870  *
871  * scsi lldd's with a FC transport call this routine to temporarily stop all
872  * scsi commands to all devices managed by this scsi target.  Called 
873  * from interrupt or normal process context.
874  *
875  * Returns zero if successful or error if not
876  *
877  * Notes:       
878  *      The timeout and timer types are extracted from the fc transport 
879  *      attributes from the caller's target pointer.  This routine assumes no
880  *      locks are held on entry.
881  **/
882 int
883 fc_target_block(struct scsi_target *starget)
884 {
885         int timeout = fc_starget_dev_loss_tmo(starget);
886         struct work_struct *work = &fc_starget_dev_loss_work(starget);
887
888         if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
889                 return -EINVAL;
890
891         starget_for_each_device(starget, NULL, fc_device_block);
892
893         /* The scsi lld blocks this target for the timeout period only. */
894         schedule_delayed_work(work, timeout * HZ);
895
896         return 0;
897 }
898 EXPORT_SYMBOL(fc_target_block);
899
900 /**
901  * fc_target_unblock - unblock a target following a fc_target_block request.
902  * @starget:    scsi target managed by this fc scsi lldd.       
903  *
904  * scsi lld's with a FC transport call this routine to restart IO to all 
905  * devices associated with the caller's scsi target following a fc_target_block
906  * request.  Called from interrupt or normal process context.
907  *
908  * Notes:       
909  *      This routine assumes no locks are held on entry.
910  **/
911 void
912 fc_target_unblock(struct scsi_target *starget)
913 {
914         /* 
915          * Stop the target timer first. Take no action on the del_timer
916          * failure as the state machine state change will validate the
917          * transaction. 
918          */
919         if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
920                 flush_scheduled_work();
921
922         starget_for_each_device(starget, NULL, fc_device_unblock);
923 }
924 EXPORT_SYMBOL(fc_target_unblock);
925
926 /**
927  * fc_timeout_blocked_host - Timeout handler for blocked scsi hosts
928  *                       that fail to recover in the alloted time.
929  * @data:       scsi host that failed to recover its devices in the alloted
930  *              time.
931  **/
932 static void fc_timeout_blocked_host(void  *data)
933 {
934         struct Scsi_Host *shost = (struct Scsi_Host *)data;
935         struct scsi_device *sdev;
936
937         dev_printk(KERN_ERR, &shost->shost_gendev, 
938                 "blocked host time out: host resuming\n");
939
940         shost_for_each_device(sdev, shost) {
941                 /* 
942                  * set the device going again ... if the scsi lld didn't
943                  * unblock this device, then IO errors will probably
944                  * result if the host still isn't ready.
945                  */
946                 scsi_internal_device_unblock(sdev);
947         }
948 }
949
950 /**
951  * fc_host_block - block all scsi devices managed by the calling host temporarily 
952  *              by putting each device in the SDEV_BLOCK state.
953  * @shost:      scsi host pointer that contains all scsi device siblings.
954  *
955  * scsi lld's with a FC transport call this routine to temporarily stop all
956  * scsi commands to all devices managed by this host.  Called 
957  * from interrupt or normal process context.
958  *
959  * Returns zero if successful or error if not
960  *
961  * Notes:
962  *      The timeout and timer types are extracted from the fc transport 
963  *      attributes from the caller's host pointer.  This routine assumes no
964  *      locks are held on entry.
965  **/
966 int
967 fc_host_block(struct Scsi_Host *shost)
968 {
969         struct scsi_device *sdev;
970         int timeout = fc_host_link_down_tmo(shost);
971         struct work_struct *work = &fc_host_link_down_work(shost);
972
973         if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
974                 return -EINVAL;
975
976         shost_for_each_device(sdev, shost) {
977                 scsi_internal_device_block(sdev);
978         }
979
980         schedule_delayed_work(work, timeout * HZ);
981
982         return 0;
983 }
984 EXPORT_SYMBOL(fc_host_block);
985
986 /**
987  * fc_host_unblock - unblock all devices managed by this host following a 
988  *              fc_host_block request.
989  * @shost:      scsi host containing all scsi device siblings to unblock.
990  *
991  * scsi lld's with a FC transport call this routine to restart IO to all scsi
992  * devices managed by the specified scsi host following an fc_host_block 
993  * request.  Called from interrupt or normal process context.
994  *
995  * Notes:       
996  *      This routine assumes no locks are held on entry.
997  **/
998 void
999 fc_host_unblock(struct Scsi_Host *shost)
1000 {
1001         struct scsi_device *sdev;
1002
1003         /* 
1004          * Stop the host timer first. Take no action on the del_timer
1005          * failure as the state machine state change will validate the
1006          * transaction.
1007          */
1008         if (cancel_delayed_work(&fc_host_link_down_work(shost)))
1009                 flush_scheduled_work();
1010
1011         shost_for_each_device(sdev, shost) {
1012                 scsi_internal_device_unblock(sdev);
1013         }
1014 }
1015 EXPORT_SYMBOL(fc_host_unblock);
1016
1017 MODULE_AUTHOR("Martin Hicks");
1018 MODULE_DESCRIPTION("FC Transport Attributes");
1019 MODULE_LICENSE("GPL");
1020
1021 module_init(fc_transport_init);
1022 module_exit(fc_transport_exit);