Callback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /** @file
  2. This file contains the callback routines for undi3.1.
  3. the callback routines for Undi3.1 have an extra parameter UniqueId which
  4. stores the interface context for the NIC that snp is trying to talk.
  5. Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
  6. Copyright (c) Microsoft Corporation.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "Snp.h"
  10. /**
  11. Acquire or release a lock of the exclusive access to a critical section of the
  12. code/data.
  13. This is a callback routine supplied to UNDI3.1 at undi_start time.
  14. New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1
  15. because undi3.1 uses the MemMap call to map the required address by itself!
  16. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
  17. store Undi interface context (Undi does not read or write
  18. this variable).
  19. @param Enable Non-zero indicates acquire; Zero indicates release.
  20. **/
  21. VOID
  22. EFIAPI
  23. SnpUndi32CallbackBlock (
  24. IN UINT64 UniqueId,
  25. IN UINT32 Enable
  26. )
  27. {
  28. SNP_DRIVER *Snp;
  29. Snp = (SNP_DRIVER *)(UINTN)UniqueId;
  30. //
  31. // tcpip was calling snp at tpl_notify and when we acquire a lock that was
  32. // created at a lower level (TPL_CALLBACK) it gives an assert!
  33. //
  34. if (Enable != 0) {
  35. EfiAcquireLock (&Snp->Lock);
  36. } else {
  37. EfiReleaseLock (&Snp->Lock);
  38. }
  39. }
  40. /**
  41. Delay MicroSeconds of micro seconds.
  42. This is a callback routine supplied to UNDI at undi_start time.
  43. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
  44. store Undi interface context (Undi does not read or write
  45. this variable).
  46. @param MicroSeconds Number of micro seconds to pause, usually multiple of 10.
  47. **/
  48. VOID
  49. EFIAPI
  50. SnpUndi32CallbackDelay (
  51. IN UINT64 UniqueId,
  52. IN UINT64 MicroSeconds
  53. )
  54. {
  55. if (MicroSeconds != 0) {
  56. gBS->Stall ((UINTN)MicroSeconds);
  57. }
  58. }
  59. /**
  60. IO routine for UNDI3.1.
  61. This is a callback routine supplied to UNDI at undi_start time.
  62. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this
  63. to store Undi interface context (Undi does not read or
  64. write this variable).
  65. @param ReadOrWrite Indicates read or write, IO or Memory.
  66. @param NumBytes Number of bytes to read or write.
  67. @param MemOrPortAddr IO or memory address to read from or write to.
  68. @param BufferPtr Memory location to read into or that contains the bytes
  69. to write.
  70. **/
  71. VOID
  72. EFIAPI
  73. SnpUndi32CallbackMemio (
  74. IN UINT64 UniqueId,
  75. IN UINT8 ReadOrWrite,
  76. IN UINT8 NumBytes,
  77. IN UINT64 MemOrPortAddr,
  78. IN OUT UINT64 BufferPtr
  79. )
  80. {
  81. SNP_DRIVER *Snp;
  82. EFI_PCI_IO_PROTOCOL_WIDTH Width;
  83. Snp = (SNP_DRIVER *)(UINTN)UniqueId;
  84. Width = (EFI_PCI_IO_PROTOCOL_WIDTH)0;
  85. switch (NumBytes) {
  86. case 2:
  87. Width = (EFI_PCI_IO_PROTOCOL_WIDTH)1;
  88. break;
  89. case 4:
  90. Width = (EFI_PCI_IO_PROTOCOL_WIDTH)2;
  91. break;
  92. case 8:
  93. Width = (EFI_PCI_IO_PROTOCOL_WIDTH)3;
  94. break;
  95. }
  96. switch (ReadOrWrite) {
  97. case PXE_IO_READ:
  98. ASSERT (Snp->IoBarIndex < PCI_MAX_BAR);
  99. if (Snp->IoBarIndex < PCI_MAX_BAR) {
  100. Snp->PciIo->Io.Read (
  101. Snp->PciIo,
  102. Width,
  103. Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
  104. MemOrPortAddr,
  105. 1, // count
  106. (VOID *)(UINTN)BufferPtr
  107. );
  108. }
  109. break;
  110. case PXE_IO_WRITE:
  111. ASSERT (Snp->IoBarIndex < PCI_MAX_BAR);
  112. if (Snp->IoBarIndex < PCI_MAX_BAR) {
  113. Snp->PciIo->Io.Write (
  114. Snp->PciIo,
  115. Width,
  116. Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
  117. MemOrPortAddr,
  118. 1, // count
  119. (VOID *)(UINTN)BufferPtr
  120. );
  121. }
  122. break;
  123. case PXE_MEM_READ:
  124. ASSERT (Snp->MemoryBarIndex < PCI_MAX_BAR);
  125. if (Snp->MemoryBarIndex < PCI_MAX_BAR) {
  126. Snp->PciIo->Mem.Read (
  127. Snp->PciIo,
  128. Width,
  129. Snp->MemoryBarIndex, // BAR 0, Memory base address
  130. MemOrPortAddr,
  131. 1, // count
  132. (VOID *)(UINTN)BufferPtr
  133. );
  134. }
  135. break;
  136. case PXE_MEM_WRITE:
  137. ASSERT (Snp->MemoryBarIndex < PCI_MAX_BAR);
  138. if (Snp->MemoryBarIndex < PCI_MAX_BAR) {
  139. Snp->PciIo->Mem.Write (
  140. Snp->PciIo,
  141. Width,
  142. Snp->MemoryBarIndex, // BAR 0, Memory base address
  143. MemOrPortAddr,
  144. 1, // count
  145. (VOID *)(UINTN)BufferPtr
  146. );
  147. }
  148. break;
  149. }
  150. return;
  151. }
  152. /**
  153. Map a CPU address to a device address.
  154. This is a callback routine supplied to UNDI at undi_start time.
  155. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
  156. store Undi interface context (Undi does not read or write
  157. this variable).
  158. @param CpuAddr Virtual address to be mapped.
  159. @param NumBytes Size of memory to be mapped.
  160. @param Direction Direction of data flow for this memory's usage:
  161. cpu->device, device->cpu or both ways.
  162. @param DeviceAddrPtr Pointer to return the mapped device address.
  163. **/
  164. VOID
  165. EFIAPI
  166. SnpUndi32CallbackMap (
  167. IN UINT64 UniqueId,
  168. IN UINT64 CpuAddr,
  169. IN UINT32 NumBytes,
  170. IN UINT32 Direction,
  171. IN OUT UINT64 DeviceAddrPtr
  172. )
  173. {
  174. EFI_PHYSICAL_ADDRESS *DevAddrPtr;
  175. EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;
  176. UINTN BuffSize;
  177. SNP_DRIVER *Snp;
  178. UINTN Index;
  179. EFI_STATUS Status;
  180. BuffSize = (UINTN)NumBytes;
  181. Snp = (SNP_DRIVER *)(UINTN)UniqueId;
  182. DevAddrPtr = (EFI_PHYSICAL_ADDRESS *)(UINTN)DeviceAddrPtr;
  183. if (CpuAddr == 0) {
  184. *DevAddrPtr = 0;
  185. return;
  186. }
  187. switch (Direction) {
  188. case TO_AND_FROM_DEVICE:
  189. DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;
  190. break;
  191. case FROM_DEVICE:
  192. DirectionFlag = EfiPciIoOperationBusMasterWrite;
  193. break;
  194. case TO_DEVICE:
  195. DirectionFlag = EfiPciIoOperationBusMasterRead;
  196. break;
  197. default:
  198. *DevAddrPtr = 0;
  199. //
  200. // any non zero indicates error!
  201. //
  202. return;
  203. }
  204. //
  205. // find an unused map_list entry
  206. //
  207. for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
  208. if (Snp->MapList[Index].VirtualAddress == 0) {
  209. break;
  210. }
  211. }
  212. if (Index >= MAX_MAP_LENGTH) {
  213. DEBUG ((DEBUG_INFO, "SNP maplist is FULL\n"));
  214. *DevAddrPtr = 0;
  215. return;
  216. }
  217. Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS)CpuAddr;
  218. Status = Snp->PciIo->Map (
  219. Snp->PciIo,
  220. DirectionFlag,
  221. (VOID *)(UINTN)CpuAddr,
  222. &BuffSize,
  223. DevAddrPtr,
  224. &(Snp->MapList[Index].MapCookie)
  225. );
  226. if (Status != EFI_SUCCESS) {
  227. *DevAddrPtr = 0;
  228. Snp->MapList[Index].VirtualAddress = 0;
  229. }
  230. return;
  231. }
  232. /**
  233. Unmap an address that was previously mapped using map callback.
  234. This is a callback routine supplied to UNDI at undi_start time.
  235. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
  236. store. Undi interface context (Undi does not read or write
  237. this variable).
  238. @param CpuAddr Virtual address that was mapped.
  239. @param NumBytes Size of memory mapped.
  240. @param Direction Direction of data flow for this memory's usage:
  241. cpu->device, device->cpu or both ways.
  242. @param DeviceAddr The mapped device address.
  243. **/
  244. VOID
  245. EFIAPI
  246. SnpUndi32CallbackUnmap (
  247. IN UINT64 UniqueId,
  248. IN UINT64 CpuAddr,
  249. IN UINT32 NumBytes,
  250. IN UINT32 Direction,
  251. IN UINT64 DeviceAddr
  252. )
  253. {
  254. SNP_DRIVER *Snp;
  255. UINT16 Index;
  256. Snp = (SNP_DRIVER *)(UINTN)UniqueId;
  257. for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
  258. if (Snp->MapList[Index].VirtualAddress == CpuAddr) {
  259. break;
  260. }
  261. }
  262. if (Index >= MAX_MAP_LENGTH) {
  263. DEBUG ((DEBUG_ERROR, "SNP could not find a mapping, failed to unmap.\n"));
  264. return;
  265. }
  266. Snp->PciIo->Unmap (Snp->PciIo, Snp->MapList[Index].MapCookie);
  267. Snp->MapList[Index].VirtualAddress = 0;
  268. Snp->MapList[Index].MapCookie = NULL;
  269. return;
  270. }
  271. /**
  272. Synchronize the virtual buffer contents with the mapped buffer contents.
  273. This is a callback routine supplied to UNDI at undi_start time. The virtual
  274. and mapped buffers need not correspond to the same physical memory (especially
  275. if the virtual address is > 4GB). Depending on the direction for which the
  276. buffer is mapped, undi will need to synchronize their contents whenever it
  277. writes to/reads from the buffer using either the cpu address or the device
  278. address.
  279. EFI does not provide a sync call since virt=physical, we should just do the
  280. synchronization ourselves here.
  281. @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
  282. store Undi interface context (Undi does not read or write
  283. this variable).
  284. @param CpuAddr Virtual address that was mapped.
  285. @param NumBytes Size of memory mapped.
  286. @param Direction Direction of data flow for this memory's usage:
  287. cpu->device, device->cpu or both ways.
  288. @param DeviceAddr The mapped device address.
  289. **/
  290. VOID
  291. EFIAPI
  292. SnpUndi32CallbackSync (
  293. IN UINT64 UniqueId,
  294. IN UINT64 CpuAddr,
  295. IN UINT32 NumBytes,
  296. IN UINT32 Direction,
  297. IN UINT64 DeviceAddr
  298. )
  299. {
  300. if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {
  301. return;
  302. }
  303. switch (Direction) {
  304. case FROM_DEVICE:
  305. CopyMem ((UINT8 *)(UINTN)CpuAddr, (UINT8 *)(UINTN)DeviceAddr, NumBytes);
  306. break;
  307. case TO_DEVICE:
  308. CopyMem ((UINT8 *)(UINTN)DeviceAddr, (UINT8 *)(UINTN)CpuAddr, NumBytes);
  309. break;
  310. }
  311. return;
  312. }