Blame SOURCES/libhbalinux-fix-non-pci-netdev.patch

21a8dc
diff --git a/Makefile.am b/Makefile.am
21a8dc
index 1349e7b..068dc10 100644
21a8dc
--- a/Makefile.am
21a8dc
+++ b/Makefile.am
21a8dc
@@ -1,12 +1,12 @@
21a8dc
-AM_CFLAGS = $(HBAAPI_CFLAGS) $(PCIACCESS_CFLAGS)
21a8dc
-AM_LDFLAGS= $(PCIACCESS_LIBS)
21a8dc
+AM_CFLAGS = $(HBAAPI_CFLAGS) $(PCIACCESS_CFLAGS) $(LIBUDEV_CFLAGS)
21a8dc
+AM_LDFLAGS= $(PCIACCESS_LIBS) $(LIBUDEV_LIBS)
21a8dc
 
21a8dc
 lib_LTLIBRARIES = libhbalinux.la
21a8dc
 libhbalinux_la_SOURCES = adapt.c adapt_impl.h api_lib.h bind.c bind_impl.h \
21a8dc
 fc_scsi.h fc_types.h lib.c lport.c net_types.h pci.c rport.c scsi.c sg.c \
21a8dc
 utils.c utils.h
21a8dc
 libhbalinux_la_LDFLAGS = -version-info 2:2:0
21a8dc
-libhbalinux_la_LIBADD = $(PCIACCESS_LIBS)
21a8dc
+libhbalinux_la_LIBADD = $(PCIACCESS_LIBS) $(LIBUDEV_LIBS)
21a8dc
 
21a8dc
 pkgconfigdir = $(libdir)/pkgconfig
21a8dc
 pkgconfig_DATA = libhbalinux.pc
