[MIPS] Yosemite: Fix missing parens in SERIAL_READ_1 macro
[linux-2.6/linux-loongson.git] / drivers / scsi / scsi_transport_fc.c
blob3571ce8934e7af15f8924a832c6be7047fbbecb4
1 /*
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
20 * ========
22 * Copyright (C) 2004-2005 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 <linux/sched.h> /* workqueue stuff, HZ */
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <linux/netlink.h>
36 #include <net/netlink.h>
37 #include <scsi/scsi_netlink_fc.h>
38 #include "scsi_priv.h"
40 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
43 * Redefine so that we can have same named attributes in the
44 * sdev/starget/host objects.
46 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
47 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
48 __ATTR(_name,_mode,_show,_store)
50 #define fc_enum_name_search(title, table_type, table) \
51 static const char *get_fc_##title##_name(enum table_type table_key) \
52 { \
53 int i; \
54 char *name = NULL; \
56 for (i = 0; i < ARRAY_SIZE(table); i++) { \
57 if (table[i].value == table_key) { \
58 name = table[i].name; \
59 break; \
60 } \
61 } \
62 return name; \
65 #define fc_enum_name_match(title, table_type, table) \
66 static int get_fc_##title##_match(const char *table_key, \
67 enum table_type *value) \
68 { \
69 int i; \
71 for (i = 0; i < ARRAY_SIZE(table); i++) { \
72 if (strncmp(table_key, table[i].name, \
73 table[i].matchlen) == 0) { \
74 *value = table[i].value; \
75 return 0; /* success */ \
76 } \
77 } \
78 return 1; /* failure */ \
82 /* Convert fc_port_type values to ascii string name */
83 static struct {
84 enum fc_port_type value;
85 char *name;
86 } fc_port_type_names[] = {
87 { FC_PORTTYPE_UNKNOWN, "Unknown" },
88 { FC_PORTTYPE_OTHER, "Other" },
89 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
90 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
91 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
92 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
93 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
95 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
96 #define FC_PORTTYPE_MAX_NAMELEN 50
99 /* Convert fc_host_event_code values to ascii string name */
100 static const struct {
101 enum fc_host_event_code value;
102 char *name;
103 } fc_host_event_code_names[] = {
104 { FCH_EVT_LIP, "lip" },
105 { FCH_EVT_LINKUP, "link_up" },
106 { FCH_EVT_LINKDOWN, "link_down" },
107 { FCH_EVT_LIPRESET, "lip_reset" },
108 { FCH_EVT_RSCN, "rscn" },
109 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
110 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
111 { FCH_EVT_PORT_ONLINE, "port_online" },
112 { FCH_EVT_PORT_OFFLINE, "port_offline" },
113 { FCH_EVT_PORT_FABRIC, "port_fabric" },
114 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
115 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
117 fc_enum_name_search(host_event_code, fc_host_event_code,
118 fc_host_event_code_names)
119 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
122 /* Convert fc_port_state values to ascii string name */
123 static struct {
124 enum fc_port_state value;
125 char *name;
126 } fc_port_state_names[] = {
127 { FC_PORTSTATE_UNKNOWN, "Unknown" },
128 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
129 { FC_PORTSTATE_ONLINE, "Online" },
130 { FC_PORTSTATE_OFFLINE, "Offline" },
131 { FC_PORTSTATE_BLOCKED, "Blocked" },
132 { FC_PORTSTATE_BYPASSED, "Bypassed" },
133 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
134 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
135 { FC_PORTSTATE_ERROR, "Error" },
136 { FC_PORTSTATE_LOOPBACK, "Loopback" },
137 { FC_PORTSTATE_DELETED, "Deleted" },
139 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
140 #define FC_PORTSTATE_MAX_NAMELEN 20
143 /* Convert fc_tgtid_binding_type values to ascii string name */
144 static const struct {
145 enum fc_tgtid_binding_type value;
146 char *name;
147 int matchlen;
148 } fc_tgtid_binding_type_names[] = {
149 { FC_TGTID_BIND_NONE, "none", 4 },
150 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
151 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
152 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
154 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
155 fc_tgtid_binding_type_names)
156 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
157 fc_tgtid_binding_type_names)
158 #define FC_BINDTYPE_MAX_NAMELEN 30
161 #define fc_bitfield_name_search(title, table) \
162 static ssize_t \
163 get_fc_##title##_names(u32 table_key, char *buf) \
165 char *prefix = ""; \
166 ssize_t len = 0; \
167 int i; \
169 for (i = 0; i < ARRAY_SIZE(table); i++) { \
170 if (table[i].value & table_key) { \
171 len += sprintf(buf + len, "%s%s", \
172 prefix, table[i].name); \
173 prefix = ", "; \
176 len += sprintf(buf + len, "\n"); \
177 return len; \
181 /* Convert FC_COS bit values to ascii string name */
182 static const struct {
183 u32 value;
184 char *name;
185 } fc_cos_names[] = {
186 { FC_COS_CLASS1, "Class 1" },
187 { FC_COS_CLASS2, "Class 2" },
188 { FC_COS_CLASS3, "Class 3" },
189 { FC_COS_CLASS4, "Class 4" },
190 { FC_COS_CLASS6, "Class 6" },
192 fc_bitfield_name_search(cos, fc_cos_names)
195 /* Convert FC_PORTSPEED bit values to ascii string name */
196 static const struct {
197 u32 value;
198 char *name;
199 } fc_port_speed_names[] = {
200 { FC_PORTSPEED_1GBIT, "1 Gbit" },
201 { FC_PORTSPEED_2GBIT, "2 Gbit" },
202 { FC_PORTSPEED_4GBIT, "4 Gbit" },
203 { FC_PORTSPEED_10GBIT, "10 Gbit" },
204 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
206 fc_bitfield_name_search(port_speed, fc_port_speed_names)
209 static int
210 show_fc_fc4s (char *buf, u8 *fc4_list)
212 int i, len=0;
214 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
215 len += sprintf(buf + len , "0x%02x ", *fc4_list);
216 len += sprintf(buf + len, "\n");
217 return len;
221 /* Convert FC_RPORT_ROLE bit values to ascii string name */
222 static const struct {
223 u32 value;
224 char *name;
225 } fc_remote_port_role_names[] = {
226 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
227 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
228 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
230 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
233 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
235 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
236 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
237 #define FC_FPORT_PORTID 0x00000e
238 #define FC_FABCTLR_PORTID 0x00000d
239 #define FC_DIRSRVR_PORTID 0x00000c
240 #define FC_TIMESRVR_PORTID 0x00000b
241 #define FC_MGMTSRVR_PORTID 0x00000a
244 static void fc_timeout_deleted_rport(struct work_struct *work);
245 static void fc_timeout_fail_rport_io(struct work_struct *work);
246 static void fc_scsi_scan_rport(struct work_struct *work);
249 * Attribute counts pre object type...
250 * Increase these values if you add attributes
252 #define FC_STARGET_NUM_ATTRS 3
253 #define FC_RPORT_NUM_ATTRS 10
254 #define FC_HOST_NUM_ATTRS 17
256 struct fc_internal {
257 struct scsi_transport_template t;
258 struct fc_function_template *f;
261 * For attributes : each object has :
262 * An array of the actual attributes structures
263 * An array of null-terminated pointers to the attribute
264 * structures - used for mid-layer interaction.
266 * The attribute containers for the starget and host are are
267 * part of the midlayer. As the remote port is specific to the
268 * fc transport, we must provide the attribute container.
270 struct class_device_attribute private_starget_attrs[
271 FC_STARGET_NUM_ATTRS];
272 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
274 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
275 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
277 struct transport_container rport_attr_cont;
278 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
279 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
282 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
284 static int fc_target_setup(struct transport_container *tc, struct device *dev,
285 struct class_device *cdev)
287 struct scsi_target *starget = to_scsi_target(dev);
288 struct fc_rport *rport = starget_to_rport(starget);
291 * if parent is remote port, use values from remote port.
292 * Otherwise, this host uses the fc_transport, but not the
293 * remote port interface. As such, initialize to known non-values.
295 if (rport) {
296 fc_starget_node_name(starget) = rport->node_name;
297 fc_starget_port_name(starget) = rport->port_name;
298 fc_starget_port_id(starget) = rport->port_id;
299 } else {
300 fc_starget_node_name(starget) = -1;
301 fc_starget_port_name(starget) = -1;
302 fc_starget_port_id(starget) = -1;
305 return 0;
308 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
309 "fc_transport",
310 fc_target_setup,
311 NULL,
312 NULL);
314 static int fc_host_setup(struct transport_container *tc, struct device *dev,
315 struct class_device *cdev)
317 struct Scsi_Host *shost = dev_to_shost(dev);
318 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
321 * Set default values easily detected by the midlayer as
322 * failure cases. The scsi lldd is responsible for initializing
323 * all transport attributes to valid values per host.
325 fc_host->node_name = -1;
326 fc_host->port_name = -1;
327 fc_host->permanent_port_name = -1;
328 fc_host->supported_classes = FC_COS_UNSPECIFIED;
329 memset(fc_host->supported_fc4s, 0,
330 sizeof(fc_host->supported_fc4s));
331 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
332 fc_host->maxframe_size = -1;
333 memset(fc_host->serial_number, 0,
334 sizeof(fc_host->serial_number));
336 fc_host->port_id = -1;
337 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
338 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
339 memset(fc_host->active_fc4s, 0,
340 sizeof(fc_host->active_fc4s));
341 fc_host->speed = FC_PORTSPEED_UNKNOWN;
342 fc_host->fabric_name = -1;
343 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
344 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
346 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
348 INIT_LIST_HEAD(&fc_host->rports);
349 INIT_LIST_HEAD(&fc_host->rport_bindings);
350 fc_host->next_rport_number = 0;
351 fc_host->next_target_id = 0;
353 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
354 shost->host_no);
355 fc_host->work_q = create_singlethread_workqueue(
356 fc_host->work_q_name);
357 if (!fc_host->work_q)
358 return -ENOMEM;
360 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
361 shost->host_no);
362 fc_host->devloss_work_q = create_singlethread_workqueue(
363 fc_host->devloss_work_q_name);
364 if (!fc_host->devloss_work_q) {
365 destroy_workqueue(fc_host->work_q);
366 fc_host->work_q = NULL;
367 return -ENOMEM;
370 return 0;
373 static DECLARE_TRANSPORT_CLASS(fc_host_class,
374 "fc_host",
375 fc_host_setup,
376 NULL,
377 NULL);
380 * Setup and Remove actions for remote ports are handled
381 * in the service functions below.
383 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
384 "fc_remote_ports",
385 NULL,
386 NULL,
387 NULL);
390 * Module Parameters
394 * dev_loss_tmo: the default number of seconds that the FC transport
395 * should insulate the loss of a remote port.
396 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
398 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
400 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
401 MODULE_PARM_DESC(dev_loss_tmo,
402 "Maximum number of seconds that the FC transport should"
403 " insulate the loss of a remote port. Once this value is"
404 " exceeded, the scsi target is removed. Value should be"
405 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
408 * Netlink Infrastructure
411 static atomic_t fc_event_seq;
414 * fc_get_event_number - Obtain the next sequential FC event number
416 * Notes:
417 * We could have inline'd this, but it would have required fc_event_seq to
418 * be exposed. For now, live with the subroutine call.
419 * Atomic used to avoid lock/unlock...
422 fc_get_event_number(void)
424 return atomic_add_return(1, &fc_event_seq);
426 EXPORT_SYMBOL(fc_get_event_number);
430 * fc_host_post_event - called to post an even on an fc_host.
432 * @shost: host the event occurred on
433 * @event_number: fc event number obtained from get_fc_event_number()
434 * @event_code: fc_host event being posted
435 * @event_data: 32bits of data for the event being posted
437 * Notes:
438 * This routine assumes no locks are held on entry.
440 void
441 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
442 enum fc_host_event_code event_code, u32 event_data)
444 struct sk_buff *skb;
445 struct nlmsghdr *nlh;
446 struct fc_nl_event *event;
447 const char *name;
448 u32 len, skblen;
449 int err;
451 if (!scsi_nl_sock) {
452 err = -ENOENT;
453 goto send_fail;
456 len = FC_NL_MSGALIGN(sizeof(*event));
457 skblen = NLMSG_SPACE(len);
459 skb = alloc_skb(skblen, GFP_KERNEL);
460 if (!skb) {
461 err = -ENOBUFS;
462 goto send_fail;
465 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
466 skblen - sizeof(*nlh), 0);
467 if (!nlh) {
468 err = -ENOBUFS;
469 goto send_fail_skb;
471 event = NLMSG_DATA(nlh);
473 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
474 FC_NL_ASYNC_EVENT, len);
475 event->seconds = get_seconds();
476 event->vendor_id = 0;
477 event->host_no = shost->host_no;
478 event->event_datalen = sizeof(u32); /* bytes */
479 event->event_num = event_number;
480 event->event_code = event_code;
481 event->event_data = event_data;
483 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
484 GFP_KERNEL);
485 if (err && (err != -ESRCH)) /* filter no recipient errors */
486 /* nlmsg_multicast already kfree_skb'd */
487 goto send_fail;
489 return;
491 send_fail_skb:
492 kfree_skb(skb);
493 send_fail:
494 name = get_fc_host_event_code_name(event_code);
495 printk(KERN_WARNING
496 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
497 __FUNCTION__, shost->host_no,
498 (name) ? name : "<unknown>", event_data, err);
499 return;
501 EXPORT_SYMBOL(fc_host_post_event);
505 * fc_host_post_vendor_event - called to post a vendor unique event on
506 * a fc_host
508 * @shost: host the event occurred on
509 * @event_number: fc event number obtained from get_fc_event_number()
510 * @data_len: amount, in bytes, of vendor unique data
511 * @data_buf: pointer to vendor unique data
513 * Notes:
514 * This routine assumes no locks are held on entry.
516 void
517 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
518 u32 data_len, char * data_buf, u64 vendor_id)
520 struct sk_buff *skb;
521 struct nlmsghdr *nlh;
522 struct fc_nl_event *event;
523 u32 len, skblen;
524 int err;
526 if (!scsi_nl_sock) {
527 err = -ENOENT;
528 goto send_vendor_fail;
531 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
532 skblen = NLMSG_SPACE(len);
534 skb = alloc_skb(skblen, GFP_KERNEL);
535 if (!skb) {
536 err = -ENOBUFS;
537 goto send_vendor_fail;
540 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
541 skblen - sizeof(*nlh), 0);
542 if (!nlh) {
543 err = -ENOBUFS;
544 goto send_vendor_fail_skb;
546 event = NLMSG_DATA(nlh);
548 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
549 FC_NL_ASYNC_EVENT, len);
550 event->seconds = get_seconds();
551 event->vendor_id = vendor_id;
552 event->host_no = shost->host_no;
553 event->event_datalen = data_len; /* bytes */
554 event->event_num = event_number;
555 event->event_code = FCH_EVT_VENDOR_UNIQUE;
556 memcpy(&event->event_data, data_buf, data_len);
558 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
559 GFP_KERNEL);
560 if (err && (err != -ESRCH)) /* filter no recipient errors */
561 /* nlmsg_multicast already kfree_skb'd */
562 goto send_vendor_fail;
564 return;
566 send_vendor_fail_skb:
567 kfree_skb(skb);
568 send_vendor_fail:
569 printk(KERN_WARNING
570 "%s: Dropped Event : host %d vendor_unique - err %d\n",
571 __FUNCTION__, shost->host_no, err);
572 return;
574 EXPORT_SYMBOL(fc_host_post_vendor_event);
578 static __init int fc_transport_init(void)
580 int error;
582 atomic_set(&fc_event_seq, 0);
584 error = transport_class_register(&fc_host_class);
585 if (error)
586 return error;
587 error = transport_class_register(&fc_rport_class);
588 if (error)
589 return error;
590 return transport_class_register(&fc_transport_class);
593 static void __exit fc_transport_exit(void)
595 transport_class_unregister(&fc_transport_class);
596 transport_class_unregister(&fc_rport_class);
597 transport_class_unregister(&fc_host_class);
601 * FC Remote Port Attribute Management
604 #define fc_rport_show_function(field, format_string, sz, cast) \
605 static ssize_t \
606 show_fc_rport_##field (struct class_device *cdev, char *buf) \
608 struct fc_rport *rport = transport_class_to_rport(cdev); \
609 struct Scsi_Host *shost = rport_to_shost(rport); \
610 struct fc_internal *i = to_fc_internal(shost->transportt); \
611 if ((i->f->get_rport_##field) && \
612 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
613 (rport->port_state == FC_PORTSTATE_DELETED) || \
614 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
615 i->f->get_rport_##field(rport); \
616 return snprintf(buf, sz, format_string, cast rport->field); \
619 #define fc_rport_store_function(field) \
620 static ssize_t \
621 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
622 size_t count) \
624 int val; \
625 struct fc_rport *rport = transport_class_to_rport(cdev); \
626 struct Scsi_Host *shost = rport_to_shost(rport); \
627 struct fc_internal *i = to_fc_internal(shost->transportt); \
628 char *cp; \
629 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
630 (rport->port_state == FC_PORTSTATE_DELETED) || \
631 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
632 return -EBUSY; \
633 val = simple_strtoul(buf, &cp, 0); \
634 if (*cp && (*cp != '\n')) \
635 return -EINVAL; \
636 i->f->set_rport_##field(rport, val); \
637 return count; \
640 #define fc_rport_rd_attr(field, format_string, sz) \
641 fc_rport_show_function(field, format_string, sz, ) \
642 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
643 show_fc_rport_##field, NULL)
645 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
646 fc_rport_show_function(field, format_string, sz, (cast)) \
647 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
648 show_fc_rport_##field, NULL)
650 #define fc_rport_rw_attr(field, format_string, sz) \
651 fc_rport_show_function(field, format_string, sz, ) \
652 fc_rport_store_function(field) \
653 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
654 show_fc_rport_##field, \
655 store_fc_rport_##field)
658 #define fc_private_rport_show_function(field, format_string, sz, cast) \
659 static ssize_t \
660 show_fc_rport_##field (struct class_device *cdev, char *buf) \
662 struct fc_rport *rport = transport_class_to_rport(cdev); \
663 return snprintf(buf, sz, format_string, cast rport->field); \
666 #define fc_private_rport_rd_attr(field, format_string, sz) \
667 fc_private_rport_show_function(field, format_string, sz, ) \
668 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
669 show_fc_rport_##field, NULL)
671 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
672 fc_private_rport_show_function(field, format_string, sz, (cast)) \
673 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
674 show_fc_rport_##field, NULL)
677 #define fc_private_rport_rd_enum_attr(title, maxlen) \
678 static ssize_t \
679 show_fc_rport_##title (struct class_device *cdev, char *buf) \
681 struct fc_rport *rport = transport_class_to_rport(cdev); \
682 const char *name; \
683 name = get_fc_##title##_name(rport->title); \
684 if (!name) \
685 return -EINVAL; \
686 return snprintf(buf, maxlen, "%s\n", name); \
688 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
689 show_fc_rport_##title, NULL)
692 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
693 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
694 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
695 i->private_rport_attrs[count].store = NULL; \
696 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
697 if (i->f->show_rport_##field) \
698 count++
700 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
701 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
702 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
703 i->private_rport_attrs[count].store = NULL; \
704 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
705 count++
707 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
708 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
709 if (!i->f->set_rport_##field) { \
710 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
711 i->private_rport_attrs[count].store = NULL; \
713 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
714 if (i->f->show_rport_##field) \
715 count++
717 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
719 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
720 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
721 count++; \
725 /* The FC Transport Remote Port Attributes: */
727 /* Fixed Remote Port Attributes */
729 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
731 static ssize_t
732 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
734 struct fc_rport *rport = transport_class_to_rport(cdev);
735 if (rport->supported_classes == FC_COS_UNSPECIFIED)
736 return snprintf(buf, 20, "unspecified\n");
737 return get_fc_cos_names(rport->supported_classes, buf);
739 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
740 show_fc_rport_supported_classes, NULL);
742 /* Dynamic Remote Port Attributes */
745 * dev_loss_tmo attribute
747 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
748 static ssize_t
749 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
750 size_t count)
752 int val;
753 struct fc_rport *rport = transport_class_to_rport(cdev);
754 struct Scsi_Host *shost = rport_to_shost(rport);
755 struct fc_internal *i = to_fc_internal(shost->transportt);
756 char *cp;
757 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
758 (rport->port_state == FC_PORTSTATE_DELETED) ||
759 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
760 return -EBUSY;
761 val = simple_strtoul(buf, &cp, 0);
762 if ((*cp && (*cp != '\n')) ||
763 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
764 return -EINVAL;
765 i->f->set_rport_dev_loss_tmo(rport, val);
766 return count;
768 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
769 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
772 /* Private Remote Port Attributes */
774 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
775 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
776 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
778 static ssize_t
779 show_fc_rport_roles (struct class_device *cdev, char *buf)
781 struct fc_rport *rport = transport_class_to_rport(cdev);
783 /* identify any roles that are port_id specific */
784 if ((rport->port_id != -1) &&
785 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
786 FC_WELLKNOWN_PORTID_MASK) {
787 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
788 case FC_FPORT_PORTID:
789 return snprintf(buf, 30, "Fabric Port\n");
790 case FC_FABCTLR_PORTID:
791 return snprintf(buf, 30, "Fabric Controller\n");
792 case FC_DIRSRVR_PORTID:
793 return snprintf(buf, 30, "Directory Server\n");
794 case FC_TIMESRVR_PORTID:
795 return snprintf(buf, 30, "Time Server\n");
796 case FC_MGMTSRVR_PORTID:
797 return snprintf(buf, 30, "Management Server\n");
798 default:
799 return snprintf(buf, 30, "Unknown Fabric Entity\n");
801 } else {
802 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
803 return snprintf(buf, 20, "unknown\n");
804 return get_fc_remote_port_roles_names(rport->roles, buf);
807 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
808 show_fc_rport_roles, NULL);
810 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
811 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
814 * fast_io_fail_tmo attribute
816 static ssize_t
817 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
819 struct fc_rport *rport = transport_class_to_rport(cdev);
821 if (rport->fast_io_fail_tmo == -1)
822 return snprintf(buf, 5, "off\n");
823 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
826 static ssize_t
827 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
828 size_t count)
830 int val;
831 char *cp;
832 struct fc_rport *rport = transport_class_to_rport(cdev);
834 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
835 (rport->port_state == FC_PORTSTATE_DELETED) ||
836 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
837 return -EBUSY;
838 if (strncmp(buf, "off", 3) == 0)
839 rport->fast_io_fail_tmo = -1;
840 else {
841 val = simple_strtoul(buf, &cp, 0);
842 if ((*cp && (*cp != '\n')) ||
843 (val < 0) || (val >= rport->dev_loss_tmo))
844 return -EINVAL;
845 rport->fast_io_fail_tmo = val;
847 return count;
849 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
850 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
854 * FC SCSI Target Attribute Management
858 * Note: in the target show function we recognize when the remote
859 * port is in the heirarchy and do not allow the driver to get
860 * involved in sysfs functions. The driver only gets involved if
861 * it's the "old" style that doesn't use rports.
863 #define fc_starget_show_function(field, format_string, sz, cast) \
864 static ssize_t \
865 show_fc_starget_##field (struct class_device *cdev, char *buf) \
867 struct scsi_target *starget = transport_class_to_starget(cdev); \
868 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
869 struct fc_internal *i = to_fc_internal(shost->transportt); \
870 struct fc_rport *rport = starget_to_rport(starget); \
871 if (rport) \
872 fc_starget_##field(starget) = rport->field; \
873 else if (i->f->get_starget_##field) \
874 i->f->get_starget_##field(starget); \
875 return snprintf(buf, sz, format_string, \
876 cast fc_starget_##field(starget)); \
879 #define fc_starget_rd_attr(field, format_string, sz) \
880 fc_starget_show_function(field, format_string, sz, ) \
881 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
882 show_fc_starget_##field, NULL)
884 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
885 fc_starget_show_function(field, format_string, sz, (cast)) \
886 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
887 show_fc_starget_##field, NULL)
889 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
890 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
891 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
892 i->private_starget_attrs[count].store = NULL; \
893 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
894 if (i->f->show_starget_##field) \
895 count++
897 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
898 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
899 if (!i->f->set_starget_##field) { \
900 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
901 i->private_starget_attrs[count].store = NULL; \
903 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
904 if (i->f->show_starget_##field) \
905 count++
907 /* The FC Transport SCSI Target Attributes: */
908 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
909 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
910 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
914 * Host Attribute Management
917 #define fc_host_show_function(field, format_string, sz, cast) \
918 static ssize_t \
919 show_fc_host_##field (struct class_device *cdev, char *buf) \
921 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
922 struct fc_internal *i = to_fc_internal(shost->transportt); \
923 if (i->f->get_host_##field) \
924 i->f->get_host_##field(shost); \
925 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
928 #define fc_host_store_function(field) \
929 static ssize_t \
930 store_fc_host_##field(struct class_device *cdev, const char *buf, \
931 size_t count) \
933 int val; \
934 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
935 struct fc_internal *i = to_fc_internal(shost->transportt); \
936 char *cp; \
938 val = simple_strtoul(buf, &cp, 0); \
939 if (*cp && (*cp != '\n')) \
940 return -EINVAL; \
941 i->f->set_host_##field(shost, val); \
942 return count; \
945 #define fc_host_store_str_function(field, slen) \
946 static ssize_t \
947 store_fc_host_##field(struct class_device *cdev, const char *buf, \
948 size_t count) \
950 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
951 struct fc_internal *i = to_fc_internal(shost->transportt); \
952 unsigned int cnt=count; \
954 /* count may include a LF at end of string */ \
955 if (buf[cnt-1] == '\n') \
956 cnt--; \
957 if (cnt > ((slen) - 1)) \
958 return -EINVAL; \
959 memcpy(fc_host_##field(shost), buf, cnt); \
960 i->f->set_host_##field(shost); \
961 return count; \
964 #define fc_host_rd_attr(field, format_string, sz) \
965 fc_host_show_function(field, format_string, sz, ) \
966 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
967 show_fc_host_##field, NULL)
969 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
970 fc_host_show_function(field, format_string, sz, (cast)) \
971 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
972 show_fc_host_##field, NULL)
974 #define fc_host_rw_attr(field, format_string, sz) \
975 fc_host_show_function(field, format_string, sz, ) \
976 fc_host_store_function(field) \
977 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
978 show_fc_host_##field, \
979 store_fc_host_##field)
981 #define fc_host_rd_enum_attr(title, maxlen) \
982 static ssize_t \
983 show_fc_host_##title (struct class_device *cdev, char *buf) \
985 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
986 struct fc_internal *i = to_fc_internal(shost->transportt); \
987 const char *name; \
988 if (i->f->get_host_##title) \
989 i->f->get_host_##title(shost); \
990 name = get_fc_##title##_name(fc_host_##title(shost)); \
991 if (!name) \
992 return -EINVAL; \
993 return snprintf(buf, maxlen, "%s\n", name); \
995 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
997 #define SETUP_HOST_ATTRIBUTE_RD(field) \
998 i->private_host_attrs[count] = class_device_attr_host_##field; \
999 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1000 i->private_host_attrs[count].store = NULL; \
1001 i->host_attrs[count] = &i->private_host_attrs[count]; \
1002 if (i->f->show_host_##field) \
1003 count++
1005 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1006 i->private_host_attrs[count] = class_device_attr_host_##field; \
1007 if (!i->f->set_host_##field) { \
1008 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1009 i->private_host_attrs[count].store = NULL; \
1011 i->host_attrs[count] = &i->private_host_attrs[count]; \
1012 if (i->f->show_host_##field) \
1013 count++
1016 #define fc_private_host_show_function(field, format_string, sz, cast) \
1017 static ssize_t \
1018 show_fc_host_##field (struct class_device *cdev, char *buf) \
1020 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1021 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1024 #define fc_private_host_rd_attr(field, format_string, sz) \
1025 fc_private_host_show_function(field, format_string, sz, ) \
1026 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1027 show_fc_host_##field, NULL)
1029 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1030 fc_private_host_show_function(field, format_string, sz, (cast)) \
1031 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1032 show_fc_host_##field, NULL)
1034 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1035 i->private_host_attrs[count] = class_device_attr_host_##field; \
1036 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1037 i->private_host_attrs[count].store = NULL; \
1038 i->host_attrs[count] = &i->private_host_attrs[count]; \
1039 count++
1041 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1043 i->private_host_attrs[count] = class_device_attr_host_##field; \
1044 i->host_attrs[count] = &i->private_host_attrs[count]; \
1045 count++; \
1049 /* Fixed Host Attributes */
1051 static ssize_t
1052 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1054 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1056 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1057 return snprintf(buf, 20, "unspecified\n");
1059 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1061 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1062 show_fc_host_supported_classes, NULL);
1064 static ssize_t
1065 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1067 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1068 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1070 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1071 show_fc_host_supported_fc4s, NULL);
1073 static ssize_t
1074 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1076 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1078 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1079 return snprintf(buf, 20, "unknown\n");
1081 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1083 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1084 show_fc_host_supported_speeds, NULL);
1087 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1088 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1089 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1090 unsigned long long);
1091 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1092 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1095 /* Dynamic Host Attributes */
1097 static ssize_t
1098 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1100 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1101 struct fc_internal *i = to_fc_internal(shost->transportt);
1103 if (i->f->get_host_active_fc4s)
1104 i->f->get_host_active_fc4s(shost);
1106 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1108 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1109 show_fc_host_active_fc4s, NULL);
1111 static ssize_t
1112 show_fc_host_speed (struct class_device *cdev, char *buf)
1114 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1115 struct fc_internal *i = to_fc_internal(shost->transportt);
1117 if (i->f->get_host_speed)
1118 i->f->get_host_speed(shost);
1120 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1121 return snprintf(buf, 20, "unknown\n");
1123 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1125 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1126 show_fc_host_speed, NULL);
1129 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1130 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1131 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1132 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1133 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1135 fc_private_host_show_function(system_hostname, "%s\n",
1136 FC_SYMBOLIC_NAME_SIZE + 1, )
1137 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1138 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1139 show_fc_host_system_hostname, store_fc_host_system_hostname);
1142 /* Private Host Attributes */
1144 static ssize_t
1145 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1147 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1148 const char *name;
1150 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1151 if (!name)
1152 return -EINVAL;
1153 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1156 #define get_list_head_entry(pos, head, member) \
1157 pos = list_entry((head)->next, typeof(*pos), member)
1159 static ssize_t
1160 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1161 const char *buf, size_t count)
1163 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1164 struct fc_rport *rport;
1165 enum fc_tgtid_binding_type val;
1166 unsigned long flags;
1168 if (get_fc_tgtid_bind_type_match(buf, &val))
1169 return -EINVAL;
1171 /* if changing bind type, purge all unused consistent bindings */
1172 if (val != fc_host_tgtid_bind_type(shost)) {
1173 spin_lock_irqsave(shost->host_lock, flags);
1174 while (!list_empty(&fc_host_rport_bindings(shost))) {
1175 get_list_head_entry(rport,
1176 &fc_host_rport_bindings(shost), peers);
1177 list_del(&rport->peers);
1178 rport->port_state = FC_PORTSTATE_DELETED;
1179 fc_queue_work(shost, &rport->rport_delete_work);
1181 spin_unlock_irqrestore(shost->host_lock, flags);
1184 fc_host_tgtid_bind_type(shost) = val;
1185 return count;
1188 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1189 show_fc_private_host_tgtid_bind_type,
1190 store_fc_private_host_tgtid_bind_type);
1192 static ssize_t
1193 store_fc_private_host_issue_lip(struct class_device *cdev,
1194 const char *buf, size_t count)
1196 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1197 struct fc_internal *i = to_fc_internal(shost->transportt);
1198 int ret;
1200 /* ignore any data value written to the attribute */
1201 if (i->f->issue_fc_host_lip) {
1202 ret = i->f->issue_fc_host_lip(shost);
1203 return ret ? ret: count;
1206 return -ENOENT;
1209 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1210 store_fc_private_host_issue_lip);
1213 * Host Statistics Management
1216 /* Show a given an attribute in the statistics group */
1217 static ssize_t
1218 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1220 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1221 struct fc_internal *i = to_fc_internal(shost->transportt);
1222 struct fc_host_statistics *stats;
1223 ssize_t ret = -ENOENT;
1225 if (offset > sizeof(struct fc_host_statistics) ||
1226 offset % sizeof(u64) != 0)
1227 WARN_ON(1);
1229 if (i->f->get_fc_host_stats) {
1230 stats = (i->f->get_fc_host_stats)(shost);
1231 if (stats)
1232 ret = snprintf(buf, 20, "0x%llx\n",
1233 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1235 return ret;
1239 /* generate a read-only statistics attribute */
1240 #define fc_host_statistic(name) \
1241 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
1243 return fc_stat_show(cd, buf, \
1244 offsetof(struct fc_host_statistics, name)); \
1246 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1248 fc_host_statistic(seconds_since_last_reset);
1249 fc_host_statistic(tx_frames);
1250 fc_host_statistic(tx_words);
1251 fc_host_statistic(rx_frames);
1252 fc_host_statistic(rx_words);
1253 fc_host_statistic(lip_count);
1254 fc_host_statistic(nos_count);
1255 fc_host_statistic(error_frames);
1256 fc_host_statistic(dumped_frames);
1257 fc_host_statistic(link_failure_count);
1258 fc_host_statistic(loss_of_sync_count);
1259 fc_host_statistic(loss_of_signal_count);
1260 fc_host_statistic(prim_seq_protocol_err_count);
1261 fc_host_statistic(invalid_tx_word_count);
1262 fc_host_statistic(invalid_crc_count);
1263 fc_host_statistic(fcp_input_requests);
1264 fc_host_statistic(fcp_output_requests);
1265 fc_host_statistic(fcp_control_requests);
1266 fc_host_statistic(fcp_input_megabytes);
1267 fc_host_statistic(fcp_output_megabytes);
1269 static ssize_t
1270 fc_reset_statistics(struct class_device *cdev, const char *buf,
1271 size_t count)
1273 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1274 struct fc_internal *i = to_fc_internal(shost->transportt);
1276 /* ignore any data value written to the attribute */
1277 if (i->f->reset_fc_host_stats) {
1278 i->f->reset_fc_host_stats(shost);
1279 return count;
1282 return -ENOENT;
1284 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1285 fc_reset_statistics);
1288 static struct attribute *fc_statistics_attrs[] = {
1289 &class_device_attr_host_seconds_since_last_reset.attr,
1290 &class_device_attr_host_tx_frames.attr,
1291 &class_device_attr_host_tx_words.attr,
1292 &class_device_attr_host_rx_frames.attr,
1293 &class_device_attr_host_rx_words.attr,
1294 &class_device_attr_host_lip_count.attr,
1295 &class_device_attr_host_nos_count.attr,
1296 &class_device_attr_host_error_frames.attr,
1297 &class_device_attr_host_dumped_frames.attr,
1298 &class_device_attr_host_link_failure_count.attr,
1299 &class_device_attr_host_loss_of_sync_count.attr,
1300 &class_device_attr_host_loss_of_signal_count.attr,
1301 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1302 &class_device_attr_host_invalid_tx_word_count.attr,
1303 &class_device_attr_host_invalid_crc_count.attr,
1304 &class_device_attr_host_fcp_input_requests.attr,
1305 &class_device_attr_host_fcp_output_requests.attr,
1306 &class_device_attr_host_fcp_control_requests.attr,
1307 &class_device_attr_host_fcp_input_megabytes.attr,
1308 &class_device_attr_host_fcp_output_megabytes.attr,
1309 &class_device_attr_host_reset_statistics.attr,
1310 NULL
1313 static struct attribute_group fc_statistics_group = {
1314 .name = "statistics",
1315 .attrs = fc_statistics_attrs,
1318 static int fc_host_match(struct attribute_container *cont,
1319 struct device *dev)
1321 struct Scsi_Host *shost;
1322 struct fc_internal *i;
1324 if (!scsi_is_host_device(dev))
1325 return 0;
1327 shost = dev_to_shost(dev);
1328 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1329 != &fc_host_class.class)
1330 return 0;
1332 i = to_fc_internal(shost->transportt);
1334 return &i->t.host_attrs.ac == cont;
1337 static int fc_target_match(struct attribute_container *cont,
1338 struct device *dev)
1340 struct Scsi_Host *shost;
1341 struct fc_internal *i;
1343 if (!scsi_is_target_device(dev))
1344 return 0;
1346 shost = dev_to_shost(dev->parent);
1347 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1348 != &fc_host_class.class)
1349 return 0;
1351 i = to_fc_internal(shost->transportt);
1353 return &i->t.target_attrs.ac == cont;
1356 static void fc_rport_dev_release(struct device *dev)
1358 struct fc_rport *rport = dev_to_rport(dev);
1359 put_device(dev->parent);
1360 kfree(rport);
1363 int scsi_is_fc_rport(const struct device *dev)
1365 return dev->release == fc_rport_dev_release;
1367 EXPORT_SYMBOL(scsi_is_fc_rport);
1369 static int fc_rport_match(struct attribute_container *cont,
1370 struct device *dev)
1372 struct Scsi_Host *shost;
1373 struct fc_internal *i;
1375 if (!scsi_is_fc_rport(dev))
1376 return 0;
1378 shost = dev_to_shost(dev->parent);
1379 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1380 != &fc_host_class.class)
1381 return 0;
1383 i = to_fc_internal(shost->transportt);
1385 return &i->rport_attr_cont.ac == cont;
1390 * fc_timed_out - FC Transport I/O timeout intercept handler
1392 * @scmd: The SCSI command which timed out
1394 * This routine protects against error handlers getting invoked while a
1395 * rport is in a blocked state, typically due to a temporarily loss of
1396 * connectivity. If the error handlers are allowed to proceed, requests
1397 * to abort i/o, reset the target, etc will likely fail as there is no way
1398 * to communicate with the device to perform the requested function. These
1399 * failures may result in the midlayer taking the device offline, requiring
1400 * manual intervention to restore operation.
1402 * This routine, called whenever an i/o times out, validates the state of
1403 * the underlying rport. If the rport is blocked, it returns
1404 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1405 * Eventually, either the device will return, or devloss_tmo will fire,
1406 * and when the timeout then fires, it will be handled normally.
1407 * If the rport is not blocked, normal error handling continues.
1409 * Notes:
1410 * This routine assumes no locks are held on entry.
1412 static enum scsi_eh_timer_return
1413 fc_timed_out(struct scsi_cmnd *scmd)
1415 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1417 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1418 return EH_RESET_TIMER;
1420 return EH_NOT_HANDLED;
1424 * Must be called with shost->host_lock held
1426 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1427 uint id, uint lun)
1429 struct fc_rport *rport;
1431 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1432 if (rport->scsi_target_id == -1)
1433 continue;
1435 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1436 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1437 scsi_scan_target(&rport->dev, rport->channel,
1438 rport->scsi_target_id, lun, 1);
1442 return 0;
1445 struct scsi_transport_template *
1446 fc_attach_transport(struct fc_function_template *ft)
1448 int count;
1449 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1450 GFP_KERNEL);
1452 if (unlikely(!i))
1453 return NULL;
1455 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1456 i->t.target_attrs.ac.class = &fc_transport_class.class;
1457 i->t.target_attrs.ac.match = fc_target_match;
1458 i->t.target_size = sizeof(struct fc_starget_attrs);
1459 transport_container_register(&i->t.target_attrs);
1461 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1462 i->t.host_attrs.ac.class = &fc_host_class.class;
1463 i->t.host_attrs.ac.match = fc_host_match;
1464 i->t.host_size = sizeof(struct fc_host_attrs);
1465 if (ft->get_fc_host_stats)
1466 i->t.host_attrs.statistics = &fc_statistics_group;
1467 transport_container_register(&i->t.host_attrs);
1469 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1470 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1471 i->rport_attr_cont.ac.match = fc_rport_match;
1472 transport_container_register(&i->rport_attr_cont);
1474 i->f = ft;
1476 /* Transport uses the shost workq for scsi scanning */
1477 i->t.create_work_queue = 1;
1479 i->t.eh_timed_out = fc_timed_out;
1481 i->t.user_scan = fc_user_scan;
1484 * Setup SCSI Target Attributes.
1486 count = 0;
1487 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1488 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1489 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1491 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1493 i->starget_attrs[count] = NULL;
1497 * Setup SCSI Host Attributes.
1499 count=0;
1500 SETUP_HOST_ATTRIBUTE_RD(node_name);
1501 SETUP_HOST_ATTRIBUTE_RD(port_name);
1502 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1503 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1504 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1505 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1506 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1507 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1509 SETUP_HOST_ATTRIBUTE_RD(port_id);
1510 SETUP_HOST_ATTRIBUTE_RD(port_type);
1511 SETUP_HOST_ATTRIBUTE_RD(port_state);
1512 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1513 SETUP_HOST_ATTRIBUTE_RD(speed);
1514 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1515 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1516 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1518 /* Transport-managed attributes */
1519 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1520 if (ft->issue_fc_host_lip)
1521 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1523 BUG_ON(count > FC_HOST_NUM_ATTRS);
1525 i->host_attrs[count] = NULL;
1528 * Setup Remote Port Attributes.
1530 count=0;
1531 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1532 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1533 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1534 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1535 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1536 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1537 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1538 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1539 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1540 if (ft->terminate_rport_io)
1541 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1543 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1545 i->rport_attrs[count] = NULL;
1547 return &i->t;
1549 EXPORT_SYMBOL(fc_attach_transport);
1551 void fc_release_transport(struct scsi_transport_template *t)
1553 struct fc_internal *i = to_fc_internal(t);
1555 transport_container_unregister(&i->t.target_attrs);
1556 transport_container_unregister(&i->t.host_attrs);
1557 transport_container_unregister(&i->rport_attr_cont);
1559 kfree(i);
1561 EXPORT_SYMBOL(fc_release_transport);
1564 * fc_queue_work - Queue work to the fc_host workqueue.
1565 * @shost: Pointer to Scsi_Host bound to fc_host.
1566 * @work: Work to queue for execution.
1568 * Return value:
1569 * 1 - work queued for execution
1570 * 0 - work is already queued
1571 * -EINVAL - work queue doesn't exist
1573 static int
1574 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1576 if (unlikely(!fc_host_work_q(shost))) {
1577 printk(KERN_ERR
1578 "ERROR: FC host '%s' attempted to queue work, "
1579 "when no workqueue created.\n", shost->hostt->name);
1580 dump_stack();
1582 return -EINVAL;
1585 return queue_work(fc_host_work_q(shost), work);
1589 * fc_flush_work - Flush a fc_host's workqueue.
1590 * @shost: Pointer to Scsi_Host bound to fc_host.
1592 static void
1593 fc_flush_work(struct Scsi_Host *shost)
1595 if (!fc_host_work_q(shost)) {
1596 printk(KERN_ERR
1597 "ERROR: FC host '%s' attempted to flush work, "
1598 "when no workqueue created.\n", shost->hostt->name);
1599 dump_stack();
1600 return;
1603 flush_workqueue(fc_host_work_q(shost));
1607 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1608 * @shost: Pointer to Scsi_Host bound to fc_host.
1609 * @work: Work to queue for execution.
1610 * @delay: jiffies to delay the work queuing
1612 * Return value:
1613 * 1 on success / 0 already queued / < 0 for error
1615 static int
1616 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
1617 unsigned long delay)
1619 if (unlikely(!fc_host_devloss_work_q(shost))) {
1620 printk(KERN_ERR
1621 "ERROR: FC host '%s' attempted to queue work, "
1622 "when no workqueue created.\n", shost->hostt->name);
1623 dump_stack();
1625 return -EINVAL;
1628 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
1632 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
1633 * @shost: Pointer to Scsi_Host bound to fc_host.
1635 static void
1636 fc_flush_devloss(struct Scsi_Host *shost)
1638 if (!fc_host_devloss_work_q(shost)) {
1639 printk(KERN_ERR
1640 "ERROR: FC host '%s' attempted to flush work, "
1641 "when no workqueue created.\n", shost->hostt->name);
1642 dump_stack();
1643 return;
1646 flush_workqueue(fc_host_devloss_work_q(shost));
1651 * fc_remove_host - called to terminate any fc_transport-related elements
1652 * for a scsi host.
1653 * @rport: remote port to be unblocked.
1655 * This routine is expected to be called immediately preceeding the
1656 * a driver's call to scsi_remove_host().
1658 * WARNING: A driver utilizing the fc_transport, which fails to call
1659 * this routine prior to scsi_remote_host(), will leave dangling
1660 * objects in /sys/class/fc_remote_ports. Access to any of these
1661 * objects can result in a system crash !!!
1663 * Notes:
1664 * This routine assumes no locks are held on entry.
1666 void
1667 fc_remove_host(struct Scsi_Host *shost)
1669 struct fc_rport *rport, *next_rport;
1670 struct workqueue_struct *work_q;
1671 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1673 /* Remove any remote ports */
1674 list_for_each_entry_safe(rport, next_rport,
1675 &fc_host->rports, peers) {
1676 list_del(&rport->peers);
1677 rport->port_state = FC_PORTSTATE_DELETED;
1678 fc_queue_work(shost, &rport->rport_delete_work);
1681 list_for_each_entry_safe(rport, next_rport,
1682 &fc_host->rport_bindings, peers) {
1683 list_del(&rport->peers);
1684 rport->port_state = FC_PORTSTATE_DELETED;
1685 fc_queue_work(shost, &rport->rport_delete_work);
1688 /* flush all scan work items */
1689 scsi_flush_work(shost);
1691 /* flush all stgt delete, and rport delete work items, then kill it */
1692 if (fc_host->work_q) {
1693 work_q = fc_host->work_q;
1694 fc_host->work_q = NULL;
1695 destroy_workqueue(work_q);
1698 /* flush all devloss work items, then kill it */
1699 if (fc_host->devloss_work_q) {
1700 work_q = fc_host->devloss_work_q;
1701 fc_host->devloss_work_q = NULL;
1702 destroy_workqueue(work_q);
1705 EXPORT_SYMBOL(fc_remove_host);
1709 * fc_starget_delete - called to delete the scsi decendents of an rport
1710 * (target and all sdevs)
1712 * @work: remote port to be operated on.
1714 static void
1715 fc_starget_delete(struct work_struct *work)
1717 struct fc_rport *rport =
1718 container_of(work, struct fc_rport, stgt_delete_work);
1719 struct Scsi_Host *shost = rport_to_shost(rport);
1720 unsigned long flags;
1721 struct fc_internal *i = to_fc_internal(shost->transportt);
1724 * Involve the LLDD if possible. All io on the rport is to
1725 * be terminated, either as part of the dev_loss_tmo callback
1726 * processing, or via the terminate_rport_io function.
1728 if (i->f->dev_loss_tmo_callbk)
1729 i->f->dev_loss_tmo_callbk(rport);
1730 else if (i->f->terminate_rport_io)
1731 i->f->terminate_rport_io(rport);
1733 spin_lock_irqsave(shost->host_lock, flags);
1734 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
1735 spin_unlock_irqrestore(shost->host_lock, flags);
1736 if (!cancel_delayed_work(&rport->fail_io_work))
1737 fc_flush_devloss(shost);
1738 if (!cancel_delayed_work(&rport->dev_loss_work))
1739 fc_flush_devloss(shost);
1740 spin_lock_irqsave(shost->host_lock, flags);
1741 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1743 spin_unlock_irqrestore(shost->host_lock, flags);
1745 scsi_remove_target(&rport->dev);
1750 * fc_rport_final_delete - finish rport termination and delete it.
1752 * @work: remote port to be deleted.
1754 static void
1755 fc_rport_final_delete(struct work_struct *work)
1757 struct fc_rport *rport =
1758 container_of(work, struct fc_rport, rport_delete_work);
1759 struct device *dev = &rport->dev;
1760 struct Scsi_Host *shost = rport_to_shost(rport);
1761 struct fc_internal *i = to_fc_internal(shost->transportt);
1764 * if a scan is pending, flush the SCSI Host work_q so that
1765 * that we can reclaim the rport scan work element.
1767 if (rport->flags & FC_RPORT_SCAN_PENDING)
1768 scsi_flush_work(shost);
1770 /* Delete SCSI target and sdevs */
1771 if (rport->scsi_target_id != -1)
1772 fc_starget_delete(&rport->stgt_delete_work);
1773 else if (i->f->dev_loss_tmo_callbk)
1774 i->f->dev_loss_tmo_callbk(rport);
1775 else if (i->f->terminate_rport_io)
1776 i->f->terminate_rport_io(rport);
1778 transport_remove_device(dev);
1779 device_del(dev);
1780 transport_destroy_device(dev);
1781 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1782 put_device(dev); /* for self-reference */
1787 * fc_rport_create - allocates and creates a remote FC port.
1788 * @shost: scsi host the remote port is connected to.
1789 * @channel: Channel on shost port connected to.
1790 * @ids: The world wide names, fc address, and FC4 port
1791 * roles for the remote port.
1793 * Allocates and creates the remoter port structure, including the
1794 * class and sysfs creation.
1796 * Notes:
1797 * This routine assumes no locks are held on entry.
1799 struct fc_rport *
1800 fc_rport_create(struct Scsi_Host *shost, int channel,
1801 struct fc_rport_identifiers *ids)
1803 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1804 struct fc_internal *fci = to_fc_internal(shost->transportt);
1805 struct fc_rport *rport;
1806 struct device *dev;
1807 unsigned long flags;
1808 int error;
1809 size_t size;
1811 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1812 rport = kzalloc(size, GFP_KERNEL);
1813 if (unlikely(!rport)) {
1814 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1815 return NULL;
1818 rport->maxframe_size = -1;
1819 rport->supported_classes = FC_COS_UNSPECIFIED;
1820 rport->dev_loss_tmo = fc_dev_loss_tmo;
1821 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1822 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1823 rport->port_id = ids->port_id;
1824 rport->roles = ids->roles;
1825 rport->port_state = FC_PORTSTATE_ONLINE;
1826 if (fci->f->dd_fcrport_size)
1827 rport->dd_data = &rport[1];
1828 rport->channel = channel;
1829 rport->fast_io_fail_tmo = -1;
1831 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
1832 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
1833 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
1834 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
1835 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
1837 spin_lock_irqsave(shost->host_lock, flags);
1839 rport->number = fc_host->next_rport_number++;
1840 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1841 rport->scsi_target_id = fc_host->next_target_id++;
1842 else
1843 rport->scsi_target_id = -1;
1844 list_add_tail(&rport->peers, &fc_host->rports);
1845 get_device(&shost->shost_gendev); /* for fc_host->rport list */
1847 spin_unlock_irqrestore(shost->host_lock, flags);
1849 dev = &rport->dev;
1850 device_initialize(dev); /* takes self reference */
1851 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1852 dev->release = fc_rport_dev_release;
1853 sprintf(dev->bus_id, "rport-%d:%d-%d",
1854 shost->host_no, channel, rport->number);
1855 transport_setup_device(dev);
1857 error = device_add(dev);
1858 if (error) {
1859 printk(KERN_ERR "FC Remote Port device_add failed\n");
1860 goto delete_rport;
1862 transport_add_device(dev);
1863 transport_configure_device(dev);
1865 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1866 /* initiate a scan of the target */
1867 rport->flags |= FC_RPORT_SCAN_PENDING;
1868 scsi_queue_work(shost, &rport->scan_work);
1871 return rport;
1873 delete_rport:
1874 transport_destroy_device(dev);
1875 spin_lock_irqsave(shost->host_lock, flags);
1876 list_del(&rport->peers);
1877 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1878 spin_unlock_irqrestore(shost->host_lock, flags);
1879 put_device(dev->parent);
1880 kfree(rport);
1881 return NULL;
1885 * fc_remote_port_add - notifies the fc transport of the existence
1886 * of a remote FC port.
1887 * @shost: scsi host the remote port is connected to.
1888 * @channel: Channel on shost port connected to.
1889 * @ids: The world wide names, fc address, and FC4 port
1890 * roles for the remote port.
1892 * The LLDD calls this routine to notify the transport of the existence
1893 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1894 * of the port, it's FC address (port_id), and the FC4 roles that are
1895 * active for the port.
1897 * For ports that are FCP targets (aka scsi targets), the FC transport
1898 * maintains consistent target id bindings on behalf of the LLDD.
1899 * A consistent target id binding is an assignment of a target id to
1900 * a remote port identifier, which persists while the scsi host is
1901 * attached. The remote port can disappear, then later reappear, and
1902 * it's target id assignment remains the same. This allows for shifts
1903 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1904 * changes to the scsi subsystem which is based on scsi host number and
1905 * target id values. Bindings are only valid during the attachment of
1906 * the scsi host. If the host detaches, then later re-attaches, target
1907 * id bindings may change.
1909 * This routine is responsible for returning a remote port structure.
1910 * The routine will search the list of remote ports it maintains
1911 * internally on behalf of consistent target id mappings. If found, the
1912 * remote port structure will be reused. Otherwise, a new remote port
1913 * structure will be allocated.
1915 * Whenever a remote port is allocated, a new fc_remote_port class
1916 * device is created.
1918 * Should not be called from interrupt context.
1920 * Notes:
1921 * This routine assumes no locks are held on entry.
1923 struct fc_rport *
1924 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1925 struct fc_rport_identifiers *ids)
1927 struct fc_internal *fci = to_fc_internal(shost->transportt);
1928 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1929 struct fc_rport *rport;
1930 unsigned long flags;
1931 int match = 0;
1933 /* ensure any stgt delete functions are done */
1934 fc_flush_work(shost);
1937 * Search the list of "active" rports, for an rport that has been
1938 * deleted, but we've held off the real delete while the target
1939 * is in a "blocked" state.
1941 spin_lock_irqsave(shost->host_lock, flags);
1943 list_for_each_entry(rport, &fc_host->rports, peers) {
1945 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1946 (rport->channel == channel)) {
1948 switch (fc_host->tgtid_bind_type) {
1949 case FC_TGTID_BIND_BY_WWPN:
1950 case FC_TGTID_BIND_NONE:
1951 if (rport->port_name == ids->port_name)
1952 match = 1;
1953 break;
1954 case FC_TGTID_BIND_BY_WWNN:
1955 if (rport->node_name == ids->node_name)
1956 match = 1;
1957 break;
1958 case FC_TGTID_BIND_BY_ID:
1959 if (rport->port_id == ids->port_id)
1960 match = 1;
1961 break;
1964 if (match) {
1965 struct delayed_work *work =
1966 &rport->dev_loss_work;
1968 memcpy(&rport->node_name, &ids->node_name,
1969 sizeof(rport->node_name));
1970 memcpy(&rport->port_name, &ids->port_name,
1971 sizeof(rport->port_name));
1972 rport->port_id = ids->port_id;
1974 rport->port_state = FC_PORTSTATE_ONLINE;
1975 rport->roles = ids->roles;
1977 spin_unlock_irqrestore(shost->host_lock, flags);
1979 if (fci->f->dd_fcrport_size)
1980 memset(rport->dd_data, 0,
1981 fci->f->dd_fcrport_size);
1984 * If we were blocked, we were a target.
1985 * If no longer a target, we leave the timer
1986 * running in case the port changes roles
1987 * prior to the timer expiring. If the timer
1988 * fires, the target will be torn down.
1990 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1991 return rport;
1993 /* restart the target */
1996 * Stop the target timers first. Take no action
1997 * on the del_timer failure as the state
1998 * machine state change will validate the
1999 * transaction.
2001 if (!cancel_delayed_work(&rport->fail_io_work))
2002 fc_flush_devloss(shost);
2003 if (!cancel_delayed_work(work))
2004 fc_flush_devloss(shost);
2006 spin_lock_irqsave(shost->host_lock, flags);
2008 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2010 /* initiate a scan of the target */
2011 rport->flags |= FC_RPORT_SCAN_PENDING;
2012 scsi_queue_work(shost, &rport->scan_work);
2014 spin_unlock_irqrestore(shost->host_lock, flags);
2016 scsi_target_unblock(&rport->dev);
2018 return rport;
2023 /* Search the bindings array */
2024 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2026 /* search for a matching consistent binding */
2028 list_for_each_entry(rport, &fc_host->rport_bindings,
2029 peers) {
2030 if (rport->channel != channel)
2031 continue;
2033 switch (fc_host->tgtid_bind_type) {
2034 case FC_TGTID_BIND_BY_WWPN:
2035 if (rport->port_name == ids->port_name)
2036 match = 1;
2037 break;
2038 case FC_TGTID_BIND_BY_WWNN:
2039 if (rport->node_name == ids->node_name)
2040 match = 1;
2041 break;
2042 case FC_TGTID_BIND_BY_ID:
2043 if (rport->port_id == ids->port_id)
2044 match = 1;
2045 break;
2046 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2047 break;
2050 if (match) {
2051 list_move_tail(&rport->peers, &fc_host->rports);
2052 break;
2056 if (match) {
2057 memcpy(&rport->node_name, &ids->node_name,
2058 sizeof(rport->node_name));
2059 memcpy(&rport->port_name, &ids->port_name,
2060 sizeof(rport->port_name));
2061 rport->port_id = ids->port_id;
2062 rport->roles = ids->roles;
2063 rport->port_state = FC_PORTSTATE_ONLINE;
2065 if (fci->f->dd_fcrport_size)
2066 memset(rport->dd_data, 0,
2067 fci->f->dd_fcrport_size);
2069 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
2070 /* initiate a scan of the target */
2071 rport->flags |= FC_RPORT_SCAN_PENDING;
2072 scsi_queue_work(shost, &rport->scan_work);
2073 spin_unlock_irqrestore(shost->host_lock, flags);
2074 scsi_target_unblock(&rport->dev);
2075 } else
2076 spin_unlock_irqrestore(shost->host_lock, flags);
2078 return rport;
2082 spin_unlock_irqrestore(shost->host_lock, flags);
2084 /* No consistent binding found - create new remote port entry */
2085 rport = fc_rport_create(shost, channel, ids);
2087 return rport;
2089 EXPORT_SYMBOL(fc_remote_port_add);
2093 * fc_remote_port_delete - notifies the fc transport that a remote
2094 * port is no longer in existence.
2095 * @rport: The remote port that no longer exists
2097 * The LLDD calls this routine to notify the transport that a remote
2098 * port is no longer part of the topology. Note: Although a port
2099 * may no longer be part of the topology, it may persist in the remote
2100 * ports displayed by the fc_host. We do this under 2 conditions:
2101 * - If the port was a scsi target, we delay its deletion by "blocking" it.
2102 * This allows the port to temporarily disappear, then reappear without
2103 * disrupting the SCSI device tree attached to it. During the "blocked"
2104 * period the port will still exist.
2105 * - If the port was a scsi target and disappears for longer than we
2106 * expect, we'll delete the port and the tear down the SCSI device tree
2107 * attached to it. However, we want to semi-persist the target id assigned
2108 * to that port if it eventually does exist. The port structure will
2109 * remain (although with minimal information) so that the target id
2110 * bindings remails.
2112 * If the remote port is not an FCP Target, it will be fully torn down
2113 * and deallocated, including the fc_remote_port class device.
2115 * If the remote port is an FCP Target, the port will be placed in a
2116 * temporary blocked state. From the LLDD's perspective, the rport no
2117 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2118 * exists, but all sdevs on it are blocked from further I/O. The following
2119 * is then expected:
2120 * If the remote port does not return (signaled by a LLDD call to
2121 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2122 * scsi target is removed - killing all outstanding i/o and removing the
2123 * scsi devices attached ot it. The port structure will be marked Not
2124 * Present and be partially cleared, leaving only enough information to
2125 * recognize the remote port relative to the scsi target id binding if
2126 * it later appears. The port will remain as long as there is a valid
2127 * binding (e.g. until the user changes the binding type or unloads the
2128 * scsi host with the binding).
2130 * If the remote port returns within the dev_loss_tmo value (and matches
2131 * according to the target id binding type), the port structure will be
2132 * reused. If it is no longer a SCSI target, the target will be torn
2133 * down. If it continues to be a SCSI target, then the target will be
2134 * unblocked (allowing i/o to be resumed), and a scan will be activated
2135 * to ensure that all luns are detected.
2137 * Called from normal process context only - cannot be called from interrupt.
2139 * Notes:
2140 * This routine assumes no locks are held on entry.
2142 void
2143 fc_remote_port_delete(struct fc_rport *rport)
2145 struct Scsi_Host *shost = rport_to_shost(rport);
2146 struct fc_internal *i = to_fc_internal(shost->transportt);
2147 int timeout = rport->dev_loss_tmo;
2148 unsigned long flags;
2151 * No need to flush the fc_host work_q's, as all adds are synchronous.
2153 * We do need to reclaim the rport scan work element, so eventually
2154 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2155 * there's still a scan pending.
2158 spin_lock_irqsave(shost->host_lock, flags);
2160 /* If no scsi target id mapping, delete it */
2161 if (rport->scsi_target_id == -1) {
2162 list_del(&rport->peers);
2163 rport->port_state = FC_PORTSTATE_DELETED;
2164 fc_queue_work(shost, &rport->rport_delete_work);
2165 spin_unlock_irqrestore(shost->host_lock, flags);
2166 return;
2169 rport->port_state = FC_PORTSTATE_BLOCKED;
2171 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2173 spin_unlock_irqrestore(shost->host_lock, flags);
2175 scsi_target_block(&rport->dev);
2177 /* see if we need to kill io faster than waiting for device loss */
2178 if ((rport->fast_io_fail_tmo != -1) &&
2179 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2180 fc_queue_devloss_work(shost, &rport->fail_io_work,
2181 rport->fast_io_fail_tmo * HZ);
2183 /* cap the length the devices can be blocked until they are deleted */
2184 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2186 EXPORT_SYMBOL(fc_remote_port_delete);
2189 * fc_remote_port_rolechg - notifies the fc transport that the roles
2190 * on a remote may have changed.
2191 * @rport: The remote port that changed.
2193 * The LLDD calls this routine to notify the transport that the roles
2194 * on a remote port may have changed. The largest effect of this is
2195 * if a port now becomes a FCP Target, it must be allocated a
2196 * scsi target id. If the port is no longer a FCP target, any
2197 * scsi target id value assigned to it will persist in case the
2198 * role changes back to include FCP Target. No changes in the scsi
2199 * midlayer will be invoked if the role changes (in the expectation
2200 * that the role will be resumed. If it doesn't normal error processing
2201 * will take place).
2203 * Should not be called from interrupt context.
2205 * Notes:
2206 * This routine assumes no locks are held on entry.
2208 void
2209 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2211 struct Scsi_Host *shost = rport_to_shost(rport);
2212 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2213 unsigned long flags;
2214 int create = 0;
2216 spin_lock_irqsave(shost->host_lock, flags);
2217 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
2218 if (rport->scsi_target_id == -1) {
2219 rport->scsi_target_id = fc_host->next_target_id++;
2220 create = 1;
2221 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
2222 create = 1;
2225 rport->roles = roles;
2227 spin_unlock_irqrestore(shost->host_lock, flags);
2229 if (create) {
2231 * There may have been a delete timer running on the
2232 * port. Ensure that it is cancelled as we now know
2233 * the port is an FCP Target.
2234 * Note: we know the rport is exists and in an online
2235 * state as the LLDD would not have had an rport
2236 * reference to pass us.
2238 * Take no action on the del_timer failure as the state
2239 * machine state change will validate the
2240 * transaction.
2242 if (!cancel_delayed_work(&rport->fail_io_work))
2243 fc_flush_devloss(shost);
2244 if (!cancel_delayed_work(&rport->dev_loss_work))
2245 fc_flush_devloss(shost);
2247 spin_lock_irqsave(shost->host_lock, flags);
2248 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2249 spin_unlock_irqrestore(shost->host_lock, flags);
2251 /* ensure any stgt delete functions are done */
2252 fc_flush_work(shost);
2254 /* initiate a scan of the target */
2255 spin_lock_irqsave(shost->host_lock, flags);
2256 rport->flags |= FC_RPORT_SCAN_PENDING;
2257 scsi_queue_work(shost, &rport->scan_work);
2258 spin_unlock_irqrestore(shost->host_lock, flags);
2259 scsi_target_unblock(&rport->dev);
2262 EXPORT_SYMBOL(fc_remote_port_rolechg);
2265 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
2266 * was a SCSI target (thus was blocked), and failed
2267 * to return in the alloted time.
2269 * @work: rport target that failed to reappear in the alloted time.
2271 static void
2272 fc_timeout_deleted_rport(struct work_struct *work)
2274 struct fc_rport *rport =
2275 container_of(work, struct fc_rport, dev_loss_work.work);
2276 struct Scsi_Host *shost = rport_to_shost(rport);
2277 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2278 unsigned long flags;
2280 spin_lock_irqsave(shost->host_lock, flags);
2282 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2285 * If the port is ONLINE, then it came back. Validate it's still an
2286 * FCP target. If not, tear down the scsi_target on it.
2288 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2289 !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2290 dev_printk(KERN_ERR, &rport->dev,
2291 "blocked FC remote port time out: no longer"
2292 " a FCP target, removing starget\n");
2293 spin_unlock_irqrestore(shost->host_lock, flags);
2294 scsi_target_unblock(&rport->dev);
2295 fc_queue_work(shost, &rport->stgt_delete_work);
2296 return;
2299 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2300 spin_unlock_irqrestore(shost->host_lock, flags);
2301 dev_printk(KERN_ERR, &rport->dev,
2302 "blocked FC remote port time out: leaving target alone\n");
2303 return;
2306 if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
2307 list_del(&rport->peers);
2308 rport->port_state = FC_PORTSTATE_DELETED;
2309 dev_printk(KERN_ERR, &rport->dev,
2310 "blocked FC remote port time out: removing target\n");
2311 fc_queue_work(shost, &rport->rport_delete_work);
2312 spin_unlock_irqrestore(shost->host_lock, flags);
2313 return;
2316 dev_printk(KERN_ERR, &rport->dev,
2317 "blocked FC remote port time out: removing target and "
2318 "saving binding\n");
2320 list_move_tail(&rport->peers, &fc_host->rport_bindings);
2323 * Note: We do not remove or clear the hostdata area. This allows
2324 * host-specific target data to persist along with the
2325 * scsi_target_id. It's up to the host to manage it's hostdata area.
2329 * Reinitialize port attributes that may change if the port comes back.
2331 rport->maxframe_size = -1;
2332 rport->supported_classes = FC_COS_UNSPECIFIED;
2333 rport->roles = FC_RPORT_ROLE_UNKNOWN;
2334 rport->port_state = FC_PORTSTATE_NOTPRESENT;
2336 /* remove the identifiers that aren't used in the consisting binding */
2337 switch (fc_host->tgtid_bind_type) {
2338 case FC_TGTID_BIND_BY_WWPN:
2339 rport->node_name = -1;
2340 rport->port_id = -1;
2341 break;
2342 case FC_TGTID_BIND_BY_WWNN:
2343 rport->port_name = -1;
2344 rport->port_id = -1;
2345 break;
2346 case FC_TGTID_BIND_BY_ID:
2347 rport->node_name = -1;
2348 rport->port_name = -1;
2349 break;
2350 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2351 break;
2355 * As this only occurs if the remote port (scsi target)
2356 * went away and didn't come back - we'll remove
2357 * all attached scsi devices.
2359 spin_unlock_irqrestore(shost->host_lock, flags);
2361 scsi_target_unblock(&rport->dev);
2362 fc_queue_work(shost, &rport->stgt_delete_work);
2366 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2367 * disconnected SCSI target.
2369 * @work: rport to terminate io on.
2371 * Notes: Only requests the failure of the io, not that all are flushed
2372 * prior to returning.
2374 static void
2375 fc_timeout_fail_rport_io(struct work_struct *work)
2377 struct fc_rport *rport =
2378 container_of(work, struct fc_rport, fail_io_work.work);
2379 struct Scsi_Host *shost = rport_to_shost(rport);
2380 struct fc_internal *i = to_fc_internal(shost->transportt);
2382 if (rport->port_state != FC_PORTSTATE_BLOCKED)
2383 return;
2385 i->f->terminate_rport_io(rport);
2389 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2391 * @work: remote port to be scanned.
2393 static void
2394 fc_scsi_scan_rport(struct work_struct *work)
2396 struct fc_rport *rport =
2397 container_of(work, struct fc_rport, scan_work);
2398 struct Scsi_Host *shost = rport_to_shost(rport);
2399 unsigned long flags;
2401 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2402 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2403 scsi_scan_target(&rport->dev, rport->channel,
2404 rport->scsi_target_id, SCAN_WILD_CARD, 1);
2407 spin_lock_irqsave(shost->host_lock, flags);
2408 rport->flags &= ~FC_RPORT_SCAN_PENDING;
2409 spin_unlock_irqrestore(shost->host_lock, flags);
2413 MODULE_AUTHOR("Martin Hicks");
2414 MODULE_DESCRIPTION("FC Transport Attributes");
2415 MODULE_LICENSE("GPL");
2417 module_init(fc_transport_init);
2418 module_exit(fc_transport_exit);