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.

snp.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2014 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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <ipxe/efi/efi.h>
  25. #include <ipxe/efi/Protocol/SimpleNetwork.h>
  26. #include <ipxe/efi/efi_driver.h>
  27. #include <ipxe/efi/efi_snp.h>
  28. #include <ipxe/efi/efi_pci.h>
  29. #include "snpnet.h"
  30. #include "snp.h"
  31. /** @file
  32. *
  33. * SNP driver
  34. *
  35. */
  36. /** EFI simple network protocol GUID */
  37. static EFI_GUID efi_simple_network_protocol_guid
  38. = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
  39. /** EFI PCI I/O protocol GUID */
  40. static EFI_GUID efi_pci_io_protocol_guid
  41. = EFI_PCI_IO_PROTOCOL_GUID;
  42. /**
  43. * Check to see if driver supports a device
  44. *
  45. * @v device EFI device handle
  46. * @ret rc Return status code
  47. */
  48. static int snp_supported ( EFI_HANDLE device ) {
  49. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  50. EFI_STATUS efirc;
  51. /* Check that this is not a device we are providing ourselves */
  52. if ( find_snpdev ( device ) != NULL ) {
  53. DBGCP ( device, "SNP %p %s is provided by this binary\n",
  54. device, efi_handle_devpath_text ( device ) );
  55. return -ENOTTY;
  56. }
  57. /* Test for presence of simple network protocol */
  58. if ( ( efirc = bs->OpenProtocol ( device,
  59. &efi_simple_network_protocol_guid,
  60. NULL, efi_image_handle, device,
  61. EFI_OPEN_PROTOCOL_TEST_PROTOCOL))!=0){
  62. DBGCP ( device, "SNP %p %s is not an SNP device\n",
  63. device, efi_handle_devpath_text ( device ) );
  64. return -EEFI ( efirc );
  65. }
  66. DBGC ( device, "SNP %p %s is an SNP device\n",
  67. device, efi_handle_devpath_text ( device ) );
  68. return 0;
  69. }
  70. /**
  71. * Get underlying PCI device information
  72. *
  73. * @v snpdev SNP device
  74. * @ret rc Return status code
  75. */
  76. static int snp_pci_info ( struct snp_device *snpdev ) {
  77. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  78. struct efi_device *efidev = snpdev->efidev;
  79. EFI_DEVICE_PATH_PROTOCOL *devpath = efidev->path;
  80. struct pci_device pci;
  81. EFI_HANDLE device;
  82. EFI_STATUS efirc;
  83. int rc;
  84. /* Check for presence of PCI I/O protocol */
  85. if ( ( efirc = bs->LocateDevicePath ( &efi_pci_io_protocol_guid,
  86. &devpath, &device ) ) != 0 ) {
  87. DBGC ( efidev->device, "SNP %p %s is not a PCI device\n",
  88. efidev->device, efi_devpath_text ( efidev->path ) );
  89. return -EEFI ( efirc );
  90. }
  91. /* Get PCI device information */
  92. if ( ( rc = efipci_info ( device, &pci ) ) != 0 ) {
  93. DBGC ( efidev->device, "SNP %p %s could not get PCI "
  94. "information: %s\n", efidev->device,
  95. efi_devpath_text ( efidev->path ), strerror ( rc ) );
  96. return rc;
  97. }
  98. /* Populate SNP device information */
  99. memcpy ( &snpdev->dev.desc, &pci.dev.desc, sizeof ( snpdev->dev.desc ));
  100. snprintf ( snpdev->dev.name, sizeof ( snpdev->dev.name ), "SNP-%s",
  101. pci.dev.name );
  102. return 0;
  103. }
  104. /**
  105. * Attach driver to device
  106. *
  107. * @v efidev EFI device
  108. * @ret rc Return status code
  109. */
  110. static int snp_start ( struct efi_device *efidev ) {
  111. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  112. EFI_HANDLE device = efidev->device;
  113. struct snp_device *snpdev;
  114. union {
  115. EFI_SIMPLE_NETWORK_PROTOCOL *snp;
  116. void *interface;
  117. } snp;
  118. EFI_STATUS efirc;
  119. int rc;
  120. /* Check that this is not a device we are providing ourselves */
  121. if ( find_snpdev ( efidev->device ) != NULL ) {
  122. DBGCP ( device, "SNP %p %s is provided by this binary\n",
  123. device, efi_devpath_text ( efidev->path ) );
  124. rc = -ENOTTY;
  125. goto err_own;
  126. }
  127. /* Allocate and initialise structure */
  128. snpdev = zalloc ( sizeof ( *snpdev ) );
  129. if ( ! snpdev ) {
  130. rc = -ENOMEM;
  131. goto err_alloc;
  132. }
  133. snpdev->efidev = efidev;
  134. snpdev->dev.driver_name = "SNP";
  135. INIT_LIST_HEAD ( &snpdev->dev.children );
  136. /* See if device is an SNP device */
  137. if ( ( efirc = bs->OpenProtocol ( device,
  138. &efi_simple_network_protocol_guid,
  139. &snp.interface, efi_image_handle,
  140. device,
  141. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  142. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  143. rc = -EEFI ( efirc );
  144. DBGCP ( device, "SNP %p %s cannot open SNP protocol: %s\n",
  145. device, efi_devpath_text ( efidev->path ),
  146. strerror ( rc ) );
  147. goto err_open_protocol;
  148. }
  149. snpdev->snp = snp.snp;
  150. /* Get underlying device information */
  151. if ( ( rc = snp_pci_info ( snpdev ) ) != 0 )
  152. goto err_info;
  153. /* Mark SNP device as a child of the EFI device */
  154. snpdev->dev.parent = &efidev->dev;
  155. list_add ( &snpdev->dev.siblings, &efidev->dev.children );
  156. /* Create SNP network device */
  157. if ( ( rc = snpnet_probe ( snpdev ) ) != 0 )
  158. goto err_probe;
  159. efidev_set_drvdata ( efidev, snpdev );
  160. return 0;
  161. snpnet_remove ( snpdev );
  162. err_probe:
  163. list_del ( &snpdev->dev.siblings );
  164. err_info:
  165. bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
  166. efi_image_handle, device );
  167. err_open_protocol:
  168. free ( snpdev );
  169. err_alloc:
  170. err_own:
  171. return rc;
  172. }
  173. /**
  174. * Detach driver from device
  175. *
  176. * @v efidev EFI device
  177. */
  178. static void snp_stop ( struct efi_device *efidev ) {
  179. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  180. struct snp_device *snpdev = efidev_get_drvdata ( efidev );
  181. snpnet_remove ( snpdev );
  182. list_del ( &snpdev->dev.siblings );
  183. bs->CloseProtocol ( efidev->device, &efi_simple_network_protocol_guid,
  184. efi_image_handle, efidev->device );
  185. free ( snpdev );
  186. }
  187. /** EFI SNP driver */
  188. struct efi_driver snp_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  189. .name = "SNP",
  190. .supported = snp_supported,
  191. .start = snp_start,
  192. .stop = snp_stop,
  193. };