21a8dc
diff --git a/adapt_impl.h b/adapt_impl.h
21a8dc
index d86c2f8..9d9a347 100644
21a8dc
--- a/adapt_impl.h
21a8dc
+++ b/adapt_impl.h
21a8dc
@@ -151,7 +151,7 @@ HBA_STATUS scsi_report_luns_v2(HBA_HANDLE, HBA_WWN, HBA_WWN,
21a8dc
 HBA_STATUS sg_issue_inquiry(const char *, HBA_UINT8, HBA_UINT8,
21a8dc
 		void *, HBA_UINT32 *, HBA_UINT8 *, void *, HBA_UINT32 *);
21a8dc
 
21a8dc
-void adapter_init(void);
21a8dc
+int adapter_init(void);
21a8dc
 void adapter_shutdown(void);
21a8dc
 
21a8dc
 /* struct port_stats; */
21a8dc
diff --git a/configure.ac b/configure.ac
21a8dc
index 8dadf29..758cadb 100644
21a8dc
--- a/configure.ac
21a8dc
+++ b/configure.ac
21a8dc
@@ -13,6 +13,10 @@ AC_SUBST(PCIACCESS_LIBS)
21a8dc
 PKG_CHECK_MODULES(HBAAPI, HBAAPI)
21a8dc
 AC_SUBST(HBAAPI_CFLAGS)
21a8dc
 
21a8dc
+PKG_CHECK_MODULES(LIBUDEV, libudev)
21a8dc
+AC_SUBST(LIBUDEV_CFLAGS)
21a8dc
+AC_SUBST(LIBUDEV_LIBS)
21a8dc
+
21a8dc
 AC_CONFIG_FILES([Makefile libhbalinux.spec libhbalinux.pc])
21a8dc
 AC_OUTPUT
21a8dc
 
21a8dc
diff --git a/libhbalinux.spec.in b/libhbalinux.spec.in
21a8dc
index 344c166..4690da9 100644
21a8dc
--- a/libhbalinux.spec.in
21a8dc
+++ b/libhbalinux.spec.in
21a8dc
@@ -9,7 +9,7 @@ URL:            http://www.open-fcoe.org
21a8dc
 Source0:        http://www.open-fcoe.org/openfc/%{name}-%{version}.tar.gz
21a8dc
 BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
21a8dc
 
21a8dc
-BuildRequires:  libHBAAPI-devel libpciaccess-devel
21a8dc
+BuildRequires:  libHBAAPI-devel libpciaccess-devel systemd-devel
21a8dc
 Requires:       libHBAAPI
21a8dc
 
21a8dc
 %description
21a8dc
diff --git a/lport.c b/lport.c
21a8dc
index bc34078..695bdc6 100644
21a8dc
--- a/lport.c
21a8dc
+++ b/lport.c
21a8dc
@@ -20,6 +20,8 @@
21a8dc
 #include "api_lib.h"
21a8dc
 #include "adapt_impl.h"
21a8dc
 
21a8dc
+#include <libudev.h>
21a8dc
+
21a8dc
 #ifndef HBA_STATUS_ERROR_ILLEGAL_FCID
21a8dc
 #define HBA_STATUS_ERROR_ILLEGAL_FCID 33	/* defined after HBA-API 2.2 */
21a8dc
 #endif
21a8dc
@@ -88,18 +90,16 @@ struct sa_nameval port_speeds_table[] = {
21a8dc
  * and convert them to bitmasks for the HBA_PORTSPEED supported
21a8dc
  * Format expected: "1 Gbit[, 10 Gbit]", etc.
21a8dc
  */
21a8dc
-static int sys_read_speed(const char *dir, const char *file, char *buf,
21a8dc
-			  size_t buflen, HBA_PORTSPEED *speeds)
21a8dc
+static int sys_read_speed(const char *speedstr, HBA_PORTSPEED *speeds)
21a8dc
 {
21a8dc
 	int rc = 0;
21a8dc
 	u_int32_t val = 0;
21a8dc
 	int len = 0;
21a8dc
-	char *cp;
21a8dc
+	const char *cp;
21a8dc
 	struct sa_nameval *tp = port_speeds_table;
21a8dc
 
21a8dc
-	rc = sa_sys_read_line(dir, file, buf, buflen);
21a8dc
-	if (rc == 0 && strstr(buf, "Unknown") == NULL) {
21a8dc
-		for (cp = buf; *cp != '\0';) {
21a8dc
+	if (rc == 0 && strstr(speedstr, "Unknown") == NULL) {
21a8dc
+		for (cp = speedstr; *cp != '\0';) {
21a8dc
 			for (; tp->nv_name != NULL; tp++) {
21a8dc
 				len = strlen(tp->nv_name);
21a8dc
 				if (strncasecmp(tp->nv_name, cp, len) == 0) {
21a8dc
@@ -140,69 +140,189 @@ counting_rports(struct dirent *dp, void *arg)
21a8dc
 	return HBA_STATUS_OK;
21a8dc
 }
21a8dc
 
21a8dc
-static int
21a8dc
-check_ifindex(struct dirent *dp, void *arg)
21a8dc
+static void 
21a8dc
+sysfs_scan_pci(struct udev_device *pci,
21a8dc
+	struct hba_info *hba_info,
21a8dc
+	HBA_ADAPTERATTRIBUTES *atp
21a8dc
+	)
21a8dc
 {
21a8dc
-	char *ifindex = (char *)arg;
21a8dc
-	char hba_dir[256];
21a8dc
+	const char *attr;
21a8dc
+	const char *hba_dir;
21a8dc
 	char buf[256];
21a8dc
-	int rc;
21a8dc
-
21a8dc
-	snprintf(hba_dir, sizeof(hba_dir),
21a8dc
-		 SYSFS_HBA_DIR "/%s", dp->d_name);
21a8dc
-	memset(buf, 0, sizeof(buf));
21a8dc
-	rc = sa_sys_read_line(hba_dir, "ifindex", buf, sizeof(buf) - 1);
21a8dc
-	if (rc)
21a8dc
-		return 0;
21a8dc
-	if (!strncmp(ifindex, buf, sizeof(buf))) {
21a8dc
-		strcpy(arg, dp->d_name);
21a8dc
-		return 1;
21a8dc
+	char *saveptr;	/* for strtok_r */
21a8dc
+
21a8dc
+	/* Get vendor_id */
21a8dc
+	attr = udev_device_get_sysattr_value(pci, "vendor");
21a8dc
+	hba_info->vendor_id = strtoul(attr, NULL, 0);
21a8dc
+
21a8dc
+	/* Get device_id */
21a8dc
+	attr = udev_device_get_sysattr_value(pci, "device");
21a8dc
+	hba_info->device_id = strtoul(attr, NULL, 0);
21a8dc
+
21a8dc
+	/* Get subsystem_vendor_id */
21a8dc
+	attr = udev_device_get_sysattr_value(pci, "subsystem_vendor");
21a8dc
+	hba_info->subsystem_vendor_id = strtoul(attr, NULL, 0);
21a8dc
+
21a8dc
+	/* Get subsystem_device_id */
21a8dc
+	attr = udev_device_get_sysattr_value(pci, "subsystem_device");
21a8dc
+	hba_info->subsystem_device_id = strtoul(attr, NULL, 0);
21a8dc
+
21a8dc
+	/* Get device_class */
21a8dc
+	attr = udev_device_get_sysattr_value(pci, "class");
21a8dc
+	hba_info->device_class = strtoul(attr, NULL, 0);
21a8dc
+	hba_info->device_class = hba_info->device_class>>8;
21a8dc
+
21a8dc
+	/*
21a8dc
+	 * Get Hardware Information via PCI Library
21a8dc
+	 */
21a8dc
+
21a8dc
+	sscanf(udev_device_get_sysname(pci), "%x:%x:%x:%x",
21a8dc
+			&hba_info->domain, &hba_info->bus,
21a8dc
+			&hba_info->dev, &hba_info->func);
21a8dc
+	(void) find_pci_device(hba_info);
21a8dc
+
21a8dc
+	/* Get Number of Ports */
21a8dc
+	atp->NumberOfPorts = hba_info->NumberOfPorts;
21a8dc
+
21a8dc
+	/* Get Manufacturer */
21a8dc
+	sa_strncpy_safe(atp->Manufacturer, sizeof(atp->Manufacturer),
21a8dc
+			hba_info->Manufacturer, sizeof(hba_info->Manufacturer));
21a8dc
+
21a8dc
+	/* Get SerialNumber */
21a8dc
+	sa_strncpy_safe(atp->SerialNumber, sizeof(atp->SerialNumber),
21a8dc
+			hba_info->SerialNumber, sizeof(hba_info->SerialNumber));
21a8dc
+
21a8dc
+
21a8dc
+	/* Get ModelDescription */
21a8dc
+	sa_strncpy_safe(atp->ModelDescription, sizeof(atp->ModelDescription),
21a8dc
+			hba_info->ModelDescription,
21a8dc
+			sizeof(hba_info->ModelDescription));
21a8dc
+	if (!strncmp(hba_info->ModelDescription, "Unknown",
21a8dc
+		 sizeof(hba_info->ModelDescription))) {
21a8dc
+		snprintf(atp->ModelDescription, sizeof(atp->ModelDescription),
21a8dc
+			"[%04x:%04x]-[%04x:%04x]-(%04x)",
21a8dc
+			hba_info->vendor_id, hba_info->device_id,
21a8dc
+			hba_info->subsystem_vendor_id,
21a8dc
+			hba_info->subsystem_device_id,
21a8dc
+			hba_info->device_class);
21a8dc
+		/*
21a8dc
+		 * Get Model
21a8dc
+		 *
21a8dc
+		 * If the device is a newly developed product, and
21a8dc
+		 * the model description is not in pci.ids yet, use
21a8dc
+		 * the model description constructed above as the
21a8dc
+		 * model string.
21a8dc
+		 */
21a8dc
+		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
+				atp->ModelDescription,
21a8dc
+				sizeof(atp->ModelDescription));
21a8dc
 	}
21a8dc
-	return 0;
21a8dc
+
21a8dc
+	/*
21a8dc
+	 * Get Model
21a8dc
+	 *
21a8dc
+	 * If the device name has already been added into
21a8dc
+	 * the pci.ids file, use the first word of the model
21a8dc
+	 * description as the model. If the space after the
21a8dc
+	 * first word is not found (new product), use the
21a8dc
+	 * model description as the model.
21a8dc
+	 */
21a8dc
+	sa_strncpy_safe(buf, sizeof(buf), atp->ModelDescription,
21a8dc
+			sizeof(atp->ModelDescription));
21a8dc
+	if (strtok_r(buf, " ", &saveptr))
21a8dc
+		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
+				buf, strnlen(buf, sizeof(buf)));
21a8dc
+	else
21a8dc
+		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
+				atp->ModelDescription,
21a8dc
+				sizeof(atp->ModelDescription));
21a8dc
+
21a8dc
+	/* Get HardwareVersion */
21a8dc
+	sa_strncpy_safe(atp->HardwareVersion, sizeof(atp->HardwareVersion),
21a8dc
+			hba_info->HardwareVersion,
21a8dc
+			sizeof(hba_info->HardwareVersion));
21a8dc
+
21a8dc
+	/* Get OptionROMVersion (TODO) */
21a8dc
+	sa_strncpy_safe(atp->OptionROMVersion, sizeof(atp->OptionROMVersion),
21a8dc
+			HBA_ROM_VERSION, sizeof(HBA_ROM_VERSION));
21a8dc
+
21a8dc
+	/* Get FirmwareVersion (TODO) */
21a8dc
+	sa_strncpy_safe(atp->FirmwareVersion, sizeof(atp->FirmwareVersion),
21a8dc
+			HBA_FW_VERSION, sizeof(HBA_FW_VERSION));
21a8dc
+
21a8dc
+	/* Get VendorSpecificID (TODO) */
21a8dc
+	atp->VendorSpecificID = HBA_VENDOR_SPECIFIC_ID;
21a8dc
+
21a8dc
+	/* Get DriverVersion */
21a8dc
+	hba_dir = udev_device_get_syspath(pci);
21a8dc
+	sa_sys_read_line(hba_dir, SYSFS_MODULE_VER,
21a8dc
+			atp->DriverVersion, sizeof(atp->DriverVersion));
21a8dc
 }
21a8dc
 
21a8dc
-/*
21a8dc
- * find_phys_if - find the regular network interface name that
21a8dc
- *                has the ifindex that matches the specified iflink.
21a8dc
- *                This ifname will be used to find the PCI info
21a8dc
- *                of a VLAN interface.
21a8dc
- * hba_dir: hba_dir of VLAN interface.
21a8dc
- * buf: returns ifname of regular network interface.
21a8dc
- */
21a8dc
-static int
21a8dc
-find_phys_if(char *hba_dir, char *buf, size_t len)
21a8dc
+struct udev_device *
21a8dc
+find_netdev_by_ifindex(struct udev *udev, const char *ifindex)
21a8dc
 {
21a8dc
-	int rc;
21a8dc
+	struct udev_enumerate *ue;
21a8dc
+	struct udev_list_entry *head;
21a8dc
+	struct udev_device *newnet = NULL;
21a8dc
 
21a8dc
-	rc = sa_sys_read_line(hba_dir, "iflink", buf, len);
21a8dc
-	if (rc)
21a8dc
-		return 1;
21a8dc
-	/*
21a8dc
-	 * Search for the regular network interface and
21a8dc
-	 * return the interface name in the buf.
21a8dc
-	 */
21a8dc
-	sa_dir_read(SYSFS_HBA_DIR, check_ifindex, buf);
21a8dc
-	return 0;
21a8dc
+	ue = udev_enumerate_new(udev);
21a8dc
+	udev_enumerate_add_match_subsystem(ue, "net");
21a8dc
+	udev_enumerate_add_match_sysattr(ue, "ifindex", ifindex); 
21a8dc
+	udev_enumerate_scan_devices(ue);
21a8dc
+	/* enumerate returns a list, but there should only ever be one device
21a8dc
+	 * with a given ifindex */
21a8dc
+	head = udev_enumerate_get_list_entry(ue);
21a8dc
+	if (head)
21a8dc
+		newnet = udev_device_new_from_syspath(udev, udev_list_entry_get_name(head));
21a8dc
+	udev_enumerate_unref(ue);
21a8dc
+	return newnet;
21a8dc
+}
21a8dc
+
21a8dc
+struct udev_device *
21a8dc
+find_phys_if(struct udev_device *net)
21a8dc
+{
21a8dc
+	const char *ifindex;
21a8dc
+	const char *iflink;
21a8dc
+	struct udev *udev;
21a8dc
+	struct udev_device *lower = NULL;
21a8dc
+
21a8dc
+	ifindex = udev_device_get_sysattr_value(net, "ifindex");
21a8dc
+	iflink = udev_device_get_sysattr_value(net, "iflink");
21a8dc
+	if (strcmp(ifindex, iflink)) {
21a8dc
+		udev = udev_device_get_udev(net);
21a8dc
+		lower = find_netdev_by_ifindex(udev, iflink);
21a8dc
+	}
21a8dc
+	if (lower) {
21a8dc
+		return lower;
21a8dc
+	} else {
21a8dc
+		udev_device_ref(net);
21a8dc
+		return net;
21a8dc
+	}
21a8dc
 }
21a8dc
 
21a8dc
 static int
21a8dc
-sysfs_scan(struct dirent *dp, void *arg)
21a8dc
+sysfs_scan(struct udev_device *fc_host)
21a8dc
 {
21a8dc
 	HBA_ADAPTERATTRIBUTES *atp;
21a8dc
 	HBA_PORTATTRIBUTES *pap;
21a8dc
-	HBA_WWN wwnn;
21a8dc
+	uint64_t wwnn;
21a8dc
 	struct hba_info hba_info;
21a8dc
 	struct adapter_info *ap;
21a8dc
 	struct port_info *pp;
21a8dc
-	char host_dir[80], hba_dir[80], drv_dir[80];
21a8dc
-	char dev_dir[128];
21a8dc
+	const char *hba_dir;
21a8dc
+	char drv_dir[80];
21a8dc
 	char ifname[20], buf[256];
21a8dc
 	char *driverName;
21a8dc
 	int data[32], rc, i;
21a8dc
 	char *cp;
21a8dc
-	char *saveptr;	/* for strtok_r */
21a8dc
-	unsigned int ifindex;
21a8dc
-	unsigned int iflink;
21a8dc
+
21a8dc
+	const char *sysname = udev_device_get_sysname(fc_host);
21a8dc
+	const char *syspath = udev_device_get_syspath(fc_host);
21a8dc
+	struct udev_device *pci;
21a8dc
+	struct udev_device *net;
21a8dc
+	const char *ptr = NULL;
21a8dc
+	const char *attr;
21a8dc
 
21a8dc
 	memset(&hba_info, 0, sizeof(hba_info));
21a8dc
 
21a8dc
@@ -217,7 +337,7 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 		return HBA_STATUS_ERROR;
21a8dc
 	}
21a8dc
 	memset(ap, 0, sizeof(*ap));
21a8dc
-	ap->ad_kern_index = atoi(dp->d_name + sizeof("host") - 1);
21a8dc
+	ap->ad_kern_index = atoi(sysname + sizeof("host") - 1);
21a8dc
 	ap->ad_port_count = 1;
21a8dc
 
21a8dc
 	/* atp points to the HBA attributes structure */
21a8dc
@@ -239,137 +359,76 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 	memset(pp, 0, sizeof(*pp));
21a8dc
 	pp->ap_adapt = ap;
21a8dc
 	pp->ap_index = ap->ad_port_count - 1;
21a8dc
-	pp->ap_kern_hba = atoi(dp->d_name + sizeof("host") - 1);
21a8dc
+	pp->ap_kern_hba = atoi(sysname + sizeof("host") - 1);
21a8dc
 
21a8dc
 	/* pap points to the local port attributes structure */
21a8dc
 	pap = &pp->ap_attr;
21a8dc
 
21a8dc
-	/* Construct the host directory name from the input name */
21a8dc
-	snprintf(host_dir, sizeof(host_dir),
21a8dc
-		SYSFS_HOST_DIR "/%s", dp->d_name);
21a8dc
-
21a8dc
-	rc = sa_sys_read_line(host_dir, "symbolic_name", buf, sizeof(buf));
21a8dc
-
21a8dc
 	/* Get PortSymbolicName */
21a8dc
+	ptr = udev_device_get_sysattr_value(fc_host, "symbolic_name");
21a8dc
 	sa_strncpy_safe(pap->PortSymbolicName, sizeof(pap->PortSymbolicName),
21a8dc
-			buf, sizeof(buf));
21a8dc
+			ptr, strlen(ptr));
21a8dc
 
21a8dc
 	/* Skip the HBA if it isn't OpenFC */
21a8dc
 	cp = strstr(pap->PortSymbolicName, " over ");
21a8dc
 	if (!cp)
21a8dc
 		goto skip;
21a8dc
 
21a8dc
-	/*
21a8dc
-	 * See if <host_dir>/device is a PCI symlink.
21a8dc
-	 * If not, try it as a net device.
21a8dc
-	 */
21a8dc
-	snprintf(dev_dir, sizeof(dev_dir), "%s/device", host_dir);
21a8dc
-	i = readlink(dev_dir, buf, sizeof(buf) - 1);
21a8dc
-	if (i < 0)
21a8dc
-		i = 0;
21a8dc
-	buf[i] = '\0';
21a8dc
-
21a8dc
-	if (strstr(buf, "devices/pci") && !strstr(buf, "/net/")) {
21a8dc
-		snprintf(hba_dir, sizeof(hba_dir), "%s/device/..", host_dir);
21a8dc
-	} else {
21a8dc
-		/* assume a net device */
21a8dc
-		cp += 6;
21a8dc
-		sa_strncpy_safe(ifname, sizeof(ifname), cp, strlen(cp));
21a8dc
-		snprintf(hba_dir, sizeof(hba_dir),
21a8dc
-			 SYSFS_HBA_DIR "/%s", ifname);
21a8dc
-		/*
21a8dc
-		 * Try as VLAN device or other virtual net device.
21a8dc
-		 * If this is the case, ifindex and iflink will be different.
21a8dc
-		 * iflink is the ifindex of the physical device.
21a8dc
-		 */
21a8dc
-		rc = sa_sys_read_u32(hba_dir, "ifindex", &ifindex);
21a8dc
-		if (rc < 0)
21a8dc
-			goto skip;
21a8dc
-		rc = sa_sys_read_u32(hba_dir, "iflink", &iflink);
21a8dc
-		if (rc < 0)
21a8dc
-			goto skip;
21a8dc
-		if (ifindex != iflink) {
21a8dc
-			rc = find_phys_if(hba_dir, buf, sizeof(buf));
21a8dc
-			if (rc)
21a8dc
-				goto skip;
21a8dc
-			strncpy(ifname, buf, sizeof(ifname));
21a8dc
-		}
21a8dc
-
21a8dc
-		snprintf(hba_dir, sizeof(hba_dir),
21a8dc
-			 SYSFS_HBA_DIR "/%s/device", ifname);
21a8dc
-		i = readlink(hba_dir, buf, sizeof(buf) - 1);
21a8dc
-		if (i < 0)
21a8dc
-			goto skip;
21a8dc
-		buf[i] = '\0';
21a8dc
+	pci = udev_device_get_parent_with_subsystem_devtype(fc_host, "pci", NULL);
21a8dc
+	net = udev_device_get_parent_with_subsystem_devtype(fc_host, "net", NULL);
21a8dc
+	if (!pci && net) {
21a8dc
+		/* check for a vlan device, stacked on a real PCI network device */
21a8dc
+		net = find_phys_if(net);
21a8dc
+		pci = udev_device_get_parent_with_subsystem_devtype(net, "pci", NULL);
21a8dc
 	}
21a8dc
 
21a8dc
 	/*
21a8dc
-	 * Assume a PCI symlink value is in buf.
21a8dc
-	 * Back up to the last path component that looks like a PCI element.
21a8dc
-	 * A sample link value is like:
21a8dc
-	 *	../../devices/pci*.../0000:03:00.0
21a8dc
-	 */
21a8dc
-	rc = 0;
21a8dc
-	do {
21a8dc
-		cp = strrchr(buf, '/');
21a8dc
-		if (!cp)
21a8dc
-			break;
21a8dc
-		rc = sscanf(cp + 1, "%x:%x:%x.%x",
21a8dc
-			    &hba_info.domain, &hba_info.bus,
21a8dc
-			    &hba_info.dev, &hba_info.func);
21a8dc
-		if (rc == 4)
21a8dc
-			break;
21a8dc
-		*cp = '\0';
21a8dc
-	} while (cp && cp > buf);
21a8dc
-
21a8dc
-	if (rc != 4)
21a8dc
-		goto skip;
21a8dc
-
21a8dc
-	/*
21a8dc
 	 * Save the host directory and the hba directory
21a8dc
 	 * in local port structure
21a8dc
 	 */
21a8dc
 	sa_strncpy_safe(pp->host_dir, sizeof(pp->host_dir),
21a8dc
-			host_dir, sizeof(host_dir));
21a8dc
+			syspath, strlen(syspath));
21a8dc
 
21a8dc
 	/* Get NodeWWN */
21a8dc
-	rc = sys_read_wwn(pp->host_dir, "node_name", &wwnn);
21a8dc
-	memcpy(&pap->NodeWWN, &wwnn, sizeof(wwnn));
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "node_name");
21a8dc
+	wwnn = strtoull(attr, NULL, 16);
21a8dc
+	copy_wwn(&pap->NodeWWN, wwnn);
21a8dc
 
21a8dc
 	/* Get PortWWN */
21a8dc
-	rc = sys_read_wwn(pp->host_dir, "port_name", &pap->PortWWN);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "port_name");
21a8dc
+	wwnn = strtoull(attr, NULL, 16);
21a8dc
+	copy_wwn(&pap->PortWWN, wwnn);
21a8dc
 
21a8dc
 	/* Get PortFcId */
21a8dc
-	rc = sa_sys_read_u32(pp->host_dir, "port_id", &pap->PortFcId);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "port_id");
21a8dc
+	pap->PortFcId = strtoul(attr, NULL, 0);
21a8dc
 
21a8dc
 	/* Get PortType */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "port_type", buf, sizeof(buf));
21a8dc
-	rc = sa_enum_encode(port_types_table, buf, &pap->PortType);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "port_type");
21a8dc
+	rc = sa_enum_encode(port_types_table, attr, &pap->PortType);
21a8dc
 
