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 21
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
;
2400 * if a scan is pending, flush the SCSI Host work_q so that
2401 * that we can reclaim the rport scan work element.
2403 if (rport
->flags
& FC_RPORT_SCAN_PENDING
)
2404 scsi_flush_work(shost
);
2406 fc_terminate_rport_io(rport
);
2409 * Cancel any outstanding timers. These should really exist
2410 * only when rmmod'ing the LLDD and we're asking for
2411 * immediate termination of the rports
2413 spin_lock_irqsave(shost
->host_lock
, flags
);
2414 if (rport
->flags
& FC_RPORT_DEVLOSS_PENDING
) {
2415 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2416 if (!cancel_delayed_work(&rport
->fail_io_work
))
2417 fc_flush_devloss(shost
);
2418 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2419 fc_flush_devloss(shost
);
2420 spin_lock_irqsave(shost
->host_lock
, flags
);
2421 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
2423 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2425 /* Delete SCSI target and sdevs */
2426 if (rport
->scsi_target_id
!= -1)
2427 fc_starget_delete(&rport
->stgt_delete_work
);
2430 * Notify the driver that the rport is now dead. The LLDD will
2431 * also guarantee that any communication to the rport is terminated
2433 * Avoid this call if we already called it when we preserved the
2434 * rport for the binding.
2436 if (!(rport
->flags
& FC_RPORT_DEVLOSS_CALLBK_DONE
) &&
2437 (i
->f
->dev_loss_tmo_callbk
))
2438 i
->f
->dev_loss_tmo_callbk(rport
);
2440 fc_bsg_remove(rport
->rqst_q
);
2442 transport_remove_device(dev
);
2444 transport_destroy_device(dev
);
2445 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2446 put_device(dev
); /* for self-reference */
2451 * fc_rport_create - allocates and creates a remote FC port.
2452 * @shost: scsi host the remote port is connected to.
2453 * @channel: Channel on shost port connected to.
2454 * @ids: The world wide names, fc address, and FC4 port
2455 * roles for the remote port.
2457 * Allocates and creates the remoter port structure, including the
2458 * class and sysfs creation.
2461 * This routine assumes no locks are held on entry.
2463 static struct fc_rport
*
2464 fc_rport_create(struct Scsi_Host
*shost
, int channel
,
2465 struct fc_rport_identifiers
*ids
)
2467 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2468 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2469 struct fc_rport
*rport
;
2471 unsigned long flags
;
2475 size
= (sizeof(struct fc_rport
) + fci
->f
->dd_fcrport_size
);
2476 rport
= kzalloc(size
, GFP_KERNEL
);
2477 if (unlikely(!rport
)) {
2478 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
2482 rport
->maxframe_size
= -1;
2483 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
2484 rport
->dev_loss_tmo
= fc_dev_loss_tmo
;
2485 memcpy(&rport
->node_name
, &ids
->node_name
, sizeof(rport
->node_name
));
2486 memcpy(&rport
->port_name
, &ids
->port_name
, sizeof(rport
->port_name
));
2487 rport
->port_id
= ids
->port_id
;
2488 rport
->roles
= ids
->roles
;
2489 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2490 if (fci
->f
->dd_fcrport_size
)
2491 rport
->dd_data
= &rport
[1];
2492 rport
->channel
= channel
;
2493 rport
->fast_io_fail_tmo
= -1;
2495 INIT_DELAYED_WORK(&rport
->dev_loss_work
, fc_timeout_deleted_rport
);
2496 INIT_DELAYED_WORK(&rport
->fail_io_work
, fc_timeout_fail_rport_io
);
2497 INIT_WORK(&rport
->scan_work
, fc_scsi_scan_rport
);
2498 INIT_WORK(&rport
->stgt_delete_work
, fc_starget_delete
);
2499 INIT_WORK(&rport
->rport_delete_work
, fc_rport_final_delete
);
2501 spin_lock_irqsave(shost
->host_lock
, flags
);
2503 rport
->number
= fc_host
->next_rport_number
++;
2504 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)
2505 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2507 rport
->scsi_target_id
= -1;
2508 list_add_tail(&rport
->peers
, &fc_host
->rports
);
2509 get_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2511 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2514 device_initialize(dev
); /* takes self reference */
2515 dev
->parent
= get_device(&shost
->shost_gendev
); /* parent reference */
2516 dev
->release
= fc_rport_dev_release
;
2517 dev_set_name(dev
, "rport-%d:%d-%d",
2518 shost
->host_no
, channel
, rport
->number
);
2519 transport_setup_device(dev
);
2521 error
= device_add(dev
);
2523 printk(KERN_ERR
"FC Remote Port device_add failed\n");
2526 transport_add_device(dev
);
2527 transport_configure_device(dev
);
2529 fc_bsg_rportadd(shost
, rport
);
2530 /* ignore any bsg add error - we just can't do sgio */
2532 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2533 /* initiate a scan of the target */
2534 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2535 scsi_queue_work(shost
, &rport
->scan_work
);
2541 transport_destroy_device(dev
);
2542 spin_lock_irqsave(shost
->host_lock
, flags
);
2543 list_del(&rport
->peers
);
2544 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2545 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2546 put_device(dev
->parent
);
2552 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2553 * @shost: scsi host the remote port is connected to.
2554 * @channel: Channel on shost port connected to.
2555 * @ids: The world wide names, fc address, and FC4 port
2556 * roles for the remote port.
2558 * The LLDD calls this routine to notify the transport of the existence
2559 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2560 * of the port, it's FC address (port_id), and the FC4 roles that are
2561 * active for the port.
2563 * For ports that are FCP targets (aka scsi targets), the FC transport
2564 * maintains consistent target id bindings on behalf of the LLDD.
2565 * A consistent target id binding is an assignment of a target id to
2566 * a remote port identifier, which persists while the scsi host is
2567 * attached. The remote port can disappear, then later reappear, and
2568 * it's target id assignment remains the same. This allows for shifts
2569 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2570 * changes to the scsi subsystem which is based on scsi host number and
2571 * target id values. Bindings are only valid during the attachment of
2572 * the scsi host. If the host detaches, then later re-attaches, target
2573 * id bindings may change.
2575 * This routine is responsible for returning a remote port structure.
2576 * The routine will search the list of remote ports it maintains
2577 * internally on behalf of consistent target id mappings. If found, the
2578 * remote port structure will be reused. Otherwise, a new remote port
2579 * structure will be allocated.
2581 * Whenever a remote port is allocated, a new fc_remote_port class
2582 * device is created.
2584 * Should not be called from interrupt context.
2587 * This routine assumes no locks are held on entry.
2590 fc_remote_port_add(struct Scsi_Host
*shost
, int channel
,
2591 struct fc_rport_identifiers
*ids
)
2593 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2594 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2595 struct fc_rport
*rport
;
2596 unsigned long flags
;
2599 /* ensure any stgt delete functions are done */
2600 fc_flush_work(shost
);
2603 * Search the list of "active" rports, for an rport that has been
2604 * deleted, but we've held off the real delete while the target
2605 * is in a "blocked" state.
2607 spin_lock_irqsave(shost
->host_lock
, flags
);
2609 list_for_each_entry(rport
, &fc_host
->rports
, peers
) {
2611 if ((rport
->port_state
== FC_PORTSTATE_BLOCKED
) &&
2612 (rport
->channel
== channel
)) {
2614 switch (fc_host
->tgtid_bind_type
) {
2615 case FC_TGTID_BIND_BY_WWPN
:
2616 case FC_TGTID_BIND_NONE
:
2617 if (rport
->port_name
== ids
->port_name
)
2620 case FC_TGTID_BIND_BY_WWNN
:
2621 if (rport
->node_name
== ids
->node_name
)
2624 case FC_TGTID_BIND_BY_ID
:
2625 if (rport
->port_id
== ids
->port_id
)
2632 memcpy(&rport
->node_name
, &ids
->node_name
,
2633 sizeof(rport
->node_name
));
2634 memcpy(&rport
->port_name
, &ids
->port_name
,
2635 sizeof(rport
->port_name
));
2636 rport
->port_id
= ids
->port_id
;
2638 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2639 rport
->roles
= ids
->roles
;
2641 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2643 if (fci
->f
->dd_fcrport_size
)
2644 memset(rport
->dd_data
, 0,
2645 fci
->f
->dd_fcrport_size
);
2648 * If we were not a target, cancel the
2649 * io terminate and rport timers, and
2652 * If we were a target, but our new role
2653 * doesn't indicate a target, leave the
2654 * timers running expecting the role to
2655 * change as the target fully logs in. If
2656 * it doesn't, the target will be torn down.
2658 * If we were a target, and our role shows
2659 * we're still a target, cancel the timers
2660 * and kick off a scan.
2663 /* was a target, not in roles */
2664 if ((rport
->scsi_target_id
!= -1) &&
2665 (!(ids
->roles
& FC_PORT_ROLE_FCP_TARGET
)))
2669 * Stop the fail io and dev_loss timers.
2670 * If they flush, the port_state will
2671 * be checked and will NOOP the function.
2673 if (!cancel_delayed_work(&rport
->fail_io_work
))
2674 fc_flush_devloss(shost
);
2675 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2676 fc_flush_devloss(shost
);
2678 spin_lock_irqsave(shost
->host_lock
, flags
);
2680 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2681 FC_RPORT_DEVLOSS_PENDING
|
2682 FC_RPORT_DEVLOSS_CALLBK_DONE
);
2684 /* if target, initiate a scan */
2685 if (rport
->scsi_target_id
!= -1) {
2686 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2687 scsi_queue_work(shost
,
2689 spin_unlock_irqrestore(shost
->host_lock
,
2691 scsi_target_unblock(&rport
->dev
);
2693 spin_unlock_irqrestore(shost
->host_lock
,
2696 fc_bsg_goose_queue(rport
);
2704 * Search the bindings array
2705 * Note: if never a FCP target, you won't be on this list
2707 if (fc_host
->tgtid_bind_type
!= FC_TGTID_BIND_NONE
) {
2709 /* search for a matching consistent binding */
2711 list_for_each_entry(rport
, &fc_host
->rport_bindings
,
2713 if (rport
->channel
!= channel
)
2716 switch (fc_host
->tgtid_bind_type
) {
2717 case FC_TGTID_BIND_BY_WWPN
:
2718 if (rport
->port_name
== ids
->port_name
)
2721 case FC_TGTID_BIND_BY_WWNN
:
2722 if (rport
->node_name
== ids
->node_name
)
2725 case FC_TGTID_BIND_BY_ID
:
2726 if (rport
->port_id
== ids
->port_id
)
2729 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
2734 list_move_tail(&rport
->peers
, &fc_host
->rports
);
2740 memcpy(&rport
->node_name
, &ids
->node_name
,
2741 sizeof(rport
->node_name
));
2742 memcpy(&rport
->port_name
, &ids
->port_name
,
2743 sizeof(rport
->port_name
));
2744 rport
->port_id
= ids
->port_id
;
2745 rport
->roles
= ids
->roles
;
2746 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2747 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
2749 if (fci
->f
->dd_fcrport_size
)
2750 memset(rport
->dd_data
, 0,
2751 fci
->f
->dd_fcrport_size
);
2753 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2754 /* initiate a scan of the target */
2755 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2756 scsi_queue_work(shost
, &rport
->scan_work
);
2757 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2758 scsi_target_unblock(&rport
->dev
);
2760 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2766 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2768 /* No consistent binding found - create new remote port entry */
2769 rport
= fc_rport_create(shost
, channel
, ids
);
2773 EXPORT_SYMBOL(fc_remote_port_add
);
2777 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2778 * @rport: The remote port that no longer exists
2780 * The LLDD calls this routine to notify the transport that a remote
2781 * port is no longer part of the topology. Note: Although a port
2782 * may no longer be part of the topology, it may persist in the remote
2783 * ports displayed by the fc_host. We do this under 2 conditions:
2784 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2785 * This allows the port to temporarily disappear, then reappear without
2786 * disrupting the SCSI device tree attached to it. During the "blocked"
2787 * period the port will still exist.
2788 * 2) If the port was a scsi target and disappears for longer than we
2789 * expect, we'll delete the port and the tear down the SCSI device tree
2790 * attached to it. However, we want to semi-persist the target id assigned
2791 * to that port if it eventually does exist. The port structure will
2792 * remain (although with minimal information) so that the target id
2795 * If the remote port is not an FCP Target, it will be fully torn down
2796 * and deallocated, including the fc_remote_port class device.
2798 * If the remote port is an FCP Target, the port will be placed in a
2799 * temporary blocked state. From the LLDD's perspective, the rport no
2800 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2801 * exists, but all sdevs on it are blocked from further I/O. The following
2804 * If the remote port does not return (signaled by a LLDD call to
2805 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2806 * scsi target is removed - killing all outstanding i/o and removing the
2807 * scsi devices attached ot it. The port structure will be marked Not
2808 * Present and be partially cleared, leaving only enough information to
2809 * recognize the remote port relative to the scsi target id binding if
2810 * it later appears. The port will remain as long as there is a valid
2811 * binding (e.g. until the user changes the binding type or unloads the
2812 * scsi host with the binding).
2814 * If the remote port returns within the dev_loss_tmo value (and matches
2815 * according to the target id binding type), the port structure will be
2816 * reused. If it is no longer a SCSI target, the target will be torn
2817 * down. If it continues to be a SCSI target, then the target will be
2818 * unblocked (allowing i/o to be resumed), and a scan will be activated
2819 * to ensure that all luns are detected.
2821 * Called from normal process context only - cannot be called from interrupt.
2824 * This routine assumes no locks are held on entry.
2827 fc_remote_port_delete(struct fc_rport
*rport
)
2829 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2830 int timeout
= rport
->dev_loss_tmo
;
2831 unsigned long flags
;
2834 * No need to flush the fc_host work_q's, as all adds are synchronous.
2836 * We do need to reclaim the rport scan work element, so eventually
2837 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2838 * there's still a scan pending.
2841 spin_lock_irqsave(shost
->host_lock
, flags
);
2843 if (rport
->port_state
!= FC_PORTSTATE_ONLINE
) {
2844 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2849 * In the past, we if this was not an FCP-Target, we would
2850 * unconditionally just jump to deleting the rport.
2851 * However, rports can be used as node containers by the LLDD,
2852 * and its not appropriate to just terminate the rport at the
2853 * first sign of a loss in connectivity. The LLDD may want to
2854 * send ELS traffic to re-validate the login. If the rport is
2855 * immediately deleted, it makes it inappropriate for a node
2857 * So... we now unconditionally wait dev_loss_tmo before
2858 * destroying an rport.
2861 rport
->port_state
= FC_PORTSTATE_BLOCKED
;
2863 rport
->flags
|= FC_RPORT_DEVLOSS_PENDING
;
2865 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2867 if (rport
->roles
& FC_PORT_ROLE_FCP_INITIATOR
&&
2868 shost
->active_mode
& MODE_TARGET
)
2869 fc_tgt_it_nexus_destroy(shost
, (unsigned long)rport
);
2871 scsi_target_block(&rport
->dev
);
2873 /* see if we need to kill io faster than waiting for device loss */
2874 if ((rport
->fast_io_fail_tmo
!= -1) &&
2875 (rport
->fast_io_fail_tmo
< timeout
))
2876 fc_queue_devloss_work(shost
, &rport
->fail_io_work
,
2877 rport
->fast_io_fail_tmo
* HZ
);
2879 /* cap the length the devices can be blocked until they are deleted */
2880 fc_queue_devloss_work(shost
, &rport
->dev_loss_work
, timeout
* HZ
);
2882 EXPORT_SYMBOL(fc_remote_port_delete
);
2885 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2886 * @rport: The remote port that changed.
2887 * @roles: New roles for this port.
2889 * Description: The LLDD calls this routine to notify the transport that the
2890 * roles on a remote port may have changed. The largest effect of this is
2891 * if a port now becomes a FCP Target, it must be allocated a
2892 * scsi target id. If the port is no longer a FCP target, any
2893 * scsi target id value assigned to it will persist in case the
2894 * role changes back to include FCP Target. No changes in the scsi
2895 * midlayer will be invoked if the role changes (in the expectation
2896 * that the role will be resumed. If it doesn't normal error processing
2899 * Should not be called from interrupt context.
2902 * This routine assumes no locks are held on entry.
2905 fc_remote_port_rolechg(struct fc_rport
*rport
, u32 roles
)
2907 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2908 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2909 unsigned long flags
;
2913 spin_lock_irqsave(shost
->host_lock
, flags
);
2914 if (roles
& FC_PORT_ROLE_FCP_TARGET
) {
2915 if (rport
->scsi_target_id
== -1) {
2916 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2918 } else if (!(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
))
2920 } else if (shost
->active_mode
& MODE_TARGET
) {
2921 ret
= fc_tgt_it_nexus_create(shost
, (unsigned long)rport
,
2922 (char *)&rport
->node_name
);
2924 printk(KERN_ERR
"FC Remore Port tgt nexus failed %d\n",
2928 rport
->roles
= roles
;
2930 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2934 * There may have been a delete timer running on the
2935 * port. Ensure that it is cancelled as we now know
2936 * the port is an FCP Target.
2937 * Note: we know the rport is exists and in an online
2938 * state as the LLDD would not have had an rport
2939 * reference to pass us.
2941 * Take no action on the del_timer failure as the state
2942 * machine state change will validate the
2945 if (!cancel_delayed_work(&rport
->fail_io_work
))
2946 fc_flush_devloss(shost
);
2947 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2948 fc_flush_devloss(shost
);
2950 spin_lock_irqsave(shost
->host_lock
, flags
);
2951 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2952 FC_RPORT_DEVLOSS_PENDING
);
2953 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2955 /* ensure any stgt delete functions are done */
2956 fc_flush_work(shost
);
2958 /* initiate a scan of the target */
2959 spin_lock_irqsave(shost
->host_lock
, flags
);
2960 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2961 scsi_queue_work(shost
, &rport
->scan_work
);
2962 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2963 scsi_target_unblock(&rport
->dev
);
2966 EXPORT_SYMBOL(fc_remote_port_rolechg
);
2969 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
2970 * @work: rport target that failed to reappear in the allotted time.
2972 * Description: An attempt to delete a remote port blocks, and if it fails
2973 * to return in the allotted time this gets called.
2976 fc_timeout_deleted_rport(struct work_struct
*work
)
2978 struct fc_rport
*rport
=
2979 container_of(work
, struct fc_rport
, dev_loss_work
.work
);
2980 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2981 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2982 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2983 unsigned long flags
;
2985 spin_lock_irqsave(shost
->host_lock
, flags
);
2987 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
2990 * If the port is ONLINE, then it came back. If it was a SCSI
2991 * target, validate it still is. If not, tear down the
2992 * scsi_target on it.
2994 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
2995 (rport
->scsi_target_id
!= -1) &&
2996 !(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)) {
2997 dev_printk(KERN_ERR
, &rport
->dev
,
2998 "blocked FC remote port time out: no longer"
2999 " a FCP target, removing starget\n");
3000 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3001 scsi_target_unblock(&rport
->dev
);
3002 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3006 /* NOOP state - we're flushing workq's */
3007 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
) {
3008 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3009 dev_printk(KERN_ERR
, &rport
->dev
,
3010 "blocked FC remote port time out: leaving"
3012 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3016 if ((fc_host
->tgtid_bind_type
== FC_TGTID_BIND_NONE
) ||
3017 (rport
->scsi_target_id
== -1)) {
3018 list_del(&rport
->peers
);
3019 rport
->port_state
= FC_PORTSTATE_DELETED
;
3020 dev_printk(KERN_ERR
, &rport
->dev
,
3021 "blocked FC remote port time out: removing"
3023 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3024 fc_queue_work(shost
, &rport
->rport_delete_work
);
3025 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3029 dev_printk(KERN_ERR
, &rport
->dev
,
3030 "blocked FC remote port time out: removing target and "
3031 "saving binding\n");
3033 list_move_tail(&rport
->peers
, &fc_host
->rport_bindings
);
3036 * Note: We do not remove or clear the hostdata area. This allows
3037 * host-specific target data to persist along with the
3038 * scsi_target_id. It's up to the host to manage it's hostdata area.
3042 * Reinitialize port attributes that may change if the port comes back.
3044 rport
->maxframe_size
= -1;
3045 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
3046 rport
->roles
= FC_PORT_ROLE_UNKNOWN
;
3047 rport
->port_state
= FC_PORTSTATE_NOTPRESENT
;
3048 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
3049 rport
->flags
|= FC_RPORT_DEVLOSS_CALLBK_DONE
;
3052 * Pre-emptively kill I/O rather than waiting for the work queue
3053 * item to teardown the starget. (FCOE libFC folks prefer this
3054 * and to have the rport_port_id still set when it's done).
3056 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3057 fc_terminate_rport_io(rport
);
3059 BUG_ON(rport
->port_state
!= FC_PORTSTATE_NOTPRESENT
);
3061 /* remove the identifiers that aren't used in the consisting binding */
3062 switch (fc_host
->tgtid_bind_type
) {
3063 case FC_TGTID_BIND_BY_WWPN
:
3064 rport
->node_name
= -1;
3065 rport
->port_id
= -1;
3067 case FC_TGTID_BIND_BY_WWNN
:
3068 rport
->port_name
= -1;
3069 rport
->port_id
= -1;
3071 case FC_TGTID_BIND_BY_ID
:
3072 rport
->node_name
= -1;
3073 rport
->port_name
= -1;
3075 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
3080 * As this only occurs if the remote port (scsi target)
3081 * went away and didn't come back - we'll remove
3082 * all attached scsi devices.
3084 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3087 * Notify the driver that the rport is now dead. The LLDD will
3088 * also guarantee that any communication to the rport is terminated
3090 * Note: we set the CALLBK_DONE flag above to correspond
3092 if (i
->f
->dev_loss_tmo_callbk
)
3093 i
->f
->dev_loss_tmo_callbk(rport
);
3098 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3099 * @work: rport to terminate io on.
3101 * Notes: Only requests the failure of the io, not that all are flushed
3102 * prior to returning.
3105 fc_timeout_fail_rport_io(struct work_struct
*work
)
3107 struct fc_rport
*rport
=
3108 container_of(work
, struct fc_rport
, fail_io_work
.work
);
3110 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
)
3113 rport
->flags
|= FC_RPORT_FAST_FAIL_TIMEDOUT
;
3114 fc_terminate_rport_io(rport
);
3118 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3119 * @work: remote port to be scanned.
3122 fc_scsi_scan_rport(struct work_struct
*work
)
3124 struct fc_rport
*rport
=
3125 container_of(work
, struct fc_rport
, scan_work
);
3126 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3127 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3128 unsigned long flags
;
3130 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
3131 (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) &&
3132 !(i
->f
->disable_target_scan
)) {
3133 scsi_scan_target(&rport
->dev
, rport
->channel
,
3134 rport
->scsi_target_id
, SCAN_WILD_CARD
, 1);
3137 spin_lock_irqsave(shost
->host_lock
, flags
);
3138 rport
->flags
&= ~FC_RPORT_SCAN_PENDING
;
3139 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3144 * fc_vport_setup - allocates and creates a FC virtual port.
3145 * @shost: scsi host the virtual port is connected to.
3146 * @channel: Channel on shost port connected to.
3147 * @pdev: parent device for vport
3148 * @ids: The world wide names, FC4 port roles, etc for
3150 * @ret_vport: The pointer to the created vport.
3152 * Allocates and creates the vport structure, calls the parent host
3153 * to instantiate the vport, the completes w/ class and sysfs creation.
3156 * This routine assumes no locks are held on entry.
3159 fc_vport_setup(struct Scsi_Host
*shost
, int channel
, struct device
*pdev
,
3160 struct fc_vport_identifiers
*ids
, struct fc_vport
**ret_vport
)
3162 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3163 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
3164 struct fc_vport
*vport
;
3166 unsigned long flags
;
3172 if ( ! fci
->f
->vport_create
)
3175 size
= (sizeof(struct fc_vport
) + fci
->f
->dd_fcvport_size
);
3176 vport
= kzalloc(size
, GFP_KERNEL
);
3177 if (unlikely(!vport
)) {
3178 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
3182 vport
->vport_state
= FC_VPORT_UNKNOWN
;
3183 vport
->vport_last_state
= FC_VPORT_UNKNOWN
;
3184 vport
->node_name
= ids
->node_name
;
3185 vport
->port_name
= ids
->port_name
;
3186 vport
->roles
= ids
->roles
;
3187 vport
->vport_type
= ids
->vport_type
;
3188 if (fci
->f
->dd_fcvport_size
)
3189 vport
->dd_data
= &vport
[1];
3190 vport
->shost
= shost
;
3191 vport
->channel
= channel
;
3192 vport
->flags
= FC_VPORT_CREATING
;
3193 INIT_WORK(&vport
->vport_delete_work
, fc_vport_sched_delete
);
3195 spin_lock_irqsave(shost
->host_lock
, flags
);
3197 if (fc_host
->npiv_vports_inuse
>= fc_host
->max_npiv_vports
) {
3198 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3202 fc_host
->npiv_vports_inuse
++;
3203 vport
->number
= fc_host
->next_vport_number
++;
3204 list_add_tail(&vport
->peers
, &fc_host
->vports
);
3205 get_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3207 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3210 device_initialize(dev
); /* takes self reference */
3211 dev
->parent
= get_device(pdev
); /* takes parent reference */
3212 dev
->release
= fc_vport_dev_release
;
3213 dev_set_name(dev
, "vport-%d:%d-%d",
3214 shost
->host_no
, channel
, vport
->number
);
3215 transport_setup_device(dev
);
3217 error
= device_add(dev
);
3219 printk(KERN_ERR
"FC Virtual Port device_add failed\n");
3222 transport_add_device(dev
);
3223 transport_configure_device(dev
);
3225 error
= fci
->f
->vport_create(vport
, ids
->disable
);
3227 printk(KERN_ERR
"FC Virtual Port LLDD Create failed\n");
3228 goto delete_vport_all
;
3232 * if the parent isn't the physical adapter's Scsi_Host, ensure
3233 * the Scsi_Host at least contains ia symlink to the vport.
3235 if (pdev
!= &shost
->shost_gendev
) {
3236 error
= sysfs_create_link(&shost
->shost_gendev
.kobj
,
3237 &dev
->kobj
, dev_name(dev
));
3240 "%s: Cannot create vport symlinks for "
3242 __func__
, dev_name(dev
), error
);
3244 spin_lock_irqsave(shost
->host_lock
, flags
);
3245 vport
->flags
&= ~FC_VPORT_CREATING
;
3246 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3248 dev_printk(KERN_NOTICE
, pdev
,
3249 "%s created via shost%d channel %d\n", dev_name(dev
),
3250 shost
->host_no
, channel
);
3257 transport_remove_device(dev
);
3260 transport_destroy_device(dev
);
3261 spin_lock_irqsave(shost
->host_lock
, flags
);
3262 list_del(&vport
->peers
);
3263 put_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3264 fc_host
->npiv_vports_inuse
--;
3265 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3266 put_device(dev
->parent
);
3273 * fc_vport_create - Admin App or LLDD requests creation of a vport
3274 * @shost: scsi host the virtual port is connected to.
3275 * @channel: channel on shost port connected to.
3276 * @ids: The world wide names, FC4 port roles, etc for
3280 * This routine assumes no locks are held on entry.
3283 fc_vport_create(struct Scsi_Host
*shost
, int channel
,
3284 struct fc_vport_identifiers
*ids
)
3287 struct fc_vport
*vport
;
3289 stat
= fc_vport_setup(shost
, channel
, &shost
->shost_gendev
,
3291 return stat
? NULL
: vport
;
3293 EXPORT_SYMBOL(fc_vport_create
);
3296 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3297 * @vport: fc_vport to be terminated
3299 * Calls the LLDD vport_delete() function, then deallocates and removes
3300 * the vport from the shost and object tree.
3303 * This routine assumes no locks are held on entry.
3306 fc_vport_terminate(struct fc_vport
*vport
)
3308 struct Scsi_Host
*shost
= vport_to_shost(vport
);
3309 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3310 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3311 struct device
*dev
= &vport
->dev
;
3312 unsigned long flags
;
3315 spin_lock_irqsave(shost
->host_lock
, flags
);
3316 if (vport
->flags
& FC_VPORT_CREATING
) {
3317 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3320 if (vport
->flags
& (FC_VPORT_DEL
)) {
3321 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3324 vport
->flags
|= FC_VPORT_DELETING
;
3325 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3327 if (i
->f
->vport_delete
)
3328 stat
= i
->f
->vport_delete(vport
);
3332 spin_lock_irqsave(shost
->host_lock
, flags
);
3333 vport
->flags
&= ~FC_VPORT_DELETING
;
3335 vport
->flags
|= FC_VPORT_DELETED
;
3336 list_del(&vport
->peers
);
3337 fc_host
->npiv_vports_inuse
--;
3338 put_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3340 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3345 if (dev
->parent
!= &shost
->shost_gendev
)
3346 sysfs_remove_link(&shost
->shost_gendev
.kobj
, dev_name(dev
));
3347 transport_remove_device(dev
);
3349 transport_destroy_device(dev
);
3352 * Removing our self-reference should mean our
3353 * release function gets called, which will drop the remaining
3354 * parent reference and free the data structure.
3356 put_device(dev
); /* for self-reference */
3358 return 0; /* SUCCESS */
3360 EXPORT_SYMBOL(fc_vport_terminate
);
3363 * fc_vport_sched_delete - workq-based delete request for a vport
3364 * @work: vport to be deleted.
3367 fc_vport_sched_delete(struct work_struct
*work
)
3369 struct fc_vport
*vport
=
3370 container_of(work
, struct fc_vport
, vport_delete_work
);
3373 stat
= fc_vport_terminate(vport
);
3375 dev_printk(KERN_ERR
, vport
->dev
.parent
,
3376 "%s: %s could not be deleted created via "
3377 "shost%d channel %d - error %d\n", __func__
,
3378 dev_name(&vport
->dev
), vport
->shost
->host_no
,
3379 vport
->channel
, stat
);
3389 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3390 * @job: fc_bsg_job that is to be torn down
3393 fc_destroy_bsgjob(struct fc_bsg_job
*job
)
3395 unsigned long flags
;
3397 spin_lock_irqsave(&job
->job_lock
, flags
);
3399 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3402 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3404 put_device(job
->dev
); /* release reference for the request */
3406 kfree(job
->request_payload
.sg_list
);
3407 kfree(job
->reply_payload
.sg_list
);
3412 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3414 * @job: fc_bsg_job that is complete
3417 fc_bsg_jobdone(struct fc_bsg_job
*job
)
3419 struct request
*req
= job
->req
;
3420 struct request
*rsp
= req
->next_rq
;
3423 err
= job
->req
->errors
= job
->reply
->result
;
3426 /* we're only returning the result field in the reply */
3427 job
->req
->sense_len
= sizeof(uint32_t);
3429 job
->req
->sense_len
= job
->reply_len
;
3431 /* we assume all request payload was transferred, residual == 0 */
3435 WARN_ON(job
->reply
->reply_payload_rcv_len
> rsp
->resid_len
);
3437 /* set reply (bidi) residual */
3438 rsp
->resid_len
-= min(job
->reply
->reply_payload_rcv_len
,
3441 blk_complete_request(req
);
3445 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3446 * @req: BSG request that holds the job to be destroyed
3448 static void fc_bsg_softirq_done(struct request
*rq
)
3450 struct fc_bsg_job
*job
= rq
->special
;
3451 unsigned long flags
;
3453 spin_lock_irqsave(&job
->job_lock
, flags
);
3454 job
->state_flags
|= FC_RQST_STATE_DONE
;
3456 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3458 blk_end_request_all(rq
, rq
->errors
);
3459 fc_destroy_bsgjob(job
);
3463 * fc_bsg_job_timeout - handler for when a bsg request timesout
3464 * @req: request that timed out
3466 static enum blk_eh_timer_return
3467 fc_bsg_job_timeout(struct request
*req
)
3469 struct fc_bsg_job
*job
= (void *) req
->special
;
3470 struct Scsi_Host
*shost
= job
->shost
;
3471 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3472 unsigned long flags
;
3473 int err
= 0, done
= 0;
3475 if (job
->rport
&& job
->rport
->port_state
== FC_PORTSTATE_BLOCKED
)
3476 return BLK_EH_RESET_TIMER
;
3478 spin_lock_irqsave(&job
->job_lock
, flags
);
3479 if (job
->state_flags
& FC_RQST_STATE_DONE
)
3483 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3485 if (!done
&& i
->f
->bsg_timeout
) {
3486 /* call LLDD to abort the i/o as it has timed out */
3487 err
= i
->f
->bsg_timeout(job
);
3489 printk(KERN_ERR
"ERROR: FC BSG request timeout - LLD "
3490 "abort failed with status %d\n", err
);
3493 /* the blk_end_sync_io() doesn't check the error */
3495 return BLK_EH_NOT_HANDLED
;
3497 return BLK_EH_HANDLED
;
3501 fc_bsg_map_buffer(struct fc_bsg_buffer
*buf
, struct request
*req
)
3503 size_t sz
= (sizeof(struct scatterlist
) * req
->nr_phys_segments
);
3505 BUG_ON(!req
->nr_phys_segments
);
3507 buf
->sg_list
= kzalloc(sz
, GFP_KERNEL
);
3510 sg_init_table(buf
->sg_list
, req
->nr_phys_segments
);
3511 buf
->sg_cnt
= blk_rq_map_sg(req
->q
, req
, buf
->sg_list
);
3512 buf
->payload_len
= blk_rq_bytes(req
);
3518 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3520 * @shost: SCSI Host corresponding to the bsg object
3521 * @rport: (optional) FC Remote Port corresponding to the bsg object
3522 * @req: BSG request that needs a job structure
3525 fc_req_to_bsgjob(struct Scsi_Host
*shost
, struct fc_rport
*rport
,
3526 struct request
*req
)
3528 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3529 struct request
*rsp
= req
->next_rq
;
3530 struct fc_bsg_job
*job
;
3533 BUG_ON(req
->special
);
3535 job
= kzalloc(sizeof(struct fc_bsg_job
) + i
->f
->dd_bsg_size
,
3541 * Note: this is a bit silly.
3542 * The request gets formatted as a SGIO v4 ioctl request, which
3543 * then gets reformatted as a blk request, which then gets
3544 * reformatted as a fc bsg request. And on completion, we have
3545 * to wrap return results such that SGIO v4 thinks it was a scsi
3546 * status. I hope this was all worth it.
3553 if (i
->f
->dd_bsg_size
)
3554 job
->dd_data
= (void *)&job
[1];
3555 spin_lock_init(&job
->job_lock
);
3556 job
->request
= (struct fc_bsg_request
*)req
->cmd
;
3557 job
->request_len
= req
->cmd_len
;
3558 job
->reply
= req
->sense
;
3559 job
->reply_len
= SCSI_SENSE_BUFFERSIZE
; /* Size of sense buffer
3562 ret
= fc_bsg_map_buffer(&job
->request_payload
, req
);
3564 goto failjob_rls_job
;
3566 if (rsp
&& rsp
->bio
) {
3567 ret
= fc_bsg_map_buffer(&job
->reply_payload
, rsp
);
3569 goto failjob_rls_rqst_payload
;
3571 job
->job_done
= fc_bsg_jobdone
;
3573 job
->dev
= &rport
->dev
;
3575 job
->dev
= &shost
->shost_gendev
;
3576 get_device(job
->dev
); /* take a reference for the request */
3583 failjob_rls_rqst_payload
:
3584 kfree(job
->request_payload
.sg_list
);
3591 enum fc_dispatch_result
{
3592 FC_DISPATCH_BREAK
, /* on return, q is locked, break from q loop */
3593 FC_DISPATCH_LOCKED
, /* on return, q is locked, continue on */
3594 FC_DISPATCH_UNLOCKED
, /* on return, q is unlocked, continue on */
3599 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3600 * @shost: scsi host rport attached to
3601 * @job: bsg job to be processed
3603 static enum fc_dispatch_result
3604 fc_bsg_host_dispatch(struct request_queue
*q
, struct Scsi_Host
*shost
,
3605 struct fc_bsg_job
*job
)
3607 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3608 int cmdlen
= sizeof(uint32_t); /* start with length of msgcode */
3611 /* Validate the host command */
3612 switch (job
->request
->msgcode
) {
3613 case FC_BSG_HST_ADD_RPORT
:
3614 cmdlen
+= sizeof(struct fc_bsg_host_add_rport
);
3617 case FC_BSG_HST_DEL_RPORT
:
3618 cmdlen
+= sizeof(struct fc_bsg_host_del_rport
);
3621 case FC_BSG_HST_ELS_NOLOGIN
:
3622 cmdlen
+= sizeof(struct fc_bsg_host_els
);
3623 /* there better be a xmt and rcv payloads */
3624 if ((!job
->request_payload
.payload_len
) ||
3625 (!job
->reply_payload
.payload_len
)) {
3632 cmdlen
+= sizeof(struct fc_bsg_host_ct
);
3633 /* there better be xmt and rcv payloads */
3634 if ((!job
->request_payload
.payload_len
) ||
3635 (!job
->reply_payload
.payload_len
)) {
3641 case FC_BSG_HST_VENDOR
:
3642 cmdlen
+= sizeof(struct fc_bsg_host_vendor
);
3643 if ((shost
->hostt
->vendor_id
== 0L) ||
3644 (job
->request
->rqst_data
.h_vendor
.vendor_id
!=
3645 shost
->hostt
->vendor_id
)) {
3656 /* check if we really have all the request data needed */
3657 if (job
->request_len
< cmdlen
) {
3662 ret
= i
->f
->bsg_request(job
);
3664 return FC_DISPATCH_UNLOCKED
;
3667 /* return the errno failure code as the only status */
3668 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3669 job
->reply
->result
= ret
;
3670 job
->reply_len
= sizeof(uint32_t);
3671 fc_bsg_jobdone(job
);
3672 return FC_DISPATCH_UNLOCKED
;
3677 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3678 * @rport: rport to be restarted
3681 fc_bsg_goose_queue(struct fc_rport
*rport
)
3684 unsigned long flags
;
3689 get_device(&rport
->dev
);
3691 spin_lock_irqsave(rport
->rqst_q
->queue_lock
, flags
);
3692 flagset
= test_bit(QUEUE_FLAG_REENTER
, &rport
->rqst_q
->queue_flags
) &&
3693 !test_bit(QUEUE_FLAG_REENTER
, &rport
->rqst_q
->queue_flags
);
3695 queue_flag_set(QUEUE_FLAG_REENTER
, rport
->rqst_q
);
3696 __blk_run_queue(rport
->rqst_q
);
3698 queue_flag_clear(QUEUE_FLAG_REENTER
, rport
->rqst_q
);
3699 spin_unlock_irqrestore(rport
->rqst_q
->queue_lock
, flags
);
3701 put_device(&rport
->dev
);
3706 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3707 * @shost: scsi host rport attached to
3708 * @rport: rport request destined to
3709 * @job: bsg job to be processed
3711 static enum fc_dispatch_result
3712 fc_bsg_rport_dispatch(struct request_queue
*q
, struct Scsi_Host
*shost
,
3713 struct fc_rport
*rport
, struct fc_bsg_job
*job
)
3715 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3716 int cmdlen
= sizeof(uint32_t); /* start with length of msgcode */
3719 /* Validate the rport command */
3720 switch (job
->request
->msgcode
) {
3721 case FC_BSG_RPT_ELS
:
3722 cmdlen
+= sizeof(struct fc_bsg_rport_els
);
3726 cmdlen
+= sizeof(struct fc_bsg_rport_ct
);
3728 /* there better be xmt and rcv payloads */
3729 if ((!job
->request_payload
.payload_len
) ||
3730 (!job
->reply_payload
.payload_len
)) {
3732 goto fail_rport_msg
;
3737 goto fail_rport_msg
;
3740 /* check if we really have all the request data needed */
3741 if (job
->request_len
< cmdlen
) {
3743 goto fail_rport_msg
;
3746 ret
= i
->f
->bsg_request(job
);
3748 return FC_DISPATCH_UNLOCKED
;
3751 /* return the errno failure code as the only status */
3752 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3753 job
->reply
->result
= ret
;
3754 job
->reply_len
= sizeof(uint32_t);
3755 fc_bsg_jobdone(job
);
3756 return FC_DISPATCH_UNLOCKED
;
3761 * fc_bsg_request_handler - generic handler for bsg requests
3762 * @q: request queue to manage
3763 * @shost: Scsi_Host related to the bsg object
3764 * @rport: FC remote port related to the bsg object (optional)
3765 * @dev: device structure for bsg object
3768 fc_bsg_request_handler(struct request_queue
*q
, struct Scsi_Host
*shost
,
3769 struct fc_rport
*rport
, struct device
*dev
)
3771 struct request
*req
;
3772 struct fc_bsg_job
*job
;
3773 enum fc_dispatch_result ret
;
3775 if (!get_device(dev
))
3778 while (!blk_queue_plugged(q
)) {
3779 if (rport
&& (rport
->port_state
== FC_PORTSTATE_BLOCKED
))
3782 req
= blk_fetch_request(q
);
3786 if (rport
&& (rport
->port_state
!= FC_PORTSTATE_ONLINE
)) {
3787 req
->errors
= -ENXIO
;
3788 spin_unlock_irq(q
->queue_lock
);
3789 blk_end_request(req
, -ENXIO
, blk_rq_bytes(req
));
3790 spin_lock_irq(q
->queue_lock
);
3794 spin_unlock_irq(q
->queue_lock
);
3796 ret
= fc_req_to_bsgjob(shost
, rport
, req
);
3799 blk_end_request(req
, ret
, blk_rq_bytes(req
));
3800 spin_lock_irq(q
->queue_lock
);
3806 /* check if we have the msgcode value at least */
3807 if (job
->request_len
< sizeof(uint32_t)) {
3808 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3809 job
->reply
->result
= -ENOMSG
;
3810 job
->reply_len
= sizeof(uint32_t);
3811 fc_bsg_jobdone(job
);
3812 spin_lock_irq(q
->queue_lock
);
3816 /* the dispatch routines will unlock the queue_lock */
3818 ret
= fc_bsg_rport_dispatch(q
, shost
, rport
, job
);
3820 ret
= fc_bsg_host_dispatch(q
, shost
, job
);
3822 /* did dispatcher hit state that can't process any more */
3823 if (ret
== FC_DISPATCH_BREAK
)
3826 /* did dispatcher had released the lock */
3827 if (ret
== FC_DISPATCH_UNLOCKED
)
3828 spin_lock_irq(q
->queue_lock
);
3831 spin_unlock_irq(q
->queue_lock
);
3833 spin_lock_irq(q
->queue_lock
);
3838 * fc_bsg_host_handler - handler for bsg requests for a fc host
3839 * @q: fc host request queue
3842 fc_bsg_host_handler(struct request_queue
*q
)
3844 struct Scsi_Host
*shost
= q
->queuedata
;
3846 fc_bsg_request_handler(q
, shost
, NULL
, &shost
->shost_gendev
);
3851 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3852 * @q: rport request queue
3855 fc_bsg_rport_handler(struct request_queue
*q
)
3857 struct fc_rport
*rport
= q
->queuedata
;
3858 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3860 fc_bsg_request_handler(q
, shost
, rport
, &rport
->dev
);
3865 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3866 * @shost: shost for fc_host
3867 * @fc_host: fc_host adding the structures to
3870 fc_bsg_hostadd(struct Scsi_Host
*shost
, struct fc_host_attrs
*fc_host
)
3872 struct device
*dev
= &shost
->shost_gendev
;
3873 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3874 struct request_queue
*q
;
3878 fc_host
->rqst_q
= NULL
;
3880 if (!i
->f
->bsg_request
)
3883 snprintf(bsg_name
, sizeof(bsg_name
),
3884 "fc_host%d", shost
->host_no
);
3886 q
= __scsi_alloc_queue(shost
, fc_bsg_host_handler
);
3888 printk(KERN_ERR
"fc_host%d: bsg interface failed to "
3889 "initialize - no request queue\n",
3894 q
->queuedata
= shost
;
3895 queue_flag_set_unlocked(QUEUE_FLAG_BIDI
, q
);
3896 blk_queue_softirq_done(q
, fc_bsg_softirq_done
);
3897 blk_queue_rq_timed_out(q
, fc_bsg_job_timeout
);
3898 blk_queue_rq_timeout(q
, FC_DEFAULT_BSG_TIMEOUT
);
3900 err
= bsg_register_queue(q
, dev
, bsg_name
, NULL
);
3902 printk(KERN_ERR
"fc_host%d: bsg interface failed to "
3903 "initialize - register queue\n",
3905 blk_cleanup_queue(q
);
3909 fc_host
->rqst_q
= q
;
3915 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3916 * @shost: shost that rport is attached to
3917 * @rport: rport that the bsg hooks are being attached to
3920 fc_bsg_rportadd(struct Scsi_Host
*shost
, struct fc_rport
*rport
)
3922 struct device
*dev
= &rport
->dev
;
3923 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3924 struct request_queue
*q
;
3927 rport
->rqst_q
= NULL
;
3929 if (!i
->f
->bsg_request
)
3932 q
= __scsi_alloc_queue(shost
, fc_bsg_rport_handler
);
3934 printk(KERN_ERR
"%s: bsg interface failed to "
3935 "initialize - no request queue\n",
3940 q
->queuedata
= rport
;
3941 queue_flag_set_unlocked(QUEUE_FLAG_BIDI
, q
);
3942 blk_queue_softirq_done(q
, fc_bsg_softirq_done
);
3943 blk_queue_rq_timed_out(q
, fc_bsg_job_timeout
);
3944 blk_queue_rq_timeout(q
, BLK_DEFAULT_SG_TIMEOUT
);
3946 err
= bsg_register_queue(q
, dev
, NULL
, NULL
);
3948 printk(KERN_ERR
"%s: bsg interface failed to "
3949 "initialize - register queue\n",
3951 blk_cleanup_queue(q
);
3961 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3962 * @q: the request_queue that is to be torn down.
3965 fc_bsg_remove(struct request_queue
*q
)
3968 bsg_unregister_queue(q
);
3969 blk_cleanup_queue(q
);
3974 /* Original Author: Martin Hicks */
3975 MODULE_AUTHOR("James Smart");
3976 MODULE_DESCRIPTION("FC Transport Attributes");
3977 MODULE_LICENSE("GPL");
3979 module_init(fc_transport_init
);
3980 module_exit(fc_transport_exit
);