Initialize.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /** @file
  2. Implementation of initializing a network adapter.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Snp.h"
  7. /**
  8. Call UNDI to initialize the interface.
  9. @param Snp Pointer to snp driver structure.
  10. @param CableDetectFlag Do/don't detect the cable (depending on what
  11. undi supports).
  12. @retval EFI_SUCCESS UNDI is initialized successfully.
  13. @retval EFI_DEVICE_ERROR UNDI could not be initialized.
  14. @retval Other Other errors as indicated.
  15. **/
  16. EFI_STATUS
  17. PxeInit (
  18. SNP_DRIVER *Snp,
  19. UINT16 CableDetectFlag
  20. )
  21. {
  22. PXE_CPB_INITIALIZE *Cpb;
  23. VOID *Addr;
  24. EFI_STATUS Status;
  25. Status = EFI_SUCCESS;
  26. Cpb = Snp->Cpb;
  27. if (Snp->TxRxBufferSize != 0) {
  28. Status = Snp->PciIo->AllocateBuffer (
  29. Snp->PciIo,
  30. AllocateAnyPages,
  31. EfiBootServicesData,
  32. SNP_MEM_PAGES (Snp->TxRxBufferSize),
  33. &Addr,
  34. 0
  35. );
  36. if (Status != EFI_SUCCESS) {
  37. DEBUG (
  38. (DEBUG_ERROR,
  39. "\nSnp->PxeInit() AllocateBuffer %xh (%r)\n",
  40. Status,
  41. Status)
  42. );
  43. return Status;
  44. }
  45. ASSERT (Addr);
  46. Snp->TxRxBuffer = Addr;
  47. }
  48. Cpb->MemoryAddr = (UINT64)(UINTN)Snp->TxRxBuffer;
  49. Cpb->MemoryLength = Snp->TxRxBufferSize;
  50. //
  51. // let UNDI decide/detect these values
  52. //
  53. Cpb->LinkSpeed = 0;
  54. Cpb->TxBufCnt = 0;
  55. Cpb->TxBufSize = 0;
  56. Cpb->RxBufCnt = 0;
  57. Cpb->RxBufSize = 0;
  58. Cpb->DuplexMode = PXE_DUPLEX_DEFAULT;
  59. Cpb->LoopBackMode = LOOPBACK_NORMAL;
  60. Snp->Cdb.OpCode = PXE_OPCODE_INITIALIZE;
  61. Snp->Cdb.OpFlags = CableDetectFlag;
  62. Snp->Cdb.CPBsize = (UINT16)sizeof (PXE_CPB_INITIALIZE);
  63. Snp->Cdb.DBsize = (UINT16)sizeof (PXE_DB_INITIALIZE);
  64. Snp->Cdb.CPBaddr = (UINT64)(UINTN)Snp->Cpb;
  65. Snp->Cdb.DBaddr = (UINT64)(UINTN)Snp->Db;
  66. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  67. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  68. Snp->Cdb.IFnum = Snp->IfNum;
  69. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  70. DEBUG ((DEBUG_NET, "\nSnp->undi.initialize() "));
  71. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  72. //
  73. // There are two fields need to be checked here:
  74. // First is the upper two bits (14 & 15) in the CDB.StatFlags field. Until these bits change to report
  75. // PXE_STATFLAGS_COMMAND_COMPLETE or PXE_STATFLAGS_COMMAND_FAILED, the command has not been executed by the UNDI.
  76. // Second is the CDB.StatCode field. After command execution completes, either successfully or not,
  77. // the CDB.StatCode field contains the result of the command execution.
  78. //
  79. if ((((Snp->Cdb.StatFlags) & PXE_STATFLAGS_STATUS_MASK) == PXE_STATFLAGS_COMMAND_COMPLETE) &&
  80. (Snp->Cdb.StatCode == PXE_STATCODE_SUCCESS))
  81. {
  82. //
  83. // If cable detect feature is enabled in CDB.OpFlags, check the CDB.StatFlags to see if there is an
  84. // active connection to this network device. If the no media StatFlag is set, the UNDI and network
  85. // device are still initialized.
  86. //
  87. if (CableDetectFlag == PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) {
  88. if (((Snp->Cdb.StatFlags) & PXE_STATFLAGS_INITIALIZED_NO_MEDIA) != PXE_STATFLAGS_INITIALIZED_NO_MEDIA) {
  89. Snp->Mode.MediaPresent = TRUE;
  90. } else {
  91. Snp->Mode.MediaPresent = FALSE;
  92. }
  93. }
  94. Snp->Mode.State = EfiSimpleNetworkInitialized;
  95. Status = EFI_SUCCESS;
  96. } else {
  97. DEBUG (
  98. (DEBUG_WARN,
  99. "\nSnp->undi.initialize() %xh:%xh\n",
  100. Snp->Cdb.StatFlags,
  101. Snp->Cdb.StatCode)
  102. );
  103. if (Snp->TxRxBuffer != NULL) {
  104. Snp->PciIo->FreeBuffer (
  105. Snp->PciIo,
  106. SNP_MEM_PAGES (Snp->TxRxBufferSize),
  107. (VOID *)Snp->TxRxBuffer
  108. );
  109. }
  110. Snp->TxRxBuffer = NULL;
  111. Status = EFI_DEVICE_ERROR;
  112. }
  113. return Status;
  114. }
  115. /**
  116. Resets a network adapter and allocates the transmit and receive buffers
  117. required by the network interface; optionally, also requests allocation of
  118. additional transmit and receive buffers.
  119. This function allocates the transmit and receive buffers required by the network
  120. interface. If this allocation fails, then EFI_OUT_OF_RESOURCES is returned.
  121. If the allocation succeeds and the network interface is successfully initialized,
  122. then EFI_SUCCESS will be returned.
  123. @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
  124. @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
  125. that the driver should allocate for the network interface.
  126. Some network interfaces will not be able to use the
  127. extra buffer, and the caller will not know if it is
  128. actually being used.
  129. @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
  130. that the driver should allocate for the network interface.
  131. Some network interfaces will not be able to use the
  132. extra buffer, and the caller will not know if it is
  133. actually being used.
  134. @retval EFI_SUCCESS The network interface was initialized.
  135. @retval EFI_NOT_STARTED The network interface has not been started.
  136. @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit and
  137. receive buffers.
  138. @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid
  139. EFI_SIMPLE_NETWORK_PROTOCOL structure.
  140. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  141. @retval EFI_UNSUPPORTED The increased buffer size feature is not supported.
  142. **/
  143. EFI_STATUS
  144. EFIAPI
  145. SnpUndi32Initialize (
  146. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  147. IN UINTN ExtraRxBufferSize OPTIONAL,
  148. IN UINTN ExtraTxBufferSize OPTIONAL
  149. )
  150. {
  151. EFI_STATUS EfiStatus;
  152. SNP_DRIVER *Snp;
  153. EFI_TPL OldTpl;
  154. if (This == NULL) {
  155. return EFI_INVALID_PARAMETER;
  156. }
  157. Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
  158. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  159. if (Snp == NULL) {
  160. EfiStatus = EFI_INVALID_PARAMETER;
  161. goto ON_EXIT;
  162. }
  163. switch (Snp->Mode.State) {
  164. case EfiSimpleNetworkStarted:
  165. break;
  166. case EfiSimpleNetworkStopped:
  167. EfiStatus = EFI_NOT_STARTED;
  168. goto ON_EXIT;
  169. default:
  170. EfiStatus = EFI_DEVICE_ERROR;
  171. goto ON_EXIT;
  172. }
  173. EfiStatus = gBS->CreateEvent (
  174. EVT_NOTIFY_WAIT,
  175. TPL_NOTIFY,
  176. &SnpWaitForPacketNotify,
  177. Snp,
  178. &Snp->Snp.WaitForPacket
  179. );
  180. if (EFI_ERROR (EfiStatus)) {
  181. Snp->Snp.WaitForPacket = NULL;
  182. EfiStatus = EFI_DEVICE_ERROR;
  183. goto ON_EXIT;
  184. }
  185. //
  186. //
  187. //
  188. Snp->Mode.MCastFilterCount = 0;
  189. Snp->Mode.ReceiveFilterSetting = 0;
  190. ZeroMem (Snp->Mode.MCastFilter, sizeof Snp->Mode.MCastFilter);
  191. CopyMem (
  192. &Snp->Mode.CurrentAddress,
  193. &Snp->Mode.PermanentAddress,
  194. sizeof (EFI_MAC_ADDRESS)
  195. );
  196. //
  197. // Compute tx/rx buffer sizes based on UNDI init info and parameters.
  198. //
  199. Snp->TxRxBufferSize = (UINT32)(Snp->InitInfo.MemoryRequired + ExtraRxBufferSize + ExtraTxBufferSize);
  200. //
  201. // If UNDI support cable detect for INITIALIZE command, try it first.
  202. //
  203. if (Snp->CableDetectSupported) {
  204. if (PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) == EFI_SUCCESS) {
  205. goto ON_EXIT;
  206. }
  207. }
  208. Snp->Mode.MediaPresent = FALSE;
  209. EfiStatus = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);
  210. if (EFI_ERROR (EfiStatus)) {
  211. gBS->CloseEvent (Snp->Snp.WaitForPacket);
  212. goto ON_EXIT;
  213. }
  214. //
  215. // Try to update the MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support it.
  216. //
  217. if (Snp->MediaStatusSupported) {
  218. PxeGetStatus (Snp, NULL, FALSE);
  219. }
  220. ON_EXIT:
  221. gBS->RestoreTPL (OldTpl);
  222. return EfiStatus;
  223. }