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...
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <linux/netlink.h>
35 #include <net/netlink.h>
36 #include <scsi/scsi_netlink_fc.h>
37 #include "scsi_priv.h"
39 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
42 * This is a temporary carrier for creating a vport. It will eventually
43 * be replaced by a real message definition for sgio or netlink.
45 * fc_vport_identifiers: This set of data contains all elements
46 * to uniquely identify and instantiate a FC virtual port.
49 * symbolic_name: The driver is to append the symbolic_name string data
50 * to the symbolic_node_name data that it generates by default.
51 * the resulting combination should then be registered with the switch.
52 * It is expected that things like Xen may stuff a VM title into
55 struct fc_vport_identifiers {
60 enum fc_port_type vport_type; /* only FC_PORTTYPE_NPIV allowed */
61 char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
64 static int fc_vport_create(struct Scsi_Host *shost, int channel,
65 struct device *pdev, struct fc_vport_identifiers *ids,
66 struct fc_vport **vport);
69 * Redefine so that we can have same named attributes in the
70 * sdev/starget/host objects.
72 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
73 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
74 __ATTR(_name,_mode,_show,_store)
76 #define fc_enum_name_search(title, table_type, table) \
77 static const char *get_fc_##title##_name(enum table_type table_key) \
82 for (i = 0; i < ARRAY_SIZE(table); i++) { \
83 if (table[i].value == table_key) { \
84 name = table[i].name; \
91 #define fc_enum_name_match(title, table_type, table) \
92 static int get_fc_##title##_match(const char *table_key, \
93 enum table_type *value) \
97 for (i = 0; i < ARRAY_SIZE(table); i++) { \
98 if (strncmp(table_key, table[i].name, \
99 table[i].matchlen) == 0) { \
100 *value = table[i].value; \
101 return 0; /* success */ \
104 return 1; /* failure */ \
108 /* Convert fc_port_type values to ascii string name */
110 enum fc_port_type value;
112 } fc_port_type_names[] = {
113 { FC_PORTTYPE_UNKNOWN, "Unknown" },
114 { FC_PORTTYPE_OTHER, "Other" },
115 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
116 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
117 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
118 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
119 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
120 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
122 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
123 #define FC_PORTTYPE_MAX_NAMELEN 50
125 /* Reuse fc_port_type enum function for vport_type */
126 #define get_fc_vport_type_name get_fc_port_type_name
129 /* Convert fc_host_event_code values to ascii string name */
130 static const struct {
131 enum fc_host_event_code value;
133 } fc_host_event_code_names[] = {
134 { FCH_EVT_LIP, "lip" },
135 { FCH_EVT_LINKUP, "link_up" },
136 { FCH_EVT_LINKDOWN, "link_down" },
137 { FCH_EVT_LIPRESET, "lip_reset" },
138 { FCH_EVT_RSCN, "rscn" },
139 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
140 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
141 { FCH_EVT_PORT_ONLINE, "port_online" },
142 { FCH_EVT_PORT_OFFLINE, "port_offline" },
143 { FCH_EVT_PORT_FABRIC, "port_fabric" },
144 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
145 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
147 fc_enum_name_search(host_event_code, fc_host_event_code,
148 fc_host_event_code_names)
149 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
152 /* Convert fc_port_state values to ascii string name */
154 enum fc_port_state value;
156 } fc_port_state_names[] = {
157 { FC_PORTSTATE_UNKNOWN, "Unknown" },
158 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
159 { FC_PORTSTATE_ONLINE, "Online" },
160 { FC_PORTSTATE_OFFLINE, "Offline" },
161 { FC_PORTSTATE_BLOCKED, "Blocked" },
162 { FC_PORTSTATE_BYPASSED, "Bypassed" },
163 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
164 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
165 { FC_PORTSTATE_ERROR, "Error" },
166 { FC_PORTSTATE_LOOPBACK, "Loopback" },
167 { FC_PORTSTATE_DELETED, "Deleted" },
169 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
170 #define FC_PORTSTATE_MAX_NAMELEN 20
173 /* Convert fc_vport_state values to ascii string name */
175 enum fc_vport_state value;
177 } fc_vport_state_names[] = {
178 { FC_VPORT_UNKNOWN, "Unknown" },
179 { FC_VPORT_ACTIVE, "Active" },
180 { FC_VPORT_DISABLED, "Disabled" },
181 { FC_VPORT_LINKDOWN, "Linkdown" },
182 { FC_VPORT_INITIALIZING, "Initializing" },
183 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
184 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
185 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
186 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
187 { FC_VPORT_FAILED, "VPort Failed" },
189 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
190 #define FC_VPORTSTATE_MAX_NAMELEN 24
192 /* Reuse fc_vport_state enum function for vport_last_state */
193 #define get_fc_vport_last_state_name get_fc_vport_state_name
196 /* Convert fc_tgtid_binding_type values to ascii string name */
197 static const struct {
198 enum fc_tgtid_binding_type value;
201 } fc_tgtid_binding_type_names[] = {
202 { FC_TGTID_BIND_NONE, "none", 4 },
203 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
204 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
205 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
207 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
208 fc_tgtid_binding_type_names)
209 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
210 fc_tgtid_binding_type_names)
211 #define FC_BINDTYPE_MAX_NAMELEN 30
214 #define fc_bitfield_name_search(title, table) \
216 get_fc_##title##_names(u32 table_key, char *buf) \
222 for (i = 0; i < ARRAY_SIZE(table); i++) { \
223 if (table[i].value & table_key) { \
224 len += sprintf(buf + len, "%s%s", \
225 prefix, table[i].name); \
229 len += sprintf(buf + len, "\n"); \
234 /* Convert FC_COS bit values to ascii string name */
235 static const struct {
239 { FC_COS_CLASS1, "Class 1" },
240 { FC_COS_CLASS2, "Class 2" },
241 { FC_COS_CLASS3, "Class 3" },
242 { FC_COS_CLASS4, "Class 4" },
243 { FC_COS_CLASS6, "Class 6" },
245 fc_bitfield_name_search(cos, fc_cos_names)
248 /* Convert FC_PORTSPEED bit values to ascii string name */
249 static const struct {
252 } fc_port_speed_names[] = {
253 { FC_PORTSPEED_1GBIT, "1 Gbit" },
254 { FC_PORTSPEED_2GBIT, "2 Gbit" },
255 { FC_PORTSPEED_4GBIT, "4 Gbit" },
256 { FC_PORTSPEED_10GBIT, "10 Gbit" },
257 { FC_PORTSPEED_8GBIT, "8 Gbit" },
258 { FC_PORTSPEED_16GBIT, "16 Gbit" },
259 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
261 fc_bitfield_name_search(port_speed, fc_port_speed_names)
265 show_fc_fc4s (char *buf, u8 *fc4_list)
269 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
270 len += sprintf(buf + len , "0x%02x ", *fc4_list);
271 len += sprintf(buf + len, "\n");
276 /* Convert FC_PORT_ROLE bit values to ascii string name */
277 static const struct {
280 } fc_port_role_names[] = {
281 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
282 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
283 { FC_PORT_ROLE_IP_PORT, "IP Port" },
285 fc_bitfield_name_search(port_roles, fc_port_role_names)
288 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
290 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
291 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
292 #define FC_FPORT_PORTID 0x00000e
293 #define FC_FABCTLR_PORTID 0x00000d
294 #define FC_DIRSRVR_PORTID 0x00000c
295 #define FC_TIMESRVR_PORTID 0x00000b
296 #define FC_MGMTSRVR_PORTID 0x00000a
299 static void fc_timeout_deleted_rport(struct work_struct *work);
300 static void fc_timeout_fail_rport_io(struct work_struct *work);
301 static void fc_scsi_scan_rport(struct work_struct *work);
304 * Attribute counts pre object type...
305 * Increase these values if you add attributes
307 #define FC_STARGET_NUM_ATTRS 3
308 #define FC_RPORT_NUM_ATTRS 10
309 #define FC_VPORT_NUM_ATTRS 9
310 #define FC_HOST_NUM_ATTRS 21
313 struct scsi_transport_template t;
314 struct fc_function_template *f;
317 * For attributes : each object has :
318 * An array of the actual attributes structures
319 * An array of null-terminated pointers to the attribute
320 * structures - used for mid-layer interaction.
322 * The attribute containers for the starget and host are are
323 * part of the midlayer. As the remote port is specific to the
324 * fc transport, we must provide the attribute container.
326 struct class_device_attribute private_starget_attrs[
327 FC_STARGET_NUM_ATTRS];
328 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
330 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
331 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
333 struct transport_container rport_attr_cont;
334 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
335 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
337 struct transport_container vport_attr_cont;
338 struct class_device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
339 struct class_device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
342 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
344 static int fc_target_setup(struct transport_container *tc, struct device *dev,
345 struct class_device *cdev)
347 struct scsi_target *starget = to_scsi_target(dev);
348 struct fc_rport *rport = starget_to_rport(starget);
351 * if parent is remote port, use values from remote port.
352 * Otherwise, this host uses the fc_transport, but not the
353 * remote port interface. As such, initialize to known non-values.
356 fc_starget_node_name(starget) = rport->node_name;
357 fc_starget_port_name(starget) = rport->port_name;
358 fc_starget_port_id(starget) = rport->port_id;
360 fc_starget_node_name(starget) = -1;
361 fc_starget_port_name(starget) = -1;
362 fc_starget_port_id(starget) = -1;
368 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
374 static int fc_host_setup(struct transport_container *tc, struct device *dev,
375 struct class_device *cdev)
377 struct Scsi_Host *shost = dev_to_shost(dev);
378 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
381 * Set default values easily detected by the midlayer as
382 * failure cases. The scsi lldd is responsible for initializing
383 * all transport attributes to valid values per host.
385 fc_host->node_name = -1;
386 fc_host->port_name = -1;
387 fc_host->permanent_port_name = -1;
388 fc_host->supported_classes = FC_COS_UNSPECIFIED;
389 memset(fc_host->supported_fc4s, 0,
390 sizeof(fc_host->supported_fc4s));
391 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
392 fc_host->maxframe_size = -1;
393 fc_host->max_npiv_vports = 0;
394 memset(fc_host->serial_number, 0,
395 sizeof(fc_host->serial_number));
397 fc_host->port_id = -1;
398 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
399 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
400 memset(fc_host->active_fc4s, 0,
401 sizeof(fc_host->active_fc4s));
402 fc_host->speed = FC_PORTSPEED_UNKNOWN;
403 fc_host->fabric_name = -1;
404 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
405 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
407 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
409 INIT_LIST_HEAD(&fc_host->rports);
410 INIT_LIST_HEAD(&fc_host->rport_bindings);
411 INIT_LIST_HEAD(&fc_host->vports);
412 fc_host->next_rport_number = 0;
413 fc_host->next_target_id = 0;
414 fc_host->next_vport_number = 0;
415 fc_host->npiv_vports_inuse = 0;
417 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
419 fc_host->work_q = create_singlethread_workqueue(
420 fc_host->work_q_name);
421 if (!fc_host->work_q)
424 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
426 fc_host->devloss_work_q = create_singlethread_workqueue(
427 fc_host->devloss_work_q_name);
428 if (!fc_host->devloss_work_q) {
429 destroy_workqueue(fc_host->work_q);
430 fc_host->work_q = NULL;
437 static DECLARE_TRANSPORT_CLASS(fc_host_class,
444 * Setup and Remove actions for remote ports are handled
445 * in the service functions below.
447 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
454 * Setup and Remove actions for virtual ports are handled
455 * in the service functions below.
457 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
468 * dev_loss_tmo: the default number of seconds that the FC transport
469 * should insulate the loss of a remote port.
470 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
472 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
474 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
475 MODULE_PARM_DESC(dev_loss_tmo,
476 "Maximum number of seconds that the FC transport should"
477 " insulate the loss of a remote port. Once this value is"
478 " exceeded, the scsi target is removed. Value should be"
479 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
482 * Netlink Infrastructure
485 static atomic_t fc_event_seq;
488 * fc_get_event_number - Obtain the next sequential FC event number
491 * We could have inline'd this, but it would have required fc_event_seq to
492 * be exposed. For now, live with the subroutine call.
493 * Atomic used to avoid lock/unlock...
496 fc_get_event_number(void)
498 return atomic_add_return(1, &fc_event_seq);
500 EXPORT_SYMBOL(fc_get_event_number);
504 * fc_host_post_event - called to post an even on an fc_host.
506 * @shost: host the event occurred on
507 * @event_number: fc event number obtained from get_fc_event_number()
508 * @event_code: fc_host event being posted
509 * @event_data: 32bits of data for the event being posted
512 * This routine assumes no locks are held on entry.
515 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
516 enum fc_host_event_code event_code, u32 event_data)
519 struct nlmsghdr *nlh;
520 struct fc_nl_event *event;
530 len = FC_NL_MSGALIGN(sizeof(*event));
531 skblen = NLMSG_SPACE(len);
533 skb = alloc_skb(skblen, GFP_KERNEL);
539 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
540 skblen - sizeof(*nlh), 0);
545 event = NLMSG_DATA(nlh);
547 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
548 FC_NL_ASYNC_EVENT, len);
549 event->seconds = get_seconds();
550 event->vendor_id = 0;
551 event->host_no = shost->host_no;
552 event->event_datalen = sizeof(u32); /* bytes */
553 event->event_num = event_number;
554 event->event_code = event_code;
555 event->event_data = event_data;
557 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
559 if (err && (err != -ESRCH)) /* filter no recipient errors */
560 /* nlmsg_multicast already kfree_skb'd */
568 name = get_fc_host_event_code_name(event_code);
570 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
571 __FUNCTION__, shost->host_no,
572 (name) ? name : "<unknown>", event_data, err);
575 EXPORT_SYMBOL(fc_host_post_event);
579 * fc_host_post_vendor_event - called to post a vendor unique event on
582 * @shost: host the event occurred on
583 * @event_number: fc event number obtained from get_fc_event_number()
584 * @data_len: amount, in bytes, of vendor unique data
585 * @data_buf: pointer to vendor unique data
588 * This routine assumes no locks are held on entry.
591 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
592 u32 data_len, char * data_buf, u64 vendor_id)
595 struct nlmsghdr *nlh;
596 struct fc_nl_event *event;
602 goto send_vendor_fail;
605 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
606 skblen = NLMSG_SPACE(len);
608 skb = alloc_skb(skblen, GFP_KERNEL);
611 goto send_vendor_fail;
614 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
615 skblen - sizeof(*nlh), 0);
618 goto send_vendor_fail_skb;
620 event = NLMSG_DATA(nlh);
622 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
623 FC_NL_ASYNC_EVENT, len);
624 event->seconds = get_seconds();
625 event->vendor_id = vendor_id;
626 event->host_no = shost->host_no;
627 event->event_datalen = data_len; /* bytes */
628 event->event_num = event_number;
629 event->event_code = FCH_EVT_VENDOR_UNIQUE;
630 memcpy(&event->event_data, data_buf, data_len);
632 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
634 if (err && (err != -ESRCH)) /* filter no recipient errors */
635 /* nlmsg_multicast already kfree_skb'd */
636 goto send_vendor_fail;
640 send_vendor_fail_skb:
644 "%s: Dropped Event : host %d vendor_unique - err %d\n",
645 __FUNCTION__, shost->host_no, err);
648 EXPORT_SYMBOL(fc_host_post_vendor_event);
652 static __init int fc_transport_init(void)
656 atomic_set(&fc_event_seq, 0);
658 error = transport_class_register(&fc_host_class);
661 error = transport_class_register(&fc_vport_class);
664 error = transport_class_register(&fc_rport_class);
667 return transport_class_register(&fc_transport_class);
670 static void __exit fc_transport_exit(void)
672 transport_class_unregister(&fc_transport_class);
673 transport_class_unregister(&fc_rport_class);
674 transport_class_unregister(&fc_host_class);
675 transport_class_unregister(&fc_vport_class);
679 * FC Remote Port Attribute Management
682 #define fc_rport_show_function(field, format_string, sz, cast) \
684 show_fc_rport_##field (struct class_device *cdev, char *buf) \
686 struct fc_rport *rport = transport_class_to_rport(cdev); \
687 struct Scsi_Host *shost = rport_to_shost(rport); \
688 struct fc_internal *i = to_fc_internal(shost->transportt); \
689 if ((i->f->get_rport_##field) && \
690 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
691 (rport->port_state == FC_PORTSTATE_DELETED) || \
692 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
693 i->f->get_rport_##field(rport); \
694 return snprintf(buf, sz, format_string, cast rport->field); \
697 #define fc_rport_store_function(field) \
699 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
703 struct fc_rport *rport = transport_class_to_rport(cdev); \
704 struct Scsi_Host *shost = rport_to_shost(rport); \
705 struct fc_internal *i = to_fc_internal(shost->transportt); \
707 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
708 (rport->port_state == FC_PORTSTATE_DELETED) || \
709 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
711 val = simple_strtoul(buf, &cp, 0); \
712 if (*cp && (*cp != '\n')) \
714 i->f->set_rport_##field(rport, val); \
718 #define fc_rport_rd_attr(field, format_string, sz) \
719 fc_rport_show_function(field, format_string, sz, ) \
720 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
721 show_fc_rport_##field, NULL)
723 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
724 fc_rport_show_function(field, format_string, sz, (cast)) \
725 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
726 show_fc_rport_##field, NULL)
728 #define fc_rport_rw_attr(field, format_string, sz) \
729 fc_rport_show_function(field, format_string, sz, ) \
730 fc_rport_store_function(field) \
731 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
732 show_fc_rport_##field, \
733 store_fc_rport_##field)
736 #define fc_private_rport_show_function(field, format_string, sz, cast) \
738 show_fc_rport_##field (struct class_device *cdev, char *buf) \
740 struct fc_rport *rport = transport_class_to_rport(cdev); \
741 return snprintf(buf, sz, format_string, cast rport->field); \
744 #define fc_private_rport_rd_attr(field, format_string, sz) \
745 fc_private_rport_show_function(field, format_string, sz, ) \
746 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
747 show_fc_rport_##field, NULL)
749 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
750 fc_private_rport_show_function(field, format_string, sz, (cast)) \
751 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
752 show_fc_rport_##field, NULL)
755 #define fc_private_rport_rd_enum_attr(title, maxlen) \
757 show_fc_rport_##title (struct class_device *cdev, char *buf) \
759 struct fc_rport *rport = transport_class_to_rport(cdev); \
761 name = get_fc_##title##_name(rport->title); \
764 return snprintf(buf, maxlen, "%s\n", name); \
766 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
767 show_fc_rport_##title, NULL)
770 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
771 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
772 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
773 i->private_rport_attrs[count].store = NULL; \
774 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
775 if (i->f->show_rport_##field) \
778 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
779 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
780 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
781 i->private_rport_attrs[count].store = NULL; \
782 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
785 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
786 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
787 if (!i->f->set_rport_##field) { \
788 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
789 i->private_rport_attrs[count].store = NULL; \
791 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
792 if (i->f->show_rport_##field) \
795 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
797 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
798 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
803 /* The FC Transport Remote Port Attributes: */
805 /* Fixed Remote Port Attributes */
807 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
810 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
812 struct fc_rport *rport = transport_class_to_rport(cdev);
813 if (rport->supported_classes == FC_COS_UNSPECIFIED)
814 return snprintf(buf, 20, "unspecified\n");
815 return get_fc_cos_names(rport->supported_classes, buf);
817 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
818 show_fc_rport_supported_classes, NULL);
820 /* Dynamic Remote Port Attributes */
823 * dev_loss_tmo attribute
825 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
827 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
831 struct fc_rport *rport = transport_class_to_rport(cdev);
832 struct Scsi_Host *shost = rport_to_shost(rport);
833 struct fc_internal *i = to_fc_internal(shost->transportt);
835 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
836 (rport->port_state == FC_PORTSTATE_DELETED) ||
837 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
839 val = simple_strtoul(buf, &cp, 0);
840 if ((*cp && (*cp != '\n')) ||
841 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
843 i->f->set_rport_dev_loss_tmo(rport, val);
846 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
847 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
850 /* Private Remote Port Attributes */
852 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
853 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
854 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
857 show_fc_rport_roles (struct class_device *cdev, char *buf)
859 struct fc_rport *rport = transport_class_to_rport(cdev);
861 /* identify any roles that are port_id specific */
862 if ((rport->port_id != -1) &&
863 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
864 FC_WELLKNOWN_PORTID_MASK) {
865 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
866 case FC_FPORT_PORTID:
867 return snprintf(buf, 30, "Fabric Port\n");
868 case FC_FABCTLR_PORTID:
869 return snprintf(buf, 30, "Fabric Controller\n");
870 case FC_DIRSRVR_PORTID:
871 return snprintf(buf, 30, "Directory Server\n");
872 case FC_TIMESRVR_PORTID:
873 return snprintf(buf, 30, "Time Server\n");
874 case FC_MGMTSRVR_PORTID:
875 return snprintf(buf, 30, "Management Server\n");
877 return snprintf(buf, 30, "Unknown Fabric Entity\n");
880 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
881 return snprintf(buf, 20, "unknown\n");
882 return get_fc_port_roles_names(rport->roles, buf);
885 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
886 show_fc_rport_roles, NULL);
888 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
889 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
892 * fast_io_fail_tmo attribute
895 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
897 struct fc_rport *rport = transport_class_to_rport(cdev);
899 if (rport->fast_io_fail_tmo == -1)
900 return snprintf(buf, 5, "off\n");
901 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
905 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
910 struct fc_rport *rport = transport_class_to_rport(cdev);
912 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
913 (rport->port_state == FC_PORTSTATE_DELETED) ||
914 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
916 if (strncmp(buf, "off", 3) == 0)
917 rport->fast_io_fail_tmo = -1;
919 val = simple_strtoul(buf, &cp, 0);
920 if ((*cp && (*cp != '\n')) ||
921 (val < 0) || (val >= rport->dev_loss_tmo))
923 rport->fast_io_fail_tmo = val;
927 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
928 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
932 * FC SCSI Target Attribute Management
936 * Note: in the target show function we recognize when the remote
937 * port is in the heirarchy and do not allow the driver to get
938 * involved in sysfs functions. The driver only gets involved if
939 * it's the "old" style that doesn't use rports.
941 #define fc_starget_show_function(field, format_string, sz, cast) \
943 show_fc_starget_##field (struct class_device *cdev, char *buf) \
945 struct scsi_target *starget = transport_class_to_starget(cdev); \
946 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
947 struct fc_internal *i = to_fc_internal(shost->transportt); \
948 struct fc_rport *rport = starget_to_rport(starget); \
950 fc_starget_##field(starget) = rport->field; \
951 else if (i->f->get_starget_##field) \
952 i->f->get_starget_##field(starget); \
953 return snprintf(buf, sz, format_string, \
954 cast fc_starget_##field(starget)); \
957 #define fc_starget_rd_attr(field, format_string, sz) \
958 fc_starget_show_function(field, format_string, sz, ) \
959 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
960 show_fc_starget_##field, NULL)
962 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
963 fc_starget_show_function(field, format_string, sz, (cast)) \
964 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
965 show_fc_starget_##field, NULL)
967 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
968 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
969 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
970 i->private_starget_attrs[count].store = NULL; \
971 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
972 if (i->f->show_starget_##field) \
975 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
976 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
977 if (!i->f->set_starget_##field) { \
978 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
979 i->private_starget_attrs[count].store = NULL; \
981 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
982 if (i->f->show_starget_##field) \
985 /* The FC Transport SCSI Target Attributes: */
986 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
987 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
988 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
992 * FC Virtual Port Attribute Management
995 #define fc_vport_show_function(field, format_string, sz, cast) \
997 show_fc_vport_##field (struct class_device *cdev, char *buf) \
999 struct fc_vport *vport = transport_class_to_vport(cdev); \
1000 struct Scsi_Host *shost = vport_to_shost(vport); \
1001 struct fc_internal *i = to_fc_internal(shost->transportt); \
1002 if ((i->f->get_vport_##field) && \
1003 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1004 i->f->get_vport_##field(vport); \
1005 return snprintf(buf, sz, format_string, cast vport->field); \
1008 #define fc_vport_store_function(field) \
1010 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1014 struct fc_vport *vport = transport_class_to_vport(cdev); \
1015 struct Scsi_Host *shost = vport_to_shost(vport); \
1016 struct fc_internal *i = to_fc_internal(shost->transportt); \
1018 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1020 val = simple_strtoul(buf, &cp, 0); \
1021 if (*cp && (*cp != '\n')) \
1023 i->f->set_vport_##field(vport, val); \
1027 #define fc_vport_store_str_function(field, slen) \
1029 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1032 struct fc_vport *vport = transport_class_to_vport(cdev); \
1033 struct Scsi_Host *shost = vport_to_shost(vport); \
1034 struct fc_internal *i = to_fc_internal(shost->transportt); \
1035 unsigned int cnt=count; \
1037 /* count may include a LF at end of string */ \
1038 if (buf[cnt-1] == '\n') \
1040 if (cnt > ((slen) - 1)) \
1042 memcpy(vport->field, buf, cnt); \
1043 i->f->set_vport_##field(vport); \
1047 #define fc_vport_rd_attr(field, format_string, sz) \
1048 fc_vport_show_function(field, format_string, sz, ) \
1049 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1050 show_fc_vport_##field, NULL)
1052 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1053 fc_vport_show_function(field, format_string, sz, (cast)) \
1054 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1055 show_fc_vport_##field, NULL)
1057 #define fc_vport_rw_attr(field, format_string, sz) \
1058 fc_vport_show_function(field, format_string, sz, ) \
1059 fc_vport_store_function(field) \
1060 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1061 show_fc_vport_##field, \
1062 store_fc_vport_##field)
1064 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1066 show_fc_vport_##field (struct class_device *cdev, char *buf) \
1068 struct fc_vport *vport = transport_class_to_vport(cdev); \
1069 return snprintf(buf, sz, format_string, cast vport->field); \
1072 #define fc_private_vport_store_u32_function(field) \
1074 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1078 struct fc_vport *vport = transport_class_to_vport(cdev); \
1080 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1082 val = simple_strtoul(buf, &cp, 0); \
1083 if (*cp && (*cp != '\n')) \
1085 vport->field = val; \
1090 #define fc_private_vport_rd_attr(field, format_string, sz) \
1091 fc_private_vport_show_function(field, format_string, sz, ) \
1092 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1093 show_fc_vport_##field, NULL)
1095 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1096 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1097 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1098 show_fc_vport_##field, NULL)
1100 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1101 fc_private_vport_show_function(field, format_string, sz, ) \
1102 fc_private_vport_store_u32_function(field) \
1103 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1104 show_fc_vport_##field, \
1105 store_fc_vport_##field)
1108 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1110 show_fc_vport_##title (struct class_device *cdev, char *buf) \
1112 struct fc_vport *vport = transport_class_to_vport(cdev); \
1114 name = get_fc_##title##_name(vport->title); \
1117 return snprintf(buf, maxlen, "%s\n", name); \
1119 static FC_CLASS_DEVICE_ATTR(vport, title, S_IRUGO, \
1120 show_fc_vport_##title, NULL)
1123 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1124 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1125 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1126 i->private_vport_attrs[count].store = NULL; \
1127 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1128 if (i->f->get_##field) \
1130 /* NOTE: Above MACRO differs: checks function not show bit */
1132 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1133 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1134 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1135 i->private_vport_attrs[count].store = NULL; \
1136 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1139 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1140 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1141 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1144 /* NOTE: Above MACRO differs: checks function */
1146 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1147 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1148 if (!i->f->set_vport_##field) { \
1149 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1150 i->private_vport_attrs[count].store = NULL; \
1152 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1154 /* NOTE: Above MACRO differs: does not check show bit */
1156 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1158 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1159 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1164 /* The FC Transport Virtual Port Attributes: */
1166 /* Fixed Virtual Port Attributes */
1168 /* Dynamic Virtual Port Attributes */
1170 /* Private Virtual Port Attributes */
1172 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1173 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1174 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1175 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1178 show_fc_vport_roles (struct class_device *cdev, char *buf)
1180 struct fc_vport *vport = transport_class_to_vport(cdev);
1182 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1183 return snprintf(buf, 20, "unknown\n");
1184 return get_fc_port_roles_names(vport->roles, buf);
1186 static FC_CLASS_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1188 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1190 fc_private_vport_show_function(symbolic_name, "%s\n",
1191 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1192 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1193 static FC_CLASS_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1194 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1197 store_fc_vport_delete(struct class_device *cdev, const char *buf,
1200 struct fc_vport *vport = transport_class_to_vport(cdev);
1203 stat = fc_vport_terminate(vport);
1209 static FC_CLASS_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1210 NULL, store_fc_vport_delete);
1214 * Enable/Disable vport
1215 * Write "1" to disable, write "0" to enable
1218 store_fc_vport_disable(struct class_device *cdev, const char *buf,
1221 struct fc_vport *vport = transport_class_to_vport(cdev);
1222 struct Scsi_Host *shost = vport_to_shost(vport);
1223 struct fc_internal *i = to_fc_internal(shost->transportt);
1226 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1230 if (vport->vport_state != FC_VPORT_DISABLED)
1232 } else if (*buf == '1') {
1233 if (vport->vport_state == FC_VPORT_DISABLED)
1238 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1239 return stat ? stat : count;
1241 static FC_CLASS_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1242 NULL, store_fc_vport_disable);
1246 * Host Attribute Management
1249 #define fc_host_show_function(field, format_string, sz, cast) \
1251 show_fc_host_##field (struct class_device *cdev, char *buf) \
1253 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1254 struct fc_internal *i = to_fc_internal(shost->transportt); \
1255 if (i->f->get_host_##field) \
1256 i->f->get_host_##field(shost); \
1257 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1260 #define fc_host_store_function(field) \
1262 store_fc_host_##field(struct class_device *cdev, const char *buf, \
1266 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1267 struct fc_internal *i = to_fc_internal(shost->transportt); \
1270 val = simple_strtoul(buf, &cp, 0); \
1271 if (*cp && (*cp != '\n')) \
1273 i->f->set_host_##field(shost, val); \
1277 #define fc_host_store_str_function(field, slen) \
1279 store_fc_host_##field(struct class_device *cdev, const char *buf, \
1282 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1283 struct fc_internal *i = to_fc_internal(shost->transportt); \
1284 unsigned int cnt=count; \
1286 /* count may include a LF at end of string */ \
1287 if (buf[cnt-1] == '\n') \
1289 if (cnt > ((slen) - 1)) \
1291 memcpy(fc_host_##field(shost), buf, cnt); \
1292 i->f->set_host_##field(shost); \
1296 #define fc_host_rd_attr(field, format_string, sz) \
1297 fc_host_show_function(field, format_string, sz, ) \
1298 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1299 show_fc_host_##field, NULL)
1301 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1302 fc_host_show_function(field, format_string, sz, (cast)) \
1303 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1304 show_fc_host_##field, NULL)
1306 #define fc_host_rw_attr(field, format_string, sz) \
1307 fc_host_show_function(field, format_string, sz, ) \
1308 fc_host_store_function(field) \
1309 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1310 show_fc_host_##field, \
1311 store_fc_host_##field)
1313 #define fc_host_rd_enum_attr(title, maxlen) \
1315 show_fc_host_##title (struct class_device *cdev, char *buf) \
1317 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1318 struct fc_internal *i = to_fc_internal(shost->transportt); \
1320 if (i->f->get_host_##title) \
1321 i->f->get_host_##title(shost); \
1322 name = get_fc_##title##_name(fc_host_##title(shost)); \
1325 return snprintf(buf, maxlen, "%s\n", name); \
1327 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1329 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1330 i->private_host_attrs[count] = class_device_attr_host_##field; \
1331 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1332 i->private_host_attrs[count].store = NULL; \
1333 i->host_attrs[count] = &i->private_host_attrs[count]; \
1334 if (i->f->show_host_##field) \
1337 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1338 i->private_host_attrs[count] = class_device_attr_host_##field; \
1339 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1340 i->private_host_attrs[count].store = NULL; \
1341 i->host_attrs[count] = &i->private_host_attrs[count]; \
1344 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1345 i->private_host_attrs[count] = class_device_attr_host_##field; \
1346 if (!i->f->set_host_##field) { \
1347 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1348 i->private_host_attrs[count].store = NULL; \
1350 i->host_attrs[count] = &i->private_host_attrs[count]; \
1351 if (i->f->show_host_##field) \
1355 #define fc_private_host_show_function(field, format_string, sz, cast) \
1357 show_fc_host_##field (struct class_device *cdev, char *buf) \
1359 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1360 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1363 #define fc_private_host_rd_attr(field, format_string, sz) \
1364 fc_private_host_show_function(field, format_string, sz, ) \
1365 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1366 show_fc_host_##field, NULL)
1368 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1369 fc_private_host_show_function(field, format_string, sz, (cast)) \
1370 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1371 show_fc_host_##field, NULL)
1373 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1374 i->private_host_attrs[count] = class_device_attr_host_##field; \
1375 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1376 i->private_host_attrs[count].store = NULL; \
1377 i->host_attrs[count] = &i->private_host_attrs[count]; \
1380 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1382 i->private_host_attrs[count] = class_device_attr_host_##field; \
1383 i->host_attrs[count] = &i->private_host_attrs[count]; \
1388 /* Fixed Host Attributes */
1391 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1393 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1395 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1396 return snprintf(buf, 20, "unspecified\n");
1398 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1400 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1401 show_fc_host_supported_classes, NULL);
1404 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1406 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1407 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1409 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1410 show_fc_host_supported_fc4s, NULL);
1413 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1415 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1417 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1418 return snprintf(buf, 20, "unknown\n");
1420 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1422 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1423 show_fc_host_supported_speeds, NULL);
1426 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1427 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1428 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1429 unsigned long long);
1430 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1431 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1432 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1435 /* Dynamic Host Attributes */
1438 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1440 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1441 struct fc_internal *i = to_fc_internal(shost->transportt);
1443 if (i->f->get_host_active_fc4s)
1444 i->f->get_host_active_fc4s(shost);
1446 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1448 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1449 show_fc_host_active_fc4s, NULL);
1452 show_fc_host_speed (struct class_device *cdev, char *buf)
1454 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1455 struct fc_internal *i = to_fc_internal(shost->transportt);
1457 if (i->f->get_host_speed)
1458 i->f->get_host_speed(shost);
1460 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1461 return snprintf(buf, 20, "unknown\n");
1463 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1465 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1466 show_fc_host_speed, NULL);
1469 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1470 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1471 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1472 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1473 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1475 fc_private_host_show_function(system_hostname, "%s\n",
1476 FC_SYMBOLIC_NAME_SIZE + 1, )
1477 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1478 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1479 show_fc_host_system_hostname, store_fc_host_system_hostname);
1482 /* Private Host Attributes */
1485 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1487 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1490 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1493 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1496 #define get_list_head_entry(pos, head, member) \
1497 pos = list_entry((head)->next, typeof(*pos), member)
1500 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1501 const char *buf, size_t count)
1503 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1504 struct fc_rport *rport;
1505 enum fc_tgtid_binding_type val;
1506 unsigned long flags;
1508 if (get_fc_tgtid_bind_type_match(buf, &val))
1511 /* if changing bind type, purge all unused consistent bindings */
1512 if (val != fc_host_tgtid_bind_type(shost)) {
1513 spin_lock_irqsave(shost->host_lock, flags);
1514 while (!list_empty(&fc_host_rport_bindings(shost))) {
1515 get_list_head_entry(rport,
1516 &fc_host_rport_bindings(shost), peers);
1517 list_del(&rport->peers);
1518 rport->port_state = FC_PORTSTATE_DELETED;
1519 fc_queue_work(shost, &rport->rport_delete_work);
1521 spin_unlock_irqrestore(shost->host_lock, flags);
1524 fc_host_tgtid_bind_type(shost) = val;
1528 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1529 show_fc_private_host_tgtid_bind_type,
1530 store_fc_private_host_tgtid_bind_type);
1533 store_fc_private_host_issue_lip(struct class_device *cdev,
1534 const char *buf, size_t count)
1536 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1537 struct fc_internal *i = to_fc_internal(shost->transportt);
1540 /* ignore any data value written to the attribute */
1541 if (i->f->issue_fc_host_lip) {
1542 ret = i->f->issue_fc_host_lip(shost);
1543 return ret ? ret: count;
1549 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1550 store_fc_private_host_issue_lip);
1552 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1556 * Host Statistics Management
1559 /* Show a given an attribute in the statistics group */
1561 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1563 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1564 struct fc_internal *i = to_fc_internal(shost->transportt);
1565 struct fc_host_statistics *stats;
1566 ssize_t ret = -ENOENT;
1568 if (offset > sizeof(struct fc_host_statistics) ||
1569 offset % sizeof(u64) != 0)
1572 if (i->f->get_fc_host_stats) {
1573 stats = (i->f->get_fc_host_stats)(shost);
1575 ret = snprintf(buf, 20, "0x%llx\n",
1576 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1582 /* generate a read-only statistics attribute */
1583 #define fc_host_statistic(name) \
1584 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
1586 return fc_stat_show(cd, buf, \
1587 offsetof(struct fc_host_statistics, name)); \
1589 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1591 fc_host_statistic(seconds_since_last_reset);
1592 fc_host_statistic(tx_frames);
1593 fc_host_statistic(tx_words);
1594 fc_host_statistic(rx_frames);
1595 fc_host_statistic(rx_words);
1596 fc_host_statistic(lip_count);
1597 fc_host_statistic(nos_count);
1598 fc_host_statistic(error_frames);
1599 fc_host_statistic(dumped_frames);
1600 fc_host_statistic(link_failure_count);
1601 fc_host_statistic(loss_of_sync_count);
1602 fc_host_statistic(loss_of_signal_count);
1603 fc_host_statistic(prim_seq_protocol_err_count);
1604 fc_host_statistic(invalid_tx_word_count);
1605 fc_host_statistic(invalid_crc_count);
1606 fc_host_statistic(fcp_input_requests);
1607 fc_host_statistic(fcp_output_requests);
1608 fc_host_statistic(fcp_control_requests);
1609 fc_host_statistic(fcp_input_megabytes);
1610 fc_host_statistic(fcp_output_megabytes);
1613 fc_reset_statistics(struct class_device *cdev, const char *buf,
1616 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1617 struct fc_internal *i = to_fc_internal(shost->transportt);
1619 /* ignore any data value written to the attribute */
1620 if (i->f->reset_fc_host_stats) {
1621 i->f->reset_fc_host_stats(shost);
1627 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1628 fc_reset_statistics);
1630 static struct attribute *fc_statistics_attrs[] = {
1631 &class_device_attr_host_seconds_since_last_reset.attr,
1632 &class_device_attr_host_tx_frames.attr,
1633 &class_device_attr_host_tx_words.attr,
1634 &class_device_attr_host_rx_frames.attr,
1635 &class_device_attr_host_rx_words.attr,
1636 &class_device_attr_host_lip_count.attr,
1637 &class_device_attr_host_nos_count.attr,
1638 &class_device_attr_host_error_frames.attr,
1639 &class_device_attr_host_dumped_frames.attr,
1640 &class_device_attr_host_link_failure_count.attr,
1641 &class_device_attr_host_loss_of_sync_count.attr,
1642 &class_device_attr_host_loss_of_signal_count.attr,
1643 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1644 &class_device_attr_host_invalid_tx_word_count.attr,
1645 &class_device_attr_host_invalid_crc_count.attr,
1646 &class_device_attr_host_fcp_input_requests.attr,
1647 &class_device_attr_host_fcp_output_requests.attr,
1648 &class_device_attr_host_fcp_control_requests.attr,
1649 &class_device_attr_host_fcp_input_megabytes.attr,
1650 &class_device_attr_host_fcp_output_megabytes.attr,
1651 &class_device_attr_host_reset_statistics.attr,
1655 static struct attribute_group fc_statistics_group = {
1656 .name = "statistics",
1657 .attrs = fc_statistics_attrs,
1661 /* Host Vport Attributes */
1664 fc_parse_wwn(const char *ns, u64 *nm)
1669 memset(wwn, 0, sizeof(wwn));
1671 /* Validate and store the new name */
1672 for (i=0, j=0; i < 16; i++) {
1673 if ((*ns >= 'a') && (*ns <= 'f'))
1674 j = ((j << 4) | ((*ns++ -'a') + 10));
1675 else if ((*ns >= 'A') && (*ns <= 'F'))
1676 j = ((j << 4) | ((*ns++ -'A') + 10));
1677 else if ((*ns >= '0') && (*ns <= '9'))
1678 j = ((j << 4) | (*ns++ -'0'));
1682 wwn[i/2] = j & 0xff;
1687 *nm = wwn_to_u64(wwn);
1694 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1695 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1696 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1697 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1700 store_fc_host_vport_create(struct class_device *cdev, const char *buf,
1703 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1704 struct fc_vport_identifiers vid;
1705 struct fc_vport *vport;
1706 unsigned int cnt=count;
1709 memset(&vid, 0, sizeof(vid));
1711 /* count may include a LF at end of string */
1712 if (buf[cnt-1] == '\n')
1715 /* validate we have enough characters for WWPN */
1716 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1719 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1723 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1727 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1728 vid.vport_type = FC_PORTTYPE_NPIV;
1729 /* vid.symbolic_name is already zero/NULL's */
1730 vid.disable = false; /* always enabled */
1732 /* we only allow support on Channel 0 !!! */
1733 stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
1734 return stat ? stat : count;
1736 static FC_CLASS_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1737 store_fc_host_vport_create);
1741 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1742 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1743 * The WWNs are specified as hex characters, and may *not* contain
1744 * any prefixes (e.g. 0x, x, etc)
1747 store_fc_host_vport_delete(struct class_device *cdev, const char *buf,
1750 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1751 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1752 struct fc_vport *vport;
1754 unsigned long flags;
1755 unsigned int cnt=count;
1758 /* count may include a LF at end of string */
1759 if (buf[cnt-1] == '\n')
1762 /* validate we have enough characters for WWPN */
1763 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1766 stat = fc_parse_wwn(&buf[0], &wwpn);
1770 stat = fc_parse_wwn(&buf[17], &wwnn);
1774 spin_lock_irqsave(shost->host_lock, flags);
1776 /* we only allow support on Channel 0 !!! */
1777 list_for_each_entry(vport, &fc_host->vports, peers) {
1778 if ((vport->channel == 0) &&
1779 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1784 spin_unlock_irqrestore(shost->host_lock, flags);
1789 stat = fc_vport_terminate(vport);
1790 return stat ? stat : count;
1792 static FC_CLASS_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1793 store_fc_host_vport_delete);
1796 static int fc_host_match(struct attribute_container *cont,
1799 struct Scsi_Host *shost;
1800 struct fc_internal *i;
1802 if (!scsi_is_host_device(dev))
1805 shost = dev_to_shost(dev);
1806 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1807 != &fc_host_class.class)
1810 i = to_fc_internal(shost->transportt);
1812 return &i->t.host_attrs.ac == cont;
1815 static int fc_target_match(struct attribute_container *cont,
1818 struct Scsi_Host *shost;
1819 struct fc_internal *i;
1821 if (!scsi_is_target_device(dev))
1824 shost = dev_to_shost(dev->parent);
1825 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1826 != &fc_host_class.class)
1829 i = to_fc_internal(shost->transportt);
1831 return &i->t.target_attrs.ac == cont;
1834 static void fc_rport_dev_release(struct device *dev)
1836 struct fc_rport *rport = dev_to_rport(dev);
1837 put_device(dev->parent);
1841 int scsi_is_fc_rport(const struct device *dev)
1843 return dev->release == fc_rport_dev_release;
1845 EXPORT_SYMBOL(scsi_is_fc_rport);
1847 static int fc_rport_match(struct attribute_container *cont,
1850 struct Scsi_Host *shost;
1851 struct fc_internal *i;
1853 if (!scsi_is_fc_rport(dev))
1856 shost = dev_to_shost(dev->parent);
1857 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1858 != &fc_host_class.class)
1861 i = to_fc_internal(shost->transportt);
1863 return &i->rport_attr_cont.ac == cont;
1867 static void fc_vport_dev_release(struct device *dev)
1869 struct fc_vport *vport = dev_to_vport(dev);
1870 put_device(dev->parent); /* release kobj parent */
1874 int scsi_is_fc_vport(const struct device *dev)
1876 return dev->release == fc_vport_dev_release;
1878 EXPORT_SYMBOL(scsi_is_fc_vport);
1880 static int fc_vport_match(struct attribute_container *cont,
1883 struct fc_vport *vport;
1884 struct Scsi_Host *shost;
1885 struct fc_internal *i;
1887 if (!scsi_is_fc_vport(dev))
1889 vport = dev_to_vport(dev);
1891 shost = vport_to_shost(vport);
1892 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1893 != &fc_host_class.class)
1896 i = to_fc_internal(shost->transportt);
1897 return &i->vport_attr_cont.ac == cont;
1902 * fc_timed_out - FC Transport I/O timeout intercept handler
1904 * @scmd: The SCSI command which timed out
1906 * This routine protects against error handlers getting invoked while a
1907 * rport is in a blocked state, typically due to a temporarily loss of
1908 * connectivity. If the error handlers are allowed to proceed, requests
1909 * to abort i/o, reset the target, etc will likely fail as there is no way
1910 * to communicate with the device to perform the requested function. These
1911 * failures may result in the midlayer taking the device offline, requiring
1912 * manual intervention to restore operation.
1914 * This routine, called whenever an i/o times out, validates the state of
1915 * the underlying rport. If the rport is blocked, it returns
1916 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1917 * Eventually, either the device will return, or devloss_tmo will fire,
1918 * and when the timeout then fires, it will be handled normally.
1919 * If the rport is not blocked, normal error handling continues.
1922 * This routine assumes no locks are held on entry.
1924 static enum scsi_eh_timer_return
1925 fc_timed_out(struct scsi_cmnd *scmd)
1927 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1929 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1930 return EH_RESET_TIMER;
1932 return EH_NOT_HANDLED;
1936 * Must be called with shost->host_lock held
1938 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1941 struct fc_rport *rport;
1943 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1944 if (rport->scsi_target_id == -1)
1947 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1948 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1949 scsi_scan_target(&rport->dev, rport->channel,
1950 rport->scsi_target_id, lun, 1);
1957 struct scsi_transport_template *
1958 fc_attach_transport(struct fc_function_template *ft)
1961 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1967 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1968 i->t.target_attrs.ac.class = &fc_transport_class.class;
1969 i->t.target_attrs.ac.match = fc_target_match;
1970 i->t.target_size = sizeof(struct fc_starget_attrs);
1971 transport_container_register(&i->t.target_attrs);
1973 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1974 i->t.host_attrs.ac.class = &fc_host_class.class;
1975 i->t.host_attrs.ac.match = fc_host_match;
1976 i->t.host_size = sizeof(struct fc_host_attrs);
1977 if (ft->get_fc_host_stats)
1978 i->t.host_attrs.statistics = &fc_statistics_group;
1979 transport_container_register(&i->t.host_attrs);
1981 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1982 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1983 i->rport_attr_cont.ac.match = fc_rport_match;
1984 transport_container_register(&i->rport_attr_cont);
1986 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
1987 i->vport_attr_cont.ac.class = &fc_vport_class.class;
1988 i->vport_attr_cont.ac.match = fc_vport_match;
1989 transport_container_register(&i->vport_attr_cont);
1993 /* Transport uses the shost workq for scsi scanning */
1994 i->t.create_work_queue = 1;
1996 i->t.eh_timed_out = fc_timed_out;
1998 i->t.user_scan = fc_user_scan;
2001 * Setup SCSI Target Attributes.
2004 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2005 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2006 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2008 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2010 i->starget_attrs[count] = NULL;
2014 * Setup SCSI Host Attributes.
2017 SETUP_HOST_ATTRIBUTE_RD(node_name);
2018 SETUP_HOST_ATTRIBUTE_RD(port_name);
2019 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2020 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2021 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2022 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2023 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2024 if (ft->vport_create) {
2025 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2026 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2028 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2030 SETUP_HOST_ATTRIBUTE_RD(port_id);
2031 SETUP_HOST_ATTRIBUTE_RD(port_type);
2032 SETUP_HOST_ATTRIBUTE_RD(port_state);
2033 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2034 SETUP_HOST_ATTRIBUTE_RD(speed);
2035 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2036 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2037 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2039 /* Transport-managed attributes */
2040 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2041 if (ft->issue_fc_host_lip)
2042 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2043 if (ft->vport_create)
2044 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2045 if (ft->vport_delete)
2046 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2048 BUG_ON(count > FC_HOST_NUM_ATTRS);
2050 i->host_attrs[count] = NULL;
2053 * Setup Remote Port Attributes.
2056 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2057 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2058 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2059 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2060 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2061 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2062 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2063 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2064 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2065 if (ft->terminate_rport_io)
2066 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2068 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2070 i->rport_attrs[count] = NULL;
2073 * Setup Virtual Port Attributes.
2076 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2077 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2078 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2079 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2080 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2081 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2082 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2083 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2084 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2086 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2088 i->vport_attrs[count] = NULL;
2092 EXPORT_SYMBOL(fc_attach_transport);
2094 void fc_release_transport(struct scsi_transport_template *t)
2096 struct fc_internal *i = to_fc_internal(t);
2098 transport_container_unregister(&i->t.target_attrs);
2099 transport_container_unregister(&i->t.host_attrs);
2100 transport_container_unregister(&i->rport_attr_cont);
2101 transport_container_unregister(&i->vport_attr_cont);
2105 EXPORT_SYMBOL(fc_release_transport);
2108 * fc_queue_work - Queue work to the fc_host workqueue.
2109 * @shost: Pointer to Scsi_Host bound to fc_host.
2110 * @work: Work to queue for execution.
2113 * 1 - work queued for execution
2114 * 0 - work is already queued
2115 * -EINVAL - work queue doesn't exist
2118 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2120 if (unlikely(!fc_host_work_q(shost))) {
2122 "ERROR: FC host '%s' attempted to queue work, "
2123 "when no workqueue created.\n", shost->hostt->name);
2129 return queue_work(fc_host_work_q(shost), work);
2133 * fc_flush_work - Flush a fc_host's workqueue.
2134 * @shost: Pointer to Scsi_Host bound to fc_host.
2137 fc_flush_work(struct Scsi_Host *shost)
2139 if (!fc_host_work_q(shost)) {
2141 "ERROR: FC host '%s' attempted to flush work, "
2142 "when no workqueue created.\n", shost->hostt->name);
2147 flush_workqueue(fc_host_work_q(shost));
2151 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2152 * @shost: Pointer to Scsi_Host bound to fc_host.
2153 * @work: Work to queue for execution.
2154 * @delay: jiffies to delay the work queuing
2157 * 1 on success / 0 already queued / < 0 for error
2160 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2161 unsigned long delay)
2163 if (unlikely(!fc_host_devloss_work_q(shost))) {
2165 "ERROR: FC host '%s' attempted to queue work, "
2166 "when no workqueue created.\n", shost->hostt->name);
2172 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2176 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2177 * @shost: Pointer to Scsi_Host bound to fc_host.
2180 fc_flush_devloss(struct Scsi_Host *shost)
2182 if (!fc_host_devloss_work_q(shost)) {
2184 "ERROR: FC host '%s' attempted to flush work, "
2185 "when no workqueue created.\n", shost->hostt->name);
2190 flush_workqueue(fc_host_devloss_work_q(shost));
2195 * fc_remove_host - called to terminate any fc_transport-related elements
2197 * @rport: remote port to be unblocked.
2199 * This routine is expected to be called immediately preceeding the
2200 * a driver's call to scsi_remove_host().
2202 * WARNING: A driver utilizing the fc_transport, which fails to call
2203 * this routine prior to scsi_remote_host(), will leave dangling
2204 * objects in /sys/class/fc_remote_ports. Access to any of these
2205 * objects can result in a system crash !!!
2208 * This routine assumes no locks are held on entry.
2211 fc_remove_host(struct Scsi_Host *shost)
2213 struct fc_vport *vport = NULL, *next_vport = NULL;
2214 struct fc_rport *rport = NULL, *next_rport = NULL;
2215 struct workqueue_struct *work_q;
2216 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2217 unsigned long flags;
2220 spin_lock_irqsave(shost->host_lock, flags);
2222 /* Remove any vports */
2223 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2224 spin_unlock_irqrestore(shost->host_lock, flags);
2225 /* this must be called synchronously */
2226 stat = fc_vport_terminate(vport);
2227 spin_lock_irqsave(shost->host_lock, flags);
2229 dev_printk(KERN_ERR, vport->dev.parent,
2230 "%s: %s could not be deleted created via "
2231 "shost%d channel %d\n", __FUNCTION__,
2232 vport->dev.bus_id, vport->shost->host_no,
2236 /* Remove any remote ports */
2237 list_for_each_entry_safe(rport, next_rport,
2238 &fc_host->rports, peers) {
2239 list_del(&rport->peers);
2240 rport->port_state = FC_PORTSTATE_DELETED;
2241 fc_queue_work(shost, &rport->rport_delete_work);
2244 list_for_each_entry_safe(rport, next_rport,
2245 &fc_host->rport_bindings, peers) {
2246 list_del(&rport->peers);
2247 rport->port_state = FC_PORTSTATE_DELETED;
2248 fc_queue_work(shost, &rport->rport_delete_work);
2251 spin_unlock_irqrestore(shost->host_lock, flags);
2253 /* flush all scan work items */
2254 scsi_flush_work(shost);
2256 /* flush all stgt delete, and rport delete work items, then kill it */
2257 if (fc_host->work_q) {
2258 work_q = fc_host->work_q;
2259 fc_host->work_q = NULL;
2260 destroy_workqueue(work_q);
2263 /* flush all devloss work items, then kill it */
2264 if (fc_host->devloss_work_q) {
2265 work_q = fc_host->devloss_work_q;
2266 fc_host->devloss_work_q = NULL;
2267 destroy_workqueue(work_q);
2270 EXPORT_SYMBOL(fc_remove_host);
2274 * fc_starget_delete - called to delete the scsi decendents of an rport
2275 * (target and all sdevs)
2277 * @work: remote port to be operated on.
2280 fc_starget_delete(struct work_struct *work)
2282 struct fc_rport *rport =
2283 container_of(work, struct fc_rport, stgt_delete_work);
2284 struct Scsi_Host *shost = rport_to_shost(rport);
2285 struct fc_internal *i = to_fc_internal(shost->transportt);
2287 /* Involve the LLDD if possible to terminate all io on the rport. */
2288 if (i->f->terminate_rport_io)
2289 i->f->terminate_rport_io(rport);
2291 scsi_remove_target(&rport->dev);
2296 * fc_rport_final_delete - finish rport termination and delete it.
2298 * @work: remote port to be deleted.
2301 fc_rport_final_delete(struct work_struct *work)
2303 struct fc_rport *rport =
2304 container_of(work, struct fc_rport, rport_delete_work);
2305 struct device *dev = &rport->dev;
2306 struct Scsi_Host *shost = rport_to_shost(rport);
2307 struct fc_internal *i = to_fc_internal(shost->transportt);
2308 unsigned long flags;
2311 * if a scan is pending, flush the SCSI Host work_q so that
2312 * that we can reclaim the rport scan work element.
2314 if (rport->flags & FC_RPORT_SCAN_PENDING)
2315 scsi_flush_work(shost);
2317 /* involve the LLDD to terminate all pending i/o */
2318 if (i->f->terminate_rport_io)
2319 i->f->terminate_rport_io(rport);
2322 * Cancel any outstanding timers. These should really exist
2323 * only when rmmod'ing the LLDD and we're asking for
2324 * immediate termination of the rports
2326 spin_lock_irqsave(shost->host_lock, flags);
2327 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2328 spin_unlock_irqrestore(shost->host_lock, flags);
2329 if (!cancel_delayed_work(&rport->fail_io_work))
2330 fc_flush_devloss(shost);
2331 if (!cancel_delayed_work(&rport->dev_loss_work))
2332 fc_flush_devloss(shost);
2333 spin_lock_irqsave(shost->host_lock, flags);
2334 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2336 spin_unlock_irqrestore(shost->host_lock, flags);
2338 /* Delete SCSI target and sdevs */
2339 if (rport->scsi_target_id != -1)
2340 fc_starget_delete(&rport->stgt_delete_work);
2343 * Notify the driver that the rport is now dead. The LLDD will
2344 * also guarantee that any communication to the rport is terminated
2346 if (i->f->dev_loss_tmo_callbk)
2347 i->f->dev_loss_tmo_callbk(rport);
2349 transport_remove_device(dev);
2351 transport_destroy_device(dev);
2352 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2353 put_device(dev); /* for self-reference */
2358 * fc_rport_create - allocates and creates a remote FC port.
2359 * @shost: scsi host the remote port is connected to.
2360 * @channel: Channel on shost port connected to.
2361 * @ids: The world wide names, fc address, and FC4 port
2362 * roles for the remote port.
2364 * Allocates and creates the remoter port structure, including the
2365 * class and sysfs creation.
2368 * This routine assumes no locks are held on entry.
2371 fc_rport_create(struct Scsi_Host *shost, int channel,
2372 struct fc_rport_identifiers *ids)
2374 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2375 struct fc_internal *fci = to_fc_internal(shost->transportt);
2376 struct fc_rport *rport;
2378 unsigned long flags;
2382 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2383 rport = kzalloc(size, GFP_KERNEL);
2384 if (unlikely(!rport)) {
2385 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
2389 rport->maxframe_size = -1;
2390 rport->supported_classes = FC_COS_UNSPECIFIED;
2391 rport->dev_loss_tmo = fc_dev_loss_tmo;
2392 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2393 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2394 rport->port_id = ids->port_id;
2395 rport->roles = ids->roles;
2396 rport->port_state = FC_PORTSTATE_ONLINE;
2397 if (fci->f->dd_fcrport_size)
2398 rport->dd_data = &rport[1];
2399 rport->channel = channel;
2400 rport->fast_io_fail_tmo = -1;
2402 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2403 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2404 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2405 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2406 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2408 spin_lock_irqsave(shost->host_lock, flags);
2410 rport->number = fc_host->next_rport_number++;
2411 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
2412 rport->scsi_target_id = fc_host->next_target_id++;
2414 rport->scsi_target_id = -1;
2415 list_add_tail(&rport->peers, &fc_host->rports);
2416 get_device(&shost->shost_gendev); /* for fc_host->rport list */
2418 spin_unlock_irqrestore(shost->host_lock, flags);
2421 device_initialize(dev); /* takes self reference */
2422 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2423 dev->release = fc_rport_dev_release;
2424 sprintf(dev->bus_id, "rport-%d:%d-%d",
2425 shost->host_no, channel, rport->number);
2426 transport_setup_device(dev);
2428 error = device_add(dev);
2430 printk(KERN_ERR "FC Remote Port device_add failed\n");
2433 transport_add_device(dev);
2434 transport_configure_device(dev);
2436 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2437 /* initiate a scan of the target */
2438 rport->flags |= FC_RPORT_SCAN_PENDING;
2439 scsi_queue_work(shost, &rport->scan_work);
2445 transport_destroy_device(dev);
2446 spin_lock_irqsave(shost->host_lock, flags);
2447 list_del(&rport->peers);
2448 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2449 spin_unlock_irqrestore(shost->host_lock, flags);
2450 put_device(dev->parent);
2456 * fc_remote_port_add - notifies the fc transport of the existence
2457 * of a remote FC port.
2458 * @shost: scsi host the remote port is connected to.
2459 * @channel: Channel on shost port connected to.
2460 * @ids: The world wide names, fc address, and FC4 port
2461 * roles for the remote port.
2463 * The LLDD calls this routine to notify the transport of the existence
2464 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2465 * of the port, it's FC address (port_id), and the FC4 roles that are
2466 * active for the port.
2468 * For ports that are FCP targets (aka scsi targets), the FC transport
2469 * maintains consistent target id bindings on behalf of the LLDD.
2470 * A consistent target id binding is an assignment of a target id to
2471 * a remote port identifier, which persists while the scsi host is
2472 * attached. The remote port can disappear, then later reappear, and
2473 * it's target id assignment remains the same. This allows for shifts
2474 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2475 * changes to the scsi subsystem which is based on scsi host number and
2476 * target id values. Bindings are only valid during the attachment of
2477 * the scsi host. If the host detaches, then later re-attaches, target
2478 * id bindings may change.
2480 * This routine is responsible for returning a remote port structure.
2481 * The routine will search the list of remote ports it maintains
2482 * internally on behalf of consistent target id mappings. If found, the
2483 * remote port structure will be reused. Otherwise, a new remote port
2484 * structure will be allocated.
2486 * Whenever a remote port is allocated, a new fc_remote_port class
2487 * device is created.
2489 * Should not be called from interrupt context.
2492 * This routine assumes no locks are held on entry.
2495 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2496 struct fc_rport_identifiers *ids)
2498 struct fc_internal *fci = to_fc_internal(shost->transportt);
2499 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2500 struct fc_rport *rport;
2501 unsigned long flags;
2504 /* ensure any stgt delete functions are done */
2505 fc_flush_work(shost);
2508 * Search the list of "active" rports, for an rport that has been
2509 * deleted, but we've held off the real delete while the target
2510 * is in a "blocked" state.
2512 spin_lock_irqsave(shost->host_lock, flags);
2514 list_for_each_entry(rport, &fc_host->rports, peers) {
2516 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2517 (rport->channel == channel)) {
2519 switch (fc_host->tgtid_bind_type) {
2520 case FC_TGTID_BIND_BY_WWPN:
2521 case FC_TGTID_BIND_NONE:
2522 if (rport->port_name == ids->port_name)
2525 case FC_TGTID_BIND_BY_WWNN:
2526 if (rport->node_name == ids->node_name)
2529 case FC_TGTID_BIND_BY_ID:
2530 if (rport->port_id == ids->port_id)
2537 memcpy(&rport->node_name, &ids->node_name,
2538 sizeof(rport->node_name));
2539 memcpy(&rport->port_name, &ids->port_name,
2540 sizeof(rport->port_name));
2541 rport->port_id = ids->port_id;
2543 rport->port_state = FC_PORTSTATE_ONLINE;
2544 rport->roles = ids->roles;
2546 spin_unlock_irqrestore(shost->host_lock, flags);
2548 if (fci->f->dd_fcrport_size)
2549 memset(rport->dd_data, 0,
2550 fci->f->dd_fcrport_size);
2553 * If we were not a target, cancel the
2554 * io terminate and rport timers, and
2557 * If we were a target, but our new role
2558 * doesn't indicate a target, leave the
2559 * timers running expecting the role to
2560 * change as the target fully logs in. If
2561 * it doesn't, the target will be torn down.
2563 * If we were a target, and our role shows
2564 * we're still a target, cancel the timers
2565 * and kick off a scan.
2568 /* was a target, not in roles */
2569 if ((rport->scsi_target_id != -1) &&
2570 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2574 * Stop the fail io and dev_loss timers.
2575 * If they flush, the port_state will
2576 * be checked and will NOOP the function.
2578 if (!cancel_delayed_work(&rport->fail_io_work))
2579 fc_flush_devloss(shost);
2580 if (!cancel_delayed_work(&rport->dev_loss_work))
2581 fc_flush_devloss(shost);
2583 spin_lock_irqsave(shost->host_lock, flags);
2585 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2587 /* if target, initiate a scan */
2588 if (rport->scsi_target_id != -1) {
2589 rport->flags |= FC_RPORT_SCAN_PENDING;
2590 scsi_queue_work(shost,
2592 spin_unlock_irqrestore(shost->host_lock,
2594 scsi_target_unblock(&rport->dev);
2596 spin_unlock_irqrestore(shost->host_lock,
2605 * Search the bindings array
2606 * Note: if never a FCP target, you won't be on this list
2608 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2610 /* search for a matching consistent binding */
2612 list_for_each_entry(rport, &fc_host->rport_bindings,
2614 if (rport->channel != channel)
2617 switch (fc_host->tgtid_bind_type) {
2618 case FC_TGTID_BIND_BY_WWPN:
2619 if (rport->port_name == ids->port_name)
2622 case FC_TGTID_BIND_BY_WWNN:
2623 if (rport->node_name == ids->node_name)
2626 case FC_TGTID_BIND_BY_ID:
2627 if (rport->port_id == ids->port_id)
2630 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2635 list_move_tail(&rport->peers, &fc_host->rports);
2641 memcpy(&rport->node_name, &ids->node_name,
2642 sizeof(rport->node_name));
2643 memcpy(&rport->port_name, &ids->port_name,
2644 sizeof(rport->port_name));
2645 rport->port_id = ids->port_id;
2646 rport->roles = ids->roles;
2647 rport->port_state = FC_PORTSTATE_ONLINE;
2649 if (fci->f->dd_fcrport_size)
2650 memset(rport->dd_data, 0,
2651 fci->f->dd_fcrport_size);
2653 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2654 /* initiate a scan of the target */
2655 rport->flags |= FC_RPORT_SCAN_PENDING;
2656 scsi_queue_work(shost, &rport->scan_work);
2657 spin_unlock_irqrestore(shost->host_lock, flags);
2658 scsi_target_unblock(&rport->dev);
2660 spin_unlock_irqrestore(shost->host_lock, flags);
2666 spin_unlock_irqrestore(shost->host_lock, flags);
2668 /* No consistent binding found - create new remote port entry */
2669 rport = fc_rport_create(shost, channel, ids);
2673 EXPORT_SYMBOL(fc_remote_port_add);
2677 * fc_remote_port_delete - notifies the fc transport that a remote
2678 * port is no longer in existence.
2679 * @rport: The remote port that no longer exists
2681 * The LLDD calls this routine to notify the transport that a remote
2682 * port is no longer part of the topology. Note: Although a port
2683 * may no longer be part of the topology, it may persist in the remote
2684 * ports displayed by the fc_host. We do this under 2 conditions:
2685 * - If the port was a scsi target, we delay its deletion by "blocking" it.
2686 * This allows the port to temporarily disappear, then reappear without
2687 * disrupting the SCSI device tree attached to it. During the "blocked"
2688 * period the port will still exist.
2689 * - If the port was a scsi target and disappears for longer than we
2690 * expect, we'll delete the port and the tear down the SCSI device tree
2691 * attached to it. However, we want to semi-persist the target id assigned
2692 * to that port if it eventually does exist. The port structure will
2693 * remain (although with minimal information) so that the target id
2696 * If the remote port is not an FCP Target, it will be fully torn down
2697 * and deallocated, including the fc_remote_port class device.
2699 * If the remote port is an FCP Target, the port will be placed in a
2700 * temporary blocked state. From the LLDD's perspective, the rport no
2701 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2702 * exists, but all sdevs on it are blocked from further I/O. The following
2704 * If the remote port does not return (signaled by a LLDD call to
2705 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2706 * scsi target is removed - killing all outstanding i/o and removing the
2707 * scsi devices attached ot it. The port structure will be marked Not
2708 * Present and be partially cleared, leaving only enough information to
2709 * recognize the remote port relative to the scsi target id binding if
2710 * it later appears. The port will remain as long as there is a valid
2711 * binding (e.g. until the user changes the binding type or unloads the
2712 * scsi host with the binding).
2714 * If the remote port returns within the dev_loss_tmo value (and matches
2715 * according to the target id binding type), the port structure will be
2716 * reused. If it is no longer a SCSI target, the target will be torn
2717 * down. If it continues to be a SCSI target, then the target will be
2718 * unblocked (allowing i/o to be resumed), and a scan will be activated
2719 * to ensure that all luns are detected.
2721 * Called from normal process context only - cannot be called from interrupt.
2724 * This routine assumes no locks are held on entry.
2727 fc_remote_port_delete(struct fc_rport *rport)
2729 struct Scsi_Host *shost = rport_to_shost(rport);
2730 struct fc_internal *i = to_fc_internal(shost->transportt);
2731 int timeout = rport->dev_loss_tmo;
2732 unsigned long flags;
2735 * No need to flush the fc_host work_q's, as all adds are synchronous.
2737 * We do need to reclaim the rport scan work element, so eventually
2738 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2739 * there's still a scan pending.
2742 spin_lock_irqsave(shost->host_lock, flags);
2744 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2745 spin_unlock_irqrestore(shost->host_lock, flags);
2750 * In the past, we if this was not an FCP-Target, we would
2751 * unconditionally just jump to deleting the rport.
2752 * However, rports can be used as node containers by the LLDD,
2753 * and its not appropriate to just terminate the rport at the
2754 * first sign of a loss in connectivity. The LLDD may want to
2755 * send ELS traffic to re-validate the login. If the rport is
2756 * immediately deleted, it makes it inappropriate for a node
2758 * So... we now unconditionally wait dev_loss_tmo before
2759 * destroying an rport.
2762 rport->port_state = FC_PORTSTATE_BLOCKED;
2764 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2766 spin_unlock_irqrestore(shost->host_lock, flags);
2768 scsi_target_block(&rport->dev);
2770 /* see if we need to kill io faster than waiting for device loss */
2771 if ((rport->fast_io_fail_tmo != -1) &&
2772 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2773 fc_queue_devloss_work(shost, &rport->fail_io_work,
2774 rport->fast_io_fail_tmo * HZ);
2776 /* cap the length the devices can be blocked until they are deleted */
2777 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2779 EXPORT_SYMBOL(fc_remote_port_delete);
2782 * fc_remote_port_rolechg - notifies the fc transport that the roles
2783 * on a remote may have changed.
2784 * @rport: The remote port that changed.
2786 * The LLDD calls this routine to notify the transport that the roles
2787 * on a remote port may have changed. The largest effect of this is
2788 * if a port now becomes a FCP Target, it must be allocated a
2789 * scsi target id. If the port is no longer a FCP target, any
2790 * scsi target id value assigned to it will persist in case the
2791 * role changes back to include FCP Target. No changes in the scsi
2792 * midlayer will be invoked if the role changes (in the expectation
2793 * that the role will be resumed. If it doesn't normal error processing
2796 * Should not be called from interrupt context.
2799 * This routine assumes no locks are held on entry.
2802 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2804 struct Scsi_Host *shost = rport_to_shost(rport);
2805 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2806 unsigned long flags;
2809 spin_lock_irqsave(shost->host_lock, flags);
2810 if (roles & FC_PORT_ROLE_FCP_TARGET) {
2811 if (rport->scsi_target_id == -1) {
2812 rport->scsi_target_id = fc_host->next_target_id++;
2814 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
2818 rport->roles = roles;
2820 spin_unlock_irqrestore(shost->host_lock, flags);
2824 * There may have been a delete timer running on the
2825 * port. Ensure that it is cancelled as we now know
2826 * the port is an FCP Target.
2827 * Note: we know the rport is exists and in an online
2828 * state as the LLDD would not have had an rport
2829 * reference to pass us.
2831 * Take no action on the del_timer failure as the state
2832 * machine state change will validate the
2835 if (!cancel_delayed_work(&rport->fail_io_work))
2836 fc_flush_devloss(shost);
2837 if (!cancel_delayed_work(&rport->dev_loss_work))
2838 fc_flush_devloss(shost);
2840 spin_lock_irqsave(shost->host_lock, flags);
2841 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2842 spin_unlock_irqrestore(shost->host_lock, flags);
2844 /* ensure any stgt delete functions are done */
2845 fc_flush_work(shost);
2847 /* initiate a scan of the target */
2848 spin_lock_irqsave(shost->host_lock, flags);
2849 rport->flags |= FC_RPORT_SCAN_PENDING;
2850 scsi_queue_work(shost, &rport->scan_work);
2851 spin_unlock_irqrestore(shost->host_lock, flags);
2852 scsi_target_unblock(&rport->dev);
2855 EXPORT_SYMBOL(fc_remote_port_rolechg);
2858 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port,
2859 * which we blocked, and has now failed to return
2860 * in the allotted time.
2862 * @work: rport target that failed to reappear in the allotted time.
2865 fc_timeout_deleted_rport(struct work_struct *work)
2867 struct fc_rport *rport =
2868 container_of(work, struct fc_rport, dev_loss_work.work);
2869 struct Scsi_Host *shost = rport_to_shost(rport);
2870 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2871 unsigned long flags;
2873 spin_lock_irqsave(shost->host_lock, flags);
2875 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2878 * If the port is ONLINE, then it came back. If it was a SCSI
2879 * target, validate it still is. If not, tear down the
2880 * scsi_target on it.
2882 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2883 (rport->scsi_target_id != -1) &&
2884 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
2885 dev_printk(KERN_ERR, &rport->dev,
2886 "blocked FC remote port time out: no longer"
2887 " a FCP target, removing starget\n");
2888 spin_unlock_irqrestore(shost->host_lock, flags);
2889 scsi_target_unblock(&rport->dev);
2890 fc_queue_work(shost, &rport->stgt_delete_work);
2894 /* NOOP state - we're flushing workq's */
2895 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2896 spin_unlock_irqrestore(shost->host_lock, flags);
2897 dev_printk(KERN_ERR, &rport->dev,
2898 "blocked FC remote port time out: leaving"
2900 (rport->scsi_target_id != -1) ? " and starget" : "");
2904 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
2905 (rport->scsi_target_id == -1)) {
2906 list_del(&rport->peers);
2907 rport->port_state = FC_PORTSTATE_DELETED;
2908 dev_printk(KERN_ERR, &rport->dev,
2909 "blocked FC remote port time out: removing"
2911 (rport->scsi_target_id != -1) ? " and starget" : "");
2912 fc_queue_work(shost, &rport->rport_delete_work);
2913 spin_unlock_irqrestore(shost->host_lock, flags);
2917 dev_printk(KERN_ERR, &rport->dev,
2918 "blocked FC remote port time out: removing target and "
2919 "saving binding\n");
2921 list_move_tail(&rport->peers, &fc_host->rport_bindings);
2924 * Note: We do not remove or clear the hostdata area. This allows
2925 * host-specific target data to persist along with the
2926 * scsi_target_id. It's up to the host to manage it's hostdata area.
2930 * Reinitialize port attributes that may change if the port comes back.
2932 rport->maxframe_size = -1;
2933 rport->supported_classes = FC_COS_UNSPECIFIED;
2934 rport->roles = FC_PORT_ROLE_UNKNOWN;
2935 rport->port_state = FC_PORTSTATE_NOTPRESENT;
2937 /* remove the identifiers that aren't used in the consisting binding */
2938 switch (fc_host->tgtid_bind_type) {
2939 case FC_TGTID_BIND_BY_WWPN:
2940 rport->node_name = -1;
2941 rport->port_id = -1;
2943 case FC_TGTID_BIND_BY_WWNN:
2944 rport->port_name = -1;
2945 rport->port_id = -1;
2947 case FC_TGTID_BIND_BY_ID:
2948 rport->node_name = -1;
2949 rport->port_name = -1;
2951 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2956 * As this only occurs if the remote port (scsi target)
2957 * went away and didn't come back - we'll remove
2958 * all attached scsi devices.
2960 spin_unlock_irqrestore(shost->host_lock, flags);
2962 scsi_target_unblock(&rport->dev);
2963 fc_queue_work(shost, &rport->stgt_delete_work);
2967 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2968 * disconnected SCSI target.
2970 * @work: rport to terminate io on.
2972 * Notes: Only requests the failure of the io, not that all are flushed
2973 * prior to returning.
2976 fc_timeout_fail_rport_io(struct work_struct *work)
2978 struct fc_rport *rport =
2979 container_of(work, struct fc_rport, fail_io_work.work);
2980 struct Scsi_Host *shost = rport_to_shost(rport);
2981 struct fc_internal *i = to_fc_internal(shost->transportt);
2983 if (rport->port_state != FC_PORTSTATE_BLOCKED)
2986 i->f->terminate_rport_io(rport);
2990 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2992 * @work: remote port to be scanned.
2995 fc_scsi_scan_rport(struct work_struct *work)
2997 struct fc_rport *rport =
2998 container_of(work, struct fc_rport, scan_work);
2999 struct Scsi_Host *shost = rport_to_shost(rport);
3000 unsigned long flags;
3002 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3003 (rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3004 scsi_scan_target(&rport->dev, rport->channel,
3005 rport->scsi_target_id, SCAN_WILD_CARD, 1);
3008 spin_lock_irqsave(shost->host_lock, flags);
3009 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3010 spin_unlock_irqrestore(shost->host_lock, flags);
3015 * fc_vport_create - allocates and creates a FC virtual port.
3016 * @shost: scsi host the virtual port is connected to.
3017 * @channel: Channel on shost port connected to.
3018 * @pdev: parent device for vport
3019 * @ids: The world wide names, FC4 port roles, etc for
3021 * @ret_vport: The pointer to the created vport.
3023 * Allocates and creates the vport structure, calls the parent host
3024 * to instantiate the vport, the completes w/ class and sysfs creation.
3027 * This routine assumes no locks are held on entry.
3030 fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
3031 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3033 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3034 struct fc_internal *fci = to_fc_internal(shost->transportt);
3035 struct fc_vport *vport;
3037 unsigned long flags;
3043 if ( ! fci->f->vport_create)
3046 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3047 vport = kzalloc(size, GFP_KERNEL);
3048 if (unlikely(!vport)) {
3049 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
3053 vport->vport_state = FC_VPORT_UNKNOWN;
3054 vport->vport_last_state = FC_VPORT_UNKNOWN;
3055 vport->node_name = ids->node_name;
3056 vport->port_name = ids->port_name;
3057 vport->roles = ids->roles;
3058 vport->vport_type = ids->vport_type;
3059 if (fci->f->dd_fcvport_size)
3060 vport->dd_data = &vport[1];
3061 vport->shost = shost;
3062 vport->channel = channel;
3063 vport->flags = FC_VPORT_CREATING;
3065 spin_lock_irqsave(shost->host_lock, flags);
3067 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3068 spin_unlock_irqrestore(shost->host_lock, flags);
3072 fc_host->npiv_vports_inuse++;
3073 vport->number = fc_host->next_vport_number++;
3074 list_add_tail(&vport->peers, &fc_host->vports);
3075 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3077 spin_unlock_irqrestore(shost->host_lock, flags);
3080 device_initialize(dev); /* takes self reference */
3081 dev->parent = get_device(pdev); /* takes parent reference */
3082 dev->release = fc_vport_dev_release;
3083 sprintf(dev->bus_id, "vport-%d:%d-%d",
3084 shost->host_no, channel, vport->number);
3085 transport_setup_device(dev);
3087 error = device_add(dev);
3089 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3092 transport_add_device(dev);
3093 transport_configure_device(dev);
3095 error = fci->f->vport_create(vport, ids->disable);
3097 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3098 goto delete_vport_all;
3102 * if the parent isn't the physical adapter's Scsi_Host, ensure
3103 * the Scsi_Host at least contains ia symlink to the vport.
3105 if (pdev != &shost->shost_gendev) {
3106 error = sysfs_create_link(&shost->shost_gendev.kobj,
3107 &dev->kobj, dev->bus_id);
3110 "%s: Cannot create vport symlinks for "
3112 __FUNCTION__, dev->bus_id, error);
3114 spin_lock_irqsave(shost->host_lock, flags);
3115 vport->flags &= ~FC_VPORT_CREATING;
3116 spin_unlock_irqrestore(shost->host_lock, flags);
3118 dev_printk(KERN_NOTICE, pdev,
3119 "%s created via shost%d channel %d\n", dev->bus_id,
3120 shost->host_no, channel);
3127 transport_remove_device(dev);
3130 transport_destroy_device(dev);
3131 spin_lock_irqsave(shost->host_lock, flags);
3132 list_del(&vport->peers);
3133 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3134 fc_host->npiv_vports_inuse--;
3135 spin_unlock_irqrestore(shost->host_lock, flags);
3136 put_device(dev->parent);
3144 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3145 * @vport: fc_vport to be terminated
3147 * Calls the LLDD vport_delete() function, then deallocates and removes
3148 * the vport from the shost and object tree.
3151 * This routine assumes no locks are held on entry.
3154 fc_vport_terminate(struct fc_vport *vport)
3156 struct Scsi_Host *shost = vport_to_shost(vport);
3157 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3158 struct fc_internal *i = to_fc_internal(shost->transportt);
3159 struct device *dev = &vport->dev;
3160 unsigned long flags;
3163 spin_lock_irqsave(shost->host_lock, flags);
3164 if (vport->flags & FC_VPORT_CREATING) {
3165 spin_unlock_irqrestore(shost->host_lock, flags);
3168 if (vport->flags & (FC_VPORT_DEL)) {
3169 spin_unlock_irqrestore(shost->host_lock, flags);
3172 vport->flags |= FC_VPORT_DELETING;
3173 spin_unlock_irqrestore(shost->host_lock, flags);
3175 if (i->f->vport_delete)
3176 stat = i->f->vport_delete(vport);
3180 spin_lock_irqsave(shost->host_lock, flags);
3181 vport->flags &= ~FC_VPORT_DELETING;
3183 vport->flags |= FC_VPORT_DELETED;
3184 list_del(&vport->peers);
3185 fc_host->npiv_vports_inuse--;
3186 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3188 spin_unlock_irqrestore(shost->host_lock, flags);
3193 if (dev->parent != &shost->shost_gendev)
3194 sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
3195 transport_remove_device(dev);
3197 transport_destroy_device(dev);
3200 * Removing our self-reference should mean our
3201 * release function gets called, which will drop the remaining
3202 * parent reference and free the data structure.
3204 put_device(dev); /* for self-reference */
3206 return 0; /* SUCCESS */
3208 EXPORT_SYMBOL(fc_vport_terminate);
3211 MODULE_AUTHOR("Martin Hicks");
3212 MODULE_DESCRIPTION("FC Transport Attributes");
3213 MODULE_LICENSE("GPL");
3215 module_init(fc_transport_init);
3216 module_exit(fc_transport_exit);