2 * FiberChannel transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
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.
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.
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
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"
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)
31 * Redefine so that we can have same named attributes in the
32 * sdev/starget/host objects.
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)
38 #define fc_enum_name_search(title, table_type, table) \
39 static const char *get_fc_##title##_name(enum table_type table_key) \
44 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
45 if (table[i].value == table_key) { \
46 name = table[i].name; \
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) \
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 */ \
66 return 1; /* failure */ \
70 /* Convert fc_port_type values to ascii string name */
72 enum fc_port_type value;
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" },
83 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
84 #define FC_PORTTYPE_MAX_NAMELEN 50
87 /* Convert fc_port_state values to ascii string name */
89 enum fc_port_state value;
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" },
101 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
102 #define FC_PORTSTATE_MAX_NAMELEN 20
105 /* Convert fc_tgtid_binding_type values to ascii string name */
107 enum fc_tgtid_binding_type value;
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 },
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
122 #define fc_bitfield_name_search(title, table) \
124 get_fc_##title##_names(u32 table_key, char *buf) \
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); \
137 len += sprintf(buf + len, "\n"); \
142 /* Convert fc_cos bit values to ascii string name */
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" },
153 fc_bitfield_name_search(cos, fc_cos_names)
156 /* Convert fc_port_speed bit values to ascii string 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" },
167 fc_bitfield_name_search(port_speed, fc_port_speed_names)
171 show_fc_fc4s (char *buf, u8 *fc4_list)
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");
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);
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"
191 #define FC_HOST_NUM_ATTRS 15
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];
204 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
205 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
208 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
210 struct class fc_transport_class = {
211 .name = "fc_transport",
212 .release = transport_class_release,
215 struct class fc_host_class = {
217 .release = host_class_release,
220 static __init int fc_transport_init(void)
222 int error = class_register(&fc_host_class);
225 return class_register(&fc_transport_class);
228 static void __exit fc_transport_exit(void)
230 class_unregister(&fc_transport_class);
231 class_unregister(&fc_host_class);
234 static int fc_setup_starget_transport_attrs(struct scsi_target *starget)
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.
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);
250 static void fc_destroy_starget(struct scsi_target *starget)
252 /* Stop the target timer */
253 if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
254 flush_scheduled_work();
257 static int fc_setup_host_transport_attrs(struct Scsi_Host *shost)
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.
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)));
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;
293 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
295 INIT_WORK(&fc_host_link_down_work(shost),
296 fc_timeout_blocked_host, shost);
300 static void fc_destroy_host(struct Scsi_Host *shost)
302 /* Stop the host timer */
303 if (cancel_delayed_work(&fc_host_link_down_work(shost)))
304 flush_scheduled_work();
307 static void transport_class_release(struct class_device *class_dev)
309 struct scsi_target *starget = transport_class_to_starget(class_dev);
310 put_device(&starget->dev);
313 static void host_class_release(struct class_device *class_dev)
315 struct Scsi_Host *shost = transport_class_to_shost(class_dev);
316 put_device(&shost->shost_gendev);
321 * Remote Port (Target) Attribute Management
324 #define fc_starget_show_function(field, format_string, cast) \
326 show_fc_starget_##field (struct class_device *cdev, char *buf) \
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)); \
337 #define fc_starget_store_function(field, format_string) \
339 store_fc_starget_##field(struct class_device *cdev, const char *buf, \
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); \
347 val = simple_strtoul(buf, NULL, 0); \
348 i->f->set_starget_##field(starget, val); \
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)
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)
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)
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) \
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; \
383 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
384 if (i->f->show_starget_##field) \
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");
396 * Host Attribute Management
399 #define fc_host_show_function(field, format_string, sz, cast) \
401 show_fc_host_##field (struct class_device *cdev, char *buf) \
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)); \
410 #define fc_host_store_function(field, format_string) \
412 store_fc_host_##field(struct class_device *cdev, const char *buf, \
416 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
417 struct fc_internal *i = to_fc_internal(shost->transportt); \
419 val = simple_strtoul(buf, NULL, 0); \
420 i->f->set_host_##field(shost, val); \
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)
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)
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)
441 #define fc_host_rd_enum_attr(title, maxlen) \
443 show_fc_host_##title (struct class_device *cdev, char *buf) \
445 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
446 struct fc_internal *i = to_fc_internal(shost->transportt); \
448 if (i->f->get_host_##title) \
449 i->f->get_host_##title(shost); \
450 name = get_fc_##title##_name(fc_host_##title(shost)); \
453 return snprintf(buf, maxlen, "%s\n", name); \
455 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
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) \
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; \
471 i->host_attrs[count] = &i->private_host_attrs[count]; \
472 if (i->f->show_host_##field) \
476 #define fc_private_host_show_function(field, format_string, sz, cast) \
478 show_fc_host_##field (struct class_device *cdev, char *buf) \
480 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
481 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
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)
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)
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]; \
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]; \
507 /* Fixed Host Attributes */
510 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
512 struct Scsi_Host *shost = transport_class_to_shost(cdev);
514 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
515 return snprintf(buf, 20, "unspecified\n");
517 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
519 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
520 show_fc_host_supported_classes, NULL);
523 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
525 struct Scsi_Host *shost = transport_class_to_shost(cdev);
526 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
528 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
529 show_fc_host_supported_fc4s, NULL);
532 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
534 struct Scsi_Host *shost = transport_class_to_shost(cdev);
536 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
537 return snprintf(buf, 20, "unknown\n");
539 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
541 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
542 show_fc_host_supported_speeds, NULL);
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));
556 /* Dynamic Host Attributes */
559 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
561 struct Scsi_Host *shost = transport_class_to_shost(cdev);
562 struct fc_internal *i = to_fc_internal(shost->transportt);
564 if (i->f->get_host_active_fc4s)
565 i->f->get_host_active_fc4s(shost);
567 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
569 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
570 show_fc_host_active_fc4s, NULL);
573 show_fc_host_speed (struct class_device *cdev, char *buf)
575 struct Scsi_Host *shost = transport_class_to_shost(cdev);
576 struct fc_internal *i = to_fc_internal(shost->transportt);
578 if (i->f->get_host_speed)
579 i->f->get_host_speed(shost);
581 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
582 return snprintf(buf, 20, "unknown\n");
584 return get_fc_port_speed_names(fc_host_speed(shost), buf);
586 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
587 show_fc_host_speed, NULL);
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);
597 /* Private Host Attributes */
600 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
602 struct Scsi_Host *shost = transport_class_to_shost(cdev);
605 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
608 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
612 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
613 const char *buf, size_t count)
615 struct Scsi_Host *shost = transport_class_to_shost(cdev);
616 enum fc_tgtid_binding_type val;
618 if (get_fc_tgtid_bind_type_match(buf, &val))
620 fc_host_tgtid_bind_type(shost) = val;
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);
629 * Host Statistics Management
632 /* Show a given an attribute in the statistics group */
634 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
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;
641 if (offset > sizeof(struct fc_host_statistics) ||
642 offset % sizeof(u64) != 0)
645 if (i->f->get_fc_host_stats) {
646 stats = (i->f->get_fc_host_stats)(shost);
648 ret = snprintf(buf, 20, "0x%llx\n",
649 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
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) \
659 return fc_stat_show(cd, buf, \
660 offsetof(struct fc_host_statistics, name)); \
662 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
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);
686 fc_reset_statistics(struct class_device *cdev, const char *buf,
689 struct Scsi_Host *shost = transport_class_to_shost(cdev);
690 struct fc_internal *i = to_fc_internal(shost->transportt);
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);
700 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
701 fc_reset_statistics);
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,
729 static struct attribute_group fc_statistics_group = {
730 .name = "statistics",
731 .attrs = fc_statistics_attrs,
736 struct scsi_transport_template *
737 fc_attach_transport(struct fc_function_template *ft)
739 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
746 memset(i, 0, sizeof(struct fc_internal));
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);
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);
760 if (ft->get_fc_host_stats)
761 i->t.host_statistics = &fc_statistics_group;
767 * setup remote port (target) attributes
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);
774 BUG_ON(count > FC_STARGET_NUM_ATTRS);
776 /* Setup the always-on attributes here */
778 i->starget_attrs[count] = NULL;
781 /* setup host attributes */
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);
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);
804 /* Transport-managed attributes */
805 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
807 BUG_ON(count > FC_HOST_NUM_ATTRS);
809 i->host_attrs[count] = NULL;
814 EXPORT_SYMBOL(fc_attach_transport);
816 void fc_release_transport(struct scsi_transport_template *t)
818 struct fc_internal *i = to_fc_internal(t);
822 EXPORT_SYMBOL(fc_release_transport);
827 * fc_device_block - called by target functions to block a scsi device
831 static void fc_device_block(struct scsi_device *sdev, void *data)
833 scsi_internal_device_block(sdev);
837 * fc_device_unblock - called by target functions to unblock a scsi device
841 static void fc_device_unblock(struct scsi_device *sdev, void *data)
843 scsi_internal_device_unblock(sdev);
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.
851 static void fc_timeout_blocked_tgt(void *data)
853 struct scsi_target *starget = (struct scsi_target *)data;
855 dev_printk(KERN_ERR, &starget->dev,
856 "blocked target time out: target resuming\n");
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.
863 starget_for_each_device(starget, NULL, fc_device_unblock);
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.
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.
875 * Returns zero if successful or error if not
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.
883 fc_target_block(struct scsi_target *starget)
885 int timeout = fc_starget_dev_loss_tmo(starget);
886 struct work_struct *work = &fc_starget_dev_loss_work(starget);
888 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
891 starget_for_each_device(starget, NULL, fc_device_block);
893 /* The scsi lld blocks this target for the timeout period only. */
894 schedule_delayed_work(work, timeout * HZ);
898 EXPORT_SYMBOL(fc_target_block);
901 * fc_target_unblock - unblock a target following a fc_target_block request.
902 * @starget: scsi target managed by this fc scsi lldd.
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.
909 * This routine assumes no locks are held on entry.
912 fc_target_unblock(struct scsi_target *starget)
915 * Stop the target timer first. Take no action on the del_timer
916 * failure as the state machine state change will validate the
919 if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
920 flush_scheduled_work();
922 starget_for_each_device(starget, NULL, fc_device_unblock);
924 EXPORT_SYMBOL(fc_target_unblock);
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
932 static void fc_timeout_blocked_host(void *data)
934 struct Scsi_Host *shost = (struct Scsi_Host *)data;
935 struct scsi_device *sdev;
937 dev_printk(KERN_ERR, &shost->shost_gendev,
938 "blocked host time out: host resuming\n");
940 shost_for_each_device(sdev, shost) {
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.
946 scsi_internal_device_unblock(sdev);
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.
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.
959 * Returns zero if successful or error if not
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.
967 fc_host_block(struct Scsi_Host *shost)
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);
973 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
976 shost_for_each_device(sdev, shost) {
977 scsi_internal_device_block(sdev);
980 schedule_delayed_work(work, timeout * HZ);
984 EXPORT_SYMBOL(fc_host_block);
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.
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.
996 * This routine assumes no locks are held on entry.
999 fc_host_unblock(struct Scsi_Host *shost)
1001 struct scsi_device *sdev;
1004 * Stop the host timer first. Take no action on the del_timer
1005 * failure as the state machine state change will validate the
1008 if (cancel_delayed_work(&fc_host_link_down_work(shost)))
1009 flush_scheduled_work();
1011 shost_for_each_device(sdev, shost) {
1012 scsi_internal_device_unblock(sdev);
1015 EXPORT_SYMBOL(fc_host_unblock);
1017 MODULE_AUTHOR("Martin Hicks");
1018 MODULE_DESCRIPTION("FC Transport Attributes");
1019 MODULE_LICENSE("GPL");
1021 module_init(fc_transport_init);
1022 module_exit(fc_transport_exit);