2 * FiberChannel transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Copyright (C) 2004-2005 James Smart, Emulex Corporation
23 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include "scsi_priv.h"
36 * Redefine so that we can have same named attributes in the
37 * sdev/starget/host objects.
39 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
40 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
41 __ATTR(_name,_mode,_show,_store)
43 #define fc_enum_name_search(title, table_type, table) \
44 static const char *get_fc_##title##_name(enum table_type table_key) \
49 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
50 if (table[i].value == table_key) { \
51 name = table[i].name; \
58 #define fc_enum_name_match(title, table_type, table) \
59 static int get_fc_##title##_match(const char *table_key, \
60 enum table_type *value) \
64 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
65 if (strncmp(table_key, table[i].name, \
66 table[i].matchlen) == 0) { \
67 *value = table[i].value; \
68 return 0; /* success */ \
71 return 1; /* failure */ \
75 /* Convert fc_port_type values to ascii string name */
77 enum fc_port_type value
;
79 } fc_port_type_names
[] = {
80 { FC_PORTTYPE_UNKNOWN
, "Unknown" },
81 { FC_PORTTYPE_OTHER
, "Other" },
82 { FC_PORTTYPE_NOTPRESENT
, "Not Present" },
83 { FC_PORTTYPE_NPORT
, "NPort (fabric via point-to-point)" },
84 { FC_PORTTYPE_NLPORT
, "NLPort (fabric via loop)" },
85 { FC_PORTTYPE_LPORT
, "LPort (private loop)" },
86 { FC_PORTTYPE_PTP
, "Point-To-Point (direct nport connection" },
88 fc_enum_name_search(port_type
, fc_port_type
, fc_port_type_names
)
89 #define FC_PORTTYPE_MAX_NAMELEN 50
92 /* Convert fc_port_state values to ascii string name */
94 enum fc_port_state value
;
96 } fc_port_state_names
[] = {
97 { FC_PORTSTATE_UNKNOWN
, "Unknown" },
98 { FC_PORTSTATE_NOTPRESENT
, "Not Present" },
99 { FC_PORTSTATE_ONLINE
, "Online" },
100 { FC_PORTSTATE_OFFLINE
, "Offline" },
101 { FC_PORTSTATE_BLOCKED
, "Blocked" },
102 { FC_PORTSTATE_BYPASSED
, "Bypassed" },
103 { FC_PORTSTATE_DIAGNOSTICS
, "Diagnostics" },
104 { FC_PORTSTATE_LINKDOWN
, "Linkdown" },
105 { FC_PORTSTATE_ERROR
, "Error" },
106 { FC_PORTSTATE_LOOPBACK
, "Loopback" },
108 fc_enum_name_search(port_state
, fc_port_state
, fc_port_state_names
)
109 #define FC_PORTSTATE_MAX_NAMELEN 20
112 /* Convert fc_tgtid_binding_type values to ascii string name */
114 enum fc_tgtid_binding_type value
;
117 } fc_tgtid_binding_type_names
[] = {
118 { FC_TGTID_BIND_NONE
, "none", 4 },
119 { FC_TGTID_BIND_BY_WWPN
, "wwpn (World Wide Port Name)", 4 },
120 { FC_TGTID_BIND_BY_WWNN
, "wwnn (World Wide Node Name)", 4 },
121 { FC_TGTID_BIND_BY_ID
, "port_id (FC Address)", 7 },
123 fc_enum_name_search(tgtid_bind_type
, fc_tgtid_binding_type
,
124 fc_tgtid_binding_type_names
)
125 fc_enum_name_match(tgtid_bind_type
, fc_tgtid_binding_type
,
126 fc_tgtid_binding_type_names
)
127 #define FC_BINDTYPE_MAX_NAMELEN 30
130 #define fc_bitfield_name_search(title, table) \
132 get_fc_##title##_names(u32 table_key, char *buf) \
138 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
139 if (table[i].value & table_key) { \
140 len += sprintf(buf + len, "%s%s", \
141 prefix, table[i].name); \
145 len += sprintf(buf + len, "\n"); \
150 /* Convert FC_COS bit values to ascii string name */
155 { FC_COS_CLASS1
, "Class 1" },
156 { FC_COS_CLASS2
, "Class 2" },
157 { FC_COS_CLASS3
, "Class 3" },
158 { FC_COS_CLASS4
, "Class 4" },
159 { FC_COS_CLASS6
, "Class 6" },
161 fc_bitfield_name_search(cos
, fc_cos_names
)
164 /* Convert FC_PORTSPEED bit values to ascii string name */
168 } fc_port_speed_names
[] = {
169 { FC_PORTSPEED_1GBIT
, "1 Gbit" },
170 { FC_PORTSPEED_2GBIT
, "2 Gbit" },
171 { FC_PORTSPEED_4GBIT
, "4 Gbit" },
172 { FC_PORTSPEED_10GBIT
, "10 Gbit" },
173 { FC_PORTSPEED_NOT_NEGOTIATED
, "Not Negotiated" },
175 fc_bitfield_name_search(port_speed
, fc_port_speed_names
)
179 show_fc_fc4s (char *buf
, u8
*fc4_list
)
183 for (i
= 0; i
< FC_FC4_LIST_SIZE
; i
++, fc4_list
++)
184 len
+= sprintf(buf
+ len
, "0x%02x ", *fc4_list
);
185 len
+= sprintf(buf
+ len
, "\n");
190 /* Convert FC_RPORT_ROLE bit values to ascii string name */
194 } fc_remote_port_role_names
[] = {
195 { FC_RPORT_ROLE_FCP_TARGET
, "FCP Target" },
196 { FC_RPORT_ROLE_FCP_INITIATOR
, "FCP Initiator" },
197 { FC_RPORT_ROLE_IP_PORT
, "IP Port" },
199 fc_bitfield_name_search(remote_port_roles
, fc_remote_port_role_names
)
202 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
204 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
205 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
206 #define FC_FPORT_PORTID 0x00000e
207 #define FC_FABCTLR_PORTID 0x00000d
208 #define FC_DIRSRVR_PORTID 0x00000c
209 #define FC_TIMESRVR_PORTID 0x00000b
210 #define FC_MGMTSRVR_PORTID 0x00000a
213 static void fc_timeout_blocked_rport(void *data
);
214 static void fc_scsi_scan_rport(void *data
);
215 static void fc_rport_terminate(struct fc_rport
*rport
);
218 * Attribute counts pre object type...
219 * Increase these values if you add attributes
221 #define FC_STARGET_NUM_ATTRS 3
222 #define FC_RPORT_NUM_ATTRS 9
223 #define FC_HOST_NUM_ATTRS 15
226 struct scsi_transport_template t
;
227 struct fc_function_template
*f
;
230 * For attributes : each object has :
231 * An array of the actual attributes structures
232 * An array of null-terminated pointers to the attribute
233 * structures - used for mid-layer interaction.
235 * The attribute containers for the starget and host are are
236 * part of the midlayer. As the remote port is specific to the
237 * fc transport, we must provide the attribute container.
239 struct class_device_attribute private_starget_attrs
[
240 FC_STARGET_NUM_ATTRS
];
241 struct class_device_attribute
*starget_attrs
[FC_STARGET_NUM_ATTRS
+ 1];
243 struct class_device_attribute private_host_attrs
[FC_HOST_NUM_ATTRS
];
244 struct class_device_attribute
*host_attrs
[FC_HOST_NUM_ATTRS
+ 1];
246 struct transport_container rport_attr_cont
;
247 struct class_device_attribute private_rport_attrs
[FC_RPORT_NUM_ATTRS
];
248 struct class_device_attribute
*rport_attrs
[FC_RPORT_NUM_ATTRS
+ 1];
251 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
253 static int fc_target_setup(struct transport_container
*tc
, struct device
*dev
,
254 struct class_device
*cdev
)
256 struct scsi_target
*starget
= to_scsi_target(dev
);
257 struct fc_rport
*rport
= starget_to_rport(starget
);
260 * if parent is remote port, use values from remote port.
261 * Otherwise, this host uses the fc_transport, but not the
262 * remote port interface. As such, initialize to known non-values.
265 fc_starget_node_name(starget
) = rport
->node_name
;
266 fc_starget_port_name(starget
) = rport
->port_name
;
267 fc_starget_port_id(starget
) = rport
->port_id
;
269 fc_starget_node_name(starget
) = -1;
270 fc_starget_port_name(starget
) = -1;
271 fc_starget_port_id(starget
) = -1;
277 static DECLARE_TRANSPORT_CLASS(fc_transport_class
,
283 static int fc_host_setup(struct transport_container
*tc
, struct device
*dev
,
284 struct class_device
*cdev
)
286 struct Scsi_Host
*shost
= dev_to_shost(dev
);
289 * Set default values easily detected by the midlayer as
290 * failure cases. The scsi lldd is responsible for initializing
291 * all transport attributes to valid values per host.
293 fc_host_node_name(shost
) = -1;
294 fc_host_port_name(shost
) = -1;
295 fc_host_supported_classes(shost
) = FC_COS_UNSPECIFIED
;
296 memset(fc_host_supported_fc4s(shost
), 0,
297 sizeof(fc_host_supported_fc4s(shost
)));
298 memset(fc_host_symbolic_name(shost
), 0,
299 sizeof(fc_host_symbolic_name(shost
)));
300 fc_host_supported_speeds(shost
) = FC_PORTSPEED_UNKNOWN
;
301 fc_host_maxframe_size(shost
) = -1;
302 memset(fc_host_serial_number(shost
), 0,
303 sizeof(fc_host_serial_number(shost
)));
305 fc_host_port_id(shost
) = -1;
306 fc_host_port_type(shost
) = FC_PORTTYPE_UNKNOWN
;
307 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
308 memset(fc_host_active_fc4s(shost
), 0,
309 sizeof(fc_host_active_fc4s(shost
)));
310 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
311 fc_host_fabric_name(shost
) = -1;
313 fc_host_tgtid_bind_type(shost
) = FC_TGTID_BIND_BY_WWPN
;
315 INIT_LIST_HEAD(&fc_host_rports(shost
));
316 INIT_LIST_HEAD(&fc_host_rport_bindings(shost
));
317 fc_host_next_rport_number(shost
) = 0;
318 fc_host_next_target_id(shost
) = 0;
323 static DECLARE_TRANSPORT_CLASS(fc_host_class
,
330 * Setup and Remove actions for remote ports are handled
331 * in the service functions below.
333 static DECLARE_TRANSPORT_CLASS(fc_rport_class
,
344 * dev_loss_tmo: the default number of seconds that the FC transport
345 * should insulate the loss of a remote port.
346 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
348 static unsigned int fc_dev_loss_tmo
= SCSI_DEVICE_BLOCK_MAX_TIMEOUT
;
350 module_param_named(dev_loss_tmo
, fc_dev_loss_tmo
, int, S_IRUGO
|S_IWUSR
);
351 MODULE_PARM_DESC(dev_loss_tmo
,
352 "Maximum number of seconds that the FC transport should"
353 " insulate the loss of a remote port. Once this value is"
354 " exceeded, the scsi target is removed. Value should be"
355 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
358 static __init
int fc_transport_init(void)
360 int error
= transport_class_register(&fc_host_class
);
363 error
= transport_class_register(&fc_rport_class
);
366 return transport_class_register(&fc_transport_class
);
369 static void __exit
fc_transport_exit(void)
371 transport_class_unregister(&fc_transport_class
);
372 transport_class_unregister(&fc_rport_class
);
373 transport_class_unregister(&fc_host_class
);
377 * FC Remote Port Attribute Management
380 #define fc_rport_show_function(field, format_string, sz, cast) \
382 show_fc_rport_##field (struct class_device *cdev, char *buf) \
384 struct fc_rport *rport = transport_class_to_rport(cdev); \
385 struct Scsi_Host *shost = rport_to_shost(rport); \
386 struct fc_internal *i = to_fc_internal(shost->transportt); \
387 if (i->f->get_rport_##field) \
388 i->f->get_rport_##field(rport); \
389 return snprintf(buf, sz, format_string, cast rport->field); \
392 #define fc_rport_store_function(field) \
394 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
398 struct fc_rport *rport = transport_class_to_rport(cdev); \
399 struct Scsi_Host *shost = rport_to_shost(rport); \
400 struct fc_internal *i = to_fc_internal(shost->transportt); \
401 val = simple_strtoul(buf, NULL, 0); \
402 i->f->set_rport_##field(rport, val); \
406 #define fc_rport_rd_attr(field, format_string, sz) \
407 fc_rport_show_function(field, format_string, sz, ) \
408 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
409 show_fc_rport_##field, NULL)
411 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
412 fc_rport_show_function(field, format_string, sz, (cast)) \
413 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
414 show_fc_rport_##field, NULL)
416 #define fc_rport_rw_attr(field, format_string, sz) \
417 fc_rport_show_function(field, format_string, sz, ) \
418 fc_rport_store_function(field) \
419 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
420 show_fc_rport_##field, \
421 store_fc_rport_##field)
424 #define fc_private_rport_show_function(field, format_string, sz, cast) \
426 show_fc_rport_##field (struct class_device *cdev, char *buf) \
428 struct fc_rport *rport = transport_class_to_rport(cdev); \
429 return snprintf(buf, sz, format_string, cast rport->field); \
432 #define fc_private_rport_rd_attr(field, format_string, sz) \
433 fc_private_rport_show_function(field, format_string, sz, ) \
434 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
435 show_fc_rport_##field, NULL)
437 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
438 fc_private_rport_show_function(field, format_string, sz, (cast)) \
439 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
440 show_fc_rport_##field, NULL)
443 #define fc_private_rport_rd_enum_attr(title, maxlen) \
445 show_fc_rport_##title (struct class_device *cdev, char *buf) \
447 struct fc_rport *rport = transport_class_to_rport(cdev); \
449 name = get_fc_##title##_name(rport->title); \
452 return snprintf(buf, maxlen, "%s\n", name); \
454 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
455 show_fc_rport_##title, NULL)
458 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
459 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
460 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
461 i->private_rport_attrs[count].store = NULL; \
462 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
463 if (i->f->show_rport_##field) \
466 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
467 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
468 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
469 i->private_rport_attrs[count].store = NULL; \
470 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
473 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
474 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
475 if (!i->f->set_rport_##field) { \
476 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
477 i->private_rport_attrs[count].store = NULL; \
479 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
480 if (i->f->show_rport_##field) \
484 /* The FC Transport Remote Port Attributes: */
486 /* Fixed Remote Port Attributes */
488 fc_private_rport_rd_attr(maxframe_size
, "%u bytes\n", 20);
491 show_fc_rport_supported_classes (struct class_device
*cdev
, char *buf
)
493 struct fc_rport
*rport
= transport_class_to_rport(cdev
);
494 if (rport
->supported_classes
== FC_COS_UNSPECIFIED
)
495 return snprintf(buf
, 20, "unspecified\n");
496 return get_fc_cos_names(rport
->supported_classes
, buf
);
498 static FC_CLASS_DEVICE_ATTR(rport
, supported_classes
, S_IRUGO
,
499 show_fc_rport_supported_classes
, NULL
);
501 /* Dynamic Remote Port Attributes */
503 fc_rport_rw_attr(dev_loss_tmo
, "%d\n", 20);
506 /* Private Remote Port Attributes */
508 fc_private_rport_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
509 fc_private_rport_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
510 fc_private_rport_rd_attr(port_id
, "0x%06x\n", 20);
513 show_fc_rport_roles (struct class_device
*cdev
, char *buf
)
515 struct fc_rport
*rport
= transport_class_to_rport(cdev
);
517 /* identify any roles that are port_id specific */
518 if ((rport
->port_id
!= -1) &&
519 (rport
->port_id
& FC_WELLKNOWN_PORTID_MASK
) ==
520 FC_WELLKNOWN_PORTID_MASK
) {
521 switch (rport
->port_id
& FC_WELLKNOWN_ROLE_MASK
) {
522 case FC_FPORT_PORTID
:
523 return snprintf(buf
, 30, "Fabric Port\n");
524 case FC_FABCTLR_PORTID
:
525 return snprintf(buf
, 30, "Fabric Controller\n");
526 case FC_DIRSRVR_PORTID
:
527 return snprintf(buf
, 30, "Directory Server\n");
528 case FC_TIMESRVR_PORTID
:
529 return snprintf(buf
, 30, "Time Server\n");
530 case FC_MGMTSRVR_PORTID
:
531 return snprintf(buf
, 30, "Management Server\n");
533 return snprintf(buf
, 30, "Unknown Fabric Entity\n");
536 if (rport
->roles
== FC_RPORT_ROLE_UNKNOWN
)
537 return snprintf(buf
, 20, "unknown\n");
538 return get_fc_remote_port_roles_names(rport
->roles
, buf
);
541 static FC_CLASS_DEVICE_ATTR(rport
, roles
, S_IRUGO
,
542 show_fc_rport_roles
, NULL
);
544 fc_private_rport_rd_enum_attr(port_state
, FC_PORTSTATE_MAX_NAMELEN
);
545 fc_private_rport_rd_attr(scsi_target_id
, "%d\n", 20);
550 * FC SCSI Target Attribute Management
554 * Note: in the target show function we recognize when the remote
555 * port is in the heirarchy and do not allow the driver to get
556 * involved in sysfs functions. The driver only gets involved if
557 * it's the "old" style that doesn't use rports.
559 #define fc_starget_show_function(field, format_string, sz, cast) \
561 show_fc_starget_##field (struct class_device *cdev, char *buf) \
563 struct scsi_target *starget = transport_class_to_starget(cdev); \
564 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
565 struct fc_internal *i = to_fc_internal(shost->transportt); \
566 struct fc_rport *rport = starget_to_rport(starget); \
568 fc_starget_##field(starget) = rport->field; \
569 else if (i->f->get_starget_##field) \
570 i->f->get_starget_##field(starget); \
571 return snprintf(buf, sz, format_string, \
572 cast fc_starget_##field(starget)); \
575 #define fc_starget_rd_attr(field, format_string, sz) \
576 fc_starget_show_function(field, format_string, sz, ) \
577 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
578 show_fc_starget_##field, NULL)
580 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
581 fc_starget_show_function(field, format_string, sz, (cast)) \
582 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
583 show_fc_starget_##field, NULL)
585 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
586 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
587 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
588 i->private_starget_attrs[count].store = NULL; \
589 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
590 if (i->f->show_starget_##field) \
593 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
594 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
595 if (!i->f->set_starget_##field) { \
596 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
597 i->private_starget_attrs[count].store = NULL; \
599 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
600 if (i->f->show_starget_##field) \
603 /* The FC Transport SCSI Target Attributes: */
604 fc_starget_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
605 fc_starget_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
606 fc_starget_rd_attr(port_id
, "0x%06x\n", 20);
610 * Host Attribute Management
613 #define fc_host_show_function(field, format_string, sz, cast) \
615 show_fc_host_##field (struct class_device *cdev, char *buf) \
617 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
618 struct fc_internal *i = to_fc_internal(shost->transportt); \
619 if (i->f->get_host_##field) \
620 i->f->get_host_##field(shost); \
621 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
624 #define fc_host_store_function(field) \
626 store_fc_host_##field(struct class_device *cdev, const char *buf, \
630 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
631 struct fc_internal *i = to_fc_internal(shost->transportt); \
633 val = simple_strtoul(buf, NULL, 0); \
634 i->f->set_host_##field(shost, val); \
638 #define fc_host_rd_attr(field, format_string, sz) \
639 fc_host_show_function(field, format_string, sz, ) \
640 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
641 show_fc_host_##field, NULL)
643 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
644 fc_host_show_function(field, format_string, sz, (cast)) \
645 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
646 show_fc_host_##field, NULL)
648 #define fc_host_rw_attr(field, format_string, sz) \
649 fc_host_show_function(field, format_string, sz, ) \
650 fc_host_store_function(field) \
651 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
652 show_fc_host_##field, \
653 store_fc_host_##field)
655 #define fc_host_rd_enum_attr(title, maxlen) \
657 show_fc_host_##title (struct class_device *cdev, char *buf) \
659 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
660 struct fc_internal *i = to_fc_internal(shost->transportt); \
662 if (i->f->get_host_##title) \
663 i->f->get_host_##title(shost); \
664 name = get_fc_##title##_name(fc_host_##title(shost)); \
667 return snprintf(buf, maxlen, "%s\n", name); \
669 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
671 #define SETUP_HOST_ATTRIBUTE_RD(field) \
672 i->private_host_attrs[count] = class_device_attr_host_##field; \
673 i->private_host_attrs[count].attr.mode = S_IRUGO; \
674 i->private_host_attrs[count].store = NULL; \
675 i->host_attrs[count] = &i->private_host_attrs[count]; \
676 if (i->f->show_host_##field) \
679 #define SETUP_HOST_ATTRIBUTE_RW(field) \
680 i->private_host_attrs[count] = class_device_attr_host_##field; \
681 if (!i->f->set_host_##field) { \
682 i->private_host_attrs[count].attr.mode = S_IRUGO; \
683 i->private_host_attrs[count].store = NULL; \
685 i->host_attrs[count] = &i->private_host_attrs[count]; \
686 if (i->f->show_host_##field) \
690 #define fc_private_host_show_function(field, format_string, sz, cast) \
692 show_fc_host_##field (struct class_device *cdev, char *buf) \
694 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
695 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
698 #define fc_private_host_rd_attr(field, format_string, sz) \
699 fc_private_host_show_function(field, format_string, sz, ) \
700 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
701 show_fc_host_##field, NULL)
703 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
704 fc_private_host_show_function(field, format_string, sz, (cast)) \
705 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
706 show_fc_host_##field, NULL)
708 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
709 i->private_host_attrs[count] = class_device_attr_host_##field; \
710 i->private_host_attrs[count].attr.mode = S_IRUGO; \
711 i->private_host_attrs[count].store = NULL; \
712 i->host_attrs[count] = &i->private_host_attrs[count]; \
715 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
716 i->private_host_attrs[count] = class_device_attr_host_##field; \
717 i->host_attrs[count] = &i->private_host_attrs[count]; \
721 /* Fixed Host Attributes */
724 show_fc_host_supported_classes (struct class_device
*cdev
, char *buf
)
726 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
728 if (fc_host_supported_classes(shost
) == FC_COS_UNSPECIFIED
)
729 return snprintf(buf
, 20, "unspecified\n");
731 return get_fc_cos_names(fc_host_supported_classes(shost
), buf
);
733 static FC_CLASS_DEVICE_ATTR(host
, supported_classes
, S_IRUGO
,
734 show_fc_host_supported_classes
, NULL
);
737 show_fc_host_supported_fc4s (struct class_device
*cdev
, char *buf
)
739 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
740 return (ssize_t
)show_fc_fc4s(buf
, fc_host_supported_fc4s(shost
));
742 static FC_CLASS_DEVICE_ATTR(host
, supported_fc4s
, S_IRUGO
,
743 show_fc_host_supported_fc4s
, NULL
);
746 show_fc_host_supported_speeds (struct class_device
*cdev
, char *buf
)
748 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
750 if (fc_host_supported_speeds(shost
) == FC_PORTSPEED_UNKNOWN
)
751 return snprintf(buf
, 20, "unknown\n");
753 return get_fc_port_speed_names(fc_host_supported_speeds(shost
), buf
);
755 static FC_CLASS_DEVICE_ATTR(host
, supported_speeds
, S_IRUGO
,
756 show_fc_host_supported_speeds
, NULL
);
759 fc_private_host_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
760 fc_private_host_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
761 fc_private_host_rd_attr(symbolic_name
, "%s\n", (FC_SYMBOLIC_NAME_SIZE
+1));
762 fc_private_host_rd_attr(maxframe_size
, "%u bytes\n", 20);
763 fc_private_host_rd_attr(serial_number
, "%s\n", (FC_SERIAL_NUMBER_SIZE
+1));
766 /* Dynamic Host Attributes */
769 show_fc_host_active_fc4s (struct class_device
*cdev
, char *buf
)
771 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
772 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
774 if (i
->f
->get_host_active_fc4s
)
775 i
->f
->get_host_active_fc4s(shost
);
777 return (ssize_t
)show_fc_fc4s(buf
, fc_host_active_fc4s(shost
));
779 static FC_CLASS_DEVICE_ATTR(host
, active_fc4s
, S_IRUGO
,
780 show_fc_host_active_fc4s
, NULL
);
783 show_fc_host_speed (struct class_device
*cdev
, char *buf
)
785 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
786 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
788 if (i
->f
->get_host_speed
)
789 i
->f
->get_host_speed(shost
);
791 if (fc_host_speed(shost
) == FC_PORTSPEED_UNKNOWN
)
792 return snprintf(buf
, 20, "unknown\n");
794 return get_fc_port_speed_names(fc_host_speed(shost
), buf
);
796 static FC_CLASS_DEVICE_ATTR(host
, speed
, S_IRUGO
,
797 show_fc_host_speed
, NULL
);
800 fc_host_rd_attr(port_id
, "0x%06x\n", 20);
801 fc_host_rd_enum_attr(port_type
, FC_PORTTYPE_MAX_NAMELEN
);
802 fc_host_rd_enum_attr(port_state
, FC_PORTSTATE_MAX_NAMELEN
);
803 fc_host_rd_attr_cast(fabric_name
, "0x%llx\n", 20, unsigned long long);
806 /* Private Host Attributes */
809 show_fc_private_host_tgtid_bind_type(struct class_device
*cdev
, char *buf
)
811 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
814 name
= get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost
));
817 return snprintf(buf
, FC_BINDTYPE_MAX_NAMELEN
, "%s\n", name
);
820 #define get_list_head_entry(pos, head, member) \
821 pos = list_entry((head)->next, typeof(*pos), member)
824 store_fc_private_host_tgtid_bind_type(struct class_device
*cdev
,
825 const char *buf
, size_t count
)
827 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
828 struct fc_rport
*rport
;
829 enum fc_tgtid_binding_type val
;
832 if (get_fc_tgtid_bind_type_match(buf
, &val
))
835 /* if changing bind type, purge all unused consistent bindings */
836 if (val
!= fc_host_tgtid_bind_type(shost
)) {
837 spin_lock_irqsave(shost
->host_lock
, flags
);
838 while (!list_empty(&fc_host_rport_bindings(shost
))) {
839 get_list_head_entry(rport
,
840 &fc_host_rport_bindings(shost
), peers
);
841 spin_unlock_irqrestore(shost
->host_lock
, flags
);
842 fc_rport_terminate(rport
);
843 spin_lock_irqsave(shost
->host_lock
, flags
);
845 spin_unlock_irqrestore(shost
->host_lock
, flags
);
848 fc_host_tgtid_bind_type(shost
) = val
;
852 static FC_CLASS_DEVICE_ATTR(host
, tgtid_bind_type
, S_IRUGO
| S_IWUSR
,
853 show_fc_private_host_tgtid_bind_type
,
854 store_fc_private_host_tgtid_bind_type
);
857 * Host Statistics Management
860 /* Show a given an attribute in the statistics group */
862 fc_stat_show(const struct class_device
*cdev
, char *buf
, unsigned long offset
)
864 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
865 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
866 struct fc_host_statistics
*stats
;
867 ssize_t ret
= -ENOENT
;
869 if (offset
> sizeof(struct fc_host_statistics
) ||
870 offset
% sizeof(u64
) != 0)
873 if (i
->f
->get_fc_host_stats
) {
874 stats
= (i
->f
->get_fc_host_stats
)(shost
);
876 ret
= snprintf(buf
, 20, "0x%llx\n",
877 (unsigned long long)*(u64
*)(((u8
*) stats
) + offset
));
883 /* generate a read-only statistics attribute */
884 #define fc_host_statistic(name) \
885 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
887 return fc_stat_show(cd, buf, \
888 offsetof(struct fc_host_statistics, name)); \
890 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
892 fc_host_statistic(seconds_since_last_reset
);
893 fc_host_statistic(tx_frames
);
894 fc_host_statistic(tx_words
);
895 fc_host_statistic(rx_frames
);
896 fc_host_statistic(rx_words
);
897 fc_host_statistic(lip_count
);
898 fc_host_statistic(nos_count
);
899 fc_host_statistic(error_frames
);
900 fc_host_statistic(dumped_frames
);
901 fc_host_statistic(link_failure_count
);
902 fc_host_statistic(loss_of_sync_count
);
903 fc_host_statistic(loss_of_signal_count
);
904 fc_host_statistic(prim_seq_protocol_err_count
);
905 fc_host_statistic(invalid_tx_word_count
);
906 fc_host_statistic(invalid_crc_count
);
907 fc_host_statistic(fcp_input_requests
);
908 fc_host_statistic(fcp_output_requests
);
909 fc_host_statistic(fcp_control_requests
);
910 fc_host_statistic(fcp_input_megabytes
);
911 fc_host_statistic(fcp_output_megabytes
);
914 fc_reset_statistics(struct class_device
*cdev
, const char *buf
,
917 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
918 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
920 /* ignore any data value written to the attribute */
921 if (i
->f
->reset_fc_host_stats
) {
922 i
->f
->reset_fc_host_stats(shost
);
928 static FC_CLASS_DEVICE_ATTR(host
, reset_statistics
, S_IWUSR
, NULL
,
929 fc_reset_statistics
);
932 static struct attribute
*fc_statistics_attrs
[] = {
933 &class_device_attr_host_seconds_since_last_reset
.attr
,
934 &class_device_attr_host_tx_frames
.attr
,
935 &class_device_attr_host_tx_words
.attr
,
936 &class_device_attr_host_rx_frames
.attr
,
937 &class_device_attr_host_rx_words
.attr
,
938 &class_device_attr_host_lip_count
.attr
,
939 &class_device_attr_host_nos_count
.attr
,
940 &class_device_attr_host_error_frames
.attr
,
941 &class_device_attr_host_dumped_frames
.attr
,
942 &class_device_attr_host_link_failure_count
.attr
,
943 &class_device_attr_host_loss_of_sync_count
.attr
,
944 &class_device_attr_host_loss_of_signal_count
.attr
,
945 &class_device_attr_host_prim_seq_protocol_err_count
.attr
,
946 &class_device_attr_host_invalid_tx_word_count
.attr
,
947 &class_device_attr_host_invalid_crc_count
.attr
,
948 &class_device_attr_host_fcp_input_requests
.attr
,
949 &class_device_attr_host_fcp_output_requests
.attr
,
950 &class_device_attr_host_fcp_control_requests
.attr
,
951 &class_device_attr_host_fcp_input_megabytes
.attr
,
952 &class_device_attr_host_fcp_output_megabytes
.attr
,
953 &class_device_attr_host_reset_statistics
.attr
,
957 static struct attribute_group fc_statistics_group
= {
958 .name
= "statistics",
959 .attrs
= fc_statistics_attrs
,
962 static int fc_host_match(struct attribute_container
*cont
,
965 struct Scsi_Host
*shost
;
966 struct fc_internal
*i
;
968 if (!scsi_is_host_device(dev
))
971 shost
= dev_to_shost(dev
);
972 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
973 != &fc_host_class
.class)
976 i
= to_fc_internal(shost
->transportt
);
978 return &i
->t
.host_attrs
.ac
== cont
;
981 static int fc_target_match(struct attribute_container
*cont
,
984 struct Scsi_Host
*shost
;
985 struct fc_internal
*i
;
987 if (!scsi_is_target_device(dev
))
990 shost
= dev_to_shost(dev
->parent
);
991 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
992 != &fc_host_class
.class)
995 i
= to_fc_internal(shost
->transportt
);
997 return &i
->t
.target_attrs
.ac
== cont
;
1000 static void fc_rport_dev_release(struct device
*dev
)
1002 struct fc_rport
*rport
= dev_to_rport(dev
);
1003 put_device(dev
->parent
);
1007 int scsi_is_fc_rport(const struct device
*dev
)
1009 return dev
->release
== fc_rport_dev_release
;
1011 EXPORT_SYMBOL(scsi_is_fc_rport
);
1013 static int fc_rport_match(struct attribute_container
*cont
,
1016 struct Scsi_Host
*shost
;
1017 struct fc_internal
*i
;
1019 if (!scsi_is_fc_rport(dev
))
1022 shost
= dev_to_shost(dev
->parent
);
1023 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1024 != &fc_host_class
.class)
1027 i
= to_fc_internal(shost
->transportt
);
1029 return &i
->rport_attr_cont
.ac
== cont
;
1034 * Must be called with shost->host_lock held
1036 static struct device
*fc_target_parent(struct Scsi_Host
*shost
,
1037 int channel
, uint id
)
1039 struct fc_rport
*rport
;
1041 list_for_each_entry(rport
, &fc_host_rports(shost
), peers
)
1042 if ((rport
->channel
== channel
) &&
1043 (rport
->scsi_target_id
== id
))
1049 struct scsi_transport_template
*
1050 fc_attach_transport(struct fc_function_template
*ft
)
1052 struct fc_internal
*i
= kmalloc(sizeof(struct fc_internal
),
1059 memset(i
, 0, sizeof(struct fc_internal
));
1061 i
->t
.target_attrs
.ac
.attrs
= &i
->starget_attrs
[0];
1062 i
->t
.target_attrs
.ac
.class = &fc_transport_class
.class;
1063 i
->t
.target_attrs
.ac
.match
= fc_target_match
;
1064 i
->t
.target_size
= sizeof(struct fc_starget_attrs
);
1065 transport_container_register(&i
->t
.target_attrs
);
1067 i
->t
.host_attrs
.ac
.attrs
= &i
->host_attrs
[0];
1068 i
->t
.host_attrs
.ac
.class = &fc_host_class
.class;
1069 i
->t
.host_attrs
.ac
.match
= fc_host_match
;
1070 i
->t
.host_size
= sizeof(struct fc_host_attrs
);
1071 if (ft
->get_fc_host_stats
)
1072 i
->t
.host_attrs
.statistics
= &fc_statistics_group
;
1073 transport_container_register(&i
->t
.host_attrs
);
1075 i
->rport_attr_cont
.ac
.attrs
= &i
->rport_attrs
[0];
1076 i
->rport_attr_cont
.ac
.class = &fc_rport_class
.class;
1077 i
->rport_attr_cont
.ac
.match
= fc_rport_match
;
1078 transport_container_register(&i
->rport_attr_cont
);
1082 /* Transport uses the shost workq for scsi scanning */
1083 i
->t
.create_work_queue
= 1;
1085 i
->t
.target_parent
= fc_target_parent
;
1088 * Setup SCSI Target Attributes.
1091 SETUP_STARGET_ATTRIBUTE_RD(node_name
);
1092 SETUP_STARGET_ATTRIBUTE_RD(port_name
);
1093 SETUP_STARGET_ATTRIBUTE_RD(port_id
);
1095 BUG_ON(count
> FC_STARGET_NUM_ATTRS
);
1097 i
->starget_attrs
[count
] = NULL
;
1101 * Setup SCSI Host Attributes.
1104 SETUP_HOST_ATTRIBUTE_RD(node_name
);
1105 SETUP_HOST_ATTRIBUTE_RD(port_name
);
1106 SETUP_HOST_ATTRIBUTE_RD(supported_classes
);
1107 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s
);
1108 SETUP_HOST_ATTRIBUTE_RD(symbolic_name
);
1109 SETUP_HOST_ATTRIBUTE_RD(supported_speeds
);
1110 SETUP_HOST_ATTRIBUTE_RD(maxframe_size
);
1111 SETUP_HOST_ATTRIBUTE_RD(serial_number
);
1113 SETUP_HOST_ATTRIBUTE_RD(port_id
);
1114 SETUP_HOST_ATTRIBUTE_RD(port_type
);
1115 SETUP_HOST_ATTRIBUTE_RD(port_state
);
1116 SETUP_HOST_ATTRIBUTE_RD(active_fc4s
);
1117 SETUP_HOST_ATTRIBUTE_RD(speed
);
1118 SETUP_HOST_ATTRIBUTE_RD(fabric_name
);
1120 /* Transport-managed attributes */
1121 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type
);
1123 BUG_ON(count
> FC_HOST_NUM_ATTRS
);
1125 i
->host_attrs
[count
] = NULL
;
1128 * Setup Remote Port Attributes.
1131 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size
);
1132 SETUP_RPORT_ATTRIBUTE_RD(supported_classes
);
1133 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo
);
1134 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name
);
1135 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name
);
1136 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id
);
1137 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles
);
1138 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state
);
1139 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id
);
1141 BUG_ON(count
> FC_RPORT_NUM_ATTRS
);
1143 i
->rport_attrs
[count
] = NULL
;
1147 EXPORT_SYMBOL(fc_attach_transport
);
1149 void fc_release_transport(struct scsi_transport_template
*t
)
1151 struct fc_internal
*i
= to_fc_internal(t
);
1153 transport_container_unregister(&i
->t
.target_attrs
);
1154 transport_container_unregister(&i
->t
.host_attrs
);
1155 transport_container_unregister(&i
->rport_attr_cont
);
1159 EXPORT_SYMBOL(fc_release_transport
);
1163 * fc_remove_host - called to terminate any fc_transport-related elements
1165 * @rport: remote port to be unblocked.
1167 * This routine is expected to be called immediately preceeding the
1168 * a driver's call to scsi_remove_host().
1170 * WARNING: A driver utilizing the fc_transport, which fails to call
1171 * this routine prior to scsi_remote_host(), will leave dangling
1172 * objects in /sys/class/fc_remote_ports. Access to any of these
1173 * objects can result in a system crash !!!
1176 * This routine assumes no locks are held on entry.
1179 fc_remove_host(struct Scsi_Host
*shost
)
1181 struct fc_rport
*rport
, *next_rport
;
1183 /* Remove any remote ports */
1184 list_for_each_entry_safe(rport
, next_rport
,
1185 &fc_host_rports(shost
), peers
)
1186 fc_rport_terminate(rport
);
1187 list_for_each_entry_safe(rport
, next_rport
,
1188 &fc_host_rport_bindings(shost
), peers
)
1189 fc_rport_terminate(rport
);
1191 EXPORT_SYMBOL(fc_remove_host
);
1194 * fc_rport_create - allocates and creates a remote FC port.
1195 * @shost: scsi host the remote port is connected to.
1196 * @channel: Channel on shost port connected to.
1197 * @ids: The world wide names, fc address, and FC4 port
1198 * roles for the remote port.
1200 * Allocates and creates the remoter port structure, including the
1201 * class and sysfs creation.
1204 * This routine assumes no locks are held on entry.
1207 fc_rport_create(struct Scsi_Host
*shost
, int channel
,
1208 struct fc_rport_identifiers
*ids
)
1210 struct fc_host_attrs
*fc_host
=
1211 (struct fc_host_attrs
*)shost
->shost_data
;
1212 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
1213 struct fc_rport
*rport
;
1215 unsigned long flags
;
1219 size
= (sizeof(struct fc_rport
) + fci
->f
->dd_fcrport_size
);
1220 rport
= kmalloc(size
, GFP_KERNEL
);
1221 if (unlikely(!rport
)) {
1222 printk(KERN_ERR
"%s: allocation failure\n", __FUNCTION__
);
1225 memset(rport
, 0, size
);
1227 rport
->maxframe_size
= -1;
1228 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
1229 rport
->dev_loss_tmo
= fc_dev_loss_tmo
;
1230 memcpy(&rport
->node_name
, &ids
->node_name
, sizeof(rport
->node_name
));
1231 memcpy(&rport
->port_name
, &ids
->port_name
, sizeof(rport
->port_name
));
1232 rport
->port_id
= ids
->port_id
;
1233 rport
->roles
= ids
->roles
;
1234 rport
->port_state
= FC_PORTSTATE_ONLINE
;
1235 if (fci
->f
->dd_fcrport_size
)
1236 rport
->dd_data
= &rport
[1];
1237 rport
->channel
= channel
;
1239 INIT_WORK(&rport
->dev_loss_work
, fc_timeout_blocked_rport
, rport
);
1240 INIT_WORK(&rport
->scan_work
, fc_scsi_scan_rport
, rport
);
1242 spin_lock_irqsave(shost
->host_lock
, flags
);
1244 rport
->number
= fc_host
->next_rport_number
++;
1245 if (rport
->roles
& FC_RPORT_ROLE_FCP_TARGET
)
1246 rport
->scsi_target_id
= fc_host
->next_target_id
++;
1248 rport
->scsi_target_id
= -1;
1249 list_add_tail(&rport
->peers
, &fc_host_rports(shost
));
1250 get_device(&shost
->shost_gendev
);
1252 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1255 device_initialize(dev
);
1256 dev
->parent
= get_device(&shost
->shost_gendev
);
1257 dev
->release
= fc_rport_dev_release
;
1258 sprintf(dev
->bus_id
, "rport-%d:%d-%d",
1259 shost
->host_no
, channel
, rport
->number
);
1260 transport_setup_device(dev
);
1262 error
= device_add(dev
);
1264 printk(KERN_ERR
"FC Remote Port device_add failed\n");
1267 transport_add_device(dev
);
1268 transport_configure_device(dev
);
1270 if (rport
->roles
& FC_RPORT_ROLE_FCP_TARGET
)
1271 /* initiate a scan of the target */
1272 scsi_queue_work(shost
, &rport
->scan_work
);
1277 transport_destroy_device(dev
);
1278 put_device(dev
->parent
);
1279 spin_lock_irqsave(shost
->host_lock
, flags
);
1280 list_del(&rport
->peers
);
1281 put_device(&shost
->shost_gendev
);
1282 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1283 put_device(dev
->parent
);
1289 * fc_remote_port_add - notifies the fc transport of the existence
1290 * of a remote FC port.
1291 * @shost: scsi host the remote port is connected to.
1292 * @channel: Channel on shost port connected to.
1293 * @ids: The world wide names, fc address, and FC4 port
1294 * roles for the remote port.
1296 * The LLDD calls this routine to notify the transport of the existence
1297 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1298 * of the port, it's FC address (port_id), and the FC4 roles that are
1299 * active for the port.
1301 * For ports that are FCP targets (aka scsi targets), the FC transport
1302 * maintains consistent target id bindings on behalf of the LLDD.
1303 * A consistent target id binding is an assignment of a target id to
1304 * a remote port identifier, which persists while the scsi host is
1305 * attached. The remote port can disappear, then later reappear, and
1306 * it's target id assignment remains the same. This allows for shifts
1307 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1308 * changes to the scsi subsystem which is based on scsi host number and
1309 * target id values. Bindings are only valid during the attachment of
1310 * the scsi host. If the host detaches, then later re-attaches, target
1311 * id bindings may change.
1313 * This routine is responsible for returning a remote port structure.
1314 * The routine will search the list of remote ports it maintains
1315 * internally on behalf of consistent target id mappings. If found, the
1316 * remote port structure will be reused. Otherwise, a new remote port
1317 * structure will be allocated.
1319 * Whenever a remote port is allocated, a new fc_remote_port class
1320 * device is created.
1322 * Should not be called from interrupt context.
1325 * This routine assumes no locks are held on entry.
1328 fc_remote_port_add(struct Scsi_Host
*shost
, int channel
,
1329 struct fc_rport_identifiers
*ids
)
1331 struct fc_rport
*rport
;
1332 unsigned long flags
;
1335 if (likely((ids
->roles
& FC_RPORT_ROLE_FCP_TARGET
) &&
1336 (fc_host_tgtid_bind_type(shost
) != FC_TGTID_BIND_NONE
))) {
1338 /* search for a matching consistent binding */
1340 spin_lock_irqsave(shost
->host_lock
, flags
);
1342 list_for_each_entry(rport
, &fc_host_rport_bindings(shost
),
1344 if (rport
->channel
!= channel
)
1347 switch (fc_host_tgtid_bind_type(shost
)) {
1348 case FC_TGTID_BIND_BY_WWPN
:
1349 if (rport
->port_name
== ids
->port_name
)
1352 case FC_TGTID_BIND_BY_WWNN
:
1353 if (rport
->node_name
== ids
->node_name
)
1356 case FC_TGTID_BIND_BY_ID
:
1357 if (rport
->port_id
== ids
->port_id
)
1360 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
1365 list_move_tail(&rport
->peers
,
1366 &fc_host_rports(shost
));
1371 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1374 memcpy(&rport
->node_name
, &ids
->node_name
,
1375 sizeof(rport
->node_name
));
1376 memcpy(&rport
->port_name
, &ids
->port_name
,
1377 sizeof(rport
->port_name
));
1378 rport
->port_id
= ids
->port_id
;
1379 rport
->roles
= ids
->roles
;
1380 rport
->port_state
= FC_PORTSTATE_ONLINE
;
1382 if (rport
->roles
& FC_RPORT_ROLE_FCP_TARGET
)
1383 /* initiate a scan of the target */
1384 scsi_queue_work(shost
, &rport
->scan_work
);
1390 /* No consistent binding found - create new remote port entry */
1391 rport
= fc_rport_create(shost
, channel
, ids
);
1395 EXPORT_SYMBOL(fc_remote_port_add
);
1398 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1399 * @rport: The remote port to be operated on
1402 fc_rport_tgt_remove(struct fc_rport
*rport
)
1404 struct Scsi_Host
*shost
= rport_to_shost(rport
);
1406 scsi_target_unblock(&rport
->dev
);
1408 /* Stop anything on the workq */
1409 if (!cancel_delayed_work(&rport
->dev_loss_work
))
1410 flush_scheduled_work();
1411 scsi_flush_work(shost
);
1413 scsi_remove_target(&rport
->dev
);
1417 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1418 * @rport: The remote port to be terminated
1421 * This routine assumes no locks are held on entry.
1424 fc_rport_terminate(struct fc_rport
*rport
)
1426 struct Scsi_Host
*shost
= rport_to_shost(rport
);
1427 struct device
*dev
= &rport
->dev
;
1428 unsigned long flags
;
1430 fc_rport_tgt_remove(rport
);
1432 transport_remove_device(dev
);
1434 transport_destroy_device(dev
);
1435 spin_lock_irqsave(shost
->host_lock
, flags
);
1436 list_del(&rport
->peers
);
1437 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1438 put_device(&shost
->shost_gendev
);
1442 * fc_remote_port_delete - notifies the fc transport that a remote
1443 * port is no longer in existence.
1444 * @rport: The remote port that no longer exists
1446 * The LLDD calls this routine to notify the transport that a remote
1447 * port is no longer part of the topology. Note: Although a port
1448 * may no longer be part of the topology, it may persist in the remote
1449 * ports displayed by the fc_host. This is done so that target id
1450 * mappings (managed via the remote port structures), are always visible
1451 * as long as the mapping is valid, regardless of port state,
1453 * If the remote port is not an FCP Target, it will be fully torn down
1454 * and deallocated, including the fc_remote_port class device.
1456 * If the remote port is an FCP Target, the port structure will be
1457 * marked as Not Present, but will remain as long as there is a valid
1458 * SCSI target id mapping associated with the port structure. Validity
1459 * is determined by the binding type. If binding by wwpn, then the port
1460 * structure is always valid and will not be deallocated until the host
1461 * is removed. If binding by wwnn, then the port structure is valid
1462 * until another port with the same node name is found in the topology.
1463 * If binding by port id (fc address), then the port structure is valid
1464 * valid until another port with the same address is identified.
1466 * Called from interrupt or normal process context.
1469 * This routine assumes no locks are held on entry.
1472 fc_remote_port_delete(struct fc_rport
*rport
)
1474 struct Scsi_Host
*shost
= rport_to_shost(rport
);
1475 unsigned long flags
;
1477 /* If no scsi target id mapping or consistent binding type, delete it */
1478 if ((rport
->scsi_target_id
== -1) ||
1479 (fc_host_tgtid_bind_type(shost
) == FC_TGTID_BIND_NONE
)) {
1480 fc_rport_terminate(rport
);
1484 fc_rport_tgt_remove(rport
);
1486 spin_lock_irqsave(shost
->host_lock
, flags
);
1487 list_move_tail(&rport
->peers
, &fc_host_rport_bindings(shost
));
1488 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1491 * Note: We do not remove or clear the hostdata area. This allows
1492 * host-specific target data to persist along with the
1493 * scsi_target_id. It's up to the host to manage it's hostdata area.
1497 * Reinitialize port attributes that may change if the port comes back.
1499 rport
->maxframe_size
= -1;
1500 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
1501 rport
->roles
= FC_RPORT_ROLE_UNKNOWN
;
1502 rport
->port_state
= FC_PORTSTATE_NOTPRESENT
;
1504 /* remove the identifiers that aren't used in the consisting binding */
1505 switch (fc_host_tgtid_bind_type(shost
)) {
1506 case FC_TGTID_BIND_BY_WWPN
:
1507 rport
->node_name
= -1;
1508 rport
->port_id
= -1;
1510 case FC_TGTID_BIND_BY_WWNN
:
1511 rport
->port_name
= -1;
1512 rport
->port_id
= -1;
1514 case FC_TGTID_BIND_BY_ID
:
1515 rport
->node_name
= -1;
1516 rport
->port_name
= -1;
1518 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
1522 EXPORT_SYMBOL(fc_remote_port_delete
);
1525 * fc_remote_port_rolechg - notifies the fc transport that the roles
1526 * on a remote may have changed.
1527 * @rport: The remote port that changed.
1529 * The LLDD calls this routine to notify the transport that the roles
1530 * on a remote port may have changed. The largest effect of this is
1531 * if a port now becomes a FCP Target, it must be allocated a
1532 * scsi target id. If the port is no longer a FCP target, any
1533 * scsi target id value assigned to it will persist in case the
1534 * role changes back to include FCP Target. No changes in the scsi
1535 * midlayer will be invoked if the role changes (in the expectation
1536 * that the role will be resumed. If it doesn't normal error processing
1539 * Should not be called from interrupt context.
1542 * This routine assumes no locks are held on entry.
1545 fc_remote_port_rolechg(struct fc_rport
*rport
, u32 roles
)
1547 struct Scsi_Host
*shost
= rport_to_shost(rport
);
1548 struct fc_host_attrs
*fc_host
=
1549 (struct fc_host_attrs
*)shost
->shost_data
;
1550 unsigned long flags
;
1553 rport
->roles
= roles
;
1555 spin_lock_irqsave(shost
->host_lock
, flags
);
1556 if ((rport
->scsi_target_id
== -1) &&
1557 (rport
->roles
& FC_RPORT_ROLE_FCP_TARGET
)) {
1558 rport
->scsi_target_id
= fc_host
->next_target_id
++;
1561 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1564 /* initiate a scan of the target */
1565 scsi_queue_work(shost
, &rport
->scan_work
);
1567 EXPORT_SYMBOL(fc_remote_port_rolechg
);
1570 * fc_timeout_blocked_rport - Timeout handler for blocked remote port
1571 * that fails to return in the alloted time.
1572 * @data: scsi target that failed to reappear in the alloted time.
1575 fc_timeout_blocked_rport(void *data
)
1577 struct fc_rport
*rport
= (struct fc_rport
*)data
;
1579 rport
->port_state
= FC_PORTSTATE_OFFLINE
;
1581 dev_printk(KERN_ERR
, &rport
->dev
,
1582 "blocked FC remote port time out: removing target\n");
1585 * As this only occurs if the remote port (scsi target)
1586 * went away and didn't come back - we'll remove
1587 * all attached scsi devices.
1589 scsi_target_unblock(&rport
->dev
);
1590 scsi_remove_target(&rport
->dev
);
1594 * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
1595 * @rport: remote port to be blocked.
1597 * scsi lldd's with a FC transport call this routine to temporarily stop
1598 * all scsi traffic to a remote port. If the port is not a SCSI target,
1599 * no action is taken. If the port is a SCSI target, all attached devices
1600 * are placed into a SDEV_BLOCK state and a timer is started. The timer is
1601 * represents the maximum amount of time the port may be blocked. If the
1602 * timer expires, the port is considered non-existent and the attached
1603 * scsi devices will be removed.
1605 * Called from interrupt or normal process context.
1607 * Returns zero if successful or error if not
1610 * This routine assumes no locks are held on entry.
1612 * The timeout and timer types are extracted from the fc transport
1613 * attributes from the caller's rport pointer.
1616 fc_remote_port_block(struct fc_rport
*rport
)
1618 int timeout
= rport
->dev_loss_tmo
;
1619 struct work_struct
*work
= &rport
->dev_loss_work
;
1621 if (timeout
< 0 || timeout
> SCSI_DEVICE_BLOCK_MAX_TIMEOUT
)
1624 scsi_target_block(&rport
->dev
);
1626 /* cap the length the devices can be blocked */
1627 schedule_delayed_work(work
, timeout
* HZ
);
1629 rport
->port_state
= FC_PORTSTATE_BLOCKED
;
1632 EXPORT_SYMBOL(fc_remote_port_block
);
1635 * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
1636 * @rport: remote port to be unblocked.
1638 * scsi lld's with a FC transport call this routine to restart IO to all
1639 * devices associated with the caller's scsi target following a fc_target_block
1640 * request. Called from interrupt or normal process context.
1643 * This routine assumes no locks are held on entry.
1646 fc_remote_port_unblock(struct fc_rport
*rport
)
1648 struct work_struct
*work
= &rport
->dev_loss_work
;
1649 struct Scsi_Host
*shost
= rport_to_shost(rport
);
1652 * Stop the target timer first. Take no action on the del_timer
1653 * failure as the state machine state change will validate the
1656 if (!cancel_delayed_work(work
))
1657 flush_scheduled_work();
1659 if (rport
->port_state
== FC_PORTSTATE_OFFLINE
)
1661 * initiate a scan of the target as the target has
1664 scsi_queue_work(shost
, &rport
->scan_work
);
1666 scsi_target_unblock(&rport
->dev
);
1668 rport
->port_state
= FC_PORTSTATE_ONLINE
;
1670 EXPORT_SYMBOL(fc_remote_port_unblock
);
1673 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1674 * @data: remote port to be scanned.
1677 fc_scsi_scan_rport(void *data
)
1679 struct fc_rport
*rport
= (struct fc_rport
*)data
;
1681 scsi_scan_target(&rport
->dev
, rport
->channel
, rport
->scsi_target_id
,
1686 MODULE_AUTHOR("Martin Hicks");
1687 MODULE_DESCRIPTION("FC Transport Attributes");
1688 MODULE_LICENSE("GPL");
1690 module_init(fc_transport_init
);
1691 module_exit(fc_transport_exit
);