SnpGetStatus.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /** @file
  2. Implementation of the SNP.GetStatus() function and its private helpers if
  3. any.
  4. Copyright (C) 2013, Red Hat, Inc.
  5. Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <Library/BaseLib.h>
  9. #include <Library/UefiBootServicesTableLib.h>
  10. #include "VirtioNet.h"
  11. /**
  12. Reads the current interrupt status and recycled transmit buffer status from
  13. a network interface.
  14. @param This The protocol instance pointer.
  15. @param InterruptStatus A pointer to the bit mask of the currently active
  16. interrupts If this is NULL, the interrupt status will
  17. not be read from the device. If this is not NULL, the
  18. interrupt status will be read from the device. When
  19. the interrupt status is read, it will also be
  20. cleared. Clearing the transmit interrupt does not
  21. empty the recycled transmit buffer array.
  22. @param TxBuf Recycled transmit buffer address. The network
  23. interface will not transmit if its internal recycled
  24. transmit buffer array is full. Reading the transmit
  25. buffer does not clear the transmit interrupt. If this
  26. is NULL, then the transmit buffer status will not be
  27. read. If there are no transmit buffers to recycle and
  28. TxBuf is not NULL, * TxBuf will be set to NULL.
  29. @retval EFI_SUCCESS The status of the network interface was
  30. retrieved.
  31. @retval EFI_NOT_STARTED The network interface has not been started.
  32. @retval EFI_INVALID_PARAMETER One or more of the parameters has an
  33. unsupported value.
  34. @retval EFI_DEVICE_ERROR The command could not be sent to the network
  35. interface.
  36. @retval EFI_UNSUPPORTED This function is not supported by the network
  37. interface.
  38. **/
  39. EFI_STATUS
  40. EFIAPI
  41. VirtioNetGetStatus (
  42. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  43. OUT UINT32 *InterruptStatus OPTIONAL,
  44. OUT VOID **TxBuf OPTIONAL
  45. )
  46. {
  47. VNET_DEV *Dev;
  48. EFI_TPL OldTpl;
  49. EFI_STATUS Status;
  50. UINT16 RxCurUsed;
  51. UINT16 TxCurUsed;
  52. EFI_PHYSICAL_ADDRESS DeviceAddress;
  53. if (This == NULL) {
  54. return EFI_INVALID_PARAMETER;
  55. }
  56. Dev = VIRTIO_NET_FROM_SNP (This);
  57. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  58. switch (Dev->Snm.State) {
  59. case EfiSimpleNetworkStopped:
  60. Status = EFI_NOT_STARTED;
  61. goto Exit;
  62. case EfiSimpleNetworkStarted:
  63. Status = EFI_DEVICE_ERROR;
  64. goto Exit;
  65. default:
  66. break;
  67. }
  68. //
  69. // update link status
  70. //
  71. if (Dev->Snm.MediaPresentSupported) {
  72. UINT16 LinkStatus;
  73. Status = VIRTIO_CFG_READ (Dev, LinkStatus, &LinkStatus);
  74. if (EFI_ERROR (Status)) {
  75. goto Exit;
  76. }
  77. Dev->Snm.MediaPresent =
  78. (BOOLEAN)((LinkStatus & VIRTIO_NET_S_LINK_UP) != 0);
  79. }
  80. //
  81. // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
  82. //
  83. MemoryFence ();
  84. RxCurUsed = *Dev->RxRing.Used.Idx;
  85. TxCurUsed = *Dev->TxRing.Used.Idx;
  86. MemoryFence ();
  87. if (InterruptStatus != NULL) {
  88. //
  89. // report the receive interrupt if there is data available for reception,
  90. // report the transmit interrupt if we have transmitted at least one buffer
  91. //
  92. *InterruptStatus = 0;
  93. if (Dev->RxLastUsed != RxCurUsed) {
  94. *InterruptStatus |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  95. }
  96. if (Dev->TxLastUsed != TxCurUsed) {
  97. ASSERT (Dev->TxCurPending > 0);
  98. *InterruptStatus |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  99. }
  100. }
  101. if (TxBuf != NULL) {
  102. if (Dev->TxLastUsed == TxCurUsed) {
  103. *TxBuf = NULL;
  104. } else {
  105. UINT16 UsedElemIdx;
  106. UINT32 DescIdx;
  107. //
  108. // fetch the first descriptor among those that the hypervisor reports
  109. // completed
  110. //
  111. ASSERT (Dev->TxCurPending > 0);
  112. ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);
  113. UsedElemIdx = Dev->TxLastUsed++ % Dev->TxRing.QueueSize;
  114. DescIdx = Dev->TxRing.Used.UsedElem[UsedElemIdx].Id;
  115. ASSERT (DescIdx < (UINT32)(2 * Dev->TxMaxPending - 1));
  116. //
  117. // get the device address that has been enqueued for the caller's
  118. // transmit buffer
  119. //
  120. DeviceAddress = Dev->TxRing.Desc[DescIdx + 1].Addr;
  121. //
  122. // now this descriptor can be used again to enqueue a transmit buffer
  123. //
  124. Dev->TxFreeStack[--Dev->TxCurPending] = (UINT16)DescIdx;
  125. //
  126. // Unmap the device address and perform the reverse mapping to find the
  127. // caller buffer address.
  128. //
  129. Status = VirtioNetUnmapTxBuf (
  130. Dev,
  131. TxBuf,
  132. DeviceAddress
  133. );
  134. if (EFI_ERROR (Status)) {
  135. //
  136. // VirtioNetUnmapTxBuf should never fail, if we have reached here
  137. // that means our internal state has been corrupted
  138. //
  139. ASSERT (FALSE);
  140. Status = EFI_DEVICE_ERROR;
  141. goto Exit;
  142. }
  143. }
  144. }
  145. Status = EFI_SUCCESS;
  146. Exit:
  147. gBS->RestoreTPL (OldTpl);
  148. return Status;
  149. }