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-2007 James Smart, Emulex Corporation
23 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
28 #include <linux/module.h>
29 #include <linux/init.h>
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/scsi_bsg_fc.h>
39 #include "scsi_priv.h"
40 #include "scsi_transport_fc_internal.h"
42 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
43 static void fc_vport_sched_delete(struct work_struct *work);
44 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
45 struct device *pdev, struct fc_vport_identifiers *ids,
46 struct fc_vport **vport);
47 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
48 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
49 static void fc_bsg_remove(struct request_queue *);
50 static void fc_bsg_goose_queue(struct fc_rport *);
53 * Redefine so that we can have same named attributes in the
54 * sdev/starget/host objects.
56 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct device_attribute device_attr_##_prefix##_##_name = \
58 __ATTR(_name,_mode,_show,_store)
60 #define fc_enum_name_search(title, table_type, table) \
61 static const char *get_fc_##title##_name(enum table_type table_key) \
66 for (i = 0; i < ARRAY_SIZE(table); i++) { \
67 if (table[i].value == table_key) { \
68 name = table[i].name; \
75 #define fc_enum_name_match(title, table_type, table) \
76 static int get_fc_##title##_match(const char *table_key, \
77 enum table_type *value) \
81 for (i = 0; i < ARRAY_SIZE(table); i++) { \
82 if (strncmp(table_key, table[i].name, \
83 table[i].matchlen) == 0) { \
84 *value = table[i].value; \
85 return 0; /* success */ \
88 return 1; /* failure */ \
92 /* Convert fc_port_type values to ascii string name */
94 enum fc_port_type value;
96 } fc_port_type_names[] = {
97 { FC_PORTTYPE_UNKNOWN, "Unknown" },
98 { FC_PORTTYPE_OTHER, "Other" },
99 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
100 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
101 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
102 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
103 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
104 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
106 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
107 #define FC_PORTTYPE_MAX_NAMELEN 50
109 /* Reuse fc_port_type enum function for vport_type */
110 #define get_fc_vport_type_name get_fc_port_type_name
113 /* Convert fc_host_event_code values to ascii string name */
114 static const struct {
115 enum fc_host_event_code value;
117 } fc_host_event_code_names[] = {
118 { FCH_EVT_LIP, "lip" },
119 { FCH_EVT_LINKUP, "link_up" },
120 { FCH_EVT_LINKDOWN, "link_down" },
121 { FCH_EVT_LIPRESET, "lip_reset" },
122 { FCH_EVT_RSCN, "rscn" },
123 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
124 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
125 { FCH_EVT_PORT_ONLINE, "port_online" },
126 { FCH_EVT_PORT_OFFLINE, "port_offline" },
127 { FCH_EVT_PORT_FABRIC, "port_fabric" },
128 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
129 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
131 fc_enum_name_search(host_event_code, fc_host_event_code,
132 fc_host_event_code_names)
133 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
136 /* Convert fc_port_state values to ascii string name */
138 enum fc_port_state value;
140 } fc_port_state_names[] = {
141 { FC_PORTSTATE_UNKNOWN, "Unknown" },
142 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
143 { FC_PORTSTATE_ONLINE, "Online" },
144 { FC_PORTSTATE_OFFLINE, "Offline" },
145 { FC_PORTSTATE_BLOCKED, "Blocked" },
146 { FC_PORTSTATE_BYPASSED, "Bypassed" },
147 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
148 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
149 { FC_PORTSTATE_ERROR, "Error" },
150 { FC_PORTSTATE_LOOPBACK, "Loopback" },
151 { FC_PORTSTATE_DELETED, "Deleted" },
153 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
154 #define FC_PORTSTATE_MAX_NAMELEN 20
157 /* Convert fc_vport_state values to ascii string name */
159 enum fc_vport_state value;
161 } fc_vport_state_names[] = {
162 { FC_VPORT_UNKNOWN, "Unknown" },
163 { FC_VPORT_ACTIVE, "Active" },
164 { FC_VPORT_DISABLED, "Disabled" },
165 { FC_VPORT_LINKDOWN, "Linkdown" },
166 { FC_VPORT_INITIALIZING, "Initializing" },
167 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
168 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
169 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
170 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
171 { FC_VPORT_FAILED, "VPort Failed" },
173 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
174 #define FC_VPORTSTATE_MAX_NAMELEN 24
176 /* Reuse fc_vport_state enum function for vport_last_state */
177 #define get_fc_vport_last_state_name get_fc_vport_state_name
180 /* Convert fc_tgtid_binding_type values to ascii string name */
181 static const struct {
182 enum fc_tgtid_binding_type value;
185 } fc_tgtid_binding_type_names[] = {
186 { FC_TGTID_BIND_NONE, "none", 4 },
187 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
188 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
189 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
191 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
192 fc_tgtid_binding_type_names)
193 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
194 fc_tgtid_binding_type_names)
195 #define FC_BINDTYPE_MAX_NAMELEN 30
198 #define fc_bitfield_name_search(title, table) \
200 get_fc_##title##_names(u32 table_key, char *buf) \
206 for (i = 0; i < ARRAY_SIZE(table); i++) { \
207 if (table[i].value & table_key) { \
208 len += sprintf(buf + len, "%s%s", \
209 prefix, table[i].name); \
213 len += sprintf(buf + len, "\n"); \
218 /* Convert FC_COS bit values to ascii string name */
219 static const struct {
223 { FC_COS_CLASS1, "Class 1" },
224 { FC_COS_CLASS2, "Class 2" },
225 { FC_COS_CLASS3, "Class 3" },
226 { FC_COS_CLASS4, "Class 4" },
227 { FC_COS_CLASS6, "Class 6" },
229 fc_bitfield_name_search(cos, fc_cos_names)
232 /* Convert FC_PORTSPEED bit values to ascii string name */
233 static const struct {
236 } fc_port_speed_names[] = {
237 { FC_PORTSPEED_1GBIT, "1 Gbit" },
238 { FC_PORTSPEED_2GBIT, "2 Gbit" },
239 { FC_PORTSPEED_4GBIT, "4 Gbit" },
240 { FC_PORTSPEED_10GBIT, "10 Gbit" },
241 { FC_PORTSPEED_8GBIT, "8 Gbit" },
242 { FC_PORTSPEED_16GBIT, "16 Gbit" },
243 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
245 fc_bitfield_name_search(port_speed, fc_port_speed_names)
249 show_fc_fc4s (char *buf, u8 *fc4_list)
253 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
254 len += sprintf(buf + len , "0x%02x ", *fc4_list);
255 len += sprintf(buf + len, "\n");
260 /* Convert FC_PORT_ROLE bit values to ascii string name */
261 static const struct {
264 } fc_port_role_names[] = {
265 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
266 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
267 { FC_PORT_ROLE_IP_PORT, "IP Port" },
269 fc_bitfield_name_search(port_roles, fc_port_role_names)
272 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
274 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
275 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
276 #define FC_FPORT_PORTID 0x00000e
277 #define FC_FABCTLR_PORTID 0x00000d
278 #define FC_DIRSRVR_PORTID 0x00000c
279 #define FC_TIMESRVR_PORTID 0x00000b
280 #define FC_MGMTSRVR_PORTID 0x00000a
283 static void fc_timeout_deleted_rport(struct work_struct *work);
284 static void fc_timeout_fail_rport_io(struct work_struct *work);
285 static void fc_scsi_scan_rport(struct work_struct *work);
288 * Attribute counts pre object type...
289 * Increase these values if you add attributes
291 #define FC_STARGET_NUM_ATTRS 3
292 #define FC_RPORT_NUM_ATTRS 10
293 #define FC_VPORT_NUM_ATTRS 9
294 #define FC_HOST_NUM_ATTRS 22
297 struct scsi_transport_template t;
298 struct fc_function_template *f;
301 * For attributes : each object has :
302 * An array of the actual attributes structures
303 * An array of null-terminated pointers to the attribute
304 * structures - used for mid-layer interaction.
306 * The attribute containers for the starget and host are are
307 * part of the midlayer. As the remote port is specific to the
308 * fc transport, we must provide the attribute container.
310 struct device_attribute private_starget_attrs[
311 FC_STARGET_NUM_ATTRS];
312 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
314 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
315 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
317 struct transport_container rport_attr_cont;
318 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
319 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
321 struct transport_container vport_attr_cont;
322 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
323 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
326 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
328 static int fc_target_setup(struct transport_container *tc, struct device *dev,
331 struct scsi_target *starget = to_scsi_target(dev);
332 struct fc_rport *rport = starget_to_rport(starget);
335 * if parent is remote port, use values from remote port.
336 * Otherwise, this host uses the fc_transport, but not the
337 * remote port interface. As such, initialize to known non-values.
340 fc_starget_node_name(starget) = rport->node_name;
341 fc_starget_port_name(starget) = rport->port_name;
342 fc_starget_port_id(starget) = rport->port_id;
344 fc_starget_node_name(starget) = -1;
345 fc_starget_port_name(starget) = -1;
346 fc_starget_port_id(starget) = -1;
352 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
358 static int fc_host_setup(struct transport_container *tc, struct device *dev,
361 struct Scsi_Host *shost = dev_to_shost(dev);
362 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
365 * Set default values easily detected by the midlayer as
366 * failure cases. The scsi lldd is responsible for initializing
367 * all transport attributes to valid values per host.
369 fc_host->node_name = -1;
370 fc_host->port_name = -1;
371 fc_host->permanent_port_name = -1;
372 fc_host->supported_classes = FC_COS_UNSPECIFIED;
373 memset(fc_host->supported_fc4s, 0,
374 sizeof(fc_host->supported_fc4s));
375 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
376 fc_host->maxframe_size = -1;
377 fc_host->max_npiv_vports = 0;
378 memset(fc_host->serial_number, 0,
379 sizeof(fc_host->serial_number));
381 fc_host->port_id = -1;
382 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
383 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
384 memset(fc_host->active_fc4s, 0,
385 sizeof(fc_host->active_fc4s));
386 fc_host->speed = FC_PORTSPEED_UNKNOWN;
387 fc_host->fabric_name = -1;
388 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
389 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
391 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
393 INIT_LIST_HEAD(&fc_host->rports);
394 INIT_LIST_HEAD(&fc_host->rport_bindings);
395 INIT_LIST_HEAD(&fc_host->vports);
396 fc_host->next_rport_number = 0;
397 fc_host->next_target_id = 0;
398 fc_host->next_vport_number = 0;
399 fc_host->npiv_vports_inuse = 0;
401 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
402 "fc_wq_%d", shost->host_no);
403 fc_host->work_q = create_singlethread_workqueue(
404 fc_host->work_q_name);
405 if (!fc_host->work_q)
408 snprintf(fc_host->devloss_work_q_name,
409 sizeof(fc_host->devloss_work_q_name),
410 "fc_dl_%d", shost->host_no);
411 fc_host->devloss_work_q = create_singlethread_workqueue(
412 fc_host->devloss_work_q_name);
413 if (!fc_host->devloss_work_q) {
414 destroy_workqueue(fc_host->work_q);
415 fc_host->work_q = NULL;
419 fc_bsg_hostadd(shost, fc_host);
420 /* ignore any bsg add error - we just can't do sgio */
425 static int fc_host_remove(struct transport_container *tc, struct device *dev,
428 struct Scsi_Host *shost = dev_to_shost(dev);
429 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
431 fc_bsg_remove(fc_host->rqst_q);
435 static DECLARE_TRANSPORT_CLASS(fc_host_class,
442 * Setup and Remove actions for remote ports are handled
443 * in the service functions below.
445 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
452 * Setup and Remove actions for virtual ports are handled
453 * in the service functions below.
455 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
466 * dev_loss_tmo: the default number of seconds that the FC transport
467 * should insulate the loss of a remote port.
468 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
470 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
472 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
473 MODULE_PARM_DESC(dev_loss_tmo,
474 "Maximum number of seconds that the FC transport should"
475 " insulate the loss of a remote port. Once this value is"
476 " exceeded, the scsi target is removed. Value should be"
477 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
480 * Netlink Infrastructure
483 static atomic_t fc_event_seq;
486 * fc_get_event_number - Obtain the next sequential FC event number
489 * We could have inlined this, but it would have required fc_event_seq to
490 * be exposed. For now, live with the subroutine call.
491 * Atomic used to avoid lock/unlock...
494 fc_get_event_number(void)
496 return atomic_add_return(1, &fc_event_seq);
498 EXPORT_SYMBOL(fc_get_event_number);
502 * fc_host_post_event - called to post an even on an fc_host.
503 * @shost: host the event occurred on
504 * @event_number: fc event number obtained from get_fc_event_number()
505 * @event_code: fc_host event being posted
506 * @event_data: 32bits of data for the event being posted
509 * This routine assumes no locks are held on entry.
512 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
513 enum fc_host_event_code event_code, u32 event_data)
516 struct nlmsghdr *nlh;
517 struct fc_nl_event *event;
527 len = FC_NL_MSGALIGN(sizeof(*event));
528 skblen = NLMSG_SPACE(len);
530 skb = alloc_skb(skblen, GFP_KERNEL);
536 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
537 skblen - sizeof(*nlh), 0);
542 event = NLMSG_DATA(nlh);
544 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
545 FC_NL_ASYNC_EVENT, len);
546 event->seconds = get_seconds();
547 event->vendor_id = 0;
548 event->host_no = shost->host_no;
549 event->event_datalen = sizeof(u32); /* bytes */
550 event->event_num = event_number;
551 event->event_code = event_code;
552 event->event_data = event_data;
554 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
561 name = get_fc_host_event_code_name(event_code);
563 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
564 __func__, shost->host_no,
565 (name) ? name : "<unknown>", event_data, err);
568 EXPORT_SYMBOL(fc_host_post_event);
572 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
573 * @shost: host the event occurred on
574 * @event_number: fc event number obtained from get_fc_event_number()
575 * @data_len: amount, in bytes, of vendor unique data
576 * @data_buf: pointer to vendor unique data
577 * @vendor_id: Vendor id
580 * This routine assumes no locks are held on entry.
583 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
584 u32 data_len, char * data_buf, u64 vendor_id)
587 struct nlmsghdr *nlh;
588 struct fc_nl_event *event;
594 goto send_vendor_fail;
597 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
598 skblen = NLMSG_SPACE(len);
600 skb = alloc_skb(skblen, GFP_KERNEL);
603 goto send_vendor_fail;
606 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
607 skblen - sizeof(*nlh), 0);
610 goto send_vendor_fail_skb;
612 event = NLMSG_DATA(nlh);
614 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
615 FC_NL_ASYNC_EVENT, len);
616 event->seconds = get_seconds();
617 event->vendor_id = vendor_id;
618 event->host_no = shost->host_no;
619 event->event_datalen = data_len; /* bytes */
620 event->event_num = event_number;
621 event->event_code = FCH_EVT_VENDOR_UNIQUE;
622 memcpy(&event->event_data, data_buf, data_len);
624 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
628 send_vendor_fail_skb:
632 "%s: Dropped Event : host %d vendor_unique - err %d\n",
633 __func__, shost->host_no, err);
636 EXPORT_SYMBOL(fc_host_post_vendor_event);
640 static __init int fc_transport_init(void)
644 atomic_set(&fc_event_seq, 0);
646 error = transport_class_register(&fc_host_class);
649 error = transport_class_register(&fc_vport_class);
651 goto unreg_host_class;
652 error = transport_class_register(&fc_rport_class);
654 goto unreg_vport_class;
655 error = transport_class_register(&fc_transport_class);
657 goto unreg_rport_class;
661 transport_class_unregister(&fc_rport_class);
663 transport_class_unregister(&fc_vport_class);
665 transport_class_unregister(&fc_host_class);
669 static void __exit fc_transport_exit(void)
671 transport_class_unregister(&fc_transport_class);
672 transport_class_unregister(&fc_rport_class);
673 transport_class_unregister(&fc_host_class);
674 transport_class_unregister(&fc_vport_class);
678 * FC Remote Port Attribute Management
681 #define fc_rport_show_function(field, format_string, sz, cast) \
683 show_fc_rport_##field (struct device *dev, \
684 struct device_attribute *attr, char *buf) \
686 struct fc_rport *rport = transport_class_to_rport(dev); \
687 struct Scsi_Host *shost = rport_to_shost(rport); \
688 struct fc_internal *i = to_fc_internal(shost->transportt); \
689 if ((i->f->get_rport_##field) && \
690 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
691 (rport->port_state == FC_PORTSTATE_DELETED) || \
692 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
693 i->f->get_rport_##field(rport); \
694 return snprintf(buf, sz, format_string, cast rport->field); \
697 #define fc_rport_store_function(field) \
699 store_fc_rport_##field(struct device *dev, \
700 struct device_attribute *attr, \
701 const char *buf, size_t count) \
704 struct fc_rport *rport = transport_class_to_rport(dev); \
705 struct Scsi_Host *shost = rport_to_shost(rport); \
706 struct fc_internal *i = to_fc_internal(shost->transportt); \
708 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
709 (rport->port_state == FC_PORTSTATE_DELETED) || \
710 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
712 val = simple_strtoul(buf, &cp, 0); \
713 if (*cp && (*cp != '\n')) \
715 i->f->set_rport_##field(rport, val); \
719 #define fc_rport_rd_attr(field, format_string, sz) \
720 fc_rport_show_function(field, format_string, sz, ) \
721 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
722 show_fc_rport_##field, NULL)
724 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
725 fc_rport_show_function(field, format_string, sz, (cast)) \
726 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
727 show_fc_rport_##field, NULL)
729 #define fc_rport_rw_attr(field, format_string, sz) \
730 fc_rport_show_function(field, format_string, sz, ) \
731 fc_rport_store_function(field) \
732 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
733 show_fc_rport_##field, \
734 store_fc_rport_##field)
737 #define fc_private_rport_show_function(field, format_string, sz, cast) \
739 show_fc_rport_##field (struct device *dev, \
740 struct device_attribute *attr, char *buf) \
742 struct fc_rport *rport = transport_class_to_rport(dev); \
743 return snprintf(buf, sz, format_string, cast rport->field); \
746 #define fc_private_rport_rd_attr(field, format_string, sz) \
747 fc_private_rport_show_function(field, format_string, sz, ) \
748 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
749 show_fc_rport_##field, NULL)
751 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
752 fc_private_rport_show_function(field, format_string, sz, (cast)) \
753 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
754 show_fc_rport_##field, NULL)
757 #define fc_private_rport_rd_enum_attr(title, maxlen) \
759 show_fc_rport_##title (struct device *dev, \
760 struct device_attribute *attr, char *buf) \
762 struct fc_rport *rport = transport_class_to_rport(dev); \
764 name = get_fc_##title##_name(rport->title); \
767 return snprintf(buf, maxlen, "%s\n", name); \
769 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
770 show_fc_rport_##title, NULL)
773 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
774 i->private_rport_attrs[count] = device_attr_rport_##field; \
775 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
776 i->private_rport_attrs[count].store = NULL; \
777 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
778 if (i->f->show_rport_##field) \
781 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
782 i->private_rport_attrs[count] = device_attr_rport_##field; \
783 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
784 i->private_rport_attrs[count].store = NULL; \
785 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
788 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
789 i->private_rport_attrs[count] = device_attr_rport_##field; \
790 if (!i->f->set_rport_##field) { \
791 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
792 i->private_rport_attrs[count].store = NULL; \
794 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
795 if (i->f->show_rport_##field) \
798 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
800 i->private_rport_attrs[count] = device_attr_rport_##field; \
801 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
806 /* The FC Transport Remote Port Attributes: */
808 /* Fixed Remote Port Attributes */
810 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
813 show_fc_rport_supported_classes (struct device *dev,
814 struct device_attribute *attr, char *buf)
816 struct fc_rport *rport = transport_class_to_rport(dev);
817 if (rport->supported_classes == FC_COS_UNSPECIFIED)
818 return snprintf(buf, 20, "unspecified\n");
819 return get_fc_cos_names(rport->supported_classes, buf);
821 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
822 show_fc_rport_supported_classes, NULL);
824 /* Dynamic Remote Port Attributes */
827 * dev_loss_tmo attribute
829 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
831 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
835 struct fc_rport *rport = transport_class_to_rport(dev);
836 struct Scsi_Host *shost = rport_to_shost(rport);
837 struct fc_internal *i = to_fc_internal(shost->transportt);
839 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
840 (rport->port_state == FC_PORTSTATE_DELETED) ||
841 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
843 val = simple_strtoul(buf, &cp, 0);
844 if ((*cp && (*cp != '\n')) ||
845 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
847 i->f->set_rport_dev_loss_tmo(rport, val);
850 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
851 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
854 /* Private Remote Port Attributes */
856 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
857 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
858 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
861 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
864 struct fc_rport *rport = transport_class_to_rport(dev);
866 /* identify any roles that are port_id specific */
867 if ((rport->port_id != -1) &&
868 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
869 FC_WELLKNOWN_PORTID_MASK) {
870 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
871 case FC_FPORT_PORTID:
872 return snprintf(buf, 30, "Fabric Port\n");
873 case FC_FABCTLR_PORTID:
874 return snprintf(buf, 30, "Fabric Controller\n");
875 case FC_DIRSRVR_PORTID:
876 return snprintf(buf, 30, "Directory Server\n");
877 case FC_TIMESRVR_PORTID:
878 return snprintf(buf, 30, "Time Server\n");
879 case FC_MGMTSRVR_PORTID:
880 return snprintf(buf, 30, "Management Server\n");
882 return snprintf(buf, 30, "Unknown Fabric Entity\n");
885 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
886 return snprintf(buf, 20, "unknown\n");
887 return get_fc_port_roles_names(rport->roles, buf);
890 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
891 show_fc_rport_roles, NULL);
893 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
894 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
897 * fast_io_fail_tmo attribute
900 show_fc_rport_fast_io_fail_tmo (struct device *dev,
901 struct device_attribute *attr, char *buf)
903 struct fc_rport *rport = transport_class_to_rport(dev);
905 if (rport->fast_io_fail_tmo == -1)
906 return snprintf(buf, 5, "off\n");
907 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
911 store_fc_rport_fast_io_fail_tmo(struct device *dev,
912 struct device_attribute *attr, const char *buf,
917 struct fc_rport *rport = transport_class_to_rport(dev);
919 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
920 (rport->port_state == FC_PORTSTATE_DELETED) ||
921 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
923 if (strncmp(buf, "off", 3) == 0)
924 rport->fast_io_fail_tmo = -1;
926 val = simple_strtoul(buf, &cp, 0);
927 if ((*cp && (*cp != '\n')) ||
928 (val < 0) || (val >= rport->dev_loss_tmo))
930 rport->fast_io_fail_tmo = val;
934 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
935 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
939 * FC SCSI Target Attribute Management
943 * Note: in the target show function we recognize when the remote
944 * port is in the heirarchy and do not allow the driver to get
945 * involved in sysfs functions. The driver only gets involved if
946 * it's the "old" style that doesn't use rports.
948 #define fc_starget_show_function(field, format_string, sz, cast) \
950 show_fc_starget_##field (struct device *dev, \
951 struct device_attribute *attr, char *buf) \
953 struct scsi_target *starget = transport_class_to_starget(dev); \
954 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
955 struct fc_internal *i = to_fc_internal(shost->transportt); \
956 struct fc_rport *rport = starget_to_rport(starget); \
958 fc_starget_##field(starget) = rport->field; \
959 else if (i->f->get_starget_##field) \
960 i->f->get_starget_##field(starget); \
961 return snprintf(buf, sz, format_string, \
962 cast fc_starget_##field(starget)); \
965 #define fc_starget_rd_attr(field, format_string, sz) \
966 fc_starget_show_function(field, format_string, sz, ) \
967 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
968 show_fc_starget_##field, NULL)
970 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
971 fc_starget_show_function(field, format_string, sz, (cast)) \
972 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
973 show_fc_starget_##field, NULL)
975 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
976 i->private_starget_attrs[count] = device_attr_starget_##field; \
977 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
978 i->private_starget_attrs[count].store = NULL; \
979 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
980 if (i->f->show_starget_##field) \
983 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
984 i->private_starget_attrs[count] = device_attr_starget_##field; \
985 if (!i->f->set_starget_##field) { \
986 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
987 i->private_starget_attrs[count].store = NULL; \
989 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
990 if (i->f->show_starget_##field) \
993 /* The FC Transport SCSI Target Attributes: */
994 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
995 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
996 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1000 * FC Virtual Port Attribute Management
1003 #define fc_vport_show_function(field, format_string, sz, cast) \
1005 show_fc_vport_##field (struct device *dev, \
1006 struct device_attribute *attr, char *buf) \
1008 struct fc_vport *vport = transport_class_to_vport(dev); \
1009 struct Scsi_Host *shost = vport_to_shost(vport); \
1010 struct fc_internal *i = to_fc_internal(shost->transportt); \
1011 if ((i->f->get_vport_##field) && \
1012 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1013 i->f->get_vport_##field(vport); \
1014 return snprintf(buf, sz, format_string, cast vport->field); \
1017 #define fc_vport_store_function(field) \
1019 store_fc_vport_##field(struct device *dev, \
1020 struct device_attribute *attr, \
1021 const char *buf, size_t count) \
1024 struct fc_vport *vport = transport_class_to_vport(dev); \
1025 struct Scsi_Host *shost = vport_to_shost(vport); \
1026 struct fc_internal *i = to_fc_internal(shost->transportt); \
1028 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1030 val = simple_strtoul(buf, &cp, 0); \
1031 if (*cp && (*cp != '\n')) \
1033 i->f->set_vport_##field(vport, val); \
1037 #define fc_vport_store_str_function(field, slen) \
1039 store_fc_vport_##field(struct device *dev, \
1040 struct device_attribute *attr, \
1041 const char *buf, size_t count) \
1043 struct fc_vport *vport = transport_class_to_vport(dev); \
1044 struct Scsi_Host *shost = vport_to_shost(vport); \
1045 struct fc_internal *i = to_fc_internal(shost->transportt); \
1046 unsigned int cnt=count; \
1048 /* count may include a LF at end of string */ \
1049 if (buf[cnt-1] == '\n') \
1051 if (cnt > ((slen) - 1)) \
1053 memcpy(vport->field, buf, cnt); \
1054 i->f->set_vport_##field(vport); \
1058 #define fc_vport_rd_attr(field, format_string, sz) \
1059 fc_vport_show_function(field, format_string, sz, ) \
1060 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1061 show_fc_vport_##field, NULL)
1063 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1064 fc_vport_show_function(field, format_string, sz, (cast)) \
1065 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1066 show_fc_vport_##field, NULL)
1068 #define fc_vport_rw_attr(field, format_string, sz) \
1069 fc_vport_show_function(field, format_string, sz, ) \
1070 fc_vport_store_function(field) \
1071 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1072 show_fc_vport_##field, \
1073 store_fc_vport_##field)
1075 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1077 show_fc_vport_##field (struct device *dev, \
1078 struct device_attribute *attr, char *buf) \
1080 struct fc_vport *vport = transport_class_to_vport(dev); \
1081 return snprintf(buf, sz, format_string, cast vport->field); \
1084 #define fc_private_vport_store_u32_function(field) \
1086 store_fc_vport_##field(struct device *dev, \
1087 struct device_attribute *attr, \
1088 const char *buf, size_t count) \
1091 struct fc_vport *vport = transport_class_to_vport(dev); \
1093 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1095 val = simple_strtoul(buf, &cp, 0); \
1096 if (*cp && (*cp != '\n')) \
1098 vport->field = val; \
1103 #define fc_private_vport_rd_attr(field, format_string, sz) \
1104 fc_private_vport_show_function(field, format_string, sz, ) \
1105 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1106 show_fc_vport_##field, NULL)
1108 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1109 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1110 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1111 show_fc_vport_##field, NULL)
1113 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1114 fc_private_vport_show_function(field, format_string, sz, ) \
1115 fc_private_vport_store_u32_function(field) \
1116 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1117 show_fc_vport_##field, \
1118 store_fc_vport_##field)
1121 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1123 show_fc_vport_##title (struct device *dev, \
1124 struct device_attribute *attr, \
1127 struct fc_vport *vport = transport_class_to_vport(dev); \
1129 name = get_fc_##title##_name(vport->title); \
1132 return snprintf(buf, maxlen, "%s\n", name); \
1134 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1135 show_fc_vport_##title, NULL)
1138 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1139 i->private_vport_attrs[count] = device_attr_vport_##field; \
1140 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1141 i->private_vport_attrs[count].store = NULL; \
1142 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1143 if (i->f->get_##field) \
1145 /* NOTE: Above MACRO differs: checks function not show bit */
1147 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1148 i->private_vport_attrs[count] = device_attr_vport_##field; \
1149 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1150 i->private_vport_attrs[count].store = NULL; \
1151 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1154 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1155 i->private_vport_attrs[count] = device_attr_vport_##field; \
1156 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1159 /* NOTE: Above MACRO differs: checks function */
1161 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1162 i->private_vport_attrs[count] = device_attr_vport_##field; \
1163 if (!i->f->set_vport_##field) { \
1164 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1165 i->private_vport_attrs[count].store = NULL; \
1167 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1169 /* NOTE: Above MACRO differs: does not check show bit */
1171 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1173 i->private_vport_attrs[count] = device_attr_vport_##field; \
1174 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1179 /* The FC Transport Virtual Port Attributes: */
1181 /* Fixed Virtual Port Attributes */
1183 /* Dynamic Virtual Port Attributes */
1185 /* Private Virtual Port Attributes */
1187 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1188 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1189 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1190 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1193 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1196 struct fc_vport *vport = transport_class_to_vport(dev);
1198 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1199 return snprintf(buf, 20, "unknown\n");
1200 return get_fc_port_roles_names(vport->roles, buf);
1202 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1204 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1206 fc_private_vport_show_function(symbolic_name, "%s\n",
1207 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1208 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1209 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1210 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1213 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1214 const char *buf, size_t count)
1216 struct fc_vport *vport = transport_class_to_vport(dev);
1217 struct Scsi_Host *shost = vport_to_shost(vport);
1219 fc_queue_work(shost, &vport->vport_delete_work);
1222 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1223 NULL, store_fc_vport_delete);
1227 * Enable/Disable vport
1228 * Write "1" to disable, write "0" to enable
1231 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1235 struct fc_vport *vport = transport_class_to_vport(dev);
1236 struct Scsi_Host *shost = vport_to_shost(vport);
1237 struct fc_internal *i = to_fc_internal(shost->transportt);
1240 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1244 if (vport->vport_state != FC_VPORT_DISABLED)
1246 } else if (*buf == '1') {
1247 if (vport->vport_state == FC_VPORT_DISABLED)
1252 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1253 return stat ? stat : count;
1255 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1256 NULL, store_fc_vport_disable);
1260 * Host Attribute Management
1263 #define fc_host_show_function(field, format_string, sz, cast) \
1265 show_fc_host_##field (struct device *dev, \
1266 struct device_attribute *attr, char *buf) \
1268 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1269 struct fc_internal *i = to_fc_internal(shost->transportt); \
1270 if (i->f->get_host_##field) \
1271 i->f->get_host_##field(shost); \
1272 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1275 #define fc_host_store_function(field) \
1277 store_fc_host_##field(struct device *dev, \
1278 struct device_attribute *attr, \
1279 const char *buf, size_t count) \
1282 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1283 struct fc_internal *i = to_fc_internal(shost->transportt); \
1286 val = simple_strtoul(buf, &cp, 0); \
1287 if (*cp && (*cp != '\n')) \
1289 i->f->set_host_##field(shost, val); \
1293 #define fc_host_store_str_function(field, slen) \
1295 store_fc_host_##field(struct device *dev, \
1296 struct device_attribute *attr, \
1297 const char *buf, size_t count) \
1299 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1300 struct fc_internal *i = to_fc_internal(shost->transportt); \
1301 unsigned int cnt=count; \
1303 /* count may include a LF at end of string */ \
1304 if (buf[cnt-1] == '\n') \
1306 if (cnt > ((slen) - 1)) \
1308 memcpy(fc_host_##field(shost), buf, cnt); \
1309 i->f->set_host_##field(shost); \
1313 #define fc_host_rd_attr(field, format_string, sz) \
1314 fc_host_show_function(field, format_string, sz, ) \
1315 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1316 show_fc_host_##field, NULL)
1318 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1319 fc_host_show_function(field, format_string, sz, (cast)) \
1320 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1321 show_fc_host_##field, NULL)
1323 #define fc_host_rw_attr(field, format_string, sz) \
1324 fc_host_show_function(field, format_string, sz, ) \
1325 fc_host_store_function(field) \
1326 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1327 show_fc_host_##field, \
1328 store_fc_host_##field)
1330 #define fc_host_rd_enum_attr(title, maxlen) \
1332 show_fc_host_##title (struct device *dev, \
1333 struct device_attribute *attr, char *buf) \
1335 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1336 struct fc_internal *i = to_fc_internal(shost->transportt); \
1338 if (i->f->get_host_##title) \
1339 i->f->get_host_##title(shost); \
1340 name = get_fc_##title##_name(fc_host_##title(shost)); \
1343 return snprintf(buf, maxlen, "%s\n", name); \
1345 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1347 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1348 i->private_host_attrs[count] = device_attr_host_##field; \
1349 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1350 i->private_host_attrs[count].store = NULL; \
1351 i->host_attrs[count] = &i->private_host_attrs[count]; \
1352 if (i->f->show_host_##field) \
1355 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1356 i->private_host_attrs[count] = device_attr_host_##field; \
1357 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1358 i->private_host_attrs[count].store = NULL; \
1359 i->host_attrs[count] = &i->private_host_attrs[count]; \
1362 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1363 i->private_host_attrs[count] = device_attr_host_##field; \
1364 if (!i->f->set_host_##field) { \
1365 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1366 i->private_host_attrs[count].store = NULL; \
1368 i->host_attrs[count] = &i->private_host_attrs[count]; \
1369 if (i->f->show_host_##field) \
1373 #define fc_private_host_show_function(field, format_string, sz, cast) \
1375 show_fc_host_##field (struct device *dev, \
1376 struct device_attribute *attr, char *buf) \
1378 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1379 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1382 #define fc_private_host_rd_attr(field, format_string, sz) \
1383 fc_private_host_show_function(field, format_string, sz, ) \
1384 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1385 show_fc_host_##field, NULL)
1387 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1388 fc_private_host_show_function(field, format_string, sz, (cast)) \
1389 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1390 show_fc_host_##field, NULL)
1392 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1393 i->private_host_attrs[count] = device_attr_host_##field; \
1394 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1395 i->private_host_attrs[count].store = NULL; \
1396 i->host_attrs[count] = &i->private_host_attrs[count]; \
1399 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1401 i->private_host_attrs[count] = device_attr_host_##field; \
1402 i->host_attrs[count] = &i->private_host_attrs[count]; \
1407 /* Fixed Host Attributes */
1410 show_fc_host_supported_classes (struct device *dev,
1411 struct device_attribute *attr, char *buf)
1413 struct Scsi_Host *shost = transport_class_to_shost(dev);
1415 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1416 return snprintf(buf, 20, "unspecified\n");
1418 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1420 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1421 show_fc_host_supported_classes, NULL);
1424 show_fc_host_supported_fc4s (struct device *dev,
1425 struct device_attribute *attr, char *buf)
1427 struct Scsi_Host *shost = transport_class_to_shost(dev);
1428 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1430 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1431 show_fc_host_supported_fc4s, NULL);
1434 show_fc_host_supported_speeds (struct device *dev,
1435 struct device_attribute *attr, char *buf)
1437 struct Scsi_Host *shost = transport_class_to_shost(dev);
1439 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1440 return snprintf(buf, 20, "unknown\n");
1442 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1444 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1445 show_fc_host_supported_speeds, NULL);
1448 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1449 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1450 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1451 unsigned long long);
1452 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1453 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1454 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1457 /* Dynamic Host Attributes */
1460 show_fc_host_active_fc4s (struct device *dev,
1461 struct device_attribute *attr, char *buf)
1463 struct Scsi_Host *shost = transport_class_to_shost(dev);
1464 struct fc_internal *i = to_fc_internal(shost->transportt);
1466 if (i->f->get_host_active_fc4s)
1467 i->f->get_host_active_fc4s(shost);
1469 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1471 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1472 show_fc_host_active_fc4s, NULL);
1475 show_fc_host_speed (struct device *dev,
1476 struct device_attribute *attr, char *buf)
1478 struct Scsi_Host *shost = transport_class_to_shost(dev);
1479 struct fc_internal *i = to_fc_internal(shost->transportt);
1481 if (i->f->get_host_speed)
1482 i->f->get_host_speed(shost);
1484 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1485 return snprintf(buf, 20, "unknown\n");
1487 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1489 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1490 show_fc_host_speed, NULL);
1493 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1494 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1495 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1496 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1497 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1499 fc_private_host_show_function(system_hostname, "%s\n",
1500 FC_SYMBOLIC_NAME_SIZE + 1, )
1501 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1502 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1503 show_fc_host_system_hostname, store_fc_host_system_hostname);
1506 /* Private Host Attributes */
1509 show_fc_private_host_tgtid_bind_type(struct device *dev,
1510 struct device_attribute *attr, char *buf)
1512 struct Scsi_Host *shost = transport_class_to_shost(dev);
1515 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1518 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1521 #define get_list_head_entry(pos, head, member) \
1522 pos = list_entry((head)->next, typeof(*pos), member)
1525 store_fc_private_host_tgtid_bind_type(struct device *dev,
1526 struct device_attribute *attr, const char *buf, size_t count)
1528 struct Scsi_Host *shost = transport_class_to_shost(dev);
1529 struct fc_rport *rport;
1530 enum fc_tgtid_binding_type val;
1531 unsigned long flags;
1533 if (get_fc_tgtid_bind_type_match(buf, &val))
1536 /* if changing bind type, purge all unused consistent bindings */
1537 if (val != fc_host_tgtid_bind_type(shost)) {
1538 spin_lock_irqsave(shost->host_lock, flags);
1539 while (!list_empty(&fc_host_rport_bindings(shost))) {
1540 get_list_head_entry(rport,
1541 &fc_host_rport_bindings(shost), peers);
1542 list_del(&rport->peers);
1543 rport->port_state = FC_PORTSTATE_DELETED;
1544 fc_queue_work(shost, &rport->rport_delete_work);
1546 spin_unlock_irqrestore(shost->host_lock, flags);
1549 fc_host_tgtid_bind_type(shost) = val;
1553 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1554 show_fc_private_host_tgtid_bind_type,
1555 store_fc_private_host_tgtid_bind_type);
1558 store_fc_private_host_issue_lip(struct device *dev,
1559 struct device_attribute *attr, const char *buf, size_t count)
1561 struct Scsi_Host *shost = transport_class_to_shost(dev);
1562 struct fc_internal *i = to_fc_internal(shost->transportt);
1565 /* ignore any data value written to the attribute */
1566 if (i->f->issue_fc_host_lip) {
1567 ret = i->f->issue_fc_host_lip(shost);
1568 return ret ? ret: count;
1574 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1575 store_fc_private_host_issue_lip);
1577 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1581 * Host Statistics Management
1584 /* Show a given an attribute in the statistics group */
1586 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1588 struct Scsi_Host *shost = transport_class_to_shost(dev);
1589 struct fc_internal *i = to_fc_internal(shost->transportt);
1590 struct fc_host_statistics *stats;
1591 ssize_t ret = -ENOENT;
1593 if (offset > sizeof(struct fc_host_statistics) ||
1594 offset % sizeof(u64) != 0)
1597 if (i->f->get_fc_host_stats) {
1598 stats = (i->f->get_fc_host_stats)(shost);
1600 ret = snprintf(buf, 20, "0x%llx\n",
1601 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1607 /* generate a read-only statistics attribute */
1608 #define fc_host_statistic(name) \
1609 static ssize_t show_fcstat_##name(struct device *cd, \
1610 struct device_attribute *attr, \
1613 return fc_stat_show(cd, buf, \
1614 offsetof(struct fc_host_statistics, name)); \
1616 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1618 fc_host_statistic(seconds_since_last_reset);
1619 fc_host_statistic(tx_frames);
1620 fc_host_statistic(tx_words);
1621 fc_host_statistic(rx_frames);
1622 fc_host_statistic(rx_words);
1623 fc_host_statistic(lip_count);
1624 fc_host_statistic(nos_count);
1625 fc_host_statistic(error_frames);
1626 fc_host_statistic(dumped_frames);
1627 fc_host_statistic(link_failure_count);
1628 fc_host_statistic(loss_of_sync_count);
1629 fc_host_statistic(loss_of_signal_count);
1630 fc_host_statistic(prim_seq_protocol_err_count);
1631 fc_host_statistic(invalid_tx_word_count);
1632 fc_host_statistic(invalid_crc_count);
1633 fc_host_statistic(fcp_input_requests);
1634 fc_host_statistic(fcp_output_requests);
1635 fc_host_statistic(fcp_control_requests);
1636 fc_host_statistic(fcp_input_megabytes);
1637 fc_host_statistic(fcp_output_megabytes);
1640 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1641 const char *buf, size_t count)
1643 struct Scsi_Host *shost = transport_class_to_shost(dev);
1644 struct fc_internal *i = to_fc_internal(shost->transportt);
1646 /* ignore any data value written to the attribute */
1647 if (i->f->reset_fc_host_stats) {
1648 i->f->reset_fc_host_stats(shost);
1654 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1655 fc_reset_statistics);
1657 static struct attribute *fc_statistics_attrs[] = {
1658 &device_attr_host_seconds_since_last_reset.attr,
1659 &device_attr_host_tx_frames.attr,
1660 &device_attr_host_tx_words.attr,
1661 &device_attr_host_rx_frames.attr,
1662 &device_attr_host_rx_words.attr,
1663 &device_attr_host_lip_count.attr,
1664 &device_attr_host_nos_count.attr,
1665 &device_attr_host_error_frames.attr,
1666 &device_attr_host_dumped_frames.attr,
1667 &device_attr_host_link_failure_count.attr,
1668 &device_attr_host_loss_of_sync_count.attr,
1669 &device_attr_host_loss_of_signal_count.attr,
1670 &device_attr_host_prim_seq_protocol_err_count.attr,
1671 &device_attr_host_invalid_tx_word_count.attr,
1672 &device_attr_host_invalid_crc_count.attr,
1673 &device_attr_host_fcp_input_requests.attr,
1674 &device_attr_host_fcp_output_requests.attr,
1675 &device_attr_host_fcp_control_requests.attr,
1676 &device_attr_host_fcp_input_megabytes.attr,
1677 &device_attr_host_fcp_output_megabytes.attr,
1678 &device_attr_host_reset_statistics.attr,
1682 static struct attribute_group fc_statistics_group = {
1683 .name = "statistics",
1684 .attrs = fc_statistics_attrs,
1688 /* Host Vport Attributes */
1691 fc_parse_wwn(const char *ns, u64 *nm)
1696 memset(wwn, 0, sizeof(wwn));
1698 /* Validate and store the new name */
1699 for (i=0, j=0; i < 16; i++) {
1700 if ((*ns >= 'a') && (*ns <= 'f'))
1701 j = ((j << 4) | ((*ns++ -'a') + 10));
1702 else if ((*ns >= 'A') && (*ns <= 'F'))
1703 j = ((j << 4) | ((*ns++ -'A') + 10));
1704 else if ((*ns >= '0') && (*ns <= '9'))
1705 j = ((j << 4) | (*ns++ -'0'));
1709 wwn[i/2] = j & 0xff;
1714 *nm = wwn_to_u64(wwn);
1721 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1722 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1723 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1724 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1727 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1728 const char *buf, size_t count)
1730 struct Scsi_Host *shost = transport_class_to_shost(dev);
1731 struct fc_vport_identifiers vid;
1732 struct fc_vport *vport;
1733 unsigned int cnt=count;
1736 memset(&vid, 0, sizeof(vid));
1738 /* count may include a LF at end of string */
1739 if (buf[cnt-1] == '\n')
1742 /* validate we have enough characters for WWPN */
1743 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1746 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1750 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1754 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1755 vid.vport_type = FC_PORTTYPE_NPIV;
1756 /* vid.symbolic_name is already zero/NULL's */
1757 vid.disable = false; /* always enabled */
1759 /* we only allow support on Channel 0 !!! */
1760 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1761 return stat ? stat : count;
1763 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1764 store_fc_host_vport_create);
1768 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1769 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1770 * The WWNs are specified as hex characters, and may *not* contain
1771 * any prefixes (e.g. 0x, x, etc)
1774 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1775 const char *buf, size_t count)
1777 struct Scsi_Host *shost = transport_class_to_shost(dev);
1778 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1779 struct fc_vport *vport;
1781 unsigned long flags;
1782 unsigned int cnt=count;
1785 /* count may include a LF at end of string */
1786 if (buf[cnt-1] == '\n')
1789 /* validate we have enough characters for WWPN */
1790 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1793 stat = fc_parse_wwn(&buf[0], &wwpn);
1797 stat = fc_parse_wwn(&buf[17], &wwnn);
1801 spin_lock_irqsave(shost->host_lock, flags);
1803 /* we only allow support on Channel 0 !!! */
1804 list_for_each_entry(vport, &fc_host->vports, peers) {
1805 if ((vport->channel == 0) &&
1806 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1811 spin_unlock_irqrestore(shost->host_lock, flags);
1816 stat = fc_vport_terminate(vport);
1817 return stat ? stat : count;
1819 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1820 store_fc_host_vport_delete);
1823 static int fc_host_match(struct attribute_container *cont,
1826 struct Scsi_Host *shost;
1827 struct fc_internal *i;
1829 if (!scsi_is_host_device(dev))
1832 shost = dev_to_shost(dev);
1833 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1834 != &fc_host_class.class)
1837 i = to_fc_internal(shost->transportt);
1839 return &i->t.host_attrs.ac == cont;
1842 static int fc_target_match(struct attribute_container *cont,
1845 struct Scsi_Host *shost;
1846 struct fc_internal *i;
1848 if (!scsi_is_target_device(dev))
1851 shost = dev_to_shost(dev->parent);
1852 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1853 != &fc_host_class.class)
1856 i = to_fc_internal(shost->transportt);
1858 return &i->t.target_attrs.ac == cont;
1861 static void fc_rport_dev_release(struct device *dev)
1863 struct fc_rport *rport = dev_to_rport(dev);
1864 put_device(dev->parent);
1868 int scsi_is_fc_rport(const struct device *dev)
1870 return dev->release == fc_rport_dev_release;
1872 EXPORT_SYMBOL(scsi_is_fc_rport);
1874 static int fc_rport_match(struct attribute_container *cont,
1877 struct Scsi_Host *shost;
1878 struct fc_internal *i;
1880 if (!scsi_is_fc_rport(dev))
1883 shost = dev_to_shost(dev->parent);
1884 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1885 != &fc_host_class.class)
1888 i = to_fc_internal(shost->transportt);
1890 return &i->rport_attr_cont.ac == cont;
1894 static void fc_vport_dev_release(struct device *dev)
1896 struct fc_vport *vport = dev_to_vport(dev);
1897 put_device(dev->parent); /* release kobj parent */
1901 int scsi_is_fc_vport(const struct device *dev)
1903 return dev->release == fc_vport_dev_release;
1905 EXPORT_SYMBOL(scsi_is_fc_vport);
1907 static int fc_vport_match(struct attribute_container *cont,
1910 struct fc_vport *vport;
1911 struct Scsi_Host *shost;
1912 struct fc_internal *i;
1914 if (!scsi_is_fc_vport(dev))
1916 vport = dev_to_vport(dev);
1918 shost = vport_to_shost(vport);
1919 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1920 != &fc_host_class.class)
1923 i = to_fc_internal(shost->transportt);
1924 return &i->vport_attr_cont.ac == cont;
1929 * fc_timed_out - FC Transport I/O timeout intercept handler
1930 * @scmd: The SCSI command which timed out
1932 * This routine protects against error handlers getting invoked while a
1933 * rport is in a blocked state, typically due to a temporarily loss of
1934 * connectivity. If the error handlers are allowed to proceed, requests
1935 * to abort i/o, reset the target, etc will likely fail as there is no way
1936 * to communicate with the device to perform the requested function. These
1937 * failures may result in the midlayer taking the device offline, requiring
1938 * manual intervention to restore operation.
1940 * This routine, called whenever an i/o times out, validates the state of
1941 * the underlying rport. If the rport is blocked, it returns
1942 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1943 * Eventually, either the device will return, or devloss_tmo will fire,
1944 * and when the timeout then fires, it will be handled normally.
1945 * If the rport is not blocked, normal error handling continues.
1948 * This routine assumes no locks are held on entry.
1950 static enum blk_eh_timer_return
1951 fc_timed_out(struct scsi_cmnd *scmd)
1953 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1955 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1956 return BLK_EH_RESET_TIMER;
1958 return BLK_EH_NOT_HANDLED;
1962 * Called by fc_user_scan to locate an rport on the shost that
1963 * matches the channel and target id, and invoke scsi_scan_target()
1967 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1969 struct fc_rport *rport;
1970 unsigned long flags;
1972 spin_lock_irqsave(shost->host_lock, flags);
1974 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1975 if (rport->scsi_target_id == -1)
1978 if (rport->port_state != FC_PORTSTATE_ONLINE)
1981 if ((channel == rport->channel) &&
1982 (id == rport->scsi_target_id)) {
1983 spin_unlock_irqrestore(shost->host_lock, flags);
1984 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1989 spin_unlock_irqrestore(shost->host_lock, flags);
1993 * Called via sysfs scan routines. Necessary, as the FC transport
1994 * wants to place all target objects below the rport object. So this
1995 * routine must invoke the scsi_scan_target() routine with the rport
1996 * object as the parent.
1999 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2004 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2005 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2006 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2009 if (channel == SCAN_WILD_CARD) {
2011 chhi = shost->max_channel + 1;
2017 if (id == SCAN_WILD_CARD) {
2019 tgthi = shost->max_id;
2025 for ( ; chlo < chhi; chlo++)
2026 for ( ; tgtlo < tgthi; tgtlo++)
2027 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2032 static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2035 struct fc_internal *i = to_fc_internal(shost->transportt);
2036 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2039 static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2041 struct fc_internal *i = to_fc_internal(shost->transportt);
2042 return i->f->it_nexus_response(shost, nexus, result);
2045 struct scsi_transport_template *
2046 fc_attach_transport(struct fc_function_template *ft)
2049 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2055 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2056 i->t.target_attrs.ac.class = &fc_transport_class.class;
2057 i->t.target_attrs.ac.match = fc_target_match;
2058 i->t.target_size = sizeof(struct fc_starget_attrs);
2059 transport_container_register(&i->t.target_attrs);
2061 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2062 i->t.host_attrs.ac.class = &fc_host_class.class;
2063 i->t.host_attrs.ac.match = fc_host_match;
2064 i->t.host_size = sizeof(struct fc_host_attrs);
2065 if (ft->get_fc_host_stats)
2066 i->t.host_attrs.statistics = &fc_statistics_group;
2067 transport_container_register(&i->t.host_attrs);
2069 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2070 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2071 i->rport_attr_cont.ac.match = fc_rport_match;
2072 transport_container_register(&i->rport_attr_cont);
2074 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2075 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2076 i->vport_attr_cont.ac.match = fc_vport_match;
2077 transport_container_register(&i->vport_attr_cont);
2081 /* Transport uses the shost workq for scsi scanning */
2082 i->t.create_work_queue = 1;
2084 i->t.eh_timed_out = fc_timed_out;
2086 i->t.user_scan = fc_user_scan;
2088 /* target-mode drivers' functions */
2089 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2090 i->t.it_nexus_response = fc_it_nexus_response;
2093 * Setup SCSI Target Attributes.
2096 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2097 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2098 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2100 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2102 i->starget_attrs[count] = NULL;
2106 * Setup SCSI Host Attributes.
2109 SETUP_HOST_ATTRIBUTE_RD(node_name);
2110 SETUP_HOST_ATTRIBUTE_RD(port_name);
2111 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2112 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2113 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2114 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2115 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2116 if (ft->vport_create) {
2117 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2118 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2120 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2122 SETUP_HOST_ATTRIBUTE_RD(port_id);
2123 SETUP_HOST_ATTRIBUTE_RD(port_type);
2124 SETUP_HOST_ATTRIBUTE_RD(port_state);
2125 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2126 SETUP_HOST_ATTRIBUTE_RD(speed);
2127 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2128 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2129 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2131 /* Transport-managed attributes */
2132 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2133 if (ft->issue_fc_host_lip)
2134 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2135 if (ft->vport_create)
2136 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2137 if (ft->vport_delete)
2138 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2140 BUG_ON(count > FC_HOST_NUM_ATTRS);
2142 i->host_attrs[count] = NULL;
2145 * Setup Remote Port Attributes.
2148 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2149 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2150 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2151 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2152 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2153 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2154 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2155 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2156 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2157 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2159 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2161 i->rport_attrs[count] = NULL;
2164 * Setup Virtual Port Attributes.
2167 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2168 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2169 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2170 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2171 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2172 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2173 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2174 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2175 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2177 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2179 i->vport_attrs[count] = NULL;
2183 EXPORT_SYMBOL(fc_attach_transport);
2185 void fc_release_transport(struct scsi_transport_template *t)
2187 struct fc_internal *i = to_fc_internal(t);
2189 transport_container_unregister(&i->t.target_attrs);
2190 transport_container_unregister(&i->t.host_attrs);
2191 transport_container_unregister(&i->rport_attr_cont);
2192 transport_container_unregister(&i->vport_attr_cont);
2196 EXPORT_SYMBOL(fc_release_transport);
2199 * fc_queue_work - Queue work to the fc_host workqueue.
2200 * @shost: Pointer to Scsi_Host bound to fc_host.
2201 * @work: Work to queue for execution.
2204 * 1 - work queued for execution
2205 * 0 - work is already queued
2206 * -EINVAL - work queue doesn't exist
2209 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2211 if (unlikely(!fc_host_work_q(shost))) {
2213 "ERROR: FC host '%s' attempted to queue work, "
2214 "when no workqueue created.\n", shost->hostt->name);
2220 return queue_work(fc_host_work_q(shost), work);
2224 * fc_flush_work - Flush a fc_host's workqueue.
2225 * @shost: Pointer to Scsi_Host bound to fc_host.
2228 fc_flush_work(struct Scsi_Host *shost)
2230 if (!fc_host_work_q(shost)) {
2232 "ERROR: FC host '%s' attempted to flush work, "
2233 "when no workqueue created.\n", shost->hostt->name);
2238 flush_workqueue(fc_host_work_q(shost));
2242 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2243 * @shost: Pointer to Scsi_Host bound to fc_host.
2244 * @work: Work to queue for execution.
2245 * @delay: jiffies to delay the work queuing
2248 * 1 on success / 0 already queued / < 0 for error
2251 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2252 unsigned long delay)
2254 if (unlikely(!fc_host_devloss_work_q(shost))) {
2256 "ERROR: FC host '%s' attempted to queue work, "
2257 "when no workqueue created.\n", shost->hostt->name);
2263 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2267 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2268 * @shost: Pointer to Scsi_Host bound to fc_host.
2271 fc_flush_devloss(struct Scsi_Host *shost)
2273 if (!fc_host_devloss_work_q(shost)) {
2275 "ERROR: FC host '%s' attempted to flush work, "
2276 "when no workqueue created.\n", shost->hostt->name);
2281 flush_workqueue(fc_host_devloss_work_q(shost));
2286 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2287 * @shost: Which &Scsi_Host
2289 * This routine is expected to be called immediately preceeding the
2290 * a driver's call to scsi_remove_host().
2292 * WARNING: A driver utilizing the fc_transport, which fails to call
2293 * this routine prior to scsi_remove_host(), will leave dangling
2294 * objects in /sys/class/fc_remote_ports. Access to any of these
2295 * objects can result in a system crash !!!
2298 * This routine assumes no locks are held on entry.
2301 fc_remove_host(struct Scsi_Host *shost)
2303 struct fc_vport *vport = NULL, *next_vport = NULL;
2304 struct fc_rport *rport = NULL, *next_rport = NULL;
2305 struct workqueue_struct *work_q;
2306 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2307 unsigned long flags;
2309 spin_lock_irqsave(shost->host_lock, flags);
2311 /* Remove any vports */
2312 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2313 fc_queue_work(shost, &vport->vport_delete_work);
2315 /* Remove any remote ports */
2316 list_for_each_entry_safe(rport, next_rport,
2317 &fc_host->rports, peers) {
2318 list_del(&rport->peers);
2319 rport->port_state = FC_PORTSTATE_DELETED;
2320 fc_queue_work(shost, &rport->rport_delete_work);
2323 list_for_each_entry_safe(rport, next_rport,
2324 &fc_host->rport_bindings, peers) {
2325 list_del(&rport->peers);
2326 rport->port_state = FC_PORTSTATE_DELETED;
2327 fc_queue_work(shost, &rport->rport_delete_work);
2330 spin_unlock_irqrestore(shost->host_lock, flags);
2332 /* flush all scan work items */
2333 scsi_flush_work(shost);
2335 /* flush all stgt delete, and rport delete work items, then kill it */
2336 if (fc_host->work_q) {
2337 work_q = fc_host->work_q;
2338 fc_host->work_q = NULL;
2339 destroy_workqueue(work_q);
2342 /* flush all devloss work items, then kill it */
2343 if (fc_host->devloss_work_q) {
2344 work_q = fc_host->devloss_work_q;
2345 fc_host->devloss_work_q = NULL;
2346 destroy_workqueue(work_q);
2349 EXPORT_SYMBOL(fc_remove_host);
2351 static void fc_terminate_rport_io(struct fc_rport *rport)
2353 struct Scsi_Host *shost = rport_to_shost(rport);
2354 struct fc_internal *i = to_fc_internal(shost->transportt);
2356 /* Involve the LLDD if possible to terminate all io on the rport. */
2357 if (i->f->terminate_rport_io)
2358 i->f->terminate_rport_io(rport);
2361 * must unblock to flush queued IO. The caller will have set
2362 * the port_state or flags, so that fc_remote_port_chkready will
2365 scsi_target_unblock(&rport->dev);
2369 * fc_starget_delete - called to delete the scsi decendents of an rport
2370 * @work: remote port to be operated on.
2372 * Deletes target and all sdevs.
2375 fc_starget_delete(struct work_struct *work)
2377 struct fc_rport *rport =
2378 container_of(work, struct fc_rport, stgt_delete_work);
2380 fc_terminate_rport_io(rport);
2381 scsi_remove_target(&rport->dev);
2386 * fc_rport_final_delete - finish rport termination and delete it.
2387 * @work: remote port to be deleted.
2390 fc_rport_final_delete(struct work_struct *work)
2392 struct fc_rport *rport =
2393 container_of(work, struct fc_rport, rport_delete_work);
2394 struct device *dev = &rport->dev;
2395 struct Scsi_Host *shost = rport_to_shost(rport);
2396 struct fc_internal *i = to_fc_internal(shost->transportt);
2397 unsigned long flags;
2398 int do_callback = 0;
2401 * if a scan is pending, flush the SCSI Host work_q so that
2402 * that we can reclaim the rport scan work element.
2404 if (rport->flags & FC_RPORT_SCAN_PENDING)
2405 scsi_flush_work(shost);
2407 fc_terminate_rport_io(rport);
2410 * Cancel any outstanding timers. These should really exist
2411 * only when rmmod'ing the LLDD and we're asking for
2412 * immediate termination of the rports
2414 spin_lock_irqsave(shost->host_lock, flags);
2415 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2416 spin_unlock_irqrestore(shost->host_lock, flags);
2417 if (!cancel_delayed_work(&rport->fail_io_work))
2418 fc_flush_devloss(shost);
2419 if (!cancel_delayed_work(&rport->dev_loss_work))
2420 fc_flush_devloss(shost);
2421 spin_lock_irqsave(shost->host_lock, flags);
2422 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2424 spin_unlock_irqrestore(shost->host_lock, flags);
2426 /* Delete SCSI target and sdevs */
2427 if (rport->scsi_target_id != -1)
2428 fc_starget_delete(&rport->stgt_delete_work);
2431 * Notify the driver that the rport is now dead. The LLDD will
2432 * also guarantee that any communication to the rport is terminated
2434 * Avoid this call if we already called it when we preserved the
2435 * rport for the binding.
2437 spin_lock_irqsave(shost->host_lock, flags);
2438 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2439 (i->f->dev_loss_tmo_callbk)) {
2440 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2443 spin_unlock_irqrestore(shost->host_lock, flags);
2446 i->f->dev_loss_tmo_callbk(rport);
2448 fc_bsg_remove(rport->rqst_q);
2450 transport_remove_device(dev);
2452 transport_destroy_device(dev);
2453 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2454 put_device(dev); /* for self-reference */
2459 * fc_rport_create - allocates and creates a remote FC port.
2460 * @shost: scsi host the remote port is connected to.
2461 * @channel: Channel on shost port connected to.
2462 * @ids: The world wide names, fc address, and FC4 port
2463 * roles for the remote port.
2465 * Allocates and creates the remoter port structure, including the
2466 * class and sysfs creation.
2469 * This routine assumes no locks are held on entry.
2471 static struct fc_rport *
2472 fc_rport_create(struct Scsi_Host *shost, int channel,
2473 struct fc_rport_identifiers *ids)
2475 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2476 struct fc_internal *fci = to_fc_internal(shost->transportt);
2477 struct fc_rport *rport;
2479 unsigned long flags;
2483 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2484 rport = kzalloc(size, GFP_KERNEL);
2485 if (unlikely(!rport)) {
2486 printk(KERN_ERR "%s: allocation failure\n", __func__);
2490 rport->maxframe_size = -1;
2491 rport->supported_classes = FC_COS_UNSPECIFIED;
2492 rport->dev_loss_tmo = fc_dev_loss_tmo;
2493 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2494 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2495 rport->port_id = ids->port_id;
2496 rport->roles = ids->roles;
2497 rport->port_state = FC_PORTSTATE_ONLINE;
2498 if (fci->f->dd_fcrport_size)
2499 rport->dd_data = &rport[1];
2500 rport->channel = channel;
2501 rport->fast_io_fail_tmo = -1;
2503 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2504 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2505 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2506 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2507 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2509 spin_lock_irqsave(shost->host_lock, flags);
2511 rport->number = fc_host->next_rport_number++;
2512 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
2513 rport->scsi_target_id = fc_host->next_target_id++;
2515 rport->scsi_target_id = -1;
2516 list_add_tail(&rport->peers, &fc_host->rports);
2517 get_device(&shost->shost_gendev); /* for fc_host->rport list */
2519 spin_unlock_irqrestore(shost->host_lock, flags);
2522 device_initialize(dev); /* takes self reference */
2523 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2524 dev->release = fc_rport_dev_release;
2525 dev_set_name(dev, "rport-%d:%d-%d",
2526 shost->host_no, channel, rport->number);
2527 transport_setup_device(dev);
2529 error = device_add(dev);
2531 printk(KERN_ERR "FC Remote Port device_add failed\n");
2534 transport_add_device(dev);
2535 transport_configure_device(dev);
2537 fc_bsg_rportadd(shost, rport);
2538 /* ignore any bsg add error - we just can't do sgio */
2540 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2541 /* initiate a scan of the target */
2542 rport->flags |= FC_RPORT_SCAN_PENDING;
2543 scsi_queue_work(shost, &rport->scan_work);
2549 transport_destroy_device(dev);
2550 spin_lock_irqsave(shost->host_lock, flags);
2551 list_del(&rport->peers);
2552 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2553 spin_unlock_irqrestore(shost->host_lock, flags);
2554 put_device(dev->parent);
2560 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2561 * @shost: scsi host the remote port is connected to.
2562 * @channel: Channel on shost port connected to.
2563 * @ids: The world wide names, fc address, and FC4 port
2564 * roles for the remote port.
2566 * The LLDD calls this routine to notify the transport of the existence
2567 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2568 * of the port, it's FC address (port_id), and the FC4 roles that are
2569 * active for the port.
2571 * For ports that are FCP targets (aka scsi targets), the FC transport
2572 * maintains consistent target id bindings on behalf of the LLDD.
2573 * A consistent target id binding is an assignment of a target id to
2574 * a remote port identifier, which persists while the scsi host is
2575 * attached. The remote port can disappear, then later reappear, and
2576 * it's target id assignment remains the same. This allows for shifts
2577 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2578 * changes to the scsi subsystem which is based on scsi host number and
2579 * target id values. Bindings are only valid during the attachment of
2580 * the scsi host. If the host detaches, then later re-attaches, target
2581 * id bindings may change.
2583 * This routine is responsible for returning a remote port structure.
2584 * The routine will search the list of remote ports it maintains
2585 * internally on behalf of consistent target id mappings. If found, the
2586 * remote port structure will be reused. Otherwise, a new remote port
2587 * structure will be allocated.
2589 * Whenever a remote port is allocated, a new fc_remote_port class
2590 * device is created.
2592 * Should not be called from interrupt context.
2595 * This routine assumes no locks are held on entry.
2598 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2599 struct fc_rport_identifiers *ids)
2601 struct fc_internal *fci = to_fc_internal(shost->transportt);
2602 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2603 struct fc_rport *rport;
2604 unsigned long flags;
2607 /* ensure any stgt delete functions are done */
2608 fc_flush_work(shost);
2611 * Search the list of "active" rports, for an rport that has been
2612 * deleted, but we've held off the real delete while the target
2613 * is in a "blocked" state.
2615 spin_lock_irqsave(shost->host_lock, flags);
2617 list_for_each_entry(rport, &fc_host->rports, peers) {
2619 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2620 (rport->channel == channel)) {
2622 switch (fc_host->tgtid_bind_type) {
2623 case FC_TGTID_BIND_BY_WWPN:
2624 case FC_TGTID_BIND_NONE:
2625 if (rport->port_name == ids->port_name)
2628 case FC_TGTID_BIND_BY_WWNN:
2629 if (rport->node_name == ids->node_name)
2632 case FC_TGTID_BIND_BY_ID:
2633 if (rport->port_id == ids->port_id)
2640 memcpy(&rport->node_name, &ids->node_name,
2641 sizeof(rport->node_name));
2642 memcpy(&rport->port_name, &ids->port_name,
2643 sizeof(rport->port_name));
2644 rport->port_id = ids->port_id;
2646 rport->port_state = FC_PORTSTATE_ONLINE;
2647 rport->roles = ids->roles;
2649 spin_unlock_irqrestore(shost->host_lock, flags);
2651 if (fci->f->dd_fcrport_size)
2652 memset(rport->dd_data, 0,
2653 fci->f->dd_fcrport_size);
2656 * If we were not a target, cancel the
2657 * io terminate and rport timers, and
2660 * If we were a target, but our new role
2661 * doesn't indicate a target, leave the
2662 * timers running expecting the role to
2663 * change as the target fully logs in. If
2664 * it doesn't, the target will be torn down.
2666 * If we were a target, and our role shows
2667 * we're still a target, cancel the timers
2668 * and kick off a scan.
2671 /* was a target, not in roles */
2672 if ((rport->scsi_target_id != -1) &&
2673 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2677 * Stop the fail io and dev_loss timers.
2678 * If they flush, the port_state will
2679 * be checked and will NOOP the function.
2681 if (!cancel_delayed_work(&rport->fail_io_work))
2682 fc_flush_devloss(shost);
2683 if (!cancel_delayed_work(&rport->dev_loss_work))
2684 fc_flush_devloss(shost);
2686 spin_lock_irqsave(shost->host_lock, flags);
2688 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2689 FC_RPORT_DEVLOSS_PENDING |
2690 FC_RPORT_DEVLOSS_CALLBK_DONE);
2692 /* if target, initiate a scan */
2693 if (rport->scsi_target_id != -1) {
2694 rport->flags |= FC_RPORT_SCAN_PENDING;
2695 scsi_queue_work(shost,
2697 spin_unlock_irqrestore(shost->host_lock,
2699 scsi_target_unblock(&rport->dev);
2701 spin_unlock_irqrestore(shost->host_lock,
2704 fc_bsg_goose_queue(rport);
2712 * Search the bindings array
2713 * Note: if never a FCP target, you won't be on this list
2715 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2717 /* search for a matching consistent binding */
2719 list_for_each_entry(rport, &fc_host->rport_bindings,
2721 if (rport->channel != channel)
2724 switch (fc_host->tgtid_bind_type) {
2725 case FC_TGTID_BIND_BY_WWPN:
2726 if (rport->port_name == ids->port_name)
2729 case FC_TGTID_BIND_BY_WWNN:
2730 if (rport->node_name == ids->node_name)
2733 case FC_TGTID_BIND_BY_ID:
2734 if (rport->port_id == ids->port_id)
2737 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2742 list_move_tail(&rport->peers, &fc_host->rports);
2748 memcpy(&rport->node_name, &ids->node_name,
2749 sizeof(rport->node_name));
2750 memcpy(&rport->port_name, &ids->port_name,
2751 sizeof(rport->port_name));
2752 rport->port_id = ids->port_id;
2753 rport->roles = ids->roles;
2754 rport->port_state = FC_PORTSTATE_ONLINE;
2755 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
2757 if (fci->f->dd_fcrport_size)
2758 memset(rport->dd_data, 0,
2759 fci->f->dd_fcrport_size);
2761 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2762 /* initiate a scan of the target */
2763 rport->flags |= FC_RPORT_SCAN_PENDING;
2764 scsi_queue_work(shost, &rport->scan_work);
2765 spin_unlock_irqrestore(shost->host_lock, flags);
2766 scsi_target_unblock(&rport->dev);
2768 spin_unlock_irqrestore(shost->host_lock, flags);
2774 spin_unlock_irqrestore(shost->host_lock, flags);
2776 /* No consistent binding found - create new remote port entry */
2777 rport = fc_rport_create(shost, channel, ids);
2781 EXPORT_SYMBOL(fc_remote_port_add);
2785 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2786 * @rport: The remote port that no longer exists
2788 * The LLDD calls this routine to notify the transport that a remote
2789 * port is no longer part of the topology. Note: Although a port
2790 * may no longer be part of the topology, it may persist in the remote
2791 * ports displayed by the fc_host. We do this under 2 conditions:
2792 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2793 * This allows the port to temporarily disappear, then reappear without
2794 * disrupting the SCSI device tree attached to it. During the "blocked"
2795 * period the port will still exist.
2796 * 2) If the port was a scsi target and disappears for longer than we
2797 * expect, we'll delete the port and the tear down the SCSI device tree
2798 * attached to it. However, we want to semi-persist the target id assigned
2799 * to that port if it eventually does exist. The port structure will
2800 * remain (although with minimal information) so that the target id
2803 * If the remote port is not an FCP Target, it will be fully torn down
2804 * and deallocated, including the fc_remote_port class device.
2806 * If the remote port is an FCP Target, the port will be placed in a
2807 * temporary blocked state. From the LLDD's perspective, the rport no
2808 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2809 * exists, but all sdevs on it are blocked from further I/O. The following
2812 * If the remote port does not return (signaled by a LLDD call to
2813 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2814 * scsi target is removed - killing all outstanding i/o and removing the
2815 * scsi devices attached ot it. The port structure will be marked Not
2816 * Present and be partially cleared, leaving only enough information to
2817 * recognize the remote port relative to the scsi target id binding if
2818 * it later appears. The port will remain as long as there is a valid
2819 * binding (e.g. until the user changes the binding type or unloads the
2820 * scsi host with the binding).
2822 * If the remote port returns within the dev_loss_tmo value (and matches
2823 * according to the target id binding type), the port structure will be
2824 * reused. If it is no longer a SCSI target, the target will be torn
2825 * down. If it continues to be a SCSI target, then the target will be
2826 * unblocked (allowing i/o to be resumed), and a scan will be activated
2827 * to ensure that all luns are detected.
2829 * Called from normal process context only - cannot be called from interrupt.
2832 * This routine assumes no locks are held on entry.
2835 fc_remote_port_delete(struct fc_rport *rport)
2837 struct Scsi_Host *shost = rport_to_shost(rport);
2838 int timeout = rport->dev_loss_tmo;
2839 unsigned long flags;
2842 * No need to flush the fc_host work_q's, as all adds are synchronous.
2844 * We do need to reclaim the rport scan work element, so eventually
2845 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2846 * there's still a scan pending.
2849 spin_lock_irqsave(shost->host_lock, flags);
2851 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2852 spin_unlock_irqrestore(shost->host_lock, flags);
2857 * In the past, we if this was not an FCP-Target, we would
2858 * unconditionally just jump to deleting the rport.
2859 * However, rports can be used as node containers by the LLDD,
2860 * and its not appropriate to just terminate the rport at the
2861 * first sign of a loss in connectivity. The LLDD may want to
2862 * send ELS traffic to re-validate the login. If the rport is
2863 * immediately deleted, it makes it inappropriate for a node
2865 * So... we now unconditionally wait dev_loss_tmo before
2866 * destroying an rport.
2869 rport->port_state = FC_PORTSTATE_BLOCKED;
2871 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2873 spin_unlock_irqrestore(shost->host_lock, flags);
2875 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2876 shost->active_mode & MODE_TARGET)
2877 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2879 scsi_target_block(&rport->dev);
2881 /* see if we need to kill io faster than waiting for device loss */
2882 if ((rport->fast_io_fail_tmo != -1) &&
2883 (rport->fast_io_fail_tmo < timeout))
2884 fc_queue_devloss_work(shost, &rport->fail_io_work,
2885 rport->fast_io_fail_tmo * HZ);
2887 /* cap the length the devices can be blocked until they are deleted */
2888 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2890 EXPORT_SYMBOL(fc_remote_port_delete);
2893 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2894 * @rport: The remote port that changed.
2895 * @roles: New roles for this port.
2897 * Description: The LLDD calls this routine to notify the transport that the
2898 * roles on a remote port may have changed. The largest effect of this is
2899 * if a port now becomes a FCP Target, it must be allocated a
2900 * scsi target id. If the port is no longer a FCP target, any
2901 * scsi target id value assigned to it will persist in case the
2902 * role changes back to include FCP Target. No changes in the scsi
2903 * midlayer will be invoked if the role changes (in the expectation
2904 * that the role will be resumed. If it doesn't normal error processing
2907 * Should not be called from interrupt context.
2910 * This routine assumes no locks are held on entry.
2913 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2915 struct Scsi_Host *shost = rport_to_shost(rport);
2916 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2917 unsigned long flags;
2921 spin_lock_irqsave(shost->host_lock, flags);
2922 if (roles & FC_PORT_ROLE_FCP_TARGET) {
2923 if (rport->scsi_target_id == -1) {
2924 rport->scsi_target_id = fc_host->next_target_id++;
2926 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
2928 } else if (shost->active_mode & MODE_TARGET) {
2929 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2930 (char *)&rport->node_name);
2932 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2936 rport->roles = roles;
2938 spin_unlock_irqrestore(shost->host_lock, flags);
2942 * There may have been a delete timer running on the
2943 * port. Ensure that it is cancelled as we now know
2944 * the port is an FCP Target.
2945 * Note: we know the rport is exists and in an online
2946 * state as the LLDD would not have had an rport
2947 * reference to pass us.
2949 * Take no action on the del_timer failure as the state
2950 * machine state change will validate the
2953 if (!cancel_delayed_work(&rport->fail_io_work))
2954 fc_flush_devloss(shost);
2955 if (!cancel_delayed_work(&rport->dev_loss_work))
2956 fc_flush_devloss(shost);
2958 spin_lock_irqsave(shost->host_lock, flags);
2959 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2960 FC_RPORT_DEVLOSS_PENDING);
2961 spin_unlock_irqrestore(shost->host_lock, flags);
2963 /* ensure any stgt delete functions are done */
2964 fc_flush_work(shost);
2966 /* initiate a scan of the target */
2967 spin_lock_irqsave(shost->host_lock, flags);
2968 rport->flags |= FC_RPORT_SCAN_PENDING;
2969 scsi_queue_work(shost, &rport->scan_work);
2970 spin_unlock_irqrestore(shost->host_lock, flags);
2971 scsi_target_unblock(&rport->dev);
2974 EXPORT_SYMBOL(fc_remote_port_rolechg);
2977 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
2978 * @work: rport target that failed to reappear in the allotted time.
2980 * Description: An attempt to delete a remote port blocks, and if it fails
2981 * to return in the allotted time this gets called.
2984 fc_timeout_deleted_rport(struct work_struct *work)
2986 struct fc_rport *rport =
2987 container_of(work, struct fc_rport, dev_loss_work.work);
2988 struct Scsi_Host *shost = rport_to_shost(rport);
2989 struct fc_internal *i = to_fc_internal(shost->transportt);
2990 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2991 unsigned long flags;
2992 int do_callback = 0;
2994 spin_lock_irqsave(shost->host_lock, flags);
2996 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2999 * If the port is ONLINE, then it came back. If it was a SCSI
3000 * target, validate it still is. If not, tear down the
3001 * scsi_target on it.
3003 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3004 (rport->scsi_target_id != -1) &&
3005 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3006 dev_printk(KERN_ERR, &rport->dev,
3007 "blocked FC remote port time out: no longer"
3008 " a FCP target, removing starget\n");
3009 spin_unlock_irqrestore(shost->host_lock, flags);
3010 scsi_target_unblock(&rport->dev);
3011 fc_queue_work(shost, &rport->stgt_delete_work);
3015 /* NOOP state - we're flushing workq's */
3016 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3017 spin_unlock_irqrestore(shost->host_lock, flags);
3018 dev_printk(KERN_ERR, &rport->dev,
3019 "blocked FC remote port time out: leaving"
3021 (rport->scsi_target_id != -1) ? " and starget" : "");
3025 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3026 (rport->scsi_target_id == -1)) {
3027 list_del(&rport->peers);
3028 rport->port_state = FC_PORTSTATE_DELETED;
3029 dev_printk(KERN_ERR, &rport->dev,
3030 "blocked FC remote port time out: removing"
3032 (rport->scsi_target_id != -1) ? " and starget" : "");
3033 fc_queue_work(shost, &rport->rport_delete_work);
3034 spin_unlock_irqrestore(shost->host_lock, flags);
3038 dev_printk(KERN_ERR, &rport->dev,
3039 "blocked FC remote port time out: removing target and "
3040 "saving binding\n");
3042 list_move_tail(&rport->peers, &fc_host->rport_bindings);
3045 * Note: We do not remove or clear the hostdata area. This allows
3046 * host-specific target data to persist along with the
3047 * scsi_target_id. It's up to the host to manage it's hostdata area.
3051 * Reinitialize port attributes that may change if the port comes back.
3053 rport->maxframe_size = -1;
3054 rport->supported_classes = FC_COS_UNSPECIFIED;
3055 rport->roles = FC_PORT_ROLE_UNKNOWN;
3056 rport->port_state = FC_PORTSTATE_NOTPRESENT;
3057 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3060 * Pre-emptively kill I/O rather than waiting for the work queue
3061 * item to teardown the starget. (FCOE libFC folks prefer this
3062 * and to have the rport_port_id still set when it's done).
3064 spin_unlock_irqrestore(shost->host_lock, flags);
3065 fc_terminate_rport_io(rport);
3067 spin_lock_irqsave(shost->host_lock, flags);
3069 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3071 /* remove the identifiers that aren't used in the consisting binding */
3072 switch (fc_host->tgtid_bind_type) {
3073 case FC_TGTID_BIND_BY_WWPN:
3074 rport->node_name = -1;
3075 rport->port_id = -1;
3077 case FC_TGTID_BIND_BY_WWNN:
3078 rport->port_name = -1;
3079 rport->port_id = -1;
3081 case FC_TGTID_BIND_BY_ID:
3082 rport->node_name = -1;
3083 rport->port_name = -1;
3085 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3090 * As this only occurs if the remote port (scsi target)
3091 * went away and didn't come back - we'll remove
3092 * all attached scsi devices.
3094 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3095 fc_queue_work(shost, &rport->stgt_delete_work);
3100 spin_unlock_irqrestore(shost->host_lock, flags);
3103 * Notify the driver that the rport is now dead. The LLDD will
3104 * also guarantee that any communication to the rport is terminated
3106 * Note: we set the CALLBK_DONE flag above to correspond
3108 if (do_callback && i->f->dev_loss_tmo_callbk)
3109 i->f->dev_loss_tmo_callbk(rport);
3114 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3115 * @work: rport to terminate io on.
3117 * Notes: Only requests the failure of the io, not that all are flushed
3118 * prior to returning.
3121 fc_timeout_fail_rport_io(struct work_struct *work)
3123 struct fc_rport *rport =
3124 container_of(work, struct fc_rport, fail_io_work.work);
3126 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3129 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3130 fc_terminate_rport_io(rport);
3134 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3135 * @work: remote port to be scanned.
3138 fc_scsi_scan_rport(struct work_struct *work)
3140 struct fc_rport *rport =
3141 container_of(work, struct fc_rport, scan_work);
3142 struct Scsi_Host *shost = rport_to_shost(rport);
3143 struct fc_internal *i = to_fc_internal(shost->transportt);
3144 unsigned long flags;
3146 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3147 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3148 !(i->f->disable_target_scan)) {
3149 scsi_scan_target(&rport->dev, rport->channel,
3150 rport->scsi_target_id, SCAN_WILD_CARD, 1);
3153 spin_lock_irqsave(shost->host_lock, flags);
3154 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3155 spin_unlock_irqrestore(shost->host_lock, flags);
3160 * fc_vport_setup - allocates and creates a FC virtual port.
3161 * @shost: scsi host the virtual port is connected to.
3162 * @channel: Channel on shost port connected to.
3163 * @pdev: parent device for vport
3164 * @ids: The world wide names, FC4 port roles, etc for
3166 * @ret_vport: The pointer to the created vport.
3168 * Allocates and creates the vport structure, calls the parent host
3169 * to instantiate the vport, the completes w/ class and sysfs creation.
3172 * This routine assumes no locks are held on entry.
3175 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3176 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3178 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3179 struct fc_internal *fci = to_fc_internal(shost->transportt);
3180 struct fc_vport *vport;
3182 unsigned long flags;
3188 if ( ! fci->f->vport_create)
3191 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3192 vport = kzalloc(size, GFP_KERNEL);
3193 if (unlikely(!vport)) {
3194 printk(KERN_ERR "%s: allocation failure\n", __func__);
3198 vport->vport_state = FC_VPORT_UNKNOWN;
3199 vport->vport_last_state = FC_VPORT_UNKNOWN;
3200 vport->node_name = ids->node_name;
3201 vport->port_name = ids->port_name;
3202 vport->roles = ids->roles;
3203 vport->vport_type = ids->vport_type;
3204 if (fci->f->dd_fcvport_size)
3205 vport->dd_data = &vport[1];
3206 vport->shost = shost;
3207 vport->channel = channel;
3208 vport->flags = FC_VPORT_CREATING;
3209 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3211 spin_lock_irqsave(shost->host_lock, flags);
3213 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3214 spin_unlock_irqrestore(shost->host_lock, flags);
3218 fc_host->npiv_vports_inuse++;
3219 vport->number = fc_host->next_vport_number++;
3220 list_add_tail(&vport->peers, &fc_host->vports);
3221 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3223 spin_unlock_irqrestore(shost->host_lock, flags);
3226 device_initialize(dev); /* takes self reference */
3227 dev->parent = get_device(pdev); /* takes parent reference */
3228 dev->release = fc_vport_dev_release;
3229 dev_set_name(dev, "vport-%d:%d-%d",
3230 shost->host_no, channel, vport->number);
3231 transport_setup_device(dev);
3233 error = device_add(dev);
3235 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3238 transport_add_device(dev);
3239 transport_configure_device(dev);
3241 error = fci->f->vport_create(vport, ids->disable);
3243 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3244 goto delete_vport_all;
3248 * if the parent isn't the physical adapter's Scsi_Host, ensure
3249 * the Scsi_Host at least contains ia symlink to the vport.
3251 if (pdev != &shost->shost_gendev) {
3252 error = sysfs_create_link(&shost->shost_gendev.kobj,
3253 &dev->kobj, dev_name(dev));
3256 "%s: Cannot create vport symlinks for "
3258 __func__, dev_name(dev), error);
3260 spin_lock_irqsave(shost->host_lock, flags);
3261 vport->flags &= ~FC_VPORT_CREATING;
3262 spin_unlock_irqrestore(shost->host_lock, flags);
3264 dev_printk(KERN_NOTICE, pdev,
3265 "%s created via shost%d channel %d\n", dev_name(dev),
3266 shost->host_no, channel);
3273 transport_remove_device(dev);
3276 transport_destroy_device(dev);
3277 spin_lock_irqsave(shost->host_lock, flags);
3278 list_del(&vport->peers);
3279 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3280 fc_host->npiv_vports_inuse--;
3281 spin_unlock_irqrestore(shost->host_lock, flags);
3282 put_device(dev->parent);
3289 * fc_vport_create - Admin App or LLDD requests creation of a vport
3290 * @shost: scsi host the virtual port is connected to.
3291 * @channel: channel on shost port connected to.
3292 * @ids: The world wide names, FC4 port roles, etc for
3296 * This routine assumes no locks are held on entry.
3299 fc_vport_create(struct Scsi_Host *shost, int channel,
3300 struct fc_vport_identifiers *ids)
3303 struct fc_vport *vport;
3305 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3307 return stat ? NULL : vport;
3309 EXPORT_SYMBOL(fc_vport_create);
3312 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3313 * @vport: fc_vport to be terminated
3315 * Calls the LLDD vport_delete() function, then deallocates and removes
3316 * the vport from the shost and object tree.
3319 * This routine assumes no locks are held on entry.
3322 fc_vport_terminate(struct fc_vport *vport)
3324 struct Scsi_Host *shost = vport_to_shost(vport);
3325 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3326 struct fc_internal *i = to_fc_internal(shost->transportt);
3327 struct device *dev = &vport->dev;
3328 unsigned long flags;
3331 spin_lock_irqsave(shost->host_lock, flags);
3332 if (vport->flags & FC_VPORT_CREATING) {
3333 spin_unlock_irqrestore(shost->host_lock, flags);
3336 if (vport->flags & (FC_VPORT_DEL)) {
3337 spin_unlock_irqrestore(shost->host_lock, flags);
3340 vport->flags |= FC_VPORT_DELETING;
3341 spin_unlock_irqrestore(shost->host_lock, flags);
3343 if (i->f->vport_delete)
3344 stat = i->f->vport_delete(vport);
3348 spin_lock_irqsave(shost->host_lock, flags);
3349 vport->flags &= ~FC_VPORT_DELETING;
3351 vport->flags |= FC_VPORT_DELETED;
3352 list_del(&vport->peers);
3353 fc_host->npiv_vports_inuse--;
3354 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3356 spin_unlock_irqrestore(shost->host_lock, flags);
3361 if (dev->parent != &shost->shost_gendev)
3362 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3363 transport_remove_device(dev);
3365 transport_destroy_device(dev);
3368 * Removing our self-reference should mean our
3369 * release function gets called, which will drop the remaining
3370 * parent reference and free the data structure.
3372 put_device(dev); /* for self-reference */
3374 return 0; /* SUCCESS */
3376 EXPORT_SYMBOL(fc_vport_terminate);
3379 * fc_vport_sched_delete - workq-based delete request for a vport
3380 * @work: vport to be deleted.
3383 fc_vport_sched_delete(struct work_struct *work)
3385 struct fc_vport *vport =
3386 container_of(work, struct fc_vport, vport_delete_work);
3389 stat = fc_vport_terminate(vport);
3391 dev_printk(KERN_ERR, vport->dev.parent,
3392 "%s: %s could not be deleted created via "
3393 "shost%d channel %d - error %d\n", __func__,
3394 dev_name(&vport->dev), vport->shost->host_no,
3395 vport->channel, stat);
3405 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3406 * @job: fc_bsg_job that is to be torn down
3409 fc_destroy_bsgjob(struct fc_bsg_job *job)
3411 unsigned long flags;
3413 spin_lock_irqsave(&job->job_lock, flags);
3415 spin_unlock_irqrestore(&job->job_lock, flags);
3418 spin_unlock_irqrestore(&job->job_lock, flags);
3420 put_device(job->dev); /* release reference for the request */
3422 kfree(job->request_payload.sg_list);
3423 kfree(job->reply_payload.sg_list);
3428 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3430 * @job: fc_bsg_job that is complete
3433 fc_bsg_jobdone(struct fc_bsg_job *job)
3435 struct request *req = job->req;
3436 struct request *rsp = req->next_rq;
3439 err = job->req->errors = job->reply->result;
3442 /* we're only returning the result field in the reply */
3443 job->req->sense_len = sizeof(uint32_t);
3445 job->req->sense_len = job->reply_len;
3447 /* we assume all request payload was transferred, residual == 0 */
3451 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3453 /* set reply (bidi) residual */
3454 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3457 blk_complete_request(req);
3461 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3462 * @rq: BSG request that holds the job to be destroyed
3464 static void fc_bsg_softirq_done(struct request *rq)
3466 struct fc_bsg_job *job = rq->special;
3467 unsigned long flags;
3469 spin_lock_irqsave(&job->job_lock, flags);
3470 job->state_flags |= FC_RQST_STATE_DONE;
3472 spin_unlock_irqrestore(&job->job_lock, flags);
3474 blk_end_request_all(rq, rq->errors);
3475 fc_destroy_bsgjob(job);
3479 * fc_bsg_job_timeout - handler for when a bsg request timesout
3480 * @req: request that timed out
3482 static enum blk_eh_timer_return
3483 fc_bsg_job_timeout(struct request *req)
3485 struct fc_bsg_job *job = (void *) req->special;
3486 struct Scsi_Host *shost = job->shost;
3487 struct fc_internal *i = to_fc_internal(shost->transportt);
3488 unsigned long flags;
3489 int err = 0, done = 0;
3491 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3492 return BLK_EH_RESET_TIMER;
3494 spin_lock_irqsave(&job->job_lock, flags);
3495 if (job->state_flags & FC_RQST_STATE_DONE)
3499 spin_unlock_irqrestore(&job->job_lock, flags);
3501 if (!done && i->f->bsg_timeout) {
3502 /* call LLDD to abort the i/o as it has timed out */
3503 err = i->f->bsg_timeout(job);
3505 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3506 "abort failed with status %d\n", err);
3509 /* the blk_end_sync_io() doesn't check the error */
3511 return BLK_EH_NOT_HANDLED;
3513 return BLK_EH_HANDLED;
3517 fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3519 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3521 BUG_ON(!req->nr_phys_segments);
3523 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3526 sg_init_table(buf->sg_list, req->nr_phys_segments);
3527 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3528 buf->payload_len = blk_rq_bytes(req);
3534 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3536 * @shost: SCSI Host corresponding to the bsg object
3537 * @rport: (optional) FC Remote Port corresponding to the bsg object
3538 * @req: BSG request that needs a job structure
3541 fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3542 struct request *req)
3544 struct fc_internal *i = to_fc_internal(shost->transportt);
3545 struct request *rsp = req->next_rq;
3546 struct fc_bsg_job *job;
3549 BUG_ON(req->special);
3551 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3557 * Note: this is a bit silly.
3558 * The request gets formatted as a SGIO v4 ioctl request, which
3559 * then gets reformatted as a blk request, which then gets
3560 * reformatted as a fc bsg request. And on completion, we have
3561 * to wrap return results such that SGIO v4 thinks it was a scsi
3562 * status. I hope this was all worth it.
3569 if (i->f->dd_bsg_size)
3570 job->dd_data = (void *)&job[1];
3571 spin_lock_init(&job->job_lock);
3572 job->request = (struct fc_bsg_request *)req->cmd;
3573 job->request_len = req->cmd_len;
3574 job->reply = req->sense;
3575 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3578 ret = fc_bsg_map_buffer(&job->request_payload, req);
3580 goto failjob_rls_job;
3582 if (rsp && rsp->bio) {
3583 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3585 goto failjob_rls_rqst_payload;
3587 job->job_done = fc_bsg_jobdone;
3589 job->dev = &rport->dev;
3591 job->dev = &shost->shost_gendev;
3592 get_device(job->dev); /* take a reference for the request */
3599 failjob_rls_rqst_payload:
3600 kfree(job->request_payload.sg_list);
3607 enum fc_dispatch_result {
3608 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3609 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3610 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3615 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3616 * @q: fc host request queue
3617 * @shost: scsi host rport attached to
3618 * @job: bsg job to be processed
3620 static enum fc_dispatch_result
3621 fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3622 struct fc_bsg_job *job)
3624 struct fc_internal *i = to_fc_internal(shost->transportt);
3625 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3628 /* Validate the host command */
3629 switch (job->request->msgcode) {
3630 case FC_BSG_HST_ADD_RPORT:
3631 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3634 case FC_BSG_HST_DEL_RPORT:
3635 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3638 case FC_BSG_HST_ELS_NOLOGIN:
3639 cmdlen += sizeof(struct fc_bsg_host_els);
3640 /* there better be a xmt and rcv payloads */
3641 if ((!job->request_payload.payload_len) ||
3642 (!job->reply_payload.payload_len)) {
3649 cmdlen += sizeof(struct fc_bsg_host_ct);
3650 /* there better be xmt and rcv payloads */
3651 if ((!job->request_payload.payload_len) ||
3652 (!job->reply_payload.payload_len)) {
3658 case FC_BSG_HST_VENDOR:
3659 cmdlen += sizeof(struct fc_bsg_host_vendor);
3660 if ((shost->hostt->vendor_id == 0L) ||
3661 (job->request->rqst_data.h_vendor.vendor_id !=
3662 shost->hostt->vendor_id)) {
3673 /* check if we really have all the request data needed */
3674 if (job->request_len < cmdlen) {
3679 ret = i->f->bsg_request(job);
3681 return FC_DISPATCH_UNLOCKED;
3684 /* return the errno failure code as the only status */
3685 BUG_ON(job->reply_len < sizeof(uint32_t));
3686 job->reply->reply_payload_rcv_len = 0;
3687 job->reply->result = ret;
3688 job->reply_len = sizeof(uint32_t);
3689 fc_bsg_jobdone(job);
3690 return FC_DISPATCH_UNLOCKED;
3695 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3696 * @rport: rport to be restarted
3699 fc_bsg_goose_queue(struct fc_rport *rport)
3702 unsigned long flags;
3707 get_device(&rport->dev);
3709 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
3710 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3711 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3713 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3714 __blk_run_queue(rport->rqst_q);
3716 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
3717 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
3719 put_device(&rport->dev);
3724 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3725 * @q: rport request queue
3726 * @shost: scsi host rport attached to
3727 * @rport: rport request destined to
3728 * @job: bsg job to be processed
3730 static enum fc_dispatch_result
3731 fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3732 struct fc_rport *rport, struct fc_bsg_job *job)
3734 struct fc_internal *i = to_fc_internal(shost->transportt);
3735 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3738 /* Validate the rport command */
3739 switch (job->request->msgcode) {
3740 case FC_BSG_RPT_ELS:
3741 cmdlen += sizeof(struct fc_bsg_rport_els);
3745 cmdlen += sizeof(struct fc_bsg_rport_ct);
3747 /* there better be xmt and rcv payloads */
3748 if ((!job->request_payload.payload_len) ||
3749 (!job->reply_payload.payload_len)) {
3751 goto fail_rport_msg;
3756 goto fail_rport_msg;
3759 /* check if we really have all the request data needed */
3760 if (job->request_len < cmdlen) {
3762 goto fail_rport_msg;
3765 ret = i->f->bsg_request(job);
3767 return FC_DISPATCH_UNLOCKED;
3770 /* return the errno failure code as the only status */
3771 BUG_ON(job->reply_len < sizeof(uint32_t));
3772 job->reply->reply_payload_rcv_len = 0;
3773 job->reply->result = ret;
3774 job->reply_len = sizeof(uint32_t);
3775 fc_bsg_jobdone(job);
3776 return FC_DISPATCH_UNLOCKED;
3781 * fc_bsg_request_handler - generic handler for bsg requests
3782 * @q: request queue to manage
3783 * @shost: Scsi_Host related to the bsg object
3784 * @rport: FC remote port related to the bsg object (optional)
3785 * @dev: device structure for bsg object
3788 fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3789 struct fc_rport *rport, struct device *dev)
3791 struct request *req;
3792 struct fc_bsg_job *job;
3793 enum fc_dispatch_result ret;
3795 if (!get_device(dev))
3798 while (!blk_queue_plugged(q)) {
3799 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED))
3802 req = blk_fetch_request(q);
3806 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3807 req->errors = -ENXIO;
3808 spin_unlock_irq(q->queue_lock);
3809 blk_end_request(req, -ENXIO, blk_rq_bytes(req));
3810 spin_lock_irq(q->queue_lock);
3814 spin_unlock_irq(q->queue_lock);
3816 ret = fc_req_to_bsgjob(shost, rport, req);
3819 blk_end_request(req, ret, blk_rq_bytes(req));
3820 spin_lock_irq(q->queue_lock);
3826 /* check if we have the msgcode value at least */
3827 if (job->request_len < sizeof(uint32_t)) {
3828 BUG_ON(job->reply_len < sizeof(uint32_t));
3829 job->reply->reply_payload_rcv_len = 0;
3830 job->reply->result = -ENOMSG;
3831 job->reply_len = sizeof(uint32_t);
3832 fc_bsg_jobdone(job);
3833 spin_lock_irq(q->queue_lock);
3837 /* the dispatch routines will unlock the queue_lock */
3839 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3841 ret = fc_bsg_host_dispatch(q, shost, job);
3843 /* did dispatcher hit state that can't process any more */
3844 if (ret == FC_DISPATCH_BREAK)
3847 /* did dispatcher had released the lock */
3848 if (ret == FC_DISPATCH_UNLOCKED)
3849 spin_lock_irq(q->queue_lock);
3852 spin_unlock_irq(q->queue_lock);
3854 spin_lock_irq(q->queue_lock);
3859 * fc_bsg_host_handler - handler for bsg requests for a fc host
3860 * @q: fc host request queue
3863 fc_bsg_host_handler(struct request_queue *q)
3865 struct Scsi_Host *shost = q->queuedata;
3867 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3872 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3873 * @q: rport request queue
3876 fc_bsg_rport_handler(struct request_queue *q)
3878 struct fc_rport *rport = q->queuedata;
3879 struct Scsi_Host *shost = rport_to_shost(rport);
3881 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3886 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3887 * @shost: shost for fc_host
3888 * @fc_host: fc_host adding the structures to
3891 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3893 struct device *dev = &shost->shost_gendev;
3894 struct fc_internal *i = to_fc_internal(shost->transportt);
3895 struct request_queue *q;
3899 fc_host->rqst_q = NULL;
3901 if (!i->f->bsg_request)
3904 snprintf(bsg_name, sizeof(bsg_name),
3905 "fc_host%d", shost->host_no);
3907 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3909 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3910 "initialize - no request queue\n",
3915 q->queuedata = shost;
3916 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3917 blk_queue_softirq_done(q, fc_bsg_softirq_done);
3918 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3919 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3921 err = bsg_register_queue(q, dev, bsg_name, NULL);
3923 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3924 "initialize - register queue\n",
3926 blk_cleanup_queue(q);
3930 fc_host->rqst_q = q;
3936 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3937 * @shost: shost that rport is attached to
3938 * @rport: rport that the bsg hooks are being attached to
3941 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3943 struct device *dev = &rport->dev;
3944 struct fc_internal *i = to_fc_internal(shost->transportt);
3945 struct request_queue *q;
3948 rport->rqst_q = NULL;
3950 if (!i->f->bsg_request)
3953 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
3955 printk(KERN_ERR "%s: bsg interface failed to "
3956 "initialize - no request queue\n",
3961 q->queuedata = rport;
3962 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3963 blk_queue_softirq_done(q, fc_bsg_softirq_done);
3964 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3965 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3967 err = bsg_register_queue(q, dev, NULL, NULL);
3969 printk(KERN_ERR "%s: bsg interface failed to "
3970 "initialize - register queue\n",
3972 blk_cleanup_queue(q);
3982 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3983 * @q: the request_queue that is to be torn down.
3986 fc_bsg_remove(struct request_queue *q)
3989 bsg_unregister_queue(q);
3990 blk_cleanup_queue(q);
3995 /* Original Author: Martin Hicks */
3996 MODULE_AUTHOR("James Smart");
3997 MODULE_DESCRIPTION("FC Transport Attributes");
3998 MODULE_LICENSE("GPL");
4000 module_init(fc_transport_init);
4001 module_exit(fc_transport_exit);