11f069b
diff --git a/include/fw_context.h b/include/fw_context.h
11f069b
index abdff42..770b41a 100644
11f069b
--- a/include/fw_context.h
11f069b
+++ b/include/fw_context.h
11f069b
@@ -54,6 +54,8 @@ struct boot_context {
11f069b
 	char mask[18];
11f069b
 	char lun[17];
11f069b
 	char vlan[15];
11f069b
+
11f069b
+	char scsi_host_name[64];
11f069b
 };
11f069b
 
11f069b
 extern int fw_get_entry(struct boot_context *context);
11f069b
diff --git a/usr/iface.c b/usr/iface.c
11f069b
index 27b59d0..9c74117 100644
11f069b
--- a/usr/iface.c
11f069b
+++ b/usr/iface.c
11f069b
@@ -778,31 +778,62 @@ void iface_link_ifaces(struct list_head *ifaces)
11f069b
 	iface_for_each_iface(ifaces, 1, &nr_found, iface_link);
11f069b
 }
11f069b
 
11f069b
-void iface_setup_from_boot_context(struct iface_rec *iface,
11f069b
+/**
11f069b
+ * iface_setup_from_boot_context - setup iface from boot context info
11f069b
+ * @iface: iface t setup
11f069b
+ * @context: boot context info
11f069b
+ *
11f069b
+ * Returns 1 if setup for offload.
11f069b
+ */
11f069b
+int iface_setup_from_boot_context(struct iface_rec *iface,
11f069b
 				   struct boot_context *context)
11f069b
 {
11f069b
 	if (strlen(context->initiatorname))
11f069b
 		strlcpy(iface->iname, context->initiatorname,
11f069b
 			sizeof(iface->iname));
11f069b
 
11f069b
-	if (strlen(context->iface)) {
11f069b
-		if (!net_get_transport_name_from_netdev(context->iface,
11f069b
-						iface->transport_name)) {
11f069b
-			/* set up for access through offload card */
11f069b
-			memset(iface->name, 0, sizeof(iface->name));
11f069b
-			snprintf(iface->name, sizeof(iface->name),
11f069b
-				 "%s.%s", iface->transport_name,
11f069b
-				 context->mac);
11f069b
-
11f069b
-			strlcpy(iface->netdev, context->iface,
11f069b
-				sizeof(iface->netdev));
11f069b
-			strlcpy(iface->hwaddress, context->mac,
11f069b
-				sizeof(iface->hwaddress));
11f069b
-			strlcpy(iface->ipaddress, context->ipaddr,
11f069b
-				sizeof(iface->ipaddress));
11f069b
+	if (strlen(context->scsi_host_name)) {
11f069b
+		struct iscsi_transport *t;
11f069b
+		uint32_t hostno;
11f069b
+
11f069b
+		if (sscanf(context->scsi_host_name, "iscsi_host%u", &hostno) != 		    1) {
11f069b
+			log_error("Could not parse %s's host no.",
11f069b
+				  context->scsi_host_name);
11f069b
+			return 0;
11f069b
 		}
11f069b
-	}
11f069b
+		t = iscsi_sysfs_get_transport_by_hba(hostno);
11f069b
+		if (!t) {
11f069b
+			log_error("Could not get transport for %s. "
11f069b
+				  "Make sure the iSCSI driver is loaded.",
11f069b
+				  context->scsi_host_name);
11f069b
+			return 0;
11f069b
+		}
11f069b
+
11f069b
+		log_debug(3, "boot context has %s transport %s",
11f069b
+			  context->scsi_host_name, t->name);
11f069b
+		strcpy(iface->transport_name, t->name);
11f069b
+	} else if (strlen(context->iface) &&
11f069b
+		 (!net_get_transport_name_from_netdev(context->iface,
11f069b
+						iface->transport_name))) {
11f069b
+		log_debug(3, "boot context has netdev %s",
11f069b
+			  context->iface);
11f069b
+		strlcpy(iface->netdev, context->iface,
11f069b
+			sizeof(iface->netdev));
11f069b
+	} else
11f069b
+		return 0;
11f069b
+	/*
11f069b
+	 * set up for access through a offload card.
11f069b
+	 */
11f069b
+	memset(iface->name, 0, sizeof(iface->name));
11f069b
+	snprintf(iface->name, sizeof(iface->name), "%s.%s",
11f069b
+		 iface->transport_name, context->mac);
11f069b
+
11f069b
+	strlcpy(iface->hwaddress, context->mac,
11f069b
+		sizeof(iface->hwaddress));
11f069b
+	strlcpy(iface->ipaddress, context->ipaddr,
11f069b
+		sizeof(iface->ipaddress));
11f069b
 	log_debug(1, "iface " iface_fmt "\n", iface_str(iface));
11f069b
+	return 1;
11f069b
 }
11f069b
 
11f069b
 /**
11f069b
@@ -817,32 +848,24 @@ void iface_setup_from_boot_context(struct iface_rec *iface,
11f069b
 int iface_create_ifaces_from_boot_contexts(struct list_head *ifaces,
11f069b
 					   struct list_head *targets)
11f069b
 {
11f069b
-	char transport_name[ISCSI_TRANSPORT_NAME_MAXLEN];
11f069b
-	char iface_name[ISCSI_MAX_IFACE_LEN];
11f069b
 	struct boot_context *context;
11f069b
 	struct iface_rec *iface, *tmp_iface;
11f069b
 	int rc = 0;
11f069b
 
11f069b
 	list_for_each_entry(context, targets, list) {
11f069b
-		memset(transport_name, 0, ISCSI_TRANSPORT_NAME_MAXLEN);
11f069b
-
11f069b
-		if (net_get_transport_name_from_netdev(context->iface,
11f069b
-						       transport_name))
11f069b
-			continue;
11f069b
-
11f069b
-		/* offload + ibft support */
11f069b
-		memset(iface_name, 0, ISCSI_MAX_IFACE_LEN);
11f069b
-		snprintf(iface_name, ISCSI_MAX_IFACE_LEN,
11f069b
-			 "%s.%s", transport_name, context->mac);
11f069b
-
11f069b
 		rc = 0;
11f069b
-		iface = iface_alloc(iface_name, &rc);
11f069b
+		/* use dummy name. If valid it will get overwritten below */
11f069b
+		iface = iface_alloc(DEFAULT_IFACENAME, &rc);
11f069b
 		if (!iface) {
11f069b
 			log_error("Could not setup iface %s for boot\n",
11f069b
-				  iface_name);
11f069b
+				  context->iface);
11f069b
 			goto fail;
11f069b
 		}
11f069b
-		iface_setup_from_boot_context(iface, context);
11f069b
+		if (!iface_setup_from_boot_context(iface, context)) {
11f069b
+			/* no offload so forget it */
11f069b
+			free(iface);
11f069b
+			continue;
11f069b
+		}
11f069b
 
11f069b
 		rc = iface_conf_write(iface);
11f069b
 		if (rc) {
11f069b
diff --git a/usr/iface.h b/usr/iface.h
11f069b
index f948686..9f6d47e 100644
11f069b
--- a/usr/iface.h
11f069b
+++ b/usr/iface.h
11f069b
@@ -50,7 +50,7 @@ extern int iface_conf_write(struct iface_rec *iface);
11f069b
 extern int iface_conf_delete(struct iface_rec *iface);
11f069b
 extern int iface_is_valid(struct iface_rec *iface);
11f069b
 extern void iface_link_ifaces(struct list_head *ifaces);
11f069b
-extern void iface_setup_from_boot_context(struct iface_rec *iface,
11f069b
+extern int iface_setup_from_boot_context(struct iface_rec *iface,
11f069b
                                    struct boot_context *context);
11f069b
 extern int iface_create_ifaces_from_boot_contexts(struct list_head *ifaces,
11f069b
 						  struct list_head *targets);
11f069b
diff --git a/utils/fwparam_ibft/Makefile b/utils/fwparam_ibft/Makefile
11f069b
index b9e7988..ca07b76 100644
11f069b
--- a/utils/fwparam_ibft/Makefile
11f069b
+++ b/utils/fwparam_ibft/Makefile
11f069b
@@ -22,7 +22,7 @@
11f069b
 #
11f069b
 
11f069b
 SYSDEPS_OBJS = $(wildcard ../sysdeps/*.o)
11f069b
-OBJS := fw_entry.o fwparam_ibft_sysfs.o $(SYSDEPS_OBJS) ../../usr/iscsi_net_util.o
11f069b
+OBJS := fw_entry.o fwparam_sysfs.o $(SYSDEPS_OBJS) ../../usr/iscsi_net_util.o
11f069b
 OBJS += prom_lex.o prom_parse.tab.o fwparam_ppc.o
11f069b
 CLEANFILES = $(OBJS) *.output *~
11f069b
 
11f069b
diff --git a/utils/fwparam_ibft/fw_entry.c b/utils/fwparam_ibft/fw_entry.c
11f069b
index ae5d34a..bbdb6d2 100644
11f069b
--- a/utils/fwparam_ibft/fw_entry.c
11f069b
+++ b/utils/fwparam_ibft/fw_entry.c
11f069b
@@ -102,8 +102,7 @@ int fw_get_entry(struct boot_context *context)
11f069b
 
11f069b
 	ret = fwparam_ppc_boot_info(context);
11f069b
 	if (ret)
11f069b
-		ret = fwparam_ibft_sysfs_boot_info(context);
11f069b
-
11f069b
+		ret = fwparam_sysfs_boot_info(context);
11f069b
 	return ret;
11f069b
 }
11f069b
 
11f069b
@@ -124,8 +123,7 @@ int fw_get_targets(struct list_head *list)
11f069b
 
11f069b
 	ret = fwparam_ppc_get_targets(list);
11f069b
 	if (ret)
11f069b
-		ret = fwparam_ibft_sysfs_get_targets(list);
11f069b
-
11f069b
+		ret = fwparam_sysfs_get_targets(list);
11f069b
 	return ret;
11f069b
 }
11f069b
 
11f069b
diff --git a/utils/fwparam_ibft/fwparam.h b/utils/fwparam_ibft/fwparam.h
11f069b
index a79213b..32e4961 100644
11f069b
--- a/utils/fwparam_ibft/fwparam.h
11f069b
+++ b/utils/fwparam_ibft/fwparam.h
11f069b
@@ -24,8 +24,8 @@
11f069b
 
11f069b
 struct boot_context;
11f069b
 
11f069b
-int fwparam_ibft_sysfs_boot_info(struct boot_context *context);
11f069b
-int fwparam_ibft_sysfs_get_targets(struct list_head *list);
11f069b
+int fwparam_sysfs_boot_info(struct boot_context *context);
11f069b
+int fwparam_sysfs_get_targets(struct list_head *list);
11f069b
 int fwparam_ppc_boot_info(struct boot_context *context);
11f069b
 int fwparam_ppc_get_targets(struct list_head *list);
11f069b
 
11f069b
diff --git a/utils/fwparam_ibft/fwparam_sysfs.c b/utils/fwparam_ibft/fwparam_sysfs.c
11f069b
new file mode 100644
11f069b
index 0000000..9b73d1a
11f069b
--- /dev/null
11f069b
+++ b/utils/fwparam_ibft/fwparam_sysfs.c
11f069b
@@ -0,0 +1,484 @@
11f069b
+/*
11f069b
+ * Copyright (C) IBM Corporation. 2007
11f069b
+ * Author: Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
11f069b
+ * Copyright (C) Red Hat, Inc.  All rights reserved. 2008 - 2010
11f069b
+ * Copyright (C) Mike Christie 2008 - 2010
11f069b
+ *
11f069b
+ * This program is free software: you can redistribute it and/or modify
11f069b
+ * it under the terms of the GNU General Public License as published by
11f069b
+ * the Free Software Foundation, either version 2 of the License, or
11f069b
+ * (at your option) any later version.
11f069b
+ *
11f069b
+ * This program is distributed in the hope that it will be useful,
11f069b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f069b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11f069b
+ * GNU General Public License for more details.
11f069b
+ *
11f069b
+ * You should have received a copy of the GNU General Public License
11f069b
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11f069b
+ */
11f069b
+
11f069b
+#define  _XOPEN_SOURCE 500
11f069b
+#define _SVID_SOURCE
11f069b
+#include <ftw.h>
11f069b
+#include <stdio.h>
11f069b
+#include <stdlib.h>
11f069b
+#include <string.h>
11f069b
+#include <unistd.h>
11f069b
+#include <fcntl.h>
11f069b
+#include <errno.h>
11f069b
+#include <dirent.h>
11f069b
+#include <sys/types.h>
11f069b
+#include <sys/stat.h>
11f069b
+
11f069b
+#include "sysfs.h"
11f069b
+#include "fw_context.h"
11f069b
+#include "fwparam.h"
11f069b
+#include "sysdeps.h"
11f069b
+#include "iscsi_net_util.h"
11f069b
+
11f069b
+#define ISCSI_BOOT_MAX		255
11f069b
+#define IBFT_SYSFS_ROOT		"/sys/firmware/ibft/"
11f069b
+#define IBFT_SUBSYS		"ibft"
11f069b
+
11f069b
+#define ISCSI_LLD_ROOT		"/sys/firmware/"
11f069b
+#define ISCSI_LLD_SUBSYS_PREFIX	"iscsi_host"
11f069b
+
11f069b
+static char *target_list[ISCSI_BOOT_MAX];
11f069b
+static char *nic_list[ISCSI_BOOT_MAX];
11f069b
+static int nic_cnt;
11f069b
+static int tgt_cnt;
11f069b
+
11f069b
+static int file_exist(const char *file)
11f069b
+{
11f069b
+	struct stat bootpath_stat;
11f069b
+
11f069b
+	return !stat(file, &bootpath_stat);
11f069b
+}
11f069b
+
11f069b
+/*
11f069b
+ * Finds the etherrnetX and targetX under the sysfs directory.
11f069b
+ */
11f069b
+static int find_sysfs_dirs(const char *fpath, const struct stat *sb,
11f069b
+			   int tflag, struct FTW *ftw)
11f069b
+{
11f069b
+	if (tflag == FTW_D && (strstr(fpath + ftw->base, "target"))) {
11f069b
+		if (tgt_cnt == ISCSI_BOOT_MAX) {
11f069b
+			printf("Too many targets found in iSCSI boot data."
11f069b
+			       "Max number of targets %d\n", ISCSI_BOOT_MAX);
11f069b
+			return 0;
11f069b
+		}
11f069b
+		target_list[tgt_cnt++] = strdup(strstr(fpath, "target"));
11f069b
+	}
11f069b
+
11f069b
+	if (tflag == FTW_D && (strstr(fpath + ftw->base, "ethernet"))) {
11f069b
+		if (nic_cnt == ISCSI_BOOT_MAX) {
11f069b
+			printf("Too many nics found in iSCSI boot data."
11f069b
+			       "Max number of nics %d\n", ISCSI_BOOT_MAX);
11f069b
+			return 0;
11f069b
+		}
11f069b
+		nic_list[nic_cnt++] = strdup(strstr(fpath, "ethernet"));
11f069b
+	}
11f069b
+
11f069b
+	return 0;
11f069b
+}
11f069b
+ 
11f069b
+static int get_iface_from_device(char *id, struct boot_context *context)
11f069b
+{
11f069b
+	char dev_dir[FILENAMESZ];
11f069b
+	int rc = ENODEV;
11f069b
+	DIR *dirfd;
11f069b
+	struct dirent *dent;
11f069b
+
11f069b
+	memset(dev_dir, 0, FILENAMESZ);
11f069b
+	snprintf(dev_dir, FILENAMESZ, IBFT_SYSFS_ROOT"/%s/device", id);
11f069b
+
11f069b
+	if (!file_exist(dev_dir))
11f069b
+		return 0;
11f069b
+
11f069b
+	dirfd = opendir(dev_dir);
11f069b
+	if (!dirfd)
11f069b
+		return errno;
11f069b
+
11f069b
+	while ((dent = readdir(dirfd))) {
11f069b
+		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..") ||
11f069b
+		    strncmp(dent->d_name, "net:", 4))
11f069b
+			continue;
11f069b
+
11f069b
+		if (!strncmp(dent->d_name, "net:", 4)) {
11f069b
+			if ((strlen(dent->d_name) - 4) >
11f069b
+			    (sizeof(context->iface) - 1)) {
11f069b
+				rc = EINVAL;
11f069b
+				printf("Net device %s too big for iface "
11f069b
+				       "buffer.\n", dent->d_name);
11f069b
+				break;
11f069b
+			}
11f069b
+
11f069b
+			if (sscanf(dent->d_name, "net:%s", context->iface) != 1)
11f069b
+				rc = EINVAL;
11f069b
+			rc = 0;
11f069b
+			break;
11f069b
+		} else {
11f069b
+			printf("Could not read ethernet to net link.\n");
11f069b
+			rc = EOPNOTSUPP;
11f069b
+			break;
11f069b
+		}
11f069b
+	}
11f069b
+
11f069b
+	closedir(dirfd);
11f069b
+
11f069b
+	if (rc != ENODEV)
11f069b
+		return rc;
11f069b
+
11f069b
+	/* If not found try again with newer kernel networkdev sysfs layout */
11f069b
+	strlcat(dev_dir, "/net", FILENAMESZ);
11f069b
+
11f069b
+	if (!file_exist(dev_dir))
11f069b
+		return rc;
11f069b
+
11f069b
+	dirfd = opendir(dev_dir);
11f069b
+	if (!dirfd)
11f069b
+		return errno;
11f069b
+
11f069b
+	while ((dent = readdir(dirfd))) {
11f069b
+		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
11f069b
+			continue;
11f069b
+
11f069b
+		/* Take the first "regular" directory entry */
11f069b
+		if (strlen(dent->d_name) > (sizeof(context->iface) - 1)) {
11f069b
+			rc = EINVAL;
11f069b
+			printf("Net device %s too big for iface buffer.\n",
11f069b
+			       dent->d_name);
11f069b
+			break;
11f069b
+		}
11f069b
+
11f069b
+		strcpy(context->iface, dent->d_name);
11f069b
+		rc = 0;
11f069b
+		break;
11f069b
+	}
11f069b
+
11f069b
+	closedir(dirfd);
11f069b
+	return rc;
11f069b
+}
11f069b
+
11f069b
+/*
11f069b
+ * Routines to fill in the context values.
11f069b
+ */
11f069b
+static int fill_nic_context(char *subsys, char *id,
11f069b
+			    struct boot_context *context)
11f069b
+{
11f069b
+	int rc;
11f069b
+
11f069b
+	rc = sysfs_get_str(id, subsys, "mac", context->mac,
11f069b
+			   sizeof(context->mac));
11f069b
+	if (rc)
11f069b
+		return rc;
11f069b
+
11f069b
+	/*
11f069b
+	 * Some offload cards like bnx2i use different MACs for the net and
11f069b
+	 * iscsi functions, so we have to follow the sysfs links.
11f069b
+	 *
11f069b
+	 * Other ibft implementations may not be tied to a pci function,
11f069b
+	 * so there will not be any device/net link, so we drop down to
11f069b
+	 * the MAC matching.
11f069b
+	 *
11f069b
+	 * And finally, some cards like be2iscsi and qla4xxx do not have
11f069b
+	 * any linux network subsys representation. These hosts will
11f069b
+	 * not have the ibft subsys. Instead the subsys is the scsi host
11f069b
+	 * number.
11f069b
+	 */
11f069b
+	if (!strcmp(IBFT_SUBSYS, subsys)) {
11f069b
+		rc = get_iface_from_device(id, context);
11f069b
+		if (rc) {
11f069b
+			rc = net_get_netdev_from_hwaddress(context->mac,
11f069b
+							   context->iface);
11f069b
+			if (rc)
11f069b
+				return rc;
11f069b
+		}
11f069b
+	} else
11f069b
+		strlcpy(context->scsi_host_name, subsys,
11f069b
+			sizeof(context->scsi_host_name));
11f069b
+
11f069b
+	sysfs_get_str(id, subsys, "ip-addr", context->ipaddr,
11f069b
+		      sizeof(context->ipaddr));
11f069b
+	sysfs_get_str(id, subsys, "vlan", context->vlan,
11f069b
+		      sizeof(context->vlan));
11f069b
+	sysfs_get_str(id, subsys, "subnet-mask", context->mask,
11f069b
+		      sizeof(context->mask));
11f069b
+	sysfs_get_str(id, subsys, "gateway", context->gateway,
11f069b
+		      sizeof(context->gateway));
11f069b
+	sysfs_get_str(id, subsys, "primary-dns", context->primary_dns,
11f069b
+		      sizeof(context->primary_dns));
11f069b
+	sysfs_get_str(id, subsys, "secondary-dns", context->secondary_dns,
11f069b
+		      sizeof(context->secondary_dns));
11f069b
+	sysfs_get_str(id, subsys, "dhcp", context->dhcp,
11f069b
+		      sizeof(context->dhcp));
11f069b
+	return 0;
11f069b
+}
11f069b
+
11f069b
+static void fill_initiator_context(char *subsys, struct boot_context *context)
11f069b
+{
11f069b
+	sysfs_get_str("initiator", subsys, "initiator-name",
11f069b
+		      context->initiatorname,
11f069b
+		      sizeof(context->initiatorname));
11f069b
+	sysfs_get_str("initiator", subsys, "isid", context->isid,
11f069b
+		      sizeof(context->isid));
11f069b
+}
11f069b
+static int fill_tgt_context(char *subsys, char *id,
11f069b
+			    struct boot_context *context)
11f069b
+{
11f069b
+	int rc;
11f069b
+
11f069b
+	rc = sysfs_get_str(id, subsys, "target-name", context->targetname,
11f069b
+			   sizeof(context->targetname));
11f069b
+	if (rc)
11f069b
+		return rc;
11f069b
+
11f069b
+	rc = sysfs_get_str(id, subsys, "ip-addr", context->target_ipaddr,
11f069b
+			   sizeof(context->target_ipaddr));
11f069b
+	if (rc)
11f069b
+		return rc;
11f069b
+
11f069b
+	/*
11f069b
+	 * We can live without the rest of they do not exist. If we
11f069b
+	 * failed to get them we will figure it out when we login.
11f069b
+	 */
11f069b
+	if (sysfs_get_int(id, subsys, "port", &context->target_port))
11f069b
+		context->target_port = ISCSI_LISTEN_PORT;
11f069b
+
11f069b
+	sysfs_get_str(id, subsys, "lun", context->lun,
11f069b
+		      sizeof(context->lun));
11f069b
+	sysfs_get_str(id, subsys, "chap-name", context->chap_name,
11f069b
+		      sizeof(context->chap_name));
11f069b
+	sysfs_get_str(id, subsys, "chap-secret", context->chap_password,
11f069b
+		      sizeof(context->chap_password));
11f069b
+	sysfs_get_str(id, subsys, "rev-chap-name", context->chap_name_in,
11f069b
+		      sizeof(context->chap_name_in));
11f069b
+	sysfs_get_str(id, subsys, "rev-chap-name-secret",
11f069b
+		      context->chap_password_in,
11f069b
+		      sizeof(context->chap_password_in));
11f069b
+	return 0;
11f069b
+}
11f069b
+
11f069b
+#define IBFT_SYSFS_FLAG_FW_SEL_BOOT 2
11f069b
+
11f069b
+static int find_boot_flag(char *subsys, char *list[], ssize_t size,
11f069b
+			  int *boot_idx)
11f069b
+{
11f069b
+	int rc = ENODEV;
11f069b
+	int i, flag = 0;
11f069b
+
11f069b
+	for (i = 0; i < size; i++, flag = -1) {
11f069b
+		rc = sysfs_get_int(list[i], subsys, "flags", &flag;;
11f069b
+		if (rc)
11f069b
+			continue;
11f069b
+
11f069b
+		if (flag & IBFT_SYSFS_FLAG_FW_SEL_BOOT) {
11f069b
+			*boot_idx = i;
11f069b
+			rc = 0;
11f069b
+			break;
11f069b
+		}
11f069b
+		rc = ENODEV;
11f069b
+		flag = 0;
11f069b
+
11f069b
+	}
11f069b
+
11f069b
+	return rc;
11f069b
+}
11f069b
+
11f069b
+static void deallocate_lists(void)
11f069b
+{
11f069b
+	int i;
11f069b
+
11f069b
+	for (i = 0; i < nic_cnt; i++)
11f069b
+		free(nic_list[i]);
11f069b
+
11f069b
+	nic_cnt = 0;
11f069b
+	for (i = 0; i < tgt_cnt; i++)
11f069b
+		free(target_list[i]);
11f069b
+
11f069b
+	tgt_cnt = 0;
11f069b
+
11f069b
+}
11f069b
+
11f069b
+static int get_boot_info(struct boot_context *context, char *rootdir,
11f069b
+			 char *subsys)
11f069b
+{
11f069b
+	char initiator_dir[FILENAMESZ];
11f069b
+	int rc = ENODEV;
11f069b
+	int nic_idx = -1, tgt_idx = -1;
11f069b
+
11f069b
+	memset(&initiator_dir, 0 , FILENAMESZ);
11f069b
+	snprintf(initiator_dir, FILENAMESZ, "%sinitiator", rootdir);
11f069b
+
11f069b
+	nic_cnt = 0;
11f069b
+	tgt_cnt = 0;
11f069b
+	if (file_exist(initiator_dir)) {
11f069b
+		/* Find the target's and the ethernet's */
11f069b
+		rc = nftw(rootdir, find_sysfs_dirs, 20, 1);
11f069b
+
11f069b
+		/* Find wihch target and which ethernet have
11f069b
+		the boot flag set. */
11f069b
+		rc = find_boot_flag(subsys, nic_list, nic_cnt, &nic_idx);
11f069b
+		if (rc)
11f069b
+			goto free;
11f069b
+
11f069b
+		rc = find_boot_flag(subsys, target_list, tgt_cnt, &tgt_idx);
11f069b
+		if (rc)
11f069b
+			goto free;
11f069b
+
11f069b
+		/* Fill in the context values */
11f069b
+		rc = fill_nic_context(subsys, nic_list[nic_idx], context);
11f069b
+		rc |= fill_tgt_context(subsys, target_list[tgt_idx], context);
11f069b
+		fill_initiator_context(subsys, context);
11f069b
+	}
11f069b
+free:
11f069b
+	deallocate_lists();
11f069b
+	return rc;
11f069b
+}
11f069b
+
11f069b
+int fwparam_sysfs_boot_info(struct boot_context *context)
11f069b
+{
11f069b
+	struct dirent *dent;
11f069b
+	DIR *dirfd;
11f069b
+	int rc = 0;
11f069b
+
11f069b
+	if (!get_boot_info(context, IBFT_SYSFS_ROOT, IBFT_SUBSYS))
11f069b
+		return 0;
11f069b
+	/*
11f069b
+	 * We could have multiple iscsi llds and each lld could have
11f069b
+	 * multiple targets/ethernet ports
11f069b
+	 */
11f069b
+	dirfd = opendir(ISCSI_LLD_ROOT);
11f069b
+	if (!dirfd)
11f069b
+		return errno;
11f069b
+
11f069b
+	while ((dent = readdir(dirfd))) {
11f069b
+		char lld_root[FILENAMESZ];
11f069b
+
11f069b
+		memset(&lld_root, 0 , FILENAMESZ);
11f069b
+
11f069b
+		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
11f069b
+			continue;
11f069b
+
11f069b
+		if (strncmp(dent->d_name, ISCSI_LLD_SUBSYS_PREFIX, 10))
11f069b
+			continue;
11f069b
+
11f069b
+		snprintf(lld_root, FILENAMESZ, ISCSI_LLD_ROOT"%s",
11f069b
+			 dent->d_name);
11f069b
+		if (!get_boot_info(context, lld_root, dent->d_name))
11f069b
+			goto done;
11f069b
+	}
11f069b
+	rc = ENODEV;
11f069b
+done:
11f069b
+	closedir(dirfd);
11f069b
+	return rc;
11f069b
+}
11f069b
+
11f069b
+static int get_targets(struct list_head *list, char *rootdir, char *subsys)
11f069b
+{
11f069b
+	struct boot_context *context;
11f069b
+	int rc = 0, i, nic_idx, nic;
11f069b
+	char initiator_dir[FILENAMESZ];
11f069b
+
11f069b
+	memset(&initiator_dir, 0 , FILENAMESZ);
11f069b
+	snprintf(initiator_dir, FILENAMESZ, "%sinitiator", rootdir);
11f069b
+
11f069b
+	if (!file_exist(initiator_dir))
11f069b
+		return ENODEV;
11f069b
+
11f069b
+	nic_cnt = 0;
11f069b
+	tgt_cnt = 0;
11f069b
+
11f069b
+	/* Find the target's and the ethernet's */
11f069b
+	nftw(rootdir, find_sysfs_dirs, 20, 1);
11f069b
+	for (i = 0; i < tgt_cnt; i++) {
11f069b
+		context = calloc(1, sizeof(*context));
11f069b
+		if (!context) {
11f069b
+			rc = ENOMEM;
11f069b
+			break;
11f069b
+		}
11f069b
+
11f069b
+		rc = fill_tgt_context(subsys, target_list[i], context);
11f069b
+		if (rc)
11f069b
+			break;
11f069b
+
11f069b
+		rc = sysfs_get_int(target_list[i], subsys, "nic-assoc",
11f069b
+				   &nic_idx);
11f069b
+		if (rc)
11f069b
+			break;
11f069b
+
11f069b
+		for (nic = 0; nic < nic_cnt; nic++) {
11f069b
+			int id;
11f069b
+
11f069b
+			rc = sysfs_get_int(nic_list[nic], subsys, "index",
11f069b
+					   &id;;
11f069b
+			if (!rc && (id == nic_idx))
11f069b
+				break;
11f069b
+		}
11f069b
+
11f069b
+		if (nic == nic_cnt) {
11f069b
+			printf("Invalid nic-assoc of %d. Max id %d.\n",
11f069b
+			       nic_idx, nic_cnt);
11f069b
+			break;
11f069b
+		}
11f069b
+
11f069b
+		rc = fill_nic_context(subsys, nic_list[nic], context);
11f069b
+		if (rc)
11f069b
+			break;
11f069b
+
11f069b
+		fill_initiator_context(subsys, context);
11f069b
+		list_add_tail(&context->list, list);
11f069b
+	}
11f069b
+
11f069b
+	if (rc) {
11f069b
+		if (context)
11f069b
+			free(context);
11f069b
+		fw_free_targets(list);
11f069b
+	}
11f069b
+
11f069b
+	deallocate_lists();
11f069b
+	return rc;
11f069b
+}
11f069b
+
11f069b
+int fwparam_sysfs_get_targets(struct list_head *list)
11f069b
+{
11f069b
+	struct dirent *dent;
11f069b
+	DIR *dirfd;
11f069b
+	int rc = 0;
11f069b
+
11f069b
+	/* ibft only has one instance */
11f069b
+	get_targets(list, IBFT_SYSFS_ROOT, IBFT_SUBSYS);
11f069b
+	/*
11f069b
+	 * We could have multiple iscsi llds and each lld could have
11f069b
+	 * multiple targets/ethernet ports
11f069b
+	 */
11f069b
+	dirfd = opendir(ISCSI_LLD_ROOT);
11f069b
+	if (!dirfd) {
11f069b
+		rc = errno;
11f069b
+		goto done;
11f069b
+	}
11f069b
+
11f069b
+	while ((dent = readdir(dirfd))) {
11f069b
+		char lld_root[FILENAMESZ];
11f069b
+
11f069b
+		memset(&lld_root, 0 , FILENAMESZ);
11f069b
+
11f069b
+		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
11f069b
+			continue;
11f069b
+
11f069b
+		if (strncmp(dent->d_name, ISCSI_LLD_SUBSYS_PREFIX, 10))
11f069b
+			continue;
11f069b
+
11f069b
+		snprintf(lld_root, FILENAMESZ, ISCSI_LLD_ROOT"%s",
11f069b
+			 dent->d_name);
11f069b
+		get_targets(list, lld_root, dent->d_name);
11f069b
+	}
11f069b
+	closedir(dirfd);
11f069b
+done:
11f069b
+	if (!rc && list_empty(list))
11f069b
+		rc = ENODEV;
11f069b
+	if (rc)
11f069b
+		fw_free_targets(list);
11f069b
+	return rc;
11f069b
+}
11f069b
-- 
11f069b
1.6.6.1
11f069b