mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] PCI: ROM access changes to support PCI 2.1 VPD
       [not found] <cover.1213391292.git.bhutchings@solarflare.com>
@ 2008-06-13 21:29 ` Ben Hutchings
  2008-06-13 21:29 ` [PATCH 2/2] PCI: Expose PCI 2.1 VPD through sysfs Ben Hutchings
  1 sibling, 0 replies; 2+ messages in thread
From: Ben Hutchings @ 2008-06-13 21:29 UTC (permalink / raw)
  To: Jesse Barnes; +Cc: Matthew Wilcox, linux-kernel, linux-pci

Move body of ROM image sizing loop into a separate function so it can be
reused for finding VPD.

Move first part of pci_map_rom() into a separate function for use when
reading VPD.

Signed-off-by: Ben Hutchings <bhutchings@solarflare.com>
---
 drivers/pci/rom.c |  107 ++++++++++++++++++++++++++++++++--------------------
 1 files changed, 66 insertions(+), 41 deletions(-)

diff --git a/drivers/pci/rom.c b/drivers/pci/rom.c
index bd5c0e0..94d2afd 100644
--- a/drivers/pci/rom.c
+++ b/drivers/pci/rom.c
@@ -54,59 +54,65 @@ static void pci_disable_rom(struct pci_dev *pdev)
 }
 
 /**
- * pci_get_rom_size - obtain the actual size of the ROM image
- * @rom: kernel virtual pointer to image of ROM
+ * pci_get_rom_image_size - obtain the actual size of a ROM image
+ * @rom: kernel virtual pointer to ROM
  * @size: size of PCI window
- *  return: size of actual ROM image
+ * @image: kernel virtual pointer to image
+ * @last: pointer to return flag for whether this is the last image
+ *  return: size of image, or 0 if image is invalid
  *
  * Determine the actual length of the ROM image.
+ */
+static size_t pci_get_rom_image_size(void __iomem *rom, size_t size,
+				     void __iomem *image, bool *last)
+{
+	void __iomem *pds;
+	size_t image_size;
+
+	/* Standard PCI ROMs start out with these bytes 55 AA */
+	if (readb(image) != 0x55 || readb(image + 1) != 0xAA)
+		goto invalid;
+
+	/* get the PCI data structure and check its signature */
+	pds = image + readw(image + 24);
+	if (readb(pds) != 'P' || readb(pds + 1) != 'C' ||
+	    readb(pds + 2) != 'I' || readb(pds + 3) != 'R')
+		goto invalid;
+
+	image_size = readw(pds + 16) * 512;
+	*last = !!(readb(pds + 21) & 0x80);
+
+	/* Check that the size does not extend outside the PCI window */
+	return min_t(size_t, image_size, rom + size - image);
+
+invalid:
+	*last = true;
+	return 0;
+}
+
+/**
+ * pci_get_rom_size - obtain the total size of the ROM images
+ * @rom: kernel virtual pointer to ROM
+ * @size: size of PCI window
+ *  return: size of actual ROM images
+ *
+ * Determine the actual length of the ROM images.
  * The PCI window size could be much larger than the
  * actual image size.
  */
 size_t pci_get_rom_size(void __iomem *rom, size_t size)
 {
 	void __iomem *image;
-	int last_image;
+	bool last;
 
 	image = rom;
-	do {
-		void __iomem *pds;
-		/* Standard PCI ROMs start out with these bytes 55 AA */
-		if (readb(image) != 0x55)
-			break;
-		if (readb(image + 1) != 0xAA)
-			break;
-		/* get the PCI data structure and check its signature */
-		pds = image + readw(image + 24);
-		if (readb(pds) != 'P')
-			break;
-		if (readb(pds + 1) != 'C')
-			break;
-		if (readb(pds + 2) != 'I')
-			break;
-		if (readb(pds + 3) != 'R')
-			break;
-		last_image = readb(pds + 21) & 0x80;
-		/* this length is reliable */
-		image += readw(pds + 16) * 512;
-	} while (!last_image);
-
-	/* never return a size larger than the PCI resource window */
-	/* there are known ROMs that get the size wrong */
-	return min((size_t)(image - rom), size);
+	do
+		image += pci_get_rom_image_size(rom, size, image, &last);
+	while (!last);
+	return image - rom;
 }
 
-/**
- * pci_map_rom - map a PCI ROM to kernel space
- * @pdev: pointer to pci device struct
- * @size: pointer to receive size of pci window over ROM
- * @return: kernel virtual pointer to image of ROM
- *
- * Map a PCI ROM into kernel space. If ROM is boot video ROM,
- * the shadow BIOS copy will be returned instead of the
- * actual ROM.
- */
-void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
+static void __iomem *__pci_map_rom(struct pci_dev *pdev, size_t *size)
 {
 	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
 	loff_t start;
@@ -150,9 +156,28 @@ void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
 				    IORESOURCE_ROM_SHADOW |
 				    IORESOURCE_ROM_COPY)))
 			pci_disable_rom(pdev);
-		return NULL;
 	}
 
+	return rom;
+}
+
+/**
+ * pci_map_rom - map a PCI ROM to kernel space
+ * @pdev: pointer to pci device struct
+ * @size: pointer to receive size of pci window over ROM
+ * @return: kernel virtual pointer to image of ROM
+ *
+ * Map a PCI ROM into kernel space. If ROM is boot video ROM,
+ * the shadow BIOS copy will be returned instead of the
+ * actual ROM.
+ */
+void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
+{
+	void __iomem *rom = __pci_map_rom(pdev, size);
+
+	if (!rom)
+		return NULL;
+
 	/*
 	 * Try to find the true size of the ROM since sometimes the PCI window
 	 * size is much larger than the actual size of the ROM.

-- 
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] PCI: Expose PCI 2.1 VPD through sysfs
       [not found] <cover.1213391292.git.bhutchings@solarflare.com>
  2008-06-13 21:29 ` [PATCH 1/2] PCI: ROM access changes to support PCI 2.1 VPD Ben Hutchings
@ 2008-06-13 21:29 ` Ben Hutchings
  1 sibling, 0 replies; 2+ messages in thread
From: Ben Hutchings @ 2008-06-13 21:29 UTC (permalink / raw)
  To: Jesse Barnes; +Cc: Matthew Wilcox, linux-kernel, linux-pci

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@solarflare.com>
---
 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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-06-13 21:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cover.1213391292.git.bhutchings@solarflare.com>
2008-06-13 21:29 ` [PATCH 1/2] PCI: ROM access changes to support PCI 2.1 VPD Ben Hutchings
2008-06-13 21:29 ` [PATCH 2/2] PCI: Expose PCI 2.1 VPD through sysfs Ben Hutchings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®