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