summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/madge/driver/sys_allo.c
blob: d643826c8b4ade69a85771a8ff0397c796caece9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492











































































































































































































































































































































































































































































































                                                                                
/****************************************************************************
*
* SYS_ALLO.C
*
* FastMAC Plus based NDIS3 miniport driver. This module contains helper 
* routines used by the FTK to allocate resources.
*                                                                         
* Copyright (c) Madge Networks Ltd 1991-1994                                    
*    
* COMPANY CONFIDENTIAL
*
* Created:             MF
* Major modifications: PBA  21/06/1994
*                                                                           
****************************************************************************/

#include <ndis.h>   

#include "ftk_defs.h"  
#include "ftk_extr.h" 

#include "ndismod.h"


/***************************************************************************
*
* Function    - sys_allocate_adapter_structure
*
* Parameter   - adapter_handle              -> FTK adapter handle.
*               adapter_structure_byte_size -> Size of the adapter structure.
*
* Purpose     - Allocate an FTK adapter structure.
*
* Returns     - A pointer to the structure on success or NULL on failure.
*
***************************************************************************/

BYTE *   
sys_alloc_adapter_structure(
    ADAPTER_HANDLE adapter_handle,
    WORD           adapter_structure_byte_size
    );

#pragma FTK_INIT_FUNCTION(sys_alloc_adapter_structure)

BYTE *   
sys_alloc_adapter_structure(
    ADAPTER_HANDLE adapter_handle,
    WORD           adapter_structure_byte_size
    )
{
    PVOID       ptr;
    NDIS_STATUS status;

    MADGE_ALLOC_MEMORY(&status, &ptr, (UINT) adapter_structure_byte_size);

    return (status == NDIS_STATUS_SUCCESS) 
        ? (BYTE *) ptr 
        : NULL;

}

/***************************************************************************
*
* Function    - sys_allocate_dma_phys_buffer
*
* Parameter   - adapter_handle    -> FTK adapter handle.
*               buffer_byte_size  -> Size of the DMA buffer.
*               buf_phys          -> Pointer to a holder for the DMA 
*                                    buffer's physical address.
*               buf_virt          -> Pointer to a holder for the DMA
*                                    buffer's virtual address.
*
* Purpose     - Allocate a DMA buffer.
*
* Returns     - TRUE on success or FALSE on failure.
*
***************************************************************************/

WBOOLEAN 
sys_alloc_dma_phys_buffer(
    ADAPTER_HANDLE   adapter_handle,
    DWORD            buffer_byte_size,
    DWORD          * buf_phys,
    DWORD          * buf_virt
    );

#pragma FTK_INIT_FUNCTION(sys_alloc_dma_phys_buffer)

