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

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