21a8dc
 	/* Get PortState */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "port_state", buf, sizeof(buf));
21a8dc
-	rc = sa_enum_encode(port_states_table, buf, &pap->PortState);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "port_state");
21a8dc
+	rc = sa_enum_encode(port_states_table, attr, &pap->PortState);
21a8dc
 
21a8dc
 	/* Get PortSpeed */
21a8dc
-	rc = sys_read_speed(pp->host_dir, "speed",
21a8dc
-				buf, sizeof(buf),
21a8dc
-				&pap->PortSpeed);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "speed");
21a8dc
+	rc = sys_read_speed(attr, &pap->PortSpeed);
21a8dc
 
21a8dc
 	/* Get PortSupportedSpeed */
21a8dc
-	rc = sys_read_speed(pp->host_dir, "supported_speeds",
21a8dc
-				buf, sizeof(buf),
21a8dc
-				&pap->PortSupportedSpeed);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "supported_speeds");
21a8dc
+	rc = sys_read_speed(attr, &pap->PortSupportedSpeed);
21a8dc
 
21a8dc
 	/* Get PortMaxFrameSize */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "maxframe_size", buf, sizeof(buf));
21a8dc
-	sscanf(buf, "%d", &pap->PortMaxFrameSize);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "maxframe_size");
21a8dc
+	sscanf(attr, "%d", &pap->PortMaxFrameSize);
21a8dc
 
