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.
6 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
7 * Rewrite for host, target, device, and remote port attributes,
8 * statistics, and service functions...
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/bsg-lib.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_transport.h>
20 #include <scsi/scsi_transport_fc.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <net/netlink.h>
23 #include <scsi/scsi_netlink_fc.h>
24 #include <scsi/scsi_bsg_fc.h>
25 #include "scsi_priv.h"
27 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
28 static void fc_vport_sched_delete(struct work_struct *work);
29 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
30 struct device *pdev, struct fc_vport_identifiers *ids,
31 struct fc_vport **vport);
32 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
33 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
34 static void fc_bsg_remove(struct request_queue *);
35 static void fc_bsg_goose_queue(struct fc_rport *);
42 * dev_loss_tmo: the default number of seconds that the FC transport
43 * should insulate the loss of a remote port.
44 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
46 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
48 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
49 MODULE_PARM_DESC(dev_loss_tmo,
50 "Maximum number of seconds that the FC transport should"
51 " insulate the loss of a remote port. Once this value is"
52 " exceeded, the scsi target is removed. Value should be"
53 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
54 " fast_io_fail_tmo is not set.");
57 * Redefine so that we can have same named attributes in the
58 * sdev/starget/host objects.
60 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
61 struct device_attribute device_attr_##_prefix##_##_name = \
62 __ATTR(_name,_mode,_show,_store)
64 #define fc_enum_name_search(title, table_type, table) \
65 static const char *get_fc_##title##_name(enum table_type table_key) \
70 for (i = 0; i < ARRAY_SIZE(table); i++) { \
71 if (table[i].value == table_key) { \
72 name = table[i].name; \
79 #define fc_enum_name_match(title, table_type, table) \
80 static int get_fc_##title##_match(const char *table_key, \
81 enum table_type *value) \
85 for (i = 0; i < ARRAY_SIZE(table); i++) { \
86 if (strncmp(table_key, table[i].name, \
87 table[i].matchlen) == 0) { \
88 *value = table[i].value; \
89 return 0; /* success */ \
92 return 1; /* failure */ \
96 /* Convert fc_port_type values to ascii string name */
98 enum fc_port_type value;
100 } fc_port_type_names[] = {
101 { FC_PORTTYPE_UNKNOWN, "Unknown" },
102 { FC_PORTTYPE_OTHER, "Other" },
103 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
104 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
105 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
106 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
107 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
108 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
110 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
111 #define FC_PORTTYPE_MAX_NAMELEN 50
113 /* Reuse fc_port_type enum function for vport_type */
114 #define get_fc_vport_type_name get_fc_port_type_name
117 /* Convert fc_host_event_code values to ascii string name */
118 static const struct {
119 enum fc_host_event_code value;
121 } fc_host_event_code_names[] = {
122 { FCH_EVT_LIP, "lip" },
123 { FCH_EVT_LINKUP, "link_up" },
124 { FCH_EVT_LINKDOWN, "link_down" },
125 { FCH_EVT_LIPRESET, "lip_reset" },
126 { FCH_EVT_RSCN, "rscn" },
127 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
128 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
129 { FCH_EVT_PORT_ONLINE, "port_online" },
130 { FCH_EVT_PORT_OFFLINE, "port_offline" },
131 { FCH_EVT_PORT_FABRIC, "port_fabric" },
132 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
133 { FCH_EVT_LINK_FPIN, "link_FPIN" },
134 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
136 fc_enum_name_search(host_event_code, fc_host_event_code,
137 fc_host_event_code_names)
138 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
141 /* Convert fc_port_state values to ascii string name */
143 enum fc_port_state value;
145 } fc_port_state_names[] = {
146 { FC_PORTSTATE_UNKNOWN, "Unknown" },
147 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
148 { FC_PORTSTATE_ONLINE, "Online" },
149 { FC_PORTSTATE_OFFLINE, "Offline" },
150 { FC_PORTSTATE_BLOCKED, "Blocked" },
151 { FC_PORTSTATE_BYPASSED, "Bypassed" },
152 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
153 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
154 { FC_PORTSTATE_ERROR, "Error" },
155 { FC_PORTSTATE_LOOPBACK, "Loopback" },
156 { FC_PORTSTATE_DELETED, "Deleted" },
158 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
159 #define FC_PORTSTATE_MAX_NAMELEN 20
162 /* Convert fc_vport_state values to ascii string name */
164 enum fc_vport_state value;
166 } fc_vport_state_names[] = {
167 { FC_VPORT_UNKNOWN, "Unknown" },
168 { FC_VPORT_ACTIVE, "Active" },
169 { FC_VPORT_DISABLED, "Disabled" },
170 { FC_VPORT_LINKDOWN, "Linkdown" },
171 { FC_VPORT_INITIALIZING, "Initializing" },
172 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
173 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
174 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
175 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
176 { FC_VPORT_FAILED, "VPort Failed" },
178 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
179 #define FC_VPORTSTATE_MAX_NAMELEN 24
181 /* Reuse fc_vport_state enum function for vport_last_state */
182 #define get_fc_vport_last_state_name get_fc_vport_state_name
185 /* Convert fc_tgtid_binding_type values to ascii string name */
186 static const struct {
187 enum fc_tgtid_binding_type value;
190 } fc_tgtid_binding_type_names[] = {
191 { FC_TGTID_BIND_NONE, "none", 4 },
192 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
193 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
194 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
196 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
197 fc_tgtid_binding_type_names)
198 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
199 fc_tgtid_binding_type_names)
200 #define FC_BINDTYPE_MAX_NAMELEN 30
203 #define fc_bitfield_name_search(title, table) \
205 get_fc_##title##_names(u32 table_key, char *buf) \
211 for (i = 0; i < ARRAY_SIZE(table); i++) { \
212 if (table[i].value & table_key) { \
213 len += sprintf(buf + len, "%s%s", \
214 prefix, table[i].name); \
218 len += sprintf(buf + len, "\n"); \
223 /* Convert FC_COS bit values to ascii string name */
224 static const struct {
228 { FC_COS_CLASS1, "Class 1" },
229 { FC_COS_CLASS2, "Class 2" },
230 { FC_COS_CLASS3, "Class 3" },
231 { FC_COS_CLASS4, "Class 4" },
232 { FC_COS_CLASS6, "Class 6" },
234 fc_bitfield_name_search(cos, fc_cos_names)
237 /* Convert FC_PORTSPEED bit values to ascii string name */
238 static const struct {
241 } fc_port_speed_names[] = {
242 { FC_PORTSPEED_1GBIT, "1 Gbit" },
243 { FC_PORTSPEED_2GBIT, "2 Gbit" },
244 { FC_PORTSPEED_4GBIT, "4 Gbit" },
245 { FC_PORTSPEED_10GBIT, "10 Gbit" },
246 { FC_PORTSPEED_8GBIT, "8 Gbit" },
247 { FC_PORTSPEED_16GBIT, "16 Gbit" },
248 { FC_PORTSPEED_32GBIT, "32 Gbit" },
249 { FC_PORTSPEED_20GBIT, "20 Gbit" },
250 { FC_PORTSPEED_40GBIT, "40 Gbit" },
251 { FC_PORTSPEED_50GBIT, "50 Gbit" },
252 { FC_PORTSPEED_100GBIT, "100 Gbit" },
253 { FC_PORTSPEED_25GBIT, "25 Gbit" },
254 { FC_PORTSPEED_64GBIT, "64 Gbit" },
255 { FC_PORTSPEED_128GBIT, "128 Gbit" },
256 { FC_PORTSPEED_256GBIT, "256 Gbit" },
257 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
259 fc_bitfield_name_search(port_speed, fc_port_speed_names)
263 show_fc_fc4s (char *buf, u8 *fc4_list)
267 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
268 len += sprintf(buf + len , "0x%02x ", *fc4_list);
269 len += sprintf(buf + len, "\n");
274 /* Convert FC_PORT_ROLE bit values to ascii string name */
275 static const struct {
278 } fc_port_role_names[] = {
279 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
280 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
281 { FC_PORT_ROLE_IP_PORT, "IP Port" },
282 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
283 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
284 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
285 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
287 fc_bitfield_name_search(port_roles, fc_port_role_names)
290 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
292 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
293 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
294 #define FC_FPORT_PORTID 0x00000e
295 #define FC_FABCTLR_PORTID 0x00000d
296 #define FC_DIRSRVR_PORTID 0x00000c
297 #define FC_TIMESRVR_PORTID 0x00000b
298 #define FC_MGMTSRVR_PORTID 0x00000a
301 static void fc_timeout_deleted_rport(struct work_struct *work);
302 static void fc_timeout_fail_rport_io(struct work_struct *work);
303 static void fc_scsi_scan_rport(struct work_struct *work);
306 * Attribute counts pre object type...
307 * Increase these values if you add attributes
309 #define FC_STARGET_NUM_ATTRS 3
310 #define FC_RPORT_NUM_ATTRS 10
311 #define FC_VPORT_NUM_ATTRS 9
312 #define FC_HOST_NUM_ATTRS 29
315 struct scsi_transport_template t;
316 struct fc_function_template *f;
319 * For attributes : each object has :
320 * An array of the actual attributes structures
321 * An array of null-terminated pointers to the attribute
322 * structures - used for mid-layer interaction.
324 * The attribute containers for the starget and host are are
325 * part of the midlayer. As the remote port is specific to the
326 * fc transport, we must provide the attribute container.
328 struct device_attribute private_starget_attrs[
329 FC_STARGET_NUM_ATTRS];
330 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
332 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
333 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
335 struct transport_container rport_attr_cont;
336 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
337 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
339 struct transport_container vport_attr_cont;
340 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
341 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
344 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
346 static int fc_target_setup(struct transport_container *tc, struct device *dev,
349 struct scsi_target *starget = to_scsi_target(dev);
350 struct fc_rport *rport = starget_to_rport(starget);
353 * if parent is remote port, use values from remote port.
354 * Otherwise, this host uses the fc_transport, but not the
355 * remote port interface. As such, initialize to known non-values.
358 fc_starget_node_name(starget) = rport->node_name;
359 fc_starget_port_name(starget) = rport->port_name;
360 fc_starget_port_id(starget) = rport->port_id;
362 fc_starget_node_name(starget) = -1;
363 fc_starget_port_name(starget) = -1;
364 fc_starget_port_id(starget) = -1;
370 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
376 static int fc_host_setup(struct transport_container *tc, struct device *dev,
379 struct Scsi_Host *shost = dev_to_shost(dev);
380 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
383 * Set default values easily detected by the midlayer as
384 * failure cases. The scsi lldd is responsible for initializing
385 * all transport attributes to valid values per host.
387 fc_host->node_name = -1;
388 fc_host->port_name = -1;
389 fc_host->permanent_port_name = -1;
390 fc_host->supported_classes = FC_COS_UNSPECIFIED;
391 memset(fc_host->supported_fc4s, 0,
392 sizeof(fc_host->supported_fc4s));
393 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
394 fc_host->maxframe_size = -1;
395 fc_host->max_npiv_vports = 0;
396 memset(fc_host->serial_number, 0,
397 sizeof(fc_host->serial_number));
398 memset(fc_host->manufacturer, 0,
399 sizeof(fc_host->manufacturer));
400 memset(fc_host->model, 0,
401 sizeof(fc_host->model));
402 memset(fc_host->model_description, 0,
403 sizeof(fc_host->model_description));
404 memset(fc_host->hardware_version, 0,
405 sizeof(fc_host->hardware_version));
406 memset(fc_host->driver_version, 0,
407 sizeof(fc_host->driver_version));
408 memset(fc_host->firmware_version, 0,
409 sizeof(fc_host->firmware_version));
410 memset(fc_host->optionrom_version, 0,
411 sizeof(fc_host->optionrom_version));
413 fc_host->port_id = -1;
414 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
415 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
416 memset(fc_host->active_fc4s, 0,
417 sizeof(fc_host->active_fc4s));
418 fc_host->speed = FC_PORTSPEED_UNKNOWN;
419 fc_host->fabric_name = -1;
420 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
421 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
423 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
425 INIT_LIST_HEAD(&fc_host->rports);
426 INIT_LIST_HEAD(&fc_host->rport_bindings);
427 INIT_LIST_HEAD(&fc_host->vports);
428 fc_host->next_rport_number = 0;
429 fc_host->next_target_id = 0;
430 fc_host->next_vport_number = 0;
431 fc_host->npiv_vports_inuse = 0;
433 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
434 "fc_wq_%d", shost->host_no);
435 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
436 if (!fc_host->work_q)
439 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
440 snprintf(fc_host->devloss_work_q_name,
441 sizeof(fc_host->devloss_work_q_name),
442 "fc_dl_%d", shost->host_no);
443 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
444 fc_host->devloss_work_q_name);
445 if (!fc_host->devloss_work_q) {
446 destroy_workqueue(fc_host->work_q);
447 fc_host->work_q = NULL;
451 fc_bsg_hostadd(shost, fc_host);
452 /* ignore any bsg add error - we just can't do sgio */
457 static int fc_host_remove(struct transport_container *tc, struct device *dev,
460 struct Scsi_Host *shost = dev_to_shost(dev);
461 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
463 fc_bsg_remove(fc_host->rqst_q);
467 static DECLARE_TRANSPORT_CLASS(fc_host_class,
474 * Setup and Remove actions for remote ports are handled
475 * in the service functions below.
477 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
484 * Setup and Remove actions for virtual ports are handled
485 * in the service functions below.
487 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
494 * Netlink Infrastructure
497 static atomic_t fc_event_seq;
500 * fc_get_event_number - Obtain the next sequential FC event number
503 * We could have inlined this, but it would have required fc_event_seq to
504 * be exposed. For now, live with the subroutine call.
505 * Atomic used to avoid lock/unlock...
508 fc_get_event_number(void)
510 return atomic_add_return(1, &fc_event_seq);
512 EXPORT_SYMBOL(fc_get_event_number);
515 * fc_host_post_fc_event - routine to do the work of posting an event
517 * @shost: host the event occurred on
518 * @event_number: fc event number obtained from get_fc_event_number()
519 * @event_code: fc_host event being posted
520 * @data_len: amount, in bytes, of event data
521 * @data_buf: pointer to event data
522 * @vendor_id: value for Vendor id
525 * This routine assumes no locks are held on entry.
528 fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
529 enum fc_host_event_code event_code,
530 u32 data_len, char *data_buf, u64 vendor_id)
533 struct nlmsghdr *nlh;
534 struct fc_nl_event *event;
539 if (!data_buf || data_len < 4)
547 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
549 skb = nlmsg_new(len, GFP_KERNEL);
555 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
560 event = nlmsg_data(nlh);
562 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
563 FC_NL_ASYNC_EVENT, len);
564 event->seconds = ktime_get_real_seconds();
565 event->vendor_id = vendor_id;
566 event->host_no = shost->host_no;
567 event->event_datalen = data_len; /* bytes */
568 event->event_num = event_number;
569 event->event_code = event_code;
571 memcpy(&event->event_data, data_buf, data_len);
573 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
580 name = get_fc_host_event_code_name(event_code);
582 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
583 __func__, shost->host_no,
584 (name) ? name : "<unknown>",
585 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
588 EXPORT_SYMBOL(fc_host_post_fc_event);
591 * fc_host_post_event - called to post an even on an fc_host.
592 * @shost: host the event occurred on
593 * @event_number: fc event number obtained from get_fc_event_number()
594 * @event_code: fc_host event being posted
595 * @event_data: 32bits of data for the event being posted
598 * This routine assumes no locks are held on entry.
601 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
602 enum fc_host_event_code event_code, u32 event_data)
604 fc_host_post_fc_event(shost, event_number, event_code,
605 (u32)sizeof(u32), (char *)&event_data, 0);
607 EXPORT_SYMBOL(fc_host_post_event);
611 * fc_host_post_vendor_event - called to post a vendor unique event
613 * @shost: host the event occurred on
614 * @event_number: fc event number obtained from get_fc_event_number()
615 * @data_len: amount, in bytes, of vendor unique data
616 * @data_buf: pointer to vendor unique data
617 * @vendor_id: Vendor id
620 * This routine assumes no locks are held on entry.
623 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
624 u32 data_len, char * data_buf, u64 vendor_id)
626 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
627 data_len, data_buf, vendor_id);
629 EXPORT_SYMBOL(fc_host_post_vendor_event);
632 * fc_host_rcv_fpin - routine to process a received FPIN.
633 * @shost: host the FPIN was received on
634 * @fpin_len: length of FPIN payload, in bytes
635 * @fpin_buf: pointer to FPIN payload
638 * This routine assumes no locks are held on entry.
641 fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf)
643 fc_host_post_fc_event(shost, fc_get_event_number(),
644 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0);
646 EXPORT_SYMBOL(fc_host_fpin_rcv);
649 static __init int fc_transport_init(void)
653 atomic_set(&fc_event_seq, 0);
655 error = transport_class_register(&fc_host_class);
658 error = transport_class_register(&fc_vport_class);
660 goto unreg_host_class;
661 error = transport_class_register(&fc_rport_class);
663 goto unreg_vport_class;
664 error = transport_class_register(&fc_transport_class);
666 goto unreg_rport_class;
670 transport_class_unregister(&fc_rport_class);
672 transport_class_unregister(&fc_vport_class);
674 transport_class_unregister(&fc_host_class);
678 static void __exit fc_transport_exit(void)
680 transport_class_unregister(&fc_transport_class);
681 transport_class_unregister(&fc_rport_class);
682 transport_class_unregister(&fc_host_class);
683 transport_class_unregister(&fc_vport_class);
687 * FC Remote Port Attribute Management
690 #define fc_rport_show_function(field, format_string, sz, cast) \
692 show_fc_rport_##field (struct device *dev, \
693 struct device_attribute *attr, char *buf) \
695 struct fc_rport *rport = transport_class_to_rport(dev); \
696 struct Scsi_Host *shost = rport_to_shost(rport); \
697 struct fc_internal *i = to_fc_internal(shost->transportt); \
698 if ((i->f->get_rport_##field) && \
699 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
700 (rport->port_state == FC_PORTSTATE_DELETED) || \
701 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
702 i->f->get_rport_##field(rport); \
703 return snprintf(buf, sz, format_string, cast rport->field); \
706 #define fc_rport_store_function(field) \
708 store_fc_rport_##field(struct device *dev, \
709 struct device_attribute *attr, \
710 const char *buf, size_t count) \
713 struct fc_rport *rport = transport_class_to_rport(dev); \
714 struct Scsi_Host *shost = rport_to_shost(rport); \
715 struct fc_internal *i = to_fc_internal(shost->transportt); \
717 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
718 (rport->port_state == FC_PORTSTATE_DELETED) || \
719 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
721 val = simple_strtoul(buf, &cp, 0); \
722 if (*cp && (*cp != '\n')) \
724 i->f->set_rport_##field(rport, val); \
728 #define fc_rport_rd_attr(field, format_string, sz) \
729 fc_rport_show_function(field, format_string, sz, ) \
730 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
731 show_fc_rport_##field, NULL)
733 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
734 fc_rport_show_function(field, format_string, sz, (cast)) \
735 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
736 show_fc_rport_##field, NULL)
738 #define fc_rport_rw_attr(field, format_string, sz) \
739 fc_rport_show_function(field, format_string, sz, ) \
740 fc_rport_store_function(field) \
741 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
742 show_fc_rport_##field, \
743 store_fc_rport_##field)
746 #define fc_private_rport_show_function(field, format_string, sz, cast) \
748 show_fc_rport_##field (struct device *dev, \
749 struct device_attribute *attr, char *buf) \
751 struct fc_rport *rport = transport_class_to_rport(dev); \
752 return snprintf(buf, sz, format_string, cast rport->field); \
755 #define fc_private_rport_rd_attr(field, format_string, sz) \
756 fc_private_rport_show_function(field, format_string, sz, ) \
757 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
758 show_fc_rport_##field, NULL)
760 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
761 fc_private_rport_show_function(field, format_string, sz, (cast)) \
762 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
763 show_fc_rport_##field, NULL)
766 #define fc_private_rport_rd_enum_attr(title, maxlen) \
768 show_fc_rport_##title (struct device *dev, \
769 struct device_attribute *attr, char *buf) \
771 struct fc_rport *rport = transport_class_to_rport(dev); \
773 name = get_fc_##title##_name(rport->title); \
776 return snprintf(buf, maxlen, "%s\n", name); \
778 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
779 show_fc_rport_##title, NULL)
782 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
783 i->private_rport_attrs[count] = device_attr_rport_##field; \
784 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
785 i->private_rport_attrs[count].store = NULL; \
786 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
787 if (i->f->show_rport_##field) \
790 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
791 i->private_rport_attrs[count] = device_attr_rport_##field; \
792 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
793 i->private_rport_attrs[count].store = NULL; \
794 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
797 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
798 i->private_rport_attrs[count] = device_attr_rport_##field; \
799 if (!i->f->set_rport_##field) { \
800 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
801 i->private_rport_attrs[count].store = NULL; \
803 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
804 if (i->f->show_rport_##field) \
807 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
809 i->private_rport_attrs[count] = device_attr_rport_##field; \
810 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
815 /* The FC Transport Remote Port Attributes: */
817 /* Fixed Remote Port Attributes */
819 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
822 show_fc_rport_supported_classes (struct device *dev,
823 struct device_attribute *attr, char *buf)
825 struct fc_rport *rport = transport_class_to_rport(dev);
826 if (rport->supported_classes == FC_COS_UNSPECIFIED)
827 return snprintf(buf, 20, "unspecified\n");
828 return get_fc_cos_names(rport->supported_classes, buf);
830 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
831 show_fc_rport_supported_classes, NULL);
833 /* Dynamic Remote Port Attributes */
836 * dev_loss_tmo attribute
838 static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
842 *val = simple_strtoul(buf, &cp, 0);
843 if (*cp && (*cp != '\n'))
846 * Check for overflow; dev_loss_tmo is u32
854 static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
857 struct Scsi_Host *shost = rport_to_shost(rport);
858 struct fc_internal *i = to_fc_internal(shost->transportt);
860 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
861 (rport->port_state == FC_PORTSTATE_DELETED) ||
862 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
865 * Check for overflow; dev_loss_tmo is u32
871 * If fast_io_fail is off we have to cap
872 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
874 if (rport->fast_io_fail_tmo == -1 &&
875 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
878 i->f->set_rport_dev_loss_tmo(rport, val);
882 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
884 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
885 const char *buf, size_t count)
887 struct fc_rport *rport = transport_class_to_rport(dev);
891 rc = fc_str_to_dev_loss(buf, &val);
895 rc = fc_rport_set_dev_loss_tmo(rport, val);
900 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
901 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
904 /* Private Remote Port Attributes */
906 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
907 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
908 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
911 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
914 struct fc_rport *rport = transport_class_to_rport(dev);
916 /* identify any roles that are port_id specific */
917 if ((rport->port_id != -1) &&
918 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
919 FC_WELLKNOWN_PORTID_MASK) {
920 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
921 case FC_FPORT_PORTID:
922 return snprintf(buf, 30, "Fabric Port\n");
923 case FC_FABCTLR_PORTID:
924 return snprintf(buf, 30, "Fabric Controller\n");
925 case FC_DIRSRVR_PORTID:
926 return snprintf(buf, 30, "Directory Server\n");
927 case FC_TIMESRVR_PORTID:
928 return snprintf(buf, 30, "Time Server\n");
929 case FC_MGMTSRVR_PORTID:
930 return snprintf(buf, 30, "Management Server\n");
932 return snprintf(buf, 30, "Unknown Fabric Entity\n");
935 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
936 return snprintf(buf, 20, "unknown\n");
937 return get_fc_port_roles_names(rport->roles, buf);
940 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
941 show_fc_rport_roles, NULL);
943 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
944 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
947 * fast_io_fail_tmo attribute
950 show_fc_rport_fast_io_fail_tmo (struct device *dev,
951 struct device_attribute *attr, char *buf)
953 struct fc_rport *rport = transport_class_to_rport(dev);
955 if (rport->fast_io_fail_tmo == -1)
956 return snprintf(buf, 5, "off\n");
957 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
961 store_fc_rport_fast_io_fail_tmo(struct device *dev,
962 struct device_attribute *attr, const char *buf,
967 struct fc_rport *rport = transport_class_to_rport(dev);
969 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
970 (rport->port_state == FC_PORTSTATE_DELETED) ||
971 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
973 if (strncmp(buf, "off", 3) == 0)
974 rport->fast_io_fail_tmo = -1;
976 val = simple_strtoul(buf, &cp, 0);
977 if ((*cp && (*cp != '\n')) || (val < 0))
980 * Cap fast_io_fail by dev_loss_tmo or
981 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
983 if ((val >= rport->dev_loss_tmo) ||
984 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
987 rport->fast_io_fail_tmo = val;
991 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
992 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
996 * FC SCSI Target Attribute Management
1000 * Note: in the target show function we recognize when the remote
1001 * port is in the hierarchy and do not allow the driver to get
1002 * involved in sysfs functions. The driver only gets involved if
1003 * it's the "old" style that doesn't use rports.
1005 #define fc_starget_show_function(field, format_string, sz, cast) \
1007 show_fc_starget_##field (struct device *dev, \
1008 struct device_attribute *attr, char *buf) \
1010 struct scsi_target *starget = transport_class_to_starget(dev); \
1011 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1012 struct fc_internal *i = to_fc_internal(shost->transportt); \
1013 struct fc_rport *rport = starget_to_rport(starget); \
1015 fc_starget_##field(starget) = rport->field; \
1016 else if (i->f->get_starget_##field) \
1017 i->f->get_starget_##field(starget); \
1018 return snprintf(buf, sz, format_string, \
1019 cast fc_starget_##field(starget)); \
1022 #define fc_starget_rd_attr(field, format_string, sz) \
1023 fc_starget_show_function(field, format_string, sz, ) \
1024 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1025 show_fc_starget_##field, NULL)
1027 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1028 fc_starget_show_function(field, format_string, sz, (cast)) \
1029 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1030 show_fc_starget_##field, NULL)
1032 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
1033 i->private_starget_attrs[count] = device_attr_starget_##field; \
1034 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1035 i->private_starget_attrs[count].store = NULL; \
1036 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1037 if (i->f->show_starget_##field) \
1040 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
1041 i->private_starget_attrs[count] = device_attr_starget_##field; \
1042 if (!i->f->set_starget_##field) { \
1043 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1044 i->private_starget_attrs[count].store = NULL; \
1046 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1047 if (i->f->show_starget_##field) \
1050 /* The FC Transport SCSI Target Attributes: */
1051 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1052 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1053 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1057 * FC Virtual Port Attribute Management
1060 #define fc_vport_show_function(field, format_string, sz, cast) \
1062 show_fc_vport_##field (struct device *dev, \
1063 struct device_attribute *attr, char *buf) \
1065 struct fc_vport *vport = transport_class_to_vport(dev); \
1066 struct Scsi_Host *shost = vport_to_shost(vport); \
1067 struct fc_internal *i = to_fc_internal(shost->transportt); \
1068 if ((i->f->get_vport_##field) && \
1069 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1070 i->f->get_vport_##field(vport); \
1071 return snprintf(buf, sz, format_string, cast vport->field); \
1074 #define fc_vport_store_function(field) \
1076 store_fc_vport_##field(struct device *dev, \
1077 struct device_attribute *attr, \
1078 const char *buf, size_t count) \
1081 struct fc_vport *vport = transport_class_to_vport(dev); \
1082 struct Scsi_Host *shost = vport_to_shost(vport); \
1083 struct fc_internal *i = to_fc_internal(shost->transportt); \
1085 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1087 val = simple_strtoul(buf, &cp, 0); \
1088 if (*cp && (*cp != '\n')) \
1090 i->f->set_vport_##field(vport, val); \
1094 #define fc_vport_store_str_function(field, slen) \
1096 store_fc_vport_##field(struct device *dev, \
1097 struct device_attribute *attr, \
1098 const char *buf, size_t count) \
1100 struct fc_vport *vport = transport_class_to_vport(dev); \
1101 struct Scsi_Host *shost = vport_to_shost(vport); \
1102 struct fc_internal *i = to_fc_internal(shost->transportt); \
1103 unsigned int cnt=count; \
1105 /* count may include a LF at end of string */ \
1106 if (buf[cnt-1] == '\n') \
1108 if (cnt > ((slen) - 1)) \
1110 memcpy(vport->field, buf, cnt); \
1111 i->f->set_vport_##field(vport); \
1115 #define fc_vport_rd_attr(field, format_string, sz) \
1116 fc_vport_show_function(field, format_string, sz, ) \
1117 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1118 show_fc_vport_##field, NULL)
1120 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1121 fc_vport_show_function(field, format_string, sz, (cast)) \
1122 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1123 show_fc_vport_##field, NULL)
1125 #define fc_vport_rw_attr(field, format_string, sz) \
1126 fc_vport_show_function(field, format_string, sz, ) \
1127 fc_vport_store_function(field) \
1128 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1129 show_fc_vport_##field, \
1130 store_fc_vport_##field)
1132 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1134 show_fc_vport_##field (struct device *dev, \
1135 struct device_attribute *attr, char *buf) \
1137 struct fc_vport *vport = transport_class_to_vport(dev); \
1138 return snprintf(buf, sz, format_string, cast vport->field); \
1141 #define fc_private_vport_store_u32_function(field) \
1143 store_fc_vport_##field(struct device *dev, \
1144 struct device_attribute *attr, \
1145 const char *buf, size_t count) \
1148 struct fc_vport *vport = transport_class_to_vport(dev); \
1150 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1152 val = simple_strtoul(buf, &cp, 0); \
1153 if (*cp && (*cp != '\n')) \
1155 vport->field = val; \
1160 #define fc_private_vport_rd_attr(field, format_string, sz) \
1161 fc_private_vport_show_function(field, format_string, sz, ) \
1162 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1163 show_fc_vport_##field, NULL)
1165 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1166 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1167 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1168 show_fc_vport_##field, NULL)
1170 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1171 fc_private_vport_show_function(field, format_string, sz, ) \
1172 fc_private_vport_store_u32_function(field) \
1173 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1174 show_fc_vport_##field, \
1175 store_fc_vport_##field)
1178 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1180 show_fc_vport_##title (struct device *dev, \
1181 struct device_attribute *attr, \
1184 struct fc_vport *vport = transport_class_to_vport(dev); \
1186 name = get_fc_##title##_name(vport->title); \
1189 return snprintf(buf, maxlen, "%s\n", name); \
1191 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1192 show_fc_vport_##title, NULL)
1195 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1196 i->private_vport_attrs[count] = device_attr_vport_##field; \
1197 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1198 i->private_vport_attrs[count].store = NULL; \
1199 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1200 if (i->f->get_##field) \
1202 /* NOTE: Above MACRO differs: checks function not show bit */
1204 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1205 i->private_vport_attrs[count] = device_attr_vport_##field; \
1206 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1207 i->private_vport_attrs[count].store = NULL; \
1208 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1211 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1212 i->private_vport_attrs[count] = device_attr_vport_##field; \
1213 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1216 /* NOTE: Above MACRO differs: checks function */
1218 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1219 i->private_vport_attrs[count] = device_attr_vport_##field; \
1220 if (!i->f->set_vport_##field) { \
1221 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1222 i->private_vport_attrs[count].store = NULL; \
1224 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1226 /* NOTE: Above MACRO differs: does not check show bit */
1228 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1230 i->private_vport_attrs[count] = device_attr_vport_##field; \
1231 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1236 /* The FC Transport Virtual Port Attributes: */
1238 /* Fixed Virtual Port Attributes */
1240 /* Dynamic Virtual Port Attributes */
1242 /* Private Virtual Port Attributes */
1244 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1245 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1246 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1247 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1250 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1253 struct fc_vport *vport = transport_class_to_vport(dev);
1255 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1256 return snprintf(buf, 20, "unknown\n");
1257 return get_fc_port_roles_names(vport->roles, buf);
1259 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1261 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1263 fc_private_vport_show_function(symbolic_name, "%s\n",
1264 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1265 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1266 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1267 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1270 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1271 const char *buf, size_t count)
1273 struct fc_vport *vport = transport_class_to_vport(dev);
1274 struct Scsi_Host *shost = vport_to_shost(vport);
1275 unsigned long flags;
1277 spin_lock_irqsave(shost->host_lock, flags);
1278 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) {
1279 spin_unlock_irqrestore(shost->host_lock, flags);
1282 vport->flags |= FC_VPORT_DELETING;
1283 spin_unlock_irqrestore(shost->host_lock, flags);
1285 fc_queue_work(shost, &vport->vport_delete_work);
1288 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1289 NULL, store_fc_vport_delete);
1293 * Enable/Disable vport
1294 * Write "1" to disable, write "0" to enable
1297 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1301 struct fc_vport *vport = transport_class_to_vport(dev);
1302 struct Scsi_Host *shost = vport_to_shost(vport);
1303 struct fc_internal *i = to_fc_internal(shost->transportt);
1306 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1310 if (vport->vport_state != FC_VPORT_DISABLED)
1312 } else if (*buf == '1') {
1313 if (vport->vport_state == FC_VPORT_DISABLED)
1318 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1319 return stat ? stat : count;
1321 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1322 NULL, store_fc_vport_disable);
1326 * Host Attribute Management
1329 #define fc_host_show_function(field, format_string, sz, cast) \
1331 show_fc_host_##field (struct device *dev, \
1332 struct device_attribute *attr, char *buf) \
1334 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1335 struct fc_internal *i = to_fc_internal(shost->transportt); \
1336 if (i->f->get_host_##field) \
1337 i->f->get_host_##field(shost); \
1338 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1341 #define fc_host_store_function(field) \
1343 store_fc_host_##field(struct device *dev, \
1344 struct device_attribute *attr, \
1345 const char *buf, size_t count) \
1348 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1349 struct fc_internal *i = to_fc_internal(shost->transportt); \
1352 val = simple_strtoul(buf, &cp, 0); \
1353 if (*cp && (*cp != '\n')) \
1355 i->f->set_host_##field(shost, val); \
1359 #define fc_host_store_str_function(field, slen) \
1361 store_fc_host_##field(struct device *dev, \
1362 struct device_attribute *attr, \
1363 const char *buf, size_t count) \
1365 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1366 struct fc_internal *i = to_fc_internal(shost->transportt); \
1367 unsigned int cnt=count; \
1369 /* count may include a LF at end of string */ \
1370 if (buf[cnt-1] == '\n') \
1372 if (cnt > ((slen) - 1)) \
1374 memcpy(fc_host_##field(shost), buf, cnt); \
1375 i->f->set_host_##field(shost); \
1379 #define fc_host_rd_attr(field, format_string, sz) \
1380 fc_host_show_function(field, format_string, sz, ) \
1381 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1382 show_fc_host_##field, NULL)
1384 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1385 fc_host_show_function(field, format_string, sz, (cast)) \
1386 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1387 show_fc_host_##field, NULL)
1389 #define fc_host_rw_attr(field, format_string, sz) \
1390 fc_host_show_function(field, format_string, sz, ) \
1391 fc_host_store_function(field) \
1392 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1393 show_fc_host_##field, \
1394 store_fc_host_##field)
1396 #define fc_host_rd_enum_attr(title, maxlen) \
1398 show_fc_host_##title (struct device *dev, \
1399 struct device_attribute *attr, char *buf) \
1401 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1402 struct fc_internal *i = to_fc_internal(shost->transportt); \
1404 if (i->f->get_host_##title) \
1405 i->f->get_host_##title(shost); \
1406 name = get_fc_##title##_name(fc_host_##title(shost)); \
1409 return snprintf(buf, maxlen, "%s\n", name); \
1411 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1413 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1414 i->private_host_attrs[count] = device_attr_host_##field; \
1415 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1416 i->private_host_attrs[count].store = NULL; \
1417 i->host_attrs[count] = &i->private_host_attrs[count]; \
1418 if (i->f->show_host_##field) \
1421 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1422 i->private_host_attrs[count] = device_attr_host_##field; \
1423 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1424 i->private_host_attrs[count].store = NULL; \
1425 i->host_attrs[count] = &i->private_host_attrs[count]; \
1428 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1429 i->private_host_attrs[count] = device_attr_host_##field; \
1430 if (!i->f->set_host_##field) { \
1431 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1432 i->private_host_attrs[count].store = NULL; \
1434 i->host_attrs[count] = &i->private_host_attrs[count]; \
1435 if (i->f->show_host_##field) \
1439 #define fc_private_host_show_function(field, format_string, sz, cast) \
1441 show_fc_host_##field (struct device *dev, \
1442 struct device_attribute *attr, char *buf) \
1444 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1445 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1448 #define fc_private_host_rd_attr(field, format_string, sz) \
1449 fc_private_host_show_function(field, format_string, sz, ) \
1450 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1451 show_fc_host_##field, NULL)
1453 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1454 fc_private_host_show_function(field, format_string, sz, (cast)) \
1455 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1456 show_fc_host_##field, NULL)
1458 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1459 i->private_host_attrs[count] = device_attr_host_##field; \
1460 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1461 i->private_host_attrs[count].store = NULL; \
1462 i->host_attrs[count] = &i->private_host_attrs[count]; \
1465 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1467 i->private_host_attrs[count] = device_attr_host_##field; \
1468 i->host_attrs[count] = &i->private_host_attrs[count]; \
1473 /* Fixed Host Attributes */
1476 show_fc_host_supported_classes (struct device *dev,
1477 struct device_attribute *attr, char *buf)
1479 struct Scsi_Host *shost = transport_class_to_shost(dev);
1481 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1482 return snprintf(buf, 20, "unspecified\n");
1484 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1486 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1487 show_fc_host_supported_classes, NULL);
1490 show_fc_host_supported_fc4s (struct device *dev,
1491 struct device_attribute *attr, char *buf)
1493 struct Scsi_Host *shost = transport_class_to_shost(dev);
1494 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1496 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1497 show_fc_host_supported_fc4s, NULL);
1500 show_fc_host_supported_speeds (struct device *dev,
1501 struct device_attribute *attr, char *buf)
1503 struct Scsi_Host *shost = transport_class_to_shost(dev);
1505 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1506 return snprintf(buf, 20, "unknown\n");
1508 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1510 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1511 show_fc_host_supported_speeds, NULL);
1514 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1515 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1516 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1517 unsigned long long);
1518 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1519 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1520 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1521 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1522 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1523 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1524 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1525 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1526 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1527 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1530 /* Dynamic Host Attributes */
1533 show_fc_host_active_fc4s (struct device *dev,
1534 struct device_attribute *attr, char *buf)
1536 struct Scsi_Host *shost = transport_class_to_shost(dev);
1537 struct fc_internal *i = to_fc_internal(shost->transportt);
1539 if (i->f->get_host_active_fc4s)
1540 i->f->get_host_active_fc4s(shost);
1542 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1544 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1545 show_fc_host_active_fc4s, NULL);
1548 show_fc_host_speed (struct device *dev,
1549 struct device_attribute *attr, char *buf)
1551 struct Scsi_Host *shost = transport_class_to_shost(dev);
1552 struct fc_internal *i = to_fc_internal(shost->transportt);
1554 if (i->f->get_host_speed)
1555 i->f->get_host_speed(shost);
1557 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1558 return snprintf(buf, 20, "unknown\n");
1560 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1562 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1563 show_fc_host_speed, NULL);
1566 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1567 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1568 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1569 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1570 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1572 fc_private_host_show_function(system_hostname, "%s\n",
1573 FC_SYMBOLIC_NAME_SIZE + 1, )
1574 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1575 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1576 show_fc_host_system_hostname, store_fc_host_system_hostname);
1579 /* Private Host Attributes */
1582 show_fc_private_host_tgtid_bind_type(struct device *dev,
1583 struct device_attribute *attr, char *buf)
1585 struct Scsi_Host *shost = transport_class_to_shost(dev);
1588 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1591 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1594 #define get_list_head_entry(pos, head, member) \
1595 pos = list_entry((head)->next, typeof(*pos), member)
1598 store_fc_private_host_tgtid_bind_type(struct device *dev,
1599 struct device_attribute *attr, const char *buf, size_t count)
1601 struct Scsi_Host *shost = transport_class_to_shost(dev);
1602 struct fc_rport *rport;
1603 enum fc_tgtid_binding_type val;
1604 unsigned long flags;
1606 if (get_fc_tgtid_bind_type_match(buf, &val))
1609 /* if changing bind type, purge all unused consistent bindings */
1610 if (val != fc_host_tgtid_bind_type(shost)) {
1611 spin_lock_irqsave(shost->host_lock, flags);
1612 while (!list_empty(&fc_host_rport_bindings(shost))) {
1613 get_list_head_entry(rport,
1614 &fc_host_rport_bindings(shost), peers);
1615 list_del(&rport->peers);
1616 rport->port_state = FC_PORTSTATE_DELETED;
1617 fc_queue_work(shost, &rport->rport_delete_work);
1619 spin_unlock_irqrestore(shost->host_lock, flags);
1622 fc_host_tgtid_bind_type(shost) = val;
1626 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1627 show_fc_private_host_tgtid_bind_type,
1628 store_fc_private_host_tgtid_bind_type);
1631 store_fc_private_host_issue_lip(struct device *dev,
1632 struct device_attribute *attr, const char *buf, size_t count)
1634 struct Scsi_Host *shost = transport_class_to_shost(dev);
1635 struct fc_internal *i = to_fc_internal(shost->transportt);
1638 /* ignore any data value written to the attribute */
1639 if (i->f->issue_fc_host_lip) {
1640 ret = i->f->issue_fc_host_lip(shost);
1641 return ret ? ret: count;
1647 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1648 store_fc_private_host_issue_lip);
1651 store_fc_private_host_dev_loss_tmo(struct device *dev,
1652 struct device_attribute *attr,
1653 const char *buf, size_t count)
1655 struct Scsi_Host *shost = transport_class_to_shost(dev);
1656 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1657 struct fc_rport *rport;
1658 unsigned long val, flags;
1661 rc = fc_str_to_dev_loss(buf, &val);
1665 fc_host_dev_loss_tmo(shost) = val;
1666 spin_lock_irqsave(shost->host_lock, flags);
1667 list_for_each_entry(rport, &fc_host->rports, peers)
1668 fc_rport_set_dev_loss_tmo(rport, val);
1669 spin_unlock_irqrestore(shost->host_lock, flags);
1673 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
1674 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
1675 show_fc_host_dev_loss_tmo,
1676 store_fc_private_host_dev_loss_tmo);
1678 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1681 * Host Statistics Management
1684 /* Show a given attribute in the statistics group */
1686 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1688 struct Scsi_Host *shost = transport_class_to_shost(dev);
1689 struct fc_internal *i = to_fc_internal(shost->transportt);
1690 struct fc_host_statistics *stats;
1691 ssize_t ret = -ENOENT;
1693 if (offset > sizeof(struct fc_host_statistics) ||
1694 offset % sizeof(u64) != 0)
1697 if (i->f->get_fc_host_stats) {
1698 stats = (i->f->get_fc_host_stats)(shost);
1700 ret = snprintf(buf, 20, "0x%llx\n",
1701 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1707 /* generate a read-only statistics attribute */
1708 #define fc_host_statistic(name) \
1709 static ssize_t show_fcstat_##name(struct device *cd, \
1710 struct device_attribute *attr, \
1713 return fc_stat_show(cd, buf, \
1714 offsetof(struct fc_host_statistics, name)); \
1716 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1718 fc_host_statistic(seconds_since_last_reset);
1719 fc_host_statistic(tx_frames);
1720 fc_host_statistic(tx_words);
1721 fc_host_statistic(rx_frames);
1722 fc_host_statistic(rx_words);
1723 fc_host_statistic(lip_count);
1724 fc_host_statistic(nos_count);
1725 fc_host_statistic(error_frames);
1726 fc_host_statistic(dumped_frames);
1727 fc_host_statistic(link_failure_count);
1728 fc_host_statistic(loss_of_sync_count);
1729 fc_host_statistic(loss_of_signal_count);
1730 fc_host_statistic(prim_seq_protocol_err_count);
1731 fc_host_statistic(invalid_tx_word_count);
1732 fc_host_statistic(invalid_crc_count);
1733 fc_host_statistic(fcp_input_requests);
1734 fc_host_statistic(fcp_output_requests);
1735 fc_host_statistic(fcp_control_requests);
1736 fc_host_statistic(fcp_input_megabytes);
1737 fc_host_statistic(fcp_output_megabytes);
1738 fc_host_statistic(fcp_packet_alloc_failures);
1739 fc_host_statistic(fcp_packet_aborts);
1740 fc_host_statistic(fcp_frame_alloc_failures);
1741 fc_host_statistic(fc_no_free_exch);
1742 fc_host_statistic(fc_no_free_exch_xid);
1743 fc_host_statistic(fc_xid_not_found);
1744 fc_host_statistic(fc_xid_busy);
1745 fc_host_statistic(fc_seq_not_found);
1746 fc_host_statistic(fc_non_bls_resp);
1749 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1750 const char *buf, size_t count)
1752 struct Scsi_Host *shost = transport_class_to_shost(dev);
1753 struct fc_internal *i = to_fc_internal(shost->transportt);
1755 /* ignore any data value written to the attribute */
1756 if (i->f->reset_fc_host_stats) {
1757 i->f->reset_fc_host_stats(shost);
1763 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1764 fc_reset_statistics);
1766 static struct attribute *fc_statistics_attrs[] = {
1767 &device_attr_host_seconds_since_last_reset.attr,
1768 &device_attr_host_tx_frames.attr,
1769 &device_attr_host_tx_words.attr,
1770 &device_attr_host_rx_frames.attr,
1771 &device_attr_host_rx_words.attr,
1772 &device_attr_host_lip_count.attr,
1773 &device_attr_host_nos_count.attr,
1774 &device_attr_host_error_frames.attr,
1775 &device_attr_host_dumped_frames.attr,
1776 &device_attr_host_link_failure_count.attr,
1777 &device_attr_host_loss_of_sync_count.attr,
1778 &device_attr_host_loss_of_signal_count.attr,
1779 &device_attr_host_prim_seq_protocol_err_count.attr,
1780 &device_attr_host_invalid_tx_word_count.attr,
1781 &device_attr_host_invalid_crc_count.attr,
1782 &device_attr_host_fcp_input_requests.attr,
1783 &device_attr_host_fcp_output_requests.attr,
1784 &device_attr_host_fcp_control_requests.attr,
1785 &device_attr_host_fcp_input_megabytes.attr,
1786 &device_attr_host_fcp_output_megabytes.attr,
1787 &device_attr_host_fcp_packet_alloc_failures.attr,
1788 &device_attr_host_fcp_packet_aborts.attr,
1789 &device_attr_host_fcp_frame_alloc_failures.attr,
1790 &device_attr_host_fc_no_free_exch.attr,
1791 &device_attr_host_fc_no_free_exch_xid.attr,
1792 &device_attr_host_fc_xid_not_found.attr,
1793 &device_attr_host_fc_xid_busy.attr,
1794 &device_attr_host_fc_seq_not_found.attr,
1795 &device_attr_host_fc_non_bls_resp.attr,
1796 &device_attr_host_reset_statistics.attr,
1800 static struct attribute_group fc_statistics_group = {
1801 .name = "statistics",
1802 .attrs = fc_statistics_attrs,
1806 /* Host Vport Attributes */
1809 fc_parse_wwn(const char *ns, u64 *nm)
1814 memset(wwn, 0, sizeof(wwn));
1816 /* Validate and store the new name */
1817 for (i=0, j=0; i < 16; i++) {
1820 value = hex_to_bin(*ns++);
1822 j = (j << 4) | value;
1826 wwn[i/2] = j & 0xff;
1831 *nm = wwn_to_u64(wwn);
1838 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1839 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1840 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1841 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1844 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1845 const char *buf, size_t count)
1847 struct Scsi_Host *shost = transport_class_to_shost(dev);
1848 struct fc_vport_identifiers vid;
1849 struct fc_vport *vport;
1850 unsigned int cnt=count;
1853 memset(&vid, 0, sizeof(vid));
1855 /* count may include a LF at end of string */
1856 if (buf[cnt-1] == '\n')
1859 /* validate we have enough characters for WWPN */
1860 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1863 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1867 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1871 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1872 vid.vport_type = FC_PORTTYPE_NPIV;
1873 /* vid.symbolic_name is already zero/NULL's */
1874 vid.disable = false; /* always enabled */
1876 /* we only allow support on Channel 0 !!! */
1877 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1878 return stat ? stat : count;
1880 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1881 store_fc_host_vport_create);
1885 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1886 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1887 * The WWNs are specified as hex characters, and may *not* contain
1888 * any prefixes (e.g. 0x, x, etc)
1891 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1892 const char *buf, size_t count)
1894 struct Scsi_Host *shost = transport_class_to_shost(dev);
1895 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1896 struct fc_vport *vport;
1898 unsigned long flags;
1899 unsigned int cnt=count;
1902 /* count may include a LF at end of string */
1903 if (buf[cnt-1] == '\n')
1906 /* validate we have enough characters for WWPN */
1907 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1910 stat = fc_parse_wwn(&buf[0], &wwpn);
1914 stat = fc_parse_wwn(&buf[17], &wwnn);
1918 spin_lock_irqsave(shost->host_lock, flags);
1920 /* we only allow support on Channel 0 !!! */
1921 list_for_each_entry(vport, &fc_host->vports, peers) {
1922 if ((vport->channel == 0) &&
1923 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1924 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1926 vport->flags |= FC_VPORT_DELETING;
1931 spin_unlock_irqrestore(shost->host_lock, flags);
1936 stat = fc_vport_terminate(vport);
1937 return stat ? stat : count;
1939 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1940 store_fc_host_vport_delete);
1943 static int fc_host_match(struct attribute_container *cont,
1946 struct Scsi_Host *shost;
1947 struct fc_internal *i;
1949 if (!scsi_is_host_device(dev))
1952 shost = dev_to_shost(dev);
1953 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1954 != &fc_host_class.class)
1957 i = to_fc_internal(shost->transportt);
1959 return &i->t.host_attrs.ac == cont;
1962 static int fc_target_match(struct attribute_container *cont,
1965 struct Scsi_Host *shost;
1966 struct fc_internal *i;
1968 if (!scsi_is_target_device(dev))
1971 shost = dev_to_shost(dev->parent);
1972 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1973 != &fc_host_class.class)
1976 i = to_fc_internal(shost->transportt);
1978 return &i->t.target_attrs.ac == cont;
1981 static void fc_rport_dev_release(struct device *dev)
1983 struct fc_rport *rport = dev_to_rport(dev);
1984 put_device(dev->parent);
1988 int scsi_is_fc_rport(const struct device *dev)
1990 return dev->release == fc_rport_dev_release;
1992 EXPORT_SYMBOL(scsi_is_fc_rport);
1994 static int fc_rport_match(struct attribute_container *cont,
1997 struct Scsi_Host *shost;
1998 struct fc_internal *i;
2000 if (!scsi_is_fc_rport(dev))
2003 shost = dev_to_shost(dev->parent);
2004 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2005 != &fc_host_class.class)
2008 i = to_fc_internal(shost->transportt);
2010 return &i->rport_attr_cont.ac == cont;
2014 static void fc_vport_dev_release(struct device *dev)
2016 struct fc_vport *vport = dev_to_vport(dev);
2017 put_device(dev->parent); /* release kobj parent */
2021 static int scsi_is_fc_vport(const struct device *dev)
2023 return dev->release == fc_vport_dev_release;
2026 static int fc_vport_match(struct attribute_container *cont,
2029 struct fc_vport *vport;
2030 struct Scsi_Host *shost;
2031 struct fc_internal *i;
2033 if (!scsi_is_fc_vport(dev))
2035 vport = dev_to_vport(dev);
2037 shost = vport_to_shost(vport);
2038 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2039 != &fc_host_class.class)
2042 i = to_fc_internal(shost->transportt);
2043 return &i->vport_attr_cont.ac == cont;
2048 * fc_eh_timed_out - FC Transport I/O timeout intercept handler
2049 * @scmd: The SCSI command which timed out
2051 * This routine protects against error handlers getting invoked while a
2052 * rport is in a blocked state, typically due to a temporarily loss of
2053 * connectivity. If the error handlers are allowed to proceed, requests
2054 * to abort i/o, reset the target, etc will likely fail as there is no way
2055 * to communicate with the device to perform the requested function. These
2056 * failures may result in the midlayer taking the device offline, requiring
2057 * manual intervention to restore operation.
2059 * This routine, called whenever an i/o times out, validates the state of
2060 * the underlying rport. If the rport is blocked, it returns
2061 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2062 * Eventually, either the device will return, or devloss_tmo will fire,
2063 * and when the timeout then fires, it will be handled normally.
2064 * If the rport is not blocked, normal error handling continues.
2067 * This routine assumes no locks are held on entry.
2069 enum blk_eh_timer_return
2070 fc_eh_timed_out(struct scsi_cmnd *scmd)
2072 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2074 if (rport->port_state == FC_PORTSTATE_BLOCKED)
2075 return BLK_EH_RESET_TIMER;
2079 EXPORT_SYMBOL(fc_eh_timed_out);
2082 * Called by fc_user_scan to locate an rport on the shost that
2083 * matches the channel and target id, and invoke scsi_scan_target()
2087 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2089 struct fc_rport *rport;
2090 unsigned long flags;
2092 spin_lock_irqsave(shost->host_lock, flags);
2094 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2095 if (rport->scsi_target_id == -1)
2098 if (rport->port_state != FC_PORTSTATE_ONLINE)
2101 if ((channel == rport->channel) &&
2102 (id == rport->scsi_target_id)) {
2103 spin_unlock_irqrestore(shost->host_lock, flags);
2104 scsi_scan_target(&rport->dev, channel, id, lun,
2110 spin_unlock_irqrestore(shost->host_lock, flags);
2114 * Called via sysfs scan routines. Necessary, as the FC transport
2115 * wants to place all target objects below the rport object. So this
2116 * routine must invoke the scsi_scan_target() routine with the rport
2117 * object as the parent.
2120 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2125 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2126 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2127 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2130 if (channel == SCAN_WILD_CARD) {
2132 chhi = shost->max_channel + 1;
2138 if (id == SCAN_WILD_CARD) {
2140 tgthi = shost->max_id;
2146 for ( ; chlo < chhi; chlo++)
2147 for ( ; tgtlo < tgthi; tgtlo++)
2148 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2153 struct scsi_transport_template *
2154 fc_attach_transport(struct fc_function_template *ft)
2157 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2163 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2164 i->t.target_attrs.ac.class = &fc_transport_class.class;
2165 i->t.target_attrs.ac.match = fc_target_match;
2166 i->t.target_size = sizeof(struct fc_starget_attrs);
2167 transport_container_register(&i->t.target_attrs);
2169 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2170 i->t.host_attrs.ac.class = &fc_host_class.class;
2171 i->t.host_attrs.ac.match = fc_host_match;
2172 i->t.host_size = sizeof(struct fc_host_attrs);
2173 if (ft->get_fc_host_stats)
2174 i->t.host_attrs.statistics = &fc_statistics_group;
2175 transport_container_register(&i->t.host_attrs);
2177 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2178 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2179 i->rport_attr_cont.ac.match = fc_rport_match;
2180 transport_container_register(&i->rport_attr_cont);
2182 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2183 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2184 i->vport_attr_cont.ac.match = fc_vport_match;
2185 transport_container_register(&i->vport_attr_cont);
2189 /* Transport uses the shost workq for scsi scanning */
2190 i->t.create_work_queue = 1;
2192 i->t.user_scan = fc_user_scan;
2195 * Setup SCSI Target Attributes.
2198 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2199 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2200 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2202 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2204 i->starget_attrs[count] = NULL;
2208 * Setup SCSI Host Attributes.
2211 SETUP_HOST_ATTRIBUTE_RD(node_name);
2212 SETUP_HOST_ATTRIBUTE_RD(port_name);
2213 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2214 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2215 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2216 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2217 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2218 if (ft->vport_create) {
2219 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2220 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2222 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2223 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2224 SETUP_HOST_ATTRIBUTE_RD(model);
2225 SETUP_HOST_ATTRIBUTE_RD(model_description);
2226 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2227 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2228 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2229 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
2231 SETUP_HOST_ATTRIBUTE_RD(port_id);
2232 SETUP_HOST_ATTRIBUTE_RD(port_type);
2233 SETUP_HOST_ATTRIBUTE_RD(port_state);
2234 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2235 SETUP_HOST_ATTRIBUTE_RD(speed);
2236 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2237 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2238 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2240 /* Transport-managed attributes */
2241 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
2242 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2243 if (ft->issue_fc_host_lip)
2244 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2245 if (ft->vport_create)
2246 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2247 if (ft->vport_delete)
2248 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2250 BUG_ON(count > FC_HOST_NUM_ATTRS);
2252 i->host_attrs[count] = NULL;
2255 * Setup Remote Port Attributes.
2258 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2259 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2260 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2261 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2262 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2263 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2264 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2265 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2266 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2267 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2269 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2271 i->rport_attrs[count] = NULL;
2274 * Setup Virtual Port Attributes.
2277 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2278 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2279 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2280 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2281 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2282 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2283 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2284 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2285 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2287 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2289 i->vport_attrs[count] = NULL;
2293 EXPORT_SYMBOL(fc_attach_transport);
2295 void fc_release_transport(struct scsi_transport_template *t)
2297 struct fc_internal *i = to_fc_internal(t);
2299 transport_container_unregister(&i->t.target_attrs);
2300 transport_container_unregister(&i->t.host_attrs);
2301 transport_container_unregister(&i->rport_attr_cont);
2302 transport_container_unregister(&i->vport_attr_cont);
2306 EXPORT_SYMBOL(fc_release_transport);
2309 * fc_queue_work - Queue work to the fc_host workqueue.
2310 * @shost: Pointer to Scsi_Host bound to fc_host.
2311 * @work: Work to queue for execution.
2314 * 1 - work queued for execution
2315 * 0 - work is already queued
2316 * -EINVAL - work queue doesn't exist
2319 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2321 if (unlikely(!fc_host_work_q(shost))) {
2323 "ERROR: FC host '%s' attempted to queue work, "
2324 "when no workqueue created.\n", shost->hostt->name);
2330 return queue_work(fc_host_work_q(shost), work);
2334 * fc_flush_work - Flush a fc_host's workqueue.
2335 * @shost: Pointer to Scsi_Host bound to fc_host.
2338 fc_flush_work(struct Scsi_Host *shost)
2340 if (!fc_host_work_q(shost)) {
2342 "ERROR: FC host '%s' attempted to flush work, "
2343 "when no workqueue created.\n", shost->hostt->name);
2348 flush_workqueue(fc_host_work_q(shost));
2352 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2353 * @shost: Pointer to Scsi_Host bound to fc_host.
2354 * @work: Work to queue for execution.
2355 * @delay: jiffies to delay the work queuing
2358 * 1 on success / 0 already queued / < 0 for error
2361 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2362 unsigned long delay)
2364 if (unlikely(!fc_host_devloss_work_q(shost))) {
2366 "ERROR: FC host '%s' attempted to queue work, "
2367 "when no workqueue created.\n", shost->hostt->name);
2373 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2377 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2378 * @shost: Pointer to Scsi_Host bound to fc_host.
2381 fc_flush_devloss(struct Scsi_Host *shost)
2383 if (!fc_host_devloss_work_q(shost)) {
2385 "ERROR: FC host '%s' attempted to flush work, "
2386 "when no workqueue created.\n", shost->hostt->name);
2391 flush_workqueue(fc_host_devloss_work_q(shost));
2396 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2397 * @shost: Which &Scsi_Host
2399 * This routine is expected to be called immediately preceding the
2400 * a driver's call to scsi_remove_host().
2402 * WARNING: A driver utilizing the fc_transport, which fails to call
2403 * this routine prior to scsi_remove_host(), will leave dangling
2404 * objects in /sys/class/fc_remote_ports. Access to any of these
2405 * objects can result in a system crash !!!
2408 * This routine assumes no locks are held on entry.
2411 fc_remove_host(struct Scsi_Host *shost)
2413 struct fc_vport *vport = NULL, *next_vport = NULL;
2414 struct fc_rport *rport = NULL, *next_rport = NULL;
2415 struct workqueue_struct *work_q;
2416 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2417 unsigned long flags;
2419 spin_lock_irqsave(shost->host_lock, flags);
2421 /* Remove any vports */
2422 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2423 vport->flags |= FC_VPORT_DELETING;
2424 fc_queue_work(shost, &vport->vport_delete_work);
2427 /* Remove any remote ports */
2428 list_for_each_entry_safe(rport, next_rport,
2429 &fc_host->rports, peers) {
2430 list_del(&rport->peers);
2431 rport->port_state = FC_PORTSTATE_DELETED;
2432 fc_queue_work(shost, &rport->rport_delete_work);
2435 list_for_each_entry_safe(rport, next_rport,
2436 &fc_host->rport_bindings, peers) {
2437 list_del(&rport->peers);
2438 rport->port_state = FC_PORTSTATE_DELETED;
2439 fc_queue_work(shost, &rport->rport_delete_work);
2442 spin_unlock_irqrestore(shost->host_lock, flags);
2444 /* flush all scan work items */
2445 scsi_flush_work(shost);
2447 /* flush all stgt delete, and rport delete work items, then kill it */
2448 if (fc_host->work_q) {
2449 work_q = fc_host->work_q;
2450 fc_host->work_q = NULL;
2451 destroy_workqueue(work_q);
2454 /* flush all devloss work items, then kill it */
2455 if (fc_host->devloss_work_q) {
2456 work_q = fc_host->devloss_work_q;
2457 fc_host->devloss_work_q = NULL;
2458 destroy_workqueue(work_q);
2461 EXPORT_SYMBOL(fc_remove_host);
2463 static void fc_terminate_rport_io(struct fc_rport *rport)
2465 struct Scsi_Host *shost = rport_to_shost(rport);
2466 struct fc_internal *i = to_fc_internal(shost->transportt);
2468 /* Involve the LLDD if possible to terminate all io on the rport. */
2469 if (i->f->terminate_rport_io)
2470 i->f->terminate_rport_io(rport);
2473 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2475 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2479 * fc_starget_delete - called to delete the scsi descendants of an rport
2480 * @work: remote port to be operated on.
2482 * Deletes target and all sdevs.
2485 fc_starget_delete(struct work_struct *work)
2487 struct fc_rport *rport =
2488 container_of(work, struct fc_rport, stgt_delete_work);
2490 fc_terminate_rport_io(rport);
2491 scsi_remove_target(&rport->dev);
2496 * fc_rport_final_delete - finish rport termination and delete it.
2497 * @work: remote port to be deleted.
2500 fc_rport_final_delete(struct work_struct *work)
2502 struct fc_rport *rport =
2503 container_of(work, struct fc_rport, rport_delete_work);
2504 struct device *dev = &rport->dev;
2505 struct Scsi_Host *shost = rport_to_shost(rport);
2506 struct fc_internal *i = to_fc_internal(shost->transportt);
2507 unsigned long flags;
2508 int do_callback = 0;
2510 fc_terminate_rport_io(rport);
2513 * if a scan is pending, flush the SCSI Host work_q so that
2514 * that we can reclaim the rport scan work element.
2516 if (rport->flags & FC_RPORT_SCAN_PENDING)
2517 scsi_flush_work(shost);
2520 * Cancel any outstanding timers. These should really exist
2521 * only when rmmod'ing the LLDD and we're asking for
2522 * immediate termination of the rports
2524 spin_lock_irqsave(shost->host_lock, flags);
2525 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2526 spin_unlock_irqrestore(shost->host_lock, flags);
2527 if (!cancel_delayed_work(&rport->fail_io_work))
2528 fc_flush_devloss(shost);
2529 if (!cancel_delayed_work(&rport->dev_loss_work))
2530 fc_flush_devloss(shost);
2531 cancel_work_sync(&rport->scan_work);
2532 spin_lock_irqsave(shost->host_lock, flags);
2533 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2535 spin_unlock_irqrestore(shost->host_lock, flags);
2537 /* Delete SCSI target and sdevs */
2538 if (rport->scsi_target_id != -1)
2539 fc_starget_delete(&rport->stgt_delete_work);
2542 * Notify the driver that the rport is now dead. The LLDD will
2543 * also guarantee that any communication to the rport is terminated
2545 * Avoid this call if we already called it when we preserved the
2546 * rport for the binding.
2548 spin_lock_irqsave(shost->host_lock, flags);
2549 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2550 (i->f->dev_loss_tmo_callbk)) {
2551 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2554 spin_unlock_irqrestore(shost->host_lock, flags);
2557 i->f->dev_loss_tmo_callbk(rport);
2559 fc_bsg_remove(rport->rqst_q);
2561 transport_remove_device(dev);
2563 transport_destroy_device(dev);
2564 scsi_host_put(shost); /* for fc_host->rport list */
2565 put_device(dev); /* for self-reference */
2570 * fc_remote_port_create - allocates and creates a remote FC port.
2571 * @shost: scsi host the remote port is connected to.
2572 * @channel: Channel on shost port connected to.
2573 * @ids: The world wide names, fc address, and FC4 port
2574 * roles for the remote port.
2576 * Allocates and creates the remoter port structure, including the
2577 * class and sysfs creation.
2580 * This routine assumes no locks are held on entry.
2582 static struct fc_rport *
2583 fc_remote_port_create(struct Scsi_Host *shost, int channel,
2584 struct fc_rport_identifiers *ids)
2586 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2587 struct fc_internal *fci = to_fc_internal(shost->transportt);
2588 struct fc_rport *rport;
2590 unsigned long flags;
2594 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2595 rport = kzalloc(size, GFP_KERNEL);
2596 if (unlikely(!rport)) {
2597 printk(KERN_ERR "%s: allocation failure\n", __func__);
2601 rport->maxframe_size = -1;
2602 rport->supported_classes = FC_COS_UNSPECIFIED;
2603 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
2604 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2605 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2606 rport->port_id = ids->port_id;
2607 rport->roles = ids->roles;
2608 rport->port_state = FC_PORTSTATE_ONLINE;
2609 if (fci->f->dd_fcrport_size)
2610 rport->dd_data = &rport[1];
2611 rport->channel = channel;
2612 rport->fast_io_fail_tmo = -1;
2614 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2615 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2616 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2617 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2618 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2620 spin_lock_irqsave(shost->host_lock, flags);
2622 rport->number = fc_host->next_rport_number++;
2623 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
2624 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
2625 rport->scsi_target_id = fc_host->next_target_id++;
2627 rport->scsi_target_id = -1;
2628 list_add_tail(&rport->peers, &fc_host->rports);
2629 scsi_host_get(shost); /* for fc_host->rport list */
2631 spin_unlock_irqrestore(shost->host_lock, flags);
2634 device_initialize(dev); /* takes self reference */
2635 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2636 dev->release = fc_rport_dev_release;
2637 dev_set_name(dev, "rport-%d:%d-%d",
2638 shost->host_no, channel, rport->number);
2639 transport_setup_device(dev);
2641 error = device_add(dev);
2643 printk(KERN_ERR "FC Remote Port device_add failed\n");
2646 transport_add_device(dev);
2647 transport_configure_device(dev);
2649 fc_bsg_rportadd(shost, rport);
2650 /* ignore any bsg add error - we just can't do sgio */
2652 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2653 /* initiate a scan of the target */
2654 rport->flags |= FC_RPORT_SCAN_PENDING;
2655 scsi_queue_work(shost, &rport->scan_work);
2661 transport_destroy_device(dev);
2662 spin_lock_irqsave(shost->host_lock, flags);
2663 list_del(&rport->peers);
2664 scsi_host_put(shost); /* for fc_host->rport list */
2665 spin_unlock_irqrestore(shost->host_lock, flags);
2666 put_device(dev->parent);
2672 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2673 * @shost: scsi host the remote port is connected to.
2674 * @channel: Channel on shost port connected to.
2675 * @ids: The world wide names, fc address, and FC4 port
2676 * roles for the remote port.
2678 * The LLDD calls this routine to notify the transport of the existence
2679 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2680 * of the port, it's FC address (port_id), and the FC4 roles that are
2681 * active for the port.
2683 * For ports that are FCP targets (aka scsi targets), the FC transport
2684 * maintains consistent target id bindings on behalf of the LLDD.
2685 * A consistent target id binding is an assignment of a target id to
2686 * a remote port identifier, which persists while the scsi host is
2687 * attached. The remote port can disappear, then later reappear, and
2688 * it's target id assignment remains the same. This allows for shifts
2689 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2690 * changes to the scsi subsystem which is based on scsi host number and
2691 * target id values. Bindings are only valid during the attachment of
2692 * the scsi host. If the host detaches, then later re-attaches, target
2693 * id bindings may change.
2695 * This routine is responsible for returning a remote port structure.
2696 * The routine will search the list of remote ports it maintains
2697 * internally on behalf of consistent target id mappings. If found, the
2698 * remote port structure will be reused. Otherwise, a new remote port
2699 * structure will be allocated.
2701 * Whenever a remote port is allocated, a new fc_remote_port class
2702 * device is created.
2704 * Should not be called from interrupt context.
2707 * This routine assumes no locks are held on entry.
2710 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2711 struct fc_rport_identifiers *ids)
2713 struct fc_internal *fci = to_fc_internal(shost->transportt);
2714 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2715 struct fc_rport *rport;
2716 unsigned long flags;
2719 /* ensure any stgt delete functions are done */
2720 fc_flush_work(shost);
2723 * Search the list of "active" rports, for an rport that has been
2724 * deleted, but we've held off the real delete while the target
2725 * is in a "blocked" state.
2727 spin_lock_irqsave(shost->host_lock, flags);
2729 list_for_each_entry(rport, &fc_host->rports, peers) {
2731 if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
2732 rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
2733 (rport->channel == channel)) {
2735 switch (fc_host->tgtid_bind_type) {
2736 case FC_TGTID_BIND_BY_WWPN:
2737 case FC_TGTID_BIND_NONE:
2738 if (rport->port_name == ids->port_name)
2741 case FC_TGTID_BIND_BY_WWNN:
2742 if (rport->node_name == ids->node_name)
2745 case FC_TGTID_BIND_BY_ID:
2746 if (rport->port_id == ids->port_id)
2753 memcpy(&rport->node_name, &ids->node_name,
2754 sizeof(rport->node_name));
2755 memcpy(&rport->port_name, &ids->port_name,
2756 sizeof(rport->port_name));
2757 rport->port_id = ids->port_id;
2759 rport->port_state = FC_PORTSTATE_ONLINE;
2760 rport->roles = ids->roles;
2762 spin_unlock_irqrestore(shost->host_lock, flags);
2764 if (fci->f->dd_fcrport_size)
2765 memset(rport->dd_data, 0,
2766 fci->f->dd_fcrport_size);
2769 * If we were not a target, cancel the
2770 * io terminate and rport timers, and
2773 * If we were a target, but our new role
2774 * doesn't indicate a target, leave the
2775 * timers running expecting the role to
2776 * change as the target fully logs in. If
2777 * it doesn't, the target will be torn down.
2779 * If we were a target, and our role shows
2780 * we're still a target, cancel the timers
2781 * and kick off a scan.
2784 /* was a target, not in roles */
2785 if ((rport->scsi_target_id != -1) &&
2786 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2790 * Stop the fail io and dev_loss timers.
2791 * If they flush, the port_state will
2792 * be checked and will NOOP the function.
2794 if (!cancel_delayed_work(&rport->fail_io_work))
2795 fc_flush_devloss(shost);
2796 if (!cancel_delayed_work(&rport->dev_loss_work))
2797 fc_flush_devloss(shost);
2799 spin_lock_irqsave(shost->host_lock, flags);
2801 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2802 FC_RPORT_DEVLOSS_PENDING |
2803 FC_RPORT_DEVLOSS_CALLBK_DONE);
2805 spin_unlock_irqrestore(shost->host_lock, flags);
2807 /* if target, initiate a scan */
2808 if (rport->scsi_target_id != -1) {
2809 scsi_target_unblock(&rport->dev,
2811 spin_lock_irqsave(shost->host_lock,
2813 rport->flags |= FC_RPORT_SCAN_PENDING;
2814 scsi_queue_work(shost,
2816 spin_unlock_irqrestore(shost->host_lock,
2820 fc_bsg_goose_queue(rport);
2828 * Search the bindings array
2829 * Note: if never a FCP target, you won't be on this list
2831 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2833 /* search for a matching consistent binding */
2835 list_for_each_entry(rport, &fc_host->rport_bindings,
2837 if (rport->channel != channel)
2840 switch (fc_host->tgtid_bind_type) {
2841 case FC_TGTID_BIND_BY_WWPN:
2842 if (rport->port_name == ids->port_name)
2845 case FC_TGTID_BIND_BY_WWNN:
2846 if (rport->node_name == ids->node_name)
2849 case FC_TGTID_BIND_BY_ID:
2850 if (rport->port_id == ids->port_id)
2853 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2858 list_move_tail(&rport->peers, &fc_host->rports);
2864 memcpy(&rport->node_name, &ids->node_name,
2865 sizeof(rport->node_name));
2866 memcpy(&rport->port_name, &ids->port_name,
2867 sizeof(rport->port_name));
2868 rport->port_id = ids->port_id;
2869 rport->port_state = FC_PORTSTATE_ONLINE;
2870 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
2872 if (fci->f->dd_fcrport_size)
2873 memset(rport->dd_data, 0,
2874 fci->f->dd_fcrport_size);
2875 spin_unlock_irqrestore(shost->host_lock, flags);
2877 fc_remote_port_rolechg(rport, ids->roles);
2882 spin_unlock_irqrestore(shost->host_lock, flags);
2884 /* No consistent binding found - create new remote port entry */
2885 rport = fc_remote_port_create(shost, channel, ids);
2889 EXPORT_SYMBOL(fc_remote_port_add);
2893 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2894 * @rport: The remote port that no longer exists
2896 * The LLDD calls this routine to notify the transport that a remote
2897 * port is no longer part of the topology. Note: Although a port
2898 * may no longer be part of the topology, it may persist in the remote
2899 * ports displayed by the fc_host. We do this under 2 conditions:
2901 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2902 * This allows the port to temporarily disappear, then reappear without
2903 * disrupting the SCSI device tree attached to it. During the "blocked"
2904 * period the port will still exist.
2906 * 2) If the port was a scsi target and disappears for longer than we
2907 * expect, we'll delete the port and the tear down the SCSI device tree
2908 * attached to it. However, we want to semi-persist the target id assigned
2909 * to that port if it eventually does exist. The port structure will
2910 * remain (although with minimal information) so that the target id
2911 * bindings also remain.
2913 * If the remote port is not an FCP Target, it will be fully torn down
2914 * and deallocated, including the fc_remote_port class device.
2916 * If the remote port is an FCP Target, the port will be placed in a
2917 * temporary blocked state. From the LLDD's perspective, the rport no
2918 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2919 * exists, but all sdevs on it are blocked from further I/O. The following
2922 * If the remote port does not return (signaled by a LLDD call to
2923 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2924 * scsi target is removed - killing all outstanding i/o and removing the
2925 * scsi devices attached to it. The port structure will be marked Not
2926 * Present and be partially cleared, leaving only enough information to
2927 * recognize the remote port relative to the scsi target id binding if
2928 * it later appears. The port will remain as long as there is a valid
2929 * binding (e.g. until the user changes the binding type or unloads the
2930 * scsi host with the binding).
2932 * If the remote port returns within the dev_loss_tmo value (and matches
2933 * according to the target id binding type), the port structure will be
2934 * reused. If it is no longer a SCSI target, the target will be torn
2935 * down. If it continues to be a SCSI target, then the target will be
2936 * unblocked (allowing i/o to be resumed), and a scan will be activated
2937 * to ensure that all luns are detected.
2939 * Called from normal process context only - cannot be called from interrupt.
2942 * This routine assumes no locks are held on entry.
2945 fc_remote_port_delete(struct fc_rport *rport)
2947 struct Scsi_Host *shost = rport_to_shost(rport);
2948 unsigned long timeout = rport->dev_loss_tmo;
2949 unsigned long flags;
2952 * No need to flush the fc_host work_q's, as all adds are synchronous.
2954 * We do need to reclaim the rport scan work element, so eventually
2955 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2956 * there's still a scan pending.
2959 spin_lock_irqsave(shost->host_lock, flags);
2961 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2962 spin_unlock_irqrestore(shost->host_lock, flags);
2967 * In the past, we if this was not an FCP-Target, we would
2968 * unconditionally just jump to deleting the rport.
2969 * However, rports can be used as node containers by the LLDD,
2970 * and its not appropriate to just terminate the rport at the
2971 * first sign of a loss in connectivity. The LLDD may want to
2972 * send ELS traffic to re-validate the login. If the rport is
2973 * immediately deleted, it makes it inappropriate for a node
2975 * So... we now unconditionally wait dev_loss_tmo before
2976 * destroying an rport.
2979 rport->port_state = FC_PORTSTATE_BLOCKED;
2981 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2983 spin_unlock_irqrestore(shost->host_lock, flags);
2985 scsi_target_block(&rport->dev);
2987 /* see if we need to kill io faster than waiting for device loss */
2988 if ((rport->fast_io_fail_tmo != -1) &&
2989 (rport->fast_io_fail_tmo < timeout))
2990 fc_queue_devloss_work(shost, &rport->fail_io_work,
2991 rport->fast_io_fail_tmo * HZ);
2993 /* cap the length the devices can be blocked until they are deleted */
2994 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2996 EXPORT_SYMBOL(fc_remote_port_delete);
2999 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
3000 * @rport: The remote port that changed.
3001 * @roles: New roles for this port.
3003 * Description: The LLDD calls this routine to notify the transport that the
3004 * roles on a remote port may have changed. The largest effect of this is
3005 * if a port now becomes a FCP Target, it must be allocated a
3006 * scsi target id. If the port is no longer a FCP target, any
3007 * scsi target id value assigned to it will persist in case the
3008 * role changes back to include FCP Target. No changes in the scsi
3009 * midlayer will be invoked if the role changes (in the expectation
3010 * that the role will be resumed. If it doesn't normal error processing
3013 * Should not be called from interrupt context.
3016 * This routine assumes no locks are held on entry.
3019 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3021 struct Scsi_Host *shost = rport_to_shost(rport);
3022 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3023 unsigned long flags;
3026 spin_lock_irqsave(shost->host_lock, flags);
3027 if (roles & FC_PORT_ROLE_FCP_TARGET) {
3028 if (rport->scsi_target_id == -1) {
3029 rport->scsi_target_id = fc_host->next_target_id++;
3031 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3035 rport->roles = roles;
3037 spin_unlock_irqrestore(shost->host_lock, flags);
3041 * There may have been a delete timer running on the
3042 * port. Ensure that it is cancelled as we now know
3043 * the port is an FCP Target.
3044 * Note: we know the rport exists and is in an online
3045 * state as the LLDD would not have had an rport
3046 * reference to pass us.
3048 * Take no action on the del_timer failure as the state
3049 * machine state change will validate the
3052 if (!cancel_delayed_work(&rport->fail_io_work))
3053 fc_flush_devloss(shost);
3054 if (!cancel_delayed_work(&rport->dev_loss_work))
3055 fc_flush_devloss(shost);
3057 spin_lock_irqsave(shost->host_lock, flags);
3058 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3059 FC_RPORT_DEVLOSS_PENDING |
3060 FC_RPORT_DEVLOSS_CALLBK_DONE);
3061 spin_unlock_irqrestore(shost->host_lock, flags);
3063 /* ensure any stgt delete functions are done */
3064 fc_flush_work(shost);
3066 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
3067 /* initiate a scan of the target */
3068 spin_lock_irqsave(shost->host_lock, flags);
3069 rport->flags |= FC_RPORT_SCAN_PENDING;
3070 scsi_queue_work(shost, &rport->scan_work);
3071 spin_unlock_irqrestore(shost->host_lock, flags);
3074 EXPORT_SYMBOL(fc_remote_port_rolechg);
3077 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3078 * @work: rport target that failed to reappear in the allotted time.
3080 * Description: An attempt to delete a remote port blocks, and if it fails
3081 * to return in the allotted time this gets called.
3084 fc_timeout_deleted_rport(struct work_struct *work)
3086 struct fc_rport *rport =
3087 container_of(work, struct fc_rport, dev_loss_work.work);
3088 struct Scsi_Host *shost = rport_to_shost(rport);
3089 struct fc_internal *i = to_fc_internal(shost->transportt);
3090 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3091 unsigned long flags;
3092 int do_callback = 0;
3094 spin_lock_irqsave(shost->host_lock, flags);
3096 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3099 * If the port is ONLINE, then it came back. If it was a SCSI
3100 * target, validate it still is. If not, tear down the
3101 * scsi_target on it.
3103 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3104 (rport->scsi_target_id != -1) &&
3105 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3106 dev_printk(KERN_ERR, &rport->dev,
3107 "blocked FC remote port time out: no longer"
3108 " a FCP target, removing starget\n");
3109 spin_unlock_irqrestore(shost->host_lock, flags);
3110 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3111 fc_queue_work(shost, &rport->stgt_delete_work);
3115 /* NOOP state - we're flushing workq's */
3116 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3117 spin_unlock_irqrestore(shost->host_lock, flags);
3118 dev_printk(KERN_ERR, &rport->dev,
3119 "blocked FC remote port time out: leaving"
3121 (rport->scsi_target_id != -1) ? " and starget" : "");
3125 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3126 (rport->scsi_target_id == -1)) {
3127 list_del(&rport->peers);
3128 rport->port_state = FC_PORTSTATE_DELETED;
3129 dev_printk(KERN_ERR, &rport->dev,
3130 "blocked FC remote port time out: removing"
3132 (rport->scsi_target_id != -1) ? " and starget" : "");
3133 fc_queue_work(shost, &rport->rport_delete_work);
3134 spin_unlock_irqrestore(shost->host_lock, flags);
3138 dev_printk(KERN_ERR, &rport->dev,
3139 "blocked FC remote port time out: removing target and "
3140 "saving binding\n");
3142 list_move_tail(&rport->peers, &fc_host->rport_bindings);
3145 * Note: We do not remove or clear the hostdata area. This allows
3146 * host-specific target data to persist along with the
3147 * scsi_target_id. It's up to the host to manage it's hostdata area.
3151 * Reinitialize port attributes that may change if the port comes back.
3153 rport->maxframe_size = -1;
3154 rport->supported_classes = FC_COS_UNSPECIFIED;
3155 rport->roles = FC_PORT_ROLE_UNKNOWN;
3156 rport->port_state = FC_PORTSTATE_NOTPRESENT;
3157 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3160 * Pre-emptively kill I/O rather than waiting for the work queue
3161 * item to teardown the starget. (FCOE libFC folks prefer this
3162 * and to have the rport_port_id still set when it's done).
3164 spin_unlock_irqrestore(shost->host_lock, flags);
3165 fc_terminate_rport_io(rport);
3167 spin_lock_irqsave(shost->host_lock, flags);
3169 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3171 /* remove the identifiers that aren't used in the consisting binding */
3172 switch (fc_host->tgtid_bind_type) {
3173 case FC_TGTID_BIND_BY_WWPN:
3174 rport->node_name = -1;
3175 rport->port_id = -1;
3177 case FC_TGTID_BIND_BY_WWNN:
3178 rport->port_name = -1;
3179 rport->port_id = -1;
3181 case FC_TGTID_BIND_BY_ID:
3182 rport->node_name = -1;
3183 rport->port_name = -1;
3185 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3190 * As this only occurs if the remote port (scsi target)
3191 * went away and didn't come back - we'll remove
3192 * all attached scsi devices.
3194 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3195 fc_queue_work(shost, &rport->stgt_delete_work);
3200 spin_unlock_irqrestore(shost->host_lock, flags);
3203 * Notify the driver that the rport is now dead. The LLDD will
3204 * also guarantee that any communication to the rport is terminated
3206 * Note: we set the CALLBK_DONE flag above to correspond
3208 if (do_callback && i->f->dev_loss_tmo_callbk)
3209 i->f->dev_loss_tmo_callbk(rport);
3214 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3215 * @work: rport to terminate io on.
3217 * Notes: Only requests the failure of the io, not that all are flushed
3218 * prior to returning.
3221 fc_timeout_fail_rport_io(struct work_struct *work)
3223 struct fc_rport *rport =
3224 container_of(work, struct fc_rport, fail_io_work.work);
3226 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3229 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3230 fc_terminate_rport_io(rport);
3234 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3235 * @work: remote port to be scanned.
3238 fc_scsi_scan_rport(struct work_struct *work)
3240 struct fc_rport *rport =
3241 container_of(work, struct fc_rport, scan_work);
3242 struct Scsi_Host *shost = rport_to_shost(rport);
3243 struct fc_internal *i = to_fc_internal(shost->transportt);
3244 unsigned long flags;
3246 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3247 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3248 !(i->f->disable_target_scan)) {
3249 scsi_scan_target(&rport->dev, rport->channel,
3250 rport->scsi_target_id, SCAN_WILD_CARD,
3254 spin_lock_irqsave(shost->host_lock, flags);
3255 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3256 spin_unlock_irqrestore(shost->host_lock, flags);
3260 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3261 * @rport: Remote port that scsi_eh is trying to recover.
3263 * This routine can be called from a FC LLD scsi_eh callback. It
3264 * blocks the scsi_eh thread until the fc_rport leaves the
3265 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3266 * necessary to avoid the scsi_eh failing recovery actions for blocked
3267 * rports which would lead to offlined SCSI devices.
3269 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3270 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3271 * passed back to scsi_eh.
3273 int fc_block_rport(struct fc_rport *rport)
3275 struct Scsi_Host *shost = rport_to_shost(rport);
3276 unsigned long flags;
3278 spin_lock_irqsave(shost->host_lock, flags);
3279 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3280 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3281 spin_unlock_irqrestore(shost->host_lock, flags);
3283 spin_lock_irqsave(shost->host_lock, flags);
3285 spin_unlock_irqrestore(shost->host_lock, flags);
3287 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3288 return FAST_IO_FAIL;
3292 EXPORT_SYMBOL(fc_block_rport);
3295 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3296 * @cmnd: SCSI command that scsi_eh is trying to recover
3298 * This routine can be called from a FC LLD scsi_eh callback. It
3299 * blocks the scsi_eh thread until the fc_rport leaves the
3300 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3301 * necessary to avoid the scsi_eh failing recovery actions for blocked
3302 * rports which would lead to offlined SCSI devices.
3304 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3305 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3306 * passed back to scsi_eh.
3308 int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3310 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3312 if (WARN_ON_ONCE(!rport))
3313 return FAST_IO_FAIL;
3315 return fc_block_rport(rport);
3317 EXPORT_SYMBOL(fc_block_scsi_eh);
3320 * fc_vport_setup - allocates and creates a FC virtual port.
3321 * @shost: scsi host the virtual port is connected to.
3322 * @channel: Channel on shost port connected to.
3323 * @pdev: parent device for vport
3324 * @ids: The world wide names, FC4 port roles, etc for
3326 * @ret_vport: The pointer to the created vport.
3328 * Allocates and creates the vport structure, calls the parent host
3329 * to instantiate the vport, this completes w/ class and sysfs creation.
3332 * This routine assumes no locks are held on entry.
3335 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3336 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3338 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3339 struct fc_internal *fci = to_fc_internal(shost->transportt);
3340 struct fc_vport *vport;
3342 unsigned long flags;
3348 if ( ! fci->f->vport_create)
3351 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3352 vport = kzalloc(size, GFP_KERNEL);
3353 if (unlikely(!vport)) {
3354 printk(KERN_ERR "%s: allocation failure\n", __func__);
3358 vport->vport_state = FC_VPORT_UNKNOWN;
3359 vport->vport_last_state = FC_VPORT_UNKNOWN;
3360 vport->node_name = ids->node_name;
3361 vport->port_name = ids->port_name;
3362 vport->roles = ids->roles;
3363 vport->vport_type = ids->vport_type;
3364 if (fci->f->dd_fcvport_size)
3365 vport->dd_data = &vport[1];
3366 vport->shost = shost;
3367 vport->channel = channel;
3368 vport->flags = FC_VPORT_CREATING;
3369 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3371 spin_lock_irqsave(shost->host_lock, flags);
3373 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3374 spin_unlock_irqrestore(shost->host_lock, flags);
3378 fc_host->npiv_vports_inuse++;
3379 vport->number = fc_host->next_vport_number++;
3380 list_add_tail(&vport->peers, &fc_host->vports);
3381 scsi_host_get(shost); /* for fc_host->vport list */
3383 spin_unlock_irqrestore(shost->host_lock, flags);
3386 device_initialize(dev); /* takes self reference */
3387 dev->parent = get_device(pdev); /* takes parent reference */
3388 dev->release = fc_vport_dev_release;
3389 dev_set_name(dev, "vport-%d:%d-%d",
3390 shost->host_no, channel, vport->number);
3391 transport_setup_device(dev);
3393 error = device_add(dev);
3395 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3398 transport_add_device(dev);
3399 transport_configure_device(dev);
3401 error = fci->f->vport_create(vport, ids->disable);
3403 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3404 goto delete_vport_all;
3408 * if the parent isn't the physical adapter's Scsi_Host, ensure
3409 * the Scsi_Host at least contains a symlink to the vport.
3411 if (pdev != &shost->shost_gendev) {
3412 error = sysfs_create_link(&shost->shost_gendev.kobj,
3413 &dev->kobj, dev_name(dev));
3416 "%s: Cannot create vport symlinks for "
3418 __func__, dev_name(dev), error);
3420 spin_lock_irqsave(shost->host_lock, flags);
3421 vport->flags &= ~FC_VPORT_CREATING;
3422 spin_unlock_irqrestore(shost->host_lock, flags);
3424 dev_printk(KERN_NOTICE, pdev,
3425 "%s created via shost%d channel %d\n", dev_name(dev),
3426 shost->host_no, channel);
3433 transport_remove_device(dev);
3436 transport_destroy_device(dev);
3437 spin_lock_irqsave(shost->host_lock, flags);
3438 list_del(&vport->peers);
3439 scsi_host_put(shost); /* for fc_host->vport list */
3440 fc_host->npiv_vports_inuse--;
3441 spin_unlock_irqrestore(shost->host_lock, flags);
3442 put_device(dev->parent);
3449 * fc_vport_create - Admin App or LLDD requests creation of a vport
3450 * @shost: scsi host the virtual port is connected to.
3451 * @channel: channel on shost port connected to.
3452 * @ids: The world wide names, FC4 port roles, etc for
3456 * This routine assumes no locks are held on entry.
3459 fc_vport_create(struct Scsi_Host *shost, int channel,
3460 struct fc_vport_identifiers *ids)
3463 struct fc_vport *vport;
3465 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3467 return stat ? NULL : vport;
3469 EXPORT_SYMBOL(fc_vport_create);
3472 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3473 * @vport: fc_vport to be terminated
3475 * Calls the LLDD vport_delete() function, then deallocates and removes
3476 * the vport from the shost and object tree.
3479 * This routine assumes no locks are held on entry.
3482 fc_vport_terminate(struct fc_vport *vport)
3484 struct Scsi_Host *shost = vport_to_shost(vport);
3485 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3486 struct fc_internal *i = to_fc_internal(shost->transportt);
3487 struct device *dev = &vport->dev;
3488 unsigned long flags;
3491 if (i->f->vport_delete)
3492 stat = i->f->vport_delete(vport);
3496 spin_lock_irqsave(shost->host_lock, flags);
3497 vport->flags &= ~FC_VPORT_DELETING;
3499 vport->flags |= FC_VPORT_DELETED;
3500 list_del(&vport->peers);
3501 fc_host->npiv_vports_inuse--;
3502 scsi_host_put(shost); /* for fc_host->vport list */
3504 spin_unlock_irqrestore(shost->host_lock, flags);
3509 if (dev->parent != &shost->shost_gendev)
3510 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3511 transport_remove_device(dev);
3513 transport_destroy_device(dev);
3516 * Removing our self-reference should mean our
3517 * release function gets called, which will drop the remaining
3518 * parent reference and free the data structure.
3520 put_device(dev); /* for self-reference */
3522 return 0; /* SUCCESS */
3524 EXPORT_SYMBOL(fc_vport_terminate);
3527 * fc_vport_sched_delete - workq-based delete request for a vport
3528 * @work: vport to be deleted.
3531 fc_vport_sched_delete(struct work_struct *work)
3533 struct fc_vport *vport =
3534 container_of(work, struct fc_vport, vport_delete_work);
3537 stat = fc_vport_terminate(vport);
3539 dev_printk(KERN_ERR, vport->dev.parent,
3540 "%s: %s could not be deleted created via "
3541 "shost%d channel %d - error %d\n", __func__,
3542 dev_name(&vport->dev), vport->shost->host_no,
3543 vport->channel, stat);
3552 * fc_bsg_job_timeout - handler for when a bsg request timesout
3553 * @req: request that timed out
3555 static enum blk_eh_timer_return
3556 fc_bsg_job_timeout(struct request *req)
3558 struct bsg_job *job = blk_mq_rq_to_pdu(req);
3559 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3560 struct fc_rport *rport = fc_bsg_to_rport(job);
3561 struct fc_internal *i = to_fc_internal(shost->transportt);
3562 int err = 0, inflight = 0;
3564 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
3565 return BLK_EH_RESET_TIMER;
3567 inflight = bsg_job_get(job);
3569 if (inflight && i->f->bsg_timeout) {
3570 /* call LLDD to abort the i/o as it has timed out */
3571 err = i->f->bsg_timeout(job);
3572 if (err == -EAGAIN) {
3574 return BLK_EH_RESET_TIMER;
3576 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3577 "abort failed with status %d\n", err);
3580 /* the blk_end_sync_io() doesn't check the error */
3582 blk_mq_end_request(req, BLK_STS_IOERR);
3587 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3588 * @shost: scsi host rport attached to
3589 * @job: bsg job to be processed
3591 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3593 struct fc_internal *i = to_fc_internal(shost->transportt);
3594 struct fc_bsg_request *bsg_request = job->request;
3595 struct fc_bsg_reply *bsg_reply = job->reply;
3596 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3599 /* check if we really have all the request data needed */
3600 if (job->request_len < cmdlen) {
3605 /* Validate the host command */
3606 switch (bsg_request->msgcode) {
3607 case FC_BSG_HST_ADD_RPORT:
3608 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3611 case FC_BSG_HST_DEL_RPORT:
3612 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3615 case FC_BSG_HST_ELS_NOLOGIN:
3616 cmdlen += sizeof(struct fc_bsg_host_els);
3617 /* there better be a xmt and rcv payloads */
3618 if ((!job->request_payload.payload_len) ||
3619 (!job->reply_payload.payload_len)) {
3626 cmdlen += sizeof(struct fc_bsg_host_ct);
3627 /* there better be xmt and rcv payloads */
3628 if ((!job->request_payload.payload_len) ||
3629 (!job->reply_payload.payload_len)) {
3635 case FC_BSG_HST_VENDOR:
3636 cmdlen += sizeof(struct fc_bsg_host_vendor);
3637 if ((shost->hostt->vendor_id == 0L) ||
3638 (bsg_request->rqst_data.h_vendor.vendor_id !=
3639 shost->hostt->vendor_id)) {
3650 ret = i->f->bsg_request(job);
3655 /* return the errno failure code as the only status */
3656 BUG_ON(job->reply_len < sizeof(uint32_t));
3657 bsg_reply->reply_payload_rcv_len = 0;
3658 bsg_reply->result = ret;
3659 job->reply_len = sizeof(uint32_t);
3660 bsg_job_done(job, bsg_reply->result,
3661 bsg_reply->reply_payload_rcv_len);
3667 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3668 * @rport: rport to be restarted
3671 fc_bsg_goose_queue(struct fc_rport *rport)
3673 struct request_queue *q = rport->rqst_q;
3676 blk_mq_run_hw_queues(q, true);
3680 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3681 * @shost: scsi host rport attached to
3682 * @job: bsg job to be processed
3684 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3686 struct fc_internal *i = to_fc_internal(shost->transportt);
3687 struct fc_bsg_request *bsg_request = job->request;
3688 struct fc_bsg_reply *bsg_reply = job->reply;
3689 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3692 /* check if we really have all the request data needed */
3693 if (job->request_len < cmdlen) {
3695 goto fail_rport_msg;
3698 /* Validate the rport command */
3699 switch (bsg_request->msgcode) {
3700 case FC_BSG_RPT_ELS:
3701 cmdlen += sizeof(struct fc_bsg_rport_els);
3705 cmdlen += sizeof(struct fc_bsg_rport_ct);
3707 /* there better be xmt and rcv payloads */
3708 if ((!job->request_payload.payload_len) ||
3709 (!job->reply_payload.payload_len)) {
3711 goto fail_rport_msg;
3716 goto fail_rport_msg;
3719 ret = i->f->bsg_request(job);
3724 /* return the errno failure code as the only status */
3725 BUG_ON(job->reply_len < sizeof(uint32_t));
3726 bsg_reply->reply_payload_rcv_len = 0;
3727 bsg_reply->result = ret;
3728 job->reply_len = sizeof(uint32_t);
3729 bsg_job_done(job, bsg_reply->result,
3730 bsg_reply->reply_payload_rcv_len);
3734 static int fc_bsg_dispatch(struct bsg_job *job)
3736 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3738 if (scsi_is_fc_rport(job->dev))
3739 return fc_bsg_rport_dispatch(shost, job);
3741 return fc_bsg_host_dispatch(shost, job);
3744 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
3746 if (rport->port_state == FC_PORTSTATE_BLOCKED &&
3747 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3748 return BLK_STS_RESOURCE;
3750 if (rport->port_state != FC_PORTSTATE_ONLINE)
3751 return BLK_STS_IOERR;
3757 static int fc_bsg_dispatch_prep(struct bsg_job *job)
3759 struct fc_rport *rport = fc_bsg_to_rport(job);
3762 ret = fc_bsg_rport_prep(rport);
3766 case BLK_STS_RESOURCE:
3772 return fc_bsg_dispatch(job);
3776 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3777 * @shost: shost for fc_host
3778 * @fc_host: fc_host adding the structures to
3781 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3783 struct device *dev = &shost->shost_gendev;
3784 struct fc_internal *i = to_fc_internal(shost->transportt);
3785 struct request_queue *q;
3788 fc_host->rqst_q = NULL;
3790 if (!i->f->bsg_request)
3793 snprintf(bsg_name, sizeof(bsg_name),
3794 "fc_host%d", shost->host_no);
3796 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout,
3800 "fc_host%d: bsg interface failed to initialize - setup queue\n",
3804 __scsi_init_queue(shost, q);
3805 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3806 fc_host->rqst_q = q;
3811 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3812 * @shost: shost that rport is attached to
3813 * @rport: rport that the bsg hooks are being attached to
3816 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3818 struct device *dev = &rport->dev;
3819 struct fc_internal *i = to_fc_internal(shost->transportt);
3820 struct request_queue *q;
3822 rport->rqst_q = NULL;
3824 if (!i->f->bsg_request)
3827 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep,
3828 fc_bsg_job_timeout, i->f->dd_bsg_size);
3830 dev_err(dev, "failed to setup bsg queue\n");
3833 __scsi_init_queue(shost, q);
3834 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3841 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3842 * @q: the request_queue that is to be torn down.
3845 * Before unregistering the queue empty any requests that are blocked
3850 fc_bsg_remove(struct request_queue *q)
3852 bsg_remove_queue(q);
3856 /* Original Author: Martin Hicks */
3857 MODULE_AUTHOR("James Smart");
3858 MODULE_DESCRIPTION("FC Transport Attributes");
3859 MODULE_LICENSE("GPL");
3861 module_init(fc_transport_init);
3862 module_exit(fc_transport_exit);