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
);
1236 fc_queue_work(shost
, &vport
->vport_delete_work
);
1239 static FC_DEVICE_ATTR(vport
, vport_delete
, S_IWUSR
,
1240 NULL
, store_fc_vport_delete
);
1244 * Enable/Disable vport
1245 * Write "1" to disable, write "0" to enable
1248 store_fc_vport_disable(struct device
*dev
, struct device_attribute
*attr
,
1252 struct fc_vport
*vport
= transport_class_to_vport(dev
);
1253 struct Scsi_Host
*shost
= vport_to_shost(vport
);
1254 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1257 if (vport
->flags
& (FC_VPORT_DEL
| FC_VPORT_CREATING
))
1261 if (vport
->vport_state
!= FC_VPORT_DISABLED
)
1263 } else if (*buf
== '1') {
1264 if (vport
->vport_state
== FC_VPORT_DISABLED
)
1269 stat
= i
->f
->vport_disable(vport
, ((*buf
== '0') ? false : true));
1270 return stat
? stat
: count
;
1272 static FC_DEVICE_ATTR(vport
, vport_disable
, S_IWUSR
,
1273 NULL
, store_fc_vport_disable
);
1277 * Host Attribute Management
1280 #define fc_host_show_function(field, format_string, sz, cast) \
1282 show_fc_host_##field (struct device *dev, \
1283 struct device_attribute *attr, char *buf) \
1285 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1286 struct fc_internal *i = to_fc_internal(shost->transportt); \
1287 if (i->f->get_host_##field) \
1288 i->f->get_host_##field(shost); \
1289 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1292 #define fc_host_store_function(field) \
1294 store_fc_host_##field(struct device *dev, \
1295 struct device_attribute *attr, \
1296 const char *buf, size_t count) \
1299 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1300 struct fc_internal *i = to_fc_internal(shost->transportt); \
1303 val = simple_strtoul(buf, &cp, 0); \
1304 if (*cp && (*cp != '\n')) \
1306 i->f->set_host_##field(shost, val); \
1310 #define fc_host_store_str_function(field, slen) \
1312 store_fc_host_##field(struct device *dev, \
1313 struct device_attribute *attr, \
1314 const char *buf, size_t count) \
1316 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1317 struct fc_internal *i = to_fc_internal(shost->transportt); \
1318 unsigned int cnt=count; \
1320 /* count may include a LF at end of string */ \
1321 if (buf[cnt-1] == '\n') \
1323 if (cnt > ((slen) - 1)) \
1325 memcpy(fc_host_##field(shost), buf, cnt); \
1326 i->f->set_host_##field(shost); \
1330 #define fc_host_rd_attr(field, format_string, sz) \
1331 fc_host_show_function(field, format_string, sz, ) \
1332 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1333 show_fc_host_##field, NULL)
1335 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1336 fc_host_show_function(field, format_string, sz, (cast)) \
1337 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1338 show_fc_host_##field, NULL)
1340 #define fc_host_rw_attr(field, format_string, sz) \
1341 fc_host_show_function(field, format_string, sz, ) \
1342 fc_host_store_function(field) \
1343 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1344 show_fc_host_##field, \
1345 store_fc_host_##field)
1347 #define fc_host_rd_enum_attr(title, maxlen) \
1349 show_fc_host_##title (struct device *dev, \
1350 struct device_attribute *attr, char *buf) \
1352 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1353 struct fc_internal *i = to_fc_internal(shost->transportt); \
1355 if (i->f->get_host_##title) \
1356 i->f->get_host_##title(shost); \
1357 name = get_fc_##title##_name(fc_host_##title(shost)); \
1360 return snprintf(buf, maxlen, "%s\n", name); \
1362 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1364 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1365 i->private_host_attrs[count] = device_attr_host_##field; \
1366 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1367 i->private_host_attrs[count].store = NULL; \
1368 i->host_attrs[count] = &i->private_host_attrs[count]; \
1369 if (i->f->show_host_##field) \
1372 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1373 i->private_host_attrs[count] = device_attr_host_##field; \
1374 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1375 i->private_host_attrs[count].store = NULL; \
1376 i->host_attrs[count] = &i->private_host_attrs[count]; \
1379 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1380 i->private_host_attrs[count] = device_attr_host_##field; \
1381 if (!i->f->set_host_##field) { \
1382 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1383 i->private_host_attrs[count].store = NULL; \
1385 i->host_attrs[count] = &i->private_host_attrs[count]; \
1386 if (i->f->show_host_##field) \
1390 #define fc_private_host_show_function(field, format_string, sz, cast) \
1392 show_fc_host_##field (struct device *dev, \
1393 struct device_attribute *attr, char *buf) \
1395 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1396 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1399 #define fc_private_host_rd_attr(field, format_string, sz) \
1400 fc_private_host_show_function(field, format_string, sz, ) \
1401 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1402 show_fc_host_##field, NULL)
1404 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1405 fc_private_host_show_function(field, format_string, sz, (cast)) \
1406 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1407 show_fc_host_##field, NULL)
1409 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1410 i->private_host_attrs[count] = device_attr_host_##field; \
1411 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1412 i->private_host_attrs[count].store = NULL; \
1413 i->host_attrs[count] = &i->private_host_attrs[count]; \
1416 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1418 i->private_host_attrs[count] = device_attr_host_##field; \
1419 i->host_attrs[count] = &i->private_host_attrs[count]; \
1424 /* Fixed Host Attributes */
1427 show_fc_host_supported_classes (struct device
*dev
,
1428 struct device_attribute
*attr
, char *buf
)
1430 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1432 if (fc_host_supported_classes(shost
) == FC_COS_UNSPECIFIED
)
1433 return snprintf(buf
, 20, "unspecified\n");
1435 return get_fc_cos_names(fc_host_supported_classes(shost
), buf
);
1437 static FC_DEVICE_ATTR(host
, supported_classes
, S_IRUGO
,
1438 show_fc_host_supported_classes
, NULL
);
1441 show_fc_host_supported_fc4s (struct device
*dev
,
1442 struct device_attribute
*attr
, char *buf
)
1444 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1445 return (ssize_t
)show_fc_fc4s(buf
, fc_host_supported_fc4s(shost
));
1447 static FC_DEVICE_ATTR(host
, supported_fc4s
, S_IRUGO
,
1448 show_fc_host_supported_fc4s
, NULL
);
1451 show_fc_host_supported_speeds (struct device
*dev
,
1452 struct device_attribute
*attr
, char *buf
)
1454 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1456 if (fc_host_supported_speeds(shost
) == FC_PORTSPEED_UNKNOWN
)
1457 return snprintf(buf
, 20, "unknown\n");
1459 return get_fc_port_speed_names(fc_host_supported_speeds(shost
), buf
);
1461 static FC_DEVICE_ATTR(host
, supported_speeds
, S_IRUGO
,
1462 show_fc_host_supported_speeds
, NULL
);
1465 fc_private_host_rd_attr_cast(node_name
, "0x%llx\n", 20, unsigned long long);
1466 fc_private_host_rd_attr_cast(port_name
, "0x%llx\n", 20, unsigned long long);
1467 fc_private_host_rd_attr_cast(permanent_port_name
, "0x%llx\n", 20,
1468 unsigned long long);
1469 fc_private_host_rd_attr(maxframe_size
, "%u bytes\n", 20);
1470 fc_private_host_rd_attr(max_npiv_vports
, "%u\n", 20);
1471 fc_private_host_rd_attr(serial_number
, "%s\n", (FC_SERIAL_NUMBER_SIZE
+1));
1474 /* Dynamic Host Attributes */
1477 show_fc_host_active_fc4s (struct device
*dev
,
1478 struct device_attribute
*attr
, char *buf
)
1480 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1481 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1483 if (i
->f
->get_host_active_fc4s
)
1484 i
->f
->get_host_active_fc4s(shost
);
1486 return (ssize_t
)show_fc_fc4s(buf
, fc_host_active_fc4s(shost
));
1488 static FC_DEVICE_ATTR(host
, active_fc4s
, S_IRUGO
,
1489 show_fc_host_active_fc4s
, NULL
);
1492 show_fc_host_speed (struct device
*dev
,
1493 struct device_attribute
*attr
, char *buf
)
1495 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1496 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1498 if (i
->f
->get_host_speed
)
1499 i
->f
->get_host_speed(shost
);
1501 if (fc_host_speed(shost
) == FC_PORTSPEED_UNKNOWN
)
1502 return snprintf(buf
, 20, "unknown\n");
1504 return get_fc_port_speed_names(fc_host_speed(shost
), buf
);
1506 static FC_DEVICE_ATTR(host
, speed
, S_IRUGO
,
1507 show_fc_host_speed
, NULL
);
1510 fc_host_rd_attr(port_id
, "0x%06x\n", 20);
1511 fc_host_rd_enum_attr(port_type
, FC_PORTTYPE_MAX_NAMELEN
);
1512 fc_host_rd_enum_attr(port_state
, FC_PORTSTATE_MAX_NAMELEN
);
1513 fc_host_rd_attr_cast(fabric_name
, "0x%llx\n", 20, unsigned long long);
1514 fc_host_rd_attr(symbolic_name
, "%s\n", FC_SYMBOLIC_NAME_SIZE
+ 1);
1516 fc_private_host_show_function(system_hostname
, "%s\n",
1517 FC_SYMBOLIC_NAME_SIZE
+ 1, )
1518 fc_host_store_str_function(system_hostname
, FC_SYMBOLIC_NAME_SIZE
)
1519 static FC_DEVICE_ATTR(host
, system_hostname
, S_IRUGO
| S_IWUSR
,
1520 show_fc_host_system_hostname
, store_fc_host_system_hostname
);
1523 /* Private Host Attributes */
1526 show_fc_private_host_tgtid_bind_type(struct device
*dev
,
1527 struct device_attribute
*attr
, char *buf
)
1529 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1532 name
= get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost
));
1535 return snprintf(buf
, FC_BINDTYPE_MAX_NAMELEN
, "%s\n", name
);
1538 #define get_list_head_entry(pos, head, member) \
1539 pos = list_entry((head)->next, typeof(*pos), member)
1542 store_fc_private_host_tgtid_bind_type(struct device
*dev
,
1543 struct device_attribute
*attr
, const char *buf
, size_t count
)
1545 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1546 struct fc_rport
*rport
;
1547 enum fc_tgtid_binding_type val
;
1548 unsigned long flags
;
1550 if (get_fc_tgtid_bind_type_match(buf
, &val
))
1553 /* if changing bind type, purge all unused consistent bindings */
1554 if (val
!= fc_host_tgtid_bind_type(shost
)) {
1555 spin_lock_irqsave(shost
->host_lock
, flags
);
1556 while (!list_empty(&fc_host_rport_bindings(shost
))) {
1557 get_list_head_entry(rport
,
1558 &fc_host_rport_bindings(shost
), peers
);
1559 list_del(&rport
->peers
);
1560 rport
->port_state
= FC_PORTSTATE_DELETED
;
1561 fc_queue_work(shost
, &rport
->rport_delete_work
);
1563 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1566 fc_host_tgtid_bind_type(shost
) = val
;
1570 static FC_DEVICE_ATTR(host
, tgtid_bind_type
, S_IRUGO
| S_IWUSR
,
1571 show_fc_private_host_tgtid_bind_type
,
1572 store_fc_private_host_tgtid_bind_type
);
1575 store_fc_private_host_issue_lip(struct device
*dev
,
1576 struct device_attribute
*attr
, const char *buf
, size_t count
)
1578 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1579 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1582 /* ignore any data value written to the attribute */
1583 if (i
->f
->issue_fc_host_lip
) {
1584 ret
= i
->f
->issue_fc_host_lip(shost
);
1585 return ret
? ret
: count
;
1591 static FC_DEVICE_ATTR(host
, issue_lip
, S_IWUSR
, NULL
,
1592 store_fc_private_host_issue_lip
);
1594 fc_private_host_rd_attr(npiv_vports_inuse
, "%u\n", 20);
1598 * Host Statistics Management
1601 /* Show a given an attribute in the statistics group */
1603 fc_stat_show(const struct device
*dev
, char *buf
, unsigned long offset
)
1605 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1606 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1607 struct fc_host_statistics
*stats
;
1608 ssize_t ret
= -ENOENT
;
1610 if (offset
> sizeof(struct fc_host_statistics
) ||
1611 offset
% sizeof(u64
) != 0)
1614 if (i
->f
->get_fc_host_stats
) {
1615 stats
= (i
->f
->get_fc_host_stats
)(shost
);
1617 ret
= snprintf(buf
, 20, "0x%llx\n",
1618 (unsigned long long)*(u64
*)(((u8
*) stats
) + offset
));
1624 /* generate a read-only statistics attribute */
1625 #define fc_host_statistic(name) \
1626 static ssize_t show_fcstat_##name(struct device *cd, \
1627 struct device_attribute *attr, \
1630 return fc_stat_show(cd, buf, \
1631 offsetof(struct fc_host_statistics, name)); \
1633 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1635 fc_host_statistic(seconds_since_last_reset
);
1636 fc_host_statistic(tx_frames
);
1637 fc_host_statistic(tx_words
);
1638 fc_host_statistic(rx_frames
);
1639 fc_host_statistic(rx_words
);
1640 fc_host_statistic(lip_count
);
1641 fc_host_statistic(nos_count
);
1642 fc_host_statistic(error_frames
);
1643 fc_host_statistic(dumped_frames
);
1644 fc_host_statistic(link_failure_count
);
1645 fc_host_statistic(loss_of_sync_count
);
1646 fc_host_statistic(loss_of_signal_count
);
1647 fc_host_statistic(prim_seq_protocol_err_count
);
1648 fc_host_statistic(invalid_tx_word_count
);
1649 fc_host_statistic(invalid_crc_count
);
1650 fc_host_statistic(fcp_input_requests
);
1651 fc_host_statistic(fcp_output_requests
);
1652 fc_host_statistic(fcp_control_requests
);
1653 fc_host_statistic(fcp_input_megabytes
);
1654 fc_host_statistic(fcp_output_megabytes
);
1657 fc_reset_statistics(struct device
*dev
, struct device_attribute
*attr
,
1658 const char *buf
, size_t count
)
1660 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1661 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
1663 /* ignore any data value written to the attribute */
1664 if (i
->f
->reset_fc_host_stats
) {
1665 i
->f
->reset_fc_host_stats(shost
);
1671 static FC_DEVICE_ATTR(host
, reset_statistics
, S_IWUSR
, NULL
,
1672 fc_reset_statistics
);
1674 static struct attribute
*fc_statistics_attrs
[] = {
1675 &device_attr_host_seconds_since_last_reset
.attr
,
1676 &device_attr_host_tx_frames
.attr
,
1677 &device_attr_host_tx_words
.attr
,
1678 &device_attr_host_rx_frames
.attr
,
1679 &device_attr_host_rx_words
.attr
,
1680 &device_attr_host_lip_count
.attr
,
1681 &device_attr_host_nos_count
.attr
,
1682 &device_attr_host_error_frames
.attr
,
1683 &device_attr_host_dumped_frames
.attr
,
1684 &device_attr_host_link_failure_count
.attr
,
1685 &device_attr_host_loss_of_sync_count
.attr
,
1686 &device_attr_host_loss_of_signal_count
.attr
,
1687 &device_attr_host_prim_seq_protocol_err_count
.attr
,
1688 &device_attr_host_invalid_tx_word_count
.attr
,
1689 &device_attr_host_invalid_crc_count
.attr
,
1690 &device_attr_host_fcp_input_requests
.attr
,
1691 &device_attr_host_fcp_output_requests
.attr
,
1692 &device_attr_host_fcp_control_requests
.attr
,
1693 &device_attr_host_fcp_input_megabytes
.attr
,
1694 &device_attr_host_fcp_output_megabytes
.attr
,
1695 &device_attr_host_reset_statistics
.attr
,
1699 static struct attribute_group fc_statistics_group
= {
1700 .name
= "statistics",
1701 .attrs
= fc_statistics_attrs
,
1705 /* Host Vport Attributes */
1708 fc_parse_wwn(const char *ns
, u64
*nm
)
1713 memset(wwn
, 0, sizeof(wwn
));
1715 /* Validate and store the new name */
1716 for (i
=0, j
=0; i
< 16; i
++) {
1717 if ((*ns
>= 'a') && (*ns
<= 'f'))
1718 j
= ((j
<< 4) | ((*ns
++ -'a') + 10));
1719 else if ((*ns
>= 'A') && (*ns
<= 'F'))
1720 j
= ((j
<< 4) | ((*ns
++ -'A') + 10));
1721 else if ((*ns
>= '0') && (*ns
<= '9'))
1722 j
= ((j
<< 4) | (*ns
++ -'0'));
1726 wwn
[i
/2] = j
& 0xff;
1731 *nm
= wwn_to_u64(wwn
);
1738 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1739 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1740 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1741 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1744 store_fc_host_vport_create(struct device
*dev
, struct device_attribute
*attr
,
1745 const char *buf
, size_t count
)
1747 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1748 struct fc_vport_identifiers vid
;
1749 struct fc_vport
*vport
;
1750 unsigned int cnt
=count
;
1753 memset(&vid
, 0, sizeof(vid
));
1755 /* count may include a LF at end of string */
1756 if (buf
[cnt
-1] == '\n')
1759 /* validate we have enough characters for WWPN */
1760 if ((cnt
!= (16+1+16)) || (buf
[16] != ':'))
1763 stat
= fc_parse_wwn(&buf
[0], &vid
.port_name
);
1767 stat
= fc_parse_wwn(&buf
[17], &vid
.node_name
);
1771 vid
.roles
= FC_PORT_ROLE_FCP_INITIATOR
;
1772 vid
.vport_type
= FC_PORTTYPE_NPIV
;
1773 /* vid.symbolic_name is already zero/NULL's */
1774 vid
.disable
= false; /* always enabled */
1776 /* we only allow support on Channel 0 !!! */
1777 stat
= fc_vport_setup(shost
, 0, &shost
->shost_gendev
, &vid
, &vport
);
1778 return stat
? stat
: count
;
1780 static FC_DEVICE_ATTR(host
, vport_create
, S_IWUSR
, NULL
,
1781 store_fc_host_vport_create
);
1785 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1786 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1787 * The WWNs are specified as hex characters, and may *not* contain
1788 * any prefixes (e.g. 0x, x, etc)
1791 store_fc_host_vport_delete(struct device
*dev
, struct device_attribute
*attr
,
1792 const char *buf
, size_t count
)
1794 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
1795 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
1796 struct fc_vport
*vport
;
1798 unsigned long flags
;
1799 unsigned int cnt
=count
;
1802 /* count may include a LF at end of string */
1803 if (buf
[cnt
-1] == '\n')
1806 /* validate we have enough characters for WWPN */
1807 if ((cnt
!= (16+1+16)) || (buf
[16] != ':'))
1810 stat
= fc_parse_wwn(&buf
[0], &wwpn
);
1814 stat
= fc_parse_wwn(&buf
[17], &wwnn
);
1818 spin_lock_irqsave(shost
->host_lock
, flags
);
1820 /* we only allow support on Channel 0 !!! */
1821 list_for_each_entry(vport
, &fc_host
->vports
, peers
) {
1822 if ((vport
->channel
== 0) &&
1823 (vport
->port_name
== wwpn
) && (vport
->node_name
== wwnn
)) {
1828 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1833 stat
= fc_vport_terminate(vport
);
1834 return stat
? stat
: count
;
1836 static FC_DEVICE_ATTR(host
, vport_delete
, S_IWUSR
, NULL
,
1837 store_fc_host_vport_delete
);
1840 static int fc_host_match(struct attribute_container
*cont
,
1843 struct Scsi_Host
*shost
;
1844 struct fc_internal
*i
;
1846 if (!scsi_is_host_device(dev
))
1849 shost
= dev_to_shost(dev
);
1850 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1851 != &fc_host_class
.class)
1854 i
= to_fc_internal(shost
->transportt
);
1856 return &i
->t
.host_attrs
.ac
== cont
;
1859 static int fc_target_match(struct attribute_container
*cont
,
1862 struct Scsi_Host
*shost
;
1863 struct fc_internal
*i
;
1865 if (!scsi_is_target_device(dev
))
1868 shost
= dev_to_shost(dev
->parent
);
1869 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1870 != &fc_host_class
.class)
1873 i
= to_fc_internal(shost
->transportt
);
1875 return &i
->t
.target_attrs
.ac
== cont
;
1878 static void fc_rport_dev_release(struct device
*dev
)
1880 struct fc_rport
*rport
= dev_to_rport(dev
);
1881 put_device(dev
->parent
);
1885 int scsi_is_fc_rport(const struct device
*dev
)
1887 return dev
->release
== fc_rport_dev_release
;
1889 EXPORT_SYMBOL(scsi_is_fc_rport
);
1891 static int fc_rport_match(struct attribute_container
*cont
,
1894 struct Scsi_Host
*shost
;
1895 struct fc_internal
*i
;
1897 if (!scsi_is_fc_rport(dev
))
1900 shost
= dev_to_shost(dev
->parent
);
1901 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1902 != &fc_host_class
.class)
1905 i
= to_fc_internal(shost
->transportt
);
1907 return &i
->rport_attr_cont
.ac
== cont
;
1911 static void fc_vport_dev_release(struct device
*dev
)
1913 struct fc_vport
*vport
= dev_to_vport(dev
);
1914 put_device(dev
->parent
); /* release kobj parent */
1918 int scsi_is_fc_vport(const struct device
*dev
)
1920 return dev
->release
== fc_vport_dev_release
;
1922 EXPORT_SYMBOL(scsi_is_fc_vport
);
1924 static int fc_vport_match(struct attribute_container
*cont
,
1927 struct fc_vport
*vport
;
1928 struct Scsi_Host
*shost
;
1929 struct fc_internal
*i
;
1931 if (!scsi_is_fc_vport(dev
))
1933 vport
= dev_to_vport(dev
);
1935 shost
= vport_to_shost(vport
);
1936 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1937 != &fc_host_class
.class)
1940 i
= to_fc_internal(shost
->transportt
);
1941 return &i
->vport_attr_cont
.ac
== cont
;
1946 * fc_timed_out - FC Transport I/O timeout intercept handler
1947 * @scmd: The SCSI command which timed out
1949 * This routine protects against error handlers getting invoked while a
1950 * rport is in a blocked state, typically due to a temporarily loss of
1951 * connectivity. If the error handlers are allowed to proceed, requests
1952 * to abort i/o, reset the target, etc will likely fail as there is no way
1953 * to communicate with the device to perform the requested function. These
1954 * failures may result in the midlayer taking the device offline, requiring
1955 * manual intervention to restore operation.
1957 * This routine, called whenever an i/o times out, validates the state of
1958 * the underlying rport. If the rport is blocked, it returns
1959 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1960 * Eventually, either the device will return, or devloss_tmo will fire,
1961 * and when the timeout then fires, it will be handled normally.
1962 * If the rport is not blocked, normal error handling continues.
1965 * This routine assumes no locks are held on entry.
1967 static enum blk_eh_timer_return
1968 fc_timed_out(struct scsi_cmnd
*scmd
)
1970 struct fc_rport
*rport
= starget_to_rport(scsi_target(scmd
->device
));
1972 if (rport
->port_state
== FC_PORTSTATE_BLOCKED
)
1973 return BLK_EH_RESET_TIMER
;
1975 return BLK_EH_NOT_HANDLED
;
1979 * Called by fc_user_scan to locate an rport on the shost that
1980 * matches the channel and target id, and invoke scsi_scan_target()
1984 fc_user_scan_tgt(struct Scsi_Host
*shost
, uint channel
, uint id
, uint lun
)
1986 struct fc_rport
*rport
;
1987 unsigned long flags
;
1989 spin_lock_irqsave(shost
->host_lock
, flags
);
1991 list_for_each_entry(rport
, &fc_host_rports(shost
), peers
) {
1992 if (rport
->scsi_target_id
== -1)
1995 if (rport
->port_state
!= FC_PORTSTATE_ONLINE
)
1998 if ((channel
== rport
->channel
) &&
1999 (id
== rport
->scsi_target_id
)) {
2000 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2001 scsi_scan_target(&rport
->dev
, channel
, id
, lun
, 1);
2006 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2010 * Called via sysfs scan routines. Necessary, as the FC transport
2011 * wants to place all target objects below the rport object. So this
2012 * routine must invoke the scsi_scan_target() routine with the rport
2013 * object as the parent.
2016 fc_user_scan(struct Scsi_Host
*shost
, uint channel
, uint id
, uint lun
)
2021 if (((channel
!= SCAN_WILD_CARD
) && (channel
> shost
->max_channel
)) ||
2022 ((id
!= SCAN_WILD_CARD
) && (id
>= shost
->max_id
)) ||
2023 ((lun
!= SCAN_WILD_CARD
) && (lun
> shost
->max_lun
)))
2026 if (channel
== SCAN_WILD_CARD
) {
2028 chhi
= shost
->max_channel
+ 1;
2034 if (id
== SCAN_WILD_CARD
) {
2036 tgthi
= shost
->max_id
;
2042 for ( ; chlo
< chhi
; chlo
++)
2043 for ( ; tgtlo
< tgthi
; tgtlo
++)
2044 fc_user_scan_tgt(shost
, chlo
, tgtlo
, lun
);
2049 static int fc_tsk_mgmt_response(struct Scsi_Host
*shost
, u64 nexus
, u64 tm_id
,
2052 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2053 return i
->f
->tsk_mgmt_response(shost
, nexus
, tm_id
, result
);
2056 static int fc_it_nexus_response(struct Scsi_Host
*shost
, u64 nexus
, int result
)
2058 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2059 return i
->f
->it_nexus_response(shost
, nexus
, result
);
2062 struct scsi_transport_template
*
2063 fc_attach_transport(struct fc_function_template
*ft
)
2066 struct fc_internal
*i
= kzalloc(sizeof(struct fc_internal
),
2072 i
->t
.target_attrs
.ac
.attrs
= &i
->starget_attrs
[0];
2073 i
->t
.target_attrs
.ac
.class = &fc_transport_class
.class;
2074 i
->t
.target_attrs
.ac
.match
= fc_target_match
;
2075 i
->t
.target_size
= sizeof(struct fc_starget_attrs
);
2076 transport_container_register(&i
->t
.target_attrs
);
2078 i
->t
.host_attrs
.ac
.attrs
= &i
->host_attrs
[0];
2079 i
->t
.host_attrs
.ac
.class = &fc_host_class
.class;
2080 i
->t
.host_attrs
.ac
.match
= fc_host_match
;
2081 i
->t
.host_size
= sizeof(struct fc_host_attrs
);
2082 if (ft
->get_fc_host_stats
)
2083 i
->t
.host_attrs
.statistics
= &fc_statistics_group
;
2084 transport_container_register(&i
->t
.host_attrs
);
2086 i
->rport_attr_cont
.ac
.attrs
= &i
->rport_attrs
[0];
2087 i
->rport_attr_cont
.ac
.class = &fc_rport_class
.class;
2088 i
->rport_attr_cont
.ac
.match
= fc_rport_match
;
2089 transport_container_register(&i
->rport_attr_cont
);
2091 i
->vport_attr_cont
.ac
.attrs
= &i
->vport_attrs
[0];
2092 i
->vport_attr_cont
.ac
.class = &fc_vport_class
.class;
2093 i
->vport_attr_cont
.ac
.match
= fc_vport_match
;
2094 transport_container_register(&i
->vport_attr_cont
);
2098 /* Transport uses the shost workq for scsi scanning */
2099 i
->t
.create_work_queue
= 1;
2101 i
->t
.eh_timed_out
= fc_timed_out
;
2103 i
->t
.user_scan
= fc_user_scan
;
2105 /* target-mode drivers' functions */
2106 i
->t
.tsk_mgmt_response
= fc_tsk_mgmt_response
;
2107 i
->t
.it_nexus_response
= fc_it_nexus_response
;
2110 * Setup SCSI Target Attributes.
2113 SETUP_STARGET_ATTRIBUTE_RD(node_name
);
2114 SETUP_STARGET_ATTRIBUTE_RD(port_name
);
2115 SETUP_STARGET_ATTRIBUTE_RD(port_id
);
2117 BUG_ON(count
> FC_STARGET_NUM_ATTRS
);
2119 i
->starget_attrs
[count
] = NULL
;
2123 * Setup SCSI Host Attributes.
2126 SETUP_HOST_ATTRIBUTE_RD(node_name
);
2127 SETUP_HOST_ATTRIBUTE_RD(port_name
);
2128 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name
);
2129 SETUP_HOST_ATTRIBUTE_RD(supported_classes
);
2130 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s
);
2131 SETUP_HOST_ATTRIBUTE_RD(supported_speeds
);
2132 SETUP_HOST_ATTRIBUTE_RD(maxframe_size
);
2133 if (ft
->vport_create
) {
2134 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports
);
2135 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse
);
2137 SETUP_HOST_ATTRIBUTE_RD(serial_number
);
2139 SETUP_HOST_ATTRIBUTE_RD(port_id
);
2140 SETUP_HOST_ATTRIBUTE_RD(port_type
);
2141 SETUP_HOST_ATTRIBUTE_RD(port_state
);
2142 SETUP_HOST_ATTRIBUTE_RD(active_fc4s
);
2143 SETUP_HOST_ATTRIBUTE_RD(speed
);
2144 SETUP_HOST_ATTRIBUTE_RD(fabric_name
);
2145 SETUP_HOST_ATTRIBUTE_RD(symbolic_name
);
2146 SETUP_HOST_ATTRIBUTE_RW(system_hostname
);
2148 /* Transport-managed attributes */
2149 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type
);
2150 if (ft
->issue_fc_host_lip
)
2151 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip
);
2152 if (ft
->vport_create
)
2153 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create
);
2154 if (ft
->vport_delete
)
2155 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete
);
2157 BUG_ON(count
> FC_HOST_NUM_ATTRS
);
2159 i
->host_attrs
[count
] = NULL
;
2162 * Setup Remote Port Attributes.
2165 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size
);
2166 SETUP_RPORT_ATTRIBUTE_RD(supported_classes
);
2167 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo
);
2168 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name
);
2169 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name
);
2170 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id
);
2171 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles
);
2172 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state
);
2173 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id
);
2174 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo
);
2176 BUG_ON(count
> FC_RPORT_NUM_ATTRS
);
2178 i
->rport_attrs
[count
] = NULL
;
2181 * Setup Virtual Port Attributes.
2184 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state
);
2185 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state
);
2186 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name
);
2187 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name
);
2188 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles
);
2189 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type
);
2190 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name
);
2191 SETUP_VPORT_ATTRIBUTE_WR(vport_delete
);
2192 SETUP_VPORT_ATTRIBUTE_WR(vport_disable
);
2194 BUG_ON(count
> FC_VPORT_NUM_ATTRS
);
2196 i
->vport_attrs
[count
] = NULL
;
2200 EXPORT_SYMBOL(fc_attach_transport
);
2202 void fc_release_transport(struct scsi_transport_template
*t
)
2204 struct fc_internal
*i
= to_fc_internal(t
);
2206 transport_container_unregister(&i
->t
.target_attrs
);
2207 transport_container_unregister(&i
->t
.host_attrs
);
2208 transport_container_unregister(&i
->rport_attr_cont
);
2209 transport_container_unregister(&i
->vport_attr_cont
);
2213 EXPORT_SYMBOL(fc_release_transport
);
2216 * fc_queue_work - Queue work to the fc_host workqueue.
2217 * @shost: Pointer to Scsi_Host bound to fc_host.
2218 * @work: Work to queue for execution.
2221 * 1 - work queued for execution
2222 * 0 - work is already queued
2223 * -EINVAL - work queue doesn't exist
2226 fc_queue_work(struct Scsi_Host
*shost
, struct work_struct
*work
)
2228 if (unlikely(!fc_host_work_q(shost
))) {
2230 "ERROR: FC host '%s' attempted to queue work, "
2231 "when no workqueue created.\n", shost
->hostt
->name
);
2237 return queue_work(fc_host_work_q(shost
), work
);
2241 * fc_flush_work - Flush a fc_host's workqueue.
2242 * @shost: Pointer to Scsi_Host bound to fc_host.
2245 fc_flush_work(struct Scsi_Host
*shost
)
2247 if (!fc_host_work_q(shost
)) {
2249 "ERROR: FC host '%s' attempted to flush work, "
2250 "when no workqueue created.\n", shost
->hostt
->name
);
2255 flush_workqueue(fc_host_work_q(shost
));
2259 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2260 * @shost: Pointer to Scsi_Host bound to fc_host.
2261 * @work: Work to queue for execution.
2262 * @delay: jiffies to delay the work queuing
2265 * 1 on success / 0 already queued / < 0 for error
2268 fc_queue_devloss_work(struct Scsi_Host
*shost
, struct delayed_work
*work
,
2269 unsigned long delay
)
2271 if (unlikely(!fc_host_devloss_work_q(shost
))) {
2273 "ERROR: FC host '%s' attempted to queue work, "
2274 "when no workqueue created.\n", shost
->hostt
->name
);
2280 return queue_delayed_work(fc_host_devloss_work_q(shost
), work
, delay
);
2284 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2285 * @shost: Pointer to Scsi_Host bound to fc_host.
2288 fc_flush_devloss(struct Scsi_Host
*shost
)
2290 if (!fc_host_devloss_work_q(shost
)) {
2292 "ERROR: FC host '%s' attempted to flush work, "
2293 "when no workqueue created.\n", shost
->hostt
->name
);
2298 flush_workqueue(fc_host_devloss_work_q(shost
));
2303 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2304 * @shost: Which &Scsi_Host
2306 * This routine is expected to be called immediately preceeding the
2307 * a driver's call to scsi_remove_host().
2309 * WARNING: A driver utilizing the fc_transport, which fails to call
2310 * this routine prior to scsi_remove_host(), will leave dangling
2311 * objects in /sys/class/fc_remote_ports. Access to any of these
2312 * objects can result in a system crash !!!
2315 * This routine assumes no locks are held on entry.
2318 fc_remove_host(struct Scsi_Host
*shost
)
2320 struct fc_vport
*vport
= NULL
, *next_vport
= NULL
;
2321 struct fc_rport
*rport
= NULL
, *next_rport
= NULL
;
2322 struct workqueue_struct
*work_q
;
2323 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2324 unsigned long flags
;
2326 spin_lock_irqsave(shost
->host_lock
, flags
);
2328 /* Remove any vports */
2329 list_for_each_entry_safe(vport
, next_vport
, &fc_host
->vports
, peers
)
2330 fc_queue_work(shost
, &vport
->vport_delete_work
);
2332 /* Remove any remote ports */
2333 list_for_each_entry_safe(rport
, next_rport
,
2334 &fc_host
->rports
, peers
) {
2335 list_del(&rport
->peers
);
2336 rport
->port_state
= FC_PORTSTATE_DELETED
;
2337 fc_queue_work(shost
, &rport
->rport_delete_work
);
2340 list_for_each_entry_safe(rport
, next_rport
,
2341 &fc_host
->rport_bindings
, peers
) {
2342 list_del(&rport
->peers
);
2343 rport
->port_state
= FC_PORTSTATE_DELETED
;
2344 fc_queue_work(shost
, &rport
->rport_delete_work
);
2347 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2349 /* flush all scan work items */
2350 scsi_flush_work(shost
);
2352 /* flush all stgt delete, and rport delete work items, then kill it */
2353 if (fc_host
->work_q
) {
2354 work_q
= fc_host
->work_q
;
2355 fc_host
->work_q
= NULL
;
2356 destroy_workqueue(work_q
);
2359 /* flush all devloss work items, then kill it */
2360 if (fc_host
->devloss_work_q
) {
2361 work_q
= fc_host
->devloss_work_q
;
2362 fc_host
->devloss_work_q
= NULL
;
2363 destroy_workqueue(work_q
);
2366 EXPORT_SYMBOL(fc_remove_host
);
2368 static void fc_terminate_rport_io(struct fc_rport
*rport
)
2370 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2371 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2373 /* Involve the LLDD if possible to terminate all io on the rport. */
2374 if (i
->f
->terminate_rport_io
)
2375 i
->f
->terminate_rport_io(rport
);
2378 * must unblock to flush queued IO. The caller will have set
2379 * the port_state or flags, so that fc_remote_port_chkready will
2382 scsi_target_unblock(&rport
->dev
);
2386 * fc_starget_delete - called to delete the scsi decendents of an rport
2387 * @work: remote port to be operated on.
2389 * Deletes target and all sdevs.
2392 fc_starget_delete(struct work_struct
*work
)
2394 struct fc_rport
*rport
=
2395 container_of(work
, struct fc_rport
, stgt_delete_work
);
2397 fc_terminate_rport_io(rport
);
2398 scsi_remove_target(&rport
->dev
);
2403 * fc_rport_final_delete - finish rport termination and delete it.
2404 * @work: remote port to be deleted.
2407 fc_rport_final_delete(struct work_struct
*work
)
2409 struct fc_rport
*rport
=
2410 container_of(work
, struct fc_rport
, rport_delete_work
);
2411 struct device
*dev
= &rport
->dev
;
2412 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2413 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
2414 unsigned long flags
;
2415 int do_callback
= 0;
2418 * if a scan is pending, flush the SCSI Host work_q so that
2419 * that we can reclaim the rport scan work element.
2421 if (rport
->flags
& FC_RPORT_SCAN_PENDING
)
2422 scsi_flush_work(shost
);
2424 fc_terminate_rport_io(rport
);
2427 * Cancel any outstanding timers. These should really exist
2428 * only when rmmod'ing the LLDD and we're asking for
2429 * immediate termination of the rports
2431 spin_lock_irqsave(shost
->host_lock
, flags
);
2432 if (rport
->flags
& FC_RPORT_DEVLOSS_PENDING
) {
2433 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2434 if (!cancel_delayed_work(&rport
->fail_io_work
))
2435 fc_flush_devloss(shost
);
2436 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2437 fc_flush_devloss(shost
);
2438 spin_lock_irqsave(shost
->host_lock
, flags
);
2439 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
2441 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2443 /* Delete SCSI target and sdevs */
2444 if (rport
->scsi_target_id
!= -1)
2445 fc_starget_delete(&rport
->stgt_delete_work
);
2448 * Notify the driver that the rport is now dead. The LLDD will
2449 * also guarantee that any communication to the rport is terminated
2451 * Avoid this call if we already called it when we preserved the
2452 * rport for the binding.
2454 spin_lock_irqsave(shost
->host_lock
, flags
);
2455 if (!(rport
->flags
& FC_RPORT_DEVLOSS_CALLBK_DONE
) &&
2456 (i
->f
->dev_loss_tmo_callbk
)) {
2457 rport
->flags
|= FC_RPORT_DEVLOSS_CALLBK_DONE
;
2460 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2463 i
->f
->dev_loss_tmo_callbk(rport
);
2465 fc_bsg_remove(rport
->rqst_q
);
2467 transport_remove_device(dev
);
2469 transport_destroy_device(dev
);
2470 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2471 put_device(dev
); /* for self-reference */
2476 * fc_rport_create - allocates and creates a remote FC port.
2477 * @shost: scsi host the remote port is connected to.
2478 * @channel: Channel on shost port connected to.
2479 * @ids: The world wide names, fc address, and FC4 port
2480 * roles for the remote port.
2482 * Allocates and creates the remoter port structure, including the
2483 * class and sysfs creation.
2486 * This routine assumes no locks are held on entry.
2488 static struct fc_rport
*
2489 fc_rport_create(struct Scsi_Host
*shost
, int channel
,
2490 struct fc_rport_identifiers
*ids
)
2492 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2493 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2494 struct fc_rport
*rport
;
2496 unsigned long flags
;
2500 size
= (sizeof(struct fc_rport
) + fci
->f
->dd_fcrport_size
);
2501 rport
= kzalloc(size
, GFP_KERNEL
);
2502 if (unlikely(!rport
)) {
2503 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
2507 rport
->maxframe_size
= -1;
2508 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
2509 rport
->dev_loss_tmo
= fc_dev_loss_tmo
;
2510 memcpy(&rport
->node_name
, &ids
->node_name
, sizeof(rport
->node_name
));
2511 memcpy(&rport
->port_name
, &ids
->port_name
, sizeof(rport
->port_name
));
2512 rport
->port_id
= ids
->port_id
;
2513 rport
->roles
= ids
->roles
;
2514 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2515 if (fci
->f
->dd_fcrport_size
)
2516 rport
->dd_data
= &rport
[1];
2517 rport
->channel
= channel
;
2518 rport
->fast_io_fail_tmo
= -1;
2520 INIT_DELAYED_WORK(&rport
->dev_loss_work
, fc_timeout_deleted_rport
);
2521 INIT_DELAYED_WORK(&rport
->fail_io_work
, fc_timeout_fail_rport_io
);
2522 INIT_WORK(&rport
->scan_work
, fc_scsi_scan_rport
);
2523 INIT_WORK(&rport
->stgt_delete_work
, fc_starget_delete
);
2524 INIT_WORK(&rport
->rport_delete_work
, fc_rport_final_delete
);
2526 spin_lock_irqsave(shost
->host_lock
, flags
);
2528 rport
->number
= fc_host
->next_rport_number
++;
2529 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)
2530 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2532 rport
->scsi_target_id
= -1;
2533 list_add_tail(&rport
->peers
, &fc_host
->rports
);
2534 get_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2536 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2539 device_initialize(dev
); /* takes self reference */
2540 dev
->parent
= get_device(&shost
->shost_gendev
); /* parent reference */
2541 dev
->release
= fc_rport_dev_release
;
2542 dev_set_name(dev
, "rport-%d:%d-%d",
2543 shost
->host_no
, channel
, rport
->number
);
2544 transport_setup_device(dev
);
2546 error
= device_add(dev
);
2548 printk(KERN_ERR
"FC Remote Port device_add failed\n");
2551 transport_add_device(dev
);
2552 transport_configure_device(dev
);
2554 fc_bsg_rportadd(shost
, rport
);
2555 /* ignore any bsg add error - we just can't do sgio */
2557 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2558 /* initiate a scan of the target */
2559 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2560 scsi_queue_work(shost
, &rport
->scan_work
);
2566 transport_destroy_device(dev
);
2567 spin_lock_irqsave(shost
->host_lock
, flags
);
2568 list_del(&rport
->peers
);
2569 put_device(&shost
->shost_gendev
); /* for fc_host->rport list */
2570 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2571 put_device(dev
->parent
);
2577 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2578 * @shost: scsi host the remote port is connected to.
2579 * @channel: Channel on shost port connected to.
2580 * @ids: The world wide names, fc address, and FC4 port
2581 * roles for the remote port.
2583 * The LLDD calls this routine to notify the transport of the existence
2584 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2585 * of the port, it's FC address (port_id), and the FC4 roles that are
2586 * active for the port.
2588 * For ports that are FCP targets (aka scsi targets), the FC transport
2589 * maintains consistent target id bindings on behalf of the LLDD.
2590 * A consistent target id binding is an assignment of a target id to
2591 * a remote port identifier, which persists while the scsi host is
2592 * attached. The remote port can disappear, then later reappear, and
2593 * it's target id assignment remains the same. This allows for shifts
2594 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2595 * changes to the scsi subsystem which is based on scsi host number and
2596 * target id values. Bindings are only valid during the attachment of
2597 * the scsi host. If the host detaches, then later re-attaches, target
2598 * id bindings may change.
2600 * This routine is responsible for returning a remote port structure.
2601 * The routine will search the list of remote ports it maintains
2602 * internally on behalf of consistent target id mappings. If found, the
2603 * remote port structure will be reused. Otherwise, a new remote port
2604 * structure will be allocated.
2606 * Whenever a remote port is allocated, a new fc_remote_port class
2607 * device is created.
2609 * Should not be called from interrupt context.
2612 * This routine assumes no locks are held on entry.
2615 fc_remote_port_add(struct Scsi_Host
*shost
, int channel
,
2616 struct fc_rport_identifiers
*ids
)
2618 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
2619 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2620 struct fc_rport
*rport
;
2621 unsigned long flags
;
2624 /* ensure any stgt delete functions are done */
2625 fc_flush_work(shost
);
2628 * Search the list of "active" rports, for an rport that has been
2629 * deleted, but we've held off the real delete while the target
2630 * is in a "blocked" state.
2632 spin_lock_irqsave(shost
->host_lock
, flags
);
2634 list_for_each_entry(rport
, &fc_host
->rports
, peers
) {
2636 if ((rport
->port_state
== FC_PORTSTATE_BLOCKED
) &&
2637 (rport
->channel
== channel
)) {
2639 switch (fc_host
->tgtid_bind_type
) {
2640 case FC_TGTID_BIND_BY_WWPN
:
2641 case FC_TGTID_BIND_NONE
:
2642 if (rport
->port_name
== ids
->port_name
)
2645 case FC_TGTID_BIND_BY_WWNN
:
2646 if (rport
->node_name
== ids
->node_name
)
2649 case FC_TGTID_BIND_BY_ID
:
2650 if (rport
->port_id
== ids
->port_id
)
2657 memcpy(&rport
->node_name
, &ids
->node_name
,
2658 sizeof(rport
->node_name
));
2659 memcpy(&rport
->port_name
, &ids
->port_name
,
2660 sizeof(rport
->port_name
));
2661 rport
->port_id
= ids
->port_id
;
2663 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2664 rport
->roles
= ids
->roles
;
2666 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2668 if (fci
->f
->dd_fcrport_size
)
2669 memset(rport
->dd_data
, 0,
2670 fci
->f
->dd_fcrport_size
);
2673 * If we were not a target, cancel the
2674 * io terminate and rport timers, and
2677 * If we were a target, but our new role
2678 * doesn't indicate a target, leave the
2679 * timers running expecting the role to
2680 * change as the target fully logs in. If
2681 * it doesn't, the target will be torn down.
2683 * If we were a target, and our role shows
2684 * we're still a target, cancel the timers
2685 * and kick off a scan.
2688 /* was a target, not in roles */
2689 if ((rport
->scsi_target_id
!= -1) &&
2690 (!(ids
->roles
& FC_PORT_ROLE_FCP_TARGET
)))
2694 * Stop the fail io and dev_loss timers.
2695 * If they flush, the port_state will
2696 * be checked and will NOOP the function.
2698 if (!cancel_delayed_work(&rport
->fail_io_work
))
2699 fc_flush_devloss(shost
);
2700 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2701 fc_flush_devloss(shost
);
2703 spin_lock_irqsave(shost
->host_lock
, flags
);
2705 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2706 FC_RPORT_DEVLOSS_PENDING
|
2707 FC_RPORT_DEVLOSS_CALLBK_DONE
);
2709 /* if target, initiate a scan */
2710 if (rport
->scsi_target_id
!= -1) {
2711 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2712 scsi_queue_work(shost
,
2714 spin_unlock_irqrestore(shost
->host_lock
,
2716 scsi_target_unblock(&rport
->dev
);
2718 spin_unlock_irqrestore(shost
->host_lock
,
2721 fc_bsg_goose_queue(rport
);
2729 * Search the bindings array
2730 * Note: if never a FCP target, you won't be on this list
2732 if (fc_host
->tgtid_bind_type
!= FC_TGTID_BIND_NONE
) {
2734 /* search for a matching consistent binding */
2736 list_for_each_entry(rport
, &fc_host
->rport_bindings
,
2738 if (rport
->channel
!= channel
)
2741 switch (fc_host
->tgtid_bind_type
) {
2742 case FC_TGTID_BIND_BY_WWPN
:
2743 if (rport
->port_name
== ids
->port_name
)
2746 case FC_TGTID_BIND_BY_WWNN
:
2747 if (rport
->node_name
== ids
->node_name
)
2750 case FC_TGTID_BIND_BY_ID
:
2751 if (rport
->port_id
== ids
->port_id
)
2754 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
2759 list_move_tail(&rport
->peers
, &fc_host
->rports
);
2765 memcpy(&rport
->node_name
, &ids
->node_name
,
2766 sizeof(rport
->node_name
));
2767 memcpy(&rport
->port_name
, &ids
->port_name
,
2768 sizeof(rport
->port_name
));
2769 rport
->port_id
= ids
->port_id
;
2770 rport
->roles
= ids
->roles
;
2771 rport
->port_state
= FC_PORTSTATE_ONLINE
;
2772 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
2774 if (fci
->f
->dd_fcrport_size
)
2775 memset(rport
->dd_data
, 0,
2776 fci
->f
->dd_fcrport_size
);
2778 if (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) {
2779 /* initiate a scan of the target */
2780 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2781 scsi_queue_work(shost
, &rport
->scan_work
);
2782 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2783 scsi_target_unblock(&rport
->dev
);
2785 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2791 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2793 /* No consistent binding found - create new remote port entry */
2794 rport
= fc_rport_create(shost
, channel
, ids
);
2798 EXPORT_SYMBOL(fc_remote_port_add
);
2802 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2803 * @rport: The remote port that no longer exists
2805 * The LLDD calls this routine to notify the transport that a remote
2806 * port is no longer part of the topology. Note: Although a port
2807 * may no longer be part of the topology, it may persist in the remote
2808 * ports displayed by the fc_host. We do this under 2 conditions:
2809 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2810 * This allows the port to temporarily disappear, then reappear without
2811 * disrupting the SCSI device tree attached to it. During the "blocked"
2812 * period the port will still exist.
2813 * 2) If the port was a scsi target and disappears for longer than we
2814 * expect, we'll delete the port and the tear down the SCSI device tree
2815 * attached to it. However, we want to semi-persist the target id assigned
2816 * to that port if it eventually does exist. The port structure will
2817 * remain (although with minimal information) so that the target id
2820 * If the remote port is not an FCP Target, it will be fully torn down
2821 * and deallocated, including the fc_remote_port class device.
2823 * If the remote port is an FCP Target, the port will be placed in a
2824 * temporary blocked state. From the LLDD's perspective, the rport no
2825 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2826 * exists, but all sdevs on it are blocked from further I/O. The following
2829 * If the remote port does not return (signaled by a LLDD call to
2830 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2831 * scsi target is removed - killing all outstanding i/o and removing the
2832 * scsi devices attached ot it. The port structure will be marked Not
2833 * Present and be partially cleared, leaving only enough information to
2834 * recognize the remote port relative to the scsi target id binding if
2835 * it later appears. The port will remain as long as there is a valid
2836 * binding (e.g. until the user changes the binding type or unloads the
2837 * scsi host with the binding).
2839 * If the remote port returns within the dev_loss_tmo value (and matches
2840 * according to the target id binding type), the port structure will be
2841 * reused. If it is no longer a SCSI target, the target will be torn
2842 * down. If it continues to be a SCSI target, then the target will be
2843 * unblocked (allowing i/o to be resumed), and a scan will be activated
2844 * to ensure that all luns are detected.
2846 * Called from normal process context only - cannot be called from interrupt.
2849 * This routine assumes no locks are held on entry.
2852 fc_remote_port_delete(struct fc_rport
*rport
)
2854 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2855 int timeout
= rport
->dev_loss_tmo
;
2856 unsigned long flags
;
2859 * No need to flush the fc_host work_q's, as all adds are synchronous.
2861 * We do need to reclaim the rport scan work element, so eventually
2862 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2863 * there's still a scan pending.
2866 spin_lock_irqsave(shost
->host_lock
, flags
);
2868 if (rport
->port_state
!= FC_PORTSTATE_ONLINE
) {
2869 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2874 * In the past, we if this was not an FCP-Target, we would
2875 * unconditionally just jump to deleting the rport.
2876 * However, rports can be used as node containers by the LLDD,
2877 * and its not appropriate to just terminate the rport at the
2878 * first sign of a loss in connectivity. The LLDD may want to
2879 * send ELS traffic to re-validate the login. If the rport is
2880 * immediately deleted, it makes it inappropriate for a node
2882 * So... we now unconditionally wait dev_loss_tmo before
2883 * destroying an rport.
2886 rport
->port_state
= FC_PORTSTATE_BLOCKED
;
2888 rport
->flags
|= FC_RPORT_DEVLOSS_PENDING
;
2890 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2892 if (rport
->roles
& FC_PORT_ROLE_FCP_INITIATOR
&&
2893 shost
->active_mode
& MODE_TARGET
)
2894 fc_tgt_it_nexus_destroy(shost
, (unsigned long)rport
);
2896 scsi_target_block(&rport
->dev
);
2898 /* see if we need to kill io faster than waiting for device loss */
2899 if ((rport
->fast_io_fail_tmo
!= -1) &&
2900 (rport
->fast_io_fail_tmo
< timeout
))
2901 fc_queue_devloss_work(shost
, &rport
->fail_io_work
,
2902 rport
->fast_io_fail_tmo
* HZ
);
2904 /* cap the length the devices can be blocked until they are deleted */
2905 fc_queue_devloss_work(shost
, &rport
->dev_loss_work
, timeout
* HZ
);
2907 EXPORT_SYMBOL(fc_remote_port_delete
);
2910 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2911 * @rport: The remote port that changed.
2912 * @roles: New roles for this port.
2914 * Description: The LLDD calls this routine to notify the transport that the
2915 * roles on a remote port may have changed. The largest effect of this is
2916 * if a port now becomes a FCP Target, it must be allocated a
2917 * scsi target id. If the port is no longer a FCP target, any
2918 * scsi target id value assigned to it will persist in case the
2919 * role changes back to include FCP Target. No changes in the scsi
2920 * midlayer will be invoked if the role changes (in the expectation
2921 * that the role will be resumed. If it doesn't normal error processing
2924 * Should not be called from interrupt context.
2927 * This routine assumes no locks are held on entry.
2930 fc_remote_port_rolechg(struct fc_rport
*rport
, u32 roles
)
2932 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2933 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
2934 unsigned long flags
;
2938 spin_lock_irqsave(shost
->host_lock
, flags
);
2939 if (roles
& FC_PORT_ROLE_FCP_TARGET
) {
2940 if (rport
->scsi_target_id
== -1) {
2941 rport
->scsi_target_id
= fc_host
->next_target_id
++;
2943 } else if (!(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
))
2945 } else if (shost
->active_mode
& MODE_TARGET
) {
2946 ret
= fc_tgt_it_nexus_create(shost
, (unsigned long)rport
,
2947 (char *)&rport
->node_name
);
2949 printk(KERN_ERR
"FC Remore Port tgt nexus failed %d\n",
2953 rport
->roles
= roles
;
2955 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2959 * There may have been a delete timer running on the
2960 * port. Ensure that it is cancelled as we now know
2961 * the port is an FCP Target.
2962 * Note: we know the rport is exists and in an online
2963 * state as the LLDD would not have had an rport
2964 * reference to pass us.
2966 * Take no action on the del_timer failure as the state
2967 * machine state change will validate the
2970 if (!cancel_delayed_work(&rport
->fail_io_work
))
2971 fc_flush_devloss(shost
);
2972 if (!cancel_delayed_work(&rport
->dev_loss_work
))
2973 fc_flush_devloss(shost
);
2975 spin_lock_irqsave(shost
->host_lock
, flags
);
2976 rport
->flags
&= ~(FC_RPORT_FAST_FAIL_TIMEDOUT
|
2977 FC_RPORT_DEVLOSS_PENDING
);
2978 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2980 /* ensure any stgt delete functions are done */
2981 fc_flush_work(shost
);
2983 /* initiate a scan of the target */
2984 spin_lock_irqsave(shost
->host_lock
, flags
);
2985 rport
->flags
|= FC_RPORT_SCAN_PENDING
;
2986 scsi_queue_work(shost
, &rport
->scan_work
);
2987 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2988 scsi_target_unblock(&rport
->dev
);
2991 EXPORT_SYMBOL(fc_remote_port_rolechg
);
2994 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
2995 * @work: rport target that failed to reappear in the allotted time.
2997 * Description: An attempt to delete a remote port blocks, and if it fails
2998 * to return in the allotted time this gets called.
3001 fc_timeout_deleted_rport(struct work_struct
*work
)
3003 struct fc_rport
*rport
=
3004 container_of(work
, struct fc_rport
, dev_loss_work
.work
);
3005 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3006 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3007 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3008 unsigned long flags
;
3009 int do_callback
= 0;
3011 spin_lock_irqsave(shost
->host_lock
, flags
);
3013 rport
->flags
&= ~FC_RPORT_DEVLOSS_PENDING
;
3016 * If the port is ONLINE, then it came back. If it was a SCSI
3017 * target, validate it still is. If not, tear down the
3018 * scsi_target on it.
3020 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
3021 (rport
->scsi_target_id
!= -1) &&
3022 !(rport
->roles
& FC_PORT_ROLE_FCP_TARGET
)) {
3023 dev_printk(KERN_ERR
, &rport
->dev
,
3024 "blocked FC remote port time out: no longer"
3025 " a FCP target, removing starget\n");
3026 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3027 scsi_target_unblock(&rport
->dev
);
3028 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3032 /* NOOP state - we're flushing workq's */
3033 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
) {
3034 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3035 dev_printk(KERN_ERR
, &rport
->dev
,
3036 "blocked FC remote port time out: leaving"
3038 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3042 if ((fc_host
->tgtid_bind_type
== FC_TGTID_BIND_NONE
) ||
3043 (rport
->scsi_target_id
== -1)) {
3044 list_del(&rport
->peers
);
3045 rport
->port_state
= FC_PORTSTATE_DELETED
;
3046 dev_printk(KERN_ERR
, &rport
->dev
,
3047 "blocked FC remote port time out: removing"
3049 (rport
->scsi_target_id
!= -1) ? " and starget" : "");
3050 fc_queue_work(shost
, &rport
->rport_delete_work
);
3051 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3055 dev_printk(KERN_ERR
, &rport
->dev
,
3056 "blocked FC remote port time out: removing target and "
3057 "saving binding\n");
3059 list_move_tail(&rport
->peers
, &fc_host
->rport_bindings
);
3062 * Note: We do not remove or clear the hostdata area. This allows
3063 * host-specific target data to persist along with the
3064 * scsi_target_id. It's up to the host to manage it's hostdata area.
3068 * Reinitialize port attributes that may change if the port comes back.
3070 rport
->maxframe_size
= -1;
3071 rport
->supported_classes
= FC_COS_UNSPECIFIED
;
3072 rport
->roles
= FC_PORT_ROLE_UNKNOWN
;
3073 rport
->port_state
= FC_PORTSTATE_NOTPRESENT
;
3074 rport
->flags
&= ~FC_RPORT_FAST_FAIL_TIMEDOUT
;
3077 * Pre-emptively kill I/O rather than waiting for the work queue
3078 * item to teardown the starget. (FCOE libFC folks prefer this
3079 * and to have the rport_port_id still set when it's done).
3081 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3082 fc_terminate_rport_io(rport
);
3084 spin_lock_irqsave(shost
->host_lock
, flags
);
3086 if (rport
->port_state
== FC_PORTSTATE_NOTPRESENT
) { /* still missing */
3088 /* remove the identifiers that aren't used in the consisting binding */
3089 switch (fc_host
->tgtid_bind_type
) {
3090 case FC_TGTID_BIND_BY_WWPN
:
3091 rport
->node_name
= -1;
3092 rport
->port_id
= -1;
3094 case FC_TGTID_BIND_BY_WWNN
:
3095 rport
->port_name
= -1;
3096 rport
->port_id
= -1;
3098 case FC_TGTID_BIND_BY_ID
:
3099 rport
->node_name
= -1;
3100 rport
->port_name
= -1;
3102 case FC_TGTID_BIND_NONE
: /* to keep compiler happy */
3107 * As this only occurs if the remote port (scsi target)
3108 * went away and didn't come back - we'll remove
3109 * all attached scsi devices.
3111 rport
->flags
|= FC_RPORT_DEVLOSS_CALLBK_DONE
;
3112 fc_queue_work(shost
, &rport
->stgt_delete_work
);
3117 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3120 * Notify the driver that the rport is now dead. The LLDD will
3121 * also guarantee that any communication to the rport is terminated
3123 * Note: we set the CALLBK_DONE flag above to correspond
3125 if (do_callback
&& i
->f
->dev_loss_tmo_callbk
)
3126 i
->f
->dev_loss_tmo_callbk(rport
);
3131 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3132 * @work: rport to terminate io on.
3134 * Notes: Only requests the failure of the io, not that all are flushed
3135 * prior to returning.
3138 fc_timeout_fail_rport_io(struct work_struct
*work
)
3140 struct fc_rport
*rport
=
3141 container_of(work
, struct fc_rport
, fail_io_work
.work
);
3143 if (rport
->port_state
!= FC_PORTSTATE_BLOCKED
)
3146 rport
->flags
|= FC_RPORT_FAST_FAIL_TIMEDOUT
;
3147 fc_terminate_rport_io(rport
);
3151 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3152 * @work: remote port to be scanned.
3155 fc_scsi_scan_rport(struct work_struct
*work
)
3157 struct fc_rport
*rport
=
3158 container_of(work
, struct fc_rport
, scan_work
);
3159 struct Scsi_Host
*shost
= rport_to_shost(rport
);
3160 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3161 unsigned long flags
;
3163 if ((rport
->port_state
== FC_PORTSTATE_ONLINE
) &&
3164 (rport
->roles
& FC_PORT_ROLE_FCP_TARGET
) &&
3165 !(i
->f
->disable_target_scan
)) {
3166 scsi_scan_target(&rport
->dev
, rport
->channel
,
3167 rport
->scsi_target_id
, SCAN_WILD_CARD
, 1);
3170 spin_lock_irqsave(shost
->host_lock
, flags
);
3171 rport
->flags
&= ~FC_RPORT_SCAN_PENDING
;
3172 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3176 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3177 * @cmnd: SCSI command that scsi_eh is trying to recover
3179 * This routine can be called from a FC LLD scsi_eh callback. It
3180 * blocks the scsi_eh thread until the fc_rport leaves the
3181 * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
3182 * failing recovery actions for blocked rports which would lead to
3183 * offlined SCSI devices.
3185 void fc_block_scsi_eh(struct scsi_cmnd
*cmnd
)
3187 struct Scsi_Host
*shost
= cmnd
->device
->host
;
3188 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
3189 unsigned long flags
;
3191 spin_lock_irqsave(shost
->host_lock
, flags
);
3192 while (rport
->port_state
== FC_PORTSTATE_BLOCKED
) {
3193 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3195 spin_lock_irqsave(shost
->host_lock
, flags
);
3197 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3199 EXPORT_SYMBOL(fc_block_scsi_eh
);
3202 * fc_vport_setup - allocates and creates a FC virtual port.
3203 * @shost: scsi host the virtual port is connected to.
3204 * @channel: Channel on shost port connected to.
3205 * @pdev: parent device for vport
3206 * @ids: The world wide names, FC4 port roles, etc for
3208 * @ret_vport: The pointer to the created vport.
3210 * Allocates and creates the vport structure, calls the parent host
3211 * to instantiate the vport, the completes w/ class and sysfs creation.
3214 * This routine assumes no locks are held on entry.
3217 fc_vport_setup(struct Scsi_Host
*shost
, int channel
, struct device
*pdev
,
3218 struct fc_vport_identifiers
*ids
, struct fc_vport
**ret_vport
)
3220 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3221 struct fc_internal
*fci
= to_fc_internal(shost
->transportt
);
3222 struct fc_vport
*vport
;
3224 unsigned long flags
;
3230 if ( ! fci
->f
->vport_create
)
3233 size
= (sizeof(struct fc_vport
) + fci
->f
->dd_fcvport_size
);
3234 vport
= kzalloc(size
, GFP_KERNEL
);
3235 if (unlikely(!vport
)) {
3236 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
3240 vport
->vport_state
= FC_VPORT_UNKNOWN
;
3241 vport
->vport_last_state
= FC_VPORT_UNKNOWN
;
3242 vport
->node_name
= ids
->node_name
;
3243 vport
->port_name
= ids
->port_name
;
3244 vport
->roles
= ids
->roles
;
3245 vport
->vport_type
= ids
->vport_type
;
3246 if (fci
->f
->dd_fcvport_size
)
3247 vport
->dd_data
= &vport
[1];
3248 vport
->shost
= shost
;
3249 vport
->channel
= channel
;
3250 vport
->flags
= FC_VPORT_CREATING
;
3251 INIT_WORK(&vport
->vport_delete_work
, fc_vport_sched_delete
);
3253 spin_lock_irqsave(shost
->host_lock
, flags
);
3255 if (fc_host
->npiv_vports_inuse
>= fc_host
->max_npiv_vports
) {
3256 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3260 fc_host
->npiv_vports_inuse
++;
3261 vport
->number
= fc_host
->next_vport_number
++;
3262 list_add_tail(&vport
->peers
, &fc_host
->vports
);
3263 get_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3265 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3268 device_initialize(dev
); /* takes self reference */
3269 dev
->parent
= get_device(pdev
); /* takes parent reference */
3270 dev
->release
= fc_vport_dev_release
;
3271 dev_set_name(dev
, "vport-%d:%d-%d",
3272 shost
->host_no
, channel
, vport
->number
);
3273 transport_setup_device(dev
);
3275 error
= device_add(dev
);
3277 printk(KERN_ERR
"FC Virtual Port device_add failed\n");
3280 transport_add_device(dev
);
3281 transport_configure_device(dev
);
3283 error
= fci
->f
->vport_create(vport
, ids
->disable
);
3285 printk(KERN_ERR
"FC Virtual Port LLDD Create failed\n");
3286 goto delete_vport_all
;
3290 * if the parent isn't the physical adapter's Scsi_Host, ensure
3291 * the Scsi_Host at least contains ia symlink to the vport.
3293 if (pdev
!= &shost
->shost_gendev
) {
3294 error
= sysfs_create_link(&shost
->shost_gendev
.kobj
,
3295 &dev
->kobj
, dev_name(dev
));
3298 "%s: Cannot create vport symlinks for "
3300 __func__
, dev_name(dev
), error
);
3302 spin_lock_irqsave(shost
->host_lock
, flags
);
3303 vport
->flags
&= ~FC_VPORT_CREATING
;
3304 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3306 dev_printk(KERN_NOTICE
, pdev
,
3307 "%s created via shost%d channel %d\n", dev_name(dev
),
3308 shost
->host_no
, channel
);
3315 transport_remove_device(dev
);
3318 transport_destroy_device(dev
);
3319 spin_lock_irqsave(shost
->host_lock
, flags
);
3320 list_del(&vport
->peers
);
3321 put_device(&shost
->shost_gendev
); /* for fc_host->vport list */
3322 fc_host
->npiv_vports_inuse
--;
3323 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3324 put_device(dev
->parent
);
3331 * fc_vport_create - Admin App or LLDD requests creation of a vport
3332 * @shost: scsi host the virtual port is connected to.
3333 * @channel: channel on shost port connected to.
3334 * @ids: The world wide names, FC4 port roles, etc for
3338 * This routine assumes no locks are held on entry.
3341 fc_vport_create(struct Scsi_Host
*shost
, int channel
,
3342 struct fc_vport_identifiers
*ids
)
3345 struct fc_vport
*vport
;
3347 stat
= fc_vport_setup(shost
, channel
, &shost
->shost_gendev
,
3349 return stat
? NULL
: vport
;
3351 EXPORT_SYMBOL(fc_vport_create
);
3354 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3355 * @vport: fc_vport to be terminated
3357 * Calls the LLDD vport_delete() function, then deallocates and removes
3358 * the vport from the shost and object tree.
3361 * This routine assumes no locks are held on entry.
3364 fc_vport_terminate(struct fc_vport
*vport
)
3366 struct Scsi_Host
*shost
= vport_to_shost(vport
);
3367 struct fc_host_attrs
*fc_host
= shost_to_fc_host(shost
);
3368 struct fc_internal
*i
= to_fc_internal(shost
->transportt
);
3369 struct device
*dev
= &vport
->dev
;
3370 unsigned long flags
;
3373 spin_lock_irqsave(shost
->host_lock
, flags
);
3374 if (vport
->flags
& FC_VPORT_CREATING
) {
3375 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3378 if (vport
->flags
& (FC_VPORT_DEL
)) {
3379 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3382 vport
->flags
|= FC_VPORT_DELETING
;
3383 spin_unlock_irqrestore(shost
->host_lock
, 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
);