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"
36 #define FC_PRINTK(x, l, f, a...) printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
39 * Redefine so that we can have same named attributes in the
40 * sdev/starget/host objects.
42 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
43 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
44 __ATTR(_name,_mode,_show,_store)
46 #define fc_enum_name_search(title, table_type, table) \
47 static const char *get_fc_##title##_name(enum table_type table_key) \
52 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
53 if (table[i].value == table_key) { \
54 name = table[i].name; \
61 #define fc_enum_name_match(title, table_type, table) \
62 static int get_fc_##title##_match(const char *table_key, \
63 enum table_type *value) \
67 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
68 if (strncmp(table_key, table[i].name, \
69 table[i].matchlen) == 0) { \
70 *value = table[i].value; \
71 return 0; /* success */ \
74 return 1; /* failure */ \
78 /* Convert fc_port_type values to ascii string name */
80 enum fc_port_type value;
82 } fc_port_type_names[] = {
83 { FC_PORTTYPE_UNKNOWN, "Unknown" },
84 { FC_PORTTYPE_OTHER, "Other" },
85 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
86 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
87 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
88 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
89 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
91 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
92 #define FC_PORTTYPE_MAX_NAMELEN 50
95 /* Convert fc_port_state values to ascii string name */
97 enum fc_port_state value;
99 } fc_port_state_names[] = {
100 { FC_PORTSTATE_UNKNOWN, "Unknown" },
101 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
102 { FC_PORTSTATE_ONLINE, "Online" },
103 { FC_PORTSTATE_OFFLINE, "Offline" },
104 { FC_PORTSTATE_BLOCKED, "Blocked" },
105 { FC_PORTSTATE_BYPASSED, "Bypassed" },
106 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
107 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
108 { FC_PORTSTATE_ERROR, "Error" },
109 { FC_PORTSTATE_LOOPBACK, "Loopback" },
111 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
112 #define FC_PORTSTATE_MAX_NAMELEN 20
115 /* Convert fc_tgtid_binding_type values to ascii string name */
117 enum fc_tgtid_binding_type value;
120 } fc_tgtid_binding_type_names[] = {
121 { FC_TGTID_BIND_NONE, "none", 4 },
122 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
123 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
124 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
126 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
127 fc_tgtid_binding_type_names)
128 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
129 fc_tgtid_binding_type_names)
130 #define FC_BINDTYPE_MAX_NAMELEN 30
133 #define fc_bitfield_name_search(title, table) \
135 get_fc_##title##_names(u32 table_key, char *buf) \
141 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
142 if (table[i].value & table_key) { \
143 len += sprintf(buf + len, "%s%s", \
144 prefix, table[i].name); \
148 len += sprintf(buf + len, "\n"); \
153 /* Convert FC_COS bit values to ascii string name */
158 { FC_COS_CLASS1, "Class 1" },
159 { FC_COS_CLASS2, "Class 2" },
160 { FC_COS_CLASS3, "Class 3" },
161 { FC_COS_CLASS4, "Class 4" },
162 { FC_COS_CLASS6, "Class 6" },
164 fc_bitfield_name_search(cos, fc_cos_names)
167 /* Convert FC_PORTSPEED bit values to ascii string name */
171 } fc_port_speed_names[] = {
172 { FC_PORTSPEED_1GBIT, "1 Gbit" },
173 { FC_PORTSPEED_2GBIT, "2 Gbit" },
174 { FC_PORTSPEED_4GBIT, "4 Gbit" },
175 { FC_PORTSPEED_10GBIT, "10 Gbit" },
176 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
178 fc_bitfield_name_search(port_speed, fc_port_speed_names)
182 show_fc_fc4s (char *buf, u8 *fc4_list)
186 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
187 len += sprintf(buf + len , "0x%02x ", *fc4_list);
188 len += sprintf(buf + len, "\n");
193 /* Convert FC_RPORT_ROLE bit values to ascii string name */
197 } fc_remote_port_role_names[] = {
198 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
199 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
200 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
202 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
205 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
207 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
208 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
209 #define FC_FPORT_PORTID 0x00000e
210 #define FC_FABCTLR_PORTID 0x00000d
211 #define FC_DIRSRVR_PORTID 0x00000c
212 #define FC_TIMESRVR_PORTID 0x00000b
213 #define FC_MGMTSRVR_PORTID 0x00000a
216 static void fc_timeout_blocked_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 15
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_supported_classes(shost) = FC_COS_UNSPECIFIED;
299 memset(fc_host_supported_fc4s(shost), 0,
300 sizeof(fc_host_supported_fc4s(shost)));
301 memset(fc_host_symbolic_name(shost), 0,
302 sizeof(fc_host_symbolic_name(shost)));
303 fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
304 fc_host_maxframe_size(shost) = -1;
305 memset(fc_host_serial_number(shost), 0,
306 sizeof(fc_host_serial_number(shost)));
308 fc_host_port_id(shost) = -1;
309 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
310 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
311 memset(fc_host_active_fc4s(shost), 0,
312 sizeof(fc_host_active_fc4s(shost)));
313 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
314 fc_host_fabric_name(shost) = -1;
316 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
318 INIT_LIST_HEAD(&fc_host_rports(shost));
319 INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
320 fc_host_next_rport_number(shost) = 0;
321 fc_host_next_target_id(shost) = 0;
326 static DECLARE_TRANSPORT_CLASS(fc_host_class,
333 * Setup and Remove actions for remote ports are handled
334 * in the service functions below.
336 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
347 * dev_loss_tmo: the default number of seconds that the FC transport
348 * should insulate the loss of a remote port.
349 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
351 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
353 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
354 MODULE_PARM_DESC(dev_loss_tmo,
355 "Maximum number of seconds that the FC transport should"
356 " insulate the loss of a remote port. Once this value is"
357 " exceeded, the scsi target is removed. Value should be"
358 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
361 static __init int fc_transport_init(void)
363 int error = transport_class_register(&fc_host_class);
366 error = transport_class_register(&fc_rport_class);
369 return transport_class_register(&fc_transport_class);
372 static void __exit fc_transport_exit(void)
374 transport_class_unregister(&fc_transport_class);
375 transport_class_unregister(&fc_rport_class);
376 transport_class_unregister(&fc_host_class);
380 * FC Remote Port Attribute Management
383 #define fc_rport_show_function(field, format_string, sz, cast) \
385 show_fc_rport_##field (struct class_device *cdev, char *buf) \
387 struct fc_rport *rport = transport_class_to_rport(cdev); \
388 struct Scsi_Host *shost = rport_to_shost(rport); \
389 struct fc_internal *i = to_fc_internal(shost->transportt); \
390 if (i->f->get_rport_##field) \
391 i->f->get_rport_##field(rport); \
392 return snprintf(buf, sz, format_string, cast rport->field); \
395 #define fc_rport_store_function(field) \
397 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
401 struct fc_rport *rport = transport_class_to_rport(cdev); \
402 struct Scsi_Host *shost = rport_to_shost(rport); \
403 struct fc_internal *i = to_fc_internal(shost->transportt); \
404 val = simple_strtoul(buf, NULL, 0); \
405 i->f->set_rport_##field(rport, val); \
409 #define fc_rport_rd_attr(field, format_string, sz) \
410 fc_rport_show_function(field, format_string, sz, ) \
411 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
412 show_fc_rport_##field, NULL)
414 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
415 fc_rport_show_function(field, format_string, sz, (cast)) \
416 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
417 show_fc_rport_##field, NULL)
419 #define fc_rport_rw_attr(field, format_string, sz) \
420 fc_rport_show_function(field, format_string, sz, ) \
421 fc_rport_store_function(field) \
422 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
423 show_fc_rport_##field, \
424 store_fc_rport_##field)
427 #define fc_private_rport_show_function(field, format_string, sz, cast) \
429 show_fc_rport_##field (struct class_device *cdev, char *buf) \
431 struct fc_rport *rport = transport_class_to_rport(cdev); \
432 return snprintf(buf, sz, format_string, cast rport->field); \
435 #define fc_private_rport_rd_attr(field, format_string, sz) \
436 fc_private_rport_show_function(field, format_string, sz, ) \
437 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
438 show_fc_rport_##field, NULL)
440 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
441 fc_private_rport_show_function(field, format_string, sz, (cast)) \
442 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
443 show_fc_rport_##field, NULL)
446 #define fc_private_rport_rd_enum_attr(title, maxlen) \
448 show_fc_rport_##title (struct class_device *cdev, char *buf) \
450 struct fc_rport *rport = transport_class_to_rport(cdev); \
452 name = get_fc_##title##_name(rport->title); \
455 return snprintf(buf, maxlen, "%s\n", name); \
457 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
458 show_fc_rport_##title, NULL)
461 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
462 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
463 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
464 i->private_rport_attrs[count].store = NULL; \
465 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
466 if (i->f->show_rport_##field) \
469 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
470 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
471 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
472 i->private_rport_attrs[count].store = NULL; \
473 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
476 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
477 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
478 if (!i->f->set_rport_##field) { \
479 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
480 i->private_rport_attrs[count].store = NULL; \
482 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
483 if (i->f->show_rport_##field) \
487 /* The FC Transport Remote Port Attributes: */
489 /* Fixed Remote Port Attributes */
491 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
494 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
496 struct fc_rport *rport = transport_class_to_rport(cdev);
497 if (rport->supported_classes == FC_COS_UNSPECIFIED)
498 return snprintf(buf, 20, "unspecified\n");
499 return get_fc_cos_names(rport->supported_classes, buf);
501 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
502 show_fc_rport_supported_classes, NULL);
504 /* Dynamic Remote Port Attributes */
506 fc_rport_rw_attr(dev_loss_tmo, "%d\n", 20);
509 /* Private Remote Port Attributes */
511 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
512 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
513 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
516 show_fc_rport_roles (struct class_device *cdev, char *buf)
518 struct fc_rport *rport = transport_class_to_rport(cdev);
520 /* identify any roles that are port_id specific */
521 if ((rport->port_id != -1) &&
522 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
523 FC_WELLKNOWN_PORTID_MASK) {
524 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
525 case FC_FPORT_PORTID:
526 return snprintf(buf, 30, "Fabric Port\n");
527 case FC_FABCTLR_PORTID:
528 return snprintf(buf, 30, "Fabric Controller\n");
529 case FC_DIRSRVR_PORTID:
530 return snprintf(buf, 30, "Directory Server\n");
531 case FC_TIMESRVR_PORTID:
532 return snprintf(buf, 30, "Time Server\n");
533 case FC_MGMTSRVR_PORTID:
534 return snprintf(buf, 30, "Management Server\n");
536 return snprintf(buf, 30, "Unknown Fabric Entity\n");
539 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
540 return snprintf(buf, 20, "unknown\n");
541 return get_fc_remote_port_roles_names(rport->roles, buf);
544 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
545 show_fc_rport_roles, NULL);
547 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
548 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
553 * FC SCSI Target Attribute Management
557 * Note: in the target show function we recognize when the remote
558 * port is in the heirarchy and do not allow the driver to get
559 * involved in sysfs functions. The driver only gets involved if
560 * it's the "old" style that doesn't use rports.
562 #define fc_starget_show_function(field, format_string, sz, cast) \
564 show_fc_starget_##field (struct class_device *cdev, char *buf) \
566 struct scsi_target *starget = transport_class_to_starget(cdev); \
567 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
568 struct fc_internal *i = to_fc_internal(shost->transportt); \
569 struct fc_rport *rport = starget_to_rport(starget); \
571 fc_starget_##field(starget) = rport->field; \
572 else if (i->f->get_starget_##field) \
573 i->f->get_starget_##field(starget); \
574 return snprintf(buf, sz, format_string, \
575 cast fc_starget_##field(starget)); \
578 #define fc_starget_rd_attr(field, format_string, sz) \
579 fc_starget_show_function(field, format_string, sz, ) \
580 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
581 show_fc_starget_##field, NULL)
583 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
584 fc_starget_show_function(field, format_string, sz, (cast)) \
585 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
586 show_fc_starget_##field, NULL)
588 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
589 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
590 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
591 i->private_starget_attrs[count].store = NULL; \
592 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
593 if (i->f->show_starget_##field) \
596 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
597 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
598 if (!i->f->set_starget_##field) { \
599 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
600 i->private_starget_attrs[count].store = NULL; \
602 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
603 if (i->f->show_starget_##field) \
606 /* The FC Transport SCSI Target Attributes: */
607 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
608 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
609 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
613 * Host Attribute Management
616 #define fc_host_show_function(field, format_string, sz, cast) \
618 show_fc_host_##field (struct class_device *cdev, char *buf) \
620 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
621 struct fc_internal *i = to_fc_internal(shost->transportt); \
622 if (i->f->get_host_##field) \
623 i->f->get_host_##field(shost); \
624 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
627 #define fc_host_store_function(field) \
629 store_fc_host_##field(struct class_device *cdev, const char *buf, \
633 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
634 struct fc_internal *i = to_fc_internal(shost->transportt); \
636 val = simple_strtoul(buf, NULL, 0); \
637 i->f->set_host_##field(shost, val); \
641 #define fc_host_rd_attr(field, format_string, sz) \
642 fc_host_show_function(field, format_string, sz, ) \
643 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
644 show_fc_host_##field, NULL)
646 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
647 fc_host_show_function(field, format_string, sz, (cast)) \
648 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
649 show_fc_host_##field, NULL)
651 #define fc_host_rw_attr(field, format_string, sz) \
652 fc_host_show_function(field, format_string, sz, ) \
653 fc_host_store_function(field) \
654 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
655 show_fc_host_##field, \
656 store_fc_host_##field)
658 #define fc_host_rd_enum_attr(title, maxlen) \
660 show_fc_host_##title (struct class_device *cdev, char *buf) \
662 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
663 struct fc_internal *i = to_fc_internal(shost->transportt); \
665 if (i->f->get_host_##title) \
666 i->f->get_host_##title(shost); \
667 name = get_fc_##title##_name(fc_host_##title(shost)); \
670 return snprintf(buf, maxlen, "%s\n", name); \
672 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
674 #define SETUP_HOST_ATTRIBUTE_RD(field) \
675 i->private_host_attrs[count] = class_device_attr_host_##field; \
676 i->private_host_attrs[count].attr.mode = S_IRUGO; \
677 i->private_host_attrs[count].store = NULL; \
678 i->host_attrs[count] = &i->private_host_attrs[count]; \
679 if (i->f->show_host_##field) \
682 #define SETUP_HOST_ATTRIBUTE_RW(field) \
683 i->private_host_attrs[count] = class_device_attr_host_##field; \
684 if (!i->f->set_host_##field) { \
685 i->private_host_attrs[count].attr.mode = S_IRUGO; \
686 i->private_host_attrs[count].store = NULL; \
688 i->host_attrs[count] = &i->private_host_attrs[count]; \
689 if (i->f->show_host_##field) \
693 #define fc_private_host_show_function(field, format_string, sz, cast) \
695 show_fc_host_##field (struct class_device *cdev, char *buf) \
697 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
698 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
701 #define fc_private_host_rd_attr(field, format_string, sz) \
702 fc_private_host_show_function(field, format_string, sz, ) \
703 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
704 show_fc_host_##field, NULL)
706 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
707 fc_private_host_show_function(field, format_string, sz, (cast)) \
708 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
709 show_fc_host_##field, NULL)
711 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
712 i->private_host_attrs[count] = class_device_attr_host_##field; \
713 i->private_host_attrs[count].attr.mode = S_IRUGO; \
714 i->private_host_attrs[count].store = NULL; \
715 i->host_attrs[count] = &i->private_host_attrs[count]; \
718 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
719 i->private_host_attrs[count] = class_device_attr_host_##field; \
720 i->host_attrs[count] = &i->private_host_attrs[count]; \
724 /* Fixed Host Attributes */
727 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
729 struct Scsi_Host *shost = transport_class_to_shost(cdev);
731 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
732 return snprintf(buf, 20, "unspecified\n");
734 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
736 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
737 show_fc_host_supported_classes, NULL);
740 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
742 struct Scsi_Host *shost = transport_class_to_shost(cdev);
743 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
745 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
746 show_fc_host_supported_fc4s, NULL);
749 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
751 struct Scsi_Host *shost = transport_class_to_shost(cdev);
753 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
754 return snprintf(buf, 20, "unknown\n");
756 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
758 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
759 show_fc_host_supported_speeds, NULL);
762 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
763 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
764 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
765 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
766 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
769 /* Dynamic Host Attributes */
772 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
774 struct Scsi_Host *shost = transport_class_to_shost(cdev);
775 struct fc_internal *i = to_fc_internal(shost->transportt);
777 if (i->f->get_host_active_fc4s)
778 i->f->get_host_active_fc4s(shost);
780 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
782 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
783 show_fc_host_active_fc4s, NULL);
786 show_fc_host_speed (struct class_device *cdev, char *buf)
788 struct Scsi_Host *shost = transport_class_to_shost(cdev);
789 struct fc_internal *i = to_fc_internal(shost->transportt);
791 if (i->f->get_host_speed)
792 i->f->get_host_speed(shost);
794 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
795 return snprintf(buf, 20, "unknown\n");
797 return get_fc_port_speed_names(fc_host_speed(shost), buf);
799 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
800 show_fc_host_speed, NULL);
803 fc_host_rd_attr(port_id, "0x%06x\n", 20);
804 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
805 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
806 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
809 /* Private Host Attributes */
812 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
814 struct Scsi_Host *shost = transport_class_to_shost(cdev);
817 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
820 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
823 #define get_list_head_entry(pos, head, member) \
824 pos = list_entry((head)->next, typeof(*pos), member)
827 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
828 const char *buf, size_t count)
830 struct Scsi_Host *shost = transport_class_to_shost(cdev);
831 struct fc_rport *rport;
832 enum fc_tgtid_binding_type val;
835 if (get_fc_tgtid_bind_type_match(buf, &val))
838 /* if changing bind type, purge all unused consistent bindings */
839 if (val != fc_host_tgtid_bind_type(shost)) {
840 spin_lock_irqsave(shost->host_lock, flags);
841 while (!list_empty(&fc_host_rport_bindings(shost))) {
842 get_list_head_entry(rport,
843 &fc_host_rport_bindings(shost), peers);
844 spin_unlock_irqrestore(shost->host_lock, flags);
845 fc_rport_terminate(rport);
846 spin_lock_irqsave(shost->host_lock, flags);
848 spin_unlock_irqrestore(shost->host_lock, flags);
851 fc_host_tgtid_bind_type(shost) = val;
855 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
856 show_fc_private_host_tgtid_bind_type,
857 store_fc_private_host_tgtid_bind_type);
860 * Host Statistics Management
863 /* Show a given an attribute in the statistics group */
865 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
867 struct Scsi_Host *shost = transport_class_to_shost(cdev);
868 struct fc_internal *i = to_fc_internal(shost->transportt);
869 struct fc_host_statistics *stats;
870 ssize_t ret = -ENOENT;
872 if (offset > sizeof(struct fc_host_statistics) ||
873 offset % sizeof(u64) != 0)
876 if (i->f->get_fc_host_stats) {
877 stats = (i->f->get_fc_host_stats)(shost);
879 ret = snprintf(buf, 20, "0x%llx\n",
880 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
886 /* generate a read-only statistics attribute */
887 #define fc_host_statistic(name) \
888 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
890 return fc_stat_show(cd, buf, \
891 offsetof(struct fc_host_statistics, name)); \
893 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
895 fc_host_statistic(seconds_since_last_reset);
896 fc_host_statistic(tx_frames);
897 fc_host_statistic(tx_words);
898 fc_host_statistic(rx_frames);
899 fc_host_statistic(rx_words);
900 fc_host_statistic(lip_count);
901 fc_host_statistic(nos_count);
902 fc_host_statistic(error_frames);
903 fc_host_statistic(dumped_frames);
904 fc_host_statistic(link_failure_count);
905 fc_host_statistic(loss_of_sync_count);
906 fc_host_statistic(loss_of_signal_count);
907 fc_host_statistic(prim_seq_protocol_err_count);
908 fc_host_statistic(invalid_tx_word_count);
909 fc_host_statistic(invalid_crc_count);
910 fc_host_statistic(fcp_input_requests);
911 fc_host_statistic(fcp_output_requests);
912 fc_host_statistic(fcp_control_requests);
913 fc_host_statistic(fcp_input_megabytes);
914 fc_host_statistic(fcp_output_megabytes);
917 fc_reset_statistics(struct class_device *cdev, const char *buf,
920 struct Scsi_Host *shost = transport_class_to_shost(cdev);
921 struct fc_internal *i = to_fc_internal(shost->transportt);
923 /* ignore any data value written to the attribute */
924 if (i->f->reset_fc_host_stats) {
925 i->f->reset_fc_host_stats(shost);
931 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
932 fc_reset_statistics);
935 static struct attribute *fc_statistics_attrs[] = {
936 &class_device_attr_host_seconds_since_last_reset.attr,
937 &class_device_attr_host_tx_frames.attr,
938 &class_device_attr_host_tx_words.attr,
939 &class_device_attr_host_rx_frames.attr,
940 &class_device_attr_host_rx_words.attr,
941 &class_device_attr_host_lip_count.attr,
942 &class_device_attr_host_nos_count.attr,
943 &class_device_attr_host_error_frames.attr,
944 &class_device_attr_host_dumped_frames.attr,
945 &class_device_attr_host_link_failure_count.attr,
946 &class_device_attr_host_loss_of_sync_count.attr,
947 &class_device_attr_host_loss_of_signal_count.attr,
948 &class_device_attr_host_prim_seq_protocol_err_count.attr,
949 &class_device_attr_host_invalid_tx_word_count.attr,
950 &class_device_attr_host_invalid_crc_count.attr,
951 &class_device_attr_host_fcp_input_requests.attr,
952 &class_device_attr_host_fcp_output_requests.attr,
953 &class_device_attr_host_fcp_control_requests.attr,
954 &class_device_attr_host_fcp_input_megabytes.attr,
955 &class_device_attr_host_fcp_output_megabytes.attr,
956 &class_device_attr_host_reset_statistics.attr,
960 static struct attribute_group fc_statistics_group = {
961 .name = "statistics",
962 .attrs = fc_statistics_attrs,
965 static int fc_host_match(struct attribute_container *cont,
968 struct Scsi_Host *shost;
969 struct fc_internal *i;
971 if (!scsi_is_host_device(dev))
974 shost = dev_to_shost(dev);
975 if (!shost->transportt || shost->transportt->host_attrs.ac.class
976 != &fc_host_class.class)
979 i = to_fc_internal(shost->transportt);
981 return &i->t.host_attrs.ac == cont;
984 static int fc_target_match(struct attribute_container *cont,
987 struct Scsi_Host *shost;
988 struct fc_internal *i;
990 if (!scsi_is_target_device(dev))
993 shost = dev_to_shost(dev->parent);
994 if (!shost->transportt || shost->transportt->host_attrs.ac.class
995 != &fc_host_class.class)
998 i = to_fc_internal(shost->transportt);
1000 return &i->t.target_attrs.ac == cont;
1003 static void fc_rport_dev_release(struct device *dev)
1005 struct fc_rport *rport = dev_to_rport(dev);
1006 put_device(dev->parent);
1010 int scsi_is_fc_rport(const struct device *dev)
1012 return dev->release == fc_rport_dev_release;
1014 EXPORT_SYMBOL(scsi_is_fc_rport);
1016 static int fc_rport_match(struct attribute_container *cont,
1019 struct Scsi_Host *shost;
1020 struct fc_internal *i;
1022 if (!scsi_is_fc_rport(dev))
1025 shost = dev_to_shost(dev->parent);
1026 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1027 != &fc_host_class.class)
1030 i = to_fc_internal(shost->transportt);
1032 return &i->rport_attr_cont.ac == cont;
1037 * Must be called with shost->host_lock held
1039 static struct device *fc_target_parent(struct Scsi_Host *shost,
1040 int channel, uint id)
1042 struct fc_rport *rport;
1044 list_for_each_entry(rport, &fc_host_rports(shost), peers)
1045 if ((rport->channel == channel) &&
1046 (rport->scsi_target_id == id))
1052 struct scsi_transport_template *
1053 fc_attach_transport(struct fc_function_template *ft)
1055 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1062 memset(i, 0, sizeof(struct fc_internal));
1064 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1065 i->t.target_attrs.ac.class = &fc_transport_class.class;
1066 i->t.target_attrs.ac.match = fc_target_match;
1067 i->t.target_size = sizeof(struct fc_starget_attrs);
1068 transport_container_register(&i->t.target_attrs);
1070 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1071 i->t.host_attrs.ac.class = &fc_host_class.class;
1072 i->t.host_attrs.ac.match = fc_host_match;
1073 i->t.host_size = sizeof(struct fc_host_attrs);
1074 if (ft->get_fc_host_stats)
1075 i->t.host_attrs.statistics = &fc_statistics_group;
1076 transport_container_register(&i->t.host_attrs);
1078 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1079 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1080 i->rport_attr_cont.ac.match = fc_rport_match;
1081 transport_container_register(&i->rport_attr_cont);
1085 /* Transport uses the shost workq for scsi scanning */
1086 i->t.create_work_queue = 1;
1088 i->t.target_parent = fc_target_parent;
1091 * Setup SCSI Target Attributes.
1094 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1095 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1096 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1098 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1100 i->starget_attrs[count] = NULL;
1104 * Setup SCSI Host Attributes.
1107 SETUP_HOST_ATTRIBUTE_RD(node_name);
1108 SETUP_HOST_ATTRIBUTE_RD(port_name);
1109 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1110 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1111 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1112 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1113 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1114 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1116 SETUP_HOST_ATTRIBUTE_RD(port_id);
1117 SETUP_HOST_ATTRIBUTE_RD(port_type);
1118 SETUP_HOST_ATTRIBUTE_RD(port_state);
1119 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1120 SETUP_HOST_ATTRIBUTE_RD(speed);
1121 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1123 /* Transport-managed attributes */
1124 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1126 BUG_ON(count > FC_HOST_NUM_ATTRS);
1128 i->host_attrs[count] = NULL;
1131 * Setup Remote Port Attributes.
1134 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1135 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1136 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1137 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1138 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1139 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1140 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1141 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1142 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1144 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1146 i->rport_attrs[count] = NULL;
1150 EXPORT_SYMBOL(fc_attach_transport);
1152 void fc_release_transport(struct scsi_transport_template *t)
1154 struct fc_internal *i = to_fc_internal(t);
1156 transport_container_unregister(&i->t.target_attrs);
1157 transport_container_unregister(&i->t.host_attrs);
1158 transport_container_unregister(&i->rport_attr_cont);
1162 EXPORT_SYMBOL(fc_release_transport);
1166 * fc_remove_host - called to terminate any fc_transport-related elements
1168 * @rport: remote port to be unblocked.
1170 * This routine is expected to be called immediately preceeding the
1171 * a driver's call to scsi_remove_host().
1173 * WARNING: A driver utilizing the fc_transport, which fails to call
1174 * this routine prior to scsi_remote_host(), will leave dangling
1175 * objects in /sys/class/fc_remote_ports. Access to any of these
1176 * objects can result in a system crash !!!
1179 * This routine assumes no locks are held on entry.
1182 fc_remove_host(struct Scsi_Host *shost)
1184 struct fc_rport *rport, *next_rport;
1186 /* Remove any remote ports */
1187 list_for_each_entry_safe(rport, next_rport,
1188 &fc_host_rports(shost), peers)
1189 fc_rport_terminate(rport);
1190 list_for_each_entry_safe(rport, next_rport,
1191 &fc_host_rport_bindings(shost), peers)
1192 fc_rport_terminate(rport);
1194 EXPORT_SYMBOL(fc_remove_host);
1197 * fc_rport_create - allocates and creates a remote FC port.
1198 * @shost: scsi host the remote port is connected to.
1199 * @channel: Channel on shost port connected to.
1200 * @ids: The world wide names, fc address, and FC4 port
1201 * roles for the remote port.
1203 * Allocates and creates the remoter port structure, including the
1204 * class and sysfs creation.
1207 * This routine assumes no locks are held on entry.
1210 fc_rport_create(struct Scsi_Host *shost, int channel,
1211 struct fc_rport_identifiers *ids)
1213 struct fc_host_attrs *fc_host =
1214 (struct fc_host_attrs *)shost->shost_data;
1215 struct fc_internal *fci = to_fc_internal(shost->transportt);
1216 struct fc_rport *rport;
1218 unsigned long flags;
1222 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1223 rport = kmalloc(size, GFP_KERNEL);
1224 if (unlikely(!rport)) {
1225 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1228 memset(rport, 0, size);
1230 rport->maxframe_size = -1;
1231 rport->supported_classes = FC_COS_UNSPECIFIED;
1232 rport->dev_loss_tmo = fc_dev_loss_tmo;
1233 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1234 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1235 rport->port_id = ids->port_id;
1236 rport->roles = ids->roles;
1237 rport->port_state = FC_PORTSTATE_ONLINE;
1238 if (fci->f->dd_fcrport_size)
1239 rport->dd_data = &rport[1];
1240 rport->channel = channel;
1242 INIT_WORK(&rport->dev_loss_work, fc_timeout_blocked_rport, rport);
1243 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1245 spin_lock_irqsave(shost->host_lock, flags);
1247 rport->number = fc_host->next_rport_number++;
1248 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1249 rport->scsi_target_id = fc_host->next_target_id++;
1251 rport->scsi_target_id = -1;
1252 list_add_tail(&rport->peers, &fc_host_rports(shost));
1253 get_device(&shost->shost_gendev);
1255 spin_unlock_irqrestore(shost->host_lock, flags);
1258 device_initialize(dev);
1259 dev->parent = get_device(&shost->shost_gendev);
1260 dev->release = fc_rport_dev_release;
1261 sprintf(dev->bus_id, "rport-%d:%d-%d",
1262 shost->host_no, channel, rport->number);
1263 transport_setup_device(dev);
1265 error = device_add(dev);
1267 printk(KERN_ERR "FC Remote Port device_add failed\n");
1270 transport_add_device(dev);
1271 transport_configure_device(dev);
1273 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1274 /* initiate a scan of the target */
1275 scsi_queue_work(shost, &rport->scan_work);
1280 transport_destroy_device(dev);
1281 put_device(dev->parent);
1282 spin_lock_irqsave(shost->host_lock, flags);
1283 list_del(&rport->peers);
1284 put_device(&shost->shost_gendev);
1285 spin_unlock_irqrestore(shost->host_lock, flags);
1286 put_device(dev->parent);
1292 * fc_remote_port_add - notifies the fc transport of the existence
1293 * of a remote FC port.
1294 * @shost: scsi host the remote port is connected to.
1295 * @channel: Channel on shost port connected to.
1296 * @ids: The world wide names, fc address, and FC4 port
1297 * roles for the remote port.
1299 * The LLDD calls this routine to notify the transport of the existence
1300 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1301 * of the port, it's FC address (port_id), and the FC4 roles that are
1302 * active for the port.
1304 * For ports that are FCP targets (aka scsi targets), the FC transport
1305 * maintains consistent target id bindings on behalf of the LLDD.
1306 * A consistent target id binding is an assignment of a target id to
1307 * a remote port identifier, which persists while the scsi host is
1308 * attached. The remote port can disappear, then later reappear, and
1309 * it's target id assignment remains the same. This allows for shifts
1310 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1311 * changes to the scsi subsystem which is based on scsi host number and
1312 * target id values. Bindings are only valid during the attachment of
1313 * the scsi host. If the host detaches, then later re-attaches, target
1314 * id bindings may change.
1316 * This routine is responsible for returning a remote port structure.
1317 * The routine will search the list of remote ports it maintains
1318 * internally on behalf of consistent target id mappings. If found, the
1319 * remote port structure will be reused. Otherwise, a new remote port
1320 * structure will be allocated.
1322 * Whenever a remote port is allocated, a new fc_remote_port class
1323 * device is created.
1325 * Should not be called from interrupt context.
1328 * This routine assumes no locks are held on entry.
1331 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1332 struct fc_rport_identifiers *ids)
1334 struct fc_rport *rport;
1335 unsigned long flags;
1338 if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1339 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1341 /* search for a matching consistent binding */
1343 spin_lock_irqsave(shost->host_lock, flags);
1345 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1347 if (rport->channel != channel)
1350 switch (fc_host_tgtid_bind_type(shost)) {
1351 case FC_TGTID_BIND_BY_WWPN:
1352 if (rport->port_name == ids->port_name)
1355 case FC_TGTID_BIND_BY_WWNN:
1356 if (rport->node_name == ids->node_name)
1359 case FC_TGTID_BIND_BY_ID:
1360 if (rport->port_id == ids->port_id)
1363 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1368 list_move_tail(&rport->peers,
1369 &fc_host_rports(shost));
1374 spin_unlock_irqrestore(shost->host_lock, flags);
1377 memcpy(&rport->node_name, &ids->node_name,
1378 sizeof(rport->node_name));
1379 memcpy(&rport->port_name, &ids->port_name,
1380 sizeof(rport->port_name));
1381 rport->port_id = ids->port_id;
1382 rport->roles = ids->roles;
1383 rport->port_state = FC_PORTSTATE_ONLINE;
1385 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1386 /* initiate a scan of the target */
1387 scsi_queue_work(shost, &rport->scan_work);
1393 /* No consistent binding found - create new remote port entry */
1394 rport = fc_rport_create(shost, channel, ids);
1398 EXPORT_SYMBOL(fc_remote_port_add);
1401 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1402 * @rport: The remote port to be operated on
1405 fc_rport_tgt_remove(struct fc_rport *rport)
1407 struct Scsi_Host *shost = rport_to_shost(rport);
1409 scsi_target_unblock(&rport->dev);
1411 /* Stop anything on the workq */
1412 if (!cancel_delayed_work(&rport->dev_loss_work))
1413 flush_scheduled_work();
1414 scsi_flush_work(shost);
1416 scsi_remove_target(&rport->dev);
1420 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1421 * @rport: The remote port to be terminated
1424 * This routine assumes no locks are held on entry.
1427 fc_rport_terminate(struct fc_rport *rport)
1429 struct Scsi_Host *shost = rport_to_shost(rport);
1430 struct device *dev = &rport->dev;
1431 unsigned long flags;
1433 fc_rport_tgt_remove(rport);
1435 transport_remove_device(dev);
1437 transport_destroy_device(dev);
1438 spin_lock_irqsave(shost->host_lock, flags);
1439 list_del(&rport->peers);
1440 spin_unlock_irqrestore(shost->host_lock, flags);
1441 put_device(&shost->shost_gendev);
1445 * fc_remote_port_delete - notifies the fc transport that a remote
1446 * port is no longer in existence.
1447 * @rport: The remote port that no longer exists
1449 * The LLDD calls this routine to notify the transport that a remote
1450 * port is no longer part of the topology. Note: Although a port
1451 * may no longer be part of the topology, it may persist in the remote
1452 * ports displayed by the fc_host. This is done so that target id
1453 * mappings (managed via the remote port structures), are always visible
1454 * as long as the mapping is valid, regardless of port state,
1456 * If the remote port is not an FCP Target, it will be fully torn down
1457 * and deallocated, including the fc_remote_port class device.
1459 * If the remote port is an FCP Target, the port structure will be
1460 * marked as Not Present, but will remain as long as there is a valid
1461 * SCSI target id mapping associated with the port structure. Validity
1462 * is determined by the binding type. If binding by wwpn, then the port
1463 * structure is always valid and will not be deallocated until the host
1464 * is removed. If binding by wwnn, then the port structure is valid
1465 * until another port with the same node name is found in the topology.
1466 * If binding by port id (fc address), then the port structure is valid
1467 * valid until another port with the same address is identified.
1469 * Called from interrupt or normal process context.
1472 * This routine assumes no locks are held on entry.
1475 fc_remote_port_delete(struct fc_rport *rport)
1477 struct Scsi_Host *shost = rport_to_shost(rport);
1478 unsigned long flags;
1480 /* If no scsi target id mapping or consistent binding type, delete it */
1481 if ((rport->scsi_target_id == -1) ||
1482 (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE)) {
1483 fc_rport_terminate(rport);
1487 fc_rport_tgt_remove(rport);
1489 spin_lock_irqsave(shost->host_lock, flags);
1490 list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1491 spin_unlock_irqrestore(shost->host_lock, flags);
1494 * Note: We do not remove or clear the hostdata area. This allows
1495 * host-specific target data to persist along with the
1496 * scsi_target_id. It's up to the host to manage it's hostdata area.
1500 * Reinitialize port attributes that may change if the port comes back.
1502 rport->maxframe_size = -1;
1503 rport->supported_classes = FC_COS_UNSPECIFIED;
1504 rport->roles = FC_RPORT_ROLE_UNKNOWN;
1505 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1507 /* remove the identifiers that aren't used in the consisting binding */
1508 switch (fc_host_tgtid_bind_type(shost)) {
1509 case FC_TGTID_BIND_BY_WWPN:
1510 rport->node_name = -1;
1511 rport->port_id = -1;
1513 case FC_TGTID_BIND_BY_WWNN:
1514 rport->port_name = -1;
1515 rport->port_id = -1;
1517 case FC_TGTID_BIND_BY_ID:
1518 rport->node_name = -1;
1519 rport->port_name = -1;
1521 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1525 EXPORT_SYMBOL(fc_remote_port_delete);
1528 * fc_remote_port_rolechg - notifies the fc transport that the roles
1529 * on a remote may have changed.
1530 * @rport: The remote port that changed.
1532 * The LLDD calls this routine to notify the transport that the roles
1533 * on a remote port may have changed. The largest effect of this is
1534 * if a port now becomes a FCP Target, it must be allocated a
1535 * scsi target id. If the port is no longer a FCP target, any
1536 * scsi target id value assigned to it will persist in case the
1537 * role changes back to include FCP Target. No changes in the scsi
1538 * midlayer will be invoked if the role changes (in the expectation
1539 * that the role will be resumed. If it doesn't normal error processing
1542 * Should not be called from interrupt context.
1545 * This routine assumes no locks are held on entry.
1548 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1550 struct Scsi_Host *shost = rport_to_shost(rport);
1551 struct fc_host_attrs *fc_host =
1552 (struct fc_host_attrs *)shost->shost_data;
1553 unsigned long flags;
1556 rport->roles = roles;
1558 spin_lock_irqsave(shost->host_lock, flags);
1559 if ((rport->scsi_target_id == -1) &&
1560 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
1561 rport->scsi_target_id = fc_host->next_target_id++;
1564 spin_unlock_irqrestore(shost->host_lock, flags);
1567 /* initiate a scan of the target */
1568 scsi_queue_work(shost, &rport->scan_work);
1570 EXPORT_SYMBOL(fc_remote_port_rolechg);
1573 * fc_timeout_blocked_rport - Timeout handler for blocked remote port
1574 * that fails to return in the alloted time.
1575 * @data: scsi target that failed to reappear in the alloted time.
1578 fc_timeout_blocked_rport(void *data)
1580 struct fc_rport *rport = (struct fc_rport *)data;
1582 rport->port_state = FC_PORTSTATE_OFFLINE;
1584 dev_printk(KERN_ERR, &rport->dev,
1585 "blocked FC remote port time out: removing target\n");
1588 * As this only occurs if the remote port (scsi target)
1589 * went away and didn't come back - we'll remove
1590 * all attached scsi devices.
1592 scsi_target_unblock(&rport->dev);
1593 scsi_remove_target(&rport->dev);
1597 * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
1598 * @rport: remote port to be blocked.
1600 * scsi lldd's with a FC transport call this routine to temporarily stop
1601 * all scsi traffic to a remote port. If the port is not a SCSI target,
1602 * no action is taken. If the port is a SCSI target, all attached devices
1603 * are placed into a SDEV_BLOCK state and a timer is started. The timer is
1604 * represents the maximum amount of time the port may be blocked. If the
1605 * timer expires, the port is considered non-existent and the attached
1606 * scsi devices will be removed.
1608 * Called from interrupt or normal process context.
1610 * Returns zero if successful or error if not
1613 * This routine assumes no locks are held on entry.
1615 * The timeout and timer types are extracted from the fc transport
1616 * attributes from the caller's rport pointer.
1619 fc_remote_port_block(struct fc_rport *rport)
1621 int timeout = rport->dev_loss_tmo;
1622 struct work_struct *work = &rport->dev_loss_work;
1624 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1627 scsi_target_block(&rport->dev);
1629 /* cap the length the devices can be blocked */
1630 schedule_delayed_work(work, timeout * HZ);
1632 rport->port_state = FC_PORTSTATE_BLOCKED;
1635 EXPORT_SYMBOL(fc_remote_port_block);
1638 * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
1639 * @rport: remote port to be unblocked.
1641 * scsi lld's with a FC transport call this routine to restart IO to all
1642 * devices associated with the caller's scsi target following a fc_target_block
1643 * request. Called from interrupt or normal process context.
1646 * This routine assumes no locks are held on entry.
1649 fc_remote_port_unblock(struct fc_rport *rport)
1651 struct work_struct *work = &rport->dev_loss_work;
1652 struct Scsi_Host *shost = rport_to_shost(rport);
1655 * Stop the target timer first. Take no action on the del_timer
1656 * failure as the state machine state change will validate the
1659 if (!cancel_delayed_work(work))
1660 flush_scheduled_work();
1662 if (rport->port_state == FC_PORTSTATE_OFFLINE)
1664 * initiate a scan of the target as the target has
1667 scsi_queue_work(shost, &rport->scan_work);
1669 scsi_target_unblock(&rport->dev);
1671 rport->port_state = FC_PORTSTATE_ONLINE;
1673 EXPORT_SYMBOL(fc_remote_port_unblock);
1676 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1677 * @data: remote port to be scanned.
1680 fc_scsi_scan_rport(void *data)
1682 struct fc_rport *rport = (struct fc_rport *)data;
1684 scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1689 MODULE_AUTHOR("Martin Hicks");
1690 MODULE_DESCRIPTION("FC Transport Attributes");
1691 MODULE_LICENSE("GPL");
1693 module_init(fc_transport_init);
1694 module_exit(fc_transport_exit);