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/scsi_cmnd.h>
34 #include <linux/netlink.h>
35 #include <net/netlink.h>
36 #include <scsi/scsi_netlink_fc.h>
37 #include "scsi_priv.h"
39 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
42 * Redefine so that we can have same named attributes in the
43 * sdev/starget/host objects.
45 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
46 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
47 __ATTR(_name,_mode,_show,_store)
49 #define fc_enum_name_search(title, table_type, table) \
50 static const char *get_fc_##title##_name(enum table_type table_key) \
55 for (i = 0; i < ARRAY_SIZE(table); i++) { \
56 if (table[i].value == table_key) { \
57 name = table[i].name; \
64 #define fc_enum_name_match(title, table_type, table) \
65 static int get_fc_##title##_match(const char *table_key, \
66 enum table_type *value) \
70 for (i = 0; i < ARRAY_SIZE(table); i++) { \
71 if (strncmp(table_key, table[i].name, \
72 table[i].matchlen) == 0) { \
73 *value = table[i].value; \
74 return 0; /* success */ \
77 return 1; /* failure */ \
81 /* Convert fc_port_type values to ascii string name */
83 enum fc_port_type value;
85 } fc_port_type_names[] = {
86 { FC_PORTTYPE_UNKNOWN, "Unknown" },
87 { FC_PORTTYPE_OTHER, "Other" },
88 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
89 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
90 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
91 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
92 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
94 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
95 #define FC_PORTTYPE_MAX_NAMELEN 50
98 /* Convert fc_host_event_code values to ascii string name */
100 enum fc_host_event_code value;
102 } fc_host_event_code_names[] = {
103 { FCH_EVT_LIP, "lip" },
104 { FCH_EVT_LINKUP, "link_up" },
105 { FCH_EVT_LINKDOWN, "link_down" },
106 { FCH_EVT_LIPRESET, "lip_reset" },
107 { FCH_EVT_RSCN, "rscn" },
108 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
109 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
110 { FCH_EVT_PORT_ONLINE, "port_online" },
111 { FCH_EVT_PORT_OFFLINE, "port_offline" },
112 { FCH_EVT_PORT_FABRIC, "port_fabric" },
113 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
114 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
116 fc_enum_name_search(host_event_code, fc_host_event_code,
117 fc_host_event_code_names)
118 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
121 /* Convert fc_port_state values to ascii string name */
123 enum fc_port_state value;
125 } fc_port_state_names[] = {
126 { FC_PORTSTATE_UNKNOWN, "Unknown" },
127 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
128 { FC_PORTSTATE_ONLINE, "Online" },
129 { FC_PORTSTATE_OFFLINE, "Offline" },
130 { FC_PORTSTATE_BLOCKED, "Blocked" },
131 { FC_PORTSTATE_BYPASSED, "Bypassed" },
132 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
133 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
134 { FC_PORTSTATE_ERROR, "Error" },
135 { FC_PORTSTATE_LOOPBACK, "Loopback" },
136 { FC_PORTSTATE_DELETED, "Deleted" },
138 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
139 #define FC_PORTSTATE_MAX_NAMELEN 20
142 /* Convert fc_tgtid_binding_type values to ascii string name */
143 static const struct {
144 enum fc_tgtid_binding_type value;
147 } fc_tgtid_binding_type_names[] = {
148 { FC_TGTID_BIND_NONE, "none", 4 },
149 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
150 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
151 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
153 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
154 fc_tgtid_binding_type_names)
155 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
156 fc_tgtid_binding_type_names)
157 #define FC_BINDTYPE_MAX_NAMELEN 30
160 #define fc_bitfield_name_search(title, table) \
162 get_fc_##title##_names(u32 table_key, char *buf) \
168 for (i = 0; i < ARRAY_SIZE(table); i++) { \
169 if (table[i].value & table_key) { \
170 len += sprintf(buf + len, "%s%s", \
171 prefix, table[i].name); \
175 len += sprintf(buf + len, "\n"); \
180 /* Convert FC_COS bit values to ascii string name */
181 static const struct {
185 { FC_COS_CLASS1, "Class 1" },
186 { FC_COS_CLASS2, "Class 2" },
187 { FC_COS_CLASS3, "Class 3" },
188 { FC_COS_CLASS4, "Class 4" },
189 { FC_COS_CLASS6, "Class 6" },
191 fc_bitfield_name_search(cos, fc_cos_names)
194 /* Convert FC_PORTSPEED bit values to ascii string name */
195 static const struct {
198 } fc_port_speed_names[] = {
199 { FC_PORTSPEED_1GBIT, "1 Gbit" },
200 { FC_PORTSPEED_2GBIT, "2 Gbit" },
201 { FC_PORTSPEED_4GBIT, "4 Gbit" },
202 { FC_PORTSPEED_10GBIT, "10 Gbit" },
203 { FC_PORTSPEED_8GBIT, "8 Gbit" },
204 { FC_PORTSPEED_16GBIT, "16 Gbit" },
205 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
207 fc_bitfield_name_search(port_speed, fc_port_speed_names)
211 show_fc_fc4s (char *buf, u8 *fc4_list)
215 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
216 len += sprintf(buf + len , "0x%02x ", *fc4_list);
217 len += sprintf(buf + len, "\n");
222 /* Convert FC_RPORT_ROLE bit values to ascii string name */
223 static const struct {
226 } fc_remote_port_role_names[] = {
227 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
228 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
229 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
231 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
234 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
236 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
237 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
238 #define FC_FPORT_PORTID 0x00000e
239 #define FC_FABCTLR_PORTID 0x00000d
240 #define FC_DIRSRVR_PORTID 0x00000c
241 #define FC_TIMESRVR_PORTID 0x00000b
242 #define FC_MGMTSRVR_PORTID 0x00000a
245 static void fc_timeout_deleted_rport(struct work_struct *work);
246 static void fc_timeout_fail_rport_io(struct work_struct *work);
247 static void fc_scsi_scan_rport(struct work_struct *work);
250 * Attribute counts pre object type...
251 * Increase these values if you add attributes
253 #define FC_STARGET_NUM_ATTRS 3
254 #define FC_RPORT_NUM_ATTRS 10
255 #define FC_HOST_NUM_ATTRS 17
258 struct scsi_transport_template t;
259 struct fc_function_template *f;
262 * For attributes : each object has :
263 * An array of the actual attributes structures
264 * An array of null-terminated pointers to the attribute
265 * structures - used for mid-layer interaction.
267 * The attribute containers for the starget and host are are
268 * part of the midlayer. As the remote port is specific to the
269 * fc transport, we must provide the attribute container.
271 struct class_device_attribute private_starget_attrs[
272 FC_STARGET_NUM_ATTRS];
273 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
275 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
276 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
278 struct transport_container rport_attr_cont;
279 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
280 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
283 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
285 static int fc_target_setup(struct transport_container *tc, struct device *dev,
286 struct class_device *cdev)
288 struct scsi_target *starget = to_scsi_target(dev);
289 struct fc_rport *rport = starget_to_rport(starget);
292 * if parent is remote port, use values from remote port.
293 * Otherwise, this host uses the fc_transport, but not the
294 * remote port interface. As such, initialize to known non-values.
297 fc_starget_node_name(starget) = rport->node_name;
298 fc_starget_port_name(starget) = rport->port_name;
299 fc_starget_port_id(starget) = rport->port_id;
301 fc_starget_node_name(starget) = -1;
302 fc_starget_port_name(starget) = -1;
303 fc_starget_port_id(starget) = -1;
309 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
315 static int fc_host_setup(struct transport_container *tc, struct device *dev,
316 struct class_device *cdev)
318 struct Scsi_Host *shost = dev_to_shost(dev);
319 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
322 * Set default values easily detected by the midlayer as
323 * failure cases. The scsi lldd is responsible for initializing
324 * all transport attributes to valid values per host.
326 fc_host->node_name = -1;
327 fc_host->port_name = -1;
328 fc_host->permanent_port_name = -1;
329 fc_host->supported_classes = FC_COS_UNSPECIFIED;
330 memset(fc_host->supported_fc4s, 0,
331 sizeof(fc_host->supported_fc4s));
332 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
333 fc_host->maxframe_size = -1;
334 memset(fc_host->serial_number, 0,
335 sizeof(fc_host->serial_number));
337 fc_host->port_id = -1;
338 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
339 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
340 memset(fc_host->active_fc4s, 0,
341 sizeof(fc_host->active_fc4s));
342 fc_host->speed = FC_PORTSPEED_UNKNOWN;
343 fc_host->fabric_name = -1;
344 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
345 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
347 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
349 INIT_LIST_HEAD(&fc_host->rports);
350 INIT_LIST_HEAD(&fc_host->rport_bindings);
351 fc_host->next_rport_number = 0;
352 fc_host->next_target_id = 0;
354 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
356 fc_host->work_q = create_singlethread_workqueue(
357 fc_host->work_q_name);
358 if (!fc_host->work_q)
361 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
363 fc_host->devloss_work_q = create_singlethread_workqueue(
364 fc_host->devloss_work_q_name);
365 if (!fc_host->devloss_work_q) {
366 destroy_workqueue(fc_host->work_q);
367 fc_host->work_q = NULL;
374 static DECLARE_TRANSPORT_CLASS(fc_host_class,
381 * Setup and Remove actions for remote ports are handled
382 * in the service functions below.
384 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
395 * dev_loss_tmo: the default number of seconds that the FC transport
396 * should insulate the loss of a remote port.
397 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
399 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
401 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
402 MODULE_PARM_DESC(dev_loss_tmo,
403 "Maximum number of seconds that the FC transport should"
404 " insulate the loss of a remote port. Once this value is"
405 " exceeded, the scsi target is removed. Value should be"
406 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
409 * Netlink Infrastructure
412 static atomic_t fc_event_seq;
415 * fc_get_event_number - Obtain the next sequential FC event number
418 * We could have inline'd this, but it would have required fc_event_seq to
419 * be exposed. For now, live with the subroutine call.
420 * Atomic used to avoid lock/unlock...
423 fc_get_event_number(void)
425 return atomic_add_return(1, &fc_event_seq);
427 EXPORT_SYMBOL(fc_get_event_number);
431 * fc_host_post_event - called to post an even on an fc_host.
433 * @shost: host the event occurred on
434 * @event_number: fc event number obtained from get_fc_event_number()
435 * @event_code: fc_host event being posted
436 * @event_data: 32bits of data for the event being posted
439 * This routine assumes no locks are held on entry.
442 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
443 enum fc_host_event_code event_code, u32 event_data)
446 struct nlmsghdr *nlh;
447 struct fc_nl_event *event;
457 len = FC_NL_MSGALIGN(sizeof(*event));
458 skblen = NLMSG_SPACE(len);
460 skb = alloc_skb(skblen, GFP_KERNEL);
466 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
467 skblen - sizeof(*nlh), 0);
472 event = NLMSG_DATA(nlh);
474 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
475 FC_NL_ASYNC_EVENT, len);
476 event->seconds = get_seconds();
477 event->vendor_id = 0;
478 event->host_no = shost->host_no;
479 event->event_datalen = sizeof(u32); /* bytes */
480 event->event_num = event_number;
481 event->event_code = event_code;
482 event->event_data = event_data;
484 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
486 if (err && (err != -ESRCH)) /* filter no recipient errors */
487 /* nlmsg_multicast already kfree_skb'd */
495 name = get_fc_host_event_code_name(event_code);
497 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
498 __FUNCTION__, shost->host_no,
499 (name) ? name : "<unknown>", event_data, err);
502 EXPORT_SYMBOL(fc_host_post_event);
506 * fc_host_post_vendor_event - called to post a vendor unique event on
509 * @shost: host the event occurred on
510 * @event_number: fc event number obtained from get_fc_event_number()
511 * @data_len: amount, in bytes, of vendor unique data
512 * @data_buf: pointer to vendor unique data
515 * This routine assumes no locks are held on entry.
518 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
519 u32 data_len, char * data_buf, u64 vendor_id)
522 struct nlmsghdr *nlh;
523 struct fc_nl_event *event;
529 goto send_vendor_fail;
532 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
533 skblen = NLMSG_SPACE(len);
535 skb = alloc_skb(skblen, GFP_KERNEL);
538 goto send_vendor_fail;
541 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
542 skblen - sizeof(*nlh), 0);
545 goto send_vendor_fail_skb;
547 event = NLMSG_DATA(nlh);
549 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
550 FC_NL_ASYNC_EVENT, len);
551 event->seconds = get_seconds();
552 event->vendor_id = vendor_id;
553 event->host_no = shost->host_no;
554 event->event_datalen = data_len; /* bytes */
555 event->event_num = event_number;
556 event->event_code = FCH_EVT_VENDOR_UNIQUE;
557 memcpy(&event->event_data, data_buf, data_len);
559 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
561 if (err && (err != -ESRCH)) /* filter no recipient errors */
562 /* nlmsg_multicast already kfree_skb'd */
563 goto send_vendor_fail;
567 send_vendor_fail_skb:
571 "%s: Dropped Event : host %d vendor_unique - err %d\n",
572 __FUNCTION__, shost->host_no, err);
575 EXPORT_SYMBOL(fc_host_post_vendor_event);
579 static __init int fc_transport_init(void)
583 atomic_set(&fc_event_seq, 0);
585 error = transport_class_register(&fc_host_class);
588 error = transport_class_register(&fc_rport_class);
591 return transport_class_register(&fc_transport_class);
594 static void __exit fc_transport_exit(void)
596 transport_class_unregister(&fc_transport_class);
597 transport_class_unregister(&fc_rport_class);
598 transport_class_unregister(&fc_host_class);
602 * FC Remote Port Attribute Management
605 #define fc_rport_show_function(field, format_string, sz, cast) \
607 show_fc_rport_##field (struct class_device *cdev, char *buf) \
609 struct fc_rport *rport = transport_class_to_rport(cdev); \
610 struct Scsi_Host *shost = rport_to_shost(rport); \
611 struct fc_internal *i = to_fc_internal(shost->transportt); \
612 if ((i->f->get_rport_##field) && \
613 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
614 (rport->port_state == FC_PORTSTATE_DELETED) || \
615 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
616 i->f->get_rport_##field(rport); \
617 return snprintf(buf, sz, format_string, cast rport->field); \
620 #define fc_rport_store_function(field) \
622 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
626 struct fc_rport *rport = transport_class_to_rport(cdev); \
627 struct Scsi_Host *shost = rport_to_shost(rport); \
628 struct fc_internal *i = to_fc_internal(shost->transportt); \
630 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
631 (rport->port_state == FC_PORTSTATE_DELETED) || \
632 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
634 val = simple_strtoul(buf, &cp, 0); \
635 if (*cp && (*cp != '\n')) \
637 i->f->set_rport_##field(rport, val); \
641 #define fc_rport_rd_attr(field, format_string, sz) \
642 fc_rport_show_function(field, format_string, sz, ) \
643 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
644 show_fc_rport_##field, NULL)
646 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
647 fc_rport_show_function(field, format_string, sz, (cast)) \
648 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
649 show_fc_rport_##field, NULL)
651 #define fc_rport_rw_attr(field, format_string, sz) \
652 fc_rport_show_function(field, format_string, sz, ) \
653 fc_rport_store_function(field) \
654 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
655 show_fc_rport_##field, \
656 store_fc_rport_##field)
659 #define fc_private_rport_show_function(field, format_string, sz, cast) \
661 show_fc_rport_##field (struct class_device *cdev, char *buf) \
663 struct fc_rport *rport = transport_class_to_rport(cdev); \
664 return snprintf(buf, sz, format_string, cast rport->field); \
667 #define fc_private_rport_rd_attr(field, format_string, sz) \
668 fc_private_rport_show_function(field, format_string, sz, ) \
669 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
670 show_fc_rport_##field, NULL)
672 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
673 fc_private_rport_show_function(field, format_string, sz, (cast)) \
674 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
675 show_fc_rport_##field, NULL)
678 #define fc_private_rport_rd_enum_attr(title, maxlen) \
680 show_fc_rport_##title (struct class_device *cdev, char *buf) \
682 struct fc_rport *rport = transport_class_to_rport(cdev); \
684 name = get_fc_##title##_name(rport->title); \
687 return snprintf(buf, maxlen, "%s\n", name); \
689 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
690 show_fc_rport_##title, NULL)
693 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
694 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
695 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
696 i->private_rport_attrs[count].store = NULL; \
697 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
698 if (i->f->show_rport_##field) \
701 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
702 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
703 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
704 i->private_rport_attrs[count].store = NULL; \
705 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
708 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
709 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
710 if (!i->f->set_rport_##field) { \
711 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
712 i->private_rport_attrs[count].store = NULL; \
714 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
715 if (i->f->show_rport_##field) \
718 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
720 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
721 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
726 /* The FC Transport Remote Port Attributes: */
728 /* Fixed Remote Port Attributes */
730 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
733 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
735 struct fc_rport *rport = transport_class_to_rport(cdev);
736 if (rport->supported_classes == FC_COS_UNSPECIFIED)
737 return snprintf(buf, 20, "unspecified\n");
738 return get_fc_cos_names(rport->supported_classes, buf);
740 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
741 show_fc_rport_supported_classes, NULL);
743 /* Dynamic Remote Port Attributes */
746 * dev_loss_tmo attribute
748 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
750 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
754 struct fc_rport *rport = transport_class_to_rport(cdev);
755 struct Scsi_Host *shost = rport_to_shost(rport);
756 struct fc_internal *i = to_fc_internal(shost->transportt);
758 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
759 (rport->port_state == FC_PORTSTATE_DELETED) ||
760 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
762 val = simple_strtoul(buf, &cp, 0);
763 if ((*cp && (*cp != '\n')) ||
764 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
766 i->f->set_rport_dev_loss_tmo(rport, val);
769 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
770 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
773 /* Private Remote Port Attributes */
775 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
776 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
777 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
780 show_fc_rport_roles (struct class_device *cdev, char *buf)
782 struct fc_rport *rport = transport_class_to_rport(cdev);
784 /* identify any roles that are port_id specific */
785 if ((rport->port_id != -1) &&
786 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
787 FC_WELLKNOWN_PORTID_MASK) {
788 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
789 case FC_FPORT_PORTID:
790 return snprintf(buf, 30, "Fabric Port\n");
791 case FC_FABCTLR_PORTID:
792 return snprintf(buf, 30, "Fabric Controller\n");
793 case FC_DIRSRVR_PORTID:
794 return snprintf(buf, 30, "Directory Server\n");
795 case FC_TIMESRVR_PORTID:
796 return snprintf(buf, 30, "Time Server\n");
797 case FC_MGMTSRVR_PORTID:
798 return snprintf(buf, 30, "Management Server\n");
800 return snprintf(buf, 30, "Unknown Fabric Entity\n");
803 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
804 return snprintf(buf, 20, "unknown\n");
805 return get_fc_remote_port_roles_names(rport->roles, buf);
808 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
809 show_fc_rport_roles, NULL);
811 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
812 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
815 * fast_io_fail_tmo attribute
818 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
820 struct fc_rport *rport = transport_class_to_rport(cdev);
822 if (rport->fast_io_fail_tmo == -1)
823 return snprintf(buf, 5, "off\n");
824 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
828 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
833 struct fc_rport *rport = transport_class_to_rport(cdev);
835 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
836 (rport->port_state == FC_PORTSTATE_DELETED) ||
837 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
839 if (strncmp(buf, "off", 3) == 0)
840 rport->fast_io_fail_tmo = -1;
842 val = simple_strtoul(buf, &cp, 0);
843 if ((*cp && (*cp != '\n')) ||
844 (val < 0) || (val >= rport->dev_loss_tmo))
846 rport->fast_io_fail_tmo = val;
850 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
851 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
855 * FC SCSI Target Attribute Management
859 * Note: in the target show function we recognize when the remote
860 * port is in the hierarchy and do not allow the driver to get
861 * involved in sysfs functions. The driver only gets involved if
862 * it's the "old" style that doesn't use rports.
864 #define fc_starget_show_function(field, format_string, sz, cast) \
866 show_fc_starget_##field (struct class_device *cdev, char *buf) \
868 struct scsi_target *starget = transport_class_to_starget(cdev); \
869 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
870 struct fc_internal *i = to_fc_internal(shost->transportt); \
871 struct fc_rport *rport = starget_to_rport(starget); \
873 fc_starget_##field(starget) = rport->field; \
874 else if (i->f->get_starget_##field) \
875 i->f->get_starget_##field(starget); \
876 return snprintf(buf, sz, format_string, \
877 cast fc_starget_##field(starget)); \
880 #define fc_starget_rd_attr(field, format_string, sz) \
881 fc_starget_show_function(field, format_string, sz, ) \
882 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
883 show_fc_starget_##field, NULL)
885 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
886 fc_starget_show_function(field, format_string, sz, (cast)) \
887 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
888 show_fc_starget_##field, NULL)
890 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
891 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
892 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
893 i->private_starget_attrs[count].store = NULL; \
894 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
895 if (i->f->show_starget_##field) \
898 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
899 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
900 if (!i->f->set_starget_##field) { \
901 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
902 i->private_starget_attrs[count].store = NULL; \
904 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
905 if (i->f->show_starget_##field) \
908 /* The FC Transport SCSI Target Attributes: */
909 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
910 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
911 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
915 * Host Attribute Management
918 #define fc_host_show_function(field, format_string, sz, cast) \
920 show_fc_host_##field (struct class_device *cdev, char *buf) \
922 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
923 struct fc_internal *i = to_fc_internal(shost->transportt); \
924 if (i->f->get_host_##field) \
925 i->f->get_host_##field(shost); \
926 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
929 #define fc_host_store_function(field) \
931 store_fc_host_##field(struct class_device *cdev, const char *buf, \
935 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
936 struct fc_internal *i = to_fc_internal(shost->transportt); \
939 val = simple_strtoul(buf, &cp, 0); \
940 if (*cp && (*cp != '\n')) \
942 i->f->set_host_##field(shost, val); \
946 #define fc_host_store_str_function(field, slen) \
948 store_fc_host_##field(struct class_device *cdev, const char *buf, \
951 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
952 struct fc_internal *i = to_fc_internal(shost->transportt); \
953 unsigned int cnt=count; \
955 /* count may include a LF at end of string */ \
956 if (buf[cnt-1] == '\n') \
958 if (cnt > ((slen) - 1)) \
960 memcpy(fc_host_##field(shost), buf, cnt); \
961 i->f->set_host_##field(shost); \
965 #define fc_host_rd_attr(field, format_string, sz) \
966 fc_host_show_function(field, format_string, sz, ) \
967 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
968 show_fc_host_##field, NULL)
970 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
971 fc_host_show_function(field, format_string, sz, (cast)) \
972 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
973 show_fc_host_##field, NULL)
975 #define fc_host_rw_attr(field, format_string, sz) \
976 fc_host_show_function(field, format_string, sz, ) \
977 fc_host_store_function(field) \
978 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
979 show_fc_host_##field, \
980 store_fc_host_##field)
982 #define fc_host_rd_enum_attr(title, maxlen) \
984 show_fc_host_##title (struct class_device *cdev, char *buf) \
986 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
987 struct fc_internal *i = to_fc_internal(shost->transportt); \
989 if (i->f->get_host_##title) \
990 i->f->get_host_##title(shost); \
991 name = get_fc_##title##_name(fc_host_##title(shost)); \
994 return snprintf(buf, maxlen, "%s\n", name); \
996 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
998 #define SETUP_HOST_ATTRIBUTE_RD(field) \
999 i->private_host_attrs[count] = class_device_attr_host_##field; \
1000 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1001 i->private_host_attrs[count].store = NULL; \
1002 i->host_attrs[count] = &i->private_host_attrs[count]; \
1003 if (i->f->show_host_##field) \
1006 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1007 i->private_host_attrs[count] = class_device_attr_host_##field; \
1008 if (!i->f->set_host_##field) { \
1009 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1010 i->private_host_attrs[count].store = NULL; \
1012 i->host_attrs[count] = &i->private_host_attrs[count]; \
1013 if (i->f->show_host_##field) \
1017 #define fc_private_host_show_function(field, format_string, sz, cast) \
1019 show_fc_host_##field (struct class_device *cdev, char *buf) \
1021 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1022 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1025 #define fc_private_host_rd_attr(field, format_string, sz) \
1026 fc_private_host_show_function(field, format_string, sz, ) \
1027 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1028 show_fc_host_##field, NULL)
1030 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1031 fc_private_host_show_function(field, format_string, sz, (cast)) \
1032 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1033 show_fc_host_##field, NULL)
1035 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1036 i->private_host_attrs[count] = class_device_attr_host_##field; \
1037 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1038 i->private_host_attrs[count].store = NULL; \
1039 i->host_attrs[count] = &i->private_host_attrs[count]; \
1042 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1044 i->private_host_attrs[count] = class_device_attr_host_##field; \
1045 i->host_attrs[count] = &i->private_host_attrs[count]; \
1050 /* Fixed Host Attributes */
1053 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1055 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1057 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1058 return snprintf(buf, 20, "unspecified\n");
1060 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1062 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1063 show_fc_host_supported_classes, NULL);
1066 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1068 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1069 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1071 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1072 show_fc_host_supported_fc4s, NULL);
1075 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1077 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1079 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1080 return snprintf(buf, 20, "unknown\n");
1082 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1084 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1085 show_fc_host_supported_speeds, NULL);
1088 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1089 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1090 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1091 unsigned long long);
1092 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1093 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1096 /* Dynamic Host Attributes */
1099 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1101 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1102 struct fc_internal *i = to_fc_internal(shost->transportt);
1104 if (i->f->get_host_active_fc4s)
1105 i->f->get_host_active_fc4s(shost);
1107 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1109 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1110 show_fc_host_active_fc4s, NULL);
1113 show_fc_host_speed (struct class_device *cdev, char *buf)
1115 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1116 struct fc_internal *i = to_fc_internal(shost->transportt);
1118 if (i->f->get_host_speed)
1119 i->f->get_host_speed(shost);
1121 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1122 return snprintf(buf, 20, "unknown\n");
1124 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1126 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1127 show_fc_host_speed, NULL);
1130 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1131 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1132 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1133 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1134 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1136 fc_private_host_show_function(system_hostname, "%s\n",
1137 FC_SYMBOLIC_NAME_SIZE + 1, )
1138 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1139 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1140 show_fc_host_system_hostname, store_fc_host_system_hostname);
1143 /* Private Host Attributes */
1146 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1148 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1151 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1154 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1157 #define get_list_head_entry(pos, head, member) \
1158 pos = list_entry((head)->next, typeof(*pos), member)
1161 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1162 const char *buf, size_t count)
1164 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1165 struct fc_rport *rport;
1166 enum fc_tgtid_binding_type val;
1167 unsigned long flags;
1169 if (get_fc_tgtid_bind_type_match(buf, &val))
1172 /* if changing bind type, purge all unused consistent bindings */
1173 if (val != fc_host_tgtid_bind_type(shost)) {
1174 spin_lock_irqsave(shost->host_lock, flags);
1175 while (!list_empty(&fc_host_rport_bindings(shost))) {
1176 get_list_head_entry(rport,
1177 &fc_host_rport_bindings(shost), peers);
1178 list_del(&rport->peers);
1179 rport->port_state = FC_PORTSTATE_DELETED;
1180 fc_queue_work(shost, &rport->rport_delete_work);
1182 spin_unlock_irqrestore(shost->host_lock, flags);
1185 fc_host_tgtid_bind_type(shost) = val;
1189 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1190 show_fc_private_host_tgtid_bind_type,
1191 store_fc_private_host_tgtid_bind_type);
1194 store_fc_private_host_issue_lip(struct class_device *cdev,
1195 const char *buf, size_t count)
1197 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1198 struct fc_internal *i = to_fc_internal(shost->transportt);
1201 /* ignore any data value written to the attribute */
1202 if (i->f->issue_fc_host_lip) {
1203 ret = i->f->issue_fc_host_lip(shost);
1204 return ret ? ret: count;
1210 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1211 store_fc_private_host_issue_lip);
1214 * Host Statistics Management
1217 /* Show a given an attribute in the statistics group */
1219 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1221 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1222 struct fc_internal *i = to_fc_internal(shost->transportt);
1223 struct fc_host_statistics *stats;
1224 ssize_t ret = -ENOENT;
1226 if (offset > sizeof(struct fc_host_statistics) ||
1227 offset % sizeof(u64) != 0)
1230 if (i->f->get_fc_host_stats) {
1231 stats = (i->f->get_fc_host_stats)(shost);
1233 ret = snprintf(buf, 20, "0x%llx\n",
1234 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1240 /* generate a read-only statistics attribute */
1241 #define fc_host_statistic(name) \
1242 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
1244 return fc_stat_show(cd, buf, \
1245 offsetof(struct fc_host_statistics, name)); \
1247 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1249 fc_host_statistic(seconds_since_last_reset);
1250 fc_host_statistic(tx_frames);
1251 fc_host_statistic(tx_words);
1252 fc_host_statistic(rx_frames);
1253 fc_host_statistic(rx_words);
1254 fc_host_statistic(lip_count);
1255 fc_host_statistic(nos_count);
1256 fc_host_statistic(error_frames);
1257 fc_host_statistic(dumped_frames);
1258 fc_host_statistic(link_failure_count);
1259 fc_host_statistic(loss_of_sync_count);
1260 fc_host_statistic(loss_of_signal_count);
1261 fc_host_statistic(prim_seq_protocol_err_count);
1262 fc_host_statistic(invalid_tx_word_count);
1263 fc_host_statistic(invalid_crc_count);
1264 fc_host_statistic(fcp_input_requests);
1265 fc_host_statistic(fcp_output_requests);
1266 fc_host_statistic(fcp_control_requests);
1267 fc_host_statistic(fcp_input_megabytes);
1268 fc_host_statistic(fcp_output_megabytes);
1271 fc_reset_statistics(struct class_device *cdev, const char *buf,
1274 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1275 struct fc_internal *i = to_fc_internal(shost->transportt);
1277 /* ignore any data value written to the attribute */
1278 if (i->f->reset_fc_host_stats) {
1279 i->f->reset_fc_host_stats(shost);
1285 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1286 fc_reset_statistics);
1289 static struct attribute *fc_statistics_attrs[] = {
1290 &class_device_attr_host_seconds_since_last_reset.attr,
1291 &class_device_attr_host_tx_frames.attr,
1292 &class_device_attr_host_tx_words.attr,
1293 &class_device_attr_host_rx_frames.attr,
1294 &class_device_attr_host_rx_words.attr,
1295 &class_device_attr_host_lip_count.attr,
1296 &class_device_attr_host_nos_count.attr,
1297 &class_device_attr_host_error_frames.attr,
1298 &class_device_attr_host_dumped_frames.attr,
1299 &class_device_attr_host_link_failure_count.attr,
1300 &class_device_attr_host_loss_of_sync_count.attr,
1301 &class_device_attr_host_loss_of_signal_count.attr,
1302 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1303 &class_device_attr_host_invalid_tx_word_count.attr,
1304 &class_device_attr_host_invalid_crc_count.attr,
1305 &class_device_attr_host_fcp_input_requests.attr,
1306 &class_device_attr_host_fcp_output_requests.attr,
1307 &class_device_attr_host_fcp_control_requests.attr,
1308 &class_device_attr_host_fcp_input_megabytes.attr,
1309 &class_device_attr_host_fcp_output_megabytes.attr,
1310 &class_device_attr_host_reset_statistics.attr,
1314 static struct attribute_group fc_statistics_group = {
1315 .name = "statistics",
1316 .attrs = fc_statistics_attrs,
1319 static int fc_host_match(struct attribute_container *cont,
1322 struct Scsi_Host *shost;
1323 struct fc_internal *i;
1325 if (!scsi_is_host_device(dev))
1328 shost = dev_to_shost(dev);
1329 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1330 != &fc_host_class.class)
1333 i = to_fc_internal(shost->transportt);
1335 return &i->t.host_attrs.ac == cont;
1338 static int fc_target_match(struct attribute_container *cont,
1341 struct Scsi_Host *shost;
1342 struct fc_internal *i;
1344 if (!scsi_is_target_device(dev))
1347 shost = dev_to_shost(dev->parent);
1348 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1349 != &fc_host_class.class)
1352 i = to_fc_internal(shost->transportt);
1354 return &i->t.target_attrs.ac == cont;
1357 static void fc_rport_dev_release(struct device *dev)
1359 struct fc_rport *rport = dev_to_rport(dev);
1360 put_device(dev->parent);
1364 int scsi_is_fc_rport(const struct device *dev)
1366 return dev->release == fc_rport_dev_release;
1368 EXPORT_SYMBOL(scsi_is_fc_rport);
1370 static int fc_rport_match(struct attribute_container *cont,
1373 struct Scsi_Host *shost;
1374 struct fc_internal *i;
1376 if (!scsi_is_fc_rport(dev))
1379 shost = dev_to_shost(dev->parent);
1380 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1381 != &fc_host_class.class)
1384 i = to_fc_internal(shost->transportt);
1386 return &i->rport_attr_cont.ac == cont;
1391 * fc_timed_out - FC Transport I/O timeout intercept handler
1393 * @scmd: The SCSI command which timed out
1395 * This routine protects against error handlers getting invoked while a
1396 * rport is in a blocked state, typically due to a temporarily loss of
1397 * connectivity. If the error handlers are allowed to proceed, requests
1398 * to abort i/o, reset the target, etc will likely fail as there is no way
1399 * to communicate with the device to perform the requested function. These
1400 * failures may result in the midlayer taking the device offline, requiring
1401 * manual intervention to restore operation.
1403 * This routine, called whenever an i/o times out, validates the state of
1404 * the underlying rport. If the rport is blocked, it returns
1405 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1406 * Eventually, either the device will return, or devloss_tmo will fire,
1407 * and when the timeout then fires, it will be handled normally.
1408 * If the rport is not blocked, normal error handling continues.
1411 * This routine assumes no locks are held on entry.
1413 static enum scsi_eh_timer_return
1414 fc_timed_out(struct scsi_cmnd *scmd)
1416 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1418 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1419 return EH_RESET_TIMER;
1421 return EH_NOT_HANDLED;
1425 * Must be called with shost->host_lock held
1427 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1430 struct fc_rport *rport;
1432 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1433 if (rport->scsi_target_id == -1)
1436 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1437 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1438 scsi_scan_target(&rport->dev, rport->channel,
1439 rport->scsi_target_id, lun, 1);
1446 struct scsi_transport_template *
1447 fc_attach_transport(struct fc_function_template *ft)
1450 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1456 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1457 i->t.target_attrs.ac.class = &fc_transport_class.class;
1458 i->t.target_attrs.ac.match = fc_target_match;
1459 i->t.target_size = sizeof(struct fc_starget_attrs);
1460 transport_container_register(&i->t.target_attrs);
1462 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1463 i->t.host_attrs.ac.class = &fc_host_class.class;
1464 i->t.host_attrs.ac.match = fc_host_match;
1465 i->t.host_size = sizeof(struct fc_host_attrs);
1466 if (ft->get_fc_host_stats)
1467 i->t.host_attrs.statistics = &fc_statistics_group;
1468 transport_container_register(&i->t.host_attrs);
1470 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1471 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1472 i->rport_attr_cont.ac.match = fc_rport_match;
1473 transport_container_register(&i->rport_attr_cont);
1477 /* Transport uses the shost workq for scsi scanning */
1478 i->t.create_work_queue = 1;
1480 i->t.eh_timed_out = fc_timed_out;
1482 i->t.user_scan = fc_user_scan;
1485 * Setup SCSI Target Attributes.
1488 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1489 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1490 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1492 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1494 i->starget_attrs[count] = NULL;
1498 * Setup SCSI Host Attributes.
1501 SETUP_HOST_ATTRIBUTE_RD(node_name);
1502 SETUP_HOST_ATTRIBUTE_RD(port_name);
1503 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1504 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1505 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1506 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1507 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1508 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1510 SETUP_HOST_ATTRIBUTE_RD(port_id);
1511 SETUP_HOST_ATTRIBUTE_RD(port_type);
1512 SETUP_HOST_ATTRIBUTE_RD(port_state);
1513 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1514 SETUP_HOST_ATTRIBUTE_RD(speed);
1515 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1516 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1517 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1519 /* Transport-managed attributes */
1520 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1521 if (ft->issue_fc_host_lip)
1522 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1524 BUG_ON(count > FC_HOST_NUM_ATTRS);
1526 i->host_attrs[count] = NULL;
1529 * Setup Remote Port Attributes.
1532 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1533 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1534 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1535 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1536 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1537 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1538 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1539 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1540 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1541 if (ft->terminate_rport_io)
1542 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1544 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1546 i->rport_attrs[count] = NULL;
1550 EXPORT_SYMBOL(fc_attach_transport);
1552 void fc_release_transport(struct scsi_transport_template *t)
1554 struct fc_internal *i = to_fc_internal(t);
1556 transport_container_unregister(&i->t.target_attrs);
1557 transport_container_unregister(&i->t.host_attrs);
1558 transport_container_unregister(&i->rport_attr_cont);
1562 EXPORT_SYMBOL(fc_release_transport);
1565 * fc_queue_work - Queue work to the fc_host workqueue.
1566 * @shost: Pointer to Scsi_Host bound to fc_host.
1567 * @work: Work to queue for execution.
1570 * 1 - work queued for execution
1571 * 0 - work is already queued
1572 * -EINVAL - work queue doesn't exist
1575 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1577 if (unlikely(!fc_host_work_q(shost))) {
1579 "ERROR: FC host '%s' attempted to queue work, "
1580 "when no workqueue created.\n", shost->hostt->name);
1586 return queue_work(fc_host_work_q(shost), work);
1590 * fc_flush_work - Flush a fc_host's workqueue.
1591 * @shost: Pointer to Scsi_Host bound to fc_host.
1594 fc_flush_work(struct Scsi_Host *shost)
1596 if (!fc_host_work_q(shost)) {
1598 "ERROR: FC host '%s' attempted to flush work, "
1599 "when no workqueue created.\n", shost->hostt->name);
1604 flush_workqueue(fc_host_work_q(shost));
1608 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1609 * @shost: Pointer to Scsi_Host bound to fc_host.
1610 * @work: Work to queue for execution.
1611 * @delay: jiffies to delay the work queuing
1614 * 1 on success / 0 already queued / < 0 for error
1617 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
1618 unsigned long delay)
1620 if (unlikely(!fc_host_devloss_work_q(shost))) {
1622 "ERROR: FC host '%s' attempted to queue work, "
1623 "when no workqueue created.\n", shost->hostt->name);
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 * @work: remote port to be operated on.
1716 fc_starget_delete(struct work_struct *work)
1718 struct fc_rport *rport =
1719 container_of(work, struct fc_rport, stgt_delete_work);
1720 struct Scsi_Host *shost = rport_to_shost(rport);
1721 unsigned long flags;
1722 struct fc_internal *i = to_fc_internal(shost->transportt);
1725 * Involve the LLDD if possible. All io on the rport is to
1726 * be terminated, either as part of the dev_loss_tmo callback
1727 * processing, or via the terminate_rport_io function.
1729 if (i->f->dev_loss_tmo_callbk)
1730 i->f->dev_loss_tmo_callbk(rport);
1731 else if (i->f->terminate_rport_io)
1732 i->f->terminate_rport_io(rport);
1734 spin_lock_irqsave(shost->host_lock, flags);
1735 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
1736 spin_unlock_irqrestore(shost->host_lock, flags);
1737 if (!cancel_delayed_work(&rport->fail_io_work))
1738 fc_flush_devloss(shost);
1739 if (!cancel_delayed_work(&rport->dev_loss_work))
1740 fc_flush_devloss(shost);
1741 spin_lock_irqsave(shost->host_lock, flags);
1742 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1744 spin_unlock_irqrestore(shost->host_lock, flags);
1746 scsi_remove_target(&rport->dev);
1751 * fc_rport_final_delete - finish rport termination and delete it.
1753 * @work: remote port to be deleted.
1756 fc_rport_final_delete(struct work_struct *work)
1758 struct fc_rport *rport =
1759 container_of(work, struct fc_rport, rport_delete_work);
1760 struct device *dev = &rport->dev;
1761 struct Scsi_Host *shost = rport_to_shost(rport);
1762 struct fc_internal *i = to_fc_internal(shost->transportt);
1765 * if a scan is pending, flush the SCSI Host work_q so that
1766 * that we can reclaim the rport scan work element.
1768 if (rport->flags & FC_RPORT_SCAN_PENDING)
1769 scsi_flush_work(shost);
1771 /* Delete SCSI target and sdevs */
1772 if (rport->scsi_target_id != -1)
1773 fc_starget_delete(&rport->stgt_delete_work);
1774 else if (i->f->dev_loss_tmo_callbk)
1775 i->f->dev_loss_tmo_callbk(rport);
1776 else if (i->f->terminate_rport_io)
1777 i->f->terminate_rport_io(rport);
1779 transport_remove_device(dev);
1781 transport_destroy_device(dev);
1782 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1783 put_device(dev); /* for self-reference */
1788 * fc_rport_create - allocates and creates a remote FC port.
1789 * @shost: scsi host the remote port is connected to.
1790 * @channel: Channel on shost port connected to.
1791 * @ids: The world wide names, fc address, and FC4 port
1792 * roles for the remote port.
1794 * Allocates and creates the remoter port structure, including the
1795 * class and sysfs creation.
1798 * This routine assumes no locks are held on entry.
1801 fc_rport_create(struct Scsi_Host *shost, int channel,
1802 struct fc_rport_identifiers *ids)
1804 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1805 struct fc_internal *fci = to_fc_internal(shost->transportt);
1806 struct fc_rport *rport;
1808 unsigned long flags;
1812 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1813 rport = kzalloc(size, GFP_KERNEL);
1814 if (unlikely(!rport)) {
1815 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1819 rport->maxframe_size = -1;
1820 rport->supported_classes = FC_COS_UNSPECIFIED;
1821 rport->dev_loss_tmo = fc_dev_loss_tmo;
1822 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1823 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1824 rport->port_id = ids->port_id;
1825 rport->roles = ids->roles;
1826 rport->port_state = FC_PORTSTATE_ONLINE;
1827 if (fci->f->dd_fcrport_size)
1828 rport->dd_data = &rport[1];
1829 rport->channel = channel;
1830 rport->fast_io_fail_tmo = -1;
1832 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
1833 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
1834 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
1835 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
1836 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
1838 spin_lock_irqsave(shost->host_lock, flags);
1840 rport->number = fc_host->next_rport_number++;
1841 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1842 rport->scsi_target_id = fc_host->next_target_id++;
1844 rport->scsi_target_id = -1;
1845 list_add_tail(&rport->peers, &fc_host->rports);
1846 get_device(&shost->shost_gendev); /* for fc_host->rport list */
1848 spin_unlock_irqrestore(shost->host_lock, flags);
1851 device_initialize(dev); /* takes self reference */
1852 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1853 dev->release = fc_rport_dev_release;
1854 sprintf(dev->bus_id, "rport-%d:%d-%d",
1855 shost->host_no, channel, rport->number);
1856 transport_setup_device(dev);
1858 error = device_add(dev);
1860 printk(KERN_ERR "FC Remote Port device_add failed\n");
1863 transport_add_device(dev);
1864 transport_configure_device(dev);
1866 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1867 /* initiate a scan of the target */
1868 rport->flags |= FC_RPORT_SCAN_PENDING;
1869 scsi_queue_work(shost, &rport->scan_work);
1875 transport_destroy_device(dev);
1876 spin_lock_irqsave(shost->host_lock, flags);
1877 list_del(&rport->peers);
1878 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1879 spin_unlock_irqrestore(shost->host_lock, flags);
1880 put_device(dev->parent);
1886 * fc_remote_port_add - notifies the fc transport of the existence
1887 * of a remote FC port.
1888 * @shost: scsi host the remote port is connected to.
1889 * @channel: Channel on shost port connected to.
1890 * @ids: The world wide names, fc address, and FC4 port
1891 * roles for the remote port.
1893 * The LLDD calls this routine to notify the transport of the existence
1894 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1895 * of the port, it's FC address (port_id), and the FC4 roles that are
1896 * active for the port.
1898 * For ports that are FCP targets (aka scsi targets), the FC transport
1899 * maintains consistent target id bindings on behalf of the LLDD.
1900 * A consistent target id binding is an assignment of a target id to
1901 * a remote port identifier, which persists while the scsi host is
1902 * attached. The remote port can disappear, then later reappear, and
1903 * it's target id assignment remains the same. This allows for shifts
1904 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1905 * changes to the scsi subsystem which is based on scsi host number and
1906 * target id values. Bindings are only valid during the attachment of
1907 * the scsi host. If the host detaches, then later re-attaches, target
1908 * id bindings may change.
1910 * This routine is responsible for returning a remote port structure.
1911 * The routine will search the list of remote ports it maintains
1912 * internally on behalf of consistent target id mappings. If found, the
1913 * remote port structure will be reused. Otherwise, a new remote port
1914 * structure will be allocated.
1916 * Whenever a remote port is allocated, a new fc_remote_port class
1917 * device is created.
1919 * Should not be called from interrupt context.
1922 * This routine assumes no locks are held on entry.
1925 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1926 struct fc_rport_identifiers *ids)
1928 struct fc_internal *fci = to_fc_internal(shost->transportt);
1929 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1930 struct fc_rport *rport;
1931 unsigned long flags;
1934 /* ensure any stgt delete functions are done */
1935 fc_flush_work(shost);
1938 * Search the list of "active" rports, for an rport that has been
1939 * deleted, but we've held off the real delete while the target
1940 * is in a "blocked" state.
1942 spin_lock_irqsave(shost->host_lock, flags);
1944 list_for_each_entry(rport, &fc_host->rports, peers) {
1946 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1947 (rport->channel == channel)) {
1949 switch (fc_host->tgtid_bind_type) {
1950 case FC_TGTID_BIND_BY_WWPN:
1951 case FC_TGTID_BIND_NONE:
1952 if (rport->port_name == ids->port_name)
1955 case FC_TGTID_BIND_BY_WWNN:
1956 if (rport->node_name == ids->node_name)
1959 case FC_TGTID_BIND_BY_ID:
1960 if (rport->port_id == ids->port_id)
1966 struct delayed_work *work =
1967 &rport->dev_loss_work;
1969 memcpy(&rport->node_name, &ids->node_name,
1970 sizeof(rport->node_name));
1971 memcpy(&rport->port_name, &ids->port_name,
1972 sizeof(rport->port_name));
1973 rport->port_id = ids->port_id;
1975 rport->port_state = FC_PORTSTATE_ONLINE;
1976 rport->roles = ids->roles;
1978 spin_unlock_irqrestore(shost->host_lock, flags);
1980 if (fci->f->dd_fcrport_size)
1981 memset(rport->dd_data, 0,
1982 fci->f->dd_fcrport_size);
1985 * If we were blocked, we were a target.
1986 * If no longer a target, we leave the timer
1987 * running in case the port changes roles
1988 * prior to the timer expiring. If the timer
1989 * fires, the target will be torn down.
1991 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1994 /* restart the target */
1997 * Stop the target timers first. Take no action
1998 * on the del_timer failure as the state
1999 * machine state change will validate the
2002 if (!cancel_delayed_work(&rport->fail_io_work))
2003 fc_flush_devloss(shost);
2004 if (!cancel_delayed_work(work))
2005 fc_flush_devloss(shost);
2007 spin_lock_irqsave(shost->host_lock, flags);
2009 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2011 /* initiate a scan of the target */
2012 rport->flags |= FC_RPORT_SCAN_PENDING;
2013 scsi_queue_work(shost, &rport->scan_work);
2015 spin_unlock_irqrestore(shost->host_lock, flags);
2017 scsi_target_unblock(&rport->dev);
2024 /* Search the bindings array */
2025 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2027 /* search for a matching consistent binding */
2029 list_for_each_entry(rport, &fc_host->rport_bindings,
2031 if (rport->channel != channel)
2034 switch (fc_host->tgtid_bind_type) {
2035 case FC_TGTID_BIND_BY_WWPN:
2036 if (rport->port_name == ids->port_name)
2039 case FC_TGTID_BIND_BY_WWNN:
2040 if (rport->node_name == ids->node_name)
2043 case FC_TGTID_BIND_BY_ID:
2044 if (rport->port_id == ids->port_id)
2047 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2052 list_move_tail(&rport->peers, &fc_host->rports);
2058 memcpy(&rport->node_name, &ids->node_name,
2059 sizeof(rport->node_name));
2060 memcpy(&rport->port_name, &ids->port_name,
2061 sizeof(rport->port_name));
2062 rport->port_id = ids->port_id;
2063 rport->roles = ids->roles;
2064 rport->port_state = FC_PORTSTATE_ONLINE;
2066 if (fci->f->dd_fcrport_size)
2067 memset(rport->dd_data, 0,
2068 fci->f->dd_fcrport_size);
2070 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
2071 /* initiate a scan of the target */
2072 rport->flags |= FC_RPORT_SCAN_PENDING;
2073 scsi_queue_work(shost, &rport->scan_work);
2074 spin_unlock_irqrestore(shost->host_lock, flags);
2075 scsi_target_unblock(&rport->dev);
2077 spin_unlock_irqrestore(shost->host_lock, flags);
2083 spin_unlock_irqrestore(shost->host_lock, flags);
2085 /* No consistent binding found - create new remote port entry */
2086 rport = fc_rport_create(shost, channel, ids);
2090 EXPORT_SYMBOL(fc_remote_port_add);
2094 * fc_remote_port_delete - notifies the fc transport that a remote
2095 * port is no longer in existence.
2096 * @rport: The remote port that no longer exists
2098 * The LLDD calls this routine to notify the transport that a remote
2099 * port is no longer part of the topology. Note: Although a port
2100 * may no longer be part of the topology, it may persist in the remote
2101 * ports displayed by the fc_host. We do this under 2 conditions:
2102 * - If the port was a scsi target, we delay its deletion by "blocking" it.
2103 * This allows the port to temporarily disappear, then reappear without
2104 * disrupting the SCSI device tree attached to it. During the "blocked"
2105 * period the port will still exist.
2106 * - If the port was a scsi target and disappears for longer than we
2107 * expect, we'll delete the port and the tear down the SCSI device tree
2108 * attached to it. However, we want to semi-persist the target id assigned
2109 * to that port if it eventually does exist. The port structure will
2110 * remain (although with minimal information) so that the target id
2113 * If the remote port is not an FCP Target, it will be fully torn down
2114 * and deallocated, including the fc_remote_port class device.
2116 * If the remote port is an FCP Target, the port will be placed in a
2117 * temporary blocked state. From the LLDD's perspective, the rport no
2118 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2119 * exists, but all sdevs on it are blocked from further I/O. The following
2121 * If the remote port does not return (signaled by a LLDD call to
2122 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2123 * scsi target is removed - killing all outstanding i/o and removing the
2124 * scsi devices attached ot it. The port structure will be marked Not
2125 * Present and be partially cleared, leaving only enough information to
2126 * recognize the remote port relative to the scsi target id binding if
2127 * it later appears. The port will remain as long as there is a valid
2128 * binding (e.g. until the user changes the binding type or unloads the
2129 * scsi host with the binding).
2131 * If the remote port returns within the dev_loss_tmo value (and matches
2132 * according to the target id binding type), the port structure will be
2133 * reused. If it is no longer a SCSI target, the target will be torn
2134 * down. If it continues to be a SCSI target, then the target will be
2135 * unblocked (allowing i/o to be resumed), and a scan will be activated
2136 * to ensure that all luns are detected.
2138 * Called from normal process context only - cannot be called from interrupt.
2141 * This routine assumes no locks are held on entry.
2144 fc_remote_port_delete(struct fc_rport *rport)
2146 struct Scsi_Host *shost = rport_to_shost(rport);
2147 struct fc_internal *i = to_fc_internal(shost->transportt);
2148 int timeout = rport->dev_loss_tmo;
2149 unsigned long flags;
2152 * No need to flush the fc_host work_q's, as all adds are synchronous.
2154 * We do need to reclaim the rport scan work element, so eventually
2155 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2156 * there's still a scan pending.
2159 spin_lock_irqsave(shost->host_lock, flags);
2161 /* If no scsi target id mapping, delete it */
2162 if (rport->scsi_target_id == -1) {
2163 list_del(&rport->peers);
2164 rport->port_state = FC_PORTSTATE_DELETED;
2165 fc_queue_work(shost, &rport->rport_delete_work);
2166 spin_unlock_irqrestore(shost->host_lock, flags);
2170 rport->port_state = FC_PORTSTATE_BLOCKED;
2172 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2174 spin_unlock_irqrestore(shost->host_lock, flags);
2176 scsi_target_block(&rport->dev);
2178 /* see if we need to kill io faster than waiting for device loss */
2179 if ((rport->fast_io_fail_tmo != -1) &&
2180 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2181 fc_queue_devloss_work(shost, &rport->fail_io_work,
2182 rport->fast_io_fail_tmo * HZ);
2184 /* cap the length the devices can be blocked until they are deleted */
2185 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2187 EXPORT_SYMBOL(fc_remote_port_delete);
2190 * fc_remote_port_rolechg - notifies the fc transport that the roles
2191 * on a remote may have changed.
2192 * @rport: The remote port that changed.
2194 * The LLDD calls this routine to notify the transport that the roles
2195 * on a remote port may have changed. The largest effect of this is
2196 * if a port now becomes a FCP Target, it must be allocated a
2197 * scsi target id. If the port is no longer a FCP target, any
2198 * scsi target id value assigned to it will persist in case the
2199 * role changes back to include FCP Target. No changes in the scsi
2200 * midlayer will be invoked if the role changes (in the expectation
2201 * that the role will be resumed. If it doesn't normal error processing
2204 * Should not be called from interrupt context.
2207 * This routine assumes no locks are held on entry.
2210 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2212 struct Scsi_Host *shost = rport_to_shost(rport);
2213 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2214 unsigned long flags;
2217 spin_lock_irqsave(shost->host_lock, flags);
2218 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
2219 if (rport->scsi_target_id == -1) {
2220 rport->scsi_target_id = fc_host->next_target_id++;
2222 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
2226 rport->roles = roles;
2228 spin_unlock_irqrestore(shost->host_lock, flags);
2232 * There may have been a delete timer running on the
2233 * port. Ensure that it is cancelled as we now know
2234 * the port is an FCP Target.
2235 * Note: we know the rport is exists and in an online
2236 * state as the LLDD would not have had an rport
2237 * reference to pass us.
2239 * Take no action on the del_timer failure as the state
2240 * machine state change will validate the
2243 if (!cancel_delayed_work(&rport->fail_io_work))
2244 fc_flush_devloss(shost);
2245 if (!cancel_delayed_work(&rport->dev_loss_work))
2246 fc_flush_devloss(shost);
2248 spin_lock_irqsave(shost->host_lock, flags);
2249 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2250 spin_unlock_irqrestore(shost->host_lock, flags);
2252 /* ensure any stgt delete functions are done */
2253 fc_flush_work(shost);
2255 /* initiate a scan of the target */
2256 spin_lock_irqsave(shost->host_lock, flags);
2257 rport->flags |= FC_RPORT_SCAN_PENDING;
2258 scsi_queue_work(shost, &rport->scan_work);
2259 spin_unlock_irqrestore(shost->host_lock, flags);
2260 scsi_target_unblock(&rport->dev);
2263 EXPORT_SYMBOL(fc_remote_port_rolechg);
2266 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
2267 * was a SCSI target (thus was blocked), and failed
2268 * to return in the alloted time.
2270 * @work: rport target that failed to reappear in the alloted time.
2273 fc_timeout_deleted_rport(struct work_struct *work)
2275 struct fc_rport *rport =
2276 container_of(work, struct fc_rport, dev_loss_work.work);
2277 struct Scsi_Host *shost = rport_to_shost(rport);
2278 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2279 unsigned long flags;
2281 spin_lock_irqsave(shost->host_lock, flags);
2283 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2286 * If the port is ONLINE, then it came back. Validate it's still an
2287 * FCP target. If not, tear down the scsi_target on it.
2289 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2290 !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2291 dev_printk(KERN_ERR, &rport->dev,
2292 "blocked FC remote port time out: no longer"
2293 " a FCP target, removing starget\n");
2294 spin_unlock_irqrestore(shost->host_lock, flags);
2295 scsi_target_unblock(&rport->dev);
2296 fc_queue_work(shost, &rport->stgt_delete_work);
2300 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2301 spin_unlock_irqrestore(shost->host_lock, flags);
2302 dev_printk(KERN_ERR, &rport->dev,
2303 "blocked FC remote port time out: leaving target alone\n");
2307 if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
2308 list_del(&rport->peers);
2309 rport->port_state = FC_PORTSTATE_DELETED;
2310 dev_printk(KERN_ERR, &rport->dev,
2311 "blocked FC remote port time out: removing target\n");
2312 fc_queue_work(shost, &rport->rport_delete_work);
2313 spin_unlock_irqrestore(shost->host_lock, flags);
2317 dev_printk(KERN_ERR, &rport->dev,
2318 "blocked FC remote port time out: removing target and "
2319 "saving binding\n");
2321 list_move_tail(&rport->peers, &fc_host->rport_bindings);
2324 * Note: We do not remove or clear the hostdata area. This allows
2325 * host-specific target data to persist along with the
2326 * scsi_target_id. It's up to the host to manage it's hostdata area.
2330 * Reinitialize port attributes that may change if the port comes back.
2332 rport->maxframe_size = -1;
2333 rport->supported_classes = FC_COS_UNSPECIFIED;
2334 rport->roles = FC_RPORT_ROLE_UNKNOWN;
2335 rport->port_state = FC_PORTSTATE_NOTPRESENT;
2337 /* remove the identifiers that aren't used in the consisting binding */
2338 switch (fc_host->tgtid_bind_type) {
2339 case FC_TGTID_BIND_BY_WWPN:
2340 rport->node_name = -1;
2341 rport->port_id = -1;
2343 case FC_TGTID_BIND_BY_WWNN:
2344 rport->port_name = -1;
2345 rport->port_id = -1;
2347 case FC_TGTID_BIND_BY_ID:
2348 rport->node_name = -1;
2349 rport->port_name = -1;
2351 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2356 * As this only occurs if the remote port (scsi target)
2357 * went away and didn't come back - we'll remove
2358 * all attached scsi devices.
2360 spin_unlock_irqrestore(shost->host_lock, flags);
2362 scsi_target_unblock(&rport->dev);
2363 fc_queue_work(shost, &rport->stgt_delete_work);
2367 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2368 * disconnected SCSI target.
2370 * @work: rport to terminate io on.
2372 * Notes: Only requests the failure of the io, not that all are flushed
2373 * prior to returning.
2376 fc_timeout_fail_rport_io(struct work_struct *work)
2378 struct fc_rport *rport =
2379 container_of(work, struct fc_rport, fail_io_work.work);
2380 struct Scsi_Host *shost = rport_to_shost(rport);
2381 struct fc_internal *i = to_fc_internal(shost->transportt);
2383 if (rport->port_state != FC_PORTSTATE_BLOCKED)
2386 i->f->terminate_rport_io(rport);
2390 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2392 * @work: remote port to be scanned.
2395 fc_scsi_scan_rport(struct work_struct *work)
2397 struct fc_rport *rport =
2398 container_of(work, struct fc_rport, scan_work);
2399 struct Scsi_Host *shost = rport_to_shost(rport);
2400 unsigned long flags;
2402 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2403 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2404 scsi_scan_target(&rport->dev, rport->channel,
2405 rport->scsi_target_id, SCAN_WILD_CARD, 1);
2408 spin_lock_irqsave(shost->host_lock, flags);
2409 rport->flags &= ~FC_RPORT_SCAN_PENDING;
2410 spin_unlock_irqrestore(shost->host_lock, flags);
2414 MODULE_AUTHOR("Martin Hicks");
2415 MODULE_DESCRIPTION("FC Transport Attributes");
2416 MODULE_LICENSE("GPL");
2418 module_init(fc_transport_init);
2419 module_exit(fc_transport_exit);