mirror of https://github.com/ipxe/ipxe.git
[pci] Auto-resize VPD fields used for non-volatile storage
Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/1/head
parent
e67c79b856
commit
7bf37147b3
|
@ -19,9 +19,11 @@
|
||||||
FILE_LICENCE ( GPL2_OR_LATER );
|
FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <ipxe/nvs.h>
|
#include <ipxe/nvs.h>
|
||||||
#include <ipxe/pci.h>
|
#include <ipxe/pci.h>
|
||||||
#include <ipxe/pcivpd.h>
|
#include <ipxe/pcivpd.h>
|
||||||
|
#include <ipxe/nvo.h>
|
||||||
#include <ipxe/nvsvpd.h>
|
#include <ipxe/nvsvpd.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
|
@ -31,24 +33,50 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from VPD
|
* Read from VPD field
|
||||||
*
|
*
|
||||||
* @v nvs NVS device
|
* @v nvs NVS device
|
||||||
* @v address Starting address
|
* @v field VPD field descriptor
|
||||||
* @v buf Data buffer
|
* @v data Data buffer
|
||||||
* @v len Length of data buffer
|
* @v len Length of data buffer
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
static int nvs_vpd_read ( struct nvs_device *nvs, unsigned int address,
|
static int nvs_vpd_read ( struct nvs_device *nvs, unsigned int field,
|
||||||
void *data, size_t len ) {
|
void *data, size_t len ) {
|
||||||
struct nvs_vpd_device *nvsvpd =
|
struct nvs_vpd_device *nvsvpd =
|
||||||
container_of ( nvs, struct nvs_vpd_device, nvs );
|
container_of ( nvs, struct nvs_vpd_device, nvs );
|
||||||
|
struct pci_device *pci = nvsvpd->vpd.pci;
|
||||||
|
unsigned int address;
|
||||||
|
size_t max_len;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if ( ( rc = pci_vpd_read ( &nvsvpd->vpd, ( nvsvpd->address + address ),
|
/* Allow reading non-existent field */
|
||||||
data, len ) ) != 0 ) {
|
if ( len == 0 )
|
||||||
DBGC ( nvsvpd->vpd.pci, PCI_FMT " NVS could not read "
|
return 0;
|
||||||
"[%04x,%04zx): %s\n", PCI_ARGS ( nvsvpd->vpd.pci ),
|
|
||||||
|
/* Locate VPD field */
|
||||||
|
if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
|
||||||
|
&max_len ) ) != 0 ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
|
||||||
|
PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
|
||||||
|
PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sanity check */
|
||||||
|
if ( len > max_len ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD cannot read %#02zx bytes "
|
||||||
|
"beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
|
||||||
|
PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
|
||||||
|
address, ( address + max_len ) );
|
||||||
|
return -ENXIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read from VPD field */
|
||||||
|
if ( ( rc = pci_vpd_read ( &nvsvpd->vpd, address, data, len ) ) != 0 ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD could not read field "
|
||||||
|
PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
|
||||||
|
PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
|
||||||
address, ( address + len ), strerror ( rc ) );
|
address, ( address + len ), strerror ( rc ) );
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -57,24 +85,47 @@ static int nvs_vpd_read ( struct nvs_device *nvs, unsigned int address,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to VPD
|
* Write to VPD field
|
||||||
*
|
*
|
||||||
* @v nvs NVS device
|
* @v nvs NVS device
|
||||||
* @v address Starting address
|
* @v field VPD field descriptor
|
||||||
* @v buf Data buffer
|
* @v data Data buffer
|
||||||
* @v len Length of data buffer
|
* @v len Length of data buffer
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int address,
|
static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int field,
|
||||||
const void *data, size_t len ) {
|
const void *data, size_t len ) {
|
||||||
struct nvs_vpd_device *nvsvpd =
|
struct nvs_vpd_device *nvsvpd =
|
||||||
container_of ( nvs, struct nvs_vpd_device, nvs );
|
container_of ( nvs, struct nvs_vpd_device, nvs );
|
||||||
|
struct pci_device *pci = nvsvpd->vpd.pci;
|
||||||
|
unsigned int address;
|
||||||
|
size_t max_len;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if ( ( rc = pci_vpd_write ( &nvsvpd->vpd, ( nvsvpd->address + address ),
|
/* Locate VPD field */
|
||||||
data, len ) ) != 0 ) {
|
if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
|
||||||
DBGC ( nvsvpd->vpd.pci, PCI_FMT " NVS could not write "
|
&max_len ) ) != 0 ) {
|
||||||
"[%04x,%04zx): %s\n", PCI_ARGS ( nvsvpd->vpd.pci ),
|
DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
|
||||||
|
PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
|
||||||
|
PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sanity check */
|
||||||
|
if ( len > max_len ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD cannot write %#02zx bytes "
|
||||||
|
"beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
|
||||||
|
PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
|
||||||
|
address, ( address + max_len ) );
|
||||||
|
return -ENXIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write field */
|
||||||
|
if ( ( rc = pci_vpd_write ( &nvsvpd->vpd, address, data,
|
||||||
|
len ) ) != 0 ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD could not write field "
|
||||||
|
PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
|
||||||
|
PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
|
||||||
address, ( address + len ), strerror ( rc ) );
|
address, ( address + len ), strerror ( rc ) );
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
@ -82,6 +133,36 @@ static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int address,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resize VPD field
|
||||||
|
*
|
||||||
|
* @v nvs NVS device
|
||||||
|
* @v field VPD field descriptor
|
||||||
|
* @v data Data buffer
|
||||||
|
* @v len Length of data buffer
|
||||||
|
* @ret rc Return status code
|
||||||
|
*/
|
||||||
|
static int nvs_vpd_resize ( struct nvs_device *nvs, unsigned int field,
|
||||||
|
size_t len ) {
|
||||||
|
struct nvs_vpd_device *nvsvpd =
|
||||||
|
container_of ( nvs, struct nvs_vpd_device, nvs );
|
||||||
|
struct pci_device *pci = nvsvpd->vpd.pci;
|
||||||
|
unsigned int address;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* Resize field */
|
||||||
|
if ( ( rc = pci_vpd_resize ( &nvsvpd->vpd, field, len,
|
||||||
|
&address ) ) != 0 ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD could not resize field "
|
||||||
|
PCI_VPD_FIELD_FMT " to %#02zx bytes: %s\n",
|
||||||
|
PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
|
||||||
|
len, strerror ( rc ) );
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise NVS VPD device
|
* Initialise NVS VPD device
|
||||||
*
|
*
|
||||||
|
@ -89,9 +170,7 @@ static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int address,
|
||||||
* @v pci PCI device
|
* @v pci PCI device
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci,
|
int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci ) {
|
||||||
unsigned int field ) {
|
|
||||||
size_t len;
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
/* Initialise VPD device */
|
/* Initialise VPD device */
|
||||||
|
@ -101,23 +180,54 @@ int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci,
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Locate VPD field */
|
|
||||||
if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &nvsvpd->address,
|
|
||||||
&len ) ) != 0 ) {
|
|
||||||
DBGC ( pci, PCI_FMT " NVS could not locate VPD field "
|
|
||||||
PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
|
|
||||||
PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialise NVS device */
|
/* Initialise NVS device */
|
||||||
nvsvpd->nvs.size = len;
|
|
||||||
nvsvpd->nvs.read = nvs_vpd_read;
|
nvsvpd->nvs.read = nvs_vpd_read;
|
||||||
nvsvpd->nvs.write = nvs_vpd_write;
|
nvsvpd->nvs.write = nvs_vpd_write;
|
||||||
|
|
||||||
DBGC ( pci, PCI_FMT " NVS using VPD field " PCI_VPD_FIELD_FMT " at "
|
return 0;
|
||||||
"[%04x,%04x)\n", PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
|
}
|
||||||
nvsvpd->address, ( nvsvpd->address + nvsvpd->nvs.size ) );
|
|
||||||
|
/**
|
||||||
|
* Resize non-volatile option storage within NVS VPD device
|
||||||
|
*
|
||||||
|
* @v nvo Non-volatile options block
|
||||||
|
* @v len New length
|
||||||
|
* @ret rc Return status code
|
||||||
|
*/
|
||||||
|
static int nvs_vpd_nvo_resize ( struct nvo_block *nvo, size_t len ) {
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* Resize VPD field */
|
||||||
|
if ( ( rc = nvs_vpd_resize ( nvo->nvs, nvo->address, len ) ) != 0 )
|
||||||
|
return rc;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise non-volatile option storage within NVS VPD device
|
||||||
|
*
|
||||||
|
* @v nvsvpd NVS VPD device
|
||||||
|
* @v field VPD field descriptor
|
||||||
|
* @v nvo Non-volatile options block
|
||||||
|
* @v refcnt Containing object reference counter, or NULL
|
||||||
|
*/
|
||||||
|
void nvs_vpd_nvo_init ( struct nvs_vpd_device *nvsvpd, unsigned int field,
|
||||||
|
struct nvo_block *nvo, struct refcnt *refcnt ) {
|
||||||
|
struct pci_device *pci = nvsvpd->vpd.pci;
|
||||||
|
unsigned int address;
|
||||||
|
size_t len;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* Locate VPD field, if present */
|
||||||
|
if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
|
||||||
|
&len ) ) != 0 ) {
|
||||||
|
DBGC ( pci, PCI_FMT " NVS VPD field " PCI_VPD_FIELD_FMT
|
||||||
|
" not present; assuming empty\n",
|
||||||
|
PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ) );
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialise non-volatile options block */
|
||||||
|
nvo_init ( nvo, &nvsvpd->nvs, field, len, nvs_vpd_nvo_resize, refcnt );
|
||||||
|
}
|
||||||
|
|
|
@ -73,6 +73,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#define ERRFILE_spi ( ERRFILE_DRIVER | 0x00110000 )
|
#define ERRFILE_spi ( ERRFILE_DRIVER | 0x00110000 )
|
||||||
#define ERRFILE_i2c_bit ( ERRFILE_DRIVER | 0x00120000 )
|
#define ERRFILE_i2c_bit ( ERRFILE_DRIVER | 0x00120000 )
|
||||||
#define ERRFILE_spi_bit ( ERRFILE_DRIVER | 0x00130000 )
|
#define ERRFILE_spi_bit ( ERRFILE_DRIVER | 0x00130000 )
|
||||||
|
#define ERRFILE_nvsvpd ( ERRFILE_DRIVER | 0x00140000 )
|
||||||
|
|
||||||
#define ERRFILE_3c509 ( ERRFILE_DRIVER | 0x00200000 )
|
#define ERRFILE_3c509 ( ERRFILE_DRIVER | 0x00200000 )
|
||||||
#define ERRFILE_bnx2 ( ERRFILE_DRIVER | 0x00210000 )
|
#define ERRFILE_bnx2 ( ERRFILE_DRIVER | 0x00210000 )
|
||||||
|
|
|
@ -13,21 +13,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <ipxe/nvs.h>
|
#include <ipxe/nvs.h>
|
||||||
#include <ipxe/pcivpd.h>
|
#include <ipxe/pcivpd.h>
|
||||||
|
|
||||||
|
struct nvo_block;
|
||||||
|
struct refcnt;
|
||||||
|
|
||||||
/** An NVS VPD device */
|
/** An NVS VPD device */
|
||||||
struct nvs_vpd_device {
|
struct nvs_vpd_device {
|
||||||
/** NVS device */
|
/** NVS device */
|
||||||
struct nvs_device nvs;
|
struct nvs_device nvs;
|
||||||
/** PCI VPD device */
|
/** PCI VPD device */
|
||||||
struct pci_vpd vpd;
|
struct pci_vpd vpd;
|
||||||
/** Starting address
|
|
||||||
*
|
|
||||||
* This address is added to the NVS address to form the VPD
|
|
||||||
* address.
|
|
||||||
*/
|
|
||||||
unsigned int address;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci,
|
extern int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd,
|
||||||
unsigned int field );
|
struct pci_device *pci );
|
||||||
|
extern void nvs_vpd_nvo_init ( struct nvs_vpd_device *nvsvpd,
|
||||||
|
unsigned int field, struct nvo_block *nvo,
|
||||||
|
struct refcnt *refcnt );
|
||||||
|
|
||||||
#endif /* IPXE_NVSVPD_H */
|
#endif /* IPXE_NVSVPD_H */
|
||||||
|
|
Loading…
Reference in New Issue