summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brown2010-11-24 22:01:24 +0100
committerMichael Brown2010-11-25 00:58:27 +0100
commit69db6e7d8f34b625ba65c879dc2375b8f59adff2 (patch)
treea7186317e95604b9cfea2c0e5e42d6252fa5e00c /src
parent[pci] Standardise debug message format (diff)
downloadipxe-69db6e7d8f34b625ba65c879dc2375b8f59adff2.tar.gz
ipxe-69db6e7d8f34b625ba65c879dc2375b8f59adff2.tar.xz
ipxe-69db6e7d8f34b625ba65c879dc2375b8f59adff2.zip
[pci] Add support for reading and writing PCI Vital Product Data (VPD)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src')
-rw-r--r--src/drivers/bus/pcivpd.c386
-rw-r--r--src/include/ipxe/errfile.h1
-rw-r--r--src/include/ipxe/pcivpd.h165
3 files changed, 552 insertions, 0 deletions
diff --git a/src/drivers/bus/pcivpd.c b/src/drivers/bus/pcivpd.c
new file mode 100644
index 00000000..1f7d832b
--- /dev/null
+++ b/src/drivers/bus/pcivpd.c
@@ -0,0 +1,386 @@
+/*
+ * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <stdint.h>
+#include <unistd.h>
+#include <errno.h>
+#include <byteswap.h>
+#include <ipxe/pci.h>
+#include <ipxe/isapnp.h>
+#include <ipxe/pcivpd.h>
+
+/** @file
+ *
+ * PCI Vital Product Data
+ *
+ */
+
+/**
+ * Initialise PCI Vital Product Data
+ *
+ * @v vpd PCI VPD
+ * @v pci PCI device
+ * @ret rc Return status code
+ */
+int pci_vpd_init ( struct pci_vpd *vpd, struct pci_device *pci ) {
+
+ /* Initialise structure */
+ vpd->pci = pci;
+ pci_vpd_invalidate_cache ( vpd );
+
+ /* Locate VPD capability */
+ vpd->cap = pci_find_capability ( pci, PCI_CAP_ID_VPD );
+ if ( ! vpd->cap ) {
+ DBGC ( vpd, PCI_FMT " does not support VPD\n",
+ PCI_ARGS ( pci ) );
+ return -ENOTTY;
+ }
+
+ DBGC ( vpd, PCI_FMT " VPD is at offset %02x\n",
+ PCI_ARGS ( pci ), vpd->cap );
+ return 0;
+}
+
+/**
+ * Read one dword of PCI Vital Product Data
+ *
+ * @v vpd PCI VPD
+ * @v address Address to read
+ * @ret data Read data
+ * @ret rc Return status code
+ */
+static int pci_vpd_read_dword ( struct pci_vpd *vpd, int address,
+ uint32_t *data ) {
+ struct pci_device *pci = vpd->pci;
+ unsigned int cap = vpd->cap;
+ unsigned int retries;
+ uint16_t flag;
+
+ /* Return cached value, if present */
+ if ( pci_vpd_cache_is_valid ( vpd ) &&
+ ( vpd->cache.address == address ) ) {
+ *data = vpd->cache.data;
+ return 0;
+ }
+
+ /* Initiate read */
+ pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), address );
+
+ /* Wait for read to complete */
+ for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
+
+ /* Check if data is ready */
+ pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
+ if ( flag & PCI_VPD_FLAG ) {
+
+ /* Read data */
+ pci_read_config_dword ( pci, ( cap + PCI_VPD_DATA ),
+ data );
+ DBGC2 ( vpd, PCI_FMT " VPD %04x => %08x\n",
+ PCI_ARGS ( pci ), address, htonl ( *data ) );
+
+ /* Populate cache */
+ vpd->cache.address = address;
+ vpd->cache.data = *data;
+
+ return 0;
+ }
+
+ /* Wait 1ms before retrying */
+ mdelay ( 1 );
+ }
+
+ DBGC ( vpd, PCI_FMT " VPD %04x read via %02x timed out\n",
+ PCI_ARGS ( pci ), address, cap );
+ return -ETIMEDOUT;
+}
+
+/**
+ * Write one dword of PCI Vital Product Data
+ *
+ * @v vpd PCI VPD
+ * @v address Address to write
+ * @v data Data to write
+ * @ret rc Return status code
+ */
+static int pci_vpd_write_dword ( struct pci_vpd *vpd, int address,
+ uint32_t data ) {
+ struct pci_device *pci = vpd->pci;
+ unsigned int cap = vpd->cap;
+ unsigned int retries;
+ uint16_t flag;
+
+ /* Invalidate cache */
+ pci_vpd_invalidate_cache ( vpd );
+
+ DBGC2 ( vpd, PCI_FMT " VPD %04x <= %08x\n",
+ PCI_ARGS ( pci ), address, htonl ( data ) );
+
+ /* Write data */
+ pci_write_config_dword ( pci, ( cap + PCI_VPD_DATA ), data );
+
+ /* Initiate write */
+ pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ),
+ ( address | PCI_VPD_FLAG ) );
+
+ /* Wait for write to complete */
+ for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
+
+ /* Check if write has completed */
+ pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
+ if ( ! ( flag & PCI_VPD_FLAG ) )
+ return 0;
+
+ /* Wait 1ms before retrying */
+ mdelay ( 1 );
+ }
+
+ DBGC ( vpd, PCI_FMT " VPD %04x write via %02x timed out\n",
+ PCI_ARGS ( pci ), address, cap );
+ return -ETIMEDOUT;
+}
+
+/**
+ * Read PCI VPD
+ *
+ * @v vpd PCI VPD
+ * @v address Starting address
+ * @v buf Data buffer
+ * @v len Length of data buffer
+ * @ret rc Return status code
+ */
+int pci_vpd_read ( struct pci_vpd *vpd, unsigned int address, void *buf,
+ size_t len ) {
+ uint8_t *bytes = buf;
+ uint32_t data;
+ size_t skip_len;
+ unsigned int i;
+ int rc;
+
+ /* Calculate length to skip at start of data */
+ skip_len = ( address & 0x03 );
+
+ /* Read data, a dword at a time */
+ for ( address &= ~0x03 ; len ; address += 4 ) {
+
+ /* Read whole dword */
+ if ( ( rc = pci_vpd_read_dword ( vpd, address, &data ) ) != 0 )
+ return rc;
+
+ /* Copy data to buffer */
+ for ( i = 4 ; i ; i-- ) {
+ if ( skip_len ) {
+ skip_len--;
+ } else if ( len ) {
+ *(bytes++) = data;
+ len--;
+ }
+ data = ( ( data << 24 ) | ( data >> 8 ) );
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * Write PCI VPD
+ *
+ * @v vpd PCI VPD
+ * @v address Starting address
+ * @v buf Data buffer
+ * @v len Length of data buffer
+ * @ret rc Return status code
+ */
+int pci_vpd_write ( struct pci_vpd *vpd, unsigned int address, const void *buf,
+ size_t len ) {
+ const uint8_t *bytes = buf;
+ uint32_t data;
+ size_t skip_len;
+ unsigned int i;
+ int rc;
+
+ /* Calculate length to skip at start of data */
+ skip_len = ( address & 0x03 );
+
+ /* Write data, a dword at a time */
+ for ( address &= ~0x03 ; len ; address += 4 ) {
+
+ /* Read existing dword, if necessary */
+ if ( skip_len || ( len <= 0x03 ) ) {
+ if ( ( rc = pci_vpd_read_dword ( vpd, address,
+ &data ) ) != 0 )
+ return rc;
+ }
+
+ /* Copy data from buffer */
+ for ( i = 4 ; i ; i-- ) {
+ if ( skip_len ) {
+ skip_len--;
+ } else if ( len ) {
+ data = ( ( data & ~0xff ) | *(bytes++) );
+ len--;
+ }
+ data = ( ( data << 24 ) | ( data >> 8 ) );
+ }
+
+ /* Write whole dword */
+ if ( ( rc = pci_vpd_write_dword ( vpd, address, data ) ) != 0 )
+ return rc;
+ }
+ return 0;
+}
+
+/**
+ * Dump PCI VPD region (for debugging)
+ *
+ * @v vpd PCI VPD
+ * @v address Starting address
+ * @v len Length of data
+ */
+static void pci_vpd_dump ( struct pci_vpd *vpd, unsigned int address,
+ size_t len ) {
+ int rc;
+
+ /* Do nothing in non-debug builds */
+ if ( ! DBG_LOG )
+ return;
+
+ /* Read data */
+ {
+ char buf[len];
+ if ( ( rc = pci_vpd_read ( vpd, address, buf,
+ sizeof ( buf ) ) ) != 0 )
+ return;
+ DBGC_HDA ( vpd, address, buf, sizeof ( buf ) );
+ }
+}
+
+/**
+ * Locate PCI VPD tag
+ *
+ * @v vpd PCI VPD
+ * @v tag ISAPnP tag
+ * @ret address Address of tag body
+ * @ret len Length of tag body
+ * @ret rc Return status code
+ */
+static int pci_vpd_find_tag ( struct pci_vpd *vpd, unsigned int tag,
+ unsigned int *address, size_t *len ) {
+ uint8_t read_tag;
+ uint16_t read_len;
+ int rc;
+
+ /* Scan through tags looking for a match */
+ *address = 0;
+ do {
+ /* Read tag byte */
+ if ( ( rc = pci_vpd_read ( vpd, (*address)++, &read_tag,
+ sizeof ( read_tag ) ) ) != 0 )
+ return rc;
+
+ /* Extract tag and length */
+ if ( ISAPNP_IS_LARGE_TAG ( read_tag ) ) {
+ if ( ( rc = pci_vpd_read ( vpd, *address, &read_len,
+ sizeof ( read_len ) ) ) != 0)
+ return rc;
+ *address += sizeof ( read_len );
+ read_len = le16_to_cpu ( read_len );
+ read_tag = ISAPNP_LARGE_TAG_NAME ( read_tag );
+ } else {
+ read_len = ISAPNP_SMALL_TAG_LEN ( read_tag );
+ read_tag = ISAPNP_SMALL_TAG_NAME ( read_tag );
+ }
+
+ /* Check for tag match */
+ if ( tag == read_tag ) {
+ *len = read_len;
+ DBGC ( vpd, PCI_FMT " VPD tag %02x is at "
+ "[%04x,%04zx)\n", PCI_ARGS ( vpd->pci ), tag,
+ *address, ( *address + *len ) );
+ return 0;
+ }
+
+ /* Move to next tag */
+ *address += read_len;
+
+ } while ( read_tag != ISAPNP_TAG_END );
+
+ DBGC ( vpd, PCI_FMT " VPD tag %02x not found\n",
+ PCI_ARGS ( vpd->pci ), tag );
+ return -ENOENT;
+}
+
+/**
+ * Locate PCI VPD field
+ *
+ * @v vpd PCI VPD
+ * @v field VPD field descriptor
+ * @ret address Address of field body
+ * @ret len Length of field body
+ * @ret rc Return status code
+ */
+int pci_vpd_find ( struct pci_vpd *vpd, unsigned int field,
+ unsigned int *address, size_t *len ) {
+ struct pci_vpd_field read_field;
+ int rc;
+
+ /* Locate containing tag */
+ if ( ( rc = pci_vpd_find_tag ( vpd, PCI_VPD_TAG ( field ),
+ address, len ) ) != 0 )
+ return rc;
+
+ /* Return immediately if we are searching for a whole-tag field */
+ if ( ! PCI_VPD_KEYWORD ( field ) ) {
+ pci_vpd_dump ( vpd, *address, *len );
+ return 0;
+ }
+
+ /* Scan through fields looking for a match */
+ while ( *len >= sizeof ( read_field ) ) {
+
+ /* Read field header */
+ if ( ( rc = pci_vpd_read ( vpd, *address, &read_field,
+ sizeof ( read_field ) ) ) != 0 )
+ return rc;
+ *address += sizeof ( read_field );
+ *len -= sizeof ( read_field );
+
+ /* Check for keyword match */
+ if ( read_field.keyword == PCI_VPD_KEYWORD ( field ) ) {
+ *len = read_field.len;
+ DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
+ " is at [%04x,%04zx)\n", PCI_ARGS ( vpd->pci ),
+ PCI_VPD_FIELD_ARGS ( field ),
+ *address, ( *address + *len ) );
+ pci_vpd_dump ( vpd, *address, *len );
+ return 0;
+ }
+
+ /* Move to next field */
+ if ( read_field.len > *len )
+ break;
+ *address += read_field.len;
+ *len -= read_field.len;
+ }
+
+ DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " not found\n",
+ PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ) );
+ return -ENOENT;
+}
diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h
index 17e5a4f8..74724149 100644
--- a/src/include/ipxe/errfile.h
+++ b/src/include/ipxe/errfile.h
@@ -67,6 +67,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#define ERRFILE_mca ( ERRFILE_DRIVER | 0x00030000 )
#define ERRFILE_pci ( ERRFILE_DRIVER | 0x00040000 )
#define ERRFILE_linux ( ERRFILE_DRIVER | 0x00050000 )
+#define ERRFILE_pcivpd ( ERRFILE_DRIVER | 0x00060000 )
#define ERRFILE_nvs ( ERRFILE_DRIVER | 0x00100000 )
#define ERRFILE_spi ( ERRFILE_DRIVER | 0x00110000 )
diff --git a/src/include/ipxe/pcivpd.h b/src/include/ipxe/pcivpd.h
new file mode 100644
index 00000000..8b973ea1
--- /dev/null
+++ b/src/include/ipxe/pcivpd.h
@@ -0,0 +1,165 @@
+#ifndef _IPXE_PCIVPD_H
+#define _IPXE_PCIVPD_H
+
+/**
+ * @file
+ *
+ * PCI Vital Product Data
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <stdint.h>
+#include <byteswap.h>
+#include <ipxe/isapnp.h>
+#include <ipxe/pci.h>
+
+/** PCI VPD address register */
+#define PCI_VPD_ADDRESS 0x02
+
+/** PCI VPD write flag */
+#define PCI_VPD_FLAG 0x8000
+
+/** PCI VPD data register */
+#define PCI_VPD_DATA 0x04
+
+/** A PCI VPD field */
+struct pci_vpd_field {
+ /** Keyword */
+ uint16_t keyword;
+ /** Length */
+ uint8_t len;
+} __attribute__ (( packed ));
+
+/** Construct PCI VPD field descriptor
+ *
+ * @v tag ISAPnP tag
+ * @v keyword1 First character of keyword
+ * @v keyword2 Second character of keyword
+ * @ret field VPD field descriptor
+ */
+#define PCI_VPD_FIELD( tag, keyword1, keyword2 ) \
+ ( ( (tag) << 16 ) | ( (keyword2) << 8 ) | ( (keyword1) << 0 ) )
+
+/** Construct PCI VPD whole-tag field descriptor
+ *
+ * @v tag ISAPnP tag
+ * @ret field VPD field descriptor
+ */
+#define PCI_VPD_WHOLE_TAG_FIELD( tag ) PCI_VPD_FIELD ( (tag), '\0', '\0' )
+
+/** Extract PCI VPD ISAPnP tag
+ *
+ * @v field VPD field descriptor
+ * @ret tag ISAPnP tag
+ */
+#define PCI_VPD_TAG( field ) ( (field) >> 16 )
+
+/** Extract PCI VPD keyword
+ *
+ * @v field VPD field descriptor
+ * @ret keyword Keyword
+ */
+#define PCI_VPD_KEYWORD( field ) ( cpu_to_le16 ( (field) & 0xffff ) )
+
+/** PCI VPD field debug message format */
+#define PCI_VPD_FIELD_FMT "%c%c"
+
+/** PCI VPD field debug message arguments */
+#define PCI_VPD_FIELD_ARGS( field ) \
+ ( (field) >> 0 ), ( (field) >> 8 )
+
+/** PCI VPD Read-Only field tag */
+#define PCI_VPD_TAG_RO 0x90
+
+/** PCI VPD Read-Write field tag */
+#define PCI_VPD_TAG_RW 0x91
+
+/** PCI VPD Card Name field descriptor */
+#define PCI_VPD_FIELD_NAME PCI_VPD_WHOLE_TAG_FIELD ( ISAPNP_TAG_ANSISTR )
+
+/** PCI VPD Part Number field descriptor */
+#define PCI_VPD_FIELD_PN PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'P', 'N' )
+
+/** PCI VPD Engineering Change Level field descriptor */
+#define PCI_VPD_FIELD_EC PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'E', 'C' )
+
+/** PCI VPD Fabric Geography field descriptor */
+#define PCI_VPD_FIELD_FG PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'F', 'G' )
+
+/** PCI VPD Location field descriptor */
+#define PCI_VPD_FIELD_LC PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'L', 'C' )
+
+/** PCI VPD Manufacturer ID field descriptor */
+#define PCI_VPD_FIELD_MN PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'M', 'N' )
+
+/** PCI VPD PCI Geography field descriptor */
+#define PCI_VPD_FIELD_PG PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'P', 'G' )
+
+/** PCI VPD Serial Number field descriptor */
+#define PCI_VPD_FIELD_SN PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'S', 'N' )
+
+/** PCI VPD Extended Capability field descriptor */
+#define PCI_VPD_FIELD_CP PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'C', 'P' )
+
+/** PCI VPD Checksum and Reserved field descriptor */
+#define PCI_VPD_FIELD_RV PCI_VPD_FIELD ( PCI_VPD_TAG_RO, 'R', 'V' )
+
+/** PCI VPD Asset Tag field descriptor */
+#define PCI_VPD_FIELD_YA PCI_VPD_FIELD ( PCI_VPD_TAG_RW, 'Y', 'A' )
+
+/** PCI VPD Remaining Read/Write Area field descriptor */
+#define PCI_VPD_FIELD_RW PCI_VPD_FIELD ( PCI_VPD_TAG_RW, 'R', 'W' )
+
+/** Maximum wait for PCI VPD (in ms) */
+#define PCI_VPD_MAX_WAIT_MS 100
+
+/** PCI VPD cache */
+struct pci_vpd_cache {
+ /** Address */
+ int address;
+ /** Data */
+ uint32_t data;
+};
+
+/** PCI VPD */
+struct pci_vpd {
+ /** PCI device */
+ struct pci_device *pci;
+ /** VPD capability offset */
+ int cap;
+ /** Read cache */
+ struct pci_vpd_cache cache;
+};
+
+/**
+ * Check if PCI VPD read cache is valid
+ *
+ * @v vpd PCI VPD
+ * @ret is_valid Read cache is valid
+ */
+static inline __attribute__ (( always_inline )) int
+pci_vpd_cache_is_valid ( struct pci_vpd *vpd ) {
+ return ( vpd->cache.address >= 0 );
+}
+
+/**
+ * Invalidate PCI VPD read cache
+ *
+ * @v vpd PCI VPD
+ */
+static inline __attribute__ (( always_inline )) void
+pci_vpd_invalidate_cache ( struct pci_vpd *vpd ) {
+ vpd->cache.address = -1;
+}
+
+extern int pci_vpd_init ( struct pci_vpd *vpd, struct pci_device *pci );
+extern int pci_vpd_read ( struct pci_vpd *vpd, unsigned int address,
+ void *buf, size_t len );
+extern int pci_vpd_write ( struct pci_vpd *vpd, unsigned int address,
+ const void *buf, size_t len );
+extern int pci_vpd_find ( struct pci_vpd *vpd, unsigned int field,
+ unsigned int *address, size_t *len );
+
+#endif /* _IPXE_PCIVPD_H */