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 );
32 #include <ipxe/isapnp.h>
33 #include <ipxe/pcivpd.h>
37 * PCI Vital Product Data
42 * Initialise PCI Vital Product Data
46 * @ret rc Return status code
48 int pci_vpd_init ( struct pci_vpd *vpd, struct pci_device *pci ) {
50 /* Initialise structure */
52 pci_vpd_invalidate_cache ( vpd );
54 /* Locate VPD capability */
55 vpd->cap = pci_find_capability ( pci, PCI_CAP_ID_VPD );
57 DBGC ( vpd, PCI_FMT " does not support VPD\n",
62 DBGC ( vpd, PCI_FMT " VPD is at offset %02x\n",
63 PCI_ARGS ( pci ), vpd->cap );
68 * Read one dword of PCI Vital Product Data
71 * @v address Address to read
73 * @ret rc Return status code
75 static int pci_vpd_read_dword ( struct pci_vpd *vpd, int address,
77 struct pci_device *pci = vpd->pci;
78 unsigned int cap = vpd->cap;
82 /* Fail if no VPD present */
86 /* Return cached value, if present */
87 if ( pci_vpd_cache_is_valid ( vpd ) &&
88 ( vpd->cache.address == address ) ) {
89 *data = vpd->cache.data;
94 pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), address );
96 /* Wait for read to complete */
97 for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
99 /* Check if data is ready */
100 pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
101 if ( flag & PCI_VPD_FLAG ) {
104 pci_read_config_dword ( pci, ( cap + PCI_VPD_DATA ),
106 DBGC2 ( vpd, PCI_FMT " VPD %04x => %08x\n",
107 PCI_ARGS ( pci ), address, htonl ( *data ) );
110 vpd->cache.address = address;
111 vpd->cache.data = *data;
116 /* Wait 1ms before retrying */
120 DBGC ( vpd, PCI_FMT " VPD %04x read via %02x timed out\n",
121 PCI_ARGS ( pci ), address, cap );
126 * Write one dword of PCI Vital Product Data
129 * @v address Address to write
130 * @v data Data to write
131 * @ret rc Return status code
133 static int pci_vpd_write_dword ( struct pci_vpd *vpd, int address,
135 struct pci_device *pci = vpd->pci;
136 unsigned int cap = vpd->cap;
137 unsigned int retries;
140 /* Fail if no VPD present */
144 /* Invalidate cache */
145 pci_vpd_invalidate_cache ( vpd );
147 DBGC2 ( vpd, PCI_FMT " VPD %04x <= %08x\n",
148 PCI_ARGS ( pci ), address, htonl ( data ) );
151 pci_write_config_dword ( pci, ( cap + PCI_VPD_DATA ), data );
154 pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ),
155 ( address | PCI_VPD_FLAG ) );
157 /* Wait for write to complete */
158 for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
160 /* Check if write has completed */
161 pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
162 if ( ! ( flag & PCI_VPD_FLAG ) )
165 /* Wait 1ms before retrying */
169 DBGC ( vpd, PCI_FMT " VPD %04x write via %02x timed out\n",
170 PCI_ARGS ( pci ), address, cap );
178 * @v address Starting address
180 * @v len Length of data buffer
181 * @ret rc Return status code
183 int pci_vpd_read ( struct pci_vpd *vpd, unsigned int address, void *buf,
185 uint8_t *bytes = buf;
191 /* Calculate length to skip at start of data */
192 skip_len = ( address & 0x03 );
194 /* Read data, a dword at a time */
195 for ( address &= ~0x03 ; len ; address += 4 ) {
197 /* Read whole dword */
198 if ( ( rc = pci_vpd_read_dword ( vpd, address, &data ) ) != 0 )
201 /* Copy data to buffer */
202 for ( i = 4 ; i ; i-- ) {
209 data = ( ( data << 24 ) | ( data >> 8 ) );
220 * @v address Starting address
222 * @v len Length of data buffer
223 * @ret rc Return status code
225 int pci_vpd_write ( struct pci_vpd *vpd, unsigned int address, const void *buf,
227 const uint8_t *bytes = buf;
233 /* Calculate length to skip at start of data */
234 skip_len = ( address & 0x03 );
236 /* Write data, a dword at a time */
237 for ( address &= ~0x03 ; len ; address += 4 ) {
239 /* Read existing dword, if necessary */
240 if ( skip_len || ( len <= 0x03 ) ) {
241 if ( ( rc = pci_vpd_read_dword ( vpd, address,
246 /* Copy data from buffer */
247 for ( i = 4 ; i ; i-- ) {
251 data = ( ( data & ~0xff ) | *(bytes++) );
254 data = ( ( data << 24 ) | ( data >> 8 ) );
257 /* Write whole dword */
258 if ( ( rc = pci_vpd_write_dword ( vpd, address, data ) ) != 0 )
265 * Dump PCI VPD region (for debugging)
268 * @v address Starting address
269 * @v len Length of data
271 static void pci_vpd_dump ( struct pci_vpd *vpd, unsigned int address,
275 /* Do nothing in non-debug builds */
282 if ( ( rc = pci_vpd_read ( vpd, address, buf,
283 sizeof ( buf ) ) ) != 0 )
285 DBGC_HDA ( vpd, address, buf, sizeof ( buf ) );
294 * @ret address Address of tag body
295 * @ret len Length of tag body
296 * @ret rc Return status code
298 static int pci_vpd_find_tag ( struct pci_vpd *vpd, unsigned int tag,
299 unsigned int *address, size_t *len ) {
304 /* Scan through tags looking for a match */
308 if ( ( rc = pci_vpd_read ( vpd, (*address)++, &read_tag,
309 sizeof ( read_tag ) ) ) != 0 )
312 /* Extract tag and length */
313 if ( ISAPNP_IS_LARGE_TAG ( read_tag ) ) {
314 if ( ( rc = pci_vpd_read ( vpd, *address, &read_len,
315 sizeof ( read_len ) ) ) != 0)
317 *address += sizeof ( read_len );
318 read_len = le16_to_cpu ( read_len );
319 read_tag = ISAPNP_LARGE_TAG_NAME ( read_tag );
321 read_len = ISAPNP_SMALL_TAG_LEN ( read_tag );
322 read_tag = ISAPNP_SMALL_TAG_NAME ( read_tag );
325 /* Check for tag match */
326 if ( tag == read_tag ) {
328 DBGC ( vpd, PCI_FMT " VPD tag %02x is at "
329 "[%04x,%04zx)\n", PCI_ARGS ( vpd->pci ), tag,
330 *address, ( *address + *len ) );
334 /* Move to next tag */
335 *address += read_len;
337 } while ( read_tag != ISAPNP_TAG_END );
339 DBGC ( vpd, PCI_FMT " VPD tag %02x not found\n",
340 PCI_ARGS ( vpd->pci ), tag );
345 * Locate PCI VPD field
348 * @v field VPD field descriptor
349 * @ret address Address of field body
350 * @ret len Length of field body
351 * @ret rc Return status code
353 int pci_vpd_find ( struct pci_vpd *vpd, unsigned int field,
354 unsigned int *address, size_t *len ) {
355 struct pci_vpd_field read_field;
358 /* Locate containing tag */
359 if ( ( rc = pci_vpd_find_tag ( vpd, PCI_VPD_TAG ( field ),
360 address, len ) ) != 0 )
363 /* Return immediately if we are searching for a whole-tag field */
364 if ( ! PCI_VPD_KEYWORD ( field ) ) {
365 pci_vpd_dump ( vpd, *address, *len );
369 /* Scan through fields looking for a match */
370 while ( *len >= sizeof ( read_field ) ) {
372 /* Read field header */
373 if ( ( rc = pci_vpd_read ( vpd, *address, &read_field,
374 sizeof ( read_field ) ) ) != 0 )
376 *address += sizeof ( read_field );
377 *len -= sizeof ( read_field );
379 /* Check for keyword match */
380 if ( read_field.keyword == PCI_VPD_KEYWORD ( field ) ) {
381 *len = read_field.len;
382 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
383 " is at [%04x,%04zx)\n", PCI_ARGS ( vpd->pci ),
384 PCI_VPD_FIELD_ARGS ( field ),
385 *address, ( *address + *len ) );
386 pci_vpd_dump ( vpd, *address, *len );
390 /* Move to next field */
391 if ( read_field.len > *len )
393 *address += read_field.len;
394 *len -= read_field.len;
397 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " not found\n",
398 PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ) );
406 * @v field VPD field descriptor
407 * @v len New length of field body
408 * @ret address Address of field body
409 * @ret rc Return status code
411 int pci_vpd_resize ( struct pci_vpd *vpd, unsigned int field, size_t len,
412 unsigned int *address ) {
413 struct pci_vpd_field rw_field;
414 struct pci_vpd_field old_field;
415 struct pci_vpd_field new_field;
416 unsigned int rw_address;
417 unsigned int old_address;
418 unsigned int copy_address;
419 unsigned int dst_address;
420 unsigned int dump_address;
423 size_t available_len;
430 assert ( PCI_VPD_TAG ( field ) == PCI_VPD_TAG_RW );
431 assert ( PCI_VPD_KEYWORD ( field ) != 0 );
432 assert ( field != PCI_VPD_FIELD_RW );
434 /* Locate 'RW' field */
435 if ( ( rc = pci_vpd_find ( vpd, PCI_VPD_FIELD_RW, &rw_address,
439 /* Locate old field, if any */
440 if ( ( rc = pci_vpd_find ( vpd, field, &old_address,
441 &old_len ) ) == 0 ) {
443 /* Field already exists */
444 if ( old_address > rw_address ) {
445 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
446 " at [%04x,%04zx) is after field "
447 PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
448 PCI_ARGS ( vpd->pci ),
449 PCI_VPD_FIELD_ARGS ( field ),
450 old_address, ( old_address + old_len ),
451 PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ),
452 rw_address, ( rw_address + rw_len ) );
456 dst_address = ( old_address - sizeof ( old_field ) );
457 copy_address = ( old_address + old_len );
458 copy_len = ( rw_address - sizeof ( rw_field ) - copy_address );
460 /* Calculate available length */
461 available_len = ( rw_len + old_len );
465 /* Field does not yet exist */
466 dst_address = ( rw_address - sizeof ( rw_field ) );
467 copy_address = dst_address;
470 /* Calculate available length */
471 available_len = ( ( rw_len > sizeof ( new_field ) ) ?
472 ( rw_len - sizeof ( new_field ) ) : 0 );
475 /* Dump region before changes */
476 dump_address = dst_address;
477 dump_len = ( rw_address + rw_len - dump_address );
478 DBGC ( vpd, PCI_FMT " VPD before resizing field " PCI_VPD_FIELD_FMT
479 " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
480 PCI_VPD_FIELD_ARGS ( field ), len );
481 pci_vpd_dump ( vpd, dump_address, dump_len );
483 /* Check available length */
484 if ( available_len > PCI_VPD_MAX_LEN )
485 available_len = PCI_VPD_MAX_LEN;
486 if ( len > available_len ) {
487 DBGC ( vpd, PCI_FMT " VPD no space for field "
488 PCI_VPD_FIELD_FMT " (need %02zx, have %02zx)\n",
489 PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ),
490 len, available_len );
495 /* Preserve intermediate fields, if any */
496 copy = malloc ( copy_len );
501 if ( ( rc = pci_vpd_read ( vpd, copy_address, copy, copy_len ) ) != 0 )
504 /* Create new field, if applicable */
506 new_field.keyword = PCI_VPD_KEYWORD ( field );
508 if ( ( rc = pci_vpd_write ( vpd, dst_address, &new_field,
509 sizeof ( new_field ) ) ) != 0 )
511 dst_address += sizeof ( new_field );
512 *address = dst_address;
513 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
514 "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
515 PCI_VPD_FIELD_ARGS ( field ), dst_address,
516 ( dst_address + new_field.len ) );
519 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
520 " no longer exists\n", PCI_ARGS ( vpd->pci ),
521 PCI_VPD_FIELD_ARGS ( field ) );
524 /* Restore intermediate fields, if any */
525 if ( ( rc = pci_vpd_write ( vpd, dst_address, copy, copy_len ) ) != 0 )
527 dst_address += copy_len;
529 /* Create 'RW' field */
530 rw_field.keyword = PCI_VPD_KEYWORD ( PCI_VPD_FIELD_RW );
531 rw_field.len = ( rw_len +
532 ( rw_address - sizeof ( rw_field ) ) - dst_address );
533 if ( ( rc = pci_vpd_write ( vpd, dst_address, &rw_field,
534 sizeof ( rw_field ) ) ) != 0 )
536 dst_address += sizeof ( rw_field );
537 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
538 "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
539 PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ), dst_address,
540 ( dst_address + rw_field.len ) );
542 /* Dump region after changes */
543 DBGC ( vpd, PCI_FMT " VPD after resizing field " PCI_VPD_FIELD_FMT
544 " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
545 PCI_VPD_FIELD_ARGS ( field ), len );
546 pci_vpd_dump ( vpd, dump_address, dump_len );