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