Pull release into acpica branch
[linux-2.6] / 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_priv.h"
35
36 /*
37  * Redefine so that we can have same named attributes in the
38  * sdev/starget/host objects.
39  */
40 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)          \
41 struct class_device_attribute class_device_attr_##_prefix##_##_name =   \
42         __ATTR(_name,_mode,_show,_store)
43
44 #define fc_enum_name_search(title, table_type, table)                   \
45 static const char *get_fc_##title##_name(enum table_type table_key)     \
46 {                                                                       \
47         int i;                                                          \
48         char *name = NULL;                                              \
49                                                                         \
50         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {          \
51                 if (table[i].value == table_key) {                      \
52                         name = table[i].name;                           \
53                         break;                                          \
54                 }                                                       \
55         }                                                               \
56         return name;                                                    \
57 }
58
59 #define fc_enum_name_match(title, table_type, table)                    \
60 static int get_fc_##title##_match(const char *table_key,                \
61                 enum table_type *value)                                 \
62 {                                                                       \
63         int i;                                                          \
64                                                                         \
65         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {          \
66                 if (strncmp(table_key, table[i].name,                   \
67                                 table[i].matchlen) == 0) {              \
68                         *value = table[i].value;                        \
69                         return 0; /* success */                         \
70                 }                                                       \
71         }                                                               \
72         return 1; /* failure */                                         \
73 }
74
75
76 /* Convert fc_port_type values to ascii string name */
77 static struct {
78         enum fc_port_type       value;
79         char                    *name;
80 } fc_port_type_names[] = {
81         { FC_PORTTYPE_UNKNOWN,          "Unknown" },
82         { FC_PORTTYPE_OTHER,            "Other" },
83         { FC_PORTTYPE_NOTPRESENT,       "Not Present" },
84         { FC_PORTTYPE_NPORT,    "NPort (fabric via point-to-point)" },
85         { FC_PORTTYPE_NLPORT,   "NLPort (fabric via loop)" },
86         { FC_PORTTYPE_LPORT,    "LPort (private loop)" },
87         { FC_PORTTYPE_PTP,      "Point-To-Point (direct nport connection" },
88 };
89 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
90 #define FC_PORTTYPE_MAX_NAMELEN         50
91
92
93 /* Convert fc_port_state values to ascii string name */
94 static struct {
95         enum fc_port_state      value;
96         char                    *name;
97 } fc_port_state_names[] = {
98         { FC_PORTSTATE_UNKNOWN,         "Unknown" },
99         { FC_PORTSTATE_NOTPRESENT,      "Not Present" },
100         { FC_PORTSTATE_ONLINE,          "Online" },
101         { FC_PORTSTATE_OFFLINE,         "Offline" },
102         { FC_PORTSTATE_BLOCKED,         "Blocked" },
103         { FC_PORTSTATE_BYPASSED,        "Bypassed" },
104         { FC_PORTSTATE_DIAGNOSTICS,     "Diagnostics" },
105         { FC_PORTSTATE_LINKDOWN,        "Linkdown" },
106         { FC_PORTSTATE_ERROR,           "Error" },
107         { FC_PORTSTATE_LOOPBACK,        "Loopback" },
108 };
109 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
110 #define FC_PORTSTATE_MAX_NAMELEN        20
111
112
113 /* Convert fc_tgtid_binding_type values to ascii string name */
114 static struct {
115         enum fc_tgtid_binding_type      value;
116         char                            *name;
117         int                             matchlen;
118 } fc_tgtid_binding_type_names[] = {
119         { FC_TGTID_BIND_NONE, "none", 4 },
120         { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
121         { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
122         { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
123 };
124 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
125                 fc_tgtid_binding_type_names)
126 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
127                 fc_tgtid_binding_type_names)
128 #define FC_BINDTYPE_MAX_NAMELEN 30
129
130
131 #define fc_bitfield_name_search(title, table)                   \
132 static ssize_t                                                  \
133 get_fc_##title##_names(u32 table_key, char *buf)                \
134 {                                                               \
135         char *prefix = "";                                      \
136         ssize_t len = 0;                                        \
137         int i;                                                  \
138                                                                 \
139         for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {  \
140                 if (table[i].value & table_key) {               \
141                         len += sprintf(buf + len, "%s%s",       \
142                                 prefix, table[i].name);         \
143                         prefix = ", ";                          \
144                 }                                               \
145         }                                                       \
146         len += sprintf(buf + len, "\n");                        \
147         return len;                                             \
148 }
149
150
151 /* Convert FC_COS bit values to ascii string name */
152 static struct {
153         u32                     value;
154         char                    *name;
155 } fc_cos_names[] = {
156         { FC_COS_CLASS1,        "Class 1" },
157         { FC_COS_CLASS2,        "Class 2" },
158         { FC_COS_CLASS3,        "Class 3" },
159         { FC_COS_CLASS4,        "Class 4" },
160         { FC_COS_CLASS6,        "Class 6" },
161 };
162 fc_bitfield_name_search(cos, fc_cos_names)
163
164
165 /* Convert FC_PORTSPEED bit values to ascii string name */
166 static struct {
167         u32                     value;
168         char                    *name;
169 } fc_port_speed_names[] = {
170         { FC_PORTSPEED_1GBIT,           "1 Gbit" },
171         { FC_PORTSPEED_2GBIT,           "2 Gbit" },
172         { FC_PORTSPEED_4GBIT,           "4 Gbit" },
173         { FC_PORTSPEED_10GBIT,          "10 Gbit" },
174         { FC_PORTSPEED_NOT_NEGOTIATED,  "Not Negotiated" },
175 };
176 fc_bitfield_name_search(port_speed, fc_port_speed_names)
177
178
179 static int
180 show_fc_fc4s (char *buf, u8 *fc4_list)
181 {
182         int i, len=0;
183
184         for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
185                 len += sprintf(buf + len , "0x%02x ", *fc4_list);
186         len += sprintf(buf + len, "\n");
187         return len;
188 }
189
190
191 /* Convert FC_RPORT_ROLE bit values to ascii string name */
192 static struct {
193         u32                     value;
194         char                    *name;
195 } fc_remote_port_role_names[] = {
196         { FC_RPORT_ROLE_FCP_TARGET,     "FCP Target" },
197         { FC_RPORT_ROLE_FCP_INITIATOR,  "FCP Initiator" },
198         { FC_RPORT_ROLE_IP_PORT,        "IP Port" },
199 };
200 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
201
202 /*
203  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
204  */
205 #define FC_WELLKNOWN_PORTID_MASK        0xfffff0
206 #define FC_WELLKNOWN_ROLE_MASK          0x00000f
207 #define FC_FPORT_PORTID                 0x00000e
208 #define FC_FABCTLR_PORTID               0x00000d
209 #define FC_DIRSRVR_PORTID               0x00000c
210 #define FC_TIMESRVR_PORTID              0x00000b
211 #define FC_MGMTSRVR_PORTID              0x00000a
212
213
214 static void fc_timeout_deleted_rport(void *data);
215 static void fc_scsi_scan_rport(void *data);
216 static void fc_rport_terminate(struct fc_rport  *rport);
217
218 /*
219  * Attribute counts pre object type...
220  * Increase these values if you add attributes
221  */
222 #define FC_STARGET_NUM_ATTRS    3
223 #define FC_RPORT_NUM_ATTRS      9
224 #define FC_HOST_NUM_ATTRS       16
225
226 struct fc_internal {
227         struct scsi_transport_template t;
228         struct fc_function_template *f;
229
230         /*
231          * For attributes : each object has :
232          *   An array of the actual attributes structures
233          *   An array of null-terminated pointers to the attribute
234          *     structures - used for mid-layer interaction.
235          *
236          * The attribute containers for the starget and host are are
237          * part of the midlayer. As the remote port is specific to the
238          * fc transport, we must provide the attribute container.
239          */
240         struct class_device_attribute private_starget_attrs[
241                                                         FC_STARGET_NUM_ATTRS];
242         struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
243
244         struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
245         struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
246
247         struct transport_container rport_attr_cont;
248         struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
249         struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
250 };
251
252 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
253
254 static int fc_target_setup(struct transport_container *tc, struct device *dev,
255                            struct class_device *cdev)
256 {
257         struct scsi_target *starget = to_scsi_target(dev);
258         struct fc_rport *rport = starget_to_rport(starget);
259
260         /*
261          * if parent is remote port, use values from remote port.
262          * Otherwise, this host uses the fc_transport, but not the
263          * remote port interface. As such, initialize to known non-values.
264          */
265         if (rport) {
266                 fc_starget_node_name(starget) = rport->node_name;
267                 fc_starget_port_name(starget) = rport->port_name;
268                 fc_starget_port_id(starget) = rport->port_id;
269         } else {
270                 fc_starget_node_name(starget) = -1;
271                 fc_starget_port_name(starget) = -1;
272                 fc_starget_port_id(starget) = -1;
273         }
274
275         return 0;
276 }
277
278 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
279                                "fc_transport",
280                                fc_target_setup,
281                                NULL,
282                                NULL);
283
284 static int fc_host_setup(struct transport_container *tc, struct device *dev,
285                          struct class_device *cdev)
286 {
287         struct Scsi_Host *shost = dev_to_shost(dev);
288
289         /* 
290          * Set default values easily detected by the midlayer as
291          * failure cases.  The scsi lldd is responsible for initializing
292          * all transport attributes to valid values per host.
293          */
294         fc_host_node_name(shost) = -1;
295         fc_host_port_name(shost) = -1;
296         fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
297         memset(fc_host_supported_fc4s(shost), 0,
298                 sizeof(fc_host_supported_fc4s(shost)));
299         memset(fc_host_symbolic_name(shost), 0,
300                 sizeof(fc_host_symbolic_name(shost)));
301         fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
302         fc_host_maxframe_size(shost) = -1;
303         memset(fc_host_serial_number(shost), 0,
304                 sizeof(fc_host_serial_number(shost)));
305
306         fc_host_port_id(shost) = -1;
307         fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
308         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
309         memset(fc_host_active_fc4s(shost), 0,
310                 sizeof(fc_host_active_fc4s(shost)));
311         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
312         fc_host_fabric_name(shost) = -1;
313
314         fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
315
316         INIT_LIST_HEAD(&fc_host_rports(shost));
317         INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
318         fc_host_next_rport_number(shost) = 0;
319         fc_host_next_target_id(shost) = 0;
320
321         return 0;
322 }
323
324 static DECLARE_TRANSPORT_CLASS(fc_host_class,
325                                "fc_host",
326                                fc_host_setup,
327                                NULL,
328                                NULL);
329
330 /*
331  * Setup and Remove actions for remote ports are handled
332  * in the service functions below.
333  */
334 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
335                                "fc_remote_ports",
336                                NULL,
337                                NULL,
338                                NULL);
339
340 /*
341  * Module Parameters
342  */
343
344 /*
345  * dev_loss_tmo: the default number of seconds that the FC transport
346  *   should insulate the loss of a remote port.
347  *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
348  */
349 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
350
351 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
352 MODULE_PARM_DESC(dev_loss_tmo,
353                  "Maximum number of seconds that the FC transport should"
354                  " insulate the loss of a remote port. Once this value is"
355                  " exceeded, the scsi target is removed. Value should be"
356                  " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
357
358
359 static __init int fc_transport_init(void)
360 {
361         int error = transport_class_register(&fc_host_class);
362         if (error)
363                 return error;
364         error = transport_class_register(&fc_rport_class);
365         if (error)
366                 return error;
367         return transport_class_register(&fc_transport_class);
368 }
369
370 static void __exit fc_transport_exit(void)
371 {
372         transport_class_unregister(&fc_transport_class);
373         transport_class_unregister(&fc_rport_class);
374         transport_class_unregister(&fc_host_class);
375 }
376
377 /*
378  * FC Remote Port Attribute Management
379  */
380
381 #define fc_rport_show_function(field, format_string, sz, cast)          \
382 static ssize_t                                                          \
383 show_fc_rport_##field (struct class_device *cdev, char *buf)            \
384 {                                                                       \
385         struct fc_rport *rport = transport_class_to_rport(cdev);        \
386         struct Scsi_Host *shost = rport_to_shost(rport);                \
387         struct fc_internal *i = to_fc_internal(shost->transportt);      \
388         if ((i->f->get_rport_##field) &&                                \
389             !((rport->port_state == FC_PORTSTATE_BLOCKED) ||            \
390               (rport->port_state == FC_PORTSTATE_NOTPRESENT)))          \
391                 i->f->get_rport_##field(rport);                         \
392         return snprintf(buf, sz, format_string, cast rport->field);     \
393 }
394
395 #define fc_rport_store_function(field)                                  \
396 static ssize_t                                                          \
397 store_fc_rport_##field(struct class_device *cdev, const char *buf,      \
398                            size_t count)                                \
399 {                                                                       \
400         int val;                                                        \
401         struct fc_rport *rport = transport_class_to_rport(cdev);        \
402         struct Scsi_Host *shost = rport_to_shost(rport);                \
403         struct fc_internal *i = to_fc_internal(shost->transportt);      \
404         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||              \
405             (rport->port_state == FC_PORTSTATE_NOTPRESENT))             \
406                 return -EBUSY;                                          \
407         val = simple_strtoul(buf, NULL, 0);                             \
408         i->f->set_rport_##field(rport, val);                            \
409         return count;                                                   \
410 }
411
412 #define fc_rport_rd_attr(field, format_string, sz)                      \
413         fc_rport_show_function(field, format_string, sz, )              \
414 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
415                          show_fc_rport_##field, NULL)
416
417 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)           \
418         fc_rport_show_function(field, format_string, sz, (cast))        \
419 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
420                           show_fc_rport_##field, NULL)
421
422 #define fc_rport_rw_attr(field, format_string, sz)                      \
423         fc_rport_show_function(field, format_string, sz, )              \
424         fc_rport_store_function(field)                                  \
425 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,            \
426                         show_fc_rport_##field,                          \
427                         store_fc_rport_##field)
428
429
430 #define fc_private_rport_show_function(field, format_string, sz, cast)  \
431 static ssize_t                                                          \
432 show_fc_rport_##field (struct class_device *cdev, char *buf)            \
433 {                                                                       \
434         struct fc_rport *rport = transport_class_to_rport(cdev);        \
435         return snprintf(buf, sz, format_string, cast rport->field);     \
436 }
437
438 #define fc_private_rport_rd_attr(field, format_string, sz)              \
439         fc_private_rport_show_function(field, format_string, sz, )      \
440 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
441                          show_fc_rport_##field, NULL)
442
443 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)   \
444         fc_private_rport_show_function(field, format_string, sz, (cast)) \
445 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
446                           show_fc_rport_##field, NULL)
447
448
449 #define fc_private_rport_rd_enum_attr(title, maxlen)                    \
450 static ssize_t                                                          \
451 show_fc_rport_##title (struct class_device *cdev, char *buf)            \
452 {                                                                       \
453         struct fc_rport *rport = transport_class_to_rport(cdev);        \
454         const char *name;                                               \
455         name = get_fc_##title##_name(rport->title);                     \
456         if (!name)                                                      \
457                 return -EINVAL;                                         \
458         return snprintf(buf, maxlen, "%s\n", name);                     \
459 }                                                                       \
460 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO,                      \
461                         show_fc_rport_##title, NULL)
462
463
464 #define SETUP_RPORT_ATTRIBUTE_RD(field)                                 \
465         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
466         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
467         i->private_rport_attrs[count].store = NULL;                     \
468         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
469         if (i->f->show_rport_##field)                                   \
470                 count++
471
472 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)                         \
473         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
474         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
475         i->private_rport_attrs[count].store = NULL;                     \
476         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
477         count++
478
479 #define SETUP_RPORT_ATTRIBUTE_RW(field)                                 \
480         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
481         if (!i->f->set_rport_##field) {                                 \
482                 i->private_rport_attrs[count].attr.mode = S_IRUGO;      \
483                 i->private_rport_attrs[count].store = NULL;             \
484         }                                                               \
485         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
486         if (i->f->show_rport_##field)                                   \
487                 count++
488
489
490 /* The FC Transport Remote Port Attributes: */
491
492 /* Fixed Remote Port Attributes */
493
494 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
495
496 static ssize_t
497 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
498 {
499         struct fc_rport *rport = transport_class_to_rport(cdev);
500         if (rport->supported_classes == FC_COS_UNSPECIFIED)
501                 return snprintf(buf, 20, "unspecified\n");
502         return get_fc_cos_names(rport->supported_classes, buf);
503 }
504 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
505                 show_fc_rport_supported_classes, NULL);
506
507 /* Dynamic Remote Port Attributes */
508
509 /*
510  * dev_loss_tmo attribute
511  */
512 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
513 static ssize_t
514 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
515                            size_t count)
516 {
517         int val;
518         struct fc_rport *rport = transport_class_to_rport(cdev);
519         struct Scsi_Host *shost = rport_to_shost(rport);
520         struct fc_internal *i = to_fc_internal(shost->transportt);
521         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
522             (rport->port_state == FC_PORTSTATE_NOTPRESENT))
523                 return -EBUSY;
524         val = simple_strtoul(buf, NULL, 0);
525         if ((val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
526                 return -EINVAL;
527         i->f->set_rport_dev_loss_tmo(rport, val);
528         return count;
529 }
530 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
531                 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
532
533
534 /* Private Remote Port Attributes */
535
536 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
537 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
538 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
539
540 static ssize_t
541 show_fc_rport_roles (struct class_device *cdev, char *buf)
542 {
543         struct fc_rport *rport = transport_class_to_rport(cdev);
544
545         /* identify any roles that are port_id specific */
546         if ((rport->port_id != -1) &&
547             (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
548                                         FC_WELLKNOWN_PORTID_MASK) {
549                 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
550                 case FC_FPORT_PORTID:
551                         return snprintf(buf, 30, "Fabric Port\n");
552                 case FC_FABCTLR_PORTID:
553                         return snprintf(buf, 30, "Fabric Controller\n");
554                 case FC_DIRSRVR_PORTID:
555                         return snprintf(buf, 30, "Directory Server\n");
556                 case FC_TIMESRVR_PORTID:
557                         return snprintf(buf, 30, "Time Server\n");
558                 case FC_MGMTSRVR_PORTID:
559                         return snprintf(buf, 30, "Management Server\n");
560                 default:
561                         return snprintf(buf, 30, "Unknown Fabric Entity\n");
562                 }
563         } else {
564                 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
565                         return snprintf(buf, 20, "unknown\n");
566                 return get_fc_remote_port_roles_names(rport->roles, buf);
567         }
568 }
569 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
570                 show_fc_rport_roles, NULL);
571
572 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
573 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
574
575
576
577 /*
578  * FC SCSI Target Attribute Management
579  */
580
581 /*
582  * Note: in the target show function we recognize when the remote
583  *  port is in the heirarchy and do not allow the driver to get
584  *  involved in sysfs functions. The driver only gets involved if
585  *  it's the "old" style that doesn't use rports.
586  */
587 #define fc_starget_show_function(field, format_string, sz, cast)        \
588 static ssize_t                                                          \
589 show_fc_starget_##field (struct class_device *cdev, char *buf)          \
590 {                                                                       \
591         struct scsi_target *starget = transport_class_to_starget(cdev); \
592         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
593         struct fc_internal *i = to_fc_internal(shost->transportt);      \
594         struct fc_rport *rport = starget_to_rport(starget);             \
595         if (rport)                                                      \
596                 fc_starget_##field(starget) = rport->field;             \
597         else if (i->f->get_starget_##field)                             \
598                 i->f->get_starget_##field(starget);                     \
599         return snprintf(buf, sz, format_string,                         \
600                 cast fc_starget_##field(starget));                      \
601 }
602
603 #define fc_starget_rd_attr(field, format_string, sz)                    \
604         fc_starget_show_function(field, format_string, sz, )            \
605 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
606                          show_fc_starget_##field, NULL)
607
608 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)         \
609         fc_starget_show_function(field, format_string, sz, (cast))      \
610 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
611                           show_fc_starget_##field, NULL)
612
613 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
614         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
615         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
616         i->private_starget_attrs[count].store = NULL;                   \
617         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
618         if (i->f->show_starget_##field)                                 \
619                 count++
620
621 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
622         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
623         if (!i->f->set_starget_##field) {                               \
624                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
625                 i->private_starget_attrs[count].store = NULL;           \
626         }                                                               \
627         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
628         if (i->f->show_starget_##field)                                 \
629                 count++
630
631 /* The FC Transport SCSI Target Attributes: */
632 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
633 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
634 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
635
636
637 /*
638  * Host Attribute Management
639  */
640
641 #define fc_host_show_function(field, format_string, sz, cast)           \
642 static ssize_t                                                          \
643 show_fc_host_##field (struct class_device *cdev, char *buf)             \
644 {                                                                       \
645         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
646         struct fc_internal *i = to_fc_internal(shost->transportt);      \
647         if (i->f->get_host_##field)                                     \
648                 i->f->get_host_##field(shost);                          \
649         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
650 }
651
652 #define fc_host_store_function(field)                                   \
653 static ssize_t                                                          \
654 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
655                            size_t count)                                \
656 {                                                                       \
657         int val;                                                        \
658         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
659         struct fc_internal *i = to_fc_internal(shost->transportt);      \
660                                                                         \
661         val = simple_strtoul(buf, NULL, 0);                             \
662         i->f->set_host_##field(shost, val);                             \
663         return count;                                                   \
664 }
665
666 #define fc_host_rd_attr(field, format_string, sz)                       \
667         fc_host_show_function(field, format_string, sz, )               \
668 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
669                          show_fc_host_##field, NULL)
670
671 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
672         fc_host_show_function(field, format_string, sz, (cast))         \
673 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
674                           show_fc_host_##field, NULL)
675
676 #define fc_host_rw_attr(field, format_string, sz)                       \
677         fc_host_show_function(field, format_string, sz, )               \
678         fc_host_store_function(field)                                   \
679 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,             \
680                         show_fc_host_##field,                           \
681                         store_fc_host_##field)
682
683 #define fc_host_rd_enum_attr(title, maxlen)                             \
684 static ssize_t                                                          \
685 show_fc_host_##title (struct class_device *cdev, char *buf)             \
686 {                                                                       \
687         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
688         struct fc_internal *i = to_fc_internal(shost->transportt);      \
689         const char *name;                                               \
690         if (i->f->get_host_##title)                                     \
691                 i->f->get_host_##title(shost);                          \
692         name = get_fc_##title##_name(fc_host_##title(shost));           \
693         if (!name)                                                      \
694                 return -EINVAL;                                         \
695         return snprintf(buf, maxlen, "%s\n", name);                     \
696 }                                                                       \
697 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
698
699 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
700         i->private_host_attrs[count] = class_device_attr_host_##field;  \
701         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
702         i->private_host_attrs[count].store = NULL;                      \
703         i->host_attrs[count] = &i->private_host_attrs[count];           \
704         if (i->f->show_host_##field)                                    \
705                 count++
706
707 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
708         i->private_host_attrs[count] = class_device_attr_host_##field;  \
709         if (!i->f->set_host_##field) {                                  \
710                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
711                 i->private_host_attrs[count].store = NULL;              \
712         }                                                               \
713         i->host_attrs[count] = &i->private_host_attrs[count];           \
714         if (i->f->show_host_##field)                                    \
715                 count++
716
717
718 #define fc_private_host_show_function(field, format_string, sz, cast)   \
719 static ssize_t                                                          \
720 show_fc_host_##field (struct class_device *cdev, char *buf)             \
721 {                                                                       \
722         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
723         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
724 }
725
726 #define fc_private_host_rd_attr(field, format_string, sz)               \
727         fc_private_host_show_function(field, format_string, sz, )       \
728 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
729                          show_fc_host_##field, NULL)
730
731 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
732         fc_private_host_show_function(field, format_string, sz, (cast)) \
733 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
734                           show_fc_host_##field, NULL)
735
736 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
737         i->private_host_attrs[count] = class_device_attr_host_##field;  \
738         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
739         i->private_host_attrs[count].store = NULL;                      \
740         i->host_attrs[count] = &i->private_host_attrs[count];           \
741         count++
742
743 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
744 {                                                                       \
745         i->private_host_attrs[count] = class_device_attr_host_##field;  \
746         i->host_attrs[count] = &i->private_host_attrs[count];           \
747         count++;                                                        \
748 }
749
750
751 /* Fixed Host Attributes */
752
753 static ssize_t
754 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
755 {
756         struct Scsi_Host *shost = transport_class_to_shost(cdev);
757
758         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
759                 return snprintf(buf, 20, "unspecified\n");
760
761         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
762 }
763 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
764                 show_fc_host_supported_classes, NULL);
765
766 static ssize_t
767 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
768 {
769         struct Scsi_Host *shost = transport_class_to_shost(cdev);
770         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
771 }
772 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
773                 show_fc_host_supported_fc4s, NULL);
774
775 static ssize_t
776 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
777 {
778         struct Scsi_Host *shost = transport_class_to_shost(cdev);
779
780         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
781                 return snprintf(buf, 20, "unknown\n");
782
783         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
784 }
785 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
786                 show_fc_host_supported_speeds, NULL);
787
788
789 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
790 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
791 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
792 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
793 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
794
795
796 /* Dynamic Host Attributes */
797
798 static ssize_t
799 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
800 {
801         struct Scsi_Host *shost = transport_class_to_shost(cdev);
802         struct fc_internal *i = to_fc_internal(shost->transportt);
803
804         if (i->f->get_host_active_fc4s)
805                 i->f->get_host_active_fc4s(shost);
806
807         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
808 }
809 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
810                 show_fc_host_active_fc4s, NULL);
811
812 static ssize_t
813 show_fc_host_speed (struct class_device *cdev, char *buf)
814 {
815         struct Scsi_Host *shost = transport_class_to_shost(cdev);
816         struct fc_internal *i = to_fc_internal(shost->transportt);
817
818         if (i->f->get_host_speed)
819                 i->f->get_host_speed(shost);
820
821         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
822                 return snprintf(buf, 20, "unknown\n");
823
824         return get_fc_port_speed_names(fc_host_speed(shost), buf);
825 }
826 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
827                 show_fc_host_speed, NULL);
828
829
830 fc_host_rd_attr(port_id, "0x%06x\n", 20);
831 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
832 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
833 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
834
835
836 /* Private Host Attributes */
837
838 static ssize_t
839 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
840 {
841         struct Scsi_Host *shost = transport_class_to_shost(cdev);
842         const char *name;
843
844         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
845         if (!name)
846                 return -EINVAL;
847         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
848 }
849
850 #define get_list_head_entry(pos, head, member)          \
851         pos = list_entry((head)->next, typeof(*pos), member)
852
853 static ssize_t
854 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
855         const char *buf, size_t count)
856 {
857         struct Scsi_Host *shost = transport_class_to_shost(cdev);
858         struct fc_rport *rport;
859         enum fc_tgtid_binding_type val;
860         unsigned long flags;
861
862         if (get_fc_tgtid_bind_type_match(buf, &val))
863                 return -EINVAL;
864
865         /* if changing bind type, purge all unused consistent bindings */
866         if (val != fc_host_tgtid_bind_type(shost)) {
867                 spin_lock_irqsave(shost->host_lock, flags);
868                 while (!list_empty(&fc_host_rport_bindings(shost))) {
869                         get_list_head_entry(rport,
870                                 &fc_host_rport_bindings(shost), peers);
871                         spin_unlock_irqrestore(shost->host_lock, flags);
872                         fc_rport_terminate(rport);
873                         spin_lock_irqsave(shost->host_lock, flags);
874                 }
875                 spin_unlock_irqrestore(shost->host_lock, flags);
876         }
877
878         fc_host_tgtid_bind_type(shost) = val;
879         return count;
880 }
881
882 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
883                         show_fc_private_host_tgtid_bind_type,
884                         store_fc_private_host_tgtid_bind_type);
885
886 static ssize_t
887 store_fc_private_host_issue_lip(struct class_device *cdev,
888         const char *buf, size_t count)
889 {
890         struct Scsi_Host *shost = transport_class_to_shost(cdev);
891         struct fc_internal *i = to_fc_internal(shost->transportt);
892         int ret;
893
894         /* ignore any data value written to the attribute */
895         if (i->f->issue_fc_host_lip) {
896                 ret = i->f->issue_fc_host_lip(shost);
897                 return ret ? ret: count;
898         }
899
900         return -ENOENT;
901 }
902
903 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
904                         store_fc_private_host_issue_lip);
905
906 /*
907  * Host Statistics Management
908  */
909
910 /* Show a given an attribute in the statistics group */
911 static ssize_t
912 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
913 {
914         struct Scsi_Host *shost = transport_class_to_shost(cdev);
915         struct fc_internal *i = to_fc_internal(shost->transportt);
916         struct fc_host_statistics *stats;
917         ssize_t ret = -ENOENT;
918
919         if (offset > sizeof(struct fc_host_statistics) ||
920             offset % sizeof(u64) != 0)
921                 WARN_ON(1);
922
923         if (i->f->get_fc_host_stats) {
924                 stats = (i->f->get_fc_host_stats)(shost);
925                 if (stats)
926                         ret = snprintf(buf, 20, "0x%llx\n",
927                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
928         }
929         return ret;
930 }
931
932
933 /* generate a read-only statistics attribute */
934 #define fc_host_statistic(name)                                         \
935 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf)   \
936 {                                                                       \
937         return fc_stat_show(cd, buf,                                    \
938                             offsetof(struct fc_host_statistics, name)); \
939 }                                                                       \
940 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
941
942 fc_host_statistic(seconds_since_last_reset);
943 fc_host_statistic(tx_frames);
944 fc_host_statistic(tx_words);
945 fc_host_statistic(rx_frames);
946 fc_host_statistic(rx_words);
947 fc_host_statistic(lip_count);
948 fc_host_statistic(nos_count);
949 fc_host_statistic(error_frames);
950 fc_host_statistic(dumped_frames);
951 fc_host_statistic(link_failure_count);
952 fc_host_statistic(loss_of_sync_count);
953 fc_host_statistic(loss_of_signal_count);
954 fc_host_statistic(prim_seq_protocol_err_count);
955 fc_host_statistic(invalid_tx_word_count);
956 fc_host_statistic(invalid_crc_count);
957 fc_host_statistic(fcp_input_requests);
958 fc_host_statistic(fcp_output_requests);
959 fc_host_statistic(fcp_control_requests);
960 fc_host_statistic(fcp_input_megabytes);
961 fc_host_statistic(fcp_output_megabytes);
962
963 static ssize_t
964 fc_reset_statistics(struct class_device *cdev, const char *buf,
965                            size_t count)
966 {
967         struct Scsi_Host *shost = transport_class_to_shost(cdev);
968         struct fc_internal *i = to_fc_internal(shost->transportt);
969
970         /* ignore any data value written to the attribute */
971         if (i->f->reset_fc_host_stats) {
972                 i->f->reset_fc_host_stats(shost);
973                 return count;
974         }
975
976         return -ENOENT;
977 }
978 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
979                                 fc_reset_statistics);
980
981
982 static struct attribute *fc_statistics_attrs[] = {
983         &class_device_attr_host_seconds_since_last_reset.attr,
984         &class_device_attr_host_tx_frames.attr,
985         &class_device_attr_host_tx_words.attr,
986         &class_device_attr_host_rx_frames.attr,
987         &class_device_attr_host_rx_words.attr,
988         &class_device_attr_host_lip_count.attr,
989         &class_device_attr_host_nos_count.attr,
990         &class_device_attr_host_error_frames.attr,
991         &class_device_attr_host_dumped_frames.attr,
992         &class_device_attr_host_link_failure_count.attr,
993         &class_device_attr_host_loss_of_sync_count.attr,
994         &class_device_attr_host_loss_of_signal_count.attr,
995         &class_device_attr_host_prim_seq_protocol_err_count.attr,
996         &class_device_attr_host_invalid_tx_word_count.attr,
997         &class_device_attr_host_invalid_crc_count.attr,
998         &class_device_attr_host_fcp_input_requests.attr,
999         &class_device_attr_host_fcp_output_requests.attr,
1000         &class_device_attr_host_fcp_control_requests.attr,
1001         &class_device_attr_host_fcp_input_megabytes.attr,
1002         &class_device_attr_host_fcp_output_megabytes.attr,
1003         &class_device_attr_host_reset_statistics.attr,
1004         NULL
1005 };
1006
1007 static struct attribute_group fc_statistics_group = {
1008         .name = "statistics",
1009         .attrs = fc_statistics_attrs,
1010 };
1011
1012 static int fc_host_match(struct attribute_container *cont,
1013                           struct device *dev)
1014 {
1015         struct Scsi_Host *shost;
1016         struct fc_internal *i;
1017
1018         if (!scsi_is_host_device(dev))
1019                 return 0;
1020
1021         shost = dev_to_shost(dev);
1022         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1023             != &fc_host_class.class)
1024                 return 0;
1025
1026         i = to_fc_internal(shost->transportt);
1027
1028         return &i->t.host_attrs.ac == cont;
1029 }
1030
1031 static int fc_target_match(struct attribute_container *cont,
1032                             struct device *dev)
1033 {
1034         struct Scsi_Host *shost;
1035         struct fc_internal *i;
1036
1037         if (!scsi_is_target_device(dev))
1038                 return 0;
1039
1040         shost = dev_to_shost(dev->parent);
1041         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1042             != &fc_host_class.class)
1043                 return 0;
1044
1045         i = to_fc_internal(shost->transportt);
1046
1047         return &i->t.target_attrs.ac == cont;
1048 }
1049
1050 static void fc_rport_dev_release(struct device *dev)
1051 {
1052         struct fc_rport *rport = dev_to_rport(dev);
1053         put_device(dev->parent);
1054         kfree(rport);
1055 }
1056
1057 int scsi_is_fc_rport(const struct device *dev)
1058 {
1059         return dev->release == fc_rport_dev_release;
1060 }
1061 EXPORT_SYMBOL(scsi_is_fc_rport);
1062
1063 static int fc_rport_match(struct attribute_container *cont,
1064                             struct device *dev)
1065 {
1066         struct Scsi_Host *shost;
1067         struct fc_internal *i;
1068
1069         if (!scsi_is_fc_rport(dev))
1070                 return 0;
1071
1072         shost = dev_to_shost(dev->parent);
1073         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1074             != &fc_host_class.class)
1075                 return 0;
1076
1077         i = to_fc_internal(shost->transportt);
1078
1079         return &i->rport_attr_cont.ac == cont;
1080 }
1081
1082
1083 /*
1084  * Must be called with shost->host_lock held
1085  */
1086 static struct device *fc_target_parent(struct Scsi_Host *shost,
1087                                         int channel, uint id)
1088 {
1089         struct fc_rport *rport;
1090
1091         list_for_each_entry(rport, &fc_host_rports(shost), peers)
1092                 if ((rport->channel == channel) &&
1093                     (rport->scsi_target_id == id))
1094                         return &rport->dev;
1095
1096         return NULL;
1097 }
1098
1099 struct scsi_transport_template *
1100 fc_attach_transport(struct fc_function_template *ft)
1101 {
1102         struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1103                                         GFP_KERNEL);
1104         int count;
1105
1106         if (unlikely(!i))
1107                 return NULL;
1108
1109         memset(i, 0, sizeof(struct fc_internal));
1110
1111         i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1112         i->t.target_attrs.ac.class = &fc_transport_class.class;
1113         i->t.target_attrs.ac.match = fc_target_match;
1114         i->t.target_size = sizeof(struct fc_starget_attrs);
1115         transport_container_register(&i->t.target_attrs);
1116
1117         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1118         i->t.host_attrs.ac.class = &fc_host_class.class;
1119         i->t.host_attrs.ac.match = fc_host_match;
1120         i->t.host_size = sizeof(struct fc_host_attrs);
1121         if (ft->get_fc_host_stats)
1122                 i->t.host_attrs.statistics = &fc_statistics_group;
1123         transport_container_register(&i->t.host_attrs);
1124
1125         i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1126         i->rport_attr_cont.ac.class = &fc_rport_class.class;
1127         i->rport_attr_cont.ac.match = fc_rport_match;
1128         transport_container_register(&i->rport_attr_cont);
1129
1130         i->f = ft;
1131
1132         /* Transport uses the shost workq for scsi scanning */
1133         i->t.create_work_queue = 1;
1134
1135         i->t.target_parent = fc_target_parent;
1136         
1137         /*
1138          * Setup SCSI Target Attributes.
1139          */
1140         count = 0;
1141         SETUP_STARGET_ATTRIBUTE_RD(node_name);
1142         SETUP_STARGET_ATTRIBUTE_RD(port_name);
1143         SETUP_STARGET_ATTRIBUTE_RD(port_id);
1144
1145         BUG_ON(count > FC_STARGET_NUM_ATTRS);
1146
1147         i->starget_attrs[count] = NULL;
1148
1149
1150         /*
1151          * Setup SCSI Host Attributes.
1152          */
1153         count=0;
1154         SETUP_HOST_ATTRIBUTE_RD(node_name);
1155         SETUP_HOST_ATTRIBUTE_RD(port_name);
1156         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1157         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1158         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1159         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1160         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1161         SETUP_HOST_ATTRIBUTE_RD(serial_number);
1162
1163         SETUP_HOST_ATTRIBUTE_RD(port_id);
1164         SETUP_HOST_ATTRIBUTE_RD(port_type);
1165         SETUP_HOST_ATTRIBUTE_RD(port_state);
1166         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1167         SETUP_HOST_ATTRIBUTE_RD(speed);
1168         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1169
1170         /* Transport-managed attributes */
1171         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1172         if (ft->issue_fc_host_lip)
1173                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1174
1175         BUG_ON(count > FC_HOST_NUM_ATTRS);
1176
1177         i->host_attrs[count] = NULL;
1178
1179         /*
1180          * Setup Remote Port Attributes.
1181          */
1182         count=0;
1183         SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1184         SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1185         SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1186         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1187         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1188         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1189         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1190         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1191         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1192
1193         BUG_ON(count > FC_RPORT_NUM_ATTRS);
1194
1195         i->rport_attrs[count] = NULL;
1196
1197         return &i->t;
1198 }
1199 EXPORT_SYMBOL(fc_attach_transport);
1200
1201 void fc_release_transport(struct scsi_transport_template *t)
1202 {
1203         struct fc_internal *i = to_fc_internal(t);
1204
1205         transport_container_unregister(&i->t.target_attrs);
1206         transport_container_unregister(&i->t.host_attrs);
1207         transport_container_unregister(&i->rport_attr_cont);
1208
1209         kfree(i);
1210 }
1211 EXPORT_SYMBOL(fc_release_transport);
1212
1213
1214 /**
1215  * fc_remove_host - called to terminate any fc_transport-related elements
1216  *                  for a scsi host.
1217  * @rport:      remote port to be unblocked.
1218  *
1219  * This routine is expected to be called immediately preceeding the
1220  * a driver's call to scsi_remove_host().
1221  *
1222  * WARNING: A driver utilizing the fc_transport, which fails to call
1223  *   this routine prior to scsi_remote_host(), will leave dangling
1224  *   objects in /sys/class/fc_remote_ports. Access to any of these
1225  *   objects can result in a system crash !!!
1226  *
1227  * Notes:
1228  *      This routine assumes no locks are held on entry.
1229  **/
1230 void
1231 fc_remove_host(struct Scsi_Host *shost)
1232 {
1233         struct fc_rport *rport, *next_rport;
1234
1235         /* Remove any remote ports */
1236         list_for_each_entry_safe(rport, next_rport,
1237                         &fc_host_rports(shost), peers)
1238                 fc_rport_terminate(rport);
1239         list_for_each_entry_safe(rport, next_rport,
1240                         &fc_host_rport_bindings(shost), peers)
1241                 fc_rport_terminate(rport);
1242 }
1243 EXPORT_SYMBOL(fc_remove_host);
1244
1245 /*
1246  * fc_rport_tgt_remove - Removes the scsi target on the remote port
1247  * @rport:      The remote port to be operated on
1248  */
1249 static void
1250 fc_rport_tgt_remove(struct fc_rport *rport)
1251 {
1252         struct Scsi_Host *shost = rport_to_shost(rport);
1253
1254         scsi_target_unblock(&rport->dev);
1255
1256         /* Stop anything on the workq */
1257         if (!cancel_delayed_work(&rport->dev_loss_work))
1258                 flush_scheduled_work();
1259         scsi_flush_work(shost);
1260
1261         scsi_remove_target(&rport->dev);
1262 }
1263
1264 /**
1265  * fc_rport_create - allocates and creates a remote FC port.
1266  * @shost:      scsi host the remote port is connected to.
1267  * @channel:    Channel on shost port connected to.
1268  * @ids:        The world wide names, fc address, and FC4 port
1269  *              roles for the remote port.
1270  *
1271  * Allocates and creates the remoter port structure, including the
1272  * class and sysfs creation.
1273  *
1274  * Notes:
1275  *      This routine assumes no locks are held on entry.
1276  **/
1277 struct fc_rport *
1278 fc_rport_create(struct Scsi_Host *shost, int channel,
1279         struct fc_rport_identifiers  *ids)
1280 {
1281         struct fc_host_attrs *fc_host =
1282                         (struct fc_host_attrs *)shost->shost_data;
1283         struct fc_internal *fci = to_fc_internal(shost->transportt);
1284         struct fc_rport *rport;
1285         struct device *dev;
1286         unsigned long flags;
1287         int error;
1288         size_t size;
1289
1290         size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1291         rport = kmalloc(size, GFP_KERNEL);
1292         if (unlikely(!rport)) {
1293                 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1294                 return NULL;
1295         }
1296         memset(rport, 0, size);
1297
1298         rport->maxframe_size = -1;
1299         rport->supported_classes = FC_COS_UNSPECIFIED;
1300         rport->dev_loss_tmo = fc_dev_loss_tmo;
1301         memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1302         memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1303         rport->port_id = ids->port_id;
1304         rport->roles = ids->roles;
1305         rport->port_state = FC_PORTSTATE_ONLINE;
1306         if (fci->f->dd_fcrport_size)
1307                 rport->dd_data = &rport[1];
1308         rport->channel = channel;
1309
1310         INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1311         INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1312
1313         spin_lock_irqsave(shost->host_lock, flags);
1314
1315         rport->number = fc_host->next_rport_number++;
1316         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1317                 rport->scsi_target_id = fc_host->next_target_id++;
1318         else
1319                 rport->scsi_target_id = -1;
1320         list_add_tail(&rport->peers, &fc_host_rports(shost));
1321         get_device(&shost->shost_gendev);
1322
1323         spin_unlock_irqrestore(shost->host_lock, flags);
1324
1325         dev = &rport->dev;
1326         device_initialize(dev);
1327         dev->parent = get_device(&shost->shost_gendev);
1328         dev->release = fc_rport_dev_release;
1329         sprintf(dev->bus_id, "rport-%d:%d-%d",
1330                 shost->host_no, channel, rport->number);
1331         transport_setup_device(dev);
1332
1333         error = device_add(dev);
1334         if (error) {
1335                 printk(KERN_ERR "FC Remote Port device_add failed\n");
1336                 goto delete_rport;
1337         }
1338         transport_add_device(dev);
1339         transport_configure_device(dev);
1340
1341         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1342                 /* initiate a scan of the target */
1343                 scsi_queue_work(shost, &rport->scan_work);
1344
1345         return rport;
1346
1347 delete_rport:
1348         transport_destroy_device(dev);
1349         put_device(dev->parent);
1350         spin_lock_irqsave(shost->host_lock, flags);
1351         list_del(&rport->peers);
1352         put_device(&shost->shost_gendev);
1353         spin_unlock_irqrestore(shost->host_lock, flags);
1354         put_device(dev->parent);
1355         kfree(rport);
1356         return NULL;
1357 }
1358
1359 /**
1360  * fc_remote_port_add - notifies the fc transport of the existence
1361  *              of a remote FC port.
1362  * @shost:      scsi host the remote port is connected to.
1363  * @channel:    Channel on shost port connected to.
1364  * @ids:        The world wide names, fc address, and FC4 port
1365  *              roles for the remote port.
1366  *
1367  * The LLDD calls this routine to notify the transport of the existence
1368  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1369  * of the port, it's FC address (port_id), and the FC4 roles that are
1370  * active for the port.
1371  *
1372  * For ports that are FCP targets (aka scsi targets), the FC transport
1373  * maintains consistent target id bindings on behalf of the LLDD.
1374  * A consistent target id binding is an assignment of a target id to
1375  * a remote port identifier, which persists while the scsi host is
1376  * attached. The remote port can disappear, then later reappear, and
1377  * it's target id assignment remains the same. This allows for shifts
1378  * in FC addressing (if binding by wwpn or wwnn) with no apparent
1379  * changes to the scsi subsystem which is based on scsi host number and
1380  * target id values.  Bindings are only valid during the attachment of
1381  * the scsi host. If the host detaches, then later re-attaches, target
1382  * id bindings may change.
1383  *
1384  * This routine is responsible for returning a remote port structure.
1385  * The routine will search the list of remote ports it maintains
1386  * internally on behalf of consistent target id mappings. If found, the
1387  * remote port structure will be reused. Otherwise, a new remote port
1388  * structure will be allocated.
1389  *
1390  * Whenever a remote port is allocated, a new fc_remote_port class
1391  * device is created.
1392  *
1393  * Should not be called from interrupt context.
1394  *
1395  * Notes:
1396  *      This routine assumes no locks are held on entry.
1397  **/
1398 struct fc_rport *
1399 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1400         struct fc_rport_identifiers  *ids)
1401 {
1402         struct fc_internal *fci = to_fc_internal(shost->transportt);
1403         struct fc_rport *rport;
1404         unsigned long flags;
1405         int match = 0;
1406
1407         /*
1408          * Search the list of "active" rports, for an rport that has been
1409          * deleted, but we've held off the real delete while the target
1410          * is in a "blocked" state.
1411          */
1412         spin_lock_irqsave(shost->host_lock, flags);
1413
1414         list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1415
1416                 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1417                         (rport->channel == channel)) {
1418
1419                         switch (fc_host_tgtid_bind_type(shost)) {
1420                         case FC_TGTID_BIND_BY_WWPN:
1421                         case FC_TGTID_BIND_NONE:
1422                                 if (rport->port_name == ids->port_name)
1423                                         match = 1;
1424                                 break;
1425                         case FC_TGTID_BIND_BY_WWNN:
1426                                 if (rport->node_name == ids->node_name)
1427                                         match = 1;
1428                                 break;
1429                         case FC_TGTID_BIND_BY_ID:
1430                                 if (rport->port_id == ids->port_id)
1431                                         match = 1;
1432                                 break;
1433                         }
1434
1435                         if (match) {
1436                                 struct work_struct *work = 
1437                                                         &rport->dev_loss_work;
1438
1439                                 memcpy(&rport->node_name, &ids->node_name,
1440                                         sizeof(rport->node_name));
1441                                 memcpy(&rport->port_name, &ids->port_name,
1442                                         sizeof(rport->port_name));
1443                                 rport->port_id = ids->port_id;
1444
1445                                 rport->port_state = FC_PORTSTATE_ONLINE;
1446                                 rport->roles = ids->roles;
1447
1448                                 spin_unlock_irqrestore(shost->host_lock, flags);
1449
1450                                 if (fci->f->dd_fcrport_size)
1451                                         memset(rport->dd_data, 0,
1452                                                 fci->f->dd_fcrport_size);
1453
1454                                 /*
1455                                  * If we were blocked, we were a target.
1456                                  * If no longer a target, we leave the timer
1457                                  * running in case the port changes roles
1458                                  * prior to the timer expiring. If the timer
1459                                  * fires, the target will be torn down.
1460                                  */
1461                                 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1462                                         return rport;
1463
1464                                 /* restart the target */
1465
1466                                 /*
1467                                  * Stop the target timer first. Take no action
1468                                  * on the del_timer failure as the state
1469                                  * machine state change will validate the
1470                                  * transaction.
1471                                  */
1472                                 if (!cancel_delayed_work(work))
1473                                         flush_scheduled_work();
1474
1475                                 /* initiate a scan of the target */
1476                                 scsi_queue_work(shost, &rport->scan_work);
1477
1478                                 return rport;
1479                         }
1480                 }
1481         }
1482
1483         /* Search the bindings array */
1484         if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1485                 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1486
1487                 /* search for a matching consistent binding */
1488
1489                 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1490                                         peers) {
1491                         if (rport->channel != channel)
1492                                 continue;
1493
1494                         switch (fc_host_tgtid_bind_type(shost)) {
1495                         case FC_TGTID_BIND_BY_WWPN:
1496                                 if (rport->port_name == ids->port_name)
1497                                         match = 1;
1498                                 break;
1499                         case FC_TGTID_BIND_BY_WWNN:
1500                                 if (rport->node_name == ids->node_name)
1501                                         match = 1;
1502                                 break;
1503                         case FC_TGTID_BIND_BY_ID:
1504                                 if (rport->port_id == ids->port_id)
1505                                         match = 1;
1506                                 break;
1507                         case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1508                                 break;
1509                         }
1510
1511                         if (match) {
1512                                 list_move_tail(&rport->peers,
1513                                         &fc_host_rports(shost));
1514                                 break;
1515                         }
1516                 }
1517
1518                 if (match) {
1519                         memcpy(&rport->node_name, &ids->node_name,
1520                                 sizeof(rport->node_name));
1521                         memcpy(&rport->port_name, &ids->port_name,
1522                                 sizeof(rport->port_name));
1523                         rport->port_id = ids->port_id;
1524                         rport->roles = ids->roles;
1525                         rport->port_state = FC_PORTSTATE_ONLINE;
1526
1527                         spin_unlock_irqrestore(shost->host_lock, flags);
1528
1529                         if (fci->f->dd_fcrport_size)
1530                                 memset(rport->dd_data, 0,
1531                                                 fci->f->dd_fcrport_size);
1532
1533                         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1534                                 /* initiate a scan of the target */
1535                                 scsi_queue_work(shost, &rport->scan_work);
1536
1537                         return rport;
1538                 }
1539         }
1540
1541         spin_unlock_irqrestore(shost->host_lock, flags);
1542
1543         /* No consistent binding found - create new remote port entry */
1544         rport = fc_rport_create(shost, channel, ids);
1545
1546         return rport;
1547 }
1548 EXPORT_SYMBOL(fc_remote_port_add);
1549
1550 /*
1551  * fc_rport_terminate - this routine tears down and deallocates a remote port.
1552  * @rport:      The remote port to be terminated
1553  *
1554  * Notes:
1555  *      This routine assumes no locks are held on entry.
1556  */
1557 static void
1558 fc_rport_terminate(struct fc_rport  *rport)
1559 {
1560         struct Scsi_Host *shost = rport_to_shost(rport);
1561         struct device *dev = &rport->dev;
1562         unsigned long flags;
1563
1564         fc_rport_tgt_remove(rport);
1565
1566         transport_remove_device(dev);
1567         device_del(dev);
1568         transport_destroy_device(dev);
1569         spin_lock_irqsave(shost->host_lock, flags);
1570         list_del(&rport->peers);
1571         spin_unlock_irqrestore(shost->host_lock, flags);
1572         put_device(&shost->shost_gendev);
1573 }
1574
1575 /**
1576  * fc_remote_port_delete - notifies the fc transport that a remote
1577  *              port is no longer in existence.
1578  * @rport:      The remote port that no longer exists
1579  *
1580  * The LLDD calls this routine to notify the transport that a remote
1581  * port is no longer part of the topology. Note: Although a port
1582  * may no longer be part of the topology, it may persist in the remote
1583  * ports displayed by the fc_host. We do this under 2 conditions:
1584  * - If the port was a scsi target, we delay its deletion by "blocking" it.
1585  *   This allows the port to temporarily disappear, then reappear without
1586  *   disrupting the SCSI device tree attached to it. During the "blocked"
1587  *   period the port will still exist.
1588  * - If the port was a scsi target and disappears for longer than we
1589  *   expect, we'll delete the port and the tear down the SCSI device tree
1590  *   attached to it. However, we want to semi-persist the target id assigned
1591  *   to that port if it eventually does exist. The port structure will
1592  *   remain (although with minimal information) so that the target id
1593  *   bindings remails.
1594  *
1595  * If the remote port is not an FCP Target, it will be fully torn down
1596  * and deallocated, including the fc_remote_port class device.
1597  *
1598  * If the remote port is an FCP Target, the port will be placed in a
1599  * temporary blocked state. From the LLDD's perspective, the rport no
1600  * longer exists. From the SCSI midlayer's perspective, the SCSI target
1601  * exists, but all sdevs on it are blocked from further I/O. The following
1602  * is then expected:
1603  *   If the remote port does not return (signaled by a LLDD call to
1604  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
1605  *   scsi target is removed - killing all outstanding i/o and removing the
1606  *   scsi devices attached ot it. The port structure will be marked Not
1607  *   Present and be partially cleared, leaving only enough information to
1608  *   recognize the remote port relative to the scsi target id binding if
1609  *   it later appears.  The port will remain as long as there is a valid
1610  *   binding (e.g. until the user changes the binding type or unloads the
1611  *   scsi host with the binding).
1612  *
1613  *   If the remote port returns within the dev_loss_tmo value (and matches
1614  *   according to the target id binding type), the port structure will be
1615  *   reused. If it is no longer a SCSI target, the target will be torn
1616  *   down. If it continues to be a SCSI target, then the target will be
1617  *   unblocked (allowing i/o to be resumed), and a scan will be activated
1618  *   to ensure that all luns are detected.
1619  *
1620  * Called from normal process context only - cannot be called from interrupt.
1621  *
1622  * Notes:
1623  *      This routine assumes no locks are held on entry.
1624  **/
1625 void
1626 fc_remote_port_delete(struct fc_rport  *rport)
1627 {
1628         int timeout = rport->dev_loss_tmo;
1629
1630         /* If no scsi target id mapping, delete it */
1631         if (rport->scsi_target_id == -1) {
1632                 fc_rport_terminate(rport);
1633                 return;
1634         }
1635
1636         scsi_target_block(&rport->dev);
1637
1638         /* cap the length the devices can be blocked until they are deleted */
1639         schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
1640
1641         rport->port_state = FC_PORTSTATE_BLOCKED;
1642 }
1643 EXPORT_SYMBOL(fc_remote_port_delete);
1644
1645 /**
1646  * fc_remote_port_rolechg - notifies the fc transport that the roles
1647  *              on a remote may have changed.
1648  * @rport:      The remote port that changed.
1649  *
1650  * The LLDD calls this routine to notify the transport that the roles
1651  * on a remote port may have changed. The largest effect of this is
1652  * if a port now becomes a FCP Target, it must be allocated a
1653  * scsi target id.  If the port is no longer a FCP target, any
1654  * scsi target id value assigned to it will persist in case the
1655  * role changes back to include FCP Target. No changes in the scsi
1656  * midlayer will be invoked if the role changes (in the expectation
1657  * that the role will be resumed. If it doesn't normal error processing
1658  * will take place).
1659  *
1660  * Should not be called from interrupt context.
1661  *
1662  * Notes:
1663  *      This routine assumes no locks are held on entry.
1664  **/
1665 void
1666 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
1667 {
1668         struct Scsi_Host *shost = rport_to_shost(rport);
1669         struct fc_host_attrs *fc_host =
1670                         (struct fc_host_attrs *)shost->shost_data;
1671         unsigned long flags;
1672         int create = 0;
1673
1674         spin_lock_irqsave(shost->host_lock, flags);
1675         if (roles & FC_RPORT_ROLE_FCP_TARGET) {
1676                 if (rport->scsi_target_id == -1) {
1677                         rport->scsi_target_id = fc_host->next_target_id++;
1678                         create = 1;
1679                 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
1680                         create = 1;
1681         }
1682         spin_unlock_irqrestore(shost->host_lock, flags);
1683
1684         rport->roles = roles;
1685
1686         if (create) {
1687                 /*
1688                  * There may have been a delete timer running on the
1689                  * port. Ensure that it is cancelled as we now know
1690                  * the port is an FCP Target.
1691                  * Note: we know the rport is exists and in an online
1692                  *  state as the LLDD would not have had an rport
1693                  *  reference to pass us.
1694                  *
1695                  * Take no action on the del_timer failure as the state
1696                  * machine state change will validate the
1697                  * transaction.
1698                  */
1699                 if (!cancel_delayed_work(&rport->dev_loss_work))
1700                         flush_scheduled_work();
1701
1702                 /* initiate a scan of the target */
1703                 scsi_queue_work(shost, &rport->scan_work);
1704         }
1705 }
1706 EXPORT_SYMBOL(fc_remote_port_rolechg);
1707
1708 /**
1709  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
1710  *                       was a SCSI target (thus was blocked), and failed
1711  *                       to return in the alloted time.
1712  * 
1713  * @data:       rport target that failed to reappear in the alloted time.
1714  **/
1715 static void
1716 fc_timeout_deleted_rport(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
1722         spin_lock_irqsave(shost->host_lock, flags);
1723
1724         /*
1725          * If the port is ONLINE, then it came back, but was no longer an
1726          * FCP target. Thus we need to tear down the scsi_target on it.
1727          */
1728         if (rport->port_state == FC_PORTSTATE_ONLINE) {
1729                 spin_unlock_irqrestore(shost->host_lock, flags);
1730
1731                 dev_printk(KERN_ERR, &rport->dev,
1732                         "blocked FC remote port time out: removing target\n");
1733
1734                 fc_rport_tgt_remove(rport);
1735
1736                 return;
1737         }
1738
1739         if (rport->port_state != FC_PORTSTATE_BLOCKED) {
1740                 spin_unlock_irqrestore(shost->host_lock, flags);
1741                 dev_printk(KERN_ERR, &rport->dev,
1742                         "blocked FC remote port time out: leaving target alone\n");
1743                 return;
1744         }
1745
1746         if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
1747                 spin_unlock_irqrestore(shost->host_lock, flags);
1748                 dev_printk(KERN_ERR, &rport->dev,
1749                         "blocked FC remote port time out: removing target\n");
1750                 fc_rport_terminate(rport);
1751                 return;
1752         }
1753
1754         dev_printk(KERN_ERR, &rport->dev,
1755                 "blocked FC remote port time out: removing target and "
1756                 "saving binding\n");
1757
1758         list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1759
1760         /*
1761          * Note: We do not remove or clear the hostdata area. This allows
1762          *   host-specific target data to persist along with the
1763          *   scsi_target_id. It's up to the host to manage it's hostdata area.
1764          */
1765
1766         /*
1767          * Reinitialize port attributes that may change if the port comes back.
1768          */
1769         rport->maxframe_size = -1;
1770         rport->supported_classes = FC_COS_UNSPECIFIED;
1771         rport->roles = FC_RPORT_ROLE_UNKNOWN;
1772         rport->port_state = FC_PORTSTATE_NOTPRESENT;
1773
1774         /* remove the identifiers that aren't used in the consisting binding */
1775         switch (fc_host_tgtid_bind_type(shost)) {
1776         case FC_TGTID_BIND_BY_WWPN:
1777                 rport->node_name = -1;
1778                 rport->port_id = -1;
1779                 break;
1780         case FC_TGTID_BIND_BY_WWNN:
1781                 rport->port_name = -1;
1782                 rport->port_id = -1;
1783                 break;
1784         case FC_TGTID_BIND_BY_ID:
1785                 rport->node_name = -1;
1786                 rport->port_name = -1;
1787                 break;
1788         case FC_TGTID_BIND_NONE:        /* to keep compiler happy */
1789                 break;
1790         }
1791
1792         spin_unlock_irqrestore(shost->host_lock, flags);
1793
1794         /*
1795          * As this only occurs if the remote port (scsi target)
1796          * went away and didn't come back - we'll remove
1797          * all attached scsi devices.
1798          */
1799         fc_rport_tgt_remove(rport);
1800 }
1801
1802 /**
1803  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1804  *
1805  * Will unblock the target (in case it went away and has now come back),
1806  * then invoke a scan.
1807  *
1808  * @data:       remote port to be scanned.
1809  **/
1810 static void
1811 fc_scsi_scan_rport(void *data)
1812 {
1813         struct fc_rport *rport = (struct fc_rport *)data;
1814
1815         scsi_target_unblock(&rport->dev);
1816         scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1817                         SCAN_WILD_CARD, 1);
1818 }
1819
1820
1821 MODULE_AUTHOR("Martin Hicks");
1822 MODULE_DESCRIPTION("FC Transport Attributes");
1823 MODULE_LICENSE("GPL");
1824
1825 module_init(fc_transport_init);
1826 module_exit(fc_transport_exit);