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 - 2008, 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
52 SnpUndi32CallbackV2p30 (
54 IN OUT UINT64 DeviceAddrPtr
59 // Do nothing if virtual address is zero or physical pointer is NULL.
60 // No need to map if the virtual address is within 4GB limit since
61 // EFI uses identical mapping
63 if ((CpuAddr
== 0) || (DeviceAddrPtr
== 0)) {
64 DEBUG ((EFI_D_NET
, "\nv2p: Null virtual address or physical pointer.\n"));
68 if (CpuAddr
< FOUR_GIGABYTES
) {
69 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
73 // SNP creates a vaddr tp paddr mapping at the time of calling undi with any
74 // big address, this callback routine just looks up in the v2p list and
75 // returns the physical address for any given virtual address.
77 if (FindV2p (&V2p
, (VOID
*) (UINTN
) CpuAddr
) != EFI_SUCCESS
) {
78 *(UINT64
*) (UINTN
) DeviceAddrPtr
= CpuAddr
;
80 *(UINT64
*) (UINTN
) DeviceAddrPtr
= V2p
->PhysicalAddress
;
85 Acquire or release a lock of an exclusive access to a critical section of the
88 This is a callback routine supplied to UNDI at undi_start time.
90 @param Enable Non-zero indicates acquire; Zero indicates release.
94 SnpUndi32CallbackBlock30 (
99 // tcpip was calling snp at tpl_notify and if we acquire a lock that was
100 // created at a lower level (TPL_CALLBACK) it gives an assert!
102 if (mInitializeLock
) {
103 EfiInitializeLock (&mLock
, TPL_NOTIFY
);
104 mInitializeLock
= FALSE
;
108 EfiAcquireLock (&mLock
);
110 EfiReleaseLock (&mLock
);
115 Delay MicroSeconds of micro seconds.
117 This is a callback routine supplied to UNDI at undi_start time.
119 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
123 SnpUndi32CallbackDelay30 (
124 IN UINT64 MicroSeconds
127 if (MicroSeconds
!= 0) {
128 gBS
->Stall ((UINTN
) MicroSeconds
);
135 This is a callback routine supplied to UNDI at undi_start time. This is not
136 currently being used by UNDI3.0 because Undi3.0 uses io/mem offsets relative
137 to the beginning of the device io/mem address and so it needs to use the
138 PCI_IO_FUNCTION that abstracts the start of the device's io/mem addresses.
139 Since SNP cannot retrive the context of the undi3.0 interface it cannot use
140 the PCI_IO_FUNCTION that specific for that NIC and uses one global IO
141 functions structure, this does not work. This however works fine for EFI1.0
142 Undis because they use absolute addresses for io/mem access.
144 @param ReadOrWrite Indicates read or write, IO or Memory.
145 @param NumBytes Number of bytes to read or write.
146 @param Address IO or memory address to read from or write to.
147 @param BufferAddr Memory location to read into or that contains the bytes to
152 SnpUndi32CallbackMemio30 (
153 IN UINT8 ReadOrWrite
,
156 IN OUT UINT64 BufferAddr
159 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
163 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
167 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
171 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
175 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
178 switch (ReadOrWrite
) {
183 1, // BAR 1, IO base address
186 (VOID
*) (UINTN
) BufferAddr
194 1, // BAR 1, IO base address
197 (VOID
*) (UINTN
) BufferAddr
205 0, // BAR 0, Memory base address
208 (VOID
*) (UINTN
) BufferAddr
216 0, // BAR 0, Memory base address
219 (VOID
*) (UINTN
) BufferAddr
228 Acquire or release a lock of the exclusive access to a critical section of the
231 This is a callback routine supplied to UNDI3.1 at undi_start time.
232 New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1
233 because undi3.1 uses the MemMap call to map the required address by itself!
235 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
236 store Undi interface context (Undi does not read or write
238 @param Enable Non-zero indicates acquire; Zero indicates release.
242 SnpUndi32CallbackBlock (
249 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
251 // tcpip was calling snp at tpl_notify and when we acquire a lock that was
252 // created at a lower level (TPL_CALLBACK) it gives an assert!
255 EfiAcquireLock (&Snp
->Lock
);
257 EfiReleaseLock (&Snp
->Lock
);
262 Delay MicroSeconds of micro seconds.
264 This is a callback routine supplied to UNDI at undi_start time.
266 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
267 store Undi interface context (Undi does not read or write
269 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
273 SnpUndi32CallbackDelay (
275 IN UINT64 MicroSeconds
278 if (MicroSeconds
!= 0) {
279 gBS
->Stall ((UINTN
) MicroSeconds
);
284 IO routine for UNDI3.1.
286 This is a callback routine supplied to UNDI at undi_start time.
288 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this
289 to store Undi interface context (Undi does not read or
290 write this variable).
291 @param ReadOrWrite Indicates read or write, IO or Memory.
292 @param NumBytes Number of bytes to read or write.
293 @param MemOrPortAddr IO or memory address to read from or write to.
294 @param BufferPtr Memory location to read into or that contains the bytes
299 SnpUndi32CallbackMemio (
301 IN UINT8 ReadOrWrite
,
303 IN UINT64 MemOrPortAddr
,
304 IN OUT UINT64 BufferPtr
308 EFI_PCI_IO_PROTOCOL_WIDTH Width
;
310 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
312 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 0;
315 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 1;
319 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 2;
323 Width
= (EFI_PCI_IO_PROTOCOL_WIDTH
) 3;
327 switch (ReadOrWrite
) {
329 Snp
->PciIo
->Io
.Read (
332 Snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
335 (VOID
*) (UINTN
) BufferPtr
340 Snp
->PciIo
->Io
.Write (
343 Snp
->IoBarIndex
, // BAR 1 (for 32bit regs), IO base address
346 (VOID
*) (UINTN
) BufferPtr
351 Snp
->PciIo
->Mem
.Read (
354 Snp
->MemoryBarIndex
, // BAR 0, Memory base address
357 (VOID
*) (UINTN
) BufferPtr
362 Snp
->PciIo
->Mem
.Write (
365 Snp
->MemoryBarIndex
, // BAR 0, Memory base address
368 (VOID
*) (UINTN
) BufferPtr
377 Map a CPU address to a device address.
379 This is a callback routine supplied to UNDI at undi_start time.
381 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
382 store Undi interface context (Undi does not read or write
384 @param CpuAddr Virtual address to be mapped.
385 @param NumBytes Size of memory to be mapped.
386 @param Direction Direction of data flow for this memory's usage:
387 cpu->device, device->cpu or both ways.
388 @param DeviceAddrPtr Pointer to return the mapped device address.
392 SnpUndi32CallbackMap (
397 IN OUT UINT64 DeviceAddrPtr
400 EFI_PHYSICAL_ADDRESS
*DevAddrPtr
;
401 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag
;
407 BuffSize
= (UINTN
) NumBytes
;
408 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
409 DevAddrPtr
= (EFI_PHYSICAL_ADDRESS
*) (UINTN
) DeviceAddrPtr
;
417 case TO_AND_FROM_DEVICE
:
418 DirectionFlag
= EfiPciIoOperationBusMasterCommonBuffer
;
422 DirectionFlag
= EfiPciIoOperationBusMasterWrite
;
426 DirectionFlag
= EfiPciIoOperationBusMasterRead
;
432 // any non zero indicates error!
437 // find an unused map_list entry
439 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
440 if (Snp
->MapList
[Index
].VirtualAddress
== 0) {
445 if (Index
>= MAX_MAP_LENGTH
) {
446 DEBUG ((EFI_D_INFO
, "SNP maplist is FULL\n"));
451 Snp
->MapList
[Index
].VirtualAddress
= (EFI_PHYSICAL_ADDRESS
) CpuAddr
;
453 Status
= Snp
->PciIo
->Map (
456 (VOID
*) (UINTN
) CpuAddr
,
459 &(Snp
->MapList
[Index
].MapCookie
)
461 if (Status
!= EFI_SUCCESS
) {
463 Snp
->MapList
[Index
].VirtualAddress
= 0;
470 Unmap an address that was previously mapped using map callback.
472 This is a callback routine supplied to UNDI at undi_start time.
474 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
475 store. Undi interface context (Undi does not read or write
477 @param CpuAddr Virtual address that was mapped.
478 @param NumBytes Size of memory mapped.
479 @param Direction Direction of data flow for this memory's usage:
480 cpu->device, device->cpu or both ways.
481 @param DeviceAddr The mapped device address.
485 SnpUndi32CallbackUnmap (
496 Snp
= (SNP_DRIVER
*) (UINTN
) UniqueId
;
498 for (Index
= 0; Index
< MAX_MAP_LENGTH
; Index
++) {
499 if (Snp
->MapList
[Index
].VirtualAddress
== CpuAddr
) {
504 if (Index
>= MAX_MAP_LENGTH
) {
505 DEBUG ((EFI_D_ERROR
, "SNP could not find a mapping, failed to unmap.\n"));
509 Snp
->PciIo
->Unmap (Snp
->PciIo
, Snp
->MapList
[Index
].MapCookie
);
510 Snp
->MapList
[Index
].VirtualAddress
= 0;
511 Snp
->MapList
[Index
].MapCookie
= NULL
;
516 Synchronize the virtual buffer contents with the mapped buffer contents.
518 This is a callback routine supplied to UNDI at undi_start time. The virtual
519 and mapped buffers need not correspond to the same physical memory (especially
520 if the virtual address is > 4GB). Depending on the direction for which the
521 buffer is mapped, undi will need to synchronize their contents whenever it
522 writes to/reads from the buffer using either the cpu address or the device
524 EFI does not provide a sync call since virt=physical, we should just do the
525 synchronization ourselves here.
527 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
528 store Undi interface context (Undi does not read or write
530 @param CpuAddr Virtual address that was mapped.
531 @param NumBytes Size of memory mapped.
532 @param Direction Direction of data flow for this memory's usage:
533 cpu->device, device->cpu or both ways.
534 @param DeviceAddr The mapped device address.
538 SnpUndi32CallbackSync (
546 if ((CpuAddr
== 0) || (DeviceAddr
== 0) || (NumBytes
== 0)) {
553 CopyMem ((UINT8
*) (UINTN
) CpuAddr
, (UINT8
*) (UINTN
) DeviceAddr
, NumBytes
);
557 CopyMem ((UINT8
*) (UINTN
) DeviceAddr
, (UINT8
*) (UINTN
) CpuAddr
, NumBytes
);