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 fc_timeout_blocked_host(void *data);
184 static void fc_timeout_blocked_tgt(void *data);
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"
189 #define FC_HOST_NUM_ATTRS 15
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];
202 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
203 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
206 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
208 static int fc_add_target(struct device *dev)
210 struct scsi_target *starget = to_scsi_target(dev);
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.
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);
225 static int fc_remove_target(struct device *dev)
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();
234 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
240 static int fc_add_host(struct device *dev)
242 struct Scsi_Host *shost = dev_to_shost(dev);
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.
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)));
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;
277 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
279 INIT_WORK(&fc_host_link_down_work(shost),
280 fc_timeout_blocked_host, shost);
284 static int fc_remove_host(struct device *dev)
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();
293 static DECLARE_TRANSPORT_CLASS(fc_host_class,
299 static __init int fc_transport_init(void)
301 int error = transport_class_register(&fc_host_class);
304 return transport_class_register(&fc_transport_class);
307 static void __exit fc_transport_exit(void)
309 transport_class_unregister(&fc_transport_class);
310 transport_class_unregister(&fc_host_class);
314 * Remote Port (Target) Attribute Management
317 #define fc_starget_show_function(field, format_string, cast) \
319 show_fc_starget_##field (struct class_device *cdev, char *buf) \
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)); \
330 #define fc_starget_store_function(field, format_string) \
332 store_fc_starget_##field(struct class_device *cdev, const char *buf, \
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); \
340 val = simple_strtoul(buf, NULL, 0); \
341 i->f->set_starget_##field(starget, val); \
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)
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)
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)
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) \
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; \
376 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
377 if (i->f->show_starget_##field) \
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");
389 * Host Attribute Management
392 #define fc_host_show_function(field, format_string, sz, cast) \
394 show_fc_host_##field (struct class_device *cdev, char *buf) \
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)); \
403 #define fc_host_store_function(field, format_string) \
405 store_fc_host_##field(struct class_device *cdev, const char *buf, \
409 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
410 struct fc_internal *i = to_fc_internal(shost->transportt); \
412 val = simple_strtoul(buf, NULL, 0); \
413 i->f->set_host_##field(shost, val); \
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)
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)
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)
434 #define fc_host_rd_enum_attr(title, maxlen) \
436 show_fc_host_##title (struct class_device *cdev, char *buf) \
438 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
439 struct fc_internal *i = to_fc_internal(shost->transportt); \
441 if (i->f->get_host_##title) \
442 i->f->get_host_##title(shost); \
443 name = get_fc_##title##_name(fc_host_##title(shost)); \
446 return snprintf(buf, maxlen, "%s\n", name); \
448 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
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) \
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; \
464 i->host_attrs[count] = &i->private_host_attrs[count]; \
465 if (i->f->show_host_##field) \
469 #define fc_private_host_show_function(field, format_string, sz, cast) \
471 show_fc_host_##field (struct class_device *cdev, char *buf) \
473 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
474 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
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)
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)
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]; \
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]; \
500 /* Fixed Host Attributes */
503 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
505 struct Scsi_Host *shost = transport_class_to_shost(cdev);
507 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
508 return snprintf(buf, 20, "unspecified\n");
510 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
512 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
513 show_fc_host_supported_classes, NULL);
516 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
518 struct Scsi_Host *shost = transport_class_to_shost(cdev);
519 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
521 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
522 show_fc_host_supported_fc4s, NULL);
525 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
527 struct Scsi_Host *shost = transport_class_to_shost(cdev);
529 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
530 return snprintf(buf, 20, "unknown\n");
532 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
534 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
535 show_fc_host_supported_speeds, NULL);
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));
549 /* Dynamic Host Attributes */
552 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
554 struct Scsi_Host *shost = transport_class_to_shost(cdev);
555 struct fc_internal *i = to_fc_internal(shost->transportt);
557 if (i->f->get_host_active_fc4s)
558 i->f->get_host_active_fc4s(shost);
560 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
562 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
563 show_fc_host_active_fc4s, NULL);
566 show_fc_host_speed (struct class_device *cdev, char *buf)
568 struct Scsi_Host *shost = transport_class_to_shost(cdev);
569 struct fc_internal *i = to_fc_internal(shost->transportt);
571 if (i->f->get_host_speed)
572 i->f->get_host_speed(shost);
574 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
575 return snprintf(buf, 20, "unknown\n");
577 return get_fc_port_speed_names(fc_host_speed(shost), buf);
579 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
580 show_fc_host_speed, NULL);
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);
590 /* Private Host Attributes */
593 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
595 struct Scsi_Host *shost = transport_class_to_shost(cdev);
598 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
601 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
605 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
606 const char *buf, size_t count)
608 struct Scsi_Host *shost = transport_class_to_shost(cdev);
609 enum fc_tgtid_binding_type val;
611 if (get_fc_tgtid_bind_type_match(buf, &val))
613 fc_host_tgtid_bind_type(shost) = val;
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);
622 * Host Statistics Management
625 /* Show a given an attribute in the statistics group */
627 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
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;
634 if (offset > sizeof(struct fc_host_statistics) ||
635 offset % sizeof(u64) != 0)
638 if (i->f->get_fc_host_stats) {
639 stats = (i->f->get_fc_host_stats)(shost);
641 ret = snprintf(buf, 20, "0x%llx\n",
642 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
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) \
652 return fc_stat_show(cd, buf, \
653 offsetof(struct fc_host_statistics, name)); \
655 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
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);
679 fc_reset_statistics(struct class_device *cdev, const char *buf,
682 struct Scsi_Host *shost = transport_class_to_shost(cdev);
683 struct fc_internal *i = to_fc_internal(shost->transportt);
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);
693 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
694 fc_reset_statistics);
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,
722 static struct attribute_group fc_statistics_group = {
723 .name = "statistics",
724 .attrs = fc_statistics_attrs,
727 static int fc_host_match(struct attribute_container *cont,
730 struct Scsi_Host *shost;
731 struct fc_internal *i;
733 if (!scsi_is_host_device(dev))
736 shost = dev_to_shost(dev);
737 if (!shost->transportt || shost->transportt->host_attrs.class
738 != &fc_host_class.class)
741 i = to_fc_internal(shost->transportt);
743 return &i->t.host_attrs == cont;
746 static int fc_target_match(struct attribute_container *cont,
749 struct Scsi_Host *shost;
750 struct fc_internal *i;
752 if (!scsi_is_target_device(dev))
755 shost = dev_to_shost(dev->parent);
756 if (!shost->transportt || shost->transportt->host_attrs.class
757 != &fc_host_class.class)
760 i = to_fc_internal(shost->transportt);
762 return &i->t.target_attrs == cont;
766 struct scsi_transport_template *
767 fc_attach_transport(struct fc_function_template *ft)
769 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
776 memset(i, 0, sizeof(struct fc_internal));
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);
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);
790 if (ft->get_fc_host_stats)
791 i->t.host_statistics = &fc_statistics_group;
797 * setup remote port (target) attributes
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);
804 BUG_ON(count > FC_STARGET_NUM_ATTRS);
806 /* Setup the always-on attributes here */
808 i->starget_attrs[count] = NULL;
811 /* setup host attributes */
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);
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);
834 /* Transport-managed attributes */
835 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
837 BUG_ON(count > FC_HOST_NUM_ATTRS);
839 i->host_attrs[count] = NULL;
844 EXPORT_SYMBOL(fc_attach_transport);
846 void fc_release_transport(struct scsi_transport_template *t)
848 struct fc_internal *i = to_fc_internal(t);
850 attribute_container_unregister(&i->t.target_attrs);
851 attribute_container_unregister(&i->t.host_attrs);
855 EXPORT_SYMBOL(fc_release_transport);
860 * fc_device_block - called by target functions to block a scsi device
864 static void fc_device_block(struct scsi_device *sdev, void *data)
866 scsi_internal_device_block(sdev);
870 * fc_device_unblock - called by target functions to unblock a scsi device
874 static void fc_device_unblock(struct scsi_device *sdev, void *data)
876 scsi_internal_device_unblock(sdev);
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.
884 static void fc_timeout_blocked_tgt(void *data)
886 struct scsi_target *starget = (struct scsi_target *)data;
888 dev_printk(KERN_ERR, &starget->dev,
889 "blocked target time out: target resuming\n");
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.
896 starget_for_each_device(starget, NULL, fc_device_unblock);
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.
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.
908 * Returns zero if successful or error if not
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.
916 fc_target_block(struct scsi_target *starget)
918 int timeout = fc_starget_dev_loss_tmo(starget);
919 struct work_struct *work = &fc_starget_dev_loss_work(starget);
921 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
924 starget_for_each_device(starget, NULL, fc_device_block);
926 /* The scsi lld blocks this target for the timeout period only. */
927 schedule_delayed_work(work, timeout * HZ);
931 EXPORT_SYMBOL(fc_target_block);
934 * fc_target_unblock - unblock a target following a fc_target_block request.
935 * @starget: scsi target managed by this fc scsi lldd.
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.
942 * This routine assumes no locks are held on entry.
945 fc_target_unblock(struct scsi_target *starget)
948 * Stop the target timer first. Take no action on the del_timer
949 * failure as the state machine state change will validate the
952 if (cancel_delayed_work(&fc_starget_dev_loss_work(starget)))
953 flush_scheduled_work();
955 starget_for_each_device(starget, NULL, fc_device_unblock);
957 EXPORT_SYMBOL(fc_target_unblock);
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
965 static void fc_timeout_blocked_host(void *data)
967 struct Scsi_Host *shost = (struct Scsi_Host *)data;
968 struct scsi_device *sdev;
970 dev_printk(KERN_ERR, &shost->shost_gendev,
971 "blocked host time out: host resuming\n");
973 shost_for_each_device(sdev, shost) {
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.
979 scsi_internal_device_unblock(sdev);
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.
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.
992 * Returns zero if successful or error if not
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.
1000 fc_host_block(struct Scsi_Host *shost)
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);
1006 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1009 shost_for_each_device(sdev, shost) {
1010 scsi_internal_device_block(sdev);
1013 schedule_delayed_work(work, timeout * HZ);
1017 EXPORT_SYMBOL(fc_host_block);
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.
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.
1029 * This routine assumes no locks are held on entry.
1032 fc_host_unblock(struct Scsi_Host *shost)
1034 struct scsi_device *sdev;
1037 * Stop the host timer first. Take no action on the del_timer
1038 * failure as the state machine state change will validate the
1041 if (cancel_delayed_work(&fc_host_link_down_work(shost)))
1042 flush_scheduled_work();
1044 shost_for_each_device(sdev, shost) {
1045 scsi_internal_device_unblock(sdev);
1048 EXPORT_SYMBOL(fc_host_unblock);
1050 MODULE_AUTHOR("Martin Hicks");
1051 MODULE_DESCRIPTION("FC Transport Attributes");
1052 MODULE_LICENSE("GPL");
1054 module_init(fc_transport_init);
1055 module_exit(fc_transport_exit);