2 This file contains two sets of callback routines for undi3.0 and undi3.1.
3 the callback routines for Undi3.1 have an extra parameter UniqueId which
4 stores the interface context for the NIC that snp is trying to talk.
6 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 // these 2 global variables are used only for 3.0 undi. we could not place
22 // them in the snp structure because we will not know which snp structure
23 // in the callback context!
25 BOOLEAN mInitializeLock
= TRUE
;
29 // End Global variables
31 extern EFI_PCI_IO_PROTOCOL
*mPciIo
;
34 Convert a virtual or CPU address provided by SNP to a physical or device
37 This is a callback routine supplied to UNDI at undi_start time. Since EFI uses
38 the identical mapping, this routine returns the physical address same as the
39 virtual address for most of the addresses. an address above 4GB cannot
40 generally be used as a device address, it needs to be mapped to a lower
41 physical address. This routine does not call the map routine itself, but it
42 assumes that the mapping was done at the time of providing the address to
43 UNDI. This routine just looks up the address in a map table (which is the v2p
46 @param CpuAddr Virtual address.
47 @param DeviceAddrPtr Pointer to the physical address, or 0 in case of any
53 SnpUndi32CallbackV2p30 (
55 IN OUT UINT64 DeviceAddrPtr
60 // Do nothing if virtual address is zero or physical pointer is NULL.
61 // No need to map if the virtual address is within 4GB limit since
62 // EFI uses identical mapping
64 if ((CpuAddr
== 0) || (DeviceAddrPtr
== 0)) {
65 DEBUG ((EFI_D_NET
, "\nv2p: Null virtual address or physical pointer.\n"));
69 if (CpuAddr
< FOUR_GIGABYTES
) {
70 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
74 // SNP creates a vaddr tp paddr mapping at the time of calling undi with any
75 // big address, this callback routine just looks up in the v2p list and
76 // returns the physical address for any given virtual address.
78 if (FindV2p (&V2p
, (VOID
*) (UINTN
) CpuAddr
) != EFI_SUCCESS
) {
79 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
81 *(UINT64
*) (UINTN
) DeviceAddrPtr
= V2p
->PhysicalAddress
;
86 Acquire or release a lock of an exclusive access to a critical section of the
89 This is a callback routine supplied to UNDI at undi_start time.
91 @param Enable Non-zero indicates acquire; Zero indicates release.
96 SnpUndi32CallbackBlock30 (
101 // tcpip was calling snp at tpl_notify and if we acquire a lock that was
102 // created at a lower level (TPL_CALLBACK) it gives an assert!
104 if (mInitializeLock
) {
105 EfiInitializeLock (&mLock
, TPL_NOTIFY
);
106 mInitializeLock
= FALSE
;
110 EfiAcquireLock (&mLock
);
112 EfiReleaseLock (&mLock
);
117 Delay MicroSeconds of micro seconds.
119 This is a callback routine supplied to UNDI at undi_start time.
121 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
126 SnpUndi32CallbackDelay30 (
127 IN UINT64 MicroSeconds
130 if (MicroSeconds
!= 0) {
131 gBS
->Stall ((UINTN
) MicroSeconds
);
138 This is a callback routine supplied to UNDI at undi_start time. This is not
139 currently being used by UNDI3.0 because Undi3.0 uses io/mem offsets relative
140 to the beginning of the device io/mem address and so it needs to use the
141 PCI_IO_FUNCTION that abstracts the start of the device's io/mem addresses.
142 Since SNP cannot retrive the context of the undi3.0 interface it cannot use
143 the PCI_IO_FUNCTION that specific for that NIC and uses one global IO
144 functions structure, this does not work. This however works fine for EFI1.0
145 Undis because they use absolute addresses for io/mem access.
147 @param ReadOrWrite Indicates read or write, IO or Memory.
148 @param NumBytes Number of bytes to read or write.
149 @param Address IO or memory address to read from or write to.
150 @param BufferAddr Memory location to read into or that contains the bytes to
156 SnpUndi32CallbackMemio30 (
157 IN UINT8 ReadOrWrite
,
160 IN OUT UINT64 BufferAddr
163 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
167 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
171 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
175 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
179 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
182 switch (ReadOrWrite
) {
187 1, // BAR 1, IO base address
190 (VOID
*) (UINTN
) BufferAddr
198 1, // BAR 1, IO base address
201 (VOID
*) (UINTN
) BufferAddr
209 0, // BAR 0, Memory base address
212 (VOID
*) (UINTN
) BufferAddr
220 0, // BAR 0, Memory base address
223 (VOID
*) (UINTN
) BufferAddr
232 Acquire or release a lock of the exclusive access to a critical section of the
235 This is a callback routine supplied to UNDI3.1 at undi_start time.
236 New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1
237 because undi3.1 uses the MemMap call to map the required address by itself!
239 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
240 store Undi interface context (Undi does not read or write
242 @param Enable Non-zero indicates acquire; Zero indicates release.
247 SnpUndi32CallbackBlock (
254 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
256 // tcpip was calling snp at tpl_notify and when we acquire a lock that was
257 // created at a lower level (TPL_CALLBACK) it gives an assert!
260 EfiAcquireLock (&Snp
->Lock
);
262 EfiReleaseLock (&Snp
->Lock
);
267 Delay MicroSeconds of micro seconds.
269 This is a callback routine supplied to UNDI at undi_start time.
271 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
272 store Undi interface context (Undi does not read or write
274 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
279 SnpUndi32CallbackDelay (
281 IN UINT64 MicroSeconds
284 if (MicroSeconds
!= 0) {
285 gBS
->Stall ((UINTN
) MicroSeconds
);
290 IO routine for UNDI3.1.
292 This is a callback routine supplied to UNDI at undi_start time.
294 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this
295 to store Undi interface context (Undi does not read or
296 write this variable).
297 @param ReadOrWrite Indicates read or write, IO or Memory.
298 @param NumBytes Number of bytes to read or write.
299 @param MemOrPortAddr IO or memory address to read from or write to.
300 @param BufferPtr Memory location to read into or that contains the bytes
306 SnpUndi32CallbackMemio (
308 IN UINT8 ReadOrWrite
,
310 IN UINT64 MemOrPortAddr
,
311 IN OUT UINT64 BufferPtr
315 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
317 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
319 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
322 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
326 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
330 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
334 switch (ReadOrWrite
) {
336 Snp
->PciIo
->Io
.Read (
339 Snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
342 (VOID
*) (UINTN
) BufferPtr
347 Snp
->PciIo
->Io
.Write (
350 Snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
353 (VOID
*) (UINTN
) BufferPtr
358 Snp
->PciIo
->Mem
.Read (
361 Snp
->MemoryBarIndex
, // BAR 0, Memory base address
364 (VOID
*) (UINTN
) BufferPtr
369 Snp
->PciIo
->Mem
.Write (
372 Snp
->MemoryBarIndex
, // BAR 0, Memory base address
375 (VOID
*) (UINTN
) BufferPtr
384 Map a CPU address to a device address.
386 This is a callback routine supplied to UNDI at undi_start time.
388 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
389 store Undi interface context (Undi does not read or write
391 @param CpuAddr Virtual address to be mapped.
392 @param NumBytes Size of memory to be mapped.
393 @param Direction Direction of data flow for this memory's usage:
394 cpu->device, device->cpu or both ways.
395 @param DeviceAddrPtr Pointer to return the mapped device address.
400 SnpUndi32CallbackMap (
405 IN OUT UINT64 DeviceAddrPtr
408 EFI_PHYSICAL_ADDRESS
*DevAddrPtr
;
409 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag
;
415 BuffSize
= (UINTN
) NumBytes
;
416 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
417 DevAddrPtr
= (EFI_PHYSICAL_ADDRESS
*) (UINTN
) DeviceAddrPtr
;
425 case TO_AND_FROM_DEVICE
:
426 DirectionFlag
= EfiPciIoOperationBusMasterCommonBuffer
;
430 DirectionFlag
= EfiPciIoOperationBusMasterWrite
;
434 DirectionFlag
= EfiPciIoOperationBusMasterRead
;
440 // any non zero indicates error!
445 // find an unused map_list entry
447 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
448 if (Snp
->MapList
[Index
].VirtualAddress
== 0) {
453 if (Index
>= MAX_MAP_LENGTH
) {
454 DEBUG ((EFI_D_INFO
, "SNP maplist is FULL\n"));
459 Snp
->MapList
[Index
].VirtualAddress
= (EFI_PHYSICAL_ADDRESS
) CpuAddr
;
461 Status
= Snp
->PciIo
->Map (
464 (VOID
*) (UINTN
) CpuAddr
,
467 &(Snp
->MapList
[Index
].MapCookie
)
469 if (Status
!= EFI_SUCCESS
) {
471 Snp
->MapList
[Index
].VirtualAddress
= 0;
478 Unmap an address that was previously mapped using map callback.
480 This is a callback routine supplied to UNDI at undi_start time.
482 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
483 store. Undi interface context (Undi does not read or write
485 @param CpuAddr Virtual address that was mapped.
486 @param NumBytes Size of memory mapped.
487 @param Direction Direction of data flow for this memory's usage:
488 cpu->device, device->cpu or both ways.
489 @param DeviceAddr The mapped device address.
494 SnpUndi32CallbackUnmap (
505 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
507 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
508 if (Snp
->MapList
[Index
].VirtualAddress
== CpuAddr
) {
513 if (Index
>= MAX_MAP_LENGTH
) {
514 DEBUG ((EFI_D_ERROR
, "SNP could not find a mapping, failed to unmap.\n"));
518 Snp
->PciIo
->Unmap (Snp
->PciIo
, Snp
->MapList
[Index
].MapCookie
);
519 Snp
->MapList
[Index
].VirtualAddress
= 0;
520 Snp
->MapList
[Index
].MapCookie
= NULL
;
525 Synchronize the virtual buffer contents with the mapped buffer contents.
527 This is a callback routine supplied to UNDI at undi_start time. The virtual
528 and mapped buffers need not correspond to the same physical memory (especially
529 if the virtual address is > 4GB). Depending on the direction for which the
530 buffer is mapped, undi will need to synchronize their contents whenever it
531 writes to/reads from the buffer using either the cpu address or the device
533 EFI does not provide a sync call since virt=physical, we should just do the
534 synchronization ourselves here.
536 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
537 store Undi interface context (Undi does not read or write
539 @param CpuAddr Virtual address that was mapped.
540 @param NumBytes Size of memory mapped.
541 @param Direction Direction of data flow for this memory's usage:
542 cpu->device, device->cpu or both ways.
543 @param DeviceAddr The mapped device address.
548 SnpUndi32CallbackSync (
556 if ((CpuAddr
== 0) || (DeviceAddr
== 0) || (NumBytes
== 0)) {
563 CopyMem ((UINT8
*) (UINTN
) CpuAddr
, (UINT8
*) (UINTN
) DeviceAddr
, NumBytes
);
567 CopyMem ((UINT8
*) (UINTN
) DeviceAddr
, (UINT8
*) (UINTN
) CpuAddr
, NumBytes
);