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/scsi_cmnd.h>
35 #include <linux/netlink.h>
36 #include <net/netlink.h>
37 #include <scsi/scsi_netlink_fc.h>
38 #include "scsi_priv.h"
40 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
43 * Redefine so that we can have same named attributes in the
44 * sdev/starget/host objects.
46 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
47 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
48 __ATTR(_name,_mode,_show,_store)
50 #define fc_enum_name_search(title, table_type, table) \
51 static const char *get_fc_##title##_name(enum table_type table_key) \
56 for (i = 0; i < ARRAY_SIZE(table); i++) { \
57 if (table[i].value == table_key) { \
58 name = table[i].name; \
65 #define fc_enum_name_match(title, table_type, table) \
66 static int get_fc_##title##_match(const char *table_key, \
67 enum table_type *value) \
71 for (i = 0; i < ARRAY_SIZE(table); i++) { \
72 if (strncmp(table_key, table[i].name, \
73 table[i].matchlen) == 0) { \
74 *value = table[i].value; \
75 return 0; /* success */ \
78 return 1; /* failure */ \
82 /* Convert fc_port_type values to ascii string name */
84 enum fc_port_type value;
86 } fc_port_type_names[] = {
87 { FC_PORTTYPE_UNKNOWN, "Unknown" },
88 { FC_PORTTYPE_OTHER, "Other" },
89 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
90 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
91 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
92 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
93 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
95 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
96 #define FC_PORTTYPE_MAX_NAMELEN 50
99 /* Convert fc_host_event_code values to ascii string name */
100 static const struct {
101 enum fc_host_event_code value;
103 } fc_host_event_code_names[] = {
104 { FCH_EVT_LIP, "lip" },
105 { FCH_EVT_LINKUP, "link_up" },
106 { FCH_EVT_LINKDOWN, "link_down" },
107 { FCH_EVT_LIPRESET, "lip_reset" },
108 { FCH_EVT_RSCN, "rscn" },
109 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
110 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
111 { FCH_EVT_PORT_ONLINE, "port_online" },
112 { FCH_EVT_PORT_OFFLINE, "port_offline" },
113 { FCH_EVT_PORT_FABRIC, "port_fabric" },
114 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
115 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
117 fc_enum_name_search(host_event_code, fc_host_event_code,
118 fc_host_event_code_names)
119 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
122 /* Convert fc_port_state values to ascii string name */
124 enum fc_port_state value;
126 } fc_port_state_names[] = {
127 { FC_PORTSTATE_UNKNOWN, "Unknown" },
128 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
129 { FC_PORTSTATE_ONLINE, "Online" },
130 { FC_PORTSTATE_OFFLINE, "Offline" },
131 { FC_PORTSTATE_BLOCKED, "Blocked" },
132 { FC_PORTSTATE_BYPASSED, "Bypassed" },
133 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
134 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
135 { FC_PORTSTATE_ERROR, "Error" },
136 { FC_PORTSTATE_LOOPBACK, "Loopback" },
137 { FC_PORTSTATE_DELETED, "Deleted" },
139 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
140 #define FC_PORTSTATE_MAX_NAMELEN 20
143 /* Convert fc_tgtid_binding_type values to ascii string name */
144 static const struct {
145 enum fc_tgtid_binding_type value;
148 } fc_tgtid_binding_type_names[] = {
149 { FC_TGTID_BIND_NONE, "none", 4 },
150 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
151 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
152 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
154 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
155 fc_tgtid_binding_type_names)
156 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
157 fc_tgtid_binding_type_names)
158 #define FC_BINDTYPE_MAX_NAMELEN 30
161 #define fc_bitfield_name_search(title, table) \
163 get_fc_##title##_names(u32 table_key, char *buf) \
169 for (i = 0; i < ARRAY_SIZE(table); i++) { \
170 if (table[i].value & table_key) { \
171 len += sprintf(buf + len, "%s%s", \
172 prefix, table[i].name); \
176 len += sprintf(buf + len, "\n"); \
181 /* Convert FC_COS bit values to ascii string name */
182 static const struct {
186 { FC_COS_CLASS1, "Class 1" },
187 { FC_COS_CLASS2, "Class 2" },
188 { FC_COS_CLASS3, "Class 3" },
189 { FC_COS_CLASS4, "Class 4" },
190 { FC_COS_CLASS6, "Class 6" },
192 fc_bitfield_name_search(cos, fc_cos_names)
195 /* Convert FC_PORTSPEED bit values to ascii string name */
196 static const struct {
199 } fc_port_speed_names[] = {
200 { FC_PORTSPEED_1GBIT, "1 Gbit" },
201 { FC_PORTSPEED_2GBIT, "2 Gbit" },
202 { FC_PORTSPEED_4GBIT, "4 Gbit" },
203 { FC_PORTSPEED_10GBIT, "10 Gbit" },
204 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
206 fc_bitfield_name_search(port_speed, fc_port_speed_names)
210 show_fc_fc4s (char *buf, u8 *fc4_list)
214 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
215 len += sprintf(buf + len , "0x%02x ", *fc4_list);
216 len += sprintf(buf + len, "\n");
221 /* Convert FC_RPORT_ROLE bit values to ascii string name */
222 static const struct {
225 } fc_remote_port_role_names[] = {
226 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
227 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
228 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
230 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
233 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
235 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
236 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
237 #define FC_FPORT_PORTID 0x00000e
238 #define FC_FABCTLR_PORTID 0x00000d
239 #define FC_DIRSRVR_PORTID 0x00000c
240 #define FC_TIMESRVR_PORTID 0x00000b
241 #define FC_MGMTSRVR_PORTID 0x00000a
244 static void fc_timeout_deleted_rport(void *data);
245 static void fc_timeout_fail_rport_io(void *data);
246 static void fc_scsi_scan_rport(void *data);
249 * Attribute counts pre object type...
250 * Increase these values if you add attributes
252 #define FC_STARGET_NUM_ATTRS 3
253 #define FC_RPORT_NUM_ATTRS 10
254 #define FC_HOST_NUM_ATTRS 17
257 struct scsi_transport_template t;
258 struct fc_function_template *f;
261 * For attributes : each object has :
262 * An array of the actual attributes structures
263 * An array of null-terminated pointers to the attribute
264 * structures - used for mid-layer interaction.
266 * The attribute containers for the starget and host are are
267 * part of the midlayer. As the remote port is specific to the
268 * fc transport, we must provide the attribute container.
270 struct class_device_attribute private_starget_attrs[
271 FC_STARGET_NUM_ATTRS];
272 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
274 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
275 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
277 struct transport_container rport_attr_cont;
278 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
279 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
282 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
284 static int fc_target_setup(struct transport_container *tc, struct device *dev,
285 struct class_device *cdev)
287 struct scsi_target *starget = to_scsi_target(dev);
288 struct fc_rport *rport = starget_to_rport(starget);
291 * if parent is remote port, use values from remote port.
292 * Otherwise, this host uses the fc_transport, but not the
293 * remote port interface. As such, initialize to known non-values.
296 fc_starget_node_name(starget) = rport->node_name;
297 fc_starget_port_name(starget) = rport->port_name;
298 fc_starget_port_id(starget) = rport->port_id;
300 fc_starget_node_name(starget) = -1;
301 fc_starget_port_name(starget) = -1;
302 fc_starget_port_id(starget) = -1;
308 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
314 static int fc_host_setup(struct transport_container *tc, struct device *dev,
315 struct class_device *cdev)
317 struct Scsi_Host *shost = dev_to_shost(dev);
318 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
321 * Set default values easily detected by the midlayer as
322 * failure cases. The scsi lldd is responsible for initializing
323 * all transport attributes to valid values per host.
325 fc_host->node_name = -1;
326 fc_host->port_name = -1;
327 fc_host->permanent_port_name = -1;
328 fc_host->supported_classes = FC_COS_UNSPECIFIED;
329 memset(fc_host->supported_fc4s, 0,
330 sizeof(fc_host->supported_fc4s));
331 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
332 fc_host->maxframe_size = -1;
333 memset(fc_host->serial_number, 0,
334 sizeof(fc_host->serial_number));
336 fc_host->port_id = -1;
337 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
338 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
339 memset(fc_host->active_fc4s, 0,
340 sizeof(fc_host->active_fc4s));
341 fc_host->speed = FC_PORTSPEED_UNKNOWN;
342 fc_host->fabric_name = -1;
343 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
344 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
346 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
348 INIT_LIST_HEAD(&fc_host->rports);
349 INIT_LIST_HEAD(&fc_host->rport_bindings);
350 fc_host->next_rport_number = 0;
351 fc_host->next_target_id = 0;
353 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
355 fc_host->work_q = create_singlethread_workqueue(
356 fc_host->work_q_name);
357 if (!fc_host->work_q)
360 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
362 fc_host->devloss_work_q = create_singlethread_workqueue(
363 fc_host->devloss_work_q_name);
364 if (!fc_host->devloss_work_q) {
365 destroy_workqueue(fc_host->work_q);
366 fc_host->work_q = NULL;
373 static DECLARE_TRANSPORT_CLASS(fc_host_class,
380 * Setup and Remove actions for remote ports are handled
381 * in the service functions below.
383 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
394 * dev_loss_tmo: the default number of seconds that the FC transport
395 * should insulate the loss of a remote port.
396 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
398 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
400 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
401 MODULE_PARM_DESC(dev_loss_tmo,
402 "Maximum number of seconds that the FC transport should"
403 " insulate the loss of a remote port. Once this value is"
404 " exceeded, the scsi target is removed. Value should be"
405 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
408 * Netlink Infrastructure
411 static atomic_t fc_event_seq;
414 * fc_get_event_number - Obtain the next sequential FC event number
417 * We could have inline'd this, but it would have required fc_event_seq to
418 * be exposed. For now, live with the subroutine call.
419 * Atomic used to avoid lock/unlock...
422 fc_get_event_number(void)
424 return atomic_add_return(1, &fc_event_seq);
426 EXPORT_SYMBOL(fc_get_event_number);
430 * fc_host_post_event - called to post an even on an fc_host.
432 * @shost: host the event occurred on
433 * @event_number: fc event number obtained from get_fc_event_number()
434 * @event_code: fc_host event being posted
435 * @event_data: 32bits of data for the event being posted
438 * This routine assumes no locks are held on entry.
441 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
442 enum fc_host_event_code event_code, u32 event_data)
445 struct nlmsghdr *nlh;
446 struct fc_nl_event *event;
456 len = FC_NL_MSGALIGN(sizeof(*event));
457 skblen = NLMSG_SPACE(len);
459 skb = alloc_skb(skblen, GFP_KERNEL);
465 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
466 skblen - sizeof(*nlh), 0);
471 event = NLMSG_DATA(nlh);
473 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
474 FC_NL_ASYNC_EVENT, len);
475 event->seconds = get_seconds();
476 event->vendor_id = 0;
477 event->host_no = shost->host_no;
478 event->event_datalen = sizeof(u32); /* bytes */
479 event->event_num = event_number;
480 event->event_code = event_code;
481 event->event_data = event_data;
483 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS);
484 if (err && (err != -ESRCH)) /* filter no recipient errors */
485 /* nlmsg_multicast already kfree_skb'd */
493 name = get_fc_host_event_code_name(event_code);
495 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
496 __FUNCTION__, shost->host_no,
497 (name) ? name : "<unknown>", event_data, err);
500 EXPORT_SYMBOL(fc_host_post_event);
504 * fc_host_post_vendor_event - called to post a vendor unique event on
507 * @shost: host the event occurred on
508 * @event_number: fc event number obtained from get_fc_event_number()
509 * @data_len: amount, in bytes, of vendor unique data
510 * @data_buf: pointer to vendor unique data
513 * This routine assumes no locks are held on entry.
516 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
517 u32 data_len, char * data_buf, u64 vendor_id)
520 struct nlmsghdr *nlh;
521 struct fc_nl_event *event;
527 goto send_vendor_fail;
530 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
531 skblen = NLMSG_SPACE(len);
533 skb = alloc_skb(skblen, GFP_KERNEL);
536 goto send_vendor_fail;
539 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
540 skblen - sizeof(*nlh), 0);
543 goto send_vendor_fail_skb;
545 event = NLMSG_DATA(nlh);
547 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
548 FC_NL_ASYNC_EVENT, len);
549 event->seconds = get_seconds();
550 event->vendor_id = vendor_id;
551 event->host_no = shost->host_no;
552 event->event_datalen = data_len; /* bytes */
553 event->event_num = event_number;
554 event->event_code = FCH_EVT_VENDOR_UNIQUE;
555 memcpy(&event->event_data, data_buf, data_len);
557 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS);
558 if (err && (err != -ESRCH)) /* filter no recipient errors */
559 /* nlmsg_multicast already kfree_skb'd */
560 goto send_vendor_fail;
564 send_vendor_fail_skb:
568 "%s: Dropped Event : host %d vendor_unique - err %d\n",
569 __FUNCTION__, shost->host_no, err);
572 EXPORT_SYMBOL(fc_host_post_vendor_event);
576 static __init int fc_transport_init(void)
580 atomic_set(&fc_event_seq, 0);
582 error = transport_class_register(&fc_host_class);
585 error = transport_class_register(&fc_rport_class);
588 return transport_class_register(&fc_transport_class);
591 static void __exit fc_transport_exit(void)
593 transport_class_unregister(&fc_transport_class);
594 transport_class_unregister(&fc_rport_class);
595 transport_class_unregister(&fc_host_class);
599 * FC Remote Port Attribute Management
602 #define fc_rport_show_function(field, format_string, sz, cast) \
604 show_fc_rport_##field (struct class_device *cdev, char *buf) \
606 struct fc_rport *rport = transport_class_to_rport(cdev); \
607 struct Scsi_Host *shost = rport_to_shost(rport); \
608 struct fc_internal *i = to_fc_internal(shost->transportt); \
609 if ((i->f->get_rport_##field) && \
610 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
611 (rport->port_state == FC_PORTSTATE_DELETED) || \
612 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
613 i->f->get_rport_##field(rport); \
614 return snprintf(buf, sz, format_string, cast rport->field); \
617 #define fc_rport_store_function(field) \
619 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
623 struct fc_rport *rport = transport_class_to_rport(cdev); \
624 struct Scsi_Host *shost = rport_to_shost(rport); \
625 struct fc_internal *i = to_fc_internal(shost->transportt); \
627 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
628 (rport->port_state == FC_PORTSTATE_DELETED) || \
629 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
631 val = simple_strtoul(buf, &cp, 0); \
632 if (*cp && (*cp != '\n')) \
634 i->f->set_rport_##field(rport, val); \
638 #define fc_rport_rd_attr(field, format_string, sz) \
639 fc_rport_show_function(field, format_string, sz, ) \
640 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
641 show_fc_rport_##field, NULL)
643 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
644 fc_rport_show_function(field, format_string, sz, (cast)) \
645 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
646 show_fc_rport_##field, NULL)
648 #define fc_rport_rw_attr(field, format_string, sz) \
649 fc_rport_show_function(field, format_string, sz, ) \
650 fc_rport_store_function(field) \
651 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
652 show_fc_rport_##field, \
653 store_fc_rport_##field)
656 #define fc_private_rport_show_function(field, format_string, sz, cast) \
658 show_fc_rport_##field (struct class_device *cdev, char *buf) \
660 struct fc_rport *rport = transport_class_to_rport(cdev); \
661 return snprintf(buf, sz, format_string, cast rport->field); \
664 #define fc_private_rport_rd_attr(field, format_string, sz) \
665 fc_private_rport_show_function(field, format_string, sz, ) \
666 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
667 show_fc_rport_##field, NULL)
669 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
670 fc_private_rport_show_function(field, format_string, sz, (cast)) \
671 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
672 show_fc_rport_##field, NULL)
675 #define fc_private_rport_rd_enum_attr(title, maxlen) \
677 show_fc_rport_##title (struct class_device *cdev, char *buf) \
679 struct fc_rport *rport = transport_class_to_rport(cdev); \
681 name = get_fc_##title##_name(rport->title); \
684 return snprintf(buf, maxlen, "%s\n", name); \
686 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
687 show_fc_rport_##title, NULL)
690 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
691 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
692 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
693 i->private_rport_attrs[count].store = NULL; \
694 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
695 if (i->f->show_rport_##field) \
698 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
699 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
700 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
701 i->private_rport_attrs[count].store = NULL; \
702 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
705 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
706 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
707 if (!i->f->set_rport_##field) { \
708 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
709 i->private_rport_attrs[count].store = NULL; \
711 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
712 if (i->f->show_rport_##field) \
715 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
717 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
718 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
723 /* The FC Transport Remote Port Attributes: */
725 /* Fixed Remote Port Attributes */
727 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
730 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
732 struct fc_rport *rport = transport_class_to_rport(cdev);
733 if (rport->supported_classes == FC_COS_UNSPECIFIED)
734 return snprintf(buf, 20, "unspecified\n");
735 return get_fc_cos_names(rport->supported_classes, buf);
737 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
738 show_fc_rport_supported_classes, NULL);
740 /* Dynamic Remote Port Attributes */
743 * dev_loss_tmo attribute
745 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
747 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
751 struct fc_rport *rport = transport_class_to_rport(cdev);
752 struct Scsi_Host *shost = rport_to_shost(rport);
753 struct fc_internal *i = to_fc_internal(shost->transportt);
755 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
756 (rport->port_state == FC_PORTSTATE_DELETED) ||
757 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
759 val = simple_strtoul(buf, &cp, 0);
760 if ((*cp && (*cp != '\n')) ||
761 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
763 i->f->set_rport_dev_loss_tmo(rport, val);
766 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
767 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
770 /* Private Remote Port Attributes */
772 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
773 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
774 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
777 show_fc_rport_roles (struct class_device *cdev, char *buf)
779 struct fc_rport *rport = transport_class_to_rport(cdev);
781 /* identify any roles that are port_id specific */
782 if ((rport->port_id != -1) &&
783 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
784 FC_WELLKNOWN_PORTID_MASK) {
785 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
786 case FC_FPORT_PORTID:
787 return snprintf(buf, 30, "Fabric Port\n");
788 case FC_FABCTLR_PORTID:
789 return snprintf(buf, 30, "Fabric Controller\n");
790 case FC_DIRSRVR_PORTID:
791 return snprintf(buf, 30, "Directory Server\n");
792 case FC_TIMESRVR_PORTID:
793 return snprintf(buf, 30, "Time Server\n");
794 case FC_MGMTSRVR_PORTID:
795 return snprintf(buf, 30, "Management Server\n");
797 return snprintf(buf, 30, "Unknown Fabric Entity\n");
800 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
801 return snprintf(buf, 20, "unknown\n");
802 return get_fc_remote_port_roles_names(rport->roles, buf);
805 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
806 show_fc_rport_roles, NULL);
808 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
809 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
812 * fast_io_fail_tmo attribute
815 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
817 struct fc_rport *rport = transport_class_to_rport(cdev);
819 if (rport->fast_io_fail_tmo == -1)
820 return snprintf(buf, 5, "off\n");
821 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
825 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
830 struct fc_rport *rport = transport_class_to_rport(cdev);
832 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
833 (rport->port_state == FC_PORTSTATE_DELETED) ||
834 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
836 if (strncmp(buf, "off", 3) == 0)
837 rport->fast_io_fail_tmo = -1;
839 val = simple_strtoul(buf, &cp, 0);
840 if ((*cp && (*cp != '\n')) ||
841 (val < 0) || (val >= rport->dev_loss_tmo))
843 rport->fast_io_fail_tmo = val;
847 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
848 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
852 * FC SCSI Target Attribute Management
856 * Note: in the target show function we recognize when the remote
857 * port is in the heirarchy and do not allow the driver to get
858 * involved in sysfs functions. The driver only gets involved if
859 * it's the "old" style that doesn't use rports.
861 #define fc_starget_show_function(field, format_string, sz, cast) \
863 show_fc_starget_##field (struct class_device *cdev, char *buf) \
865 struct scsi_target *starget = transport_class_to_starget(cdev); \
866 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
867 struct fc_internal *i = to_fc_internal(shost->transportt); \
868 struct fc_rport *rport = starget_to_rport(starget); \
870 fc_starget_##field(starget) = rport->field; \
871 else if (i->f->get_starget_##field) \
872 i->f->get_starget_##field(starget); \
873 return snprintf(buf, sz, format_string, \
874 cast fc_starget_##field(starget)); \
877 #define fc_starget_rd_attr(field, format_string, sz) \
878 fc_starget_show_function(field, format_string, sz, ) \
879 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
880 show_fc_starget_##field, NULL)
882 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
883 fc_starget_show_function(field, format_string, sz, (cast)) \
884 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
885 show_fc_starget_##field, NULL)
887 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
888 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
889 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
890 i->private_starget_attrs[count].store = NULL; \
891 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
892 if (i->f->show_starget_##field) \
895 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
896 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
897 if (!i->f->set_starget_##field) { \
898 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
899 i->private_starget_attrs[count].store = NULL; \
901 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
902 if (i->f->show_starget_##field) \
905 /* The FC Transport SCSI Target Attributes: */
906 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
907 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
908 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
912 * Host Attribute Management
915 #define fc_host_show_function(field, format_string, sz, cast) \
917 show_fc_host_##field (struct class_device *cdev, char *buf) \
919 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
920 struct fc_internal *i = to_fc_internal(shost->transportt); \
921 if (i->f->get_host_##field) \
922 i->f->get_host_##field(shost); \
923 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
926 #define fc_host_store_function(field) \
928 store_fc_host_##field(struct class_device *cdev, const char *buf, \
932 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
933 struct fc_internal *i = to_fc_internal(shost->transportt); \
936 val = simple_strtoul(buf, &cp, 0); \
937 if (*cp && (*cp != '\n')) \
939 i->f->set_host_##field(shost, val); \
943 #define fc_host_store_str_function(field, slen) \
945 store_fc_host_##field(struct class_device *cdev, const char *buf, \
948 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
949 struct fc_internal *i = to_fc_internal(shost->transportt); \
950 unsigned int cnt=count; \
952 /* count may include a LF at end of string */ \
953 if (buf[cnt-1] == '\n') \
955 if (cnt > ((slen) - 1)) \
957 memcpy(fc_host_##field(shost), buf, cnt); \
958 i->f->set_host_##field(shost); \
962 #define fc_host_rd_attr(field, format_string, sz) \
963 fc_host_show_function(field, format_string, sz, ) \
964 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
965 show_fc_host_##field, NULL)
967 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
968 fc_host_show_function(field, format_string, sz, (cast)) \
969 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
970 show_fc_host_##field, NULL)
972 #define fc_host_rw_attr(field, format_string, sz) \
973 fc_host_show_function(field, format_string, sz, ) \
974 fc_host_store_function(field) \
975 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
976 show_fc_host_##field, \
977 store_fc_host_##field)
979 #define fc_host_rd_enum_attr(title, maxlen) \
981 show_fc_host_##title (struct class_device *cdev, char *buf) \
983 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
984 struct fc_internal *i = to_fc_internal(shost->transportt); \
986 if (i->f->get_host_##title) \
987 i->f->get_host_##title(shost); \
988 name = get_fc_##title##_name(fc_host_##title(shost)); \
991 return snprintf(buf, maxlen, "%s\n", name); \
993 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
995 #define SETUP_HOST_ATTRIBUTE_RD(field) \
996 i->private_host_attrs[count] = class_device_attr_host_##field; \
997 i->private_host_attrs[count].attr.mode = S_IRUGO; \
998 i->private_host_attrs[count].store = NULL; \
999 i->host_attrs[count] = &i->private_host_attrs[count]; \
1000 if (i->f->show_host_##field) \
1003 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1004 i->private_host_attrs[count] = class_device_attr_host_##field; \
1005 if (!i->f->set_host_##field) { \
1006 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1007 i->private_host_attrs[count].store = NULL; \
1009 i->host_attrs[count] = &i->private_host_attrs[count]; \
1010 if (i->f->show_host_##field) \
1014 #define fc_private_host_show_function(field, format_string, sz, cast) \
1016 show_fc_host_##field (struct class_device *cdev, char *buf) \
1018 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1019 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1022 #define fc_private_host_rd_attr(field, format_string, sz) \
1023 fc_private_host_show_function(field, format_string, sz, ) \
1024 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1025 show_fc_host_##field, NULL)
1027 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1028 fc_private_host_show_function(field, format_string, sz, (cast)) \
1029 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1030 show_fc_host_##field, NULL)
1032 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1033 i->private_host_attrs[count] = class_device_attr_host_##field; \
1034 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1035 i->private_host_attrs[count].store = NULL; \
1036 i->host_attrs[count] = &i->private_host_attrs[count]; \
1039 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1041 i->private_host_attrs[count] = class_device_attr_host_##field; \
1042 i->host_attrs[count] = &i->private_host_attrs[count]; \
1047 /* Fixed Host Attributes */
1050 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1052 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1054 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1055 return snprintf(buf, 20, "unspecified\n");
1057 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1059 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1060 show_fc_host_supported_classes, NULL);
1063 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1065 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1066 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1068 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1069 show_fc_host_supported_fc4s, NULL);
1072 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1074 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1076 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1077 return snprintf(buf, 20, "unknown\n");
1079 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1081 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1082 show_fc_host_supported_speeds, NULL);
1085 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1086 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1087 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1088 unsigned long long);
1089 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1090 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1093 /* Dynamic Host Attributes */
1096 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1098 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1099 struct fc_internal *i = to_fc_internal(shost->transportt);
1101 if (i->f->get_host_active_fc4s)
1102 i->f->get_host_active_fc4s(shost);
1104 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1106 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1107 show_fc_host_active_fc4s, NULL);
1110 show_fc_host_speed (struct class_device *cdev, char *buf)
1112 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1113 struct fc_internal *i = to_fc_internal(shost->transportt);
1115 if (i->f->get_host_speed)
1116 i->f->get_host_speed(shost);
1118 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1119 return snprintf(buf, 20, "unknown\n");
1121 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1123 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1124 show_fc_host_speed, NULL);
1127 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1128 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1129 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1130 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1131 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1133 fc_private_host_show_function(system_hostname, "%s\n",
1134 FC_SYMBOLIC_NAME_SIZE + 1, )
1135 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1136 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1137 show_fc_host_system_hostname, store_fc_host_system_hostname);
1140 /* Private Host Attributes */
1143 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1145 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1148 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1151 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1154 #define get_list_head_entry(pos, head, member) \
1155 pos = list_entry((head)->next, typeof(*pos), member)
1158 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1159 const char *buf, size_t count)
1161 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1162 struct fc_rport *rport;
1163 enum fc_tgtid_binding_type val;
1164 unsigned long flags;
1166 if (get_fc_tgtid_bind_type_match(buf, &val))
1169 /* if changing bind type, purge all unused consistent bindings */
1170 if (val != fc_host_tgtid_bind_type(shost)) {
1171 spin_lock_irqsave(shost->host_lock, flags);
1172 while (!list_empty(&fc_host_rport_bindings(shost))) {
1173 get_list_head_entry(rport,
1174 &fc_host_rport_bindings(shost), peers);
1175 list_del(&rport->peers);
1176 rport->port_state = FC_PORTSTATE_DELETED;
1177 fc_queue_work(shost, &rport->rport_delete_work);
1179 spin_unlock_irqrestore(shost->host_lock, flags);
1182 fc_host_tgtid_bind_type(shost) = val;
1186 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1187 show_fc_private_host_tgtid_bind_type,
1188 store_fc_private_host_tgtid_bind_type);
1191 store_fc_private_host_issue_lip(struct class_device *cdev,
1192 const char *buf, size_t count)
1194 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1195 struct fc_internal *i = to_fc_internal(shost->transportt);
1198 /* ignore any data value written to the attribute */
1199 if (i->f->issue_fc_host_lip) {
1200 ret = i->f->issue_fc_host_lip(shost);
1201 return ret ? ret: count;
1207 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1208 store_fc_private_host_issue_lip);
1211 * Host Statistics Management
1214 /* Show a given an attribute in the statistics group */
1216 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1218 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1219 struct fc_internal *i = to_fc_internal(shost->transportt);
1220 struct fc_host_statistics *stats;
1221 ssize_t ret = -ENOENT;
1223 if (offset > sizeof(struct fc_host_statistics) ||
1224 offset % sizeof(u64) != 0)
1227 if (i->f->get_fc_host_stats) {
1228 stats = (i->f->get_fc_host_stats)(shost);
1230 ret = snprintf(buf, 20, "0x%llx\n",
1231 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1237 /* generate a read-only statistics attribute */
1238 #define fc_host_statistic(name) \
1239 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
1241 return fc_stat_show(cd, buf, \
1242 offsetof(struct fc_host_statistics, name)); \
1244 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1246 fc_host_statistic(seconds_since_last_reset);
1247 fc_host_statistic(tx_frames);
1248 fc_host_statistic(tx_words);
1249 fc_host_statistic(rx_frames);
1250 fc_host_statistic(rx_words);
1251 fc_host_statistic(lip_count);
1252 fc_host_statistic(nos_count);
1253 fc_host_statistic(error_frames);
1254 fc_host_statistic(dumped_frames);
1255 fc_host_statistic(link_failure_count);
1256 fc_host_statistic(loss_of_sync_count);
1257 fc_host_statistic(loss_of_signal_count);
1258 fc_host_statistic(prim_seq_protocol_err_count);
1259 fc_host_statistic(invalid_tx_word_count);
1260 fc_host_statistic(invalid_crc_count);
1261 fc_host_statistic(fcp_input_requests);
1262 fc_host_statistic(fcp_output_requests);
1263 fc_host_statistic(fcp_control_requests);
1264 fc_host_statistic(fcp_input_megabytes);
1265 fc_host_statistic(fcp_output_megabytes);
1268 fc_reset_statistics(struct class_device *cdev, const char *buf,
1271 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1272 struct fc_internal *i = to_fc_internal(shost->transportt);
1274 /* ignore any data value written to the attribute */
1275 if (i->f->reset_fc_host_stats) {
1276 i->f->reset_fc_host_stats(shost);
1282 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1283 fc_reset_statistics);
1286 static struct attribute *fc_statistics_attrs[] = {
1287 &class_device_attr_host_seconds_since_last_reset.attr,
1288 &class_device_attr_host_tx_frames.attr,
1289 &class_device_attr_host_tx_words.attr,
1290 &class_device_attr_host_rx_frames.attr,
1291 &class_device_attr_host_rx_words.attr,
1292 &class_device_attr_host_lip_count.attr,
1293 &class_device_attr_host_nos_count.attr,
1294 &class_device_attr_host_error_frames.attr,
1295 &class_device_attr_host_dumped_frames.attr,
1296 &class_device_attr_host_link_failure_count.attr,
1297 &class_device_attr_host_loss_of_sync_count.attr,
1298 &class_device_attr_host_loss_of_signal_count.attr,
1299 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1300 &class_device_attr_host_invalid_tx_word_count.attr,
1301 &class_device_attr_host_invalid_crc_count.attr,
1302 &class_device_attr_host_fcp_input_requests.attr,
1303 &class_device_attr_host_fcp_output_requests.attr,
1304 &class_device_attr_host_fcp_control_requests.attr,
1305 &class_device_attr_host_fcp_input_megabytes.attr,
1306 &class_device_attr_host_fcp_output_megabytes.attr,
1307 &class_device_attr_host_reset_statistics.attr,
1311 static struct attribute_group fc_statistics_group = {
1312 .name = "statistics",
1313 .attrs = fc_statistics_attrs,
1316 static int fc_host_match(struct attribute_container *cont,
1319 struct Scsi_Host *shost;
1320 struct fc_internal *i;
1322 if (!scsi_is_host_device(dev))
1325 shost = dev_to_shost(dev);
1326 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1327 != &fc_host_class.class)
1330 i = to_fc_internal(shost->transportt);
1332 return &i->t.host_attrs.ac == cont;
1335 static int fc_target_match(struct attribute_container *cont,
1338 struct Scsi_Host *shost;
1339 struct fc_internal *i;
1341 if (!scsi_is_target_device(dev))
1344 shost = dev_to_shost(dev->parent);
1345 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1346 != &fc_host_class.class)
1349 i = to_fc_internal(shost->transportt);
1351 return &i->t.target_attrs.ac == cont;
1354 static void fc_rport_dev_release(struct device *dev)
1356 struct fc_rport *rport = dev_to_rport(dev);
1357 put_device(dev->parent);
1361 int scsi_is_fc_rport(const struct device *dev)
1363 return dev->release == fc_rport_dev_release;
1365 EXPORT_SYMBOL(scsi_is_fc_rport);
1367 static int fc_rport_match(struct attribute_container *cont,
1370 struct Scsi_Host *shost;
1371 struct fc_internal *i;
1373 if (!scsi_is_fc_rport(dev))
1376 shost = dev_to_shost(dev->parent);
1377 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1378 != &fc_host_class.class)
1381 i = to_fc_internal(shost->transportt);
1383 return &i->rport_attr_cont.ac == cont;
1388 * fc_timed_out - FC Transport I/O timeout intercept handler
1390 * @scmd: The SCSI command which timed out
1392 * This routine protects against error handlers getting invoked while a
1393 * rport is in a blocked state, typically due to a temporarily loss of
1394 * connectivity. If the error handlers are allowed to proceed, requests
1395 * to abort i/o, reset the target, etc will likely fail as there is no way
1396 * to communicate with the device to perform the requested function. These
1397 * failures may result in the midlayer taking the device offline, requiring
1398 * manual intervention to restore operation.
1400 * This routine, called whenever an i/o times out, validates the state of
1401 * the underlying rport. If the rport is blocked, it returns
1402 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1403 * Eventually, either the device will return, or devloss_tmo will fire,
1404 * and when the timeout then fires, it will be handled normally.
1405 * If the rport is not blocked, normal error handling continues.
1408 * This routine assumes no locks are held on entry.
1410 static enum scsi_eh_timer_return
1411 fc_timed_out(struct scsi_cmnd *scmd)
1413 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1415 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1416 return EH_RESET_TIMER;
1418 return EH_NOT_HANDLED;
1422 * Must be called with shost->host_lock held
1424 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1427 struct fc_rport *rport;
1429 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1430 if (rport->scsi_target_id == -1)
1433 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1434 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1435 scsi_scan_target(&rport->dev, rport->channel,
1436 rport->scsi_target_id, lun, 1);
1443 struct scsi_transport_template *
1444 fc_attach_transport(struct fc_function_template *ft)
1447 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1453 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1454 i->t.target_attrs.ac.class = &fc_transport_class.class;
1455 i->t.target_attrs.ac.match = fc_target_match;
1456 i->t.target_size = sizeof(struct fc_starget_attrs);
1457 transport_container_register(&i->t.target_attrs);
1459 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1460 i->t.host_attrs.ac.class = &fc_host_class.class;
1461 i->t.host_attrs.ac.match = fc_host_match;
1462 i->t.host_size = sizeof(struct fc_host_attrs);
1463 if (ft->get_fc_host_stats)
1464 i->t.host_attrs.statistics = &fc_statistics_group;
1465 transport_container_register(&i->t.host_attrs);
1467 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1468 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1469 i->rport_attr_cont.ac.match = fc_rport_match;
1470 transport_container_register(&i->rport_attr_cont);
1474 /* Transport uses the shost workq for scsi scanning */
1475 i->t.create_work_queue = 1;
1477 i->t.eh_timed_out = fc_timed_out;
1479 i->t.user_scan = fc_user_scan;
1482 * Setup SCSI Target Attributes.
1485 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1486 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1487 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1489 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1491 i->starget_attrs[count] = NULL;
1495 * Setup SCSI Host Attributes.
1498 SETUP_HOST_ATTRIBUTE_RD(node_name);
1499 SETUP_HOST_ATTRIBUTE_RD(port_name);
1500 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1501 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1502 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1503 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1504 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1505 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1507 SETUP_HOST_ATTRIBUTE_RD(port_id);
1508 SETUP_HOST_ATTRIBUTE_RD(port_type);
1509 SETUP_HOST_ATTRIBUTE_RD(port_state);
1510 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1511 SETUP_HOST_ATTRIBUTE_RD(speed);
1512 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1513 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1514 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1516 /* Transport-managed attributes */
1517 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1518 if (ft->issue_fc_host_lip)
1519 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1521 BUG_ON(count > FC_HOST_NUM_ATTRS);
1523 i->host_attrs[count] = NULL;
1526 * Setup Remote Port Attributes.
1529 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1530 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1531 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1532 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1533 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1534 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1535 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1536 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1537 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1538 if (ft->terminate_rport_io)
1539 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1541 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1543 i->rport_attrs[count] = NULL;
1547 EXPORT_SYMBOL(fc_attach_transport);
1549 void fc_release_transport(struct scsi_transport_template *t)
1551 struct fc_internal *i = to_fc_internal(t);
1553 transport_container_unregister(&i->t.target_attrs);
1554 transport_container_unregister(&i->t.host_attrs);
1555 transport_container_unregister(&i->rport_attr_cont);
1559 EXPORT_SYMBOL(fc_release_transport);
1562 * fc_queue_work - Queue work to the fc_host workqueue.
1563 * @shost: Pointer to Scsi_Host bound to fc_host.
1564 * @work: Work to queue for execution.
1567 * 1 - work queued for execution
1568 * 0 - work is already queued
1569 * -EINVAL - work queue doesn't exist
1572 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1574 if (unlikely(!fc_host_work_q(shost))) {
1576 "ERROR: FC host '%s' attempted to queue work, "
1577 "when no workqueue created.\n", shost->hostt->name);
1583 return queue_work(fc_host_work_q(shost), work);
1587 * fc_flush_work - Flush a fc_host's workqueue.
1588 * @shost: Pointer to Scsi_Host bound to fc_host.
1591 fc_flush_work(struct Scsi_Host *shost)
1593 if (!fc_host_work_q(shost)) {
1595 "ERROR: FC host '%s' attempted to flush work, "
1596 "when no workqueue created.\n", shost->hostt->name);
1601 flush_workqueue(fc_host_work_q(shost));
1605 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1606 * @shost: Pointer to Scsi_Host bound to fc_host.
1607 * @work: Work to queue for execution.
1608 * @delay: jiffies to delay the work queuing
1611 * 1 on success / 0 already queued / < 0 for error
1614 fc_queue_devloss_work(struct Scsi_Host *shost, struct work_struct *work,
1615 unsigned long delay)
1617 if (unlikely(!fc_host_devloss_work_q(shost))) {
1619 "ERROR: FC host '%s' attempted to queue work, "
1620 "when no workqueue created.\n", shost->hostt->name);
1627 return queue_work(fc_host_devloss_work_q(shost), work);
1629 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
1633 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
1634 * @shost: Pointer to Scsi_Host bound to fc_host.
1637 fc_flush_devloss(struct Scsi_Host *shost)
1639 if (!fc_host_devloss_work_q(shost)) {
1641 "ERROR: FC host '%s' attempted to flush work, "
1642 "when no workqueue created.\n", shost->hostt->name);
1647 flush_workqueue(fc_host_devloss_work_q(shost));
1652 * fc_remove_host - called to terminate any fc_transport-related elements
1654 * @rport: remote port to be unblocked.
1656 * This routine is expected to be called immediately preceeding the
1657 * a driver's call to scsi_remove_host().
1659 * WARNING: A driver utilizing the fc_transport, which fails to call
1660 * this routine prior to scsi_remote_host(), will leave dangling
1661 * objects in /sys/class/fc_remote_ports. Access to any of these
1662 * objects can result in a system crash !!!
1665 * This routine assumes no locks are held on entry.
1668 fc_remove_host(struct Scsi_Host *shost)
1670 struct fc_rport *rport, *next_rport;
1671 struct workqueue_struct *work_q;
1672 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1674 /* Remove any remote ports */
1675 list_for_each_entry_safe(rport, next_rport,
1676 &fc_host->rports, peers) {
1677 list_del(&rport->peers);
1678 rport->port_state = FC_PORTSTATE_DELETED;
1679 fc_queue_work(shost, &rport->rport_delete_work);
1682 list_for_each_entry_safe(rport, next_rport,
1683 &fc_host->rport_bindings, peers) {
1684 list_del(&rport->peers);
1685 rport->port_state = FC_PORTSTATE_DELETED;
1686 fc_queue_work(shost, &rport->rport_delete_work);
1689 /* flush all scan work items */
1690 scsi_flush_work(shost);
1692 /* flush all stgt delete, and rport delete work items, then kill it */
1693 if (fc_host->work_q) {
1694 work_q = fc_host->work_q;
1695 fc_host->work_q = NULL;
1696 destroy_workqueue(work_q);
1699 /* flush all devloss work items, then kill it */
1700 if (fc_host->devloss_work_q) {
1701 work_q = fc_host->devloss_work_q;
1702 fc_host->devloss_work_q = NULL;
1703 destroy_workqueue(work_q);
1706 EXPORT_SYMBOL(fc_remove_host);
1710 * fc_starget_delete - called to delete the scsi decendents of an rport
1711 * (target and all sdevs)
1713 * @data: remote port to be operated on.
1716 fc_starget_delete(void *data)
1718 struct fc_rport *rport = (struct fc_rport *)data;
1719 struct Scsi_Host *shost = rport_to_shost(rport);
1720 unsigned long flags;
1721 struct fc_internal *i = to_fc_internal(shost->transportt);
1724 * Involve the LLDD if possible. All io on the rport is to
1725 * be terminated, either as part of the dev_loss_tmo callback
1726 * processing, or via the terminate_rport_io function.
1728 if (i->f->dev_loss_tmo_callbk)
1729 i->f->dev_loss_tmo_callbk(rport);
1730 else if (i->f->terminate_rport_io)
1731 i->f->terminate_rport_io(rport);
1733 spin_lock_irqsave(shost->host_lock, flags);
1734 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
1735 spin_unlock_irqrestore(shost->host_lock, flags);
1736 if (!cancel_delayed_work(&rport->fail_io_work))
1737 fc_flush_devloss(shost);
1738 if (!cancel_delayed_work(&rport->dev_loss_work))
1739 fc_flush_devloss(shost);
1740 spin_lock_irqsave(shost->host_lock, flags);
1741 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1743 spin_unlock_irqrestore(shost->host_lock, flags);
1745 scsi_remove_target(&rport->dev);
1750 * fc_rport_final_delete - finish rport termination and delete it.
1752 * @data: remote port to be deleted.
1755 fc_rport_final_delete(void *data)
1757 struct fc_rport *rport = (struct fc_rport *)data;
1758 struct device *dev = &rport->dev;
1759 struct Scsi_Host *shost = rport_to_shost(rport);
1760 struct fc_internal *i = to_fc_internal(shost->transportt);
1763 * if a scan is pending, flush the SCSI Host work_q so that
1764 * that we can reclaim the rport scan work element.
1766 if (rport->flags & FC_RPORT_SCAN_PENDING)
1767 scsi_flush_work(shost);
1769 /* Delete SCSI target and sdevs */
1770 if (rport->scsi_target_id != -1)
1771 fc_starget_delete(data);
1772 else if (i->f->dev_loss_tmo_callbk)
1773 i->f->dev_loss_tmo_callbk(rport);
1774 else if (i->f->terminate_rport_io)
1775 i->f->terminate_rport_io(rport);
1777 transport_remove_device(dev);
1779 transport_destroy_device(dev);
1780 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1781 put_device(dev); /* for self-reference */
1786 * fc_rport_create - allocates and creates a remote FC port.
1787 * @shost: scsi host the remote port is connected to.
1788 * @channel: Channel on shost port connected to.
1789 * @ids: The world wide names, fc address, and FC4 port
1790 * roles for the remote port.
1792 * Allocates and creates the remoter port structure, including the
1793 * class and sysfs creation.
1796 * This routine assumes no locks are held on entry.
1799 fc_rport_create(struct Scsi_Host *shost, int channel,
1800 struct fc_rport_identifiers *ids)
1802 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1803 struct fc_internal *fci = to_fc_internal(shost->transportt);
1804 struct fc_rport *rport;
1806 unsigned long flags;
1810 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1811 rport = kzalloc(size, GFP_KERNEL);
1812 if (unlikely(!rport)) {
1813 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1817 rport->maxframe_size = -1;
1818 rport->supported_classes = FC_COS_UNSPECIFIED;
1819 rport->dev_loss_tmo = fc_dev_loss_tmo;
1820 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1821 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1822 rport->port_id = ids->port_id;
1823 rport->roles = ids->roles;
1824 rport->port_state = FC_PORTSTATE_ONLINE;
1825 if (fci->f->dd_fcrport_size)
1826 rport->dd_data = &rport[1];
1827 rport->channel = channel;
1828 rport->fast_io_fail_tmo = -1;
1830 INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1831 INIT_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io, rport);
1832 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1833 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete, rport);
1834 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete, rport);
1836 spin_lock_irqsave(shost->host_lock, flags);
1838 rport->number = fc_host->next_rport_number++;
1839 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1840 rport->scsi_target_id = fc_host->next_target_id++;
1842 rport->scsi_target_id = -1;
1843 list_add_tail(&rport->peers, &fc_host->rports);
1844 get_device(&shost->shost_gendev); /* for fc_host->rport list */
1846 spin_unlock_irqrestore(shost->host_lock, flags);
1849 device_initialize(dev); /* takes self reference */
1850 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1851 dev->release = fc_rport_dev_release;
1852 sprintf(dev->bus_id, "rport-%d:%d-%d",
1853 shost->host_no, channel, rport->number);
1854 transport_setup_device(dev);
1856 error = device_add(dev);
1858 printk(KERN_ERR "FC Remote Port device_add failed\n");
1861 transport_add_device(dev);
1862 transport_configure_device(dev);
1864 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1865 /* initiate a scan of the target */
1866 rport->flags |= FC_RPORT_SCAN_PENDING;
1867 scsi_queue_work(shost, &rport->scan_work);
1873 transport_destroy_device(dev);
1874 spin_lock_irqsave(shost->host_lock, flags);
1875 list_del(&rport->peers);
1876 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1877 spin_unlock_irqrestore(shost->host_lock, flags);
1878 put_device(dev->parent);
1884 * fc_remote_port_add - notifies the fc transport of the existence
1885 * of a remote FC port.
1886 * @shost: scsi host the remote port is connected to.
1887 * @channel: Channel on shost port connected to.
1888 * @ids: The world wide names, fc address, and FC4 port
1889 * roles for the remote port.
1891 * The LLDD calls this routine to notify the transport of the existence
1892 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1893 * of the port, it's FC address (port_id), and the FC4 roles that are
1894 * active for the port.
1896 * For ports that are FCP targets (aka scsi targets), the FC transport
1897 * maintains consistent target id bindings on behalf of the LLDD.
1898 * A consistent target id binding is an assignment of a target id to
1899 * a remote port identifier, which persists while the scsi host is
1900 * attached. The remote port can disappear, then later reappear, and
1901 * it's target id assignment remains the same. This allows for shifts
1902 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1903 * changes to the scsi subsystem which is based on scsi host number and
1904 * target id values. Bindings are only valid during the attachment of
1905 * the scsi host. If the host detaches, then later re-attaches, target
1906 * id bindings may change.
1908 * This routine is responsible for returning a remote port structure.
1909 * The routine will search the list of remote ports it maintains
1910 * internally on behalf of consistent target id mappings. If found, the
1911 * remote port structure will be reused. Otherwise, a new remote port
1912 * structure will be allocated.
1914 * Whenever a remote port is allocated, a new fc_remote_port class
1915 * device is created.
1917 * Should not be called from interrupt context.
1920 * This routine assumes no locks are held on entry.
1923 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1924 struct fc_rport_identifiers *ids)
1926 struct fc_internal *fci = to_fc_internal(shost->transportt);
1927 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1928 struct fc_rport *rport;
1929 unsigned long flags;
1932 /* ensure any stgt delete functions are done */
1933 fc_flush_work(shost);
1936 * Search the list of "active" rports, for an rport that has been
1937 * deleted, but we've held off the real delete while the target
1938 * is in a "blocked" state.
1940 spin_lock_irqsave(shost->host_lock, flags);
1942 list_for_each_entry(rport, &fc_host->rports, peers) {
1944 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1945 (rport->channel == channel)) {
1947 switch (fc_host->tgtid_bind_type) {
1948 case FC_TGTID_BIND_BY_WWPN:
1949 case FC_TGTID_BIND_NONE:
1950 if (rport->port_name == ids->port_name)
1953 case FC_TGTID_BIND_BY_WWNN:
1954 if (rport->node_name == ids->node_name)
1957 case FC_TGTID_BIND_BY_ID:
1958 if (rport->port_id == ids->port_id)
1964 struct work_struct *work =
1965 &rport->dev_loss_work;
1967 memcpy(&rport->node_name, &ids->node_name,
1968 sizeof(rport->node_name));
1969 memcpy(&rport->port_name, &ids->port_name,
1970 sizeof(rport->port_name));
1971 rport->port_id = ids->port_id;
1973 rport->port_state = FC_PORTSTATE_ONLINE;
1974 rport->roles = ids->roles;
1976 spin_unlock_irqrestore(shost->host_lock, flags);
1978 if (fci->f->dd_fcrport_size)
1979 memset(rport->dd_data, 0,
1980 fci->f->dd_fcrport_size);
1983 * If we were blocked, we were a target.
1984 * If no longer a target, we leave the timer
1985 * running in case the port changes roles
1986 * prior to the timer expiring. If the timer
1987 * fires, the target will be torn down.
1989 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1992 /* restart the target */
1995 * Stop the target timers first. Take no action
1996 * on the del_timer failure as the state
1997 * machine state change will validate the
2000 if (!cancel_delayed_work(&rport->fail_io_work))
2001 fc_flush_devloss(shost);
2002 if (!cancel_delayed_work(work))
2003 fc_flush_devloss(shost);
2005 spin_lock_irqsave(shost->host_lock, flags);
2007 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2009 /* initiate a scan of the target */
2010 rport->flags |= FC_RPORT_SCAN_PENDING;
2011 scsi_queue_work(shost, &rport->scan_work);
2013 spin_unlock_irqrestore(shost->host_lock, flags);
2015 scsi_target_unblock(&rport->dev);
2022 /* Search the bindings array */
2023 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2025 /* search for a matching consistent binding */
2027 list_for_each_entry(rport, &fc_host->rport_bindings,
2029 if (rport->channel != channel)
2032 switch (fc_host->tgtid_bind_type) {
2033 case FC_TGTID_BIND_BY_WWPN:
2034 if (rport->port_name == ids->port_name)
2037 case FC_TGTID_BIND_BY_WWNN:
2038 if (rport->node_name == ids->node_name)
2041 case FC_TGTID_BIND_BY_ID:
2042 if (rport->port_id == ids->port_id)
2045 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2050 list_move_tail(&rport->peers, &fc_host->rports);
2056 memcpy(&rport->node_name, &ids->node_name,
2057 sizeof(rport->node_name));
2058 memcpy(&rport->port_name, &ids->port_name,
2059 sizeof(rport->port_name));
2060 rport->port_id = ids->port_id;
2061 rport->roles = ids->roles;
2062 rport->port_state = FC_PORTSTATE_ONLINE;
2064 if (fci->f->dd_fcrport_size)
2065 memset(rport->dd_data, 0,
2066 fci->f->dd_fcrport_size);
2068 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
2069 /* initiate a scan of the target */
2070 rport->flags |= FC_RPORT_SCAN_PENDING;
2071 scsi_queue_work(shost, &rport->scan_work);
2072 spin_unlock_irqrestore(shost->host_lock, flags);
2073 scsi_target_unblock(&rport->dev);
2075 spin_unlock_irqrestore(shost->host_lock, flags);
2081 spin_unlock_irqrestore(shost->host_lock, flags);
2083 /* No consistent binding found - create new remote port entry */
2084 rport = fc_rport_create(shost, channel, ids);
2088 EXPORT_SYMBOL(fc_remote_port_add);
2092 * fc_remote_port_delete - notifies the fc transport that a remote
2093 * port is no longer in existence.
2094 * @rport: The remote port that no longer exists
2096 * The LLDD calls this routine to notify the transport that a remote
2097 * port is no longer part of the topology. Note: Although a port
2098 * may no longer be part of the topology, it may persist in the remote
2099 * ports displayed by the fc_host. We do this under 2 conditions:
2100 * - If the port was a scsi target, we delay its deletion by "blocking" it.
2101 * This allows the port to temporarily disappear, then reappear without
2102 * disrupting the SCSI device tree attached to it. During the "blocked"
2103 * period the port will still exist.
2104 * - If the port was a scsi target and disappears for longer than we
2105 * expect, we'll delete the port and the tear down the SCSI device tree
2106 * attached to it. However, we want to semi-persist the target id assigned
2107 * to that port if it eventually does exist. The port structure will
2108 * remain (although with minimal information) so that the target id
2111 * If the remote port is not an FCP Target, it will be fully torn down
2112 * and deallocated, including the fc_remote_port class device.
2114 * If the remote port is an FCP Target, the port will be placed in a
2115 * temporary blocked state. From the LLDD's perspective, the rport no
2116 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2117 * exists, but all sdevs on it are blocked from further I/O. The following
2119 * If the remote port does not return (signaled by a LLDD call to
2120 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2121 * scsi target is removed - killing all outstanding i/o and removing the
2122 * scsi devices attached ot it. The port structure will be marked Not
2123 * Present and be partially cleared, leaving only enough information to
2124 * recognize the remote port relative to the scsi target id binding if
2125 * it later appears. The port will remain as long as there is a valid
2126 * binding (e.g. until the user changes the binding type or unloads the
2127 * scsi host with the binding).
2129 * If the remote port returns within the dev_loss_tmo value (and matches
2130 * according to the target id binding type), the port structure will be
2131 * reused. If it is no longer a SCSI target, the target will be torn
2132 * down. If it continues to be a SCSI target, then the target will be
2133 * unblocked (allowing i/o to be resumed), and a scan will be activated
2134 * to ensure that all luns are detected.
2136 * Called from normal process context only - cannot be called from interrupt.
2139 * This routine assumes no locks are held on entry.
2142 fc_remote_port_delete(struct fc_rport *rport)
2144 struct Scsi_Host *shost = rport_to_shost(rport);
2145 struct fc_internal *i = to_fc_internal(shost->transportt);
2146 int timeout = rport->dev_loss_tmo;
2147 unsigned long flags;
2150 * No need to flush the fc_host work_q's, as all adds are synchronous.
2152 * We do need to reclaim the rport scan work element, so eventually
2153 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2154 * there's still a scan pending.
2157 spin_lock_irqsave(shost->host_lock, flags);
2159 /* If no scsi target id mapping, delete it */
2160 if (rport->scsi_target_id == -1) {
2161 list_del(&rport->peers);
2162 rport->port_state = FC_PORTSTATE_DELETED;
2163 fc_queue_work(shost, &rport->rport_delete_work);
2164 spin_unlock_irqrestore(shost->host_lock, flags);
2168 rport->port_state = FC_PORTSTATE_BLOCKED;
2170 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2172 spin_unlock_irqrestore(shost->host_lock, flags);
2174 scsi_target_block(&rport->dev);
2176 /* see if we need to kill io faster than waiting for device loss */
2177 if ((rport->fast_io_fail_tmo != -1) &&
2178 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2179 fc_queue_devloss_work(shost, &rport->fail_io_work,
2180 rport->fast_io_fail_tmo * HZ);
2182 /* cap the length the devices can be blocked until they are deleted */
2183 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2185 EXPORT_SYMBOL(fc_remote_port_delete);
2188 * fc_remote_port_rolechg - notifies the fc transport that the roles
2189 * on a remote may have changed.
2190 * @rport: The remote port that changed.
2192 * The LLDD calls this routine to notify the transport that the roles
2193 * on a remote port may have changed. The largest effect of this is
2194 * if a port now becomes a FCP Target, it must be allocated a
2195 * scsi target id. If the port is no longer a FCP target, any
2196 * scsi target id value assigned to it will persist in case the
2197 * role changes back to include FCP Target. No changes in the scsi
2198 * midlayer will be invoked if the role changes (in the expectation
2199 * that the role will be resumed. If it doesn't normal error processing
2202 * Should not be called from interrupt context.
2205 * This routine assumes no locks are held on entry.
2208 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2210 struct Scsi_Host *shost = rport_to_shost(rport);
2211 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2212 unsigned long flags;
2215 spin_lock_irqsave(shost->host_lock, flags);
2216 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
2217 if (rport->scsi_target_id == -1) {
2218 rport->scsi_target_id = fc_host->next_target_id++;
2220 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
2224 rport->roles = roles;
2226 spin_unlock_irqrestore(shost->host_lock, flags);
2230 * There may have been a delete timer running on the
2231 * port. Ensure that it is cancelled as we now know
2232 * the port is an FCP Target.
2233 * Note: we know the rport is exists and in an online
2234 * state as the LLDD would not have had an rport
2235 * reference to pass us.
2237 * Take no action on the del_timer failure as the state
2238 * machine state change will validate the
2241 if (!cancel_delayed_work(&rport->fail_io_work))
2242 fc_flush_devloss(shost);
2243 if (!cancel_delayed_work(&rport->dev_loss_work))
2244 fc_flush_devloss(shost);
2246 spin_lock_irqsave(shost->host_lock, flags);
2247 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2248 spin_unlock_irqrestore(shost->host_lock, flags);
2250 /* ensure any stgt delete functions are done */
2251 fc_flush_work(shost);
2253 /* initiate a scan of the target */
2254 spin_lock_irqsave(shost->host_lock, flags);
2255 rport->flags |= FC_RPORT_SCAN_PENDING;
2256 scsi_queue_work(shost, &rport->scan_work);
2257 spin_unlock_irqrestore(shost->host_lock, flags);
2258 scsi_target_unblock(&rport->dev);
2261 EXPORT_SYMBOL(fc_remote_port_rolechg);
2264 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
2265 * was a SCSI target (thus was blocked), and failed
2266 * to return in the alloted time.
2268 * @data: rport target that failed to reappear in the alloted time.
2271 fc_timeout_deleted_rport(void *data)
2273 struct fc_rport *rport = (struct fc_rport *)data;
2274 struct Scsi_Host *shost = rport_to_shost(rport);
2275 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2276 unsigned long flags;
2278 spin_lock_irqsave(shost->host_lock, flags);
2280 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2283 * If the port is ONLINE, then it came back. Validate it's still an
2284 * FCP target. If not, tear down the scsi_target on it.
2286 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2287 !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2288 dev_printk(KERN_ERR, &rport->dev,
2289 "blocked FC remote port time out: no longer"
2290 " a FCP target, removing starget\n");
2291 spin_unlock_irqrestore(shost->host_lock, flags);
2292 scsi_target_unblock(&rport->dev);
2293 fc_queue_work(shost, &rport->stgt_delete_work);
2297 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2298 spin_unlock_irqrestore(shost->host_lock, flags);
2299 dev_printk(KERN_ERR, &rport->dev,
2300 "blocked FC remote port time out: leaving target alone\n");
2304 if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
2305 list_del(&rport->peers);
2306 rport->port_state = FC_PORTSTATE_DELETED;
2307 dev_printk(KERN_ERR, &rport->dev,
2308 "blocked FC remote port time out: removing target\n");
2309 fc_queue_work(shost, &rport->rport_delete_work);
2310 spin_unlock_irqrestore(shost->host_lock, flags);
2314 dev_printk(KERN_ERR, &rport->dev,
2315 "blocked FC remote port time out: removing target and "
2316 "saving binding\n");
2318 list_move_tail(&rport->peers, &fc_host->rport_bindings);
2321 * Note: We do not remove or clear the hostdata area. This allows
2322 * host-specific target data to persist along with the
2323 * scsi_target_id. It's up to the host to manage it's hostdata area.
2327 * Reinitialize port attributes that may change if the port comes back.
2329 rport->maxframe_size = -1;
2330 rport->supported_classes = FC_COS_UNSPECIFIED;
2331 rport->roles = FC_RPORT_ROLE_UNKNOWN;
2332 rport->port_state = FC_PORTSTATE_NOTPRESENT;
2334 /* remove the identifiers that aren't used in the consisting binding */
2335 switch (fc_host->tgtid_bind_type) {
2336 case FC_TGTID_BIND_BY_WWPN:
2337 rport->node_name = -1;
2338 rport->port_id = -1;
2340 case FC_TGTID_BIND_BY_WWNN:
2341 rport->port_name = -1;
2342 rport->port_id = -1;
2344 case FC_TGTID_BIND_BY_ID:
2345 rport->node_name = -1;
2346 rport->port_name = -1;
2348 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2353 * As this only occurs if the remote port (scsi target)
2354 * went away and didn't come back - we'll remove
2355 * all attached scsi devices.
2357 spin_unlock_irqrestore(shost->host_lock, flags);
2359 scsi_target_unblock(&rport->dev);
2360 fc_queue_work(shost, &rport->stgt_delete_work);
2364 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2365 * disconnected SCSI target.
2367 * @data: rport to terminate io on.
2369 * Notes: Only requests the failure of the io, not that all are flushed
2370 * prior to returning.
2373 fc_timeout_fail_rport_io(void *data)
2375 struct fc_rport *rport = (struct fc_rport *)data;
2376 struct Scsi_Host *shost = rport_to_shost(rport);
2377 struct fc_internal *i = to_fc_internal(shost->transportt);
2379 if (rport->port_state != FC_PORTSTATE_BLOCKED)
2382 i->f->terminate_rport_io(rport);
2386 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2388 * @data: remote port to be scanned.
2391 fc_scsi_scan_rport(void *data)
2393 struct fc_rport *rport = (struct fc_rport *)data;
2394 struct Scsi_Host *shost = rport_to_shost(rport);
2395 unsigned long flags;
2397 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2398 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2399 scsi_scan_target(&rport->dev, rport->channel,
2400 rport->scsi_target_id, SCAN_WILD_CARD, 1);
2403 spin_lock_irqsave(shost->host_lock, flags);
2404 rport->flags &= ~FC_RPORT_SCAN_PENDING;
2405 spin_unlock_irqrestore(shost->host_lock, flags);
2409 MODULE_AUTHOR("Martin Hicks");
2410 MODULE_DESCRIPTION("FC Transport Attributes");
2411 MODULE_LICENSE("GPL");
2413 module_init(fc_transport_init);
2414 module_exit(fc_transport_exit);