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.4KB

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