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"
36 * Redefine so that we can have same named attributes in the
37 * sdev/starget/host objects.
39 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
40 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
41 __ATTR(_name,_mode,_show,_store)
43 #define fc_enum_name_search(title, table_type, table) \
44 static const char *get_fc_##title##_name(enum table_type table_key) \
49 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
50 if (table[i].value == table_key) { \
51 name = table[i].name; \
58 #define fc_enum_name_match(title, table_type, table) \
59 static int get_fc_##title##_match(const char *table_key, \
60 enum table_type *value) \
64 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
65 if (strncmp(table_key, table[i].name, \
66 table[i].matchlen) == 0) { \
67 *value = table[i].value; \
68 return 0; /* success */ \
71 return 1; /* failure */ \
75 /* Convert fc_port_type values to ascii string name */
77 enum fc_port_type value;
79 } fc_port_type_names[] = {
80 { FC_PORTTYPE_UNKNOWN, "Unknown" },
81 { FC_PORTTYPE_OTHER, "Other" },
82 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
83 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
84 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
85 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
86 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
88 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
89 #define FC_PORTTYPE_MAX_NAMELEN 50
92 /* Convert fc_port_state values to ascii string name */
94 enum fc_port_state value;
96 } fc_port_state_names[] = {
97 { FC_PORTSTATE_UNKNOWN, "Unknown" },
98 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
99 { FC_PORTSTATE_ONLINE, "Online" },
100 { FC_PORTSTATE_OFFLINE, "Offline" },
101 { FC_PORTSTATE_BLOCKED, "Blocked" },
102 { FC_PORTSTATE_BYPASSED, "Bypassed" },
103 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
104 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
105 { FC_PORTSTATE_ERROR, "Error" },
106 { FC_PORTSTATE_LOOPBACK, "Loopback" },
108 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
109 #define FC_PORTSTATE_MAX_NAMELEN 20
112 /* Convert fc_tgtid_binding_type values to ascii string name */
114 enum fc_tgtid_binding_type value;
117 } fc_tgtid_binding_type_names[] = {
118 { FC_TGTID_BIND_NONE, "none", 4 },
119 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
120 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
121 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
123 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
124 fc_tgtid_binding_type_names)
125 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
126 fc_tgtid_binding_type_names)
127 #define FC_BINDTYPE_MAX_NAMELEN 30
130 #define fc_bitfield_name_search(title, table) \
132 get_fc_##title##_names(u32 table_key, char *buf) \
138 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
139 if (table[i].value & table_key) { \
140 len += sprintf(buf + len, "%s%s", \
141 prefix, table[i].name); \
145 len += sprintf(buf + len, "\n"); \
150 /* Convert FC_COS bit values to ascii string name */
155 { FC_COS_CLASS1, "Class 1" },
156 { FC_COS_CLASS2, "Class 2" },
157 { FC_COS_CLASS3, "Class 3" },
158 { FC_COS_CLASS4, "Class 4" },
159 { FC_COS_CLASS6, "Class 6" },
161 fc_bitfield_name_search(cos, fc_cos_names)
164 /* Convert FC_PORTSPEED bit values to ascii string name */
168 } fc_port_speed_names[] = {
169 { FC_PORTSPEED_1GBIT, "1 Gbit" },
170 { FC_PORTSPEED_2GBIT, "2 Gbit" },
171 { FC_PORTSPEED_4GBIT, "4 Gbit" },
172 { FC_PORTSPEED_10GBIT, "10 Gbit" },
173 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
175 fc_bitfield_name_search(port_speed, fc_port_speed_names)
179 show_fc_fc4s (char *buf, u8 *fc4_list)
183 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
184 len += sprintf(buf + len , "0x%02x ", *fc4_list);
185 len += sprintf(buf + len, "\n");
190 /* Convert FC_RPORT_ROLE bit values to ascii string name */
194 } fc_remote_port_role_names[] = {
195 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
196 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
197 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
199 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
202 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
204 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
205 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
206 #define FC_FPORT_PORTID 0x00000e
207 #define FC_FABCTLR_PORTID 0x00000d
208 #define FC_DIRSRVR_PORTID 0x00000c
209 #define FC_TIMESRVR_PORTID 0x00000b
210 #define FC_MGMTSRVR_PORTID 0x00000a
213 static void fc_timeout_deleted_rport(void *data);
214 static void fc_scsi_scan_rport(void *data);
215 static void fc_rport_terminate(struct fc_rport *rport);
218 * Attribute counts pre object type...
219 * Increase these values if you add attributes
221 #define FC_STARGET_NUM_ATTRS 3
222 #define FC_RPORT_NUM_ATTRS 9
223 #define FC_HOST_NUM_ATTRS 16
226 struct scsi_transport_template t;
227 struct fc_function_template *f;
230 * For attributes : each object has :
231 * An array of the actual attributes structures
232 * An array of null-terminated pointers to the attribute
233 * structures - used for mid-layer interaction.
235 * The attribute containers for the starget and host are are
236 * part of the midlayer. As the remote port is specific to the
237 * fc transport, we must provide the attribute container.
239 struct class_device_attribute private_starget_attrs[
240 FC_STARGET_NUM_ATTRS];
241 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
243 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
244 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
246 struct transport_container rport_attr_cont;
247 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
248 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
251 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
253 static int fc_target_setup(struct transport_container *tc, struct device *dev,
254 struct class_device *cdev)
256 struct scsi_target *starget = to_scsi_target(dev);
257 struct fc_rport *rport = starget_to_rport(starget);
260 * if parent is remote port, use values from remote port.
261 * Otherwise, this host uses the fc_transport, but not the
262 * remote port interface. As such, initialize to known non-values.
265 fc_starget_node_name(starget) = rport->node_name;
266 fc_starget_port_name(starget) = rport->port_name;
267 fc_starget_port_id(starget) = rport->port_id;
269 fc_starget_node_name(starget) = -1;
270 fc_starget_port_name(starget) = -1;
271 fc_starget_port_id(starget) = -1;
277 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
283 static int fc_host_setup(struct transport_container *tc, struct device *dev,
284 struct class_device *cdev)
286 struct Scsi_Host *shost = dev_to_shost(dev);
289 * Set default values easily detected by the midlayer as
290 * failure cases. The scsi lldd is responsible for initializing
291 * all transport attributes to valid values per host.
293 fc_host_node_name(shost) = -1;
294 fc_host_port_name(shost) = -1;
295 fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
296 memset(fc_host_supported_fc4s(shost), 0,
297 sizeof(fc_host_supported_fc4s(shost)));
298 memset(fc_host_symbolic_name(shost), 0,
299 sizeof(fc_host_symbolic_name(shost)));
300 fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
301 fc_host_maxframe_size(shost) = -1;
302 memset(fc_host_serial_number(shost), 0,
303 sizeof(fc_host_serial_number(shost)));
305 fc_host_port_id(shost) = -1;
306 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
307 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
308 memset(fc_host_active_fc4s(shost), 0,
309 sizeof(fc_host_active_fc4s(shost)));
310 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
311 fc_host_fabric_name(shost) = -1;
313 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
315 INIT_LIST_HEAD(&fc_host_rports(shost));
316 INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
317 fc_host_next_rport_number(shost) = 0;
318 fc_host_next_target_id(shost) = 0;
323 static DECLARE_TRANSPORT_CLASS(fc_host_class,
330 * Setup and Remove actions for remote ports are handled
331 * in the service functions below.
333 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
344 * dev_loss_tmo: the default number of seconds that the FC transport
345 * should insulate the loss of a remote port.
346 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
348 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
350 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
351 MODULE_PARM_DESC(dev_loss_tmo,
352 "Maximum number of seconds that the FC transport should"
353 " insulate the loss of a remote port. Once this value is"
354 " exceeded, the scsi target is removed. Value should be"
355 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
358 static __init int fc_transport_init(void)
360 int error = transport_class_register(&fc_host_class);
363 error = transport_class_register(&fc_rport_class);
366 return transport_class_register(&fc_transport_class);
369 static void __exit fc_transport_exit(void)
371 transport_class_unregister(&fc_transport_class);
372 transport_class_unregister(&fc_rport_class);
373 transport_class_unregister(&fc_host_class);
377 * FC Remote Port Attribute Management
380 #define fc_rport_show_function(field, format_string, sz, cast) \
382 show_fc_rport_##field (struct class_device *cdev, char *buf) \
384 struct fc_rport *rport = transport_class_to_rport(cdev); \
385 struct Scsi_Host *shost = rport_to_shost(rport); \
386 struct fc_internal *i = to_fc_internal(shost->transportt); \
387 if ((i->f->get_rport_##field) && \
388 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
389 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
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 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
404 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
406 val = simple_strtoul(buf, NULL, 0); \
407 i->f->set_rport_##field(rport, val); \
411 #define fc_rport_rd_attr(field, format_string, sz) \
412 fc_rport_show_function(field, format_string, sz, ) \
413 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
414 show_fc_rport_##field, NULL)
416 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
417 fc_rport_show_function(field, format_string, sz, (cast)) \
418 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
419 show_fc_rport_##field, NULL)
421 #define fc_rport_rw_attr(field, format_string, sz) \
422 fc_rport_show_function(field, format_string, sz, ) \
423 fc_rport_store_function(field) \
424 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
425 show_fc_rport_##field, \
426 store_fc_rport_##field)
429 #define fc_private_rport_show_function(field, format_string, sz, cast) \
431 show_fc_rport_##field (struct class_device *cdev, char *buf) \
433 struct fc_rport *rport = transport_class_to_rport(cdev); \
434 return snprintf(buf, sz, format_string, cast rport->field); \
437 #define fc_private_rport_rd_attr(field, format_string, sz) \
438 fc_private_rport_show_function(field, format_string, sz, ) \
439 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
440 show_fc_rport_##field, NULL)
442 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
443 fc_private_rport_show_function(field, format_string, sz, (cast)) \
444 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
445 show_fc_rport_##field, NULL)
448 #define fc_private_rport_rd_enum_attr(title, maxlen) \
450 show_fc_rport_##title (struct class_device *cdev, char *buf) \
452 struct fc_rport *rport = transport_class_to_rport(cdev); \
454 name = get_fc_##title##_name(rport->title); \
457 return snprintf(buf, maxlen, "%s\n", name); \
459 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
460 show_fc_rport_##title, NULL)
463 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
464 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
465 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
466 i->private_rport_attrs[count].store = NULL; \
467 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
468 if (i->f->show_rport_##field) \
471 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
472 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
473 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
474 i->private_rport_attrs[count].store = NULL; \
475 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
478 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
479 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
480 if (!i->f->set_rport_##field) { \
481 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
482 i->private_rport_attrs[count].store = NULL; \
484 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
485 if (i->f->show_rport_##field) \
489 /* The FC Transport Remote Port Attributes: */
491 /* Fixed Remote Port Attributes */
493 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
496 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
498 struct fc_rport *rport = transport_class_to_rport(cdev);
499 if (rport->supported_classes == FC_COS_UNSPECIFIED)
500 return snprintf(buf, 20, "unspecified\n");
501 return get_fc_cos_names(rport->supported_classes, buf);
503 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
504 show_fc_rport_supported_classes, NULL);
506 /* Dynamic Remote Port Attributes */
509 * dev_loss_tmo attribute
511 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
513 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
517 struct fc_rport *rport = transport_class_to_rport(cdev);
518 struct Scsi_Host *shost = rport_to_shost(rport);
519 struct fc_internal *i = to_fc_internal(shost->transportt);
520 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
521 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
523 val = simple_strtoul(buf, NULL, 0);
524 if ((val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
526 i->f->set_rport_dev_loss_tmo(rport, val);
529 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
530 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
533 /* Private Remote Port Attributes */
535 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
536 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
537 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
540 show_fc_rport_roles (struct class_device *cdev, char *buf)
542 struct fc_rport *rport = transport_class_to_rport(cdev);
544 /* identify any roles that are port_id specific */
545 if ((rport->port_id != -1) &&
546 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
547 FC_WELLKNOWN_PORTID_MASK) {
548 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
549 case FC_FPORT_PORTID:
550 return snprintf(buf, 30, "Fabric Port\n");
551 case FC_FABCTLR_PORTID:
552 return snprintf(buf, 30, "Fabric Controller\n");
553 case FC_DIRSRVR_PORTID:
554 return snprintf(buf, 30, "Directory Server\n");
555 case FC_TIMESRVR_PORTID:
556 return snprintf(buf, 30, "Time Server\n");
557 case FC_MGMTSRVR_PORTID:
558 return snprintf(buf, 30, "Management Server\n");
560 return snprintf(buf, 30, "Unknown Fabric Entity\n");
563 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
564 return snprintf(buf, 20, "unknown\n");
565 return get_fc_remote_port_roles_names(rport->roles, buf);
568 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
569 show_fc_rport_roles, NULL);
571 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
572 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
577 * FC SCSI Target Attribute Management
581 * Note: in the target show function we recognize when the remote
582 * port is in the heirarchy and do not allow the driver to get
583 * involved in sysfs functions. The driver only gets involved if
584 * it's the "old" style that doesn't use rports.
586 #define fc_starget_show_function(field, format_string, sz, cast) \
588 show_fc_starget_##field (struct class_device *cdev, char *buf) \
590 struct scsi_target *starget = transport_class_to_starget(cdev); \
591 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
592 struct fc_internal *i = to_fc_internal(shost->transportt); \
593 struct fc_rport *rport = starget_to_rport(starget); \
595 fc_starget_##field(starget) = rport->field; \
596 else if (i->f->get_starget_##field) \
597 i->f->get_starget_##field(starget); \
598 return snprintf(buf, sz, format_string, \
599 cast fc_starget_##field(starget)); \
602 #define fc_starget_rd_attr(field, format_string, sz) \
603 fc_starget_show_function(field, format_string, sz, ) \
604 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
605 show_fc_starget_##field, NULL)
607 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
608 fc_starget_show_function(field, format_string, sz, (cast)) \
609 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
610 show_fc_starget_##field, NULL)
612 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
613 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
614 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
615 i->private_starget_attrs[count].store = NULL; \
616 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
617 if (i->f->show_starget_##field) \
620 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
621 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
622 if (!i->f->set_starget_##field) { \
623 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
624 i->private_starget_attrs[count].store = NULL; \
626 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
627 if (i->f->show_starget_##field) \
630 /* The FC Transport SCSI Target Attributes: */
631 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
632 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
633 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
637 * Host Attribute Management
640 #define fc_host_show_function(field, format_string, sz, cast) \
642 show_fc_host_##field (struct class_device *cdev, char *buf) \
644 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
645 struct fc_internal *i = to_fc_internal(shost->transportt); \
646 if (i->f->get_host_##field) \
647 i->f->get_host_##field(shost); \
648 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
651 #define fc_host_store_function(field) \
653 store_fc_host_##field(struct class_device *cdev, const char *buf, \
657 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
658 struct fc_internal *i = to_fc_internal(shost->transportt); \
660 val = simple_strtoul(buf, NULL, 0); \
661 i->f->set_host_##field(shost, val); \
665 #define fc_host_rd_attr(field, format_string, sz) \
666 fc_host_show_function(field, format_string, sz, ) \
667 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
668 show_fc_host_##field, NULL)
670 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
671 fc_host_show_function(field, format_string, sz, (cast)) \
672 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
673 show_fc_host_##field, NULL)
675 #define fc_host_rw_attr(field, format_string, sz) \
676 fc_host_show_function(field, format_string, sz, ) \
677 fc_host_store_function(field) \
678 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
679 show_fc_host_##field, \
680 store_fc_host_##field)
682 #define fc_host_rd_enum_attr(title, maxlen) \
684 show_fc_host_##title (struct class_device *cdev, char *buf) \
686 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
687 struct fc_internal *i = to_fc_internal(shost->transportt); \
689 if (i->f->get_host_##title) \
690 i->f->get_host_##title(shost); \
691 name = get_fc_##title##_name(fc_host_##title(shost)); \
694 return snprintf(buf, maxlen, "%s\n", name); \
696 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
698 #define SETUP_HOST_ATTRIBUTE_RD(field) \
699 i->private_host_attrs[count] = class_device_attr_host_##field; \
700 i->private_host_attrs[count].attr.mode = S_IRUGO; \
701 i->private_host_attrs[count].store = NULL; \
702 i->host_attrs[count] = &i->private_host_attrs[count]; \
703 if (i->f->show_host_##field) \
706 #define SETUP_HOST_ATTRIBUTE_RW(field) \
707 i->private_host_attrs[count] = class_device_attr_host_##field; \
708 if (!i->f->set_host_##field) { \
709 i->private_host_attrs[count].attr.mode = S_IRUGO; \
710 i->private_host_attrs[count].store = NULL; \
712 i->host_attrs[count] = &i->private_host_attrs[count]; \
713 if (i->f->show_host_##field) \
717 #define fc_private_host_show_function(field, format_string, sz, cast) \
719 show_fc_host_##field (struct class_device *cdev, char *buf) \
721 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
722 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
725 #define fc_private_host_rd_attr(field, format_string, sz) \
726 fc_private_host_show_function(field, format_string, sz, ) \
727 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
728 show_fc_host_##field, NULL)
730 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
731 fc_private_host_show_function(field, format_string, sz, (cast)) \
732 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
733 show_fc_host_##field, NULL)
735 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
736 i->private_host_attrs[count] = class_device_attr_host_##field; \
737 i->private_host_attrs[count].attr.mode = S_IRUGO; \
738 i->private_host_attrs[count].store = NULL; \
739 i->host_attrs[count] = &i->private_host_attrs[count]; \
742 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
744 i->private_host_attrs[count] = class_device_attr_host_##field; \
745 i->host_attrs[count] = &i->private_host_attrs[count]; \
750 /* Fixed Host Attributes */
753 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
755 struct Scsi_Host *shost = transport_class_to_shost(cdev);
757 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
758 return snprintf(buf, 20, "unspecified\n");
760 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
762 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
763 show_fc_host_supported_classes, NULL);
766 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
768 struct Scsi_Host *shost = transport_class_to_shost(cdev);
769 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
771 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
772 show_fc_host_supported_fc4s, NULL);
775 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
777 struct Scsi_Host *shost = transport_class_to_shost(cdev);
779 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
780 return snprintf(buf, 20, "unknown\n");
782 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
784 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
785 show_fc_host_supported_speeds, NULL);
788 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
789 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
790 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
791 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
792 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
795 /* Dynamic Host Attributes */
798 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
800 struct Scsi_Host *shost = transport_class_to_shost(cdev);
801 struct fc_internal *i = to_fc_internal(shost->transportt);
803 if (i->f->get_host_active_fc4s)
804 i->f->get_host_active_fc4s(shost);
806 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
808 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
809 show_fc_host_active_fc4s, NULL);
812 show_fc_host_speed (struct class_device *cdev, char *buf)
814 struct Scsi_Host *shost = transport_class_to_shost(cdev);
815 struct fc_internal *i = to_fc_internal(shost->transportt);
817 if (i->f->get_host_speed)
818 i->f->get_host_speed(shost);
820 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
821 return snprintf(buf, 20, "unknown\n");
823 return get_fc_port_speed_names(fc_host_speed(shost), buf);
825 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
826 show_fc_host_speed, NULL);
829 fc_host_rd_attr(port_id, "0x%06x\n", 20);
830 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
831 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
832 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
835 /* Private Host Attributes */
838 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
840 struct Scsi_Host *shost = transport_class_to_shost(cdev);
843 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
846 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
849 #define get_list_head_entry(pos, head, member) \
850 pos = list_entry((head)->next, typeof(*pos), member)
853 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
854 const char *buf, size_t count)
856 struct Scsi_Host *shost = transport_class_to_shost(cdev);
857 struct fc_rport *rport;
858 enum fc_tgtid_binding_type val;
861 if (get_fc_tgtid_bind_type_match(buf, &val))
864 /* if changing bind type, purge all unused consistent bindings */
865 if (val != fc_host_tgtid_bind_type(shost)) {
866 spin_lock_irqsave(shost->host_lock, flags);
867 while (!list_empty(&fc_host_rport_bindings(shost))) {
868 get_list_head_entry(rport,
869 &fc_host_rport_bindings(shost), peers);
870 spin_unlock_irqrestore(shost->host_lock, flags);
871 fc_rport_terminate(rport);
872 spin_lock_irqsave(shost->host_lock, flags);
874 spin_unlock_irqrestore(shost->host_lock, flags);
877 fc_host_tgtid_bind_type(shost) = val;
881 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
882 show_fc_private_host_tgtid_bind_type,
883 store_fc_private_host_tgtid_bind_type);
886 store_fc_private_host_issue_lip(struct class_device *cdev,
887 const char *buf, size_t count)
889 struct Scsi_Host *shost = transport_class_to_shost(cdev);
890 struct fc_internal *i = to_fc_internal(shost->transportt);
893 /* ignore any data value written to the attribute */
894 if (i->f->issue_fc_host_lip) {
895 ret = i->f->issue_fc_host_lip(shost);
896 return ret ? ret: count;
902 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
903 store_fc_private_host_issue_lip);
906 * Host Statistics Management
909 /* Show a given an attribute in the statistics group */
911 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
913 struct Scsi_Host *shost = transport_class_to_shost(cdev);
914 struct fc_internal *i = to_fc_internal(shost->transportt);
915 struct fc_host_statistics *stats;
916 ssize_t ret = -ENOENT;
918 if (offset > sizeof(struct fc_host_statistics) ||
919 offset % sizeof(u64) != 0)
922 if (i->f->get_fc_host_stats) {
923 stats = (i->f->get_fc_host_stats)(shost);
925 ret = snprintf(buf, 20, "0x%llx\n",
926 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
932 /* generate a read-only statistics attribute */
933 #define fc_host_statistic(name) \
934 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
936 return fc_stat_show(cd, buf, \
937 offsetof(struct fc_host_statistics, name)); \
939 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
941 fc_host_statistic(seconds_since_last_reset);
942 fc_host_statistic(tx_frames);
943 fc_host_statistic(tx_words);
944 fc_host_statistic(rx_frames);
945 fc_host_statistic(rx_words);
946 fc_host_statistic(lip_count);
947 fc_host_statistic(nos_count);
948 fc_host_statistic(error_frames);
949 fc_host_statistic(dumped_frames);
950 fc_host_statistic(link_failure_count);
951 fc_host_statistic(loss_of_sync_count);
952 fc_host_statistic(loss_of_signal_count);
953 fc_host_statistic(prim_seq_protocol_err_count);
954 fc_host_statistic(invalid_tx_word_count);
955 fc_host_statistic(invalid_crc_count);
956 fc_host_statistic(fcp_input_requests);
957 fc_host_statistic(fcp_output_requests);
958 fc_host_statistic(fcp_control_requests);
959 fc_host_statistic(fcp_input_megabytes);
960 fc_host_statistic(fcp_output_megabytes);
963 fc_reset_statistics(struct class_device *cdev, const char *buf,
966 struct Scsi_Host *shost = transport_class_to_shost(cdev);
967 struct fc_internal *i = to_fc_internal(shost->transportt);
969 /* ignore any data value written to the attribute */
970 if (i->f->reset_fc_host_stats) {
971 i->f->reset_fc_host_stats(shost);
977 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
978 fc_reset_statistics);
981 static struct attribute *fc_statistics_attrs[] = {
982 &class_device_attr_host_seconds_since_last_reset.attr,
983 &class_device_attr_host_tx_frames.attr,
984 &class_device_attr_host_tx_words.attr,
985 &class_device_attr_host_rx_frames.attr,
986 &class_device_attr_host_rx_words.attr,
987 &class_device_attr_host_lip_count.attr,
988 &class_device_attr_host_nos_count.attr,
989 &class_device_attr_host_error_frames.attr,
990 &class_device_attr_host_dumped_frames.attr,
991 &class_device_attr_host_link_failure_count.attr,
992 &class_device_attr_host_loss_of_sync_count.attr,
993 &class_device_attr_host_loss_of_signal_count.attr,
994 &class_device_attr_host_prim_seq_protocol_err_count.attr,
995 &class_device_attr_host_invalid_tx_word_count.attr,
996 &class_device_attr_host_invalid_crc_count.attr,
997 &class_device_attr_host_fcp_input_requests.attr,
998 &class_device_attr_host_fcp_output_requests.attr,
999 &class_device_attr_host_fcp_control_requests.attr,
1000 &class_device_attr_host_fcp_input_megabytes.attr,
1001 &class_device_attr_host_fcp_output_megabytes.attr,
1002 &class_device_attr_host_reset_statistics.attr,
1006 static struct attribute_group fc_statistics_group = {
1007 .name = "statistics",
1008 .attrs = fc_statistics_attrs,
1011 static int fc_host_match(struct attribute_container *cont,
1014 struct Scsi_Host *shost;
1015 struct fc_internal *i;
1017 if (!scsi_is_host_device(dev))
1020 shost = dev_to_shost(dev);
1021 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1022 != &fc_host_class.class)
1025 i = to_fc_internal(shost->transportt);
1027 return &i->t.host_attrs.ac == cont;
1030 static int fc_target_match(struct attribute_container *cont,
1033 struct Scsi_Host *shost;
1034 struct fc_internal *i;
1036 if (!scsi_is_target_device(dev))
1039 shost = dev_to_shost(dev->parent);
1040 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1041 != &fc_host_class.class)
1044 i = to_fc_internal(shost->transportt);
1046 return &i->t.target_attrs.ac == cont;
1049 static void fc_rport_dev_release(struct device *dev)
1051 struct fc_rport *rport = dev_to_rport(dev);
1052 put_device(dev->parent);
1056 int scsi_is_fc_rport(const struct device *dev)
1058 return dev->release == fc_rport_dev_release;
1060 EXPORT_SYMBOL(scsi_is_fc_rport);
1062 static int fc_rport_match(struct attribute_container *cont,
1065 struct Scsi_Host *shost;
1066 struct fc_internal *i;
1068 if (!scsi_is_fc_rport(dev))
1071 shost = dev_to_shost(dev->parent);
1072 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1073 != &fc_host_class.class)
1076 i = to_fc_internal(shost->transportt);
1078 return &i->rport_attr_cont.ac == cont;
1083 * Must be called with shost->host_lock held
1085 static struct device *fc_target_parent(struct Scsi_Host *shost,
1086 int channel, uint id)
1088 struct fc_rport *rport;
1090 list_for_each_entry(rport, &fc_host_rports(shost), peers)
1091 if ((rport->channel == channel) &&
1092 (rport->scsi_target_id == id))
1098 struct scsi_transport_template *
1099 fc_attach_transport(struct fc_function_template *ft)
1101 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1108 memset(i, 0, sizeof(struct fc_internal));
1110 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1111 i->t.target_attrs.ac.class = &fc_transport_class.class;
1112 i->t.target_attrs.ac.match = fc_target_match;
1113 i->t.target_size = sizeof(struct fc_starget_attrs);
1114 transport_container_register(&i->t.target_attrs);
1116 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1117 i->t.host_attrs.ac.class = &fc_host_class.class;
1118 i->t.host_attrs.ac.match = fc_host_match;
1119 i->t.host_size = sizeof(struct fc_host_attrs);
1120 if (ft->get_fc_host_stats)
1121 i->t.host_attrs.statistics = &fc_statistics_group;
1122 transport_container_register(&i->t.host_attrs);
1124 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1125 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1126 i->rport_attr_cont.ac.match = fc_rport_match;
1127 transport_container_register(&i->rport_attr_cont);
1131 /* Transport uses the shost workq for scsi scanning */
1132 i->t.create_work_queue = 1;
1134 i->t.target_parent = fc_target_parent;
1137 * Setup SCSI Target Attributes.
1140 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1141 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1142 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1144 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1146 i->starget_attrs[count] = NULL;
1150 * Setup SCSI Host Attributes.
1153 SETUP_HOST_ATTRIBUTE_RD(node_name);
1154 SETUP_HOST_ATTRIBUTE_RD(port_name);
1155 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1156 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1157 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1158 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1159 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1160 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1162 SETUP_HOST_ATTRIBUTE_RD(port_id);
1163 SETUP_HOST_ATTRIBUTE_RD(port_type);
1164 SETUP_HOST_ATTRIBUTE_RD(port_state);
1165 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1166 SETUP_HOST_ATTRIBUTE_RD(speed);
1167 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1169 /* Transport-managed attributes */
1170 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1171 if (ft->issue_fc_host_lip)
1172 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1174 BUG_ON(count > FC_HOST_NUM_ATTRS);
1176 i->host_attrs[count] = NULL;
1179 * Setup Remote Port Attributes.
1182 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1183 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1184 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1185 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1186 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1187 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1188 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1189 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1190 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1192 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1194 i->rport_attrs[count] = NULL;
1198 EXPORT_SYMBOL(fc_attach_transport);
1200 void fc_release_transport(struct scsi_transport_template *t)
1202 struct fc_internal *i = to_fc_internal(t);
1204 transport_container_unregister(&i->t.target_attrs);
1205 transport_container_unregister(&i->t.host_attrs);
1206 transport_container_unregister(&i->rport_attr_cont);
1210 EXPORT_SYMBOL(fc_release_transport);
1214 * fc_remove_host - called to terminate any fc_transport-related elements
1216 * @rport: remote port to be unblocked.
1218 * This routine is expected to be called immediately preceeding the
1219 * a driver's call to scsi_remove_host().
1221 * WARNING: A driver utilizing the fc_transport, which fails to call
1222 * this routine prior to scsi_remote_host(), will leave dangling
1223 * objects in /sys/class/fc_remote_ports. Access to any of these
1224 * objects can result in a system crash !!!
1227 * This routine assumes no locks are held on entry.
1230 fc_remove_host(struct Scsi_Host *shost)
1232 struct fc_rport *rport, *next_rport;
1234 /* Remove any remote ports */
1235 list_for_each_entry_safe(rport, next_rport,
1236 &fc_host_rports(shost), peers)
1237 fc_rport_terminate(rport);
1238 list_for_each_entry_safe(rport, next_rport,
1239 &fc_host_rport_bindings(shost), peers)
1240 fc_rport_terminate(rport);
1242 EXPORT_SYMBOL(fc_remove_host);
1245 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1246 * @rport: The remote port to be operated on
1249 fc_rport_tgt_remove(struct fc_rport *rport)
1251 struct Scsi_Host *shost = rport_to_shost(rport);
1253 scsi_target_unblock(&rport->dev);
1255 /* Stop anything on the workq */
1256 if (!cancel_delayed_work(&rport->dev_loss_work))
1257 flush_scheduled_work();
1258 scsi_flush_work(shost);
1260 scsi_remove_target(&rport->dev);
1264 * fc_rport_create - allocates and creates a remote FC port.
1265 * @shost: scsi host the remote port is connected to.
1266 * @channel: Channel on shost port connected to.
1267 * @ids: The world wide names, fc address, and FC4 port
1268 * roles for the remote port.
1270 * Allocates and creates the remoter port structure, including the
1271 * class and sysfs creation.
1274 * This routine assumes no locks are held on entry.
1277 fc_rport_create(struct Scsi_Host *shost, int channel,
1278 struct fc_rport_identifiers *ids)
1280 struct fc_host_attrs *fc_host =
1281 (struct fc_host_attrs *)shost->shost_data;
1282 struct fc_internal *fci = to_fc_internal(shost->transportt);
1283 struct fc_rport *rport;
1285 unsigned long flags;
1289 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1290 rport = kmalloc(size, GFP_KERNEL);
1291 if (unlikely(!rport)) {
1292 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1295 memset(rport, 0, size);
1297 rport->maxframe_size = -1;
1298 rport->supported_classes = FC_COS_UNSPECIFIED;
1299 rport->dev_loss_tmo = fc_dev_loss_tmo;
1300 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1301 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1302 rport->port_id = ids->port_id;
1303 rport->roles = ids->roles;
1304 rport->port_state = FC_PORTSTATE_ONLINE;
1305 if (fci->f->dd_fcrport_size)
1306 rport->dd_data = &rport[1];
1307 rport->channel = channel;
1309 INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1310 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1312 spin_lock_irqsave(shost->host_lock, flags);
1314 rport->number = fc_host->next_rport_number++;
1315 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1316 rport->scsi_target_id = fc_host->next_target_id++;
1318 rport->scsi_target_id = -1;
1319 list_add_tail(&rport->peers, &fc_host_rports(shost));
1320 get_device(&shost->shost_gendev);
1322 spin_unlock_irqrestore(shost->host_lock, flags);
1325 device_initialize(dev);
1326 dev->parent = get_device(&shost->shost_gendev);
1327 dev->release = fc_rport_dev_release;
1328 sprintf(dev->bus_id, "rport-%d:%d-%d",
1329 shost->host_no, channel, rport->number);
1330 transport_setup_device(dev);
1332 error = device_add(dev);
1334 printk(KERN_ERR "FC Remote Port device_add failed\n");
1337 transport_add_device(dev);
1338 transport_configure_device(dev);
1340 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1341 /* initiate a scan of the target */
1342 scsi_queue_work(shost, &rport->scan_work);
1347 transport_destroy_device(dev);
1348 put_device(dev->parent);
1349 spin_lock_irqsave(shost->host_lock, flags);
1350 list_del(&rport->peers);
1351 put_device(&shost->shost_gendev);
1352 spin_unlock_irqrestore(shost->host_lock, flags);
1353 put_device(dev->parent);
1359 * fc_remote_port_add - notifies the fc transport of the existence
1360 * of a remote FC port.
1361 * @shost: scsi host the remote port is connected to.
1362 * @channel: Channel on shost port connected to.
1363 * @ids: The world wide names, fc address, and FC4 port
1364 * roles for the remote port.
1366 * The LLDD calls this routine to notify the transport of the existence
1367 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1368 * of the port, it's FC address (port_id), and the FC4 roles that are
1369 * active for the port.
1371 * For ports that are FCP targets (aka scsi targets), the FC transport
1372 * maintains consistent target id bindings on behalf of the LLDD.
1373 * A consistent target id binding is an assignment of a target id to
1374 * a remote port identifier, which persists while the scsi host is
1375 * attached. The remote port can disappear, then later reappear, and
1376 * it's target id assignment remains the same. This allows for shifts
1377 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1378 * changes to the scsi subsystem which is based on scsi host number and
1379 * target id values. Bindings are only valid during the attachment of
1380 * the scsi host. If the host detaches, then later re-attaches, target
1381 * id bindings may change.
1383 * This routine is responsible for returning a remote port structure.
1384 * The routine will search the list of remote ports it maintains
1385 * internally on behalf of consistent target id mappings. If found, the
1386 * remote port structure will be reused. Otherwise, a new remote port
1387 * structure will be allocated.
1389 * Whenever a remote port is allocated, a new fc_remote_port class
1390 * device is created.
1392 * Should not be called from interrupt context.
1395 * This routine assumes no locks are held on entry.
1398 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1399 struct fc_rport_identifiers *ids)
1401 struct fc_internal *fci = to_fc_internal(shost->transportt);
1402 struct fc_rport *rport;
1403 unsigned long flags;
1407 * Search the list of "active" rports, for an rport that has been
1408 * deleted, but we've held off the real delete while the target
1409 * is in a "blocked" state.
1411 spin_lock_irqsave(shost->host_lock, flags);
1413 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1415 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1416 (rport->channel == channel)) {
1418 switch (fc_host_tgtid_bind_type(shost)) {
1419 case FC_TGTID_BIND_BY_WWPN:
1420 case FC_TGTID_BIND_NONE:
1421 if (rport->port_name == ids->port_name)
1424 case FC_TGTID_BIND_BY_WWNN:
1425 if (rport->node_name == ids->node_name)
1428 case FC_TGTID_BIND_BY_ID:
1429 if (rport->port_id == ids->port_id)
1435 struct work_struct *work =
1436 &rport->dev_loss_work;
1438 memcpy(&rport->node_name, &ids->node_name,
1439 sizeof(rport->node_name));
1440 memcpy(&rport->port_name, &ids->port_name,
1441 sizeof(rport->port_name));
1442 rport->port_id = ids->port_id;
1444 rport->port_state = FC_PORTSTATE_ONLINE;
1445 rport->roles = ids->roles;
1447 spin_unlock_irqrestore(shost->host_lock, flags);
1449 if (fci->f->dd_fcrport_size)
1450 memset(rport->dd_data, 0,
1451 fci->f->dd_fcrport_size);
1454 * If we were blocked, we were a target.
1455 * If no longer a target, we leave the timer
1456 * running in case the port changes roles
1457 * prior to the timer expiring. If the timer
1458 * fires, the target will be torn down.
1460 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1463 /* restart the target */
1466 * Stop the target timer first. Take no action
1467 * on the del_timer failure as the state
1468 * machine state change will validate the
1471 if (!cancel_delayed_work(work))
1472 flush_scheduled_work();
1474 /* initiate a scan of the target */
1475 scsi_queue_work(shost, &rport->scan_work);
1482 /* Search the bindings array */
1483 if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1484 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1486 /* search for a matching consistent binding */
1488 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1490 if (rport->channel != channel)
1493 switch (fc_host_tgtid_bind_type(shost)) {
1494 case FC_TGTID_BIND_BY_WWPN:
1495 if (rport->port_name == ids->port_name)
1498 case FC_TGTID_BIND_BY_WWNN:
1499 if (rport->node_name == ids->node_name)
1502 case FC_TGTID_BIND_BY_ID:
1503 if (rport->port_id == ids->port_id)
1506 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1511 list_move_tail(&rport->peers,
1512 &fc_host_rports(shost));
1518 memcpy(&rport->node_name, &ids->node_name,
1519 sizeof(rport->node_name));
1520 memcpy(&rport->port_name, &ids->port_name,
1521 sizeof(rport->port_name));
1522 rport->port_id = ids->port_id;
1523 rport->roles = ids->roles;
1524 rport->port_state = FC_PORTSTATE_ONLINE;
1526 spin_unlock_irqrestore(shost->host_lock, flags);
1528 if (fci->f->dd_fcrport_size)
1529 memset(rport->dd_data, 0,
1530 fci->f->dd_fcrport_size);
1532 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1533 /* initiate a scan of the target */
1534 scsi_queue_work(shost, &rport->scan_work);
1540 spin_unlock_irqrestore(shost->host_lock, flags);
1542 /* No consistent binding found - create new remote port entry */
1543 rport = fc_rport_create(shost, channel, ids);
1547 EXPORT_SYMBOL(fc_remote_port_add);
1550 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1551 * @rport: The remote port to be terminated
1554 * This routine assumes no locks are held on entry.
1557 fc_rport_terminate(struct fc_rport *rport)
1559 struct Scsi_Host *shost = rport_to_shost(rport);
1560 struct device *dev = &rport->dev;
1561 unsigned long flags;
1563 fc_rport_tgt_remove(rport);
1565 transport_remove_device(dev);
1567 transport_destroy_device(dev);
1568 spin_lock_irqsave(shost->host_lock, flags);
1569 list_del(&rport->peers);
1570 spin_unlock_irqrestore(shost->host_lock, flags);
1571 put_device(&shost->shost_gendev);
1575 * fc_remote_port_delete - notifies the fc transport that a remote
1576 * port is no longer in existence.
1577 * @rport: The remote port that no longer exists
1579 * The LLDD calls this routine to notify the transport that a remote
1580 * port is no longer part of the topology. Note: Although a port
1581 * may no longer be part of the topology, it may persist in the remote
1582 * ports displayed by the fc_host. We do this under 2 conditions:
1583 * - If the port was a scsi target, we delay its deletion by "blocking" it.
1584 * This allows the port to temporarily disappear, then reappear without
1585 * disrupting the SCSI device tree attached to it. During the "blocked"
1586 * period the port will still exist.
1587 * - If the port was a scsi target and disappears for longer than we
1588 * expect, we'll delete the port and the tear down the SCSI device tree
1589 * attached to it. However, we want to semi-persist the target id assigned
1590 * to that port if it eventually does exist. The port structure will
1591 * remain (although with minimal information) so that the target id
1594 * If the remote port is not an FCP Target, it will be fully torn down
1595 * and deallocated, including the fc_remote_port class device.
1597 * If the remote port is an FCP Target, the port will be placed in a
1598 * temporary blocked state. From the LLDD's perspective, the rport no
1599 * longer exists. From the SCSI midlayer's perspective, the SCSI target
1600 * exists, but all sdevs on it are blocked from further I/O. The following
1602 * If the remote port does not return (signaled by a LLDD call to
1603 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
1604 * scsi target is removed - killing all outstanding i/o and removing the
1605 * scsi devices attached ot it. The port structure will be marked Not
1606 * Present and be partially cleared, leaving only enough information to
1607 * recognize the remote port relative to the scsi target id binding if
1608 * it later appears. The port will remain as long as there is a valid
1609 * binding (e.g. until the user changes the binding type or unloads the
1610 * scsi host with the binding).
1612 * If the remote port returns within the dev_loss_tmo value (and matches
1613 * according to the target id binding type), the port structure will be
1614 * reused. If it is no longer a SCSI target, the target will be torn
1615 * down. If it continues to be a SCSI target, then the target will be
1616 * unblocked (allowing i/o to be resumed), and a scan will be activated
1617 * to ensure that all luns are detected.
1619 * Called from normal process context only - cannot be called from interrupt.
1622 * This routine assumes no locks are held on entry.
1625 fc_remote_port_delete(struct fc_rport *rport)
1627 int timeout = rport->dev_loss_tmo;
1629 /* If no scsi target id mapping, delete it */
1630 if (rport->scsi_target_id == -1) {
1631 fc_rport_terminate(rport);
1635 scsi_target_block(&rport->dev);
1637 /* cap the length the devices can be blocked until they are deleted */
1638 schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
1640 rport->port_state = FC_PORTSTATE_BLOCKED;
1642 EXPORT_SYMBOL(fc_remote_port_delete);
1645 * fc_remote_port_rolechg - notifies the fc transport that the roles
1646 * on a remote may have changed.
1647 * @rport: The remote port that changed.
1649 * The LLDD calls this routine to notify the transport that the roles
1650 * on a remote port may have changed. The largest effect of this is
1651 * if a port now becomes a FCP Target, it must be allocated a
1652 * scsi target id. If the port is no longer a FCP target, any
1653 * scsi target id value assigned to it will persist in case the
1654 * role changes back to include FCP Target. No changes in the scsi
1655 * midlayer will be invoked if the role changes (in the expectation
1656 * that the role will be resumed. If it doesn't normal error processing
1659 * Should not be called from interrupt context.
1662 * This routine assumes no locks are held on entry.
1665 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1667 struct Scsi_Host *shost = rport_to_shost(rport);
1668 struct fc_host_attrs *fc_host =
1669 (struct fc_host_attrs *)shost->shost_data;
1670 unsigned long flags;
1673 spin_lock_irqsave(shost->host_lock, flags);
1674 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
1675 if (rport->scsi_target_id == -1) {
1676 rport->scsi_target_id = fc_host->next_target_id++;
1678 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
1681 spin_unlock_irqrestore(shost->host_lock, flags);
1683 rport->roles = roles;
1687 * There may have been a delete timer running on the
1688 * port. Ensure that it is cancelled as we now know
1689 * the port is an FCP Target.
1690 * Note: we know the rport is exists and in an online
1691 * state as the LLDD would not have had an rport
1692 * reference to pass us.
1694 * Take no action on the del_timer failure as the state
1695 * machine state change will validate the
1698 if (!cancel_delayed_work(&rport->dev_loss_work))
1699 flush_scheduled_work();
1701 /* initiate a scan of the target */
1702 scsi_queue_work(shost, &rport->scan_work);
1705 EXPORT_SYMBOL(fc_remote_port_rolechg);
1708 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
1709 * was a SCSI target (thus was blocked), and failed
1710 * to return in the alloted time.
1712 * @data: rport target that failed to reappear in the alloted time.
1715 fc_timeout_deleted_rport(void *data)
1717 struct fc_rport *rport = (struct fc_rport *)data;
1718 struct Scsi_Host *shost = rport_to_shost(rport);
1719 unsigned long flags;
1721 spin_lock_irqsave(shost->host_lock, flags);
1724 * If the port is ONLINE, then it came back, but was no longer an
1725 * FCP target. Thus we need to tear down the scsi_target on it.
1727 if (rport->port_state == FC_PORTSTATE_ONLINE) {
1728 spin_unlock_irqrestore(shost->host_lock, flags);
1730 dev_printk(KERN_ERR, &rport->dev,
1731 "blocked FC remote port time out: removing target\n");
1733 fc_rport_tgt_remove(rport);
1738 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
1739 spin_unlock_irqrestore(shost->host_lock, flags);
1740 dev_printk(KERN_ERR, &rport->dev,
1741 "blocked FC remote port time out: leaving target alone\n");
1745 if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
1746 spin_unlock_irqrestore(shost->host_lock, flags);
1747 dev_printk(KERN_ERR, &rport->dev,
1748 "blocked FC remote port time out: removing target\n");
1749 fc_rport_terminate(rport);
1753 dev_printk(KERN_ERR, &rport->dev,
1754 "blocked FC remote port time out: removing target and "
1755 "saving binding\n");
1757 list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1760 * Note: We do not remove or clear the hostdata area. This allows
1761 * host-specific target data to persist along with the
1762 * scsi_target_id. It's up to the host to manage it's hostdata area.
1766 * Reinitialize port attributes that may change if the port comes back.
1768 rport->maxframe_size = -1;
1769 rport->supported_classes = FC_COS_UNSPECIFIED;
1770 rport->roles = FC_RPORT_ROLE_UNKNOWN;
1771 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1773 /* remove the identifiers that aren't used in the consisting binding */
1774 switch (fc_host_tgtid_bind_type(shost)) {
1775 case FC_TGTID_BIND_BY_WWPN:
1776 rport->node_name = -1;
1777 rport->port_id = -1;
1779 case FC_TGTID_BIND_BY_WWNN:
1780 rport->port_name = -1;
1781 rport->port_id = -1;
1783 case FC_TGTID_BIND_BY_ID:
1784 rport->node_name = -1;
1785 rport->port_name = -1;
1787 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1791 spin_unlock_irqrestore(shost->host_lock, flags);
1794 * As this only occurs if the remote port (scsi target)
1795 * went away and didn't come back - we'll remove
1796 * all attached scsi devices.
1798 fc_rport_tgt_remove(rport);
1802 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1804 * Will unblock the target (in case it went away and has now come back),
1805 * then invoke a scan.
1807 * @data: remote port to be scanned.
1810 fc_scsi_scan_rport(void *data)
1812 struct fc_rport *rport = (struct fc_rport *)data;
1814 scsi_target_unblock(&rport->dev);
1815 scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1820 MODULE_AUTHOR("Martin Hicks");
1821 MODULE_DESCRIPTION("FC Transport Attributes");
1822 MODULE_LICENSE("GPL");
1824 module_init(fc_transport_init);
1825 module_exit(fc_transport_exit);