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