21a8dc
 	/* Get PortSupportedFc4Types */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "supported_fc4s", buf, sizeof(buf));
21a8dc
-	sscanf(buf, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "supported_fc4s");
21a8dc
+	sscanf(attr, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
21a8dc
 		&data[0], &data[1], &data[2], &data[3], &data[4], &data[5],
21a8dc
 		&data[6], &data[7], &data[8], &data[9], &data[10], &data[11],
21a8dc
 		&data[12], &data[13], &data[14], &data[15], &data[16],
21a8dc
@@ -380,11 +439,11 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 		pap->PortSupportedFc4Types.bits[i] = data[i];
21a8dc
 
21a8dc
 	/* Get PortActiveFc4Types */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "active_fc4s", buf, sizeof(buf));
21a8dc
-	sscanf(buf, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "active_fc4s");
21a8dc
+	sscanf(attr, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
21a8dc
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
21a8dc
 		&data[0], &data[1], &data[2], &data[3], &data[4], &data[5],
21a8dc
 		&data[6], &data[7], &data[8], &data[9], &data[10], &data[11],
21a8dc
 		&data[12], &data[13], &data[14], &data[15], &data[16],
21a8dc
@@ -395,19 +454,19 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 		pap->PortActiveFc4Types.bits[i] = data[i];
21a8dc
 
21a8dc
 	/* Get FabricName */
21a8dc
-	rc = sys_read_wwn(pp->host_dir, "fabric_name", &pap->FabricName);
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "fabric_name");
21a8dc
+	wwnn = strtoull(attr, NULL, 16);
21a8dc
+	copy_wwn(&pap->FabricName, wwnn);
21a8dc
 