WBOOLEAN 
sys_alloc_dma_phys_buffer(
    ADAPTER_HANDLE   adapter_handle,
    DWORD            buffer_byte_size,
    DWORD          * buf_phys,
    DWORD          * buf_virt
    )
{
    PMADGE_ADAPTER          ndisAdap;
    VOID                  * virt;
    NDIS_PHYSICAL_ADDRESS   phys;
    NDIS_STATUS             status;

    *buf_virt = 0;
    *buf_phys = 0;
    ndisAdap  = (PMADGE_ADAPTER) FTK_ADAPTER_USER_INFORMATION(adapter_handle);

    //
    // If we are in DMA mode then we must use shared memory. If we are
    // not in DMA mode then we can use ordinary memory.
    //

    if (ndisAdap->TransferMode == DMA_DATA_TRANSFER_MODE)
    {
#ifdef _ALPHA_

	//
	// If we are running on an Alpha platform then we need
	// to allocate map registers. This allocation scheme was 
        // recommended to FrancisT by DEC.
	//

	if (NdisMAllocateMapRegisters(
		ndisAdap->UsedInISR.MiniportHandle,
		(ndisAdap->DmaChannel == 0) ? 255 : ndisAdap->DmaChannel,
		(BOOLEAN) (ndisAdap->NTCardBusType != NdisInterfaceIsa),
		BYTES_TO_PAGES(buffer_byte_size),
		buffer_byte_size
		) != NDIS_STATUS_SUCCESS)
	{
	    return FALSE;
	}

	//
	// Note that we have allocated some map registers.
	//

	ndisAdap->MapRegistersAllocated += BYTES_TO_PAGES(buffer_byte_size);

#endif

#ifdef _MIPS_

	//
	// If we are running on a MIPs platform then we only
	// seem to need one map register.
	//

	if (ndisAdap->MapRegistersAllocated == 0)
	{
	    if (NdisMAllocateMapRegisters(
		    ndisAdap->UsedInISR.MiniportHandle,
		    (ndisAdap->DmaChannel == 0) ? 255 : ndisAdap->DmaChannel,
		    (BOOLEAN) (ndisAdap->NTCardBusType != NdisInterfaceIsa),
		    1,
		    buffer_byte_size
		    ) != NDIS_STATUS_SUCCESS)
	    {
		 return FALSE;
	    }

	    //
	    // Note that we have allocated some map registers.
	    //

	    ndisAdap->MapRegistersAllocated++;
	}

#endif

#ifdef _PPC_

	//
	// If we are running on a PPC platform then we only
	// seem to need one map register.
	//

	if (ndisAdap->MapRegistersAllocated == 0)
	{
	    if (NdisMAllocateMapRegisters(
		    ndisAdap->UsedInISR.MiniportHandle,
		    (ndisAdap->DmaChannel == 0) ? 255 : ndisAdap->DmaChannel,
		    (BOOLEAN) (ndisAdap->NTCardBusType != NdisInterfaceIsa),
		    1,
		    buffer_byte_size
		    ) != NDIS_STATUS_SUCCESS)
	    {
		return FALSE;
	    }

	    //
	    // Note that we have allocated some map registers.
	    //

	    ndisAdap->MapRegistersAllocated++;
	}

#endif

#ifdef _M_IX86

	//
	// If we are running on an Intel platform then we only
	// seem to need one map register.
	//

	if (ndisAdap->MapRegistersAllocated == 0)
	{
	    if (NdisMAllocateMapRegisters(
		    ndisAdap->UsedInISR.MiniportHandle,
		    (ndisAdap->DmaChannel == 0) ? 255 : ndisAdap->DmaChannel,
		    (BOOLEAN) (ndisAdap->NTCardBusType != NdisInterfaceIsa),
		    1,
		    buffer_byte_size
		    ) != NDIS_STATUS_SUCCESS)
	    {
		return FALSE;
	    }

	    //
	    // Note that we have allocated some map registers.
	    //

	    ndisAdap->MapRegistersAllocated++;
	}

#endif

        MadgePrint1("sys_alloc_dma_phys_buffer: allocating SHARED memory\n");

        NdisMAllocateSharedMemory(
            ndisAdap->UsedInISR.MiniportHandle,
            (ULONG) buffer_byte_size,
            FALSE,
            &virt,
            &phys
            );
    
        if (virt != NULL)
        {
            *buf_virt = (DWORD) virt;
            *buf_phys = (DWORD) NdisGetPhysicalAddressLow(phys);
        }

        MadgePrint3(
            "sys_alloc_dma_phys_buffer physical low = %lx high = %lx\n",
            (DWORD) NdisGetPhysicalAddressLow(phys),
            (DWORD) NdisGetPhysicalAddressHigh(phys)
            );
    }

    else
    {
        MadgePrint1("sys_alloc_dma_phys_buffer: allocating NORMAL memory\n");

        MADGE_ALLOC_MEMORY(&status, &virt, buffer_byte_size);

        if (status == NDIS_STATUS_SUCCESS)
        {
            *buf_virt = (DWORD) virt;
        }
        else
        {
            virt = NULL;
        }
    }


    MadgePrint2("sys_alloc_dma_phys_buffer virtual = %lx\n", (DWORD) virt);

    return virt != NULL;
}


/***************************************************************************
*
* Function    - sys_allocate_status_structure
*
* Parameter   - adapter_handle             -> FTK adapter handle.
*               status_structure_byte_size -> Size of the status structure.
*
* Purpose     - Allocate an FTK status structure.
*
* Returns     - A pointer to the structure on success or NULL on failure.
*
***************************************************************************/

BYTE *   
sys_alloc_status_structure(
    ADAPTER_HANDLE adapter_handle,
    WORD           status_structure_byte_size
    );

#pragma FTK_INIT_FUNCTION(sys_alloc_status_structure)

BYTE *   
sys_alloc_status_structure(
    ADAPTER_HANDLE adapter_handle,
    WORD           status_structure_byte_size
    )
{
    PVOID       ptr;
    NDIS_STATUS status;

    MADGE_ALLOC_MEMORY(&status, &ptr, (UINT) status_structure_byte_size);

    return (status == NDIS_STATUS_SUCCESS) 
        ? (BYTE *) ptr 
        : NULL;

}


/***************************************************************************
*
* Function    - sys_allocate_init_block
*
* Parameter   - adapter_handle       -> FTK adapter handle.
*               init_block_byte_size -> Size of the adapter structure.
*
* Purpose     - Allocate an FTK initialisation block.
*
* Returns     - A pointer to the block on success or NULL on failure.
*
***************************************************************************/

