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