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