Get_status.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /** @file
  2. Implementation of reading the current interrupt status and recycled transmit
  3. buffer status from a network interface.
  4. Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Snp.h"
  8. /**
  9. Call undi to get the status of the interrupts, get the list of recycled transmit
  10. buffers that completed transmitting. The recycled transmit buffer address will
  11. be saved into Snp->RecycledTxBuf. This function will also update the MediaPresent
  12. field of EFI_SIMPLE_NETWORK_MODE if UNDI support it.
  13. @param[in] Snp Pointer to snp driver structure.
  14. @param[out] InterruptStatusPtr A non null pointer to contain the interrupt
  15. status.
  16. @param[in] GetTransmittedBuf Set to TRUE to retrieve the recycled transmit
  17. buffer address.
  18. @retval EFI_SUCCESS The status of the network interface was retrieved.
  19. @retval EFI_DEVICE_ERROR The command could not be sent to the network
  20. interface.
  21. **/
  22. EFI_STATUS
  23. PxeGetStatus (
  24. IN SNP_DRIVER *Snp,
  25. OUT UINT32 *InterruptStatusPtr,
  26. IN BOOLEAN GetTransmittedBuf
  27. )
  28. {
  29. PXE_DB_GET_STATUS *Db;
  30. UINT16 InterruptFlags;
  31. UINT32 Index;
  32. UINT64 *Tmp;
  33. Tmp = NULL;
  34. Db = Snp->Db;
  35. Snp->Cdb.OpCode = PXE_OPCODE_GET_STATUS;
  36. Snp->Cdb.OpFlags = 0;
  37. if (GetTransmittedBuf) {
  38. Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_TRANSMITTED_BUFFERS;
  39. ZeroMem (Db->TxBuffer, sizeof (Db->TxBuffer));
  40. }
  41. if (InterruptStatusPtr != NULL) {
  42. Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_INTERRUPT_STATUS;
  43. }
  44. if (Snp->MediaStatusSupported) {
  45. Snp->Cdb.OpFlags |= PXE_OPFLAGS_GET_MEDIA_STATUS;
  46. }
  47. Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
  48. Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
  49. Snp->Cdb.DBsize = (UINT16)sizeof (PXE_DB_GET_STATUS);
  50. Snp->Cdb.DBaddr = (UINT64)(UINTN)Db;
  51. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  52. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  53. Snp->Cdb.IFnum = Snp->IfNum;
  54. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  55. //
  56. // Issue UNDI command and check result.
  57. //
  58. DEBUG ((DEBUG_NET, "\nSnp->undi.get_status() "));
  59. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  60. if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
  61. DEBUG (
  62. (DEBUG_NET,
  63. "\nSnp->undi.get_status() %xh:%xh\n",
  64. Snp->Cdb.StatFlags,
  65. Snp->Cdb.StatCode)
  66. );
  67. return EFI_DEVICE_ERROR;
  68. }
  69. //
  70. // report the values back..
  71. //
  72. if (InterruptStatusPtr != NULL) {
  73. InterruptFlags = (UINT16)(Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_INTERRUPT_MASK);
  74. *InterruptStatusPtr = 0;
  75. if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_RECEIVE) == PXE_STATFLAGS_GET_STATUS_RECEIVE) {
  76. *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  77. }
  78. if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_TRANSMIT) == PXE_STATFLAGS_GET_STATUS_TRANSMIT) {
  79. *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  80. }
  81. if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_COMMAND) == PXE_STATFLAGS_GET_STATUS_COMMAND) {
  82. *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT;
  83. }
  84. if ((InterruptFlags & PXE_STATFLAGS_GET_STATUS_SOFTWARE) == PXE_STATFLAGS_GET_STATUS_SOFTWARE) {
  85. *InterruptStatusPtr |= EFI_SIMPLE_NETWORK_COMMAND_INTERRUPT;
  86. }
  87. }
  88. if (GetTransmittedBuf) {
  89. if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN) == 0) {
  90. //
  91. // UNDI has written some transmitted buffer addresses into the DB. Store them into Snp->RecycledTxBuf.
  92. //
  93. for (Index = 0; Index < MAX_XMIT_BUFFERS; Index++) {
  94. if (Db->TxBuffer[Index] != 0) {
  95. if (Snp->RecycledTxBufCount == Snp->MaxRecycledTxBuf) {
  96. //
  97. // Snp->RecycledTxBuf is full, reallocate a new one.
  98. //
  99. if ((Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT) >= SNP_MAX_TX_BUFFER_NUM) {
  100. return EFI_DEVICE_ERROR;
  101. }
  102. Tmp = AllocatePool (sizeof (UINT64) * (Snp->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT));
  103. if (Tmp == NULL) {
  104. return EFI_DEVICE_ERROR;
  105. }
  106. CopyMem (Tmp, Snp->RecycledTxBuf, sizeof (UINT64) * Snp->RecycledTxBufCount);
  107. FreePool (Snp->RecycledTxBuf);
  108. Snp->RecycledTxBuf = Tmp;
  109. Snp->MaxRecycledTxBuf += SNP_TX_BUFFER_INCREASEMENT;
  110. }
  111. Snp->RecycledTxBuf[Snp->RecycledTxBufCount] = Db->TxBuffer[Index];
  112. Snp->RecycledTxBufCount++;
  113. }
  114. }
  115. }
  116. }
  117. //
  118. // Update MediaPresent field of EFI_SIMPLE_NETWORK_MODE if the UNDI support
  119. // returning media status from GET_STATUS command
  120. //
  121. if (Snp->MediaStatusSupported) {
  122. Snp->Snp.Mode->MediaPresent =
  123. (BOOLEAN)(((Snp->Cdb.StatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA) != 0) ? FALSE : TRUE);
  124. }
  125. return EFI_SUCCESS;
  126. }
  127. /**
  128. Reads the current interrupt status and recycled transmit buffer status from a
  129. network interface.
  130. This function gets the current interrupt and recycled transmit buffer status
  131. from the network interface. The interrupt status is returned as a bit mask in
  132. InterruptStatus. If InterruptStatus is NULL, the interrupt status will not be
  133. read. If TxBuf is not NULL, a recycled transmit buffer address will be retrieved.
  134. If a recycled transmit buffer address is returned in TxBuf, then the buffer has
  135. been successfully transmitted, and the status for that buffer is cleared. If
  136. the status of the network interface is successfully collected, EFI_SUCCESS
  137. will be returned. If the driver has not been initialized, EFI_DEVICE_ERROR will
  138. be returned.
  139. @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
  140. @param InterruptStatus A pointer to the bit mask of the currently active
  141. interrupts (see "Related Definitions"). If this is NULL,
  142. the interrupt status will not be read from the device.
  143. If this is not NULL, the interrupt status will be read
  144. from the device. When the interrupt status is read, it
  145. will also be cleared. Clearing the transmit interrupt does
  146. not empty the recycled transmit buffer array.
  147. @param TxBuf Recycled transmit buffer address. The network interface
  148. will not transmit if its internal recycled transmit
  149. buffer array is full. Reading the transmit buffer does
  150. not clear the transmit interrupt. If this is NULL, then
  151. the transmit buffer status will not be read. If there
  152. are no transmit buffers to recycle and TxBuf is not NULL,
  153. TxBuf will be set to NULL.
  154. @retval EFI_SUCCESS The status of the network interface was retrieved.
  155. @retval EFI_NOT_STARTED The network interface has not been started.
  156. @retval EFI_INVALID_PARAMETER This parameter was NULL or did not point to a valid
  157. EFI_SIMPLE_NETWORK_PROTOCOL structure.
  158. @retval EFI_DEVICE_ERROR The command could not be sent to the network
  159. interface.
  160. **/
  161. EFI_STATUS
  162. EFIAPI
  163. SnpUndi32GetStatus (
  164. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  165. OUT UINT32 *InterruptStatus OPTIONAL,
  166. OUT VOID **TxBuf OPTIONAL
  167. )
  168. {
  169. SNP_DRIVER *Snp;
  170. EFI_TPL OldTpl;
  171. EFI_STATUS Status;
  172. if (This == NULL) {
  173. return EFI_INVALID_PARAMETER;
  174. }
  175. if ((InterruptStatus == NULL) && (TxBuf == NULL)) {
  176. return EFI_INVALID_PARAMETER;
  177. }
  178. Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
  179. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  180. if (Snp == NULL) {
  181. return EFI_DEVICE_ERROR;
  182. }
  183. switch (Snp->Mode.State) {
  184. case EfiSimpleNetworkInitialized:
  185. break;
  186. case EfiSimpleNetworkStopped:
  187. Status = EFI_NOT_STARTED;
  188. goto ON_EXIT;
  189. default:
  190. Status = EFI_DEVICE_ERROR;
  191. goto ON_EXIT;
  192. }
  193. if ((Snp->RecycledTxBufCount == 0) && (TxBuf != NULL)) {
  194. Status = PxeGetStatus (Snp, InterruptStatus, TRUE);
  195. } else {
  196. Status = PxeGetStatus (Snp, InterruptStatus, FALSE);
  197. }
  198. if (TxBuf != NULL) {
  199. //
  200. // Get a recycled buf from Snp->RecycledTxBuf
  201. //
  202. if (Snp->RecycledTxBufCount == 0) {
  203. *TxBuf = NULL;
  204. } else {
  205. Snp->RecycledTxBufCount--;
  206. *TxBuf = (VOID *)(UINTN)Snp->RecycledTxBuf[Snp->RecycledTxBufCount];
  207. }
  208. }
  209. ON_EXIT:
  210. gBS->RestoreTPL (OldTpl);
  211. return Status;
  212. }