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 );
28 #include <ipxe/isapnp.h>
29 #include <ipxe/pcivpd.h>
33 * PCI Vital Product Data
38 * Initialise PCI Vital Product Data
42 * @ret rc Return status code
44 int pci_vpd_init ( struct pci_vpd *vpd, struct pci_device *pci ) {
46 /* Initialise structure */
48 pci_vpd_invalidate_cache ( vpd );
50 /* Locate VPD capability */
51 vpd->cap = pci_find_capability ( pci, PCI_CAP_ID_VPD );
53 DBGC ( vpd, PCI_FMT " does not support VPD\n",
58 DBGC ( vpd, PCI_FMT " VPD is at offset %02x\n",
59 PCI_ARGS ( pci ), vpd->cap );
64 * Read one dword of PCI Vital Product Data
67 * @v address Address to read
69 * @ret rc Return status code
71 static int pci_vpd_read_dword ( struct pci_vpd *vpd, int address,
73 struct pci_device *pci = vpd->pci;
74 unsigned int cap = vpd->cap;
78 /* Fail if no VPD present */
82 /* Return cached value, if present */
83 if ( pci_vpd_cache_is_valid ( vpd ) &&
84 ( vpd->cache.address == address ) ) {
85 *data = vpd->cache.data;
90 pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), address );
92 /* Wait for read to complete */
93 for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
95 /* Check if data is ready */
96 pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
97 if ( flag & PCI_VPD_FLAG ) {
100 pci_read_config_dword ( pci, ( cap + PCI_VPD_DATA ),
102 DBGC2 ( vpd, PCI_FMT " VPD %04x => %08x\n",
103 PCI_ARGS ( pci ), address, htonl ( *data ) );
106 vpd->cache.address = address;
107 vpd->cache.data = *data;
112 /* Wait 1ms before retrying */
116 DBGC ( vpd, PCI_FMT " VPD %04x read via %02x timed out\n",
117 PCI_ARGS ( pci ), address, cap );
122 * Write one dword of PCI Vital Product Data
125 * @v address Address to write
126 * @v data Data to write
127 * @ret rc Return status code
129 static int pci_vpd_write_dword ( struct pci_vpd *vpd, int address,
131 struct pci_device *pci = vpd->pci;
132 unsigned int cap = vpd->cap;
133 unsigned int retries;
136 /* Fail if no VPD present */
140 /* Invalidate cache */
141 pci_vpd_invalidate_cache ( vpd );
143 DBGC2 ( vpd, PCI_FMT " VPD %04x <= %08x\n",
144 PCI_ARGS ( pci ), address, htonl ( data ) );
147 pci_write_config_dword ( pci, ( cap + PCI_VPD_DATA ), data );
150 pci_write_config_word ( pci, ( cap + PCI_VPD_ADDRESS ),
151 ( address | PCI_VPD_FLAG ) );
153 /* Wait for write to complete */
154 for ( retries = 0 ; retries < PCI_VPD_MAX_WAIT_MS ; retries++ ) {
156 /* Check if write has completed */
157 pci_read_config_word ( pci, ( cap + PCI_VPD_ADDRESS ), &flag );
158 if ( ! ( flag & PCI_VPD_FLAG ) )
161 /* Wait 1ms before retrying */
165 DBGC ( vpd, PCI_FMT " VPD %04x write via %02x timed out\n",
166 PCI_ARGS ( pci ), address, cap );
174 * @v address Starting address
176 * @v len Length of data buffer
177 * @ret rc Return status code
179 int pci_vpd_read ( struct pci_vpd *vpd, unsigned int address, void *buf,
181 uint8_t *bytes = buf;
187 /* Calculate length to skip at start of data */
188 skip_len = ( address & 0x03 );
190 /* Read data, a dword at a time */
191 for ( address &= ~0x03 ; len ; address += 4 ) {
193 /* Read whole dword */
194 if ( ( rc = pci_vpd_read_dword ( vpd, address, &data ) ) != 0 )
197 /* Copy data to buffer */
198 for ( i = 4 ; i ; i-- ) {
205 data = ( ( data << 24 ) | ( data >> 8 ) );
216 * @v address Starting address
218 * @v len Length of data buffer
219 * @ret rc Return status code
221 int pci_vpd_write ( struct pci_vpd *vpd, unsigned int address, const void *buf,
223 const uint8_t *bytes = buf;
229 /* Calculate length to skip at start of data */
230 skip_len = ( address & 0x03 );
232 /* Write data, a dword at a time */
233 for ( address &= ~0x03 ; len ; address += 4 ) {
235 /* Read existing dword, if necessary */
236 if ( skip_len || ( len <= 0x03 ) ) {
237 if ( ( rc = pci_vpd_read_dword ( vpd, address,
242 /* Copy data from buffer */
243 for ( i = 4 ; i ; i-- ) {
247 data = ( ( data & ~0xff ) | *(bytes++) );
250 data = ( ( data << 24 ) | ( data >> 8 ) );
253 /* Write whole dword */
254 if ( ( rc = pci_vpd_write_dword ( vpd, address, data ) ) != 0 )
261 * Dump PCI VPD region (for debugging)
264 * @v address Starting address
265 * @v len Length of data
267 static void pci_vpd_dump ( struct pci_vpd *vpd, unsigned int address,
271 /* Do nothing in non-debug builds */
278 if ( ( rc = pci_vpd_read ( vpd, address, buf,
279 sizeof ( buf ) ) ) != 0 )
281 DBGC_HDA ( vpd, address, buf, sizeof ( buf ) );
290 * @ret address Address of tag body
291 * @ret len Length of tag body
292 * @ret rc Return status code
294 static int pci_vpd_find_tag ( struct pci_vpd *vpd, unsigned int tag,
295 unsigned int *address, size_t *len ) {
300 /* Scan through tags looking for a match */
304 if ( ( rc = pci_vpd_read ( vpd, (*address)++, &read_tag,
305 sizeof ( read_tag ) ) ) != 0 )
308 /* Extract tag and length */
309 if ( ISAPNP_IS_LARGE_TAG ( read_tag ) ) {
310 if ( ( rc = pci_vpd_read ( vpd, *address, &read_len,
311 sizeof ( read_len ) ) ) != 0)
313 *address += sizeof ( read_len );
314 read_len = le16_to_cpu ( read_len );
315 read_tag = ISAPNP_LARGE_TAG_NAME ( read_tag );
317 read_len = ISAPNP_SMALL_TAG_LEN ( read_tag );
318 read_tag = ISAPNP_SMALL_TAG_NAME ( read_tag );
321 /* Check for tag match */
322 if ( tag == read_tag ) {
324 DBGC ( vpd, PCI_FMT " VPD tag %02x is at "
325 "[%04x,%04zx)\n", PCI_ARGS ( vpd->pci ), tag,
326 *address, ( *address + *len ) );
330 /* Move to next tag */
331 *address += read_len;
333 } while ( read_tag != ISAPNP_TAG_END );
335 DBGC ( vpd, PCI_FMT " VPD tag %02x not found\n",
336 PCI_ARGS ( vpd->pci ), tag );
341 * Locate PCI VPD field
344 * @v field VPD field descriptor
345 * @ret address Address of field body
346 * @ret len Length of field body
347 * @ret rc Return status code
349 int pci_vpd_find ( struct pci_vpd *vpd, unsigned int field,
350 unsigned int *address, size_t *len ) {
351 struct pci_vpd_field read_field;
354 /* Locate containing tag */
355 if ( ( rc = pci_vpd_find_tag ( vpd, PCI_VPD_TAG ( field ),
356 address, len ) ) != 0 )
359 /* Return immediately if we are searching for a whole-tag field */
360 if ( ! PCI_VPD_KEYWORD ( field ) ) {
361 pci_vpd_dump ( vpd, *address, *len );
365 /* Scan through fields looking for a match */
366 while ( *len >= sizeof ( read_field ) ) {
368 /* Read field header */
369 if ( ( rc = pci_vpd_read ( vpd, *address, &read_field,
370 sizeof ( read_field ) ) ) != 0 )
372 *address += sizeof ( read_field );
373 *len -= sizeof ( read_field );
375 /* Check for keyword match */
376 if ( read_field.keyword == PCI_VPD_KEYWORD ( field ) ) {
377 *len = read_field.len;
378 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
379 " is at [%04x,%04zx)\n", PCI_ARGS ( vpd->pci ),
380 PCI_VPD_FIELD_ARGS ( field ),
381 *address, ( *address + *len ) );
382 pci_vpd_dump ( vpd, *address, *len );
386 /* Move to next field */
387 if ( read_field.len > *len )
389 *address += read_field.len;
390 *len -= read_field.len;
393 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " not found\n",
394 PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ) );
402 * @v field VPD field descriptor
403 * @v len New length of field body
404 * @ret address Address of field body
405 * @ret rc Return status code
407 int pci_vpd_resize ( struct pci_vpd *vpd, unsigned int field, size_t len,
408 unsigned int *address ) {
409 struct pci_vpd_field rw_field;
410 struct pci_vpd_field old_field;
411 struct pci_vpd_field new_field;
412 unsigned int rw_address;
413 unsigned int old_address;
414 unsigned int copy_address;
415 unsigned int dst_address;
416 unsigned int dump_address;
419 size_t available_len;
426 assert ( PCI_VPD_TAG ( field ) == PCI_VPD_TAG_RW );
427 assert ( PCI_VPD_KEYWORD ( field ) != 0 );
428 assert ( field != PCI_VPD_FIELD_RW );
430 /* Locate 'RW' field */
431 if ( ( rc = pci_vpd_find ( vpd, PCI_VPD_FIELD_RW, &rw_address,
435 /* Locate old field, if any */
436 if ( ( rc = pci_vpd_find ( vpd, field, &old_address,
437 &old_len ) ) == 0 ) {
439 /* Field already exists */
440 if ( old_address > rw_address ) {
441 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
442 " at [%04x,%04zx) is after field "
443 PCI_VPD_FIELD_FMT " at [%04x,%04zx)\n",
444 PCI_ARGS ( vpd->pci ),
445 PCI_VPD_FIELD_ARGS ( field ),
446 old_address, ( old_address + old_len ),
447 PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ),
448 rw_address, ( rw_address + rw_len ) );
452 dst_address = ( old_address - sizeof ( old_field ) );
453 copy_address = ( old_address + old_len );
454 copy_len = ( rw_address - sizeof ( rw_field ) - copy_address );
456 /* Calculate available length */
457 available_len = ( rw_len + old_len );
461 /* Field does not yet exist */
462 dst_address = ( rw_address - sizeof ( rw_field ) );
463 copy_address = dst_address;
466 /* Calculate available length */
467 available_len = ( ( rw_len > sizeof ( new_field ) ) ?
468 ( rw_len - sizeof ( new_field ) ) : 0 );
471 /* Dump region before changes */
472 dump_address = dst_address;
473 dump_len = ( rw_address + rw_len - dump_address );
474 DBGC ( vpd, PCI_FMT " VPD before resizing field " PCI_VPD_FIELD_FMT
475 " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
476 PCI_VPD_FIELD_ARGS ( field ), len );
477 pci_vpd_dump ( vpd, dump_address, dump_len );
479 /* Check available length */
480 if ( available_len > PCI_VPD_MAX_LEN )
481 available_len = PCI_VPD_MAX_LEN;
482 if ( len > available_len ) {
483 DBGC ( vpd, PCI_FMT " VPD no space for field "
484 PCI_VPD_FIELD_FMT " (need %02zx, have %02zx)\n",
485 PCI_ARGS ( vpd->pci ), PCI_VPD_FIELD_ARGS ( field ),
486 len, available_len );
491 /* Preserve intermediate fields, if any */
492 copy = malloc ( copy_len );
497 if ( ( rc = pci_vpd_read ( vpd, copy_address, copy, copy_len ) ) != 0 )
500 /* Create new field, if applicable */
502 new_field.keyword = PCI_VPD_KEYWORD ( field );
504 if ( ( rc = pci_vpd_write ( vpd, dst_address, &new_field,
505 sizeof ( new_field ) ) ) != 0 )
507 dst_address += sizeof ( new_field );
508 *address = dst_address;
509 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
510 "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
511 PCI_VPD_FIELD_ARGS ( field ), dst_address,
512 ( dst_address + new_field.len ) );
515 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT
516 " no longer exists\n", PCI_ARGS ( vpd->pci ),
517 PCI_VPD_FIELD_ARGS ( field ) );
520 /* Restore intermediate fields, if any */
521 if ( ( rc = pci_vpd_write ( vpd, dst_address, copy, copy_len ) ) != 0 )
523 dst_address += copy_len;
525 /* Create 'RW' field */
526 rw_field.keyword = PCI_VPD_KEYWORD ( PCI_VPD_FIELD_RW );
527 rw_field.len = ( rw_len +
528 ( rw_address - sizeof ( rw_field ) ) - dst_address );
529 if ( ( rc = pci_vpd_write ( vpd, dst_address, &rw_field,
530 sizeof ( rw_field ) ) ) != 0 )
532 dst_address += sizeof ( rw_field );
533 DBGC ( vpd, PCI_FMT " VPD field " PCI_VPD_FIELD_FMT " is now "
534 "at [%04x,%04x)\n", PCI_ARGS ( vpd->pci ),
535 PCI_VPD_FIELD_ARGS ( PCI_VPD_FIELD_RW ), dst_address,
536 ( dst_address + rw_field.len ) );
538 /* Dump region after changes */
539 DBGC ( vpd, PCI_FMT " VPD after resizing field " PCI_VPD_FIELD_FMT
540 " to %zd bytes:\n", PCI_ARGS ( vpd->pci ),
541 PCI_VPD_FIELD_ARGS ( field ), len );
542 pci_vpd_dump ( vpd, dump_address, dump_len );