2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 This file contains two sets of callback routines for undi3.0 and undi3.1.
16 the callback routines for Undi3.1 have an extra parameter UniqueId which
17 stores the interface context for the NIC that snp is trying to talk..
26 // these 2 global variables are used only for 3.0 undi. we could not place
27 // them in the snp structure because we will not know which snp structure
28 // in the callback context!
30 STATIC BOOLEAN mInitializeLock
= TRUE
;
31 STATIC EFI_LOCK mLock
;
34 // End Global variables
36 extern EFI_PCI_IO_PROTOCOL
*mPciIoFncs
;
39 snp_undi32_callback_v2p_30 (
41 IN OUT UINT64 DeviceAddrPtr
46 This is a callback routine supplied to UNDI at undi_start time.
47 UNDI call this routine with a virtual or CPU address that SNP provided
48 to convert it to a physical or device address. Since EFI uses the identical
49 mapping, this routine returns the physical address same as the virtual address
50 for most of the addresses. an address above 4GB cannot generally be used as a
51 device address, it needs to be mapped to a lower physical address. This routine
52 does not call the map routine itself, but it assumes that the mapping was done
53 at the time of providing the address to UNDI. This routine just looks up the
54 address in a map table (which is the v2p structure chain)
57 CpuAddr - virtual address of a buffer
58 DeviceAddrPtr - pointer to the physical address
61 void - The DeviceAddrPtr will contain 0 in case of any error
67 // Do nothing if virtual address is zero or physical pointer is NULL.
68 // No need to map if the virtual address is within 4GB limit since
69 // EFI uses identical mapping
71 if ((CpuAddr
== 0) || (DeviceAddrPtr
== 0)) {
72 DEBUG ((EFI_D_ERROR
, "\nv2p: Null virtual address or physical pointer.\n"));
76 if (CpuAddr
< FOUR_GIGABYTES
) {
77 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
81 // SNP creates a vaddr tp paddr mapping at the time of calling undi with any
82 // big address, this callback routine just looks up in the v2p list and
83 // returns the physical address for any given virtual address.
85 if (find_v2p (&v2p
, (VOID
*) (UINTN
) CpuAddr
) != EFI_SUCCESS
) {
86 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
88 *(UINT64
*) (UINTN
) DeviceAddrPtr
= v2p
->paddr
;
93 snp_undi32_callback_block_30 (
99 This is a callback routine supplied to UNDI at undi_start time.
100 UNDI call this routine when it wants to have exclusive access to a critical
101 section of the code/data
104 Enable - non-zero indicates acquire
105 zero indicates release
112 // tcpip was calling snp at tpl_notify and if we acquire a lock that was
113 // created at a lower level (TPL_CALLBACK) it gives an assert!
115 if (mInitializeLock
) {
116 EfiInitializeLock (&mLock
, TPL_NOTIFY
);
117 mInitializeLock
= FALSE
;
121 EfiAcquireLock (&mLock
);
123 EfiReleaseLock (&mLock
);
128 snp_undi32_callback_delay_30 (
129 IN UINT64 MicroSeconds
134 This is a callback routine supplied to UNDI at undi_start time.
135 UNDI call this routine with the number of micro seconds when it wants to
139 MicroSeconds - number of micro seconds to pause, ususlly multiple of 10
145 if (MicroSeconds
!= 0) {
146 gBS
->Stall ((UINTN
) MicroSeconds
);
151 snp_undi32_callback_memio_30 (
152 IN UINT8 ReadOrWrite
,
155 IN OUT UINT64 BufferAddr
160 This is a callback routine supplied to UNDI at undi_start time.
161 This is the IO routine for UNDI. This is not currently being used by UNDI3.0
162 because Undi3.0 uses io/mem offsets relative to the beginning of the device
163 io/mem address and so it needs to use the PCI_IO_FUNCTION that abstracts the
164 start of the device's io/mem addresses. Since SNP cannot retrive the context
165 of the undi3.0 interface it cannot use the PCI_IO_FUNCTION that specific for
166 that NIC and uses one global IO functions structure, this does not work.
167 This however works fine for EFI1.0 Undis because they use absolute addresses
171 ReadOrWrite - indicates read or write, IO or Memory
172 NumBytes - number of bytes to read or write
173 Address - IO or memory address to read from or write to
174 BufferAddr - memory location to read into or that contains the bytes
181 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
185 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
189 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
193 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
197 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
200 switch (ReadOrWrite
) {
202 mPciIoFncs
->Io
.Read (
205 1, // BAR 1, IO base address
208 (VOID
*) (UINTN
) BufferAddr
213 mPciIoFncs
->Io
.Write (
216 1, // BAR 1, IO base address
219 (VOID
*) (UINTN
) BufferAddr
224 mPciIoFncs
->Mem
.Read (
227 0, // BAR 0, Memory base address
230 (VOID
*) (UINTN
) BufferAddr
235 mPciIoFncs
->Mem
.Write (
238 0, // BAR 0, Memory base address
241 (VOID
*) (UINTN
) BufferAddr
249 // New callbacks for 3.1:
250 // there won't be a virtual2physical callback for UNDI 3.1 because undi3.1 uses
251 // the MemMap call to map the required address by itself!
254 snp_undi32_callback_block (
261 This is a callback routine supplied to UNDI3.1 at undi_start time.
262 UNDI call this routine when it wants to have exclusive access to a critical
263 section of the code/data
266 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
267 Undi interface context (Undi does not read or write this variable)
268 Enable - non-zero indicates acquire
269 zero indicates release
278 snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
280 // tcpip was calling snp at tpl_notify and when we acquire a lock that was
281 // created at a lower level (TPL_CALLBACK) it gives an assert!
284 EfiAcquireLock (&snp
->lock
);
286 EfiReleaseLock (&snp
->lock
);
291 snp_undi32_callback_delay (
293 IN UINT64 MicroSeconds
298 This is a callback routine supplied to UNDI at undi_start time.
299 UNDI call this routine with the number of micro seconds when it wants to
303 MicroSeconds - number of micro seconds to pause, ususlly multiple of 10
309 if (MicroSeconds
!= 0) {
310 gBS
->Stall ((UINTN
) MicroSeconds
);
315 * IO routine for UNDI start CPB.
318 snp_undi32_callback_memio (
328 This is a callback routine supplied to UNDI at undi_start time.
329 This is the IO routine for UNDI3.1.
332 ReadOrWrite - indicates read or write, IO or Memory
333 NumBytes - number of bytes to read or write
334 Address - IO or memory address to read from or write to
335 BufferAddr - memory location to read into or that contains the bytes
343 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
345 snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
347 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
350 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
354 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
358 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
362 switch (ReadOrWrite
) {
364 snp
->IoFncs
->Io
.Read (
367 snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
370 (VOID
*) (UINTN
) BufferAddr
375 snp
->IoFncs
->Io
.Write (
378 snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
381 (VOID
*) (UINTN
) BufferAddr
386 snp
->IoFncs
->Mem
.Read (
389 snp
->MemoryBarIndex
, // BAR 0, Memory base address
392 (VOID
*) (UINTN
) BufferAddr
397 snp
->IoFncs
->Mem
.Write (
400 snp
->MemoryBarIndex
, // BAR 0, Memory base address
403 (VOID
*) (UINTN
) BufferAddr
412 snp_undi32_callback_map (
417 IN OUT UINT64 DeviceAddrPtr
422 This is a callback routine supplied to UNDI at undi_start time.
423 UNDI call this routine when it has to map a CPU address to a device
427 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
428 Undi interface context (Undi does not read or write this variable)
429 CpuAddr - Virtual address to be mapped!
430 NumBytes - size of memory to be mapped
431 Direction - direction of data flow for this memory's usage:
432 cpu->device, device->cpu or both ways
433 DeviceAddrPtr - pointer to return the mapped device address
440 EFI_PHYSICAL_ADDRESS
*DevAddrPtr
;
441 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag
;
447 BuffSize
= (UINTN
) NumBytes
;
448 snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
449 DevAddrPtr
= (EFI_PHYSICAL_ADDRESS
*) (UINTN
) DeviceAddrPtr
;
457 case TO_AND_FROM_DEVICE
:
458 DirectionFlag
= EfiPciIoOperationBusMasterCommonBuffer
;
462 DirectionFlag
= EfiPciIoOperationBusMasterWrite
;
466 DirectionFlag
= EfiPciIoOperationBusMasterRead
;
472 // any non zero indicates error!
477 // find an unused map_list entry
479 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
480 if (snp
->map_list
[Index
].virt
== 0) {
485 if (Index
>= MAX_MAP_LENGTH
) {
486 DEBUG ((EFI_D_INFO
, "SNP maplist is FULL\n"));
491 snp
->map_list
[Index
].virt
= (EFI_PHYSICAL_ADDRESS
) CpuAddr
;
493 Status
= snp
->IoFncs
->Map (
496 (VOID
*) (UINTN
) CpuAddr
,
499 &(snp
->map_list
[Index
].map_cookie
)
501 if (Status
!= EFI_SUCCESS
) {
503 snp
->map_list
[Index
].virt
= 0;
510 snp_undi32_callback_unmap (
520 This is a callback routine supplied to UNDI at undi_start time.
521 UNDI call this routine when it wants to unmap an address that was previously
522 mapped using map callback
525 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
526 Undi interface context (Undi does not read or write this variable)
527 CpuAddr - Virtual address that was mapped!
528 NumBytes - size of memory mapped
529 Direction- direction of data flow for this memory's usage:
530 cpu->device, device->cpu or both ways
531 DeviceAddr - the mapped device address
540 snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
542 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
543 if (snp
->map_list
[Index
].virt
== CpuAddr
) {
548 if (Index
>= MAX_MAP_LENGTH
)
550 DEBUG ((EFI_D_ERROR
, "SNP could not find a mapping, failed to unmap.\n"));
554 snp
->IoFncs
->Unmap (snp
->IoFncs
, snp
->map_list
[Index
].map_cookie
);
555 snp
->map_list
[Index
].virt
= 0;
556 snp
->map_list
[Index
].map_cookie
= NULL
;
561 snp_undi32_callback_sync (
571 This is a callback routine supplied to UNDI at undi_start time.
572 UNDI call this routine when it wants synchronize the virtual buffer contents
573 with the mapped buffer contents. The virtual and mapped buffers need not
574 correspond to the same physical memory (especially if the virtual address is
575 > 4GB). Depending on the direction for which the buffer is mapped, undi will
576 need to synchronize their contents whenever it writes to/reads from the buffer
577 using either the cpu address or the device address.
579 EFI does not provide a sync call, since virt=physical, we sould just do
580 the synchronization ourself here!
583 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
584 Undi interface context (Undi does not read or write this variable)
585 CpuAddr - Virtual address that was mapped!
586 NumBytes - size of memory mapped
587 Direction- direction of data flow for this memory's usage:
588 cpu->device, device->cpu or both ways
589 DeviceAddr - the mapped device address
595 if ((CpuAddr
== 0) || (DeviceAddr
== 0) || (NumBytes
== 0)) {
602 CopyMem ((UINT8
*) (UINTN
) CpuAddr
, (UINT8
*) (UINTN
) DeviceAddr
, NumBytes
);
606 CopyMem ((UINT8
*) (UINTN
) DeviceAddr
, (UINT8
*) (UINTN
) CpuAddr
, NumBytes
);