Start removing dead code and ReactOS specific structure fields (the NDIS structures...
[reactos.git] / reactos / drivers / net / ndis / ndis / memory.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS library
4 * FILE: ndis/memory.c
5 * PURPOSE: Memory management routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Vizzini (vizzini@plasmic.com)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 * 15 Aug 2003 Vizzini - DMA support
11 * 3 Oct 2003 Vizzini - formatting and minor bugfixing
12 */
13
14 #include "ndissys.h"
15
16 \f
17 /*
18 * @implemented
19 */
20 NDIS_STATUS
21 EXPORT
22 NdisAllocateMemoryWithTag(
23 OUT PVOID *VirtualAddress,
24 IN UINT Length,
25 IN ULONG Tag)
26 /*
27 * FUNCTION: Allocates a block of memory, with a 32-bit tag
28 * ARGUMENTS:
29 * VirtualAddress = a pointer to the returned memory block
30 * Length = the number of requested bytes
31 * Tag = 32-bit pool tag
32 * RETURNS:
33 * NDIS_STATUS_SUCCESS on success
34 * NDIS_STATUS_FAILURE on failure
35 */
36 {
37 PVOID Block;
38
39 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
40
41 Block = ExAllocatePoolWithTag(NonPagedPool, Length, Tag);
42 *VirtualAddress = Block;
43
44 if (!Block)
45 return NDIS_STATUS_FAILURE;
46
47 return NDIS_STATUS_SUCCESS;
48 }
49
50
51 \f
52 /*
53 * @implemented
54 */
55 NDIS_STATUS
56 EXPORT
57 NdisAllocateMemory(
58 OUT PVOID *VirtualAddress,
59 IN UINT Length,
60 IN UINT MemoryFlags,
61 IN NDIS_PHYSICAL_ADDRESS HighestAcceptableAddress)
62 /*
63 * FUNCTION: Allocates a block of memory
64 * ARGUMENTS:
65 * VirtualAddress = Address of buffer to place virtual
66 * address of the allocated memory
67 * Length = Size of the memory block to allocate
68 * MemoryFlags = Flags to specify special restrictions
69 * HighestAcceptableAddress = Specifies -1
70 * RETURNS:
71 * NDIS_STATUS_SUCCESS on success
72 * NDIS_STATUS_FAILURE on failure
73 */
74 {
75 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
76
77 if (MemoryFlags & NDIS_MEMORY_NONCACHED)
78 {
79 *VirtualAddress = MmAllocateNonCachedMemory(Length);
80 if(!*VirtualAddress)
81 return NDIS_STATUS_FAILURE;
82
83 return NDIS_STATUS_SUCCESS;
84 }
85
86 if (MemoryFlags & NDIS_MEMORY_CONTIGUOUS)
87 {
88 *VirtualAddress = MmAllocateContiguousMemory(Length, HighestAcceptableAddress);
89 if(!*VirtualAddress)
90 return NDIS_STATUS_FAILURE;
91
92 return NDIS_STATUS_SUCCESS;
93 }
94
95 /* Plain nonpaged memory */
96 *VirtualAddress = ExAllocatePool(NonPagedPool, Length);
97 if (!*VirtualAddress)
98 return NDIS_STATUS_FAILURE;
99
100 return NDIS_STATUS_SUCCESS;
101 }
102
103 \f
104 /*
105 * @implemented
106 */
107 VOID
108 EXPORT
109 NdisFreeMemory(
110 IN PVOID VirtualAddress,
111 IN UINT Length,
112 IN UINT MemoryFlags)
113 /*
114 * FUNCTION: Frees a memory block allocated with NdisAllocateMemory
115 * ARGUMENTS:
116 * VirtualAddress = Pointer to the base virtual address of the allocated memory
117 * Length = Size of the allocated memory block as passed to NdisAllocateMemory
118 * MemoryFlags = Memory flags passed to NdisAllocateMemory
119 */
120 {
121 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
122
123 if (MemoryFlags & NDIS_MEMORY_NONCACHED)
124 {
125 MmFreeNonCachedMemory(VirtualAddress, Length);
126 return;
127 }
128
129 if (MemoryFlags & NDIS_MEMORY_CONTIGUOUS)
130 {
131 MmFreeContiguousMemory(VirtualAddress);
132 return;
133 }
134
135 ExFreePool(VirtualAddress);
136 }
137
138 \f
139 /*
140 * @unimplemented
141 */
142 VOID
143 EXPORT
144 NdisImmediateReadSharedMemory(
145 IN NDIS_HANDLE WrapperConfigurationContext,
146 IN ULONG SharedMemoryAddress,
147 OUT PUCHAR Buffer,
148 IN ULONG Length)
149 {
150 UNIMPLEMENTED
151 }
152
153 \f
154 /*
155 * @unimplemented
156 */
157 VOID
158 EXPORT
159 NdisImmediateWriteSharedMemory(
160 IN NDIS_HANDLE WrapperConfigurationContext,
161 IN ULONG SharedMemoryAddress,
162 IN PUCHAR Buffer,
163 IN ULONG Length)
164 {
165 UNIMPLEMENTED
166 }
167
168 \f
169 /*
170 * @implemented
171 */
172 VOID
173 EXPORT
174 NdisMAllocateSharedMemory(
175 IN NDIS_HANDLE MiniportAdapterHandle,
176 IN ULONG Length,
177 IN BOOLEAN Cached,
178 OUT PVOID *VirtualAddress,
179 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress)
180 /*
181 * FUNCTION: Allocate a common buffer for DMA
182 * ARGUMENTS:
183 * MiniportAdapterHandle: Handle passed into MiniportInitialize
184 * Length: Number of bytes to allocate
185 * Cached: Whether or not the memory can be cached
186 * VirtualAddress: Pointer to memory is returned here
187 * PhysicalAddress: Physical address corresponding to virtual address
188 * NOTES:
189 * - Cached is ignored; we always allocate non-cached
190 */
191 {
192 PNDIS_MINIPORT_BLOCK Adapter = (PNDIS_MINIPORT_BLOCK)MiniportAdapterHandle;
193
194 NDIS_DbgPrint(MAX_TRACE,("Called.\n"));
195
196 *VirtualAddress = Adapter->SystemAdapterObject->DmaOperations->AllocateCommonBuffer(
197 Adapter->SystemAdapterObject, Length, PhysicalAddress, Cached);
198 }
199
200 \f
201 /*
202 * @unimplemented
203 */
204 NDIS_STATUS
205 EXPORT
206 NdisMAllocateSharedMemoryAsync(
207 IN NDIS_HANDLE MiniportAdapterHandle,
208 IN ULONG Length,
209 IN BOOLEAN Cached,
210 IN PVOID Context)
211 {
212 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
213 UNIMPLEMENTED
214
215 return NDIS_STATUS_FAILURE;
216 }
217
218 \f
219 VOID
220 NTAPI
221 NdisMFreeSharedMemoryPassive(
222 PVOID Context)
223 /*
224 * FUNCTION: Free a common buffer
225 * ARGUMENTS:
226 * Context: Pointer to a miniport shared memory context
227 * NOTES:
228 * - Called by NdisMFreeSharedMemory to do the actual work
229 */
230 {
231 PMINIPORT_SHARED_MEMORY Memory = (PMINIPORT_SHARED_MEMORY)Context;
232
233 NDIS_DbgPrint(MAX_TRACE, ("Called.\n"));
234
235 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
236
237 Memory->AdapterObject->DmaOperations->FreeCommonBuffer(
238 Memory->AdapterObject, Memory->Length, Memory->PhysicalAddress,
239 Memory->VirtualAddress, Memory->Cached);
240
241 ExFreePool(Memory);
242 }
243
244 \f
245 /*
246 * @implemented
247 */
248 VOID
249 EXPORT
250 NdisMFreeSharedMemory(
251 IN NDIS_HANDLE MiniportAdapterHandle,
252 IN ULONG Length,
253 IN BOOLEAN Cached,
254 IN PVOID VirtualAddress,
255 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress)
256 /*
257 * FUNCTION: Free a shared memory block
258 * ARGUMENTS:
259 * MiniportAdapterHandle: Handle passed into MiniportInitialize
260 * Length: Number of bytes in the block to free
261 * Cached: Whether or not the memory was cached
262 * VirtualAddress: Address to free
263 * PhysicalAddress: corresponding physical addres
264 * NOTES:
265 * - This function can be called at dispatch_level or passive_level.
266 * Therefore we have to do this in a worker thread.
267 */
268 {
269 HANDLE ThreadHandle;
270 PNDIS_MINIPORT_BLOCK Adapter = (PNDIS_MINIPORT_BLOCK)MiniportAdapterHandle;
271 PMINIPORT_SHARED_MEMORY Memory;
272
273 NDIS_DbgPrint(MAX_TRACE,("Called.\n"));
274
275 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
276
277 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
278 Memory = ExAllocatePool(NonPagedPool, sizeof(MINIPORT_SHARED_MEMORY));
279
280 if(!Memory)
281 {
282 NDIS_DbgPrint(MID_TRACE, ("Insufficient resources\n"));
283 return;
284 }
285
286 Memory->AdapterObject = Adapter->SystemAdapterObject;
287 Memory->Length = Length;
288 Memory->PhysicalAddress = PhysicalAddress;
289 Memory->VirtualAddress = VirtualAddress;
290 Memory->Cached = Cached;
291
292 PsCreateSystemThread(&ThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0, NdisMFreeSharedMemoryPassive, Memory);
293 ZwClose(ThreadHandle);
294 }
295
296 /* EOF */
297