You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pci_settings.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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 (at your option) any later version.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. *
  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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <ipxe/pci.h>
  27. #include <ipxe/settings.h>
  28. #include <ipxe/init.h>
  29. /** @file
  30. *
  31. * PCI device settings
  32. *
  33. */
  34. /** PCI device settings scope */
  35. static const struct settings_scope pci_settings_scope;
  36. /**
  37. * Check applicability of PCI device setting
  38. *
  39. * @v settings Settings block
  40. * @v setting Setting
  41. * @ret applies Setting applies within this settings block
  42. */
  43. static int pci_settings_applies ( struct settings *settings __unused,
  44. const struct setting *setting ) {
  45. return ( setting->scope == &pci_settings_scope );
  46. }
  47. /**
  48. * Fetch value of PCI device setting
  49. *
  50. * @v settings Settings block
  51. * @v setting Setting to fetch
  52. * @v data Buffer to fill with setting data
  53. * @v len Length of buffer
  54. * @ret len Length of setting data, or negative error
  55. */
  56. static int pci_settings_fetch ( struct settings *settings __unused,
  57. struct setting *setting,
  58. void *data, size_t len ) {
  59. struct pci_device pci;
  60. unsigned int tag_busdevfn;
  61. unsigned int tag_offset;
  62. unsigned int tag_len;
  63. unsigned int i;
  64. /* Extract busdevfn, offset, and length from tag */
  65. tag_busdevfn = ( ( setting->tag >> 16 ) & 0xffff );
  66. tag_offset = ( ( setting->tag >> 8 ) & 0xff );
  67. tag_len = ( ( setting->tag >> 0 ) & 0xff );
  68. /* Locate PCI device */
  69. memset ( &pci, 0, sizeof ( pci ) );
  70. pci_init ( &pci, tag_busdevfn );
  71. DBG ( PCI_FMT " reading %#02x+%#x\n", PCI_ARGS ( &pci ),
  72. tag_offset, tag_len );
  73. /* Read data one byte at a time, in reverse order (since PCI
  74. * is little-endian and iPXE settings are essentially
  75. * big-endian).
  76. */
  77. tag_offset += tag_len;
  78. for ( i = 0 ; ( ( i < tag_len ) && ( i < len ) ); i++ ) {
  79. pci_read_config_byte ( &pci, --tag_offset, data++ );
  80. }
  81. /* Set type to ":hexraw" if not already specified */
  82. if ( ! setting->type )
  83. setting->type = &setting_type_hexraw;
  84. return tag_len;
  85. }
  86. /** PCI device settings operations */
  87. static struct settings_operations pci_settings_operations = {
  88. .applies = pci_settings_applies,
  89. .fetch = pci_settings_fetch,
  90. };
  91. /** PCI device settings */
  92. static struct settings pci_settings = {
  93. .refcnt = NULL,
  94. .siblings = LIST_HEAD_INIT ( pci_settings.siblings ),
  95. .children = LIST_HEAD_INIT ( pci_settings.children ),
  96. .op = &pci_settings_operations,
  97. .default_scope = &pci_settings_scope,
  98. };
  99. /** Initialise PCI device settings */
  100. static void pci_settings_init ( void ) {
  101. int rc;
  102. if ( ( rc = register_settings ( &pci_settings, NULL, "pci" ) ) != 0 ) {
  103. DBG ( "PCI could not register settings: %s\n",
  104. strerror ( rc ) );
  105. return;
  106. }
  107. }
  108. /** PCI device settings initialiser */
  109. struct init_fn pci_settings_init_fn __init_fn ( INIT_NORMAL ) = {
  110. .initialise = pci_settings_init,
  111. };