Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / drivers / scsi / scsi_transport_fc.c
index b4d1ece..b1119da 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
  *  FiberChannel transport specific attributes exported to sysfs.
  *
  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
  *
  *  ========
  *
- *  Copyright (C) 2004-2005   James Smart, Emulex Corporation
+ *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
  *    Rewrite for host, target, device, and remote port attributes,
  *    statistics, and service functions...
+ *    Add vports, etc
  *
  */
 #include <linux/module.h>
 #include <net/netlink.h>
 #include <scsi/scsi_netlink_fc.h>
 #include "scsi_priv.h"
+#include "scsi_transport_fc_internal.h"
 
 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
+static void fc_vport_sched_delete(struct work_struct *work);
+
+/*
+ * This is a temporary carrier for creating a vport. It will eventually
+ * be replaced  by a real message definition for sgio or netlink.
+ *
+ * fc_vport_identifiers: This set of data contains all elements
+ * to uniquely identify and instantiate a FC virtual port.
+ *
+ * Notes:
+ *   symbolic_name: The driver is to append the symbolic_name string data
+ *      to the symbolic_node_name data that it generates by default.
+ *      the resulting combination should then be registered with the switch.
+ *      It is expected that things like Xen may stuff a VM title into
+ *      this field.
+ */
+struct fc_vport_identifiers {
+       u64 node_name;
+       u64 port_name;
+       u32 roles;
+       bool disable;
+       enum fc_port_type vport_type;   /* only FC_PORTTYPE_NPIV allowed */
+       char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
+};
+
+static int fc_vport_create(struct Scsi_Host *shost, int channel,
+       struct device *pdev, struct fc_vport_identifiers  *ids,
+       struct fc_vport **vport);
 
 /*
  * Redefine so that we can have same named attributes in the
@@ -90,10 +120,14 @@ static struct {
        { FC_PORTTYPE_NLPORT,   "NLPort (fabric via loop)" },
        { FC_PORTTYPE_LPORT,    "LPort (private loop)" },
        { FC_PORTTYPE_PTP,      "Point-To-Point (direct nport connection" },
+       { FC_PORTTYPE_NPIV,             "NPIV VPORT" },
 };
 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
 #define FC_PORTTYPE_MAX_NAMELEN                50
 
+/* Reuse fc_port_type enum function for vport_type */
+#define get_fc_vport_type_name get_fc_port_type_name
+
 
 /* Convert fc_host_event_code values to ascii string name */
 static const struct {
@@ -139,6 +173,29 @@ fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
 #define FC_PORTSTATE_MAX_NAMELEN       20
 
 
+/* Convert fc_vport_state values to ascii string name */
+static struct {
+       enum fc_vport_state     value;
+       char                    *name;
+} fc_vport_state_names[] = {
+       { FC_VPORT_UNKNOWN,             "Unknown" },
+       { FC_VPORT_ACTIVE,              "Active" },
+       { FC_VPORT_DISABLED,            "Disabled" },
+       { FC_VPORT_LINKDOWN,            "Linkdown" },
+       { FC_VPORT_INITIALIZING,        "Initializing" },
+       { FC_VPORT_NO_FABRIC_SUPP,      "No Fabric Support" },
+       { FC_VPORT_NO_FABRIC_RSCS,      "No Fabric Resources" },
+       { FC_VPORT_FABRIC_LOGOUT,       "Fabric Logout" },
+       { FC_VPORT_FABRIC_REJ_WWN,      "Fabric Rejected WWN" },
+       { FC_VPORT_FAILED,              "VPort Failed" },
+};
+fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
+#define FC_VPORTSTATE_MAX_NAMELEN      24
+
+/* Reuse fc_vport_state enum function for vport_last_state */
+#define get_fc_vport_last_state_name get_fc_vport_state_name
+
+
 /* Convert fc_tgtid_binding_type values to ascii string name */
 static const struct {
        enum fc_tgtid_binding_type      value;
@@ -219,16 +276,16 @@ show_fc_fc4s (char *buf, u8 *fc4_list)
 }
 
 
-/* Convert FC_RPORT_ROLE bit values to ascii string name */
+/* Convert FC_PORT_ROLE bit values to ascii string name */
 static const struct {
        u32                     value;
        char                    *name;
-} fc_remote_port_role_names[] = {
-       { FC_RPORT_ROLE_FCP_TARGET,     "FCP Target" },
-       { FC_RPORT_ROLE_FCP_INITIATOR,  "FCP Initiator" },
-       { FC_RPORT_ROLE_IP_PORT,        "IP Port" },
+} fc_port_role_names[] = {
+       { FC_PORT_ROLE_FCP_TARGET,      "FCP Target" },
+       { FC_PORT_ROLE_FCP_INITIATOR,   "FCP Initiator" },
+       { FC_PORT_ROLE_IP_PORT,         "IP Port" },
 };
-fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
+fc_bitfield_name_search(port_roles, fc_port_role_names)
 
 /*
  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
@@ -252,7 +309,8 @@ static void fc_scsi_scan_rport(struct work_struct *work);
  */
 #define FC_STARGET_NUM_ATTRS   3
 #define FC_RPORT_NUM_ATTRS     10
-#define FC_HOST_NUM_ATTRS      17
+#define FC_VPORT_NUM_ATTRS     9
+#define FC_HOST_NUM_ATTRS      21
 
 struct fc_internal {
        struct scsi_transport_template t;
@@ -278,6 +336,10 @@ struct fc_internal {
        struct transport_container rport_attr_cont;
        struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
        struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
+
+       struct transport_container vport_attr_cont;
+       struct class_device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
+       struct class_device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
 };
 
 #define to_fc_internal(tmpl)   container_of(tmpl, struct fc_internal, t)
@@ -318,7 +380,7 @@ static int fc_host_setup(struct transport_container *tc, struct device *dev,
        struct Scsi_Host *shost = dev_to_shost(dev);
        struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
 
-       /* 
+       /*
         * Set default values easily detected by the midlayer as
         * failure cases.  The scsi lldd is responsible for initializing
         * all transport attributes to valid values per host.
@@ -331,6 +393,7 @@ static int fc_host_setup(struct transport_container *tc, struct device *dev,
                sizeof(fc_host->supported_fc4s));
        fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
        fc_host->maxframe_size = -1;
+       fc_host->max_npiv_vports = 0;
        memset(fc_host->serial_number, 0,
                sizeof(fc_host->serial_number));
 
@@ -348,8 +411,11 @@ static int fc_host_setup(struct transport_container *tc, struct device *dev,
 
        INIT_LIST_HEAD(&fc_host->rports);
        INIT_LIST_HEAD(&fc_host->rport_bindings);
+       INIT_LIST_HEAD(&fc_host->vports);
        fc_host->next_rport_number = 0;
        fc_host->next_target_id = 0;
+       fc_host->next_vport_number = 0;
+       fc_host->npiv_vports_inuse = 0;
 
        snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
                shost->host_no);
@@ -387,6 +453,16 @@ static DECLARE_TRANSPORT_CLASS(fc_rport_class,
                               NULL,
                               NULL);
 
+/*
+ * Setup and Remove actions for virtual ports are handled
+ * in the service functions below.
+ */
+static DECLARE_TRANSPORT_CLASS(fc_vport_class,
+                              "fc_vports",
+                              NULL,
+                              NULL,
+                              NULL);
+
 /*
  * Module Parameters
  */
@@ -398,16 +474,16 @@ static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  */
 static unsigned int fc_dev_loss_tmo = 60;              /* seconds */
 
-module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
+module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(dev_loss_tmo,
                 "Maximum number of seconds that the FC transport should"
                 " insulate the loss of a remote port. Once this value is"
                 " exceeded, the scsi target is removed. Value should be"
                 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
 
-/**
+/*
  * Netlink Infrastructure
- **/
+ */
 
 static atomic_t fc_event_seq;
 
@@ -415,10 +491,10 @@ static atomic_t fc_event_seq;
  * fc_get_event_number - Obtain the next sequential FC event number
  *
  * Notes:
- *   We could have inline'd this, but it would have required fc_event_seq to
+ *   We could have inlined this, but it would have required fc_event_seq to
  *   be exposed. For now, live with the subroutine call.
  *   Atomic used to avoid lock/unlock...
- **/
+ */
 u32
 fc_get_event_number(void)
 {
@@ -429,7 +505,6 @@ EXPORT_SYMBOL(fc_get_event_number);
 
 /**
  * fc_host_post_event - called to post an even on an fc_host.
- *
  * @shost:             host the event occurred on
  * @event_number:      fc event number obtained from get_fc_event_number()
  * @event_code:                fc_host event being posted
@@ -437,7 +512,7 @@ EXPORT_SYMBOL(fc_get_event_number);
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 void
 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
                enum fc_host_event_code event_code, u32 event_data)
@@ -503,17 +578,16 @@ EXPORT_SYMBOL(fc_host_post_event);
 
 
 /**
- * fc_host_post_vendor_event - called to post a vendor unique event on
- *                             a fc_host
- *
+ * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
  * @shost:             host the event occurred on
  * @event_number:      fc event number obtained from get_fc_event_number()
  * @data_len:          amount, in bytes, of vendor unique data
  * @data_buf:          pointer to vendor unique data
+ * @vendor_id:          Vendor id
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 void
 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
                u32 data_len, char * data_buf, u64 vendor_id)
@@ -583,6 +657,9 @@ static __init int fc_transport_init(void)
        atomic_set(&fc_event_seq, 0);
 
        error = transport_class_register(&fc_host_class);
+       if (error)
+               return error;
+       error = transport_class_register(&fc_vport_class);
        if (error)
                return error;
        error = transport_class_register(&fc_rport_class);
@@ -596,6 +673,7 @@ static void __exit fc_transport_exit(void)
        transport_class_unregister(&fc_transport_class);
        transport_class_unregister(&fc_rport_class);
        transport_class_unregister(&fc_host_class);
+       transport_class_unregister(&fc_vport_class);
 }
 
 /*
@@ -800,9 +878,9 @@ show_fc_rport_roles (struct class_device *cdev, char *buf)
                        return snprintf(buf, 30, "Unknown Fabric Entity\n");
                }
        } else {
-               if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
+               if (rport->roles == FC_PORT_ROLE_UNKNOWN)
                        return snprintf(buf, 20, "unknown\n");
-               return get_fc_remote_port_roles_names(rport->roles, buf);
+               return get_fc_port_roles_names(rport->roles, buf);
        }
 }
 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
@@ -857,7 +935,7 @@ static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
 
 /*
  * Note: in the target show function we recognize when the remote
- *  port is in the hierarchy and do not allow the driver to get
+ *  port is in the heirarchy and do not allow the driver to get
  *  involved in sysfs functions. The driver only gets involved if
  *  it's the "old" style that doesn't use rports.
  */
@@ -911,6 +989,257 @@ fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
 
 
+/*
+ * FC Virtual Port Attribute Management
+ */
+
+#define fc_vport_show_function(field, format_string, sz, cast)         \
+static ssize_t                                                         \
+show_fc_vport_##field (struct class_device *cdev, char *buf)           \
+{                                                                      \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       struct Scsi_Host *shost = vport_to_shost(vport);                \
+       struct fc_internal *i = to_fc_internal(shost->transportt);      \
+       if ((i->f->get_vport_##field) &&                                \
+           !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))       \
+               i->f->get_vport_##field(vport);                         \
+       return snprintf(buf, sz, format_string, cast vport->field);     \
+}
+
+#define fc_vport_store_function(field)                                 \
+static ssize_t                                                         \
+store_fc_vport_##field(struct class_device *cdev, const char *buf,     \
+                          size_t count)                                \
+{                                                                      \
+       int val;                                                        \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       struct Scsi_Host *shost = vport_to_shost(vport);                \
+       struct fc_internal *i = to_fc_internal(shost->transportt);      \
+       char *cp;                                                       \
+       if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))  \
+               return -EBUSY;                                          \
+       val = simple_strtoul(buf, &cp, 0);                              \
+       if (*cp && (*cp != '\n'))                                       \
+               return -EINVAL;                                         \
+       i->f->set_vport_##field(vport, val);                            \
+       return count;                                                   \
+}
+
+#define fc_vport_store_str_function(field, slen)                       \
+static ssize_t                                                         \
+store_fc_vport_##field(struct class_device *cdev, const char *buf,     \
+                          size_t count)                                \
+{                                                                      \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       struct Scsi_Host *shost = vport_to_shost(vport);                \
+       struct fc_internal *i = to_fc_internal(shost->transportt);      \
+       unsigned int cnt=count;                                         \
+                                                                       \
+       /* count may include a LF at end of string */                   \
+       if (buf[cnt-1] == '\n')                                         \
+               cnt--;                                                  \
+       if (cnt > ((slen) - 1))                                         \
+               return -EINVAL;                                         \
+       memcpy(vport->field, buf, cnt);                                 \
+       i->f->set_vport_##field(vport);                                 \
+       return count;                                                   \
+}
+
+#define fc_vport_rd_attr(field, format_string, sz)                     \
+       fc_vport_show_function(field, format_string, sz, )              \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO,                     \
+                        show_fc_vport_##field, NULL)
+
+#define fc_vport_rd_attr_cast(field, format_string, sz, cast)          \
+       fc_vport_show_function(field, format_string, sz, (cast))        \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO,                     \
+                         show_fc_vport_##field, NULL)
+
+#define fc_vport_rw_attr(field, format_string, sz)                     \
+       fc_vport_show_function(field, format_string, sz, )              \
+       fc_vport_store_function(field)                                  \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,           \
+                       show_fc_vport_##field,                          \
+                       store_fc_vport_##field)
+
+#define fc_private_vport_show_function(field, format_string, sz, cast) \
+static ssize_t                                                         \
+show_fc_vport_##field (struct class_device *cdev, char *buf)           \
+{                                                                      \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       return snprintf(buf, sz, format_string, cast vport->field);     \
+}
+
+#define fc_private_vport_store_u32_function(field)                     \
+static ssize_t                                                         \
+store_fc_vport_##field(struct class_device *cdev, const char *buf,     \
+                          size_t count)                                \
+{                                                                      \
+       u32 val;                                                        \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       char *cp;                                                       \
+       if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))          \
+               return -EBUSY;                                          \
+       val = simple_strtoul(buf, &cp, 0);                              \
+       if (*cp && (*cp != '\n'))                                       \
+               return -EINVAL;                                         \
+       vport->field = val;                                             \
+       return count;                                                   \
+}
+
+
+#define fc_private_vport_rd_attr(field, format_string, sz)             \
+       fc_private_vport_show_function(field, format_string, sz, )      \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO,                     \
+                        show_fc_vport_##field, NULL)
+
+#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)  \
+       fc_private_vport_show_function(field, format_string, sz, (cast)) \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO,                     \
+                         show_fc_vport_##field, NULL)
+
+#define fc_private_vport_rw_u32_attr(field, format_string, sz)         \
+       fc_private_vport_show_function(field, format_string, sz, )      \
+       fc_private_vport_store_u32_function(field)                      \
+static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,           \
+                       show_fc_vport_##field,                          \
+                       store_fc_vport_##field)
+
+
+#define fc_private_vport_rd_enum_attr(title, maxlen)                   \
+static ssize_t                                                         \
+show_fc_vport_##title (struct class_device *cdev, char *buf)           \
+{                                                                      \
+       struct fc_vport *vport = transport_class_to_vport(cdev);        \
+       const char *name;                                               \
+       name = get_fc_##title##_name(vport->title);                     \
+       if (!name)                                                      \
+               return -EINVAL;                                         \
+       return snprintf(buf, maxlen, "%s\n", name);                     \
+}                                                                      \
+static FC_CLASS_DEVICE_ATTR(vport, title, S_IRUGO,                     \
+                       show_fc_vport_##title, NULL)
+
+
+#define SETUP_VPORT_ATTRIBUTE_RD(field)                                        \
+       i->private_vport_attrs[count] = class_device_attr_vport_##field; \
+       i->private_vport_attrs[count].attr.mode = S_IRUGO;              \
+       i->private_vport_attrs[count].store = NULL;                     \
+       i->vport_attrs[count] = &i->private_vport_attrs[count];         \
+       if (i->f->get_##field)                                          \
+               count++
+       /* NOTE: Above MACRO differs: checks function not show bit */
+
+#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)                                \
+       i->private_vport_attrs[count] = class_device_attr_vport_##field; \
+       i->private_vport_attrs[count].attr.mode = S_IRUGO;              \
+       i->private_vport_attrs[count].store = NULL;                     \
+       i->vport_attrs[count] = &i->private_vport_attrs[count];         \
+       count++
+
+#define SETUP_VPORT_ATTRIBUTE_WR(field)                                        \
+       i->private_vport_attrs[count] = class_device_attr_vport_##field; \
+       i->vport_attrs[count] = &i->private_vport_attrs[count];         \
+       if (i->f->field)                                                \
+               count++
+       /* NOTE: Above MACRO differs: checks function */
+
+#define SETUP_VPORT_ATTRIBUTE_RW(field)                                        \
+       i->private_vport_attrs[count] = class_device_attr_vport_##field; \
+       if (!i->f->set_vport_##field) {                                 \
+               i->private_vport_attrs[count].attr.mode = S_IRUGO;      \
+               i->private_vport_attrs[count].store = NULL;             \
+       }                                                               \
+       i->vport_attrs[count] = &i->private_vport_attrs[count];         \
+       count++
+       /* NOTE: Above MACRO differs: does not check show bit */
+
+#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)                                \
+{                                                                      \
+       i->private_vport_attrs[count] = class_device_attr_vport_##field; \
+       i->vport_attrs[count] = &i->private_vport_attrs[count];         \
+       count++;                                                        \
+}
+
+
+/* The FC Transport Virtual Port Attributes: */
+
+/* Fixed Virtual Port Attributes */
+
+/* Dynamic Virtual Port Attributes */
+
+/* Private Virtual Port Attributes */
+
+fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
+fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
+fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
+fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
+
+static ssize_t
+show_fc_vport_roles (struct class_device *cdev, char *buf)
+{
+       struct fc_vport *vport = transport_class_to_vport(cdev);
+
+       if (vport->roles == FC_PORT_ROLE_UNKNOWN)
+               return snprintf(buf, 20, "unknown\n");
+       return get_fc_port_roles_names(vport->roles, buf);
+}
+static FC_CLASS_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
+
+fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
+
+fc_private_vport_show_function(symbolic_name, "%s\n",
+               FC_VPORT_SYMBOLIC_NAMELEN + 1, )
+fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
+static FC_CLASS_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
+               show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
+
+static ssize_t
+store_fc_vport_delete(struct class_device *cdev, const char *buf,
+                          size_t count)
+{
+       struct fc_vport *vport = transport_class_to_vport(cdev);
+       struct Scsi_Host *shost = vport_to_shost(vport);
+
+       fc_queue_work(shost, &vport->vport_delete_work);
+       return count;
+}
+static FC_CLASS_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
+                       NULL, store_fc_vport_delete);
+
+
+/*
+ * Enable/Disable vport
+ *  Write "1" to disable, write "0" to enable
+ */
+static ssize_t
+store_fc_vport_disable(struct class_device *cdev, const char *buf,
+                          size_t count)
+{
+       struct fc_vport *vport = transport_class_to_vport(cdev);
+       struct Scsi_Host *shost = vport_to_shost(vport);
+       struct fc_internal *i = to_fc_internal(shost->transportt);
+       int stat;
+
+       if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
+               return -EBUSY;
+
+       if (*buf == '0') {
+               if (vport->vport_state != FC_VPORT_DISABLED)
+                       return -EALREADY;
+       } else if (*buf == '1') {
+               if (vport->vport_state == FC_VPORT_DISABLED)
+                       return -EALREADY;
+       } else
+               return -EINVAL;
+
+       stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
+       return stat ? stat : count;
+}
+static FC_CLASS_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
+                       NULL, store_fc_vport_disable);
+
+
 /*
  * Host Attribute Management
  */
