[SCSI] lpfc 8.1.10 : Add support for dev_loss_tmo_callbk and fast_io_fail_tmo_callbk
[pandora-kernel.git] / drivers / scsi / scsi_transport_fc.c
1 /* 
2  *  FiberChannel transport specific attributes exported to sysfs.
3  *
4  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ========
21  *
22  *  Copyright (C) 2004-2005   James Smart, Emulex Corporation
23  *    Rewrite for host, target, device, and remote port attributes,
24  *    statistics, and service functions...
25  *
26  */
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"
39
40 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
41
42 /*
43  * Redefine so that we can have same named attributes in the
44  * sdev/starget/host objects.
45  */
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)
49
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;                                              \
55                                                                         \
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;                                                    \
63 }
64
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;                                                          \
70                                                                         \
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 */                                         \
79 }
80
81
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" },
94 };
95 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
96 #define FC_PORTTYPE_MAX_NAMELEN         50
97
98
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" },
116 };
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
120
121
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" },
138 };
139 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
140 #define FC_PORTSTATE_MAX_NAMELEN        20
141
142
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 },
153 };
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
159
160
161 #define fc_bitfield_name_search(title, table)                   \
162 static ssize_t                                                  \
163 get_fc_##title##_names(u32 table_key, char *buf)                \
164 {                                                               \
165         char *prefix = "";                                      \
166         ssize_t len = 0;                                        \
167         int i;                                                  \
168                                                                 \
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 = ", ";                          \
174                 }                                               \
175         }                                                       \
176         len += sprintf(buf + len, "\n");                        \
177         return len;                                             \
178 }
179
180
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" },
191 };
192 fc_bitfield_name_search(cos, fc_cos_names)
193
194
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" },
205 };
206 fc_bitfield_name_search(port_speed, fc_port_speed_names)
207
208
209 static int
210 show_fc_fc4s (char *buf, u8 *fc4_list)
211 {
212         int i, len=0;
213
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;
218 }
219
220
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" },
229 };
230 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
231
232 /*
233  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
234  */
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
242
243
244 static void fc_timeout_deleted_rport(void *data);
245 static void fc_timeout_fail_rport_io(void *data);
246 static void fc_scsi_scan_rport(void *data);
247
248 /*
249  * Attribute counts pre object type...
250  * Increase these values if you add attributes
251  */
252 #define FC_STARGET_NUM_ATTRS    3
253 #define FC_RPORT_NUM_ATTRS      10
254 #define FC_HOST_NUM_ATTRS       17
255
256 struct fc_internal {
257         struct scsi_transport_template t;
258         struct fc_function_template *f;
259
260         /*
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.
265          *
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.
269          */
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];
273
274         struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
275         struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
276
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];
280 };
281
282 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
283
284 static int fc_target_setup(struct transport_container *tc, struct device *dev,
285                            struct class_device *cdev)
286 {
287         struct scsi_target *starget = to_scsi_target(dev);
288         struct fc_rport *rport = starget_to_rport(starget);
289
290         /*
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.
294          */
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;
303         }
304
305         return 0;
306 }
307
308 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
309                                "fc_transport",
310                                fc_target_setup,
311                                NULL,
312                                NULL);
313
314 static int fc_host_setup(struct transport_container *tc, struct device *dev,
315                          struct class_device *cdev)
316 {
317         struct Scsi_Host *shost = dev_to_shost(dev);
318         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
319
320         /* 
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.
324          */
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));
335
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));
345
346         fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
347
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;
352
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;
359
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;
368         }
369
370         return 0;
371 }
372
373 static DECLARE_TRANSPORT_CLASS(fc_host_class,
374                                "fc_host",
375                                fc_host_setup,
376                                NULL,
377                                NULL);
378
379 /*
380  * Setup and Remove actions for remote ports are handled
381  * in the service functions below.
382  */
383 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
384                                "fc_remote_ports",
385                                NULL,
386                                NULL,
387                                NULL);
388
389 /*
390  * Module Parameters
391  */
392
393 /*
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.
397  */
398 static unsigned int fc_dev_loss_tmo = 60;               /* seconds */
399
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.");
406
407 /**
408  * Netlink Infrastructure
409  **/
410
411 static atomic_t fc_event_seq;
412
413 /**
414  * fc_get_event_number - Obtain the next sequential FC event number
415  *
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...
420  **/
421 u32
422 fc_get_event_number(void)
423 {
424         return atomic_add_return(1, &fc_event_seq);
425 }
426 EXPORT_SYMBOL(fc_get_event_number);
427
428
429 /**
430  * fc_host_post_event - called to post an even on an fc_host.
431  *
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
436  *
437  * Notes:
438  *      This routine assumes no locks are held on entry.
439  **/
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)
443 {
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;
450
451         if (!scsi_nl_sock) {
452                 err = -ENOENT;
453                 goto send_fail;
454         }
455
456         len = FC_NL_MSGALIGN(sizeof(*event));
457         skblen = NLMSG_SPACE(len);
458
459         skb = alloc_skb(skblen, GFP_KERNEL);
460         if (!skb) {
461                 err = -ENOBUFS;
462                 goto send_fail;
463         }
464
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;
470         }
471         event = NLMSG_DATA(nlh);
472
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;
482
483         err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS);
484         if (err && (err != -ESRCH))     /* filter no recipient errors */
485                 /* nlmsg_multicast already kfree_skb'd */
486                 goto send_fail;
487
488         return;
489
490 send_fail_skb:
491         kfree_skb(skb);
492 send_fail:
493         name = get_fc_host_event_code_name(event_code);
494         printk(KERN_WARNING
495                 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
496                 __FUNCTION__, shost->host_no,
497                 (name) ? name : "<unknown>", event_data, err);
498         return;
499 }
500 EXPORT_SYMBOL(fc_host_post_event);
501
502
503 /**
504  * fc_host_post_vendor_event - called to post a vendor unique event on
505  *                             a fc_host
506  *
507  * @shost:              host the event occurred on
508  * @event_number:       fc event number obtained from get_fc_event_number()
509  * @data_len:           amount, in bytes, of vendor unique data
510  * @data_buf:           pointer to vendor unique data
511  *
512  * Notes:
513  *      This routine assumes no locks are held on entry.
514  **/
515 void
516 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
517                 u32 data_len, char * data_buf, u64 vendor_id)
518 {
519         struct sk_buff *skb;
520         struct nlmsghdr *nlh;
521         struct fc_nl_event *event;
522         u32 len, skblen;
523         int err;
524
525         if (!scsi_nl_sock) {
526                 err = -ENOENT;
527                 goto send_vendor_fail;
528         }
529
530         len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
531         skblen = NLMSG_SPACE(len);
532
533         skb = alloc_skb(skblen, GFP_KERNEL);
534         if (!skb) {
535                 err = -ENOBUFS;
536                 goto send_vendor_fail;
537         }
538
539         nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
540                                 skblen - sizeof(*nlh), 0);
541         if (!nlh) {
542                 err = -ENOBUFS;
543                 goto send_vendor_fail_skb;
544         }
545         event = NLMSG_DATA(nlh);
546
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 = vendor_id;
551         event->host_no = shost->host_no;
552         event->event_datalen = data_len;        /* bytes */
553         event->event_num = event_number;
554         event->event_code = FCH_EVT_VENDOR_UNIQUE;
555         memcpy(&event->event_data, data_buf, data_len);
556
557         err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS);
558         if (err && (err != -ESRCH))     /* filter no recipient errors */
559                 /* nlmsg_multicast already kfree_skb'd */
560                 goto send_vendor_fail;
561
562         return;
563
564 send_vendor_fail_skb:
565         kfree_skb(skb);
566 send_vendor_fail:
567         printk(KERN_WARNING
568                 "%s: Dropped Event : host %d vendor_unique - err %d\n",
569                 __FUNCTION__, shost->host_no, err);
570         return;
571 }
572 EXPORT_SYMBOL(fc_host_post_vendor_event);
573
574
575
576 static __init int fc_transport_init(void)
577 {
578         int error;
579
580         atomic_set(&fc_event_seq, 0);
581
582         error = transport_class_register(&fc_host_class);
583         if (error)
584                 return error;
585         error = transport_class_register(&fc_rport_class);
586         if (error)
587                 return error;
588         return transport_class_register(&fc_transport_class);
589 }
590
591 static void __exit fc_transport_exit(void)
592 {
593         transport_class_unregister(&fc_transport_class);
594         transport_class_unregister(&fc_rport_class);
595         transport_class_unregister(&fc_host_class);
596 }
597
598 /*
599  * FC Remote Port Attribute Management
600  */
601
602 #define fc_rport_show_function(field, format_string, sz, cast)          \
603 static ssize_t                                                          \
604 show_fc_rport_##field (struct class_device *cdev, char *buf)            \
605 {                                                                       \
606         struct fc_rport *rport = transport_class_to_rport(cdev);        \
607         struct Scsi_Host *shost = rport_to_shost(rport);                \
608         struct fc_internal *i = to_fc_internal(shost->transportt);      \
609         if ((i->f->get_rport_##field) &&                                \
610             !((rport->port_state == FC_PORTSTATE_BLOCKED) ||            \
611               (rport->port_state == FC_PORTSTATE_DELETED) ||            \
612               (rport->port_state == FC_PORTSTATE_NOTPRESENT)))          \
613                 i->f->get_rport_##field(rport);                         \
614         return snprintf(buf, sz, format_string, cast rport->field);     \
615 }
616
617 #define fc_rport_store_function(field)                                  \
618 static ssize_t                                                          \
619 store_fc_rport_##field(struct class_device *cdev, const char *buf,      \
620                            size_t count)                                \
621 {                                                                       \
622         int val;                                                        \
623         struct fc_rport *rport = transport_class_to_rport(cdev);        \
624         struct Scsi_Host *shost = rport_to_shost(rport);                \
625         struct fc_internal *i = to_fc_internal(shost->transportt);      \
626         char *cp;                                                       \
627         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||              \
628             (rport->port_state == FC_PORTSTATE_DELETED) ||              \
629             (rport->port_state == FC_PORTSTATE_NOTPRESENT))             \
630                 return -EBUSY;                                          \
631         val = simple_strtoul(buf, &cp, 0);                              \
632         if (*cp && (*cp != '\n'))                                       \
633                 return -EINVAL;                                         \
634         i->f->set_rport_##field(rport, val);                            \
635         return count;                                                   \
636 }
637
638 #define fc_rport_rd_attr(field, format_string, sz)                      \
639         fc_rport_show_function(field, format_string, sz, )              \
640 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
641                          show_fc_rport_##field, NULL)
642
643 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)           \
644         fc_rport_show_function(field, format_string, sz, (cast))        \
645 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
646                           show_fc_rport_##field, NULL)
647
648 #define fc_rport_rw_attr(field, format_string, sz)                      \
649         fc_rport_show_function(field, format_string, sz, )              \
650         fc_rport_store_function(field)                                  \
651 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,            \
652                         show_fc_rport_##field,                          \
653                         store_fc_rport_##field)
654
655
656 #define fc_private_rport_show_function(field, format_string, sz, cast)  \
657 static ssize_t                                                          \
658 show_fc_rport_##field (struct class_device *cdev, char *buf)            \
659 {                                                                       \
660         struct fc_rport *rport = transport_class_to_rport(cdev);        \
661         return snprintf(buf, sz, format_string, cast rport->field);     \
662 }
663
664 #define fc_private_rport_rd_attr(field, format_string, sz)              \
665         fc_private_rport_show_function(field, format_string, sz, )      \
666 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
667                          show_fc_rport_##field, NULL)
668
669 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)   \
670         fc_private_rport_show_function(field, format_string, sz, (cast)) \
671 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
672                           show_fc_rport_##field, NULL)
673
674
675 #define fc_private_rport_rd_enum_attr(title, maxlen)                    \
676 static ssize_t                                                          \
677 show_fc_rport_##title (struct class_device *cdev, char *buf)            \
678 {                                                                       \
679         struct fc_rport *rport = transport_class_to_rport(cdev);        \
680         const char *name;                                               \
681         name = get_fc_##title##_name(rport->title);                     \
682         if (!name)                                                      \
683                 return -EINVAL;                                         \
684         return snprintf(buf, maxlen, "%s\n", name);                     \
685 }                                                                       \
686 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO,                      \
687                         show_fc_rport_##title, NULL)
688
689
690 #define SETUP_RPORT_ATTRIBUTE_RD(field)                                 \
691         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
692         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
693         i->private_rport_attrs[count].store = NULL;                     \
694         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
695         if (i->f->show_rport_##field)                                   \
696                 count++
697
698 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)                         \
699         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
700         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
701         i->private_rport_attrs[count].store = NULL;                     \
702         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
703         count++
704
705 #define SETUP_RPORT_ATTRIBUTE_RW(field)                                 \
706         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
707         if (!i->f->set_rport_##field) {                                 \
708                 i->private_rport_attrs[count].attr.mode = S_IRUGO;      \
709                 i->private_rport_attrs[count].store = NULL;             \
710         }                                                               \
711         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
712         if (i->f->show_rport_##field)                                   \
713                 count++
714
715 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)                         \
716 {                                                                       \
717         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
718         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
719         count++;                                                        \
720 }
721
722
723 /* The FC Transport Remote Port Attributes: */
724
725 /* Fixed Remote Port Attributes */
726
727 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
728
729 static ssize_t
730 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
731 {
732         struct fc_rport *rport = transport_class_to_rport(cdev);
733         if (rport->supported_classes == FC_COS_UNSPECIFIED)
734                 return snprintf(buf, 20, "unspecified\n");
735         return get_fc_cos_names(rport->supported_classes, buf);
736 }
737 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
738                 show_fc_rport_supported_classes, NULL);
739
740 /* Dynamic Remote Port Attributes */
741
742 /*
743  * dev_loss_tmo attribute
744  */
745 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
746 static ssize_t
747 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
748                            size_t count)
749 {
750         int val;
751         struct fc_rport *rport = transport_class_to_rport(cdev);
752         struct Scsi_Host *shost = rport_to_shost(rport);
753         struct fc_internal *i = to_fc_internal(shost->transportt);
754         char *cp;
755         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
756             (rport->port_state == FC_PORTSTATE_DELETED) ||
757             (rport->port_state == FC_PORTSTATE_NOTPRESENT))
758                 return -EBUSY;
759         val = simple_strtoul(buf, &cp, 0);
760         if ((*cp && (*cp != '\n')) ||
761             (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
762                 return -EINVAL;
763         i->f->set_rport_dev_loss_tmo(rport, val);
764         return count;
765 }
766 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
767                 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
768
769
770 /* Private Remote Port Attributes */
771
772 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
773 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
774 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
775
776 static ssize_t
777 show_fc_rport_roles (struct class_device *cdev, char *buf)
778 {
779         struct fc_rport *rport = transport_class_to_rport(cdev);
780
781         /* identify any roles that are port_id specific */
782         if ((rport->port_id != -1) &&
783             (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
784                                         FC_WELLKNOWN_PORTID_MASK) {
785                 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
786                 case FC_FPORT_PORTID:
787                         return snprintf(buf, 30, "Fabric Port\n");
788                 case FC_FABCTLR_PORTID:
789                         return snprintf(buf, 30, "Fabric Controller\n");
790                 case FC_DIRSRVR_PORTID:
791                         return snprintf(buf, 30, "Directory Server\n");
792                 case FC_TIMESRVR_PORTID:
793                         return snprintf(buf, 30, "Time Server\n");
794                 case FC_MGMTSRVR_PORTID:
795                         return snprintf(buf, 30, "Management Server\n");
796                 default:
797                         return snprintf(buf, 30, "Unknown Fabric Entity\n");
798                 }
799         } else {
800                 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
801                         return snprintf(buf, 20, "unknown\n");
802                 return get_fc_remote_port_roles_names(rport->roles, buf);
803         }
804 }
805 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
806                 show_fc_rport_roles, NULL);
807
808 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
809 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
810
811 /*
812  * fast_io_fail_tmo attribute
813  */
814 static ssize_t
815 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
816 {
817         struct fc_rport *rport = transport_class_to_rport(cdev);
818
819         if (rport->fast_io_fail_tmo == -1)
820                 return snprintf(buf, 5, "off\n");
821         return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
822 }
823
824 static ssize_t
825 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
826                            size_t count)
827 {
828         int val;
829         char *cp;
830         struct fc_rport *rport = transport_class_to_rport(cdev);
831
832         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
833             (rport->port_state == FC_PORTSTATE_DELETED) ||
834             (rport->port_state == FC_PORTSTATE_NOTPRESENT))
835                 return -EBUSY;
836         if (strncmp(buf, "off", 3) == 0)
837                 rport->fast_io_fail_tmo = -1;
838         else {
839                 val = simple_strtoul(buf, &cp, 0);
840                 if ((*cp && (*cp != '\n')) ||
841                     (val < 0) || (val >= rport->dev_loss_tmo))
842                         return -EINVAL;
843                 rport->fast_io_fail_tmo = val;
844         }
845         return count;
846 }
847 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
848         show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
849
850
851 /*
852  * FC SCSI Target Attribute Management
853  */
854
855 /*
856  * Note: in the target show function we recognize when the remote
857  *  port is in the heirarchy and do not allow the driver to get
858  *  involved in sysfs functions. The driver only gets involved if
859  *  it's the "old" style that doesn't use rports.
860  */
861 #define fc_starget_show_function(field, format_string, sz, cast)        \
862 static ssize_t                                                          \
863 show_fc_starget_##field (struct class_device *cdev, char *buf)          \
864 {                                                                       \
865         struct scsi_target *starget = transport_class_to_starget(cdev); \
866         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
867         struct fc_internal *i = to_fc_internal(shost->transportt);      \
868         struct fc_rport *rport = starget_to_rport(starget);             \
869         if (rport)                                                      \
870                 fc_starget_##field(starget) = rport->field;             \
871         else if (i->f->get_starget_##field)                             \
872                 i->f->get_starget_##field(starget);                     \
873         return snprintf(buf, sz, format_string,                         \
874                 cast fc_starget_##field(starget));                      \
875 }
876
877 #define fc_starget_rd_attr(field, format_string, sz)                    \
878         fc_starget_show_function(field, format_string, sz, )            \
879 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
880                          show_fc_starget_##field, NULL)
881
882 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)         \
883         fc_starget_show_function(field, format_string, sz, (cast))      \
884 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
885                           show_fc_starget_##field, NULL)
886
887 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
888         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
889         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
890         i->private_starget_attrs[count].store = NULL;                   \
891         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
892         if (i->f->show_starget_##field)                                 \
893                 count++
894
895 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
896         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
897         if (!i->f->set_starget_##field) {                               \
898                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
899                 i->private_starget_attrs[count].store = NULL;           \
900         }                                                               \
901         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
902         if (i->f->show_starget_##field)                                 \
903                 count++
904
905 /* The FC Transport SCSI Target Attributes: */
906 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
907 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
908 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
909
910
911 /*
912  * Host Attribute Management
913  */
914
915 #define fc_host_show_function(field, format_string, sz, cast)           \
916 static ssize_t                                                          \
917 show_fc_host_##field (struct class_device *cdev, char *buf)             \
918 {                                                                       \
919         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
920         struct fc_internal *i = to_fc_internal(shost->transportt);      \
921         if (i->f->get_host_##field)                                     \
922                 i->f->get_host_##field(shost);                          \
923         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
924 }
925
926 #define fc_host_store_function(field)                                   \
927 static ssize_t                                                          \
928 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
929                            size_t count)                                \
930 {                                                                       \
931         int val;                                                        \
932         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
933         struct fc_internal *i = to_fc_internal(shost->transportt);      \
934         char *cp;                                                       \
935                                                                         \
936         val = simple_strtoul(buf, &cp, 0);                              \
937         if (*cp && (*cp != '\n'))                                       \
938                 return -EINVAL;                                         \
939         i->f->set_host_##field(shost, val);                             \
940         return count;                                                   \
941 }
942
943 #define fc_host_store_str_function(field, slen)                         \
944 static ssize_t                                                          \
945 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
946                            size_t count)                                \
947 {                                                                       \
948         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
949         struct fc_internal *i = to_fc_internal(shost->transportt);      \
950         unsigned int cnt=count;                                         \
951                                                                         \
952         /* count may include a LF at end of string */                   \
953         if (buf[cnt-1] == '\n')                                         \
954                 cnt--;                                                  \
955         if (cnt > ((slen) - 1))                                         \
956                 return -EINVAL;                                         \
957         memcpy(fc_host_##field(shost), buf, cnt);                       \
958         i->f->set_host_##field(shost);                                  \
959         return count;                                                   \
960 }
961
962 #define fc_host_rd_attr(field, format_string, sz)                       \
963         fc_host_show_function(field, format_string, sz, )               \
964 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
965                          show_fc_host_##field, NULL)
966
967 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
968         fc_host_show_function(field, format_string, sz, (cast))         \
969 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
970                           show_fc_host_##field, NULL)
971
972 #define fc_host_rw_attr(field, format_string, sz)                       \
973         fc_host_show_function(field, format_string, sz, )               \
974         fc_host_store_function(field)                                   \
975 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,             \
976                         show_fc_host_##field,                           \
977                         store_fc_host_##field)
978
979 #define fc_host_rd_enum_attr(title, maxlen)                             \
980 static ssize_t                                                          \
981 show_fc_host_##title (struct class_device *cdev, char *buf)             \
982 {                                                                       \
983         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
984         struct fc_internal *i = to_fc_internal(shost->transportt);      \
985         const char *name;                                               \
986         if (i->f->get_host_##title)                                     \
987                 i->f->get_host_##title(shost);                          \
988         name = get_fc_##title##_name(fc_host_##title(shost));           \
989         if (!name)                                                      \
990                 return -EINVAL;                                         \
991         return snprintf(buf, maxlen, "%s\n", name);                     \
992 }                                                                       \
993 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
994
995 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
996         i->private_host_attrs[count] = class_device_attr_host_##field;  \
997         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
998         i->private_host_attrs[count].store = NULL;                      \
999         i->host_attrs[count] = &i->private_host_attrs[count];           \
1000         if (i->f->show_host_##field)                                    \
1001                 count++
1002
1003 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
1004         i->private_host_attrs[count] = class_device_attr_host_##field;  \
1005         if (!i->f->set_host_##field) {                                  \
1006                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
1007                 i->private_host_attrs[count].store = NULL;              \
1008         }                                                               \
1009         i->host_attrs[count] = &i->private_host_attrs[count];           \
1010         if (i->f->show_host_##field)                                    \
1011                 count++
1012
1013
1014 #define fc_private_host_show_function(field, format_string, sz, cast)   \
1015 static ssize_t                                                          \
1016 show_fc_host_##field (struct class_device *cdev, char *buf)             \
1017 {                                                                       \
1018         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
1019         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1020 }
1021
1022 #define fc_private_host_rd_attr(field, format_string, sz)               \
1023         fc_private_host_show_function(field, format_string, sz, )       \
1024 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
1025                          show_fc_host_##field, NULL)
1026
1027 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
1028         fc_private_host_show_function(field, format_string, sz, (cast)) \
1029 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
1030                           show_fc_host_##field, NULL)
1031
1032 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
1033         i->private_host_attrs[count] = class_device_attr_host_##field;  \
1034         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
1035         i->private_host_attrs[count].store = NULL;                      \
1036         i->host_attrs[count] = &i->private_host_attrs[count];           \
1037         count++
1038
1039 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
1040 {                                                                       \
1041         i->private_host_attrs[count] = class_device_attr_host_##field;  \
1042         i->host_attrs[count] = &i->private_host_attrs[count];           \
1043         count++;                                                        \
1044 }
1045
1046
1047 /* Fixed Host Attributes */
1048
1049 static ssize_t
1050 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1051 {
1052         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1053
1054         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1055                 return snprintf(buf, 20, "unspecified\n");
1056
1057         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1058 }
1059 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1060                 show_fc_host_supported_classes, NULL);
1061
1062 static ssize_t
1063 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1064 {
1065         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1066         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1067 }
1068 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1069                 show_fc_host_supported_fc4s, NULL);
1070
1071 static ssize_t
1072 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1073 {
1074         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1075
1076         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1077                 return snprintf(buf, 20, "unknown\n");
1078
1079         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1080 }
1081 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1082                 show_fc_host_supported_speeds, NULL);
1083
1084
1085 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1086 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1087 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1088                              unsigned long long);
1089 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1090 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1091
1092
1093 /* Dynamic Host Attributes */
1094
1095 static ssize_t
1096 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1097 {
1098         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1099         struct fc_internal *i = to_fc_internal(shost->transportt);
1100
1101         if (i->f->get_host_active_fc4s)
1102                 i->f->get_host_active_fc4s(shost);
1103
1104         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1105 }
1106 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1107                 show_fc_host_active_fc4s, NULL);
1108
1109 static ssize_t
1110 show_fc_host_speed (struct class_device *cdev, char *buf)
1111 {
1112         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1113         struct fc_internal *i = to_fc_internal(shost->transportt);
1114
1115         if (i->f->get_host_speed)
1116                 i->f->get_host_speed(shost);
1117
1118         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1119                 return snprintf(buf, 20, "unknown\n");
1120
1121         return get_fc_port_speed_names(fc_host_speed(shost), buf);
1122 }
1123 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1124                 show_fc_host_speed, NULL);
1125
1126
1127 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1128 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1129 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1130 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1131 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1132
1133 fc_private_host_show_function(system_hostname, "%s\n",
1134                 FC_SYMBOLIC_NAME_SIZE + 1, )
1135 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1136 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1137                 show_fc_host_system_hostname, store_fc_host_system_hostname);
1138
1139
1140 /* Private Host Attributes */
1141
1142 static ssize_t
1143 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1144 {
1145         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1146         const char *name;
1147
1148         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1149         if (!name)
1150                 return -EINVAL;
1151         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1152 }
1153
1154 #define get_list_head_entry(pos, head, member)          \
1155         pos = list_entry((head)->next, typeof(*pos), member)
1156
1157 static ssize_t
1158 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1159         const char *buf, size_t count)
1160 {
1161         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1162         struct fc_rport *rport;
1163         enum fc_tgtid_binding_type val;
1164         unsigned long flags;
1165
1166         if (get_fc_tgtid_bind_type_match(buf, &val))
1167                 return -EINVAL;
1168
1169         /* if changing bind type, purge all unused consistent bindings */
1170         if (val != fc_host_tgtid_bind_type(shost)) {
1171                 spin_lock_irqsave(shost->host_lock, flags);
1172                 while (!list_empty(&fc_host_rport_bindings(shost))) {
1173                         get_list_head_entry(rport,
1174                                 &fc_host_rport_bindings(shost), peers);
1175                         list_del(&rport->peers);
1176                         rport->port_state = FC_PORTSTATE_DELETED;
1177                         fc_queue_work(shost, &rport->rport_delete_work);
1178                 }
1179                 spin_unlock_irqrestore(shost->host_lock, flags);
1180         }
1181
1182         fc_host_tgtid_bind_type(shost) = val;
1183         return count;
1184 }
1185
1186 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1187                         show_fc_private_host_tgtid_bind_type,
1188                         store_fc_private_host_tgtid_bind_type);
1189
1190 static ssize_t
1191 store_fc_private_host_issue_lip(struct class_device *cdev,
1192         const char *buf, size_t count)
1193 {
1194         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1195         struct fc_internal *i = to_fc_internal(shost->transportt);
1196         int ret;
1197
1198         /* ignore any data value written to the attribute */
1199         if (i->f->issue_fc_host_lip) {
1200                 ret = i->f->issue_fc_host_lip(shost);
1201                 return ret ? ret: count;
1202         }
1203
1204         return -ENOENT;
1205 }
1206
1207 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1208                         store_fc_private_host_issue_lip);
1209
1210 /*
1211  * Host Statistics Management
1212  */
1213
1214 /* Show a given an attribute in the statistics group */
1215 static ssize_t
1216 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1217 {
1218         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1219         struct fc_internal *i = to_fc_internal(shost->transportt);
1220         struct fc_host_statistics *stats;
1221         ssize_t ret = -ENOENT;
1222
1223         if (offset > sizeof(struct fc_host_statistics) ||
1224             offset % sizeof(u64) != 0)
1225                 WARN_ON(1);
1226
1227         if (i->f->get_fc_host_stats) {
1228                 stats = (i->f->get_fc_host_stats)(shost);
1229                 if (stats)
1230                         ret = snprintf(buf, 20, "0x%llx\n",
1231                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1232         }
1233         return ret;
1234 }
1235
1236
1237 /* generate a read-only statistics attribute */
1238 #define fc_host_statistic(name)                                         \
1239 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf)   \
1240 {                                                                       \
1241         return fc_stat_show(cd, buf,                                    \
1242                             offsetof(struct fc_host_statistics, name)); \
1243 }                                                                       \
1244 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1245
1246 fc_host_statistic(seconds_since_last_reset);
1247 fc_host_statistic(tx_frames);
1248 fc_host_statistic(tx_words);
1249 fc_host_statistic(rx_frames);
1250 fc_host_statistic(rx_words);
1251 fc_host_statistic(lip_count);
1252 fc_host_statistic(nos_count);
1253 fc_host_statistic(error_frames);
1254 fc_host_statistic(dumped_frames);
1255 fc_host_statistic(link_failure_count);
1256 fc_host_statistic(loss_of_sync_count);
1257 fc_host_statistic(loss_of_signal_count);
1258 fc_host_statistic(prim_seq_protocol_err_count);
1259 fc_host_statistic(invalid_tx_word_count);
1260 fc_host_statistic(invalid_crc_count);
1261 fc_host_statistic(fcp_input_requests);
1262 fc_host_statistic(fcp_output_requests);
1263 fc_host_statistic(fcp_control_requests);
1264 fc_host_statistic(fcp_input_megabytes);
1265 fc_host_statistic(fcp_output_megabytes);
1266
1267 static ssize_t
1268 fc_reset_statistics(struct class_device *cdev, const char *buf,
1269                            size_t count)
1270 {
1271         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1272         struct fc_internal *i = to_fc_internal(shost->transportt);
1273
1274         /* ignore any data value written to the attribute */
1275         if (i->f->reset_fc_host_stats) {
1276                 i->f->reset_fc_host_stats(shost);
1277                 return count;
1278         }
1279
1280         return -ENOENT;
1281 }
1282 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1283                                 fc_reset_statistics);
1284
1285
1286 static struct attribute *fc_statistics_attrs[] = {
1287         &class_device_attr_host_seconds_since_last_reset.attr,
1288         &class_device_attr_host_tx_frames.attr,
1289         &class_device_attr_host_tx_words.attr,
1290         &class_device_attr_host_rx_frames.attr,
1291         &class_device_attr_host_rx_words.attr,
1292         &class_device_attr_host_lip_count.attr,
1293         &class_device_attr_host_nos_count.attr,
1294         &class_device_attr_host_error_frames.attr,
1295         &class_device_attr_host_dumped_frames.attr,
1296         &class_device_attr_host_link_failure_count.attr,
1297         &class_device_attr_host_loss_of_sync_count.attr,
1298         &class_device_attr_host_loss_of_signal_count.attr,
1299         &class_device_attr_host_prim_seq_protocol_err_count.attr,
1300         &class_device_attr_host_invalid_tx_word_count.attr,
1301         &class_device_attr_host_invalid_crc_count.attr,
1302         &class_device_attr_host_fcp_input_requests.attr,
1303         &class_device_attr_host_fcp_output_requests.attr,
1304         &class_device_attr_host_fcp_control_requests.attr,
1305         &class_device_attr_host_fcp_input_megabytes.attr,
1306         &class_device_attr_host_fcp_output_megabytes.attr,
1307         &class_device_attr_host_reset_statistics.attr,
1308         NULL
1309 };
1310
1311 static struct attribute_group fc_statistics_group = {
1312         .name = "statistics",
1313         .attrs = fc_statistics_attrs,
1314 };
1315
1316 static int fc_host_match(struct attribute_container *cont,
1317                           struct device *dev)
1318 {
1319         struct Scsi_Host *shost;
1320         struct fc_internal *i;
1321
1322         if (!scsi_is_host_device(dev))
1323                 return 0;
1324
1325         shost = dev_to_shost(dev);
1326         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1327             != &fc_host_class.class)
1328                 return 0;
1329
1330         i = to_fc_internal(shost->transportt);
1331
1332         return &i->t.host_attrs.ac == cont;
1333 }
1334
1335 static int fc_target_match(struct attribute_container *cont,
1336                             struct device *dev)
1337 {
1338         struct Scsi_Host *shost;
1339         struct fc_internal *i;
1340
1341         if (!scsi_is_target_device(dev))
1342                 return 0;
1343
1344         shost = dev_to_shost(dev->parent);
1345         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1346             != &fc_host_class.class)
1347                 return 0;
1348
1349         i = to_fc_internal(shost->transportt);
1350
1351         return &i->t.target_attrs.ac == cont;
1352 }
1353
1354 static void fc_rport_dev_release(struct device *dev)
1355 {
1356         struct fc_rport *rport = dev_to_rport(dev);
1357         put_device(dev->parent);
1358         kfree(rport);
1359 }
1360
1361 int scsi_is_fc_rport(const struct device *dev)
1362 {
1363         return dev->release == fc_rport_dev_release;
1364 }
1365 EXPORT_SYMBOL(scsi_is_fc_rport);
1366
1367 static int fc_rport_match(struct attribute_container *cont,
1368                             struct device *dev)
1369 {
1370         struct Scsi_Host *shost;
1371         struct fc_internal *i;
1372
1373         if (!scsi_is_fc_rport(dev))
1374                 return 0;
1375
1376         shost = dev_to_shost(dev->parent);
1377         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1378             != &fc_host_class.class)
1379                 return 0;
1380
1381         i = to_fc_internal(shost->transportt);
1382
1383         return &i->rport_attr_cont.ac == cont;
1384 }
1385
1386
1387 /**
1388  * fc_timed_out - FC Transport I/O timeout intercept handler
1389  *
1390  * @scmd:       The SCSI command which timed out
1391  *
1392  * This routine protects against error handlers getting invoked while a
1393  * rport is in a blocked state, typically due to a temporarily loss of
1394  * connectivity. If the error handlers are allowed to proceed, requests
1395  * to abort i/o, reset the target, etc will likely fail as there is no way
1396  * to communicate with the device to perform the requested function. These
1397  * failures may result in the midlayer taking the device offline, requiring
1398  * manual intervention to restore operation.
1399  *
1400  * This routine, called whenever an i/o times out, validates the state of
1401  * the underlying rport. If the rport is blocked, it returns
1402  * EH_RESET_TIMER, which will continue to reschedule the timeout.
1403  * Eventually, either the device will return, or devloss_tmo will fire,
1404  * and when the timeout then fires, it will be handled normally.
1405  * If the rport is not blocked, normal error handling continues.
1406  *
1407  * Notes:
1408  *      This routine assumes no locks are held on entry.
1409  **/
1410 static enum scsi_eh_timer_return
1411 fc_timed_out(struct scsi_cmnd *scmd)
1412 {
1413         struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1414
1415         if (rport->port_state == FC_PORTSTATE_BLOCKED)
1416                 return EH_RESET_TIMER;
1417
1418         return EH_NOT_HANDLED;
1419 }
1420
1421 /*
1422  * Must be called with shost->host_lock held
1423  */
1424 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1425                 uint id, uint lun)
1426 {
1427         struct fc_rport *rport;
1428
1429         list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1430                 if (rport->scsi_target_id == -1)
1431                         continue;
1432
1433                 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1434                     (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1435                         scsi_scan_target(&rport->dev, rport->channel,
1436                                          rport->scsi_target_id, lun, 1);
1437                 }
1438         }
1439
1440         return 0;
1441 }
1442
1443 struct scsi_transport_template *
1444 fc_attach_transport(struct fc_function_template *ft)
1445 {
1446         int count;
1447         struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1448                                         GFP_KERNEL);
1449
1450         if (unlikely(!i))
1451                 return NULL;
1452
1453         i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1454         i->t.target_attrs.ac.class = &fc_transport_class.class;
1455         i->t.target_attrs.ac.match = fc_target_match;
1456         i->t.target_size = sizeof(struct fc_starget_attrs);
1457         transport_container_register(&i->t.target_attrs);
1458
1459         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1460         i->t.host_attrs.ac.class = &fc_host_class.class;
1461         i->t.host_attrs.ac.match = fc_host_match;
1462         i->t.host_size = sizeof(struct fc_host_attrs);
1463         if (ft->get_fc_host_stats)
1464                 i->t.host_attrs.statistics = &fc_statistics_group;
1465         transport_container_register(&i->t.host_attrs);
1466
1467         i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1468         i->rport_attr_cont.ac.class = &fc_rport_class.class;
1469         i->rport_attr_cont.ac.match = fc_rport_match;
1470         transport_container_register(&i->rport_attr_cont);
1471
1472         i->f = ft;
1473
1474         /* Transport uses the shost workq for scsi scanning */
1475         i->t.create_work_queue = 1;
1476
1477         i->t.eh_timed_out = fc_timed_out;
1478
1479         i->t.user_scan = fc_user_scan;
1480         
1481         /*
1482          * Setup SCSI Target Attributes.
1483          */
1484         count = 0;
1485         SETUP_STARGET_ATTRIBUTE_RD(node_name);
1486         SETUP_STARGET_ATTRIBUTE_RD(port_name);
1487         SETUP_STARGET_ATTRIBUTE_RD(port_id);
1488
1489         BUG_ON(count > FC_STARGET_NUM_ATTRS);
1490
1491         i->starget_attrs[count] = NULL;
1492
1493
1494         /*
1495          * Setup SCSI Host Attributes.
1496          */
1497         count=0;
1498         SETUP_HOST_ATTRIBUTE_RD(node_name);
1499         SETUP_HOST_ATTRIBUTE_RD(port_name);
1500         SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1501         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1502         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1503         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1504         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1505         SETUP_HOST_ATTRIBUTE_RD(serial_number);
1506
1507         SETUP_HOST_ATTRIBUTE_RD(port_id);
1508         SETUP_HOST_ATTRIBUTE_RD(port_type);
1509         SETUP_HOST_ATTRIBUTE_RD(port_state);
1510         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1511         SETUP_HOST_ATTRIBUTE_RD(speed);
1512         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1513         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1514         SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1515
1516         /* Transport-managed attributes */
1517         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1518         if (ft->issue_fc_host_lip)
1519                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1520
1521         BUG_ON(count > FC_HOST_NUM_ATTRS);
1522
1523         i->host_attrs[count] = NULL;
1524
1525         /*
1526          * Setup Remote Port Attributes.
1527          */
1528         count=0;
1529         SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1530         SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1531         SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1532         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1533         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1534         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1535         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1536         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1537         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1538         if (ft->terminate_rport_io)
1539                 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1540
1541         BUG_ON(count > FC_RPORT_NUM_ATTRS);
1542
1543         i->rport_attrs[count] = NULL;
1544
1545         return &i->t;
1546 }
1547 EXPORT_SYMBOL(fc_attach_transport);
1548
1549 void fc_release_transport(struct scsi_transport_template *t)
1550 {
1551         struct fc_internal *i = to_fc_internal(t);
1552
1553         transport_container_unregister(&i->t.target_attrs);
1554         transport_container_unregister(&i->t.host_attrs);
1555         transport_container_unregister(&i->rport_attr_cont);
1556
1557         kfree(i);
1558 }
1559 EXPORT_SYMBOL(fc_release_transport);
1560
1561 /**
1562  * fc_queue_work - Queue work to the fc_host workqueue.
1563  * @shost:      Pointer to Scsi_Host bound to fc_host.
1564  * @work:       Work to queue for execution.
1565  *
1566  * Return value:
1567  *      1 - work queued for execution
1568  *      0 - work is already queued
1569  *      -EINVAL - work queue doesn't exist
1570  **/
1571 static int
1572 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1573 {
1574         if (unlikely(!fc_host_work_q(shost))) {
1575                 printk(KERN_ERR
1576                         "ERROR: FC host '%s' attempted to queue work, "
1577                         "when no workqueue created.\n", shost->hostt->name);
1578                 dump_stack();
1579
1580                 return -EINVAL;
1581         }
1582
1583         return queue_work(fc_host_work_q(shost), work);
1584 }
1585
1586 /**
1587  * fc_flush_work - Flush a fc_host's workqueue.
1588  * @shost:      Pointer to Scsi_Host bound to fc_host.
1589  **/
1590 static void
1591 fc_flush_work(struct Scsi_Host *shost)
1592 {
1593         if (!fc_host_work_q(shost)) {
1594                 printk(KERN_ERR
1595                         "ERROR: FC host '%s' attempted to flush work, "
1596                         "when no workqueue created.\n", shost->hostt->name);
1597                 dump_stack();
1598                 return;
1599         }
1600
1601         flush_workqueue(fc_host_work_q(shost));
1602 }
1603
1604 /**
1605  * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1606  * @shost:      Pointer to Scsi_Host bound to fc_host.
1607  * @work:       Work to queue for execution.
1608  * @delay:      jiffies to delay the work queuing
1609  *
1610  * Return value:
1611  *      1 on success / 0 already queued / < 0 for error
1612  **/
1613 static int
1614 fc_queue_devloss_work(struct Scsi_Host *shost, struct work_struct *work,
1615                                 unsigned long delay)
1616 {
1617         if (unlikely(!fc_host_devloss_work_q(shost))) {
1618                 printk(KERN_ERR
1619                         "ERROR: FC host '%s' attempted to queue work, "
1620                         "when no workqueue created.\n", shost->hostt->name);
1621                 dump_stack();
1622
1623                 return -EINVAL;
1624         }
1625
1626         if (delay == 0)
1627                 return queue_work(fc_host_devloss_work_q(shost), work);
1628
1629         return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
1630 }
1631
1632 /**
1633  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
1634  * @shost:      Pointer to Scsi_Host bound to fc_host.
1635  **/
1636 static void
1637 fc_flush_devloss(struct Scsi_Host *shost)
1638 {
1639         if (!fc_host_devloss_work_q(shost)) {
1640                 printk(KERN_ERR
1641                         "ERROR: FC host '%s' attempted to flush work, "
1642                         "when no workqueue created.\n", shost->hostt->name);
1643                 dump_stack();
1644                 return;
1645         }
1646
1647         flush_workqueue(fc_host_devloss_work_q(shost));
1648 }
1649
1650
1651 /**
1652  * fc_remove_host - called to terminate any fc_transport-related elements
1653  *                  for a scsi host.
1654  * @rport:      remote port to be unblocked.
1655  *
1656  * This routine is expected to be called immediately preceeding the
1657  * a driver's call to scsi_remove_host().
1658  *
1659  * WARNING: A driver utilizing the fc_transport, which fails to call
1660  *   this routine prior to scsi_remote_host(), will leave dangling
1661  *   objects in /sys/class/fc_remote_ports. Access to any of these
1662  *   objects can result in a system crash !!!
1663  *
1664  * Notes:
1665  *      This routine assumes no locks are held on entry.
1666  **/
1667 void
1668 fc_remove_host(struct Scsi_Host *shost)
1669 {
1670         struct fc_rport *rport, *next_rport;
1671         struct workqueue_struct *work_q;
1672         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1673
1674         /* Remove any remote ports */
1675         list_for_each_entry_safe(rport, next_rport,
1676                         &fc_host->rports, peers) {
1677                 list_del(&rport->peers);
1678                 rport->port_state = FC_PORTSTATE_DELETED;
1679                 fc_queue_work(shost, &rport->rport_delete_work);
1680         }
1681
1682         list_for_each_entry_safe(rport, next_rport,
1683                         &fc_host->rport_bindings, peers) {
1684                 list_del(&rport->peers);
1685                 rport->port_state = FC_PORTSTATE_DELETED;
1686                 fc_queue_work(shost, &rport->rport_delete_work);
1687         }
1688
1689         /* flush all scan work items */
1690         scsi_flush_work(shost);
1691
1692         /* flush all stgt delete, and rport delete work items, then kill it  */
1693         if (fc_host->work_q) {
1694                 work_q = fc_host->work_q;
1695                 fc_host->work_q = NULL;
1696                 destroy_workqueue(work_q);
1697         }
1698
1699         /* flush all devloss work items, then kill it  */
1700         if (fc_host->devloss_work_q) {
1701                 work_q = fc_host->devloss_work_q;
1702                 fc_host->devloss_work_q = NULL;
1703                 destroy_workqueue(work_q);
1704         }
1705 }
1706 EXPORT_SYMBOL(fc_remove_host);
1707
1708
1709 /**
1710  * fc_starget_delete - called to delete the scsi decendents of an rport
1711  *                  (target and all sdevs)
1712  *
1713  * @data:       remote port to be operated on.
1714  **/
1715 static void
1716 fc_starget_delete(void *data)
1717 {
1718         struct fc_rport *rport = (struct fc_rport *)data;
1719         struct Scsi_Host *shost = rport_to_shost(rport);
1720         unsigned long flags;
1721         struct fc_internal *i = to_fc_internal(shost->transportt);
1722
1723         /*
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.
1727          */
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);
1732
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;
1742         }
1743         spin_unlock_irqrestore(shost->host_lock, flags);
1744
1745         scsi_remove_target(&rport->dev);
1746 }
1747
1748
1749 /**
1750  * fc_rport_final_delete - finish rport termination and delete it.
1751  *
1752  * @data:       remote port to be deleted.
1753  **/
1754 static void
1755 fc_rport_final_delete(void *data)
1756 {
1757         struct fc_rport *rport = (struct fc_rport *)data;
1758         struct device *dev = &rport->dev;
1759         struct Scsi_Host *shost = rport_to_shost(rport);
1760         struct fc_internal *i = to_fc_internal(shost->transportt);
1761
1762         /*
1763          * if a scan is pending, flush the SCSI Host work_q so that 
1764          * that we can reclaim the rport scan work element.
1765          */
1766         if (rport->flags & FC_RPORT_SCAN_PENDING)
1767                 scsi_flush_work(shost);
1768
1769         /* Delete SCSI target and sdevs */
1770         if (rport->scsi_target_id != -1)
1771                 fc_starget_delete(data);
1772         else if (i->f->dev_loss_tmo_callbk)
1773                 i->f->dev_loss_tmo_callbk(rport);
1774         else if (i->f->terminate_rport_io)
1775                 i->f->terminate_rport_io(rport);
1776
1777         transport_remove_device(dev);
1778         device_del(dev);
1779         transport_destroy_device(dev);
1780         put_device(&shost->shost_gendev);       /* for fc_host->rport list */
1781         put_device(dev);                        /* for self-reference */
1782 }
1783
1784
1785 /**
1786  * fc_rport_create - allocates and creates a remote FC port.
1787  * @shost:      scsi host the remote port is connected to.
1788  * @channel:    Channel on shost port connected to.
1789  * @ids:        The world wide names, fc address, and FC4 port
1790  *              roles for the remote port.
1791  *
1792  * Allocates and creates the remoter port structure, including the
1793  * class and sysfs creation.
1794  *
1795  * Notes:
1796  *      This routine assumes no locks are held on entry.
1797  **/
1798 struct fc_rport *
1799 fc_rport_create(struct Scsi_Host *shost, int channel,
1800         struct fc_rport_identifiers  *ids)
1801 {
1802         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1803         struct fc_internal *fci = to_fc_internal(shost->transportt);
1804         struct fc_rport *rport;
1805         struct device *dev;
1806         unsigned long flags;
1807         int error;
1808         size_t size;
1809
1810         size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1811         rport = kzalloc(size, GFP_KERNEL);
1812         if (unlikely(!rport)) {
1813                 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1814                 return NULL;
1815         }
1816
1817         rport->maxframe_size = -1;
1818         rport->supported_classes = FC_COS_UNSPECIFIED;
1819         rport->dev_loss_tmo = fc_dev_loss_tmo;
1820         memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1821         memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1822         rport->port_id = ids->port_id;
1823         rport->roles = ids->roles;
1824         rport->port_state = FC_PORTSTATE_ONLINE;
1825         if (fci->f->dd_fcrport_size)
1826                 rport->dd_data = &rport[1];
1827         rport->channel = channel;
1828         rport->fast_io_fail_tmo = -1;
1829
1830         INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1831         INIT_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io, rport);
1832         INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1833         INIT_WORK(&rport->stgt_delete_work, fc_starget_delete, rport);
1834         INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete, rport);
1835
1836         spin_lock_irqsave(shost->host_lock, flags);
1837
1838         rport->number = fc_host->next_rport_number++;
1839         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1840                 rport->scsi_target_id = fc_host->next_target_id++;
1841         else
1842                 rport->scsi_target_id = -1;
1843         list_add_tail(&rport->peers, &fc_host->rports);
1844         get_device(&shost->shost_gendev);       /* for fc_host->rport list */
1845
1846         spin_unlock_irqrestore(shost->host_lock, flags);
1847
1848         dev = &rport->dev;
1849         device_initialize(dev);                 /* takes self reference */
1850         dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1851         dev->release = fc_rport_dev_release;
1852         sprintf(dev->bus_id, "rport-%d:%d-%d",
1853                 shost->host_no, channel, rport->number);
1854         transport_setup_device(dev);
1855
1856         error = device_add(dev);
1857         if (error) {
1858                 printk(KERN_ERR "FC Remote Port device_add failed\n");
1859                 goto delete_rport;
1860         }
1861         transport_add_device(dev);
1862         transport_configure_device(dev);
1863
1864         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1865                 /* initiate a scan of the target */
1866                 rport->flags |= FC_RPORT_SCAN_PENDING;
1867                 scsi_queue_work(shost, &rport->scan_work);
1868         }
1869
1870         return rport;
1871
1872 delete_rport:
1873         transport_destroy_device(dev);
1874         spin_lock_irqsave(shost->host_lock, flags);
1875         list_del(&rport->peers);
1876         put_device(&shost->shost_gendev);       /* for fc_host->rport list */
1877         spin_unlock_irqrestore(shost->host_lock, flags);
1878         put_device(dev->parent);
1879         kfree(rport);
1880         return NULL;
1881 }
1882
1883 /**
1884  * fc_remote_port_add - notifies the fc transport of the existence
1885  *              of a remote FC port.
1886  * @shost:      scsi host the remote port is connected to.
1887  * @channel:    Channel on shost port connected to.
1888  * @ids:        The world wide names, fc address, and FC4 port
1889  *              roles for the remote port.
1890  *
1891  * The LLDD calls this routine to notify the transport of the existence
1892  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1893  * of the port, it's FC address (port_id), and the FC4 roles that are
1894  * active for the port.
1895  *
1896  * For ports that are FCP targets (aka scsi targets), the FC transport
1897  * maintains consistent target id bindings on behalf of the LLDD.
1898  * A consistent target id binding is an assignment of a target id to
1899  * a remote port identifier, which persists while the scsi host is
1900  * attached. The remote port can disappear, then later reappear, and
1901  * it's target id assignment remains the same. This allows for shifts
1902  * in FC addressing (if binding by wwpn or wwnn) with no apparent
1903  * changes to the scsi subsystem which is based on scsi host number and
1904  * target id values.  Bindings are only valid during the attachment of
1905  * the scsi host. If the host detaches, then later re-attaches, target
1906  * id bindings may change.
1907  *
1908  * This routine is responsible for returning a remote port structure.
1909  * The routine will search the list of remote ports it maintains
1910  * internally on behalf of consistent target id mappings. If found, the
1911  * remote port structure will be reused. Otherwise, a new remote port
1912  * structure will be allocated.
1913  *
1914  * Whenever a remote port is allocated, a new fc_remote_port class
1915  * device is created.
1916  *
1917  * Should not be called from interrupt context.
1918  *
1919  * Notes:
1920  *      This routine assumes no locks are held on entry.
1921  **/
1922 struct fc_rport *
1923 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1924         struct fc_rport_identifiers  *ids)
1925 {
1926         struct fc_internal *fci = to_fc_internal(shost->transportt);
1927         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1928         struct fc_rport *rport;
1929         unsigned long flags;
1930         int match = 0;
1931
1932         /* ensure any stgt delete functions are done */
1933         fc_flush_work(shost);
1934
1935         /*
1936          * Search the list of "active" rports, for an rport that has been
1937          * deleted, but we've held off the real delete while the target
1938          * is in a "blocked" state.
1939          */
1940         spin_lock_irqsave(shost->host_lock, flags);
1941
1942         list_for_each_entry(rport, &fc_host->rports, peers) {
1943
1944                 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1945                         (rport->channel == channel)) {
1946
1947                         switch (fc_host->tgtid_bind_type) {
1948                         case FC_TGTID_BIND_BY_WWPN:
1949                         case FC_TGTID_BIND_NONE:
1950                                 if (rport->port_name == ids->port_name)
1951                                         match = 1;
1952                                 break;
1953                         case FC_TGTID_BIND_BY_WWNN:
1954                                 if (rport->node_name == ids->node_name)
1955                                         match = 1;
1956                                 break;
1957                         case FC_TGTID_BIND_BY_ID:
1958                                 if (rport->port_id == ids->port_id)
1959                                         match = 1;
1960                                 break;
1961                         }
1962
1963                         if (match) {
1964                                 struct work_struct *work = 
1965                                                         &rport->dev_loss_work;
1966
1967                                 memcpy(&rport->node_name, &ids->node_name,
1968                                         sizeof(rport->node_name));
1969                                 memcpy(&rport->port_name, &ids->port_name,
1970                                         sizeof(rport->port_name));
1971                                 rport->port_id = ids->port_id;
1972
1973                                 rport->port_state = FC_PORTSTATE_ONLINE;
1974                                 rport->roles = ids->roles;
1975
1976                                 spin_unlock_irqrestore(shost->host_lock, flags);
1977
1978                                 if (fci->f->dd_fcrport_size)
1979                                         memset(rport->dd_data, 0,
1980                                                 fci->f->dd_fcrport_size);
1981
1982                                 /*
1983                                  * If we were blocked, we were a target.
1984                                  * If no longer a target, we leave the timer
1985                                  * running in case the port changes roles
1986                                  * prior to the timer expiring. If the timer
1987                                  * fires, the target will be torn down.
1988                                  */
1989                                 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1990                                         return rport;
1991
1992                                 /* restart the target */
1993
1994                                 /*
1995                                  * Stop the target timers first. Take no action
1996                                  * on the del_timer failure as the state
1997                                  * machine state change will validate the
1998                                  * transaction.
1999                                  */
2000                                 if (!cancel_delayed_work(&rport->fail_io_work))
2001                                         fc_flush_devloss(shost);
2002                                 if (!cancel_delayed_work(work))
2003                                         fc_flush_devloss(shost);
2004
2005                                 spin_lock_irqsave(shost->host_lock, flags);
2006
2007                                 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2008
2009                                 /* initiate a scan of the target */
2010                                 rport->flags |= FC_RPORT_SCAN_PENDING;
2011                                 scsi_queue_work(shost, &rport->scan_work);
2012
2013                                 spin_unlock_irqrestore(shost->host_lock, flags);
2014
2015                                 scsi_target_unblock(&rport->dev);
2016
2017                                 return rport;
2018                         }
2019                 }
2020         }
2021
2022         /* Search the bindings array */
2023         if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2024
2025                 /* search for a matching consistent binding */
2026
2027                 list_for_each_entry(rport, &fc_host->rport_bindings,
2028                                         peers) {
2029                         if (rport->channel != channel)
2030                                 continue;
2031
2032                         switch (fc_host->tgtid_bind_type) {
2033                         case FC_TGTID_BIND_BY_WWPN:
2034                                 if (rport->port_name == ids->port_name)
2035                                         match = 1;
2036                                 break;
2037                         case FC_TGTID_BIND_BY_WWNN:
2038                                 if (rport->node_name == ids->node_name)
2039                                         match = 1;
2040                                 break;
2041                         case FC_TGTID_BIND_BY_ID:
2042                                 if (rport->port_id == ids->port_id)
2043                                         match = 1;
2044                                 break;
2045                         case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2046                                 break;
2047                         }
2048
2049                         if (match) {
2050                                 list_move_tail(&rport->peers, &fc_host->rports);
2051                                 break;
2052                         }
2053                 }
2054
2055                 if (match) {
2056                         memcpy(&rport->node_name, &ids->node_name,
2057                                 sizeof(rport->node_name));
2058                         memcpy(&rport->port_name, &ids->port_name,
2059                                 sizeof(rport->port_name));
2060                         rport->port_id = ids->port_id;
2061                         rport->roles = ids->roles;
2062                         rport->port_state = FC_PORTSTATE_ONLINE;
2063
2064                         if (fci->f->dd_fcrport_size)
2065                                 memset(rport->dd_data, 0,
2066                                                 fci->f->dd_fcrport_size);
2067
2068                         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
2069                                 /* initiate a scan of the target */
2070                                 rport->flags |= FC_RPORT_SCAN_PENDING;
2071                                 scsi_queue_work(shost, &rport->scan_work);
2072                                 spin_unlock_irqrestore(shost->host_lock, flags);
2073                                 scsi_target_unblock(&rport->dev);
2074                         } else
2075                                 spin_unlock_irqrestore(shost->host_lock, flags);
2076
2077                         return rport;
2078                 }
2079         }
2080
2081         spin_unlock_irqrestore(shost->host_lock, flags);
2082
2083         /* No consistent binding found - create new remote port entry */
2084         rport = fc_rport_create(shost, channel, ids);
2085
2086         return rport;
2087 }
2088 EXPORT_SYMBOL(fc_remote_port_add);
2089
2090
2091 /**
2092  * fc_remote_port_delete - notifies the fc transport that a remote
2093  *              port is no longer in existence.
2094  * @rport:      The remote port that no longer exists
2095  *
2096  * The LLDD calls this routine to notify the transport that a remote
2097  * port is no longer part of the topology. Note: Although a port
2098  * may no longer be part of the topology, it may persist in the remote
2099  * ports displayed by the fc_host. We do this under 2 conditions:
2100  * - If the port was a scsi target, we delay its deletion by "blocking" it.
2101  *   This allows the port to temporarily disappear, then reappear without
2102  *   disrupting the SCSI device tree attached to it. During the "blocked"
2103  *   period the port will still exist.
2104  * - If the port was a scsi target and disappears for longer than we
2105  *   expect, we'll delete the port and the tear down the SCSI device tree
2106  *   attached to it. However, we want to semi-persist the target id assigned
2107  *   to that port if it eventually does exist. The port structure will
2108  *   remain (although with minimal information) so that the target id
2109  *   bindings remails.
2110  *
2111  * If the remote port is not an FCP Target, it will be fully torn down
2112  * and deallocated, including the fc_remote_port class device.
2113  *
2114  * If the remote port is an FCP Target, the port will be placed in a
2115  * temporary blocked state. From the LLDD's perspective, the rport no
2116  * longer exists. From the SCSI midlayer's perspective, the SCSI target
2117  * exists, but all sdevs on it are blocked from further I/O. The following
2118  * is then expected:
2119  *   If the remote port does not return (signaled by a LLDD call to
2120  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2121  *   scsi target is removed - killing all outstanding i/o and removing the
2122  *   scsi devices attached ot it. The port structure will be marked Not
2123  *   Present and be partially cleared, leaving only enough information to
2124  *   recognize the remote port relative to the scsi target id binding if
2125  *   it later appears.  The port will remain as long as there is a valid
2126  *   binding (e.g. until the user changes the binding type or unloads the
2127  *   scsi host with the binding).
2128  *
2129  *   If the remote port returns within the dev_loss_tmo value (and matches
2130  *   according to the target id binding type), the port structure will be
2131  *   reused. If it is no longer a SCSI target, the target will be torn
2132  *   down. If it continues to be a SCSI target, then the target will be
2133  *   unblocked (allowing i/o to be resumed), and a scan will be activated
2134  *   to ensure that all luns are detected.
2135  *
2136  * Called from normal process context only - cannot be called from interrupt.
2137  *
2138  * Notes:
2139  *      This routine assumes no locks are held on entry.
2140  **/
2141 void
2142 fc_remote_port_delete(struct fc_rport  *rport)
2143 {
2144         struct Scsi_Host *shost = rport_to_shost(rport);
2145         struct fc_internal *i = to_fc_internal(shost->transportt);
2146         int timeout = rport->dev_loss_tmo;
2147         unsigned long flags;
2148
2149         /*
2150          * No need to flush the fc_host work_q's, as all adds are synchronous.
2151          *
2152          * We do need to reclaim the rport scan work element, so eventually
2153          * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2154          * there's still a scan pending.
2155          */
2156
2157         spin_lock_irqsave(shost->host_lock, flags);
2158
2159         /* If no scsi target id mapping, delete it */
2160         if (rport->scsi_target_id == -1) {
2161                 list_del(&rport->peers);
2162                 rport->port_state = FC_PORTSTATE_DELETED;
2163                 fc_queue_work(shost, &rport->rport_delete_work);
2164                 spin_unlock_irqrestore(shost->host_lock, flags);
2165                 return;
2166         }
2167
2168         rport->port_state = FC_PORTSTATE_BLOCKED;
2169
2170         rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2171
2172         spin_unlock_irqrestore(shost->host_lock, flags);
2173
2174         scsi_target_block(&rport->dev);
2175
2176         /* see if we need to kill io faster than waiting for device loss */
2177         if ((rport->fast_io_fail_tmo != -1) &&
2178             (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2179                 fc_queue_devloss_work(shost, &rport->fail_io_work,
2180                                         rport->fast_io_fail_tmo * HZ);
2181
2182         /* cap the length the devices can be blocked until they are deleted */
2183         fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2184 }
2185 EXPORT_SYMBOL(fc_remote_port_delete);
2186
2187 /**
2188  * fc_remote_port_rolechg - notifies the fc transport that the roles
2189  *              on a remote may have changed.
2190  * @rport:      The remote port that changed.
2191  *
2192  * The LLDD calls this routine to notify the transport that the roles
2193  * on a remote port may have changed. The largest effect of this is
2194  * if a port now becomes a FCP Target, it must be allocated a
2195  * scsi target id.  If the port is no longer a FCP target, any
2196  * scsi target id value assigned to it will persist in case the
2197  * role changes back to include FCP Target. No changes in the scsi
2198  * midlayer will be invoked if the role changes (in the expectation
2199  * that the role will be resumed. If it doesn't normal error processing
2200  * will take place).
2201  *
2202  * Should not be called from interrupt context.
2203  *
2204  * Notes:
2205  *      This routine assumes no locks are held on entry.
2206  **/
2207 void
2208 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
2209 {
2210         struct Scsi_Host *shost = rport_to_shost(rport);
2211         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2212         unsigned long flags;
2213         int create = 0;
2214
2215         spin_lock_irqsave(shost->host_lock, flags);
2216         if (roles & FC_RPORT_ROLE_FCP_TARGET) {
2217                 if (rport->scsi_target_id == -1) {
2218                         rport->scsi_target_id = fc_host->next_target_id++;
2219                         create = 1;
2220                 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
2221                         create = 1;
2222         }
2223
2224         rport->roles = roles;
2225
2226         spin_unlock_irqrestore(shost->host_lock, flags);
2227
2228         if (create) {
2229                 /*
2230                  * There may have been a delete timer running on the
2231                  * port. Ensure that it is cancelled as we now know
2232                  * the port is an FCP Target.
2233                  * Note: we know the rport is exists and in an online
2234                  *  state as the LLDD would not have had an rport
2235                  *  reference to pass us.
2236                  *
2237                  * Take no action on the del_timer failure as the state
2238                  * machine state change will validate the
2239                  * transaction.
2240                  */
2241                 if (!cancel_delayed_work(&rport->fail_io_work))
2242                         fc_flush_devloss(shost);
2243                 if (!cancel_delayed_work(&rport->dev_loss_work))
2244                         fc_flush_devloss(shost);
2245
2246                 spin_lock_irqsave(shost->host_lock, flags);
2247                 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2248                 spin_unlock_irqrestore(shost->host_lock, flags);
2249
2250                 /* ensure any stgt delete functions are done */
2251                 fc_flush_work(shost);
2252
2253                 /* initiate a scan of the target */
2254                 spin_lock_irqsave(shost->host_lock, flags);
2255                 rport->flags |= FC_RPORT_SCAN_PENDING;
2256                 scsi_queue_work(shost, &rport->scan_work);
2257                 spin_unlock_irqrestore(shost->host_lock, flags);
2258                 scsi_target_unblock(&rport->dev);
2259         }
2260 }
2261 EXPORT_SYMBOL(fc_remote_port_rolechg);
2262
2263 /**
2264  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
2265  *                       was a SCSI target (thus was blocked), and failed
2266  *                       to return in the alloted time.
2267  * 
2268  * @data:       rport target that failed to reappear in the alloted time.
2269  **/
2270 static void
2271 fc_timeout_deleted_rport(void  *data)
2272 {
2273         struct fc_rport *rport = (struct fc_rport *)data;
2274         struct Scsi_Host *shost = rport_to_shost(rport);
2275         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2276         unsigned long flags;
2277
2278         spin_lock_irqsave(shost->host_lock, flags);
2279
2280         rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2281
2282         /*
2283          * If the port is ONLINE, then it came back. Validate it's still an
2284          * FCP target. If not, tear down the scsi_target on it.
2285          */
2286         if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2287             !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2288                 dev_printk(KERN_ERR, &rport->dev,
2289                         "blocked FC remote port time out: no longer"
2290                         " a FCP target, removing starget\n");
2291                 spin_unlock_irqrestore(shost->host_lock, flags);
2292                 scsi_target_unblock(&rport->dev);
2293                 fc_queue_work(shost, &rport->stgt_delete_work);
2294                 return;
2295         }
2296
2297         if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2298                 spin_unlock_irqrestore(shost->host_lock, flags);
2299                 dev_printk(KERN_ERR, &rport->dev,
2300                         "blocked FC remote port time out: leaving target alone\n");
2301                 return;
2302         }
2303
2304         if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
2305                 list_del(&rport->peers);
2306                 rport->port_state = FC_PORTSTATE_DELETED;
2307                 dev_printk(KERN_ERR, &rport->dev,
2308                         "blocked FC remote port time out: removing target\n");
2309                 fc_queue_work(shost, &rport->rport_delete_work);
2310                 spin_unlock_irqrestore(shost->host_lock, flags);
2311                 return;
2312         }
2313
2314         dev_printk(KERN_ERR, &rport->dev,
2315                 "blocked FC remote port time out: removing target and "
2316                 "saving binding\n");
2317
2318         list_move_tail(&rport->peers, &fc_host->rport_bindings);
2319
2320         /*
2321          * Note: We do not remove or clear the hostdata area. This allows
2322          *   host-specific target data to persist along with the
2323          *   scsi_target_id. It's up to the host to manage it's hostdata area.
2324          */
2325
2326         /*
2327          * Reinitialize port attributes that may change if the port comes back.
2328          */
2329         rport->maxframe_size = -1;
2330         rport->supported_classes = FC_COS_UNSPECIFIED;
2331         rport->roles = FC_RPORT_ROLE_UNKNOWN;
2332         rport->port_state = FC_PORTSTATE_NOTPRESENT;
2333
2334         /* remove the identifiers that aren't used in the consisting binding */
2335         switch (fc_host->tgtid_bind_type) {
2336         case FC_TGTID_BIND_BY_WWPN:
2337                 rport->node_name = -1;
2338                 rport->port_id = -1;
2339                 break;
2340         case FC_TGTID_BIND_BY_WWNN:
2341                 rport->port_name = -1;
2342                 rport->port_id = -1;
2343                 break;
2344         case FC_TGTID_BIND_BY_ID:
2345                 rport->node_name = -1;
2346                 rport->port_name = -1;
2347                 break;
2348         case FC_TGTID_BIND_NONE:        /* to keep compiler happy */
2349                 break;
2350         }
2351
2352         /*
2353          * As this only occurs if the remote port (scsi target)
2354          * went away and didn't come back - we'll remove
2355          * all attached scsi devices.
2356          */
2357         spin_unlock_irqrestore(shost->host_lock, flags);
2358
2359         scsi_target_unblock(&rport->dev);
2360         fc_queue_work(shost, &rport->stgt_delete_work);
2361 }
2362
2363 /**
2364  * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2365  *                       disconnected SCSI target.
2366  *
2367  * @data:       rport to terminate io on.
2368  *
2369  * Notes: Only requests the failure of the io, not that all are flushed
2370  *    prior to returning.
2371  **/
2372 static void
2373 fc_timeout_fail_rport_io(void  *data)
2374 {
2375         struct fc_rport *rport = (struct fc_rport *)data;
2376         struct Scsi_Host *shost = rport_to_shost(rport);
2377         struct fc_internal *i = to_fc_internal(shost->transportt);
2378
2379         if (rport->port_state != FC_PORTSTATE_BLOCKED)
2380                 return;
2381
2382         i->f->terminate_rport_io(rport);
2383 }
2384
2385 /**
2386  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2387  *
2388  * @data:       remote port to be scanned.
2389  **/
2390 static void
2391 fc_scsi_scan_rport(void *data)
2392 {
2393         struct fc_rport *rport = (struct fc_rport *)data;
2394         struct Scsi_Host *shost = rport_to_shost(rport);
2395         unsigned long flags;
2396
2397         if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2398             (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2399                 scsi_scan_target(&rport->dev, rport->channel,
2400                         rport->scsi_target_id, SCAN_WILD_CARD, 1);
2401         }
2402
2403         spin_lock_irqsave(shost->host_lock, flags);
2404         rport->flags &= ~FC_RPORT_SCAN_PENDING;
2405         spin_unlock_irqrestore(shost->host_lock, flags);
2406 }
2407
2408
2409 MODULE_AUTHOR("Martin Hicks");
2410 MODULE_DESCRIPTION("FC Transport Attributes");
2411 MODULE_LICENSE("GPL");
2412
2413 module_init(fc_transport_init);
2414 module_exit(fc_transport_exit);