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