[PATCH 2/2] PCI: Expose PCI 2.1 VPD through sysfs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PCI 2.1 specifies a way of storing VPD in the expansion ROM.  Search for
this and expose it through sysfs in the same way as PCI 2.2 VPD.

Signed-off-by: Ben Hutchings <bhutchings@xxxxxxxxxxxxxx>
---
 drivers/pci/pci-sysfs.c |    2 +-
 drivers/pci/pci.h       |    1 +
 drivers/pci/rom.c       |  138 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+), 1 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 271d41c..97ba6a1 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -693,7 +693,7 @@ int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
 		goto err;
 
 	/* If the device has VPD, try to expose it in sysfs. */
-	if (pdev->vpd) {
+	if (pdev->vpd || pci_vpd_pci21_init(pdev) == 0) {
 		attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
 		if (attr) {
 			pdev->vpd->attr = attr;
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 0a497c1..f62ad21 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -30,6 +30,7 @@ struct pci_vpd {
 	struct bin_attribute *attr; /* descriptor for sysfs VPD entry */
 };
 
+extern int pci_vpd_pci21_init(struct pci_dev *dev);
 extern int pci_vpd_pci22_init(struct pci_dev *dev);
 static inline void pci_vpd_release(struct pci_dev *dev)
 {
diff --git a/drivers/pci/rom.c b/drivers/pci/rom.c
index 94d2afd..f070599 100644
--- a/drivers/pci/rom.c
+++ b/drivers/pci/rom.c
@@ -283,5 +283,143 @@ void pci_cleanup_rom(struct pci_dev *pdev)
 	}
 }
 
+struct pci_vpd_pci21 {
+	struct pci_vpd base;
+	size_t addr; /* offset within ROM */
+	u16 size;
+};
+
+static int pci_vpd_pci21_read(struct pci_dev *pdev, int pos, int size,
+			      char *buf)
+{
+	struct pci_vpd_pci21 *vpd =
+		container_of(pdev->vpd, struct pci_vpd_pci21, base);
+	void __iomem *rom;
+	size_t win_size;
+	int ret;
+	int i;
+
+	if (pos < 0 || pos > (int)vpd->size || size > vpd->size - pos)
+		return -EINVAL;
+
+	rom = __pci_map_rom(pdev, &win_size);
+	if (!rom)
+		return -EIO;
+	if (vpd->addr > win_size || vpd->size > win_size - vpd->addr) {
+		ret = -EIO;
+		goto out_unmap;
+	}
+
+	memcpy_fromio(buf, rom + vpd->addr + pos, size);
+	ret = size;
+
+out_unmap:
+	pci_unmap_rom(pdev, rom);
+	return ret;
+}
+
+static int pci_vpd_pci21_write(struct pci_dev *pdev, int pos, int size,
+			       const char *buf)
+{
+	return -EROFS;
+}
+
+static int pci_vpd_pci21_get_size(struct pci_dev *pdev)
+{
+	struct pci_vpd_pci21 *vpd =
+		container_of(pdev->vpd, struct pci_vpd_pci21, base);
+	return vpd->size;
+}
+
+static void pci_vpd_pci21_release(struct pci_dev *pdev)
+{
+	kfree(container_of(pdev->vpd, struct pci_vpd_pci21, base));
+}
+
+static struct pci_vpd_ops pci_vpd_pci21_ops = {
+	.read = pci_vpd_pci21_read,
+	.write = pci_vpd_pci21_write,
+	.get_size = pci_vpd_pci21_get_size,
+	.release = pci_vpd_pci21_release
+};
+
+int pci_vpd_pci21_init(struct pci_dev *pdev)
+{
+	struct pci_vpd_pci21 *vpd;
+	void __iomem *rom, *image, *pds;
+	size_t rom_size, image_size, addr = 0, limit = 0;
+	bool last;
+	int ret;
+
+	if (pci_resource_len(pdev, PCI_ROM_RESOURCE) == 0 &&
+	    !(pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW))
+		return -ENODEV;
+
+	rom = __pci_map_rom(pdev, &rom_size);
+	if (!rom)
+		return -ENODEV;
+
+	/* Find first image with VPD */
+	image = rom;
+	for (;;) {
+		image_size = pci_get_rom_image_size(rom, rom_size,
+						    image, &last);
+		if (image_size != 0) {
+			pds = image + readw(image + 24);
+			addr = readw(pds + 8);
+			if (addr != 0 && addr < image_size)
+				break;
+		}
+		if (last) {
+			ret = -ENODEV;
+			goto out_unmap;
+		}
+		image += image_size;
+	}
+	addr += image - rom;
+	limit = image - rom + min_t(size_t, image_size, 1 << 16);
+
+	vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
+	if (!vpd) {
+		ret = -ENOMEM;
+		goto out_unmap;
+	}
+	vpd->base.ops = &pci_vpd_pci21_ops;
+	vpd->addr = addr;
+	vpd->size = limit - addr; /* in case we can't find an ending tag */
+	pdev->vpd = &vpd->base;
+	ret = 0;
+
+	/* Follow chain of resources to find VPD size. */
+	for (;;) {
+		u8 tag;
+		u16 res_size;
+
+		tag = readb(rom + addr);
+		if (tag & 0x80) {
+			if (addr > limit - 3)
+				break;
+			res_size = readb(rom + addr + 1);
+			res_size += readb(rom + addr + 2) << 8;
+			addr += 3;
+		} else {
+			res_size = tag & 7;
+			addr += 1;
+			if (res_size == 0) {
+				/* Valid ending tag */
+				vpd->size = addr - vpd->addr;
+				break;
+			}
+		}
+		if (addr >= limit - res_size)
+			break;
+		addr += res_size;
+	}
+
+out_unmap:
+	pci_unmap_rom(pdev, rom);
+	return ret;
+}
+
 EXPORT_SYMBOL(pci_map_rom);
 EXPORT_SYMBOL(pci_unmap_rom);

-- 
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [DMA Engine]     [Linux Coverity]     [Linux USB]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Greybus]

  Powered by Linux