27702097b7fccb2ff67d27629d88899061d81577
[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_blocked_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                 i->f->get_rport_##field(rport);                         \
389         return snprintf(buf, sz, format_string, cast rport->field);     \
390 }
391
392 #define fc_rport_store_function(field)                                  \
393 static ssize_t                                                          \
394 store_fc_rport_##field(struct class_device *cdev, const char *buf,      \
395                            size_t count)                                \
396 {                                                                       \
397         int val;                                                        \
398         struct fc_rport *rport = transport_class_to_rport(cdev);        \
399         struct Scsi_Host *shost = rport_to_shost(rport);                \
400         struct fc_internal *i = to_fc_internal(shost->transportt);      \
401         val = simple_strtoul(buf, NULL, 0);                             \
402         i->f->set_rport_##field(rport, val);                            \
403         return count;                                                   \
404 }
405
406 #define fc_rport_rd_attr(field, format_string, sz)                      \
407         fc_rport_show_function(field, format_string, sz, )              \
408 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
409                          show_fc_rport_##field, NULL)
410
411 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)           \
412         fc_rport_show_function(field, format_string, sz, (cast))        \
413 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
414                           show_fc_rport_##field, NULL)
415
416 #define fc_rport_rw_attr(field, format_string, sz)                      \
417         fc_rport_show_function(field, format_string, sz, )              \
418         fc_rport_store_function(field)                                  \
419 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,            \
420                         show_fc_rport_##field,                          \
421                         store_fc_rport_##field)
422
423
424 #define fc_private_rport_show_function(field, format_string, sz, cast)  \
425 static ssize_t                                                          \
426 show_fc_rport_##field (struct class_device *cdev, char *buf)            \
427 {                                                                       \
428         struct fc_rport *rport = transport_class_to_rport(cdev);        \
429         return snprintf(buf, sz, format_string, cast rport->field);     \
430 }
431
432 #define fc_private_rport_rd_attr(field, format_string, sz)              \
433         fc_private_rport_show_function(field, format_string, sz, )      \
434 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
435                          show_fc_rport_##field, NULL)
436
437 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)   \
438         fc_private_rport_show_function(field, format_string, sz, (cast)) \
439 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,                      \
440                           show_fc_rport_##field, NULL)
441
442
443 #define fc_private_rport_rd_enum_attr(title, maxlen)                    \
444 static ssize_t                                                          \
445 show_fc_rport_##title (struct class_device *cdev, char *buf)            \
446 {                                                                       \
447         struct fc_rport *rport = transport_class_to_rport(cdev);        \
448         const char *name;                                               \
449         name = get_fc_##title##_name(rport->title);                     \
450         if (!name)                                                      \
451                 return -EINVAL;                                         \
452         return snprintf(buf, maxlen, "%s\n", name);                     \
453 }                                                                       \
454 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO,                      \
455                         show_fc_rport_##title, NULL)
456
457
458 #define SETUP_RPORT_ATTRIBUTE_RD(field)                                 \
459         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
460         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
461         i->private_rport_attrs[count].store = NULL;                     \
462         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
463         if (i->f->show_rport_##field)                                   \
464                 count++
465
466 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)                         \
467         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
468         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
469         i->private_rport_attrs[count].store = NULL;                     \
470         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
471         count++
472
473 #define SETUP_RPORT_ATTRIBUTE_RW(field)                                 \
474         i->private_rport_attrs[count] = class_device_attr_rport_##field; \
475         if (!i->f->set_rport_##field) {                                 \
476                 i->private_rport_attrs[count].attr.mode = S_IRUGO;      \
477                 i->private_rport_attrs[count].store = NULL;             \
478         }                                                               \
479         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
480         if (i->f->show_rport_##field)                                   \
481                 count++
482
483
484 /* The FC Transport Remote Port Attributes: */
485
486 /* Fixed Remote Port Attributes */
487
488 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
489
490 static ssize_t
491 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
492 {
493         struct fc_rport *rport = transport_class_to_rport(cdev);
494         if (rport->supported_classes == FC_COS_UNSPECIFIED)
495                 return snprintf(buf, 20, "unspecified\n");
496         return get_fc_cos_names(rport->supported_classes, buf);
497 }
498 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
499                 show_fc_rport_supported_classes, NULL);
500
501 /* Dynamic Remote Port Attributes */
502
503 fc_rport_rw_attr(dev_loss_tmo, "%d\n", 20);
504
505
506 /* Private Remote Port Attributes */
507
508 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
509 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
510 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
511
512 static ssize_t
513 show_fc_rport_roles (struct class_device *cdev, char *buf)
514 {
515         struct fc_rport *rport = transport_class_to_rport(cdev);
516
517         /* identify any roles that are port_id specific */
518         if ((rport->port_id != -1) &&
519             (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
520                                         FC_WELLKNOWN_PORTID_MASK) {
521                 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
522                 case FC_FPORT_PORTID:
523                         return snprintf(buf, 30, "Fabric Port\n");
524                 case FC_FABCTLR_PORTID:
525                         return snprintf(buf, 30, "Fabric Controller\n");
526                 case FC_DIRSRVR_PORTID:
527                         return snprintf(buf, 30, "Directory Server\n");
528                 case FC_TIMESRVR_PORTID:
529                         return snprintf(buf, 30, "Time Server\n");
530                 case FC_MGMTSRVR_PORTID:
531                         return snprintf(buf, 30, "Management Server\n");
532                 default:
533                         return snprintf(buf, 30, "Unknown Fabric Entity\n");
534                 }
535         } else {
536                 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
537                         return snprintf(buf, 20, "unknown\n");
538                 return get_fc_remote_port_roles_names(rport->roles, buf);
539         }
540 }
541 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
542                 show_fc_rport_roles, NULL);
543
544 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
545 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
546
547
548
549 /*
550  * FC SCSI Target Attribute Management
551  */
552
553 /*
554  * Note: in the target show function we recognize when the remote
555  *  port is in the heirarchy and do not allow the driver to get
556  *  involved in sysfs functions. The driver only gets involved if
557  *  it's the "old" style that doesn't use rports.
558  */
559 #define fc_starget_show_function(field, format_string, sz, cast)        \
560 static ssize_t                                                          \
561 show_fc_starget_##field (struct class_device *cdev, char *buf)          \
562 {                                                                       \
563         struct scsi_target *starget = transport_class_to_starget(cdev); \
564         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
565         struct fc_internal *i = to_fc_internal(shost->transportt);      \
566         struct fc_rport *rport = starget_to_rport(starget);             \
567         if (rport)                                                      \
568                 fc_starget_##field(starget) = rport->field;             \
569         else if (i->f->get_starget_##field)                             \
570                 i->f->get_starget_##field(starget);                     \
571         return snprintf(buf, sz, format_string,                         \
572                 cast fc_starget_##field(starget));                      \
573 }
574
575 #define fc_starget_rd_attr(field, format_string, sz)                    \
576         fc_starget_show_function(field, format_string, sz, )            \
577 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
578                          show_fc_starget_##field, NULL)
579
580 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)         \
581         fc_starget_show_function(field, format_string, sz, (cast))      \
582 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,                    \
583                           show_fc_starget_##field, NULL)
584
585 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
586         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
587         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
588         i->private_starget_attrs[count].store = NULL;                   \
589         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
590         if (i->f->show_starget_##field)                                 \
591                 count++
592
593 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
594         i->private_starget_attrs[count] = class_device_attr_starget_##field; \
595         if (!i->f->set_starget_##field) {                               \
596                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
597                 i->private_starget_attrs[count].store = NULL;           \
598         }                                                               \
599         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
600         if (i->f->show_starget_##field)                                 \
601                 count++
602
603 /* The FC Transport SCSI Target Attributes: */
604 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
605 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
606 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
607
608
609 /*
610  * Host Attribute Management
611  */
612
613 #define fc_host_show_function(field, format_string, sz, cast)           \
614 static ssize_t                                                          \
615 show_fc_host_##field (struct class_device *cdev, char *buf)             \
616 {                                                                       \
617         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
618         struct fc_internal *i = to_fc_internal(shost->transportt);      \
619         if (i->f->get_host_##field)                                     \
620                 i->f->get_host_##field(shost);                          \
621         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
622 }
623
624 #define fc_host_store_function(field)                                   \
625 static ssize_t                                                          \
626 store_fc_host_##field(struct class_device *cdev, const char *buf,       \
627                            size_t count)                                \
628 {                                                                       \
629         int val;                                                        \
630         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
631         struct fc_internal *i = to_fc_internal(shost->transportt);      \
632                                                                         \
633         val = simple_strtoul(buf, NULL, 0);                             \
634         i->f->set_host_##field(shost, val);                             \
635         return count;                                                   \
636 }
637
638 #define fc_host_rd_attr(field, format_string, sz)                       \
639         fc_host_show_function(field, format_string, sz, )               \
640 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
641                          show_fc_host_##field, NULL)
642
643 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
644         fc_host_show_function(field, format_string, sz, (cast))         \
645 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
646                           show_fc_host_##field, NULL)
647
648 #define fc_host_rw_attr(field, format_string, sz)                       \
649         fc_host_show_function(field, format_string, sz, )               \
650         fc_host_store_function(field)                                   \
651 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,             \
652                         show_fc_host_##field,                           \
653                         store_fc_host_##field)
654
655 #define fc_host_rd_enum_attr(title, maxlen)                             \
656 static ssize_t                                                          \
657 show_fc_host_##title (struct class_device *cdev, char *buf)             \
658 {                                                                       \
659         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
660         struct fc_internal *i = to_fc_internal(shost->transportt);      \
661         const char *name;                                               \
662         if (i->f->get_host_##title)                                     \
663                 i->f->get_host_##title(shost);                          \
664         name = get_fc_##title##_name(fc_host_##title(shost));           \
665         if (!name)                                                      \
666                 return -EINVAL;                                         \
667         return snprintf(buf, maxlen, "%s\n", name);                     \
668 }                                                                       \
669 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
670
671 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
672         i->private_host_attrs[count] = class_device_attr_host_##field;  \
673         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
674         i->private_host_attrs[count].store = NULL;                      \
675         i->host_attrs[count] = &i->private_host_attrs[count];           \
676         if (i->f->show_host_##field)                                    \
677                 count++
678
679 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
680         i->private_host_attrs[count] = class_device_attr_host_##field;  \
681         if (!i->f->set_host_##field) {                                  \
682                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
683                 i->private_host_attrs[count].store = NULL;              \
684         }                                                               \
685         i->host_attrs[count] = &i->private_host_attrs[count];           \
686         if (i->f->show_host_##field)                                    \
687                 count++
688
689
690 #define fc_private_host_show_function(field, format_string, sz, cast)   \
691 static ssize_t                                                          \
692 show_fc_host_##field (struct class_device *cdev, char *buf)             \
693 {                                                                       \
694         struct Scsi_Host *shost = transport_class_to_shost(cdev);       \
695         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
696 }
697
698 #define fc_private_host_rd_attr(field, format_string, sz)               \
699         fc_private_host_show_function(field, format_string, sz, )       \
700 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
701                          show_fc_host_##field, NULL)
702
703 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
704         fc_private_host_show_function(field, format_string, sz, (cast)) \
705 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,                       \
706                           show_fc_host_##field, NULL)
707
708 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
709         i->private_host_attrs[count] = class_device_attr_host_##field;  \
710         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
711         i->private_host_attrs[count].store = NULL;                      \
712         i->host_attrs[count] = &i->private_host_attrs[count];           \
713         count++
714
715 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
716 {                                                                       \
717         i->private_host_attrs[count] = class_device_attr_host_##field;  \
718         i->host_attrs[count] = &i->private_host_attrs[count];           \
719         count++;                                                        \
720 }
721
722
723 /* Fixed Host Attributes */
724
725 static ssize_t
726 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
727 {
728         struct Scsi_Host *shost = transport_class_to_shost(cdev);
729
730         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
731                 return snprintf(buf, 20, "unspecified\n");
732
733         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
734 }
735 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
736                 show_fc_host_supported_classes, NULL);
737
738 static ssize_t
739 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
740 {
741         struct Scsi_Host *shost = transport_class_to_shost(cdev);
742         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
743 }
744 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
745                 show_fc_host_supported_fc4s, NULL);
746
747 static ssize_t
748 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
749 {
750         struct Scsi_Host *shost = transport_class_to_shost(cdev);
751
752         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
753                 return snprintf(buf, 20, "unknown\n");
754
755         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
756 }
757 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
758                 show_fc_host_supported_speeds, NULL);
759
760
761 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
762 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
763 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
764 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
765 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
766
767
768 /* Dynamic Host Attributes */
769
770 static ssize_t
771 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
772 {
773         struct Scsi_Host *shost = transport_class_to_shost(cdev);
774         struct fc_internal *i = to_fc_internal(shost->transportt);
775
776         if (i->f->get_host_active_fc4s)
777                 i->f->get_host_active_fc4s(shost);
778
779         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
780 }
781 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
782                 show_fc_host_active_fc4s, NULL);
783
784 static ssize_t
785 show_fc_host_speed (struct class_device *cdev, char *buf)
786 {
787         struct Scsi_Host *shost = transport_class_to_shost(cdev);
788         struct fc_internal *i = to_fc_internal(shost->transportt);
789
790         if (i->f->get_host_speed)
791                 i->f->get_host_speed(shost);
792
793         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
794                 return snprintf(buf, 20, "unknown\n");
795
796         return get_fc_port_speed_names(fc_host_speed(shost), buf);
797 }
798 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
799                 show_fc_host_speed, NULL);
800
801
802 fc_host_rd_attr(port_id, "0x%06x\n", 20);
803 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
804 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
805 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
806
807
808 /* Private Host Attributes */
809
810 static ssize_t
811 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
812 {
813         struct Scsi_Host *shost = transport_class_to_shost(cdev);
814         const char *name;
815
816         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
817         if (!name)
818                 return -EINVAL;
819         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
820 }
821
822 #define get_list_head_entry(pos, head, member)          \
823         pos = list_entry((head)->next, typeof(*pos), member)
824
825 static ssize_t
826 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
827         const char *buf, size_t count)
828 {
829         struct Scsi_Host *shost = transport_class_to_shost(cdev);
830         struct fc_rport *rport;
831         enum fc_tgtid_binding_type val;
832         unsigned long flags;
833
834         if (get_fc_tgtid_bind_type_match(buf, &val))
835                 return -EINVAL;
836
837         /* if changing bind type, purge all unused consistent bindings */
838         if (val != fc_host_tgtid_bind_type(shost)) {
839                 spin_lock_irqsave(shost->host_lock, flags);
840                 while (!list_empty(&fc_host_rport_bindings(shost))) {
841                         get_list_head_entry(rport,
842                                 &fc_host_rport_bindings(shost), peers);
843                         spin_unlock_irqrestore(shost->host_lock, flags);
844                         fc_rport_terminate(rport);
845                         spin_lock_irqsave(shost->host_lock, flags);
846                 }
847                 spin_unlock_irqrestore(shost->host_lock, flags);
848         }
849
850         fc_host_tgtid_bind_type(shost) = val;
851         return count;
852 }
853
854 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
855                         show_fc_private_host_tgtid_bind_type,
856                         store_fc_private_host_tgtid_bind_type);
857
858 static ssize_t
859 store_fc_private_host_issue_lip(struct class_device *cdev,
860         const char *buf, size_t count)
861 {
862         struct Scsi_Host *shost = transport_class_to_shost(cdev);
863         struct fc_internal *i = to_fc_internal(shost->transportt);
864         int ret;
865
866         /* ignore any data value written to the attribute */
867         if (i->f->issue_fc_host_lip) {
868                 ret = i->f->issue_fc_host_lip(shost);
869                 return ret ? ret: count;
870         }
871
872         return -ENOENT;
873 }
874
875 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
876                         store_fc_private_host_issue_lip);
877
878 /*
879  * Host Statistics Management
880  */
881
882 /* Show a given an attribute in the statistics group */
883 static ssize_t
884 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
885 {
886         struct Scsi_Host *shost = transport_class_to_shost(cdev);
887         struct fc_internal *i = to_fc_internal(shost->transportt);
888         struct fc_host_statistics *stats;
889         ssize_t ret = -ENOENT;
890
891         if (offset > sizeof(struct fc_host_statistics) ||
892             offset % sizeof(u64) != 0)
893                 WARN_ON(1);
894
895         if (i->f->get_fc_host_stats) {
896                 stats = (i->f->get_fc_host_stats)(shost);
897                 if (stats)
898                         ret = snprintf(buf, 20, "0x%llx\n",
899                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
900         }
901         return ret;
902 }
903
904
905 /* generate a read-only statistics attribute */
906 #define fc_host_statistic(name)                                         \
907 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf)   \
908 {                                                                       \
909         return fc_stat_show(cd, buf,                                    \
910                             offsetof(struct fc_host_statistics, name)); \
911 }                                                                       \
912 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
913
914 fc_host_statistic(seconds_since_last_reset);
915 fc_host_statistic(tx_frames);
916 fc_host_statistic(tx_words);
917 fc_host_statistic(rx_frames);
918 fc_host_statistic(rx_words);
919 fc_host_statistic(lip_count);
920 fc_host_statistic(nos_count);
921 fc_host_statistic(error_frames);
922 fc_host_statistic(dumped_frames);
923 fc_host_statistic(link_failure_count);
924 fc_host_statistic(loss_of_sync_count);
925 fc_host_statistic(loss_of_signal_count);
926 fc_host_statistic(prim_seq_protocol_err_count);
927 fc_host_statistic(invalid_tx_word_count);
928 fc_host_statistic(invalid_crc_count);
929 fc_host_statistic(fcp_input_requests);
930 fc_host_statistic(fcp_output_requests);
931 fc_host_statistic(fcp_control_requests);
932 fc_host_statistic(fcp_input_megabytes);
933 fc_host_statistic(fcp_output_megabytes);
934
935 static ssize_t
936 fc_reset_statistics(struct class_device *cdev, const char *buf,
937                            size_t count)
938 {
939         struct Scsi_Host *shost = transport_class_to_shost(cdev);
940         struct fc_internal *i = to_fc_internal(shost->transportt);
941
942         /* ignore any data value written to the attribute */
943         if (i->f->reset_fc_host_stats) {
944                 i->f->reset_fc_host_stats(shost);
945                 return count;
946         }
947
948         return -ENOENT;
949 }
950 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
951                                 fc_reset_statistics);
952
953
954 static struct attribute *fc_statistics_attrs[] = {
955         &class_device_attr_host_seconds_since_last_reset.attr,
956         &class_device_attr_host_tx_frames.attr,
957         &class_device_attr_host_tx_words.attr,
958         &class_device_attr_host_rx_frames.attr,
959         &class_device_attr_host_rx_words.attr,
960         &class_device_attr_host_lip_count.attr,
961         &class_device_attr_host_nos_count.attr,
962         &class_device_attr_host_error_frames.attr,
963         &class_device_attr_host_dumped_frames.attr,
964         &class_device_attr_host_link_failure_count.attr,
965         &class_device_attr_host_loss_of_sync_count.attr,
966         &class_device_attr_host_loss_of_signal_count.attr,
967         &class_device_attr_host_prim_seq_protocol_err_count.attr,
968         &class_device_attr_host_invalid_tx_word_count.attr,
969         &class_device_attr_host_invalid_crc_count.attr,
970         &class_device_attr_host_fcp_input_requests.attr,
971         &class_device_attr_host_fcp_output_requests.attr,
972         &class_device_attr_host_fcp_control_requests.attr,
973         &class_device_attr_host_fcp_input_megabytes.attr,
974         &class_device_attr_host_fcp_output_megabytes.attr,
975         &class_device_attr_host_reset_statistics.attr,
976         NULL
977 };
978
979 static struct attribute_group fc_statistics_group = {
980         .name = "statistics",
981         .attrs = fc_statistics_attrs,
982 };
983
984 static int fc_host_match(struct attribute_container *cont,
985                           struct device *dev)
986 {
987         struct Scsi_Host *shost;
988         struct fc_internal *i;
989
990         if (!scsi_is_host_device(dev))
991                 return 0;
992
993         shost = dev_to_shost(dev);
994         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
995             != &fc_host_class.class)
996                 return 0;
997
998         i = to_fc_internal(shost->transportt);
999
1000         return &i->t.host_attrs.ac == cont;
1001 }
1002
1003 static int fc_target_match(struct attribute_container *cont,
1004                             struct device *dev)
1005 {
1006         struct Scsi_Host *shost;
1007         struct fc_internal *i;
1008
1009         if (!scsi_is_target_device(dev))
1010                 return 0;
1011
1012         shost = dev_to_shost(dev->parent);
1013         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1014             != &fc_host_class.class)
1015                 return 0;
1016
1017         i = to_fc_internal(shost->transportt);
1018
1019         return &i->t.target_attrs.ac == cont;
1020 }
1021
1022 static void fc_rport_dev_release(struct device *dev)
1023 {
1024         struct fc_rport *rport = dev_to_rport(dev);
1025         put_device(dev->parent);
1026         kfree(rport);
1027 }
1028
1029 int scsi_is_fc_rport(const struct device *dev)
1030 {
1031         return dev->release == fc_rport_dev_release;
1032 }
1033 EXPORT_SYMBOL(scsi_is_fc_rport);
1034
1035 static int fc_rport_match(struct attribute_container *cont,
1036                             struct device *dev)
1037 {
1038         struct Scsi_Host *shost;
1039         struct fc_internal *i;
1040
1041         if (!scsi_is_fc_rport(dev))
1042                 return 0;
1043
1044         shost = dev_to_shost(dev->parent);
1045         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1046             != &fc_host_class.class)
1047                 return 0;
1048
1049         i = to_fc_internal(shost->transportt);
1050
1051         return &i->rport_attr_cont.ac == cont;
1052 }
1053
1054
1055 /*
1056  * Must be called with shost->host_lock held
1057  */
1058 static struct device *fc_target_parent(struct Scsi_Host *shost,
1059                                         int channel, uint id)
1060 {
1061         struct fc_rport *rport;
1062
1063         list_for_each_entry(rport, &fc_host_rports(shost), peers)
1064                 if ((rport->channel == channel) &&
1065                     (rport->scsi_target_id == id))
1066                         return &rport->dev;
1067
1068         return NULL;
1069 }
1070
1071 struct scsi_transport_template *
1072 fc_attach_transport(struct fc_function_template *ft)
1073 {
1074         struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1075                                         GFP_KERNEL);
1076         int count;
1077
1078         if (unlikely(!i))
1079                 return NULL;
1080
1081         memset(i, 0, sizeof(struct fc_internal));
1082
1083         i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1084         i->t.target_attrs.ac.class = &fc_transport_class.class;
1085         i->t.target_attrs.ac.match = fc_target_match;
1086         i->t.target_size = sizeof(struct fc_starget_attrs);
1087         transport_container_register(&i->t.target_attrs);
1088
1089         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1090         i->t.host_attrs.ac.class = &fc_host_class.class;
1091         i->t.host_attrs.ac.match = fc_host_match;
1092         i->t.host_size = sizeof(struct fc_host_attrs);
1093         if (ft->get_fc_host_stats)
1094                 i->t.host_attrs.statistics = &fc_statistics_group;
1095         transport_container_register(&i->t.host_attrs);
1096
1097         i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1098         i->rport_attr_cont.ac.class = &fc_rport_class.class;
1099         i->rport_attr_cont.ac.match = fc_rport_match;
1100         transport_container_register(&i->rport_attr_cont);
1101
1102         i->f = ft;
1103
1104         /* Transport uses the shost workq for scsi scanning */
1105         i->t.create_work_queue = 1;
1106
1107         i->t.target_parent = fc_target_parent;
1108         
1109         /*
1110          * Setup SCSI Target Attributes.
1111          */
1112         count = 0;
1113         SETUP_STARGET_ATTRIBUTE_RD(node_name);
1114         SETUP_STARGET_ATTRIBUTE_RD(port_name);
1115         SETUP_STARGET_ATTRIBUTE_RD(port_id);
1116
1117         BUG_ON(count > FC_STARGET_NUM_ATTRS);
1118
1119         i->starget_attrs[count] = NULL;
1120
1121
1122         /*
1123          * Setup SCSI Host Attributes.
1124          */
1125         count=0;
1126         SETUP_HOST_ATTRIBUTE_RD(node_name);
1127         SETUP_HOST_ATTRIBUTE_RD(port_name);
1128         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1129         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1130         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1131         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1132         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1133         SETUP_HOST_ATTRIBUTE_RD(serial_number);
1134
1135         SETUP_HOST_ATTRIBUTE_RD(port_id);
1136         SETUP_HOST_ATTRIBUTE_RD(port_type);
1137         SETUP_HOST_ATTRIBUTE_RD(port_state);
1138         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1139         SETUP_HOST_ATTRIBUTE_RD(speed);
1140         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1141
1142         /* Transport-managed attributes */
1143         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1144         if (ft->issue_fc_host_lip)
1145                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1146
1147         BUG_ON(count > FC_HOST_NUM_ATTRS);
1148
1149         i->host_attrs[count] = NULL;
1150
1151         /*
1152          * Setup Remote Port Attributes.
1153          */
1154         count=0;
1155         SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1156         SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1157         SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1158         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1159         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1160         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1161         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1162         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1163         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1164
1165         BUG_ON(count > FC_RPORT_NUM_ATTRS);
1166
1167         i->rport_attrs[count] = NULL;
1168
1169         return &i->t;
1170 }
1171 EXPORT_SYMBOL(fc_attach_transport);
1172
1173 void fc_release_transport(struct scsi_transport_template *t)
1174 {
1175         struct fc_internal *i = to_fc_internal(t);
1176
1177         transport_container_unregister(&i->t.target_attrs);
1178         transport_container_unregister(&i->t.host_attrs);
1179         transport_container_unregister(&i->rport_attr_cont);
1180
1181         kfree(i);
1182 }
1183 EXPORT_SYMBOL(fc_release_transport);
1184
1185
1186 /**
1187  * fc_remove_host - called to terminate any fc_transport-related elements
1188  *                  for a scsi host.
1189  * @rport:      remote port to be unblocked.
1190  *
1191  * This routine is expected to be called immediately preceeding the
1192  * a driver's call to scsi_remove_host().
1193  *
1194  * WARNING: A driver utilizing the fc_transport, which fails to call
1195  *   this routine prior to scsi_remote_host(), will leave dangling
1196  *   objects in /sys/class/fc_remote_ports. Access to any of these
1197  *   objects can result in a system crash !!!
1198  *
1199  * Notes:
1200  *      This routine assumes no locks are held on entry.
1201  **/
1202 void
1203 fc_remove_host(struct Scsi_Host *shost)
1204 {
1205         struct fc_rport *rport, *next_rport;
1206
1207         /* Remove any remote ports */
1208         list_for_each_entry_safe(rport, next_rport,
1209                         &fc_host_rports(shost), peers)
1210                 fc_rport_terminate(rport);
1211         list_for_each_entry_safe(rport, next_rport,
1212                         &fc_host_rport_bindings(shost), peers)
1213                 fc_rport_terminate(rport);
1214 }
1215 EXPORT_SYMBOL(fc_remove_host);
1216
1217 /**
1218  * fc_rport_create - allocates and creates a remote FC port.
1219  * @shost:      scsi host the remote port is connected to.
1220  * @channel:    Channel on shost port connected to.
1221  * @ids:        The world wide names, fc address, and FC4 port
1222  *              roles for the remote port.
1223  *
1224  * Allocates and creates the remoter port structure, including the
1225  * class and sysfs creation.
1226  *
1227  * Notes:
1228  *      This routine assumes no locks are held on entry.
1229  **/
1230 struct fc_rport *
1231 fc_rport_create(struct Scsi_Host *shost, int channel,
1232         struct fc_rport_identifiers  *ids)
1233 {
1234         struct fc_host_attrs *fc_host =
1235                         (struct fc_host_attrs *)shost->shost_data;
1236         struct fc_internal *fci = to_fc_internal(shost->transportt);
1237         struct fc_rport *rport;
1238         struct device *dev;
1239         unsigned long flags;
1240         int error;
1241         size_t size;
1242
1243         size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1244         rport = kmalloc(size, GFP_KERNEL);
1245         if (unlikely(!rport)) {
1246                 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1247                 return NULL;
1248         }
1249         memset(rport, 0, size);
1250
1251         rport->maxframe_size = -1;
1252         rport->supported_classes = FC_COS_UNSPECIFIED;
1253         rport->dev_loss_tmo = fc_dev_loss_tmo;
1254         memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1255         memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1256         rport->port_id = ids->port_id;
1257         rport->roles = ids->roles;
1258         rport->port_state = FC_PORTSTATE_ONLINE;
1259         if (fci->f->dd_fcrport_size)
1260                 rport->dd_data = &rport[1];
1261         rport->channel = channel;
1262
1263         INIT_WORK(&rport->dev_loss_work, fc_timeout_blocked_rport, rport);
1264         INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1265
1266         spin_lock_irqsave(shost->host_lock, flags);
1267
1268         rport->number = fc_host->next_rport_number++;
1269         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1270                 rport->scsi_target_id = fc_host->next_target_id++;
1271         else
1272                 rport->scsi_target_id = -1;
1273         list_add_tail(&rport->peers, &fc_host_rports(shost));
1274         get_device(&shost->shost_gendev);
1275
1276         spin_unlock_irqrestore(shost->host_lock, flags);
1277
1278         dev = &rport->dev;
1279         device_initialize(dev);
1280         dev->parent = get_device(&shost->shost_gendev);
1281         dev->release = fc_rport_dev_release;
1282         sprintf(dev->bus_id, "rport-%d:%d-%d",
1283                 shost->host_no, channel, rport->number);
1284         transport_setup_device(dev);
1285
1286         error = device_add(dev);
1287         if (error) {
1288                 printk(KERN_ERR "FC Remote Port device_add failed\n");
1289                 goto delete_rport;
1290         }
1291         transport_add_device(dev);
1292         transport_configure_device(dev);
1293
1294         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1295                 /* initiate a scan of the target */
1296                 scsi_queue_work(shost, &rport->scan_work);
1297
1298         return rport;
1299
1300 delete_rport:
1301         transport_destroy_device(dev);
1302         put_device(dev->parent);
1303         spin_lock_irqsave(shost->host_lock, flags);
1304         list_del(&rport->peers);
1305         put_device(&shost->shost_gendev);
1306         spin_unlock_irqrestore(shost->host_lock, flags);
1307         put_device(dev->parent);
1308         kfree(rport);
1309         return NULL;
1310 }
1311
1312 /**
1313  * fc_remote_port_add - notifies the fc transport of the existence
1314  *              of a remote FC port.
1315  * @shost:      scsi host the remote port is connected to.
1316  * @channel:    Channel on shost port connected to.
1317  * @ids:        The world wide names, fc address, and FC4 port
1318  *              roles for the remote port.
1319  *
1320  * The LLDD calls this routine to notify the transport of the existence
1321  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1322  * of the port, it's FC address (port_id), and the FC4 roles that are
1323  * active for the port.
1324  *
1325  * For ports that are FCP targets (aka scsi targets), the FC transport
1326  * maintains consistent target id bindings on behalf of the LLDD.
1327  * A consistent target id binding is an assignment of a target id to
1328  * a remote port identifier, which persists while the scsi host is
1329  * attached. The remote port can disappear, then later reappear, and
1330  * it's target id assignment remains the same. This allows for shifts
1331  * in FC addressing (if binding by wwpn or wwnn) with no apparent
1332  * changes to the scsi subsystem which is based on scsi host number and
1333  * target id values.  Bindings are only valid during the attachment of
1334  * the scsi host. If the host detaches, then later re-attaches, target
1335  * id bindings may change.
1336  *
1337  * This routine is responsible for returning a remote port structure.
1338  * The routine will search the list of remote ports it maintains
1339  * internally on behalf of consistent target id mappings. If found, the
1340  * remote port structure will be reused. Otherwise, a new remote port
1341  * structure will be allocated.
1342  *
1343  * Whenever a remote port is allocated, a new fc_remote_port class
1344  * device is created.
1345  *
1346  * Should not be called from interrupt context.
1347  *
1348  * Notes:
1349  *      This routine assumes no locks are held on entry.
1350  **/
1351 struct fc_rport *
1352 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1353         struct fc_rport_identifiers  *ids)
1354 {
1355         struct fc_rport *rport;
1356         unsigned long flags;
1357         int match = 0;
1358
1359         if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1360                 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1361
1362                 /* search for a matching consistent binding */
1363
1364                 spin_lock_irqsave(shost->host_lock, flags);
1365
1366                 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1367                                         peers) {
1368                         if (rport->channel != channel)
1369                                 continue;
1370
1371                         switch (fc_host_tgtid_bind_type(shost)) {
1372                         case FC_TGTID_BIND_BY_WWPN:
1373                                 if (rport->port_name == ids->port_name)
1374                                         match = 1;
1375                                 break;
1376                         case FC_TGTID_BIND_BY_WWNN:
1377                                 if (rport->node_name == ids->node_name)
1378                                         match = 1;
1379                                 break;
1380                         case FC_TGTID_BIND_BY_ID:
1381                                 if (rport->port_id == ids->port_id)
1382                                         match = 1;
1383                                 break;
1384                         case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1385                                 break;
1386                         }
1387
1388                         if (match) {
1389                                 list_move_tail(&rport->peers,
1390                                         &fc_host_rports(shost));
1391                                 break;
1392                         }
1393                 }
1394
1395                 spin_unlock_irqrestore(shost->host_lock, flags);
1396
1397                 if (match) {
1398                         memcpy(&rport->node_name, &ids->node_name,
1399                                 sizeof(rport->node_name));
1400                         memcpy(&rport->port_name, &ids->port_name,
1401                                 sizeof(rport->port_name));
1402                         rport->port_id = ids->port_id;
1403                         rport->roles = ids->roles;
1404                         rport->port_state = FC_PORTSTATE_ONLINE;
1405
1406                         if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1407                                 /* initiate a scan of the target */
1408                                 scsi_queue_work(shost, &rport->scan_work);
1409
1410                         return rport;
1411                 }
1412         }
1413
1414         /* No consistent binding found - create new remote port entry */
1415         rport = fc_rport_create(shost, channel, ids);
1416
1417         return rport;
1418 }
1419 EXPORT_SYMBOL(fc_remote_port_add);
1420
1421 /*
1422  * fc_rport_tgt_remove - Removes the scsi target on the remote port
1423  * @rport:      The remote port to be operated on
1424  */
1425 static void
1426 fc_rport_tgt_remove(struct fc_rport *rport)
1427 {
1428         struct Scsi_Host *shost = rport_to_shost(rport);
1429
1430         scsi_target_unblock(&rport->dev);
1431
1432         /* Stop anything on the workq */
1433         if (!cancel_delayed_work(&rport->dev_loss_work))
1434                 flush_scheduled_work();
1435         scsi_flush_work(shost);
1436
1437         scsi_remove_target(&rport->dev);
1438 }
1439
1440 /*
1441  * fc_rport_terminate - this routine tears down and deallocates a remote port.
1442  * @rport:      The remote port to be terminated
1443  *
1444  * Notes:
1445  *      This routine assumes no locks are held on entry.
1446  */
1447 static void
1448 fc_rport_terminate(struct fc_rport  *rport)
1449 {
1450         struct Scsi_Host *shost = rport_to_shost(rport);
1451         struct device *dev = &rport->dev;
1452         unsigned long flags;
1453
1454         fc_rport_tgt_remove(rport);
1455
1456         transport_remove_device(dev);
1457         device_del(dev);
1458         transport_destroy_device(dev);
1459         spin_lock_irqsave(shost->host_lock, flags);
1460         list_del(&rport->peers);
1461         spin_unlock_irqrestore(shost->host_lock, flags);
1462         put_device(&shost->shost_gendev);
1463 }
1464
1465 /**
1466  * fc_remote_port_delete - notifies the fc transport that a remote
1467  *              port is no longer in existence.
1468  * @rport:      The remote port that no longer exists
1469  *
1470  * The LLDD calls this routine to notify the transport that a remote
1471  * port is no longer part of the topology. Note: Although a port
1472  * may no longer be part of the topology, it may persist in the remote
1473  * ports displayed by the fc_host. This is done so that target id
1474  * mappings (managed via the remote port structures), are always visible
1475  * as long as the mapping is valid, regardless of port state,
1476  *
1477  * If the remote port is not an FCP Target, it will be fully torn down
1478  * and deallocated, including the fc_remote_port class device.
1479  *
1480  * If the remote port is an FCP Target, the port structure will be
1481  * marked as Not Present, but will remain as long as there is a valid
1482  * SCSI target id mapping associated with the port structure. Validity
1483  * is determined by the binding type. If binding by wwpn, then the port
1484  * structure is always valid and will not be deallocated until the host
1485  * is removed.  If binding by wwnn, then the port structure is valid
1486  * until another port with the same node name is found in the topology.
1487  * If binding by port id (fc address), then the port structure is valid
1488  * valid until another port with the same address is identified.
1489  *
1490  * Called from interrupt or normal process context.
1491  *
1492  * Notes:
1493  *      This routine assumes no locks are held on entry.
1494  **/
1495 void
1496 fc_remote_port_delete(struct fc_rport  *rport)
1497 {
1498         struct Scsi_Host *shost = rport_to_shost(rport);
1499         unsigned long flags;
1500
1501         /* If no scsi target id mapping or consistent binding type, delete it */
1502         if ((rport->scsi_target_id == -1) ||
1503             (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE)) {
1504                 fc_rport_terminate(rport);
1505                 return;
1506         }
1507
1508         fc_rport_tgt_remove(rport);
1509
1510         spin_lock_irqsave(shost->host_lock, flags);
1511         list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1512         spin_unlock_irqrestore(shost->host_lock, flags);
1513
1514         /*
1515          * Note: We do not remove or clear the hostdata area. This allows
1516          *   host-specific target data to persist along with the
1517          *   scsi_target_id. It's up to the host to manage it's hostdata area.
1518          */
1519
1520         /*
1521          * Reinitialize port attributes that may change if the port comes back.
1522          */
1523         rport->maxframe_size = -1;
1524         rport->supported_classes = FC_COS_UNSPECIFIED;
1525         rport->roles = FC_RPORT_ROLE_UNKNOWN;
1526         rport->port_state = FC_PORTSTATE_NOTPRESENT;
1527
1528         /* remove the identifiers that aren't used in the consisting binding */
1529         switch (fc_host_tgtid_bind_type(shost)) {
1530         case FC_TGTID_BIND_BY_WWPN:
1531                 rport->node_name = -1;
1532                 rport->port_id = -1;
1533                 break;
1534         case FC_TGTID_BIND_BY_WWNN:
1535                 rport->port_name = -1;
1536                 rport->port_id = -1;
1537                 break;
1538         case FC_TGTID_BIND_BY_ID:
1539                 rport->node_name = -1;
1540                 rport->port_name = -1;
1541                 break;
1542         case FC_TGTID_BIND_NONE:        /* to keep compiler happy */
1543                 break;
1544         }
1545 }
1546 EXPORT_SYMBOL(fc_remote_port_delete);
1547
1548 /**
1549  * fc_remote_port_rolechg - notifies the fc transport that the roles
1550  *              on a remote may have changed.
1551  * @rport:      The remote port that changed.
1552  *
1553  * The LLDD calls this routine to notify the transport that the roles
1554  * on a remote port may have changed. The largest effect of this is
1555  * if a port now becomes a FCP Target, it must be allocated a
1556  * scsi target id.  If the port is no longer a FCP target, any
1557  * scsi target id value assigned to it will persist in case the
1558  * role changes back to include FCP Target. No changes in the scsi
1559  * midlayer will be invoked if the role changes (in the expectation
1560  * that the role will be resumed. If it doesn't normal error processing
1561  * will take place).
1562  *
1563  * Should not be called from interrupt context.
1564  *
1565  * Notes:
1566  *      This routine assumes no locks are held on entry.
1567  **/
1568 void
1569 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
1570 {
1571         struct Scsi_Host *shost = rport_to_shost(rport);
1572         struct fc_host_attrs *fc_host =
1573                         (struct fc_host_attrs *)shost->shost_data;
1574         unsigned long flags;
1575         int create = 0;
1576
1577         rport->roles = roles;
1578
1579         spin_lock_irqsave(shost->host_lock, flags);
1580         if ((rport->scsi_target_id == -1) &&
1581             (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
1582                 rport->scsi_target_id = fc_host->next_target_id++;
1583                 create = 1;
1584         }
1585         spin_unlock_irqrestore(shost->host_lock, flags);
1586
1587         if (create)
1588                 /* initiate a scan of the target */
1589                 scsi_queue_work(shost, &rport->scan_work);
1590 }
1591 EXPORT_SYMBOL(fc_remote_port_rolechg);
1592
1593 /**
1594  * fc_timeout_blocked_rport - Timeout handler for blocked remote port
1595  *                       that fails to return in the alloted time.
1596  * @data:       scsi target that failed to reappear in the alloted time.
1597  **/
1598 static void
1599 fc_timeout_blocked_rport(void  *data)
1600 {
1601         struct fc_rport *rport = (struct fc_rport *)data;
1602
1603         rport->port_state = FC_PORTSTATE_OFFLINE;
1604
1605         dev_printk(KERN_ERR, &rport->dev,
1606                 "blocked FC remote port time out: removing target\n");
1607
1608         /*
1609          * As this only occurs if the remote port (scsi target)
1610          * went away and didn't come back - we'll remove
1611          * all attached scsi devices.
1612          */
1613         scsi_target_unblock(&rport->dev);
1614         scsi_remove_target(&rport->dev);
1615 }
1616
1617 /**
1618  * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
1619  * @rport:      remote port to be blocked.
1620  *
1621  * scsi lldd's with a FC transport call this routine to temporarily stop
1622  * all scsi traffic to a remote port. If the port is not a SCSI target,
1623  * no action is taken. If the port is a SCSI target, all attached devices
1624  * are placed into a SDEV_BLOCK state and a timer is started. The timer is
1625  * represents the maximum amount of time the port may be blocked. If the
1626  * timer expires, the port is considered non-existent and the attached
1627  * scsi devices will be removed.
1628  *
1629  * Called from interrupt or normal process context.
1630  *
1631  * Returns zero if successful or error if not
1632  *
1633  * Notes:
1634  *      This routine assumes no locks are held on entry.
1635  *
1636  *      The timeout and timer types are extracted from the fc transport 
1637  *      attributes from the caller's rport pointer.
1638  **/
1639 int
1640 fc_remote_port_block(struct fc_rport *rport)
1641 {
1642         int timeout = rport->dev_loss_tmo;
1643         struct work_struct *work = &rport->dev_loss_work;
1644
1645         if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1646                 return -EINVAL;
1647
1648         scsi_target_block(&rport->dev);
1649
1650         /* cap the length the devices can be blocked */
1651         schedule_delayed_work(work, timeout * HZ);
1652
1653         rport->port_state = FC_PORTSTATE_BLOCKED;
1654         return 0;
1655 }
1656 EXPORT_SYMBOL(fc_remote_port_block);
1657
1658 /**
1659  * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
1660  * @rport:      remote port to be unblocked.
1661  *
1662  * scsi lld's with a FC transport call this routine to restart IO to all
1663  * devices associated with the caller's scsi target following a fc_target_block
1664  * request.  Called from interrupt or normal process context.
1665  *
1666  * Notes:
1667  *      This routine assumes no locks are held on entry.
1668  **/
1669  void
1670 fc_remote_port_unblock(struct fc_rport *rport)
1671 {
1672         struct work_struct *work = &rport->dev_loss_work;
1673         struct Scsi_Host *shost = rport_to_shost(rport);
1674
1675         /*
1676          * Stop the target timer first. Take no action on the del_timer
1677          * failure as the state machine state change will validate the
1678          * transaction.
1679          */
1680         if (!cancel_delayed_work(work))
1681                 flush_scheduled_work();
1682
1683         if (rport->port_state == FC_PORTSTATE_OFFLINE)
1684                 /*
1685                  * initiate a scan of the target as the target has
1686                  * been torn down.
1687                  */
1688                 scsi_queue_work(shost, &rport->scan_work);
1689         else
1690                 scsi_target_unblock(&rport->dev);
1691
1692         rport->port_state = FC_PORTSTATE_ONLINE;
1693 }
1694 EXPORT_SYMBOL(fc_remote_port_unblock);
1695
1696 /**
1697  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1698  * @data:       remote port to be scanned.
1699  **/
1700 static void
1701 fc_scsi_scan_rport(void *data)
1702 {
1703         struct fc_rport *rport = (struct fc_rport *)data;
1704
1705         scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1706                         SCAN_WILD_CARD, 1);
1707 }
1708
1709
1710 MODULE_AUTHOR("Martin Hicks");
1711 MODULE_DESCRIPTION("FC Transport Attributes");
1712 MODULE_LICENSE("GPL");
1713
1714 module_init(fc_transport_init);
1715 module_exit(fc_transport_exit);