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.

undi.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2007 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 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 <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ipxe/pci.h>
  25. #include <undi.h>
  26. #include <undirom.h>
  27. #include <undiload.h>
  28. #include <undinet.h>
  29. #include <undipreload.h>
  30. /** @file
  31. *
  32. * UNDI PCI driver
  33. *
  34. */
  35. /**
  36. * Find UNDI ROM for PCI device
  37. *
  38. * @v pci PCI device
  39. * @ret undirom UNDI ROM, or NULL
  40. *
  41. * Try to find a driver for this device. Try an exact match on the
  42. * ROM address first, then fall back to a vendor/device ID match only
  43. */
  44. static struct undi_rom * undipci_find_rom ( struct pci_device *pci ) {
  45. struct undi_rom *undirom;
  46. unsigned long rombase;
  47. rombase = pci_bar_start ( pci, PCI_ROM_ADDRESS );
  48. undirom = undirom_find_pci ( pci->vendor, pci->device, rombase );
  49. if ( ! undirom )
  50. undirom = undirom_find_pci ( pci->vendor, pci->device, 0 );
  51. return undirom;
  52. }
  53. /**
  54. * Probe PCI device
  55. *
  56. * @v pci PCI device
  57. * @v id PCI ID
  58. * @ret rc Return status code
  59. */
  60. static int undipci_probe ( struct pci_device *pci ) {
  61. struct undi_device *undi;
  62. struct undi_rom *undirom;
  63. int rc;
  64. /* Ignore non-network devices */
  65. if ( PCI_BASE_CLASS ( pci->class ) != PCI_BASE_CLASS_NETWORK )
  66. return -ENOTTY;
  67. /* Allocate UNDI device structure */
  68. undi = zalloc ( sizeof ( *undi ) );
  69. if ( ! undi )
  70. return -ENOMEM;
  71. pci_set_drvdata ( pci, undi );
  72. /* Find/create our pixie */
  73. if ( preloaded_undi.pci_busdevfn == pci->busdevfn ) {
  74. /* Claim preloaded UNDI device */
  75. DBGC ( undi, "UNDI %p using preloaded UNDI device\n", undi );
  76. memcpy ( undi, &preloaded_undi, sizeof ( *undi ) );
  77. memset ( &preloaded_undi, 0, sizeof ( preloaded_undi ) );
  78. } else {
  79. /* Find UNDI ROM for PCI device */
  80. if ( ! ( undirom = undipci_find_rom ( pci ) ) ) {
  81. rc = -ENODEV;
  82. goto err_find_rom;
  83. }
  84. /* Call UNDI ROM loader to create pixie */
  85. if ( ( rc = undi_load_pci ( undi, undirom,
  86. pci->busdevfn ) ) != 0 ) {
  87. goto err_load_pci;
  88. }
  89. }
  90. /* Add to device hierarchy */
  91. snprintf ( undi->dev.name, sizeof ( undi->dev.name ),
  92. "UNDI-%s", pci->dev.name );
  93. memcpy ( &undi->dev.desc, &pci->dev.desc, sizeof ( undi->dev.desc ) );
  94. undi->dev.parent = &pci->dev;
  95. INIT_LIST_HEAD ( &undi->dev.children );
  96. list_add ( &undi->dev.siblings, &pci->dev.children );
  97. /* Create network device */
  98. if ( ( rc = undinet_probe ( undi ) ) != 0 )
  99. goto err_undinet_probe;
  100. return 0;
  101. err_undinet_probe:
  102. undi_unload ( undi );
  103. list_del ( &undi->dev.siblings );
  104. err_find_rom:
  105. err_load_pci:
  106. free ( undi );
  107. pci_set_drvdata ( pci, NULL );
  108. return rc;
  109. }
  110. /**
  111. * Remove PCI device
  112. *
  113. * @v pci PCI device
  114. */
  115. static void undipci_remove ( struct pci_device *pci ) {
  116. struct undi_device *undi = pci_get_drvdata ( pci );
  117. undinet_remove ( undi );
  118. undi_unload ( undi );
  119. list_del ( &undi->dev.siblings );
  120. free ( undi );
  121. pci_set_drvdata ( pci, NULL );
  122. }
  123. static struct pci_device_id undipci_nics[] = {
  124. PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
  125. };
  126. struct pci_driver undipci_driver __pci_driver_fallback = {
  127. .ids = undipci_nics,
  128. .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
  129. .probe = undipci_probe,
  130. .remove = undipci_remove,
  131. };