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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <gpxe/pci.h>
  23. #include <undi.h>
  24. #include <undirom.h>
  25. #include <undiload.h>
  26. #include <undinet.h>
  27. #include <undipreload.h>
  28. /** @file
  29. *
  30. * UNDI PCI driver
  31. *
  32. */
  33. /**
  34. * Find UNDI ROM for PCI device
  35. *
  36. * @v pci PCI device
  37. * @ret undirom UNDI ROM, or NULL
  38. *
  39. * Try to find a driver for this device. Try an exact match on the
  40. * ROM address first, then fall back to a vendor/device ID match only
  41. */
  42. static struct undi_rom * undipci_find_rom ( struct pci_device *pci ) {
  43. struct undi_rom *undirom;
  44. unsigned long rombase;
  45. rombase = pci_bar_start ( pci, PCI_ROM_ADDRESS );
  46. undirom = undirom_find_pci ( pci->vendor, pci->device, rombase );
  47. if ( ! undirom )
  48. undirom = undirom_find_pci ( pci->vendor, pci->device, 0 );
  49. return undirom;
  50. }
  51. /**
  52. * Probe PCI device
  53. *
  54. * @v pci PCI device
  55. * @v id PCI ID
  56. * @ret rc Return status code
  57. */
  58. static int undipci_probe ( struct pci_device *pci,
  59. const struct pci_device_id *id __unused ) {
  60. struct undi_device *undi;
  61. struct undi_rom *undirom;
  62. unsigned int busdevfn = PCI_BUSDEVFN ( pci->bus, pci->devfn );
  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 == 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, busdevfn ) ) != 0 )
  86. goto err_load_pci;
  87. }
  88. /* Add to device hierarchy */
  89. snprintf ( undi->dev.name, sizeof ( undi->dev.name ),
  90. "UNDI-%s", pci->dev.name );
  91. memcpy ( &undi->dev.desc, &pci->dev.desc, sizeof ( undi->dev.desc ) );
  92. undi->dev.parent = &pci->dev;
  93. INIT_LIST_HEAD ( &undi->dev.children );
  94. list_add ( &undi->dev.siblings, &pci->dev.children );
  95. /* Create network device */
  96. if ( ( rc = undinet_probe ( undi ) ) != 0 )
  97. goto err_undinet_probe;
  98. return 0;
  99. err_undinet_probe:
  100. undi_unload ( undi );
  101. list_del ( &undi->dev.siblings );
  102. err_find_rom:
  103. err_load_pci:
  104. free ( undi );
  105. pci_set_drvdata ( pci, NULL );
  106. return rc;
  107. }
  108. /**
  109. * Remove PCI device
  110. *
  111. * @v pci PCI device
  112. */
  113. static void undipci_remove ( struct pci_device *pci ) {
  114. struct undi_device *undi = pci_get_drvdata ( pci );
  115. undinet_remove ( undi );
  116. undi_unload ( undi );
  117. list_del ( &undi->dev.siblings );
  118. free ( undi );
  119. pci_set_drvdata ( pci, NULL );
  120. }
  121. static struct pci_device_id undipci_nics[] = {
  122. PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
  123. };
  124. struct pci_driver undipci_driver __pci_driver = {
  125. .ids = undipci_nics,
  126. .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
  127. .probe = undipci_probe,
  128. .remove = undipci_remove,
  129. };