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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 #include <ipxe/pcivpd.h>
32 #include <ipxe/nvsvpd.h>
36 * Non-Volatile Storage using Vital Product Data
44 * @v field VPD field descriptor
46 * @v len Length of data buffer
47 * @ret rc Return status code
49 static int nvs_vpd_read ( struct nvs_device *nvs, unsigned int field,
50 void *data, size_t len ) {
51 struct nvs_vpd_device *nvsvpd =
52 container_of ( nvs, struct nvs_vpd_device, nvs );
53 struct pci_device *pci = nvsvpd->vpd.pci;
58 /* Allow reading non-existent field */
62 /* Locate VPD field */
63 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
65 DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
66 PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
67 PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
72 if ( len > max_len ) {
73 DBGC ( pci, PCI_FMT " NVS VPD cannot read %#02zx bytes "
74 "beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
75 PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
76 address, ( address + max_len ) );
80 /* Read from VPD field */
81 if ( ( rc = pci_vpd_read ( &nvsvpd->vpd, address, data, len ) ) != 0 ) {
82 DBGC ( pci, PCI_FMT " NVS VPD could not read field "
83 PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
84 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
85 address, ( address + len ), strerror ( rc ) );
96 * @v field VPD field descriptor
98 * @v len Length of data buffer
99 * @ret rc Return status code
101 static int nvs_vpd_write ( struct nvs_device *nvs, unsigned int field,
102 const void *data, size_t len ) {
103 struct nvs_vpd_device *nvsvpd =
104 container_of ( nvs, struct nvs_vpd_device, nvs );
105 struct pci_device *pci = nvsvpd->vpd.pci;
106 unsigned int address;
110 /* Locate VPD field */
111 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
112 &max_len ) ) != 0 ) {
113 DBGC ( pci, PCI_FMT " NVS VPD could not locate field "
114 PCI_VPD_FIELD_FMT ": %s\n", PCI_ARGS ( pci ),
115 PCI_VPD_FIELD_ARGS ( field ), strerror ( rc ) );
120 if ( len > max_len ) {
121 DBGC ( pci, PCI_FMT " NVS VPD cannot write %#02zx bytes "
122 "beyond field " PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
123 PCI_ARGS ( pci ), len, PCI_VPD_FIELD_ARGS ( field ),
124 address, ( address + max_len ) );
129 if ( ( rc = pci_vpd_write ( &nvsvpd->vpd, address, data,
131 DBGC ( pci, PCI_FMT " NVS VPD could not write field "
132 PCI_VPD_FIELD_FMT " at [%04x,%04zx): %s\n",
133 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
134 address, ( address + len ), strerror ( rc ) );
145 * @v field VPD field descriptor
146 * @v data Data buffer
147 * @v len Length of data buffer
148 * @ret rc Return status code
150 static int nvs_vpd_resize ( struct nvs_device *nvs, unsigned int field,
152 struct nvs_vpd_device *nvsvpd =
153 container_of ( nvs, struct nvs_vpd_device, nvs );
154 struct pci_device *pci = nvsvpd->vpd.pci;
155 unsigned int address;
159 if ( ( rc = pci_vpd_resize ( &nvsvpd->vpd, field, len,
160 &address ) ) != 0 ) {
161 DBGC ( pci, PCI_FMT " NVS VPD could not resize field "
162 PCI_VPD_FIELD_FMT " to %#02zx bytes: %s\n",
163 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ),
164 len, strerror ( rc ) );
172 * Initialise NVS VPD device
174 * @v nvsvpd NVS VPD device
176 * @ret rc Return status code
178 int nvs_vpd_init ( struct nvs_vpd_device *nvsvpd, struct pci_device *pci ) {
181 /* Initialise VPD device */
182 if ( ( rc = pci_vpd_init ( &nvsvpd->vpd, pci ) ) != 0 ) {
183 DBGC ( pci, PCI_FMT " NVS could not initialise "
184 "VPD: %s\n", PCI_ARGS ( pci ), strerror ( rc ) );
188 /* Initialise NVS device */
189 nvsvpd->nvs.read = nvs_vpd_read;
190 nvsvpd->nvs.write = nvs_vpd_write;
196 * Resize non-volatile option storage within NVS VPD device
198 * @v nvo Non-volatile options block
200 * @ret rc Return status code
202 static int nvs_vpd_nvo_resize ( struct nvo_block *nvo, size_t len ) {
205 /* Resize VPD field */
206 if ( ( rc = nvs_vpd_resize ( nvo->nvs, nvo->address, len ) ) != 0 )
213 * Initialise non-volatile option storage within NVS VPD device
215 * @v nvsvpd NVS VPD device
216 * @v field VPD field descriptor
217 * @v nvo Non-volatile options block
218 * @v refcnt Containing object reference counter, or NULL
220 void nvs_vpd_nvo_init ( struct nvs_vpd_device *nvsvpd, unsigned int field,
221 struct nvo_block *nvo, struct refcnt *refcnt ) {
222 struct pci_device *pci = nvsvpd->vpd.pci;
223 unsigned int address;
227 /* Locate VPD field, if present */
228 if ( ( rc = pci_vpd_find ( &nvsvpd->vpd, field, &address,
230 DBGC ( pci, PCI_FMT " NVS VPD field " PCI_VPD_FIELD_FMT
231 " not present; assuming empty\n",
232 PCI_ARGS ( pci ), PCI_VPD_FIELD_ARGS ( field ) );
236 /* Initialise non-volatile options block */
237 nvo_init ( nvo, &nvsvpd->nvs, field, len, nvs_vpd_nvo_resize, refcnt );