2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/pcivpd.h>
28 #include <ipxe/nvsvpd.h>
32 * Non-Volatile Storage using Vital Product Data
40 * @v field VPD field descriptor
42 * @v len Length of data buffer
43 * @ret rc Return status code
45 static int nvs_vpd_read ( struct nvs_device *nvs, unsigned int field,
46 void *data, size_t len ) {
47 struct nvs_vpd_device *nvsvpd =
48 container_of ( nvs, struct nvs_vpd_device, nvs );
49 struct pci_device *pci = nvsvpd->vpd.pci;
54 /* Allow reading non-existent field */
58 /* Locate VPD field */
59 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
61 DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
62 PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
63 PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
68 if ( len > max_len ) {
69 DBGC ( pci, PCI_FMT " NVS VPD cannot read %#02zx bytes "
70 "beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
71 PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
72 address, ( address + max_len ) );
76 /* Read from VPD field */
77 if ( ( rc = pci_vpd_read ( &nvsvpd->vpd, address, data, len ) ) != 0 ) {
78 DBGC ( pci, PCI_FMT " NVS VPD could not read field "
79 PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
80 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
81 address, ( address + len ), strerror ( rc ) );
92 * @v field VPD field descriptor
94 * @v len Length of data buffer
95 * @ret rc Return status code
97 static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int field,
98 const void *data, size_t len ) {
99 struct nvs_vpd_device *nvsvpd =
100 container_of ( nvs, struct nvs_vpd_device, nvs );
101 struct pci_device *pci = nvsvpd->vpd.pci;
102 unsigned int address;
106 /* Locate VPD field */
107 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
108 &max_len ) ) != 0 ) {
109 DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
110 PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
111 PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
116 if ( len > max_len ) {
117 DBGC ( pci, PCI_FMT " NVS VPD cannot write %#02zx bytes "
118 "beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
119 PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
120 address, ( address + max_len ) );
125 if ( ( rc = pci_vpd_write ( &nvsvpd->vpd, address, data,
127 DBGC ( pci, PCI_FMT " NVS VPD could not write field "
128 PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
129 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
130 address, ( address + len ), strerror ( rc ) );
141 * @v field VPD field descriptor
142 * @v data Data buffer
143 * @v len Length of data buffer
144 * @ret rc Return status code
146 static int nvs_vpd_resize ( struct nvs_device *nvs, unsigned int field,
148 struct nvs_vpd_device *nvsvpd =
149 container_of ( nvs, struct nvs_vpd_device, nvs );
150 struct pci_device *pci = nvsvpd->vpd.pci;
151 unsigned int address;
155 if ( ( rc = pci_vpd_resize ( &nvsvpd->vpd, field, len,
156 &address ) ) != 0 ) {
157 DBGC ( pci, PCI_FMT " NVS VPD could not resize field "
158 PCI_VPD_FIELD_FMT " to %#02zx bytes: %s\n",
159 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
160 len, strerror ( rc ) );
168 * Initialise NVS VPD device
170 * @v nvsvpd NVS VPD device
172 * @ret rc Return status code
174 int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci ) {
177 /* Initialise VPD device */
178 if ( ( rc = pci_vpd_init ( &nvsvpd->vpd, pci ) ) != 0 ) {
179 DBGC ( pci, PCI_FMT " NVS could not initialise "
180 "VPD: %s\n", PCI_ARGS ( pci ), strerror ( rc ) );
184 /* Initialise NVS device */
185 nvsvpd->nvs.read = nvs_vpd_read;
186 nvsvpd->nvs.write = nvs_vpd_write;
192 * Resize non-volatile option storage within NVS VPD device
194 * @v nvo Non-volatile options block
196 * @ret rc Return status code
198 static int nvs_vpd_nvo_resize ( struct nvo_block *nvo, size_t len ) {
201 /* Resize VPD field */
202 if ( ( rc = nvs_vpd_resize ( nvo->nvs, nvo->address, len ) ) != 0 )
209 * Initialise non-volatile option storage within NVS VPD device
211 * @v nvsvpd NVS VPD device
212 * @v field VPD field descriptor
213 * @v nvo Non-volatile options block
214 * @v refcnt Containing object reference counter, or NULL
216 void nvs_vpd_nvo_init ( struct nvs_vpd_device *nvsvpd, unsigned int field,
217 struct nvo_block *nvo, struct refcnt *refcnt ) {
218 struct pci_device *pci = nvsvpd->vpd.pci;
219 unsigned int address;
223 /* Locate VPD field, if present */
224 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
226 DBGC ( pci, PCI_FMT " NVS VPD field " PCI_VPD_FIELD_FMT
227 " not present; assuming empty\n",
228 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ) );
232 /* Initialise non-volatile options block */
233 nvo_init ( nvo, &nvsvpd->nvs, field, len, nvs_vpd_nvo_resize, refcnt );