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 <linux/delay.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <linux/netlink.h>
37 #include <net/netlink.h>
38 #include <scsi/scsi_netlink_fc.h>
39 #include <scsi/scsi_bsg_fc.h>
40 #include "scsi_priv.h"
41 #include "scsi_transport_fc_internal.h"
43 static int fc_queue_work(struct Scsi_Host
*, struct work_struct
*);
44 static void fc_vport_sched_delete(struct work_struct
*work
);
45 static int fc_vport_setup(struct Scsi_Host
*shost
, int channel
,
46 struct device
*pdev
, struct fc_vport_identifiers
*ids
,
47 struct fc_vport
**vport
);
48 static int fc_bsg_hostadd(struct Scsi_Host
*, struct fc_host_attrs
*);
49 static int fc_bsg_rportadd(struct Scsi_Host
*, struct fc_rport
*);
50 static void fc_bsg_remove(struct request_queue
*);
51 static void fc_bsg_goose_queue(struct fc_rport
*);
54 * Redefine so that we can have same named attributes in the
55 * sdev/starget/host objects.
57 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
58 struct device_attribute device_attr_##_prefix##_##_name = \
59 __ATTR(_name,_mode,_show,_store)
61 #define fc_enum_name_search(title, table_type, table) \
62 static const char *get_fc_##title##_name(enum table_type table_key) \
67 for (i = 0; i < ARRAY_SIZE(table); i++) { \
68 if (table[i].value == table_key) { \
69 name = table[i].name; \
76 #define fc_enum_name_match(title, table_type, table) \
77 static int get_fc_##title##_match(const char *table_key, \
78 enum table_type *value) \
82 for (i = 0; i < ARRAY_SIZE(table); i++) { \
83 if (strncmp(table_key, table[i].name, \
84 table[i].matchlen) == 0) { \
85 *value = table[i].value; \
86 return 0; /* success */ \
89 return 1; /* failure */ \
93 /* Convert fc_port_type values to ascii string name */
95 enum fc_port_type value
;
97 } fc_port_type_names
[] = {
98 { FC_PORTTYPE_UNKNOWN
, "Unknown" },
99 { FC_PORTTYPE_OTHER
, "Other" },
100 { FC_PORTTYPE_NOTPRESENT
, "Not Present" },
101 { FC_PORTTYPE_NPORT
, "NPort (fabric via point-to-point)" },
102 { FC_PORTTYPE_NLPORT
, "NLPort (fabric via loop)" },
103 { FC_PORTTYPE_LPORT
, "LPort (private loop)" },
104 { FC_PORTTYPE_PTP
, "Point-To-Point (direct nport connection)" },
105 { FC_PORTTYPE_NPIV
, "NPIV VPORT" },
107 fc_enum_name_search(port_type
, fc_port_type
, fc_port_type_names
)
108 #define FC_PORTTYPE_MAX_NAMELEN 50
110 /* Reuse fc_port_type enum function for vport_type */
111 #define get_fc_vport_type_name get_fc_port_type_name
114 /* Convert fc_host_event_code values to ascii string name */
115 static const struct {
116 enum fc_host_event_code value
;
118 } fc_host_event_code_names
[] = {
119 { FCH_EVT_LIP
, "lip" },
120 { FCH_EVT_LINKUP
, "link_up" },
121 { FCH_EVT_LINKDOWN
, "link_down" },
122 { FCH_EVT_LIPRESET
, "lip_reset" },
123 { FCH_EVT_RSCN
, "rscn" },
124 { FCH_EVT_ADAPTER_CHANGE
, "adapter_chg" },
125 { FCH_EVT_PORT_UNKNOWN
, "port_unknown" },
126 { FCH_EVT_PORT_ONLINE
, "port_online" },
127 { FCH_EVT_PORT_OFFLINE
, "port_offline" },
128 { FCH_EVT_PORT_FABRIC
, "port_fabric" },
129 { FCH_EVT_LINK_UNKNOWN
, "link_unknown" },
130 { FCH_EVT_VENDOR_UNIQUE
, "vendor_unique" },
132 fc_enum_name_search(host_event_code
, fc_host_event_code
,
133 fc_host_event_code_names
)
134 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
137 /* Convert fc_port_state values to ascii string name */
139 enum fc_port_state value
;
141 } fc_port_state_names
[] = {
142 { FC_PORTSTATE_UNKNOWN
, "Unknown" },
143 { FC_PORTSTATE_NOTPRESENT
, "Not Present" },
144 { FC_PORTSTATE_ONLINE
, "Online" },
145 { FC_PORTSTATE_OFFLINE
, "Offline" },
146 { FC_PORTSTATE_BLOCKED
, "Blocked" },
147 { FC_PORTSTATE_BYPASSED
, "Bypassed" },
148 { FC_PORTSTATE_DIAGNOSTICS
, "Diagnostics" },
149 { FC_PORTSTATE_LINKDOWN
, "Linkdown" },
150 { FC_PORTSTATE_ERROR
, "Error" },
151 { FC_PORTSTATE_LOOPBACK
, "Loopback" },
152 { FC_PORTSTATE_DELETED
, "Deleted" },
154 fc_enum_name_search(port_state
, fc_port_state
, fc_port_state_names
)
155 #define FC_PORTSTATE_MAX_NAMELEN 20
158 /* Convert fc_vport_state values to ascii string name */
160 enum fc_vport_state value
;
162 } fc_vport_state_names
[] = {
163 { FC_VPORT_UNKNOWN
, "Unknown" },
164 { FC_VPORT_ACTIVE
, "Active" },
165 { FC_VPORT_DISABLED
, "Disabled" },
166 { FC_VPORT_LINKDOWN
, "Linkdown" },
167 { FC_VPORT_INITIALIZING
, "Initializing" },
168 { FC_VPORT_NO_FABRIC_SUPP
, "No Fabric Support" },
169 { FC_VPORT_NO_FABRIC_RSCS
, "No Fabric Resources" },
170 { FC_VPORT_FABRIC_LOGOUT
, "Fabric Logout" },
171 { FC_VPORT_FABRIC_REJ_WWN
, "Fabric Rejected WWN" },
172 { FC_VPORT_FAILED
, "VPort Failed" },
174 fc_enum_name_search(vport_state
, fc_vport_state
, fc_vport_state_names
)
175 #define FC_VPORTSTATE_MAX_NAMELEN 24
177 /* Reuse fc_vport_state enum function for vport_last_state */
178 #define get_fc_vport_last_state_name get_fc_vport_state_name
181 /* Convert fc_tgtid_binding_type values to ascii string name */
182 static const struct {
183 enum fc_tgtid_binding_type value
;
186 } fc_tgtid_binding_type_names
[] = {
187 { FC_TGTID_BIND_NONE
, "none", 4 },
188 { FC_TGTID_BIND_BY_WWPN
, "wwpn (World Wide Port Name)", 4 },
189 { FC_TGTID_BIND_BY_WWNN
, "wwnn (World Wide Node Name)", 4 },
190 { FC_TGTID_BIND_BY_ID
, "port_id (FC Address)", 7 },
192 fc_enum_name_search(tgtid_bind_type
, fc_tgtid_binding_type
,
193 fc_tgtid_binding_type_names
)
194 fc_enum_name_match(tgtid_bind_type
, fc_tgtid_binding_type
,
195 fc_tgtid_binding_type_names
)
196 #define FC_BINDTYPE_MAX_NAMELEN 30
199 #define fc_bitfield_name_search(title, table) \
201 get_fc_##title##_names(u32 table_key, char *buf) \
207 for (i = 0; i < ARRAY_SIZE(table); i++) { \
208 if (table[i].value & table_key) { \
209 len += sprintf(buf + len, "%s%s", \
210 prefix, table[i].name); \
214 len += sprintf(buf + len, "\n"); \
219 /* Convert FC_COS bit values to ascii string name */
220 static const struct {
224 { FC_COS_CLASS1
, "Class 1" },
225 { FC_COS_CLASS2
, "Class 2" },
226 { FC_COS_CLASS3
, "Class 3" },
227 { FC_COS_CLASS4
, "Class 4" },
228 { FC_COS_CLASS6
, "Class 6" },
230 fc_bitfield_name_search(cos
, fc_cos_names
)
233 /* Convert FC_PORTSPEED bit values to ascii string name */
234 static const struct {
237 } fc_port_speed_names
[] = {
238 { FC_PORTSPEED_1GBIT
, "1 Gbit" },
239 { FC_PORTSPEED_2GBIT
, "2 Gbit" },
240 { FC_PORTSPEED_4GBIT
, "4 Gbit" },
241 { FC_PORTSPEED_10GBIT
, "10 Gbit" },
242 { FC_PORTSPEED_8GBIT
, "8 Gbit" },
243 { FC_PORTSPEED_16GBIT
, "16 Gbit" },
244 { FC_PORTSPEED_NOT_NEGOTIATED
, "Not Negotiated" },
246 fc_bitfield_name_search(port_speed
, fc_port_speed_names
)
250 show_fc_fc4s (char *buf
, u8
*fc4_list
)
254 for (i
= 0; i
< FC_FC4_LIST_SIZE
; i
++, fc4_list
++)
255 len
+= sprintf(buf
+ len
, "0x%02x ", *fc4_list
);
256 len
+= sprintf(buf
+ len
, "\n");
261 /* Convert FC_PORT_ROLE bit values to ascii string name */
262 static const struct {
265 } fc_port_role_names
[] = {
266 { FC_PORT_ROLE_FCP_TARGET
, "FCP Target" },
267 { FC_PORT_ROLE_FCP_INITIATOR
, "FCP Initiator" },
268 { FC_PORT_ROLE_IP_PORT
, "IP Port" },
270 fc_bitfield_name_search(port_roles
, fc_port_role_names
)
273 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
275 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
276 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
277 #define FC_FPORT_PORTID 0x00000e
278 #define FC_FABCTLR_PORTID 0x00000d
279 #define FC_DIRSRVR_PORTID 0x00000c
280 #define FC_TIMESRVR_PORTID 0x00000b
281 #define FC_MGMTSRVR_PORTID 0x00000a
284 static void fc_timeout_deleted_rport(struct work_struct
*work
);
285 static void fc_timeout_fail_rport_io(struct work_struct
*work
);
286 static void fc_scsi_scan_rport(struct work_struct
*work
);
289 * Attribute counts pre object type...
290 * Increase these values if you add attributes
292 #define FC_STARGET_NUM_ATTRS 3
293 #define FC_RPORT_NUM_ATTRS 10
294 #define FC_VPORT_NUM_ATTRS 9
295 #define FC_HOST_NUM_ATTRS 22
298 struct scsi_transport_template t
;
299 struct fc_function_template
*f
;
302 * For attributes : each object has :
303 * An array of the actual attributes structures
304 * An array of null-terminated pointers to the attribute
305 * structures - used for mid-layer interaction.
307 * The attribute containers for the starget and host are are
308 * part of the midlayer. As the remote port is specific to the
309 * fc transport, we must provide the attribute container.
311 struct device_attribute private_starget_attrs
[
312 FC_STARGET_NUM_ATTRS
];
313 struct device_attribute
*starget_attrs
[FC_STARGET_NUM_ATTRS
+ 1];
315 struct device_attribute private_host_attrs
[FC_HOST_NUM_ATTRS
];
316 struct device_attribute
*host_attrs
[FC_HOST_NUM_ATTRS
+ 1];
318 struct transport_container rport_attr_cont
;
319 struct device_attribute private_rport_attrs
[FC_RPORT_NUM_ATTRS
];
320 struct device_attribute
*rport_attrs
[FC_RPORT_NUM_ATTRS
+ 1];
322 struct transport_container vport_attr_cont
;
323 struct device_attribute private_vport_attrs
[FC_VPORT_NUM_ATTRS
];
324 struct device_attribute
*vport_attrs
[FC_VPORT_NUM_ATTRS
+ 1];
327 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
329 static int fc_target_setup(struct transport_container
*tc
, struct device
*dev
,
332 struct scsi_target
*starget
= to_scsi_target(dev
);
333 struct fc_rport
*rport
= starget_to_rport(starget
);
336 * if parent is remote port, use values from remote port.
337 * Otherwise, this host uses the fc_transport, but not the
338 * remote port interface. As such, initialize to known non-values.
341 fc_starget_node_name(starget
) = rport
->node_name
;
342 fc_starget_port_name(starget
) = rport
->port_name
;
343 fc_starget_port_id(starget
) = rport
->port_id
;
345 fc_starget_node_name(starget
) = -1;
346 fc_starget_port_name(starget
) = -1;
347 fc_starget_port_id(starget
) = -1;
353 static DECLARE_TRANSPORT_CLASS(fc_transport_class
,
359 static int fc_host_setup(struct transport_container
*tc
, struct device
*dev
,
362 struct Scsi_Host
*shost
= dev_to_shost(dev
);
363 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
366 * Set default values easily detected by the midlayer as
367 * failure cases. The scsi lldd is responsible for initializing
368 * all transport attributes to valid values per host.
370 fc_host
->node_name
= -1;
371 fc_host
->port_name
= -1;
372 fc_host
->permanent_port_name
= -1;
373 fc_host
->supported_classes
= FC_COS_UNSPECIFIED
;
374 memset(fc_host
->supported_fc4s
, 0,
375 sizeof(fc_host
->supported_fc4s
));
376 fc_host
->supported_speeds
= FC_PORTSPEED_UNKNOWN
;
377 fc_host
->maxframe_size
= -1;
378 fc_host
->max_npiv_vports
= 0;
379 memset(fc_host
->serial_number
, 0,
380 sizeof(fc_host
->serial_number
));
382 fc_host
->port_id
= -1;
383 fc_host
->port_type
= FC_PORTTYPE_UNKNOWN
;
384 fc_host
->port_state
= FC_PORTSTATE_UNKNOWN
;
385 memset(fc_host
->active_fc4s
, 0,
386 sizeof(fc_host
->active_fc4s
));
387 fc_host
->speed
= FC_PORTSPEED_UNKNOWN
;
388 fc_host
->fabric_name
= -1;
389 memset(fc_host
->symbolic_name
, 0, sizeof(fc_host
->symbolic_name
));
390 memset(fc_host
->system_hostname
, 0, sizeof(fc_host
->system_hostname
));
392 fc_host
->tgtid_bind_type
= FC_TGTID_BIND_BY_WWPN
;
394 INIT_LIST_HEAD(&fc_host
->rports
);
395 INIT_LIST_HEAD(&fc_host
->rport_bindings
);
396 INIT_LIST_HEAD(&fc_host
->vports
);
397 fc_host
->next_rport_number
= 0;
398 fc_host
->next_target_id
= 0;
399 fc_host
->next_vport_number
= 0;
400 fc_host
->npiv_vports_inuse
= 0;
402 snprintf(fc_host
->work_q_name
, sizeof(fc_host
->work_q_name
),
403 "fc_wq_%d", shost
->host_no
);
404 fc_host
->work_q
= create_singlethread_workqueue(
405 fc_host
->work_q_name
);
406 if (!fc_host
->work_q
)
409 snprintf(fc_host
->devloss_work_q_name
,
410 sizeof(fc_host
->devloss_work_q_name
),
411 "fc_dl_%d", shost
->host_no
);
412 fc_host
->devloss_work_q
= create_singlethread_workqueue(
413 fc_host
->devloss_work_q_name
);
414 if (!fc_host
->devloss_work_q
) {
415 destroy_workqueue(fc_host
->work_q
);
416 fc_host
->work_q
= NULL
;
420 fc_bsg_hostadd(shost
, fc_host
);
421 /* ignore any bsg add error - we just can't do sgio */
426 static int fc_host_remove(struct transport_container
*tc
, struct device
*dev
,
429 struct Scsi_Host
*shost
= dev_to_shost(dev
);
430 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
432 fc_bsg_remove(fc_host
->rqst_q
);
436 static DECLARE_TRANSPORT_CLASS(fc_host_class
,
443 * Setup and Remove actions for remote ports are handled
444 * in the service functions below.
446 static DECLARE_TRANSPORT_CLASS(fc_rport_class
,
453 * Setup and Remove actions for virtual ports are handled
454 * in the service functions below.
456 static DECLARE_TRANSPORT_CLASS(fc_vport_class
,
467 * dev_loss_tmo: the default number of seconds that the FC transport
468 * should insulate the loss of a remote port.
469 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
471 static unsigned int fc_dev_loss_tmo
= 60; /* seconds */
473 module_param_named(dev_loss_tmo
, fc_dev_loss_tmo
, uint
, S_IRUGO
|S_IWUSR
);
474 MODULE_PARM_DESC(dev_loss_tmo
,
475 "Maximum number of seconds that the FC transport should"
476 " insulate the loss of a remote port. Once this value is"
477 " exceeded, the scsi target is removed. Value should be"
478 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
479 " fast_io_fail_tmo is not set.");
482 * Netlink Infrastructure
485 static atomic_t fc_event_seq
;
488 * fc_get_event_number - Obtain the next sequential FC event number
491 * We could have inlined this, but it would have required fc_event_seq to
492 * be exposed. For now, live with the subroutine call.
493 * Atomic used to avoid lock/unlock...
496 fc_get_event_number(void)
498 return atomic_add_return(1, &fc_event_seq
);
500 EXPORT_SYMBOL(fc_get_event_number
);
504 * fc_host_post_event - called to post an even on an fc_host.
505 * @shost: host the event occurred on
506 * @event_number: fc event number obtained from get_fc_event_number()
507 * @event_code: fc_host event being posted
508 * @event_data: 32bits of data for the event being posted
511 * This routine assumes no locks are held on entry.
514 fc_host_post_event(struct Scsi_Host
*shost
, u32 event_number
,
515 enum fc_host_event_code event_code
, u32 event_data
)
518 struct nlmsghdr
*nlh
;
519 struct fc_nl_event
*event
;
529 len
= FC_NL_MSGALIGN(sizeof(*event
));
530 skblen
= NLMSG_SPACE(len
);
532 skb
= alloc_skb(skblen
, GFP_KERNEL
);
538 nlh
= nlmsg_put(skb
, 0, 0, SCSI_TRANSPORT_MSG
,
539 skblen
- sizeof(*nlh
), 0);
544 event
= NLMSG_DATA(nlh
);
546 INIT_SCSI_NL_HDR(&event
->snlh
, SCSI_NL_TRANSPORT_FC
,
547 FC_NL_ASYNC_EVENT
, len
);
548 event
->seconds
= get_seconds();
549 event
->vendor_id
= 0;
550 event
->host_no
= shost
->host_no
;
551 event
->event_datalen
= sizeof(u32
); /* bytes */
552 event
->event_num
= event_number
;
553 event
->event_code
= event_code
;
554 event
->event_data
= event_data
;
556 nlmsg_multicast(scsi_nl_sock
, skb
, 0, SCSI_NL_GRP_FC_EVENTS
,
563 name
= get_fc_host_event_code_name(event_code
);
565 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
566 __func__
, shost
->host_no
,
567 (name
) ? name
: "<unknown>", event_data
, err
);
570 EXPORT_SYMBOL(fc_host_post_event
);
574 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
575 * @shost: host the event occurred on
576 * @event_number: fc event number obtained from get_fc_event_number()
577 * @data_len: amount, in bytes, of vendor unique data
578 * @data_buf: pointer to vendor unique data
579 * @vendor_id: Vendor id
582 * This routine assumes no locks are held on entry.
585 fc_host_post_vendor_event(struct Scsi_Host
*shost
, u32 event_number
,
586 u32 data_len
, char * data_buf
, u64 vendor_id
)
589 struct nlmsghdr
*nlh
;
590 struct fc_nl_event
*event
;
596 goto send_vendor_fail
;
599 len
= FC_NL_MSGALIGN(sizeof(*event
) + data_len
);
600 skblen
= NLMSG_SPACE(len
);
602 skb
= alloc_skb(skblen
, GFP_KERNEL
);
605 goto send_vendor_fail
;
608 nlh
= nlmsg_put(skb
, 0, 0, SCSI_TRANSPORT_MSG
,
609 skblen
- sizeof(*nlh
), 0);
612 goto send_vendor_fail_skb
;
614 event
= NLMSG_DATA(nlh
);
616 INIT_SCSI_NL_HDR(&event
->snlh
, SCSI_NL_TRANSPORT_FC
,
617 FC_NL_ASYNC_EVENT
, len
);
618 event
->seconds
= get_seconds();
619 event
->vendor_id
= vendor_id
;
620 event
->host_no
= shost
->host_no
;
621 event
->event_datalen
= data_len
; /* bytes */
622 event
->event_num
= event_number
;
623 event
->event_code
= FCH_EVT_VENDOR_UNIQUE
;
624 memcpy(&event
->event_data
, data_buf
, data_len
);
626 nlmsg_multicast(scsi_nl_sock
, skb
, 0, SCSI_NL_GRP_FC_EVENTS
,
630 send_vendor_fail_skb
:
634 "%s: Dropped Event : host %d vendor_unique - err %d\n",
635 __func__
, shost
->host_no
, err
);
638 EXPORT_SYMBOL(fc_host_post_vendor_event
);
642 static __init
int fc_transport_init(void)
646 atomic_set(&fc_event_seq
, 0);
648 error
= transport_class_register(&fc_host_class
);
651 error
= transport_class_register(&fc_vport_class
);
653 goto unreg_host_class
;
654 error
= transport_class_register(&fc_rport_class
);
656 goto unreg_vport_class
;
657 error
= transport_class_register(&fc_transport_class
);
659 goto unreg_rport_class
;
663 transport_class_unregister(&fc_rport_class
);
665 transport_class_unregister(&fc_vport_class
);
667 transport_class_unregister(&fc_host_class
);
671 static void __exit
fc_transport_exit(void)
673 transport_class_unregister(&fc_transport_class
);
674 transport_class_unregister(&fc_rport_class
);
675 transport_class_unregister(&fc_host_class
);
676 transport_class_unregister(&fc_vport_class
);
680 * FC Remote Port Attribute Management
683 #define fc_rport_show_function(field, format_string, sz, cast) \
685 show_fc_rport_##field (struct device *dev, \
686 struct device_attribute *attr, char *buf) \
688 struct fc_rport *rport = transport_class_to_rport(dev); \
689 struct Scsi_Host *shost = rport_to_shost(rport); \
690 struct fc_internal *i = to_fc_internal(shost->transportt); \
691 if ((i->f->get_rport_##field) && \
692 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
693 (rport->port_state == FC_PORTSTATE_DELETED) || \
694 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
695 i->f->get_rport_##field(rport); \
696 return snprintf(buf, sz, format_string, cast rport->field); \
699 #define fc_rport_store_function(field) \
701 store_fc_rport_##field(struct device *dev, \
702 struct device_attribute *attr, \
703 const char *buf, size_t count) \
706 struct fc_rport *rport = transport_class_to_rport(dev); \
707 struct Scsi_Host *shost = rport_to_shost(rport); \
708 struct fc_internal *i = to_fc_internal(shost->transportt); \
710 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
711 (rport->port_state == FC_PORTSTATE_DELETED) || \
712 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
714 val = simple_strtoul(buf, &cp, 0); \
715 if (*cp && (*cp != '\n')) \
717 i->f->set_rport_##field(rport, val); \
721 #define fc_rport_rd_attr(field, format_string, sz) \
722 fc_rport_show_function(field, format_string, sz, ) \
723 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
724 show_fc_rport_##field, NULL)
726 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
727 fc_rport_show_function(field, format_string, sz, (cast)) \
728 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
729 show_fc_rport_##field, NULL)
731 #define fc_rport_rw_attr(field, format_string, sz) \
732 fc_rport_show_function(field, format_string, sz, ) \
733 fc_rport_store_function(field) \
734 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
735 show_fc_rport_##field, \
736 store_fc_rport_##field)
739 #define fc_private_rport_show_function(field, format_string, sz, cast) \
741 show_fc_rport_##field (struct device *dev, \
742 struct device_attribute *attr, char *buf) \
744 struct fc_rport *rport = transport_class_to_rport(dev); \
745 return snprintf(buf, sz, format_string, cast rport->field); \
748 #define fc_private_rport_rd_attr(field, format_string, sz) \
749 fc_private_rport_show_function(field, format_string, sz, ) \
750 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
751 show_fc_rport_##field, NULL)
753 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
754 fc_private_rport_show_function(field, format_string, sz, (cast)) \
755 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
756 show_fc_rport_##field, NULL)
759 #define fc_private_rport_rd_enum_attr(title, maxlen) \
761 show_fc_rport_##title (struct device *dev, \
762 struct device_attribute *attr, char *buf) \
764 struct fc_rport *rport = transport_class_to_rport(dev); \
766 name = get_fc_##title##_name(rport->title); \
769 return snprintf(buf, maxlen, "%s\n", name); \
771 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
772 show_fc_rport_##title, NULL)
775 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
776 i->private_rport_attrs[count] = device_attr_rport_##field; \
777 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
778 i->private_rport_attrs[count].store = NULL; \
779 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
780 if (i->f->show_rport_##field) \
783 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
784 i->private_rport_attrs[count] = device_attr_rport_##field; \
785 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
786 i->private_rport_attrs[count].store = NULL; \
787 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
790 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
791 i->private_rport_attrs[count] = device_attr_rport_##field; \
792 if (!i->f->set_rport_##field) { \
793 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
794 i->private_rport_attrs[count].store = NULL; \
796 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
797 if (i->f->show_rport_##field) \
800 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
802 i->private_rport_attrs[count] = device_attr_rport_##field; \
803 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
808 /* The FC Transport Remote Port Attributes: */
810 /* Fixed Remote Port Attributes */
812 fc_private_rport_rd_attr(maxframe_size
, "%u bytes\n", 20);
815 show_fc_rport_supported_classes (struct device
*dev
,
816 struct device_attribute
*attr
, char *buf
)
818 struct fc_rport
*rport
= transport_class_to_rport(dev
);
819 if (rport
->supported_classes
== FC_COS_UNSPECIFIED
)
820 return snprintf(buf
, 20, "unspecified\n");
821 return get_fc_cos_names(rport
->supported_classes
, buf
);
823 static FC_DEVICE_ATTR(rport
, supported_classes
, S_IRUGO
,
824 show_fc_rport_supported_classes
, NULL
);
826 /* Dynamic Remote Port Attributes */
829 * dev_loss_tmo attribute
831 fc_rport_show_function(dev_loss_tmo
, "%d\n", 20, )
833 store_fc_rport_dev_loss_tmo(struct device
*dev
, struct device_attribute
*attr
,
834 const char *buf
, size_t count
)
837 struct fc_rport
*rport
= transport_class_to_rport(dev
);
838 struct Scsi_Host
*shost
= rport_to_shost(rport
);
839 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
841 if ((rport
->port_state
== FC_PORTSTATE_BLOCKED
) ||
842 (rport
->port_state
== FC_PORTSTATE_DELETED
) ||
843 (rport
->port_state
== FC_PORTSTATE_NOTPRESENT
))
845 val
= simple_strtoul(buf
, &cp
, 0);
846 if ((*cp
&& (*cp
!= '\n')) || (val
< 0))
850 * If fast_io_fail is off we have to cap
851 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
853 if (rport
->fast_io_fail_tmo
== -1 &&
854 val
> SCSI_DEVICE_BLOCK_MAX_TIMEOUT
)
857 i
->f
->set_rport_dev_loss_tmo(rport
, val
);
860 static FC_DEVICE_ATTR(rport
, dev_loss_tmo
, S_IRUGO
| S_IWUSR
,
861 show_fc_rport_dev_loss_tmo
, store_fc_rport_dev_loss_tmo
);
864 /* Private Remote Port Attributes */
866 fc_private_rport_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
867 fc_private_rport_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
868 fc_private_rport_rd_attr(port_id
, "0x%06x\n", 20);
871 show_fc_rport_roles (struct device
*dev
, struct device_attribute
*attr
,
874 struct fc_rport
*rport
= transport_class_to_rport(dev
);
876 /* identify any roles that are port_id specific */
877 if ((rport
->port_id
!= -1) &&
878 (rport
->port_id
& FC_WELLKNOWN_PORTID_MASK
) ==
879 FC_WELLKNOWN_PORTID_MASK
) {
880 switch (rport
->port_id
& FC_WELLKNOWN_ROLE_MASK
) {
881 case FC_FPORT_PORTID
:
882 return snprintf(buf
, 30, "Fabric Port\n");
883 case FC_FABCTLR_PORTID
:
884 return snprintf(buf
, 30, "Fabric Controller\n");
885 case FC_DIRSRVR_PORTID
:
886 return snprintf(buf
, 30, "Directory Server\n");
887 case FC_TIMESRVR_PORTID
:
888 return snprintf(buf
, 30, "Time Server\n");
889 case FC_MGMTSRVR_PORTID
:
890 return snprintf(buf
, 30, "Management Server\n");
892 return snprintf(buf
, 30, "Unknown Fabric Entity\n");
895 if (rport
->roles
== FC_PORT_ROLE_UNKNOWN
)
896 return snprintf(buf
, 20, "unknown\n");
897 return get_fc_port_roles_names(rport
->roles
, buf
);
900 static FC_DEVICE_ATTR(rport
, roles
, S_IRUGO
,
901 show_fc_rport_roles
, NULL
);
903 fc_private_rport_rd_enum_attr(port_state
, FC_PORTSTATE_MAX_NAMELEN
);
904 fc_private_rport_rd_attr(scsi_target_id
, "%d\n", 20);
907 * fast_io_fail_tmo attribute
910 show_fc_rport_fast_io_fail_tmo (struct device
*dev
,
911 struct device_attribute
*attr
, char *buf
)
913 struct fc_rport
*rport
= transport_class_to_rport(dev
);
915 if (rport
->fast_io_fail_tmo
== -1)
916 return snprintf(buf
, 5, "off\n");
917 return snprintf(buf
, 20, "%d\n", rport
->fast_io_fail_tmo
);
921 store_fc_rport_fast_io_fail_tmo(struct device
*dev
,
922 struct device_attribute
*attr
, const char *buf
,
927 struct fc_rport
*rport
= transport_class_to_rport(dev
);
929 if ((rport
->port_state
== FC_PORTSTATE_BLOCKED
) ||
930 (rport
->port_state
== FC_PORTSTATE_DELETED
) ||
931 (rport
->port_state
== FC_PORTSTATE_NOTPRESENT
))
933 if (strncmp(buf
, "off", 3) == 0)
934 rport
->fast_io_fail_tmo
= -1;
936 val
= simple_strtoul(buf
, &cp
, 0);
937 if ((*cp
&& (*cp
!= '\n')) || (val
< 0))
940 * Cap fast_io_fail by dev_loss_tmo or
941 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
943 if ((val
>= rport
->dev_loss_tmo
) ||
944 (val
> SCSI_DEVICE_BLOCK_MAX_TIMEOUT
))
947 rport
->fast_io_fail_tmo
= val
;
951 static FC_DEVICE_ATTR(rport
, fast_io_fail_tmo
, S_IRUGO
| S_IWUSR
,
952 show_fc_rport_fast_io_fail_tmo
, store_fc_rport_fast_io_fail_tmo
);
956 * FC SCSI Target Attribute Management
960 * Note: in the target show function we recognize when the remote
961 * port is in the heirarchy and do not allow the driver to get
962 * involved in sysfs functions. The driver only gets involved if
963 * it's the "old" style that doesn't use rports.
965 #define fc_starget_show_function(field, format_string, sz, cast) \
967 show_fc_starget_##field (struct device *dev, \
968 struct device_attribute *attr, char *buf) \
970 struct scsi_target *starget = transport_class_to_starget(dev); \
971 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
972 struct fc_internal *i = to_fc_internal(shost->transportt); \
973 struct fc_rport *rport = starget_to_rport(starget); \
975 fc_starget_##field(starget) = rport->field; \
976 else if (i->f->get_starget_##field) \
977 i->f->get_starget_##field(starget); \
978 return snprintf(buf, sz, format_string, \
979 cast fc_starget_##field(starget)); \
982 #define fc_starget_rd_attr(field, format_string, sz) \
983 fc_starget_show_function(field, format_string, sz, ) \
984 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
985 show_fc_starget_##field, NULL)
987 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
988 fc_starget_show_function(field, format_string, sz, (cast)) \
989 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
990 show_fc_starget_##field, NULL)
992 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
993 i->private_starget_attrs[count] = device_attr_starget_##field; \
994 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
995 i->private_starget_attrs[count].store = NULL; \
996 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
997 if (i->f->show_starget_##field) \
1000 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
1001 i->private_starget_attrs[count] = device_attr_starget_##field; \
1002 if (!i->f->set_starget_##field) { \
1003 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1004 i->private_starget_attrs[count].store = NULL; \
1006 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1007 if (i->f->show_starget_##field) \
1010 /* The FC Transport SCSI Target Attributes: */
1011 fc_starget_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
1012 fc_starget_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
1013 fc_starget_rd_attr(port_id
, "0x%06x\n", 20);
1017 * FC Virtual Port Attribute Management
1020 #define fc_vport_show_function(field, format_string, sz, cast) \
1022 show_fc_vport_##field (struct device *dev, \
1023 struct device_attribute *attr, char *buf) \
1025 struct fc_vport *vport = transport_class_to_vport(dev); \
1026 struct Scsi_Host *shost = vport_to_shost(vport); \
1027 struct fc_internal *i = to_fc_internal(shost->transportt); \
1028 if ((i->f->get_vport_##field) && \
1029 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1030 i->f->get_vport_##field(vport); \
1031 return snprintf(buf, sz, format_string, cast vport->field); \
1034 #define fc_vport_store_function(field) \
1036 store_fc_vport_##field(struct device *dev, \
1037 struct device_attribute *attr, \
1038 const char *buf, size_t count) \
1041 struct fc_vport *vport = transport_class_to_vport(dev); \
1042 struct Scsi_Host *shost = vport_to_shost(vport); \
1043 struct fc_internal *i = to_fc_internal(shost->transportt); \
1045 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1047 val = simple_strtoul(buf, &cp, 0); \
1048 if (*cp && (*cp != '\n')) \
1050 i->f->set_vport_##field(vport, val); \
1054 #define fc_vport_store_str_function(field, slen) \
1056 store_fc_vport_##field(struct device *dev, \
1057 struct device_attribute *attr, \
1058 const char *buf, size_t count) \
1060 struct fc_vport *vport = transport_class_to_vport(dev); \
1061 struct Scsi_Host *shost = vport_to_shost(vport); \
1062 struct fc_internal *i = to_fc_internal(shost->transportt); \
1063 unsigned int cnt=count; \
1065 /* count may include a LF at end of string */ \
1066 if (buf[cnt-1] == '\n') \
1068 if (cnt > ((slen) - 1)) \
1070 memcpy(vport->field, buf, cnt); \
1071 i->f->set_vport_##field(vport); \
1075 #define fc_vport_rd_attr(field, format_string, sz) \
1076 fc_vport_show_function(field, format_string, sz, ) \
1077 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1078 show_fc_vport_##field, NULL)
1080 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1081 fc_vport_show_function(field, format_string, sz, (cast)) \
1082 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1083 show_fc_vport_##field, NULL)
1085 #define fc_vport_rw_attr(field, format_string, sz) \
1086 fc_vport_show_function(field, format_string, sz, ) \
1087 fc_vport_store_function(field) \
1088 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1089 show_fc_vport_##field, \
1090 store_fc_vport_##field)
1092 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1094 show_fc_vport_##field (struct device *dev, \
1095 struct device_attribute *attr, char *buf) \
1097 struct fc_vport *vport = transport_class_to_vport(dev); \
1098 return snprintf(buf, sz, format_string, cast vport->field); \
1101 #define fc_private_vport_store_u32_function(field) \
1103 store_fc_vport_##field(struct device *dev, \
1104 struct device_attribute *attr, \
1105 const char *buf, size_t count) \
1108 struct fc_vport *vport = transport_class_to_vport(dev); \
1110 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1112 val = simple_strtoul(buf, &cp, 0); \
1113 if (*cp && (*cp != '\n')) \
1115 vport->field = val; \
1120 #define fc_private_vport_rd_attr(field, format_string, sz) \
1121 fc_private_vport_show_function(field, format_string, sz, ) \
1122 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1123 show_fc_vport_##field, NULL)
1125 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1126 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1127 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1128 show_fc_vport_##field, NULL)
1130 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1131 fc_private_vport_show_function(field, format_string, sz, ) \
1132 fc_private_vport_store_u32_function(field) \
1133 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1134 show_fc_vport_##field, \
1135 store_fc_vport_##field)
1138 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1140 show_fc_vport_##title (struct device *dev, \
1141 struct device_attribute *attr, \
1144 struct fc_vport *vport = transport_class_to_vport(dev); \
1146 name = get_fc_##title##_name(vport->title); \
1149 return snprintf(buf, maxlen, "%s\n", name); \
1151 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1152 show_fc_vport_##title, NULL)
1155 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1156 i->private_vport_attrs[count] = device_attr_vport_##field; \
1157 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1158 i->private_vport_attrs[count].store = NULL; \
1159 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1160 if (i->f->get_##field) \
1162 /* NOTE: Above MACRO differs: checks function not show bit */
1164 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1165 i->private_vport_attrs[count] = device_attr_vport_##field; \
1166 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1167 i->private_vport_attrs[count].store = NULL; \
1168 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1171 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1172 i->private_vport_attrs[count] = device_attr_vport_##field; \
1173 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1176 /* NOTE: Above MACRO differs: checks function */
1178 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1179 i->private_vport_attrs[count] = device_attr_vport_##field; \
1180 if (!i->f->set_vport_##field) { \
1181 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1182 i->private_vport_attrs[count].store = NULL; \
1184 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1186 /* NOTE: Above MACRO differs: does not check show bit */
1188 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1190 i->private_vport_attrs[count] = device_attr_vport_##field; \
1191 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1196 /* The FC Transport Virtual Port Attributes: */
1198 /* Fixed Virtual Port Attributes */
1200 /* Dynamic Virtual Port Attributes */
1202 /* Private Virtual Port Attributes */
1204 fc_private_vport_rd_enum_attr(vport_state
, FC_VPORTSTATE_MAX_NAMELEN
);
1205 fc_private_vport_rd_enum_attr(vport_last_state
, FC_VPORTSTATE_MAX_NAMELEN
);
1206 fc_private_vport_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
1207 fc_private_vport_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
1210 show_fc_vport_roles (struct device
*dev
, struct device_attribute
*attr
,
1213 struct fc_vport
*vport
= transport_class_to_vport(dev
);
1215 if (vport
->roles
== FC_PORT_ROLE_UNKNOWN
)
1216 return snprintf(buf
, 20, "unknown\n");
1217 return get_fc_port_roles_names(vport
->roles
, buf
);
1219 static FC_DEVICE_ATTR(vport
, roles
, S_IRUGO
, show_fc_vport_roles
, NULL
);
1221 fc_private_vport_rd_enum_attr(vport_type
, FC_PORTTYPE_MAX_NAMELEN
);
1223 fc_private_vport_show_function(symbolic_name
, "%s\n",
1224 FC_VPORT_SYMBOLIC_NAMELEN
+ 1, )
1225 fc_vport_store_str_function(symbolic_name
, FC_VPORT_SYMBOLIC_NAMELEN
)
1226 static FC_DEVICE_ATTR(vport
, symbolic_name
, S_IRUGO
| S_IWUSR
,
1227 show_fc_vport_symbolic_name
, store_fc_vport_symbolic_name
);
1230 store_fc_vport_delete(struct device
*dev
, struct device_attribute
*attr
,
1231 const char *buf
, size_t count
)
1233 struct fc_vport
*vport
= transport_class_to_vport(dev
);
1234 struct Scsi_Host
*shost
= vport_to_shost(vport
);
1235 unsigned long flags
;
1237 spin_lock_irqsave(shost
->host_lock
, flags
);
1238 if (vport
->flags
& (FC_VPORT_DEL
| FC_VPORT_CREATING
)) {
1239 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1242 vport
->flags
|= FC_VPORT_DELETING
;
1243 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1245 fc_queue_work(shost
, &vport
->vport_delete_work
);
1248 static FC_DEVICE_ATTR(vport
, vport_delete
, S_IWUSR
,
1249 NULL
, store_fc_vport_delete
);
1253 * Enable/Disable vport
1254 * Write "1" to disable, write "0" to enable
1257 store_fc_vport_disable(struct device
*dev
, struct device_attribute
*attr
,
1261 struct fc_vport
*vport
= transport_class_to_vport(dev
);
1262 struct Scsi_Host
*shost
= vport_to_shost(vport
);
1263 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1266 if (vport
->flags
& (FC_VPORT_DEL
| FC_VPORT_CREATING
))
1270 if (vport
->vport_state
!= FC_VPORT_DISABLED
)
1272 } else if (*buf
== '1') {
1273 if (vport
->vport_state
== FC_VPORT_DISABLED
)
1278 stat
= i
->f
->vport_disable(vport
, ((*buf
== '0') ? false : true));
1279 return stat
? stat
: count
;
1281 static FC_DEVICE_ATTR(vport
, vport_disable
, S_IWUSR
,
1282 NULL
, store_fc_vport_disable
);
1286 * Host Attribute Management
1289 #define fc_host_show_function(field, format_string, sz, cast) \
1291 show_fc_host_##field (struct device *dev, \
1292 struct device_attribute *attr, char *buf) \
1294 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1295 struct fc_internal *i = to_fc_internal(shost->transportt); \
1296 if (i->f->get_host_##field) \
1297 i->f->get_host_##field(shost); \
1298 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1301 #define fc_host_store_function(field) \
1303 store_fc_host_##field(struct device *dev, \
1304 struct device_attribute *attr, \
1305 const char *buf, size_t count) \
1308 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1309 struct fc_internal *i = to_fc_internal(shost->transportt); \
1312 val = simple_strtoul(buf, &cp, 0); \
1313 if (*cp && (*cp != '\n')) \
1315 i->f->set_host_##field(shost, val); \
1319 #define fc_host_store_str_function(field, slen) \
1321 store_fc_host_##field(struct device *dev, \
1322 struct device_attribute *attr, \
1323 const char *buf, size_t count) \
1325 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1326 struct fc_internal *i = to_fc_internal(shost->transportt); \
1327 unsigned int cnt=count; \
1329 /* count may include a LF at end of string */ \
1330 if (buf[cnt-1] == '\n') \
1332 if (cnt > ((slen) - 1)) \
1334 memcpy(fc_host_##field(shost), buf, cnt); \
1335 i->f->set_host_##field(shost); \
1339 #define fc_host_rd_attr(field, format_string, sz) \
1340 fc_host_show_function(field, format_string, sz, ) \
1341 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1342 show_fc_host_##field, NULL)
1344 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1345 fc_host_show_function(field, format_string, sz, (cast)) \
1346 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1347 show_fc_host_##field, NULL)
1349 #define fc_host_rw_attr(field, format_string, sz) \
1350 fc_host_show_function(field, format_string, sz, ) \
1351 fc_host_store_function(field) \
1352 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1353 show_fc_host_##field, \
1354 store_fc_host_##field)
1356 #define fc_host_rd_enum_attr(title, maxlen) \
1358 show_fc_host_##title (struct device *dev, \
1359 struct device_attribute *attr, char *buf) \
1361 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1362 struct fc_internal *i = to_fc_internal(shost->transportt); \
1364 if (i->f->get_host_##title) \
1365 i->f->get_host_##title(shost); \
1366 name = get_fc_##title##_name(fc_host_##title(shost)); \
1369 return snprintf(buf, maxlen, "%s\n", name); \
1371 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1373 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1374 i->private_host_attrs[count] = device_attr_host_##field; \
1375 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1376 i->private_host_attrs[count].store = NULL; \
1377 i->host_attrs[count] = &i->private_host_attrs[count]; \
1378 if (i->f->show_host_##field) \
1381 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1382 i->private_host_attrs[count] = device_attr_host_##field; \
1383 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1384 i->private_host_attrs[count].store = NULL; \
1385 i->host_attrs[count] = &i->private_host_attrs[count]; \
1388 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1389 i->private_host_attrs[count] = device_attr_host_##field; \
1390 if (!i->f->set_host_##field) { \
1391 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1392 i->private_host_attrs[count].store = NULL; \
1394 i->host_attrs[count] = &i->private_host_attrs[count]; \
1395 if (i->f->show_host_##field) \
1399 #define fc_private_host_show_function(field, format_string, sz, cast) \
1401 show_fc_host_##field (struct device *dev, \
1402 struct device_attribute *attr, char *buf) \
1404 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1405 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1408 #define fc_private_host_rd_attr(field, format_string, sz) \
1409 fc_private_host_show_function(field, format_string, sz, ) \
1410 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1411 show_fc_host_##field, NULL)
1413 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1414 fc_private_host_show_function(field, format_string, sz, (cast)) \
1415 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1416 show_fc_host_##field, NULL)
1418 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1419 i->private_host_attrs[count] = device_attr_host_##field; \
1420 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1421 i->private_host_attrs[count].store = NULL; \
1422 i->host_attrs[count] = &i->private_host_attrs[count]; \
1425 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1427 i->private_host_attrs[count] = device_attr_host_##field; \
1428 i->host_attrs[count] = &i->private_host_attrs[count]; \
1433 /* Fixed Host Attributes */
1436 show_fc_host_supported_classes (struct device
*dev
,
1437 struct device_attribute
*attr
, char *buf
)
1439 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1441 if (fc_host_supported_classes(shost
) == FC_COS_UNSPECIFIED
)
1442 return snprintf(buf
, 20, "unspecified\n");
1444 return get_fc_cos_names(fc_host_supported_classes(shost
), buf
);
1446 static FC_DEVICE_ATTR(host
, supported_classes
, S_IRUGO
,
1447 show_fc_host_supported_classes
, NULL
);
1450 show_fc_host_supported_fc4s (struct device
*dev
,
1451 struct device_attribute
*attr
, char *buf
)
1453 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1454 return (ssize_t
)show_fc_fc4s(buf
, fc_host_supported_fc4s(shost
));
1456 static FC_DEVICE_ATTR(host
, supported_fc4s
, S_IRUGO
,
1457 show_fc_host_supported_fc4s
, NULL
);
1460 show_fc_host_supported_speeds (struct device
*dev
,
1461 struct device_attribute
*attr
, char *buf
)
1463 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1465 if (fc_host_supported_speeds(shost
) == FC_PORTSPEED_UNKNOWN
)
1466 return snprintf(buf
, 20, "unknown\n");
1468 return get_fc_port_speed_names(fc_host_supported_speeds(shost
), buf
);
1470 static FC_DEVICE_ATTR(host
, supported_speeds
, S_IRUGO
,
1471 show_fc_host_supported_speeds
, NULL
);
1474 fc_private_host_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
1475 fc_private_host_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
1476 fc_private_host_rd_attr_cast(permanent_port_name
, "0x%llx\n", 20,
1477 unsigned long long);
1478 fc_private_host_rd_attr(maxframe_size
, "%u bytes\n", 20);
1479 fc_private_host_rd_attr(max_npiv_vports
, "%u\n", 20);
1480 fc_private_host_rd_attr(serial_number
, "%s\n", (FC_SERIAL_NUMBER_SIZE
+1));
1483 /* Dynamic Host Attributes */
1486 show_fc_host_active_fc4s (struct device
*dev
,
1487 struct device_attribute
*attr
, char *buf
)
1489 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1490 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1492 if (i
->f
->get_host_active_fc4s
)
1493 i
->f
->get_host_active_fc4s(shost
);
1495 return (ssize_t
)show_fc_fc4s(buf
, fc_host_active_fc4s(shost
));
1497 static FC_DEVICE_ATTR(host
, active_fc4s
, S_IRUGO
,
1498 show_fc_host_active_fc4s
, NULL
);
1501 show_fc_host_speed (struct device
*dev
,
1502 struct device_attribute
*attr
, char *buf
)
1504 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1505 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1507 if (i
->f
->get_host_speed
)
1508 i
->f
->get_host_speed(shost
);
1510 if (fc_host_speed(shost
) == FC_PORTSPEED_UNKNOWN
)
1511 return snprintf(buf
, 20, "unknown\n");
1513 return get_fc_port_speed_names(fc_host_speed(shost
), buf
);
1515 static FC_DEVICE_ATTR(host
, speed
, S_IRUGO
,
1516 show_fc_host_speed
, NULL
);
1519 fc_host_rd_attr(port_id
, "0x%06x\n", 20);
1520 fc_host_rd_enum_attr(port_type
, FC_PORTTYPE_MAX_NAMELEN
);
1521 fc_host_rd_enum_attr(port_state
, FC_PORTSTATE_MAX_NAMELEN
);
1522 fc_host_rd_attr_cast(fabric_name
, "0x%llx\n", 20, unsigned long long);
1523 fc_host_rd_attr(symbolic_name
, "%s\n", FC_SYMBOLIC_NAME_SIZE
+ 1);
1525 fc_private_host_show_function(system_hostname
, "%s\n",
1526 FC_SYMBOLIC_NAME_SIZE
+ 1, )
1527 fc_host_store_str_function(system_hostname
, FC_SYMBOLIC_NAME_SIZE
)
1528 static FC_DEVICE_ATTR(host
, system_hostname
, S_IRUGO
| S_IWUSR
,
1529 show_fc_host_system_hostname
, store_fc_host_system_hostname
);
1532 /* Private Host Attributes */
1535 show_fc_private_host_tgtid_bind_type(struct device
*dev
,
1536 struct device_attribute
*attr
, char *buf
)
1538 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1541 name
= get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost
));
1544 return snprintf(buf
, FC_BINDTYPE_MAX_NAMELEN
, "%s\n", name
);
1547 #define get_list_head_entry(pos, head, member) \
1548 pos = list_entry((head)->next, typeof(*pos), member)
1551 store_fc_private_host_tgtid_bind_type(struct device
*dev
,
1552 struct device_attribute
*attr
, const char *buf
, size_t count
)
1554 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1555 struct fc_rport
*rport
;
1556 enum fc_tgtid_binding_type val
;
1557 unsigned long flags
;
1559 if (get_fc_tgtid_bind_type_match(buf
, &val
))
1562 /* if changing bind type, purge all unused consistent bindings */
1563 if (val
!= fc_host_tgtid_bind_type(shost
)) {
1564 spin_lock_irqsave(shost
->host_lock
, flags
);
1565 while (!list_empty(&fc_host_rport_bindings(shost
))) {
1566 get_list_head_entry(rport
,
1567 &fc_host_rport_bindings(shost
), peers
);
1568 list_del(&rport
->peers
);
1569 rport
->port_state
= FC_PORTSTATE_DELETED
;
1570 fc_queue_work(shost
, &rport
->rport_delete_work
);
1572 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1575 fc_host_tgtid_bind_type(shost
) = val
;
1579 static FC_DEVICE_ATTR(host
, tgtid_bind_type
, S_IRUGO
| S_IWUSR
,
1580 show_fc_private_host_tgtid_bind_type
,
1581 store_fc_private_host_tgtid_bind_type
);
1584 store_fc_private_host_issue_lip(struct device
*dev
,
1585 struct device_attribute
*attr
, const char *buf
, size_t count
)
1587 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1588 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1591 /* ignore any data value written to the attribute */
1592 if (i
->f
->issue_fc_host_lip
) {
1593 ret
= i
->f
->issue_fc_host_lip(shost
);
1594 return ret
? ret
: count
;
1600 static FC_DEVICE_ATTR(host
, issue_lip
, S_IWUSR
, NULL
,
1601 store_fc_private_host_issue_lip
);
1603 fc_private_host_rd_attr(npiv_vports_inuse
, "%u\n", 20);
1607 * Host Statistics Management
1610 /* Show a given an attribute in the statistics group */
1612 fc_stat_show(const struct device
*dev
, char *buf
, unsigned long offset
)
1614 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1615 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1616 struct fc_host_statistics
*stats
;
1617 ssize_t ret
= -ENOENT
;
1619 if (offset
> sizeof(struct fc_host_statistics
) ||
1620 offset
% sizeof(u64
) != 0)
1623 if (i
->f
->get_fc_host_stats
) {
1624 stats
= (i
->f
->get_fc_host_stats
)(shost
);
1626 ret
= snprintf(buf
, 20, "0x%llx\n",
1627 (unsigned long long)*(u64
*)(((u8
*) stats
) + offset
));
1633 /* generate a read-only statistics attribute */
1634 #define fc_host_statistic(name) \
1635 static ssize_t show_fcstat_##name(struct device *cd, \
1636 struct device_attribute *attr, \
1639 return fc_stat_show(cd, buf, \
1640 offsetof(struct fc_host_statistics, name)); \
1642 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1644 fc_host_statistic(seconds_since_last_reset
);
1645 fc_host_statistic(tx_frames
);
1646 fc_host_statistic(tx_words
);
1647 fc_host_statistic(rx_frames
);
1648 fc_host_statistic(rx_words
);
1649 fc_host_statistic(lip_count
);
1650 fc_host_statistic(nos_count
);
1651 fc_host_statistic(error_frames
);
1652 fc_host_statistic(dumped_frames
);
1653 fc_host_statistic(link_failure_count
);
1654 fc_host_statistic(loss_of_sync_count
);
1655 fc_host_statistic(loss_of_signal_count
);
1656 fc_host_statistic(prim_seq_protocol_err_count
);
1657 fc_host_statistic(invalid_tx_word_count
);
1658 fc_host_statistic(invalid_crc_count
);
1659 fc_host_statistic(fcp_input_requests
);
1660 fc_host_statistic(fcp_output_requests
);
1661 fc_host_statistic(fcp_control_requests
);
1662 fc_host_statistic(fcp_input_megabytes
);
1663 fc_host_statistic(fcp_output_megabytes
);
1666 fc_reset_statistics(struct device
*dev
, struct device_attribute
*attr
,
1667 const char *buf
, size_t count
)
1669 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1670 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1672 /* ignore any data value written to the attribute */
1673 if (i
->f
->reset_fc_host_stats
) {
1674 i
->f
->reset_fc_host_stats(shost
);
1680 static FC_DEVICE_ATTR(host
, reset_statistics
, S_IWUSR
, NULL
,
1681 fc_reset_statistics
);
1683 static struct attribute
*fc_statistics_attrs
[] = {
1684 &device_attr_host_seconds_since_last_reset
.attr
,
1685 &device_attr_host_tx_frames
.attr
,
1686 &device_attr_host_tx_words
.attr
,
1687 &device_attr_host_rx_frames
.attr
,
1688 &device_attr_host_rx_words
.attr
,
1689 &device_attr_host_lip_count
.attr
,
1690 &device_attr_host_nos_count
.attr
,
1691 &device_attr_host_error_frames
.attr
,
1692 &device_attr_host_dumped_frames
.attr
,
1693 &device_attr_host_link_failure_count
.attr
,
1694 &device_attr_host_loss_of_sync_count
.attr
,
1695 &device_attr_host_loss_of_signal_count
.attr
,
1696 &device_attr_host_prim_seq_protocol_err_count
.attr
,
1697 &device_attr_host_invalid_tx_word_count
.attr
,
1698 &device_attr_host_invalid_crc_count
.attr
,
1699 &device_attr_host_fcp_input_requests
.attr
,
1700 &device_attr_host_fcp_output_requests
.attr
,
1701 &device_attr_host_fcp_control_requests
.attr
,
1702 &device_attr_host_fcp_input_megabytes
.attr
,
1703 &device_attr_host_fcp_output_megabytes
.attr
,
1704 &device_attr_host_reset_statistics
.attr
,
1708 static struct attribute_group fc_statistics_group
= {
1709 .name
= "statistics",
1710 .attrs
= fc_statistics_attrs
,
1714 /* Host Vport Attributes */
1717 fc_parse_wwn(const char *ns
, u64
*nm
)
1722 memset(wwn
, 0, sizeof(wwn
));
1724 /* Validate and store the new name */
1725 for (i
=0, j
=0; i
< 16; i
++) {
1726 if ((*ns
>= 'a') && (*ns
<= 'f'))
1727 j
= ((j
<< 4) | ((*ns
++ -'a') + 10));
1728 else if ((*ns
>= 'A') && (*ns
<= 'F'))
1729 j
= ((j
<< 4) | ((*ns
++ -'A') + 10));
1730 else if ((*ns
>= '0') && (*ns
<= '9'))
1731 j
= ((j
<< 4) | (*ns
++ -'0'));
1735 wwn
[i
/2] = j
& 0xff;
1740 *nm
= wwn_to_u64(wwn
);
1747 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1748 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1749 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1750 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1753 store_fc_host_vport_create(struct device
*dev
, struct device_attribute
*attr
,
1754 const char *buf
, size_t count
)
1756 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1757 struct fc_vport_identifiers vid
;
1758 struct fc_vport
*vport
;
1759 unsigned int cnt
=count
;
1762 memset(&vid
, 0, sizeof(vid
));
1764 /* count may include a LF at end of string */
1765 if (buf
[cnt
-1] == '\n')
1768 /* validate we have enough characters for WWPN */
1769 if ((cnt
!= (16+1+16)) || (buf
[16] != ':'))
1772 stat
= fc_parse_wwn(&buf
[0], &vid
.port_name
);
1776 stat
= fc_parse_wwn(&buf
[17], &vid
.node_name
);
1780 vid
.roles
= FC_PORT_ROLE_FCP_INITIATOR
;
1781 vid
.vport_type
= FC_PORTTYPE_NPIV
;
1782 /* vid.symbolic_name is already zero/NULL's */
1783 vid
.disable
= false; /* always enabled */
1785 /* we only allow support on Channel 0 !!! */
1786 stat
= fc_vport_setup(shost
, 0, &shost
->shost_gendev
, &vid
, &vport
);
1787 return stat
? stat
: count
;
1789 static FC_DEVICE_ATTR(host
, vport_create
, S_IWUSR
, NULL
,
1790 store_fc_host_vport_create
);
1794 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1795 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1796 * The WWNs are specified as hex characters, and may *not* contain
1797 * any prefixes (e.g. 0x, x, etc)
1800 store_fc_host_vport_delete(struct device
*dev
, struct device_attribute
*attr
,
1801 const char *buf
, size_t count
)
1803 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1804 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
1805 struct fc_vport
*vport
;
1807 unsigned long flags
;
1808 unsigned int cnt
=count
;
1811 /* count may include a LF at end of string */
1812 if (buf
[cnt
-1] == '\n')
1815 /* validate we have enough characters for WWPN */
1816 if ((cnt
!= (16+1+16)) || (buf
[16] != ':'))
1819 stat
= fc_parse_wwn(&buf
[0], &wwpn
);
1823 stat
= fc_parse_wwn(&buf
[17], &wwnn
);
1827 spin_lock_irqsave(shost
->host_lock
, flags
);
1829 /* we only allow support on Channel 0 !!! */
1830 list_for_each_entry(vport
, &fc_host
->vports
, peers
) {
1831 if ((vport
->channel
== 0) &&
1832 (vport
->port_name
== wwpn
) && (vport
->node_name
== wwnn
)) {
1833 if (vport
->flags
& (FC_VPORT_DEL
| FC_VPORT_CREATING
))
1835 vport
->flags
|= FC_VPORT_DELETING
;
1840 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1845 stat
= fc_vport_terminate(vport
);
1846 return stat
? stat
: count
;
1848 static FC_DEVICE_ATTR(host
, vport_delete
, S_IWUSR
, NULL
,
1849 store_fc_host_vport_delete
);
1852 static int fc_host_match(struct attribute_container
*cont
,
1855 struct Scsi_Host
*shost
;
1856 struct fc_internal
*i
;
1858 if (!scsi_is_host_device(dev
))
1861 shost
= dev_to_shost(dev
);
1862 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1863 != &fc_host_class
.class)
1866 i
= to_fc_internal(shost
->transportt
);
1868 return &i
->t
.host_attrs
.ac
== cont
;
1871 static int fc_target_match(struct attribute_container
*cont
,
1874 struct Scsi_Host
*shost
;
1875 struct fc_internal
*i
;
1877 if (!scsi_is_target_device(dev
))
1880 shost
= dev_to_shost(dev
->parent
);
1881 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1882 != &fc_host_class
.class)
1885 i
= to_fc_internal(shost
->transportt
);
1887 return &i
->t
.target_attrs
.ac
== cont
;
1890 static void fc_rport_dev_release(struct device
*dev
)
1892 struct fc_rport
*rport
= dev_to_rport(dev
);
1893 put_device(dev
->parent
);
1897 int scsi_is_fc_rport(const struct device
*dev
)
1899 return dev
->release
== fc_rport_dev_release
;
1901 EXPORT_SYMBOL(scsi_is_fc_rport
);
1903 static int fc_rport_match(struct attribute_container
*cont
,
1906 struct Scsi_Host
*shost
;
1907 struct fc_internal
*i
;
1909 if (!scsi_is_fc_rport(dev
))
1912 shost
= dev_to_shost(dev
->parent
);
1913 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1914 != &fc_host_class
.class)
1917 i
= to_fc_internal(shost
->transportt
);
1919 return &i
->rport_attr_cont
.ac
== cont
;
1923 static void fc_vport_dev_release(struct device
*dev
)
1925 struct fc_vport
*vport
= dev_to_vport(dev
);
1926 put_device(dev
->parent
); /* release kobj parent */
1930 int scsi_is_fc_vport(const struct device
*dev
)
1932 return dev
->release
== fc_vport_dev_release
;
1934 EXPORT_SYMBOL(scsi_is_fc_vport
);
1936 static int fc_vport_match(struct attribute_container
*cont
,
1939 struct fc_vport
*vport
;
1940 struct Scsi_Host
*shost
;
1941 struct fc_internal
*i
;
1943 if (!scsi_is_fc_vport(dev
))
1945 vport
= dev_to_vport(dev
);
1947 shost
= vport_to_shost(vport
);
1948 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1949 != &fc_host_class
.class)
1952 i
= to_fc_internal(shost
->transportt
);
1953 return &i
->vport_attr_cont
.ac
== cont
;
1958 * fc_timed_out - FC Transport I/O timeout intercept handler
1959 * @scmd: The SCSI command which timed out
1961 * This routine protects against error handlers getting invoked while a
1962 * rport is in a blocked state, typically due to a temporarily loss of
1963 * connectivity. If the error handlers are allowed to proceed, requests
1964 * to abort i/o, reset the target, etc will likely fail as there is no way
1965 * to communicate with the device to perform the requested function. These
1966 * failures may result in the midlayer taking the device offline, requiring
1967 * manual intervention to restore operation.
1969 * This routine, called whenever an i/o times out, validates the state of
1970 * the underlying rport. If the rport is blocked, it returns
1971 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1972 * Eventually, either the device will return, or devloss_tmo will fire,
1973 * and when the timeout then fires, it will be handled normally.
1974 * If the rport is not blocked, normal error handling continues.
1977 * This routine assumes no locks are held on entry.
1979 static enum blk_eh_timer_return
1980 fc_timed_out(struct scsi_cmnd
*scmd
)
1982 struct fc_rport
*rport
= starget_to_rport(scsi_target(scmd
->device
));
1984 if (rport
->port_state
== FC_PORTSTATE_BLOCKED
)
1985 return BLK_EH_RESET_TIMER
;
1987 return BLK_EH_NOT_HANDLED
;
1991 * Called by fc_user_scan to locate an rport on the shost that
1992 * matches the channel and target id, and invoke scsi_scan_target()
1996 fc_user_scan_tgt(struct Scsi_Host
*shost
, uint channel
, uint id
, uint lun
)
1998 struct fc_rport
*rport
;
1999 unsigned long flags
;
2001 spin_lock_irqsave(shost
->host_lock
, flags
);
2003 list_for_each_entry(rport
, &fc_host_rports(shost
), peers
) {
2004 if (rport
->scsi_target_id
== -1)
2007 if (rport
->port_state
!= FC_PORTSTATE_ONLINE
)
2010 if ((channel
== rport
->channel
) &&
2011 (id
== rport
->scsi_target_id
)) {
2012 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2013 scsi_scan_target(&rport
->dev
, channel
, id
, lun
, 1);
2018 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2022 * Called via sysfs scan routines. Necessary, as the FC transport
2023 * wants to place all target objects below the rport object. So this
2024 * routine must invoke the scsi_scan_target() routine with the rport
2025 * object as the parent.
2028 fc_user_scan(struct Scsi_Host
*shost
, uint channel
, uint id
, uint lun
)
2033 if (((channel
!= SCAN_WILD_CARD
) && (channel
> shost
->max_channel
)) ||
2034 ((id
!= SCAN_WILD_CARD
) && (id
>= shost
->max_id
)) ||
2035 ((lun
!= SCAN_WILD_CARD
) && (lun
> shost
->max_lun
)))
2038 if (channel
== SCAN_WILD_CARD
) {
2040 chhi
= shost
->max_channel
+ 1;
2046 if (id
== SCAN_WILD_CARD
) {
2048 tgthi
= shost
->max_id
;
2054 for ( ; chlo
< chhi
; chlo
++)
2055 for ( ; tgtlo
< tgthi
; tgtlo
++)
2056 fc_user_scan_tgt(shost
, chlo
, tgtlo
, lun
);
2061 static int fc_tsk_mgmt_response(struct Scsi_Host
*shost
, u64 nexus
, u64 tm_id
,
2064 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2065 return i
->f
->tsk_mgmt_response(shost
, nexus
, tm_id
, result
);
2068 static int fc_it_nexus_response(struct Scsi_Host
*shost
, u64 nexus
, int result
)
2070 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2071 return i
->f
->it_nexus_response(shost
, nexus
, result
);
2074 struct scsi_transport_template
*
2075 fc_attach_transport(struct fc_function_template
*ft
)
2078 struct fc_internal
*i
= kzalloc(sizeof(struct fc_internal
),
2084 i
->t
.target_attrs
.ac
.attrs
= &i
->starget_attrs
[0];
2085 i
->t
.target_attrs
.ac
.class = &fc_transport_class
.class;
2086 i
->t
.target_attrs
.ac
.match
= fc_target_match
;
2087 i
->t
.target_size
= sizeof(struct fc_starget_attrs
);
2088 transport_container_register(&i
->t
.target_attrs
);
2090 i
->t
.host_attrs
.ac
.attrs
= &i
->host_attrs
[0];
2091 i
->t
.host_attrs
.ac
.class = &fc_host_class
.class;
2092 i
->t
.host_attrs
.ac
.match
= fc_host_match
;
2093 i
->t
.host_size
= sizeof(struct fc_host_attrs
);
2094 if (ft
->get_fc_host_stats
)
2095 i
->t
.host_attrs
.statistics
= &fc_statistics_group
;
2096 transport_container_register(&i
->t
.host_attrs
);
2098 i
->rport_attr_cont
.ac
.attrs
= &i
->rport_attrs
[0];
2099 i
->rport_attr_cont
.ac
.class = &fc_rport_class
.class;
2100 i
->rport_attr_cont
.ac
.match
= fc_rport_match
;
2101 transport_container_register(&i
->rport_attr_cont
);
2103 i
->vport_attr_cont
.ac
.attrs
= &i
->vport_attrs
[0];
2104 i
->vport_attr_cont
.ac
.class = &fc_vport_class
.class;
2105 i
->vport_attr_cont
.ac
.match
= fc_vport_match
;
2106 transport_container_register(&i
->vport_attr_cont
);
2110 /* Transport uses the shost workq for scsi scanning */
2111 i
->t
.create_work_queue
= 1;
2113 i
->t
.eh_timed_out
= fc_timed_out
;
2115 i
->t
.user_scan
= fc_user_scan
;
2117 /* target-mode drivers' functions */
2118 i
->t
.tsk_mgmt_response
= fc_tsk_mgmt_response
;
2119 i
->t
.it_nexus_response
= fc_it_nexus_response
;
2122 * Setup SCSI Target Attributes.
2125 SETUP_STARGET_ATTRIBUTE_RD(node_name
);
2126 SETUP_STARGET_ATTRIBUTE_RD(port_name
);
2127 SETUP_STARGET_ATTRIBUTE_RD(port_id
);
2129 BUG_ON(count
> FC_STARGET_NUM_ATTRS
);
2131 i
->starget_attrs
[count
] = NULL
;
2135 * Setup SCSI Host Attributes.
2138 SETUP_HOST_ATTRIBUTE_RD(node_name
);
2139 SETUP_HOST_ATTRIBUTE_RD(port_name
);
2140 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name
);
2141 SETUP_HOST_ATTRIBUTE_RD(supported_classes
);
2142 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s
);
2143 SETUP_HOST_ATTRIBUTE_RD(supported_speeds
);
2144 SETUP_HOST_ATTRIBUTE_RD(maxframe_size
);
2145 if (ft
->vport_create
) {
2146 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports
);
2147 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse
);
2149 SETUP_HOST_ATTRIBUTE_RD(serial_number
);
2151 SETUP_HOST_ATTRIBUTE_RD(port_id
);
2152 SETUP_HOST_ATTRIBUTE_RD(port_type
);
2153 SETUP_HOST_ATTRIBUTE_RD(port_state
);
2154 SETUP_HOST_ATTRIBUTE_RD(active_fc4s
);
2155 SETUP_HOST_ATTRIBUTE_RD(speed
);
2156 SETUP_HOST_ATTRIBUTE_RD(fabric_name
);
2157 SETUP_HOST_ATTRIBUTE_RD(symbolic_name
);
2158 SETUP_HOST_ATTRIBUTE_RW(system_hostname
);
2160 /* Transport-managed attributes */
2161 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type
);
2162 if (ft
->issue_fc_host_lip
)
2163 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip
);
2164 if (ft
->vport_create
)
2165 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create
);
2166 if (ft
->vport_delete
)
2167 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete
);
2169 BUG_ON(count
> FC_HOST_NUM_ATTRS
);
2171 i
->host_attrs
[count
] = NULL
;
2174 * Setup Remote Port Attributes.
2177 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size
);
2178 SETUP_RPORT_ATTRIBUTE_RD(supported_classes
);
2179 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo
);
2180 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name
);
2181 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name
);
2182 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id
);
2183 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles
);
2184 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state
);
2185 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id
);
2186 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo
);
2188 BUG_ON(count
> FC_RPORT_NUM_ATTRS
);
2190 i
->rport_attrs
[count
] = NULL
;
2193 * Setup Virtual Port Attributes.
2196 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state
);
2197 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state
);
2198 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name
);
2199 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name
);
2200 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles
);
2201 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type
);
2202 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name
);
2203 SETUP_VPORT_ATTRIBUTE_WR(vport_delete
);
2204 SETUP_VPORT_ATTRIBUTE_WR(vport_disable
);
2206 BUG_ON(count
> FC_VPORT_NUM_ATTRS
);
2208 i
->vport_attrs
[count
] = NULL
;
2212 EXPORT_SYMBOL(fc_attach_transport
);
2214 void fc_release_transport(struct scsi_transport_template
*t
)
2216 struct fc_internal
*i
= to_fc_internal(t
);
2218 transport_container_unregister(&i
->t
.target_attrs
);
2219 transport_container_unregister(&i
->t
.host_attrs
);
2220 transport_container_unregister(&i
->rport_attr_cont
);
2221 transport_container_unregister(&i
->vport_attr_cont
);
2225 EXPORT_SYMBOL(fc_release_transport
);
2228 * fc_queue_work - Queue work to the fc_host workqueue.
2229 * @shost: Pointer to Scsi_Host bound to fc_host.
2230 * @work: Work to queue for execution.
2233 * 1 - work queued for execution
2234 * 0 - work is already queued
2235 * -EINVAL - work queue doesn't exist
2238 fc_queue_work(struct Scsi_Host
*shost
, struct work_struct
*work
)
2240 if (unlikely(!fc_host_work_q(shost
))) {
2242 "ERROR: FC host '%s' attempted to queue work, "
2243 "when no workqueue created.\n", shost
->hostt
->name
);
2249 return queue_work(fc_host_work_q(shost
), work
);
2253 * fc_flush_work - Flush a fc_host's workqueue.
2254 * @shost: Pointer to Scsi_Host bound to fc_host.
2257 fc_flush_work(struct Scsi_Host
*shost
)
2259 if (!fc_host_work_q(shost
)) {
2261 "ERROR: FC host '%s' attempted to flush work, "
2262 "when no workqueue created.\n", shost
->hostt
->name
);
2267 flush_workqueue(fc_host_work_q(shost
));
2271 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2272 * @shost: Pointer to Scsi_Host bound to fc_host.
2273 * @work: Work to queue for execution.
2274 * @delay: jiffies to delay the work queuing
2277 * 1 on success / 0 already queued / < 0 for error
2280 fc_queue_devloss_work(struct Scsi_Host
*shost
, struct delayed_work
*work
,
2281 unsigned long delay
)
2283 if (unlikely(!fc_host_devloss_work_q(shost
))) {
2285 "ERROR: FC host '%s' attempted to queue work, "
2286 "when no workqueue created.\n", shost
->hostt
->name
);
2292 return queue_delayed_work(fc_host_devloss_work_q(shost
), work
, delay
);
2296 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2297 * @shost: Pointer to Scsi_Host bound to fc_host.
2300 fc_flush_devloss(struct Scsi_Host
*shost
)
2302 if (!fc_host_devloss_work_q(shost
)) {
2304 "ERROR: FC host '%s' attempted to flush work, "
2305 "when no workqueue created.\n", shost
->hostt
->name
);
2310 flush_workqueue(fc_host_devloss_work_q(shost
));
2315 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2316 * @shost: Which &Scsi_Host
2318 * This routine is expected to be called immediately preceeding the
2319 * a driver's call to scsi_remove_host().
2321 * WARNING: A driver utilizing the fc_transport, which fails to call
2322 * this routine prior to scsi_remove_host(), will leave dangling
2323 * objects in /sys/class/fc_remote_ports. Access to any of these
2324 * objects can result in a system crash !!!
2327 * This routine assumes no locks are held on entry.
2330 fc_remove_host(struct Scsi_Host
*shost
)
2332 struct fc_vport
*vport
= NULL
, *next_vport
= NULL
;
2333 struct fc_rport
*rport
= NULL
, *next_rport
= NULL
;
2334 struct workqueue_struct
*work_q
;
2335 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2336 unsigned long flags
;
2338 spin_lock_irqsave(shost
->host_lock
, flags
);
2340 /* Remove any vports */
2341 list_for_each_entry_safe(vport
, next_vport
, &fc_host
->vports
, peers
)
2342 fc_queue_work(shost
, &vport
->vport_delete_work
);
2344 /* Remove any remote ports */
2345 list_for_each_entry_safe(rport
, next_rport
,
2346 &fc_host
->rports
, peers
) {
2347 list_del(&rport
->peers
);
2348 rport
->port_state
= FC_PORTSTATE_DELETED
;
2349 fc_queue_work(shost
, &rport
->rport_delete_work
);
2352 list_for_each_entry_safe(rport
, next_rport
,
2353 &fc_host
->rport_bindings
, peers
) {
2354 list_del(&rport
->peers
);
2355 rport
->port_state
= FC_PORTSTATE_DELETED
;
2356 fc_queue_work(shost
, &rport
->rport_delete_work
);
2359 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2361 /* flush all scan work items */
2362 scsi_flush_work(shost
);
2364 /* flush all stgt delete, and rport delete work items, then kill it */
2365 if (fc_host
->work_q
) {
2366 work_q
= fc_host
->work_q
;
2367 fc_host
->work_q
= NULL
;
2368 destroy_workqueue(work_q
);
2371 /* flush all devloss work items, then kill it */
2372 if (fc_host
->devloss_work_q
) {
2373 work_q
= fc_host
->devloss_work_q
;
2374 fc_host
->devloss_work_q
= NULL
;
2375 destroy_workqueue(work_q
);
2378 EXPORT_SYMBOL(fc_remove_host
);
2380 static void fc_terminate_rport_io(struct fc_rport
*rport
)
2382 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2383 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2385 /* Involve the LLDD if possible to terminate all io on the rport. */
2386 if (i
->f
->terminate_rport_io
)
2387 i
->f
->terminate_rport_io(rport
);
2390 * must unblock to flush queued IO. The caller will have set
2391 * the port_state or flags, so that fc_remote_port_chkready will
2394 scsi_target_unblock(&rport
->dev
);
2398 * fc_starget_delete - called to delete the scsi decendents of an rport
2399 * @work: remote port to be operated on.
2401 * Deletes target and all sdevs.
2404 fc_starget_delete(struct work_struct
*work
)
2406 struct fc_rport
*rport
=
2407 container_of(work
, struct fc_rport
, stgt_delete_work
);
2409 fc_terminate_rport_io(rport
);
2410 scsi_remove_target(&rport
->dev
);
2415 * fc_rport_final_delete - finish rport termination and delete it.
2416 * @work: remote port to be deleted.
2419 fc_rport_final_delete(struct work_struct
*work
)
2421 struct fc_rport
*rport
=
2422 container_of(work
, struct fc_rport
, rport_delete_work
);
2423 struct device
*dev
= &rport
->dev
;
2424 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2425 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2426 unsigned long flags
;
2427 int do_callback
= 0;
2430 * if a scan is pending, flush the SCSI Host work_q so that
2431 * that we can reclaim the rport scan work element.
2433 if (rport
->flags
& FC_RPORT_SCAN_PENDING
)
2434 scsi_flush_work(shost
);
2436 fc_terminate_rport_io(rport
);
2439 * Cancel any outstanding timers. These should really exist
2440 * only when rmmod'ing the LLDD and we're asking for
2441 * immediate termination of the rports
2443 spin_lock_irqsave(shost
->host_lock
, flags
);
2444 if (rport
->flags
& FC_RPORT_DEVLOSS_PENDING
) {
2445 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2446 if (!cancel_delayed_work(&rport
->fail_io_work
))
2447 fc_flush_devloss(shost
);
2448 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2449 fc_flush_devloss(shost
);
2450 spin_lock_irqsave(shost
->host_lock
, flags
);
2451 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
2453 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2455 /* Delete SCSI target and sdevs */
2456 if (rport
->scsi_target_id
!= -1)
2457 fc_starget_delete(&rport
->stgt_delete_work
);
2460 * Notify the driver that the rport is now dead. The LLDD will
2461 * also guarantee that any communication to the rport is terminated
2463 * Avoid this call if we already called it when we preserved the
2464 * rport for the binding.
2466 spin_lock_irqsave(shost
->host_lock
, flags
);
2467 if (!(rport
->flags
& FC_RPORT_DEVLOSS_CALLBK_DONE
) &&
2468 (i
->f
->dev_loss_tmo_callbk
)) {
2469 rport
->flags
|= FC_RPORT_DEVLOSS_CALLBK_DONE
;
2472 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2475 i
->f
->dev_loss_tmo_callbk(rport
);
2477 fc_bsg_remove(rport
->rqst_q
);
2479 transport_remove_device(dev
);
2481 transport_destroy_device(dev
);
2482 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2483 put_device(dev
); /* for self-reference */
2488 * fc_rport_create - allocates and creates a remote FC port.
2489 * @shost: scsi host the remote port is connected to.
2490 * @channel: Channel on shost port connected to.
2491 * @ids: The world wide names, fc address, and FC4 port
2492 * roles for the remote port.
2494 * Allocates and creates the remoter port structure, including the
2495 * class and sysfs creation.
2498 * This routine assumes no locks are held on entry.
2500 static struct fc_rport
*
2501 fc_rport_create(struct Scsi_Host
*shost
, int channel
,
2502 struct fc_rport_identifiers
*ids
)
2504 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2505 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2506 struct fc_rport
*rport
;
2508 unsigned long flags
;
2512 size
= (sizeof(struct fc_rport
) + fci
->f
->dd_fcrport_size
);
2513 rport
= kzalloc(size
, GFP_KERNEL
);
2514 if (unlikely(!rport
)) {
2515 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
2519 rport
->maxframe_size
= -1;
2520 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
2521 rport
->dev_loss_tmo
= fc_dev_loss_tmo
;
2522 memcpy(&rport
->node_name
, &ids
->node_name
, sizeof(rport
->node_name
));
2523 memcpy(&rport
->port_name
, &ids
->port_name
, sizeof(rport
->port_name
));
2524 rport
->port_id
= ids
->port_id
;
2525 rport
->roles
= ids
->roles
;
2526 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2527 if (fci
->f
->dd_fcrport_size
)
2528 rport
->dd_data
= &rport
[1];
2529 rport
->channel
= channel
;
2530 rport
->fast_io_fail_tmo
= -1;
2532 INIT_DELAYED_WORK(&rport
->dev_loss_work
, fc_timeout_deleted_rport
);
2533 INIT_DELAYED_WORK(&rport
->fail_io_work
, fc_timeout_fail_rport_io
);
2534 INIT_WORK(&rport
->scan_work
, fc_scsi_scan_rport
);
2535 INIT_WORK(&rport
->stgt_delete_work
, fc_starget_delete
);
2536 INIT_WORK(&rport
->rport_delete_work
, fc_rport_final_delete
);
2538 spin_lock_irqsave(shost
->host_lock
, flags
);
2540 rport
->number
= fc_host
->next_rport_number
++;
2541 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)
2542 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2544 rport
->scsi_target_id
= -1;
2545 list_add_tail(&rport
->peers
, &fc_host
->rports
);
2546 get_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2548 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2551 device_initialize(dev
); /* takes self reference */
2552 dev
->parent
= get_device(&shost
->shost_gendev
); /* parent reference */
2553 dev
->release
= fc_rport_dev_release
;
2554 dev_set_name(dev
, "rport-%d:%d-%d",
2555 shost
->host_no
, channel
, rport
->number
);
2556 transport_setup_device(dev
);
2558 error
= device_add(dev
);
2560 printk(KERN_ERR
"FC Remote Port device_add failed\n");
2563 transport_add_device(dev
);
2564 transport_configure_device(dev
);
2566 fc_bsg_rportadd(shost
, rport
);
2567 /* ignore any bsg add error - we just can't do sgio */
2569 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2570 /* initiate a scan of the target */
2571 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2572 scsi_queue_work(shost
, &rport
->scan_work
);
2578 transport_destroy_device(dev
);
2579 spin_lock_irqsave(shost
->host_lock
, flags
);
2580 list_del(&rport
->peers
);
2581 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2582 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2583 put_device(dev
->parent
);
2589 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2590 * @shost: scsi host the remote port is connected to.
2591 * @channel: Channel on shost port connected to.
2592 * @ids: The world wide names, fc address, and FC4 port
2593 * roles for the remote port.
2595 * The LLDD calls this routine to notify the transport of the existence
2596 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2597 * of the port, it's FC address (port_id), and the FC4 roles that are
2598 * active for the port.
2600 * For ports that are FCP targets (aka scsi targets), the FC transport
2601 * maintains consistent target id bindings on behalf of the LLDD.
2602 * A consistent target id binding is an assignment of a target id to
2603 * a remote port identifier, which persists while the scsi host is
2604 * attached. The remote port can disappear, then later reappear, and
2605 * it's target id assignment remains the same. This allows for shifts
2606 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2607 * changes to the scsi subsystem which is based on scsi host number and
2608 * target id values. Bindings are only valid during the attachment of
2609 * the scsi host. If the host detaches, then later re-attaches, target
2610 * id bindings may change.
2612 * This routine is responsible for returning a remote port structure.
2613 * The routine will search the list of remote ports it maintains
2614 * internally on behalf of consistent target id mappings. If found, the
2615 * remote port structure will be reused. Otherwise, a new remote port
2616 * structure will be allocated.
2618 * Whenever a remote port is allocated, a new fc_remote_port class
2619 * device is created.
2621 * Should not be called from interrupt context.
2624 * This routine assumes no locks are held on entry.
2627 fc_remote_port_add(struct Scsi_Host
*shost
, int channel
,
2628 struct fc_rport_identifiers
*ids
)
2630 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2631 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2632 struct fc_rport
*rport
;
2633 unsigned long flags
;
2636 /* ensure any stgt delete functions are done */
2637 fc_flush_work(shost
);
2640 * Search the list of "active" rports, for an rport that has been
2641 * deleted, but we've held off the real delete while the target
2642 * is in a "blocked" state.
2644 spin_lock_irqsave(shost
->host_lock
, flags
);
2646 list_for_each_entry(rport
, &fc_host
->rports
, peers
) {
2648 if ((rport
->port_state
== FC_PORTSTATE_BLOCKED
) &&
2649 (rport
->channel
== channel
)) {
2651 switch (fc_host
->tgtid_bind_type
) {
2652 case FC_TGTID_BIND_BY_WWPN
:
2653 case FC_TGTID_BIND_NONE
:
2654 if (rport
->port_name
== ids
->port_name
)
2657 case FC_TGTID_BIND_BY_WWNN
:
2658 if (rport
->node_name
== ids
->node_name
)
2661 case FC_TGTID_BIND_BY_ID
:
2662 if (rport
->port_id
== ids
->port_id
)
2669 memcpy(&rport
->node_name
, &ids
->node_name
,
2670 sizeof(rport
->node_name
));
2671 memcpy(&rport
->port_name
, &ids
->port_name
,
2672 sizeof(rport
->port_name
));
2673 rport
->port_id
= ids
->port_id
;
2675 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2676 rport
->roles
= ids
->roles
;
2678 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2680 if (fci
->f
->dd_fcrport_size
)
2681 memset(rport
->dd_data
, 0,
2682 fci
->f
->dd_fcrport_size
);
2685 * If we were not a target, cancel the
2686 * io terminate and rport timers, and
2689 * If we were a target, but our new role
2690 * doesn't indicate a target, leave the
2691 * timers running expecting the role to
2692 * change as the target fully logs in. If
2693 * it doesn't, the target will be torn down.
2695 * If we were a target, and our role shows
2696 * we're still a target, cancel the timers
2697 * and kick off a scan.
2700 /* was a target, not in roles */
2701 if ((rport
->scsi_target_id
!= -1) &&
2702 (!(ids
->roles
& FC_PORT_ROLE_FCP_TARGET
)))
2706 * Stop the fail io and dev_loss timers.
2707 * If they flush, the port_state will
2708 * be checked and will NOOP the function.
2710 if (!cancel_delayed_work(&rport
->fail_io_work
))
2711 fc_flush_devloss(shost
);
2712 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2713 fc_flush_devloss(shost
);
2715 spin_lock_irqsave(shost
->host_lock
, flags
);
2717 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2718 FC_RPORT_DEVLOSS_PENDING
|
2719 FC_RPORT_DEVLOSS_CALLBK_DONE
);
2721 /* if target, initiate a scan */
2722 if (rport
->scsi_target_id
!= -1) {
2723 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2724 scsi_queue_work(shost
,
2726 spin_unlock_irqrestore(shost
->host_lock
,
2728 scsi_target_unblock(&rport
->dev
);
2730 spin_unlock_irqrestore(shost
->host_lock
,
2733 fc_bsg_goose_queue(rport
);
2741 * Search the bindings array
2742 * Note: if never a FCP target, you won't be on this list
2744 if (fc_host
->tgtid_bind_type
!= FC_TGTID_BIND_NONE
) {
2746 /* search for a matching consistent binding */
2748 list_for_each_entry(rport
, &fc_host
->rport_bindings
,
2750 if (rport
->channel
!= channel
)
2753 switch (fc_host
->tgtid_bind_type
) {
2754 case FC_TGTID_BIND_BY_WWPN
:
2755 if (rport
->port_name
== ids
->port_name
)
2758 case FC_TGTID_BIND_BY_WWNN
:
2759 if (rport
->node_name
== ids
->node_name
)
2762 case FC_TGTID_BIND_BY_ID
:
2763 if (rport
->port_id
== ids
->port_id
)
2766 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
2771 list_move_tail(&rport
->peers
, &fc_host
->rports
);
2777 memcpy(&rport
->node_name
, &ids
->node_name
,
2778 sizeof(rport
->node_name
));
2779 memcpy(&rport
->port_name
, &ids
->port_name
,
2780 sizeof(rport
->port_name
));
2781 rport
->port_id
= ids
->port_id
;
2782 rport
->roles
= ids
->roles
;
2783 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2784 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
2786 if (fci
->f
->dd_fcrport_size
)
2787 memset(rport
->dd_data
, 0,
2788 fci
->f
->dd_fcrport_size
);
2790 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2791 /* initiate a scan of the target */
2792 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2793 scsi_queue_work(shost
, &rport
->scan_work
);
2794 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2795 scsi_target_unblock(&rport
->dev
);
2797 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2803 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2805 /* No consistent binding found - create new remote port entry */
2806 rport
= fc_rport_create(shost
, channel
, ids
);
2810 EXPORT_SYMBOL(fc_remote_port_add
);
2814 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2815 * @rport: The remote port that no longer exists
2817 * The LLDD calls this routine to notify the transport that a remote
2818 * port is no longer part of the topology. Note: Although a port
2819 * may no longer be part of the topology, it may persist in the remote
2820 * ports displayed by the fc_host. We do this under 2 conditions:
2821 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2822 * This allows the port to temporarily disappear, then reappear without
2823 * disrupting the SCSI device tree attached to it. During the "blocked"
2824 * period the port will still exist.
2825 * 2) If the port was a scsi target and disappears for longer than we
2826 * expect, we'll delete the port and the tear down the SCSI device tree
2827 * attached to it. However, we want to semi-persist the target id assigned
2828 * to that port if it eventually does exist. The port structure will
2829 * remain (although with minimal information) so that the target id
2832 * If the remote port is not an FCP Target, it will be fully torn down
2833 * and deallocated, including the fc_remote_port class device.
2835 * If the remote port is an FCP Target, the port will be placed in a
2836 * temporary blocked state. From the LLDD's perspective, the rport no
2837 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2838 * exists, but all sdevs on it are blocked from further I/O. The following
2841 * If the remote port does not return (signaled by a LLDD call to
2842 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2843 * scsi target is removed - killing all outstanding i/o and removing the
2844 * scsi devices attached ot it. The port structure will be marked Not
2845 * Present and be partially cleared, leaving only enough information to
2846 * recognize the remote port relative to the scsi target id binding if
2847 * it later appears. The port will remain as long as there is a valid
2848 * binding (e.g. until the user changes the binding type or unloads the
2849 * scsi host with the binding).
2851 * If the remote port returns within the dev_loss_tmo value (and matches
2852 * according to the target id binding type), the port structure will be
2853 * reused. If it is no longer a SCSI target, the target will be torn
2854 * down. If it continues to be a SCSI target, then the target will be
2855 * unblocked (allowing i/o to be resumed), and a scan will be activated
2856 * to ensure that all luns are detected.
2858 * Called from normal process context only - cannot be called from interrupt.
2861 * This routine assumes no locks are held on entry.
2864 fc_remote_port_delete(struct fc_rport
*rport
)
2866 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2867 int timeout
= rport
->dev_loss_tmo
;
2868 unsigned long flags
;
2871 * No need to flush the fc_host work_q's, as all adds are synchronous.
2873 * We do need to reclaim the rport scan work element, so eventually
2874 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2875 * there's still a scan pending.
2878 spin_lock_irqsave(shost
->host_lock
, flags
);
2880 if (rport
->port_state
!= FC_PORTSTATE_ONLINE
) {
2881 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2886 * In the past, we if this was not an FCP-Target, we would
2887 * unconditionally just jump to deleting the rport.
2888 * However, rports can be used as node containers by the LLDD,
2889 * and its not appropriate to just terminate the rport at the
2890 * first sign of a loss in connectivity. The LLDD may want to
2891 * send ELS traffic to re-validate the login. If the rport is
2892 * immediately deleted, it makes it inappropriate for a node
2894 * So... we now unconditionally wait dev_loss_tmo before
2895 * destroying an rport.
2898 rport
->port_state
= FC_PORTSTATE_BLOCKED
;
2900 rport
->flags
|= FC_RPORT_DEVLOSS_PENDING
;
2902 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2904 if (rport
->roles
& FC_PORT_ROLE_FCP_INITIATOR
&&
2905 shost
->active_mode
& MODE_TARGET
)
2906 fc_tgt_it_nexus_destroy(shost
, (unsigned long)rport
);
2908 scsi_target_block(&rport
->dev
);
2910 /* see if we need to kill io faster than waiting for device loss */
2911 if ((rport
->fast_io_fail_tmo
!= -1) &&
2912 (rport
->fast_io_fail_tmo
< timeout
))
2913 fc_queue_devloss_work(shost
, &rport
->fail_io_work
,
2914 rport
->fast_io_fail_tmo
* HZ
);
2916 /* cap the length the devices can be blocked until they are deleted */
2917 fc_queue_devloss_work(shost
, &rport
->dev_loss_work
, timeout
* HZ
);
2919 EXPORT_SYMBOL(fc_remote_port_delete
);
2922 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2923 * @rport: The remote port that changed.
2924 * @roles: New roles for this port.
2926 * Description: The LLDD calls this routine to notify the transport that the
2927 * roles on a remote port may have changed. The largest effect of this is
2928 * if a port now becomes a FCP Target, it must be allocated a
2929 * scsi target id. If the port is no longer a FCP target, any
2930 * scsi target id value assigned to it will persist in case the
2931 * role changes back to include FCP Target. No changes in the scsi
2932 * midlayer will be invoked if the role changes (in the expectation
2933 * that the role will be resumed. If it doesn't normal error processing
2936 * Should not be called from interrupt context.
2939 * This routine assumes no locks are held on entry.
2942 fc_remote_port_rolechg(struct fc_rport
*rport
, u32 roles
)
2944 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2945 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2946 unsigned long flags
;
2950 spin_lock_irqsave(shost
->host_lock
, flags
);
2951 if (roles
& FC_PORT_ROLE_FCP_TARGET
) {
2952 if (rport
->scsi_target_id
== -1) {
2953 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2955 } else if (!(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
))
2957 } else if (shost
->active_mode
& MODE_TARGET
) {
2958 ret
= fc_tgt_it_nexus_create(shost
, (unsigned long)rport
,
2959 (char *)&rport
->node_name
);
2961 printk(KERN_ERR
"FC Remore Port tgt nexus failed %d\n",
2965 rport
->roles
= roles
;
2967 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2971 * There may have been a delete timer running on the
2972 * port. Ensure that it is cancelled as we now know
2973 * the port is an FCP Target.
2974 * Note: we know the rport is exists and in an online
2975 * state as the LLDD would not have had an rport
2976 * reference to pass us.
2978 * Take no action on the del_timer failure as the state
2979 * machine state change will validate the
2982 if (!cancel_delayed_work(&rport
->fail_io_work
))
2983 fc_flush_devloss(shost
);
2984 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2985 fc_flush_devloss(shost
);
2987 spin_lock_irqsave(shost
->host_lock
, flags
);
2988 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2989 FC_RPORT_DEVLOSS_PENDING
);
2990 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2992 /* ensure any stgt delete functions are done */
2993 fc_flush_work(shost
);
2995 /* initiate a scan of the target */
2996 spin_lock_irqsave(shost
->host_lock
, flags
);
2997 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2998 scsi_queue_work(shost
, &rport
->scan_work
);
2999 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3000 scsi_target_unblock(&rport
->dev
);
3003 EXPORT_SYMBOL(fc_remote_port_rolechg
);
3006 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3007 * @work: rport target that failed to reappear in the allotted time.
3009 * Description: An attempt to delete a remote port blocks, and if it fails
3010 * to return in the allotted time this gets called.
3013 fc_timeout_deleted_rport(struct work_struct
*work
)
3015 struct fc_rport
*rport
=
3016 container_of(work
, struct fc_rport
, dev_loss_work
.work
);
3017 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3018 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3019 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3020 unsigned long flags
;
3021 int do_callback
= 0;
3023 spin_lock_irqsave(shost
->host_lock
, flags
);
3025 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
3028 * If the port is ONLINE, then it came back. If it was a SCSI
3029 * target, validate it still is. If not, tear down the
3030 * scsi_target on it.
3032 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
3033 (rport
->scsi_target_id
!= -1) &&
3034 !(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)) {
3035 dev_printk(KERN_ERR
, &rport
->dev
,
3036 "blocked FC remote port time out: no longer"
3037 " a FCP target, removing starget\n");
3038 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3039 scsi_target_unblock(&rport
->dev
);
3040 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3044 /* NOOP state - we're flushing workq's */
3045 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
) {
3046 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3047 dev_printk(KERN_ERR
, &rport
->dev
,
3048 "blocked FC remote port time out: leaving"
3050 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3054 if ((fc_host
->tgtid_bind_type
== FC_TGTID_BIND_NONE
) ||
3055 (rport
->scsi_target_id
== -1)) {
3056 list_del(&rport
->peers
);
3057 rport
->port_state
= FC_PORTSTATE_DELETED
;
3058 dev_printk(KERN_ERR
, &rport
->dev
,
3059 "blocked FC remote port time out: removing"
3061 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3062 fc_queue_work(shost
, &rport
->rport_delete_work
);
3063 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3067 dev_printk(KERN_ERR
, &rport
->dev
,
3068 "blocked FC remote port time out: removing target and "
3069 "saving binding\n");
3071 list_move_tail(&rport
->peers
, &fc_host
->rport_bindings
);
3074 * Note: We do not remove or clear the hostdata area. This allows
3075 * host-specific target data to persist along with the
3076 * scsi_target_id. It's up to the host to manage it's hostdata area.
3080 * Reinitialize port attributes that may change if the port comes back.
3082 rport
->maxframe_size
= -1;
3083 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
3084 rport
->roles
= FC_PORT_ROLE_UNKNOWN
;
3085 rport
->port_state
= FC_PORTSTATE_NOTPRESENT
;
3086 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
3089 * Pre-emptively kill I/O rather than waiting for the work queue
3090 * item to teardown the starget. (FCOE libFC folks prefer this
3091 * and to have the rport_port_id still set when it's done).
3093 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3094 fc_terminate_rport_io(rport
);
3096 spin_lock_irqsave(shost
->host_lock
, flags
);
3098 if (rport
->port_state
== FC_PORTSTATE_NOTPRESENT
) { /* still missing */
3100 /* remove the identifiers that aren't used in the consisting binding */
3101 switch (fc_host
->tgtid_bind_type
) {
3102 case FC_TGTID_BIND_BY_WWPN
:
3103 rport
->node_name
= -1;
3104 rport
->port_id
= -1;
3106 case FC_TGTID_BIND_BY_WWNN
:
3107 rport
->port_name
= -1;
3108 rport
->port_id
= -1;
3110 case FC_TGTID_BIND_BY_ID
:
3111 rport
->node_name
= -1;
3112 rport
->port_name
= -1;
3114 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
3119 * As this only occurs if the remote port (scsi target)
3120 * went away and didn't come back - we'll remove
3121 * all attached scsi devices.
3123 rport
->flags
|= FC_RPORT_DEVLOSS_CALLBK_DONE
;
3124 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3129 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3132 * Notify the driver that the rport is now dead. The LLDD will
3133 * also guarantee that any communication to the rport is terminated
3135 * Note: we set the CALLBK_DONE flag above to correspond
3137 if (do_callback
&& i
->f
->dev_loss_tmo_callbk
)
3138 i
->f
->dev_loss_tmo_callbk(rport
);
3143 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3144 * @work: rport to terminate io on.
3146 * Notes: Only requests the failure of the io, not that all are flushed
3147 * prior to returning.
3150 fc_timeout_fail_rport_io(struct work_struct
*work
)
3152 struct fc_rport
*rport
=
3153 container_of(work
, struct fc_rport
, fail_io_work
.work
);
3155 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
)
3158 rport
->flags
|= FC_RPORT_FAST_FAIL_TIMEDOUT
;
3159 fc_terminate_rport_io(rport
);
3163 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3164 * @work: remote port to be scanned.
3167 fc_scsi_scan_rport(struct work_struct
*work
)
3169 struct fc_rport
*rport
=
3170 container_of(work
, struct fc_rport
, scan_work
);
3171 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3172 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3173 unsigned long flags
;
3175 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
3176 (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) &&
3177 !(i
->f
->disable_target_scan
)) {
3178 scsi_scan_target(&rport
->dev
, rport
->channel
,
3179 rport
->scsi_target_id
, SCAN_WILD_CARD
, 1);
3182 spin_lock_irqsave(shost
->host_lock
, flags
);
3183 rport
->flags
&= ~FC_RPORT_SCAN_PENDING
;
3184 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3188 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3189 * @cmnd: SCSI command that scsi_eh is trying to recover
3191 * This routine can be called from a FC LLD scsi_eh callback. It
3192 * blocks the scsi_eh thread until the fc_rport leaves the
3193 * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
3194 * failing recovery actions for blocked rports which would lead to
3195 * offlined SCSI devices.
3197 void fc_block_scsi_eh(struct scsi_cmnd
*cmnd
)
3199 struct Scsi_Host
*shost
= cmnd
->device
->host
;
3200 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
3201 unsigned long flags
;
3203 spin_lock_irqsave(shost
->host_lock
, flags
);
3204 while (rport
->port_state
== FC_PORTSTATE_BLOCKED
) {
3205 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3207 spin_lock_irqsave(shost
->host_lock
, flags
);
3209 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3211 EXPORT_SYMBOL(fc_block_scsi_eh
);
3214 * fc_vport_setup - allocates and creates a FC virtual port.
3215 * @shost: scsi host the virtual port is connected to.
3216 * @channel: Channel on shost port connected to.
3217 * @pdev: parent device for vport
3218 * @ids: The world wide names, FC4 port roles, etc for
3220 * @ret_vport: The pointer to the created vport.
3222 * Allocates and creates the vport structure, calls the parent host
3223 * to instantiate the vport, the completes w/ class and sysfs creation.
3226 * This routine assumes no locks are held on entry.
3229 fc_vport_setup(struct Scsi_Host
*shost
, int channel
, struct device
*pdev
,
3230 struct fc_vport_identifiers
*ids
, struct fc_vport
**ret_vport
)
3232 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3233 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
3234 struct fc_vport
*vport
;
3236 unsigned long flags
;
3242 if ( ! fci
->f
->vport_create
)
3245 size
= (sizeof(struct fc_vport
) + fci
->f
->dd_fcvport_size
);
3246 vport
= kzalloc(size
, GFP_KERNEL
);
3247 if (unlikely(!vport
)) {
3248 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
3252 vport
->vport_state
= FC_VPORT_UNKNOWN
;
3253 vport
->vport_last_state
= FC_VPORT_UNKNOWN
;
3254 vport
->node_name
= ids
->node_name
;
3255 vport
->port_name
= ids
->port_name
;
3256 vport
->roles
= ids
->roles
;
3257 vport
->vport_type
= ids
->vport_type
;
3258 if (fci
->f
->dd_fcvport_size
)
3259 vport
->dd_data
= &vport
[1];
3260 vport
->shost
= shost
;
3261 vport
->channel
= channel
;
3262 vport
->flags
= FC_VPORT_CREATING
;
3263 INIT_WORK(&vport
->vport_delete_work
, fc_vport_sched_delete
);
3265 spin_lock_irqsave(shost
->host_lock
, flags
);
3267 if (fc_host
->npiv_vports_inuse
>= fc_host
->max_npiv_vports
) {
3268 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3272 fc_host
->npiv_vports_inuse
++;
3273 vport
->number
= fc_host
->next_vport_number
++;
3274 list_add_tail(&vport
->peers
, &fc_host
->vports
);
3275 get_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3277 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3280 device_initialize(dev
); /* takes self reference */
3281 dev
->parent
= get_device(pdev
); /* takes parent reference */
3282 dev
->release
= fc_vport_dev_release
;
3283 dev_set_name(dev
, "vport-%d:%d-%d",
3284 shost
->host_no
, channel
, vport
->number
);
3285 transport_setup_device(dev
);
3287 error
= device_add(dev
);
3289 printk(KERN_ERR
"FC Virtual Port device_add failed\n");
3292 transport_add_device(dev
);
3293 transport_configure_device(dev
);
3295 error
= fci
->f
->vport_create(vport
, ids
->disable
);
3297 printk(KERN_ERR
"FC Virtual Port LLDD Create failed\n");
3298 goto delete_vport_all
;
3302 * if the parent isn't the physical adapter's Scsi_Host, ensure
3303 * the Scsi_Host at least contains ia symlink to the vport.
3305 if (pdev
!= &shost
->shost_gendev
) {
3306 error
= sysfs_create_link(&shost
->shost_gendev
.kobj
,
3307 &dev
->kobj
, dev_name(dev
));
3310 "%s: Cannot create vport symlinks for "
3312 __func__
, dev_name(dev
), error
);
3314 spin_lock_irqsave(shost
->host_lock
, flags
);
3315 vport
->flags
&= ~FC_VPORT_CREATING
;
3316 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3318 dev_printk(KERN_NOTICE
, pdev
,
3319 "%s created via shost%d channel %d\n", dev_name(dev
),
3320 shost
->host_no
, channel
);
3327 transport_remove_device(dev
);
3330 transport_destroy_device(dev
);
3331 spin_lock_irqsave(shost
->host_lock
, flags
);
3332 list_del(&vport
->peers
);
3333 put_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3334 fc_host
->npiv_vports_inuse
--;
3335 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3336 put_device(dev
->parent
);
3343 * fc_vport_create - Admin App or LLDD requests creation of a vport
3344 * @shost: scsi host the virtual port is connected to.
3345 * @channel: channel on shost port connected to.
3346 * @ids: The world wide names, FC4 port roles, etc for
3350 * This routine assumes no locks are held on entry.
3353 fc_vport_create(struct Scsi_Host
*shost
, int channel
,
3354 struct fc_vport_identifiers
*ids
)
3357 struct fc_vport
*vport
;
3359 stat
= fc_vport_setup(shost
, channel
, &shost
->shost_gendev
,
3361 return stat
? NULL
: vport
;
3363 EXPORT_SYMBOL(fc_vport_create
);
3366 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3367 * @vport: fc_vport to be terminated
3369 * Calls the LLDD vport_delete() function, then deallocates and removes
3370 * the vport from the shost and object tree.
3373 * This routine assumes no locks are held on entry.
3376 fc_vport_terminate(struct fc_vport
*vport
)
3378 struct Scsi_Host
*shost
= vport_to_shost(vport
);
3379 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3380 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3381 struct device
*dev
= &vport
->dev
;
3382 unsigned long flags
;
3385 if (i
->f
->vport_delete
)
3386 stat
= i
->f
->vport_delete(vport
);
3390 spin_lock_irqsave(shost
->host_lock
, flags
);
3391 vport
->flags
&= ~FC_VPORT_DELETING
;
3393 vport
->flags
|= FC_VPORT_DELETED
;
3394 list_del(&vport
->peers
);
3395 fc_host
->npiv_vports_inuse
--;
3396 put_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3398 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3403 if (dev
->parent
!= &shost
->shost_gendev
)
3404 sysfs_remove_link(&shost
->shost_gendev
.kobj
, dev_name(dev
));
3405 transport_remove_device(dev
);
3407 transport_destroy_device(dev
);
3410 * Removing our self-reference should mean our
3411 * release function gets called, which will drop the remaining
3412 * parent reference and free the data structure.
3414 put_device(dev
); /* for self-reference */
3416 return 0; /* SUCCESS */
3418 EXPORT_SYMBOL(fc_vport_terminate
);
3421 * fc_vport_sched_delete - workq-based delete request for a vport
3422 * @work: vport to be deleted.
3425 fc_vport_sched_delete(struct work_struct
*work
)
3427 struct fc_vport
*vport
=
3428 container_of(work
, struct fc_vport
, vport_delete_work
);
3431 stat
= fc_vport_terminate(vport
);
3433 dev_printk(KERN_ERR
, vport
->dev
.parent
,
3434 "%s: %s could not be deleted created via "
3435 "shost%d channel %d - error %d\n", __func__
,
3436 dev_name(&vport
->dev
), vport
->shost
->host_no
,
3437 vport
->channel
, stat
);
3447 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3448 * @job: fc_bsg_job that is to be torn down
3451 fc_destroy_bsgjob(struct fc_bsg_job
*job
)
3453 unsigned long flags
;
3455 spin_lock_irqsave(&job
->job_lock
, flags
);
3457 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3460 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3462 put_device(job
->dev
); /* release reference for the request */
3464 kfree(job
->request_payload
.sg_list
);
3465 kfree(job
->reply_payload
.sg_list
);
3470 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3472 * @job: fc_bsg_job that is complete
3475 fc_bsg_jobdone(struct fc_bsg_job
*job
)
3477 struct request
*req
= job
->req
;
3478 struct request
*rsp
= req
->next_rq
;
3481 err
= job
->req
->errors
= job
->reply
->result
;
3484 /* we're only returning the result field in the reply */
3485 job
->req
->sense_len
= sizeof(uint32_t);
3487 job
->req
->sense_len
= job
->reply_len
;
3489 /* we assume all request payload was transferred, residual == 0 */
3493 WARN_ON(job
->reply
->reply_payload_rcv_len
> rsp
->resid_len
);
3495 /* set reply (bidi) residual */
3496 rsp
->resid_len
-= min(job
->reply
->reply_payload_rcv_len
,
3499 blk_complete_request(req
);
3503 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3504 * @rq: BSG request that holds the job to be destroyed
3506 static void fc_bsg_softirq_done(struct request
*rq
)
3508 struct fc_bsg_job
*job
= rq
->special
;
3509 unsigned long flags
;
3511 spin_lock_irqsave(&job
->job_lock
, flags
);
3512 job
->state_flags
|= FC_RQST_STATE_DONE
;
3514 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3516 blk_end_request_all(rq
, rq
->errors
);
3517 fc_destroy_bsgjob(job
);
3521 * fc_bsg_job_timeout - handler for when a bsg request timesout
3522 * @req: request that timed out
3524 static enum blk_eh_timer_return
3525 fc_bsg_job_timeout(struct request
*req
)
3527 struct fc_bsg_job
*job
= (void *) req
->special
;
3528 struct Scsi_Host
*shost
= job
->shost
;
3529 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3530 unsigned long flags
;
3531 int err
= 0, done
= 0;
3533 if (job
->rport
&& job
->rport
->port_state
== FC_PORTSTATE_BLOCKED
)
3534 return BLK_EH_RESET_TIMER
;
3536 spin_lock_irqsave(&job
->job_lock
, flags
);
3537 if (job
->state_flags
& FC_RQST_STATE_DONE
)
3541 spin_unlock_irqrestore(&job
->job_lock
, flags
);
3543 if (!done
&& i
->f
->bsg_timeout
) {
3544 /* call LLDD to abort the i/o as it has timed out */
3545 err
= i
->f
->bsg_timeout(job
);
3546 if (err
== -EAGAIN
) {
3548 return BLK_EH_RESET_TIMER
;
3550 printk(KERN_ERR
"ERROR: FC BSG request timeout - LLD "
3551 "abort failed with status %d\n", err
);
3554 /* the blk_end_sync_io() doesn't check the error */
3556 return BLK_EH_NOT_HANDLED
;
3558 return BLK_EH_HANDLED
;
3562 fc_bsg_map_buffer(struct fc_bsg_buffer
*buf
, struct request
*req
)
3564 size_t sz
= (sizeof(struct scatterlist
) * req
->nr_phys_segments
);
3566 BUG_ON(!req
->nr_phys_segments
);
3568 buf
->sg_list
= kzalloc(sz
, GFP_KERNEL
);
3571 sg_init_table(buf
->sg_list
, req
->nr_phys_segments
);
3572 buf
->sg_cnt
= blk_rq_map_sg(req
->q
, req
, buf
->sg_list
);
3573 buf
->payload_len
= blk_rq_bytes(req
);
3579 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3581 * @shost: SCSI Host corresponding to the bsg object
3582 * @rport: (optional) FC Remote Port corresponding to the bsg object
3583 * @req: BSG request that needs a job structure
3586 fc_req_to_bsgjob(struct Scsi_Host
*shost
, struct fc_rport
*rport
,
3587 struct request
*req
)
3589 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3590 struct request
*rsp
= req
->next_rq
;
3591 struct fc_bsg_job
*job
;
3594 BUG_ON(req
->special
);
3596 job
= kzalloc(sizeof(struct fc_bsg_job
) + i
->f
->dd_bsg_size
,
3602 * Note: this is a bit silly.
3603 * The request gets formatted as a SGIO v4 ioctl request, which
3604 * then gets reformatted as a blk request, which then gets
3605 * reformatted as a fc bsg request. And on completion, we have
3606 * to wrap return results such that SGIO v4 thinks it was a scsi
3607 * status. I hope this was all worth it.
3614 if (i
->f
->dd_bsg_size
)
3615 job
->dd_data
= (void *)&job
[1];
3616 spin_lock_init(&job
->job_lock
);
3617 job
->request
= (struct fc_bsg_request
*)req
->cmd
;
3618 job
->request_len
= req
->cmd_len
;
3619 job
->reply
= req
->sense
;
3620 job
->reply_len
= SCSI_SENSE_BUFFERSIZE
; /* Size of sense buffer
3623 ret
= fc_bsg_map_buffer(&job
->request_payload
, req
);
3625 goto failjob_rls_job
;
3627 if (rsp
&& rsp
->bio
) {
3628 ret
= fc_bsg_map_buffer(&job
->reply_payload
, rsp
);
3630 goto failjob_rls_rqst_payload
;
3632 job
->job_done
= fc_bsg_jobdone
;
3634 job
->dev
= &rport
->dev
;
3636 job
->dev
= &shost
->shost_gendev
;
3637 get_device(job
->dev
); /* take a reference for the request */
3644 failjob_rls_rqst_payload
:
3645 kfree(job
->request_payload
.sg_list
);
3652 enum fc_dispatch_result
{
3653 FC_DISPATCH_BREAK
, /* on return, q is locked, break from q loop */
3654 FC_DISPATCH_LOCKED
, /* on return, q is locked, continue on */
3655 FC_DISPATCH_UNLOCKED
, /* on return, q is unlocked, continue on */
3660 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3661 * @q: fc host request queue
3662 * @shost: scsi host rport attached to
3663 * @job: bsg job to be processed
3665 static enum fc_dispatch_result
3666 fc_bsg_host_dispatch(struct request_queue
*q
, struct Scsi_Host
*shost
,
3667 struct fc_bsg_job
*job
)
3669 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3670 int cmdlen
= sizeof(uint32_t); /* start with length of msgcode */
3673 /* Validate the host command */
3674 switch (job
->request
->msgcode
) {
3675 case FC_BSG_HST_ADD_RPORT
:
3676 cmdlen
+= sizeof(struct fc_bsg_host_add_rport
);
3679 case FC_BSG_HST_DEL_RPORT
:
3680 cmdlen
+= sizeof(struct fc_bsg_host_del_rport
);
3683 case FC_BSG_HST_ELS_NOLOGIN
:
3684 cmdlen
+= sizeof(struct fc_bsg_host_els
);
3685 /* there better be a xmt and rcv payloads */
3686 if ((!job
->request_payload
.payload_len
) ||
3687 (!job
->reply_payload
.payload_len
)) {
3694 cmdlen
+= sizeof(struct fc_bsg_host_ct
);
3695 /* there better be xmt and rcv payloads */
3696 if ((!job
->request_payload
.payload_len
) ||
3697 (!job
->reply_payload
.payload_len
)) {
3703 case FC_BSG_HST_VENDOR
:
3704 cmdlen
+= sizeof(struct fc_bsg_host_vendor
);
3705 if ((shost
->hostt
->vendor_id
== 0L) ||
3706 (job
->request
->rqst_data
.h_vendor
.vendor_id
!=
3707 shost
->hostt
->vendor_id
)) {
3718 /* check if we really have all the request data needed */
3719 if (job
->request_len
< cmdlen
) {
3724 ret
= i
->f
->bsg_request(job
);
3726 return FC_DISPATCH_UNLOCKED
;
3729 /* return the errno failure code as the only status */
3730 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3731 job
->reply
->reply_payload_rcv_len
= 0;
3732 job
->reply
->result
= ret
;
3733 job
->reply_len
= sizeof(uint32_t);
3734 fc_bsg_jobdone(job
);
3735 return FC_DISPATCH_UNLOCKED
;
3740 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3741 * @rport: rport to be restarted
3744 fc_bsg_goose_queue(struct fc_rport
*rport
)
3747 unsigned long flags
;
3752 get_device(&rport
->dev
);
3754 spin_lock_irqsave(rport
->rqst_q
->queue_lock
, flags
);
3755 flagset
= test_bit(QUEUE_FLAG_REENTER
, &rport
->rqst_q
->queue_flags
) &&
3756 !test_bit(QUEUE_FLAG_REENTER
, &rport
->rqst_q
->queue_flags
);
3758 queue_flag_set(QUEUE_FLAG_REENTER
, rport
->rqst_q
);
3759 __blk_run_queue(rport
->rqst_q
);
3761 queue_flag_clear(QUEUE_FLAG_REENTER
, rport
->rqst_q
);
3762 spin_unlock_irqrestore(rport
->rqst_q
->queue_lock
, flags
);
3764 put_device(&rport
->dev
);
3769 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3770 * @q: rport request queue
3771 * @shost: scsi host rport attached to
3772 * @rport: rport request destined to
3773 * @job: bsg job to be processed
3775 static enum fc_dispatch_result
3776 fc_bsg_rport_dispatch(struct request_queue
*q
, struct Scsi_Host
*shost
,
3777 struct fc_rport
*rport
, struct fc_bsg_job
*job
)
3779 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3780 int cmdlen
= sizeof(uint32_t); /* start with length of msgcode */
3783 /* Validate the rport command */
3784 switch (job
->request
->msgcode
) {
3785 case FC_BSG_RPT_ELS
:
3786 cmdlen
+= sizeof(struct fc_bsg_rport_els
);
3790 cmdlen
+= sizeof(struct fc_bsg_rport_ct
);
3792 /* there better be xmt and rcv payloads */
3793 if ((!job
->request_payload
.payload_len
) ||
3794 (!job
->reply_payload
.payload_len
)) {
3796 goto fail_rport_msg
;
3801 goto fail_rport_msg
;
3804 /* check if we really have all the request data needed */
3805 if (job
->request_len
< cmdlen
) {
3807 goto fail_rport_msg
;
3810 ret
= i
->f
->bsg_request(job
);
3812 return FC_DISPATCH_UNLOCKED
;
3815 /* return the errno failure code as the only status */
3816 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3817 job
->reply
->reply_payload_rcv_len
= 0;
3818 job
->reply
->result
= ret
;
3819 job
->reply_len
= sizeof(uint32_t);
3820 fc_bsg_jobdone(job
);
3821 return FC_DISPATCH_UNLOCKED
;
3826 * fc_bsg_request_handler - generic handler for bsg requests
3827 * @q: request queue to manage
3828 * @shost: Scsi_Host related to the bsg object
3829 * @rport: FC remote port related to the bsg object (optional)
3830 * @dev: device structure for bsg object
3833 fc_bsg_request_handler(struct request_queue
*q
, struct Scsi_Host
*shost
,
3834 struct fc_rport
*rport
, struct device
*dev
)
3836 struct request
*req
;
3837 struct fc_bsg_job
*job
;
3838 enum fc_dispatch_result ret
;
3840 if (!get_device(dev
))
3843 while (!blk_queue_plugged(q
)) {
3844 if (rport
&& (rport
->port_state
== FC_PORTSTATE_BLOCKED
) &&
3845 !(rport
->flags
& FC_RPORT_FAST_FAIL_TIMEDOUT
))
3848 req
= blk_fetch_request(q
);
3852 if (rport
&& (rport
->port_state
!= FC_PORTSTATE_ONLINE
)) {
3853 req
->errors
= -ENXIO
;
3854 spin_unlock_irq(q
->queue_lock
);
3855 blk_end_request(req
, -ENXIO
, blk_rq_bytes(req
));
3856 spin_lock_irq(q
->queue_lock
);
3860 spin_unlock_irq(q
->queue_lock
);
3862 ret
= fc_req_to_bsgjob(shost
, rport
, req
);
3865 blk_end_request(req
, ret
, blk_rq_bytes(req
));
3866 spin_lock_irq(q
->queue_lock
);
3872 /* check if we have the msgcode value at least */
3873 if (job
->request_len
< sizeof(uint32_t)) {
3874 BUG_ON(job
->reply_len
< sizeof(uint32_t));
3875 job
->reply
->reply_payload_rcv_len
= 0;
3876 job
->reply
->result
= -ENOMSG
;
3877 job
->reply_len
= sizeof(uint32_t);
3878 fc_bsg_jobdone(job
);
3879 spin_lock_irq(q
->queue_lock
);
3883 /* the dispatch routines will unlock the queue_lock */
3885 ret
= fc_bsg_rport_dispatch(q
, shost
, rport
, job
);
3887 ret
= fc_bsg_host_dispatch(q
, shost
, job
);
3889 /* did dispatcher hit state that can't process any more */
3890 if (ret
== FC_DISPATCH_BREAK
)
3893 /* did dispatcher had released the lock */
3894 if (ret
== FC_DISPATCH_UNLOCKED
)
3895 spin_lock_irq(q
->queue_lock
);
3898 spin_unlock_irq(q
->queue_lock
);
3900 spin_lock_irq(q
->queue_lock
);
3905 * fc_bsg_host_handler - handler for bsg requests for a fc host
3906 * @q: fc host request queue
3909 fc_bsg_host_handler(struct request_queue
*q
)
3911 struct Scsi_Host
*shost
= q
->queuedata
;
3913 fc_bsg_request_handler(q
, shost
, NULL
, &shost
->shost_gendev
);
3918 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3919 * @q: rport request queue
3922 fc_bsg_rport_handler(struct request_queue
*q
)
3924 struct fc_rport
*rport
= q
->queuedata
;
3925 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3927 fc_bsg_request_handler(q
, shost
, rport
, &rport
->dev
);
3932 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3933 * @shost: shost for fc_host
3934 * @fc_host: fc_host adding the structures to
3937 fc_bsg_hostadd(struct Scsi_Host
*shost
, struct fc_host_attrs
*fc_host
)
3939 struct device
*dev
= &shost
->shost_gendev
;
3940 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3941 struct request_queue
*q
;
3945 fc_host
->rqst_q
= NULL
;
3947 if (!i
->f
->bsg_request
)
3950 snprintf(bsg_name
, sizeof(bsg_name
),
3951 "fc_host%d", shost
->host_no
);
3953 q
= __scsi_alloc_queue(shost
, fc_bsg_host_handler
);
3955 printk(KERN_ERR
"fc_host%d: bsg interface failed to "
3956 "initialize - no request queue\n",
3961 q
->queuedata
= shost
;
3962 queue_flag_set_unlocked(QUEUE_FLAG_BIDI
, q
);
3963 blk_queue_softirq_done(q
, fc_bsg_softirq_done
);
3964 blk_queue_rq_timed_out(q
, fc_bsg_job_timeout
);
3965 blk_queue_rq_timeout(q
, FC_DEFAULT_BSG_TIMEOUT
);
3967 err
= bsg_register_queue(q
, dev
, bsg_name
, NULL
);
3969 printk(KERN_ERR
"fc_host%d: bsg interface failed to "
3970 "initialize - register queue\n",
3972 blk_cleanup_queue(q
);
3976 fc_host
->rqst_q
= q
;
3982 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3983 * @shost: shost that rport is attached to
3984 * @rport: rport that the bsg hooks are being attached to
3987 fc_bsg_rportadd(struct Scsi_Host
*shost
, struct fc_rport
*rport
)
3989 struct device
*dev
= &rport
->dev
;
3990 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3991 struct request_queue
*q
;
3994 rport
->rqst_q
= NULL
;
3996 if (!i
->f
->bsg_request
)
3999 q
= __scsi_alloc_queue(shost
, fc_bsg_rport_handler
);
4001 printk(KERN_ERR
"%s: bsg interface failed to "
4002 "initialize - no request queue\n",
4007 q
->queuedata
= rport
;
4008 queue_flag_set_unlocked(QUEUE_FLAG_BIDI
, q
);
4009 blk_queue_softirq_done(q
, fc_bsg_softirq_done
);
4010 blk_queue_rq_timed_out(q
, fc_bsg_job_timeout
);
4011 blk_queue_rq_timeout(q
, BLK_DEFAULT_SG_TIMEOUT
);
4013 err
= bsg_register_queue(q
, dev
, NULL
, NULL
);
4015 printk(KERN_ERR
"%s: bsg interface failed to "
4016 "initialize - register queue\n",
4018 blk_cleanup_queue(q
);
4028 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4029 * @q: the request_queue that is to be torn down.
4032 fc_bsg_remove(struct request_queue
*q
)
4035 bsg_unregister_queue(q
);
4036 blk_cleanup_queue(q
);
4041 /* Original Author: Martin Hicks */
4042 MODULE_AUTHOR("James Smart");
4043 MODULE_DESCRIPTION("FC Transport Attributes");
4044 MODULE_LICENSE("GPL");
4046 module_init(fc_transport_init
);
4047 module_exit(fc_transport_exit
);