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