Blame SOURCES/open-iscsi-2.0.874-8-iscsiuio-Add-support-for-the-new-qedi-transport.patch

c487f3
From 264e48a0bda2d6cd5d0607acd2669894ee95b3b5 Mon Sep 17 00:00:00 2001
c487f3
From: Nilesh Javali <nilesh.javali@cavium.com>
c487f3
Date: Fri, 11 Nov 2016 08:17:51 +0200
c487f3
Subject: iscsiuio: Add support for the new qedi transport
c487f3
c487f3
Signed-off-by: Manish Rangankar <manish.rangankar@cavium.com>
c487f3
Signed-off-by: Adheer Chandravanshi <adheer.chandravanshi@qlogic.com>
c487f3
Signed-off-by: Nilesh Javali <nilesh.javali@cavium.com>
c487f3
---
c487f3
 iscsiuio/src/unix/libs/Makefile.am |    3 +-
c487f3
 iscsiuio/src/unix/libs/cnic.c      |    9 +
c487f3
 iscsiuio/src/unix/libs/cnic.h      |    2 +
c487f3
 iscsiuio/src/unix/libs/qedi.c      | 1151 ++++++++++++++++++++++++++++++++++++
c487f3
 iscsiuio/src/unix/libs/qedi.h      |  159 +++++
c487f3
 iscsiuio/src/unix/nic.c            |    6 +-
c487f3
 iscsiuio/src/unix/nic.h            |    1 +
c487f3
 iscsiuio/src/unix/nic_utils.c      |  147 ++++-
c487f3
 iscsiuio/src/unix/nic_utils.h      |    2 +
c487f3
 iscsiuio/src/unix/options.h        |    1 +
c487f3
 10 files changed, 1475 insertions(+), 6 deletions(-)
c487f3
 create mode 100644 iscsiuio/src/unix/libs/qedi.c
c487f3
 create mode 100644 iscsiuio/src/unix/libs/qedi.h
c487f3
c487f3
diff --git a/iscsiuio/src/unix/libs/Makefile.am b/iscsiuio/src/unix/libs/Makefile.am
c487f3
index 890415f5a79a..737546b04917 100644
c487f3
--- a/iscsiuio/src/unix/libs/Makefile.am
c487f3
+++ b/iscsiuio/src/unix/libs/Makefile.am
c487f3
@@ -10,4 +10,5 @@ noinst_LIBRARIES = lib_iscsiuio_hw_cnic.a
c487f3
 lib_iscsiuio_hw_cnic_a_SOURCES =	../build_date.c \
c487f3
 					cnic.c 		\
c487f3
 					bnx2.c		\
c487f3
-					bnx2x.c
c487f3
+					bnx2x.c		\
c487f3
+					qedi.c
c487f3
diff --git a/iscsiuio/src/unix/libs/cnic.c b/iscsiuio/src/unix/libs/cnic.c
c487f3
index 228c4b9e25b1..5d60f898ad57 100644
c487f3
--- a/iscsiuio/src/unix/libs/cnic.c
c487f3
+++ b/iscsiuio/src/unix/libs/cnic.c
c487f3
@@ -55,6 +55,7 @@
c487f3
 #include <sys/types.h>
c487f3
 #include <sys/user.h>
c487f3
 #include <sys/socket.h>
c487f3
+#include <sys/mman.h>
c487f3
 
c487f3
 #include "uip_arp.h"
c487f3
 #include "nic.h"
c487f3
@@ -65,6 +66,7 @@
c487f3
 #include "cnic.h"
c487f3
 #include "iscsi_if.h"
c487f3
 #include "ipv6_ndpc.h"
c487f3
+#include "qedi.h"
c487f3
 
c487f3
 /*******************************************************************************
c487f3
  * Constants
c487f3
@@ -81,6 +83,13 @@ const char bnx2i_library_transport_name[] = "bnx2i";
c487f3
 const size_t bnx2i_library_transport_name_size =
c487f3
 			sizeof(bnx2i_library_transport_name);
c487f3
 
c487f3
+/*******************************************************************************
c487f3
+ * Constants for qedi module
c487f3
+ ******************************************************************************/
c487f3
+const char qedi_library_transport_name[] = "qedi";
c487f3
+const size_t qedi_library_transport_name_size =
c487f3
+			sizeof(qedi_library_transport_name);
c487f3
+
c487f3
 /******************************************************************************
c487f3
  * Netlink Functions
c487f3
  ******************************************************************************/
c487f3
diff --git a/iscsiuio/src/unix/libs/cnic.h b/iscsiuio/src/unix/libs/cnic.h
c487f3
index 6244a94012c1..c86595c512b0 100644
c487f3
--- a/iscsiuio/src/unix/libs/cnic.h
c487f3
+++ b/iscsiuio/src/unix/libs/cnic.h
c487f3
@@ -44,6 +44,8 @@
c487f3
  ******************************************************************************/
c487f3
 extern const char bnx2i_library_transport_name[];
c487f3
 extern const size_t bnx2i_library_transport_name_size;
c487f3
+extern const char qedi_library_transport_name[];
c487f3
+extern const size_t qedi_library_transport_name_size;
c487f3
 
c487f3
 int cnic_nl_open();
c487f3
 void cnic_nl_close();
