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 <linux/sched.h> /* workqueue stuff, HZ */
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include "scsi_priv.h"
37 * Redefine so that we can have same named attributes in the
38 * sdev/starget/host objects.
40 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
41 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
42 __ATTR(_name,_mode,_show,_store)
44 #define fc_enum_name_search(title, table_type, table) \
45 static const char *get_fc_##title##_name(enum table_type table_key) \
50 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
51 if (table[i].value == table_key) { \
52 name = table[i].name; \
59 #define fc_enum_name_match(title, table_type, table) \
60 static int get_fc_##title##_match(const char *table_key, \
61 enum table_type *value) \
65 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
66 if (strncmp(table_key, table[i].name, \
67 table[i].matchlen) == 0) { \
68 *value = table[i].value; \
69 return 0; /* success */ \
72 return 1; /* failure */ \
76 /* Convert fc_port_type values to ascii string name */
78 enum fc_port_type value;
80 } fc_port_type_names[] = {
81 { FC_PORTTYPE_UNKNOWN, "Unknown" },
82 { FC_PORTTYPE_OTHER, "Other" },
83 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
84 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
85 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
86 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
87 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
89 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
90 #define FC_PORTTYPE_MAX_NAMELEN 50
93 /* Convert fc_port_state values to ascii string name */
95 enum fc_port_state value;
97 } fc_port_state_names[] = {
98 { FC_PORTSTATE_UNKNOWN, "Unknown" },
99 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
100 { FC_PORTSTATE_ONLINE, "Online" },
101 { FC_PORTSTATE_OFFLINE, "Offline" },
102 { FC_PORTSTATE_BLOCKED, "Blocked" },
103 { FC_PORTSTATE_BYPASSED, "Bypassed" },
104 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
105 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
106 { FC_PORTSTATE_ERROR, "Error" },
107 { FC_PORTSTATE_LOOPBACK, "Loopback" },
108 { FC_PORTSTATE_DELETED, "Deleted" },
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 */
115 static const struct {
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 */
153 static const struct {
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 */
167 static const struct {
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 */
193 static const struct {
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_shost_remove_rports(void *data);
216 static void fc_timeout_deleted_rport(void *data);
217 static void fc_scsi_scan_rport(void *data);
218 static void fc_rport_terminate(struct fc_rport *rport);
221 * Attribute counts pre object type...
222 * Increase these values if you add attributes
224 #define FC_STARGET_NUM_ATTRS 3
225 #define FC_RPORT_NUM_ATTRS 9
226 #define FC_HOST_NUM_ATTRS 16
229 struct scsi_transport_template t;
230 struct fc_function_template *f;
233 * For attributes : each object has :
234 * An array of the actual attributes structures
235 * An array of null-terminated pointers to the attribute
236 * structures - used for mid-layer interaction.
238 * The attribute containers for the starget and host are are
239 * part of the midlayer. As the remote port is specific to the
240 * fc transport, we must provide the attribute container.
242 struct class_device_attribute private_starget_attrs[
243 FC_STARGET_NUM_ATTRS];
244 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
246 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
247 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
249 struct transport_container rport_attr_cont;
250 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
251 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
254 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
256 static int fc_target_setup(struct transport_container *tc, struct device *dev,
257 struct class_device *cdev)
259 struct scsi_target *starget = to_scsi_target(dev);
260 struct fc_rport *rport = starget_to_rport(starget);
263 * if parent is remote port, use values from remote port.
264 * Otherwise, this host uses the fc_transport, but not the
265 * remote port interface. As such, initialize to known non-values.
268 fc_starget_node_name(starget) = rport->node_name;
269 fc_starget_port_name(starget) = rport->port_name;
270 fc_starget_port_id(starget) = rport->port_id;
272 fc_starget_node_name(starget) = -1;
273 fc_starget_port_name(starget) = -1;
274 fc_starget_port_id(starget) = -1;
280 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
286 static int fc_host_setup(struct transport_container *tc, struct device *dev,
287 struct class_device *cdev)
289 struct Scsi_Host *shost = dev_to_shost(dev);
292 * Set default values easily detected by the midlayer as
293 * failure cases. The scsi lldd is responsible for initializing
294 * all transport attributes to valid values per host.
296 fc_host_node_name(shost) = -1;
297 fc_host_port_name(shost) = -1;
298 fc_host_permanent_port_name(shost) = -1;
299 fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
300 memset(fc_host_supported_fc4s(shost), 0,
301 sizeof(fc_host_supported_fc4s(shost)));
302 memset(fc_host_symbolic_name(shost), 0,
303 sizeof(fc_host_symbolic_name(shost)));
304 fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
305 fc_host_maxframe_size(shost) = -1;
306 memset(fc_host_serial_number(shost), 0,
307 sizeof(fc_host_serial_number(shost)));
309 fc_host_port_id(shost) = -1;
310 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
311 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
312 memset(fc_host_active_fc4s(shost), 0,
313 sizeof(fc_host_active_fc4s(shost)));
314 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
315 fc_host_fabric_name(shost) = -1;
317 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
319 INIT_LIST_HEAD(&fc_host_rports(shost));
320 INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
321 fc_host_next_rport_number(shost) = 0;
322 fc_host_next_target_id(shost) = 0;
324 fc_host_flags(shost) = 0;
325 INIT_WORK(&fc_host_rport_del_work(shost), fc_shost_remove_rports, shost);
329 static DECLARE_TRANSPORT_CLASS(fc_host_class,
336 * Setup and Remove actions for remote ports are handled
337 * in the service functions below.
339 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
350 * dev_loss_tmo: the default number of seconds that the FC transport
351 * should insulate the loss of a remote port.
352 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
354 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
356 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
357 MODULE_PARM_DESC(dev_loss_tmo,
358 "Maximum number of seconds that the FC transport should"
359 " insulate the loss of a remote port. Once this value is"
360 " exceeded, the scsi target is removed. Value should be"
361 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
364 static __init int fc_transport_init(void)
366 int error = transport_class_register(&fc_host_class);
369 error = transport_class_register(&fc_rport_class);
372 return transport_class_register(&fc_transport_class);
375 static void __exit fc_transport_exit(void)
377 transport_class_unregister(&fc_transport_class);
378 transport_class_unregister(&fc_rport_class);
379 transport_class_unregister(&fc_host_class);
383 * FC Remote Port Attribute Management
386 #define fc_rport_show_function(field, format_string, sz, cast) \
388 show_fc_rport_##field (struct class_device *cdev, char *buf) \
390 struct fc_rport *rport = transport_class_to_rport(cdev); \
391 struct Scsi_Host *shost = rport_to_shost(rport); \
392 struct fc_internal *i = to_fc_internal(shost->transportt); \
393 if ((i->f->get_rport_##field) && \
394 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
395 (rport->port_state == FC_PORTSTATE_DELETED) || \
396 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
397 i->f->get_rport_##field(rport); \
398 return snprintf(buf, sz, format_string, cast rport->field); \
401 #define fc_rport_store_function(field) \
403 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
407 struct fc_rport *rport = transport_class_to_rport(cdev); \
408 struct Scsi_Host *shost = rport_to_shost(rport); \
409 struct fc_internal *i = to_fc_internal(shost->transportt); \
410 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
411 (rport->port_state == FC_PORTSTATE_DELETED) || \
412 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
414 val = simple_strtoul(buf, NULL, 0); \
415 i->f->set_rport_##field(rport, val); \
419 #define fc_rport_rd_attr(field, format_string, sz) \
420 fc_rport_show_function(field, format_string, sz, ) \
421 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
422 show_fc_rport_##field, NULL)
424 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
425 fc_rport_show_function(field, format_string, sz, (cast)) \
426 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
427 show_fc_rport_##field, NULL)
429 #define fc_rport_rw_attr(field, format_string, sz) \
430 fc_rport_show_function(field, format_string, sz, ) \
431 fc_rport_store_function(field) \
432 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
433 show_fc_rport_##field, \
434 store_fc_rport_##field)
437 #define fc_private_rport_show_function(field, format_string, sz, cast) \
439 show_fc_rport_##field (struct class_device *cdev, char *buf) \
441 struct fc_rport *rport = transport_class_to_rport(cdev); \
442 return snprintf(buf, sz, format_string, cast rport->field); \
445 #define fc_private_rport_rd_attr(field, format_string, sz) \
446 fc_private_rport_show_function(field, format_string, sz, ) \
447 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
448 show_fc_rport_##field, NULL)
450 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
451 fc_private_rport_show_function(field, format_string, sz, (cast)) \
452 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
453 show_fc_rport_##field, NULL)
456 #define fc_private_rport_rd_enum_attr(title, maxlen) \
458 show_fc_rport_##title (struct class_device *cdev, char *buf) \
460 struct fc_rport *rport = transport_class_to_rport(cdev); \
462 name = get_fc_##title##_name(rport->title); \
465 return snprintf(buf, maxlen, "%s\n", name); \
467 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
468 show_fc_rport_##title, NULL)
471 #define SETUP_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]; \
476 if (i->f->show_rport_##field) \
479 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
480 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
481 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
482 i->private_rport_attrs[count].store = NULL; \
483 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
486 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
487 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
488 if (!i->f->set_rport_##field) { \
489 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
490 i->private_rport_attrs[count].store = NULL; \
492 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
493 if (i->f->show_rport_##field) \
497 /* The FC Transport Remote Port Attributes: */
499 /* Fixed Remote Port Attributes */
501 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
504 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
506 struct fc_rport *rport = transport_class_to_rport(cdev);
507 if (rport->supported_classes == FC_COS_UNSPECIFIED)
508 return snprintf(buf, 20, "unspecified\n");
509 return get_fc_cos_names(rport->supported_classes, buf);
511 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
512 show_fc_rport_supported_classes, NULL);
514 /* Dynamic Remote Port Attributes */
517 * dev_loss_tmo attribute
519 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
521 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
525 struct fc_rport *rport = transport_class_to_rport(cdev);
526 struct Scsi_Host *shost = rport_to_shost(rport);
527 struct fc_internal *i = to_fc_internal(shost->transportt);
528 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
529 (rport->port_state == FC_PORTSTATE_DELETED) ||
530 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
532 val = simple_strtoul(buf, NULL, 0);
533 if ((val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
535 i->f->set_rport_dev_loss_tmo(rport, val);
538 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
539 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
542 /* Private Remote Port Attributes */
544 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
545 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
546 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
549 show_fc_rport_roles (struct class_device *cdev, char *buf)
551 struct fc_rport *rport = transport_class_to_rport(cdev);
553 /* identify any roles that are port_id specific */
554 if ((rport->port_id != -1) &&
555 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
556 FC_WELLKNOWN_PORTID_MASK) {
557 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
558 case FC_FPORT_PORTID:
559 return snprintf(buf, 30, "Fabric Port\n");
560 case FC_FABCTLR_PORTID:
561 return snprintf(buf, 30, "Fabric Controller\n");
562 case FC_DIRSRVR_PORTID:
563 return snprintf(buf, 30, "Directory Server\n");
564 case FC_TIMESRVR_PORTID:
565 return snprintf(buf, 30, "Time Server\n");
566 case FC_MGMTSRVR_PORTID:
567 return snprintf(buf, 30, "Management Server\n");
569 return snprintf(buf, 30, "Unknown Fabric Entity\n");
572 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
573 return snprintf(buf, 20, "unknown\n");
574 return get_fc_remote_port_roles_names(rport->roles, buf);
577 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
578 show_fc_rport_roles, NULL);
580 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
581 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
586 * FC SCSI Target Attribute Management
590 * Note: in the target show function we recognize when the remote
591 * port is in the heirarchy and do not allow the driver to get
592 * involved in sysfs functions. The driver only gets involved if
593 * it's the "old" style that doesn't use rports.
595 #define fc_starget_show_function(field, format_string, sz, cast) \
597 show_fc_starget_##field (struct class_device *cdev, char *buf) \
599 struct scsi_target *starget = transport_class_to_starget(cdev); \
600 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
601 struct fc_internal *i = to_fc_internal(shost->transportt); \
602 struct fc_rport *rport = starget_to_rport(starget); \
604 fc_starget_##field(starget) = rport->field; \
605 else if (i->f->get_starget_##field) \
606 i->f->get_starget_##field(starget); \
607 return snprintf(buf, sz, format_string, \
608 cast fc_starget_##field(starget)); \
611 #define fc_starget_rd_attr(field, format_string, sz) \
612 fc_starget_show_function(field, format_string, sz, ) \
613 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
614 show_fc_starget_##field, NULL)
616 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
617 fc_starget_show_function(field, format_string, sz, (cast)) \
618 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
619 show_fc_starget_##field, NULL)
621 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
622 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
623 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
624 i->private_starget_attrs[count].store = NULL; \
625 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
626 if (i->f->show_starget_##field) \
629 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
630 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
631 if (!i->f->set_starget_##field) { \
632 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
633 i->private_starget_attrs[count].store = NULL; \
635 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
636 if (i->f->show_starget_##field) \
639 /* The FC Transport SCSI Target Attributes: */
640 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
641 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
642 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
646 * Host Attribute Management
649 #define fc_host_show_function(field, format_string, sz, cast) \
651 show_fc_host_##field (struct class_device *cdev, char *buf) \
653 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
654 struct fc_internal *i = to_fc_internal(shost->transportt); \
655 if (i->f->get_host_##field) \
656 i->f->get_host_##field(shost); \
657 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
660 #define fc_host_store_function(field) \
662 store_fc_host_##field(struct class_device *cdev, const char *buf, \
666 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
667 struct fc_internal *i = to_fc_internal(shost->transportt); \
669 val = simple_strtoul(buf, NULL, 0); \
670 i->f->set_host_##field(shost, val); \
674 #define fc_host_rd_attr(field, format_string, sz) \
675 fc_host_show_function(field, format_string, sz, ) \
676 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
677 show_fc_host_##field, NULL)
679 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
680 fc_host_show_function(field, format_string, sz, (cast)) \
681 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
682 show_fc_host_##field, NULL)
684 #define fc_host_rw_attr(field, format_string, sz) \
685 fc_host_show_function(field, format_string, sz, ) \
686 fc_host_store_function(field) \
687 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
688 show_fc_host_##field, \
689 store_fc_host_##field)
691 #define fc_host_rd_enum_attr(title, maxlen) \
693 show_fc_host_##title (struct class_device *cdev, char *buf) \
695 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
696 struct fc_internal *i = to_fc_internal(shost->transportt); \
698 if (i->f->get_host_##title) \
699 i->f->get_host_##title(shost); \
700 name = get_fc_##title##_name(fc_host_##title(shost)); \
703 return snprintf(buf, maxlen, "%s\n", name); \
705 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
707 #define SETUP_HOST_ATTRIBUTE_RD(field) \
708 i->private_host_attrs[count] = class_device_attr_host_##field; \
709 i->private_host_attrs[count].attr.mode = S_IRUGO; \
710 i->private_host_attrs[count].store = NULL; \
711 i->host_attrs[count] = &i->private_host_attrs[count]; \
712 if (i->f->show_host_##field) \
715 #define SETUP_HOST_ATTRIBUTE_RW(field) \
716 i->private_host_attrs[count] = class_device_attr_host_##field; \
717 if (!i->f->set_host_##field) { \
718 i->private_host_attrs[count].attr.mode = S_IRUGO; \
719 i->private_host_attrs[count].store = NULL; \
721 i->host_attrs[count] = &i->private_host_attrs[count]; \
722 if (i->f->show_host_##field) \
726 #define fc_private_host_show_function(field, format_string, sz, cast) \
728 show_fc_host_##field (struct class_device *cdev, char *buf) \
730 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
731 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
734 #define fc_private_host_rd_attr(field, format_string, sz) \
735 fc_private_host_show_function(field, format_string, sz, ) \
736 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
737 show_fc_host_##field, NULL)
739 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
740 fc_private_host_show_function(field, format_string, sz, (cast)) \
741 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
742 show_fc_host_##field, NULL)
744 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
745 i->private_host_attrs[count] = class_device_attr_host_##field; \
746 i->private_host_attrs[count].attr.mode = S_IRUGO; \
747 i->private_host_attrs[count].store = NULL; \
748 i->host_attrs[count] = &i->private_host_attrs[count]; \
751 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
753 i->private_host_attrs[count] = class_device_attr_host_##field; \
754 i->host_attrs[count] = &i->private_host_attrs[count]; \
759 /* Fixed Host Attributes */
762 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
764 struct Scsi_Host *shost = transport_class_to_shost(cdev);
766 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
767 return snprintf(buf, 20, "unspecified\n");
769 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
771 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
772 show_fc_host_supported_classes, NULL);
775 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
777 struct Scsi_Host *shost = transport_class_to_shost(cdev);
778 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
780 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
781 show_fc_host_supported_fc4s, NULL);
784 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
786 struct Scsi_Host *shost = transport_class_to_shost(cdev);
788 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
789 return snprintf(buf, 20, "unknown\n");
791 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
793 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
794 show_fc_host_supported_speeds, NULL);
797 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
798 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
799 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
801 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
802 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
803 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
806 /* Dynamic Host Attributes */
809 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
811 struct Scsi_Host *shost = transport_class_to_shost(cdev);
812 struct fc_internal *i = to_fc_internal(shost->transportt);
814 if (i->f->get_host_active_fc4s)
815 i->f->get_host_active_fc4s(shost);
817 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
819 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
820 show_fc_host_active_fc4s, NULL);
823 show_fc_host_speed (struct class_device *cdev, char *buf)
825 struct Scsi_Host *shost = transport_class_to_shost(cdev);
826 struct fc_internal *i = to_fc_internal(shost->transportt);
828 if (i->f->get_host_speed)
829 i->f->get_host_speed(shost);
831 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
832 return snprintf(buf, 20, "unknown\n");
834 return get_fc_port_speed_names(fc_host_speed(shost), buf);
836 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
837 show_fc_host_speed, NULL);
840 fc_host_rd_attr(port_id, "0x%06x\n", 20);
841 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
842 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
843 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
846 /* Private Host Attributes */
849 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
851 struct Scsi_Host *shost = transport_class_to_shost(cdev);
854 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
857 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
860 #define get_list_head_entry(pos, head, member) \
861 pos = list_entry((head)->next, typeof(*pos), member)
864 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
865 const char *buf, size_t count)
867 struct Scsi_Host *shost = transport_class_to_shost(cdev);
868 struct fc_rport *rport;
869 enum fc_tgtid_binding_type val;
872 if (get_fc_tgtid_bind_type_match(buf, &val))
875 /* if changing bind type, purge all unused consistent bindings */
876 if (val != fc_host_tgtid_bind_type(shost)) {
877 spin_lock_irqsave(shost->host_lock, flags);
878 while (!list_empty(&fc_host_rport_bindings(shost))) {
879 get_list_head_entry(rport,
880 &fc_host_rport_bindings(shost), peers);
881 spin_unlock_irqrestore(shost->host_lock, flags);
882 fc_rport_terminate(rport);
883 spin_lock_irqsave(shost->host_lock, flags);
885 spin_unlock_irqrestore(shost->host_lock, flags);
888 fc_host_tgtid_bind_type(shost) = val;
892 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
893 show_fc_private_host_tgtid_bind_type,
894 store_fc_private_host_tgtid_bind_type);
897 store_fc_private_host_issue_lip(struct class_device *cdev,
898 const char *buf, size_t count)
900 struct Scsi_Host *shost = transport_class_to_shost(cdev);
901 struct fc_internal *i = to_fc_internal(shost->transportt);
904 /* ignore any data value written to the attribute */
905 if (i->f->issue_fc_host_lip) {
906 ret = i->f->issue_fc_host_lip(shost);
907 return ret ? ret: count;
913 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
914 store_fc_private_host_issue_lip);
917 * Host Statistics Management
920 /* Show a given an attribute in the statistics group */
922 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
924 struct Scsi_Host *shost = transport_class_to_shost(cdev);
925 struct fc_internal *i = to_fc_internal(shost->transportt);
926 struct fc_host_statistics *stats;
927 ssize_t ret = -ENOENT;
929 if (offset > sizeof(struct fc_host_statistics) ||
930 offset % sizeof(u64) != 0)
933 if (i->f->get_fc_host_stats) {
934 stats = (i->f->get_fc_host_stats)(shost);
936 ret = snprintf(buf, 20, "0x%llx\n",
937 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
943 /* generate a read-only statistics attribute */
944 #define fc_host_statistic(name) \
945 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
947 return fc_stat_show(cd, buf, \
948 offsetof(struct fc_host_statistics, name)); \
950 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
952 fc_host_statistic(seconds_since_last_reset);
953 fc_host_statistic(tx_frames);
954 fc_host_statistic(tx_words);
955 fc_host_statistic(rx_frames);
956 fc_host_statistic(rx_words);
957 fc_host_statistic(lip_count);
958 fc_host_statistic(nos_count);
959 fc_host_statistic(error_frames);
960 fc_host_statistic(dumped_frames);
961 fc_host_statistic(link_failure_count);
962 fc_host_statistic(loss_of_sync_count);
963 fc_host_statistic(loss_of_signal_count);
964 fc_host_statistic(prim_seq_protocol_err_count);
965 fc_host_statistic(invalid_tx_word_count);
966 fc_host_statistic(invalid_crc_count);
967 fc_host_statistic(fcp_input_requests);
968 fc_host_statistic(fcp_output_requests);
969 fc_host_statistic(fcp_control_requests);
970 fc_host_statistic(fcp_input_megabytes);
971 fc_host_statistic(fcp_output_megabytes);
974 fc_reset_statistics(struct class_device *cdev, const char *buf,
977 struct Scsi_Host *shost = transport_class_to_shost(cdev);
978 struct fc_internal *i = to_fc_internal(shost->transportt);
980 /* ignore any data value written to the attribute */
981 if (i->f->reset_fc_host_stats) {
982 i->f->reset_fc_host_stats(shost);
988 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
989 fc_reset_statistics);
992 static struct attribute *fc_statistics_attrs[] = {
993 &class_device_attr_host_seconds_since_last_reset.attr,
994 &class_device_attr_host_tx_frames.attr,
995 &class_device_attr_host_tx_words.attr,
996 &class_device_attr_host_rx_frames.attr,
997 &class_device_attr_host_rx_words.attr,
998 &class_device_attr_host_lip_count.attr,
999 &class_device_attr_host_nos_count.attr,
1000 &class_device_attr_host_error_frames.attr,
1001 &class_device_attr_host_dumped_frames.attr,
1002 &class_device_attr_host_link_failure_count.attr,
1003 &class_device_attr_host_loss_of_sync_count.attr,
1004 &class_device_attr_host_loss_of_signal_count.attr,
1005 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1006 &class_device_attr_host_invalid_tx_word_count.attr,
1007 &class_device_attr_host_invalid_crc_count.attr,
1008 &class_device_attr_host_fcp_input_requests.attr,
1009 &class_device_attr_host_fcp_output_requests.attr,
1010 &class_device_attr_host_fcp_control_requests.attr,
1011 &class_device_attr_host_fcp_input_megabytes.attr,
1012 &class_device_attr_host_fcp_output_megabytes.attr,
1013 &class_device_attr_host_reset_statistics.attr,
1017 static struct attribute_group fc_statistics_group = {
1018 .name = "statistics",
1019 .attrs = fc_statistics_attrs,
1022 static int fc_host_match(struct attribute_container *cont,
1025 struct Scsi_Host *shost;
1026 struct fc_internal *i;
1028 if (!scsi_is_host_device(dev))
1031 shost = dev_to_shost(dev);
1032 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1033 != &fc_host_class.class)
1036 i = to_fc_internal(shost->transportt);
1038 return &i->t.host_attrs.ac == cont;
1041 static int fc_target_match(struct attribute_container *cont,
1044 struct Scsi_Host *shost;
1045 struct fc_internal *i;
1047 if (!scsi_is_target_device(dev))
1050 shost = dev_to_shost(dev->parent);
1051 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1052 != &fc_host_class.class)
1055 i = to_fc_internal(shost->transportt);
1057 return &i->t.target_attrs.ac == cont;
1060 static void fc_rport_dev_release(struct device *dev)
1062 struct fc_rport *rport = dev_to_rport(dev);
1063 put_device(dev->parent);
1067 int scsi_is_fc_rport(const struct device *dev)
1069 return dev->release == fc_rport_dev_release;
1071 EXPORT_SYMBOL(scsi_is_fc_rport);
1073 static int fc_rport_match(struct attribute_container *cont,
1076 struct Scsi_Host *shost;
1077 struct fc_internal *i;
1079 if (!scsi_is_fc_rport(dev))
1082 shost = dev_to_shost(dev->parent);
1083 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1084 != &fc_host_class.class)
1087 i = to_fc_internal(shost->transportt);
1089 return &i->rport_attr_cont.ac == cont;
1094 * Must be called with shost->host_lock held
1096 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1099 struct fc_rport *rport;
1101 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1102 if (rport->scsi_target_id == -1)
1105 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1106 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1107 scsi_scan_target(&rport->dev, rport->channel,
1108 rport->scsi_target_id, lun, 1);
1115 struct scsi_transport_template *
1116 fc_attach_transport(struct fc_function_template *ft)
1118 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1125 memset(i, 0, sizeof(struct fc_internal));
1127 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1128 i->t.target_attrs.ac.class = &fc_transport_class.class;
1129 i->t.target_attrs.ac.match = fc_target_match;
1130 i->t.target_size = sizeof(struct fc_starget_attrs);
1131 transport_container_register(&i->t.target_attrs);
1133 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1134 i->t.host_attrs.ac.class = &fc_host_class.class;
1135 i->t.host_attrs.ac.match = fc_host_match;
1136 i->t.host_size = sizeof(struct fc_host_attrs);
1137 if (ft->get_fc_host_stats)
1138 i->t.host_attrs.statistics = &fc_statistics_group;
1139 transport_container_register(&i->t.host_attrs);
1141 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1142 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1143 i->rport_attr_cont.ac.match = fc_rport_match;
1144 transport_container_register(&i->rport_attr_cont);
1148 /* Transport uses the shost workq for scsi scanning */
1149 i->t.create_work_queue = 1;
1151 i->t.user_scan = fc_user_scan;
1154 * Setup SCSI Target Attributes.
1157 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1158 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1159 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1161 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1163 i->starget_attrs[count] = NULL;
1167 * Setup SCSI Host Attributes.
1170 SETUP_HOST_ATTRIBUTE_RD(node_name);
1171 SETUP_HOST_ATTRIBUTE_RD(port_name);
1172 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1173 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1174 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1175 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1176 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1177 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1178 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1180 SETUP_HOST_ATTRIBUTE_RD(port_id);
1181 SETUP_HOST_ATTRIBUTE_RD(port_type);
1182 SETUP_HOST_ATTRIBUTE_RD(port_state);
1183 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1184 SETUP_HOST_ATTRIBUTE_RD(speed);
1185 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1187 /* Transport-managed attributes */
1188 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1189 if (ft->issue_fc_host_lip)
1190 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1192 BUG_ON(count > FC_HOST_NUM_ATTRS);
1194 i->host_attrs[count] = NULL;
1197 * Setup Remote Port Attributes.
1200 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1201 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1202 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1203 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1204 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1205 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1206 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1207 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1208 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1210 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1212 i->rport_attrs[count] = NULL;
1216 EXPORT_SYMBOL(fc_attach_transport);
1218 void fc_release_transport(struct scsi_transport_template *t)
1220 struct fc_internal *i = to_fc_internal(t);
1222 transport_container_unregister(&i->t.target_attrs);
1223 transport_container_unregister(&i->t.host_attrs);
1224 transport_container_unregister(&i->rport_attr_cont);
1228 EXPORT_SYMBOL(fc_release_transport);
1232 * fc_remove_host - called to terminate any fc_transport-related elements
1234 * @rport: remote port to be unblocked.
1236 * This routine is expected to be called immediately preceeding the
1237 * a driver's call to scsi_remove_host().
1239 * WARNING: A driver utilizing the fc_transport, which fails to call
1240 * this routine prior to scsi_remote_host(), will leave dangling
1241 * objects in /sys/class/fc_remote_ports. Access to any of these
1242 * objects can result in a system crash !!!
1245 * This routine assumes no locks are held on entry.
1248 fc_remove_host(struct Scsi_Host *shost)
1250 struct fc_rport *rport, *next_rport;
1252 /* Remove any remote ports */
1253 list_for_each_entry_safe(rport, next_rport,
1254 &fc_host_rports(shost), peers)
1255 fc_rport_terminate(rport);
1256 list_for_each_entry_safe(rport, next_rport,
1257 &fc_host_rport_bindings(shost), peers)
1258 fc_rport_terminate(rport);
1260 EXPORT_SYMBOL(fc_remove_host);
1263 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1264 * @rport: The remote port to be operated on
1267 fc_rport_tgt_remove(struct fc_rport *rport)
1269 struct Scsi_Host *shost = rport_to_shost(rport);
1271 scsi_target_unblock(&rport->dev);
1273 /* Stop anything on the workq */
1274 if (!cancel_delayed_work(&rport->dev_loss_work))
1275 flush_scheduled_work();
1276 scsi_flush_work(shost);
1278 scsi_remove_target(&rport->dev);
1282 * fc_rport_create - allocates and creates a remote FC port.
1283 * @shost: scsi host the remote port is connected to.
1284 * @channel: Channel on shost port connected to.
1285 * @ids: The world wide names, fc address, and FC4 port
1286 * roles for the remote port.
1288 * Allocates and creates the remoter port structure, including the
1289 * class and sysfs creation.
1292 * This routine assumes no locks are held on entry.
1295 fc_rport_create(struct Scsi_Host *shost, int channel,
1296 struct fc_rport_identifiers *ids)
1298 struct fc_host_attrs *fc_host =
1299 (struct fc_host_attrs *)shost->shost_data;
1300 struct fc_internal *fci = to_fc_internal(shost->transportt);
1301 struct fc_rport *rport;
1303 unsigned long flags;
1307 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1308 rport = kmalloc(size, GFP_KERNEL);
1309 if (unlikely(!rport)) {
1310 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1313 memset(rport, 0, size);
1315 rport->maxframe_size = -1;
1316 rport->supported_classes = FC_COS_UNSPECIFIED;
1317 rport->dev_loss_tmo = fc_dev_loss_tmo;
1318 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1319 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1320 rport->port_id = ids->port_id;
1321 rport->roles = ids->roles;
1322 rport->port_state = FC_PORTSTATE_ONLINE;
1323 if (fci->f->dd_fcrport_size)
1324 rport->dd_data = &rport[1];
1325 rport->channel = channel;
1327 INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1328 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1330 spin_lock_irqsave(shost->host_lock, flags);
1332 rport->number = fc_host->next_rport_number++;
1333 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1334 rport->scsi_target_id = fc_host->next_target_id++;
1336 rport->scsi_target_id = -1;
1337 list_add_tail(&rport->peers, &fc_host_rports(shost));
1338 get_device(&shost->shost_gendev);
1340 spin_unlock_irqrestore(shost->host_lock, flags);
1343 device_initialize(dev);
1344 dev->parent = get_device(&shost->shost_gendev);
1345 dev->release = fc_rport_dev_release;
1346 sprintf(dev->bus_id, "rport-%d:%d-%d",
1347 shost->host_no, channel, rport->number);
1348 transport_setup_device(dev);
1350 error = device_add(dev);
1352 printk(KERN_ERR "FC Remote Port device_add failed\n");
1355 transport_add_device(dev);
1356 transport_configure_device(dev);
1358 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1359 /* initiate a scan of the target */
1360 scsi_queue_work(shost, &rport->scan_work);
1365 transport_destroy_device(dev);
1366 put_device(dev->parent);
1367 spin_lock_irqsave(shost->host_lock, flags);
1368 list_del(&rport->peers);
1369 put_device(&shost->shost_gendev);
1370 spin_unlock_irqrestore(shost->host_lock, flags);
1371 put_device(dev->parent);
1377 * fc_remote_port_add - notifies the fc transport of the existence
1378 * of a remote FC port.
1379 * @shost: scsi host the remote port is connected to.
1380 * @channel: Channel on shost port connected to.
1381 * @ids: The world wide names, fc address, and FC4 port
1382 * roles for the remote port.
1384 * The LLDD calls this routine to notify the transport of the existence
1385 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1386 * of the port, it's FC address (port_id), and the FC4 roles that are
1387 * active for the port.
1389 * For ports that are FCP targets (aka scsi targets), the FC transport
1390 * maintains consistent target id bindings on behalf of the LLDD.
1391 * A consistent target id binding is an assignment of a target id to
1392 * a remote port identifier, which persists while the scsi host is
1393 * attached. The remote port can disappear, then later reappear, and
1394 * it's target id assignment remains the same. This allows for shifts
1395 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1396 * changes to the scsi subsystem which is based on scsi host number and
1397 * target id values. Bindings are only valid during the attachment of
1398 * the scsi host. If the host detaches, then later re-attaches, target
1399 * id bindings may change.
1401 * This routine is responsible for returning a remote port structure.
1402 * The routine will search the list of remote ports it maintains
1403 * internally on behalf of consistent target id mappings. If found, the
1404 * remote port structure will be reused. Otherwise, a new remote port
1405 * structure will be allocated.
1407 * Whenever a remote port is allocated, a new fc_remote_port class
1408 * device is created.
1410 * Should not be called from interrupt context.
1413 * This routine assumes no locks are held on entry.
1416 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1417 struct fc_rport_identifiers *ids)
1419 struct fc_internal *fci = to_fc_internal(shost->transportt);
1420 struct fc_rport *rport;
1421 unsigned long flags;
1425 * Search the list of "active" rports, for an rport that has been
1426 * deleted, but we've held off the real delete while the target
1427 * is in a "blocked" state.
1429 spin_lock_irqsave(shost->host_lock, flags);
1431 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1433 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1434 (rport->channel == channel)) {
1436 switch (fc_host_tgtid_bind_type(shost)) {
1437 case FC_TGTID_BIND_BY_WWPN:
1438 case FC_TGTID_BIND_NONE:
1439 if (rport->port_name == ids->port_name)
1442 case FC_TGTID_BIND_BY_WWNN:
1443 if (rport->node_name == ids->node_name)
1446 case FC_TGTID_BIND_BY_ID:
1447 if (rport->port_id == ids->port_id)
1453 struct work_struct *work =
1454 &rport->dev_loss_work;
1456 memcpy(&rport->node_name, &ids->node_name,
1457 sizeof(rport->node_name));
1458 memcpy(&rport->port_name, &ids->port_name,
1459 sizeof(rport->port_name));
1460 rport->port_id = ids->port_id;
1462 rport->port_state = FC_PORTSTATE_ONLINE;
1463 rport->roles = ids->roles;
1465 spin_unlock_irqrestore(shost->host_lock, flags);
1467 if (fci->f->dd_fcrport_size)
1468 memset(rport->dd_data, 0,
1469 fci->f->dd_fcrport_size);
1472 * If we were blocked, we were a target.
1473 * If no longer a target, we leave the timer
1474 * running in case the port changes roles
1475 * prior to the timer expiring. If the timer
1476 * fires, the target will be torn down.
1478 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1481 /* restart the target */
1484 * Stop the target timer first. Take no action
1485 * on the del_timer failure as the state
1486 * machine state change will validate the
1489 if (!cancel_delayed_work(work))
1490 flush_scheduled_work();
1492 /* initiate a scan of the target */
1493 scsi_queue_work(shost, &rport->scan_work);
1500 /* Search the bindings array */
1501 if (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE) {
1503 /* search for a matching consistent binding */
1505 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1507 if (rport->channel != channel)
1510 switch (fc_host_tgtid_bind_type(shost)) {
1511 case FC_TGTID_BIND_BY_WWPN:
1512 if (rport->port_name == ids->port_name)
1515 case FC_TGTID_BIND_BY_WWNN:
1516 if (rport->node_name == ids->node_name)
1519 case FC_TGTID_BIND_BY_ID:
1520 if (rport->port_id == ids->port_id)
1523 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1528 list_move_tail(&rport->peers,
1529 &fc_host_rports(shost));
1535 memcpy(&rport->node_name, &ids->node_name,
1536 sizeof(rport->node_name));
1537 memcpy(&rport->port_name, &ids->port_name,
1538 sizeof(rport->port_name));
1539 rport->port_id = ids->port_id;
1540 rport->roles = ids->roles;
1541 rport->port_state = FC_PORTSTATE_ONLINE;
1543 spin_unlock_irqrestore(shost->host_lock, flags);
1545 if (fci->f->dd_fcrport_size)
1546 memset(rport->dd_data, 0,
1547 fci->f->dd_fcrport_size);
1549 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1550 /* initiate a scan of the target */
1551 scsi_queue_work(shost, &rport->scan_work);
1557 spin_unlock_irqrestore(shost->host_lock, flags);
1559 /* No consistent binding found - create new remote port entry */
1560 rport = fc_rport_create(shost, channel, ids);
1564 EXPORT_SYMBOL(fc_remote_port_add);
1567 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1568 * @rport: The remote port to be terminated
1571 * This routine assumes no locks are held on entry.
1574 fc_rport_terminate(struct fc_rport *rport)
1576 struct Scsi_Host *shost = rport_to_shost(rport);
1577 struct device *dev = &rport->dev;
1578 unsigned long flags;
1580 fc_rport_tgt_remove(rport);
1582 transport_remove_device(dev);
1584 transport_destroy_device(dev);
1585 spin_lock_irqsave(shost->host_lock, flags);
1586 list_del(&rport->peers);
1587 spin_unlock_irqrestore(shost->host_lock, flags);
1588 put_device(&shost->shost_gendev);
1592 * fc_remote_port_delete - notifies the fc transport that a remote
1593 * port is no longer in existence.
1594 * @rport: The remote port that no longer exists
1596 * The LLDD calls this routine to notify the transport that a remote
1597 * port is no longer part of the topology. Note: Although a port
1598 * may no longer be part of the topology, it may persist in the remote
1599 * ports displayed by the fc_host. We do this under 2 conditions:
1600 * - If the port was a scsi target, we delay its deletion by "blocking" it.
1601 * This allows the port to temporarily disappear, then reappear without
1602 * disrupting the SCSI device tree attached to it. During the "blocked"
1603 * period the port will still exist.
1604 * - If the port was a scsi target and disappears for longer than we
1605 * expect, we'll delete the port and the tear down the SCSI device tree
1606 * attached to it. However, we want to semi-persist the target id assigned
1607 * to that port if it eventually does exist. The port structure will
1608 * remain (although with minimal information) so that the target id
1611 * If the remote port is not an FCP Target, it will be fully torn down
1612 * and deallocated, including the fc_remote_port class device.
1614 * If the remote port is an FCP Target, the port will be placed in a
1615 * temporary blocked state. From the LLDD's perspective, the rport no
1616 * longer exists. From the SCSI midlayer's perspective, the SCSI target
1617 * exists, but all sdevs on it are blocked from further I/O. The following
1619 * If the remote port does not return (signaled by a LLDD call to
1620 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
1621 * scsi target is removed - killing all outstanding i/o and removing the
1622 * scsi devices attached ot it. The port structure will be marked Not
1623 * Present and be partially cleared, leaving only enough information to
1624 * recognize the remote port relative to the scsi target id binding if
1625 * it later appears. The port will remain as long as there is a valid
1626 * binding (e.g. until the user changes the binding type or unloads the
1627 * scsi host with the binding).
1629 * If the remote port returns within the dev_loss_tmo value (and matches
1630 * according to the target id binding type), the port structure will be
1631 * reused. If it is no longer a SCSI target, the target will be torn
1632 * down. If it continues to be a SCSI target, then the target will be
1633 * unblocked (allowing i/o to be resumed), and a scan will be activated
1634 * to ensure that all luns are detected.
1636 * Called from normal process context only - cannot be called from interrupt.
1639 * This routine assumes no locks are held on entry.
1642 fc_remote_port_delete(struct fc_rport *rport)
1644 int timeout = rport->dev_loss_tmo;
1646 /* If no scsi target id mapping, delete it */
1647 if (rport->scsi_target_id == -1) {
1648 fc_rport_terminate(rport);
1652 scsi_target_block(&rport->dev);
1654 /* cap the length the devices can be blocked until they are deleted */
1655 schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
1657 rport->port_state = FC_PORTSTATE_BLOCKED;
1659 EXPORT_SYMBOL(fc_remote_port_delete);
1662 * fc_remote_port_rolechg - notifies the fc transport that the roles
1663 * on a remote may have changed.
1664 * @rport: The remote port that changed.
1666 * The LLDD calls this routine to notify the transport that the roles
1667 * on a remote port may have changed. The largest effect of this is
1668 * if a port now becomes a FCP Target, it must be allocated a
1669 * scsi target id. If the port is no longer a FCP target, any
1670 * scsi target id value assigned to it will persist in case the
1671 * role changes back to include FCP Target. No changes in the scsi
1672 * midlayer will be invoked if the role changes (in the expectation
1673 * that the role will be resumed. If it doesn't normal error processing
1676 * Should not be called from interrupt context.
1679 * This routine assumes no locks are held on entry.
1682 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1684 struct Scsi_Host *shost = rport_to_shost(rport);
1685 struct fc_host_attrs *fc_host =
1686 (struct fc_host_attrs *)shost->shost_data;
1687 unsigned long flags;
1690 spin_lock_irqsave(shost->host_lock, flags);
1691 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
1692 if (rport->scsi_target_id == -1) {
1693 rport->scsi_target_id = fc_host->next_target_id++;
1695 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
1698 spin_unlock_irqrestore(shost->host_lock, flags);
1700 rport->roles = roles;
1704 * There may have been a delete timer running on the
1705 * port. Ensure that it is cancelled as we now know
1706 * the port is an FCP Target.
1707 * Note: we know the rport is exists and in an online
1708 * state as the LLDD would not have had an rport
1709 * reference to pass us.
1711 * Take no action on the del_timer failure as the state
1712 * machine state change will validate the
1715 if (!cancel_delayed_work(&rport->dev_loss_work))
1716 flush_scheduled_work();
1718 /* initiate a scan of the target */
1719 scsi_queue_work(shost, &rport->scan_work);
1722 EXPORT_SYMBOL(fc_remote_port_rolechg);
1725 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
1726 * was a SCSI target (thus was blocked), and failed
1727 * to return in the alloted time.
1729 * @data: rport target that failed to reappear in the alloted time.
1732 fc_timeout_deleted_rport(void *data)
1734 struct fc_rport *rport = (struct fc_rport *)data;
1735 struct Scsi_Host *shost = rport_to_shost(rport);
1736 unsigned long flags;
1738 spin_lock_irqsave(shost->host_lock, flags);
1741 * If the port is ONLINE, then it came back, but was no longer an
1742 * FCP target. Thus we need to tear down the scsi_target on it.
1744 if (rport->port_state == FC_PORTSTATE_ONLINE) {
1745 spin_unlock_irqrestore(shost->host_lock, flags);
1747 dev_printk(KERN_ERR, &rport->dev,
1748 "blocked FC remote port time out: removing target\n");
1750 fc_rport_tgt_remove(rport);
1755 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
1756 spin_unlock_irqrestore(shost->host_lock, flags);
1757 dev_printk(KERN_ERR, &rport->dev,
1758 "blocked FC remote port time out: leaving target alone\n");
1762 if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
1763 spin_unlock_irqrestore(shost->host_lock, flags);
1764 dev_printk(KERN_ERR, &rport->dev,
1765 "blocked FC remote port time out: removing target\n");
1766 fc_rport_terminate(rport);
1770 dev_printk(KERN_ERR, &rport->dev,
1771 "blocked FC remote port time out: removing target and "
1772 "saving binding\n");
1774 list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1777 * Note: We do not remove or clear the hostdata area. This allows
1778 * host-specific target data to persist along with the
1779 * scsi_target_id. It's up to the host to manage it's hostdata area.
1783 * Reinitialize port attributes that may change if the port comes back.
1785 rport->maxframe_size = -1;
1786 rport->supported_classes = FC_COS_UNSPECIFIED;
1787 rport->roles = FC_RPORT_ROLE_UNKNOWN;
1788 rport->port_state = FC_PORTSTATE_DELETED;
1790 /* remove the identifiers that aren't used in the consisting binding */
1791 switch (fc_host_tgtid_bind_type(shost)) {
1792 case FC_TGTID_BIND_BY_WWPN:
1793 rport->node_name = -1;
1794 rport->port_id = -1;
1796 case FC_TGTID_BIND_BY_WWNN:
1797 rport->port_name = -1;
1798 rport->port_id = -1;
1800 case FC_TGTID_BIND_BY_ID:
1801 rport->node_name = -1;
1802 rport->port_name = -1;
1804 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1809 * As this only occurs if the remote port (scsi target)
1810 * went away and didn't come back - we'll remove
1811 * all attached scsi devices.
1813 * We'll schedule the shost work item to perform the actual removal
1814 * to avoid recursion in the different flush calls if we perform
1815 * the removal in each target - and there are lots of targets
1816 * whose timeouts fire at the same time.
1819 if ( !(fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED)) {
1820 fc_host_flags(shost) |= FC_SHOST_RPORT_DEL_SCHEDULED;
1821 scsi_queue_work(shost, &fc_host_rport_del_work(shost));
1824 spin_unlock_irqrestore(shost->host_lock, flags);
1828 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1830 * Will unblock the target (in case it went away and has now come back),
1831 * then invoke a scan.
1833 * @data: remote port to be scanned.
1836 fc_scsi_scan_rport(void *data)
1838 struct fc_rport *rport = (struct fc_rport *)data;
1840 scsi_target_unblock(&rport->dev);
1841 scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1847 * fc_shost_remove_rports - called to remove all rports that are marked
1848 * as in a deleted (not connected) state.
1850 * @data: shost whose rports are to be looked at
1853 fc_shost_remove_rports(void *data)
1855 struct Scsi_Host *shost = (struct Scsi_Host *)data;
1856 struct fc_rport *rport, *next_rport;
1857 unsigned long flags;
1859 spin_lock_irqsave(shost->host_lock, flags);
1860 while (fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED) {
1862 fc_host_flags(shost) &= ~FC_SHOST_RPORT_DEL_SCHEDULED;
1865 list_for_each_entry_safe(rport, next_rport,
1866 &fc_host_rport_bindings(shost), peers) {
1867 if (rport->port_state == FC_PORTSTATE_DELETED) {
1868 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1869 spin_unlock_irqrestore(shost->host_lock, flags);
1870 fc_rport_tgt_remove(rport);
1871 spin_lock_irqsave(shost->host_lock, flags);
1872 goto restart_search;
1877 spin_unlock_irqrestore(shost->host_lock, flags);
1881 MODULE_AUTHOR("Martin Hicks");
1882 MODULE_DESCRIPTION("FC Transport Attributes");
1883 MODULE_LICENSE("GPL");
1885 module_init(fc_transport_init);
1886 module_exit(fc_transport_exit);