VirtioNet.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /** @file
  2. Internal definitions for the virtio-net driver, which produces Simple Network
  3. Protocol instances for virtio-net devices.
  4. Copyright (C) 2013, Red Hat, Inc.
  5. Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #ifndef _VIRTIO_NET_DXE_H_
  9. #define _VIRTIO_NET_DXE_H_
  10. #include <IndustryStandard/VirtioNet.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/VirtioLib.h>
  13. #include <Protocol/ComponentName.h>
  14. #include <Protocol/ComponentName2.h>
  15. #include <Protocol/DevicePath.h>
  16. #include <Protocol/DriverBinding.h>
  17. #include <Protocol/SimpleNetwork.h>
  18. #include <Library/OrderedCollectionLib.h>
  19. #define VNET_SIG SIGNATURE_32 ('V', 'N', 'E', 'T')
  20. //
  21. // maximum number of pending packets, separately for each direction
  22. //
  23. #define VNET_MAX_PENDING 64
  24. //
  25. // State diagram:
  26. //
  27. // | ^
  28. // | |
  29. // BindingStart BindingStop
  30. // +SnpPopulate |
  31. // ++GetFeatures |
  32. // | |
  33. // v |
  34. // +---------+ virtio-net device is reset, no resources are
  35. // | stopped | allocated for traffic, but MAC address has
  36. // +---------+ been retrieved
  37. // | ^
  38. // | |
  39. // SNP.Start SNP.Stop
  40. // | |
  41. // v |
  42. // +---------+
  43. // | started | functionally identical to stopped
  44. // +---------+
  45. // | ^
  46. // | |
  47. // SNP.Initialize SNP.Shutdown
  48. // | |
  49. // v |
  50. // +-------------+ Virtio-net setup complete, including DRIVER_OK
  51. // | initialized | bit. The receive queue is populated with
  52. // +-------------+ requests; McastIpToMac, GetStatus, Transmit,
  53. // Receive are callable.
  54. //
  55. typedef struct {
  56. //
  57. // Parts of this structure are initialized / torn down in various functions
  58. // at various call depths. The table to the right should make it easier to
  59. // track them.
  60. //
  61. // field init function
  62. // ------------------ ------------------------------
  63. UINT32 Signature; // VirtioNetDriverBindingStart
  64. VIRTIO_DEVICE_PROTOCOL *VirtIo; // VirtioNetDriverBindingStart
  65. EFI_SIMPLE_NETWORK_PROTOCOL Snp; // VirtioNetSnpPopulate
  66. EFI_SIMPLE_NETWORK_MODE Snm; // VirtioNetSnpPopulate
  67. EFI_EVENT ExitBoot; // VirtioNetSnpPopulate
  68. EFI_DEVICE_PATH_PROTOCOL *MacDevicePath; // VirtioNetDriverBindingStart
  69. EFI_HANDLE MacHandle; // VirtioNetDriverBindingStart
  70. VRING RxRing; // VirtioNetInitRing
  71. VOID *RxRingMap; // VirtioRingMap and
  72. // VirtioNetInitRing
  73. UINT8 *RxBuf; // VirtioNetInitRx
  74. UINT16 RxLastUsed; // VirtioNetInitRx
  75. UINTN RxBufNrPages; // VirtioNetInitRx
  76. EFI_PHYSICAL_ADDRESS RxBufDeviceBase; // VirtioNetInitRx
  77. VOID *RxBufMap; // VirtioNetInitRx
  78. VRING TxRing; // VirtioNetInitRing
  79. VOID *TxRingMap; // VirtioRingMap and
  80. // VirtioNetInitRing
  81. UINT16 TxMaxPending; // VirtioNetInitTx
  82. UINT16 TxCurPending; // VirtioNetInitTx
  83. UINT16 *TxFreeStack; // VirtioNetInitTx
  84. VIRTIO_1_0_NET_REQ *TxSharedReq; // VirtioNetInitTx
  85. VOID *TxSharedReqMap; // VirtioNetInitTx
  86. UINT16 TxLastUsed; // VirtioNetInitTx
  87. ORDERED_COLLECTION *TxBufCollection; // VirtioNetInitTx
  88. } VNET_DEV;
  89. //
  90. // In order to avoid duplication of interface documentation, please find all
  91. // leading comments near the respective function / variable definitions (not
  92. // the declarations here), which is where your code editor of choice takes you
  93. // anyway when jumping to a function.
  94. //
  95. //
  96. // utility macros
  97. //
  98. #define VIRTIO_NET_FROM_SNP(SnpPointer) \
  99. CR (SnpPointer, VNET_DEV, Snp, VNET_SIG)
  100. #define VIRTIO_CFG_WRITE(Dev, Field, Value) ((Dev)->VirtIo->WriteDevice ( \
  101. (Dev)->VirtIo, \
  102. OFFSET_OF_VNET (Field), \
  103. SIZE_OF_VNET (Field), \
  104. (Value) \
  105. ))
  106. #define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice ( \
  107. (Dev)->VirtIo, \
  108. OFFSET_OF_VNET (Field), \
  109. SIZE_OF_VNET (Field), \
  110. sizeof *(Pointer), \
  111. (Pointer) \
  112. ))
  113. //
  114. // component naming
  115. //
  116. extern EFI_COMPONENT_NAME_PROTOCOL gVirtioNetComponentName;
  117. extern EFI_COMPONENT_NAME2_PROTOCOL gVirtioNetComponentName2;
  118. //
  119. // driver binding
  120. //
  121. extern EFI_DRIVER_BINDING_PROTOCOL gVirtioNetDriverBinding;
  122. //
  123. // member functions implementing the Simple Network Protocol
  124. //
  125. EFI_STATUS
  126. EFIAPI
  127. VirtioNetStart (
  128. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  129. );
  130. EFI_STATUS
  131. EFIAPI
  132. VirtioNetStop (
  133. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  134. );
  135. EFI_STATUS
  136. EFIAPI
  137. VirtioNetInitialize (
  138. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  139. IN UINTN ExtraRxBufferSize OPTIONAL,
  140. IN UINTN ExtraTxBufferSize OPTIONAL
  141. );
  142. EFI_STATUS
  143. EFIAPI
  144. VirtioNetReset (
  145. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  146. IN BOOLEAN ExtendedVerification
  147. );
  148. EFI_STATUS
  149. EFIAPI
  150. VirtioNetShutdown (
  151. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  152. );
  153. EFI_STATUS
  154. EFIAPI
  155. VirtioNetReceiveFilters (
  156. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  157. IN UINT32 Enable,
  158. IN UINT32 Disable,
  159. IN BOOLEAN ResetMCastFilter,
  160. IN UINTN MCastFilterCnt OPTIONAL,
  161. IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
  162. );
  163. EFI_STATUS
  164. EFIAPI
  165. VirtioNetStationAddress (
  166. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  167. IN BOOLEAN Reset,
  168. IN EFI_MAC_ADDRESS *New OPTIONAL
  169. );
  170. EFI_STATUS
  171. EFIAPI
  172. VirtioNetStatistics (
  173. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  174. IN BOOLEAN Reset,
  175. IN OUT UINTN *StatisticsSize OPTIONAL,
  176. OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
  177. );
  178. EFI_STATUS
  179. EFIAPI
  180. VirtioNetMcastIpToMac (
  181. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  182. IN BOOLEAN IPv6,
  183. IN EFI_IP_ADDRESS *Ip,
  184. OUT EFI_MAC_ADDRESS *Mac
  185. );
  186. EFI_STATUS
  187. EFIAPI
  188. VirtioNetNvData (
  189. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  190. IN BOOLEAN ReadWrite,
  191. IN UINTN Offset,
  192. IN UINTN BufferSize,
  193. IN OUT VOID *Buffer
  194. );
  195. EFI_STATUS
  196. EFIAPI
  197. VirtioNetGetStatus (
  198. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  199. OUT UINT32 *InterruptStatus OPTIONAL,
  200. OUT VOID **TxBuf OPTIONAL
  201. );
  202. EFI_STATUS
  203. EFIAPI
  204. VirtioNetTransmit (
  205. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  206. IN UINTN HeaderSize,
  207. IN UINTN BufferSize,
  208. IN /* +OUT! */ VOID *Buffer,
  209. IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  210. IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  211. IN UINT16 *Protocol OPTIONAL
  212. );
  213. EFI_STATUS
  214. EFIAPI
  215. VirtioNetReceive (
  216. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  217. OUT UINTN *HeaderSize OPTIONAL,
  218. IN OUT UINTN *BufferSize,
  219. OUT VOID *Buffer,
  220. OUT EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  221. OUT EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  222. OUT UINT16 *Protocol OPTIONAL
  223. );
  224. //
  225. // utility functions shared by various SNP member functions
  226. //
  227. VOID
  228. EFIAPI
  229. VirtioNetShutdownRx (
  230. IN OUT VNET_DEV *Dev
  231. );
  232. VOID
  233. EFIAPI
  234. VirtioNetShutdownTx (
  235. IN OUT VNET_DEV *Dev
  236. );
  237. VOID
  238. EFIAPI
  239. VirtioNetUninitRing (
  240. IN OUT VNET_DEV *Dev,
  241. IN OUT VRING *Ring,
  242. IN VOID *RingMap
  243. );
  244. //
  245. // utility functions to map caller-supplied Tx buffer system physical address
  246. // to a device address and vice versa
  247. //
  248. EFI_STATUS
  249. EFIAPI
  250. VirtioNetMapTxBuf (
  251. IN VNET_DEV *Dev,
  252. IN VOID *Buffer,
  253. IN UINTN NumberOfBytes,
  254. OUT EFI_PHYSICAL_ADDRESS *DeviceAddress
  255. );
  256. EFI_STATUS
  257. EFIAPI
  258. VirtioNetUnmapTxBuf (
  259. IN VNET_DEV *Dev,
  260. OUT VOID **Buffer,
  261. IN EFI_PHYSICAL_ADDRESS DeviceAddress
  262. );
  263. INTN
  264. EFIAPI
  265. VirtioNetTxBufMapInfoCompare (
  266. IN CONST VOID *UserStruct1,
  267. IN CONST VOID *UserStruct2
  268. );
  269. INTN
  270. EFIAPI
  271. VirtioNetTxBufDeviceAddressCompare (
  272. IN CONST VOID *StandaloneKey,
  273. IN CONST VOID *UserStruct
  274. );
  275. //
  276. // event callbacks
  277. //
  278. VOID
  279. EFIAPI
  280. VirtioNetIsPacketAvailable (
  281. IN EFI_EVENT Event,
  282. IN VOID *Context
  283. );
  284. VOID
  285. EFIAPI
  286. VirtioNetExitBoot (
  287. IN EFI_EVENT Event,
  288. IN VOID *Context
  289. );
  290. #endif // _VIRTIO_NET_DXE_H_