1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * FiberChannel transport specific attributes exported to sysfs.
5 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
9 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
10 * Rewrite for host, target, device, and remote port attributes,
11 * statistics, and service functions...
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/bsg-lib.h>
20 #include <scsi/scsi_device.h>
21 #include <scsi/scsi_host.h>
22 #include <scsi/scsi_transport.h>
23 #include <scsi/scsi_transport_fc.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <net/netlink.h>
26 #include <scsi/scsi_netlink_fc.h>
27 #include <scsi/scsi_bsg_fc.h>
28 #include "scsi_priv.h"
30 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
31 static void fc_vport_sched_delete(struct work_struct *work);
32 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
33 struct device *pdev, struct fc_vport_identifiers *ids,
34 struct fc_vport **vport);
35 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
36 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
37 static void fc_bsg_remove(struct request_queue *);
38 static void fc_bsg_goose_queue(struct fc_rport *);
45 * dev_loss_tmo: the default number of seconds that the FC transport
46 * should insulate the loss of a remote port.
47 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
49 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
51 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
52 MODULE_PARM_DESC(dev_loss_tmo,
53 "Maximum number of seconds that the FC transport should"
54 " insulate the loss of a remote port. Once this value is"
55 " exceeded, the scsi target is removed. Value should be"
56 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
57 " fast_io_fail_tmo is not set.");
60 * Redefine so that we can have same named attributes in the
61 * sdev/starget/host objects.
63 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
64 struct device_attribute device_attr_##_prefix##_##_name = \
65 __ATTR(_name,_mode,_show,_store)
67 #define fc_enum_name_search(title, table_type, table) \
68 static const char *get_fc_##title##_name(enum table_type table_key) \
73 for (i = 0; i < ARRAY_SIZE(table); i++) { \
74 if (table[i].value == table_key) { \
75 name = table[i].name; \
82 #define fc_enum_name_match(title, table_type, table) \
83 static int get_fc_##title##_match(const char *table_key, \
84 enum table_type *value) \
88 for (i = 0; i < ARRAY_SIZE(table); i++) { \
89 if (strncmp(table_key, table[i].name, \
90 table[i].matchlen) == 0) { \
91 *value = table[i].value; \
92 return 0; /* success */ \
95 return 1; /* failure */ \
99 /* Convert fc_port_type values to ascii string name */
101 enum fc_port_type value;
103 } fc_port_type_names[] = {
104 { FC_PORTTYPE_UNKNOWN, "Unknown" },
105 { FC_PORTTYPE_OTHER, "Other" },
106 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
107 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
108 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
109 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
110 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
111 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
113 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
114 #define FC_PORTTYPE_MAX_NAMELEN 50
116 /* Reuse fc_port_type enum function for vport_type */
117 #define get_fc_vport_type_name get_fc_port_type_name
120 /* Convert fc_host_event_code values to ascii string name */
121 static const struct {
122 enum fc_host_event_code value;
124 } fc_host_event_code_names[] = {
125 { FCH_EVT_LIP, "lip" },
126 { FCH_EVT_LINKUP, "link_up" },
127 { FCH_EVT_LINKDOWN, "link_down" },
128 { FCH_EVT_LIPRESET, "lip_reset" },
129 { FCH_EVT_RSCN, "rscn" },
130 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
131 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
132 { FCH_EVT_PORT_ONLINE, "port_online" },
133 { FCH_EVT_PORT_OFFLINE, "port_offline" },
134 { FCH_EVT_PORT_FABRIC, "port_fabric" },
135 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
136 { FCH_EVT_LINK_FPIN, "link_FPIN" },
137 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
139 fc_enum_name_search(host_event_code, fc_host_event_code,
140 fc_host_event_code_names)
141 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
144 /* Convert fc_port_state values to ascii string name */
146 enum fc_port_state value;
148 } fc_port_state_names[] = {
149 { FC_PORTSTATE_UNKNOWN, "Unknown" },
150 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
151 { FC_PORTSTATE_ONLINE, "Online" },
152 { FC_PORTSTATE_OFFLINE, "Offline" },
153 { FC_PORTSTATE_BLOCKED, "Blocked" },
154 { FC_PORTSTATE_BYPASSED, "Bypassed" },
155 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
156 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
157 { FC_PORTSTATE_ERROR, "Error" },
158 { FC_PORTSTATE_LOOPBACK, "Loopback" },
159 { FC_PORTSTATE_DELETED, "Deleted" },
161 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
162 #define FC_PORTSTATE_MAX_NAMELEN 20
165 /* Convert fc_vport_state values to ascii string name */
167 enum fc_vport_state value;
169 } fc_vport_state_names[] = {
170 { FC_VPORT_UNKNOWN, "Unknown" },
171 { FC_VPORT_ACTIVE, "Active" },
172 { FC_VPORT_DISABLED, "Disabled" },
173 { FC_VPORT_LINKDOWN, "Linkdown" },
174 { FC_VPORT_INITIALIZING, "Initializing" },
175 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
176 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
177 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
178 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
179 { FC_VPORT_FAILED, "VPort Failed" },
181 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
182 #define FC_VPORTSTATE_MAX_NAMELEN 24
184 /* Reuse fc_vport_state enum function for vport_last_state */
185 #define get_fc_vport_last_state_name get_fc_vport_state_name
188 /* Convert fc_tgtid_binding_type values to ascii string name */
189 static const struct {
190 enum fc_tgtid_binding_type value;
193 } fc_tgtid_binding_type_names[] = {
194 { FC_TGTID_BIND_NONE, "none", 4 },
195 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
196 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
197 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
199 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
200 fc_tgtid_binding_type_names)
201 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
202 fc_tgtid_binding_type_names)
203 #define FC_BINDTYPE_MAX_NAMELEN 30
206 #define fc_bitfield_name_search(title, table) \
208 get_fc_##title##_names(u32 table_key, char *buf) \
214 for (i = 0; i < ARRAY_SIZE(table); i++) { \
215 if (table[i].value & table_key) { \
216 len += sprintf(buf + len, "%s%s", \
217 prefix, table[i].name); \
221 len += sprintf(buf + len, "\n"); \
226 /* Convert FC_COS bit values to ascii string name */
227 static const struct {
231 { FC_COS_CLASS1, "Class 1" },
232 { FC_COS_CLASS2, "Class 2" },
233 { FC_COS_CLASS3, "Class 3" },
234 { FC_COS_CLASS4, "Class 4" },
235 { FC_COS_CLASS6, "Class 6" },
237 fc_bitfield_name_search(cos, fc_cos_names)
240 /* Convert FC_PORTSPEED bit values to ascii string name */
241 static const struct {
244 } fc_port_speed_names[] = {
245 { FC_PORTSPEED_1GBIT, "1 Gbit" },
246 { FC_PORTSPEED_2GBIT, "2 Gbit" },
247 { FC_PORTSPEED_4GBIT, "4 Gbit" },
248 { FC_PORTSPEED_10GBIT, "10 Gbit" },
249 { FC_PORTSPEED_8GBIT, "8 Gbit" },
250 { FC_PORTSPEED_16GBIT, "16 Gbit" },
251 { FC_PORTSPEED_32GBIT, "32 Gbit" },
252 { FC_PORTSPEED_20GBIT, "20 Gbit" },
253 { FC_PORTSPEED_40GBIT, "40 Gbit" },
254 { FC_PORTSPEED_50GBIT, "50 Gbit" },
255 { FC_PORTSPEED_100GBIT, "100 Gbit" },
256 { FC_PORTSPEED_25GBIT, "25 Gbit" },
257 { FC_PORTSPEED_64GBIT, "64 Gbit" },
258 { FC_PORTSPEED_128GBIT, "128 Gbit" },
259 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
261 fc_bitfield_name_search(port_speed, fc_port_speed_names)
265 show_fc_fc4s (char *buf, u8 *fc4_list)
269 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
270 len += sprintf(buf + len , "0x%02x ", *fc4_list);
271 len += sprintf(buf + len, "\n");
276 /* Convert FC_PORT_ROLE bit values to ascii string name */
277 static const struct {
280 } fc_port_role_names[] = {
281 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
282 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
283 { FC_PORT_ROLE_IP_PORT, "IP Port" },
284 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
285 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
286 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
287 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
289 fc_bitfield_name_search(port_roles, fc_port_role_names)
292 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
294 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
295 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
296 #define FC_FPORT_PORTID 0x00000e
297 #define FC_FABCTLR_PORTID 0x00000d
298 #define FC_DIRSRVR_PORTID 0x00000c
299 #define FC_TIMESRVR_PORTID 0x00000b
300 #define FC_MGMTSRVR_PORTID 0x00000a
303 static void fc_timeout_deleted_rport(struct work_struct *work);
304 static void fc_timeout_fail_rport_io(struct work_struct *work);
305 static void fc_scsi_scan_rport(struct work_struct *work);
308 * Attribute counts pre object type...
309 * Increase these values if you add attributes
311 #define FC_STARGET_NUM_ATTRS 3
312 #define FC_RPORT_NUM_ATTRS 10
313 #define FC_VPORT_NUM_ATTRS 9
314 #define FC_HOST_NUM_ATTRS 29
317 struct scsi_transport_template t;
318 struct fc_function_template *f;
321 * For attributes : each object has :
322 * An array of the actual attributes structures
323 * An array of null-terminated pointers to the attribute
324 * structures - used for mid-layer interaction.
326 * The attribute containers for the starget and host are are
327 * part of the midlayer. As the remote port is specific to the
328 * fc transport, we must provide the attribute container.
330 struct device_attribute private_starget_attrs[
331 FC_STARGET_NUM_ATTRS];
332 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
334 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
335 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
337 struct transport_container rport_attr_cont;
338 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
339 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
341 struct transport_container vport_attr_cont;
342 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
343 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
346 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
348 static int fc_target_setup(struct transport_container *tc, struct device *dev,
351 struct scsi_target *starget = to_scsi_target(dev);
352 struct fc_rport *rport = starget_to_rport(starget);
355 * if parent is remote port, use values from remote port.
356 * Otherwise, this host uses the fc_transport, but not the
357 * remote port interface. As such, initialize to known non-values.
360 fc_starget_node_name(starget) = rport->node_name;
361 fc_starget_port_name(starget) = rport->port_name;
362 fc_starget_port_id(starget) = rport->port_id;
364 fc_starget_node_name(starget) = -1;
365 fc_starget_port_name(starget) = -1;
366 fc_starget_port_id(starget) = -1;
372 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
378 static int fc_host_setup(struct transport_container *tc, struct device *dev,
381 struct Scsi_Host *shost = dev_to_shost(dev);
382 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
385 * Set default values easily detected by the midlayer as
386 * failure cases. The scsi lldd is responsible for initializing
387 * all transport attributes to valid values per host.
389 fc_host->node_name = -1;
390 fc_host->port_name = -1;
391 fc_host->permanent_port_name = -1;
392 fc_host->supported_classes = FC_COS_UNSPECIFIED;
393 memset(fc_host->supported_fc4s, 0,
394 sizeof(fc_host->supported_fc4s));
395 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
396 fc_host->maxframe_size = -1;
397 fc_host->max_npiv_vports = 0;
398 memset(fc_host->serial_number, 0,
399 sizeof(fc_host->serial_number));
400 memset(fc_host->manufacturer, 0,
401 sizeof(fc_host->manufacturer));
402 memset(fc_host->model, 0,
403 sizeof(fc_host->model));
404 memset(fc_host->model_description, 0,
405 sizeof(fc_host->model_description));
406 memset(fc_host->hardware_version, 0,
407 sizeof(fc_host->hardware_version));
408 memset(fc_host->driver_version, 0,
409 sizeof(fc_host->driver_version));
410 memset(fc_host->firmware_version, 0,
411 sizeof(fc_host->firmware_version));
412 memset(fc_host->optionrom_version, 0,
413 sizeof(fc_host->optionrom_version));
415 fc_host->port_id = -1;
416 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
417 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
418 memset(fc_host->active_fc4s, 0,
419 sizeof(fc_host->active_fc4s));
420 fc_host->speed = FC_PORTSPEED_UNKNOWN;
421 fc_host->fabric_name = -1;
422 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
423 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
425 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
427 INIT_LIST_HEAD(&fc_host->rports);
428 INIT_LIST_HEAD(&fc_host->rport_bindings);
429 INIT_LIST_HEAD(&fc_host->vports);
430 fc_host->next_rport_number = 0;
431 fc_host->next_target_id = 0;
432 fc_host->next_vport_number = 0;
433 fc_host->npiv_vports_inuse = 0;
435 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
436 "fc_wq_%d", shost->host_no);
437 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
438 if (!fc_host->work_q)
441 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
442 snprintf(fc_host->devloss_work_q_name,
443 sizeof(fc_host->devloss_work_q_name),
444 "fc_dl_%d", shost->host_no);
445 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
446 fc_host->devloss_work_q_name);
447 if (!fc_host->devloss_work_q) {
448 destroy_workqueue(fc_host->work_q);
449 fc_host->work_q = NULL;
453 fc_bsg_hostadd(shost, fc_host);
454 /* ignore any bsg add error - we just can't do sgio */
459 static int fc_host_remove(struct transport_container *tc, struct device *dev,
462 struct Scsi_Host *shost = dev_to_shost(dev);
463 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
465 fc_bsg_remove(fc_host->rqst_q);
469 static DECLARE_TRANSPORT_CLASS(fc_host_class,
476 * Setup and Remove actions for remote ports are handled
477 * in the service functions below.
479 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
486 * Setup and Remove actions for virtual ports are handled
487 * in the service functions below.
489 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
496 * Netlink Infrastructure
499 static atomic_t fc_event_seq;
502 * fc_get_event_number - Obtain the next sequential FC event number
505 * We could have inlined this, but it would have required fc_event_seq to
506 * be exposed. For now, live with the subroutine call.
507 * Atomic used to avoid lock/unlock...
510 fc_get_event_number(void)
512 return atomic_add_return(1, &fc_event_seq);
514 EXPORT_SYMBOL(fc_get_event_number);
517 * fc_host_post_fc_event - routine to do the work of posting an event
519 * @shost: host the event occurred on
520 * @event_number: fc event number obtained from get_fc_event_number()
521 * @event_code: fc_host event being posted
522 * @data_len: amount, in bytes, of event data
523 * @data_buf: pointer to event data
524 * @vendor_id: value for Vendor id
527 * This routine assumes no locks are held on entry.
530 fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
531 enum fc_host_event_code event_code,
532 u32 data_len, char *data_buf, u64 vendor_id)
535 struct nlmsghdr *nlh;
536 struct fc_nl_event *event;
541 if (!data_buf || data_len < 4)
549 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
551 skb = nlmsg_new(len, GFP_KERNEL);
557 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
562 event = nlmsg_data(nlh);
564 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
565 FC_NL_ASYNC_EVENT, len);
566 event->seconds = ktime_get_real_seconds();
567 event->vendor_id = vendor_id;
568 event->host_no = shost->host_no;
569 event->event_datalen = data_len; /* bytes */
570 event->event_num = event_number;
571 event->event_code = event_code;
573 memcpy(&event->event_data, data_buf, data_len);
575 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
582 name = get_fc_host_event_code_name(event_code);
584 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
585 __func__, shost->host_no,
586 (name) ? name : "<unknown>",
587 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
590 EXPORT_SYMBOL(fc_host_post_fc_event);
593 * fc_host_post_event - called to post an even on an fc_host.
594 * @shost: host the event occurred on
595 * @event_number: fc event number obtained from get_fc_event_number()
596 * @event_code: fc_host event being posted
597 * @event_data: 32bits of data for the event being posted
600 * This routine assumes no locks are held on entry.
603 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
604 enum fc_host_event_code event_code, u32 event_data)
606 fc_host_post_fc_event(shost, event_number, event_code,
607 (u32)sizeof(u32), (char *)&event_data, 0);
609 EXPORT_SYMBOL(fc_host_post_event);
613 * fc_host_post_vendor_event - called to post a vendor unique event
615 * @shost: host the event occurred on
616 * @event_number: fc event number obtained from get_fc_event_number()
617 * @data_len: amount, in bytes, of vendor unique data
618 * @data_buf: pointer to vendor unique data
619 * @vendor_id: Vendor id
622 * This routine assumes no locks are held on entry.
625 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
626 u32 data_len, char * data_buf, u64 vendor_id)
628 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
629 data_len, data_buf, vendor_id);
631 EXPORT_SYMBOL(fc_host_post_vendor_event);
634 * fc_host_rcv_fpin - routine to process a received FPIN.
635 * @shost: host the FPIN was received on
636 * @fpin_len: length of FPIN payload, in bytes
637 * @fpin_buf: pointer to FPIN payload
640 * This routine assumes no locks are held on entry.
643 fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf)
645 fc_host_post_fc_event(shost, fc_get_event_number(),
646 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0);
648 EXPORT_SYMBOL(fc_host_fpin_rcv);
651 static __init int fc_transport_init(void)
655 atomic_set(&fc_event_seq, 0);
657 error = transport_class_register(&fc_host_class);
660 error = transport_class_register(&fc_vport_class);
662 goto unreg_host_class;
663 error = transport_class_register(&fc_rport_class);
665 goto unreg_vport_class;
666 error = transport_class_register(&fc_transport_class);
668 goto unreg_rport_class;
672 transport_class_unregister(&fc_rport_class);
674 transport_class_unregister(&fc_vport_class);
676 transport_class_unregister(&fc_host_class);
680 static void __exit fc_transport_exit(void)
682 transport_class_unregister(&fc_transport_class);
683 transport_class_unregister(&fc_rport_class);
684 transport_class_unregister(&fc_host_class);
685 transport_class_unregister(&fc_vport_class);
689 * FC Remote Port Attribute Management
692 #define fc_rport_show_function(field, format_string, sz, cast) \
694 show_fc_rport_##field (struct device *dev, \
695 struct device_attribute *attr, char *buf) \
697 struct fc_rport *rport = transport_class_to_rport(dev); \
698 struct Scsi_Host *shost = rport_to_shost(rport); \
699 struct fc_internal *i = to_fc_internal(shost->transportt); \
700 if ((i->f->get_rport_##field) && \
701 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
702 (rport->port_state == FC_PORTSTATE_DELETED) || \
703 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
704 i->f->get_rport_##field(rport); \
705 return snprintf(buf, sz, format_string, cast rport->field); \
708 #define fc_rport_store_function(field) \
710 store_fc_rport_##field(struct device *dev, \
711 struct device_attribute *attr, \
712 const char *buf, size_t count) \
715 struct fc_rport *rport = transport_class_to_rport(dev); \
716 struct Scsi_Host *shost = rport_to_shost(rport); \
717 struct fc_internal *i = to_fc_internal(shost->transportt); \
719 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
720 (rport->port_state == FC_PORTSTATE_DELETED) || \
721 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
723 val = simple_strtoul(buf, &cp, 0); \
724 if (*cp && (*cp != '\n')) \
726 i->f->set_rport_##field(rport, val); \
730 #define fc_rport_rd_attr(field, format_string, sz) \
731 fc_rport_show_function(field, format_string, sz, ) \
732 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
733 show_fc_rport_##field, NULL)
735 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
736 fc_rport_show_function(field, format_string, sz, (cast)) \
737 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
738 show_fc_rport_##field, NULL)
740 #define fc_rport_rw_attr(field, format_string, sz) \
741 fc_rport_show_function(field, format_string, sz, ) \
742 fc_rport_store_function(field) \
743 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
744 show_fc_rport_##field, \
745 store_fc_rport_##field)
748 #define fc_private_rport_show_function(field, format_string, sz, cast) \
750 show_fc_rport_##field (struct device *dev, \
751 struct device_attribute *attr, char *buf) \
753 struct fc_rport *rport = transport_class_to_rport(dev); \
754 return snprintf(buf, sz, format_string, cast rport->field); \
757 #define fc_private_rport_rd_attr(field, format_string, sz) \
758 fc_private_rport_show_function(field, format_string, sz, ) \
759 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
760 show_fc_rport_##field, NULL)
762 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
763 fc_private_rport_show_function(field, format_string, sz, (cast)) \
764 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
765 show_fc_rport_##field, NULL)
768 #define fc_private_rport_rd_enum_attr(title, maxlen) \
770 show_fc_rport_##title (struct device *dev, \
771 struct device_attribute *attr, char *buf) \
773 struct fc_rport *rport = transport_class_to_rport(dev); \
775 name = get_fc_##title##_name(rport->title); \
778 return snprintf(buf, maxlen, "%s\n", name); \
780 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
781 show_fc_rport_##title, NULL)
784 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
785 i->private_rport_attrs[count] = device_attr_rport_##field; \
786 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
787 i->private_rport_attrs[count].store = NULL; \
788 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
789 if (i->f->show_rport_##field) \
792 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
793 i->private_rport_attrs[count] = device_attr_rport_##field; \
794 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
795 i->private_rport_attrs[count].store = NULL; \
796 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
799 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
800 i->private_rport_attrs[count] = device_attr_rport_##field; \
801 if (!i->f->set_rport_##field) { \
802 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
803 i->private_rport_attrs[count].store = NULL; \
805 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
806 if (i->f->show_rport_##field) \
809 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
811 i->private_rport_attrs[count] = device_attr_rport_##field; \
812 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
817 /* The FC Transport Remote Port Attributes: */
819 /* Fixed Remote Port Attributes */
821 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
824 show_fc_rport_supported_classes (struct device *dev,
825 struct device_attribute *attr, char *buf)
827 struct fc_rport *rport = transport_class_to_rport(dev);
828 if (rport->supported_classes == FC_COS_UNSPECIFIED)
829 return snprintf(buf, 20, "unspecified\n");
830 return get_fc_cos_names(rport->supported_classes, buf);
832 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
833 show_fc_rport_supported_classes, NULL);
835 /* Dynamic Remote Port Attributes */
838 * dev_loss_tmo attribute
840 static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
844 *val = simple_strtoul(buf, &cp, 0);
845 if (*cp && (*cp != '\n'))
848 * Check for overflow; dev_loss_tmo is u32
856 static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
859 struct Scsi_Host *shost = rport_to_shost(rport);
860 struct fc_internal *i = to_fc_internal(shost->transportt);
862 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
863 (rport->port_state == FC_PORTSTATE_DELETED) ||
864 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
867 * Check for overflow; dev_loss_tmo is u32
873 * If fast_io_fail is off we have to cap
874 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
876 if (rport->fast_io_fail_tmo == -1 &&
877 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
880 i->f->set_rport_dev_loss_tmo(rport, val);
884 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
886 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
887 const char *buf, size_t count)
889 struct fc_rport *rport = transport_class_to_rport(dev);
893 rc = fc_str_to_dev_loss(buf, &val);
897 rc = fc_rport_set_dev_loss_tmo(rport, val);
902 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
903 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
906 /* Private Remote Port Attributes */
908 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
909 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
910 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
913 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
916 struct fc_rport *rport = transport_class_to_rport(dev);
918 /* identify any roles that are port_id specific */
919 if ((rport->port_id != -1) &&
920 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
921 FC_WELLKNOWN_PORTID_MASK) {
922 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
923 case FC_FPORT_PORTID:
924 return snprintf(buf, 30, "Fabric Port\n");
925 case FC_FABCTLR_PORTID:
926 return snprintf(buf, 30, "Fabric Controller\n");
927 case FC_DIRSRVR_PORTID:
928 return snprintf(buf, 30, "Directory Server\n");
929 case FC_TIMESRVR_PORTID:
930 return snprintf(buf, 30, "Time Server\n");
931 case FC_MGMTSRVR_PORTID:
932 return snprintf(buf, 30, "Management Server\n");
934 return snprintf(buf, 30, "Unknown Fabric Entity\n");
937 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
938 return snprintf(buf, 20, "unknown\n");
939 return get_fc_port_roles_names(rport->roles, buf);
942 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
943 show_fc_rport_roles, NULL);
945 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
946 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
949 * fast_io_fail_tmo attribute
952 show_fc_rport_fast_io_fail_tmo (struct device *dev,
953 struct device_attribute *attr, char *buf)
955 struct fc_rport *rport = transport_class_to_rport(dev);
957 if (rport->fast_io_fail_tmo == -1)
958 return snprintf(buf, 5, "off\n");
959 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
963 store_fc_rport_fast_io_fail_tmo(struct device *dev,
964 struct device_attribute *attr, const char *buf,
969 struct fc_rport *rport = transport_class_to_rport(dev);
971 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
972 (rport->port_state == FC_PORTSTATE_DELETED) ||
973 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
975 if (strncmp(buf, "off", 3) == 0)
976 rport->fast_io_fail_tmo = -1;
978 val = simple_strtoul(buf, &cp, 0);
979 if ((*cp && (*cp != '\n')) || (val < 0))
982 * Cap fast_io_fail by dev_loss_tmo or
983 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
985 if ((val >= rport->dev_loss_tmo) ||
986 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
989 rport->fast_io_fail_tmo = val;
993 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
994 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
998 * FC SCSI Target Attribute Management
1002 * Note: in the target show function we recognize when the remote
1003 * port is in the hierarchy and do not allow the driver to get
1004 * involved in sysfs functions. The driver only gets involved if
1005 * it's the "old" style that doesn't use rports.
1007 #define fc_starget_show_function(field, format_string, sz, cast) \
1009 show_fc_starget_##field (struct device *dev, \
1010 struct device_attribute *attr, char *buf) \
1012 struct scsi_target *starget = transport_class_to_starget(dev); \
1013 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1014 struct fc_internal *i = to_fc_internal(shost->transportt); \
1015 struct fc_rport *rport = starget_to_rport(starget); \
1017 fc_starget_##field(starget) = rport->field; \
1018 else if (i->f->get_starget_##field) \
1019 i->f->get_starget_##field(starget); \
1020 return snprintf(buf, sz, format_string, \
1021 cast fc_starget_##field(starget)); \
1024 #define fc_starget_rd_attr(field, format_string, sz) \
1025 fc_starget_show_function(field, format_string, sz, ) \
1026 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1027 show_fc_starget_##field, NULL)
1029 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1030 fc_starget_show_function(field, format_string, sz, (cast)) \
1031 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1032 show_fc_starget_##field, NULL)
1034 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
1035 i->private_starget_attrs[count] = device_attr_starget_##field; \
1036 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1037 i->private_starget_attrs[count].store = NULL; \
1038 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1039 if (i->f->show_starget_##field) \
1042 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
1043 i->private_starget_attrs[count] = device_attr_starget_##field; \
1044 if (!i->f->set_starget_##field) { \
1045 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1046 i->private_starget_attrs[count].store = NULL; \
1048 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1049 if (i->f->show_starget_##field) \
1052 /* The FC Transport SCSI Target Attributes: */
1053 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1054 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1055 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1059 * FC Virtual Port Attribute Management
1062 #define fc_vport_show_function(field, format_string, sz, cast) \
1064 show_fc_vport_##field (struct device *dev, \
1065 struct device_attribute *attr, char *buf) \
1067 struct fc_vport *vport = transport_class_to_vport(dev); \
1068 struct Scsi_Host *shost = vport_to_shost(vport); \
1069 struct fc_internal *i = to_fc_internal(shost->transportt); \
1070 if ((i->f->get_vport_##field) && \
1071 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1072 i->f->get_vport_##field(vport); \
1073 return snprintf(buf, sz, format_string, cast vport->field); \
1076 #define fc_vport_store_function(field) \
1078 store_fc_vport_##field(struct device *dev, \
1079 struct device_attribute *attr, \
1080 const char *buf, size_t count) \
1083 struct fc_vport *vport = transport_class_to_vport(dev); \
1084 struct Scsi_Host *shost = vport_to_shost(vport); \
1085 struct fc_internal *i = to_fc_internal(shost->transportt); \
1087 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1089 val = simple_strtoul(buf, &cp, 0); \
1090 if (*cp && (*cp != '\n')) \
1092 i->f->set_vport_##field(vport, val); \
1096 #define fc_vport_store_str_function(field, slen) \
1098 store_fc_vport_##field(struct device *dev, \
1099 struct device_attribute *attr, \
1100 const char *buf, size_t count) \
1102 struct fc_vport *vport = transport_class_to_vport(dev); \
1103 struct Scsi_Host *shost = vport_to_shost(vport); \
1104 struct fc_internal *i = to_fc_internal(shost->transportt); \
1105 unsigned int cnt=count; \
1107 /* count may include a LF at end of string */ \
1108 if (buf[cnt-1] == '\n') \
1110 if (cnt > ((slen) - 1)) \
1112 memcpy(vport->field, buf, cnt); \
1113 i->f->set_vport_##field(vport); \
1117 #define fc_vport_rd_attr(field, format_string, sz) \
1118 fc_vport_show_function(field, format_string, sz, ) \
1119 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1120 show_fc_vport_##field, NULL)
1122 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1123 fc_vport_show_function(field, format_string, sz, (cast)) \
1124 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1125 show_fc_vport_##field, NULL)
1127 #define fc_vport_rw_attr(field, format_string, sz) \
1128 fc_vport_show_function(field, format_string, sz, ) \
1129 fc_vport_store_function(field) \
1130 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1131 show_fc_vport_##field, \
1132 store_fc_vport_##field)
1134 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1136 show_fc_vport_##field (struct device *dev, \
1137 struct device_attribute *attr, char *buf) \
1139 struct fc_vport *vport = transport_class_to_vport(dev); \
1140 return snprintf(buf, sz, format_string, cast vport->field); \
1143 #define fc_private_vport_store_u32_function(field) \
1145 store_fc_vport_##field(struct device *dev, \
1146 struct device_attribute *attr, \
1147 const char *buf, size_t count) \
1150 struct fc_vport *vport = transport_class_to_vport(dev); \
1152 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1154 val = simple_strtoul(buf, &cp, 0); \
1155 if (*cp && (*cp != '\n')) \
1157 vport->field = val; \
1162 #define fc_private_vport_rd_attr(field, format_string, sz) \
1163 fc_private_vport_show_function(field, format_string, sz, ) \
1164 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1165 show_fc_vport_##field, NULL)
1167 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1168 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1169 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1170 show_fc_vport_##field, NULL)
1172 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1173 fc_private_vport_show_function(field, format_string, sz, ) \
1174 fc_private_vport_store_u32_function(field) \
1175 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1176 show_fc_vport_##field, \
1177 store_fc_vport_##field)
1180 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1182 show_fc_vport_##title (struct device *dev, \
1183 struct device_attribute *attr, \
1186 struct fc_vport *vport = transport_class_to_vport(dev); \
1188 name = get_fc_##title##_name(vport->title); \
1191 return snprintf(buf, maxlen, "%s\n", name); \
1193 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1194 show_fc_vport_##title, NULL)
1197 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1198 i->private_vport_attrs[count] = device_attr_vport_##field; \
1199 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1200 i->private_vport_attrs[count].store = NULL; \
1201 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1202 if (i->f->get_##field) \
1204 /* NOTE: Above MACRO differs: checks function not show bit */
1206 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1207 i->private_vport_attrs[count] = device_attr_vport_##field; \
1208 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1209 i->private_vport_attrs[count].store = NULL; \
1210 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1213 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1214 i->private_vport_attrs[count] = device_attr_vport_##field; \
1215 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1218 /* NOTE: Above MACRO differs: checks function */
1220 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1221 i->private_vport_attrs[count] = device_attr_vport_##field; \
1222 if (!i->f->set_vport_##field) { \
1223 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1224 i->private_vport_attrs[count].store = NULL; \
1226 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1228 /* NOTE: Above MACRO differs: does not check show bit */
1230 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1232 i->private_vport_attrs[count] = device_attr_vport_##field; \
1233 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1238 /* The FC Transport Virtual Port Attributes: */
1240 /* Fixed Virtual Port Attributes */
1242 /* Dynamic Virtual Port Attributes */
1244 /* Private Virtual Port Attributes */
1246 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1247 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1248 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1249 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1252 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1255 struct fc_vport *vport = transport_class_to_vport(dev);
1257 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1258 return snprintf(buf, 20, "unknown\n");
1259 return get_fc_port_roles_names(vport->roles, buf);
1261 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1263 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1265 fc_private_vport_show_function(symbolic_name, "%s\n",
1266 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1267 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1268 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1269 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1272 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1273 const char *buf, size_t count)
1275 struct fc_vport *vport = transport_class_to_vport(dev);
1276 struct Scsi_Host *shost = vport_to_shost(vport);
1277 unsigned long flags;
1279 spin_lock_irqsave(shost->host_lock, flags);
1280 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) {
1281 spin_unlock_irqrestore(shost->host_lock, flags);
1284 vport->flags |= FC_VPORT_DELETING;
1285 spin_unlock_irqrestore(shost->host_lock, flags);
1287 fc_queue_work(shost, &vport->vport_delete_work);
1290 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1291 NULL, store_fc_vport_delete);
1295 * Enable/Disable vport
1296 * Write "1" to disable, write "0" to enable
1299 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1303 struct fc_vport *vport = transport_class_to_vport(dev);
1304 struct Scsi_Host *shost = vport_to_shost(vport);
1305 struct fc_internal *i = to_fc_internal(shost->transportt);
1308 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1312 if (vport->vport_state != FC_VPORT_DISABLED)
1314 } else if (*buf == '1') {
1315 if (vport->vport_state == FC_VPORT_DISABLED)
1320 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1321 return stat ? stat : count;
1323 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1324 NULL, store_fc_vport_disable);
1328 * Host Attribute Management
1331 #define fc_host_show_function(field, format_string, sz, cast) \
1333 show_fc_host_##field (struct device *dev, \
1334 struct device_attribute *attr, char *buf) \
1336 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1337 struct fc_internal *i = to_fc_internal(shost->transportt); \
1338 if (i->f->get_host_##field) \
1339 i->f->get_host_##field(shost); \
1340 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1343 #define fc_host_store_function(field) \
1345 store_fc_host_##field(struct device *dev, \
1346 struct device_attribute *attr, \
1347 const char *buf, size_t count) \
1350 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1351 struct fc_internal *i = to_fc_internal(shost->transportt); \
1354 val = simple_strtoul(buf, &cp, 0); \
1355 if (*cp && (*cp != '\n')) \
1357 i->f->set_host_##field(shost, val); \
1361 #define fc_host_store_str_function(field, slen) \
1363 store_fc_host_##field(struct device *dev, \
1364 struct device_attribute *attr, \
1365 const char *buf, size_t count) \
1367 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1368 struct fc_internal *i = to_fc_internal(shost->transportt); \
1369 unsigned int cnt=count; \
1371 /* count may include a LF at end of string */ \
1372 if (buf[cnt-1] == '\n') \
1374 if (cnt > ((slen) - 1)) \
1376 memcpy(fc_host_##field(shost), buf, cnt); \
1377 i->f->set_host_##field(shost); \
1381 #define fc_host_rd_attr(field, format_string, sz) \
1382 fc_host_show_function(field, format_string, sz, ) \
1383 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1384 show_fc_host_##field, NULL)
1386 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1387 fc_host_show_function(field, format_string, sz, (cast)) \
1388 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1389 show_fc_host_##field, NULL)
1391 #define fc_host_rw_attr(field, format_string, sz) \
1392 fc_host_show_function(field, format_string, sz, ) \
1393 fc_host_store_function(field) \
1394 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1395 show_fc_host_##field, \
1396 store_fc_host_##field)
1398 #define fc_host_rd_enum_attr(title, maxlen) \
1400 show_fc_host_##title (struct device *dev, \
1401 struct device_attribute *attr, char *buf) \
1403 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1404 struct fc_internal *i = to_fc_internal(shost->transportt); \
1406 if (i->f->get_host_##title) \
1407 i->f->get_host_##title(shost); \
1408 name = get_fc_##title##_name(fc_host_##title(shost)); \
1411 return snprintf(buf, maxlen, "%s\n", name); \
1413 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1415 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1416 i->private_host_attrs[count] = device_attr_host_##field; \
1417 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1418 i->private_host_attrs[count].store = NULL; \
1419 i->host_attrs[count] = &i->private_host_attrs[count]; \
1420 if (i->f->show_host_##field) \
1423 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1424 i->private_host_attrs[count] = device_attr_host_##field; \
1425 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1426 i->private_host_attrs[count].store = NULL; \
1427 i->host_attrs[count] = &i->private_host_attrs[count]; \
1430 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1431 i->private_host_attrs[count] = device_attr_host_##field; \
1432 if (!i->f->set_host_##field) { \
1433 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1434 i->private_host_attrs[count].store = NULL; \
1436 i->host_attrs[count] = &i->private_host_attrs[count]; \
1437 if (i->f->show_host_##field) \
1441 #define fc_private_host_show_function(field, format_string, sz, cast) \
1443 show_fc_host_##field (struct device *dev, \
1444 struct device_attribute *attr, char *buf) \
1446 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1447 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1450 #define fc_private_host_rd_attr(field, format_string, sz) \
1451 fc_private_host_show_function(field, format_string, sz, ) \
1452 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1453 show_fc_host_##field, NULL)
1455 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1456 fc_private_host_show_function(field, format_string, sz, (cast)) \
1457 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1458 show_fc_host_##field, NULL)
1460 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1461 i->private_host_attrs[count] = device_attr_host_##field; \
1462 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1463 i->private_host_attrs[count].store = NULL; \
1464 i->host_attrs[count] = &i->private_host_attrs[count]; \
1467 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1469 i->private_host_attrs[count] = device_attr_host_##field; \
1470 i->host_attrs[count] = &i->private_host_attrs[count]; \
1475 /* Fixed Host Attributes */
1478 show_fc_host_supported_classes (struct device *dev,
1479 struct device_attribute *attr, char *buf)
1481 struct Scsi_Host *shost = transport_class_to_shost(dev);
1483 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1484 return snprintf(buf, 20, "unspecified\n");
1486 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1488 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1489 show_fc_host_supported_classes, NULL);
1492 show_fc_host_supported_fc4s (struct device *dev,
1493 struct device_attribute *attr, char *buf)
1495 struct Scsi_Host *shost = transport_class_to_shost(dev);
1496 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1498 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1499 show_fc_host_supported_fc4s, NULL);
1502 show_fc_host_supported_speeds (struct device *dev,
1503 struct device_attribute *attr, char *buf)
1505 struct Scsi_Host *shost = transport_class_to_shost(dev);
1507 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1508 return snprintf(buf, 20, "unknown\n");
1510 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1512 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1513 show_fc_host_supported_speeds, NULL);
1516 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1517 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1518 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1519 unsigned long long);
1520 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1521 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1522 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1523 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1524 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1525 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1526 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1527 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1528 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1529 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1532 /* Dynamic Host Attributes */
1535 show_fc_host_active_fc4s (struct device *dev,
1536 struct device_attribute *attr, char *buf)
1538 struct Scsi_Host *shost = transport_class_to_shost(dev);
1539 struct fc_internal *i = to_fc_internal(shost->transportt);
1541 if (i->f->get_host_active_fc4s)
1542 i->f->get_host_active_fc4s(shost);
1544 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1546 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1547 show_fc_host_active_fc4s, NULL);
1550 show_fc_host_speed (struct device *dev,
1551 struct device_attribute *attr, char *buf)
1553 struct Scsi_Host *shost = transport_class_to_shost(dev);
1554 struct fc_internal *i = to_fc_internal(shost->transportt);
1556 if (i->f->get_host_speed)
1557 i->f->get_host_speed(shost);
1559 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1560 return snprintf(buf, 20, "unknown\n");
1562 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1564 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1565 show_fc_host_speed, NULL);
1568 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1569 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1570 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1571 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1572 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1574 fc_private_host_show_function(system_hostname, "%s\n",
1575 FC_SYMBOLIC_NAME_SIZE + 1, )
1576 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1577 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1578 show_fc_host_system_hostname, store_fc_host_system_hostname);
1581 /* Private Host Attributes */
1584 show_fc_private_host_tgtid_bind_type(struct device *dev,
1585 struct device_attribute *attr, char *buf)
1587 struct Scsi_Host *shost = transport_class_to_shost(dev);
1590 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1593 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1596 #define get_list_head_entry(pos, head, member) \
1597 pos = list_entry((head)->next, typeof(*pos), member)
1600 store_fc_private_host_tgtid_bind_type(struct device *dev,
1601 struct device_attribute *attr, const char *buf, size_t count)
1603 struct Scsi_Host *shost = transport_class_to_shost(dev);
1604 struct fc_rport *rport;
1605 enum fc_tgtid_binding_type val;
1606 unsigned long flags;
1608 if (get_fc_tgtid_bind_type_match(buf, &val))
1611 /* if changing bind type, purge all unused consistent bindings */
1612 if (val != fc_host_tgtid_bind_type(shost)) {
1613 spin_lock_irqsave(shost->host_lock, flags);
1614 while (!list_empty(&fc_host_rport_bindings(shost))) {
1615 get_list_head_entry(rport,
1616 &fc_host_rport_bindings(shost), peers);
1617 list_del(&rport->peers);
1618 rport->port_state = FC_PORTSTATE_DELETED;
1619 fc_queue_work(shost, &rport->rport_delete_work);
1621 spin_unlock_irqrestore(shost->host_lock, flags);
1624 fc_host_tgtid_bind_type(shost) = val;
1628 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1629 show_fc_private_host_tgtid_bind_type,
1630 store_fc_private_host_tgtid_bind_type);
1633 store_fc_private_host_issue_lip(struct device *dev,
1634 struct device_attribute *attr, const char *buf, size_t count)
1636 struct Scsi_Host *shost = transport_class_to_shost(dev);
1637 struct fc_internal *i = to_fc_internal(shost->transportt);
1640 /* ignore any data value written to the attribute */
1641 if (i->f->issue_fc_host_lip) {
1642 ret = i->f->issue_fc_host_lip(shost);
1643 return ret ? ret: count;
1649 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1650 store_fc_private_host_issue_lip);
1653 store_fc_private_host_dev_loss_tmo(struct device *dev,
1654 struct device_attribute *attr,
1655 const char *buf, size_t count)
1657 struct Scsi_Host *shost = transport_class_to_shost(dev);
1658 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1659 struct fc_rport *rport;
1660 unsigned long val, flags;
1663 rc = fc_str_to_dev_loss(buf, &val);
1667 fc_host_dev_loss_tmo(shost) = val;
1668 spin_lock_irqsave(shost->host_lock, flags);
1669 list_for_each_entry(rport, &fc_host->rports, peers)
1670 fc_rport_set_dev_loss_tmo(rport, val);
1671 spin_unlock_irqrestore(shost->host_lock, flags);
1675 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
1676 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
1677 show_fc_host_dev_loss_tmo,
1678 store_fc_private_host_dev_loss_tmo);
1680 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1683 * Host Statistics Management
1686 /* Show a given attribute in the statistics group */
1688 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1690 struct Scsi_Host *shost = transport_class_to_shost(dev);
1691 struct fc_internal *i = to_fc_internal(shost->transportt);
1692 struct fc_host_statistics *stats;
1693 ssize_t ret = -ENOENT;
1695 if (offset > sizeof(struct fc_host_statistics) ||
1696 offset % sizeof(u64) != 0)
1699 if (i->f->get_fc_host_stats) {
1700 stats = (i->f->get_fc_host_stats)(shost);
1702 ret = snprintf(buf, 20, "0x%llx\n",
1703 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1709 /* generate a read-only statistics attribute */
1710 #define fc_host_statistic(name) \
1711 static ssize_t show_fcstat_##name(struct device *cd, \
1712 struct device_attribute *attr, \
1715 return fc_stat_show(cd, buf, \
1716 offsetof(struct fc_host_statistics, name)); \
1718 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1720 fc_host_statistic(seconds_since_last_reset);
1721 fc_host_statistic(tx_frames);
1722 fc_host_statistic(tx_words);
1723 fc_host_statistic(rx_frames);
1724 fc_host_statistic(rx_words);
1725 fc_host_statistic(lip_count);
1726 fc_host_statistic(nos_count);
1727 fc_host_statistic(error_frames);
1728 fc_host_statistic(dumped_frames);
1729 fc_host_statistic(link_failure_count);
1730 fc_host_statistic(loss_of_sync_count);
1731 fc_host_statistic(loss_of_signal_count);
1732 fc_host_statistic(prim_seq_protocol_err_count);
1733 fc_host_statistic(invalid_tx_word_count);
1734 fc_host_statistic(invalid_crc_count);
1735 fc_host_statistic(fcp_input_requests);
1736 fc_host_statistic(fcp_output_requests);
1737 fc_host_statistic(fcp_control_requests);
1738 fc_host_statistic(fcp_input_megabytes);
1739 fc_host_statistic(fcp_output_megabytes);
1740 fc_host_statistic(fcp_packet_alloc_failures);
1741 fc_host_statistic(fcp_packet_aborts);
1742 fc_host_statistic(fcp_frame_alloc_failures);
1743 fc_host_statistic(fc_no_free_exch);
1744 fc_host_statistic(fc_no_free_exch_xid);
1745 fc_host_statistic(fc_xid_not_found);
1746 fc_host_statistic(fc_xid_busy);
1747 fc_host_statistic(fc_seq_not_found);
1748 fc_host_statistic(fc_non_bls_resp);
1751 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1752 const char *buf, size_t count)
1754 struct Scsi_Host *shost = transport_class_to_shost(dev);
1755 struct fc_internal *i = to_fc_internal(shost->transportt);
1757 /* ignore any data value written to the attribute */
1758 if (i->f->reset_fc_host_stats) {
1759 i->f->reset_fc_host_stats(shost);
1765 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1766 fc_reset_statistics);
1768 static struct attribute *fc_statistics_attrs[] = {
1769 &device_attr_host_seconds_since_last_reset.attr,
1770 &device_attr_host_tx_frames.attr,
1771 &device_attr_host_tx_words.attr,
1772 &device_attr_host_rx_frames.attr,
1773 &device_attr_host_rx_words.attr,
1774 &device_attr_host_lip_count.attr,
1775 &device_attr_host_nos_count.attr,
1776 &device_attr_host_error_frames.attr,
1777 &device_attr_host_dumped_frames.attr,
1778 &device_attr_host_link_failure_count.attr,
1779 &device_attr_host_loss_of_sync_count.attr,
1780 &device_attr_host_loss_of_signal_count.attr,
1781 &device_attr_host_prim_seq_protocol_err_count.attr,
1782 &device_attr_host_invalid_tx_word_count.attr,
1783 &device_attr_host_invalid_crc_count.attr,
1784 &device_attr_host_fcp_input_requests.attr,
1785 &device_attr_host_fcp_output_requests.attr,
1786 &device_attr_host_fcp_control_requests.attr,
1787 &device_attr_host_fcp_input_megabytes.attr,
1788 &device_attr_host_fcp_output_megabytes.attr,
1789 &device_attr_host_fcp_packet_alloc_failures.attr,
1790 &device_attr_host_fcp_packet_aborts.attr,
1791 &device_attr_host_fcp_frame_alloc_failures.attr,
1792 &device_attr_host_fc_no_free_exch.attr,
1793 &device_attr_host_fc_no_free_exch_xid.attr,
1794 &device_attr_host_fc_xid_not_found.attr,
1795 &device_attr_host_fc_xid_busy.attr,
1796 &device_attr_host_fc_seq_not_found.attr,
1797 &device_attr_host_fc_non_bls_resp.attr,
1798 &device_attr_host_reset_statistics.attr,
1802 static struct attribute_group fc_statistics_group = {
1803 .name = "statistics",
1804 .attrs = fc_statistics_attrs,
1808 /* Host Vport Attributes */
1811 fc_parse_wwn(const char *ns, u64 *nm)
1816 memset(wwn, 0, sizeof(wwn));
1818 /* Validate and store the new name */
1819 for (i=0, j=0; i < 16; i++) {
1822 value = hex_to_bin(*ns++);
1824 j = (j << 4) | value;
1828 wwn[i/2] = j & 0xff;
1833 *nm = wwn_to_u64(wwn);
1840 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1841 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1842 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1843 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1846 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1847 const char *buf, size_t count)
1849 struct Scsi_Host *shost = transport_class_to_shost(dev);
1850 struct fc_vport_identifiers vid;
1851 struct fc_vport *vport;
1852 unsigned int cnt=count;
1855 memset(&vid, 0, sizeof(vid));
1857 /* count may include a LF at end of string */
1858 if (buf[cnt-1] == '\n')
1861 /* validate we have enough characters for WWPN */
1862 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1865 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1869 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1873 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1874 vid.vport_type = FC_PORTTYPE_NPIV;
1875 /* vid.symbolic_name is already zero/NULL's */
1876 vid.disable = false; /* always enabled */
1878 /* we only allow support on Channel 0 !!! */
1879 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1880 return stat ? stat : count;
1882 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1883 store_fc_host_vport_create);
1887 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1888 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1889 * The WWNs are specified as hex characters, and may *not* contain
1890 * any prefixes (e.g. 0x, x, etc)
1893 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1894 const char *buf, size_t count)
1896 struct Scsi_Host *shost = transport_class_to_shost(dev);
1897 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1898 struct fc_vport *vport;
1900 unsigned long flags;
1901 unsigned int cnt=count;
1904 /* count may include a LF at end of string */
1905 if (buf[cnt-1] == '\n')
1908 /* validate we have enough characters for WWPN */
1909 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1912 stat = fc_parse_wwn(&buf[0], &wwpn);
1916 stat = fc_parse_wwn(&buf[17], &wwnn);
1920 spin_lock_irqsave(shost->host_lock, flags);
1922 /* we only allow support on Channel 0 !!! */
1923 list_for_each_entry(vport, &fc_host->vports, peers) {
1924 if ((vport->channel == 0) &&
1925 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1926 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1928 vport->flags |= FC_VPORT_DELETING;
1933 spin_unlock_irqrestore(shost->host_lock, flags);
1938 stat = fc_vport_terminate(vport);
1939 return stat ? stat : count;
1941 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1942 store_fc_host_vport_delete);
1945 static int fc_host_match(struct attribute_container *cont,
1948 struct Scsi_Host *shost;
1949 struct fc_internal *i;
1951 if (!scsi_is_host_device(dev))
1954 shost = dev_to_shost(dev);
1955 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1956 != &fc_host_class.class)
1959 i = to_fc_internal(shost->transportt);
1961 return &i->t.host_attrs.ac == cont;
1964 static int fc_target_match(struct attribute_container *cont,
1967 struct Scsi_Host *shost;
1968 struct fc_internal *i;
1970 if (!scsi_is_target_device(dev))
1973 shost = dev_to_shost(dev->parent);
1974 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1975 != &fc_host_class.class)
1978 i = to_fc_internal(shost->transportt);
1980 return &i->t.target_attrs.ac == cont;
1983 static void fc_rport_dev_release(struct device *dev)
1985 struct fc_rport *rport = dev_to_rport(dev);
1986 put_device(dev->parent);
1990 int scsi_is_fc_rport(const struct device *dev)
1992 return dev->release == fc_rport_dev_release;
1994 EXPORT_SYMBOL(scsi_is_fc_rport);
1996 static int fc_rport_match(struct attribute_container *cont,
1999 struct Scsi_Host *shost;
2000 struct fc_internal *i;
2002 if (!scsi_is_fc_rport(dev))
2005 shost = dev_to_shost(dev->parent);
2006 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2007 != &fc_host_class.class)
2010 i = to_fc_internal(shost->transportt);
2012 return &i->rport_attr_cont.ac == cont;
2016 static void fc_vport_dev_release(struct device *dev)
2018 struct fc_vport *vport = dev_to_vport(dev);
2019 put_device(dev->parent); /* release kobj parent */
2023 static int scsi_is_fc_vport(const struct device *dev)
2025 return dev->release == fc_vport_dev_release;
2028 static int fc_vport_match(struct attribute_container *cont,
2031 struct fc_vport *vport;
2032 struct Scsi_Host *shost;
2033 struct fc_internal *i;
2035 if (!scsi_is_fc_vport(dev))
2037 vport = dev_to_vport(dev);
2039 shost = vport_to_shost(vport);
2040 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2041 != &fc_host_class.class)
2044 i = to_fc_internal(shost->transportt);
2045 return &i->vport_attr_cont.ac == cont;
2050 * fc_eh_timed_out - FC Transport I/O timeout intercept handler
2051 * @scmd: The SCSI command which timed out
2053 * This routine protects against error handlers getting invoked while a
2054 * rport is in a blocked state, typically due to a temporarily loss of
2055 * connectivity. If the error handlers are allowed to proceed, requests
2056 * to abort i/o, reset the target, etc will likely fail as there is no way
2057 * to communicate with the device to perform the requested function. These
2058 * failures may result in the midlayer taking the device offline, requiring
2059 * manual intervention to restore operation.
2061 * This routine, called whenever an i/o times out, validates the state of
2062 * the underlying rport. If the rport is blocked, it returns
2063 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2064 * Eventually, either the device will return, or devloss_tmo will fire,
2065 * and when the timeout then fires, it will be handled normally.
2066 * If the rport is not blocked, normal error handling continues.
2069 * This routine assumes no locks are held on entry.
2071 enum blk_eh_timer_return
2072 fc_eh_timed_out(struct scsi_cmnd *scmd)
2074 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2076 if (rport->port_state == FC_PORTSTATE_BLOCKED)
2077 return BLK_EH_RESET_TIMER;
2081 EXPORT_SYMBOL(fc_eh_timed_out);
2084 * Called by fc_user_scan to locate an rport on the shost that
2085 * matches the channel and target id, and invoke scsi_scan_target()
2089 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2091 struct fc_rport *rport;
2092 unsigned long flags;
2094 spin_lock_irqsave(shost->host_lock, flags);
2096 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2097 if (rport->scsi_target_id == -1)
2100 if (rport->port_state != FC_PORTSTATE_ONLINE)
2103 if ((channel == rport->channel) &&
2104 (id == rport->scsi_target_id)) {
2105 spin_unlock_irqrestore(shost->host_lock, flags);
2106 scsi_scan_target(&rport->dev, channel, id, lun,
2112 spin_unlock_irqrestore(shost->host_lock, flags);
2116 * Called via sysfs scan routines. Necessary, as the FC transport
2117 * wants to place all target objects below the rport object. So this
2118 * routine must invoke the scsi_scan_target() routine with the rport
2119 * object as the parent.
2122 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2127 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2128 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2129 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2132 if (channel == SCAN_WILD_CARD) {
2134 chhi = shost->max_channel + 1;
2140 if (id == SCAN_WILD_CARD) {
2142 tgthi = shost->max_id;
2148 for ( ; chlo < chhi; chlo++)
2149 for ( ; tgtlo < tgthi; tgtlo++)
2150 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2155 struct scsi_transport_template *
2156 fc_attach_transport(struct fc_function_template *ft)
2159 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2165 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2166 i->t.target_attrs.ac.class = &fc_transport_class.class;
2167 i->t.target_attrs.ac.match = fc_target_match;
2168 i->t.target_size = sizeof(struct fc_starget_attrs);
2169 transport_container_register(&i->t.target_attrs);
2171 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2172 i->t.host_attrs.ac.class = &fc_host_class.class;
2173 i->t.host_attrs.ac.match = fc_host_match;
2174 i->t.host_size = sizeof(struct fc_host_attrs);
2175 if (ft->get_fc_host_stats)
2176 i->t.host_attrs.statistics = &fc_statistics_group;
2177 transport_container_register(&i->t.host_attrs);
2179 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2180 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2181 i->rport_attr_cont.ac.match = fc_rport_match;
2182 transport_container_register(&i->rport_attr_cont);
2184 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2185 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2186 i->vport_attr_cont.ac.match = fc_vport_match;
2187 transport_container_register(&i->vport_attr_cont);
2191 /* Transport uses the shost workq for scsi scanning */
2192 i->t.create_work_queue = 1;
2194 i->t.user_scan = fc_user_scan;
2197 * Setup SCSI Target Attributes.
2200 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2201 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2202 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2204 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2206 i->starget_attrs[count] = NULL;
2210 * Setup SCSI Host Attributes.
2213 SETUP_HOST_ATTRIBUTE_RD(node_name);
2214 SETUP_HOST_ATTRIBUTE_RD(port_name);
2215 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2216 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2217 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2218 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2219 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2220 if (ft->vport_create) {
2221 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2222 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2224 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2225 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2226 SETUP_HOST_ATTRIBUTE_RD(model);
2227 SETUP_HOST_ATTRIBUTE_RD(model_description);
2228 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2229 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2230 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2231 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
2233 SETUP_HOST_ATTRIBUTE_RD(port_id);
2234 SETUP_HOST_ATTRIBUTE_RD(port_type);
2235 SETUP_HOST_ATTRIBUTE_RD(port_state);
2236 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2237 SETUP_HOST_ATTRIBUTE_RD(speed);
2238 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2239 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2240 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2242 /* Transport-managed attributes */
2243 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
2244 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2245 if (ft->issue_fc_host_lip)
2246 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2247 if (ft->vport_create)
2248 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2249 if (ft->vport_delete)
2250 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2252 BUG_ON(count > FC_HOST_NUM_ATTRS);
2254 i->host_attrs[count] = NULL;
2257 * Setup Remote Port Attributes.
2260 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2261 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2262 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2263 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2264 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2265 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2266 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2267 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2268 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2269 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2271 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2273 i->rport_attrs[count] = NULL;
2276 * Setup Virtual Port Attributes.
2279 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2280 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2281 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2282 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2283 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2284 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2285 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2286 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2287 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2289 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2291 i->vport_attrs[count] = NULL;
2295 EXPORT_SYMBOL(fc_attach_transport);
2297 void fc_release_transport(struct scsi_transport_template *t)
2299 struct fc_internal *i = to_fc_internal(t);
2301 transport_container_unregister(&i->t.target_attrs);
2302 transport_container_unregister(&i->t.host_attrs);
2303 transport_container_unregister(&i->rport_attr_cont);
2304 transport_container_unregister(&i->vport_attr_cont);
2308 EXPORT_SYMBOL(fc_release_transport);
2311 * fc_queue_work - Queue work to the fc_host workqueue.
2312 * @shost: Pointer to Scsi_Host bound to fc_host.
2313 * @work: Work to queue for execution.
2316 * 1 - work queued for execution
2317 * 0 - work is already queued
2318 * -EINVAL - work queue doesn't exist
2321 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2323 if (unlikely(!fc_host_work_q(shost))) {
2325 "ERROR: FC host '%s' attempted to queue work, "
2326 "when no workqueue created.\n", shost->hostt->name);
2332 return queue_work(fc_host_work_q(shost), work);
2336 * fc_flush_work - Flush a fc_host's workqueue.
2337 * @shost: Pointer to Scsi_Host bound to fc_host.
2340 fc_flush_work(struct Scsi_Host *shost)
2342 if (!fc_host_work_q(shost)) {
2344 "ERROR: FC host '%s' attempted to flush work, "
2345 "when no workqueue created.\n", shost->hostt->name);
2350 flush_workqueue(fc_host_work_q(shost));
2354 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2355 * @shost: Pointer to Scsi_Host bound to fc_host.
2356 * @work: Work to queue for execution.
2357 * @delay: jiffies to delay the work queuing
2360 * 1 on success / 0 already queued / < 0 for error
2363 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2364 unsigned long delay)
2366 if (unlikely(!fc_host_devloss_work_q(shost))) {
2368 "ERROR: FC host '%s' attempted to queue work, "
2369 "when no workqueue created.\n", shost->hostt->name);
2375 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2379 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2380 * @shost: Pointer to Scsi_Host bound to fc_host.
2383 fc_flush_devloss(struct Scsi_Host *shost)
2385 if (!fc_host_devloss_work_q(shost)) {
2387 "ERROR: FC host '%s' attempted to flush work, "
2388 "when no workqueue created.\n", shost->hostt->name);
2393 flush_workqueue(fc_host_devloss_work_q(shost));
2398 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2399 * @shost: Which &Scsi_Host
2401 * This routine is expected to be called immediately preceding the
2402 * a driver's call to scsi_remove_host().
2404 * WARNING: A driver utilizing the fc_transport, which fails to call
2405 * this routine prior to scsi_remove_host(), will leave dangling
2406 * objects in /sys/class/fc_remote_ports. Access to any of these
2407 * objects can result in a system crash !!!
2410 * This routine assumes no locks are held on entry.
2413 fc_remove_host(struct Scsi_Host *shost)
2415 struct fc_vport *vport = NULL, *next_vport = NULL;
2416 struct fc_rport *rport = NULL, *next_rport = NULL;
2417 struct workqueue_struct *work_q;
2418 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2419 unsigned long flags;
2421 spin_lock_irqsave(shost->host_lock, flags);
2423 /* Remove any vports */
2424 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2425 vport->flags |= FC_VPORT_DELETING;
2426 fc_queue_work(shost, &vport->vport_delete_work);
2429 /* Remove any remote ports */
2430 list_for_each_entry_safe(rport, next_rport,
2431 &fc_host->rports, peers) {
2432 list_del(&rport->peers);
2433 rport->port_state = FC_PORTSTATE_DELETED;
2434 fc_queue_work(shost, &rport->rport_delete_work);
2437 list_for_each_entry_safe(rport, next_rport,
2438 &fc_host->rport_bindings, peers) {
2439 list_del(&rport->peers);
2440 rport->port_state = FC_PORTSTATE_DELETED;
2441 fc_queue_work(shost, &rport->rport_delete_work);
2444 spin_unlock_irqrestore(shost->host_lock, flags);
2446 /* flush all scan work items */
2447 scsi_flush_work(shost);
2449 /* flush all stgt delete, and rport delete work items, then kill it */
2450 if (fc_host->work_q) {
2451 work_q = fc_host->work_q;
2452 fc_host->work_q = NULL;
2453 destroy_workqueue(work_q);
2456 /* flush all devloss work items, then kill it */
2457 if (fc_host->devloss_work_q) {
2458 work_q = fc_host->devloss_work_q;
2459 fc_host->devloss_work_q = NULL;
2460 destroy_workqueue(work_q);
2463 EXPORT_SYMBOL(fc_remove_host);
2465 static void fc_terminate_rport_io(struct fc_rport *rport)
2467 struct Scsi_Host *shost = rport_to_shost(rport);
2468 struct fc_internal *i = to_fc_internal(shost->transportt);
2470 /* Involve the LLDD if possible to terminate all io on the rport. */
2471 if (i->f->terminate_rport_io)
2472 i->f->terminate_rport_io(rport);
2475 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2477 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2481 * fc_starget_delete - called to delete the scsi descendants of an rport
2482 * @work: remote port to be operated on.
2484 * Deletes target and all sdevs.
2487 fc_starget_delete(struct work_struct *work)
2489 struct fc_rport *rport =
2490 container_of(work, struct fc_rport, stgt_delete_work);
2492 fc_terminate_rport_io(rport);
2493 scsi_remove_target(&rport->dev);
2498 * fc_rport_final_delete - finish rport termination and delete it.
2499 * @work: remote port to be deleted.
2502 fc_rport_final_delete(struct work_struct *work)
2504 struct fc_rport *rport =
2505 container_of(work, struct fc_rport, rport_delete_work);
2506 struct device *dev = &rport->dev;
2507 struct Scsi_Host *shost = rport_to_shost(rport);
2508 struct fc_internal *i = to_fc_internal(shost->transportt);
2509 unsigned long flags;
2510 int do_callback = 0;
2512 fc_terminate_rport_io(rport);
2515 * if a scan is pending, flush the SCSI Host work_q so that
2516 * that we can reclaim the rport scan work element.
2518 if (rport->flags & FC_RPORT_SCAN_PENDING)
2519 scsi_flush_work(shost);
2522 * Cancel any outstanding timers. These should really exist
2523 * only when rmmod'ing the LLDD and we're asking for
2524 * immediate termination of the rports
2526 spin_lock_irqsave(shost->host_lock, flags);
2527 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2528 spin_unlock_irqrestore(shost->host_lock, flags);
2529 if (!cancel_delayed_work(&rport->fail_io_work))
2530 fc_flush_devloss(shost);
2531 if (!cancel_delayed_work(&rport->dev_loss_work))
2532 fc_flush_devloss(shost);
2533 cancel_work_sync(&rport->scan_work);
2534 spin_lock_irqsave(shost->host_lock, flags);
2535 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2537 spin_unlock_irqrestore(shost->host_lock, flags);
2539 /* Delete SCSI target and sdevs */
2540 if (rport->scsi_target_id != -1)
2541 fc_starget_delete(&rport->stgt_delete_work);
2544 * Notify the driver that the rport is now dead. The LLDD will
2545 * also guarantee that any communication to the rport is terminated
2547 * Avoid this call if we already called it when we preserved the
2548 * rport for the binding.
2550 spin_lock_irqsave(shost->host_lock, flags);
2551 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2552 (i->f->dev_loss_tmo_callbk)) {
2553 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2556 spin_unlock_irqrestore(shost->host_lock, flags);
2559 i->f->dev_loss_tmo_callbk(rport);
2561 fc_bsg_remove(rport->rqst_q);
2563 transport_remove_device(dev);
2565 transport_destroy_device(dev);
2566 scsi_host_put(shost); /* for fc_host->rport list */
2567 put_device(dev); /* for self-reference */
2572 * fc_remote_port_create - allocates and creates a remote FC port.
2573 * @shost: scsi host the remote port is connected to.
2574 * @channel: Channel on shost port connected to.
2575 * @ids: The world wide names, fc address, and FC4 port
2576 * roles for the remote port.
2578 * Allocates and creates the remoter port structure, including the
2579 * class and sysfs creation.
2582 * This routine assumes no locks are held on entry.
2584 static struct fc_rport *
2585 fc_remote_port_create(struct Scsi_Host *shost, int channel,
2586 struct fc_rport_identifiers *ids)
2588 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2589 struct fc_internal *fci = to_fc_internal(shost->transportt);
2590 struct fc_rport *rport;
2592 unsigned long flags;
2596 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2597 rport = kzalloc(size, GFP_KERNEL);
2598 if (unlikely(!rport)) {
2599 printk(KERN_ERR "%s: allocation failure\n", __func__);
2603 rport->maxframe_size = -1;
2604 rport->supported_classes = FC_COS_UNSPECIFIED;
2605 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
2606 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2607 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2608 rport->port_id = ids->port_id;
2609 rport->roles = ids->roles;
2610 rport->port_state = FC_PORTSTATE_ONLINE;
2611 if (fci->f->dd_fcrport_size)
2612 rport->dd_data = &rport[1];
2613 rport->channel = channel;
2614 rport->fast_io_fail_tmo = -1;
2616 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2617 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2618 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2619 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2620 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2622 spin_lock_irqsave(shost->host_lock, flags);
2624 rport->number = fc_host->next_rport_number++;
2625 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
2626 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
2627 rport->scsi_target_id = fc_host->next_target_id++;
2629 rport->scsi_target_id = -1;
2630 list_add_tail(&rport->peers, &fc_host->rports);
2631 scsi_host_get(shost); /* for fc_host->rport list */
2633 spin_unlock_irqrestore(shost->host_lock, flags);
2636 device_initialize(dev); /* takes self reference */
2637 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2638 dev->release = fc_rport_dev_release;
2639 dev_set_name(dev, "rport-%d:%d-%d",
2640 shost->host_no, channel, rport->number);
2641 transport_setup_device(dev);
2643 error = device_add(dev);
2645 printk(KERN_ERR "FC Remote Port device_add failed\n");
2648 transport_add_device(dev);
2649 transport_configure_device(dev);
2651 fc_bsg_rportadd(shost, rport);
2652 /* ignore any bsg add error - we just can't do sgio */
2654 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2655 /* initiate a scan of the target */
2656 rport->flags |= FC_RPORT_SCAN_PENDING;
2657 scsi_queue_work(shost, &rport->scan_work);
2663 transport_destroy_device(dev);
2664 spin_lock_irqsave(shost->host_lock, flags);
2665 list_del(&rport->peers);
2666 scsi_host_put(shost); /* for fc_host->rport list */
2667 spin_unlock_irqrestore(shost->host_lock, flags);
2668 put_device(dev->parent);
2674 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2675 * @shost: scsi host the remote port is connected to.
2676 * @channel: Channel on shost port connected to.
2677 * @ids: The world wide names, fc address, and FC4 port
2678 * roles for the remote port.
2680 * The LLDD calls this routine to notify the transport of the existence
2681 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2682 * of the port, it's FC address (port_id), and the FC4 roles that are
2683 * active for the port.
2685 * For ports that are FCP targets (aka scsi targets), the FC transport
2686 * maintains consistent target id bindings on behalf of the LLDD.
2687 * A consistent target id binding is an assignment of a target id to
2688 * a remote port identifier, which persists while the scsi host is
2689 * attached. The remote port can disappear, then later reappear, and
2690 * it's target id assignment remains the same. This allows for shifts
2691 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2692 * changes to the scsi subsystem which is based on scsi host number and
2693 * target id values. Bindings are only valid during the attachment of
2694 * the scsi host. If the host detaches, then later re-attaches, target
2695 * id bindings may change.
2697 * This routine is responsible for returning a remote port structure.
2698 * The routine will search the list of remote ports it maintains
2699 * internally on behalf of consistent target id mappings. If found, the
2700 * remote port structure will be reused. Otherwise, a new remote port
2701 * structure will be allocated.
2703 * Whenever a remote port is allocated, a new fc_remote_port class
2704 * device is created.
2706 * Should not be called from interrupt context.
2709 * This routine assumes no locks are held on entry.
2712 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2713 struct fc_rport_identifiers *ids)
2715 struct fc_internal *fci = to_fc_internal(shost->transportt);
2716 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2717 struct fc_rport *rport;
2718 unsigned long flags;
2721 /* ensure any stgt delete functions are done */
2722 fc_flush_work(shost);
2725 * Search the list of "active" rports, for an rport that has been
2726 * deleted, but we've held off the real delete while the target
2727 * is in a "blocked" state.
2729 spin_lock_irqsave(shost->host_lock, flags);
2731 list_for_each_entry(rport, &fc_host->rports, peers) {
2733 if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
2734 rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
2735 (rport->channel == channel)) {
2737 switch (fc_host->tgtid_bind_type) {
2738 case FC_TGTID_BIND_BY_WWPN:
2739 case FC_TGTID_BIND_NONE:
2740 if (rport->port_name == ids->port_name)
2743 case FC_TGTID_BIND_BY_WWNN:
2744 if (rport->node_name == ids->node_name)
2747 case FC_TGTID_BIND_BY_ID:
2748 if (rport->port_id == ids->port_id)
2755 memcpy(&rport->node_name, &ids->node_name,
2756 sizeof(rport->node_name));
2757 memcpy(&rport->port_name, &ids->port_name,
2758 sizeof(rport->port_name));
2759 rport->port_id = ids->port_id;
2761 rport->port_state = FC_PORTSTATE_ONLINE;
2762 rport->roles = ids->roles;
2764 spin_unlock_irqrestore(shost->host_lock, flags);
2766 if (fci->f->dd_fcrport_size)
2767 memset(rport->dd_data, 0,
2768 fci->f->dd_fcrport_size);
2771 * If we were not a target, cancel the
2772 * io terminate and rport timers, and
2775 * If we were a target, but our new role
2776 * doesn't indicate a target, leave the
2777 * timers running expecting the role to
2778 * change as the target fully logs in. If
2779 * it doesn't, the target will be torn down.
2781 * If we were a target, and our role shows
2782 * we're still a target, cancel the timers
2783 * and kick off a scan.
2786 /* was a target, not in roles */
2787 if ((rport->scsi_target_id != -1) &&
2788 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2792 * Stop the fail io and dev_loss timers.
2793 * If they flush, the port_state will
2794 * be checked and will NOOP the function.
2796 if (!cancel_delayed_work(&rport->fail_io_work))
2797 fc_flush_devloss(shost);
2798 if (!cancel_delayed_work(&rport->dev_loss_work))
2799 fc_flush_devloss(shost);
2801 spin_lock_irqsave(shost->host_lock, flags);
2803 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2804 FC_RPORT_DEVLOSS_PENDING |
2805 FC_RPORT_DEVLOSS_CALLBK_DONE);
2807 spin_unlock_irqrestore(shost->host_lock, flags);
2809 /* if target, initiate a scan */
2810 if (rport->scsi_target_id != -1) {
2811 scsi_target_unblock(&rport->dev,
2813 spin_lock_irqsave(shost->host_lock,
2815 rport->flags |= FC_RPORT_SCAN_PENDING;
2816 scsi_queue_work(shost,
2818 spin_unlock_irqrestore(shost->host_lock,
2822 fc_bsg_goose_queue(rport);
2830 * Search the bindings array
2831 * Note: if never a FCP target, you won't be on this list
2833 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2835 /* search for a matching consistent binding */
2837 list_for_each_entry(rport, &fc_host->rport_bindings,
2839 if (rport->channel != channel)
2842 switch (fc_host->tgtid_bind_type) {
2843 case FC_TGTID_BIND_BY_WWPN:
2844 if (rport->port_name == ids->port_name)
2847 case FC_TGTID_BIND_BY_WWNN:
2848 if (rport->node_name == ids->node_name)
2851 case FC_TGTID_BIND_BY_ID:
2852 if (rport->port_id == ids->port_id)
2855 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2860 list_move_tail(&rport->peers, &fc_host->rports);
2866 memcpy(&rport->node_name, &ids->node_name,
2867 sizeof(rport->node_name));
2868 memcpy(&rport->port_name, &ids->port_name,
2869 sizeof(rport->port_name));
2870 rport->port_id = ids->port_id;
2871 rport->port_state = FC_PORTSTATE_ONLINE;
2872 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
2874 if (fci->f->dd_fcrport_size)
2875 memset(rport->dd_data, 0,
2876 fci->f->dd_fcrport_size);
2877 spin_unlock_irqrestore(shost->host_lock, flags);
2879 fc_remote_port_rolechg(rport, ids->roles);
2884 spin_unlock_irqrestore(shost->host_lock, flags);
2886 /* No consistent binding found - create new remote port entry */
2887 rport = fc_remote_port_create(shost, channel, ids);
2891 EXPORT_SYMBOL(fc_remote_port_add);
2895 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2896 * @rport: The remote port that no longer exists
2898 * The LLDD calls this routine to notify the transport that a remote
2899 * port is no longer part of the topology. Note: Although a port
2900 * may no longer be part of the topology, it may persist in the remote
2901 * ports displayed by the fc_host. We do this under 2 conditions:
2903 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2904 * This allows the port to temporarily disappear, then reappear without
2905 * disrupting the SCSI device tree attached to it. During the "blocked"
2906 * period the port will still exist.
2908 * 2) If the port was a scsi target and disappears for longer than we
2909 * expect, we'll delete the port and the tear down the SCSI device tree
2910 * attached to it. However, we want to semi-persist the target id assigned
2911 * to that port if it eventually does exist. The port structure will
2912 * remain (although with minimal information) so that the target id
2913 * bindings also remain.
2915 * If the remote port is not an FCP Target, it will be fully torn down
2916 * and deallocated, including the fc_remote_port class device.
2918 * If the remote port is an FCP Target, the port will be placed in a
2919 * temporary blocked state. From the LLDD's perspective, the rport no
2920 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2921 * exists, but all sdevs on it are blocked from further I/O. The following
2924 * If the remote port does not return (signaled by a LLDD call to
2925 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2926 * scsi target is removed - killing all outstanding i/o and removing the
2927 * scsi devices attached to it. The port structure will be marked Not
2928 * Present and be partially cleared, leaving only enough information to
2929 * recognize the remote port relative to the scsi target id binding if
2930 * it later appears. The port will remain as long as there is a valid
2931 * binding (e.g. until the user changes the binding type or unloads the
2932 * scsi host with the binding).
2934 * If the remote port returns within the dev_loss_tmo value (and matches
2935 * according to the target id binding type), the port structure will be
2936 * reused. If it is no longer a SCSI target, the target will be torn
2937 * down. If it continues to be a SCSI target, then the target will be
2938 * unblocked (allowing i/o to be resumed), and a scan will be activated
2939 * to ensure that all luns are detected.
2941 * Called from normal process context only - cannot be called from interrupt.
2944 * This routine assumes no locks are held on entry.
2947 fc_remote_port_delete(struct fc_rport *rport)
2949 struct Scsi_Host *shost = rport_to_shost(rport);
2950 unsigned long timeout = rport->dev_loss_tmo;
2951 unsigned long flags;
2954 * No need to flush the fc_host work_q's, as all adds are synchronous.
2956 * We do need to reclaim the rport scan work element, so eventually
2957 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2958 * there's still a scan pending.
2961 spin_lock_irqsave(shost->host_lock, flags);
2963 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2964 spin_unlock_irqrestore(shost->host_lock, flags);
2969 * In the past, we if this was not an FCP-Target, we would
2970 * unconditionally just jump to deleting the rport.
2971 * However, rports can be used as node containers by the LLDD,
2972 * and its not appropriate to just terminate the rport at the
2973 * first sign of a loss in connectivity. The LLDD may want to
2974 * send ELS traffic to re-validate the login. If the rport is
2975 * immediately deleted, it makes it inappropriate for a node
2977 * So... we now unconditionally wait dev_loss_tmo before
2978 * destroying an rport.
2981 rport->port_state = FC_PORTSTATE_BLOCKED;
2983 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2985 spin_unlock_irqrestore(shost->host_lock, flags);
2987 scsi_target_block(&rport->dev);
2989 /* see if we need to kill io faster than waiting for device loss */
2990 if ((rport->fast_io_fail_tmo != -1) &&
2991 (rport->fast_io_fail_tmo < timeout))
2992 fc_queue_devloss_work(shost, &rport->fail_io_work,
2993 rport->fast_io_fail_tmo * HZ);
2995 /* cap the length the devices can be blocked until they are deleted */
2996 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2998 EXPORT_SYMBOL(fc_remote_port_delete);
3001 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
3002 * @rport: The remote port that changed.
3003 * @roles: New roles for this port.
3005 * Description: The LLDD calls this routine to notify the transport that the
3006 * roles on a remote port may have changed. The largest effect of this is
3007 * if a port now becomes a FCP Target, it must be allocated a
3008 * scsi target id. If the port is no longer a FCP target, any
3009 * scsi target id value assigned to it will persist in case the
3010 * role changes back to include FCP Target. No changes in the scsi
3011 * midlayer will be invoked if the role changes (in the expectation
3012 * that the role will be resumed. If it doesn't normal error processing
3015 * Should not be called from interrupt context.
3018 * This routine assumes no locks are held on entry.
3021 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3023 struct Scsi_Host *shost = rport_to_shost(rport);
3024 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3025 unsigned long flags;
3028 spin_lock_irqsave(shost->host_lock, flags);
3029 if (roles & FC_PORT_ROLE_FCP_TARGET) {
3030 if (rport->scsi_target_id == -1) {
3031 rport->scsi_target_id = fc_host->next_target_id++;
3033 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3037 rport->roles = roles;
3039 spin_unlock_irqrestore(shost->host_lock, flags);
3043 * There may have been a delete timer running on the
3044 * port. Ensure that it is cancelled as we now know
3045 * the port is an FCP Target.
3046 * Note: we know the rport exists and is in an online
3047 * state as the LLDD would not have had an rport
3048 * reference to pass us.
3050 * Take no action on the del_timer failure as the state
3051 * machine state change will validate the
3054 if (!cancel_delayed_work(&rport->fail_io_work))
3055 fc_flush_devloss(shost);
3056 if (!cancel_delayed_work(&rport->dev_loss_work))
3057 fc_flush_devloss(shost);
3059 spin_lock_irqsave(shost->host_lock, flags);
3060 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3061 FC_RPORT_DEVLOSS_PENDING |
3062 FC_RPORT_DEVLOSS_CALLBK_DONE);
3063 spin_unlock_irqrestore(shost->host_lock, flags);
3065 /* ensure any stgt delete functions are done */
3066 fc_flush_work(shost);
3068 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
3069 /* initiate a scan of the target */
3070 spin_lock_irqsave(shost->host_lock, flags);
3071 rport->flags |= FC_RPORT_SCAN_PENDING;
3072 scsi_queue_work(shost, &rport->scan_work);
3073 spin_unlock_irqrestore(shost->host_lock, flags);
3076 EXPORT_SYMBOL(fc_remote_port_rolechg);
3079 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3080 * @work: rport target that failed to reappear in the allotted time.
3082 * Description: An attempt to delete a remote port blocks, and if it fails
3083 * to return in the allotted time this gets called.
3086 fc_timeout_deleted_rport(struct work_struct *work)
3088 struct fc_rport *rport =
3089 container_of(work, struct fc_rport, dev_loss_work.work);
3090 struct Scsi_Host *shost = rport_to_shost(rport);
3091 struct fc_internal *i = to_fc_internal(shost->transportt);
3092 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3093 unsigned long flags;
3094 int do_callback = 0;
3096 spin_lock_irqsave(shost->host_lock, flags);
3098 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3101 * If the port is ONLINE, then it came back. If it was a SCSI
3102 * target, validate it still is. If not, tear down the
3103 * scsi_target on it.
3105 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3106 (rport->scsi_target_id != -1) &&
3107 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3108 dev_printk(KERN_ERR, &rport->dev,
3109 "blocked FC remote port time out: no longer"
3110 " a FCP target, removing starget\n");
3111 spin_unlock_irqrestore(shost->host_lock, flags);
3112 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3113 fc_queue_work(shost, &rport->stgt_delete_work);
3117 /* NOOP state - we're flushing workq's */
3118 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3119 spin_unlock_irqrestore(shost->host_lock, flags);
3120 dev_printk(KERN_ERR, &rport->dev,
3121 "blocked FC remote port time out: leaving"
3123 (rport->scsi_target_id != -1) ? " and starget" : "");
3127 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3128 (rport->scsi_target_id == -1)) {
3129 list_del(&rport->peers);
3130 rport->port_state = FC_PORTSTATE_DELETED;
3131 dev_printk(KERN_ERR, &rport->dev,
3132 "blocked FC remote port time out: removing"
3134 (rport->scsi_target_id != -1) ? " and starget" : "");
3135 fc_queue_work(shost, &rport->rport_delete_work);
3136 spin_unlock_irqrestore(shost->host_lock, flags);
3140 dev_printk(KERN_ERR, &rport->dev,
3141 "blocked FC remote port time out: removing target and "
3142 "saving binding\n");
3144 list_move_tail(&rport->peers, &fc_host->rport_bindings);
3147 * Note: We do not remove or clear the hostdata area. This allows
3148 * host-specific target data to persist along with the
3149 * scsi_target_id. It's up to the host to manage it's hostdata area.
3153 * Reinitialize port attributes that may change if the port comes back.
3155 rport->maxframe_size = -1;
3156 rport->supported_classes = FC_COS_UNSPECIFIED;
3157 rport->roles = FC_PORT_ROLE_UNKNOWN;
3158 rport->port_state = FC_PORTSTATE_NOTPRESENT;
3159 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3162 * Pre-emptively kill I/O rather than waiting for the work queue
3163 * item to teardown the starget. (FCOE libFC folks prefer this
3164 * and to have the rport_port_id still set when it's done).
3166 spin_unlock_irqrestore(shost->host_lock, flags);
3167 fc_terminate_rport_io(rport);
3169 spin_lock_irqsave(shost->host_lock, flags);
3171 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3173 /* remove the identifiers that aren't used in the consisting binding */
3174 switch (fc_host->tgtid_bind_type) {
3175 case FC_TGTID_BIND_BY_WWPN:
3176 rport->node_name = -1;
3177 rport->port_id = -1;
3179 case FC_TGTID_BIND_BY_WWNN:
3180 rport->port_name = -1;
3181 rport->port_id = -1;
3183 case FC_TGTID_BIND_BY_ID:
3184 rport->node_name = -1;
3185 rport->port_name = -1;
3187 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3192 * As this only occurs if the remote port (scsi target)
3193 * went away and didn't come back - we'll remove
3194 * all attached scsi devices.
3196 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3197 fc_queue_work(shost, &rport->stgt_delete_work);
3202 spin_unlock_irqrestore(shost->host_lock, flags);
3205 * Notify the driver that the rport is now dead. The LLDD will
3206 * also guarantee that any communication to the rport is terminated
3208 * Note: we set the CALLBK_DONE flag above to correspond
3210 if (do_callback && i->f->dev_loss_tmo_callbk)
3211 i->f->dev_loss_tmo_callbk(rport);
3216 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3217 * @work: rport to terminate io on.
3219 * Notes: Only requests the failure of the io, not that all are flushed
3220 * prior to returning.
3223 fc_timeout_fail_rport_io(struct work_struct *work)
3225 struct fc_rport *rport =
3226 container_of(work, struct fc_rport, fail_io_work.work);
3228 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3231 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3232 fc_terminate_rport_io(rport);
3236 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3237 * @work: remote port to be scanned.
3240 fc_scsi_scan_rport(struct work_struct *work)
3242 struct fc_rport *rport =
3243 container_of(work, struct fc_rport, scan_work);
3244 struct Scsi_Host *shost = rport_to_shost(rport);
3245 struct fc_internal *i = to_fc_internal(shost->transportt);
3246 unsigned long flags;
3248 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3249 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3250 !(i->f->disable_target_scan)) {
3251 scsi_scan_target(&rport->dev, rport->channel,
3252 rport->scsi_target_id, SCAN_WILD_CARD,
3256 spin_lock_irqsave(shost->host_lock, flags);
3257 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3258 spin_unlock_irqrestore(shost->host_lock, flags);
3262 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3263 * @rport: Remote port that scsi_eh is trying to recover.
3265 * This routine can be called from a FC LLD scsi_eh callback. It
3266 * blocks the scsi_eh thread until the fc_rport leaves the
3267 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3268 * necessary to avoid the scsi_eh failing recovery actions for blocked
3269 * rports which would lead to offlined SCSI devices.
3271 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3272 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3273 * passed back to scsi_eh.
3275 int fc_block_rport(struct fc_rport *rport)
3277 struct Scsi_Host *shost = rport_to_shost(rport);
3278 unsigned long flags;
3280 spin_lock_irqsave(shost->host_lock, flags);
3281 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3282 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3283 spin_unlock_irqrestore(shost->host_lock, flags);
3285 spin_lock_irqsave(shost->host_lock, flags);
3287 spin_unlock_irqrestore(shost->host_lock, flags);
3289 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3290 return FAST_IO_FAIL;
3294 EXPORT_SYMBOL(fc_block_rport);
3297 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3298 * @cmnd: SCSI command that scsi_eh is trying to recover
3300 * This routine can be called from a FC LLD scsi_eh callback. It
3301 * blocks the scsi_eh thread until the fc_rport leaves the
3302 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3303 * necessary to avoid the scsi_eh failing recovery actions for blocked
3304 * rports which would lead to offlined SCSI devices.
3306 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3307 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3308 * passed back to scsi_eh.
3310 int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3312 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3314 if (WARN_ON_ONCE(!rport))
3315 return FAST_IO_FAIL;
3317 return fc_block_rport(rport);
3319 EXPORT_SYMBOL(fc_block_scsi_eh);
3322 * fc_vport_setup - allocates and creates a FC virtual port.
3323 * @shost: scsi host the virtual port is connected to.
3324 * @channel: Channel on shost port connected to.
3325 * @pdev: parent device for vport
3326 * @ids: The world wide names, FC4 port roles, etc for
3328 * @ret_vport: The pointer to the created vport.
3330 * Allocates and creates the vport structure, calls the parent host
3331 * to instantiate the vport, this completes w/ class and sysfs creation.
3334 * This routine assumes no locks are held on entry.
3337 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3338 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3340 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3341 struct fc_internal *fci = to_fc_internal(shost->transportt);
3342 struct fc_vport *vport;
3344 unsigned long flags;
3350 if ( ! fci->f->vport_create)
3353 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3354 vport = kzalloc(size, GFP_KERNEL);
3355 if (unlikely(!vport)) {
3356 printk(KERN_ERR "%s: allocation failure\n", __func__);
3360 vport->vport_state = FC_VPORT_UNKNOWN;
3361 vport->vport_last_state = FC_VPORT_UNKNOWN;
3362 vport->node_name = ids->node_name;
3363 vport->port_name = ids->port_name;
3364 vport->roles = ids->roles;
3365 vport->vport_type = ids->vport_type;
3366 if (fci->f->dd_fcvport_size)
3367 vport->dd_data = &vport[1];
3368 vport->shost = shost;
3369 vport->channel = channel;
3370 vport->flags = FC_VPORT_CREATING;
3371 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3373 spin_lock_irqsave(shost->host_lock, flags);
3375 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3376 spin_unlock_irqrestore(shost->host_lock, flags);
3380 fc_host->npiv_vports_inuse++;
3381 vport->number = fc_host->next_vport_number++;
3382 list_add_tail(&vport->peers, &fc_host->vports);
3383 scsi_host_get(shost); /* for fc_host->vport list */
3385 spin_unlock_irqrestore(shost->host_lock, flags);
3388 device_initialize(dev); /* takes self reference */
3389 dev->parent = get_device(pdev); /* takes parent reference */
3390 dev->release = fc_vport_dev_release;
3391 dev_set_name(dev, "vport-%d:%d-%d",
3392 shost->host_no, channel, vport->number);
3393 transport_setup_device(dev);
3395 error = device_add(dev);
3397 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3400 transport_add_device(dev);
3401 transport_configure_device(dev);
3403 error = fci->f->vport_create(vport, ids->disable);
3405 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3406 goto delete_vport_all;
3410 * if the parent isn't the physical adapter's Scsi_Host, ensure
3411 * the Scsi_Host at least contains a symlink to the vport.
3413 if (pdev != &shost->shost_gendev) {
3414 error = sysfs_create_link(&shost->shost_gendev.kobj,
3415 &dev->kobj, dev_name(dev));
3418 "%s: Cannot create vport symlinks for "
3420 __func__, dev_name(dev), error);
3422 spin_lock_irqsave(shost->host_lock, flags);
3423 vport->flags &= ~FC_VPORT_CREATING;
3424 spin_unlock_irqrestore(shost->host_lock, flags);
3426 dev_printk(KERN_NOTICE, pdev,
3427 "%s created via shost%d channel %d\n", dev_name(dev),
3428 shost->host_no, channel);
3435 transport_remove_device(dev);
3438 transport_destroy_device(dev);
3439 spin_lock_irqsave(shost->host_lock, flags);
3440 list_del(&vport->peers);
3441 scsi_host_put(shost); /* for fc_host->vport list */
3442 fc_host->npiv_vports_inuse--;
3443 spin_unlock_irqrestore(shost->host_lock, flags);
3444 put_device(dev->parent);
3451 * fc_vport_create - Admin App or LLDD requests creation of a vport
3452 * @shost: scsi host the virtual port is connected to.
3453 * @channel: channel on shost port connected to.
3454 * @ids: The world wide names, FC4 port roles, etc for
3458 * This routine assumes no locks are held on entry.
3461 fc_vport_create(struct Scsi_Host *shost, int channel,
3462 struct fc_vport_identifiers *ids)
3465 struct fc_vport *vport;
3467 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3469 return stat ? NULL : vport;
3471 EXPORT_SYMBOL(fc_vport_create);
3474 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3475 * @vport: fc_vport to be terminated
3477 * Calls the LLDD vport_delete() function, then deallocates and removes
3478 * the vport from the shost and object tree.
3481 * This routine assumes no locks are held on entry.
3484 fc_vport_terminate(struct fc_vport *vport)
3486 struct Scsi_Host *shost = vport_to_shost(vport);
3487 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3488 struct fc_internal *i = to_fc_internal(shost->transportt);
3489 struct device *dev = &vport->dev;
3490 unsigned long flags;
3493 if (i->f->vport_delete)
3494 stat = i->f->vport_delete(vport);
3498 spin_lock_irqsave(shost->host_lock, flags);
3499 vport->flags &= ~FC_VPORT_DELETING;
3501 vport->flags |= FC_VPORT_DELETED;
3502 list_del(&vport->peers);
3503 fc_host->npiv_vports_inuse--;
3504 scsi_host_put(shost); /* for fc_host->vport list */
3506 spin_unlock_irqrestore(shost->host_lock, flags);
3511 if (dev->parent != &shost->shost_gendev)
3512 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3513 transport_remove_device(dev);
3515 transport_destroy_device(dev);
3518 * Removing our self-reference should mean our
3519 * release function gets called, which will drop the remaining
3520 * parent reference and free the data structure.
3522 put_device(dev); /* for self-reference */
3524 return 0; /* SUCCESS */
3526 EXPORT_SYMBOL(fc_vport_terminate);
3529 * fc_vport_sched_delete - workq-based delete request for a vport
3530 * @work: vport to be deleted.
3533 fc_vport_sched_delete(struct work_struct *work)
3535 struct fc_vport *vport =
3536 container_of(work, struct fc_vport, vport_delete_work);
3539 stat = fc_vport_terminate(vport);
3541 dev_printk(KERN_ERR, vport->dev.parent,
3542 "%s: %s could not be deleted created via "
3543 "shost%d channel %d - error %d\n", __func__,
3544 dev_name(&vport->dev), vport->shost->host_no,
3545 vport->channel, stat);
3554 * fc_bsg_job_timeout - handler for when a bsg request timesout
3555 * @req: request that timed out
3557 static enum blk_eh_timer_return
3558 fc_bsg_job_timeout(struct request *req)
3560 struct bsg_job *job = blk_mq_rq_to_pdu(req);
3561 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3562 struct fc_rport *rport = fc_bsg_to_rport(job);
3563 struct fc_internal *i = to_fc_internal(shost->transportt);
3564 int err = 0, inflight = 0;
3566 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
3567 return BLK_EH_RESET_TIMER;
3569 inflight = bsg_job_get(job);
3571 if (inflight && i->f->bsg_timeout) {
3572 /* call LLDD to abort the i/o as it has timed out */
3573 err = i->f->bsg_timeout(job);
3574 if (err == -EAGAIN) {
3576 return BLK_EH_RESET_TIMER;
3578 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3579 "abort failed with status %d\n", err);
3582 /* the blk_end_sync_io() doesn't check the error */
3584 blk_mq_end_request(req, BLK_STS_IOERR);
3589 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3590 * @shost: scsi host rport attached to
3591 * @job: bsg job to be processed
3593 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3595 struct fc_internal *i = to_fc_internal(shost->transportt);
3596 struct fc_bsg_request *bsg_request = job->request;
3597 struct fc_bsg_reply *bsg_reply = job->reply;
3598 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3601 /* check if we really have all the request data needed */
3602 if (job->request_len < cmdlen) {
3607 /* Validate the host command */
3608 switch (bsg_request->msgcode) {
3609 case FC_BSG_HST_ADD_RPORT:
3610 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3613 case FC_BSG_HST_DEL_RPORT:
3614 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3617 case FC_BSG_HST_ELS_NOLOGIN:
3618 cmdlen += sizeof(struct fc_bsg_host_els);
3619 /* there better be a xmt and rcv payloads */
3620 if ((!job->request_payload.payload_len) ||
3621 (!job->reply_payload.payload_len)) {
3628 cmdlen += sizeof(struct fc_bsg_host_ct);
3629 /* there better be xmt and rcv payloads */
3630 if ((!job->request_payload.payload_len) ||
3631 (!job->reply_payload.payload_len)) {
3637 case FC_BSG_HST_VENDOR:
3638 cmdlen += sizeof(struct fc_bsg_host_vendor);
3639 if ((shost->hostt->vendor_id == 0L) ||
3640 (bsg_request->rqst_data.h_vendor.vendor_id !=
3641 shost->hostt->vendor_id)) {
3652 ret = i->f->bsg_request(job);
3657 /* return the errno failure code as the only status */
3658 BUG_ON(job->reply_len < sizeof(uint32_t));
3659 bsg_reply->reply_payload_rcv_len = 0;
3660 bsg_reply->result = ret;
3661 job->reply_len = sizeof(uint32_t);
3662 bsg_job_done(job, bsg_reply->result,
3663 bsg_reply->reply_payload_rcv_len);
3669 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3670 * @rport: rport to be restarted
3673 fc_bsg_goose_queue(struct fc_rport *rport)
3675 struct request_queue *q = rport->rqst_q;
3678 blk_mq_run_hw_queues(q, true);
3682 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3683 * @shost: scsi host rport attached to
3684 * @job: bsg job to be processed
3686 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3688 struct fc_internal *i = to_fc_internal(shost->transportt);
3689 struct fc_bsg_request *bsg_request = job->request;
3690 struct fc_bsg_reply *bsg_reply = job->reply;
3691 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3694 /* check if we really have all the request data needed */
3695 if (job->request_len < cmdlen) {
3697 goto fail_rport_msg;
3700 /* Validate the rport command */
3701 switch (bsg_request->msgcode) {
3702 case FC_BSG_RPT_ELS:
3703 cmdlen += sizeof(struct fc_bsg_rport_els);
3707 cmdlen += sizeof(struct fc_bsg_rport_ct);
3709 /* there better be xmt and rcv payloads */
3710 if ((!job->request_payload.payload_len) ||
3711 (!job->reply_payload.payload_len)) {
3713 goto fail_rport_msg;
3718 goto fail_rport_msg;
3721 ret = i->f->bsg_request(job);
3726 /* return the errno failure code as the only status */
3727 BUG_ON(job->reply_len < sizeof(uint32_t));
3728 bsg_reply->reply_payload_rcv_len = 0;
3729 bsg_reply->result = ret;
3730 job->reply_len = sizeof(uint32_t);
3731 bsg_job_done(job, bsg_reply->result,
3732 bsg_reply->reply_payload_rcv_len);
3736 static int fc_bsg_dispatch(struct bsg_job *job)
3738 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3740 if (scsi_is_fc_rport(job->dev))
3741 return fc_bsg_rport_dispatch(shost, job);
3743 return fc_bsg_host_dispatch(shost, job);
3746 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
3748 if (rport->port_state == FC_PORTSTATE_BLOCKED &&
3749 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3750 return BLK_STS_RESOURCE;
3752 if (rport->port_state != FC_PORTSTATE_ONLINE)
3753 return BLK_STS_IOERR;
3759 static int fc_bsg_dispatch_prep(struct bsg_job *job)
3761 struct fc_rport *rport = fc_bsg_to_rport(job);
3764 ret = fc_bsg_rport_prep(rport);
3768 case BLK_STS_RESOURCE:
3774 return fc_bsg_dispatch(job);
3778 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3779 * @shost: shost for fc_host
3780 * @fc_host: fc_host adding the structures to
3783 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3785 struct device *dev = &shost->shost_gendev;
3786 struct fc_internal *i = to_fc_internal(shost->transportt);
3787 struct request_queue *q;
3790 fc_host->rqst_q = NULL;
3792 if (!i->f->bsg_request)
3795 snprintf(bsg_name, sizeof(bsg_name),
3796 "fc_host%d", shost->host_no);
3798 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout,
3802 "fc_host%d: bsg interface failed to initialize - setup queue\n",
3806 __scsi_init_queue(shost, q);
3807 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3808 fc_host->rqst_q = q;
3813 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3814 * @shost: shost that rport is attached to
3815 * @rport: rport that the bsg hooks are being attached to
3818 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3820 struct device *dev = &rport->dev;
3821 struct fc_internal *i = to_fc_internal(shost->transportt);
3822 struct request_queue *q;
3824 rport->rqst_q = NULL;
3826 if (!i->f->bsg_request)
3829 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep,
3830 fc_bsg_job_timeout, i->f->dd_bsg_size);
3832 dev_err(dev, "failed to setup bsg queue\n");
3835 __scsi_init_queue(shost, q);
3836 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3843 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3844 * @q: the request_queue that is to be torn down.
3847 * Before unregistering the queue empty any requests that are blocked
3852 fc_bsg_remove(struct request_queue *q)
3854 bsg_remove_queue(q);
3858 /* Original Author: Martin Hicks */
3859 MODULE_AUTHOR("James Smart");
3860 MODULE_DESCRIPTION("FC Transport Attributes");
3861 MODULE_LICENSE("GPL");
3863 module_init(fc_transport_init);
3864 module_exit(fc_transport_exit);