Transmit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /** @file
  2. Implementation of transmitting a packet.
  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 create the meadia header for the given data buffer.
  9. @param Snp Pointer to SNP driver structure.
  10. @param MacHeaderPtr Address where the media header will be filled in.
  11. @param HeaderSize Size of the memory at MacHeaderPtr.
  12. @param Buffer Data buffer pointer.
  13. @param BufferSize Size of data in the Buffer
  14. @param DestAddr Address of the destination mac address buffer.
  15. @param SrcAddr Address of the source mac address buffer.
  16. @param ProtocolPtr Address of the protocol type.
  17. @retval EFI_SUCCESS Successfully completed the undi call.
  18. @retval Other Error return from undi call.
  19. **/
  20. EFI_STATUS
  21. PxeFillHeader (
  22. SNP_DRIVER *Snp,
  23. VOID *MacHeaderPtr,
  24. UINTN HeaderSize,
  25. VOID *Buffer,
  26. UINTN BufferSize,
  27. EFI_MAC_ADDRESS *DestAddr,
  28. EFI_MAC_ADDRESS *SrcAddr,
  29. UINT16 *ProtocolPtr
  30. )
  31. {
  32. PXE_CPB_FILL_HEADER_FRAGMENTED *Cpb;
  33. Cpb = Snp->Cpb;
  34. if (SrcAddr != NULL) {
  35. CopyMem (
  36. (VOID *)Cpb->SrcAddr,
  37. (VOID *)SrcAddr,
  38. Snp->Mode.HwAddressSize
  39. );
  40. } else {
  41. CopyMem (
  42. (VOID *)Cpb->SrcAddr,
  43. (VOID *)&(Snp->Mode.CurrentAddress),
  44. Snp->Mode.HwAddressSize
  45. );
  46. }
  47. CopyMem (
  48. (VOID *)Cpb->DestAddr,
  49. (VOID *)DestAddr,
  50. Snp->Mode.HwAddressSize
  51. );
  52. //
  53. // we need to do the byte swapping
  54. //
  55. Cpb->Protocol = (UINT16)PXE_SWAP_UINT16 (*ProtocolPtr);
  56. Cpb->PacketLen = (UINT32)(BufferSize);
  57. Cpb->MediaHeaderLen = (UINT16)HeaderSize;
  58. Cpb->FragCnt = 2;
  59. Cpb->reserved = 0;
  60. Cpb->FragDesc[0].FragAddr = (UINT64)(UINTN)MacHeaderPtr;
  61. Cpb->FragDesc[0].FragLen = (UINT32)HeaderSize;
  62. Cpb->FragDesc[1].FragAddr = (UINT64)(UINTN)Buffer;
  63. Cpb->FragDesc[1].FragLen = (UINT32)BufferSize;
  64. Cpb->FragDesc[0].reserved = Cpb->FragDesc[1].reserved = 0;
  65. Snp->Cdb.OpCode = PXE_OPCODE_FILL_HEADER;
  66. Snp->Cdb.OpFlags = PXE_OPFLAGS_FILL_HEADER_FRAGMENTED;
  67. Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
  68. Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
  69. Snp->Cdb.CPBsize = (UINT16)sizeof (PXE_CPB_FILL_HEADER_FRAGMENTED);
  70. Snp->Cdb.CPBaddr = (UINT64)(UINTN)Cpb;
  71. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  72. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  73. Snp->Cdb.IFnum = Snp->IfNum;
  74. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  75. //
  76. // Issue UNDI command and check result.
  77. //
  78. DEBUG ((DEBUG_NET, "\nSnp->undi.fill_header() "));
  79. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  80. switch (Snp->Cdb.StatCode) {
  81. case PXE_STATCODE_SUCCESS:
  82. return EFI_SUCCESS;
  83. case PXE_STATCODE_INVALID_PARAMETER:
  84. DEBUG (
  85. (DEBUG_ERROR,
  86. "\nSnp->undi.fill_header() %xh:%xh\n",
  87. Snp->Cdb.StatFlags,
  88. Snp->Cdb.StatCode)
  89. );
  90. return EFI_INVALID_PARAMETER;
  91. default:
  92. DEBUG (
  93. (DEBUG_ERROR,
  94. "\nSnp->undi.fill_header() %xh:%xh\n",
  95. Snp->Cdb.StatFlags,
  96. Snp->Cdb.StatCode)
  97. );
  98. return EFI_DEVICE_ERROR;
  99. }
  100. }
  101. /**
  102. This routine calls undi to transmit the given data buffer
  103. @param Snp pointer to SNP driver structure
  104. @param Buffer data buffer pointer
  105. @param BufferSize Size of data in the Buffer
  106. @retval EFI_SUCCESS if successfully completed the undi call
  107. @retval Other error return from undi call.
  108. **/
  109. EFI_STATUS
  110. PxeTransmit (
  111. SNP_DRIVER *Snp,
  112. VOID *Buffer,
  113. UINTN BufferSize
  114. )
  115. {
  116. PXE_CPB_TRANSMIT *Cpb;
  117. EFI_STATUS Status;
  118. Cpb = Snp->Cpb;
  119. Cpb->FrameAddr = (UINT64)(UINTN)Buffer;
  120. Cpb->DataLen = (UINT32)BufferSize;
  121. Cpb->MediaheaderLen = 0;
  122. Cpb->reserved = 0;
  123. Snp->Cdb.OpFlags = PXE_OPFLAGS_TRANSMIT_WHOLE;
  124. Snp->Cdb.CPBsize = (UINT16)sizeof (PXE_CPB_TRANSMIT);
  125. Snp->Cdb.CPBaddr = (UINT64)(UINTN)Cpb;
  126. Snp->Cdb.OpCode = PXE_OPCODE_TRANSMIT;
  127. Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
  128. Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
  129. Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
  130. Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
  131. Snp->Cdb.IFnum = Snp->IfNum;
  132. Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
  133. //
  134. // Issue UNDI command and check result.
  135. //
  136. DEBUG ((DEBUG_NET, "\nSnp->undi.transmit() "));
  137. DEBUG ((DEBUG_NET, "\nSnp->Cdb.OpCode == %x", Snp->Cdb.OpCode));
  138. DEBUG ((DEBUG_NET, "\nSnp->Cdb.CPBaddr == %LX", Snp->Cdb.CPBaddr));
  139. DEBUG ((DEBUG_NET, "\nSnp->Cdb.DBaddr == %LX", Snp->Cdb.DBaddr));
  140. DEBUG ((DEBUG_NET, "\nCpb->FrameAddr == %LX\n", Cpb->FrameAddr));
  141. (*Snp->IssueUndi32Command)((UINT64)(UINTN)&Snp->Cdb);
  142. DEBUG ((DEBUG_NET, "\nexit Snp->undi.transmit() "));
  143. //
  144. // we will unmap the buffers in get_status call, not here
  145. //
  146. switch (Snp->Cdb.StatCode) {
  147. case PXE_STATCODE_SUCCESS:
  148. return EFI_SUCCESS;
  149. case PXE_STATCODE_BUFFER_FULL:
  150. case PXE_STATCODE_QUEUE_FULL:
  151. case PXE_STATCODE_BUSY:
  152. Status = EFI_NOT_READY;
  153. DEBUG (
  154. (DEBUG_NET,
  155. "\nSnp->undi.transmit() %xh:%xh\n",
  156. Snp->Cdb.StatFlags,
  157. Snp->Cdb.StatCode)
  158. );
  159. break;
  160. default:
  161. DEBUG (
  162. (DEBUG_ERROR,
  163. "\nSnp->undi.transmit() %xh:%xh\n",
  164. Snp->Cdb.StatFlags,
  165. Snp->Cdb.StatCode)
  166. );
  167. Status = EFI_DEVICE_ERROR;
  168. }
  169. return Status;
  170. }
  171. /**
  172. Places a packet in the transmit queue of a network interface.
  173. This function places the packet specified by Header and Buffer on the transmit
  174. queue. If HeaderSize is nonzero and HeaderSize is not equal to
  175. This->Mode->MediaHeaderSize, then EFI_INVALID_PARAMETER will be returned. If
  176. BufferSize is less than This->Mode->MediaHeaderSize, then EFI_BUFFER_TOO_SMALL
  177. will be returned. If Buffer is NULL, then EFI_INVALID_PARAMETER will be
  178. returned. If HeaderSize is nonzero and DestAddr or Protocol is NULL, then
  179. EFI_INVALID_PARAMETER will be returned. If the transmit engine of the network
  180. interface is busy, then EFI_NOT_READY will be returned. If this packet can be
  181. accepted by the transmit engine of the network interface, the packet contents
  182. specified by Buffer will be placed on the transmit queue of the network
  183. interface, and EFI_SUCCESS will be returned. GetStatus() can be used to
  184. determine when the packet has actually been transmitted. The contents of the
  185. Buffer must not be modified until the packet has actually been transmitted.
  186. The Transmit() function performs nonblocking I/O. A caller who wants to perform
  187. blocking I/O, should call Transmit(), and then GetStatus() until the
  188. transmitted buffer shows up in the recycled transmit buffer.
  189. If the driver has not been initialized, EFI_DEVICE_ERROR will be returned.
  190. @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
  191. @param HeaderSize The size, in bytes, of the media header to be filled in by the
  192. Transmit() function. If HeaderSize is nonzero, then it must
  193. be equal to This->Mode->MediaHeaderSize and the DestAddr and
  194. Protocol parameters must not be NULL.
  195. @param BufferSize The size, in bytes, of the entire packet (media header and
  196. data) to be transmitted through the network interface.
  197. @param Buffer A pointer to the packet (media header followed by data) to be
  198. transmitted. This parameter cannot be NULL. If HeaderSize is
  199. zero, then the media header in Buffer must already be filled
  200. in by the caller. If HeaderSize is nonzero, then the media
  201. header will be filled in by the Transmit() function.
  202. @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this
  203. parameter is ignored. If HeaderSize is nonzero and SrcAddr
  204. is NULL, then This->Mode->CurrentAddress is used for the
  205. source HW MAC address.
  206. @param DestAddr The destination HW MAC address. If HeaderSize is zero, then
  207. this parameter is ignored.
  208. @param Protocol The type of header to build. If HeaderSize is zero, then this
  209. parameter is ignored. See RFC 1700, section "Ether Types,"
  210. for examples.
  211. @retval EFI_SUCCESS The packet was placed on the transmit queue.
  212. @retval EFI_NOT_STARTED The network interface has not been started.
  213. @retval EFI_NOT_READY The network interface is too busy to accept this
  214. transmit request.
  215. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  216. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported
  217. value.
  218. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  219. @retval EFI_UNSUPPORTED This function is not supported by the network interface.
  220. **/
  221. EFI_STATUS
  222. EFIAPI
  223. SnpUndi32Transmit (
  224. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  225. IN UINTN HeaderSize,
  226. IN UINTN BufferSize,
  227. IN VOID *Buffer,
  228. IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  229. IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  230. IN UINT16 *Protocol OPTIONAL
  231. )
  232. {
  233. SNP_DRIVER *Snp;
  234. EFI_STATUS Status;
  235. EFI_TPL OldTpl;
  236. if (This == NULL) {
  237. return EFI_INVALID_PARAMETER;
  238. }
  239. Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
  240. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  241. if (Snp == NULL) {
  242. return EFI_DEVICE_ERROR;
  243. }
  244. switch (Snp->Mode.State) {
  245. case EfiSimpleNetworkInitialized:
  246. break;
  247. case EfiSimpleNetworkStopped:
  248. Status = EFI_NOT_STARTED;
  249. goto ON_EXIT;
  250. default:
  251. Status = EFI_DEVICE_ERROR;
  252. goto ON_EXIT;
  253. }
  254. if (Buffer == NULL) {
  255. Status = EFI_INVALID_PARAMETER;
  256. goto ON_EXIT;
  257. }
  258. if (BufferSize < Snp->Mode.MediaHeaderSize) {
  259. Status = EFI_BUFFER_TOO_SMALL;
  260. goto ON_EXIT;
  261. }
  262. //
  263. // if the HeaderSize is non-zero, we need to fill up the header and for that
  264. // we need the destination address and the protocol
  265. //
  266. if (HeaderSize != 0) {
  267. if ((HeaderSize != Snp->Mode.MediaHeaderSize) || (DestAddr == 0) || (Protocol == 0)) {
  268. Status = EFI_INVALID_PARAMETER;
  269. goto ON_EXIT;
  270. }
  271. Status = PxeFillHeader (
  272. Snp,
  273. Buffer,
  274. HeaderSize,
  275. (UINT8 *)Buffer + HeaderSize,
  276. BufferSize - HeaderSize,
  277. DestAddr,
  278. SrcAddr,
  279. Protocol
  280. );
  281. if (EFI_ERROR (Status)) {
  282. goto ON_EXIT;
  283. }
  284. }
  285. Status = PxeTransmit (Snp, Buffer, BufferSize);
  286. ON_EXIT:
  287. gBS->RestoreTPL (OldTpl);
  288. return Status;
  289. }