2 * FiberChannel transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
23 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/kernel.h>
33 #include <linux/bsg-lib.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_fc.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <net/netlink.h>
40 #include <scsi/scsi_netlink_fc.h>
41 #include <scsi/scsi_bsg_fc.h>
42 #include "scsi_priv.h"
44 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
45 static void fc_vport_sched_delete(struct work_struct *work);
46 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
47 struct device *pdev, struct fc_vport_identifiers *ids,
48 struct fc_vport **vport);
49 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
50 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
51 static void fc_bsg_remove(struct request_queue *);
52 static void fc_bsg_goose_queue(struct fc_rport *);
59 * dev_loss_tmo: the default number of seconds that the FC transport
60 * should insulate the loss of a remote port.
61 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
63 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
65 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
66 MODULE_PARM_DESC(dev_loss_tmo,
67 "Maximum number of seconds that the FC transport should"
68 " insulate the loss of a remote port. Once this value is"
69 " exceeded, the scsi target is removed. Value should be"
70 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
71 " fast_io_fail_tmo is not set.");
74 * Redefine so that we can have same named attributes in the
75 * sdev/starget/host objects.
77 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
78 struct device_attribute device_attr_##_prefix##_##_name = \
79 __ATTR(_name,_mode,_show,_store)
81 #define fc_enum_name_search(title, table_type, table) \
82 static const char *get_fc_##title##_name(enum table_type table_key) \
87 for (i = 0; i < ARRAY_SIZE(table); i++) { \
88 if (table[i].value == table_key) { \
89 name = table[i].name; \
96 #define fc_enum_name_match(title, table_type, table) \
97 static int get_fc_##title##_match(const char *table_key, \
98 enum table_type *value) \
102 for (i = 0; i < ARRAY_SIZE(table); i++) { \
103 if (strncmp(table_key, table[i].name, \
104 table[i].matchlen) == 0) { \
105 *value = table[i].value; \
106 return 0; /* success */ \
109 return 1; /* failure */ \
113 /* Convert fc_port_type values to ascii string name */
115 enum fc_port_type value;
117 } fc_port_type_names[] = {
118 { FC_PORTTYPE_UNKNOWN, "Unknown" },
119 { FC_PORTTYPE_OTHER, "Other" },
120 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
121 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
122 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
123 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
124 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
125 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
127 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
128 #define FC_PORTTYPE_MAX_NAMELEN 50
130 /* Reuse fc_port_type enum function for vport_type */
131 #define get_fc_vport_type_name get_fc_port_type_name
134 /* Convert fc_host_event_code values to ascii string name */
135 static const struct {
136 enum fc_host_event_code value;
138 } fc_host_event_code_names[] = {
139 { FCH_EVT_LIP, "lip" },
140 { FCH_EVT_LINKUP, "link_up" },
141 { FCH_EVT_LINKDOWN, "link_down" },
142 { FCH_EVT_LIPRESET, "lip_reset" },
143 { FCH_EVT_RSCN, "rscn" },
144 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
145 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
146 { FCH_EVT_PORT_ONLINE, "port_online" },
147 { FCH_EVT_PORT_OFFLINE, "port_offline" },
148 { FCH_EVT_PORT_FABRIC, "port_fabric" },
149 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
150 { FCH_EVT_LINK_FPIN, "link_FPIN" },
151 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
153 fc_enum_name_search(host_event_code, fc_host_event_code,
154 fc_host_event_code_names)
155 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
158 /* Convert fc_port_state values to ascii string name */
160 enum fc_port_state value;
162 } fc_port_state_names[] = {
163 { FC_PORTSTATE_UNKNOWN, "Unknown" },
164 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
165 { FC_PORTSTATE_ONLINE, "Online" },
166 { FC_PORTSTATE_OFFLINE, "Offline" },
167 { FC_PORTSTATE_BLOCKED, "Blocked" },
168 { FC_PORTSTATE_BYPASSED, "Bypassed" },
169 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
170 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
171 { FC_PORTSTATE_ERROR, "Error" },
172 { FC_PORTSTATE_LOOPBACK, "Loopback" },
173 { FC_PORTSTATE_DELETED, "Deleted" },
175 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
176 #define FC_PORTSTATE_MAX_NAMELEN 20
179 /* Convert fc_vport_state values to ascii string name */
181 enum fc_vport_state value;
183 } fc_vport_state_names[] = {
184 { FC_VPORT_UNKNOWN, "Unknown" },
185 { FC_VPORT_ACTIVE, "Active" },
186 { FC_VPORT_DISABLED, "Disabled" },
187 { FC_VPORT_LINKDOWN, "Linkdown" },
188 { FC_VPORT_INITIALIZING, "Initializing" },
189 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
190 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
191 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
192 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
193 { FC_VPORT_FAILED, "VPort Failed" },
195 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
196 #define FC_VPORTSTATE_MAX_NAMELEN 24
198 /* Reuse fc_vport_state enum function for vport_last_state */
199 #define get_fc_vport_last_state_name get_fc_vport_state_name
202 /* Convert fc_tgtid_binding_type values to ascii string name */
203 static const struct {
204 enum fc_tgtid_binding_type value;
207 } fc_tgtid_binding_type_names[] = {
208 { FC_TGTID_BIND_NONE, "none", 4 },
209 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
210 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
211 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
213 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
214 fc_tgtid_binding_type_names)
215 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
216 fc_tgtid_binding_type_names)
217 #define FC_BINDTYPE_MAX_NAMELEN 30
220 #define fc_bitfield_name_search(title, table) \
222 get_fc_##title##_names(u32 table_key, char *buf) \
228 for (i = 0; i < ARRAY_SIZE(table); i++) { \
229 if (table[i].value & table_key) { \
230 len += sprintf(buf + len, "%s%s", \
231 prefix, table[i].name); \
235 len += sprintf(buf + len, "\n"); \
240 /* Convert FC_COS bit values to ascii string name */
241 static const struct {
245 { FC_COS_CLASS1, "Class 1" },
246 { FC_COS_CLASS2, "Class 2" },
247 { FC_COS_CLASS3, "Class 3" },
248 { FC_COS_CLASS4, "Class 4" },
249 { FC_COS_CLASS6, "Class 6" },
251 fc_bitfield_name_search(cos, fc_cos_names)
254 /* Convert FC_PORTSPEED bit values to ascii string name */
255 static const struct {
258 } fc_port_speed_names[] = {
259 { FC_PORTSPEED_1GBIT, "1 Gbit" },
260 { FC_PORTSPEED_2GBIT, "2 Gbit" },
261 { FC_PORTSPEED_4GBIT, "4 Gbit" },
262 { FC_PORTSPEED_10GBIT, "10 Gbit" },
263 { FC_PORTSPEED_8GBIT, "8 Gbit" },
264 { FC_PORTSPEED_16GBIT, "16 Gbit" },
265 { FC_PORTSPEED_32GBIT, "32 Gbit" },
266 { FC_PORTSPEED_20GBIT, "20 Gbit" },
267 { FC_PORTSPEED_40GBIT, "40 Gbit" },
268 { FC_PORTSPEED_50GBIT, "50 Gbit" },
269 { FC_PORTSPEED_100GBIT, "100 Gbit" },
270 { FC_PORTSPEED_25GBIT, "25 Gbit" },
271 { FC_PORTSPEED_64GBIT, "64 Gbit" },
272 { FC_PORTSPEED_128GBIT, "128 Gbit" },
273 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
275 fc_bitfield_name_search(port_speed, fc_port_speed_names)
279 show_fc_fc4s (char *buf, u8 *fc4_list)
283 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
284 len += sprintf(buf + len , "0x%02x ", *fc4_list);
285 len += sprintf(buf + len, "\n");
290 /* Convert FC_PORT_ROLE bit values to ascii string name */
291 static const struct {
294 } fc_port_role_names[] = {
295 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
296 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
297 { FC_PORT_ROLE_IP_PORT, "IP Port" },
298 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
299 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
300 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
301 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
303 fc_bitfield_name_search(port_roles, fc_port_role_names)
306 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
308 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
309 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
310 #define FC_FPORT_PORTID 0x00000e
311 #define FC_FABCTLR_PORTID 0x00000d
312 #define FC_DIRSRVR_PORTID 0x00000c
313 #define FC_TIMESRVR_PORTID 0x00000b
314 #define FC_MGMTSRVR_PORTID 0x00000a
317 static void fc_timeout_deleted_rport(struct work_struct *work);
318 static void fc_timeout_fail_rport_io(struct work_struct *work);
319 static void fc_scsi_scan_rport(struct work_struct *work);
322 * Attribute counts pre object type...
323 * Increase these values if you add attributes
325 #define FC_STARGET_NUM_ATTRS 3
326 #define FC_RPORT_NUM_ATTRS 10
327 #define FC_VPORT_NUM_ATTRS 9
328 #define FC_HOST_NUM_ATTRS 29
331 struct scsi_transport_template t;
332 struct fc_function_template *f;
335 * For attributes : each object has :
336 * An array of the actual attributes structures
337 * An array of null-terminated pointers to the attribute
338 * structures - used for mid-layer interaction.
340 * The attribute containers for the starget and host are are
341 * part of the midlayer. As the remote port is specific to the
342 * fc transport, we must provide the attribute container.
344 struct device_attribute private_starget_attrs[
345 FC_STARGET_NUM_ATTRS];
346 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
348 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
349 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
351 struct transport_container rport_attr_cont;
352 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
353 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
355 struct transport_container vport_attr_cont;
356 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
357 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
360 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
362 static int fc_target_setup(struct transport_container *tc, struct device *dev,
365 struct scsi_target *starget = to_scsi_target(dev);
366 struct fc_rport *rport = starget_to_rport(starget);
369 * if parent is remote port, use values from remote port.
370 * Otherwise, this host uses the fc_transport, but not the
371 * remote port interface. As such, initialize to known non-values.
374 fc_starget_node_name(starget) = rport->node_name;
375 fc_starget_port_name(starget) = rport->port_name;
376 fc_starget_port_id(starget) = rport->port_id;
378 fc_starget_node_name(starget) = -1;
379 fc_starget_port_name(starget) = -1;
380 fc_starget_port_id(starget) = -1;
386 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
392 static int fc_host_setup(struct transport_container *tc, struct device *dev,
395 struct Scsi_Host *shost = dev_to_shost(dev);
396 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
399 * Set default values easily detected by the midlayer as
400 * failure cases. The scsi lldd is responsible for initializing
401 * all transport attributes to valid values per host.
403 fc_host->node_name = -1;
404 fc_host->port_name = -1;
405 fc_host->permanent_port_name = -1;
406 fc_host->supported_classes = FC_COS_UNSPECIFIED;
407 memset(fc_host->supported_fc4s, 0,
408 sizeof(fc_host->supported_fc4s));
409 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
410 fc_host->maxframe_size = -1;
411 fc_host->max_npiv_vports = 0;
412 memset(fc_host->serial_number, 0,
413 sizeof(fc_host->serial_number));
414 memset(fc_host->manufacturer, 0,
415 sizeof(fc_host->manufacturer));
416 memset(fc_host->model, 0,
417 sizeof(fc_host->model));
418 memset(fc_host->model_description, 0,
419 sizeof(fc_host->model_description));
420 memset(fc_host->hardware_version, 0,
421 sizeof(fc_host->hardware_version));
422 memset(fc_host->driver_version, 0,
423 sizeof(fc_host->driver_version));
424 memset(fc_host->firmware_version, 0,
425 sizeof(fc_host->firmware_version));
426 memset(fc_host->optionrom_version, 0,
427 sizeof(fc_host->optionrom_version));
429 fc_host->port_id = -1;
430 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
431 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
432 memset(fc_host->active_fc4s, 0,
433 sizeof(fc_host->active_fc4s));
434 fc_host->speed = FC_PORTSPEED_UNKNOWN;
435 fc_host->fabric_name = -1;
436 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
437 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
439 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
441 INIT_LIST_HEAD(&fc_host->rports);
442 INIT_LIST_HEAD(&fc_host->rport_bindings);
443 INIT_LIST_HEAD(&fc_host->vports);
444 fc_host->next_rport_number = 0;
445 fc_host->next_target_id = 0;
446 fc_host->next_vport_number = 0;
447 fc_host->npiv_vports_inuse = 0;
449 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
450 "fc_wq_%d", shost->host_no);
451 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
452 if (!fc_host->work_q)
455 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
456 snprintf(fc_host->devloss_work_q_name,
457 sizeof(fc_host->devloss_work_q_name),
458 "fc_dl_%d", shost->host_no);
459 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
460 fc_host->devloss_work_q_name);
461 if (!fc_host->devloss_work_q) {
462 destroy_workqueue(fc_host->work_q);
463 fc_host->work_q = NULL;
467 fc_bsg_hostadd(shost, fc_host);
468 /* ignore any bsg add error - we just can't do sgio */
473 static int fc_host_remove(struct transport_container *tc, struct device *dev,
476 struct Scsi_Host *shost = dev_to_shost(dev);
477 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
479 fc_bsg_remove(fc_host->rqst_q);
483 static DECLARE_TRANSPORT_CLASS(fc_host_class,
490 * Setup and Remove actions for remote ports are handled
491 * in the service functions below.
493 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
500 * Setup and Remove actions for virtual ports are handled
501 * in the service functions below.
503 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
510 * Netlink Infrastructure
513 static atomic_t fc_event_seq;
516 * fc_get_event_number - Obtain the next sequential FC event number
519 * We could have inlined this, but it would have required fc_event_seq to
520 * be exposed. For now, live with the subroutine call.
521 * Atomic used to avoid lock/unlock...
524 fc_get_event_number(void)
526 return atomic_add_return(1, &fc_event_seq);
528 EXPORT_SYMBOL(fc_get_event_number);
531 * fc_host_post_fc_event - routine to do the work of posting an event
533 * @shost: host the event occurred on
534 * @event_number: fc event number obtained from get_fc_event_number()
535 * @event_code: fc_host event being posted
536 * @data_len: amount, in bytes, of event data
537 * @data_buf: pointer to event data
538 * @vendor_id: value for Vendor id
541 * This routine assumes no locks are held on entry.
544 fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
545 enum fc_host_event_code event_code,
546 u32 data_len, char *data_buf, u64 vendor_id)
549 struct nlmsghdr *nlh;
550 struct fc_nl_event *event;
555 if (!data_buf || data_len < 4)
563 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
565 skb = nlmsg_new(len, GFP_KERNEL);
571 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
576 event = nlmsg_data(nlh);
578 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
579 FC_NL_ASYNC_EVENT, len);
580 event->seconds = ktime_get_real_seconds();
581 event->vendor_id = vendor_id;
582 event->host_no = shost->host_no;
583 event->event_datalen = data_len; /* bytes */
584 event->event_num = event_number;
585 event->event_code = event_code;
587 memcpy(&event->event_data, data_buf, data_len);
589 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
596 name = get_fc_host_event_code_name(event_code);
598 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
599 __func__, shost->host_no,
600 (name) ? name : "<unknown>",
601 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
604 EXPORT_SYMBOL(fc_host_post_fc_event);
607 * fc_host_post_event - called to post an even on an fc_host.
608 * @shost: host the event occurred on
609 * @event_number: fc event number obtained from get_fc_event_number()
610 * @event_code: fc_host event being posted
611 * @event_data: 32bits of data for the event being posted
614 * This routine assumes no locks are held on entry.
617 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
618 enum fc_host_event_code event_code, u32 event_data)
620 fc_host_post_fc_event(shost, event_number, event_code,
621 (u32)sizeof(u32), (char *)&event_data, 0);
623 EXPORT_SYMBOL(fc_host_post_event);
627 * fc_host_post_vendor_event - called to post a vendor unique event
629 * @shost: host the event occurred on
630 * @event_number: fc event number obtained from get_fc_event_number()
631 * @data_len: amount, in bytes, of vendor unique data
632 * @data_buf: pointer to vendor unique data
633 * @vendor_id: Vendor id
636 * This routine assumes no locks are held on entry.
639 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
640 u32 data_len, char * data_buf, u64 vendor_id)
642 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
643 data_len, data_buf, vendor_id);
645 EXPORT_SYMBOL(fc_host_post_vendor_event);
648 * fc_host_rcv_fpin - routine to process a received FPIN.
649 * @shost: host the FPIN was received on
650 * @fpin_len: length of FPIN payload, in bytes
651 * @fpin_buf: pointer to FPIN payload
654 * This routine assumes no locks are held on entry.
657 fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf)
659 fc_host_post_fc_event(shost, fc_get_event_number(),
660 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0);
662 EXPORT_SYMBOL(fc_host_fpin_rcv);
665 static __init int fc_transport_init(void)
669 atomic_set(&fc_event_seq, 0);
671 error = transport_class_register(&fc_host_class);
674 error = transport_class_register(&fc_vport_class);
676 goto unreg_host_class;
677 error = transport_class_register(&fc_rport_class);
679 goto unreg_vport_class;
680 error = transport_class_register(&fc_transport_class);
682 goto unreg_rport_class;
686 transport_class_unregister(&fc_rport_class);
688 transport_class_unregister(&fc_vport_class);
690 transport_class_unregister(&fc_host_class);
694 static void __exit fc_transport_exit(void)
696 transport_class_unregister(&fc_transport_class);
697 transport_class_unregister(&fc_rport_class);
698 transport_class_unregister(&fc_host_class);
699 transport_class_unregister(&fc_vport_class);
703 * FC Remote Port Attribute Management
706 #define fc_rport_show_function(field, format_string, sz, cast) \
708 show_fc_rport_##field (struct device *dev, \
709 struct device_attribute *attr, char *buf) \
711 struct fc_rport *rport = transport_class_to_rport(dev); \
712 struct Scsi_Host *shost = rport_to_shost(rport); \
713 struct fc_internal *i = to_fc_internal(shost->transportt); \
714 if ((i->f->get_rport_##field) && \
715 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
716 (rport->port_state == FC_PORTSTATE_DELETED) || \
717 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
718 i->f->get_rport_##field(rport); \
719 return snprintf(buf, sz, format_string, cast rport->field); \
722 #define fc_rport_store_function(field) \
724 store_fc_rport_##field(struct device *dev, \
725 struct device_attribute *attr, \
726 const char *buf, size_t count) \
729 struct fc_rport *rport = transport_class_to_rport(dev); \
730 struct Scsi_Host *shost = rport_to_shost(rport); \
731 struct fc_internal *i = to_fc_internal(shost->transportt); \
733 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
734 (rport->port_state == FC_PORTSTATE_DELETED) || \
735 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
737 val = simple_strtoul(buf, &cp, 0); \
738 if (*cp && (*cp != '\n')) \
740 i->f->set_rport_##field(rport, val); \
744 #define fc_rport_rd_attr(field, format_string, sz) \
745 fc_rport_show_function(field, format_string, sz, ) \
746 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
747 show_fc_rport_##field, NULL)
749 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
750 fc_rport_show_function(field, format_string, sz, (cast)) \
751 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
752 show_fc_rport_##field, NULL)
754 #define fc_rport_rw_attr(field, format_string, sz) \
755 fc_rport_show_function(field, format_string, sz, ) \
756 fc_rport_store_function(field) \
757 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
758 show_fc_rport_##field, \
759 store_fc_rport_##field)
762 #define fc_private_rport_show_function(field, format_string, sz, cast) \
764 show_fc_rport_##field (struct device *dev, \
765 struct device_attribute *attr, char *buf) \
767 struct fc_rport *rport = transport_class_to_rport(dev); \
768 return snprintf(buf, sz, format_string, cast rport->field); \
771 #define fc_private_rport_rd_attr(field, format_string, sz) \
772 fc_private_rport_show_function(field, format_string, sz, ) \
773 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
774 show_fc_rport_##field, NULL)
776 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
777 fc_private_rport_show_function(field, format_string, sz, (cast)) \
778 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
779 show_fc_rport_##field, NULL)
782 #define fc_private_rport_rd_enum_attr(title, maxlen) \
784 show_fc_rport_##title (struct device *dev, \
785 struct device_attribute *attr, char *buf) \
787 struct fc_rport *rport = transport_class_to_rport(dev); \
789 name = get_fc_##title##_name(rport->title); \
792 return snprintf(buf, maxlen, "%s\n", name); \
794 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
795 show_fc_rport_##title, NULL)
798 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
799 i->private_rport_attrs[count] = device_attr_rport_##field; \
800 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
801 i->private_rport_attrs[count].store = NULL; \
802 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
803 if (i->f->show_rport_##field) \
806 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
807 i->private_rport_attrs[count] = device_attr_rport_##field; \
808 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
809 i->private_rport_attrs[count].store = NULL; \
810 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
813 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
814 i->private_rport_attrs[count] = device_attr_rport_##field; \
815 if (!i->f->set_rport_##field) { \
816 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
817 i->private_rport_attrs[count].store = NULL; \
819 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
820 if (i->f->show_rport_##field) \
823 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
825 i->private_rport_attrs[count] = device_attr_rport_##field; \
826 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
831 /* The FC Transport Remote Port Attributes: */
833 /* Fixed Remote Port Attributes */
835 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
838 show_fc_rport_supported_classes (struct device *dev,
839 struct device_attribute *attr, char *buf)
841 struct fc_rport *rport = transport_class_to_rport(dev);
842 if (rport->supported_classes == FC_COS_UNSPECIFIED)
843 return snprintf(buf, 20, "unspecified\n");
844 return get_fc_cos_names(rport->supported_classes, buf);
846 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
847 show_fc_rport_supported_classes, NULL);
849 /* Dynamic Remote Port Attributes */
852 * dev_loss_tmo attribute
854 static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
858 *val = simple_strtoul(buf, &cp, 0);
859 if (*cp && (*cp != '\n'))
862 * Check for overflow; dev_loss_tmo is u32
870 static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
873 struct Scsi_Host *shost = rport_to_shost(rport);
874 struct fc_internal *i = to_fc_internal(shost->transportt);
876 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
877 (rport->port_state == FC_PORTSTATE_DELETED) ||
878 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
881 * Check for overflow; dev_loss_tmo is u32
887 * If fast_io_fail is off we have to cap
888 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
890 if (rport->fast_io_fail_tmo == -1 &&
891 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
894 i->f->set_rport_dev_loss_tmo(rport, val);
898 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
900 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
901 const char *buf, size_t count)
903 struct fc_rport *rport = transport_class_to_rport(dev);
907 rc = fc_str_to_dev_loss(buf, &val);
911 rc = fc_rport_set_dev_loss_tmo(rport, val);
916 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
917 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
920 /* Private Remote Port Attributes */
922 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
923 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
924 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
927 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
930 struct fc_rport *rport = transport_class_to_rport(dev);
932 /* identify any roles that are port_id specific */
933 if ((rport->port_id != -1) &&
934 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
935 FC_WELLKNOWN_PORTID_MASK) {
936 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
937 case FC_FPORT_PORTID:
938 return snprintf(buf, 30, "Fabric Port\n");
939 case FC_FABCTLR_PORTID:
940 return snprintf(buf, 30, "Fabric Controller\n");
941 case FC_DIRSRVR_PORTID:
942 return snprintf(buf, 30, "Directory Server\n");
943 case FC_TIMESRVR_PORTID:
944 return snprintf(buf, 30, "Time Server\n");
945 case FC_MGMTSRVR_PORTID:
946 return snprintf(buf, 30, "Management Server\n");
948 return snprintf(buf, 30, "Unknown Fabric Entity\n");
951 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
952 return snprintf(buf, 20, "unknown\n");
953 return get_fc_port_roles_names(rport->roles, buf);
956 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
957 show_fc_rport_roles, NULL);
959 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
960 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
963 * fast_io_fail_tmo attribute
966 show_fc_rport_fast_io_fail_tmo (struct device *dev,
967 struct device_attribute *attr, char *buf)
969 struct fc_rport *rport = transport_class_to_rport(dev);
971 if (rport->fast_io_fail_tmo == -1)
972 return snprintf(buf, 5, "off\n");
973 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
977 store_fc_rport_fast_io_fail_tmo(struct device *dev,
978 struct device_attribute *attr, const char *buf,
983 struct fc_rport *rport = transport_class_to_rport(dev);
985 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
986 (rport->port_state == FC_PORTSTATE_DELETED) ||
987 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
989 if (strncmp(buf, "off", 3) == 0)
990 rport->fast_io_fail_tmo = -1;
992 val = simple_strtoul(buf, &cp, 0);
993 if ((*cp && (*cp != '\n')) || (val < 0))
996 * Cap fast_io_fail by dev_loss_tmo or
997 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
999 if ((val >= rport->dev_loss_tmo) ||
1000 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
1003 rport->fast_io_fail_tmo = val;
1007 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
1008 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
1012 * FC SCSI Target Attribute Management
1016 * Note: in the target show function we recognize when the remote
1017 * port is in the hierarchy and do not allow the driver to get
1018 * involved in sysfs functions. The driver only gets involved if
1019 * it's the "old" style that doesn't use rports.
1021 #define fc_starget_show_function(field, format_string, sz, cast) \
1023 show_fc_starget_##field (struct device *dev, \
1024 struct device_attribute *attr, char *buf) \
1026 struct scsi_target *starget = transport_class_to_starget(dev); \
1027 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1028 struct fc_internal *i = to_fc_internal(shost->transportt); \
1029 struct fc_rport *rport = starget_to_rport(starget); \
1031 fc_starget_##field(starget) = rport->field; \
1032 else if (i->f->get_starget_##field) \
1033 i->f->get_starget_##field(starget); \
1034 return snprintf(buf, sz, format_string, \
1035 cast fc_starget_##field(starget)); \
1038 #define fc_starget_rd_attr(field, format_string, sz) \
1039 fc_starget_show_function(field, format_string, sz, ) \
1040 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1041 show_fc_starget_##field, NULL)
1043 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1044 fc_starget_show_function(field, format_string, sz, (cast)) \
1045 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1046 show_fc_starget_##field, NULL)
1048 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
1049 i->private_starget_attrs[count] = device_attr_starget_##field; \
1050 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1051 i->private_starget_attrs[count].store = NULL; \
1052 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1053 if (i->f->show_starget_##field) \
1056 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
1057 i->private_starget_attrs[count] = device_attr_starget_##field; \
1058 if (!i->f->set_starget_##field) { \
1059 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1060 i->private_starget_attrs[count].store = NULL; \
1062 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1063 if (i->f->show_starget_##field) \
1066 /* The FC Transport SCSI Target Attributes: */
1067 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1068 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1069 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1073 * FC Virtual Port Attribute Management
1076 #define fc_vport_show_function(field, format_string, sz, cast) \
1078 show_fc_vport_##field (struct device *dev, \
1079 struct device_attribute *attr, char *buf) \
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); \
1084 if ((i->f->get_vport_##field) && \
1085 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1086 i->f->get_vport_##field(vport); \
1087 return snprintf(buf, sz, format_string, cast vport->field); \
1090 #define fc_vport_store_function(field) \
1092 store_fc_vport_##field(struct device *dev, \
1093 struct device_attribute *attr, \
1094 const char *buf, size_t count) \
1097 struct fc_vport *vport = transport_class_to_vport(dev); \
1098 struct Scsi_Host *shost = vport_to_shost(vport); \
1099 struct fc_internal *i = to_fc_internal(shost->transportt); \
1101 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1103 val = simple_strtoul(buf, &cp, 0); \
1104 if (*cp && (*cp != '\n')) \
1106 i->f->set_vport_##field(vport, val); \
1110 #define fc_vport_store_str_function(field, slen) \
1112 store_fc_vport_##field(struct device *dev, \
1113 struct device_attribute *attr, \
1114 const char *buf, size_t count) \
1116 struct fc_vport *vport = transport_class_to_vport(dev); \
1117 struct Scsi_Host *shost = vport_to_shost(vport); \
1118 struct fc_internal *i = to_fc_internal(shost->transportt); \
1119 unsigned int cnt=count; \
1121 /* count may include a LF at end of string */ \
1122 if (buf[cnt-1] == '\n') \
1124 if (cnt > ((slen) - 1)) \
1126 memcpy(vport->field, buf, cnt); \
1127 i->f->set_vport_##field(vport); \
1131 #define fc_vport_rd_attr(field, format_string, sz) \
1132 fc_vport_show_function(field, format_string, sz, ) \
1133 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1134 show_fc_vport_##field, NULL)
1136 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1137 fc_vport_show_function(field, format_string, sz, (cast)) \
1138 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1139 show_fc_vport_##field, NULL)
1141 #define fc_vport_rw_attr(field, format_string, sz) \
1142 fc_vport_show_function(field, format_string, sz, ) \
1143 fc_vport_store_function(field) \
1144 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1145 show_fc_vport_##field, \
1146 store_fc_vport_##field)
1148 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1150 show_fc_vport_##field (struct device *dev, \
1151 struct device_attribute *attr, char *buf) \
1153 struct fc_vport *vport = transport_class_to_vport(dev); \
1154 return snprintf(buf, sz, format_string, cast vport->field); \
1157 #define fc_private_vport_store_u32_function(field) \
1159 store_fc_vport_##field(struct device *dev, \
1160 struct device_attribute *attr, \
1161 const char *buf, size_t count) \
1164 struct fc_vport *vport = transport_class_to_vport(dev); \
1166 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1168 val = simple_strtoul(buf, &cp, 0); \
1169 if (*cp && (*cp != '\n')) \
1171 vport->field = val; \
1176 #define fc_private_vport_rd_attr(field, format_string, sz) \
1177 fc_private_vport_show_function(field, format_string, sz, ) \
1178 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1179 show_fc_vport_##field, NULL)
1181 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1182 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1183 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1184 show_fc_vport_##field, NULL)
1186 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1187 fc_private_vport_show_function(field, format_string, sz, ) \
1188 fc_private_vport_store_u32_function(field) \
1189 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1190 show_fc_vport_##field, \
1191 store_fc_vport_##field)
1194 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1196 show_fc_vport_##title (struct device *dev, \
1197 struct device_attribute *attr, \
1200 struct fc_vport *vport = transport_class_to_vport(dev); \
1202 name = get_fc_##title##_name(vport->title); \
1205 return snprintf(buf, maxlen, "%s\n", name); \
1207 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1208 show_fc_vport_##title, NULL)
1211 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1212 i->private_vport_attrs[count] = device_attr_vport_##field; \
1213 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1214 i->private_vport_attrs[count].store = NULL; \
1215 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1216 if (i->f->get_##field) \
1218 /* NOTE: Above MACRO differs: checks function not show bit */
1220 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1221 i->private_vport_attrs[count] = device_attr_vport_##field; \
1222 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1223 i->private_vport_attrs[count].store = NULL; \
1224 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1227 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1228 i->private_vport_attrs[count] = device_attr_vport_##field; \
1229 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1232 /* NOTE: Above MACRO differs: checks function */
1234 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1235 i->private_vport_attrs[count] = device_attr_vport_##field; \
1236 if (!i->f->set_vport_##field) { \
1237 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1238 i->private_vport_attrs[count].store = NULL; \
1240 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1242 /* NOTE: Above MACRO differs: does not check show bit */
1244 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1246 i->private_vport_attrs[count] = device_attr_vport_##field; \
1247 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1252 /* The FC Transport Virtual Port Attributes: */
1254 /* Fixed Virtual Port Attributes */
1256 /* Dynamic Virtual Port Attributes */
1258 /* Private Virtual Port Attributes */
1260 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1261 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1262 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1263 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1266 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1269 struct fc_vport *vport = transport_class_to_vport(dev);
1271 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1272 return snprintf(buf, 20, "unknown\n");
1273 return get_fc_port_roles_names(vport->roles, buf);
1275 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1277 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1279 fc_private_vport_show_function(symbolic_name, "%s\n",
1280 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1281 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1282 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1283 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1286 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1287 const char *buf, size_t count)
1289 struct fc_vport *vport = transport_class_to_vport(dev);
1290 struct Scsi_Host *shost = vport_to_shost(vport);
1291 unsigned long flags;
1293 spin_lock_irqsave(shost->host_lock, flags);
1294 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) {
1295 spin_unlock_irqrestore(shost->host_lock, flags);
1298 vport->flags |= FC_VPORT_DELETING;
1299 spin_unlock_irqrestore(shost->host_lock, flags);
1301 fc_queue_work(shost, &vport->vport_delete_work);
1304 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1305 NULL, store_fc_vport_delete);
1309 * Enable/Disable vport
1310 * Write "1" to disable, write "0" to enable
1313 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1317 struct fc_vport *vport = transport_class_to_vport(dev);
1318 struct Scsi_Host *shost = vport_to_shost(vport);
1319 struct fc_internal *i = to_fc_internal(shost->transportt);
1322 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1326 if (vport->vport_state != FC_VPORT_DISABLED)
1328 } else if (*buf == '1') {
1329 if (vport->vport_state == FC_VPORT_DISABLED)
1334 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1335 return stat ? stat : count;
1337 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1338 NULL, store_fc_vport_disable);
1342 * Host Attribute Management
1345 #define fc_host_show_function(field, format_string, sz, cast) \
1347 show_fc_host_##field (struct device *dev, \
1348 struct device_attribute *attr, char *buf) \
1350 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1351 struct fc_internal *i = to_fc_internal(shost->transportt); \
1352 if (i->f->get_host_##field) \
1353 i->f->get_host_##field(shost); \
1354 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1357 #define fc_host_store_function(field) \
1359 store_fc_host_##field(struct device *dev, \
1360 struct device_attribute *attr, \
1361 const char *buf, size_t count) \
1364 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1365 struct fc_internal *i = to_fc_internal(shost->transportt); \
1368 val = simple_strtoul(buf, &cp, 0); \
1369 if (*cp && (*cp != '\n')) \
1371 i->f->set_host_##field(shost, val); \
1375 #define fc_host_store_str_function(field, slen) \
1377 store_fc_host_##field(struct device *dev, \
1378 struct device_attribute *attr, \
1379 const char *buf, size_t count) \
1381 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1382 struct fc_internal *i = to_fc_internal(shost->transportt); \
1383 unsigned int cnt=count; \
1385 /* count may include a LF at end of string */ \
1386 if (buf[cnt-1] == '\n') \
1388 if (cnt > ((slen) - 1)) \
1390 memcpy(fc_host_##field(shost), buf, cnt); \
1391 i->f->set_host_##field(shost); \
1395 #define fc_host_rd_attr(field, format_string, sz) \
1396 fc_host_show_function(field, format_string, sz, ) \
1397 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1398 show_fc_host_##field, NULL)
1400 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1401 fc_host_show_function(field, format_string, sz, (cast)) \
1402 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1403 show_fc_host_##field, NULL)
1405 #define fc_host_rw_attr(field, format_string, sz) \
1406 fc_host_show_function(field, format_string, sz, ) \
1407 fc_host_store_function(field) \
1408 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1409 show_fc_host_##field, \
1410 store_fc_host_##field)
1412 #define fc_host_rd_enum_attr(title, maxlen) \
1414 show_fc_host_##title (struct device *dev, \
1415 struct device_attribute *attr, char *buf) \
1417 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1418 struct fc_internal *i = to_fc_internal(shost->transportt); \
1420 if (i->f->get_host_##title) \
1421 i->f->get_host_##title(shost); \
1422 name = get_fc_##title##_name(fc_host_##title(shost)); \
1425 return snprintf(buf, maxlen, "%s\n", name); \
1427 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1429 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1430 i->private_host_attrs[count] = device_attr_host_##field; \
1431 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1432 i->private_host_attrs[count].store = NULL; \
1433 i->host_attrs[count] = &i->private_host_attrs[count]; \
1434 if (i->f->show_host_##field) \
1437 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1438 i->private_host_attrs[count] = device_attr_host_##field; \
1439 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1440 i->private_host_attrs[count].store = NULL; \
1441 i->host_attrs[count] = &i->private_host_attrs[count]; \
1444 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1445 i->private_host_attrs[count] = device_attr_host_##field; \
1446 if (!i->f->set_host_##field) { \
1447 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1448 i->private_host_attrs[count].store = NULL; \
1450 i->host_attrs[count] = &i->private_host_attrs[count]; \
1451 if (i->f->show_host_##field) \
1455 #define fc_private_host_show_function(field, format_string, sz, cast) \
1457 show_fc_host_##field (struct device *dev, \
1458 struct device_attribute *attr, char *buf) \
1460 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1461 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1464 #define fc_private_host_rd_attr(field, format_string, sz) \
1465 fc_private_host_show_function(field, format_string, sz, ) \
1466 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1467 show_fc_host_##field, NULL)
1469 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1470 fc_private_host_show_function(field, format_string, sz, (cast)) \
1471 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1472 show_fc_host_##field, NULL)
1474 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1475 i->private_host_attrs[count] = device_attr_host_##field; \
1476 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1477 i->private_host_attrs[count].store = NULL; \
1478 i->host_attrs[count] = &i->private_host_attrs[count]; \
1481 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1483 i->private_host_attrs[count] = device_attr_host_##field; \
1484 i->host_attrs[count] = &i->private_host_attrs[count]; \
1489 /* Fixed Host Attributes */
1492 show_fc_host_supported_classes (struct device *dev,
1493 struct device_attribute *attr, char *buf)
1495 struct Scsi_Host *shost = transport_class_to_shost(dev);
1497 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1498 return snprintf(buf, 20, "unspecified\n");
1500 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1502 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1503 show_fc_host_supported_classes, NULL);
1506 show_fc_host_supported_fc4s (struct device *dev,
1507 struct device_attribute *attr, char *buf)
1509 struct Scsi_Host *shost = transport_class_to_shost(dev);
1510 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1512 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1513 show_fc_host_supported_fc4s, NULL);
1516 show_fc_host_supported_speeds (struct device *dev,
1517 struct device_attribute *attr, char *buf)
1519 struct Scsi_Host *shost = transport_class_to_shost(dev);
1521 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1522 return snprintf(buf, 20, "unknown\n");
1524 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1526 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1527 show_fc_host_supported_speeds, NULL);
1530 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1531 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1532 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1533 unsigned long long);
1534 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1535 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1536 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1537 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1538 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1539 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1540 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1541 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1542 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1543 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1546 /* Dynamic Host Attributes */
1549 show_fc_host_active_fc4s (struct device *dev,
1550 struct device_attribute *attr, char *buf)
1552 struct Scsi_Host *shost = transport_class_to_shost(dev);
1553 struct fc_internal *i = to_fc_internal(shost->transportt);
1555 if (i->f->get_host_active_fc4s)
1556 i->f->get_host_active_fc4s(shost);
1558 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1560 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1561 show_fc_host_active_fc4s, NULL);
1564 show_fc_host_speed (struct device *dev,
1565 struct device_attribute *attr, char *buf)
1567 struct Scsi_Host *shost = transport_class_to_shost(dev);
1568 struct fc_internal *i = to_fc_internal(shost->transportt);
1570 if (i->f->get_host_speed)
1571 i->f->get_host_speed(shost);
1573 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1574 return snprintf(buf, 20, "unknown\n");
1576 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1578 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1579 show_fc_host_speed, NULL);
1582 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1583 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1584 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1585 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1586 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1588 fc_private_host_show_function(system_hostname, "%s\n",
1589 FC_SYMBOLIC_NAME_SIZE + 1, )
1590 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1591 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1592 show_fc_host_system_hostname, store_fc_host_system_hostname);
1595 /* Private Host Attributes */
1598 show_fc_private_host_tgtid_bind_type(struct device *dev,
1599 struct device_attribute *attr, char *buf)
1601 struct Scsi_Host *shost = transport_class_to_shost(dev);
1604 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1607 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1610 #define get_list_head_entry(pos, head, member) \
1611 pos = list_entry((head)->next, typeof(*pos), member)
1614 store_fc_private_host_tgtid_bind_type(struct device *dev,
1615 struct device_attribute *attr, const char *buf, size_t count)
1617 struct Scsi_Host *shost = transport_class_to_shost(dev);
1618 struct fc_rport *rport;
1619 enum fc_tgtid_binding_type val;
1620 unsigned long flags;
1622 if (get_fc_tgtid_bind_type_match(buf, &val))
1625 /* if changing bind type, purge all unused consistent bindings */
1626 if (val != fc_host_tgtid_bind_type(shost)) {
1627 spin_lock_irqsave(shost->host_lock, flags);
1628 while (!list_empty(&fc_host_rport_bindings(shost))) {
1629 get_list_head_entry(rport,
1630 &fc_host_rport_bindings(shost), peers);
1631 list_del(&rport->peers);
1632 rport->port_state = FC_PORTSTATE_DELETED;
1633 fc_queue_work(shost, &rport->rport_delete_work);
1635 spin_unlock_irqrestore(shost->host_lock, flags);
1638 fc_host_tgtid_bind_type(shost) = val;
1642 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1643 show_fc_private_host_tgtid_bind_type,
1644 store_fc_private_host_tgtid_bind_type);
1647 store_fc_private_host_issue_lip(struct device *dev,
1648 struct device_attribute *attr, const char *buf, size_t count)
1650 struct Scsi_Host *shost = transport_class_to_shost(dev);
1651 struct fc_internal *i = to_fc_internal(shost->transportt);
1654 /* ignore any data value written to the attribute */
1655 if (i->f->issue_fc_host_lip) {
1656 ret = i->f->issue_fc_host_lip(shost);
1657 return ret ? ret: count;
1663 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1664 store_fc_private_host_issue_lip);
1667 store_fc_private_host_dev_loss_tmo(struct device *dev,
1668 struct device_attribute *attr,
1669 const char *buf, size_t count)
1671 struct Scsi_Host *shost = transport_class_to_shost(dev);
1672 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1673 struct fc_rport *rport;
1674 unsigned long val, flags;
1677 rc = fc_str_to_dev_loss(buf, &val);
1681 fc_host_dev_loss_tmo(shost) = val;
1682 spin_lock_irqsave(shost->host_lock, flags);
1683 list_for_each_entry(rport, &fc_host->rports, peers)
1684 fc_rport_set_dev_loss_tmo(rport, val);
1685 spin_unlock_irqrestore(shost->host_lock, flags);
1689 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
1690 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
1691 show_fc_host_dev_loss_tmo,
1692 store_fc_private_host_dev_loss_tmo);
1694 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1697 * Host Statistics Management
1700 /* Show a given attribute in the statistics group */
1702 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1704 struct Scsi_Host *shost = transport_class_to_shost(dev);
1705 struct fc_internal *i = to_fc_internal(shost->transportt);
1706 struct fc_host_statistics *stats;
1707 ssize_t ret = -ENOENT;
1709 if (offset > sizeof(struct fc_host_statistics) ||
1710 offset % sizeof(u64) != 0)
1713 if (i->f->get_fc_host_stats) {
1714 stats = (i->f->get_fc_host_stats)(shost);
1716 ret = snprintf(buf, 20, "0x%llx\n",
1717 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1723 /* generate a read-only statistics attribute */
1724 #define fc_host_statistic(name) \
1725 static ssize_t show_fcstat_##name(struct device *cd, \
1726 struct device_attribute *attr, \
1729 return fc_stat_show(cd, buf, \
1730 offsetof(struct fc_host_statistics, name)); \
1732 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1734 fc_host_statistic(seconds_since_last_reset);
1735 fc_host_statistic(tx_frames);
1736 fc_host_statistic(tx_words);
1737 fc_host_statistic(rx_frames);
1738 fc_host_statistic(rx_words);
1739 fc_host_statistic(lip_count);
1740 fc_host_statistic(nos_count);
1741 fc_host_statistic(error_frames);
1742 fc_host_statistic(dumped_frames);
1743 fc_host_statistic(link_failure_count);
1744 fc_host_statistic(loss_of_sync_count);
1745 fc_host_statistic(loss_of_signal_count);
1746 fc_host_statistic(prim_seq_protocol_err_count);
1747 fc_host_statistic(invalid_tx_word_count);
1748 fc_host_statistic(invalid_crc_count);
1749 fc_host_statistic(fcp_input_requests);
1750 fc_host_statistic(fcp_output_requests);
1751 fc_host_statistic(fcp_control_requests);
1752 fc_host_statistic(fcp_input_megabytes);
1753 fc_host_statistic(fcp_output_megabytes);
1754 fc_host_statistic(fcp_packet_alloc_failures);
1755 fc_host_statistic(fcp_packet_aborts);
1756 fc_host_statistic(fcp_frame_alloc_failures);
1757 fc_host_statistic(fc_no_free_exch);
1758 fc_host_statistic(fc_no_free_exch_xid);
1759 fc_host_statistic(fc_xid_not_found);
1760 fc_host_statistic(fc_xid_busy);
1761 fc_host_statistic(fc_seq_not_found);
1762 fc_host_statistic(fc_non_bls_resp);
1765 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1766 const char *buf, size_t count)
1768 struct Scsi_Host *shost = transport_class_to_shost(dev);
1769 struct fc_internal *i = to_fc_internal(shost->transportt);
1771 /* ignore any data value written to the attribute */
1772 if (i->f->reset_fc_host_stats) {
1773 i->f->reset_fc_host_stats(shost);
1779 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1780 fc_reset_statistics);
1782 static struct attribute *fc_statistics_attrs[] = {
1783 &device_attr_host_seconds_since_last_reset.attr,
1784 &device_attr_host_tx_frames.attr,
1785 &device_attr_host_tx_words.attr,
1786 &device_attr_host_rx_frames.attr,
1787 &device_attr_host_rx_words.attr,
1788 &device_attr_host_lip_count.attr,
1789 &device_attr_host_nos_count.attr,
1790 &device_attr_host_error_frames.attr,
1791 &device_attr_host_dumped_frames.attr,
1792 &device_attr_host_link_failure_count.attr,
1793 &device_attr_host_loss_of_sync_count.attr,
1794 &device_attr_host_loss_of_signal_count.attr,
1795 &device_attr_host_prim_seq_protocol_err_count.attr,
1796 &device_attr_host_invalid_tx_word_count.attr,
1797 &device_attr_host_invalid_crc_count.attr,
1798 &device_attr_host_fcp_input_requests.attr,
1799 &device_attr_host_fcp_output_requests.attr,
1800 &device_attr_host_fcp_control_requests.attr,
1801 &device_attr_host_fcp_input_megabytes.attr,
1802 &device_attr_host_fcp_output_megabytes.attr,
1803 &device_attr_host_fcp_packet_alloc_failures.attr,
1804 &device_attr_host_fcp_packet_aborts.attr,
1805 &device_attr_host_fcp_frame_alloc_failures.attr,
1806 &device_attr_host_fc_no_free_exch.attr,
1807 &device_attr_host_fc_no_free_exch_xid.attr,
1808 &device_attr_host_fc_xid_not_found.attr,
1809 &device_attr_host_fc_xid_busy.attr,
1810 &device_attr_host_fc_seq_not_found.attr,
1811 &device_attr_host_fc_non_bls_resp.attr,
1812 &device_attr_host_reset_statistics.attr,
1816 static struct attribute_group fc_statistics_group = {
1817 .name = "statistics",
1818 .attrs = fc_statistics_attrs,
1822 /* Host Vport Attributes */
1825 fc_parse_wwn(const char *ns, u64 *nm)
1830 memset(wwn, 0, sizeof(wwn));
1832 /* Validate and store the new name */
1833 for (i=0, j=0; i < 16; i++) {
1836 value = hex_to_bin(*ns++);
1838 j = (j << 4) | value;
1842 wwn[i/2] = j & 0xff;
1847 *nm = wwn_to_u64(wwn);
1854 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1855 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1856 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1857 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1860 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1861 const char *buf, size_t count)
1863 struct Scsi_Host *shost = transport_class_to_shost(dev);
1864 struct fc_vport_identifiers vid;
1865 struct fc_vport *vport;
1866 unsigned int cnt=count;
1869 memset(&vid, 0, sizeof(vid));
1871 /* count may include a LF at end of string */
1872 if (buf[cnt-1] == '\n')
1875 /* validate we have enough characters for WWPN */
1876 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1879 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1883 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1887 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1888 vid.vport_type = FC_PORTTYPE_NPIV;
1889 /* vid.symbolic_name is already zero/NULL's */
1890 vid.disable = false; /* always enabled */
1892 /* we only allow support on Channel 0 !!! */
1893 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1894 return stat ? stat : count;
1896 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1897 store_fc_host_vport_create);
1901 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1902 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1903 * The WWNs are specified as hex characters, and may *not* contain
1904 * any prefixes (e.g. 0x, x, etc)
1907 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1908 const char *buf, size_t count)
1910 struct Scsi_Host *shost = transport_class_to_shost(dev);
1911 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1912 struct fc_vport *vport;
1914 unsigned long flags;
1915 unsigned int cnt=count;
1918 /* count may include a LF at end of string */
1919 if (buf[cnt-1] == '\n')
1922 /* validate we have enough characters for WWPN */
1923 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1926 stat = fc_parse_wwn(&buf[0], &wwpn);
1930 stat = fc_parse_wwn(&buf[17], &wwnn);
1934 spin_lock_irqsave(shost->host_lock, flags);
1936 /* we only allow support on Channel 0 !!! */
1937 list_for_each_entry(vport, &fc_host->vports, peers) {
1938 if ((vport->channel == 0) &&
1939 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1940 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1942 vport->flags |= FC_VPORT_DELETING;
1947 spin_unlock_irqrestore(shost->host_lock, flags);
1952 stat = fc_vport_terminate(vport);
1953 return stat ? stat : count;
1955 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1956 store_fc_host_vport_delete);
1959 static int fc_host_match(struct attribute_container *cont,
1962 struct Scsi_Host *shost;
1963 struct fc_internal *i;
1965 if (!scsi_is_host_device(dev))
1968 shost = dev_to_shost(dev);
1969 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1970 != &fc_host_class.class)
1973 i = to_fc_internal(shost->transportt);
1975 return &i->t.host_attrs.ac == cont;
1978 static int fc_target_match(struct attribute_container *cont,
1981 struct Scsi_Host *shost;
1982 struct fc_internal *i;
1984 if (!scsi_is_target_device(dev))
1987 shost = dev_to_shost(dev->parent);
1988 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1989 != &fc_host_class.class)
1992 i = to_fc_internal(shost->transportt);
1994 return &i->t.target_attrs.ac == cont;
1997 static void fc_rport_dev_release(struct device *dev)
1999 struct fc_rport *rport = dev_to_rport(dev);
2000 put_device(dev->parent);
2004 int scsi_is_fc_rport(const struct device *dev)
2006 return dev->release == fc_rport_dev_release;
2008 EXPORT_SYMBOL(scsi_is_fc_rport);
2010 static int fc_rport_match(struct attribute_container *cont,
2013 struct Scsi_Host *shost;
2014 struct fc_internal *i;
2016 if (!scsi_is_fc_rport(dev))
2019 shost = dev_to_shost(dev->parent);
2020 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2021 != &fc_host_class.class)
2024 i = to_fc_internal(shost->transportt);
2026 return &i->rport_attr_cont.ac == cont;
2030 static void fc_vport_dev_release(struct device *dev)
2032 struct fc_vport *vport = dev_to_vport(dev);
2033 put_device(dev->parent); /* release kobj parent */
2037 static int scsi_is_fc_vport(const struct device *dev)
2039 return dev->release == fc_vport_dev_release;
2042 static int fc_vport_match(struct attribute_container *cont,
2045 struct fc_vport *vport;
2046 struct Scsi_Host *shost;
2047 struct fc_internal *i;
2049 if (!scsi_is_fc_vport(dev))
2051 vport = dev_to_vport(dev);
2053 shost = vport_to_shost(vport);
2054 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2055 != &fc_host_class.class)
2058 i = to_fc_internal(shost->transportt);
2059 return &i->vport_attr_cont.ac == cont;
2064 * fc_eh_timed_out - FC Transport I/O timeout intercept handler
2065 * @scmd: The SCSI command which timed out
2067 * This routine protects against error handlers getting invoked while a
2068 * rport is in a blocked state, typically due to a temporarily loss of
2069 * connectivity. If the error handlers are allowed to proceed, requests
2070 * to abort i/o, reset the target, etc will likely fail as there is no way
2071 * to communicate with the device to perform the requested function. These
2072 * failures may result in the midlayer taking the device offline, requiring
2073 * manual intervention to restore operation.
2075 * This routine, called whenever an i/o times out, validates the state of
2076 * the underlying rport. If the rport is blocked, it returns
2077 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2078 * Eventually, either the device will return, or devloss_tmo will fire,
2079 * and when the timeout then fires, it will be handled normally.
2080 * If the rport is not blocked, normal error handling continues.
2083 * This routine assumes no locks are held on entry.
2085 enum blk_eh_timer_return
2086 fc_eh_timed_out(struct scsi_cmnd *scmd)
2088 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2090 if (rport->port_state == FC_PORTSTATE_BLOCKED)
2091 return BLK_EH_RESET_TIMER;
2095 EXPORT_SYMBOL(fc_eh_timed_out);
2098 * Called by fc_user_scan to locate an rport on the shost that
2099 * matches the channel and target id, and invoke scsi_scan_target()
2103 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2105 struct fc_rport *rport;
2106 unsigned long flags;
2108 spin_lock_irqsave(shost->host_lock, flags);
2110 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2111 if (rport->scsi_target_id == -1)
2114 if (rport->port_state != FC_PORTSTATE_ONLINE)
2117 if ((channel == rport->channel) &&
2118 (id == rport->scsi_target_id)) {
2119 spin_unlock_irqrestore(shost->host_lock, flags);
2120 scsi_scan_target(&rport->dev, channel, id, lun,
2126 spin_unlock_irqrestore(shost->host_lock, flags);
2130 * Called via sysfs scan routines. Necessary, as the FC transport
2131 * wants to place all target objects below the rport object. So this
2132 * routine must invoke the scsi_scan_target() routine with the rport
2133 * object as the parent.
2136 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2141 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2142 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2143 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2146 if (channel == SCAN_WILD_CARD) {
2148 chhi = shost->max_channel + 1;
2154 if (id == SCAN_WILD_CARD) {
2156 tgthi = shost->max_id;
2162 for ( ; chlo < chhi; chlo++)
2163 for ( ; tgtlo < tgthi; tgtlo++)
2164 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2169 struct scsi_transport_template *
2170 fc_attach_transport(struct fc_function_template *ft)
2173 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2179 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2180 i->t.target_attrs.ac.class = &fc_transport_class.class;
2181 i->t.target_attrs.ac.match = fc_target_match;
2182 i->t.target_size = sizeof(struct fc_starget_attrs);
2183 transport_container_register(&i->t.target_attrs);
2185 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2186 i->t.host_attrs.ac.class = &fc_host_class.class;
2187 i->t.host_attrs.ac.match = fc_host_match;
2188 i->t.host_size = sizeof(struct fc_host_attrs);
2189 if (ft->get_fc_host_stats)
2190 i->t.host_attrs.statistics = &fc_statistics_group;
2191 transport_container_register(&i->t.host_attrs);
2193 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2194 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2195 i->rport_attr_cont.ac.match = fc_rport_match;
2196 transport_container_register(&i->rport_attr_cont);
2198 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2199 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2200 i->vport_attr_cont.ac.match = fc_vport_match;
2201 transport_container_register(&i->vport_attr_cont);
2205 /* Transport uses the shost workq for scsi scanning */
2206 i->t.create_work_queue = 1;
2208 i->t.user_scan = fc_user_scan;
2211 * Setup SCSI Target Attributes.
2214 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2215 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2216 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2218 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2220 i->starget_attrs[count] = NULL;
2224 * Setup SCSI Host Attributes.
2227 SETUP_HOST_ATTRIBUTE_RD(node_name);
2228 SETUP_HOST_ATTRIBUTE_RD(port_name);
2229 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2230 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2231 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2232 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2233 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2234 if (ft->vport_create) {
2235 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2236 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2238 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2239 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2240 SETUP_HOST_ATTRIBUTE_RD(model);
2241 SETUP_HOST_ATTRIBUTE_RD(model_description);
2242 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2243 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2244 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2245 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
2247 SETUP_HOST_ATTRIBUTE_RD(port_id);
2248 SETUP_HOST_ATTRIBUTE_RD(port_type);
2249 SETUP_HOST_ATTRIBUTE_RD(port_state);
2250 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2251 SETUP_HOST_ATTRIBUTE_RD(speed);
2252 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2253 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2254 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2256 /* Transport-managed attributes */
2257 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
2258 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2259 if (ft->issue_fc_host_lip)
2260 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2261 if (ft->vport_create)
2262 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2263 if (ft->vport_delete)
2264 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2266 BUG_ON(count > FC_HOST_NUM_ATTRS);
2268 i->host_attrs[count] = NULL;
2271 * Setup Remote Port Attributes.
2274 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2275 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2276 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2277 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2278 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2279 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2280 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2281 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2282 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2283 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2285 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2287 i->rport_attrs[count] = NULL;
2290 * Setup Virtual Port Attributes.
2293 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2294 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2295 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2296 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2297 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2298 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2299 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2300 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2301 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2303 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2305 i->vport_attrs[count] = NULL;
2309 EXPORT_SYMBOL(fc_attach_transport);
2311 void fc_release_transport(struct scsi_transport_template *t)
2313 struct fc_internal *i = to_fc_internal(t);
2315 transport_container_unregister(&i->t.target_attrs);
2316 transport_container_unregister(&i->t.host_attrs);
2317 transport_container_unregister(&i->rport_attr_cont);
2318 transport_container_unregister(&i->vport_attr_cont);
2322 EXPORT_SYMBOL(fc_release_transport);
2325 * fc_queue_work - Queue work to the fc_host workqueue.
2326 * @shost: Pointer to Scsi_Host bound to fc_host.
2327 * @work: Work to queue for execution.
2330 * 1 - work queued for execution
2331 * 0 - work is already queued
2332 * -EINVAL - work queue doesn't exist
2335 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2337 if (unlikely(!fc_host_work_q(shost))) {
2339 "ERROR: FC host '%s' attempted to queue work, "
2340 "when no workqueue created.\n", shost->hostt->name);
2346 return queue_work(fc_host_work_q(shost), work);
2350 * fc_flush_work - Flush a fc_host's workqueue.
2351 * @shost: Pointer to Scsi_Host bound to fc_host.
2354 fc_flush_work(struct Scsi_Host *shost)
2356 if (!fc_host_work_q(shost)) {
2358 "ERROR: FC host '%s' attempted to flush work, "
2359 "when no workqueue created.\n", shost->hostt->name);
2364 flush_workqueue(fc_host_work_q(shost));
2368 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2369 * @shost: Pointer to Scsi_Host bound to fc_host.
2370 * @work: Work to queue for execution.
2371 * @delay: jiffies to delay the work queuing
2374 * 1 on success / 0 already queued / < 0 for error
2377 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2378 unsigned long delay)
2380 if (unlikely(!fc_host_devloss_work_q(shost))) {
2382 "ERROR: FC host '%s' attempted to queue work, "
2383 "when no workqueue created.\n", shost->hostt->name);
2389 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2393 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2394 * @shost: Pointer to Scsi_Host bound to fc_host.
2397 fc_flush_devloss(struct Scsi_Host *shost)
2399 if (!fc_host_devloss_work_q(shost)) {
2401 "ERROR: FC host '%s' attempted to flush work, "
2402 "when no workqueue created.\n", shost->hostt->name);
2407 flush_workqueue(fc_host_devloss_work_q(shost));
2412 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2413 * @shost: Which &Scsi_Host
2415 * This routine is expected to be called immediately preceding the
2416 * a driver's call to scsi_remove_host().
2418 * WARNING: A driver utilizing the fc_transport, which fails to call
2419 * this routine prior to scsi_remove_host(), will leave dangling
2420 * objects in /sys/class/fc_remote_ports. Access to any of these
2421 * objects can result in a system crash !!!
2424 * This routine assumes no locks are held on entry.
2427 fc_remove_host(struct Scsi_Host *shost)
2429 struct fc_vport *vport = NULL, *next_vport = NULL;
2430 struct fc_rport *rport = NULL, *next_rport = NULL;
2431 struct workqueue_struct *work_q;
2432 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2433 unsigned long flags;
2435 spin_lock_irqsave(shost->host_lock, flags);
2437 /* Remove any vports */
2438 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2439 vport->flags |= FC_VPORT_DELETING;
2440 fc_queue_work(shost, &vport->vport_delete_work);
2443 /* Remove any remote ports */
2444 list_for_each_entry_safe(rport, next_rport,
2445 &fc_host->rports, peers) {
2446 list_del(&rport->peers);
2447 rport->port_state = FC_PORTSTATE_DELETED;
2448 fc_queue_work(shost, &rport->rport_delete_work);
2451 list_for_each_entry_safe(rport, next_rport,
2452 &fc_host->rport_bindings, peers) {
2453 list_del(&rport->peers);
2454 rport->port_state = FC_PORTSTATE_DELETED;
2455 fc_queue_work(shost, &rport->rport_delete_work);
2458 spin_unlock_irqrestore(shost->host_lock, flags);
2460 /* flush all scan work items */
2461 scsi_flush_work(shost);
2463 /* flush all stgt delete, and rport delete work items, then kill it */
2464 if (fc_host->work_q) {
2465 work_q = fc_host->work_q;
2466 fc_host->work_q = NULL;
2467 destroy_workqueue(work_q);
2470 /* flush all devloss work items, then kill it */
2471 if (fc_host->devloss_work_q) {
2472 work_q = fc_host->devloss_work_q;
2473 fc_host->devloss_work_q = NULL;
2474 destroy_workqueue(work_q);
2477 EXPORT_SYMBOL(fc_remove_host);
2479 static void fc_terminate_rport_io(struct fc_rport *rport)
2481 struct Scsi_Host *shost = rport_to_shost(rport);
2482 struct fc_internal *i = to_fc_internal(shost->transportt);
2484 /* Involve the LLDD if possible to terminate all io on the rport. */
2485 if (i->f->terminate_rport_io)
2486 i->f->terminate_rport_io(rport);
2489 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2491 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2495 * fc_starget_delete - called to delete the scsi descendants of an rport
2496 * @work: remote port to be operated on.
2498 * Deletes target and all sdevs.
2501 fc_starget_delete(struct work_struct *work)
2503 struct fc_rport *rport =
2504 container_of(work, struct fc_rport, stgt_delete_work);
2506 fc_terminate_rport_io(rport);
2507 scsi_remove_target(&rport->dev);
2512 * fc_rport_final_delete - finish rport termination and delete it.
2513 * @work: remote port to be deleted.
2516 fc_rport_final_delete(struct work_struct *work)
2518 struct fc_rport *rport =
2519 container_of(work, struct fc_rport, rport_delete_work);
2520 struct device *dev = &rport->dev;
2521 struct Scsi_Host *shost = rport_to_shost(rport);
2522 struct fc_internal *i = to_fc_internal(shost->transportt);
2523 unsigned long flags;
2524 int do_callback = 0;
2526 fc_terminate_rport_io(rport);
2529 * if a scan is pending, flush the SCSI Host work_q so that
2530 * that we can reclaim the rport scan work element.
2532 if (rport->flags & FC_RPORT_SCAN_PENDING)
2533 scsi_flush_work(shost);
2536 * Cancel any outstanding timers. These should really exist
2537 * only when rmmod'ing the LLDD and we're asking for
2538 * immediate termination of the rports
2540 spin_lock_irqsave(shost->host_lock, flags);
2541 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2542 spin_unlock_irqrestore(shost->host_lock, flags);
2543 if (!cancel_delayed_work(&rport->fail_io_work))
2544 fc_flush_devloss(shost);
2545 if (!cancel_delayed_work(&rport->dev_loss_work))
2546 fc_flush_devloss(shost);
2547 cancel_work_sync(&rport->scan_work);
2548 spin_lock_irqsave(shost->host_lock, flags);
2549 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2551 spin_unlock_irqrestore(shost->host_lock, flags);
2553 /* Delete SCSI target and sdevs */
2554 if (rport->scsi_target_id != -1)
2555 fc_starget_delete(&rport->stgt_delete_work);
2558 * Notify the driver that the rport is now dead. The LLDD will
2559 * also guarantee that any communication to the rport is terminated
2561 * Avoid this call if we already called it when we preserved the
2562 * rport for the binding.
2564 spin_lock_irqsave(shost->host_lock, flags);
2565 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2566 (i->f->dev_loss_tmo_callbk)) {
2567 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2570 spin_unlock_irqrestore(shost->host_lock, flags);
2573 i->f->dev_loss_tmo_callbk(rport);
2575 fc_bsg_remove(rport->rqst_q);
2577 transport_remove_device(dev);
2579 transport_destroy_device(dev);
2580 scsi_host_put(shost); /* for fc_host->rport list */
2581 put_device(dev); /* for self-reference */
2586 * fc_remote_port_create - allocates and creates a remote FC port.
2587 * @shost: scsi host the remote port is connected to.
2588 * @channel: Channel on shost port connected to.
2589 * @ids: The world wide names, fc address, and FC4 port
2590 * roles for the remote port.
2592 * Allocates and creates the remoter port structure, including the
2593 * class and sysfs creation.
2596 * This routine assumes no locks are held on entry.
2598 static struct fc_rport *
2599 fc_remote_port_create(struct Scsi_Host *shost, int channel,
2600 struct fc_rport_identifiers *ids)
2602 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2603 struct fc_internal *fci = to_fc_internal(shost->transportt);
2604 struct fc_rport *rport;
2606 unsigned long flags;
2610 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2611 rport = kzalloc(size, GFP_KERNEL);
2612 if (unlikely(!rport)) {
2613 printk(KERN_ERR "%s: allocation failure\n", __func__);
2617 rport->maxframe_size = -1;
2618 rport->supported_classes = FC_COS_UNSPECIFIED;
2619 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
2620 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2621 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2622 rport->port_id = ids->port_id;
2623 rport->roles = ids->roles;
2624 rport->port_state = FC_PORTSTATE_ONLINE;
2625 if (fci->f->dd_fcrport_size)
2626 rport->dd_data = &rport[1];
2627 rport->channel = channel;
2628 rport->fast_io_fail_tmo = -1;
2630 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2631 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2632 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2633 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2634 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2636 spin_lock_irqsave(shost->host_lock, flags);
2638 rport->number = fc_host->next_rport_number++;
2639 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
2640 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
2641 rport->scsi_target_id = fc_host->next_target_id++;
2643 rport->scsi_target_id = -1;
2644 list_add_tail(&rport->peers, &fc_host->rports);
2645 scsi_host_get(shost); /* for fc_host->rport list */
2647 spin_unlock_irqrestore(shost->host_lock, flags);
2650 device_initialize(dev); /* takes self reference */
2651 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2652 dev->release = fc_rport_dev_release;
2653 dev_set_name(dev, "rport-%d:%d-%d",
2654 shost->host_no, channel, rport->number);
2655 transport_setup_device(dev);
2657 error = device_add(dev);
2659 printk(KERN_ERR "FC Remote Port device_add failed\n");
2662 transport_add_device(dev);
2663 transport_configure_device(dev);
2665 fc_bsg_rportadd(shost, rport);
2666 /* ignore any bsg add error - we just can't do sgio */
2668 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2669 /* initiate a scan of the target */
2670 rport->flags |= FC_RPORT_SCAN_PENDING;
2671 scsi_queue_work(shost, &rport->scan_work);
2677 transport_destroy_device(dev);
2678 spin_lock_irqsave(shost->host_lock, flags);
2679 list_del(&rport->peers);
2680 scsi_host_put(shost); /* for fc_host->rport list */
2681 spin_unlock_irqrestore(shost->host_lock, flags);
2682 put_device(dev->parent);
2688 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2689 * @shost: scsi host the remote port is connected to.
2690 * @channel: Channel on shost port connected to.
2691 * @ids: The world wide names, fc address, and FC4 port
2692 * roles for the remote port.
2694 * The LLDD calls this routine to notify the transport of the existence
2695 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2696 * of the port, it's FC address (port_id), and the FC4 roles that are
2697 * active for the port.
2699 * For ports that are FCP targets (aka scsi targets), the FC transport
2700 * maintains consistent target id bindings on behalf of the LLDD.
2701 * A consistent target id binding is an assignment of a target id to
2702 * a remote port identifier, which persists while the scsi host is
2703 * attached. The remote port can disappear, then later reappear, and
2704 * it's target id assignment remains the same. This allows for shifts
2705 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2706 * changes to the scsi subsystem which is based on scsi host number and
2707 * target id values. Bindings are only valid during the attachment of
2708 * the scsi host. If the host detaches, then later re-attaches, target
2709 * id bindings may change.
2711 * This routine is responsible for returning a remote port structure.
2712 * The routine will search the list of remote ports it maintains
2713 * internally on behalf of consistent target id mappings. If found, the
2714 * remote port structure will be reused. Otherwise, a new remote port
2715 * structure will be allocated.
2717 * Whenever a remote port is allocated, a new fc_remote_port class
2718 * device is created.
2720 * Should not be called from interrupt context.
2723 * This routine assumes no locks are held on entry.
2726 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2727 struct fc_rport_identifiers *ids)
2729 struct fc_internal *fci = to_fc_internal(shost->transportt);
2730 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2731 struct fc_rport *rport;
2732 unsigned long flags;
2735 /* ensure any stgt delete functions are done */
2736 fc_flush_work(shost);
2739 * Search the list of "active" rports, for an rport that has been
2740 * deleted, but we've held off the real delete while the target
2741 * is in a "blocked" state.
2743 spin_lock_irqsave(shost->host_lock, flags);
2745 list_for_each_entry(rport, &fc_host->rports, peers) {
2747 if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
2748 rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
2749 (rport->channel == channel)) {
2751 switch (fc_host->tgtid_bind_type) {
2752 case FC_TGTID_BIND_BY_WWPN:
2753 case FC_TGTID_BIND_NONE:
2754 if (rport->port_name == ids->port_name)
2757 case FC_TGTID_BIND_BY_WWNN:
2758 if (rport->node_name == ids->node_name)
2761 case FC_TGTID_BIND_BY_ID:
2762 if (rport->port_id == ids->port_id)
2769 memcpy(&rport->node_name, &ids->node_name,
2770 sizeof(rport->node_name));
2771 memcpy(&rport->port_name, &ids->port_name,
2772 sizeof(rport->port_name));
2773 rport->port_id = ids->port_id;
2775 rport->port_state = FC_PORTSTATE_ONLINE;
2776 rport->roles = ids->roles;
2778 spin_unlock_irqrestore(shost->host_lock, flags);
2780 if (fci->f->dd_fcrport_size)
2781 memset(rport->dd_data, 0,
2782 fci->f->dd_fcrport_size);
2785 * If we were not a target, cancel the
2786 * io terminate and rport timers, and
2789 * If we were a target, but our new role
2790 * doesn't indicate a target, leave the
2791 * timers running expecting the role to
2792 * change as the target fully logs in. If
2793 * it doesn't, the target will be torn down.
2795 * If we were a target, and our role shows
2796 * we're still a target, cancel the timers
2797 * and kick off a scan.
2800 /* was a target, not in roles */
2801 if ((rport->scsi_target_id != -1) &&
2802 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2806 * Stop the fail io and dev_loss timers.
2807 * If they flush, the port_state will
2808 * be checked and will NOOP the function.
2810 if (!cancel_delayed_work(&rport->fail_io_work))
2811 fc_flush_devloss(shost);
2812 if (!cancel_delayed_work(&rport->dev_loss_work))
2813 fc_flush_devloss(shost);
2815 spin_lock_irqsave(shost->host_lock, flags);
2817 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2818 FC_RPORT_DEVLOSS_PENDING |
2819 FC_RPORT_DEVLOSS_CALLBK_DONE);
2821 spin_unlock_irqrestore(shost->host_lock, flags);
2823 /* if target, initiate a scan */
2824 if (rport->scsi_target_id != -1) {
2825 scsi_target_unblock(&rport->dev,
2827 spin_lock_irqsave(shost->host_lock,
2829 rport->flags |= FC_RPORT_SCAN_PENDING;
2830 scsi_queue_work(shost,
2832 spin_unlock_irqrestore(shost->host_lock,
2836 fc_bsg_goose_queue(rport);
2844 * Search the bindings array
2845 * Note: if never a FCP target, you won't be on this list
2847 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2849 /* search for a matching consistent binding */
2851 list_for_each_entry(rport, &fc_host->rport_bindings,
2853 if (rport->channel != channel)
2856 switch (fc_host->tgtid_bind_type) {
2857 case FC_TGTID_BIND_BY_WWPN:
2858 if (rport->port_name == ids->port_name)
2861 case FC_TGTID_BIND_BY_WWNN:
2862 if (rport->node_name == ids->node_name)
2865 case FC_TGTID_BIND_BY_ID:
2866 if (rport->port_id == ids->port_id)
2869 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2874 list_move_tail(&rport->peers, &fc_host->rports);
2880 memcpy(&rport->node_name, &ids->node_name,
2881 sizeof(rport->node_name));
2882 memcpy(&rport->port_name, &ids->port_name,
2883 sizeof(rport->port_name));
2884 rport->port_id = ids->port_id;
2885 rport->port_state = FC_PORTSTATE_ONLINE;
2886 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
2888 if (fci->f->dd_fcrport_size)
2889 memset(rport->dd_data, 0,
2890 fci->f->dd_fcrport_size);
2891 spin_unlock_irqrestore(shost->host_lock, flags);
2893 fc_remote_port_rolechg(rport, ids->roles);
2898 spin_unlock_irqrestore(shost->host_lock, flags);
2900 /* No consistent binding found - create new remote port entry */
2901 rport = fc_remote_port_create(shost, channel, ids);
2905 EXPORT_SYMBOL(fc_remote_port_add);
2909 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2910 * @rport: The remote port that no longer exists
2912 * The LLDD calls this routine to notify the transport that a remote
2913 * port is no longer part of the topology. Note: Although a port
2914 * may no longer be part of the topology, it may persist in the remote
2915 * ports displayed by the fc_host. We do this under 2 conditions:
2917 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2918 * This allows the port to temporarily disappear, then reappear without
2919 * disrupting the SCSI device tree attached to it. During the "blocked"
2920 * period the port will still exist.
2922 * 2) If the port was a scsi target and disappears for longer than we
2923 * expect, we'll delete the port and the tear down the SCSI device tree
2924 * attached to it. However, we want to semi-persist the target id assigned
2925 * to that port if it eventually does exist. The port structure will
2926 * remain (although with minimal information) so that the target id
2927 * bindings also remain.
2929 * If the remote port is not an FCP Target, it will be fully torn down
2930 * and deallocated, including the fc_remote_port class device.
2932 * If the remote port is an FCP Target, the port will be placed in a
2933 * temporary blocked state. From the LLDD's perspective, the rport no
2934 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2935 * exists, but all sdevs on it are blocked from further I/O. The following
2938 * If the remote port does not return (signaled by a LLDD call to
2939 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2940 * scsi target is removed - killing all outstanding i/o and removing the
2941 * scsi devices attached to it. The port structure will be marked Not
2942 * Present and be partially cleared, leaving only enough information to
2943 * recognize the remote port relative to the scsi target id binding if
2944 * it later appears. The port will remain as long as there is a valid
2945 * binding (e.g. until the user changes the binding type or unloads the
2946 * scsi host with the binding).
2948 * If the remote port returns within the dev_loss_tmo value (and matches
2949 * according to the target id binding type), the port structure will be
2950 * reused. If it is no longer a SCSI target, the target will be torn
2951 * down. If it continues to be a SCSI target, then the target will be
2952 * unblocked (allowing i/o to be resumed), and a scan will be activated
2953 * to ensure that all luns are detected.
2955 * Called from normal process context only - cannot be called from interrupt.
2958 * This routine assumes no locks are held on entry.
2961 fc_remote_port_delete(struct fc_rport *rport)
2963 struct Scsi_Host *shost = rport_to_shost(rport);
2964 unsigned long timeout = rport->dev_loss_tmo;
2965 unsigned long flags;
2968 * No need to flush the fc_host work_q's, as all adds are synchronous.
2970 * We do need to reclaim the rport scan work element, so eventually
2971 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2972 * there's still a scan pending.
2975 spin_lock_irqsave(shost->host_lock, flags);
2977 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2978 spin_unlock_irqrestore(shost->host_lock, flags);
2983 * In the past, we if this was not an FCP-Target, we would
2984 * unconditionally just jump to deleting the rport.
2985 * However, rports can be used as node containers by the LLDD,
2986 * and its not appropriate to just terminate the rport at the
2987 * first sign of a loss in connectivity. The LLDD may want to
2988 * send ELS traffic to re-validate the login. If the rport is
2989 * immediately deleted, it makes it inappropriate for a node
2991 * So... we now unconditionally wait dev_loss_tmo before
2992 * destroying an rport.
2995 rport->port_state = FC_PORTSTATE_BLOCKED;
2997 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2999 spin_unlock_irqrestore(shost->host_lock, flags);
3001 scsi_target_block(&rport->dev);
3003 /* see if we need to kill io faster than waiting for device loss */
3004 if ((rport->fast_io_fail_tmo != -1) &&
3005 (rport->fast_io_fail_tmo < timeout))
3006 fc_queue_devloss_work(shost, &rport->fail_io_work,
3007 rport->fast_io_fail_tmo * HZ);
3009 /* cap the length the devices can be blocked until they are deleted */
3010 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
3012 EXPORT_SYMBOL(fc_remote_port_delete);
3015 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
3016 * @rport: The remote port that changed.
3017 * @roles: New roles for this port.
3019 * Description: The LLDD calls this routine to notify the transport that the
3020 * roles on a remote port may have changed. The largest effect of this is
3021 * if a port now becomes a FCP Target, it must be allocated a
3022 * scsi target id. If the port is no longer a FCP target, any
3023 * scsi target id value assigned to it will persist in case the
3024 * role changes back to include FCP Target. No changes in the scsi
3025 * midlayer will be invoked if the role changes (in the expectation
3026 * that the role will be resumed. If it doesn't normal error processing
3029 * Should not be called from interrupt context.
3032 * This routine assumes no locks are held on entry.
3035 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3037 struct Scsi_Host *shost = rport_to_shost(rport);
3038 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3039 unsigned long flags;
3042 spin_lock_irqsave(shost->host_lock, flags);
3043 if (roles & FC_PORT_ROLE_FCP_TARGET) {
3044 if (rport->scsi_target_id == -1) {
3045 rport->scsi_target_id = fc_host->next_target_id++;
3047 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3051 rport->roles = roles;
3053 spin_unlock_irqrestore(shost->host_lock, flags);
3057 * There may have been a delete timer running on the
3058 * port. Ensure that it is cancelled as we now know
3059 * the port is an FCP Target.
3060 * Note: we know the rport exists and is in an online
3061 * state as the LLDD would not have had an rport
3062 * reference to pass us.
3064 * Take no action on the del_timer failure as the state
3065 * machine state change will validate the
3068 if (!cancel_delayed_work(&rport->fail_io_work))
3069 fc_flush_devloss(shost);
3070 if (!cancel_delayed_work(&rport->dev_loss_work))
3071 fc_flush_devloss(shost);
3073 spin_lock_irqsave(shost->host_lock, flags);
3074 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3075 FC_RPORT_DEVLOSS_PENDING |
3076 FC_RPORT_DEVLOSS_CALLBK_DONE);
3077 spin_unlock_irqrestore(shost->host_lock, flags);
3079 /* ensure any stgt delete functions are done */
3080 fc_flush_work(shost);
3082 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
3083 /* initiate a scan of the target */
3084 spin_lock_irqsave(shost->host_lock, flags);
3085 rport->flags |= FC_RPORT_SCAN_PENDING;
3086 scsi_queue_work(shost, &rport->scan_work);
3087 spin_unlock_irqrestore(shost->host_lock, flags);
3090 EXPORT_SYMBOL(fc_remote_port_rolechg);
3093 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3094 * @work: rport target that failed to reappear in the allotted time.
3096 * Description: An attempt to delete a remote port blocks, and if it fails
3097 * to return in the allotted time this gets called.
3100 fc_timeout_deleted_rport(struct work_struct *work)
3102 struct fc_rport *rport =
3103 container_of(work, struct fc_rport, dev_loss_work.work);
3104 struct Scsi_Host *shost = rport_to_shost(rport);
3105 struct fc_internal *i = to_fc_internal(shost->transportt);
3106 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3107 unsigned long flags;
3108 int do_callback = 0;
3110 spin_lock_irqsave(shost->host_lock, flags);
3112 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3115 * If the port is ONLINE, then it came back. If it was a SCSI
3116 * target, validate it still is. If not, tear down the
3117 * scsi_target on it.
3119 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3120 (rport->scsi_target_id != -1) &&
3121 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3122 dev_printk(KERN_ERR, &rport->dev,
3123 "blocked FC remote port time out: no longer"
3124 " a FCP target, removing starget\n");
3125 spin_unlock_irqrestore(shost->host_lock, flags);
3126 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3127 fc_queue_work(shost, &rport->stgt_delete_work);
3131 /* NOOP state - we're flushing workq's */
3132 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3133 spin_unlock_irqrestore(shost->host_lock, flags);
3134 dev_printk(KERN_ERR, &rport->dev,
3135 "blocked FC remote port time out: leaving"
3137 (rport->scsi_target_id != -1) ? " and starget" : "");
3141 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3142 (rport->scsi_target_id == -1)) {
3143 list_del(&rport->peers);
3144 rport->port_state = FC_PORTSTATE_DELETED;
3145 dev_printk(KERN_ERR, &rport->dev,
3146 "blocked FC remote port time out: removing"
3148 (rport->scsi_target_id != -1) ? " and starget" : "");
3149 fc_queue_work(shost, &rport->rport_delete_work);
3150 spin_unlock_irqrestore(shost->host_lock, flags);
3154 dev_printk(KERN_ERR, &rport->dev,
3155 "blocked FC remote port time out: removing target and "
3156 "saving binding\n");
3158 list_move_tail(&rport->peers, &fc_host->rport_bindings);
3161 * Note: We do not remove or clear the hostdata area. This allows
3162 * host-specific target data to persist along with the
3163 * scsi_target_id. It's up to the host to manage it's hostdata area.
3167 * Reinitialize port attributes that may change if the port comes back.
3169 rport->maxframe_size = -1;
3170 rport->supported_classes = FC_COS_UNSPECIFIED;
3171 rport->roles = FC_PORT_ROLE_UNKNOWN;
3172 rport->port_state = FC_PORTSTATE_NOTPRESENT;
3173 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3176 * Pre-emptively kill I/O rather than waiting for the work queue
3177 * item to teardown the starget. (FCOE libFC folks prefer this
3178 * and to have the rport_port_id still set when it's done).
3180 spin_unlock_irqrestore(shost->host_lock, flags);
3181 fc_terminate_rport_io(rport);
3183 spin_lock_irqsave(shost->host_lock, flags);
3185 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3187 /* remove the identifiers that aren't used in the consisting binding */
3188 switch (fc_host->tgtid_bind_type) {
3189 case FC_TGTID_BIND_BY_WWPN:
3190 rport->node_name = -1;
3191 rport->port_id = -1;
3193 case FC_TGTID_BIND_BY_WWNN:
3194 rport->port_name = -1;
3195 rport->port_id = -1;
3197 case FC_TGTID_BIND_BY_ID:
3198 rport->node_name = -1;
3199 rport->port_name = -1;
3201 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3206 * As this only occurs if the remote port (scsi target)
3207 * went away and didn't come back - we'll remove
3208 * all attached scsi devices.
3210 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3211 fc_queue_work(shost, &rport->stgt_delete_work);
3216 spin_unlock_irqrestore(shost->host_lock, flags);
3219 * Notify the driver that the rport is now dead. The LLDD will
3220 * also guarantee that any communication to the rport is terminated
3222 * Note: we set the CALLBK_DONE flag above to correspond
3224 if (do_callback && i->f->dev_loss_tmo_callbk)
3225 i->f->dev_loss_tmo_callbk(rport);
3230 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3231 * @work: rport to terminate io on.
3233 * Notes: Only requests the failure of the io, not that all are flushed
3234 * prior to returning.
3237 fc_timeout_fail_rport_io(struct work_struct *work)
3239 struct fc_rport *rport =
3240 container_of(work, struct fc_rport, fail_io_work.work);
3242 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3245 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3246 fc_terminate_rport_io(rport);
3250 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3251 * @work: remote port to be scanned.
3254 fc_scsi_scan_rport(struct work_struct *work)
3256 struct fc_rport *rport =
3257 container_of(work, struct fc_rport, scan_work);
3258 struct Scsi_Host *shost = rport_to_shost(rport);
3259 struct fc_internal *i = to_fc_internal(shost->transportt);
3260 unsigned long flags;
3262 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3263 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3264 !(i->f->disable_target_scan)) {
3265 scsi_scan_target(&rport->dev, rport->channel,
3266 rport->scsi_target_id, SCAN_WILD_CARD,
3270 spin_lock_irqsave(shost->host_lock, flags);
3271 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3272 spin_unlock_irqrestore(shost->host_lock, flags);
3276 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3277 * @rport: Remote port that scsi_eh is trying to recover.
3279 * This routine can be called from a FC LLD scsi_eh callback. It
3280 * blocks the scsi_eh thread until the fc_rport leaves the
3281 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3282 * necessary to avoid the scsi_eh failing recovery actions for blocked
3283 * rports which would lead to offlined SCSI devices.
3285 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3286 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3287 * passed back to scsi_eh.
3289 int fc_block_rport(struct fc_rport *rport)
3291 struct Scsi_Host *shost = rport_to_shost(rport);
3292 unsigned long flags;
3294 spin_lock_irqsave(shost->host_lock, flags);
3295 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3296 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3297 spin_unlock_irqrestore(shost->host_lock, flags);
3299 spin_lock_irqsave(shost->host_lock, flags);
3301 spin_unlock_irqrestore(shost->host_lock, flags);
3303 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3304 return FAST_IO_FAIL;
3308 EXPORT_SYMBOL(fc_block_rport);
3311 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3312 * @cmnd: SCSI command that scsi_eh is trying to recover
3314 * This routine can be called from a FC LLD scsi_eh callback. It
3315 * blocks the scsi_eh thread until the fc_rport leaves the
3316 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3317 * necessary to avoid the scsi_eh failing recovery actions for blocked
3318 * rports which would lead to offlined SCSI devices.
3320 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3321 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3322 * passed back to scsi_eh.
3324 int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3326 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3328 if (WARN_ON_ONCE(!rport))
3329 return FAST_IO_FAIL;
3331 return fc_block_rport(rport);
3333 EXPORT_SYMBOL(fc_block_scsi_eh);
3336 * fc_vport_setup - allocates and creates a FC virtual port.
3337 * @shost: scsi host the virtual port is connected to.
3338 * @channel: Channel on shost port connected to.
3339 * @pdev: parent device for vport
3340 * @ids: The world wide names, FC4 port roles, etc for
3342 * @ret_vport: The pointer to the created vport.
3344 * Allocates and creates the vport structure, calls the parent host
3345 * to instantiate the vport, this completes w/ class and sysfs creation.
3348 * This routine assumes no locks are held on entry.
3351 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3352 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3354 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3355 struct fc_internal *fci = to_fc_internal(shost->transportt);
3356 struct fc_vport *vport;
3358 unsigned long flags;
3364 if ( ! fci->f->vport_create)
3367 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3368 vport = kzalloc(size, GFP_KERNEL);
3369 if (unlikely(!vport)) {
3370 printk(KERN_ERR "%s: allocation failure\n", __func__);
3374 vport->vport_state = FC_VPORT_UNKNOWN;
3375 vport->vport_last_state = FC_VPORT_UNKNOWN;
3376 vport->node_name = ids->node_name;
3377 vport->port_name = ids->port_name;
3378 vport->roles = ids->roles;
3379 vport->vport_type = ids->vport_type;
3380 if (fci->f->dd_fcvport_size)
3381 vport->dd_data = &vport[1];
3382 vport->shost = shost;
3383 vport->channel = channel;
3384 vport->flags = FC_VPORT_CREATING;
3385 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3387 spin_lock_irqsave(shost->host_lock, flags);
3389 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3390 spin_unlock_irqrestore(shost->host_lock, flags);
3394 fc_host->npiv_vports_inuse++;
3395 vport->number = fc_host->next_vport_number++;
3396 list_add_tail(&vport->peers, &fc_host->vports);
3397 scsi_host_get(shost); /* for fc_host->vport list */
3399 spin_unlock_irqrestore(shost->host_lock, flags);
3402 device_initialize(dev); /* takes self reference */
3403 dev->parent = get_device(pdev); /* takes parent reference */
3404 dev->release = fc_vport_dev_release;
3405 dev_set_name(dev, "vport-%d:%d-%d",
3406 shost->host_no, channel, vport->number);
3407 transport_setup_device(dev);
3409 error = device_add(dev);
3411 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3414 transport_add_device(dev);
3415 transport_configure_device(dev);
3417 error = fci->f->vport_create(vport, ids->disable);
3419 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3420 goto delete_vport_all;
3424 * if the parent isn't the physical adapter's Scsi_Host, ensure
3425 * the Scsi_Host at least contains a symlink to the vport.
3427 if (pdev != &shost->shost_gendev) {
3428 error = sysfs_create_link(&shost->shost_gendev.kobj,
3429 &dev->kobj, dev_name(dev));
3432 "%s: Cannot create vport symlinks for "
3434 __func__, dev_name(dev), error);
3436 spin_lock_irqsave(shost->host_lock, flags);
3437 vport->flags &= ~FC_VPORT_CREATING;
3438 spin_unlock_irqrestore(shost->host_lock, flags);
3440 dev_printk(KERN_NOTICE, pdev,
3441 "%s created via shost%d channel %d\n", dev_name(dev),
3442 shost->host_no, channel);
3449 transport_remove_device(dev);
3452 transport_destroy_device(dev);
3453 spin_lock_irqsave(shost->host_lock, flags);
3454 list_del(&vport->peers);
3455 scsi_host_put(shost); /* for fc_host->vport list */
3456 fc_host->npiv_vports_inuse--;
3457 spin_unlock_irqrestore(shost->host_lock, flags);
3458 put_device(dev->parent);
3465 * fc_vport_create - Admin App or LLDD requests creation of a vport
3466 * @shost: scsi host the virtual port is connected to.
3467 * @channel: channel on shost port connected to.
3468 * @ids: The world wide names, FC4 port roles, etc for
3472 * This routine assumes no locks are held on entry.
3475 fc_vport_create(struct Scsi_Host *shost, int channel,
3476 struct fc_vport_identifiers *ids)
3479 struct fc_vport *vport;
3481 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3483 return stat ? NULL : vport;
3485 EXPORT_SYMBOL(fc_vport_create);
3488 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3489 * @vport: fc_vport to be terminated
3491 * Calls the LLDD vport_delete() function, then deallocates and removes
3492 * the vport from the shost and object tree.
3495 * This routine assumes no locks are held on entry.
3498 fc_vport_terminate(struct fc_vport *vport)
3500 struct Scsi_Host *shost = vport_to_shost(vport);
3501 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3502 struct fc_internal *i = to_fc_internal(shost->transportt);
3503 struct device *dev = &vport->dev;
3504 unsigned long flags;
3507 if (i->f->vport_delete)
3508 stat = i->f->vport_delete(vport);
3512 spin_lock_irqsave(shost->host_lock, flags);
3513 vport->flags &= ~FC_VPORT_DELETING;
3515 vport->flags |= FC_VPORT_DELETED;
3516 list_del(&vport->peers);
3517 fc_host->npiv_vports_inuse--;
3518 scsi_host_put(shost); /* for fc_host->vport list */
3520 spin_unlock_irqrestore(shost->host_lock, flags);
3525 if (dev->parent != &shost->shost_gendev)
3526 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3527 transport_remove_device(dev);
3529 transport_destroy_device(dev);
3532 * Removing our self-reference should mean our
3533 * release function gets called, which will drop the remaining
3534 * parent reference and free the data structure.
3536 put_device(dev); /* for self-reference */
3538 return 0; /* SUCCESS */
3540 EXPORT_SYMBOL(fc_vport_terminate);
3543 * fc_vport_sched_delete - workq-based delete request for a vport
3544 * @work: vport to be deleted.
3547 fc_vport_sched_delete(struct work_struct *work)
3549 struct fc_vport *vport =
3550 container_of(work, struct fc_vport, vport_delete_work);
3553 stat = fc_vport_terminate(vport);
3555 dev_printk(KERN_ERR, vport->dev.parent,
3556 "%s: %s could not be deleted created via "
3557 "shost%d channel %d - error %d\n", __func__,
3558 dev_name(&vport->dev), vport->shost->host_no,
3559 vport->channel, stat);
3568 * fc_bsg_job_timeout - handler for when a bsg request timesout
3569 * @req: request that timed out
3571 static enum blk_eh_timer_return
3572 fc_bsg_job_timeout(struct request *req)
3574 struct bsg_job *job = blk_mq_rq_to_pdu(req);
3575 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3576 struct fc_rport *rport = fc_bsg_to_rport(job);
3577 struct fc_internal *i = to_fc_internal(shost->transportt);
3578 int err = 0, inflight = 0;
3580 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
3581 return BLK_EH_RESET_TIMER;
3583 inflight = bsg_job_get(job);
3585 if (inflight && i->f->bsg_timeout) {
3586 /* call LLDD to abort the i/o as it has timed out */
3587 err = i->f->bsg_timeout(job);
3588 if (err == -EAGAIN) {
3590 return BLK_EH_RESET_TIMER;
3592 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3593 "abort failed with status %d\n", err);
3596 /* the blk_end_sync_io() doesn't check the error */
3598 blk_mq_end_request(req, BLK_STS_IOERR);
3603 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3604 * @shost: scsi host rport attached to
3605 * @job: bsg job to be processed
3607 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3609 struct fc_internal *i = to_fc_internal(shost->transportt);
3610 struct fc_bsg_request *bsg_request = job->request;
3611 struct fc_bsg_reply *bsg_reply = job->reply;
3612 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3615 /* check if we really have all the request data needed */
3616 if (job->request_len < cmdlen) {
3621 /* Validate the host command */
3622 switch (bsg_request->msgcode) {
3623 case FC_BSG_HST_ADD_RPORT:
3624 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3627 case FC_BSG_HST_DEL_RPORT:
3628 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3631 case FC_BSG_HST_ELS_NOLOGIN:
3632 cmdlen += sizeof(struct fc_bsg_host_els);
3633 /* there better be a xmt and rcv payloads */
3634 if ((!job->request_payload.payload_len) ||
3635 (!job->reply_payload.payload_len)) {
3642 cmdlen += sizeof(struct fc_bsg_host_ct);
3643 /* there better be xmt and rcv payloads */
3644 if ((!job->request_payload.payload_len) ||
3645 (!job->reply_payload.payload_len)) {
3651 case FC_BSG_HST_VENDOR:
3652 cmdlen += sizeof(struct fc_bsg_host_vendor);
3653 if ((shost->hostt->vendor_id == 0L) ||
3654 (bsg_request->rqst_data.h_vendor.vendor_id !=
3655 shost->hostt->vendor_id)) {
3666 ret = i->f->bsg_request(job);
3671 /* return the errno failure code as the only status */
3672 BUG_ON(job->reply_len < sizeof(uint32_t));
3673 bsg_reply->reply_payload_rcv_len = 0;
3674 bsg_reply->result = ret;
3675 job->reply_len = sizeof(uint32_t);
3676 bsg_job_done(job, bsg_reply->result,
3677 bsg_reply->reply_payload_rcv_len);
3683 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3684 * @rport: rport to be restarted
3687 fc_bsg_goose_queue(struct fc_rport *rport)
3689 struct request_queue *q = rport->rqst_q;
3692 blk_mq_run_hw_queues(q, true);
3696 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3697 * @shost: scsi host rport attached to
3698 * @job: bsg job to be processed
3700 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
3702 struct fc_internal *i = to_fc_internal(shost->transportt);
3703 struct fc_bsg_request *bsg_request = job->request;
3704 struct fc_bsg_reply *bsg_reply = job->reply;
3705 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3708 /* check if we really have all the request data needed */
3709 if (job->request_len < cmdlen) {
3711 goto fail_rport_msg;
3714 /* Validate the rport command */
3715 switch (bsg_request->msgcode) {
3716 case FC_BSG_RPT_ELS:
3717 cmdlen += sizeof(struct fc_bsg_rport_els);
3721 cmdlen += sizeof(struct fc_bsg_rport_ct);
3723 /* there better be xmt and rcv payloads */
3724 if ((!job->request_payload.payload_len) ||
3725 (!job->reply_payload.payload_len)) {
3727 goto fail_rport_msg;
3732 goto fail_rport_msg;
3735 ret = i->f->bsg_request(job);
3740 /* return the errno failure code as the only status */
3741 BUG_ON(job->reply_len < sizeof(uint32_t));
3742 bsg_reply->reply_payload_rcv_len = 0;
3743 bsg_reply->result = ret;
3744 job->reply_len = sizeof(uint32_t);
3745 bsg_job_done(job, bsg_reply->result,
3746 bsg_reply->reply_payload_rcv_len);
3750 static int fc_bsg_dispatch(struct bsg_job *job)
3752 struct Scsi_Host *shost = fc_bsg_to_shost(job);
3754 if (scsi_is_fc_rport(job->dev))
3755 return fc_bsg_rport_dispatch(shost, job);
3757 return fc_bsg_host_dispatch(shost, job);
3760 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
3762 if (rport->port_state == FC_PORTSTATE_BLOCKED &&
3763 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3764 return BLK_STS_RESOURCE;
3766 if (rport->port_state != FC_PORTSTATE_ONLINE)
3767 return BLK_STS_IOERR;
3773 static int fc_bsg_dispatch_prep(struct bsg_job *job)
3775 struct fc_rport *rport = fc_bsg_to_rport(job);
3778 ret = fc_bsg_rport_prep(rport);
3782 case BLK_STS_RESOURCE:
3788 return fc_bsg_dispatch(job);
3792 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3793 * @shost: shost for fc_host
3794 * @fc_host: fc_host adding the structures to
3797 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3799 struct device *dev = &shost->shost_gendev;
3800 struct fc_internal *i = to_fc_internal(shost->transportt);
3801 struct request_queue *q;
3804 fc_host->rqst_q = NULL;
3806 if (!i->f->bsg_request)
3809 snprintf(bsg_name, sizeof(bsg_name),
3810 "fc_host%d", shost->host_no);
3812 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout,
3816 "fc_host%d: bsg interface failed to initialize - setup queue\n",
3820 __scsi_init_queue(shost, q);
3821 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3822 fc_host->rqst_q = q;
3827 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3828 * @shost: shost that rport is attached to
3829 * @rport: rport that the bsg hooks are being attached to
3832 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3834 struct device *dev = &rport->dev;
3835 struct fc_internal *i = to_fc_internal(shost->transportt);
3836 struct request_queue *q;
3838 rport->rqst_q = NULL;
3840 if (!i->f->bsg_request)
3843 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep,
3844 fc_bsg_job_timeout, i->f->dd_bsg_size);
3846 dev_err(dev, "failed to setup bsg queue\n");
3849 __scsi_init_queue(shost, q);
3850 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3857 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3858 * @q: the request_queue that is to be torn down.
3861 * Before unregistering the queue empty any requests that are blocked
3866 fc_bsg_remove(struct request_queue *q)
3868 bsg_remove_queue(q);
3872 /* Original Author: Martin Hicks */
3873 MODULE_AUTHOR("James Smart");
3874 MODULE_DESCRIPTION("FC Transport Attributes");
3875 MODULE_LICENSE("GPL");
3877 module_init(fc_transport_init);
3878 module_exit(fc_transport_exit);