BYTE *   
sys_alloc_init_block(
    ADAPTER_HANDLE adapter_handle,
    WORD           init_block_byte_size
    );

#pragma FTK_INIT_FUNCTION(sys_alloc_init_block)

BYTE *   
sys_alloc_init_block(
    ADAPTER_HANDLE adapter_handle,
    WORD           init_block_byte_size
    )
{
    PVOID       ptr;
    NDIS_STATUS status;

    MADGE_ALLOC_MEMORY(&status, &ptr, (UINT) init_block_byte_size);

    return (status == NDIS_STATUS_SUCCESS) 
        ? (BYTE *) ptr 
        : NULL;
}


/***************************************************************************
*
* Function    - sys_free_adapter_structure
*
* Parameter   - adapter_handle              -> FTK adapter handle.
*               adapter_structure_addr      -> Pointer to the adapter 
*                                              structure.
*               adapter_structure_byte_size -> Size of the adapter structure.
*
* Purpose     - Deallocate an FTK adapter structure.
*
* Returns     - Nothing.
*
***************************************************************************/

void     
sys_free_adapter_structure(
    ADAPTER_HANDLE adapter_handle,
    BYTE *         adapter_structure_addr,
    WORD           adapter_structure_byte_size
    )
{
    MADGE_FREE_MEMORY(
        (PVOID) adapter_structure_addr, 
        (UINT) adapter_structure_byte_size
        );
}


/***************************************************************************
*
* Function    - sys_free_dma_phys_buffer
*
* Parameter   - adapter_handle    -> FTK adapter handle.
*               buffer_byte_size  -> Size of the DMA buffer.
*               buf_phys          -> The DMA buffer's physical address.
*               buf_virt          -> The DMA buffer's virtual address.
*
* Purpose     - Free a DMA buffer.
*
* Returns     - Nothing.
*
***************************************************************************/

void 
sys_free_dma_phys_buffer(
    ADAPTER_HANDLE   adapter_handle,
    DWORD            buffer_byte_size,
    DWORD            buf_phys,
    DWORD            buf_virt
    )
{
    PMADGE_ADAPTER          ndisAdap;
    VOID                  * virt;
    NDIS_PHYSICAL_ADDRESS   phys;

    ndisAdap = (PMADGE_ADAPTER) FTK_ADAPTER_USER_INFORMATION(adapter_handle);
    virt     = (VOID *) buf_virt;

    //
    // If we are in DMA mode then we must free shared memory, otherwise we
    // must free ordinary memory.
    //

    if (ndisAdap->TransferMode == DMA_DATA_TRANSFER_MODE)
    {
        NdisSetPhysicalAddressHigh(phys, 0);
        NdisSetPhysicalAddressLow(phys, buf_phys);

        NdisMFreeSharedMemory(
            ndisAdap->UsedInISR.MiniportHandle,
            (ULONG) buffer_byte_size,
            FALSE,
            virt,
            phys
            );
    }

    else
    {
        MADGE_FREE_MEMORY(virt, buffer_byte_size);
    }
}

/***************************************************************************
*
* Function    - sys_free_status_structure
*
* Parameter   - adapter_handle             -> FTK adapter handle.
*               status_structure_addr      -> Pointer to the status structure.
*               status_structure_byte_size -> Size of the status structure.
*
* Purpose     - Deallocate an FTK status structure.
*
* Returns     - Nothing.
*
***************************************************************************/

export void     
sys_free_status_structure(
    ADAPTER_HANDLE adapter_handle,
    BYTE *         status_structure_addr,
    WORD           status_structure_byte_size
    )
{
    MADGE_FREE_MEMORY(
        (PVOID) status_structure_addr, 
        (UINT) status_structure_byte_size
        );
}


/***************************************************************************
*
* Function    - sys_free_init_block
*
* Parameter   - adapter_handle       -> FTK adapter handle.
*               init_block_addr      -> Pointer to the initialisation block.
*               init_block_byte_size -> Size of the initialisation block.
*
* Purpose     - Deallocate an FTK initialisation block.
*
* Returns     - Nothing.
*
***************************************************************************/

export void     
sys_free_init_block(
    ADAPTER_HANDLE adapter_handle,
    BYTE *         init_block_addr,
    WORD           init_block_byte_size
    )
{
    MADGE_FREE_MEMORY(
        (PVOID) init_block_addr, 
        (UINT) init_block_byte_size
        );
}


/******** End of SYS_ALLO.C ***********************************************/