Patchwork [Open-FCoE,libhbalinux,3/6] use libudev to read fc_host attributes

login
register
mail settings
Submitter Chris Leech
Date Oct. 13, 2014, 11:13 p.m.
Message ID <1413242005-17786-4-git-send-email-cleech@redhat.com>
Download mbox | patch
Permalink /patch/170/
State RFC
Headers show

Comments

Chris Leech - Oct. 13, 2014, 11:13 p.m.
Signed-off-by: Chris Leech <cleech@redhat.com>
---
 lport.c | 99 ++++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 49 insertions(+), 50 deletions(-)

Patch

diff --git a/lport.c b/lport.c
index 3b1030b..5aff2ca 100644
--- a/lport.c
+++ b/lport.c
@@ -90,18 +90,16 @@  struct sa_nameval port_speeds_table[] = {
  * and convert them to bitmasks for the HBA_PORTSPEED supported
  * Format expected: "1 Gbit[, 10 Gbit]", etc.
  */
-static int sys_read_speed(const char *dir, const char *file, char *buf,
-			  size_t buflen, HBA_PORTSPEED *speeds)
+static int sys_read_speed(const char *speedstr, HBA_PORTSPEED *speeds)
 {
 	int rc = 0;
 	u_int32_t val = 0;
 	int len = 0;
-	char *cp;
+	const char *cp;
 	struct sa_nameval *tp = port_speeds_table;
 
-	rc = sa_sys_read_line(dir, file, buf, buflen);
-	if (rc == 0 && strstr(buf, "Unknown") == NULL) {
-		for (cp = buf; *cp != '\0';) {
+	if (rc == 0 && strstr(speedstr, "Unknown") == NULL) {
+		for (cp = speedstr; *cp != '\0';) {
 			for (; tp->nv_name != NULL; tp++) {
 				len = strlen(tp->nv_name);
 				if (strncasecmp(tp->nv_name, cp, len) == 0) {
@@ -192,12 +190,13 @@  sysfs_scan(struct udev_device *fc_host)
 {
 	HBA_ADAPTERATTRIBUTES *atp;
 	HBA_PORTATTRIBUTES *pap;
-	HBA_WWN wwnn;
+	uint64_t wwnn;
 	struct hba_info hba_info;
 	struct adapter_info *ap;
 	struct port_info *pp;
-	char host_dir[80], hba_dir[80], drv_dir[80];
-	char dev_dir[128];
+	char hba_dir[80];
+	char drv_dir[80];
+	const char *dev_dir;
 	char ifname[20], buf[256];
 	char *driverName;
 	int data[32], rc, i;
@@ -207,6 +206,9 @@  sysfs_scan(struct udev_device *fc_host)
 	unsigned int iflink;
 
 	const char *sysname = udev_device_get_sysname(fc_host);
+	const char *syspath = udev_device_get_syspath(fc_host);
+	const char *ptr = NULL;
+	const char *attr;
 
 	memset(&hba_info, 0, sizeof(hba_info));
 
@@ -248,15 +250,10 @@  sysfs_scan(struct udev_device *fc_host)
 	/* pap points to the local port attributes structure */
 	pap = &pp->ap_attr;
 
-	/* Construct the host directory name from the input name */
-	snprintf(host_dir, sizeof(host_dir),
-		SYSFS_HOST_DIR "/%s", sysname);
-
-	rc = sa_sys_read_line(host_dir, "symbolic_name", buf, sizeof(buf));
-
 	/* Get PortSymbolicName */
+	ptr = udev_device_get_sysattr_value(fc_host, "symbolic_name");
 	sa_strncpy_safe(pap->PortSymbolicName, sizeof(pap->PortSymbolicName),
-			buf, sizeof(buf));
+			ptr, strlen(ptr));
 
 	/* Skip the HBA if it isn't OpenFC */
 	cp = strstr(pap->PortSymbolicName, " over ");
@@ -267,14 +264,14 @@  sysfs_scan(struct udev_device *fc_host)
 	 * See if <host_dir>/device is a PCI symlink.
 	 * If not, try it as a net device.
 	 */
-	snprintf(dev_dir, sizeof(dev_dir), "%s/device", host_dir);
+	dev_dir = udev_device_get_sysattr_value(fc_host, "device");
 	i = readlink(dev_dir, buf, sizeof(buf) - 1);
 	if (i < 0)
 		i = 0;
 	buf[i] = '\0';
 
 	if (strstr(buf, "devices/pci") && !strstr(buf, "/net/")) {
-		snprintf(hba_dir, sizeof(hba_dir), "%s/device/..", host_dir);
+		snprintf(hba_dir, sizeof(hba_dir), "%s/device/..", syspath);
 	} else {
 		/* assume a net device */
 		cp += 6;
@@ -334,46 +331,48 @@  sysfs_scan(struct udev_device *fc_host)
 	 * in local port structure
 	 */
 	sa_strncpy_safe(pp->host_dir, sizeof(pp->host_dir),
-			host_dir, sizeof(host_dir));
+			syspath, strlen(syspath));
 
 	/* Get NodeWWN */
-	rc = sys_read_wwn(pp->host_dir, "node_name", &wwnn);
-	memcpy(&pap->NodeWWN, &wwnn, sizeof(wwnn));
+	attr = udev_device_get_sysattr_value(fc_host, "node_name");
+	wwnn = strtoull(attr, NULL, 16);
+	copy_wwn(&pap->NodeWWN, wwnn);
 
 	/* Get PortWWN */
-	rc = sys_read_wwn(pp->host_dir, "port_name", &pap->PortWWN);
+	attr = udev_device_get_sysattr_value(fc_host, "port_name");
+	wwnn = strtoull(attr, NULL, 16);
+	copy_wwn(&pap->PortWWN, wwnn);
 
 	/* Get PortFcId */
-	rc = sa_sys_read_u32(pp->host_dir, "port_id", &pap->PortFcId);
+	attr = udev_device_get_sysattr_value(fc_host, "port_id");
+	pap->PortFcId = strtoul(attr, NULL, 0);
 
 	/* Get PortType */
-	rc = sa_sys_read_line(pp->host_dir, "port_type", buf, sizeof(buf));
-	rc = sa_enum_encode(port_types_table, buf, &pap->PortType);
+	attr = udev_device_get_sysattr_value(fc_host, "port_type");
+	rc = sa_enum_encode(port_types_table, attr, &pap->PortType);
 
 	/* Get PortState */
-	rc = sa_sys_read_line(pp->host_dir, "port_state", buf, sizeof(buf));
-	rc = sa_enum_encode(port_states_table, buf, &pap->PortState);
+	attr = udev_device_get_sysattr_value(fc_host, "port_state");
+	rc = sa_enum_encode(port_states_table, attr, &pap->PortState);
 
 	/* Get PortSpeed */
-	rc = sys_read_speed(pp->host_dir, "speed",
-				buf, sizeof(buf),
-				&pap->PortSpeed);
+	attr = udev_device_get_sysattr_value(fc_host, "speed");
+	rc = sys_read_speed(attr, &pap->PortSpeed);
 
 	/* Get PortSupportedSpeed */
-	rc = sys_read_speed(pp->host_dir, "supported_speeds",
-				buf, sizeof(buf),
-				&pap->PortSupportedSpeed);
+	attr = udev_device_get_sysattr_value(fc_host, "supported_speeds");
+	rc = sys_read_speed(attr, &pap->PortSupportedSpeed);
 
 	/* Get PortMaxFrameSize */
-	rc = sa_sys_read_line(pp->host_dir, "maxframe_size", buf, sizeof(buf));
-	sscanf(buf, "%d", &pap->PortMaxFrameSize);
+	attr = udev_device_get_sysattr_value(fc_host, "maxframe_size");
+	sscanf(attr, "%d", &pap->PortMaxFrameSize);
 
 	/* Get PortSupportedFc4Types */
-	rc = sa_sys_read_line(pp->host_dir, "supported_fc4s", buf, sizeof(buf));
-	sscanf(buf, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
+	attr = udev_device_get_sysattr_value(fc_host, "supported_fc4s");
+	sscanf(attr, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
 		&data[0], &data[1], &data[2], &data[3], &data[4], &data[5],
 		&data[6], &data[7], &data[8], &data[9], &data[10], &data[11],
 		&data[12], &data[13], &data[14], &data[15], &data[16],
@@ -384,11 +383,11 @@  sysfs_scan(struct udev_device *fc_host)
 		pap->PortSupportedFc4Types.bits[i] = data[i];
 
 	/* Get PortActiveFc4Types */
-	rc = sa_sys_read_line(pp->host_dir, "active_fc4s", buf, sizeof(buf));
-	sscanf(buf, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
-		    "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
+	attr = udev_device_get_sysattr_value(fc_host, "active_fc4s");
+	sscanf(attr, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
+		     "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
 		&data[0], &data[1], &data[2], &data[3], &data[4], &data[5],
 		&data[6], &data[7], &data[8], &data[9], &data[10], &data[11],
 		&data[12], &data[13], &data[14], &data[15], &data[16],
@@ -399,13 +398,13 @@  sysfs_scan(struct udev_device *fc_host)
 		pap->PortActiveFc4Types.bits[i] = data[i];
 
 	/* Get FabricName */
-	rc = sys_read_wwn(pp->host_dir, "fabric_name", &pap->FabricName);
+	attr = udev_device_get_sysattr_value(fc_host, "fabric_name");
+	wwnn = strtoull(attr, NULL, 16);
+	copy_wwn(&pap->FabricName, wwnn);
 
 	/* Get PortSupportedClassofService */
-	rc = sa_sys_read_line(pp->host_dir, "supported_classes",
-				buf, sizeof(buf));
-
-	cp = strstr(buf, "Class");
+	attr = udev_device_get_sysattr_value(fc_host, "supported_classes");
+	cp = strstr(attr, "Class");
 	if (cp)
 		pap->PortSupportedClassofService = *(cp + 6) - '0';