Blame SOURCES/0005-imsm-add-devpath_to_char-method.patch

7140c6
From fcebeb77b18842876295b1a0dbc22d173a709434 Mon Sep 17 00:00:00 2001
7140c6
From: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
7140c6
Date: Mon, 17 May 2021 16:39:01 +0200
7140c6
Subject: [PATCH 05/15] imsm: add devpath_to_char method
7140c6
7140c6
Add method for reading sysfs attributes and propagate it across IMSM code.
7140c6
7140c6
Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
7140c6
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
7140c6
---
7140c6
 platform-intel.c | 23 +++++++++++++++++++++++
7140c6
 platform-intel.h |  2 ++
7140c6
 super-intel.c    | 33 +++++++++++++++------------------
7140c6
 3 files changed, 40 insertions(+), 18 deletions(-)
7140c6
7140c6
diff --git a/platform-intel.c b/platform-intel.c
7140c6
index 2ed63ed..9401784 100644
7140c6
--- a/platform-intel.c
7140c6
+++ b/platform-intel.c
7140c6
@@ -239,6 +239,29 @@ __u16 devpath_to_vendor(const char *dev_path)
7140c6
 	return id;
7140c6
 }
7140c6
 
7140c6
+/* Description: Read text value of dev_path/entry field
7140c6
+ * Parameters:
7140c6
+ *	dev_path - sysfs path to the device
7140c6
+ *	entry - entry to be read
7140c6
+ *	buf - buffer for read value
7140c6
+ *	len - size of buf
7140c6
+ *	verbose - error logging level
7140c6
+ */
7140c6
+int devpath_to_char(const char *dev_path, const char *entry, char *buf, int len,
7140c6
+		    int verbose)
7140c6
+{
7140c6
+	char path[PATH_MAX];
7140c6
+
7140c6
+	snprintf(path, sizeof(path), "%s/%s", dev_path, entry);
7140c6
+	if (load_sys(path, buf, len)) {
7140c6
+		if (verbose)
7140c6
+			pr_err("Cannot read %s, aborting\n", path);
7140c6
+		return 1;
7140c6
+	}
7140c6
+
7140c6
+	return 0;
7140c6
+}
7140c6
+
7140c6
 struct sys_dev *find_intel_devices(void)
7140c6
 {
7140c6
 	struct sys_dev *ahci, *isci, *nvme;
7140c6
diff --git a/platform-intel.h b/platform-intel.h
7140c6
index f93add5..45d98cd 100644
7140c6
--- a/platform-intel.h
7140c6
+++ b/platform-intel.h
7140c6
@@ -238,6 +238,8 @@ static inline char *guid_str(char *buf, struct efi_guid guid)
7140c6
 
7140c6
 char *get_nvme_multipath_dev_hw_path(const char *dev_path);
7140c6
 char *diskfd_to_devpath(int fd, int dev_level, char *buf);
7140c6
+int devpath_to_char(const char *dev_path, const char *entry, char *buf,
7140c6
+		    int len, int verbose);
7140c6
 __u16 devpath_to_vendor(const char *dev_path);
7140c6
 struct sys_dev *find_driver_devices(const char *bus, const char *driver);
7140c6
 struct sys_dev *find_intel_devices(void);
7140c6
diff --git a/super-intel.c b/super-intel.c
7140c6
index cff8550..c352f50 100644
7140c6
--- a/super-intel.c
7140c6
+++ b/super-intel.c
7140c6
@@ -2246,7 +2246,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
7140c6
 		char vendor[64];
7140c6
 		char buf[1024];
7140c6
 		int major, minor;
7140c6
-		char *device;
7140c6
+		char device[PATH_MAX];
7140c6
 		char *c;
7140c6
 		int port;
7140c6
 		int type;
7140c6
@@ -2262,20 +2262,15 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
7140c6
 			continue;
7140c6
 		}
7140c6
 
7140c6
-		/* retrieve the scsi device type */
7140c6
-		if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
7140c6
+		/* retrieve the scsi device */
7140c6
+		if (!devt_to_devpath(makedev(major, minor), 1, device)) {
7140c6
 			if (verbose > 0)
7140c6
-				pr_err("failed to allocate 'device'\n");
7140c6
+				pr_err("failed to get device\n");
7140c6
 			err = 2;
7140c6
 			break;
7140c6
 		}
7140c6
-		sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
7140c6
-		if (load_sys(device, buf, sizeof(buf)) != 0) {
7140c6
-			if (verbose > 0)
7140c6
-				pr_err("failed to read device type for %s\n",
7140c6
-					path);
7140c6
+		if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
7140c6
 			err = 2;
7140c6
-			free(device);
7140c6
 			break;
7140c6
 		}
7140c6
 		type = strtoul(buf, NULL, 10);
7140c6
@@ -2284,8 +2279,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
7140c6
 		if (!(type == 0 || type == 7 || type == 14)) {
7140c6
 			vendor[0] = '\0';
7140c6
 			model[0] = '\0';
7140c6
-			sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
7140c6
-			if (load_sys(device, buf, sizeof(buf)) == 0) {
7140c6
+
7140c6
+			if (devpath_to_char(device, "vendor", buf,
7140c6
+					    sizeof(buf), 0) == 0) {
7140c6
 				strncpy(vendor, buf, sizeof(vendor));
7140c6
 				vendor[sizeof(vendor) - 1] = '\0';
7140c6
 				c = (char *) &vendor[sizeof(vendor) - 1];
7140c6
@@ -2293,8 +2289,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
7140c6
 					*c-- = '\0';
7140c6
 
7140c6
 			}
7140c6
-			sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
7140c6
-			if (load_sys(device, buf, sizeof(buf)) == 0) {
7140c6
+
7140c6
+			if (devpath_to_char(device, "model", buf,
7140c6
+					    sizeof(buf), 0) == 0) {
7140c6
 				strncpy(model, buf, sizeof(model));
7140c6
 				model[sizeof(model) - 1] = '\0';
7140c6
 				c = (char *) &model[sizeof(model) - 1];
7140c6
@@ -2319,7 +2316,6 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
7140c6
 				}
7140c6
 		} else
7140c6
 			buf[0] = '\0';
7140c6
-		free(device);
7140c6
 
7140c6
 		/* chop device path to 'host%d' and calculate the port number */
7140c6
 		c = strchr(&path[hba_len], '/');
7140c6
@@ -4026,7 +4022,7 @@ static void fd2devname(int fd, char *name)
7140c6
 
7140c6
 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
7140c6
 {
7140c6
-	char path[60];
7140c6
+	char path[PATH_MAX];
7140c6
 	char *name = fd2kname(fd);
7140c6
 
7140c6
 	if (!name)
7140c6
@@ -4035,9 +4031,10 @@ static int nvme_get_serial(int fd, void *buf, size_t buf_len)
7140c6
 	if (strncmp(name, "nvme", 4) != 0)
7140c6
 		return 1;
7140c6
 
7140c6
-	snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
7140c6
+	if (!diskfd_to_devpath(fd, 1, path))
7140c6
+		return 1;
7140c6
 
7140c6
-	return load_sys(path, buf, buf_len);
7140c6
+	return devpath_to_char(path, "serial", buf, buf_len, 0);
7140c6
 }
7140c6
 
7140c6
 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
7140c6
-- 
7140c6
2.7.5
7140c6