Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

414 lines
26 KiB

  1. /*++
  2. Copyright (c) 1990-1995 Microsoft Corporation
  3. Module Name:
  4. sendm.h
  5. Abstract:
  6. Author:
  7. Kyle Brandon (KyleB)
  8. Environment:
  9. Kernel mode
  10. Revision History:
  11. --*/
  12. #ifndef __SENDM_H
  13. #define __SENDM_H
  14. #define SEND_PACKET_ARRAY 16
  15. //
  16. // The following definitions apply to serialized drivers only. Packets for the deserialized drivers are not queued
  17. // within NDIS
  18. //
  19. #define PACKET_LIST(_P) (PLIST_ENTRY)((_P)->WrapperReserved)
  20. #define GET_FIRST_MINIPORT_PACKET(_M, _P) \
  21. { \
  22. *(_P) = NULL; \
  23. if (!IsListEmpty(&(_M)->PacketList)) \
  24. { \
  25. PLIST_ENTRY _L; \
  26. \
  27. _L = (_M)->PacketList.Flink; \
  28. *(_P) = CONTAINING_RECORD(_L, NDIS_PACKET, WrapperReserved); \
  29. } \
  30. }
  31. //
  32. // Macros used for getting to OOB data and packet extension.
  33. //
  34. #define PNDIS_PACKET_OOB_DATA_FROM_PNDIS_PACKET(_P) \
  35. (PNDIS_PACKET_OOB_DATA)((PUCHAR)(_P) + (_P)->Private.NdisPacketOobOffset)
  36. #define INIT_PACKET_LIST(_P) InitializeListHead(PACKET_LIST(_P))
  37. #define LINK_PACKET(_M, _P, _NSR, _O) \
  38. { \
  39. (_NSR)->Open = _O; \
  40. INIT_PACKET_LIST(_P); \
  41. \
  42. InsertTailList(&(_M)->PacketList, \
  43. PACKET_LIST(_P)); \
  44. }
  45. #define LINK_WAN_PACKET(_M, _P) \
  46. { \
  47. InitializeListHead(&(_P)->WanPacketQueue); \
  48. \
  49. InsertTailList(&(_M)->PacketList, \
  50. &(_P)->WanPacketQueue); \
  51. }
  52. #define UNLINK_PACKET(_M, _P, _NSR) \
  53. { \
  54. ASSERT(_P != (_M)->FirstPendingPacket); \
  55. RemoveEntryList(PACKET_LIST(_P)); \
  56. INIT_PACKET_LIST(_P); \
  57. }
  58. #define UNLINK_WAN_PACKET(_P) \
  59. { \
  60. RemoveEntryList(&(_P)->WanPacketQueue); \
  61. InitializeListHead(&(_P)->WanPacketQueue); \
  62. }
  63. #if DBG
  64. #define VALIDATE_PACKET_OPEN(_P) \
  65. { \
  66. PNDIS_STACK_RESERVED _NSR; \
  67. \
  68. NDIS_STACK_RESERVED_FROM_PACKET(_P, &_NSR); \
  69. ASSERT(VALID_OPEN(_NSR->Open)); \
  70. }
  71. #else
  72. #define VALIDATE_PACKET_OPEN(_P)
  73. #endif
  74. #define NEXT_PACKET_PENDING(_M, _P) \
  75. { \
  76. PLIST_ENTRY _List; \
  77. \
  78. _List = PACKET_LIST(_P); \
  79. (_M)->FirstPendingPacket = NULL; \
  80. if (_List->Flink != &(_M)->PacketList) \
  81. { \
  82. (_M)->FirstPendingPacket = \
  83. CONTAINING_RECORD(_List->Flink, \
  84. NDIS_PACKET, \
  85. WrapperReserved); \
  86. VALIDATE_PACKET_OPEN((_M)->FirstPendingPacket); \
  87. } \
  88. }
  89. #if DBG
  90. #define CHECK_FOR_DUPLICATE_PACKET(_M, _P) \
  91. { \
  92. IF_DBG(DBG_COMP_SEND, DBG_LEVEL_FATAL) \
  93. { \
  94. PLIST_ENTRY _List; \
  95. \
  96. if (!IsListEmpty(&(_M)->PacketList)) \
  97. { \
  98. PNDIS_PACKET _Pkt; \
  99. \
  100. for (_List = (_M)->PacketList.Flink; \
  101. _List != &(_M)->PacketList; \
  102. _List = _List->Flink) \
  103. { \
  104. _Pkt = CONTAINING_RECORD(_List, \
  105. NDIS_PACKET, \
  106. WrapperReserved); \
  107. if (_P == _Pkt) \
  108. { \
  109. DBGBREAK(DBG_COMP_SEND, DBG_LEVEL_FATAL); \
  110. } \
  111. } \
  112. } \
  113. } \
  114. }
  115. #else
  116. #define CHECK_FOR_DUPLICATE_PACKET(_M, _P)
  117. #endif
  118. #define ADD_RESOURCE(_M, C) MINIPORT_SET_FLAG(_M, fMINIPORT_RESOURCES_AVAILABLE)
  119. #define CLEAR_RESOURCE(_M, C) MINIPORT_CLEAR_FLAG(_M, fMINIPORT_RESOURCES_AVAILABLE)
  120. #define VALID_OPEN(_O) (((ULONG_PTR)(_O) & 0xFFFFFF00) != MAGIC_OPEN)
  121. #define MAGIC_OPEN 'MOC\00'
  122. #define MAGIC_OPEN_I(x) (PVOID)((MAGIC_OPEN & 0xFFFFFF00) + 0x00000030 + x)
  123. #define CLEAR_WRAPPER_RESERVED(_NSR) (_NSR)->RefUlong = 0
  124. #if ARCNET
  125. #define FREE_ARCNET_BUFFER(_M, _P, _O) \
  126. { \
  127. /* \
  128. * If this is arcnet, then free the appended header. \
  129. */ \
  130. if ((_M)->MediaType == NdisMediumArcnet878_2) \
  131. { \
  132. ndisMFreeArcnetHeader(_M, _P, _O); \
  133. } \
  134. }
  135. #else
  136. #define FREE_ARCNET_BUFFER(_M, _P, _O)
  137. #endif
  138. #define NDISM_COMPLETE_SEND(_M, _P, _NSR, _S, _L, _I) \
  139. { \
  140. PNDIS_OPEN_BLOCK _O; \
  141. ULONG _OR; \
  142. \
  143. if (!(_L)) \
  144. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  145. \
  146. _O = (_NSR)->Open; \
  147. \
  148. UNLINK_PACKET(_M, _P, _NSR); \
  149. POP_PACKET_STACK(_P); \
  150. \
  151. ADD_RESOURCE((_M), 'F'); \
  152. \
  153. DBGPRINT(DBG_COMP_SEND, DBG_LEVEL_INFO, \
  154. ("Completed 0x%x\n", (_S))); \
  155. \
  156. FREE_ARCNET_BUFFER(_M, _P, _O); \
  157. \
  158. (_NSR)->Open = MAGIC_OPEN_I(_I); \
  159. \
  160. if (!(_L) && (NULL != (_M)->FirstPendingPacket)) \
  161. { \
  162. /* \
  163. * Do we need to queue another workitem to process more sends ? \
  164. */ \
  165. NDISM_QUEUE_WORK_ITEM(_M, NdisWorkItemSend, NULL); \
  166. } \
  167. \
  168. /* \
  169. * Indicate the completion to the protocol. \
  170. */ \
  171. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC((_M)); \
  172. \
  173. /* \
  174. * Make sure that an IM which shares send and receive packets on the same \
  175. * pool works fine with the check in the receive path. \
  176. */ \
  177. (_NSR)->RefCount = 0; \
  178. MINIPORT_CLEAR_PACKET_FLAG(_P, fPACKET_CLEAR_ITEMS); \
  179. CLEAR_WRAPPER_RESERVED(_NSR); \
  180. (_O->SendCompleteHandler)(_O->ProtocolBindingContext, \
  181. (_P), \
  182. (_S)); \
  183. \
  184. M_OPEN_DECREMENT_REF_INTERLOCKED(_O, _OR) \
  185. DBGPRINT(DBG_COMP_OPENREF, DBG_LEVEL_INFO, \
  186. ("- Open 0x%x Reference 0x%x\n", _O, _O->References)); \
  187. \
  188. if (_OR == 0) \
  189. { \
  190. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  191. ndisMFinishClose(_O); \
  192. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC((_M)); \
  193. } \
  194. \
  195. if (_L) \
  196. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  197. }
  198. #define NDISM_COMPLETE_SEND_RESOURCES(_M, _NSR, _P) \
  199. { \
  200. DBGPRINT(DBG_COMP_SEND, DBG_LEVEL_INFO, \
  201. ("Deferring send\n")); \
  202. \
  203. ASSERT(VALID_OPEN((_NSR)->Open)); \
  204. /* \
  205. * Put on pending queue \
  206. */ \
  207. MINIPORT_CLEAR_PACKET_FLAG(_P, fPACKET_PENDING); \
  208. (_M)->FirstPendingPacket = (_P); \
  209. \
  210. /* \
  211. * Mark the miniport as out of send resources. \
  212. */ \
  213. CLEAR_RESOURCE((_M), 'S'); \
  214. }
  215. #define NDISM_SEND_PACKET(_M, _O, _P, _pS) \
  216. { \
  217. UINT _Flags; \
  218. BOOLEAN _SelfDirected; \
  219. \
  220. /* \
  221. * Indicate the packet loopback if necessary. \
  222. */ \
  223. \
  224. if (NDIS_CHECK_FOR_LOOPBACK(_M, _P)) \
  225. { \
  226. _SelfDirected = ndisMLoopbackPacketX(_M, _P); \
  227. } \
  228. else \
  229. { \
  230. _SelfDirected = FALSE; \
  231. } \
  232. \
  233. if (_SelfDirected) \
  234. { \
  235. DBGPRINT(DBG_COMP_SEND, DBG_LEVEL_INFO, \
  236. ("Packet is self-directed.\n")); \
  237. \
  238. /* \
  239. * Self-directed loopback always succeeds. \
  240. */ \
  241. *(_pS) = NDIS_STATUS_SUCCESS; \
  242. } \
  243. else \
  244. { \
  245. DBGPRINT(DBG_COMP_SEND, DBG_LEVEL_INFO, \
  246. ("Sending packet 0x%p\n", _P)); \
  247. \
  248. NdisQuerySendFlags((_P), &_Flags); \
  249. \
  250. /* \
  251. * Call down to the driver. \
  252. */ \
  253. MINIPORT_SET_PACKET_FLAG(_P, fPACKET_PENDING); \
  254. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC(_M); \
  255. *(_pS) = ((_O)->WSendHandler)((_O)->MiniportAdapterContext, _P, _Flags);\
  256. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  257. } \
  258. }
  259. #define PACKETS_OUT(_M) (_M)->NdisStats.DirectedPacketsOut ++
  260. #define DIRECTED_PACKETS_IN(_M) (_M)->NdisStats.DirectedPacketsIn ++
  261. #define BYTES_OUT(_M, _B)
  262. #define DIRECTED_BYTES_IN(_M, _S)
  263. #define DIRECTED_BYTES_IN_PACKET(_M, _P)
  264. #define ndisMCheckPacketAndGetStatsOut(_M, _P, _S) \
  265. { \
  266. PNDIS_BUFFER _Buffer; \
  267. \
  268. *(_S) = NDIS_STATUS_SUCCESS; \
  269. PACKETS_OUT(_M); \
  270. _Buffer = (_P)->Private.Head; \
  271. while (_Buffer != NULL) \
  272. { \
  273. if (MDL_ADDRESS_SAFE(_Buffer, HighPagePriority) == NULL) \
  274. { \
  275. *(_S) = NDIS_STATUS_RESOURCES; \
  276. break; \
  277. } \
  278. BYTES_OUT(_M, _Buffer); \
  279. _Buffer = _Buffer->Next; \
  280. } \
  281. }
  282. #define ndisMCheckPacketAndGetStatsOutAlreadyMapped(_M, _P) \
  283. { \
  284. PNDIS_BUFFER _Buffer; \
  285. \
  286. PACKETS_OUT(_M); \
  287. _Buffer = (_P)->Private.Head; \
  288. while (_Buffer != NULL) \
  289. { \
  290. BYTES_OUT(_M, _Buffer); \
  291. _Buffer = _Buffer->Next; \
  292. } \
  293. }
  294. #define NDISM_COMPLETE_SEND_SG(_M, _P, _NSR, _S, _L, _I, _UNLINK) \
  295. { \
  296. PNDIS_OPEN_BLOCK _O; \
  297. ULONG _OR; \
  298. \
  299. if (!(_L)) \
  300. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  301. \
  302. _O = (_NSR)->Open; \
  303. \
  304. if (_UNLINK) \
  305. { \
  306. UNLINK_PACKET(_M, _P, _NSR); \
  307. } \
  308. POP_PACKET_STACK(_P); \
  309. \
  310. ADD_RESOURCE((_M), 'F'); \
  311. \
  312. DBGPRINT(DBG_COMP_SEND, DBG_LEVEL_INFO, \
  313. ("Completed 0x%x\n", (_S))); \
  314. \
  315. (_NSR)->Open = MAGIC_OPEN_I(_I); \
  316. \
  317. if (!(_L) && (NULL != (_M)->FirstPendingPacket)) \
  318. { \
  319. /* \
  320. * Do we need to queue another workitem to process more sends ? \
  321. */ \
  322. NDISM_QUEUE_WORK_ITEM(_M, NdisWorkItemSend, NULL); \
  323. } \
  324. \
  325. /* \
  326. * Indicate the completion to the protocol. \
  327. */ \
  328. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC((_M)); \
  329. \
  330. if (NDIS_PER_PACKET_INFO_FROM_PACKET(_P, ScatterGatherListPacketInfo) != NULL) \
  331. { \
  332. ndisMFreeSGList(_M, _P); \
  333. } \
  334. /* \
  335. * Make sure that an IM which shares send and receive packets on the same \
  336. * pool works fine with the check in the receive path. \
  337. */ \
  338. (_NSR)->RefCount = 0; \
  339. MINIPORT_CLEAR_PACKET_FLAG(_P, fPACKET_CLEAR_ITEMS); \
  340. CLEAR_WRAPPER_RESERVED(_NSR); \
  341. (_O->SendCompleteHandler)(_O->ProtocolBindingContext, \
  342. (_P), \
  343. (_S)); \
  344. \
  345. M_OPEN_DECREMENT_REF_INTERLOCKED(_O, _OR) \
  346. DBGPRINT(DBG_COMP_OPENREF, DBG_LEVEL_INFO, \
  347. ("- Open 0x%x Reference 0x%x\n", _O, _O->References)); \
  348. \
  349. if (_OR == 0) \
  350. { \
  351. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  352. ndisMFinishClose(_O); \
  353. NDIS_RELEASE_MINIPORT_SPIN_LOCK_DPC((_M)); \
  354. } \
  355. \
  356. if (_L) \
  357. NDIS_ACQUIRE_MINIPORT_SPIN_LOCK_DPC(_M); \
  358. }
  359. #define LINK_PACKET_SG(_M, _P, _NSR) \
  360. { \
  361. INIT_PACKET_LIST(_P); \
  362. \
  363. InsertTailList(&(_M)->PacketList, \
  364. PACKET_LIST(_P)); \
  365. }
  366. #endif // __SENDM_H