@@ -1003,6 +1332,13 @@ static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
        if (i->f->show_host_##field)                                    \
                count++
 
+#define SETUP_HOST_ATTRIBUTE_RD_NS(field)                              \
+       i->private_host_attrs[count] = class_device_attr_host_##field;  \
+       i->private_host_attrs[count].attr.mode = S_IRUGO;               \
+       i->private_host_attrs[count].store = NULL;                      \
+       i->host_attrs[count] = &i->private_host_attrs[count];           \
+       count++
+
 #define SETUP_HOST_ATTRIBUTE_RW(field)                                 \
        i->private_host_attrs[count] = class_device_attr_host_##field;  \
        if (!i->f->set_host_##field) {                                  \
@@ -1090,6 +1426,7 @@ fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
                             unsigned long long);
 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
+fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
 
 
@@ -1210,6 +1547,9 @@ store_fc_private_host_issue_lip(struct class_device *cdev,
 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
                        store_fc_private_host_issue_lip);
 
+fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
+
+
 /*
  * Host Statistics Management
  */
@@ -1285,7 +1625,6 @@ fc_reset_statistics(struct class_device *cdev, const char *buf,
 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
                                fc_reset_statistics);
 
-
 static struct attribute *fc_statistics_attrs[] = {
        &class_device_attr_host_seconds_since_last_reset.attr,
        &class_device_attr_host_tx_frames.attr,
@@ -1316,6 +1655,142 @@ static struct attribute_group fc_statistics_group = {
        .attrs = fc_statistics_attrs,
 };
 
+
+/* Host Vport Attributes */
+
+static int
+fc_parse_wwn(const char *ns, u64 *nm)
+{
+       unsigned int i, j;
+       u8 wwn[8];
+
+       memset(wwn, 0, sizeof(wwn));
+
+       /* Validate and store the new name */
+       for (i=0, j=0; i < 16; i++) {
+               if ((*ns >= 'a') && (*ns <= 'f'))
+                       j = ((j << 4) | ((*ns++ -'a') + 10));
+               else if ((*ns >= 'A') && (*ns <= 'F'))
+                       j = ((j << 4) | ((*ns++ -'A') + 10));
+               else if ((*ns >= '0') && (*ns <= '9'))
+                       j = ((j << 4) | (*ns++ -'0'));
+               else
+                       return -EINVAL;
+               if (i % 2) {
+                       wwn[i/2] = j & 0xff;
+                       j = 0;
+               }
+       }
+
+       *nm = wwn_to_u64(wwn);
+
+       return 0;
+}
+
+
+/*
+ * "Short-cut" sysfs variable to create a new vport on a FC Host.
+ * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
+ * will default to a NPIV-based FCP_Initiator; The WWNs are specified
+ * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
+ */
+static ssize_t
+store_fc_host_vport_create(struct class_device *cdev, const char *buf,
+                          size_t count)
+{
+       struct Scsi_Host *shost = transport_class_to_shost(cdev);
+       struct fc_vport_identifiers vid;
+       struct fc_vport *vport;
+       unsigned int cnt=count;
+       int stat;
+
+       memset(&vid, 0, sizeof(vid));
+
+       /* count may include a LF at end of string */
+       if (buf[cnt-1] == '\n')
+               cnt--;
+
+       /* validate we have enough characters for WWPN */
+       if ((cnt != (16+1+16)) || (buf[16] != ':'))
+               return -EINVAL;
+
+       stat = fc_parse_wwn(&buf[0], &vid.port_name);
+       if (stat)
+               return stat;
+
+       stat = fc_parse_wwn(&buf[17], &vid.node_name);
+       if (stat)
+               return stat;
+
+       vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
+       vid.vport_type = FC_PORTTYPE_NPIV;
+       /* vid.symbolic_name is already zero/NULL's */
+       vid.disable = false;            /* always enabled */
+
+       /* we only allow support on Channel 0 !!! */
+       stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
+       return stat ? stat : count;
+}
+static FC_CLASS_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
+                       store_fc_host_vport_create);
+
+
+/*
+ * "Short-cut" sysfs variable to delete a vport on a FC Host.
+ * Vport is identified by a string containing "<WWPN>:<WWNN>".
+ * The WWNs are specified as hex characters, and may *not* contain
+ * any prefixes (e.g. 0x, x, etc)
+ */
+static ssize_t
+store_fc_host_vport_delete(struct class_device *cdev, const char *buf,
+                          size_t count)
+{
+       struct Scsi_Host *shost = transport_class_to_shost(cdev);
+       struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
+       struct fc_vport *vport;
+       u64 wwpn, wwnn;
+       unsigned long flags;
+       unsigned int cnt=count;
+       int stat, match;
+
+       /* count may include a LF at end of string */
+       if (buf[cnt-1] == '\n')
+               cnt--;
+
+       /* validate we have enough characters for WWPN */
+       if ((cnt != (16+1+16)) || (buf[16] != ':'))
+               return -EINVAL;
+
+       stat = fc_parse_wwn(&buf[0], &wwpn);
+       if (stat)
+               return stat;
+
+       stat = fc_parse_wwn(&buf[17], &wwnn);
+       if (stat)
+               return stat;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+       match = 0;
+       /* we only allow support on Channel 0 !!! */
+       list_for_each_entry(vport, &fc_host->vports, peers) {
+               if ((vport->channel == 0) &&
+                   (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
+                       match = 1;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       if (!match)
+               return -ENODEV;
+
+       stat = fc_vport_terminate(vport);
+       return stat ? stat : count;
+}
+static FC_CLASS_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
+                       store_fc_host_vport_delete);
+
+
 static int fc_host_match(struct attribute_container *cont,
                          struct device *dev)
 {
@@ -1387,9 +1862,42 @@ static int fc_rport_match(struct attribute_container *cont,
 }
 
 
+static void fc_vport_dev_release(struct device *dev)
+{
+       struct fc_vport *vport = dev_to_vport(dev);
+       put_device(dev->parent);                /* release kobj parent */
+       kfree(vport);
+}
+
+int scsi_is_fc_vport(const struct device *dev)
+{
+       return dev->release == fc_vport_dev_release;
+}
+EXPORT_SYMBOL(scsi_is_fc_vport);
+
+static int fc_vport_match(struct attribute_container *cont,
+                           struct device *dev)
+{
+       struct fc_vport *vport;
+       struct Scsi_Host *shost;
+       struct fc_internal *i;
+
+       if (!scsi_is_fc_vport(dev))
+               return 0;
+       vport = dev_to_vport(dev);
+
+       shost = vport_to_shost(vport);
+       if (!shost->transportt  || shost->transportt->host_attrs.ac.class
+           != &fc_host_class.class)
+               return 0;
+
+       i = to_fc_internal(shost->transportt);
+       return &i->vport_attr_cont.ac == cont;
+}
+
+
 /**
  * fc_timed_out - FC Transport I/O timeout intercept handler
- *
  * @scmd:      The SCSI command which timed out
  *
  * This routine protects against error handlers getting invoked while a
@@ -1409,7 +1917,7 @@ static int fc_rport_match(struct attribute_container *cont,
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 static enum scsi_eh_timer_return
 fc_timed_out(struct scsi_cmnd *scmd)
 {
@@ -1433,6 +1941,9 @@ static int fc_user_scan(struct Scsi_Host *shost, uint channel,
                if (rport->scsi_target_id == -1)
                        continue;
 
+               if (rport->port_state != FC_PORTSTATE_ONLINE)
+                       continue;
+
                if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
                    (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
                        scsi_scan_target(&rport->dev, rport->channel,
@@ -1443,6 +1954,19 @@ static int fc_user_scan(struct Scsi_Host *shost, uint channel,
        return 0;
 }
 
+static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
+                               int result)
+{
+       struct fc_internal *i = to_fc_internal(shost->transportt);
+       return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
+}
+
+static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
+{
+       struct fc_internal *i = to_fc_internal(shost->transportt);
+       return i->f->it_nexus_response(shost, nexus, result);
+}
+
 struct scsi_transport_template *
 fc_attach_transport(struct fc_function_template *ft)
 {
@@ -1472,6 +1996,11 @@ fc_attach_transport(struct fc_function_template *ft)
        i->rport_attr_cont.ac.match = fc_rport_match;
        transport_container_register(&i->rport_attr_cont);
 
+       i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
+       i->vport_attr_cont.ac.class = &fc_vport_class.class;
+       i->vport_attr_cont.ac.match = fc_vport_match;
+       transport_container_register(&i->vport_attr_cont);
+
        i->f = ft;
 
        /* Transport uses the shost workq for scsi scanning */
@@ -1480,7 +2009,11 @@ fc_attach_transport(struct fc_function_template *ft)
        i->t.eh_timed_out = fc_timed_out;
 
        i->t.user_scan = fc_user_scan;
-       
+
+       /* target-mode drivers' functions */
+       i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
+       i->t.it_nexus_response = fc_it_nexus_response;
+
        /*
         * Setup SCSI Target Attributes.
         */
@@ -1505,6 +2038,10 @@ fc_attach_transport(struct fc_function_template *ft)
        SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
        SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
        SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
+       if (ft->vport_create) {
+               SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
+               SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
+       }
        SETUP_HOST_ATTRIBUTE_RD(serial_number);
 
        SETUP_HOST_ATTRIBUTE_RD(port_id);
@@ -1520,6 +2057,10 @@ fc_attach_transport(struct fc_function_template *ft)
        SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
        if (ft->issue_fc_host_lip)
                SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
+       if (ft->vport_create)
+               SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
+       if (ft->vport_delete)
+               SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
 
        BUG_ON(count > FC_HOST_NUM_ATTRS);
 
@@ -1545,6 +2086,24 @@ fc_attach_transport(struct fc_function_template *ft)
 
        i->rport_attrs[count] = NULL;
 
+       /*
+        * Setup Virtual Port Attributes.
+        */
+       count=0;
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
+       SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
+       SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
+       SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
+       SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
+
+       BUG_ON(count > FC_VPORT_NUM_ATTRS);
+
+       i->vport_attrs[count] = NULL;
+
        return &i->t;
 }
 EXPORT_SYMBOL(fc_attach_transport);
@@ -1556,6 +2115,7 @@ void fc_release_transport(struct scsi_transport_template *t)
        transport_container_unregister(&i->t.target_attrs);
        transport_container_unregister(&i->t.host_attrs);
        transport_container_unregister(&i->rport_attr_cont);
+       transport_container_unregister(&i->vport_attr_cont);
 
        kfree(i);
 }
@@ -1570,7 +2130,7 @@ EXPORT_SYMBOL(fc_release_transport);
  *     1 - work queued for execution
  *     0 - work is already queued
  *     -EINVAL - work queue doesn't exist
- **/
+ */
 static int
 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
 {
@@ -1589,7 +2149,7 @@ fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
 /**
  * fc_flush_work - Flush a fc_host's workqueue.
  * @shost:     Pointer to Scsi_Host bound to fc_host.
- **/
+ */
 static void
 fc_flush_work(struct Scsi_Host *shost)
 {
@@ -1612,7 +2172,7 @@ fc_flush_work(struct Scsi_Host *shost)
  *
  * Return value:
  *     1 on success / 0 already queued / < 0 for error
- **/
+ */
 static int
 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
                                unsigned long delay)
@@ -1632,7 +2192,7 @@ fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
 /**
  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
  * @shost:     Pointer to Scsi_Host bound to fc_host.
- **/
+ */
 static void
 fc_flush_devloss(struct Scsi_Host *shost)
 {
@@ -1649,27 +2209,34 @@ fc_flush_devloss(struct Scsi_Host *shost)
 
 
 /**
- * fc_remove_host - called to terminate any fc_transport-related elements
- *                  for a scsi host.
- * @rport:     remote port to be unblocked.
+ * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
+ * @shost:     Which &Scsi_Host
  *
  * This routine is expected to be called immediately preceeding the
  * a driver's call to scsi_remove_host().
  *
  * WARNING: A driver utilizing the fc_transport, which fails to call
- *   this routine prior to scsi_remote_host(), will leave dangling
+ *   this routine prior to scsi_remove_host(), will leave dangling
  *   objects in /sys/class/fc_remote_ports. Access to any of these
  *   objects can result in a system crash !!!
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 void
 fc_remove_host(struct Scsi_Host *shost)
 {
-       struct fc_rport *rport, *next_rport;
+       struct fc_vport *vport = NULL, *next_vport = NULL;
+       struct fc_rport *rport = NULL, *next_rport = NULL;
        struct workqueue_struct *work_q;
        struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
+       unsigned long flags;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+
+       /* Remove any vports */
+       list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
+               fc_queue_work(shost, &vport->vport_delete_work);
 
        /* Remove any remote ports */
        list_for_each_entry_safe(rport, next_rport,
@@ -1686,6 +2253,8 @@ fc_remove_host(struct Scsi_Host *shost)
                fc_queue_work(shost, &rport->rport_delete_work);
        }
 
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
        /* flush all scan work items */
        scsi_flush_work(shost);
 
@@ -1708,10 +2277,10 @@ EXPORT_SYMBOL(fc_remove_host);
 
 /**
  * fc_starget_delete - called to delete the scsi decendents of an rport
- *                  (target and all sdevs)
- *
  * @work:      remote port to be operated on.
- **/
+ *
+ * Deletes target and all sdevs.
+ */
 static void
 fc_starget_delete(struct work_struct *work)
 {
@@ -1730,9 +2299,8 @@ fc_starget_delete(struct work_struct *work)
 
 /**
  * fc_rport_final_delete - finish rport termination and delete it.
- *
  * @work:      remote port to be deleted.
- **/
+ */
 static void
 fc_rport_final_delete(struct work_struct *work)
 {
@@ -1744,7 +2312,7 @@ fc_rport_final_delete(struct work_struct *work)
        unsigned long flags;
 
        /*
-        * if a scan is pending, flush the SCSI Host work_q so that 
+        * if a scan is pending, flush the SCSI Host work_q so that
         * that we can reclaim the rport scan work element.
         */
        if (rport->flags & FC_RPORT_SCAN_PENDING)
@@ -1802,8 +2370,8 @@ fc_rport_final_delete(struct work_struct *work)
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
-struct fc_rport *
+ */
+static struct fc_rport *
 fc_rport_create(struct Scsi_Host *shost, int channel,
        struct fc_rport_identifiers  *ids)
 {
@@ -1844,7 +2412,7 @@ fc_rport_create(struct Scsi_Host *shost, int channel,
        spin_lock_irqsave(shost->host_lock, flags);
 
        rport->number = fc_host->next_rport_number++;
-       if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
+       if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
                rport->scsi_target_id = fc_host->next_target_id++;
        else
                rport->scsi_target_id = -1;
@@ -1869,7 +2437,7 @@ fc_rport_create(struct Scsi_Host *shost, int channel,
        transport_add_device(dev);
        transport_configure_device(dev);
 
-       if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
+       if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
                /* initiate a scan of the target */
                rport->flags |= FC_RPORT_SCAN_PENDING;
                scsi_queue_work(shost, &rport->scan_work);
@@ -1889,8 +2457,7 @@ delete_rport:
 }
 
 /**
- * fc_remote_port_add - notifies the fc transport of the existence
- *             of a remote FC port.
+ * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
  * @shost:     scsi host the remote port is connected to.
  * @channel:   Channel on shost port connected to.
  * @ids:       The world wide names, fc address, and FC4 port
@@ -1926,7 +2493,7 @@ delete_rport:
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 struct fc_rport *
 fc_remote_port_add(struct Scsi_Host *shost, int channel,
        struct fc_rport_identifiers  *ids)
@@ -2003,7 +2570,7 @@ fc_remote_port_add(struct Scsi_Host *shost, int channel,
 
                                /* was a target, not in roles */
                                if ((rport->scsi_target_id != -1) &&
-                                   (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET)))
+                                   (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
                                        return rport;
 
                                /*
@@ -2086,7 +2653,7 @@ fc_remote_port_add(struct Scsi_Host *shost, int channel,
                                memset(rport->dd_data, 0,
                                                fci->f->dd_fcrport_size);
 
-                       if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
+                       if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
                                /* initiate a scan of the target */
                                rport->flags |= FC_RPORT_SCAN_PENDING;
                                scsi_queue_work(shost, &rport->scan_work);
@@ -2110,19 +2677,18 @@ EXPORT_SYMBOL(fc_remote_port_add);
 
 
 /**
- * fc_remote_port_delete - notifies the fc transport that a remote
- *             port is no longer in existence.
+ * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
  * @rport:     The remote port that no longer exists
  *
  * The LLDD calls this routine to notify the transport that a remote
  * port is no longer part of the topology. Note: Although a port
  * may no longer be part of the topology, it may persist in the remote
  * ports displayed by the fc_host. We do this under 2 conditions:
- * - If the port was a scsi target, we delay its deletion by "blocking" it.
+ * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
  *   This allows the port to temporarily disappear, then reappear without
  *   disrupting the SCSI device tree attached to it. During the "blocked"
  *   period the port will still exist.
- * - If the port was a scsi target and disappears for longer than we
+ * 2) If the port was a scsi target and disappears for longer than we
  *   expect, we'll delete the port and the tear down the SCSI device tree
  *   attached to it. However, we want to semi-persist the target id assigned
  *   to that port if it eventually does exist. The port structure will
@@ -2136,7 +2702,8 @@ EXPORT_SYMBOL(fc_remote_port_add);
  * temporary blocked state. From the LLDD's perspective, the rport no
  * longer exists. From the SCSI midlayer's perspective, the SCSI target
  * exists, but all sdevs on it are blocked from further I/O. The following
- * is then expected:
+ * is then expected.
+ *
  *   If the remote port does not return (signaled by a LLDD call to
  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  *   scsi target is removed - killing all outstanding i/o and removing the
@@ -2158,7 +2725,7 @@ EXPORT_SYMBOL(fc_remote_port_add);
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 void
 fc_remote_port_delete(struct fc_rport  *rport)
 {
@@ -2201,6 +2768,10 @@ fc_remote_port_delete(struct fc_rport  *rport)
 
        spin_unlock_irqrestore(shost->host_lock, flags);
 
+       if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
+           shost->active_mode & MODE_TARGET)
+               fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
+
        scsi_target_block(&rport->dev);
 
        /* see if we need to kill io faster than waiting for device loss */
@@ -2215,12 +2786,12 @@ fc_remote_port_delete(struct fc_rport  *rport)
 EXPORT_SYMBOL(fc_remote_port_delete);
 
 /**
- * fc_remote_port_rolechg - notifies the fc transport that the roles
- *             on a remote may have changed.
+ * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
  * @rport:     The remote port that changed.
+ * @roles:      New roles for this port.
  *
- * The LLDD calls this routine to notify the transport that the roles
- * on a remote port may have changed. The largest effect of this is
+ * Description: The LLDD calls this routine to notify the transport that the
+ * roles on a remote port may have changed. The largest effect of this is
  * if a port now becomes a FCP Target, it must be allocated a
  * scsi target id.  If the port is no longer a FCP target, any
  * scsi target id value assigned to it will persist in case the
@@ -2233,7 +2804,7 @@ EXPORT_SYMBOL(fc_remote_port_delete);
  *
  * Notes:
  *     This routine assumes no locks are held on entry.
- **/
+ */
 void
 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
 {
@@ -2241,14 +2812,21 @@ fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
        struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
        unsigned long flags;
        int create = 0;
+       int ret;
 
        spin_lock_irqsave(shost->host_lock, flags);
-       if (roles & FC_RPORT_ROLE_FCP_TARGET) {
+       if (roles & FC_PORT_ROLE_FCP_TARGET) {
                if (rport->scsi_target_id == -1) {
                        rport->scsi_target_id = fc_host->next_target_id++;
                        create = 1;
-               } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
+               } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
                        create = 1;
+       } else if (shost->active_mode & MODE_TARGET) {
+               ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
+                                            (char *)&rport->node_name);
+               if (ret)
+                       printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
+                              ret);
        }
 
        rport->roles = roles;
@@ -2291,12 +2869,12 @@ fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
 EXPORT_SYMBOL(fc_remote_port_rolechg);
 
 /**
- * fc_timeout_deleted_rport - Timeout handler for a deleted remote port,
- *                     which we blocked, and has now failed to return
- *                     in the allotted time.
- * 
+ * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
  * @work:      rport target that failed to reappear in the allotted time.
- **/
+ *
+ * Description: An attempt to delete a remote port blocks, and if it fails
+ *              to return in the allotted time this gets called.
+ */
 static void
 fc_timeout_deleted_rport(struct work_struct *work)
 {
@@ -2317,7 +2895,7 @@ fc_timeout_deleted_rport(struct work_struct *work)
         */
        if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
            (rport->scsi_target_id != -1) &&
-           !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
+           !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
                dev_printk(KERN_ERR, &rport->dev,
                        "blocked FC remote port time out: no longer"
                        " a FCP target, removing starget\n");
@@ -2367,7 +2945,7 @@ fc_timeout_deleted_rport(struct work_struct *work)
         */
        rport->maxframe_size = -1;
        rport->supported_classes = FC_COS_UNSPECIFIED;
-       rport->roles = FC_RPORT_ROLE_UNKNOWN;
+       rport->roles = FC_PORT_ROLE_UNKNOWN;
        rport->port_state = FC_PORTSTATE_NOTPRESENT;
 
        /* remove the identifiers that aren't used in the consisting binding */
@@ -2400,14 +2978,12 @@ fc_timeout_deleted_rport(struct work_struct *work)
 }
 
 /**
- * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
- *                       disconnected SCSI target.
- *
+ * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
  * @work:      rport to terminate io on.
  *
  * Notes: Only requests the failure of the io, not that all are flushed
  *    prior to returning.
- **/
+ */
 static void
 fc_timeout_fail_rport_io(struct work_struct *work)
 {
@@ -2424,19 +3000,20 @@ fc_timeout_fail_rport_io(struct work_struct *work)
 
 /**
  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
- *
  * @work:      remote port to be scanned.
- **/
+ */
 static void
 fc_scsi_scan_rport(struct work_struct *work)
 {
        struct fc_rport *rport =
                container_of(work, struct fc_rport, scan_work);
        struct Scsi_Host *shost = rport_to_shost(rport);
+       struct fc_internal *i = to_fc_internal(shost->transportt);
        unsigned long flags;
 
        if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
-           (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
+           (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
+           !(i->f->disable_target_scan)) {
                scsi_scan_target(&rport->dev, rport->channel,
                        rport->scsi_target_id, SCAN_WILD_CARD, 1);
        }
@@ -2447,7 +3024,226 @@ fc_scsi_scan_rport(struct work_struct *work)
 }
 
 
-MODULE_AUTHOR("Martin Hicks");
+/**
+ * fc_vport_create - allocates and creates a FC virtual port.
+ * @shost:     scsi host the virtual port is connected to.
+ * @channel:   Channel on shost port connected to.
+ * @pdev:      parent device for vport
+ * @ids:       The world wide names, FC4 port roles, etc for
+ *              the virtual port.
+ * @ret_vport: The pointer to the created vport.
+ *
+ * Allocates and creates the vport structure, calls the parent host
+ * to instantiate the vport, the completes w/ class and sysfs creation.
+ *
+ * Notes:
+ *     This routine assumes no locks are held on entry.
+ */
+static int
+fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
+       struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
+{
+       struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
+       struct fc_internal *fci = to_fc_internal(shost->transportt);
+       struct fc_vport *vport;
+       struct device *dev;
+       unsigned long flags;
+       size_t size;
+       int error;
+
+       *ret_vport = NULL;
+
+       if ( ! fci->f->vport_create)
+               return -ENOENT;
+
+       size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
+       vport = kzalloc(size, GFP_KERNEL);
+       if (unlikely(!vport)) {
+               printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
+               return -ENOMEM;
+       }
+
+       vport->vport_state = FC_VPORT_UNKNOWN;
+       vport->vport_last_state = FC_VPORT_UNKNOWN;
+       vport->node_name = ids->node_name;
+       vport->port_name = ids->port_name;
+       vport->roles = ids->roles;
+       vport->vport_type = ids->vport_type;
+       if (fci->f->dd_fcvport_size)
+               vport->dd_data = &vport[1];
+       vport->shost = shost;
+       vport->channel = channel;
+       vport->flags = FC_VPORT_CREATING;
+       INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
+
+       spin_lock_irqsave(shost->host_lock, flags);
+
+       if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
+               spin_unlock_irqrestore(shost->host_lock, flags);
+               kfree(vport);
+               return -ENOSPC;
+       }
+       fc_host->npiv_vports_inuse++;
+       vport->number = fc_host->next_vport_number++;
+       list_add_tail(&vport->peers, &fc_host->vports);
+       get_device(&shost->shost_gendev);       /* for fc_host->vport list */
+
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       dev = &vport->dev;
+       device_initialize(dev);                 /* takes self reference */
+       dev->parent = get_device(pdev);         /* takes parent reference */
+       dev->release = fc_vport_dev_release;
+       sprintf(dev->bus_id, "vport-%d:%d-%d",
+               shost->host_no, channel, vport->number);
+       transport_setup_device(dev);
+
+       error = device_add(dev);
+       if (error) {
+               printk(KERN_ERR "FC Virtual Port device_add failed\n");
+               goto delete_vport;
+       }
+       transport_add_device(dev);
+       transport_configure_device(dev);
+
+       error = fci->f->vport_create(vport, ids->disable);
+       if (error) {
+               printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
+               goto delete_vport_all;
+       }
+
+       /*
+        * if the parent isn't the physical adapter's Scsi_Host, ensure
+        * the Scsi_Host at least contains ia symlink to the vport.
+        */
+       if (pdev != &shost->shost_gendev) {
+               error = sysfs_create_link(&shost->shost_gendev.kobj,
+                                &dev->kobj, dev->bus_id);
+               if (error)
+                       printk(KERN_ERR
+                               "%s: Cannot create vport symlinks for "
+                               "%s, err=%d\n",
+                               __FUNCTION__, dev->bus_id, error);
+       }
+       spin_lock_irqsave(shost->host_lock, flags);
+       vport->flags &= ~FC_VPORT_CREATING;
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       dev_printk(KERN_NOTICE, pdev,
+                       "%s created via shost%d channel %d\n", dev->bus_id,
+                       shost->host_no, channel);
+
+       *ret_vport = vport;
+
+       return 0;
+
+delete_vport_all:
+       transport_remove_device(dev);
+       device_del(dev);
+delete_vport:
+       transport_destroy_device(dev);
+       spin_lock_irqsave(shost->host_lock, flags);
+       list_del(&vport->peers);
+       put_device(&shost->shost_gendev);       /* for fc_host->vport list */
+       fc_host->npiv_vports_inuse--;
+       spin_unlock_irqrestore(shost->host_lock, flags);
+       put_device(dev->parent);
+       kfree(vport);
+
+       return error;
+}
+
+
+/**
+ * fc_vport_terminate - Admin App or LLDD requests termination of a vport
+ * @vport:     fc_vport to be terminated
+ *
+ * Calls the LLDD vport_delete() function, then deallocates and removes
+ * the vport from the shost and object tree.
+ *
+ * Notes:
+ *     This routine assumes no locks are held on entry.
+ */
+int
+fc_vport_terminate(struct fc_vport *vport)
+{
+       struct Scsi_Host *shost = vport_to_shost(vport);
+       struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
+       struct fc_internal *i = to_fc_internal(shost->transportt);
+       struct device *dev = &vport->dev;
+       unsigned long flags;
+       int stat;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+       if (vport->flags & FC_VPORT_CREATING) {
+               spin_unlock_irqrestore(shost->host_lock, flags);
+               return -EBUSY;
+       }
+       if (vport->flags & (FC_VPORT_DEL)) {
+               spin_unlock_irqrestore(shost->host_lock, flags);
+               return -EALREADY;
+       }
+       vport->flags |= FC_VPORT_DELETING;
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       if (i->f->vport_delete)
+               stat = i->f->vport_delete(vport);
+       else
+               stat = -ENOENT;
+
+       spin_lock_irqsave(shost->host_lock, flags);
+       vport->flags &= ~FC_VPORT_DELETING;
+       if (!stat) {
+               vport->flags |= FC_VPORT_DELETED;
+               list_del(&vport->peers);
+               fc_host->npiv_vports_inuse--;
+               put_device(&shost->shost_gendev);  /* for fc_host->vport list */
+       }
+       spin_unlock_irqrestore(shost->host_lock, flags);
+
+       if (stat)
+               return stat;
+
+       if (dev->parent != &shost->shost_gendev)
+               sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
+       transport_remove_device(dev);
+       device_del(dev);
+       transport_destroy_device(dev);
+
+       /*
+        * Removing our self-reference should mean our
+        * release function gets called, which will drop the remaining
+        * parent reference and free the data structure.
+        */
+       put_device(dev);                        /* for self-reference */
+
+       return 0; /* SUCCESS */
+}
+EXPORT_SYMBOL(fc_vport_terminate);
+
+/**
+ * fc_vport_sched_delete - workq-based delete request for a vport
+ * @work:      vport to be deleted.
+ */
+static void
+fc_vport_sched_delete(struct work_struct *work)
+{
+       struct fc_vport *vport =
+               container_of(work, struct fc_vport, vport_delete_work);
+       int stat;
+
+       stat = fc_vport_terminate(vport);
+       if (stat)
+               dev_printk(KERN_ERR, vport->dev.parent,
+                       "%s: %s could not be deleted created via "
+                       "shost%d channel %d - error %d\n", __FUNCTION__,
+                       vport->dev.bus_id, vport->shost->host_no,
+                       vport->channel, stat);
+}
+
+
+/* Original Author:  Martin Hicks */
+MODULE_AUTHOR("James Smart");
 MODULE_DESCRIPTION("FC Transport Attributes");
 MODULE_LICENSE("GPL");