/* * Copyright (C) 2010 Michael Brown . * * 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 #include #include #include #include #include #include /** @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; }