21a8dc
 	/* Get PortSupportedClassofService */
21a8dc
-	rc = sa_sys_read_line(pp->host_dir, "supported_classes",
21a8dc
-				buf, sizeof(buf));
21a8dc
-
21a8dc
-	cp = strstr(buf, "Class");
21a8dc
+	attr = udev_device_get_sysattr_value(fc_host, "supported_classes");
21a8dc
+	cp = strstr(attr, "Class");
21a8dc
 	if (cp)
21a8dc
 		pap->PortSupportedClassofService = *(cp + 6) - '0';
21a8dc
 
21a8dc
 	/* Get OSDeviceName */
21a8dc
 	sa_strncpy_safe(pap->OSDeviceName, sizeof(pap->OSDeviceName),
21a8dc
-			dp->d_name, sizeof(dp->d_name));
21a8dc
+			sysname, sizeof(sysname));
21a8dc
 
21a8dc
 	/* Get NumberofDiscoveredPorts */
21a8dc
 	snprintf(buf, sizeof(buf), "%s/device", pp->host_dir);
21a8dc
@@ -428,104 +487,8 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 	snprintf(buf, sizeof(buf), "fcoe:%s", ifname);
21a8dc
 	ap->ad_name = strdup(buf);
21a8dc
 
21a8dc
-	/* Get vendor_id */
21a8dc
-	rc = sa_sys_read_u32(hba_dir, "vendor", &hba_info.vendor_id);
21a8dc
-
21a8dc
-	/* Get device_id */
21a8dc
-	rc = sa_sys_read_u32(hba_dir, "device", &hba_info.device_id);
21a8dc
-
21a8dc
-	/* Get subsystem_vendor_id */
21a8dc
-	rc = sa_sys_read_u32(hba_dir, "subsystem_vendor",
21a8dc
-				&hba_info.subsystem_vendor_id);
21a8dc
-
21a8dc
-	/* Get subsystem_device_id */
21a8dc
-	rc = sa_sys_read_u32(hba_dir, "subsystem_device",
21a8dc
-				&hba_info.subsystem_device_id);
21a8dc
-
21a8dc
-	/* Get device_class */
21a8dc
-	rc = sa_sys_read_u32(hba_dir, "class", &hba_info.device_class);
21a8dc
-	hba_info.device_class = hba_info.device_class>>8;
21a8dc
-
21a8dc
-	/*
21a8dc
-	 * Get Hardware Information via PCI Library
21a8dc
-	 */
21a8dc
-	(void) find_pci_device(&hba_info);
21a8dc
-
21a8dc
-	/* Get Number of Ports */
21a8dc
-	atp->NumberOfPorts = hba_info.NumberOfPorts;
21a8dc
-
21a8dc
-	/* Get Manufacturer */
21a8dc
-	sa_strncpy_safe(atp->Manufacturer, sizeof(atp->Manufacturer),
21a8dc
-			hba_info.Manufacturer, sizeof(hba_info.Manufacturer));
21a8dc
-
21a8dc
-	/* Get SerialNumber */
21a8dc
-	sa_strncpy_safe(atp->SerialNumber, sizeof(atp->SerialNumber),
21a8dc
-			hba_info.SerialNumber, sizeof(hba_info.SerialNumber));
21a8dc
-
21a8dc
-
21a8dc
-	/* Get ModelDescription */
21a8dc
-	sa_strncpy_safe(atp->ModelDescription, sizeof(atp->ModelDescription),
21a8dc
-			hba_info.ModelDescription,
21a8dc
-			sizeof(hba_info.ModelDescription));
21a8dc
-	if (!strncmp(hba_info.ModelDescription, "Unknown",
21a8dc
-		 sizeof(hba_info.ModelDescription))) {
21a8dc
-		snprintf(atp->ModelDescription, sizeof(atp->ModelDescription),
21a8dc
-			"[%04x:%04x]-[%04x:%04x]-(%04x)",
21a8dc
-			hba_info.vendor_id, hba_info.device_id,
21a8dc
-			hba_info.subsystem_vendor_id,
21a8dc
-			hba_info.subsystem_device_id,
21a8dc
-			hba_info.device_class);
21a8dc
-		/*
21a8dc
-		 * Get Model
21a8dc
-		 *
21a8dc
-		 * If the device is a newly developed product, and
21a8dc
-		 * the model description is not in pci.ids yet, use
21a8dc
-		 * the model description constructed above as the
21a8dc
-		 * model string.
21a8dc
-		 */
21a8dc
-		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
-				atp->ModelDescription,
21a8dc
-				sizeof(atp->ModelDescription));
21a8dc
-	}
21a8dc
-
21a8dc
-	/*
21a8dc
-	 * Get Model
21a8dc
-	 *
21a8dc
-	 * If the device name has already been added into
21a8dc
-	 * the pci.ids file, use the first word of the model
21a8dc
-	 * description as the model. If the space after the
21a8dc
-	 * first word is not found (new product), use the
21a8dc
-	 * model description as the model.
21a8dc
-	 */
21a8dc
-	sa_strncpy_safe(buf, sizeof(buf), atp->ModelDescription,
21a8dc
-			sizeof(atp->ModelDescription));
21a8dc
-	if (strtok_r(buf, " ", &saveptr))
21a8dc
-		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
-				buf, strnlen(buf, sizeof(buf)));
21a8dc
-	else
21a8dc
-		sa_strncpy_safe(atp->Model, sizeof(atp->Model),
21a8dc
-				atp->ModelDescription,
21a8dc
-				sizeof(atp->ModelDescription));
21a8dc
-
21a8dc
-	/* Get HardwareVersion */
21a8dc
-	sa_strncpy_safe(atp->HardwareVersion, sizeof(atp->HardwareVersion),
21a8dc
-			hba_info.HardwareVersion,
21a8dc
-			sizeof(hba_info.HardwareVersion));
21a8dc
-
21a8dc
-	/* Get OptionROMVersion (TODO) */
21a8dc
-	sa_strncpy_safe(atp->OptionROMVersion, sizeof(atp->OptionROMVersion),
21a8dc
-			HBA_ROM_VERSION, sizeof(HBA_ROM_VERSION));
21a8dc
-
21a8dc
-	/* Get FirmwareVersion (TODO) */
21a8dc
-	sa_strncpy_safe(atp->FirmwareVersion, sizeof(atp->FirmwareVersion),
21a8dc
-			HBA_FW_VERSION, sizeof(HBA_FW_VERSION));
21a8dc
-
21a8dc
-	/* Get VendorSpecificID (TODO) */
21a8dc
-	atp->VendorSpecificID = HBA_VENDOR_SPECIFIC_ID;
21a8dc
-
21a8dc
-	/* Get DriverVersion */
21a8dc
-	rc = sa_sys_read_line(hba_dir, SYSFS_MODULE_VER,
21a8dc
-			atp->DriverVersion, sizeof(atp->DriverVersion));
21a8dc
+	if (pci)
21a8dc
+		sysfs_scan_pci(pci, &hba_info, atp);
21a8dc
 
