return id;
}
+/* Description: Read text value of dev_path/entry field
+ * Parameters:
+ * dev_path - sysfs path to the device
+ * entry - entry to be read
+ * buf - buffer for read value
+ * len - size of buf
+ * verbose - error logging level
+ */
+int devpath_to_char(const char *dev_path, const char *entry, char *buf, int len,
+ int verbose)
+{
+ char path[PATH_MAX];
+
+ snprintf(path, sizeof(path), "%s/%s", dev_path, entry);
+ if (load_sys(path, buf, len)) {
+ if (verbose)
+ pr_err("Cannot read %s, aborting\n", path);
+ return 1;
+ }
+
+ return 0;
+}
+
struct sys_dev *find_intel_devices(void)
{
struct sys_dev *ahci, *isci, *nvme;
char vendor[64];
char buf[1024];
int major, minor;
- char *device;
+ char device[PATH_MAX];
char *c;
int port;
int type;
continue;
}
- /* retrieve the scsi device type */
- if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
+ /* retrieve the scsi device */
+ if (!devt_to_devpath(makedev(major, minor), 1, device)) {
if (verbose > 0)
- pr_err("failed to allocate 'device'\n");
+ pr_err("failed to get device\n");
err = 2;
break;
}
- sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
- if (load_sys(device, buf, sizeof(buf)) != 0) {
- if (verbose > 0)
- pr_err("failed to read device type for %s\n",
- path);
+ if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
err = 2;
- free(device);
break;
}
type = strtoul(buf, NULL, 10);
if (!(type == 0 || type == 7 || type == 14)) {
vendor[0] = '\0';
model[0] = '\0';
- sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
- if (load_sys(device, buf, sizeof(buf)) == 0) {
+
+ if (devpath_to_char(device, "vendor", buf,
+ sizeof(buf), 0) == 0) {
strncpy(vendor, buf, sizeof(vendor));
vendor[sizeof(vendor) - 1] = '\0';
c = (char *) &vendor[sizeof(vendor) - 1];
*c-- = '\0';
}
- sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
- if (load_sys(device, buf, sizeof(buf)) == 0) {
+
+ if (devpath_to_char(device, "model", buf,
+ sizeof(buf), 0) == 0) {
strncpy(model, buf, sizeof(model));
model[sizeof(model) - 1] = '\0';
c = (char *) &model[sizeof(model) - 1];
}
} else
buf[0] = '\0';
- free(device);
/* chop device path to 'host%d' and calculate the port number */
c = strchr(&path[hba_len], '/');
static int nvme_get_serial(int fd, void *buf, size_t buf_len)
{
- char path[60];
+ char path[PATH_MAX];
char *name = fd2kname(fd);
if (!name)
if (strncmp(name, "nvme", 4) != 0)
return 1;
- snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
+ if (!diskfd_to_devpath(fd, 1, path))
+ return 1;
- return load_sys(path, buf, buf_len);
+ return devpath_to_char(path, "serial", buf, buf_len, 0);
}
extern int scsi_get_serial(int fd, void *buf, size_t buf_len);