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
22 * Copyright (C) 2004-2005 James Smart, Emulex Corporation
23 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include "scsi_priv.h"
35 #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)
38 * Redefine so that we can have same named attributes in the
39 * sdev/starget/host objects.
41 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
42 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
43 __ATTR(_name,_mode,_show,_store)
45 #define fc_enum_name_search(title, table_type, table) \
46 static const char *get_fc_##title##_name(enum table_type table_key) \
51 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
52 if (table[i].value == table_key) { \
53 name = table[i].name; \
60 #define fc_enum_name_match(title, table_type, table) \
61 static int get_fc_##title##_match(const char *table_key, \
62 enum table_type *value) \
66 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
67 if (strncmp(table_key, table[i].name, \
68 table[i].matchlen) == 0) { \
69 *value = table[i].value; \
70 return 0; /* success */ \
73 return 1; /* failure */ \
77 /* Convert fc_port_type values to ascii string name */
79 enum fc_port_type value;
81 } fc_port_type_names[] = {
82 { FC_PORTTYPE_UNKNOWN, "Unknown" },
83 { FC_PORTTYPE_OTHER, "Other" },
84 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
85 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
86 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
87 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
88 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
90 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
91 #define FC_PORTTYPE_MAX_NAMELEN 50
94 /* Convert fc_port_state values to ascii string name */
96 enum fc_port_state value;
98 } fc_port_state_names[] = {
99 { FC_PORTSTATE_UNKNOWN, "Unknown" },
100 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
101 { FC_PORTSTATE_ONLINE, "Online" },
102 { FC_PORTSTATE_OFFLINE, "Offline" },
103 { FC_PORTSTATE_BLOCKED, "Blocked" },
104 { FC_PORTSTATE_BYPASSED, "Bypassed" },
105 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
106 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
107 { FC_PORTSTATE_ERROR, "Error" },
108 { FC_PORTSTATE_LOOPBACK, "Loopback" },
110 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
111 #define FC_PORTSTATE_MAX_NAMELEN 20
114 /* Convert fc_tgtid_binding_type values to ascii string name */
116 enum fc_tgtid_binding_type value;
119 } fc_tgtid_binding_type_names[] = {
120 { FC_TGTID_BIND_NONE, "none", 4 },
121 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
122 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
123 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
125 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
126 fc_tgtid_binding_type_names)
127 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
128 fc_tgtid_binding_type_names)
129 #define FC_BINDTYPE_MAX_NAMELEN 30
132 #define fc_bitfield_name_search(title, table) \
134 get_fc_##title##_names(u32 table_key, char *buf) \
140 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
141 if (table[i].value & table_key) { \
142 len += sprintf(buf + len, "%s%s", \
143 prefix, table[i].name); \
147 len += sprintf(buf + len, "\n"); \
152 /* Convert FC_COS bit values to ascii string name */
157 { FC_COS_CLASS1, "Class 1" },
158 { FC_COS_CLASS2, "Class 2" },
159 { FC_COS_CLASS3, "Class 3" },
160 { FC_COS_CLASS4, "Class 4" },
161 { FC_COS_CLASS6, "Class 6" },
163 fc_bitfield_name_search(cos, fc_cos_names)
166 /* Convert FC_PORTSPEED bit values to ascii string name */
170 } fc_port_speed_names[] = {
171 { FC_PORTSPEED_1GBIT, "1 Gbit" },
172 { FC_PORTSPEED_2GBIT, "2 Gbit" },
173 { FC_PORTSPEED_4GBIT, "4 Gbit" },
174 { FC_PORTSPEED_10GBIT, "10 Gbit" },
175 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
177 fc_bitfield_name_search(port_speed, fc_port_speed_names)
181 show_fc_fc4s (char *buf, u8 *fc4_list)
185 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
186 len += sprintf(buf + len , "0x%02x ", *fc4_list);
187 len += sprintf(buf + len, "\n");
192 /* Convert FC_RPORT_ROLE bit values to ascii string name */
196 } fc_remote_port_role_names[] = {
197 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
198 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
199 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
201 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
204 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
206 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
207 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
208 #define FC_FPORT_PORTID 0x00000e
209 #define FC_FABCTLR_PORTID 0x00000d
210 #define FC_DIRSRVR_PORTID 0x00000c
211 #define FC_TIMESRVR_PORTID 0x00000b
212 #define FC_MGMTSRVR_PORTID 0x00000a
215 static void fc_timeout_blocked_rport(void *data);
216 static void fc_scsi_scan_rport(void *data);
217 static void fc_rport_terminate(struct fc_rport *rport);
220 * Attribute counts pre object type...
221 * Increase these values if you add attributes
223 #define FC_STARGET_NUM_ATTRS 3
224 #define FC_RPORT_NUM_ATTRS 9
225 #define FC_HOST_NUM_ATTRS 15
228 struct scsi_transport_template t;
229 struct fc_function_template *f;
232 * For attributes : each object has :
233 * An array of the actual attributes structures
234 * An array of null-terminated pointers to the attribute
235 * structures - used for mid-layer interaction.
237 * The attribute containers for the starget and host are are
238 * part of the midlayer. As the remote port is specific to the
239 * fc transport, we must provide the attribute container.
241 struct class_device_attribute private_starget_attrs[
242 FC_STARGET_NUM_ATTRS];
243 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
245 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
246 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
248 struct transport_container rport_attr_cont;
249 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
250 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
253 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
255 static int fc_target_setup(struct transport_container *tc, struct device *dev,
256 struct class_device *cdev)
258 struct scsi_target *starget = to_scsi_target(dev);
259 struct fc_rport *rport = starget_to_rport(starget);
262 * if parent is remote port, use values from remote port.
263 * Otherwise, this host uses the fc_transport, but not the
264 * remote port interface. As such, initialize to known non-values.
267 fc_starget_node_name(starget) = rport->node_name;
268 fc_starget_port_name(starget) = rport->port_name;
269 fc_starget_port_id(starget) = rport->port_id;
271 fc_starget_node_name(starget) = -1;
272 fc_starget_port_name(starget) = -1;
273 fc_starget_port_id(starget) = -1;
279 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
285 static int fc_host_setup(struct transport_container *tc, struct device *dev,
286 struct class_device *cdev)
288 struct Scsi_Host *shost = dev_to_shost(dev);
291 * Set default values easily detected by the midlayer as
292 * failure cases. The scsi lldd is responsible for initializing
293 * all transport attributes to valid values per host.
295 fc_host_node_name(shost) = -1;
296 fc_host_port_name(shost) = -1;
297 fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
298 memset(fc_host_supported_fc4s(shost), 0,
299 sizeof(fc_host_supported_fc4s(shost)));
300 memset(fc_host_symbolic_name(shost), 0,
301 sizeof(fc_host_symbolic_name(shost)));
302 fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
303 fc_host_maxframe_size(shost) = -1;
304 memset(fc_host_serial_number(shost), 0,
305 sizeof(fc_host_serial_number(shost)));
307 fc_host_port_id(shost) = -1;
308 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
309 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
310 memset(fc_host_active_fc4s(shost), 0,
311 sizeof(fc_host_active_fc4s(shost)));
312 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
313 fc_host_fabric_name(shost) = -1;
315 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
317 INIT_LIST_HEAD(&fc_host_rports(shost));
318 INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
319 fc_host_next_rport_number(shost) = 0;
320 fc_host_next_target_id(shost) = 0;
325 static DECLARE_TRANSPORT_CLASS(fc_host_class,
332 * Setup and Remove actions for remote ports are handled
333 * in the service functions below.
335 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
346 * dev_loss_tmo: the default number of seconds that the FC transport
347 * should insulate the loss of a remote port.
348 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
350 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
352 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
353 MODULE_PARM_DESC(dev_loss_tmo,
354 "Maximum number of seconds that the FC transport should"
355 " insulate the loss of a remote port. Once this value is"
356 " exceeded, the scsi target is removed. Value should be"
357 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
360 static __init int fc_transport_init(void)
362 int error = transport_class_register(&fc_host_class);
365 error = transport_class_register(&fc_rport_class);
368 return transport_class_register(&fc_transport_class);
371 static void __exit fc_transport_exit(void)
373 transport_class_unregister(&fc_transport_class);
374 transport_class_unregister(&fc_rport_class);
375 transport_class_unregister(&fc_host_class);
379 * FC Remote Port Attribute Management
382 #define fc_rport_show_function(field, format_string, sz, cast) \
384 show_fc_rport_##field (struct class_device *cdev, char *buf) \
386 struct fc_rport *rport = transport_class_to_rport(cdev); \
387 struct Scsi_Host *shost = rport_to_shost(rport); \
388 struct fc_internal *i = to_fc_internal(shost->transportt); \
389 if (i->f->get_rport_##field) \
390 i->f->get_rport_##field(rport); \
391 return snprintf(buf, sz, format_string, cast rport->field); \
394 #define fc_rport_store_function(field) \
396 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
400 struct fc_rport *rport = transport_class_to_rport(cdev); \
401 struct Scsi_Host *shost = rport_to_shost(rport); \
402 struct fc_internal *i = to_fc_internal(shost->transportt); \
403 val = simple_strtoul(buf, NULL, 0); \
404 i->f->set_rport_##field(rport, val); \
408 #define fc_rport_rd_attr(field, format_string, sz) \
409 fc_rport_show_function(field, format_string, sz, ) \
410 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
411 show_fc_rport_##field, NULL)
413 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
414 fc_rport_show_function(field, format_string, sz, (cast)) \
415 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
416 show_fc_rport_##field, NULL)
418 #define fc_rport_rw_attr(field, format_string, sz) \
419 fc_rport_show_function(field, format_string, sz, ) \
420 fc_rport_store_function(field) \
421 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
422 show_fc_rport_##field, \
423 store_fc_rport_##field)
426 #define fc_private_rport_show_function(field, format_string, sz, cast) \
428 show_fc_rport_##field (struct class_device *cdev, char *buf) \
430 struct fc_rport *rport = transport_class_to_rport(cdev); \
431 return snprintf(buf, sz, format_string, cast rport->field); \
434 #define fc_private_rport_rd_attr(field, format_string, sz) \
435 fc_private_rport_show_function(field, format_string, sz, ) \
436 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
437 show_fc_rport_##field, NULL)
439 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
440 fc_private_rport_show_function(field, format_string, sz, (cast)) \
441 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
442 show_fc_rport_##field, NULL)
445 #define fc_private_rport_rd_enum_attr(title, maxlen) \
447 show_fc_rport_##title (struct class_device *cdev, char *buf) \
449 struct fc_rport *rport = transport_class_to_rport(cdev); \
451 name = get_fc_##title##_name(rport->title); \
454 return snprintf(buf, maxlen, "%s\n", name); \
456 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
457 show_fc_rport_##title, NULL)
460 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
461 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
462 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
463 i->private_rport_attrs[count].store = NULL; \
464 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
465 if (i->f->show_rport_##field) \
468 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
469 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
470 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
471 i->private_rport_attrs[count].store = NULL; \
472 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
475 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
476 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
477 if (!i->f->set_rport_##field) { \
478 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
479 i->private_rport_attrs[count].store = NULL; \
481 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
482 if (i->f->show_rport_##field) \
486 /* The FC Transport Remote Port Attributes: */
488 /* Fixed Remote Port Attributes */
490 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
493 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
495 struct fc_rport *rport = transport_class_to_rport(cdev);
496 if (rport->supported_classes == FC_COS_UNSPECIFIED)
497 return snprintf(buf, 20, "unspecified\n");
498 return get_fc_cos_names(rport->supported_classes, buf);
500 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
501 show_fc_rport_supported_classes, NULL);
503 /* Dynamic Remote Port Attributes */
505 fc_rport_rw_attr(dev_loss_tmo, "%d\n", 20);
508 /* Private Remote Port Attributes */
510 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
511 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
512 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
515 show_fc_rport_roles (struct class_device *cdev, char *buf)
517 struct fc_rport *rport = transport_class_to_rport(cdev);
519 /* identify any roles that are port_id specific */
520 if ((rport->port_id != -1) &&
521 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
522 FC_WELLKNOWN_PORTID_MASK) {
523 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
524 case FC_FPORT_PORTID:
525 return snprintf(buf, 30, "Fabric Port\n");
526 case FC_FABCTLR_PORTID:
527 return snprintf(buf, 30, "Fabric Controller\n");
528 case FC_DIRSRVR_PORTID:
529 return snprintf(buf, 30, "Directory Server\n");
530 case FC_TIMESRVR_PORTID:
531 return snprintf(buf, 30, "Time Server\n");
532 case FC_MGMTSRVR_PORTID:
533 return snprintf(buf, 30, "Management Server\n");
535 return snprintf(buf, 30, "Unknown Fabric Entity\n");
538 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
539 return snprintf(buf, 20, "unknown\n");
540 return get_fc_remote_port_roles_names(rport->roles, buf);
543 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
544 show_fc_rport_roles, NULL);
546 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
547 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
552 * FC SCSI Target Attribute Management
556 * Note: in the target show function we recognize when the remote
557 * port is in the heirarchy and do not allow the driver to get
558 * involved in sysfs functions. The driver only gets involved if
559 * it's the "old" style that doesn't use rports.
561 #define fc_starget_show_function(field, format_string, sz, cast) \
563 show_fc_starget_##field (struct class_device *cdev, char *buf) \
565 struct scsi_target *starget = transport_class_to_starget(cdev); \
566 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
567 struct fc_internal *i = to_fc_internal(shost->transportt); \
568 struct fc_rport *rport = starget_to_rport(starget); \
570 fc_starget_##field(starget) = rport->field; \
571 else if (i->f->get_starget_##field) \
572 i->f->get_starget_##field(starget); \
573 return snprintf(buf, sz, format_string, \
574 cast fc_starget_##field(starget)); \
577 #define fc_starget_rd_attr(field, format_string, sz) \
578 fc_starget_show_function(field, format_string, sz, ) \
579 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
580 show_fc_starget_##field, NULL)
582 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
583 fc_starget_show_function(field, format_string, sz, (cast)) \
584 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
585 show_fc_starget_##field, NULL)
587 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
588 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
589 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
590 i->private_starget_attrs[count].store = NULL; \
591 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
592 if (i->f->show_starget_##field) \
595 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
596 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
597 if (!i->f->set_starget_##field) { \
598 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
599 i->private_starget_attrs[count].store = NULL; \
601 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
602 if (i->f->show_starget_##field) \
605 /* The FC Transport SCSI Target Attributes: */
606 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
607 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
608 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
612 * Host Attribute Management
615 #define fc_host_show_function(field, format_string, sz, cast) \
617 show_fc_host_##field (struct class_device *cdev, char *buf) \
619 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
620 struct fc_internal *i = to_fc_internal(shost->transportt); \
621 if (i->f->get_host_##field) \
622 i->f->get_host_##field(shost); \
623 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
626 #define fc_host_store_function(field) \
628 store_fc_host_##field(struct class_device *cdev, const char *buf, \
632 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
633 struct fc_internal *i = to_fc_internal(shost->transportt); \
635 val = simple_strtoul(buf, NULL, 0); \
636 i->f->set_host_##field(shost, val); \
640 #define fc_host_rd_attr(field, format_string, sz) \
641 fc_host_show_function(field, format_string, sz, ) \
642 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
643 show_fc_host_##field, NULL)
645 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
646 fc_host_show_function(field, format_string, sz, (cast)) \
647 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
648 show_fc_host_##field, NULL)
650 #define fc_host_rw_attr(field, format_string, sz) \
651 fc_host_show_function(field, format_string, sz, ) \
652 fc_host_store_function(field) \
653 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
654 show_fc_host_##field, \
655 store_fc_host_##field)
657 #define fc_host_rd_enum_attr(title, maxlen) \
659 show_fc_host_##title (struct class_device *cdev, char *buf) \
661 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
662 struct fc_internal *i = to_fc_internal(shost->transportt); \
664 if (i->f->get_host_##title) \
665 i->f->get_host_##title(shost); \
666 name = get_fc_##title##_name(fc_host_##title(shost)); \
669 return snprintf(buf, maxlen, "%s\n", name); \
671 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
673 #define SETUP_HOST_ATTRIBUTE_RD(field) \
674 i->private_host_attrs[count] = class_device_attr_host_##field; \
675 i->private_host_attrs[count].attr.mode = S_IRUGO; \
676 i->private_host_attrs[count].store = NULL; \
677 i->host_attrs[count] = &i->private_host_attrs[count]; \
678 if (i->f->show_host_##field) \
681 #define SETUP_HOST_ATTRIBUTE_RW(field) \
682 i->private_host_attrs[count] = class_device_attr_host_##field; \
683 if (!i->f->set_host_##field) { \
684 i->private_host_attrs[count].attr.mode = S_IRUGO; \
685 i->private_host_attrs[count].store = NULL; \
687 i->host_attrs[count] = &i->private_host_attrs[count]; \
688 if (i->f->show_host_##field) \
692 #define fc_private_host_show_function(field, format_string, sz, cast) \
694 show_fc_host_##field (struct class_device *cdev, char *buf) \
696 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
697 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
700 #define fc_private_host_rd_attr(field, format_string, sz) \
701 fc_private_host_show_function(field, format_string, sz, ) \
702 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
703 show_fc_host_##field, NULL)
705 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
706 fc_private_host_show_function(field, format_string, sz, (cast)) \
707 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
708 show_fc_host_##field, NULL)
710 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
711 i->private_host_attrs[count] = class_device_attr_host_##field; \
712 i->private_host_attrs[count].attr.mode = S_IRUGO; \
713 i->private_host_attrs[count].store = NULL; \
714 i->host_attrs[count] = &i->private_host_attrs[count]; \
717 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
718 i->private_host_attrs[count] = class_device_attr_host_##field; \
719 i->host_attrs[count] = &i->private_host_attrs[count]; \
723 /* Fixed Host Attributes */
726 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
728 struct Scsi_Host *shost = transport_class_to_shost(cdev);
730 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
731 return snprintf(buf, 20, "unspecified\n");
733 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
735 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
736 show_fc_host_supported_classes, NULL);
739 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
741 struct Scsi_Host *shost = transport_class_to_shost(cdev);
742 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
744 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
745 show_fc_host_supported_fc4s, NULL);
748 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
750 struct Scsi_Host *shost = transport_class_to_shost(cdev);
752 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
753 return snprintf(buf, 20, "unknown\n");
755 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
757 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
758 show_fc_host_supported_speeds, NULL);
761 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
762 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
763 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
764 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
765 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
768 /* Dynamic Host Attributes */
771 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
773 struct Scsi_Host *shost = transport_class_to_shost(cdev);
774 struct fc_internal *i = to_fc_internal(shost->transportt);
776 if (i->f->get_host_active_fc4s)
777 i->f->get_host_active_fc4s(shost);
779 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
781 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
782 show_fc_host_active_fc4s, NULL);
785 show_fc_host_speed (struct class_device *cdev, char *buf)
787 struct Scsi_Host *shost = transport_class_to_shost(cdev);
788 struct fc_internal *i = to_fc_internal(shost->transportt);
790 if (i->f->get_host_speed)
791 i->f->get_host_speed(shost);
793 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
794 return snprintf(buf, 20, "unknown\n");
796 return get_fc_port_speed_names(fc_host_speed(shost), buf);
798 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
799 show_fc_host_speed, NULL);
802 fc_host_rd_attr(port_id, "0x%06x\n", 20);
803 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
804 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
805 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
808 /* Private Host Attributes */
811 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
813 struct Scsi_Host *shost = transport_class_to_shost(cdev);
816 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
819 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
823 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
824 const char *buf, size_t count)
826 struct Scsi_Host *shost = transport_class_to_shost(cdev);
827 struct fc_rport *rport, *next_rport;
828 enum fc_tgtid_binding_type val;
831 if (get_fc_tgtid_bind_type_match(buf, &val))
834 /* if changing bind type, purge all unused consistent bindings */
835 if (val != fc_host_tgtid_bind_type(shost)) {
836 spin_lock_irqsave(shost->host_lock, flags);
837 list_for_each_entry_safe(rport, next_rport,
838 &fc_host_rport_bindings(shost), peers)
839 fc_rport_terminate(rport);
840 spin_unlock_irqrestore(shost->host_lock, flags);
843 fc_host_tgtid_bind_type(shost) = val;
847 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
848 show_fc_private_host_tgtid_bind_type,
849 store_fc_private_host_tgtid_bind_type);
852 * Host Statistics Management
855 /* Show a given an attribute in the statistics group */
857 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
859 struct Scsi_Host *shost = transport_class_to_shost(cdev);
860 struct fc_internal *i = to_fc_internal(shost->transportt);
861 struct fc_host_statistics *stats;
862 ssize_t ret = -ENOENT;
864 if (offset > sizeof(struct fc_host_statistics) ||
865 offset % sizeof(u64) != 0)
868 if (i->f->get_fc_host_stats) {
869 stats = (i->f->get_fc_host_stats)(shost);
871 ret = snprintf(buf, 20, "0x%llx\n",
872 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
878 /* generate a read-only statistics attribute */
879 #define fc_host_statistic(name) \
880 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
882 return fc_stat_show(cd, buf, \
883 offsetof(struct fc_host_statistics, name)); \
885 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
887 fc_host_statistic(seconds_since_last_reset);
888 fc_host_statistic(tx_frames);
889 fc_host_statistic(tx_words);
890 fc_host_statistic(rx_frames);
891 fc_host_statistic(rx_words);
892 fc_host_statistic(lip_count);
893 fc_host_statistic(nos_count);
894 fc_host_statistic(error_frames);
895 fc_host_statistic(dumped_frames);
896 fc_host_statistic(link_failure_count);
897 fc_host_statistic(loss_of_sync_count);
898 fc_host_statistic(loss_of_signal_count);
899 fc_host_statistic(prim_seq_protocol_err_count);
900 fc_host_statistic(invalid_tx_word_count);
901 fc_host_statistic(invalid_crc_count);
902 fc_host_statistic(fcp_input_requests);
903 fc_host_statistic(fcp_output_requests);
904 fc_host_statistic(fcp_control_requests);
905 fc_host_statistic(fcp_input_megabytes);
906 fc_host_statistic(fcp_output_megabytes);
909 fc_reset_statistics(struct class_device *cdev, const char *buf,
912 struct Scsi_Host *shost = transport_class_to_shost(cdev);
913 struct fc_internal *i = to_fc_internal(shost->transportt);
915 /* ignore any data value written to the attribute */
916 if (i->f->reset_fc_host_stats) {
917 i->f->reset_fc_host_stats(shost);
923 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
924 fc_reset_statistics);
927 static struct attribute *fc_statistics_attrs[] = {
928 &class_device_attr_host_seconds_since_last_reset.attr,
929 &class_device_attr_host_tx_frames.attr,
930 &class_device_attr_host_tx_words.attr,
931 &class_device_attr_host_rx_frames.attr,
932 &class_device_attr_host_rx_words.attr,
933 &class_device_attr_host_lip_count.attr,
934 &class_device_attr_host_nos_count.attr,
935 &class_device_attr_host_error_frames.attr,
936 &class_device_attr_host_dumped_frames.attr,
937 &class_device_attr_host_link_failure_count.attr,
938 &class_device_attr_host_loss_of_sync_count.attr,
939 &class_device_attr_host_loss_of_signal_count.attr,
940 &class_device_attr_host_prim_seq_protocol_err_count.attr,
941 &class_device_attr_host_invalid_tx_word_count.attr,
942 &class_device_attr_host_invalid_crc_count.attr,
943 &class_device_attr_host_fcp_input_requests.attr,
944 &class_device_attr_host_fcp_output_requests.attr,
945 &class_device_attr_host_fcp_control_requests.attr,
946 &class_device_attr_host_fcp_input_megabytes.attr,
947 &class_device_attr_host_fcp_output_megabytes.attr,
948 &class_device_attr_host_reset_statistics.attr,
952 static struct attribute_group fc_statistics_group = {
953 .name = "statistics",
954 .attrs = fc_statistics_attrs,
957 static int fc_host_match(struct attribute_container *cont,
960 struct Scsi_Host *shost;
961 struct fc_internal *i;
963 if (!scsi_is_host_device(dev))
966 shost = dev_to_shost(dev);
967 if (!shost->transportt || shost->transportt->host_attrs.ac.class
968 != &fc_host_class.class)
971 i = to_fc_internal(shost->transportt);
973 return &i->t.host_attrs.ac == cont;
976 static int fc_target_match(struct attribute_container *cont,
979 struct Scsi_Host *shost;
980 struct fc_internal *i;
982 if (!scsi_is_target_device(dev))
985 shost = dev_to_shost(dev->parent);
986 if (!shost->transportt || shost->transportt->host_attrs.ac.class
987 != &fc_host_class.class)
990 i = to_fc_internal(shost->transportt);
992 return &i->t.target_attrs.ac == cont;
995 static void fc_rport_dev_release(struct device *dev)
997 struct fc_rport *rport = dev_to_rport(dev);
998 put_device(dev->parent);
1002 int scsi_is_fc_rport(const struct device *dev)
1004 return dev->release == fc_rport_dev_release;
1006 EXPORT_SYMBOL(scsi_is_fc_rport);
1008 static int fc_rport_match(struct attribute_container *cont,
1011 struct Scsi_Host *shost;
1012 struct fc_internal *i;
1014 if (!scsi_is_fc_rport(dev))
1017 shost = dev_to_shost(dev->parent);
1018 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1019 != &fc_host_class.class)
1022 i = to_fc_internal(shost->transportt);
1024 return &i->rport_attr_cont.ac == cont;
1027 struct scsi_transport_template *
1028 fc_attach_transport(struct fc_function_template *ft)
1030 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1037 memset(i, 0, sizeof(struct fc_internal));
1039 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1040 i->t.target_attrs.ac.class = &fc_transport_class.class;
1041 i->t.target_attrs.ac.match = fc_target_match;
1042 i->t.target_size = sizeof(struct fc_starget_attrs);
1043 transport_container_register(&i->t.target_attrs);
1045 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1046 i->t.host_attrs.ac.class = &fc_host_class.class;
1047 i->t.host_attrs.ac.match = fc_host_match;
1048 i->t.host_size = sizeof(struct fc_host_attrs);
1049 if (ft->get_fc_host_stats)
1050 i->t.host_attrs.statistics = &fc_statistics_group;
1051 transport_container_register(&i->t.host_attrs);
1053 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1054 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1055 i->rport_attr_cont.ac.match = fc_rport_match;
1056 transport_container_register(&i->rport_attr_cont);
1060 /* Transport uses the shost workq for scsi scanning */
1061 i->t.create_work_queue = 1;
1064 * Setup SCSI Target Attributes.
1067 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1068 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1069 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1071 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1073 i->starget_attrs[count] = NULL;
1077 * Setup SCSI Host Attributes.
1080 SETUP_HOST_ATTRIBUTE_RD(node_name);
1081 SETUP_HOST_ATTRIBUTE_RD(port_name);
1082 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1083 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1084 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1085 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1086 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1087 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1089 SETUP_HOST_ATTRIBUTE_RD(port_id);
1090 SETUP_HOST_ATTRIBUTE_RD(port_type);
1091 SETUP_HOST_ATTRIBUTE_RD(port_state);
1092 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1093 SETUP_HOST_ATTRIBUTE_RD(speed);
1094 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1096 /* Transport-managed attributes */
1097 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1099 BUG_ON(count > FC_HOST_NUM_ATTRS);
1101 i->host_attrs[count] = NULL;
1104 * Setup Remote Port Attributes.
1107 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1108 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1109 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1110 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1111 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1112 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1113 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1114 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1115 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1117 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1119 i->rport_attrs[count] = NULL;
1123 EXPORT_SYMBOL(fc_attach_transport);
1125 void fc_release_transport(struct scsi_transport_template *t)
1127 struct fc_internal *i = to_fc_internal(t);
1129 transport_container_unregister(&i->t.target_attrs);
1130 transport_container_unregister(&i->t.host_attrs);
1131 transport_container_unregister(&i->rport_attr_cont);
1135 EXPORT_SYMBOL(fc_release_transport);
1139 * fc_remove_host - called to terminate any fc_transport-related elements
1141 * @rport: remote port to be unblocked.
1143 * This routine is expected to be called immediately preceeding the
1144 * a driver's call to scsi_remove_host().
1146 * WARNING: A driver utilizing the fc_transport, which fails to call
1147 * this routine prior to scsi_remote_host(), will leave dangling
1148 * objects in /sys/class/fc_remote_ports. Access to any of these
1149 * objects can result in a system crash !!!
1152 * This routine assumes no locks are held on entry.
1155 fc_remove_host(struct Scsi_Host *shost)
1157 struct fc_rport *rport, *next_rport;
1159 /* Remove any remote ports */
1160 list_for_each_entry_safe(rport, next_rport,
1161 &fc_host_rports(shost), peers)
1162 fc_rport_terminate(rport);
1163 list_for_each_entry_safe(rport, next_rport,
1164 &fc_host_rport_bindings(shost), peers)
1165 fc_rport_terminate(rport);
1167 EXPORT_SYMBOL(fc_remove_host);
1170 * fc_rport_create - allocates and creates a remote FC port.
1171 * @shost: scsi host the remote port is connected to.
1172 * @channel: Channel on shost port connected to.
1173 * @ids: The world wide names, fc address, and FC4 port
1174 * roles for the remote port.
1176 * Allocates and creates the remoter port structure, including the
1177 * class and sysfs creation.
1180 * This routine assumes no locks are held on entry.
1183 fc_rport_create(struct Scsi_Host *shost, int channel,
1184 struct fc_rport_identifiers *ids)
1186 struct fc_host_attrs *fc_host =
1187 (struct fc_host_attrs *)shost->shost_data;
1188 struct fc_internal *fci = to_fc_internal(shost->transportt);
1189 struct fc_rport *rport;
1191 unsigned long flags;
1195 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1196 rport = kmalloc(size, GFP_KERNEL);
1197 if (unlikely(!rport)) {
1198 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1201 memset(rport, 0, size);
1203 rport->maxframe_size = -1;
1204 rport->supported_classes = FC_COS_UNSPECIFIED;
1205 rport->dev_loss_tmo = fc_dev_loss_tmo;
1206 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1207 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1208 rport->port_id = ids->port_id;
1209 rport->roles = ids->roles;
1210 rport->port_state = FC_PORTSTATE_ONLINE;
1211 if (fci->f->dd_fcrport_size)
1212 rport->dd_data = &rport[1];
1213 rport->channel = channel;
1215 INIT_WORK(&rport->dev_loss_work, fc_timeout_blocked_rport, rport);
1216 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1218 spin_lock_irqsave(shost->host_lock, flags);
1220 rport->number = fc_host->next_rport_number++;
1221 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1222 rport->scsi_target_id = fc_host->next_target_id++;
1224 rport->scsi_target_id = -1;
1225 list_add_tail(&rport->peers, &fc_host_rports(shost));
1226 get_device(&shost->shost_gendev);
1228 spin_unlock_irqrestore(shost->host_lock, flags);
1231 device_initialize(dev);
1232 dev->parent = get_device(&shost->shost_gendev);
1233 dev->release = fc_rport_dev_release;
1234 sprintf(dev->bus_id, "rport-%d:%d-%d",
1235 shost->host_no, channel, rport->number);
1236 transport_setup_device(dev);
1238 error = device_add(dev);
1240 printk(KERN_ERR "FC Remote Port device_add failed\n");
1243 transport_add_device(dev);
1244 transport_configure_device(dev);
1246 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1247 /* initiate a scan of the target */
1248 scsi_queue_work(shost, &rport->scan_work);
1253 transport_destroy_device(dev);
1254 put_device(dev->parent);
1255 spin_lock_irqsave(shost->host_lock, flags);
1256 list_del(&rport->peers);
1257 put_device(&shost->shost_gendev);
1258 spin_unlock_irqrestore(shost->host_lock, flags);
1259 put_device(dev->parent);
1265 * fc_remote_port_add - notifies the fc transport of the existence
1266 * of a remote FC port.
1267 * @shost: scsi host the remote port is connected to.
1268 * @channel: Channel on shost port connected to.
1269 * @ids: The world wide names, fc address, and FC4 port
1270 * roles for the remote port.
1272 * The LLDD calls this routine to notify the transport of the existence
1273 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1274 * of the port, it's FC address (port_id), and the FC4 roles that are
1275 * active for the port.
1277 * For ports that are FCP targets (aka scsi targets), the FC transport
1278 * maintains consistent target id bindings on behalf of the LLDD.
1279 * A consistent target id binding is an assignment of a target id to
1280 * a remote port identifier, which persists while the scsi host is
1281 * attached. The remote port can disappear, then later reappear, and
1282 * it's target id assignment remains the same. This allows for shifts
1283 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1284 * changes to the scsi subsystem which is based on scsi host number and
1285 * target id values. Bindings are only valid during the attachment of
1286 * the scsi host. If the host detaches, then later re-attaches, target
1287 * id bindings may change.
1289 * This routine is responsible for returning a remote port structure.
1290 * The routine will search the list of remote ports it maintains
1291 * internally on behalf of consistent target id mappings. If found, the
1292 * remote port structure will be reused. Otherwise, a new remote port
1293 * structure will be allocated.
1295 * Whenever a remote port is allocated, a new fc_remote_port class
1296 * device is created.
1298 * Should not be called from interrupt context.
1301 * This routine assumes no locks are held on entry.
1304 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1305 struct fc_rport_identifiers *ids)
1307 struct fc_rport *rport;
1308 unsigned long flags;
1311 if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1312 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1314 /* search for a matching consistent binding */
1316 spin_lock_irqsave(shost->host_lock, flags);
1318 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1320 if (rport->channel != channel)
1323 switch (fc_host_tgtid_bind_type(shost)) {
1324 case FC_TGTID_BIND_BY_WWPN:
1325 if (rport->port_name == ids->port_name)
1328 case FC_TGTID_BIND_BY_WWNN:
1329 if (rport->node_name == ids->node_name)
1332 case FC_TGTID_BIND_BY_ID:
1333 if (rport->port_id == ids->port_id)
1336 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1341 list_move_tail(&rport->peers,
1342 &fc_host_rports(shost));
1347 spin_unlock_irqrestore(shost->host_lock, flags);
1350 memcpy(&rport->node_name, &ids->node_name,
1351 sizeof(rport->node_name));
1352 memcpy(&rport->port_name, &ids->port_name,
1353 sizeof(rport->port_name));
1354 rport->port_id = ids->port_id;
1355 rport->roles = ids->roles;
1356 rport->port_state = FC_PORTSTATE_ONLINE;
1358 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1359 /* initiate a scan of the target */
1360 scsi_queue_work(shost, &rport->scan_work);
1366 /* No consistent binding found - create new remote port entry */
1367 rport = fc_rport_create(shost, channel, ids);
1371 EXPORT_SYMBOL(fc_remote_port_add);
1374 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1375 * @rport: The remote port to be operated on
1378 fc_rport_tgt_remove(struct fc_rport *rport)
1380 struct Scsi_Host *shost = rport_to_shost(rport);
1382 scsi_target_unblock(&rport->dev);
1384 /* Stop anything on the workq */
1385 if (!cancel_delayed_work(&rport->dev_loss_work))
1386 flush_scheduled_work();
1387 scsi_flush_work(shost);
1389 scsi_remove_target(&rport->dev);
1393 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1394 * @rport: The remote port to be terminated
1397 * This routine assumes no locks are held on entry.
1400 fc_rport_terminate(struct fc_rport *rport)
1402 struct Scsi_Host *shost = rport_to_shost(rport);
1403 struct device *dev = &rport->dev;
1404 unsigned long flags;
1406 fc_rport_tgt_remove(rport);
1408 transport_remove_device(dev);
1410 transport_destroy_device(dev);
1411 spin_lock_irqsave(shost->host_lock, flags);
1412 list_del(&rport->peers);
1413 spin_unlock_irqrestore(shost->host_lock, flags);
1414 put_device(&shost->shost_gendev);
1418 * fc_remote_port_delete - notifies the fc transport that a remote
1419 * port is no longer in existence.
1420 * @rport: The remote port that no longer exists
1422 * The LLDD calls this routine to notify the transport that a remote
1423 * port is no longer part of the topology. Note: Although a port
1424 * may no longer be part of the topology, it may persist in the remote
1425 * ports displayed by the fc_host. This is done so that target id
1426 * mappings (managed via the remote port structures), are always visible
1427 * as long as the mapping is valid, regardless of port state,
1429 * If the remote port is not an FCP Target, it will be fully torn down
1430 * and deallocated, including the fc_remote_port class device.
1432 * If the remote port is an FCP Target, the port structure will be
1433 * marked as Not Present, but will remain as long as there is a valid
1434 * SCSI target id mapping associated with the port structure. Validity
1435 * is determined by the binding type. If binding by wwpn, then the port
1436 * structure is always valid and will not be deallocated until the host
1437 * is removed. If binding by wwnn, then the port structure is valid
1438 * until another port with the same node name is found in the topology.
1439 * If binding by port id (fc address), then the port structure is valid
1440 * valid until another port with the same address is identified.
1442 * Called from interrupt or normal process context.
1445 * This routine assumes no locks are held on entry.
1448 fc_remote_port_delete(struct fc_rport *rport)
1450 struct Scsi_Host *shost = rport_to_shost(rport);
1451 unsigned long flags;
1453 /* If no scsi target id mapping or consistent binding type, delete it */
1454 if ((rport->scsi_target_id == -1) ||
1455 (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE)) {
1456 fc_rport_terminate(rport);
1460 fc_rport_tgt_remove(rport);
1462 spin_lock_irqsave(shost->host_lock, flags);
1463 list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1464 spin_unlock_irqrestore(shost->host_lock, flags);
1467 * Note: We do not remove or clear the hostdata area. This allows
1468 * host-specific target data to persist along with the
1469 * scsi_target_id. It's up to the host to manage it's hostdata area.
1473 * Reinitialize port attributes that may change if the port comes back.
1475 rport->maxframe_size = -1;
1476 rport->supported_classes = FC_COS_UNSPECIFIED;
1477 rport->roles = FC_RPORT_ROLE_UNKNOWN;
1478 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1480 /* remove the identifiers that aren't used in the consisting binding */
1481 switch (fc_host_tgtid_bind_type(shost)) {
1482 case FC_TGTID_BIND_BY_WWPN:
1483 rport->node_name = -1;
1484 rport->port_id = -1;
1486 case FC_TGTID_BIND_BY_WWNN:
1487 rport->port_name = -1;
1488 rport->port_id = -1;
1490 case FC_TGTID_BIND_BY_ID:
1491 rport->node_name = -1;
1492 rport->port_name = -1;
1494 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1498 EXPORT_SYMBOL(fc_remote_port_delete);
1501 * fc_remote_port_rolechg - notifies the fc transport that the roles
1502 * on a remote may have changed.
1503 * @rport: The remote port that changed.
1505 * The LLDD calls this routine to notify the transport that the roles
1506 * on a remote port may have changed. The largest effect of this is
1507 * if a port now becomes a FCP Target, it must be allocated a
1508 * scsi target id. If the port is no longer a FCP target, any
1509 * scsi target id value assigned to it will persist in case the
1510 * role changes back to include FCP Target. No changes in the scsi
1511 * midlayer will be invoked if the role changes (in the expectation
1512 * that the role will be resumed. If it doesn't normal error processing
1515 * Should not be called from interrupt context.
1518 * This routine assumes no locks are held on entry.
1521 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1523 struct Scsi_Host *shost = rport_to_shost(rport);
1524 struct fc_host_attrs *fc_host =
1525 (struct fc_host_attrs *)shost->shost_data;
1526 unsigned long flags;
1529 rport->roles = roles;
1531 spin_lock_irqsave(shost->host_lock, flags);
1532 if ((rport->scsi_target_id == -1) &&
1533 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
1534 rport->scsi_target_id = fc_host->next_target_id++;
1537 spin_unlock_irqrestore(shost->host_lock, flags);
1540 /* initiate a scan of the target */
1541 scsi_queue_work(shost, &rport->scan_work);
1543 EXPORT_SYMBOL(fc_remote_port_rolechg);
1546 * fc_timeout_blocked_rport - Timeout handler for blocked remote port
1547 * that fails to return in the alloted time.
1548 * @data: scsi target that failed to reappear in the alloted time.
1551 fc_timeout_blocked_rport(void *data)
1553 struct fc_rport *rport = (struct fc_rport *)data;
1555 rport->port_state = FC_PORTSTATE_OFFLINE;
1557 dev_printk(KERN_ERR, &rport->dev,
1558 "blocked FC remote port time out: removing target\n");
1561 * As this only occurs if the remote port (scsi target)
1562 * went away and didn't come back - we'll remove
1563 * all attached scsi devices.
1565 scsi_target_unblock(&rport->dev);
1566 scsi_remove_target(&rport->dev);
1570 * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
1571 * @rport: remote port to be blocked.
1573 * scsi lldd's with a FC transport call this routine to temporarily stop
1574 * all scsi traffic to a remote port. If the port is not a SCSI target,
1575 * no action is taken. If the port is a SCSI target, all attached devices
1576 * are placed into a SDEV_BLOCK state and a timer is started. The timer is
1577 * represents the maximum amount of time the port may be blocked. If the
1578 * timer expires, the port is considered non-existent and the attached
1579 * scsi devices will be removed.
1581 * Called from interrupt or normal process context.
1583 * Returns zero if successful or error if not
1586 * This routine assumes no locks are held on entry.
1588 * The timeout and timer types are extracted from the fc transport
1589 * attributes from the caller's rport pointer.
1592 fc_remote_port_block(struct fc_rport *rport)
1594 int timeout = rport->dev_loss_tmo;
1595 struct work_struct *work = &rport->dev_loss_work;
1597 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1600 scsi_target_block(&rport->dev);
1602 /* cap the length the devices can be blocked */
1603 schedule_delayed_work(work, timeout * HZ);
1605 rport->port_state = FC_PORTSTATE_BLOCKED;
1608 EXPORT_SYMBOL(fc_remote_port_block);
1611 * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
1612 * @rport: remote port to be unblocked.
1614 * scsi lld's with a FC transport call this routine to restart IO to all
1615 * devices associated with the caller's scsi target following a fc_target_block
1616 * request. Called from interrupt or normal process context.
1619 * This routine assumes no locks are held on entry.
1622 fc_remote_port_unblock(struct fc_rport *rport)
1624 struct work_struct *work = &rport->dev_loss_work;
1625 struct Scsi_Host *shost = rport_to_shost(rport);
1628 * Stop the target timer first. Take no action on the del_timer
1629 * failure as the state machine state change will validate the
1632 if (!cancel_delayed_work(work))
1633 flush_scheduled_work();
1635 if (rport->port_state == FC_PORTSTATE_OFFLINE)
1637 * initiate a scan of the target as the target has
1640 scsi_queue_work(shost, &rport->scan_work);
1642 scsi_target_unblock(&rport->dev);
1644 rport->port_state = FC_PORTSTATE_ONLINE;
1646 EXPORT_SYMBOL(fc_remote_port_unblock);
1649 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1650 * @data: remote port to be scanned.
1653 fc_scsi_scan_rport(void *data)
1655 struct fc_rport *rport = (struct fc_rport *)data;
1657 scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1662 MODULE_AUTHOR("Martin Hicks");
1663 MODULE_DESCRIPTION("FC Transport Attributes");
1664 MODULE_LICENSE("GPL");
1666 module_init(fc_transport_init);
1667 module_exit(fc_transport_exit);