c487f3
diff --git a/iscsiuio/src/unix/libs/qedi.c b/iscsiuio/src/unix/libs/qedi.c
c487f3
new file mode 100644
c487f3
index 000000000000..c2096e59dad1
c487f3
--- /dev/null
c487f3
+++ b/iscsiuio/src/unix/libs/qedi.c
c487f3
@@ -0,0 +1,1151 @@
c487f3
+/*
c487f3
+ * Copyright (c) 2016, Cavium Inc.
c487f3
+ *
c487f3
+ * All rights reserved.
c487f3
+ *
c487f3
+ * Redistribution and use in source and binary forms, with or without
c487f3
+ * modification, are permitted provided that the following conditions
c487f3
+ * are met:
c487f3
+ * 1. Redistributions of source code must retain the above copyright
c487f3
+ *    notice, this list of conditions and the following disclaimer.
c487f3
+ * 2. Redistributions in binary form must reproduce the above copyright
c487f3
+ *    notice, this list of conditions and the following disclaimer in the
c487f3
+ *    documentation and/or other materials provided with the distribution.
c487f3
+ * 3. All advertising materials mentioning features or use of this software
c487f3
+ *    must display the following acknowledgement:
c487f3
+ *      This product includes software developed by Adam Dunkels.
c487f3
+ * 4. The name of the author may not be used to endorse or promote
c487f3
+ *    products derived from this software without specific prior
c487f3
+ *    written permission.
c487f3
+ *
c487f3
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
c487f3
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
c487f3
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c487f3
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c487f3
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c487f3
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
c487f3
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
c487f3
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
c487f3
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
c487f3
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
c487f3
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c487f3
+ *
c487f3
+ * qedi.c - qedi user space driver
c487f3
+ * This file handles different qedi NIC operations,
c487f3
+ * qedi_open - initializes all hardware resources under NIC device
c487f3
+ * qedi_close - closes the NIC device
c487f3
+ * qedi_read - reads data to the hardware
c487f3
+ * qedi_write - writes data to the hardware
c487f3
+ * qedi_start_xmit - sends a pkt of data on NIC device
c487f3
+ * qedi_get_tx_pkt - gets a Tx pkt from NIC
c487f3
+ * qedi_clear_tx_intr - clears the Tx interrupt
c487f3
+ * NOTE: nic_t is used as NIC device,
c487f3
+ * 	 qedi is not attached to netdev hence it is not mandatory
c487f3
+ * 	 for netdev to be upd
c487f3
+ */
c487f3
+#include <errno.h>
c487f3
+#include <stdio.h>
c487f3
+#include <string.h>
c487f3
+#include <arpa/inet.h>
c487f3
+#include <linux/types.h>
c487f3
+#include <linux/sockios.h>
c487f3
+#include <linux/ethtool.h>
c487f3
+#include <linux/netlink.h>
c487f3
+#include <sys/mman.h>
c487f3
+#include <sys/ioctl.h>
c487f3
+#include <sys/types.h>
c487f3
+#include <sys/stat.h>
c487f3
+#include <sys/user.h>
c487f3
+#include <fcntl.h>
c487f3
+#include <unistd.h>
c487f3
+
c487f3
+#include "config.h"
c487f3
+
c487f3
+#include "build_date.h"
c487f3
+#include "bnx2x.h"
c487f3
+#include "qedi.h"
c487f3
+#include "cnic.h"
c487f3
+#include "logger.h"
c487f3
+#include "nic.h"
c487f3
+#include "nic_id.h"
c487f3
+#include "nic_utils.h"
c487f3
+#include "options.h"
c487f3
+
c487f3
+#define PFX	"qedi "
c487f3
+
c487f3
+extern int nl_sock;
c487f3
+
c487f3
+/*  Foward struct declarations */
c487f3
+struct nic_ops qedi_op;
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * NIC Library Strings
c487f3
+ ******************************************************************************/
c487f3
+static const char library_name[] = "qedi";
c487f3
+static const char library_version[] = PACKAGE_VERSION;
c487f3
+static const char library_uio_name[] = "qedi_uio";
c487f3
+
c487f3
+/*  The name that should be returned from /sys/class/uio/uio0/name */
c487f3
+static const char cnic_uio_sysfs_name_tempate[] = "/sys/class/uio/uio%i/name";
c487f3
+static const char qedi_uio_sysfs_name[] = "qedi_uio";
c487f3
+static const char qedi_host_mac_template[] =
c487f3
+	"/sys/class/iscsi_host/host%i/hwaddress";
c487f3
+
c487f3
+struct qedi_driver_version qedi_version = {
c487f3
+	QEDI_UNKNOWN_MAJOR_VERSION,
c487f3
+	QEDI_UNKNOWN_MINOR_VERSION,
c487f3
+	QEDI_UNKNOWN_SUB_MINOR_VERSION,
c487f3
+};
c487f3
+
c487f3
+static int qedi_clear_tx_intr(nic_t *nic);
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * QEDI Library Functions
c487f3
+ ******************************************************************************/
c487f3
+/**
c487f3
+ *  qedi_get_library_name() - Used to get the name of this NIC library
c487f3
+ *  @param name - This function will return the pointer to this NIC
c487f3
+ *                library name
c487f3
+ *  @param name_size
c487f3
+ */
c487f3
+static void qedi_get_library_name(char **name, size_t *name_size)
c487f3
+{
c487f3
+	*name = (char *)library_name;
c487f3
+	*name_size = sizeof(library_name);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_library_version() - Used to get the version string of this
c487f3
+ *                                NIC library
c487f3
+ *  @param version - This function will return the pointer to this NIC
c487f3
+ *                   library version string
c487f3
+ *  @param version_size - This will be set with the version size
c487f3
+ */
c487f3
+static void qedi_get_library_version(char **version, size_t *version_size)
c487f3
+{
c487f3
+	*version = (char *)library_version;
c487f3
+	*version_size = sizeof(library_version);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_build_date() - Used to get the build date string of this library
c487f3
+ *  @param version - This function will return the pointer to this NIC
c487f3
+ *                   library build date string
c487f3
+ *  @param version_size - This will be set with the build date string size
c487f3
+ */
c487f3
+static void qedi_get_build_date(char **build, size_t *build_size)
c487f3
+{
c487f3
+	*build = (char *)build_date;
c487f3
+	*build_size = sizeof(build_date);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_transport_name() - Used to get the transport name associated
c487f3
+ *                              with this this NIC library
c487f3
+ *  @param transport_name - This function will return the pointer to this NIC
c487f3
+ *                          library's associated transport string
c487f3
+ *  @param transport_name_size - This will be set with the transport name size
c487f3
+ */
c487f3
+static void qedi_get_transport_name(char **transport_name,
c487f3
+				    size_t *transport_name_size)
c487f3
+{
c487f3
+	*transport_name = (char *)qedi_library_transport_name;
c487f3
+	*transport_name_size = qedi_library_transport_name_size;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_uio_name() - Used to get the uio name associated with this this
c487f3
+ *                        NIC library
c487f3
+ *  @param uio_name - This function will return the pointer to this NIC
c487f3
+ *                    library's associated uio string
c487f3
+ *  @param transport_name_size - This will be set with the uio name size
c487f3
+ */
c487f3
+static void qedi_get_uio_name(char **uio_name, size_t *uio_name_size)
c487f3
+{
c487f3
+	*uio_name = (char *)library_uio_name;
c487f3
+	*uio_name_size = sizeof(library_uio_name);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_ops() - Used to get the NIC library op table
c487f3
+ *  @param op - The op table of this NIC library
c487f3
+ */
c487f3
+struct nic_ops *qedi_get_ops()
c487f3
+{
c487f3
+	return &qedi_op;
c487f3
+}
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * qedi Utility Functions
c487f3
+ ******************************************************************************/
c487f3
+/*******************************************************************************
c487f3
+ * Utility Functions Used to read register from the qedi device
c487f3
+ ******************************************************************************/
c487f3
+static void qedi_set_drv_version_unknown(qedi_t *bp)
c487f3
+{
c487f3
+	bp->version.major = QEDI_UNKNOWN_MAJOR_VERSION;
c487f3
+	bp->version.minor = QEDI_UNKNOWN_MINOR_VERSION;
c487f3
+	bp->version.sub_minor = QEDI_UNKNOWN_SUB_MINOR_VERSION;
c487f3
+}
c487f3
+
c487f3
+/* Return: 1 = Unknown, 0 = Known */
c487f3
+static int qedi_is_drv_version_unknown(struct qedi_driver_version *version)
c487f3
+{
c487f3
+	if ((version->major == (uint16_t)QEDI_UNKNOWN_MAJOR_VERSION) &&
c487f3
+	    (version->minor == (uint16_t)QEDI_UNKNOWN_MINOR_VERSION) &&
c487f3
+	    (version->sub_minor == (uint16_t)QEDI_UNKNOWN_SUB_MINOR_VERSION)) {
c487f3
+		return 1;
c487f3
+	}
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ * qedi_get_drv_version() - Used to determine the driver version
c487f3
+ * @param bp - Device used to determine qedi driver version
c487f3
+ */
c487f3
+static int qedi_get_drv_version(qedi_t *bp)
c487f3
+{
c487f3
+	nic_t *nic = bp->parent;
c487f3
+
c487f3
+	/*
c487f3
+	 * CAPABILITIES: Get the iscsi driver version from qedi
c487f3
+	 * This may be obtained from sysfs
c487f3
+	 */
c487f3
+	LOG_INFO(PFX "%s: qedi driver using version %d.%d.%d",
c487f3
+		 nic->log_name,
c487f3
+		 bp->version.major, bp->version.minor, bp->version.sub_minor);
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/******************************************************************************/
c487f3
+
c487f3
+/**
c487f3
+ * qedi_get_chip_id() - Used to retrieve the chip ID from the nic
c487f3
+ * @param dev - Device used to determin NIC type
c487f3
+ * @return Chip ID read from the MISC ID register
c487f3
+ */
c487f3
+static int qedi_get_chip_id(qedi_t *bp)
c487f3
+{
c487f3
+	/* int val, id; */
c487f3
+
c487f3
+	/* Get the chip revision id and number. */
c487f3
+	/* chip num:16-31, rev:12-15, metal:4-11, bond_id:0-3 */
c487f3
+	/*
c487f3
+	 * CAPABILITIES: Get the CHIP info from qedi through sysfs or uio struct.
c487f3
+	 */
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_uio_verify()
c487f3
+ *
c487f3
+ */
c487f3
+static int qedi_uio_verify(nic_t *nic)
c487f3
+{
c487f3
+	char *raw = NULL, *raw_tmp;
c487f3
+	uint32_t raw_size = 0;
c487f3
+	char temp_path[sizeof(cnic_uio_sysfs_name_tempate) + 8];
c487f3
+	int rc = 0;
c487f3
+
c487f3
+	/*  Build the path to determine uio name */
c487f3
+	snprintf(temp_path, sizeof(temp_path),
c487f3
+		 cnic_uio_sysfs_name_tempate, nic->uio_minor);
c487f3
+
c487f3
+	rc = capture_file(&raw, &raw_size, temp_path);
c487f3
+	if (rc != 0)
c487f3
+		goto error;
c487f3
+
c487f3
+	/* sanitize name string by replacing newline with null termination */
c487f3
+	raw_tmp = raw;
c487f3
+	while (*raw_tmp != '\n')
c487f3
+		raw_tmp++;
c487f3
+	*raw_tmp = '\0';
c487f3
+
c487f3
+	if (strncmp(raw, qedi_uio_sysfs_name,
c487f3
+		    sizeof(qedi_uio_sysfs_name)) != 0) {
c487f3
+		LOG_ERR(PFX "%s: uio names not equal: expecting %s got %s from %s",
c487f3
+			nic->log_name, qedi_uio_sysfs_name, raw, temp_path);
c487f3
+		rc = -EIO;
c487f3
+	}
c487f3
+
c487f3
+	free(raw);
c487f3
+
c487f3
+	LOG_INFO(PFX "%s: Verified is a qedi_uio device", nic->log_name);
c487f3
+
c487f3
+error:
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+static int qedi_get_mac_addr(qedi_t *bp)
c487f3
+{
c487f3
+	nic_t *nic = bp->parent;
c487f3
+	char *raw = NULL, *raw_tmp;
c487f3
+	uint32_t raw_size = 0;
c487f3
+	char temp_path[sizeof(qedi_host_mac_template) + 8];
c487f3
+	int rc = 0;
c487f3
+
c487f3
+	/*  Build the path to determine mac address */
c487f3
+	snprintf(temp_path, sizeof(temp_path),
c487f3
+		 qedi_host_mac_template, nic->host_no);
c487f3
+
c487f3
+	rc = capture_file(&raw, &raw_size, temp_path);
c487f3
+	if (rc != 0)
c487f3
+		goto error;
c487f3
+
c487f3
+	/* sanitize name string by replacing newline with null termination */
c487f3
+	raw_tmp = raw;
c487f3
+	while (*raw_tmp != '\n')
c487f3
+		raw_tmp++;
c487f3
+	*raw_tmp = '\0';
c487f3
+
c487f3
+	rc = sscanf(raw, "%02x:%02x:%02x:%02x:%02x:%02x",
c487f3
+	       (uint32_t *)&nic->mac_addr[0], (uint32_t *)&nic->mac_addr[1],
c487f3
+	       (uint32_t *)&nic->mac_addr[2], (uint32_t *)&nic->mac_addr[3],
c487f3
+	       (uint32_t *)&nic->mac_addr[4], (uint32_t *)&nic->mac_addr[5]);
c487f3
+	if (rc != 1) {
c487f3
+		LOG_WARN(PFX "%s: Could not parse mac_addr",
c487f3
+			nic->log_name);
c487f3
+		rc = -ENODEV;
c487f3
+		goto error;
c487f3
+	}
c487f3
+
c487f3
+error:
c487f3
+	if (raw)
c487f3
+		free(raw);
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * qedi Utility Functions to get to the hardware consumer indexes
c487f3
+ ******************************************************************************/
c487f3
+
c487f3
+static __u32 qedi_get_rx(qedi_t *bp)
c487f3
+{
c487f3
+	return ((struct qedi_uio_ctrl *)bp->uctrl_map)->host_rx_cons;
c487f3
+}
c487f3
+
c487f3
+static __u32 qedi_get_tx(qedi_t *bp)
c487f3
+{
c487f3
+	return ((struct qedi_uio_ctrl *)bp->uctrl_map)->hw_tx_cons;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_free() - Used to free a qedi structure
c487f3
+ */
c487f3
+static void qedi_free(nic_t *nic)
c487f3
+{
c487f3
+	if (nic->priv)
c487f3
+		free(nic->priv);
c487f3
+	nic->priv = NULL;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_alloc() - Used to allocate a qedi structure
c487f3
+ */
c487f3
+static qedi_t *qedi_alloc(nic_t *nic)
c487f3
+{
c487f3
+	qedi_t *bp = malloc(sizeof(*bp));
c487f3
+
c487f3
+	if (!bp) {
c487f3
+		LOG_ERR(PFX "%s: Could not allocate QEDI space",
c487f3
+			nic->log_name);
c487f3
+		return NULL;
c487f3
+	}
c487f3
+
c487f3
+	/*  Clear out the CNIC contents */
c487f3
+	memset(bp, 0, sizeof(*bp));
c487f3
+
c487f3
+	bp->parent = nic;
c487f3
+	nic->priv = (void *)bp;
c487f3
+	get_iscsi_transport_handle(nic, &nic->transport_handle);
c487f3
+	qedi_set_drv_version_unknown(bp);
c487f3
+
c487f3
+	return bp;
c487f3
+}
c487f3
+
c487f3
+int uio_get_map_offset(nic_t *nic, uint8_t map, uint32_t *offset)
c487f3
+{
c487f3
+	char *raw = NULL;
c487f3
+	uint32_t raw_size = 0;
c487f3
+	ssize_t elements_read;
c487f3
+	char temp_path[sizeof(UIO_OFFSET_TMPL) + 8];
c487f3
+	int rc = 0;
c487f3
+
c487f3
+	/*  Capture RX buffer size */
c487f3
+	snprintf(temp_path, sizeof(temp_path),
c487f3
+		 UIO_OFFSET_TMPL, nic->uio_minor, map);
c487f3
+
c487f3
+	rc = capture_file(&raw, &raw_size, temp_path);
c487f3
+	if (rc != 0)
c487f3
+		goto error;
c487f3
+
c487f3
+	elements_read = sscanf(raw, "0x%x", offset);
c487f3
+	if (elements_read != 1) {
c487f3
+		LOG_ERR(PFX "%s: Couldn't get the offset from %s",
c487f3
+			nic->log_name, temp_path);
c487f3
+		rc = -EIO;
c487f3
+		goto error;
c487f3
+	}
c487f3
+
c487f3
+	rc = 0;
c487f3
+error:
c487f3
+	if (raw)
c487f3
+		free(raw);
c487f3
+
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+int uio_get_map_info(nic_t *nic, uint8_t map, char *attr, uint32_t *val)
c487f3
+{
c487f3
+	char *raw = NULL;
c487f3
+	uint32_t raw_size = 0;
c487f3
+	ssize_t elements_read;
c487f3
+	char temp_path[sizeof(UIO_ATTR_TMPL) + 8];
c487f3
+	int rc = 0;
c487f3
+
c487f3
+	/*  Capture RX buffer size */
c487f3
+	snprintf(temp_path, sizeof(temp_path),
c487f3
+		 UIO_ATTR_TMPL, nic->uio_minor, map, attr);
c487f3
+
c487f3
+	rc = capture_file(&raw, &raw_size, temp_path);
c487f3
+	if (rc != 0)
c487f3
+		goto error;
c487f3
+
c487f3
+	elements_read = sscanf(raw, "0x%x", val);
c487f3
+	if (elements_read != 1) {
c487f3
+		LOG_ERR(PFX "%s: Couldn't get the offset from %s",
c487f3
+			nic->log_name, temp_path);
c487f3
+		rc = -EIO;
c487f3
+		goto error;
c487f3
+	}
c487f3
+
c487f3
+	rc = 0;
c487f3
+error:
c487f3
+	if (raw)
c487f3
+		free(raw);
c487f3
+
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ * qedi_open() - This will initialize all the hardware resources underneath
c487f3
+ *               a struct cnic_uio device
c487f3
+ * @param dev - The struct cnic_uio device to attach the hardware with
c487f3
+ * @return 0 on success, on failure a errno will be returned
c487f3
+ */
c487f3
+static int qedi_open(nic_t *nic)
c487f3
+{
c487f3
+	qedi_t *bp = NULL;
c487f3
+	struct stat uio_stat;
c487f3
+	int i, rc;
c487f3
+	int count;
c487f3
+	uint32_t bus;
c487f3
+	uint32_t slot;
c487f3
+	uint32_t func;
c487f3
+	uint32_t offset;
c487f3
+
c487f3
+	/*  Sanity Check: validate the parameters */
c487f3
+	if (!nic) {
c487f3
+		LOG_ERR(PFX "nic == NULL");
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	if ((nic->priv) != NULL &&
c487f3
+	    (((qedi_t *)(nic->priv))->flags & QEDI_OPENED)) {
c487f3
+		return 0;
c487f3
+	}
c487f3
+
c487f3
+	if (nic->host_no == INVALID_HOST_NO) {
c487f3
+		rc = sscanf(nic->config_device_name, "host%d", &nic->host_no);
c487f3
+		if (rc != 1) {
c487f3
+			LOG_WARN(PFX "%s: Could not parse for host number",
c487f3
+				 nic->config_device_name);
c487f3
+			rc = -ENODEV;
c487f3
+			goto open_error;
c487f3
+		}
c487f3
+	}
c487f3
+
c487f3
+	bp = qedi_alloc(nic);
c487f3
+	if (!bp)
c487f3
+		return -ENOMEM;
c487f3
+
c487f3
+	if (qedi_is_drv_version_unknown(&qedi_version)) {
c487f3
+		/* If version is unknown, go read from ethtool */
c487f3
+		rc = qedi_get_drv_version(bp);
c487f3
+		if (rc)
c487f3
+			goto open_error;
c487f3
+	} else {
c487f3
+		/* Version is not unknown, just use it */
c487f3
+		qedi_version.major = bp->version.major;
c487f3
+		qedi_version.minor = bp->version.minor;
c487f3
+		qedi_version.sub_minor = bp->version.sub_minor;
c487f3
+	}
c487f3
+
c487f3
+	count = 0;
c487f3
+	while ((nic->fd < 0) && count < 15) {
c487f3
+		/*  udev might not have created the file yet */
c487f3
+		pthread_mutex_unlock(&nic->nic_mutex);
c487f3
+		sleep(1);
c487f3
+		pthread_mutex_lock(&nic->nic_mutex);
c487f3
+
c487f3
+		nic->fd = open(nic->uio_device_name, O_RDWR | O_NONBLOCK);
c487f3
+		if (nic->fd != INVALID_FD) {
c487f3
+			LOG_ERR(PFX "%s: uio device has been brought up via pid: %d on fd: %d",
c487f3
+				nic->uio_device_name, getpid(), nic->fd);
c487f3
+
c487f3
+			rc = qedi_uio_verify(nic);
c487f3
+			if (rc != 0)
c487f3
+				continue;
c487f3
+
c487f3
+			break;
c487f3
+		} else {
c487f3
+			LOG_WARN(PFX "%s: Could not open device: %s, [%s]",
c487f3
+				 nic->log_name, nic->uio_device_name,
c487f3
+				 strerror(errno));
c487f3
+
c487f3
+			manually_trigger_uio_event(nic, nic->uio_minor);
c487f3
+
c487f3
+			/*  udev might not have created the file yet */
c487f3
+			pthread_mutex_unlock(&nic->nic_mutex);
c487f3
+			sleep(1);
c487f3
+			pthread_mutex_lock(&nic->nic_mutex);
c487f3
+
c487f3
+			count++;
c487f3
+		}
c487f3
+	}
c487f3
+	if (fstat(nic->fd, &uio_stat) < 0) {
c487f3
+		LOG_ERR(PFX "%s: Could not fstat device", nic->log_name);
c487f3
+		rc = -ENODEV;
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+	nic->uio_minor = minor(uio_stat.st_rdev);
c487f3
+
c487f3
+	/*
c487f3
+	 * CAPABILITIES: acquire the rx buffer size and rx ring size from qedi
c487f3
+	 */
c487f3
+
c487f3
+	bp->rx_ring_size = RX_RING_SIZE;
c487f3
+	bp->rx_buffer_size = PKT_BUF_SIZE;
c487f3
+
c487f3
+	LOG_DEBUG(PFX "%s: using rx ring size: %d, rx buffer size: %d",
c487f3
+		  nic->log_name, bp->rx_ring_size, bp->rx_buffer_size);
c487f3
+
c487f3
+	/* Determine the number of UIO events that have already occurred */
c487f3
+	rc = detemine_initial_uio_events(nic, &nic->intr_count);
c487f3
+	if (rc != 0) {
c487f3
+		LOG_ERR(PFX "Could not get the no. of initial UIO events");
c487f3
+		nic->intr_count = 0;
c487f3
+	}
c487f3
+
c487f3
+	/* Allocate space for rx pkt ring */
c487f3
+	bp->rx_pkt_ring = malloc(sizeof(void *) * bp->rx_ring_size);
c487f3
+	if (!bp->rx_pkt_ring) {
c487f3
+		LOG_ERR(PFX "%s: Could not allocate space for rx_pkt_ring",
c487f3
+			nic->log_name);
c487f3
+		rc = errno;
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	/*
c487f3
+	 * Map the uio struct and packet buffer
c487f3
+	 */
c487f3
+	offset = 0;
c487f3
+	rc = uio_get_map_info(nic, QEDI_UCTRL_MAP_REG, "size", &offset);
c487f3
+	if (rc) {
c487f3
+		LOG_INFO(PFX "Failed to get the map size rc=%d", rc);
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+	LOG_INFO(PFX "uctrl map size=%u", offset);
c487f3
+
c487f3
+	offset = 0;
c487f3
+	rc = uio_get_map_info(nic, QEDI_RING_MAP_REG, "size", &offset);
c487f3
+	if (rc) {
c487f3
+		LOG_INFO(PFX "Failed to get the map size rc=%d", rc);
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+	LOG_INFO(PFX "ring map size=%u", offset);
c487f3
+
c487f3
+	offset = 0;
c487f3
+	rc = uio_get_map_info(nic, QEDI_BUF_MAP_REG, "size", &offset);
c487f3
+	if (rc) {
c487f3
+		LOG_INFO(PFX "Failed to get the map size rc=%d", rc);
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+	LOG_INFO(PFX "buf map size=%u", offset);
c487f3
+
c487f3
+	offset = 0;
c487f3
+	rc = uio_get_map_offset(nic, QEDI_UCTRL_MAP_REG, &offset);
c487f3
+	if (rc) {
c487f3
+		LOG_INFO(PFX "Failed to get the map offset rc=%d", rc);
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	bp->uctrl_map = mmap(NULL, sizeof(struct qedi_uio_ctrl),
c487f3
+			    PROT_READ | PROT_WRITE,
c487f3
+			    MAP_SHARED | MAP_LOCKED,
c487f3
+			    nic->fd, (off_t)0);
c487f3
+	if (bp->uctrl_map == MAP_FAILED) {
c487f3
+		LOG_INFO(PFX "%s: Could not mmap uio ctrl struct: %s",
c487f3
+			 nic->log_name, strerror(errno));
c487f3
+		bp->uctrl_map = NULL;
c487f3
+		rc = errno;
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	bp->uctrl_map_offset = offset;
c487f3
+	bp->uctrl_map += offset;
c487f3
+
c487f3
+	bp->rx_comp_ring = mmap(NULL, nic->page_size,
c487f3
+			   PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
c487f3
+			   nic->fd, (off_t)nic->page_size);
c487f3
+	if (bp->rx_comp_ring == MAP_FAILED) {
c487f3
+		LOG_INFO(PFX "%s: Could not mmap rx_comp_ring: %s",
c487f3
+			 nic->log_name, strerror(errno));
c487f3
+		bp->rx_comp_ring = NULL;
c487f3
+		rc = errno;
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	bp->bufs = mmap(NULL, (bp->rx_ring_size + 1) * bp->rx_buffer_size,
c487f3
+			PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
c487f3
+			nic->fd, (off_t)2 * nic->page_size);
c487f3
+	if (bp->bufs == MAP_FAILED) {
c487f3
+		LOG_INFO(PFX "%s: Could not mmap pkt buffers: %s",
c487f3
+			 nic->log_name, strerror(errno));
c487f3
+		bp->bufs = NULL;
c487f3
+		rc = errno;
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	/*
c487f3
+	 * Get all CHIP related info from qedi
c487f3
+	 */
c487f3
+	bp->chip_id = qedi_get_chip_id(bp);
c487f3
+	LOG_DEBUG(PFX "Chip ID: %x", bp->chip_id);
c487f3
+
c487f3
+	rc = get_bus_slot_func_num(nic, &bus, &slot, &func);
c487f3
+	if (rc != 0) {
c487f3
+		LOG_INFO(PFX "%s: Couldn't determine bus:slot.func",
c487f3
+			 nic->log_name);
c487f3
+		goto open_error;
c487f3
+	}
c487f3
+
c487f3
+	/*
c487f3
+	 * Get all function, pfid, client_id and cid info from qedi
c487f3
+	 */
c487f3
+	LOG_INFO(PFX "%s: func 0x%x, pfid 0x%x, client_id 0x%x, cid 0x%x",
c487f3
+		 nic->log_name, bp->func, bp->pfid, bp->client_id, bp->cid);
c487f3
+
c487f3
+	bp->get_rx_cons = qedi_get_rx;
c487f3
+	bp->get_tx_cons = qedi_get_tx;
c487f3
+	bp->tx_cons = 0;
c487f3
+	bp->tx_prod = 0;
c487f3
+	bp->tx_bd_prod = 0;
c487f3
+	bp->tx_pkt = bp->bufs;
c487f3
+	bp->rx_pkts = bp->bufs + bp->rx_buffer_size;
c487f3
+
c487f3
+	bp->rx_index = 0;
c487f3
+	bp->rx_cons = 0;
c487f3
+	bp->rx_bd_cons = 0;
c487f3
+	bp->rx_prod = 127;
c487f3
+	bp->rx_bd_prod = bp->rx_ring_size;
c487f3
+
c487f3
+	for (i = 0; i < bp->rx_ring_size; i++) {
c487f3
+		void *ptr = bp->bufs + (bp->rx_buffer_size * (i + 1));
c487f3
+
c487f3
+		bp->rx_pkt_ring[i] = ptr;
c487f3
+	}
c487f3
+
c487f3
+	qedi_get_mac_addr(bp);
c487f3
+	LOG_INFO(PFX "%s:  Using mac address: %02x:%02x:%02x:%02x:%02x:%02x",
c487f3
+		 nic->log_name,
c487f3
+		 nic->mac_addr[0], nic->mac_addr[1], nic->mac_addr[2],
c487f3
+		 nic->mac_addr[3], nic->mac_addr[4], nic->mac_addr[5]);
c487f3
+
c487f3
+	qedi_get_library_name(&nic->library_name, (size_t *)&count);
c487f3
+	LOG_INFO("%s: qedi initialized", nic->log_name);
c487f3
+
c487f3
+	bp->flags |= QEDI_OPENED;
c487f3
+
c487f3
+	return 0;
c487f3
+
c487f3
+open_error:
c487f3
+
c487f3
+	if (bp->bufs) {
c487f3
+		munmap(bp->bufs, (bp->rx_ring_size + 1) * bp->rx_buffer_size);
c487f3
+		bp->bufs = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (bp->rx_comp_ring) {
c487f3
+		munmap(bp->rx_comp_ring, nic->page_size);
c487f3
+		bp->rx_comp_ring = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (bp->uctrl_map) {
c487f3
+		bp->uctrl_map -= bp->uctrl_map_offset;
c487f3
+		munmap(bp->uctrl_map, sizeof(struct qedi_uio_ctrl));
c487f3
+		bp->uctrl_map = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (bp->rx_pkt_ring) {
c487f3
+		free(bp->rx_pkt_ring);
c487f3
+		bp->rx_pkt_ring = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (nic->fd != INVALID_FD) {
c487f3
+		close(nic->fd);
c487f3
+		nic->fd = INVALID_FD;
c487f3
+	}
c487f3
+
c487f3
+	qedi_free(nic);
c487f3
+
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_uio_close_resources() - Used to free resource for the NIC/CNIC
c487f3
+ *  @param nic - NIC device to free resource
c487f3
+ *  @param graceful - whether to wait to close gracefully
c487f3
+ *  @return 0 on success, <0 on failure
c487f3
+ */
c487f3
+static int qedi_uio_close_resources(nic_t *nic, NIC_SHUTDOWN_T graceful)
c487f3
+{
c487f3
+	qedi_t *bp = (qedi_t *)nic->priv;
c487f3
+	int rc = 0;
c487f3
+
c487f3
+	/*  Check if there is an assoicated qedi device */
c487f3
+	if (!bp) {
c487f3
+		LOG_WARN(PFX "%s: when closing resources there is no assoicated qedi",
c487f3
+			 nic->log_name);
c487f3
+		return -EIO;
c487f3
+	}
c487f3
+
c487f3
+	/*  Clean up allocated memory */
c487f3
+
c487f3
+	if (bp->rx_pkt_ring) {
c487f3
+		free(bp->rx_pkt_ring);
c487f3
+		bp->rx_pkt_ring = NULL;
c487f3
+	}
c487f3
+
c487f3
+	/*  Clean up mapped registers */
c487f3
+	if (bp->bufs) {
c487f3
+		rc = munmap(bp->bufs,
c487f3
+			    (bp->rx_ring_size + 1) * bp->rx_buffer_size);
c487f3
+		if (rc != 0)
c487f3
+			LOG_ERR(PFX "%s: Couldn't unmap bufs", nic->log_name);
c487f3
+		bp->bufs = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (bp->rx_comp_ring) {
c487f3
+		rc = munmap(bp->rx_comp_ring, nic->page_size);
c487f3
+		if (rc != 0)
c487f3
+			LOG_ERR(PFX "%s: Couldn't unmap ring", nic->log_name);
c487f3
+		bp->rx_comp_ring = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (bp->uctrl_map) {
c487f3
+		bp->uctrl_map -= bp->uctrl_map_offset;
c487f3
+		rc = munmap(bp->uctrl_map, sizeof(struct qedi_uio_ctrl));
c487f3
+		if (rc != 0) {
c487f3
+			LOG_ERR(PFX "%s: Couldn't unmap uio ctrl",
c487f3
+				nic->log_name);
c487f3
+		}
c487f3
+		bp->uctrl_map = NULL;
c487f3
+	}
c487f3
+
c487f3
+	if (nic->fd != INVALID_FD) {
c487f3
+		rc = close(nic->fd);
c487f3
+		if (rc != 0) {
c487f3
+			LOG_ERR(PFX
c487f3
+				 "%s: Couldn't close uio file descriptor: %d",
c487f3
+				 nic->log_name, nic->fd);
c487f3
+		} else {
c487f3
+			LOG_DEBUG(PFX "%s: Closed uio file descriptor: %d",
c487f3
+				  nic->log_name, nic->fd);
c487f3
+		}
c487f3
+
c487f3
+		nic->fd = INVALID_FD;
c487f3
+	} else {
c487f3
+		LOG_ERR(PFX "%s: Invalid uio file descriptor: %d",
c487f3
+			nic->log_name, nic->fd);
c487f3
+	}
c487f3
+
c487f3
+	qedi_set_drv_version_unknown(bp);
c487f3
+
c487f3
+	LOG_INFO(PFX "%s: Closed all resources", nic->log_name);
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_close() - Used to close the NIC device
c487f3
+ *  @param nic - NIC device to close
c487f3
+ *  @param graceful - whether to wait to close gracefully
c487f3
+ *  @return 0 if successful, <0 if there is an error
c487f3
+ */
c487f3
+static int qedi_close(nic_t *nic, NIC_SHUTDOWN_T graceful)
c487f3
+{
c487f3
+	/*  Sanity Check: validate the parameters */
c487f3
+	if (!nic) {
c487f3
+		LOG_ERR(PFX "%s: nic == NULL", __func__);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+	if (!nic->priv) {
c487f3
+		LOG_ERR(PFX "%s: nic->priv == NULL", __func__);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	LOG_INFO(PFX "Closing NIC device: %s", nic->log_name);
c487f3
+
c487f3
+	qedi_uio_close_resources(nic, graceful);
c487f3
+	qedi_free(nic);
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+static void qedi_prepare_xmit_packet(nic_t *nic,
c487f3
+				     nic_interface_t *nic_iface,
c487f3
+				     struct packet *pkt)
c487f3
+{
c487f3
+	qedi_t *bp = (qedi_t *)nic->priv;
c487f3
+	struct uip_vlan_eth_hdr *eth_vlan = (struct uip_vlan_eth_hdr *)pkt->buf;
c487f3
+	struct uip_eth_hdr *eth = (struct uip_eth_hdr *)bp->tx_pkt;
c487f3
+
c487f3
+	if (eth_vlan->tpid == htons(UIP_ETHTYPE_8021Q)) {
c487f3
+		memcpy(bp->tx_pkt, pkt->buf, sizeof(struct uip_eth_hdr));
c487f3
+		eth->type = eth_vlan->type;
c487f3
+		pkt->buf_size -= (sizeof(struct uip_vlan_eth_hdr) -
c487f3
+				  sizeof(struct uip_eth_hdr));
c487f3
+		memcpy(bp->tx_pkt + sizeof(struct uip_eth_hdr),
c487f3
+		       pkt->buf + sizeof(struct uip_vlan_eth_hdr),
c487f3
+		       pkt->buf_size - sizeof(struct uip_eth_hdr));
c487f3
+	} else {
c487f3
+		memcpy(bp->tx_pkt, pkt->buf, pkt->buf_size);
c487f3
+	}
c487f3
+
c487f3
+	msync(bp->tx_pkt, pkt->buf_size, MS_SYNC);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_get_tx_pkt() - This function is used to a TX packet from the NIC
c487f3
+ *  @param nic - The NIC device to send the packet
c487f3
+ */
c487f3
+void *qedi_get_tx_pkt(nic_t *nic)
c487f3
+{
c487f3
+	qedi_t *bp = (qedi_t *)nic->priv;
c487f3
+
c487f3
+	return bp->tx_pkt;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_start_xmit() - This function is used to send a packet of data
c487f3
+ *  @param nic - The NIC device to send the packet
c487f3
+ *  @param len - the length of the TX packet
c487f3
+ *
c487f3
+ */
c487f3
+void qedi_start_xmit(nic_t *nic, size_t len, u16_t vlan_id)
c487f3
+{
c487f3
+	qedi_t *bp = (qedi_t *)nic->priv;
c487f3
+	uint8_t *ubuf;
c487f3
+	struct iscsi_uevent *ev;
c487f3
+	struct iscsi_path *path_data;
c487f3
+	struct qedi_uio_ctrl *uctrl;
c487f3
+	int rc = 0;
c487f3
+	uint16_t buflen;
c487f3
+
c487f3
+	uctrl = (struct qedi_uio_ctrl *)bp->uctrl_map;
c487f3
+
c487f3
+	buflen = sizeof(struct iscsi_uevent) + sizeof(struct iscsi_path);
c487f3
+	ubuf = calloc(1, NLMSG_SPACE(buflen));
c487f3
+	if (!ubuf) {
c487f3
+		LOG_ERR(PFX "%s: alloc failed for uevent buf", __func__);
c487f3
+		return;
c487f3
+	}
c487f3
+
c487f3
+	memset(ubuf, 0, NLMSG_SPACE(buflen));
c487f3
+
c487f3
+	/*  prepare the iscsi_uevent buffer */
c487f3
+	ev = (struct iscsi_uevent *)ubuf;
c487f3
+	ev->type = ISCSI_UEVENT_PATH_UPDATE;
c487f3
+	ev->transport_handle = nic->transport_handle;
c487f3
+	ev->u.set_path.host_no = nic->host_no;
c487f3
+
c487f3
+	/*  Prepare the iscsi_path buffer */
c487f3
+	path_data = (struct iscsi_path *)(ubuf + sizeof(struct iscsi_uevent));
c487f3
+	path_data->handle = QEDI_PATH_HANDLE;
c487f3
+	path_data->vlan_id = vlan_id;
c487f3
+	uctrl->host_tx_pkt_len = len;
c487f3
+
c487f3
+	rc = __kipc_call(nl_sock, ev, buflen);
c487f3
+	if (rc > 0) {
c487f3
+		bp->tx_prod++;
c487f3
+		uctrl->host_tx_prod++;
c487f3
+		msync(uctrl, sizeof(struct qedi_uio_ctrl), MS_SYNC);
c487f3
+		LOG_PACKET(PFX "%s: sent %d bytes using bp->tx_prod: %d",
c487f3
+			   nic->log_name, len, bp->tx_prod);
c487f3
+	} else {
c487f3
+		LOG_ERR(PFX "Pkt transmission failed: %d", rc);
c487f3
+		pthread_mutex_unlock(&nic->xmit_mutex);
c487f3
+	}
c487f3
+
c487f3
+	free(ubuf);
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_write() - Used to write the data to the hardware
c487f3
+ *  @param nic - NIC hardware to read from
c487f3
+ *  @param pkt - The packet which will hold the data to be sent on the wire
c487f3
+ *  @return 0 if successful, <0 if failed
c487f3
+ */
c487f3
+int qedi_write(nic_t *nic, nic_interface_t *nic_iface, packet_t *pkt)
c487f3
+{
c487f3
+	qedi_t *bp;
c487f3
+	struct uip_stack *uip;
c487f3
+	int i = 0;
c487f3
+
c487f3
+	/* Sanity Check: validate the parameters */
c487f3
+	if (!nic || !nic_iface || !pkt) {
c487f3
+		LOG_ERR(PFX "%s: qedi_write() nic == 0x%p || nic_iface == 0x%p || pkt == 0x%x",
c487f3
+			nic, nic_iface, pkt);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+	bp = (qedi_t *)nic->priv;
c487f3
+	uip = &nic_iface->ustack;
c487f3
+
c487f3
+	if (pkt->buf_size == 0) {
c487f3
+		LOG_ERR(PFX "%s: Trying to transmitted 0 sized packet",
c487f3
+			nic->log_name);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	/*  Try to wait for a TX completion */
c487f3
+	for (i = 0; i < 15; i++) {
c487f3
+		struct timespec sleep_req = {.tv_sec = 0, .tv_nsec = 5000000 },
c487f3
+		    sleep_rem;
c487f3
+
c487f3
+		if (qedi_clear_tx_intr(nic) == 0)
c487f3
+			break;
c487f3
+
c487f3
+		nanosleep(&sleep_req, &sleep_rem);
c487f3
+	}
c487f3
+
c487f3
+	if (pthread_mutex_trylock(&nic->xmit_mutex) != 0) {
c487f3
+		LOG_PACKET(PFX "%s: Dropped previous transmitted packet",
c487f3
+			   nic->log_name);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	qedi_prepare_xmit_packet(nic, nic_iface, pkt);
c487f3
+	qedi_start_xmit(nic, pkt->buf_size,
c487f3
+			(nic_iface->vlan_priority << 12) |
c487f3
+			nic_iface->vlan_id);
c487f3
+
c487f3
+	/* bump up the tx stats */
c487f3
+	nic->stats.tx.packets++;
c487f3
+	nic->stats.tx.bytes += uip->uip_len;
c487f3
+
c487f3
+	LOG_PACKET(PFX "%s: transmitted %d bytes dev->tx_cons: %d, dev->tx_prod: %d, dev->tx_bd_prod:%d",
c487f3
+		   nic->log_name, pkt->buf_size,
c487f3
+		   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
+ *  qedi_read() - Used to read the data from the hardware
c487f3
+ *  @param nic - NIC hardware to read from
c487f3
+ *  @param pkt - The packet which will hold the data
c487f3
+ *  @return 0 if successful, < 0 if failed
c487f3
+ */
c487f3
+static int qedi_read(nic_t *nic, packet_t *pkt)
c487f3
+{
c487f3
+	qedi_t *bp;
c487f3
+	void *rx_pkt;
c487f3
+	int rc = 0;
c487f3
+	uint32_t sw_cons, bd_cons;
c487f3
+	uint32_t hw_prod;
c487f3
+	uint32_t rx_pkt_idx;
c487f3
+	int len;
c487f3
+	struct qedi_rx_bd *rx_bd;
c487f3
+	struct qedi_uio_ctrl *uctrl;
c487f3
+	uint16_t vlan_id;
c487f3
+
c487f3
+	/* Sanity Check: validate the parameters */
c487f3
+	if (!nic || !pkt) {
c487f3
+		LOG_ERR(PFX "%s: qedi_read() nic == 0x%p || pkt == 0x%x",
c487f3
+			nic, pkt);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	bp = (qedi_t *)nic->priv;
c487f3
+	msync(bp->uctrl_map, sizeof(struct qedi_uio_ctrl), MS_SYNC);
c487f3
+	msync(bp->rx_comp_ring, nic->page_size, MS_SYNC);
c487f3
+	uctrl = (struct qedi_uio_ctrl *)bp->uctrl_map;
c487f3
+	hw_prod = uctrl->hw_rx_prod;
c487f3
+	sw_cons = uctrl->host_rx_cons;
c487f3
+	bd_cons = uctrl->host_rx_bd_cons;
c487f3
+	rx_bd = bp->rx_comp_ring + (bd_cons * sizeof(*rx_bd));
c487f3
+	len = rx_bd->rx_pkt_len;
c487f3
+	rx_pkt_idx = rx_bd->rx_pkt_index;
c487f3
+	vlan_id = rx_bd->vlan_id;
c487f3
+
c487f3
+	if (sw_cons != hw_prod) {
c487f3
+		LOG_DEBUG(PFX "%s: clearing rx interrupt: %d %d",
c487f3
+			  nic->log_name, sw_cons, hw_prod);
c487f3
+		rc = 1;
c487f3
+		rx_pkt = bp->rx_pkts + (bp->rx_buffer_size * rx_pkt_idx);
c487f3
+
c487f3
+		if (len > 0) {
c487f3
+			msync(rx_pkt, len, MS_SYNC);
c487f3
+			/*  Copy the data */
c487f3
+			memcpy(pkt->buf, rx_pkt, len);
c487f3
+			pkt->buf_size = len;
c487f3
+			if (vlan_id) {
c487f3
+				pkt->vlan_tag = vlan_id;
c487f3
+				pkt->flags |= VLAN_TAGGED;
c487f3
+			} else {
c487f3
+				pkt->vlan_tag = 0;
c487f3
+			}
c487f3
+
c487f3
+			LOG_DEBUG(PFX "%s: processing packet length: %d",
c487f3
+				  nic->log_name, len);
c487f3
+
c487f3
+			/* bump up the recv stats */
c487f3
+			nic->stats.rx.packets++;
c487f3
+			nic->stats.rx.bytes += pkt->buf_size;
c487f3
+		} else {
c487f3
+			rc = 0;
c487f3
+		}
c487f3
+
c487f3
+		sw_cons = (sw_cons + 1) % RX_RING_SIZE;
c487f3
+		bd_cons = (bd_cons + 1) % QEDI_NUM_RX_BD;
c487f3
+		uctrl->host_rx_cons_cnt++;
c487f3
+	}
c487f3
+
c487f3
+	uctrl->host_rx_bd_cons = bd_cons;
c487f3
+	uctrl->host_rx_cons = sw_cons;
c487f3
+
c487f3
+	msync(uctrl, sizeof(struct qedi_uio_ctrl), MS_SYNC);
c487f3
+	msync(bp->rx_comp_ring, nic->page_size, MS_SYNC);
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * Clearing TX interrupts
c487f3
+ ******************************************************************************/
c487f3
+/**
c487f3
+ *  qedi_clear_tx_intr() - This routine is called when a TX interrupt occurs
c487f3
+ *  @param nic - the nic the interrupt occurred on
c487f3
+ *  @return  0 on success
c487f3
+ */
c487f3
+
c487f3
+static int qedi_clear_tx_intr(nic_t *nic)
c487f3
+{
c487f3
+	qedi_t *bp;
c487f3
+	uint32_t hw_cons;
c487f3
+	struct qedi_uio_ctrl *uctrl;
c487f3
+
c487f3
+	/* Sanity check: ensure the parameters passed in are valid */
c487f3
+	if (unlikely(!nic)) {
c487f3
+		LOG_ERR(PFX "%s: nic == NULL", __func__);
c487f3
+		return -EINVAL;
c487f3
+	}
c487f3
+
c487f3
+	bp = (qedi_t *)nic->priv;
c487f3
+	uctrl = (struct qedi_uio_ctrl *)bp->uctrl_map;
c487f3
+	msync(bp->uctrl_map, sizeof(struct qedi_uio_ctrl), MS_SYNC);
c487f3
+	hw_cons = uctrl->hw_tx_cons;
c487f3
+
c487f3
+	if (bp->tx_cons == hw_cons) {
c487f3
+		if (bp->tx_cons == bp->tx_prod) {
c487f3
+			/* Make sure the xmit_mutex lock is unlock */
c487f3
+			if (pthread_mutex_trylock(&nic->xmit_mutex))
c487f3
+				LOG_ERR(PFX "qedi tx lock with prod == cons");
c487f3
+
c487f3
+			pthread_mutex_unlock(&nic->xmit_mutex);
c487f3
+			return 0;
c487f3
+		}
c487f3
+		return -EAGAIN;
c487f3
+	}
c487f3
+
c487f3
+	LOG_PACKET(PFX "%s: clearing tx interrupt [%d %d]",
c487f3
+		   nic->log_name, bp->tx_cons, hw_cons);
c487f3
+	bp->tx_cons = hw_cons;
c487f3
+
c487f3
+	/* There is a queued TX packet that needs to be sent out.  The usual
c487f3
+	 * case is when stack will send an ARP packet out before sending the
c487f3
+	 * intended packet
c487f3
+	 */
c487f3
+	if (nic->tx_packet_queue) {
c487f3
+		packet_t *pkt;
c487f3
+		int i;
c487f3
+
c487f3
+		LOG_PACKET(PFX "%s: sending queued tx packet", nic->log_name);
c487f3
+		pkt = nic_dequeue_tx_packet(nic);
c487f3
+
c487f3
+		/* Got a TX packet buffer of the TX queue and put it onto
c487f3
+		 * the hardware
c487f3
+		 */
c487f3
+		if (pkt) {
c487f3
+			qedi_prepare_xmit_packet(nic, pkt->nic_iface, pkt);
c487f3
+
c487f3
+			qedi_start_xmit(nic, pkt->buf_size,
c487f3
+					(pkt->nic_iface->vlan_priority << 12) |
c487f3
+					pkt->nic_iface->vlan_id);
c487f3
+
c487f3
+			LOG_PACKET(PFX "%s: transmitted queued packet %d bytes, dev->tx_cons: %d, dev->tx_prod: %d, dev->tx_bd_prod:%d",
c487f3
+				   nic->log_name, pkt->buf_size,
c487f3
+				   bp->tx_cons, bp->tx_prod, bp->tx_bd_prod);
c487f3
+
c487f3
+			return 0;
c487f3
+		}
c487f3
+
c487f3
+		/* Try to wait for a TX completion */
c487f3
+		for (i = 0; i < 15; i++) {
c487f3
+			struct timespec sleep_req = {.tv_sec = 0,
c487f3
+				.tv_nsec = 5000000
c487f3
+			}, sleep_rem;
c487f3
+
c487f3
+			hw_cons = uctrl->hw_tx_cons;
c487f3
+			if (bp->tx_cons != hw_cons) {
c487f3
+				LOG_PACKET(PFX
c487f3
+					   "%s: clearing tx interrupt [%d %d]",
c487f3
+					   nic->log_name, bp->tx_cons, hw_cons);
c487f3
+				bp->tx_cons = hw_cons;
c487f3
+
c487f3
+				break;
c487f3
+			}
c487f3
+
c487f3
+			nanosleep(&sleep_req, &sleep_rem);
c487f3
+		}
c487f3
+	}
c487f3
+
c487f3
+	pthread_mutex_unlock(&nic->xmit_mutex);
c487f3
+
c487f3
+	return 0;
c487f3
+}
c487f3
+
c487f3
+/*******************************************************************************
c487f3
+ * qedi NIC op's table
c487f3
+ ******************************************************************************/
c487f3
+struct nic_ops qedi_op = {
c487f3
+	.description = "qedi",
c487f3
+	.open = qedi_open,
c487f3
+	.close = qedi_close,
c487f3
+	.write = qedi_write,
c487f3
+	.get_tx_pkt = qedi_get_tx_pkt,
c487f3
+	.start_xmit = qedi_start_xmit,
c487f3
+	.read = qedi_read,
c487f3
+	.clear_tx_intr = qedi_clear_tx_intr,
c487f3
+	.handle_iscsi_path_req = cnic_handle_iscsi_path_req,
c487f3
+
c487f3
+	.lib_ops = {
c487f3
+		    .get_library_name = qedi_get_library_name,
c487f3
+		    .get_library_version = qedi_get_library_version,
c487f3
+		    .get_build_date = qedi_get_build_date,
c487f3
+		    .get_transport_name = qedi_get_transport_name,
c487f3
+		    .get_uio_name = qedi_get_uio_name,
c487f3
+		    },
c487f3
+};
c487f3
diff --git a/iscsiuio/src/unix/libs/qedi.h b/iscsiuio/src/unix/libs/qedi.h
c487f3
new file mode 100644
c487f3
index 000000000000..7e0140adb2ba
c487f3
--- /dev/null
c487f3
+++ b/iscsiuio/src/unix/libs/qedi.h
c487f3
@@ -0,0 +1,159 @@
c487f3
+/*
c487f3
+ * Copyright (c) 2016, Cavium Inc.
c487f3
+ *
c487f3
+ * All rights reserved.
c487f3
+ *
c487f3
+ * Redistribution and use in source and binary forms, with or without
c487f3
+ * modification, are permitted provided that the following conditions
c487f3
+ * are met:
c487f3
+ * 1. Redistributions of source code must retain the above copyright
c487f3
+ *    notice, this list of conditions and the following disclaimer.
c487f3
+ * 2. Redistributions in binary form must reproduce the above copyright
c487f3
+ *    notice, this list of conditions and the following disclaimer in the
c487f3
+ *    documentation and/or other materials provided with the distribution.
c487f3
+ * 3. All advertising materials mentioning features or use of this software
c487f3
+ *    must display the following acknowledgement:
c487f3
+ *      This product includes software developed by Adam Dunkels.
c487f3
+ * 4. The name of the author may not be used to endorse or promote
c487f3
+ *    products derived from this software without specific prior
c487f3
+ *    written permission.
c487f3
+ *
c487f3
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
c487f3
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
c487f3
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c487f3
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c487f3
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c487f3
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
c487f3
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
c487f3
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
c487f3
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
c487f3
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
c487f3
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c487f3
+ *
c487f3
+ * qedi.h - qedi user space driver
c487f3
+ *
c487f3
+ */
c487f3
+#ifndef __QEDI_H__
c487f3
+#define __QEDI_H__
c487f3
+
c487f3
+#include "nic.h"
c487f3
+
c487f3
+#define RX_RING_SIZE	15
c487f3
+#define PKT_BUF_SIZE 0X400
c487f3
+#define QEDI_PAGE_SIZE 4096
c487f3
+
c487f3
+#define QEDI_UNKNOWN_MAJOR_VERSION	-1
c487f3
+#define QEDI_UNKNOWN_MINOR_VERSION	-1
c487f3
+#define QEDI_UNKNOWN_SUB_MINOR_VERSION	-1
c487f3
+struct qedi_driver_version {
c487f3
+	uint16_t major;
c487f3
+	uint16_t minor;
c487f3
+	uint16_t sub_minor;
c487f3
+};
c487f3
+
c487f3
+#define QEDI_UCTRL_MAP_REG	0
c487f3
+#define QEDI_RING_MAP_REG	1
c487f3
+#define QEDI_BUF_MAP_REG	2
c487f3
+#define UIO_ATTR_TMPL	"/sys/class/uio/uio%u/maps/map%u/%s"
c487f3
+#define UIO_ADDR_TMPL	"/sys/class/uio/uio%u/maps/map%u/addr"
c487f3
+#define UIO_OFFSET_TMPL	"/sys/class/uio/uio%u/maps/map%u/offset"
c487f3
+#define UIO_SIZE_TMPL	"/sys/class/uio/uio%u/maps/map%u/size"
c487f3
+
c487f3
+struct qedi_uio_ctrl {
c487f3
+	/* meta data */
c487f3
+	__u32 uio_hsi_version;
c487f3
+
c487f3
+	/* user writes */
c487f3
+	__u32 host_tx_prod;
c487f3
+	__u32 host_rx_cons;
c487f3
+	__u32 host_rx_bd_cons;
c487f3
+	__u32 host_tx_pkt_len;
c487f3
+	__u32 host_rx_cons_cnt;
c487f3
+
c487f3
+	/* driver writes */
c487f3
+	__u32 hw_tx_cons;
c487f3
+	__u32 hw_rx_prod;
c487f3
+	__u32 hw_rx_bd_prod;
c487f3
+	__u32 hw_rx_prod_cnt;
c487f3
+
c487f3
+	/* other */
c487f3
+	__u8 mac_addr[6];
c487f3
+	__u8 reserve[2];
c487f3
+};
c487f3
+
c487f3
+struct qedi_rx_bd {
c487f3
+	__u32 rx_pkt_index;
c487f3
+	__u32 rx_pkt_len;
c487f3
+	__u16 vlan_id;
c487f3
+};
c487f3
+
c487f3
+#define QEDI_RX_DESC_CNT	(QEDI_PAGE_SIZE / sizeof(struct qedi_rx_bd))
c487f3
+#define QEDI_MAX_RX_DESC_CNT	(QEDI_RX_DESC_CNT - 1)
c487f3
+#define QEDI_NUM_RX_BD		(QEDI_RX_DESC_CNT * 1)
c487f3
+#define QEDI_MAX_RX_BD		(QEDI_NUM_RX_BD - 1)
c487f3
+
c487f3
+#define QEDI_NEXT_RX_IDX(x)	((((x) & (QEDI_MAX_RX_DESC_CNT)) ==     \
c487f3
+				  (QEDI_MAX_RX_DESC_CNT - 1)) ?         \
c487f3
+				 (x) + 2 : (x) + 1)
c487f3
+
c487f3
+#define QEDI_PATH_HANDLE	0xFE0000000
c487f3
+
c487f3
+typedef struct qedi {
c487f3
+	nic_t *parent;
c487f3
+
c487f3
+	struct qedi_driver_version version;
c487f3
+
c487f3
+	uint16_t flags;
c487f3
+#define CNIC_UIO_UNITIALIZED		0x0001
c487f3
+#define CNIC_UIO_INITIALIZED		0x0002
c487f3
+#define CNIC_UIO_ENABLED		0x0004
c487f3
+#define CNIC_UIO_DISABLED		0x0008
c487f3
+#define CNIC_UIO_IPv6_ENABLED		0x0010
c487f3
+#define CNIC_UIO_ADDED_MULICAST		0x0020
c487f3
+#define CNIC_UIO_MSIX_ENABLED		0x0200
c487f3
+#define CNIC_UIO_TX_HAS_SENT		0x0400
c487f3
+#define QEDI_OPENED			0x0800
c487f3
+
c487f3
+	__u32 chip_id;
c487f3
+	int func;
c487f3
+	int port;
c487f3
+	int pfid;
c487f3
+	__u32 cid;
c487f3
+	__u32 client_id;
c487f3
+
c487f3
+	__u32 tx_prod;
c487f3
+	__u32 tx_bd_prod;
c487f3
+	__u32 tx_cons;
c487f3
+	__u8 tx_vlan_tag_bit;
c487f3
+
c487f3
+	__u32 rx_prod;
c487f3
+	__u32 rx_bd_prod;
c487f3
+	__u32 rx_cons;
c487f3
+	__u32 rx_bd_cons;
c487f3
+	__u32 rx_hw_prod;
c487f3
+
c487f3
+	 __u32 (*get_rx_cons)(struct qedi *);
c487f3
+	 __u32 (*get_tx_cons)(struct qedi *);
c487f3
+
c487f3
+	/* RX ring parameters */
c487f3
+	uint32_t rx_ring_size;
c487f3
+	uint32_t rx_buffer_size;
c487f3
+
c487f3
+	void *bufs; /* Pointer to the mapped buffer space */
c487f3
+	void *uctrl_map; /* UIO control structure */
c487f3
+	uint32_t uctrl_map_offset; /* UIO control structure mmap offset */
c487f3
+
c487f3
+	uint32_t rx_index;
c487f3
+	void *rx_comp_ring;
c487f3
+	void **rx_pkt_ring;
c487f3
+	void *tx_pkt;
c487f3
+	void *rx_pkts;
c487f3
+} qedi_t;
c487f3
+
c487f3
+/******************************************************************************
c487f3
+ *  qedi Function Declarations
c487f3
+ ******************************************************************************/
c487f3
+void qedi_start_xmit(nic_t *nic, size_t len, u16_t vlan_id);
c487f3
+struct nic_ops *qedi_get_ops();
c487f3
+
c487f3
+#endif /* __QEDI_H__ */
c487f3
diff --git a/iscsiuio/src/unix/nic.c b/iscsiuio/src/unix/nic.c
c487f3
index 8825f00b7216..a5f714a91923 100644
c487f3
--- a/iscsiuio/src/unix/nic.c
c487f3
+++ b/iscsiuio/src/unix/nic.c
c487f3
@@ -65,6 +65,7 @@
c487f3
 
c487f3
 #include "bnx2.h"
c487f3
 #include "bnx2x.h"
c487f3
+#include "qedi.h"
c487f3
 #include "ipv6.h"
c487f3
 
c487f3
 /******************************************************************************
c487f3
@@ -180,7 +181,7 @@ error:
c487f3
 }
c487f3
 
c487f3
 static struct nic_ops *(*nic_get_ops[]) () = {
c487f3
-bnx2_get_ops, bnx2x_get_ops,};
c487f3
+bnx2_get_ops, bnx2x_get_ops, qedi_get_ops};
c487f3
 
c487f3
 int load_all_nic_libraries()
c487f3
 {
c487f3
@@ -404,6 +405,7 @@ nic_t *nic_init()
c487f3
 	memset(nic, 0, sizeof(*nic));
c487f3
 	nic->uio_minor = -1;
c487f3
 	nic->fd = INVALID_FD;
c487f3
+	nic->host_no = INVALID_HOST_NO;
c487f3
 	nic->next = NULL;
c487f3
 	nic->thread = INVALID_THREAD;
c487f3
 	nic->enable_thread = INVALID_THREAD;
c487f3
@@ -1070,6 +1072,8 @@ int process_packets(nic_t *nic,
c487f3
 		}
c487f3
 nic_iface_present:
c487f3
 		pkt->nic_iface = nic_iface;
c487f3
+		LOG_DEBUG(PFX "%s: found nic iface, type=0x%x, bufsize=%d",
c487f3
+			  nic->log_name, type, pkt->buf_size);
c487f3
 
c487f3
 		ustack = &nic_iface->ustack;
c487f3
 
c487f3
diff --git a/iscsiuio/src/unix/nic.h b/iscsiuio/src/unix/nic.h
c487f3
index ec157815a375..2c41e6e3c25a 100644
c487f3
--- a/iscsiuio/src/unix/nic.h
c487f3
+++ b/iscsiuio/src/unix/nic.h
c487f3
@@ -334,6 +334,7 @@ typedef struct nic {
c487f3
 
c487f3
 	/* The thread used to perform ping */
c487f3
 	pthread_t ping_thread;
c487f3
+	uint64_t transport_handle;
c487f3
 } nic_t;
c487f3
 
c487f3
 /******************************************************************************
c487f3
diff --git a/iscsiuio/src/unix/nic_utils.c b/iscsiuio/src/unix/nic_utils.c
c487f3
index 0daffd2fcb6d..6e580f862eea 100644
c487f3
--- a/iscsiuio/src/unix/nic_utils.c
c487f3
+++ b/iscsiuio/src/unix/nic_utils.c
c487f3
@@ -81,6 +81,10 @@ static const char iscsi_host_path_netdev_template[] =
c487f3
 	"/sys/class/iscsi_host/host%d/netdev";
c487f3
 static const char cnic_uio_sysfs_resc_template[] =
c487f3
 	"/sys/class/uio/uio%i/device/resource%i";
c487f3
+static const char iscsi_transport_handle_template[] =
c487f3
+	"/sys/class/iscsi_transport/%s/handle";
c487f3
+static const char eth_pfx[] = "eth";
c487f3
+static const char host_pfx[] = "host";
c487f3
 
c487f3
 /**
c487f3
  *  manually_trigger_uio_event() - If the uio file node doesn't exist then
c487f3
@@ -250,6 +254,7 @@ int nic_discover_iscsi_hosts()
c487f3
 				strncpy(nic->eth_device_name, raw, raw_size);
c487f3
 				nic->config_device_name = nic->eth_device_name;
c487f3
 				nic->log_name = nic->eth_device_name;
c487f3
+				nic->host_no = host_no;
c487f3
 
c487f3
 				if (nic_fill_name(nic) != 0) {
c487f3
 					free(nic);
c487f3
@@ -557,6 +562,102 @@ error:
c487f3
 }
c487f3
 
c487f3
 /**
c487f3
+ *  from_uio_find_associated_host() - Given the uio minor number
c487f3
+ *      this function will try to find the assoicated iscsi host
c487f3
+ *  @param uio_minor - minor number of the UIO device
c487f3
+ *  @param name - char buffer which will be filled if successful
c487f3
+ *  @param name_size - size of the name buffer
c487f3
+ *  @return >0 minor number <0 an error
c487f3
+ */
c487f3
+static int from_uio_find_associated_host(nic_t *nic, int uio_minor,
c487f3
+					 char *name, size_t name_size)
c487f3
+{
c487f3
+	char *path;
c487f3
+	int rc;
c487f3
+	int count;
c487f3
+	struct dirent **files;
c487f3
+	char *parsed_name;
c487f3
+	int i;
c487f3
+	int path_iterator;
c487f3
+	char *search_paths[] = { "/sys/class/uio/uio%i/device/" };
c487f3
+	int path_to[] = { 5, 1 };
c487f3
+	int (*search_filters[]) (const struct dirent *) = { filter_host_name, };
c487f3
+	char *(*extract_name[]) (struct dirent **files) = {  extract_none, };
c487f3
+	int extract_name_offset[] = { 0 };
c487f3
+
c487f3
+	path = malloc(PATH_MAX);
c487f3
+	if (!path) {
c487f3
+		LOG_ERR(PFX "Could not allocate memory for path");
c487f3
+		rc = -ENOMEM;
c487f3
+		goto error;
c487f3
+	}
c487f3
+
c487f3
+	for (path_iterator = 0;
c487f3
+	     path_iterator < sizeof(search_paths) / sizeof(search_paths[0]);
c487f3
+	     path_iterator++) {
c487f3
+		/*  Build the path to determine uio name */
c487f3
+		rc = sprintf(path, search_paths[path_iterator], uio_minor);
c487f3
+
c487f3
+		wait_for_file_node_timed(nic, path, path_to[path_iterator]);
c487f3
+
c487f3
+		count = scandir(path, &files,
c487f3
+				search_filters[path_iterator], alphasort);
c487f3
+
c487f3
+		switch (count) {
c487f3
+		case 1:
c487f3
+			parsed_name = (*extract_name[path_iterator]) (files);
c487f3
+			if (!parsed_name) {
c487f3
+				LOG_WARN(PFX "Couldn't find delimiter in: %s",
c487f3
+					 files[0]->d_name);
c487f3
+
c487f3
+				break;
c487f3
+			}
c487f3
+
c487f3
+			strncpy(name,
c487f3
+				parsed_name +
c487f3
+				extract_name_offset[path_iterator], name_size);
c487f3
+
c487f3
+			free(files[0]);
c487f3
+			free(files);
c487f3
+
c487f3
+			rc = 0;
c487f3
+			break;
c487f3
+
c487f3
+		case 0:
c487f3
+			rc = -EINVAL;
c487f3
+			break;
c487f3
+
c487f3
+		case -1:
c487f3
+			LOG_WARN(PFX "Error when scanning path: %s[%s]",
c487f3
+				 path, strerror(errno));
c487f3
+			rc = -EINVAL;
c487f3
+			break;
c487f3
+
c487f3
+		default:
c487f3
+			LOG_WARN(PFX
c487f3
+				 "Too many entries when looking for device: %s",
c487f3
+				 path);
c487f3
+
c487f3
+			/*  Cleanup the scandir() call */
c487f3
+			for (i = 0; i < count; i++)
c487f3
+				free(files[i]);
c487f3
+			free(files);
c487f3
+
c487f3
+			rc = -EINVAL;
c487f3
+			break;
c487f3
+		}
c487f3
+
c487f3
+		if (rc == 0)
c487f3
+			break;
c487f3
+	}
c487f3
+
c487f3
+error:
c487f3
+	free(path);
c487f3
+
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+/**
c487f3
  *  filter_uio_name() - This is the callback used by scandir when looking for
c487f3
  *                      the number of uio entries
c487f3
  */
c487f3
@@ -652,10 +753,16 @@ int from_phys_name_find_assoicated_uio_device(nic_t *nic)
c487f3
 			continue;
c487f3
 		}
c487f3
 
c487f3
-		rc = from_uio_find_associated_eth_device(nic,
c487f3
-							 uio_minor,
c487f3
-							 eth_name,
c487f3
-							 sizeof(eth_name));
c487f3
+		if (!memcmp(host_pfx, nic->config_device_name,
c487f3
+			    strlen(host_pfx))) {
c487f3
+			rc = from_uio_find_associated_host(nic, uio_minor,
c487f3
+							   eth_name,
c487f3
+							   sizeof(eth_name));
c487f3
+		} else {
c487f3
+			rc = from_uio_find_associated_eth_device(nic, uio_minor,
c487f3
+								 eth_name,
c487f3
+								 sizeof(eth_name));
c487f3
+		}
c487f3
 		if (rc != 0) {
c487f3
 			LOG_WARN("uio minor: %d not valid [%D]", uio_minor, rc);
c487f3
 			continue;
c487f3
@@ -1124,6 +1231,38 @@ int detemine_initial_uio_events(nic_t *nic, uint32_t *num_of_events)
c487f3
 
c487f3
 	rc = 0;
c487f3
 error:
c487f3
+	if (raw)
c487f3
+		free(raw);
c487f3
+
c487f3
+	return rc;
c487f3
+}
c487f3
+
c487f3
+int get_iscsi_transport_handle(nic_t *nic, uint64_t *handle)
c487f3
+{
c487f3
+	char *raw = NULL;
c487f3
+	uint32_t raw_size = 0;
c487f3
+	ssize_t elements_read;
c487f3
+	char temp_path[sizeof(iscsi_transport_handle_template) + 8];
c487f3
+	int rc;
c487f3
+
c487f3
+	/*  Capture RX buffer size */
c487f3
+	snprintf(temp_path, sizeof(temp_path),
c487f3
+		 iscsi_transport_handle_template, nic->library_name);
c487f3
+
c487f3
+	rc = capture_file(&raw, &raw_size, temp_path);
c487f3
+	if (rc != 0)
c487f3
+		goto error;
c487f3
+
c487f3
+	elements_read = sscanf(raw, "%lu", handle);
c487f3
+	if (elements_read != 1) {
c487f3
+		LOG_ERR(PFX "%s: Couldn't parse transport handle from %s",
c487f3
+			nic->log_name, temp_path);
c487f3
+		rc = -EIO;
c487f3
+		goto error;
c487f3
+	}
c487f3
+
c487f3
+	rc = 0;
c487f3
+error:
c487f3
 	if (raw != NULL)
c487f3
 		free(raw);
c487f3
 
c487f3
diff --git a/iscsiuio/src/unix/nic_utils.h b/iscsiuio/src/unix/nic_utils.h
c487f3
index d5c1b580f340..e4cf5c13177e 100644
c487f3
--- a/iscsiuio/src/unix/nic_utils.h
c487f3
+++ b/iscsiuio/src/unix/nic_utils.h
c487f3
@@ -99,4 +99,6 @@ void dump_packet_to_log(struct nic_interface *iface,
c487f3
 int determine_file_size_read(const char *filepath);
c487f3
 int capture_file(char **raw, uint32_t *raw_size, const char *path);
c487f3
 
c487f3
+int get_iscsi_transport_handle(nic_t *nic, uint64_t *handle);
c487f3
+
c487f3
 #endif /* __NIC_UTILS_H__ */
c487f3
diff --git a/iscsiuio/src/unix/options.h b/iscsiuio/src/unix/options.h
c487f3
index df03255f7a87..63b86357aafa 100644
c487f3
--- a/iscsiuio/src/unix/options.h
c487f3
+++ b/iscsiuio/src/unix/options.h
c487f3
@@ -87,6 +87,7 @@
c487f3
 
c487f3
 #define INVALID_FD	-1
c487f3
 #define INVALID_THREAD	-1
c487f3
+#define INVALID_HOST_NO	-1
c487f3
 
c487f3
 struct options {
c487f3
 	char debug;
c487f3
-- 
c487f3
2.9.3
c487f3