vserver 1.9.5.x5
[linux-2.6.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 fc_timeout_blocked_host(void *data);
184 static void fc_timeout_blocked_tgt(void *data);
185
186 #define FC_STARGET_NUM_ATTRS    4       /* increase this if you add attributes */
187 #define FC_STARGET_OTHER_ATTRS  0       /* increase this if you add "always on"
188                                          * attributes */
189 #define FC_HOST_NUM_ATTRS       15
190
191 struct fc_internal {
192         struct scsi_transport_template t;
193         struct fc_function_template *f;
194         /* The actual attributes */
195         struct class_device_attribute private_starget_attrs[
196                                                 FC_STARGET_NUM_ATTRS];
197         /* The array of null terminated pointers to attributes
198          * needed by scsi_sysfs.c */
199         struct class_device_attribute *starget_attrs[
200                         FC_STARGET_NUM_ATTRS + FC_STARGET_OTHER_ATTRS + 1];
201
202         struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
203         struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
204 };
205
206 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
207
208 static int fc_add_target(struct device *dev)
209 {
210         struct scsi_target *starget = to_scsi_target(dev);
211         /* 
212          * Set default values easily detected by the midlayer as
213          * failure cases.  The scsi lldd is responsible for initializing
214          * all transport attributes to valid values per target.
215          */
216         fc_starget_node_name(starget) = -1;
217         fc_starget_port_name(starget) = -1;
218         fc_starget_port_id(starget) = -1;
219         fc_starget_dev_loss_tmo(starget) = -1;
220         INIT_WORK(&fc_starget_dev_loss_work(starget),
221                   fc_timeout_blocked_tgt, starget);
222         return 0;
223 }
224
225 static int fc_remove_target(struct device *dev)
226 {
227         struct scsi_target *starget = to_scsi_target(dev);
228         /* Stop the target timer */
229         if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
230                 flush_scheduled_work();
231         return 0;
232 }
233
234 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
235                                "fc_transport",
236                                fc_add_target,
237                                fc_remove_target,
238                                NULL);
239
240 static int fc_add_host(struct device *dev)
241 {
242         struct Scsi_Host *shost = dev_to_shost(dev);
243         /* 
244          * Set default values easily detected by the midlayer as
245          * failure cases.  The scsi lldd is responsible for initializing
246          * all transport attributes to valid values per host.
247          */
248         fc_host_node_name(shost) = -1;
249         fc_host_port_name(shost) = -1;
250         fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
251         memset(fc_host_supported_fc4s(shost), 0,
252                 sizeof(fc_host_supported_fc4s(shost)));
253         memset(fc_host_symbolic_name(shost), 0,
254                 sizeof(fc_host_symbolic_name(shost)));
255         fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
256         fc_host_maxframe_size(shost) = -1;
257         memset(fc_host_hardware_version(shost), 0,
258                 sizeof(fc_host_hardware_version(shost)));
259         memset(fc_host_firmware_version(shost), 0,
260                 sizeof(fc_host_firmware_version(shost)));
261         memset(fc_host_serial_number(shost), 0,
262                 sizeof(fc_host_serial_number(shost)));
263         memset(fc_host_opt_rom_version(shost), 0,
264                 sizeof(fc_host_opt_rom_version(shost)));
265         memset(fc_host_driver_version(shost), 0,
266                 sizeof(fc_host_driver_version(shost)));
267
268         fc_host_port_id(shost) = -1;
269         fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
270         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
271         memset(fc_host_active_fc4s(shost), 0,
272                 sizeof(fc_host_active_fc4s(shost)));
273         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
274         fc_host_fabric_name(shost) = -1;
275         fc_host_link_down_tmo(shost) = -1;
276
277         fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
278
279         INIT_WORK(&fc_host_link_down_work(shost),
280                   fc_timeout_blocked_host, shost);
281         return 0;
282 }
283
284 static int fc_remove_host(struct device *dev)
285 {
286         struct Scsi_Host *shost = dev_to_shost(dev);
287         /* Stop the host timer */
288         if (cancel_delayed_work(&fc_host_link_down_work(shost)))
289                 flush_scheduled_work();
290         return 0;
291 }
292
293 static DECLARE_TRANSPORT_CLASS(fc_host_class,
294                                "fc_host",
295                                fc_add_host,
296                                fc_remove_host,
297                                NULL);
298
299 static __init int fc_transport_init(void)
300 {
301         int error = transport_class_register(&fc_host_class);
302         if (error)
303                 return error;
304         return transport_class_register(&fc_transport_class);
305 }
306
307 static void __exit fc_transport_exit(void)
308 {
309         transport_class_unregister(&fc_transport_class);
310         transport_class_unregister(&fc_host_class);
311 }
312
313 /*
314  * Remote Port (Target) Attribute Management
315  */
316
317 #define fc_starget_show_function(field, format_string, cast)            \
318 static ssize_t                                                          \
319 show_fc_starget_##field (struct class_device *cdev, char *buf)          \
320 {                                                                       \
321         struct scsi_target *starget = transport_class_to_starget(cdev); \
322         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
323         struct fc_internal *i = to_fc_internal(shost->transportt);      \
324         if (i->f->get_starget_##field)                                  \
325                 i->f->get_starget_##field(starget);                     \
326         return snprintf(buf, 20, format_string,                         \
327                 cast fc_starget_##field(starget));                      \
328 }
329
330 #define fc_starget_store_function(field, format_string)                 \
331 static ssize_t                                                          \
332 store_fc_starget_##field(struct class_device *cdev, const char *buf,    \
333                            size_t count)                                \
334 {                                                                       \
335         int val;                                                        \
336         struct scsi_target *starget = transport_class_to_starget(cdev); \
337         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
338         struct fc_internal *i = to_fc_internal(shost->transportt);      \
339                                                                         \
340         val = simple_strtoul(buf, NULL, 0);                             \
341         i->f->set_starget_##field(starget, val);                        \
342         return count;                                                   \
343 }
344
345 #define fc_starget_rd_attr(field, format_string)                        \
346         fc_starget_show_function(field, format_string, )                \
347 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
348                          show_fc_starget_##field, NULL)
349
350 #define fc_starget_rd_attr_cast(field, format_string, cast)             \
351         fc_starget_show_function(field, format_string, (cast))          \
352 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
353                           show_fc_starget_##field, NULL)
354
355 #define fc_starget_rw_attr(field, format_string)                        \
356         fc_starget_show_function(field, format_string, )                \
357         fc_starget_store_function(field, format_string)                 \
358 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO | S_IWUSR,          \
359                         show_fc_starget_##field,                        \
360                         store_fc_starget_##field)
361
362 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
363         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
364         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
365         i->private_starget_attrs[count].store = NULL;                   \
366         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
367         if (i->f->show_starget_##field)                                 \
368                 count++
369
370 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
371         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
372         if (!i->f->set_starget_##field) {                               \
373                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
374                 i->private_starget_attrs[count].store = NULL;           \
375         }                                                               \
376         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
377         if (i->f->show_starget_##field)                                 \
378                 count++
379
380 /* The FC Tranport Remote Port (Target) Attributes: */
381 fc_starget_rd_attr_cast(node_name, "0x%llx\n", unsigned long long);
382 fc_starget_rd_attr_cast(port_name, "0x%llx\n", unsigned long long);
383 fc_starget_rd_attr(port_id, "0x%06x\n");
384 fc_starget_rw_attr(dev_loss_tmo, "%d\n");
385
386
387
388 /*
389  * Host Attribute Management
390  */
391
392 #define fc_host_show_function(field, format_string, sz, cast)           \
393 static ssize_t                                                          \
394 show_fc_host_##field (struct class_device *cdev, char *buf)             \
395 {                                                                       \
396         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
397         struct fc_internal *i = to_fc_internal(shost->transportt);      \
398         if (i->f->get_host_##field)                                     \
399                 i->f->get_host_##field(shost);                          \
400         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
401 }
402
403 #define fc_host_store_function(field, format_string)                    \
404 static ssize_t                                                          \
405 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
406                            size_t count)                                \
407 {                                                                       \
408         int val;                                                        \
409         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
410         struct fc_internal *i = to_fc_internal(shost->transportt);      \
411                                                                         \
412         val = simple_strtoul(buf, NULL, 0);                             \
413         i->f->set_host_##field(shost, val);                             \
414         return count;                                                   \
415 }
416
417 #define fc_host_rd_attr(field, format_string, sz)                       \
418         fc_host_show_function(field, format_string, sz, )               \
419 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
420                          show_fc_host_##field, NULL)
421
422 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
423         fc_host_show_function(field, format_string, sz, (cast))         \
424 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
425                           show_fc_host_##field, NULL)
426
427 #define fc_host_rw_attr(field, format_string, sz)                       \
428         fc_host_show_function(field, format_string, sz, )               \
429         fc_host_store_function(field, format_string)                    \
430 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,             \
431                         show_fc_host_##field,                           \
432                         store_fc_host_##field)
433
434 #define fc_host_rd_enum_attr(title, maxlen)                             \
435 static ssize_t                                                          \
436 show_fc_host_##title (struct class_device *cdev, char *buf)             \
437 {                                                                       \
438         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
439         struct fc_internal *i = to_fc_internal(shost->transportt);      \
440         const char *name;                                               \
441         if (i->f->get_host_##title)                                     \
442                 i->f->get_host_##title(shost);                          \
443         name = get_fc_##title##_name(fc_host_##title(shost));           \
444         if (!name)                                                      \
445                 return -EINVAL;                                         \
446         return snprintf(buf, maxlen, "%s\n", name);                     \
447 }                                                                       \
448 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
449
450 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
451         i->private_host_attrs[count] = class_device_attr_host_##field;  \
452         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
453         i->private_host_attrs[count].store = NULL;                      \
454         i->host_attrs[count] = &i->private_host_attrs[count];           \
455         if (i->f->show_host_##field)                                    \
456                 count++
457
458 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
459         i->private_host_attrs[count] = class_device_attr_host_##field;  \
460         if (!i->f->set_host_##field) {                                  \
461                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
462                 i->private_host_attrs[count].store = NULL;              \
463         }                                                               \
464         i->host_attrs[count] = &i->private_host_attrs[count];           \
465         if (i->f->show_host_##field)                                    \
466                 count++
467
468
469 #define fc_private_host_show_function(field, format_string, sz, cast)   \
470 static ssize_t                                                          \
471 show_fc_host_##field (struct class_device *cdev, char *buf)             \
472 {                                                                       \
473         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
474         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
475 }
476
477 #define fc_private_host_rd_attr(field, format_string, sz)               \
478         fc_private_host_show_function(field, format_string, sz, )       \
479 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
480                          show_fc_host_##field, NULL)
481
482 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
483         fc_private_host_show_function(field, format_string, sz, (cast)) \
484 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
485                           show_fc_host_##field, NULL)
486
487 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
488         i->private_host_attrs[count] = class_device_attr_host_##field;  \
489         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
490         i->private_host_attrs[count].store = NULL;                      \
491         i->host_attrs[count] = &i->private_host_attrs[count];           \
492         count++
493
494 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
495         i->private_host_attrs[count] = class_device_attr_host_##field;  \
496         i->host_attrs[count] = &i->private_host_attrs[count];           \
497         count++
498
499
500 /* Fixed Host Attributes */
501
502 static ssize_t
503 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
504 {
505         struct Scsi_Host *shost = transport_class_to_shost(cdev);
506
507         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
508                 return snprintf(buf, 20, "unspecified\n");
509
510         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
511 }
512 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
513                 show_fc_host_supported_classes, NULL);
514
515 static ssize_t
516 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
517 {
518         struct Scsi_Host *shost = transport_class_to_shost(cdev);
519         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
520 }
521 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
522                 show_fc_host_supported_fc4s, NULL);
523
524 static ssize_t
525 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
526 {
527         struct Scsi_Host *shost = transport_class_to_shost(cdev);
528
529         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
530                 return snprintf(buf, 20, "unknown\n");
531
532         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
533 }
534 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
535                 show_fc_host_supported_speeds, NULL);
536
537
538 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
539 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
540 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
541 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
542 fc_private_host_rd_attr(hardware_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
543 fc_private_host_rd_attr(firmware_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
544 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
545 fc_private_host_rd_attr(opt_rom_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
546 fc_private_host_rd_attr(driver_version, "%s\n", (FC_VERSION_STRING_SIZE +1));
547
548
549 /* Dynamic Host Attributes */
550
551 static ssize_t
552 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
553 {
554         struct Scsi_Host *shost = transport_class_to_shost(cdev);
555         struct fc_internal *i = to_fc_internal(shost->transportt);
556
557         if (i->f->get_host_active_fc4s)
558                 i->f->get_host_active_fc4s(shost);
559
560         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
561 }
562 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
563                 show_fc_host_active_fc4s, NULL);
564
565 static ssize_t
566 show_fc_host_speed (struct class_device *cdev, char *buf)
567 {
568         struct Scsi_Host *shost = transport_class_to_shost(cdev);
569         struct fc_internal *i = to_fc_internal(shost->transportt);
570
571         if (i->f->get_host_speed)
572                 i->f->get_host_speed(shost);
573
574         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
575                 return snprintf(buf, 20, "unknown\n");
576
577         return get_fc_port_speed_names(fc_host_speed(shost), buf);
578 }
579 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
580                 show_fc_host_speed, NULL);
581
582
583 fc_host_rd_attr(port_id, "0x%06x\n", 20);
584 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
585 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
586 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
587 fc_host_rw_attr(link_down_tmo, "%d\n", 20);
588
589
590 /* Private Host Attributes */
591
592 static ssize_t
593 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
594 {
595         struct Scsi_Host *shost = transport_class_to_shost(cdev);
596         const char *name;
597
598         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
599         if (!name)
600                 return -EINVAL;
601         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
602 }
603
604 static ssize_t
605 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
606         const char *buf, size_t count)
607 {
608         struct Scsi_Host *shost = transport_class_to_shost(cdev);
609         enum fc_tgtid_binding_type val;
610
611         if (get_fc_tgtid_bind_type_match(buf, &val))
612                 return -EINVAL;
613         fc_host_tgtid_bind_type(shost) = val;
614         return count;
615 }
616
617 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
618                         show_fc_private_host_tgtid_bind_type,
619                         store_fc_private_host_tgtid_bind_type);
620
621 /*
622  * Host Statistics Management
623  */
624
625 /* Show a given an attribute in the statistics group */
626 static ssize_t
627 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
628 {
629         struct Scsi_Host *shost = transport_class_to_shost(cdev);
630         struct fc_internal *i = to_fc_internal(shost->transportt);
631         struct fc_host_statistics *stats;
632         ssize_t ret = -ENOENT;
633
634         if (offset > sizeof(struct fc_host_statistics) ||
635             offset % sizeof(u64) != 0)
636                 WARN_ON(1);
637
638         if (i->f->get_fc_host_stats) {
639                 stats = (i->f->get_fc_host_stats)(shost);
640                 if (stats)
641                         ret = snprintf(buf, 20, "0x%llx\n",
642                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
643         }
644         return ret;
645 }
646
647
648 /* generate a read-only statistics attribute */
649 #define fc_host_statistic(name)                                         \
650 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf)   \
651 {                                                                       \
652         return fc_stat_show(cd, buf,                                    \
653                             offsetof(struct fc_host_statistics, name)); \
654 }                                                                       \
655 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
656
657 fc_host_statistic(seconds_since_last_reset);
658 fc_host_statistic(tx_frames);
659 fc_host_statistic(tx_words);
660 fc_host_statistic(rx_frames);
661 fc_host_statistic(rx_words);
662 fc_host_statistic(lip_count);
663 fc_host_statistic(nos_count);
664 fc_host_statistic(error_frames);
665 fc_host_statistic(dumped_frames);
666 fc_host_statistic(link_failure_count);
667 fc_host_statistic(loss_of_sync_count);
668 fc_host_statistic(loss_of_signal_count);
669 fc_host_statistic(prim_seq_protocol_err_count);
670 fc_host_statistic(invalid_tx_word_count);
671 fc_host_statistic(invalid_crc_count);
672 fc_host_statistic(fcp_input_requests);
673 fc_host_statistic(fcp_output_requests);
674 fc_host_statistic(fcp_control_requests);
675 fc_host_statistic(fcp_input_megabytes);
676 fc_host_statistic(fcp_output_megabytes);
677
678 static ssize_t
679 fc_reset_statistics(struct class_device *cdev, const char *buf,
680                            size_t count)
681 {
682         struct Scsi_Host *shost = transport_class_to_shost(cdev);
683         struct fc_internal *i = to_fc_internal(shost->transportt);
684
685         /* ignore any data value written to the attribute */
686         if (i->f->reset_fc_host_stats) {
687                 i->f->reset_fc_host_stats(shost);
688                 return count;
689         }
690
691         return -ENOENT;
692 }
693 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
694                                 fc_reset_statistics);
695
696
697 static struct attribute *fc_statistics_attrs[] = {
698         &class_device_attr_host_seconds_since_last_reset.attr,
699         &class_device_attr_host_tx_frames.attr,
700         &class_device_attr_host_tx_words.attr,
701         &class_device_attr_host_rx_frames.attr,
702         &class_device_attr_host_rx_words.attr,
703         &class_device_attr_host_lip_count.attr,
704         &class_device_attr_host_nos_count.attr,
705         &class_device_attr_host_error_frames.attr,
706         &class_device_attr_host_dumped_frames.attr,
707         &class_device_attr_host_link_failure_count.attr,
708         &class_device_attr_host_loss_of_sync_count.attr,
709         &class_device_attr_host_loss_of_signal_count.attr,
710         &class_device_attr_host_prim_seq_protocol_err_count.attr,
711         &class_device_attr_host_invalid_tx_word_count.attr,
712         &class_device_attr_host_invalid_crc_count.attr,
713         &class_device_attr_host_fcp_input_requests.attr,
714         &class_device_attr_host_fcp_output_requests.attr,
715         &class_device_attr_host_fcp_control_requests.attr,
716         &class_device_attr_host_fcp_input_megabytes.attr,
717         &class_device_attr_host_fcp_output_megabytes.attr,
718         &class_device_attr_host_reset_statistics.attr,
719         NULL
720 };
721
722 static struct attribute_group fc_statistics_group = {
723         .name = "statistics",
724         .attrs = fc_statistics_attrs,
725 };
726
727 static int fc_host_match(struct attribute_container *cont,
728                           struct device *dev)
729 {
730         struct Scsi_Host *shost;
731         struct fc_internal *i;
732
733         if (!scsi_is_host_device(dev))
734                 return 0;
735
736         shost = dev_to_shost(dev);
737         if (!shost->transportt  || shost->transportt->host_attrs.class
738             != &fc_host_class.class)
739                 return 0;
740
741         i = to_fc_internal(shost->transportt);
742         
743         return &i->t.host_attrs == cont;
744 }
745
746 static int fc_target_match(struct attribute_container *cont,
747                             struct device *dev)
748 {
749         struct Scsi_Host *shost;
750         struct fc_internal *i;
751
752         if (!scsi_is_target_device(dev))
753                 return 0;
754
755         shost = dev_to_shost(dev->parent);
756         if (!shost->transportt  || shost->transportt->host_attrs.class
757             != &fc_host_class.class)
758                 return 0;
759
760         i = to_fc_internal(shost->transportt);
761         
762         return &i->t.target_attrs == cont;
763 }
764
765
766 struct scsi_transport_template *
767 fc_attach_transport(struct fc_function_template *ft)
768 {
769         struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
770                                         GFP_KERNEL);
771         int count = 0;
772
773         if (unlikely(!i))
774                 return NULL;
775
776         memset(i, 0, sizeof(struct fc_internal));
777
778         i->t.target_attrs.attrs = &i->starget_attrs[0];
779         i->t.target_attrs.class = &fc_transport_class.class;
780         i->t.target_attrs.match = fc_target_match;
781         attribute_container_register(&i->t.target_attrs);
782         i->t.target_size = sizeof(struct fc_starget_attrs);
783
784         i->t.host_attrs.attrs = &i->host_attrs[0];
785         i->t.host_attrs.class = &fc_host_class.class;
786         i->t.host_attrs.match = fc_host_match;
787         attribute_container_register(&i->t.host_attrs);
788         i->t.host_size = sizeof(struct fc_host_attrs);
789
790         if (ft->get_fc_host_stats)
791                 i->t.host_statistics = &fc_statistics_group;
792
793         i->f = ft;
794
795         
796         /*
797          * setup remote port (target) attributes
798          */
799         SETUP_STARGET_ATTRIBUTE_RD(port_id);
800         SETUP_STARGET_ATTRIBUTE_RD(port_name);
801         SETUP_STARGET_ATTRIBUTE_RD(node_name);
802         SETUP_STARGET_ATTRIBUTE_RW(dev_loss_tmo);
803
804         BUG_ON(count > FC_STARGET_NUM_ATTRS);
805
806         /* Setup the always-on attributes here */
807
808         i->starget_attrs[count] = NULL;
809
810
811         /* setup host attributes */
812         count=0;
813         SETUP_HOST_ATTRIBUTE_RD(node_name);
814         SETUP_HOST_ATTRIBUTE_RD(port_name);
815         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
816         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
817         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
818         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
819         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
820         SETUP_HOST_ATTRIBUTE_RD(hardware_version);
821         SETUP_HOST_ATTRIBUTE_RD(firmware_version);
822         SETUP_HOST_ATTRIBUTE_RD(serial_number);
823         SETUP_HOST_ATTRIBUTE_RD(opt_rom_version);
824         SETUP_HOST_ATTRIBUTE_RD(driver_version);
825
826         SETUP_HOST_ATTRIBUTE_RD(port_id);
827         SETUP_HOST_ATTRIBUTE_RD(port_type);
828         SETUP_HOST_ATTRIBUTE_RD(port_state);
829         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
830         SETUP_HOST_ATTRIBUTE_RD(speed);
831         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
832         SETUP_HOST_ATTRIBUTE_RW(link_down_tmo);
833
834         /* Transport-managed attributes */
835         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
836
837         BUG_ON(count > FC_HOST_NUM_ATTRS);
838
839         i->host_attrs[count] = NULL;
840
841
842         return &i->t;
843 }
844 EXPORT_SYMBOL(fc_attach_transport);
845
846 void fc_release_transport(struct scsi_transport_template *t)
847 {
848         struct fc_internal *i = to_fc_internal(t);
849
850         attribute_container_unregister(&i->t.target_attrs);
851         attribute_container_unregister(&i->t.host_attrs);
852
853         kfree(i);
854 }
855 EXPORT_SYMBOL(fc_release_transport);
856
857
858
859 /**
860  * fc_device_block - called by target functions to block a scsi device
861  * @dev:        scsi device
862  * @data:       unused
863  **/
864 static void fc_device_block(struct scsi_device *sdev, void *data)
865 {
866         scsi_internal_device_block(sdev);
867 }
868
869 /**
870  * fc_device_unblock - called by target functions to unblock a scsi device
871  * @dev:        scsi device
872  * @data:       unused
873  **/
874 static void fc_device_unblock(struct scsi_device *sdev, void *data)
875 {
876         scsi_internal_device_unblock(sdev);
877 }
878
879 /**
880  * fc_timeout_blocked_tgt - Timeout handler for blocked scsi targets
881  *                       that fail to recover in the alloted time.
882  * @data:       scsi target that failed to reappear in the alloted time.
883  **/
884 static void fc_timeout_blocked_tgt(void  *data)
885 {
886         struct scsi_target *starget = (struct scsi_target *)data;
887
888         dev_printk(KERN_ERR, &starget->dev, 
889                 "blocked target time out: target resuming\n");
890
891         /* 
892          * set the device going again ... if the scsi lld didn't
893          * unblock this device, then IO errors will probably
894          * result if the host still isn't ready.
895          */
896         starget_for_each_device(starget, NULL, fc_device_unblock);
897 }
898
899 /**
900  * fc_target_block - block a target by temporarily putting all its scsi devices
901  *              into the SDEV_BLOCK state.
902  * @starget:    scsi target managed by this fc scsi lldd.
903  *
904  * scsi lldd's with a FC transport call this routine to temporarily stop all
905  * scsi commands to all devices managed by this scsi target.  Called 
906  * from interrupt or normal process context.
907  *
908  * Returns zero if successful or error if not
909  *
910  * Notes:       
911  *      The timeout and timer types are extracted from the fc transport 
912  *      attributes from the caller's target pointer.  This routine assumes no
913  *      locks are held on entry.
914  **/
915 int
916 fc_target_block(struct scsi_target *starget)
917 {
918         int timeout = fc_starget_dev_loss_tmo(starget);
919         struct work_struct *work = &fc_starget_dev_loss_work(starget);
920
921         if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
922                 return -EINVAL;
923
924         starget_for_each_device(starget, NULL, fc_device_block);
925
926         /* The scsi lld blocks this target for the timeout period only. */
927         schedule_delayed_work(work, timeout * HZ);
928
929         return 0;
930 }
931 EXPORT_SYMBOL(fc_target_block);
932
933 /**
934  * fc_target_unblock - unblock a target following a fc_target_block request.
935  * @starget:    scsi target managed by this fc scsi lldd.       
936  *
937  * scsi lld's with a FC transport call this routine to restart IO to all 
938  * devices associated with the caller's scsi target following a fc_target_block
939  * request.  Called from interrupt or normal process context.
940  *
941  * Notes:       
942  *      This routine assumes no locks are held on entry.
943  **/
944 void
945 fc_target_unblock(struct scsi_target *starget)
946 {
947         /* 
948          * Stop the target timer first. Take no action on the del_timer
949          * failure as the state machine state change will validate the
950          * transaction. 
951          */
952         if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
953                 flush_scheduled_work();
954
955         starget_for_each_device(starget, NULL, fc_device_unblock);
956 }
957 EXPORT_SYMBOL(fc_target_unblock);
958
959 /**
960  * fc_timeout_blocked_host - Timeout handler for blocked scsi hosts
961  *                       that fail to recover in the alloted time.
962  * @data:       scsi host that failed to recover its devices in the alloted
963  *              time.
964  **/
965 static void fc_timeout_blocked_host(void  *data)
966 {
967         struct Scsi_Host *shost = (struct Scsi_Host *)data;
968         struct scsi_device *sdev;
969
970         dev_printk(KERN_ERR, &shost->shost_gendev, 
971                 "blocked host time out: host resuming\n");
972
973         shost_for_each_device(sdev, shost) {
974                 /* 
975                  * set the device going again ... if the scsi lld didn't
976                  * unblock this device, then IO errors will probably
977                  * result if the host still isn't ready.
978                  */
979                 scsi_internal_device_unblock(sdev);
980         }
981 }
982
983 /**
984  * fc_host_block - block all scsi devices managed by the calling host temporarily 
985  *              by putting each device in the SDEV_BLOCK state.
986  * @shost:      scsi host pointer that contains all scsi device siblings.
987  *
988  * scsi lld's with a FC transport call this routine to temporarily stop all
989  * scsi commands to all devices managed by this host.  Called 
990  * from interrupt or normal process context.
991  *
992  * Returns zero if successful or error if not
993  *
994  * Notes:
995  *      The timeout and timer types are extracted from the fc transport 
996  *      attributes from the caller's host pointer.  This routine assumes no
997  *      locks are held on entry.
998  **/
999 int
1000 fc_host_block(struct Scsi_Host *shost)
1001 {
1002         struct scsi_device *sdev;
1003         int timeout = fc_host_link_down_tmo(shost);
1004         struct work_struct *work = &fc_host_link_down_work(shost);
1005
1006         if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1007                 return -EINVAL;
1008
1009         shost_for_each_device(sdev, shost) {
1010                 scsi_internal_device_block(sdev);
1011         }
1012
1013         schedule_delayed_work(work, timeout * HZ);
1014
1015         return 0;
1016 }
1017 EXPORT_SYMBOL(fc_host_block);
1018
1019 /**
1020  * fc_host_unblock - unblock all devices managed by this host following a 
1021  *              fc_host_block request.
1022  * @shost:      scsi host containing all scsi device siblings to unblock.
1023  *
1024  * scsi lld's with a FC transport call this routine to restart IO to all scsi
1025  * devices managed by the specified scsi host following an fc_host_block 
1026  * request.  Called from interrupt or normal process context.
1027  *
1028  * Notes:       
1029  *      This routine assumes no locks are held on entry.
1030  **/
1031 void
1032 fc_host_unblock(struct Scsi_Host *shost)
1033 {
1034         struct scsi_device *sdev;
1035
1036         /* 
1037          * Stop the host timer first. Take no action on the del_timer
1038          * failure as the state machine state change will validate the
1039          * transaction.
1040          */
1041         if (cancel_delayed_work(&fc_host_link_down_work(shost)))
1042                 flush_scheduled_work();
1043
1044         shost_for_each_device(sdev, shost) {
1045                 scsi_internal_device_unblock(sdev);
1046         }
1047 }
1048 EXPORT_SYMBOL(fc_host_unblock);
1049
1050 MODULE_AUTHOR("Martin Hicks");
1051 MODULE_DESCRIPTION("FC Transport Attributes");
1052 MODULE_LICENSE("GPL");
1053
1054 module_init(fc_transport_init);
1055 module_exit(fc_transport_exit);