21a8dc
 	/* Get NodeSymbolicName */
21a8dc
 	sa_strncpy_safe(atp->NodeSymbolicName, sizeof(atp->NodeSymbolicName),
21a8dc
@@ -538,6 +501,7 @@ sysfs_scan(struct dirent *dp, void *arg)
21a8dc
 		sizeof(pap->NodeWWN));
21a8dc
 
21a8dc
 	/* Get DriverName */
21a8dc
+	hba_dir = udev_device_get_syspath(pci);
21a8dc
 	snprintf(drv_dir, sizeof(drv_dir), "%s" SYSFS_MODULE , hba_dir);
21a8dc
 	i = readlink(drv_dir, buf, sizeof(buf));
21a8dc
 	if (i < 0)
21a8dc
@@ -662,10 +626,48 @@ sysfs_get_port_fc4stats(char *dir, HBA_FC4STATISTICS *fc4sp)
21a8dc
 /*
21a8dc
  * Open device and read adapter info if available.
21a8dc
  */
21a8dc
-void
21a8dc
+int
21a8dc
 adapter_init(void)
21a8dc
 {
21a8dc
-	sa_dir_read(SYSFS_HOST_DIR, sysfs_scan, NULL);
21a8dc
+	struct udev *udev;
21a8dc
+	struct udev_enumerate *ue;
21a8dc
+	struct udev_list_entry *head, *ul;
21a8dc
+	struct udev_device *fc_host;
21a8dc
+	const char *syspath;
21a8dc
+	int re = 0;
21a8dc
+
21a8dc
+	udev = udev_new();
21a8dc
+	if (!udev) {
21a8dc
+		return -ENOMEM;
21a8dc
+	}
21a8dc
+	ue = udev_enumerate_new(udev);
21a8dc
+	if (!ue) {
21a8dc
+		re = -ENOMEM;
21a8dc
+		goto err_enum_new;
21a8dc
+	}
21a8dc
+	re = udev_enumerate_add_match_subsystem(ue, "fc_host");
21a8dc
+	if (re) {
21a8dc
+		goto err_add_match;
21a8dc
+	}
21a8dc
+	re = udev_enumerate_scan_devices(ue);
21a8dc
+	if (re) {
21a8dc
+		goto err_scan_devs;
21a8dc
+	}
21a8dc
+	head = udev_enumerate_get_list_entry(ue);
21a8dc
+	udev_list_entry_foreach(ul, head) {
21a8dc
+		syspath = udev_list_entry_get_name(ul);
21a8dc
+		fc_host = udev_device_new_from_syspath(udev, syspath);
21a8dc
+		if (!fc_host)
21a8dc
+			continue;
21a8dc
+		sysfs_scan(fc_host);
21a8dc
+		udev_device_unref(fc_host);
21a8dc
+	}
21a8dc
+err_scan_devs:
21a8dc
+err_add_match:
21a8dc
+	udev_enumerate_unref(ue);
21a8dc
+err_enum_new:
21a8dc
+	udev_unref(udev);
21a8dc
+	return re;
21a8dc
 }
21a8dc
 
21a8dc
 void