driver core: remove KOBJ_NAME_LEN define
[linux-2.6.git] / 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-2007   James Smart, Emulex Corporation
23  *    Rewrite for host, target, device, and remote port attributes,
24  *    statistics, and service functions...
25  *    Add vports, etc
26  *
27  */
28 #include <linux/module.h>
29 #include <linux/init.h>
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 #include "scsi_transport_fc_internal.h"
40
41 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
42 static void fc_vport_sched_delete(struct work_struct *work);
43
44 /*
45  * This is a temporary carrier for creating a vport. It will eventually
46  * be replaced  by a real message definition for sgio or netlink.
47  *
48  * fc_vport_identifiers: This set of data contains all elements
49  * to uniquely identify and instantiate a FC virtual port.
50  *
51  * Notes:
52  *   symbolic_name: The driver is to append the symbolic_name string data
53  *      to the symbolic_node_name data that it generates by default.
54  *      the resulting combination should then be registered with the switch.
55  *      It is expected that things like Xen may stuff a VM title into
56  *      this field.
57  */
58 struct fc_vport_identifiers {
59         u64 node_name;
60         u64 port_name;
61         u32 roles;
62         bool disable;
63         enum fc_port_type vport_type;   /* only FC_PORTTYPE_NPIV allowed */
64         char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
65 };
66
67 static int fc_vport_create(struct Scsi_Host *shost, int channel,
68         struct device *pdev, struct fc_vport_identifiers  *ids,
69         struct fc_vport **vport);
70
71 /*
72  * Redefine so that we can have same named attributes in the
73  * sdev/starget/host objects.
74  */
75 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)                \
76 struct device_attribute device_attr_##_prefix##_##_name =       \
77         __ATTR(_name,_mode,_show,_store)
78
79 #define fc_enum_name_search(title, table_type, table)                   \
80 static const char *get_fc_##title##_name(enum table_type table_key)     \
81 {                                                                       \
82         int i;                                                          \
83         char *name = NULL;                                              \
84                                                                         \
85         for (i = 0; i < ARRAY_SIZE(table); i++) {                       \
86                 if (table[i].value == table_key) {                      \
87                         name = table[i].name;                           \
88                         break;                                          \
89                 }                                                       \
90         }                                                               \
91         return name;                                                    \
92 }
93
94 #define fc_enum_name_match(title, table_type, table)                    \
95 static int get_fc_##title##_match(const char *table_key,                \
96                 enum table_type *value)                                 \
97 {                                                                       \
98         int i;                                                          \
99                                                                         \
100         for (i = 0; i < ARRAY_SIZE(table); i++) {                       \
101                 if (strncmp(table_key, table[i].name,                   \
102                                 table[i].matchlen) == 0) {              \
103                         *value = table[i].value;                        \
104                         return 0; /* success */                         \
105                 }                                                       \
106         }                                                               \
107         return 1; /* failure */                                         \
108 }
109
110
111 /* Convert fc_port_type values to ascii string name */
112 static struct {
113         enum fc_port_type       value;
114         char                    *name;
115 } fc_port_type_names[] = {
116         { FC_PORTTYPE_UNKNOWN,          "Unknown" },
117         { FC_PORTTYPE_OTHER,            "Other" },
118         { FC_PORTTYPE_NOTPRESENT,       "Not Present" },
119         { FC_PORTTYPE_NPORT,    "NPort (fabric via point-to-point)" },
120         { FC_PORTTYPE_NLPORT,   "NLPort (fabric via loop)" },
121         { FC_PORTTYPE_LPORT,    "LPort (private loop)" },
122         { FC_PORTTYPE_PTP,      "Point-To-Point (direct nport connection" },
123         { FC_PORTTYPE_NPIV,             "NPIV VPORT" },
124 };
125 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
126 #define FC_PORTTYPE_MAX_NAMELEN         50
127
128 /* Reuse fc_port_type enum function for vport_type */
129 #define get_fc_vport_type_name get_fc_port_type_name
130
131
132 /* Convert fc_host_event_code values to ascii string name */
133 static const struct {
134         enum fc_host_event_code         value;
135         char                            *name;
136 } fc_host_event_code_names[] = {
137         { FCH_EVT_LIP,                  "lip" },
138         { FCH_EVT_LINKUP,               "link_up" },
139         { FCH_EVT_LINKDOWN,             "link_down" },
140         { FCH_EVT_LIPRESET,             "lip_reset" },
141         { FCH_EVT_RSCN,                 "rscn" },
142         { FCH_EVT_ADAPTER_CHANGE,       "adapter_chg" },
143         { FCH_EVT_PORT_UNKNOWN,         "port_unknown" },
144         { FCH_EVT_PORT_ONLINE,          "port_online" },
145         { FCH_EVT_PORT_OFFLINE,         "port_offline" },
146         { FCH_EVT_PORT_FABRIC,          "port_fabric" },
147         { FCH_EVT_LINK_UNKNOWN,         "link_unknown" },
148         { FCH_EVT_VENDOR_UNIQUE,        "vendor_unique" },
149 };
150 fc_enum_name_search(host_event_code, fc_host_event_code,
151                 fc_host_event_code_names)
152 #define FC_HOST_EVENT_CODE_MAX_NAMELEN  30
153
154
155 /* Convert fc_port_state values to ascii string name */
156 static struct {
157         enum fc_port_state      value;
158         char                    *name;
159 } fc_port_state_names[] = {
160         { FC_PORTSTATE_UNKNOWN,         "Unknown" },
161         { FC_PORTSTATE_NOTPRESENT,      "Not Present" },
162         { FC_PORTSTATE_ONLINE,          "Online" },
163         { FC_PORTSTATE_OFFLINE,         "Offline" },
164         { FC_PORTSTATE_BLOCKED,         "Blocked" },
165         { FC_PORTSTATE_BYPASSED,        "Bypassed" },
166         { FC_PORTSTATE_DIAGNOSTICS,     "Diagnostics" },
167         { FC_PORTSTATE_LINKDOWN,        "Linkdown" },
168         { FC_PORTSTATE_ERROR,           "Error" },
169         { FC_PORTSTATE_LOOPBACK,        "Loopback" },
170         { FC_PORTSTATE_DELETED,         "Deleted" },
171 };
172 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
173 #define FC_PORTSTATE_MAX_NAMELEN        20
174
175
176 /* Convert fc_vport_state values to ascii string name */
177 static struct {
178         enum fc_vport_state     value;
179         char                    *name;
180 } fc_vport_state_names[] = {
181         { FC_VPORT_UNKNOWN,             "Unknown" },
182         { FC_VPORT_ACTIVE,              "Active" },
183         { FC_VPORT_DISABLED,            "Disabled" },
184         { FC_VPORT_LINKDOWN,            "Linkdown" },
185         { FC_VPORT_INITIALIZING,        "Initializing" },
186         { FC_VPORT_NO_FABRIC_SUPP,      "No Fabric Support" },
187         { FC_VPORT_NO_FABRIC_RSCS,      "No Fabric Resources" },
188         { FC_VPORT_FABRIC_LOGOUT,       "Fabric Logout" },
189         { FC_VPORT_FABRIC_REJ_WWN,      "Fabric Rejected WWN" },
190         { FC_VPORT_FAILED,              "VPort Failed" },
191 };
192 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
193 #define FC_VPORTSTATE_MAX_NAMELEN       24
194
195 /* Reuse fc_vport_state enum function for vport_last_state */
196 #define get_fc_vport_last_state_name get_fc_vport_state_name
197
198
199 /* Convert fc_tgtid_binding_type values to ascii string name */
200 static const struct {
201         enum fc_tgtid_binding_type      value;
202         char                            *name;
203         int                             matchlen;
204 } fc_tgtid_binding_type_names[] = {
205         { FC_TGTID_BIND_NONE, "none", 4 },
206         { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
207         { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
208         { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
209 };
210 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
211                 fc_tgtid_binding_type_names)
212 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
213                 fc_tgtid_binding_type_names)
214 #define FC_BINDTYPE_MAX_NAMELEN 30
215
216
217 #define fc_bitfield_name_search(title, table)                   \
218 static ssize_t                                                  \
219 get_fc_##title##_names(u32 table_key, char *buf)                \
220 {                                                               \
221         char *prefix = "";                                      \
222         ssize_t len = 0;                                        \
223         int i;                                                  \
224                                                                 \
225         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
226                 if (table[i].value & table_key) {               \
227                         len += sprintf(buf + len, "%s%s",       \
228                                 prefix, table[i].name);         \
229                         prefix = ", ";                          \
230                 }                                               \
231         }                                                       \
232         len += sprintf(buf + len, "\n");                        \
233         return len;                                             \
234 }
235
236
237 /* Convert FC_COS bit values to ascii string name */
238 static const struct {
239         u32                     value;
240         char                    *name;
241 } fc_cos_names[] = {
242         { FC_COS_CLASS1,        "Class 1" },
243         { FC_COS_CLASS2,        "Class 2" },
244         { FC_COS_CLASS3,        "Class 3" },
245         { FC_COS_CLASS4,        "Class 4" },
246         { FC_COS_CLASS6,        "Class 6" },
247 };
248 fc_bitfield_name_search(cos, fc_cos_names)
249
250
251 /* Convert FC_PORTSPEED bit values to ascii string name */
252 static const struct {
253         u32                     value;
254         char                    *name;
255 } fc_port_speed_names[] = {
256         { FC_PORTSPEED_1GBIT,           "1 Gbit" },
257         { FC_PORTSPEED_2GBIT,           "2 Gbit" },
258         { FC_PORTSPEED_4GBIT,           "4 Gbit" },
259         { FC_PORTSPEED_10GBIT,          "10 Gbit" },
260         { FC_PORTSPEED_8GBIT,           "8 Gbit" },
261         { FC_PORTSPEED_16GBIT,          "16 Gbit" },
262         { FC_PORTSPEED_NOT_NEGOTIATED,  "Not Negotiated" },
263 };
264 fc_bitfield_name_search(port_speed, fc_port_speed_names)
265
266
267 static int
268 show_fc_fc4s (char *buf, u8 *fc4_list)
269 {
270         int i, len=0;
271
272         for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
273                 len += sprintf(buf + len , "0x%02x ", *fc4_list);
274         len += sprintf(buf + len, "\n");
275         return len;
276 }
277
278
279 /* Convert FC_PORT_ROLE bit values to ascii string name */
280 static const struct {
281         u32                     value;
282         char                    *name;
283 } fc_port_role_names[] = {
284         { FC_PORT_ROLE_FCP_TARGET,      "FCP Target" },
285         { FC_PORT_ROLE_FCP_INITIATOR,   "FCP Initiator" },
286         { FC_PORT_ROLE_IP_PORT,         "IP Port" },
287 };
288 fc_bitfield_name_search(port_roles, fc_port_role_names)
289
290 /*
291  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
292  */
293 #define FC_WELLKNOWN_PORTID_MASK        0xfffff0
294 #define FC_WELLKNOWN_ROLE_MASK          0x00000f
295 #define FC_FPORT_PORTID                 0x00000e
296 #define FC_FABCTLR_PORTID               0x00000d
297 #define FC_DIRSRVR_PORTID               0x00000c
298 #define FC_TIMESRVR_PORTID              0x00000b
299 #define FC_MGMTSRVR_PORTID              0x00000a
300
301
302 static void fc_timeout_deleted_rport(struct work_struct *work);
303 static void fc_timeout_fail_rport_io(struct work_struct *work);
304 static void fc_scsi_scan_rport(struct work_struct *work);
305
306 /*
307  * Attribute counts pre object type...
308  * Increase these values if you add attributes
309  */
310 #define FC_STARGET_NUM_ATTRS    3
311 #define FC_RPORT_NUM_ATTRS      10
312 #define FC_VPORT_NUM_ATTRS      9
313 #define FC_HOST_NUM_ATTRS       21
314
315 struct fc_internal {
316         struct scsi_transport_template t;
317         struct fc_function_template *f;
318
319         /*
320          * For attributes : each object has :
321          *   An array of the actual attributes structures
322          *   An array of null-terminated pointers to the attribute
323          *     structures - used for mid-layer interaction.
324          *
325          * The attribute containers for the starget and host are are
326          * part of the midlayer. As the remote port is specific to the
327          * fc transport, we must provide the attribute container.
328          */
329         struct device_attribute private_starget_attrs[
330                                                         FC_STARGET_NUM_ATTRS];
331         struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
332
333         struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
334         struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
335
336         struct transport_container rport_attr_cont;
337         struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
338         struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
339
340         struct transport_container vport_attr_cont;
341         struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
342         struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
343 };
344
345 #define to_fc_internal(tmpl)    container_of(tmpl, struct fc_internal, t)
346
347 static int fc_target_setup(struct transport_container *tc, struct device *dev,
348                            struct device *cdev)
349 {
350         struct scsi_target *starget = to_scsi_target(dev);
351         struct fc_rport *rport = starget_to_rport(starget);
352
353         /*
354          * if parent is remote port, use values from remote port.
355          * Otherwise, this host uses the fc_transport, but not the
356          * remote port interface. As such, initialize to known non-values.
357          */
358         if (rport) {
359                 fc_starget_node_name(starget) = rport->node_name;
360                 fc_starget_port_name(starget) = rport->port_name;
361                 fc_starget_port_id(starget) = rport->port_id;
362         } else {
363                 fc_starget_node_name(starget) = -1;
364                 fc_starget_port_name(starget) = -1;
365                 fc_starget_port_id(starget) = -1;
366         }
367
368         return 0;
369 }
370
371 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
372                                "fc_transport",
373                                fc_target_setup,
374                                NULL,
375                                NULL);
376
377 static int fc_host_setup(struct transport_container *tc, struct device *dev,
378                          struct device *cdev)
379 {
380         struct Scsi_Host *shost = dev_to_shost(dev);
381         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
382
383         /*
384          * Set default values easily detected by the midlayer as
385          * failure cases.  The scsi lldd is responsible for initializing
386          * all transport attributes to valid values per host.
387          */
388         fc_host->node_name = -1;
389         fc_host->port_name = -1;
390         fc_host->permanent_port_name = -1;
391         fc_host->supported_classes = FC_COS_UNSPECIFIED;
392         memset(fc_host->supported_fc4s, 0,
393                 sizeof(fc_host->supported_fc4s));
394         fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
395         fc_host->maxframe_size = -1;
396         fc_host->max_npiv_vports = 0;
397         memset(fc_host->serial_number, 0,
398                 sizeof(fc_host->serial_number));
399
400         fc_host->port_id = -1;
401         fc_host->port_type = FC_PORTTYPE_UNKNOWN;
402         fc_host->port_state = FC_PORTSTATE_UNKNOWN;
403         memset(fc_host->active_fc4s, 0,
404                 sizeof(fc_host->active_fc4s));
405         fc_host->speed = FC_PORTSPEED_UNKNOWN;
406         fc_host->fabric_name = -1;
407         memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
408         memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
409
410         fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
411
412         INIT_LIST_HEAD(&fc_host->rports);
413         INIT_LIST_HEAD(&fc_host->rport_bindings);
414         INIT_LIST_HEAD(&fc_host->vports);
415         fc_host->next_rport_number = 0;
416         fc_host->next_target_id = 0;
417         fc_host->next_vport_number = 0;
418         fc_host->npiv_vports_inuse = 0;
419
420         snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
421                  "fc_wq_%d", shost->host_no);
422         fc_host->work_q = create_singlethread_workqueue(
423                                         fc_host->work_q_name);
424         if (!fc_host->work_q)
425                 return -ENOMEM;
426
427         snprintf(fc_host->devloss_work_q_name,
428                  sizeof(fc_host->devloss_work_q_name),
429                  "fc_dl_%d", shost->host_no);
430         fc_host->devloss_work_q = create_singlethread_workqueue(
431                                         fc_host->devloss_work_q_name);
432         if (!fc_host->devloss_work_q) {
433                 destroy_workqueue(fc_host->work_q);
434                 fc_host->work_q = NULL;
435                 return -ENOMEM;
436         }
437
438         return 0;
439 }
440
441 static DECLARE_TRANSPORT_CLASS(fc_host_class,
442                                "fc_host",
443                                fc_host_setup,
444                                NULL,
445                                NULL);
446
447 /*
448  * Setup and Remove actions for remote ports are handled
449  * in the service functions below.
450  */
451 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
452                                "fc_remote_ports",
453                                NULL,
454                                NULL,
455                                NULL);
456
457 /*
458  * Setup and Remove actions for virtual ports are handled
459  * in the service functions below.
460  */
461 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
462                                "fc_vports",
463                                NULL,
464                                NULL,
465                                NULL);
466
467 /*
468  * Module Parameters
469  */
470
471 /*
472  * dev_loss_tmo: the default number of seconds that the FC transport
473  *   should insulate the loss of a remote port.
474  *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
475  */
476 static unsigned int fc_dev_loss_tmo = 60;               /* seconds */
477
478 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
479 MODULE_PARM_DESC(dev_loss_tmo,
480                  "Maximum number of seconds that the FC transport should"
481                  " insulate the loss of a remote port. Once this value is"
482                  " exceeded, the scsi target is removed. Value should be"
483                  " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
484
485 /*
486  * Netlink Infrastructure
487  */
488
489 static atomic_t fc_event_seq;
490
491 /**
492  * fc_get_event_number - Obtain the next sequential FC event number
493  *
494  * Notes:
495  *   We could have inlined this, but it would have required fc_event_seq to
496  *   be exposed. For now, live with the subroutine call.
497  *   Atomic used to avoid lock/unlock...
498  */
499 u32
500 fc_get_event_number(void)
501 {
502         return atomic_add_return(1, &fc_event_seq);
503 }
504 EXPORT_SYMBOL(fc_get_event_number);
505
506
507 /**
508  * fc_host_post_event - called to post an even on an fc_host.
509  * @shost:              host the event occurred on
510  * @event_number:       fc event number obtained from get_fc_event_number()
511  * @event_code:         fc_host event being posted
512  * @event_data:         32bits of data for the event being posted
513  *
514  * Notes:
515  *      This routine assumes no locks are held on entry.
516  */
517 void
518 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
519                 enum fc_host_event_code event_code, u32 event_data)
520 {
521         struct sk_buff *skb;
522         struct nlmsghdr *nlh;
523         struct fc_nl_event *event;
524         const char *name;
525         u32 len, skblen;
526         int err;
527
528         if (!scsi_nl_sock) {
529                 err = -ENOENT;
530                 goto send_fail;
531         }
532
533         len = FC_NL_MSGALIGN(sizeof(*event));
534         skblen = NLMSG_SPACE(len);
535
536         skb = alloc_skb(skblen, GFP_KERNEL);
537         if (!skb) {
538                 err = -ENOBUFS;
539                 goto send_fail;
540         }
541
542         nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
543                                 skblen - sizeof(*nlh), 0);
544         if (!nlh) {
545                 err = -ENOBUFS;
546                 goto send_fail_skb;
547         }
548         event = NLMSG_DATA(nlh);
549
550         INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
551                                 FC_NL_ASYNC_EVENT, len);
552         event->seconds = get_seconds();
553         event->vendor_id = 0;
554         event->host_no = shost->host_no;
555         event->event_datalen = sizeof(u32);     /* bytes */
556         event->event_num = event_number;
557         event->event_code = event_code;
558         event->event_data = event_data;
559
560         err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
561                               GFP_KERNEL);
562         if (err && (err != -ESRCH))     /* filter no recipient errors */
563                 /* nlmsg_multicast already kfree_skb'd */
564                 goto send_fail;
565
566         return;
567
568 send_fail_skb:
569         kfree_skb(skb);
570 send_fail:
571         name = get_fc_host_event_code_name(event_code);
572         printk(KERN_WARNING
573                 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
574                 __FUNCTION__, shost->host_no,
575                 (name) ? name : "<unknown>", event_data, err);
576         return;
577 }
578 EXPORT_SYMBOL(fc_host_post_event);
579
580
581 /**
582  * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
583  * @shost:              host the event occurred on
584  * @event_number:       fc event number obtained from get_fc_event_number()
585  * @data_len:           amount, in bytes, of vendor unique data
586  * @data_buf:           pointer to vendor unique data
587  * @vendor_id:          Vendor id
588  *
589  * Notes:
590  *      This routine assumes no locks are held on entry.
591  */
592 void
593 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
594                 u32 data_len, char * data_buf, u64 vendor_id)
595 {
596         struct sk_buff *skb;
597         struct nlmsghdr *nlh;
598         struct fc_nl_event *event;
599         u32 len, skblen;
600         int err;
601
602         if (!scsi_nl_sock) {
603                 err = -ENOENT;
604                 goto send_vendor_fail;
605         }
606
607         len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
608         skblen = NLMSG_SPACE(len);
609
610         skb = alloc_skb(skblen, GFP_KERNEL);
611         if (!skb) {
612                 err = -ENOBUFS;
613                 goto send_vendor_fail;
614         }
615
616         nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
617                                 skblen - sizeof(*nlh), 0);
618         if (!nlh) {
619                 err = -ENOBUFS;
620                 goto send_vendor_fail_skb;
621         }
622         event = NLMSG_DATA(nlh);
623
624         INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
625                                 FC_NL_ASYNC_EVENT, len);
626         event->seconds = get_seconds();
627         event->vendor_id = vendor_id;
628         event->host_no = shost->host_no;
629         event->event_datalen = data_len;        /* bytes */
630         event->event_num = event_number;
631         event->event_code = FCH_EVT_VENDOR_UNIQUE;
632         memcpy(&event->event_data, data_buf, data_len);
633
634         err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
635                               GFP_KERNEL);
636         if (err && (err != -ESRCH))     /* filter no recipient errors */
637                 /* nlmsg_multicast already kfree_skb'd */
638                 goto send_vendor_fail;
639
640         return;
641
642 send_vendor_fail_skb:
643         kfree_skb(skb);
644 send_vendor_fail:
645         printk(KERN_WARNING
646                 "%s: Dropped Event : host %d vendor_unique - err %d\n",
647                 __FUNCTION__, shost->host_no, err);
648         return;
649 }
650 EXPORT_SYMBOL(fc_host_post_vendor_event);
651
652
653
654 static __init int fc_transport_init(void)
655 {
656         int error;
657
658         atomic_set(&fc_event_seq, 0);
659
660         error = transport_class_register(&fc_host_class);
661         if (error)
662                 return error;
663         error = transport_class_register(&fc_vport_class);
664         if (error)
665                 return error;
666         error = transport_class_register(&fc_rport_class);
667         if (error)
668                 return error;
669         return transport_class_register(&fc_transport_class);
670 }
671
672 static void __exit fc_transport_exit(void)
673 {
674         transport_class_unregister(&fc_transport_class);
675         transport_class_unregister(&fc_rport_class);
676         transport_class_unregister(&fc_host_class);
677         transport_class_unregister(&fc_vport_class);
678 }
679
680 /*
681  * FC Remote Port Attribute Management
682  */
683
684 #define fc_rport_show_function(field, format_string, sz, cast)          \
685 static ssize_t                                                          \
686 show_fc_rport_##field (struct device *dev,                              \
687                        struct device_attribute *attr, char *buf)        \
688 {                                                                       \
689         struct fc_rport *rport = transport_class_to_rport(dev);         \
690         struct Scsi_Host *shost = rport_to_shost(rport);                \
691         struct fc_internal *i = to_fc_internal(shost->transportt);      \
692         if ((i->f->get_rport_##field) &&                                \
693             !((rport->port_state == FC_PORTSTATE_BLOCKED) ||            \
694               (rport->port_state == FC_PORTSTATE_DELETED) ||            \
695               (rport->port_state == FC_PORTSTATE_NOTPRESENT)))          \
696                 i->f->get_rport_##field(rport);                         \
697         return snprintf(buf, sz, format_string, cast rport->field);     \
698 }
699
700 #define fc_rport_store_function(field)                                  \
701 static ssize_t                                                          \
702 store_fc_rport_##field(struct device *dev,                              \
703                        struct device_attribute *attr,                   \
704                        const char *buf, size_t count)                   \
705 {                                                                       \
706         int val;                                                        \
707         struct fc_rport *rport = transport_class_to_rport(dev);         \
708         struct Scsi_Host *shost = rport_to_shost(rport);                \
709         struct fc_internal *i = to_fc_internal(shost->transportt);      \
710         char *cp;                                                       \
711         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||              \
712             (rport->port_state == FC_PORTSTATE_DELETED) ||              \
713             (rport->port_state == FC_PORTSTATE_NOTPRESENT))             \
714                 return -EBUSY;                                          \
715         val = simple_strtoul(buf, &cp, 0);                              \
716         if (*cp && (*cp != '\n'))                                       \
717                 return -EINVAL;                                         \
718         i->f->set_rport_##field(rport, val);                            \
719         return count;                                                   \
720 }
721
722 #define fc_rport_rd_attr(field, format_string, sz)                      \
723         fc_rport_show_function(field, format_string, sz, )              \
724 static FC_DEVICE_ATTR(rport, field, S_IRUGO,                    \
725                          show_fc_rport_##field, NULL)
726
727 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)           \
728         fc_rport_show_function(field, format_string, sz, (cast))        \
729 static FC_DEVICE_ATTR(rport, field, S_IRUGO,                    \
730                           show_fc_rport_##field, NULL)
731
732 #define fc_rport_rw_attr(field, format_string, sz)                      \
733         fc_rport_show_function(field, format_string, sz, )              \
734         fc_rport_store_function(field)                                  \
735 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,          \
736                         show_fc_rport_##field,                          \
737                         store_fc_rport_##field)
738
739
740 #define fc_private_rport_show_function(field, format_string, sz, cast)  \
741 static ssize_t                                                          \
742 show_fc_rport_##field (struct device *dev,                              \
743                        struct device_attribute *attr, char *buf)        \
744 {                                                                       \
745         struct fc_rport *rport = transport_class_to_rport(dev);         \
746         return snprintf(buf, sz, format_string, cast rport->field);     \
747 }
748
749 #define fc_private_rport_rd_attr(field, format_string, sz)              \
750         fc_private_rport_show_function(field, format_string, sz, )      \
751 static FC_DEVICE_ATTR(rport, field, S_IRUGO,                    \
752                          show_fc_rport_##field, NULL)
753
754 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)   \
755         fc_private_rport_show_function(field, format_string, sz, (cast)) \
756 static FC_DEVICE_ATTR(rport, field, S_IRUGO,                    \
757                           show_fc_rport_##field, NULL)
758
759
760 #define fc_private_rport_rd_enum_attr(title, maxlen)                    \
761 static ssize_t                                                          \
762 show_fc_rport_##title (struct device *dev,                              \
763                        struct device_attribute *attr, char *buf)        \
764 {                                                                       \
765         struct fc_rport *rport = transport_class_to_rport(dev);         \
766         const char *name;                                               \
767         name = get_fc_##title##_name(rport->title);                     \
768         if (!name)                                                      \
769                 return -EINVAL;                                         \
770         return snprintf(buf, maxlen, "%s\n", name);                     \
771 }                                                                       \
772 static FC_DEVICE_ATTR(rport, title, S_IRUGO,                    \
773                         show_fc_rport_##title, NULL)
774
775
776 #define SETUP_RPORT_ATTRIBUTE_RD(field)                                 \
777         i->private_rport_attrs[count] = device_attr_rport_##field; \
778         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
779         i->private_rport_attrs[count].store = NULL;                     \
780         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
781         if (i->f->show_rport_##field)                                   \
782                 count++
783
784 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)                         \
785         i->private_rport_attrs[count] = device_attr_rport_##field; \
786         i->private_rport_attrs[count].attr.mode = S_IRUGO;              \
787         i->private_rport_attrs[count].store = NULL;                     \
788         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
789         count++
790
791 #define SETUP_RPORT_ATTRIBUTE_RW(field)                                 \
792         i->private_rport_attrs[count] = device_attr_rport_##field; \
793         if (!i->f->set_rport_##field) {                                 \
794                 i->private_rport_attrs[count].attr.mode = S_IRUGO;      \
795                 i->private_rport_attrs[count].store = NULL;             \
796         }                                                               \
797         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
798         if (i->f->show_rport_##field)                                   \
799                 count++
800
801 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)                         \
802 {                                                                       \
803         i->private_rport_attrs[count] = device_attr_rport_##field; \
804         i->rport_attrs[count] = &i->private_rport_attrs[count];         \
805         count++;                                                        \
806 }
807
808
809 /* The FC Transport Remote Port Attributes: */
810
811 /* Fixed Remote Port Attributes */
812
813 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
814
815 static ssize_t
816 show_fc_rport_supported_classes (struct device *dev,
817                                  struct device_attribute *attr, char *buf)
818 {
819         struct fc_rport *rport = transport_class_to_rport(dev);
820         if (rport->supported_classes == FC_COS_UNSPECIFIED)
821                 return snprintf(buf, 20, "unspecified\n");
822         return get_fc_cos_names(rport->supported_classes, buf);
823 }
824 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
825                 show_fc_rport_supported_classes, NULL);
826
827 /* Dynamic Remote Port Attributes */
828
829 /*
830  * dev_loss_tmo attribute
831  */
832 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
833 static ssize_t
834 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
835                             const char *buf, size_t count)
836 {
837         int val;
838         struct fc_rport *rport = transport_class_to_rport(dev);
839         struct Scsi_Host *shost = rport_to_shost(rport);
840         struct fc_internal *i = to_fc_internal(shost->transportt);
841         char *cp;
842         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
843             (rport->port_state == FC_PORTSTATE_DELETED) ||
844             (rport->port_state == FC_PORTSTATE_NOTPRESENT))
845                 return -EBUSY;
846         val = simple_strtoul(buf, &cp, 0);
847         if ((*cp && (*cp != '\n')) ||
848             (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
849                 return -EINVAL;
850         i->f->set_rport_dev_loss_tmo(rport, val);
851         return count;
852 }
853 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
854                 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
855
856
857 /* Private Remote Port Attributes */
858
859 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
860 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
861 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
862
863 static ssize_t
864 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
865                      char *buf)
866 {
867         struct fc_rport *rport = transport_class_to_rport(dev);
868
869         /* identify any roles that are port_id specific */
870         if ((rport->port_id != -1) &&
871             (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
872                                         FC_WELLKNOWN_PORTID_MASK) {
873                 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
874                 case FC_FPORT_PORTID:
875                         return snprintf(buf, 30, "Fabric Port\n");
876                 case FC_FABCTLR_PORTID:
877                         return snprintf(buf, 30, "Fabric Controller\n");
878                 case FC_DIRSRVR_PORTID:
879                         return snprintf(buf, 30, "Directory Server\n");
880                 case FC_TIMESRVR_PORTID:
881                         return snprintf(buf, 30, "Time Server\n");
882                 case FC_MGMTSRVR_PORTID:
883                         return snprintf(buf, 30, "Management Server\n");
884                 default:
885                         return snprintf(buf, 30, "Unknown Fabric Entity\n");
886                 }
887         } else {
888                 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
889                         return snprintf(buf, 20, "unknown\n");
890                 return get_fc_port_roles_names(rport->roles, buf);
891         }
892 }
893 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
894                 show_fc_rport_roles, NULL);
895
896 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
897 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
898
899 /*
900  * fast_io_fail_tmo attribute
901  */
902 static ssize_t
903 show_fc_rport_fast_io_fail_tmo (struct device *dev,
904                                 struct device_attribute *attr, char *buf)
905 {
906         struct fc_rport *rport = transport_class_to_rport(dev);
907
908         if (rport->fast_io_fail_tmo == -1)
909                 return snprintf(buf, 5, "off\n");
910         return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
911 }
912
913 static ssize_t
914 store_fc_rport_fast_io_fail_tmo(struct device *dev,
915                                 struct device_attribute *attr, const char *buf,
916                                 size_t count)
917 {
918         int val;
919         char *cp;
920         struct fc_rport *rport = transport_class_to_rport(dev);
921
922         if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
923             (rport->port_state == FC_PORTSTATE_DELETED) ||
924             (rport->port_state == FC_PORTSTATE_NOTPRESENT))
925                 return -EBUSY;
926         if (strncmp(buf, "off", 3) == 0)
927                 rport->fast_io_fail_tmo = -1;
928         else {
929                 val = simple_strtoul(buf, &cp, 0);
930                 if ((*cp && (*cp != '\n')) ||
931                     (val < 0) || (val >= rport->dev_loss_tmo))
932                         return -EINVAL;
933                 rport->fast_io_fail_tmo = val;
934         }
935         return count;
936 }
937 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
938         show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
939
940
941 /*
942  * FC SCSI Target Attribute Management
943  */
944
945 /*
946  * Note: in the target show function we recognize when the remote
947  *  port is in the heirarchy and do not allow the driver to get
948  *  involved in sysfs functions. The driver only gets involved if
949  *  it's the "old" style that doesn't use rports.
950  */
951 #define fc_starget_show_function(field, format_string, sz, cast)        \
952 static ssize_t                                                          \
953 show_fc_starget_##field (struct device *dev,                            \
954                          struct device_attribute *attr, char *buf)      \
955 {                                                                       \
956         struct scsi_target *starget = transport_class_to_starget(dev);  \
957         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
958         struct fc_internal *i = to_fc_internal(shost->transportt);      \
959         struct fc_rport *rport = starget_to_rport(starget);             \
960         if (rport)                                                      \
961                 fc_starget_##field(starget) = rport->field;             \
962         else if (i->f->get_starget_##field)                             \
963                 i->f->get_starget_##field(starget);                     \
964         return snprintf(buf, sz, format_string,                         \
965                 cast fc_starget_##field(starget));                      \
966 }
967
968 #define fc_starget_rd_attr(field, format_string, sz)                    \
969         fc_starget_show_function(field, format_string, sz, )            \
970 static FC_DEVICE_ATTR(starget, field, S_IRUGO,                  \
971                          show_fc_starget_##field, NULL)
972
973 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)         \
974         fc_starget_show_function(field, format_string, sz, (cast))      \
975 static FC_DEVICE_ATTR(starget, field, S_IRUGO,                  \
976                           show_fc_starget_##field, NULL)
977
978 #define SETUP_STARGET_ATTRIBUTE_RD(field)                               \
979         i->private_starget_attrs[count] = device_attr_starget_##field; \
980         i->private_starget_attrs[count].attr.mode = S_IRUGO;            \
981         i->private_starget_attrs[count].store = NULL;                   \
982         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
983         if (i->f->show_starget_##field)                                 \
984                 count++
985
986 #define SETUP_STARGET_ATTRIBUTE_RW(field)                               \
987         i->private_starget_attrs[count] = device_attr_starget_##field; \
988         if (!i->f->set_starget_##field) {                               \
989                 i->private_starget_attrs[count].attr.mode = S_IRUGO;    \
990                 i->private_starget_attrs[count].store = NULL;           \
991         }                                                               \
992         i->starget_attrs[count] = &i->private_starget_attrs[count];     \
993         if (i->f->show_starget_##field)                                 \
994                 count++
995
996 /* The FC Transport SCSI Target Attributes: */
997 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
998 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
999 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1000
1001
1002 /*
1003  * FC Virtual Port Attribute Management
1004  */
1005
1006 #define fc_vport_show_function(field, format_string, sz, cast)          \
1007 static ssize_t                                                          \
1008 show_fc_vport_##field (struct device *dev,                              \
1009                        struct device_attribute *attr, char *buf)        \
1010 {                                                                       \
1011         struct fc_vport *vport = transport_class_to_vport(dev);         \
1012         struct Scsi_Host *shost = vport_to_shost(vport);                \
1013         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1014         if ((i->f->get_vport_##field) &&                                \
1015             !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))       \
1016                 i->f->get_vport_##field(vport);                         \
1017         return snprintf(buf, sz, format_string, cast vport->field);     \
1018 }
1019
1020 #define fc_vport_store_function(field)                                  \
1021 static ssize_t                                                          \
1022 store_fc_vport_##field(struct device *dev,                              \
1023                        struct device_attribute *attr,                   \
1024                        const char *buf, size_t count)                   \
1025 {                                                                       \
1026         int val;                                                        \
1027         struct fc_vport *vport = transport_class_to_vport(dev);         \
1028         struct Scsi_Host *shost = vport_to_shost(vport);                \
1029         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1030         char *cp;                                                       \
1031         if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))  \
1032                 return -EBUSY;                                          \
1033         val = simple_strtoul(buf, &cp, 0);                              \
1034         if (*cp && (*cp != '\n'))                                       \
1035                 return -EINVAL;                                         \
1036         i->f->set_vport_##field(vport, val);                            \
1037         return count;                                                   \
1038 }
1039
1040 #define fc_vport_store_str_function(field, slen)                        \
1041 static ssize_t                                                          \
1042 store_fc_vport_##field(struct device *dev,                              \
1043                        struct device_attribute *attr,                   \
1044                        const char *buf, size_t count)                   \
1045 {                                                                       \
1046         struct fc_vport *vport = transport_class_to_vport(dev);         \
1047         struct Scsi_Host *shost = vport_to_shost(vport);                \
1048         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1049         unsigned int cnt=count;                                         \
1050                                                                         \
1051         /* count may include a LF at end of string */                   \
1052         if (buf[cnt-1] == '\n')                                         \
1053                 cnt--;                                                  \
1054         if (cnt > ((slen) - 1))                                         \
1055                 return -EINVAL;                                         \
1056         memcpy(vport->field, buf, cnt);                                 \
1057         i->f->set_vport_##field(vport);                                 \
1058         return count;                                                   \
1059 }
1060
1061 #define fc_vport_rd_attr(field, format_string, sz)                      \
1062         fc_vport_show_function(field, format_string, sz, )              \
1063 static FC_DEVICE_ATTR(vport, field, S_IRUGO,                    \
1064                          show_fc_vport_##field, NULL)
1065
1066 #define fc_vport_rd_attr_cast(field, format_string, sz, cast)           \
1067         fc_vport_show_function(field, format_string, sz, (cast))        \
1068 static FC_DEVICE_ATTR(vport, field, S_IRUGO,                    \
1069                           show_fc_vport_##field, NULL)
1070
1071 #define fc_vport_rw_attr(field, format_string, sz)                      \
1072         fc_vport_show_function(field, format_string, sz, )              \
1073         fc_vport_store_function(field)                                  \
1074 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,          \
1075                         show_fc_vport_##field,                          \
1076                         store_fc_vport_##field)
1077
1078 #define fc_private_vport_show_function(field, format_string, sz, cast)  \
1079 static ssize_t                                                          \
1080 show_fc_vport_##field (struct device *dev,                              \
1081                        struct device_attribute *attr, char *buf)        \
1082 {                                                                       \
1083         struct fc_vport *vport = transport_class_to_vport(dev);         \
1084         return snprintf(buf, sz, format_string, cast vport->field);     \
1085 }
1086
1087 #define fc_private_vport_store_u32_function(field)                      \
1088 static ssize_t                                                          \
1089 store_fc_vport_##field(struct device *dev,                              \
1090                        struct device_attribute *attr,                   \
1091                        const char *buf, size_t count)                   \
1092 {                                                                       \
1093         u32 val;                                                        \
1094         struct fc_vport *vport = transport_class_to_vport(dev);         \
1095         char *cp;                                                       \
1096         if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))          \
1097                 return -EBUSY;                                          \
1098         val = simple_strtoul(buf, &cp, 0);                              \
1099         if (*cp && (*cp != '\n'))                                       \
1100                 return -EINVAL;                                         \
1101         vport->field = val;                                             \
1102         return count;                                                   \
1103 }
1104
1105
1106 #define fc_private_vport_rd_attr(field, format_string, sz)              \
1107         fc_private_vport_show_function(field, format_string, sz, )      \
1108 static FC_DEVICE_ATTR(vport, field, S_IRUGO,                    \
1109                          show_fc_vport_##field, NULL)
1110
1111 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)   \
1112         fc_private_vport_show_function(field, format_string, sz, (cast)) \
1113 static FC_DEVICE_ATTR(vport, field, S_IRUGO,                    \
1114                           show_fc_vport_##field, NULL)
1115
1116 #define fc_private_vport_rw_u32_attr(field, format_string, sz)          \
1117         fc_private_vport_show_function(field, format_string, sz, )      \
1118         fc_private_vport_store_u32_function(field)                      \
1119 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,          \
1120                         show_fc_vport_##field,                          \
1121                         store_fc_vport_##field)
1122
1123
1124 #define fc_private_vport_rd_enum_attr(title, maxlen)                    \
1125 static ssize_t                                                          \
1126 show_fc_vport_##title (struct device *dev,                              \
1127                        struct device_attribute *attr,                   \
1128                        char *buf)                                       \
1129 {                                                                       \
1130         struct fc_vport *vport = transport_class_to_vport(dev);         \
1131         const char *name;                                               \
1132         name = get_fc_##title##_name(vport->title);                     \
1133         if (!name)                                                      \
1134                 return -EINVAL;                                         \
1135         return snprintf(buf, maxlen, "%s\n", name);                     \
1136 }                                                                       \
1137 static FC_DEVICE_ATTR(vport, title, S_IRUGO,                    \
1138                         show_fc_vport_##title, NULL)
1139
1140
1141 #define SETUP_VPORT_ATTRIBUTE_RD(field)                                 \
1142         i->private_vport_attrs[count] = device_attr_vport_##field; \
1143         i->private_vport_attrs[count].attr.mode = S_IRUGO;              \
1144         i->private_vport_attrs[count].store = NULL;                     \
1145         i->vport_attrs[count] = &i->private_vport_attrs[count];         \
1146         if (i->f->get_##field)                                          \
1147                 count++
1148         /* NOTE: Above MACRO differs: checks function not show bit */
1149
1150 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)                         \
1151         i->private_vport_attrs[count] = device_attr_vport_##field; \
1152         i->private_vport_attrs[count].attr.mode = S_IRUGO;              \
1153         i->private_vport_attrs[count].store = NULL;                     \
1154         i->vport_attrs[count] = &i->private_vport_attrs[count];         \
1155         count++
1156
1157 #define SETUP_VPORT_ATTRIBUTE_WR(field)                                 \
1158         i->private_vport_attrs[count] = device_attr_vport_##field; \
1159         i->vport_attrs[count] = &i->private_vport_attrs[count];         \
1160         if (i->f->field)                                                \
1161                 count++
1162         /* NOTE: Above MACRO differs: checks function */
1163
1164 #define SETUP_VPORT_ATTRIBUTE_RW(field)                                 \
1165         i->private_vport_attrs[count] = device_attr_vport_##field; \
1166         if (!i->f->set_vport_##field) {                                 \
1167                 i->private_vport_attrs[count].attr.mode = S_IRUGO;      \
1168                 i->private_vport_attrs[count].store = NULL;             \
1169         }                                                               \
1170         i->vport_attrs[count] = &i->private_vport_attrs[count];         \
1171         count++
1172         /* NOTE: Above MACRO differs: does not check show bit */
1173
1174 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)                         \
1175 {                                                                       \
1176         i->private_vport_attrs[count] = device_attr_vport_##field; \
1177         i->vport_attrs[count] = &i->private_vport_attrs[count];         \
1178         count++;                                                        \
1179 }
1180
1181
1182 /* The FC Transport Virtual Port Attributes: */
1183
1184 /* Fixed Virtual Port Attributes */
1185
1186 /* Dynamic Virtual Port Attributes */
1187
1188 /* Private Virtual Port Attributes */
1189
1190 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1191 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1192 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1193 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1194
1195 static ssize_t
1196 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1197                      char *buf)
1198 {
1199         struct fc_vport *vport = transport_class_to_vport(dev);
1200
1201         if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1202                 return snprintf(buf, 20, "unknown\n");
1203         return get_fc_port_roles_names(vport->roles, buf);
1204 }
1205 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1206
1207 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1208
1209 fc_private_vport_show_function(symbolic_name, "%s\n",
1210                 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1211 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1212 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1213                 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1214
1215 static ssize_t
1216 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1217                       const char *buf, size_t count)
1218 {
1219         struct fc_vport *vport = transport_class_to_vport(dev);
1220         struct Scsi_Host *shost = vport_to_shost(vport);
1221
1222         fc_queue_work(shost, &vport->vport_delete_work);
1223         return count;
1224 }
1225 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1226                         NULL, store_fc_vport_delete);
1227
1228
1229 /*
1230  * Enable/Disable vport
1231  *  Write "1" to disable, write "0" to enable
1232  */
1233 static ssize_t
1234 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1235                        const char *buf,
1236                            size_t count)
1237 {
1238         struct fc_vport *vport = transport_class_to_vport(dev);
1239         struct Scsi_Host *shost = vport_to_shost(vport);
1240         struct fc_internal *i = to_fc_internal(shost->transportt);
1241         int stat;
1242
1243         if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1244                 return -EBUSY;
1245
1246         if (*buf == '0') {
1247                 if (vport->vport_state != FC_VPORT_DISABLED)
1248                         return -EALREADY;
1249         } else if (*buf == '1') {
1250                 if (vport->vport_state == FC_VPORT_DISABLED)
1251                         return -EALREADY;
1252         } else
1253                 return -EINVAL;
1254
1255         stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1256         return stat ? stat : count;
1257 }
1258 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1259                         NULL, store_fc_vport_disable);
1260
1261
1262 /*
1263  * Host Attribute Management
1264  */
1265
1266 #define fc_host_show_function(field, format_string, sz, cast)           \
1267 static ssize_t                                                          \
1268 show_fc_host_##field (struct device *dev,                               \
1269                       struct device_attribute *attr, char *buf)         \
1270 {                                                                       \
1271         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1272         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1273         if (i->f->get_host_##field)                                     \
1274                 i->f->get_host_##field(shost);                          \
1275         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1276 }
1277
1278 #define fc_host_store_function(field)                                   \
1279 static ssize_t                                                          \
1280 store_fc_host_##field(struct device *dev,                               \
1281                       struct device_attribute *attr,                    \
1282                       const char *buf,  size_t count)                   \
1283 {                                                                       \
1284         int val;                                                        \
1285         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1286         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1287         char *cp;                                                       \
1288                                                                         \
1289         val = simple_strtoul(buf, &cp, 0);                              \
1290         if (*cp && (*cp != '\n'))                                       \
1291                 return -EINVAL;                                         \
1292         i->f->set_host_##field(shost, val);                             \
1293         return count;                                                   \
1294 }
1295
1296 #define fc_host_store_str_function(field, slen)                         \
1297 static ssize_t                                                          \
1298 store_fc_host_##field(struct device *dev,                               \
1299                       struct device_attribute *attr,                    \
1300                       const char *buf, size_t count)                    \
1301 {                                                                       \
1302         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1303         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1304         unsigned int cnt=count;                                         \
1305                                                                         \
1306         /* count may include a LF at end of string */                   \
1307         if (buf[cnt-1] == '\n')                                         \
1308                 cnt--;                                                  \
1309         if (cnt > ((slen) - 1))                                         \
1310                 return -EINVAL;                                         \
1311         memcpy(fc_host_##field(shost), buf, cnt);                       \
1312         i->f->set_host_##field(shost);                                  \
1313         return count;                                                   \
1314 }
1315
1316 #define fc_host_rd_attr(field, format_string, sz)                       \
1317         fc_host_show_function(field, format_string, sz, )               \
1318 static FC_DEVICE_ATTR(host, field, S_IRUGO,                     \
1319                          show_fc_host_##field, NULL)
1320
1321 #define fc_host_rd_attr_cast(field, format_string, sz, cast)            \
1322         fc_host_show_function(field, format_string, sz, (cast))         \
1323 static FC_DEVICE_ATTR(host, field, S_IRUGO,                     \
1324                           show_fc_host_##field, NULL)
1325
1326 #define fc_host_rw_attr(field, format_string, sz)                       \
1327         fc_host_show_function(field, format_string, sz, )               \
1328         fc_host_store_function(field)                                   \
1329 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,           \
1330                         show_fc_host_##field,                           \
1331                         store_fc_host_##field)
1332
1333 #define fc_host_rd_enum_attr(title, maxlen)                             \
1334 static ssize_t                                                          \
1335 show_fc_host_##title (struct device *dev,                               \
1336                       struct device_attribute *attr, char *buf)         \
1337 {                                                                       \
1338         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1339         struct fc_internal *i = to_fc_internal(shost->transportt);      \
1340         const char *name;                                               \
1341         if (i->f->get_host_##title)                                     \
1342                 i->f->get_host_##title(shost);                          \
1343         name = get_fc_##title##_name(fc_host_##title(shost));           \
1344         if (!name)                                                      \
1345                 return -EINVAL;                                         \
1346         return snprintf(buf, maxlen, "%s\n", name);                     \
1347 }                                                                       \
1348 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1349
1350 #define SETUP_HOST_ATTRIBUTE_RD(field)                                  \
1351         i->private_host_attrs[count] = device_attr_host_##field;        \
1352         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
1353         i->private_host_attrs[count].store = NULL;                      \
1354         i->host_attrs[count] = &i->private_host_attrs[count];           \
1355         if (i->f->show_host_##field)                                    \
1356                 count++
1357
1358 #define SETUP_HOST_ATTRIBUTE_RD_NS(field)                               \
1359         i->private_host_attrs[count] = device_attr_host_##field;        \
1360         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
1361         i->private_host_attrs[count].store = NULL;                      \
1362         i->host_attrs[count] = &i->private_host_attrs[count];           \
1363         count++
1364
1365 #define SETUP_HOST_ATTRIBUTE_RW(field)                                  \
1366         i->private_host_attrs[count] = device_attr_host_##field;        \
1367         if (!i->f->set_host_##field) {                                  \
1368                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
1369                 i->private_host_attrs[count].store = NULL;              \
1370         }                                                               \
1371         i->host_attrs[count] = &i->private_host_attrs[count];           \
1372         if (i->f->show_host_##field)                                    \
1373                 count++
1374
1375
1376 #define fc_private_host_show_function(field, format_string, sz, cast)   \
1377 static ssize_t                                                          \
1378 show_fc_host_##field (struct device *dev,                               \
1379                       struct device_attribute *attr, char *buf)         \
1380 {                                                                       \
1381         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
1382         return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1383 }
1384
1385 #define fc_private_host_rd_attr(field, format_string, sz)               \
1386         fc_private_host_show_function(field, format_string, sz, )       \
1387 static FC_DEVICE_ATTR(host, field, S_IRUGO,                     \
1388                          show_fc_host_##field, NULL)
1389
1390 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)    \
1391         fc_private_host_show_function(field, format_string, sz, (cast)) \
1392 static FC_DEVICE_ATTR(host, field, S_IRUGO,                     \
1393                           show_fc_host_##field, NULL)
1394
1395 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)                  \
1396         i->private_host_attrs[count] = device_attr_host_##field;        \
1397         i->private_host_attrs[count].attr.mode = S_IRUGO;               \
1398         i->private_host_attrs[count].store = NULL;                      \
1399         i->host_attrs[count] = &i->private_host_attrs[count];           \
1400         count++
1401
1402 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)                  \
1403 {                                                                       \
1404         i->private_host_attrs[count] = device_attr_host_##field;        \
1405         i->host_attrs[count] = &i->private_host_attrs[count];           \
1406         count++;                                                        \
1407 }
1408
1409
1410 /* Fixed Host Attributes */
1411
1412 static ssize_t
1413 show_fc_host_supported_classes (struct device *dev,
1414                                 struct device_attribute *attr, char *buf)
1415 {
1416         struct Scsi_Host *shost = transport_class_to_shost(dev);
1417
1418         if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1419                 return snprintf(buf, 20, "unspecified\n");
1420
1421         return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1422 }
1423 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1424                 show_fc_host_supported_classes, NULL);
1425
1426 static ssize_t
1427 show_fc_host_supported_fc4s (struct device *dev,
1428                              struct device_attribute *attr, char *buf)
1429 {
1430         struct Scsi_Host *shost = transport_class_to_shost(dev);
1431         return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1432 }
1433 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1434                 show_fc_host_supported_fc4s, NULL);
1435
1436 static ssize_t
1437 show_fc_host_supported_speeds (struct device *dev,
1438                                struct device_attribute *attr, char *buf)
1439 {
1440         struct Scsi_Host *shost = transport_class_to_shost(dev);
1441
1442         if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1443                 return snprintf(buf, 20, "unknown\n");
1444
1445         return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1446 }
1447 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1448                 show_fc_host_supported_speeds, NULL);
1449
1450
1451 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1452 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1453 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1454                              unsigned long long);
1455 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1456 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1457 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1458
1459
1460 /* Dynamic Host Attributes */
1461
1462 static ssize_t
1463 show_fc_host_active_fc4s (struct device *dev,
1464                           struct device_attribute *attr, char *buf)
1465 {
1466         struct Scsi_Host *shost = transport_class_to_shost(dev);
1467         struct fc_internal *i = to_fc_internal(shost->transportt);
1468
1469         if (i->f->get_host_active_fc4s)
1470                 i->f->get_host_active_fc4s(shost);
1471
1472         return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1473 }
1474 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1475                 show_fc_host_active_fc4s, NULL);
1476
1477 static ssize_t
1478 show_fc_host_speed (struct device *dev,
1479                     struct device_attribute *attr, char *buf)
1480 {
1481         struct Scsi_Host *shost = transport_class_to_shost(dev);
1482         struct fc_internal *i = to_fc_internal(shost->transportt);
1483
1484         if (i->f->get_host_speed)
1485                 i->f->get_host_speed(shost);
1486
1487         if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1488                 return snprintf(buf, 20, "unknown\n");
1489
1490         return get_fc_port_speed_names(fc_host_speed(shost), buf);
1491 }
1492 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1493                 show_fc_host_speed, NULL);
1494
1495
1496 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1497 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1498 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1499 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1500 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1501
1502 fc_private_host_show_function(system_hostname, "%s\n",
1503                 FC_SYMBOLIC_NAME_SIZE + 1, )
1504 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1505 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1506                 show_fc_host_system_hostname, store_fc_host_system_hostname);
1507
1508
1509 /* Private Host Attributes */
1510
1511 static ssize_t
1512 show_fc_private_host_tgtid_bind_type(struct device *dev,
1513                                      struct device_attribute *attr, char *buf)
1514 {
1515         struct Scsi_Host *shost = transport_class_to_shost(dev);
1516         const char *name;
1517
1518         name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1519         if (!name)
1520                 return -EINVAL;
1521         return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1522 }
1523
1524 #define get_list_head_entry(pos, head, member)          \
1525         pos = list_entry((head)->next, typeof(*pos), member)
1526
1527 static ssize_t
1528 store_fc_private_host_tgtid_bind_type(struct device *dev,
1529         struct device_attribute *attr, const char *buf, size_t count)
1530 {
1531         struct Scsi_Host *shost = transport_class_to_shost(dev);
1532         struct fc_rport *rport;
1533         enum fc_tgtid_binding_type val;
1534         unsigned long flags;
1535
1536         if (get_fc_tgtid_bind_type_match(buf, &val))
1537                 return -EINVAL;
1538
1539         /* if changing bind type, purge all unused consistent bindings */
1540         if (val != fc_host_tgtid_bind_type(shost)) {
1541                 spin_lock_irqsave(shost->host_lock, flags);
1542                 while (!list_empty(&fc_host_rport_bindings(shost))) {
1543                         get_list_head_entry(rport,
1544                                 &fc_host_rport_bindings(shost), peers);
1545                         list_del(&rport->peers);
1546                         rport->port_state = FC_PORTSTATE_DELETED;
1547                         fc_queue_work(shost, &rport->rport_delete_work);
1548                 }
1549                 spin_unlock_irqrestore(shost->host_lock, flags);
1550         }
1551
1552         fc_host_tgtid_bind_type(shost) = val;
1553         return count;
1554 }
1555
1556 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1557                         show_fc_private_host_tgtid_bind_type,
1558                         store_fc_private_host_tgtid_bind_type);
1559
1560 static ssize_t
1561 store_fc_private_host_issue_lip(struct device *dev,
1562         struct device_attribute *attr, const char *buf, size_t count)
1563 {
1564         struct Scsi_Host *shost = transport_class_to_shost(dev);
1565         struct fc_internal *i = to_fc_internal(shost->transportt);
1566         int ret;
1567
1568         /* ignore any data value written to the attribute */
1569         if (i->f->issue_fc_host_lip) {
1570                 ret = i->f->issue_fc_host_lip(shost);
1571                 return ret ? ret: count;
1572         }
1573
1574         return -ENOENT;
1575 }
1576
1577 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1578                         store_fc_private_host_issue_lip);
1579
1580 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1581
1582
1583 /*
1584  * Host Statistics Management
1585  */
1586
1587 /* Show a given an attribute in the statistics group */
1588 static ssize_t
1589 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1590 {
1591         struct Scsi_Host *shost = transport_class_to_shost(dev);
1592         struct fc_internal *i = to_fc_internal(shost->transportt);
1593         struct fc_host_statistics *stats;
1594         ssize_t ret = -ENOENT;
1595
1596         if (offset > sizeof(struct fc_host_statistics) ||
1597             offset % sizeof(u64) != 0)
1598                 WARN_ON(1);
1599
1600         if (i->f->get_fc_host_stats) {
1601                 stats = (i->f->get_fc_host_stats)(shost);
1602                 if (stats)
1603                         ret = snprintf(buf, 20, "0x%llx\n",
1604                               (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1605         }
1606         return ret;
1607 }
1608
1609
1610 /* generate a read-only statistics attribute */
1611 #define fc_host_statistic(name)                                         \
1612 static ssize_t show_fcstat_##name(struct device *cd,                    \
1613                                   struct device_attribute *attr,        \
1614                                   char *buf)                            \
1615 {                                                                       \
1616         return fc_stat_show(cd, buf,                                    \
1617                             offsetof(struct fc_host_statistics, name)); \
1618 }                                                                       \
1619 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1620
1621 fc_host_statistic(seconds_since_last_reset);
1622 fc_host_statistic(tx_frames);
1623 fc_host_statistic(tx_words);
1624 fc_host_statistic(rx_frames);
1625 fc_host_statistic(rx_words);
1626 fc_host_statistic(lip_count);
1627 fc_host_statistic(nos_count);
1628 fc_host_statistic(error_frames);
1629 fc_host_statistic(dumped_frames);
1630 fc_host_statistic(link_failure_count);
1631 fc_host_statistic(loss_of_sync_count);
1632 fc_host_statistic(loss_of_signal_count);
1633 fc_host_statistic(prim_seq_protocol_err_count);
1634 fc_host_statistic(invalid_tx_word_count);
1635 fc_host_statistic(invalid_crc_count);
1636 fc_host_statistic(fcp_input_requests);
1637 fc_host_statistic(fcp_output_requests);
1638 fc_host_statistic(fcp_control_requests);
1639 fc_host_statistic(fcp_input_megabytes);
1640 fc_host_statistic(fcp_output_megabytes);
1641
1642 static ssize_t
1643 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1644                     const char *buf, size_t count)
1645 {
1646         struct Scsi_Host *shost = transport_class_to_shost(dev);
1647         struct fc_internal *i = to_fc_internal(shost->transportt);
1648
1649         /* ignore any data value written to the attribute */
1650         if (i->f->reset_fc_host_stats) {
1651                 i->f->reset_fc_host_stats(shost);
1652                 return count;
1653         }
1654
1655         return -ENOENT;
1656 }
1657 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1658                                 fc_reset_statistics);
1659
1660 static struct attribute *fc_statistics_attrs[] = {
1661         &device_attr_host_seconds_since_last_reset.attr,
1662         &device_attr_host_tx_frames.attr,
1663         &device_attr_host_tx_words.attr,
1664         &device_attr_host_rx_frames.attr,
1665         &device_attr_host_rx_words.attr,
1666         &device_attr_host_lip_count.attr,
1667         &device_attr_host_nos_count.attr,
1668         &device_attr_host_error_frames.attr,
1669         &device_attr_host_dumped_frames.attr,
1670         &device_attr_host_link_failure_count.attr,
1671         &device_attr_host_loss_of_sync_count.attr,
1672         &device_attr_host_loss_of_signal_count.attr,
1673         &device_attr_host_prim_seq_protocol_err_count.attr,
1674         &device_attr_host_invalid_tx_word_count.attr,
1675         &device_attr_host_invalid_crc_count.attr,
1676         &device_attr_host_fcp_input_requests.attr,
1677         &device_attr_host_fcp_output_requests.attr,
1678         &device_attr_host_fcp_control_requests.attr,
1679         &device_attr_host_fcp_input_megabytes.attr,
1680         &device_attr_host_fcp_output_megabytes.attr,
1681         &device_attr_host_reset_statistics.attr,
1682         NULL
1683 };
1684
1685 static struct attribute_group fc_statistics_group = {
1686         .name = "statistics",
1687         .attrs = fc_statistics_attrs,
1688 };
1689
1690
1691 /* Host Vport Attributes */
1692
1693 static int
1694 fc_parse_wwn(const char *ns, u64 *nm)
1695 {
1696         unsigned int i, j;
1697         u8 wwn[8];
1698
1699         memset(wwn, 0, sizeof(wwn));
1700
1701         /* Validate and store the new name */
1702         for (i=0, j=0; i < 16; i++) {
1703                 if ((*ns >= 'a') && (*ns <= 'f'))
1704                         j = ((j << 4) | ((*ns++ -'a') + 10));
1705                 else if ((*ns >= 'A') && (*ns <= 'F'))
1706                         j = ((j << 4) | ((*ns++ -'A') + 10));
1707                 else if ((*ns >= '0') && (*ns <= '9'))
1708                         j = ((j << 4) | (*ns++ -'0'));
1709                 else
1710                         return -EINVAL;
1711                 if (i % 2) {
1712                         wwn[i/2] = j & 0xff;
1713                         j = 0;
1714                 }
1715         }
1716
1717         *nm = wwn_to_u64(wwn);
1718
1719         return 0;
1720 }
1721
1722
1723 /*
1724  * "Short-cut" sysfs variable to create a new vport on a FC Host.
1725  * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1726  * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1727  * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1728  */
1729 static ssize_t
1730 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1731                            const char *buf, size_t count)
1732 {
1733         struct Scsi_Host *shost = transport_class_to_shost(dev);
1734         struct fc_vport_identifiers vid;
1735         struct fc_vport *vport;
1736         unsigned int cnt=count;
1737         int stat;
1738
1739         memset(&vid, 0, sizeof(vid));
1740
1741         /* count may include a LF at end of string */
1742         if (buf[cnt-1] == '\n')
1743                 cnt--;
1744
1745         /* validate we have enough characters for WWPN */
1746         if ((cnt != (16+1+16)) || (buf[16] != ':'))
1747                 return -EINVAL;
1748
1749         stat = fc_parse_wwn(&buf[0], &vid.port_name);
1750         if (stat)
1751                 return stat;
1752
1753         stat = fc_parse_wwn(&buf[17], &vid.node_name);
1754         if (stat)
1755                 return stat;
1756
1757         vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1758         vid.vport_type = FC_PORTTYPE_NPIV;
1759         /* vid.symbolic_name is already zero/NULL's */
1760         vid.disable = false;            /* always enabled */
1761
1762         /* we only allow support on Channel 0 !!! */
1763         stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
1764         return stat ? stat : count;
1765 }
1766 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1767                         store_fc_host_vport_create);
1768
1769
1770 /*
1771  * "Short-cut" sysfs variable to delete a vport on a FC Host.
1772  * Vport is identified by a string containing "<WWPN>:<WWNN>".
1773  * The WWNs are specified as hex characters, and may *not* contain
1774  * any prefixes (e.g. 0x, x, etc)
1775  */
1776 static ssize_t
1777 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1778                            const char *buf, size_t count)
1779 {
1780         struct Scsi_Host *shost = transport_class_to_shost(dev);
1781         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1782         struct fc_vport *vport;
1783         u64 wwpn, wwnn;
1784         unsigned long flags;
1785         unsigned int cnt=count;
1786         int stat, match;
1787
1788         /* count may include a LF at end of string */
1789         if (buf[cnt-1] == '\n')
1790                 cnt--;
1791
1792         /* validate we have enough characters for WWPN */
1793         if ((cnt != (16+1+16)) || (buf[16] != ':'))
1794                 return -EINVAL;
1795
1796         stat = fc_parse_wwn(&buf[0], &wwpn);
1797         if (stat)
1798                 return stat;
1799
1800         stat = fc_parse_wwn(&buf[17], &wwnn);
1801         if (stat)
1802                 return stat;
1803
1804         spin_lock_irqsave(shost->host_lock, flags);
1805         match = 0;
1806         /* we only allow support on Channel 0 !!! */
1807         list_for_each_entry(vport, &fc_host->vports, peers) {
1808                 if ((vport->channel == 0) &&
1809                     (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1810                         match = 1;
1811                         break;
1812                 }
1813         }
1814         spin_unlock_irqrestore(shost->host_lock, flags);
1815
1816         if (!match)
1817                 return -ENODEV;
1818
1819         stat = fc_vport_terminate(vport);
1820         return stat ? stat : count;
1821 }
1822 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1823                         store_fc_host_vport_delete);
1824
1825
1826 static int fc_host_match(struct attribute_container *cont,
1827                           struct device *dev)
1828 {
1829         struct Scsi_Host *shost;
1830         struct fc_internal *i;
1831
1832         if (!scsi_is_host_device(dev))
1833                 return 0;
1834
1835         shost = dev_to_shost(dev);
1836         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1837             != &fc_host_class.class)
1838                 return 0;
1839
1840         i = to_fc_internal(shost->transportt);
1841
1842         return &i->t.host_attrs.ac == cont;
1843 }
1844
1845 static int fc_target_match(struct attribute_container *cont,
1846                             struct device *dev)
1847 {
1848         struct Scsi_Host *shost;
1849         struct fc_internal *i;
1850
1851         if (!scsi_is_target_device(dev))
1852                 return 0;
1853
1854         shost = dev_to_shost(dev->parent);
1855         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1856             != &fc_host_class.class)
1857                 return 0;
1858
1859         i = to_fc_internal(shost->transportt);
1860
1861         return &i->t.target_attrs.ac == cont;
1862 }
1863
1864 static void fc_rport_dev_release(struct device *dev)
1865 {
1866         struct fc_rport *rport = dev_to_rport(dev);
1867         put_device(dev->parent);
1868         kfree(rport);
1869 }
1870
1871 int scsi_is_fc_rport(const struct device *dev)
1872 {
1873         return dev->release == fc_rport_dev_release;
1874 }
1875 EXPORT_SYMBOL(scsi_is_fc_rport);
1876
1877 static int fc_rport_match(struct attribute_container *cont,
1878                             struct device *dev)
1879 {
1880         struct Scsi_Host *shost;
1881         struct fc_internal *i;
1882
1883         if (!scsi_is_fc_rport(dev))
1884                 return 0;
1885
1886         shost = dev_to_shost(dev->parent);
1887         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1888             != &fc_host_class.class)
1889                 return 0;
1890
1891         i = to_fc_internal(shost->transportt);
1892
1893         return &i->rport_attr_cont.ac == cont;
1894 }
1895
1896
1897 static void fc_vport_dev_release(struct device *dev)
1898 {
1899         struct fc_vport *vport = dev_to_vport(dev);
1900         put_device(dev->parent);                /* release kobj parent */
1901         kfree(vport);
1902 }
1903
1904 int scsi_is_fc_vport(const struct device *dev)
1905 {
1906         return dev->release == fc_vport_dev_release;
1907 }
1908 EXPORT_SYMBOL(scsi_is_fc_vport);
1909
1910 static int fc_vport_match(struct attribute_container *cont,
1911                             struct device *dev)
1912 {
1913         struct fc_vport *vport;
1914         struct Scsi_Host *shost;
1915         struct fc_internal *i;
1916
1917         if (!scsi_is_fc_vport(dev))
1918                 return 0;
1919         vport = dev_to_vport(dev);
1920
1921         shost = vport_to_shost(vport);
1922         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1923             != &fc_host_class.class)
1924                 return 0;
1925
1926         i = to_fc_internal(shost->transportt);
1927         return &i->vport_attr_cont.ac == cont;
1928 }
1929
1930
1931 /**
1932  * fc_timed_out - FC Transport I/O timeout intercept handler
1933  * @scmd:       The SCSI command which timed out
1934  *
1935  * This routine protects against error handlers getting invoked while a
1936  * rport is in a blocked state, typically due to a temporarily loss of
1937  * connectivity. If the error handlers are allowed to proceed, requests
1938  * to abort i/o, reset the target, etc will likely fail as there is no way
1939  * to communicate with the device to perform the requested function. These
1940  * failures may result in the midlayer taking the device offline, requiring
1941  * manual intervention to restore operation.
1942  *
1943  * This routine, called whenever an i/o times out, validates the state of
1944  * the underlying rport. If the rport is blocked, it returns
1945  * EH_RESET_TIMER, which will continue to reschedule the timeout.
1946  * Eventually, either the device will return, or devloss_tmo will fire,
1947  * and when the timeout then fires, it will be handled normally.
1948  * If the rport is not blocked, normal error handling continues.
1949  *
1950  * Notes:
1951  *      This routine assumes no locks are held on entry.
1952  */
1953 static enum scsi_eh_timer_return
1954 fc_timed_out(struct scsi_cmnd *scmd)
1955 {
1956         struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1957
1958         if (rport->port_state == FC_PORTSTATE_BLOCKED)
1959                 return EH_RESET_TIMER;
1960
1961         return EH_NOT_HANDLED;
1962 }
1963
1964 /*
1965  * Called by fc_user_scan to locate an rport on the shost that
1966  * matches the channel and target id, and invoke scsi_scan_target()
1967  * on the rport.
1968  */
1969 static void
1970 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1971 {
1972         struct fc_rport *rport;
1973         unsigned long flags;
1974
1975         spin_lock_irqsave(shost->host_lock, flags);
1976
1977         list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1978                 if (rport->scsi_target_id == -1)
1979                         continue;
1980
1981                 if (rport->port_state != FC_PORTSTATE_ONLINE)
1982                         continue;
1983
1984                 if ((channel == rport->channel) &&
1985                     (id == rport->scsi_target_id)) {
1986                         spin_unlock_irqrestore(shost->host_lock, flags);
1987                         scsi_scan_target(&rport->dev, channel, id, lun, 1);
1988                         return;
1989                 }
1990         }
1991
1992         spin_unlock_irqrestore(shost->host_lock, flags);
1993 }
1994
1995 /*
1996  * Called via sysfs scan routines. Necessary, as the FC transport
1997  * wants to place all target objects below the rport object. So this
1998  * routine must invoke the scsi_scan_target() routine with the rport
1999  * object as the parent.
2000  */
2001 static int
2002 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2003 {
2004         uint chlo, chhi;
2005         uint tgtlo, tgthi;
2006
2007         if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2008             ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2009             ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2010                 return -EINVAL;
2011
2012         if (channel == SCAN_WILD_CARD) {
2013                 chlo = 0;
2014                 chhi = shost->max_channel + 1;
2015         } else {
2016                 chlo = channel;
2017                 chhi = channel + 1;
2018         }
2019
2020         if (id == SCAN_WILD_CARD) {
2021                 tgtlo = 0;
2022                 tgthi = shost->max_id;
2023         } else {
2024                 tgtlo = id;
2025                 tgthi = id + 1;
2026         }
2027
2028         for ( ; chlo < chhi; chlo++)
2029                 for ( ; tgtlo < tgthi; tgtlo++)
2030                         fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2031
2032         return 0;
2033 }
2034
2035 static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2036                                 int result)
2037 {
2038         struct fc_internal *i = to_fc_internal(shost->transportt);
2039         return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2040 }
2041
2042 static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2043 {
2044         struct fc_internal *i = to_fc_internal(shost->transportt);
2045         return i->f->it_nexus_response(shost, nexus, result);
2046 }
2047
2048 struct scsi_transport_template *
2049 fc_attach_transport(struct fc_function_template *ft)
2050 {
2051         int count;
2052         struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2053                                         GFP_KERNEL);
2054
2055         if (unlikely(!i))
2056                 return NULL;
2057
2058         i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2059         i->t.target_attrs.ac.class = &fc_transport_class.class;
2060         i->t.target_attrs.ac.match = fc_target_match;
2061         i->t.target_size = sizeof(struct fc_starget_attrs);
2062         transport_container_register(&i->t.target_attrs);
2063
2064         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2065         i->t.host_attrs.ac.class = &fc_host_class.class;
2066         i->t.host_attrs.ac.match = fc_host_match;
2067         i->t.host_size = sizeof(struct fc_host_attrs);
2068         if (ft->get_fc_host_stats)
2069                 i->t.host_attrs.statistics = &fc_statistics_group;
2070         transport_container_register(&i->t.host_attrs);
2071
2072         i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2073         i->rport_attr_cont.ac.class = &fc_rport_class.class;
2074         i->rport_attr_cont.ac.match = fc_rport_match;
2075         transport_container_register(&i->rport_attr_cont);
2076
2077         i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2078         i->vport_attr_cont.ac.class = &fc_vport_class.class;
2079         i->vport_attr_cont.ac.match = fc_vport_match;
2080         transport_container_register(&i->vport_attr_cont);
2081
2082         i->f = ft;
2083
2084         /* Transport uses the shost workq for scsi scanning */
2085         i->t.create_work_queue = 1;
2086
2087         i->t.eh_timed_out = fc_timed_out;
2088
2089         i->t.user_scan = fc_user_scan;
2090
2091         /* target-mode drivers' functions */
2092         i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2093         i->t.it_nexus_response = fc_it_nexus_response;
2094
2095         /*
2096          * Setup SCSI Target Attributes.
2097          */
2098         count = 0;
2099         SETUP_STARGET_ATTRIBUTE_RD(node_name);
2100         SETUP_STARGET_ATTRIBUTE_RD(port_name);
2101         SETUP_STARGET_ATTRIBUTE_RD(port_id);
2102
2103         BUG_ON(count > FC_STARGET_NUM_ATTRS);
2104
2105         i->starget_attrs[count] = NULL;
2106
2107
2108         /*
2109          * Setup SCSI Host Attributes.
2110          */
2111         count=0;
2112         SETUP_HOST_ATTRIBUTE_RD(node_name);
2113         SETUP_HOST_ATTRIBUTE_RD(port_name);
2114         SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2115         SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2116         SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2117         SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2118         SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2119         if (ft->vport_create) {
2120                 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2121                 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2122         }
2123         SETUP_HOST_ATTRIBUTE_RD(serial_number);
2124
2125         SETUP_HOST_ATTRIBUTE_RD(port_id);
2126         SETUP_HOST_ATTRIBUTE_RD(port_type);
2127         SETUP_HOST_ATTRIBUTE_RD(port_state);
2128         SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2129         SETUP_HOST_ATTRIBUTE_RD(speed);
2130         SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2131         SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2132         SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2133
2134         /* Transport-managed attributes */
2135         SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2136         if (ft->issue_fc_host_lip)
2137                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2138         if (ft->vport_create)
2139                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2140         if (ft->vport_delete)
2141                 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2142
2143         BUG_ON(count > FC_HOST_NUM_ATTRS);
2144
2145         i->host_attrs[count] = NULL;
2146
2147         /*
2148          * Setup Remote Port Attributes.
2149          */
2150         count=0;
2151         SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2152         SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2153         SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2154         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2155         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2156         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2157         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2158         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2159         SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2160         if (ft->terminate_rport_io)
2161                 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2162
2163         BUG_ON(count > FC_RPORT_NUM_ATTRS);
2164
2165         i->rport_attrs[count] = NULL;
2166
2167         /*
2168          * Setup Virtual Port Attributes.
2169          */
2170         count=0;
2171         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2172         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2173         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2174         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2175         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2176         SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2177         SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2178         SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2179         SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2180
2181         BUG_ON(count > FC_VPORT_NUM_ATTRS);
2182
2183         i->vport_attrs[count] = NULL;
2184
2185         return &i->t;
2186 }
2187 EXPORT_SYMBOL(fc_attach_transport);
2188
2189 void fc_release_transport(struct scsi_transport_template *t)
2190 {
2191         struct fc_internal *i = to_fc_internal(t);
2192
2193         transport_container_unregister(&i->t.target_attrs);
2194         transport_container_unregister(&i->t.host_attrs);
2195         transport_container_unregister(&i->rport_attr_cont);
2196         transport_container_unregister(&i->vport_attr_cont);
2197
2198         kfree(i);
2199 }
2200 EXPORT_SYMBOL(fc_release_transport);
2201
2202 /**
2203  * fc_queue_work - Queue work to the fc_host workqueue.
2204  * @shost:      Pointer to Scsi_Host bound to fc_host.
2205  * @work:       Work to queue for execution.
2206  *
2207  * Return value:
2208  *      1 - work queued for execution
2209  *      0 - work is already queued
2210  *      -EINVAL - work queue doesn't exist
2211  */
2212 static int
2213 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2214 {
2215         if (unlikely(!fc_host_work_q(shost))) {
2216                 printk(KERN_ERR
2217                         "ERROR: FC host '%s' attempted to queue work, "
2218                         "when no workqueue created.\n", shost->hostt->name);
2219                 dump_stack();
2220
2221                 return -EINVAL;
2222         }
2223
2224         return queue_work(fc_host_work_q(shost), work);
2225 }
2226
2227 /**
2228  * fc_flush_work - Flush a fc_host's workqueue.
2229  * @shost:      Pointer to Scsi_Host bound to fc_host.
2230  */
2231 static void
2232 fc_flush_work(struct Scsi_Host *shost)
2233 {
2234         if (!fc_host_work_q(shost)) {
2235                 printk(KERN_ERR
2236                         "ERROR: FC host '%s' attempted to flush work, "
2237                         "when no workqueue created.\n", shost->hostt->name);
2238                 dump_stack();
2239                 return;
2240         }
2241
2242         flush_workqueue(fc_host_work_q(shost));
2243 }
2244
2245 /**
2246  * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2247  * @shost:      Pointer to Scsi_Host bound to fc_host.
2248  * @work:       Work to queue for execution.
2249  * @delay:      jiffies to delay the work queuing
2250  *
2251  * Return value:
2252  *      1 on success / 0 already queued / < 0 for error
2253  */
2254 static int
2255 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2256                                 unsigned long delay)
2257 {
2258         if (unlikely(!fc_host_devloss_work_q(shost))) {
2259                 printk(KERN_ERR
2260                         "ERROR: FC host '%s' attempted to queue work, "
2261                         "when no workqueue created.\n", shost->hostt->name);
2262                 dump_stack();
2263
2264                 return -EINVAL;
2265         }
2266
2267         return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2268 }
2269
2270 /**
2271  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2272  * @shost:      Pointer to Scsi_Host bound to fc_host.
2273  */
2274 static void
2275 fc_flush_devloss(struct Scsi_Host *shost)
2276 {
2277         if (!fc_host_devloss_work_q(shost)) {
2278                 printk(KERN_ERR
2279                         "ERROR: FC host '%s' attempted to flush work, "
2280                         "when no workqueue created.\n", shost->hostt->name);
2281                 dump_stack();
2282                 return;
2283         }
2284
2285         flush_workqueue(fc_host_devloss_work_q(shost));
2286 }
2287
2288
2289 /**
2290  * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2291  * @shost:      Which &Scsi_Host
2292  *
2293  * This routine is expected to be called immediately preceeding the
2294  * a driver's call to scsi_remove_host().
2295  *
2296  * WARNING: A driver utilizing the fc_transport, which fails to call
2297  *   this routine prior to scsi_remove_host(), will leave dangling
2298  *   objects in /sys/class/fc_remote_ports. Access to any of these
2299  *   objects can result in a system crash !!!
2300  *
2301  * Notes:
2302  *      This routine assumes no locks are held on entry.
2303  */
2304 void
2305 fc_remove_host(struct Scsi_Host *shost)
2306 {
2307         struct fc_vport *vport = NULL, *next_vport = NULL;
2308         struct fc_rport *rport = NULL, *next_rport = NULL;
2309         struct workqueue_struct *work_q;
2310         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2311         unsigned long flags;
2312
2313         spin_lock_irqsave(shost->host_lock, flags);
2314
2315         /* Remove any vports */
2316         list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2317                 fc_queue_work(shost, &vport->vport_delete_work);
2318
2319         /* Remove any remote ports */
2320         list_for_each_entry_safe(rport, next_rport,
2321                         &fc_host->rports, peers) {
2322                 list_del(&rport->peers);
2323                 rport->port_state = FC_PORTSTATE_DELETED;
2324                 fc_queue_work(shost, &rport->rport_delete_work);
2325         }
2326
2327         list_for_each_entry_safe(rport, next_rport,
2328                         &fc_host->rport_bindings, peers) {
2329                 list_del(&rport->peers);
2330                 rport->port_state = FC_PORTSTATE_DELETED;
2331                 fc_queue_work(shost, &rport->rport_delete_work);
2332         }
2333
2334         spin_unlock_irqrestore(shost->host_lock, flags);
2335
2336         /* flush all scan work items */
2337         scsi_flush_work(shost);
2338
2339         /* flush all stgt delete, and rport delete work items, then kill it  */
2340         if (fc_host->work_q) {
2341                 work_q = fc_host->work_q;
2342                 fc_host->work_q = NULL;
2343                 destroy_workqueue(work_q);
2344         }
2345
2346         /* flush all devloss work items, then kill it  */
2347         if (fc_host->devloss_work_q) {
2348                 work_q = fc_host->devloss_work_q;
2349                 fc_host->devloss_work_q = NULL;
2350                 destroy_workqueue(work_q);
2351         }
2352 }
2353 EXPORT_SYMBOL(fc_remove_host);
2354
2355
2356 /**
2357  * fc_starget_delete - called to delete the scsi decendents of an rport
2358  * @work:       remote port to be operated on.
2359  *
2360  * Deletes target and all sdevs.
2361  */
2362 static void
2363 fc_starget_delete(struct work_struct *work)
2364 {
2365         struct fc_rport *rport =
2366                 container_of(work, struct fc_rport, stgt_delete_work);
2367         struct Scsi_Host *shost = rport_to_shost(rport);
2368         struct fc_internal *i = to_fc_internal(shost->transportt);
2369
2370         /* Involve the LLDD if possible to terminate all io on the rport. */
2371         if (i->f->terminate_rport_io)
2372                 i->f->terminate_rport_io(rport);
2373
2374         scsi_remove_target(&rport->dev);
2375 }
2376
2377
2378 /**
2379  * fc_rport_final_delete - finish rport termination and delete it.
2380  * @work:       remote port to be deleted.
2381  */
2382 static void
2383 fc_rport_final_delete(struct work_struct *work)
2384 {
2385         struct fc_rport *rport =
2386                 container_of(work, struct fc_rport, rport_delete_work);
2387         struct device *dev = &rport->dev;
2388         struct Scsi_Host *shost = rport_to_shost(rport);
2389         struct fc_internal *i = to_fc_internal(shost->transportt);
2390         unsigned long flags;
2391
2392         /*
2393          * if a scan is pending, flush the SCSI Host work_q so that
2394          * that we can reclaim the rport scan work element.
2395          */
2396         if (rport->flags & FC_RPORT_SCAN_PENDING)
2397                 scsi_flush_work(shost);
2398
2399         /* involve the LLDD to terminate all pending i/o */
2400         if (i->f->terminate_rport_io)
2401                 i->f->terminate_rport_io(rport);
2402
2403         /*
2404          * Cancel any outstanding timers. These should really exist
2405          * only when rmmod'ing the LLDD and we're asking for
2406          * immediate termination of the rports
2407          */
2408         spin_lock_irqsave(shost->host_lock, flags);
2409         if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2410                 spin_unlock_irqrestore(shost->host_lock, flags);
2411                 if (!cancel_delayed_work(&rport->fail_io_work))
2412                         fc_flush_devloss(shost);
2413                 if (!cancel_delayed_work(&rport->dev_loss_work))
2414                         fc_flush_devloss(shost);
2415                 spin_lock_irqsave(shost->host_lock, flags);
2416                 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2417         }
2418         spin_unlock_irqrestore(shost->host_lock, flags);
2419
2420         /* Delete SCSI target and sdevs */
2421         if (rport->scsi_target_id != -1)
2422                 fc_starget_delete(&rport->stgt_delete_work);
2423
2424         /*
2425          * Notify the driver that the rport is now dead. The LLDD will
2426          * also guarantee that any communication to the rport is terminated
2427          */
2428         if (i->f->dev_loss_tmo_callbk)
2429                 i->f->dev_loss_tmo_callbk(rport);
2430
2431         transport_remove_device(dev);
2432         device_del(dev);
2433         transport_destroy_device(dev);
2434         put_device(&shost->shost_gendev);       /* for fc_host->rport list */
2435         put_device(dev);                        /* for self-reference */
2436 }
2437
2438
2439 /**
2440  * fc_rport_create - allocates and creates a remote FC port.
2441  * @shost:      scsi host the remote port is connected to.
2442  * @channel:    Channel on shost port connected to.
2443  * @ids:        The world wide names, fc address, and FC4 port
2444  *              roles for the remote port.
2445  *
2446  * Allocates and creates the remoter port structure, including the
2447  * class and sysfs creation.
2448  *
2449  * Notes:
2450  *      This routine assumes no locks are held on entry.
2451  */
2452 static struct fc_rport *
2453 fc_rport_create(struct Scsi_Host *shost, int channel,
2454         struct fc_rport_identifiers  *ids)
2455 {
2456         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2457         struct fc_internal *fci = to_fc_internal(shost->transportt);
2458         struct fc_rport *rport;
2459         struct device *dev;
2460         unsigned long flags;
2461         int error;
2462         size_t size;
2463
2464         size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2465         rport = kzalloc(size, GFP_KERNEL);
2466         if (unlikely(!rport)) {
2467                 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
2468                 return NULL;
2469         }
2470
2471         rport->maxframe_size = -1;
2472         rport->supported_classes = FC_COS_UNSPECIFIED;
2473         rport->dev_loss_tmo = fc_dev_loss_tmo;
2474         memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2475         memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2476         rport->port_id = ids->port_id;
2477         rport->roles = ids->roles;
2478         rport->port_state = FC_PORTSTATE_ONLINE;
2479         if (fci->f->dd_fcrport_size)
2480                 rport->dd_data = &rport[1];
2481         rport->channel = channel;
2482         rport->fast_io_fail_tmo = -1;
2483
2484         INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2485         INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2486         INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2487         INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2488         INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2489
2490         spin_lock_irqsave(shost->host_lock, flags);
2491
2492         rport->number = fc_host->next_rport_number++;
2493         if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
2494                 rport->scsi_target_id = fc_host->next_target_id++;
2495         else
2496                 rport->scsi_target_id = -1;
2497         list_add_tail(&rport->peers, &fc_host->rports);
2498         get_device(&shost->shost_gendev);       /* for fc_host->rport list */
2499
2500         spin_unlock_irqrestore(shost->host_lock, flags);
2501
2502         dev = &rport->dev;
2503         device_initialize(dev);                 /* takes self reference */
2504         dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2505         dev->release = fc_rport_dev_release;
2506         sprintf(dev->bus_id, "rport-%d:%d-%d",
2507                 shost->host_no, channel, rport->number);
2508         transport_setup_device(dev);
2509
2510         error = device_add(dev);
2511         if (error) {
2512                 printk(KERN_ERR "FC Remote Port device_add failed\n");
2513                 goto delete_rport;
2514         }
2515         transport_add_device(dev);
2516         transport_configure_device(dev);
2517
2518         if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2519                 /* initiate a scan of the target */
2520                 rport->flags |= FC_RPORT_SCAN_PENDING;
2521                 scsi_queue_work(shost, &rport->scan_work);
2522         }
2523
2524         return rport;
2525
2526 delete_rport:
2527         transport_destroy_device(dev);
2528         spin_lock_irqsave(shost->host_lock, flags);
2529         list_del(&rport->peers);
2530         put_device(&shost->shost_gendev);       /* for fc_host->rport list */
2531         spin_unlock_irqrestore(shost->host_lock, flags);
2532         put_device(dev->parent);
2533         kfree(rport);
2534         return NULL;
2535 }
2536
2537 /**
2538  * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2539  * @shost:      scsi host the remote port is connected to.
2540  * @channel:    Channel on shost port connected to.
2541  * @ids:        The world wide names, fc address, and FC4 port
2542  *              roles for the remote port.
2543  *
2544  * The LLDD calls this routine to notify the transport of the existence
2545  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2546  * of the port, it's FC address (port_id), and the FC4 roles that are
2547  * active for the port.
2548  *
2549  * For ports that are FCP targets (aka scsi targets), the FC transport
2550  * maintains consistent target id bindings on behalf of the LLDD.
2551  * A consistent target id binding is an assignment of a target id to
2552  * a remote port identifier, which persists while the scsi host is
2553  * attached. The remote port can disappear, then later reappear, and
2554  * it's target id assignment remains the same. This allows for shifts
2555  * in FC addressing (if binding by wwpn or wwnn) with no apparent
2556  * changes to the scsi subsystem which is based on scsi host number and
2557  * target id values.  Bindings are only valid during the attachment of
2558  * the scsi host. If the host detaches, then later re-attaches, target
2559  * id bindings may change.
2560  *
2561  * This routine is responsible for returning a remote port structure.
2562  * The routine will search the list of remote ports it maintains
2563  * internally on behalf of consistent target id mappings. If found, the
2564  * remote port structure will be reused. Otherwise, a new remote port
2565  * structure will be allocated.
2566  *
2567  * Whenever a remote port is allocated, a new fc_remote_port class
2568  * device is created.
2569  *
2570  * Should not be called from interrupt context.
2571  *
2572  * Notes:
2573  *      This routine assumes no locks are held on entry.
2574  */
2575 struct fc_rport *
2576 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2577         struct fc_rport_identifiers  *ids)
2578 {
2579         struct fc_internal *fci = to_fc_internal(shost->transportt);
2580         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2581         struct fc_rport *rport;
2582         unsigned long flags;
2583         int match = 0;
2584
2585         /* ensure any stgt delete functions are done */
2586         fc_flush_work(shost);
2587
2588         /*
2589          * Search the list of "active" rports, for an rport that has been
2590          * deleted, but we've held off the real delete while the target
2591          * is in a "blocked" state.
2592          */
2593         spin_lock_irqsave(shost->host_lock, flags);
2594
2595         list_for_each_entry(rport, &fc_host->rports, peers) {
2596
2597                 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2598                         (rport->channel == channel)) {
2599
2600                         switch (fc_host->tgtid_bind_type) {
2601                         case FC_TGTID_BIND_BY_WWPN:
2602                         case FC_TGTID_BIND_NONE:
2603                                 if (rport->port_name == ids->port_name)
2604                                         match = 1;
2605                                 break;
2606                         case FC_TGTID_BIND_BY_WWNN:
2607                                 if (rport->node_name == ids->node_name)
2608                                         match = 1;
2609                                 break;
2610                         case FC_TGTID_BIND_BY_ID:
2611                                 if (rport->port_id == ids->port_id)
2612                                         match = 1;
2613                                 break;
2614                         }
2615
2616                         if (match) {
2617
2618                                 memcpy(&rport->node_name, &ids->node_name,
2619                                         sizeof(rport->node_name));
2620                                 memcpy(&rport->port_name, &ids->port_name,
2621                                         sizeof(rport->port_name));
2622                                 rport->port_id = ids->port_id;
2623
2624                                 rport->port_state = FC_PORTSTATE_ONLINE;
2625                                 rport->roles = ids->roles;
2626
2627                                 spin_unlock_irqrestore(shost->host_lock, flags);
2628
2629                                 if (fci->f->dd_fcrport_size)
2630                                         memset(rport->dd_data, 0,
2631                                                 fci->f->dd_fcrport_size);
2632
2633                                 /*
2634                                  * If we were not a target, cancel the
2635                                  * io terminate and rport timers, and
2636                                  * we're done.
2637                                  *
2638                                  * If we were a target, but our new role
2639                                  * doesn't indicate a target, leave the
2640                                  * timers running expecting the role to
2641                                  * change as the target fully logs in. If
2642                                  * it doesn't, the target will be torn down.
2643                                  *
2644                                  * If we were a target, and our role shows
2645                                  * we're still a target, cancel the timers
2646                                  * and kick off a scan.
2647                                  */
2648
2649                                 /* was a target, not in roles */
2650                                 if ((rport->scsi_target_id != -1) &&
2651                                     (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2652                                         return rport;
2653
2654                                 /*
2655                                  * Stop the fail io and dev_loss timers.
2656                                  * If they flush, the port_state will
2657                                  * be checked and will NOOP the function.
2658                                  */
2659                                 if (!cancel_delayed_work(&rport->fail_io_work))
2660                                         fc_flush_devloss(shost);
2661                                 if (!cancel_delayed_work(&rport->dev_loss_work))
2662                                         fc_flush_devloss(shost);
2663
2664                                 spin_lock_irqsave(shost->host_lock, flags);
2665
2666                                 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2667
2668                                 /* if target, initiate a scan */
2669                                 if (rport->scsi_target_id != -1) {
2670                                         rport->flags |= FC_RPORT_SCAN_PENDING;
2671                                         scsi_queue_work(shost,
2672                                                         &rport->scan_work);
2673                                         spin_unlock_irqrestore(shost->host_lock,
2674                                                         flags);
2675                                         scsi_target_unblock(&rport->dev);
2676                                 } else
2677                                         spin_unlock_irqrestore(shost->host_lock,
2678                                                         flags);
2679
2680                                 return rport;
2681                         }
2682                 }
2683         }
2684
2685         /*
2686          * Search the bindings array
2687          * Note: if never a FCP target, you won't be on this list
2688          */
2689         if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2690
2691                 /* search for a matching consistent binding */
2692
2693                 list_for_each_entry(rport, &fc_host->rport_bindings,
2694                                         peers) {
2695                         if (rport->channel != channel)
2696                                 continue;
2697
2698                         switch (fc_host->tgtid_bind_type) {
2699                         case FC_TGTID_BIND_BY_WWPN:
2700                                 if (rport->port_name == ids->port_name)
2701                                         match = 1;
2702                                 break;
2703                         case FC_TGTID_BIND_BY_WWNN:
2704                                 if (rport->node_name == ids->node_name)
2705                                         match = 1;
2706                                 break;
2707                         case FC_TGTID_BIND_BY_ID:
2708                                 if (rport->port_id == ids->port_id)
2709                                         match = 1;
2710                                 break;
2711                         case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2712                                 break;
2713                         }
2714
2715                         if (match) {
2716                                 list_move_tail(&rport->peers, &fc_host->rports);
2717                                 break;
2718                         }
2719                 }
2720
2721                 if (match) {
2722                         memcpy(&rport->node_name, &ids->node_name,
2723                                 sizeof(rport->node_name));
2724                         memcpy(&rport->port_name, &ids->port_name,
2725                                 sizeof(rport->port_name));
2726                         rport->port_id = ids->port_id;
2727                         rport->roles = ids->roles;
2728                         rport->port_state = FC_PORTSTATE_ONLINE;
2729
2730                         if (fci->f->dd_fcrport_size)
2731                                 memset(rport->dd_data, 0,
2732                                                 fci->f->dd_fcrport_size);
2733
2734                         if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2735                                 /* initiate a scan of the target */
2736                                 rport->flags |= FC_RPORT_SCAN_PENDING;
2737                                 scsi_queue_work(shost, &rport->scan_work);
2738                                 spin_unlock_irqrestore(shost->host_lock, flags);
2739                                 scsi_target_unblock(&rport->dev);
2740                         } else
2741                                 spin_unlock_irqrestore(shost->host_lock, flags);
2742
2743                         return rport;
2744                 }
2745         }
2746
2747         spin_unlock_irqrestore(shost->host_lock, flags);
2748
2749         /* No consistent binding found - create new remote port entry */
2750         rport = fc_rport_create(shost, channel, ids);
2751
2752         return rport;
2753 }
2754 EXPORT_SYMBOL(fc_remote_port_add);
2755
2756
2757 /**
2758  * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2759  * @rport:      The remote port that no longer exists
2760  *
2761  * The LLDD calls this routine to notify the transport that a remote
2762  * port is no longer part of the topology. Note: Although a port
2763  * may no longer be part of the topology, it may persist in the remote
2764  * ports displayed by the fc_host. We do this under 2 conditions:
2765  * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2766  *   This allows the port to temporarily disappear, then reappear without
2767  *   disrupting the SCSI device tree attached to it. During the "blocked"
2768  *   period the port will still exist.
2769  * 2) If the port was a scsi target and disappears for longer than we
2770  *   expect, we'll delete the port and the tear down the SCSI device tree
2771  *   attached to it. However, we want to semi-persist the target id assigned
2772  *   to that port if it eventually does exist. The port structure will
2773  *   remain (although with minimal information) so that the target id
2774  *   bindings remails.
2775  *
2776  * If the remote port is not an FCP Target, it will be fully torn down
2777  * and deallocated, including the fc_remote_port class device.
2778  *
2779  * If the remote port is an FCP Target, the port will be placed in a
2780  * temporary blocked state. From the LLDD's perspective, the rport no
2781  * longer exists. From the SCSI midlayer's perspective, the SCSI target
2782  * exists, but all sdevs on it are blocked from further I/O. The following
2783  * is then expected.
2784  *
2785  *   If the remote port does not return (signaled by a LLDD call to
2786  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2787  *   scsi target is removed - killing all outstanding i/o and removing the
2788  *   scsi devices attached ot it. The port structure will be marked Not
2789  *   Present and be partially cleared, leaving only enough information to
2790  *   recognize the remote port relative to the scsi target id binding if
2791  *   it later appears.  The port will remain as long as there is a valid
2792  *   binding (e.g. until the user changes the binding type or unloads the
2793  *   scsi host with the binding).
2794  *
2795  *   If the remote port returns within the dev_loss_tmo value (and matches
2796  *   according to the target id binding type), the port structure will be
2797  *   reused. If it is no longer a SCSI target, the target will be torn
2798  *   down. If it continues to be a SCSI target, then the target will be
2799  *   unblocked (allowing i/o to be resumed), and a scan will be activated
2800  *   to ensure that all luns are detected.
2801  *
2802  * Called from normal process context only - cannot be called from interrupt.
2803  *
2804  * Notes:
2805  *      This routine assumes no locks are held on entry.
2806  */
2807 void
2808 fc_remote_port_delete(struct fc_rport  *rport)
2809 {
2810         struct Scsi_Host *shost = rport_to_shost(rport);
2811         struct fc_internal *i = to_fc_internal(shost->transportt);
2812         int timeout = rport->dev_loss_tmo;
2813         unsigned long flags;
2814
2815         /*
2816          * No need to flush the fc_host work_q's, as all adds are synchronous.
2817          *
2818          * We do need to reclaim the rport scan work element, so eventually
2819          * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2820          * there's still a scan pending.
2821          */
2822
2823         spin_lock_irqsave(shost->host_lock, flags);
2824
2825         if (rport->port_state != FC_PORTSTATE_ONLINE) {
2826                 spin_unlock_irqrestore(shost->host_lock, flags);
2827                 return;
2828         }
2829
2830         /*
2831          * In the past, we if this was not an FCP-Target, we would
2832          * unconditionally just jump to deleting the rport.
2833          * However, rports can be used as node containers by the LLDD,
2834          * and its not appropriate to just terminate the rport at the
2835          * first sign of a loss in connectivity. The LLDD may want to
2836          * send ELS traffic to re-validate the login. If the rport is
2837          * immediately deleted, it makes it inappropriate for a node
2838          * container.
2839          * So... we now unconditionally wait dev_loss_tmo before
2840          * destroying an rport.
2841          */
2842
2843         rport->port_state = FC_PORTSTATE_BLOCKED;
2844
2845         rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2846
2847         spin_unlock_irqrestore(shost->host_lock, flags);
2848
2849         if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2850             shost->active_mode & MODE_TARGET)
2851                 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2852
2853         scsi_target_block(&rport->dev);
2854
2855         /* see if we need to kill io faster than waiting for device loss */
2856         if ((rport->fast_io_fail_tmo != -1) &&
2857             (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2858                 fc_queue_devloss_work(shost, &rport->fail_io_work,
2859                                         rport->fast_io_fail_tmo * HZ);
2860
2861         /* cap the length the devices can be blocked until they are deleted */
2862         fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2863 }
2864 EXPORT_SYMBOL(fc_remote_port_delete);
2865
2866 /**
2867  * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2868  * @rport:      The remote port that changed.
2869  * @roles:      New roles for this port.
2870  *
2871  * Description: The LLDD calls this routine to notify the transport that the
2872  * roles on a remote port may have changed. The largest effect of this is
2873  * if a port now becomes a FCP Target, it must be allocated a
2874  * scsi target id.  If the port is no longer a FCP target, any
2875  * scsi target id value assigned to it will persist in case the
2876  * role changes back to include FCP Target. No changes in the scsi
2877  * midlayer will be invoked if the role changes (in the expectation
2878  * that the role will be resumed. If it doesn't normal error processing
2879  * will take place).
2880  *
2881  * Should not be called from interrupt context.
2882  *
2883  * Notes:
2884  *      This routine assumes no locks are held on entry.
2885  */
2886 void
2887 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
2888 {
2889         struct Scsi_Host *shost = rport_to_shost(rport);
2890         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2891         unsigned long flags;
2892         int create = 0;
2893         int ret;
2894
2895         spin_lock_irqsave(shost->host_lock, flags);
2896         if (roles & FC_PORT_ROLE_FCP_TARGET) {
2897                 if (rport->scsi_target_id == -1) {
2898                         rport->scsi_target_id = fc_host->next_target_id++;
2899                         create = 1;
2900                 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
2901                         create = 1;
2902         } else if (shost->active_mode & MODE_TARGET) {
2903                 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2904                                              (char *)&rport->node_name);
2905                 if (ret)
2906                         printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2907                                ret);
2908         }
2909
2910         rport->roles = roles;
2911
2912         spin_unlock_irqrestore(shost->host_lock, flags);
2913
2914         if (create) {
2915                 /*
2916                  * There may have been a delete timer running on the
2917                  * port. Ensure that it is cancelled as we now know
2918                  * the port is an FCP Target.
2919                  * Note: we know the rport is exists and in an online
2920                  *  state as the LLDD would not have had an rport
2921                  *  reference to pass us.
2922                  *
2923                  * Take no action on the del_timer failure as the state
2924                  * machine state change will validate the
2925                  * transaction.
2926                  */
2927                 if (!cancel_delayed_work(&rport->fail_io_work))
2928                         fc_flush_devloss(shost);
2929                 if (!cancel_delayed_work(&rport->dev_loss_work))
2930                         fc_flush_devloss(shost);
2931
2932                 spin_lock_irqsave(shost->host_lock, flags);
2933                 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2934                 spin_unlock_irqrestore(shost->host_lock, flags);
2935
2936                 /* ensure any stgt delete functions are done */
2937                 fc_flush_work(shost);
2938
2939                 /* initiate a scan of the target */
2940                 spin_lock_irqsave(shost->host_lock, flags);
2941                 rport->flags |= FC_RPORT_SCAN_PENDING;
2942                 scsi_queue_work(shost, &rport->scan_work);
2943                 spin_unlock_irqrestore(shost->host_lock, flags);
2944                 scsi_target_unblock(&rport->dev);
2945         }
2946 }
2947 EXPORT_SYMBOL(fc_remote_port_rolechg);
2948
2949 /**
2950  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
2951  * @work:       rport target that failed to reappear in the allotted time.
2952  *
2953  * Description: An attempt to delete a remote port blocks, and if it fails
2954  *              to return in the allotted time this gets called.
2955  */
2956 static void
2957 fc_timeout_deleted_rport(struct work_struct *work)
2958 {
2959         struct fc_rport *rport =
2960                 container_of(work, struct fc_rport, dev_loss_work.work);
2961         struct Scsi_Host *shost = rport_to_shost(rport);
2962         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2963         unsigned long flags;
2964
2965         spin_lock_irqsave(shost->host_lock, flags);
2966
2967         rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2968
2969         /*
2970          * If the port is ONLINE, then it came back. If it was a SCSI
2971          * target, validate it still is. If not, tear down the
2972          * scsi_target on it.
2973          */
2974         if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2975             (rport->scsi_target_id != -1) &&
2976             !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
2977                 dev_printk(KERN_ERR, &rport->dev,
2978                         "blocked FC remote port time out: no longer"
2979                         " a FCP target, removing starget\n");
2980                 spin_unlock_irqrestore(shost->host_lock, flags);
2981                 scsi_target_unblock(&rport->dev);
2982                 fc_queue_work(shost, &rport->stgt_delete_work);
2983                 return;
2984         }
2985
2986         /* NOOP state - we're flushing workq's */
2987         if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2988                 spin_unlock_irqrestore(shost->host_lock, flags);
2989                 dev_printk(KERN_ERR, &rport->dev,
2990                         "blocked FC remote port time out: leaving"
2991                         " rport%s alone\n",
2992                         (rport->scsi_target_id != -1) ?  " and starget" : "");
2993                 return;
2994         }
2995
2996         if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
2997             (rport->scsi_target_id == -1)) {
2998                 list_del(&rport->peers);
2999                 rport->port_state = FC_PORTSTATE_DELETED;
3000                 dev_printk(KERN_ERR, &rport->dev,
3001                         "blocked FC remote port time out: removing"
3002                         " rport%s\n",
3003                         (rport->scsi_target_id != -1) ?  " and starget" : "");
3004                 fc_queue_work(shost, &rport->rport_delete_work);
3005                 spin_unlock_irqrestore(shost->host_lock, flags);
3006                 return;
3007         }
3008
3009         dev_printk(KERN_ERR, &rport->dev,
3010                 "blocked FC remote port time out: removing target and "
3011                 "saving binding\n");
3012
3013         list_move_tail(&rport->peers, &fc_host->rport_bindings);
3014
3015         /*
3016          * Note: We do not remove or clear the hostdata area. This allows
3017          *   host-specific target data to persist along with the
3018          *   scsi_target_id. It's up to the host to manage it's hostdata area.
3019          */
3020
3021         /*
3022          * Reinitialize port attributes that may change if the port comes back.
3023          */
3024         rport->maxframe_size = -1;
3025         rport->supported_classes = FC_COS_UNSPECIFIED;
3026         rport->roles = FC_PORT_ROLE_UNKNOWN;
3027         rport->port_state = FC_PORTSTATE_NOTPRESENT;
3028
3029         /* remove the identifiers that aren't used in the consisting binding */
3030         switch (fc_host->tgtid_bind_type) {
3031         case FC_TGTID_BIND_BY_WWPN:
3032                 rport->node_name = -1;
3033                 rport->port_id = -1;
3034                 break;
3035         case FC_TGTID_BIND_BY_WWNN:
3036                 rport->port_name = -1;
3037                 rport->port_id = -1;
3038                 break;
3039         case FC_TGTID_BIND_BY_ID:
3040                 rport->node_name = -1;
3041                 rport->port_name = -1;
3042                 break;
3043         case FC_TGTID_BIND_NONE:        /* to keep compiler happy */
3044                 break;
3045         }
3046
3047         /*
3048          * As this only occurs if the remote port (scsi target)
3049          * went away and didn't come back - we'll remove
3050          * all attached scsi devices.
3051          */
3052         spin_unlock_irqrestore(shost->host_lock, flags);
3053
3054         scsi_target_unblock(&rport->dev);
3055         fc_queue_work(shost, &rport->stgt_delete_work);
3056 }
3057
3058 /**
3059  * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3060  * @work:       rport to terminate io on.
3061  *
3062  * Notes: Only requests the failure of the io, not that all are flushed
3063  *    prior to returning.
3064  */
3065 static void
3066 fc_timeout_fail_rport_io(struct work_struct *work)
3067 {
3068         struct fc_rport *rport =
3069                 container_of(work, struct fc_rport, fail_io_work.work);
3070         struct Scsi_Host *shost = rport_to_shost(rport);
3071         struct fc_internal *i = to_fc_internal(shost->transportt);
3072
3073         if (rport->port_state != FC_PORTSTATE_BLOCKED)
3074                 return;
3075
3076         i->f->terminate_rport_io(rport);
3077 }
3078
3079 /**
3080  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3081  * @work:       remote port to be scanned.
3082  */
3083 static void
3084 fc_scsi_scan_rport(struct work_struct *work)
3085 {
3086         struct fc_rport *rport =
3087                 container_of(work, struct fc_rport, scan_work);
3088         struct Scsi_Host *shost = rport_to_shost(rport);
3089         struct fc_internal *i = to_fc_internal(shost->transportt);
3090         unsigned long flags;
3091
3092         if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3093             (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3094             !(i->f->disable_target_scan)) {
3095                 scsi_scan_target(&rport->dev, rport->channel,
3096                         rport->scsi_target_id, SCAN_WILD_CARD, 1);
3097         }
3098
3099         spin_lock_irqsave(shost->host_lock, flags);
3100         rport->flags &= ~FC_RPORT_SCAN_PENDING;
3101         spin_unlock_irqrestore(shost->host_lock, flags);
3102 }
3103
3104
3105 /**
3106  * fc_vport_create - allocates and creates a FC virtual port.
3107  * @shost:      scsi host the virtual port is connected to.
3108  * @channel:    Channel on shost port connected to.
3109  * @pdev:       parent device for vport
3110  * @ids:        The world wide names, FC4 port roles, etc for
3111  *              the virtual port.
3112  * @ret_vport:  The pointer to the created vport.
3113  *
3114  * Allocates and creates the vport structure, calls the parent host
3115  * to instantiate the vport, the completes w/ class and sysfs creation.
3116  *
3117  * Notes:
3118  *      This routine assumes no locks are held on entry.
3119  */
3120 static int
3121 fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
3122         struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3123 {
3124         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3125         struct fc_internal *fci = to_fc_internal(shost->transportt);
3126         struct fc_vport *vport;
3127         struct device *dev;
3128         unsigned long flags;
3129         size_t size;
3130         int error;
3131
3132         *ret_vport = NULL;
3133
3134         if ( ! fci->f->vport_create)
3135                 return -ENOENT;
3136
3137         size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3138         vport = kzalloc(size, GFP_KERNEL);
3139         if (unlikely(!vport)) {
3140                 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
3141                 return -ENOMEM;
3142         }
3143
3144         vport->vport_state = FC_VPORT_UNKNOWN;
3145         vport->vport_last_state = FC_VPORT_UNKNOWN;
3146         vport->node_name = ids->node_name;
3147         vport->port_name = ids->port_name;
3148         vport->roles = ids->roles;
3149         vport->vport_type = ids->vport_type;
3150         if (fci->f->dd_fcvport_size)
3151                 vport->dd_data = &vport[1];
3152         vport->shost = shost;
3153         vport->channel = channel;
3154         vport->flags = FC_VPORT_CREATING;
3155         INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3156
3157         spin_lock_irqsave(shost->host_lock, flags);
3158
3159         if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3160                 spin_unlock_irqrestore(shost->host_lock, flags);
3161                 kfree(vport);
3162                 return -ENOSPC;
3163         }
3164         fc_host->npiv_vports_inuse++;
3165         vport->number = fc_host->next_vport_number++;
3166         list_add_tail(&vport->peers, &fc_host->vports);
3167         get_device(&shost->shost_gendev);       /* for fc_host->vport list */
3168
3169         spin_unlock_irqrestore(shost->host_lock, flags);
3170
3171         dev = &vport->dev;
3172         device_initialize(dev);                 /* takes self reference */
3173         dev->parent = get_device(pdev);         /* takes parent reference */
3174         dev->release = fc_vport_dev_release;
3175         sprintf(dev->bus_id, "vport-%d:%d-%d",
3176                 shost->host_no, channel, vport->number);
3177         transport_setup_device(dev);
3178
3179         error = device_add(dev);
3180         if (error) {
3181                 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3182                 goto delete_vport;
3183         }
3184         transport_add_device(dev);
3185         transport_configure_device(dev);
3186
3187         error = fci->f->vport_create(vport, ids->disable);
3188         if (error) {
3189                 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3190                 goto delete_vport_all;
3191         }
3192
3193         /*
3194          * if the parent isn't the physical adapter's Scsi_Host, ensure
3195          * the Scsi_Host at least contains ia symlink to the vport.
3196          */
3197         if (pdev != &shost->shost_gendev) {
3198                 error = sysfs_create_link(&shost->shost_gendev.kobj,
3199                                  &dev->kobj, dev->bus_id);
3200                 if (error)
3201                         printk(KERN_ERR
3202                                 "%s: Cannot create vport symlinks for "
3203                                 "%s, err=%d\n",
3204                                 __FUNCTION__, dev->bus_id, error);
3205         }
3206         spin_lock_irqsave(shost->host_lock, flags);
3207         vport->flags &= ~FC_VPORT_CREATING;
3208         spin_unlock_irqrestore(shost->host_lock, flags);
3209
3210         dev_printk(KERN_NOTICE, pdev,
3211                         "%s created via shost%d channel %d\n", dev->bus_id,
3212                         shost->host_no, channel);
3213
3214         *ret_vport = vport;
3215
3216         return 0;
3217
3218 delete_vport_all:
3219         transport_remove_device(dev);
3220         device_del(dev);
3221 delete_vport:
3222         transport_destroy_device(dev);
3223         spin_lock_irqsave(shost->host_lock, flags);
3224         list_del(&vport->peers);
3225         put_device(&shost->shost_gendev);       /* for fc_host->vport list */
3226         fc_host->npiv_vports_inuse--;
3227         spin_unlock_irqrestore(shost->host_lock, flags);
3228         put_device(dev->parent);
3229         kfree(vport);
3230
3231         return error;
3232 }
3233
3234
3235 /**
3236  * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3237  * @vport:      fc_vport to be terminated
3238  *
3239  * Calls the LLDD vport_delete() function, then deallocates and removes
3240  * the vport from the shost and object tree.
3241  *
3242  * Notes:
3243  *      This routine assumes no locks are held on entry.
3244  */
3245 int
3246 fc_vport_terminate(struct fc_vport *vport)
3247 {
3248         struct Scsi_Host *shost = vport_to_shost(vport);
3249         struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3250         struct fc_internal *i = to_fc_internal(shost->transportt);
3251         struct device *dev = &vport->dev;
3252         unsigned long flags;
3253         int stat;
3254
3255         spin_lock_irqsave(shost->host_lock, flags);
3256         if (vport->flags & FC_VPORT_CREATING) {
3257                 spin_unlock_irqrestore(shost->host_lock, flags);
3258                 return -EBUSY;
3259         }
3260         if (vport->flags & (FC_VPORT_DEL)) {
3261                 spin_unlock_irqrestore(shost->host_lock, flags);
3262                 return -EALREADY;
3263         }
3264         vport->flags |= FC_VPORT_DELETING;
3265         spin_unlock_irqrestore(shost->host_lock, flags);
3266
3267         if (i->f->vport_delete)
3268                 stat = i->f->vport_delete(vport);
3269         else
3270                 stat = -ENOENT;
3271
3272         spin_lock_irqsave(shost->host_lock, flags);
3273         vport->flags &= ~FC_VPORT_DELETING;
3274         if (!stat) {
3275                 vport->flags |= FC_VPORT_DELETED;
3276                 list_del(&vport->peers);
3277                 fc_host->npiv_vports_inuse--;
3278                 put_device(&shost->shost_gendev);  /* for fc_host->vport list */
3279         }
3280         spin_unlock_irqrestore(shost->host_lock, flags);
3281
3282         if (stat)
3283                 return stat;
3284
3285         if (dev->parent != &shost->shost_gendev)
3286                 sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
3287         transport_remove_device(dev);
3288         device_del(dev);
3289         transport_destroy_device(dev);
3290
3291         /*
3292          * Removing our self-reference should mean our
3293          * release function gets called, which will drop the remaining
3294          * parent reference and free the data structure.
3295          */
3296         put_device(dev);                        /* for self-reference */
3297
3298         return 0; /* SUCCESS */
3299 }
3300 EXPORT_SYMBOL(fc_vport_terminate);
3301
3302 /**
3303  * fc_vport_sched_delete - workq-based delete request for a vport
3304  * @work:       vport to be deleted.
3305  */
3306 static void
3307 fc_vport_sched_delete(struct work_struct *work)
3308 {
3309         struct fc_vport *vport =
3310                 container_of(work, struct fc_vport, vport_delete_work);
3311         int stat;
3312
3313         stat = fc_vport_terminate(vport);
3314         if (stat)
3315                 dev_printk(KERN_ERR, vport->dev.parent,
3316                         "%s: %s could not be deleted created via "
3317                         "shost%d channel %d - error %d\n", __FUNCTION__,
3318                         vport->dev.bus_id, vport->shost->host_no,
3319                         vport->channel, stat);
3320 }
3321
3322
3323 /* Original Author:  Martin Hicks */
3324 MODULE_AUTHOR("James Smart");
3325 MODULE_DESCRIPTION("FC Transport Attributes");
3326 MODULE_LICENSE("GPL");
3327
3328 module_init(fc_transport_init);
3329 module_exit(fc_transport_exit);