--- /dev/null
+/*++\r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module name:\r
+ callback.c\r
+\r
+Abstract:\r
+ This file contains two sets of callback routines for undi3.0 and undi3.1.\r
+ the callback routines for Undi3.1 have an extra parameter UniqueId which\r
+ stores the interface context for the NIC that snp is trying to talk..\r
+\r
+--*/\r
+\r
+\r
+#include "Snp.h"\r
+\r
+//\r
+// Global variables\r
+// these 2 global variables are used only for 3.0 undi. we could not place\r
+// them in the snp structure because we will not know which snp structure\r
+// in the callback context!\r
+//\r
+STATIC BOOLEAN mInitializeLock = TRUE;\r
+STATIC EFI_LOCK mLock;\r
+\r
+//\r
+// End Global variables\r
+//\r
+extern EFI_PCI_IO_PROTOCOL *mPciIoFncs;\r
+\r
+VOID\r
+snp_undi32_callback_v2p_30 (\r
+ IN UINT64 CpuAddr,\r
+ IN OUT UINT64 DeviceAddrPtr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine with a virtual or CPU address that SNP provided\r
+ to convert it to a physical or device address. Since EFI uses the identical\r
+ mapping, this routine returns the physical address same as the virtual address\r
+ for most of the addresses. an address above 4GB cannot generally be used as a\r
+ device address, it needs to be mapped to a lower physical address. This routine\r
+ does not call the map routine itself, but it assumes that the mapping was done\r
+ at the time of providing the address to UNDI. This routine just looks up the\r
+ address in a map table (which is the v2p structure chain)\r
+\r
+Arguments:\r
+ CpuAddr - virtual address of a buffer\r
+ DeviceAddrPtr - pointer to the physical address\r
+\r
+Returns:\r
+ void - The DeviceAddrPtr will contain 0 in case of any error\r
+\r
+--*/\r
+{\r
+ struct s_v2p *v2p;\r
+ //\r
+ // Do nothing if virtual address is zero or physical pointer is NULL.\r
+ // No need to map if the virtual address is within 4GB limit since\r
+ // EFI uses identical mapping\r
+ //\r
+ if ((CpuAddr == 0) || (DeviceAddrPtr == 0)) {\r
+ DEBUG ((EFI_D_ERROR, "\nv2p: Null virtual address or physical pointer.\n"));\r
+ return ;\r
+ }\r
+\r
+ if (CpuAddr < FOUR_GIGABYTES) {\r
+ *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;\r
+ return ;\r
+ }\r
+ //\r
+ // SNP creates a vaddr tp paddr mapping at the time of calling undi with any\r
+ // big address, this callback routine just looks up in the v2p list and\r
+ // returns the physical address for any given virtual address.\r
+ //\r
+ if (find_v2p (&v2p, (VOID *) (UINTN) CpuAddr) != EFI_SUCCESS) {\r
+ *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;\r
+ } else {\r
+ *(UINT64 *) (UINTN) DeviceAddrPtr = v2p->paddr;\r
+ }\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_block_30 (\r
+ IN UINT32 Enable\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine when it wants to have exclusive access to a critical\r
+ section of the code/data\r
+\r
+Arguments:\r
+ Enable - non-zero indicates acquire\r
+ zero indicates release\r
+\r
+Returns:\r
+ void\r
+--*/\r
+{\r
+ //\r
+ // tcpip was calling snp at tpl_notify and if we acquire a lock that was\r
+ // created at a lower level (TPL_CALLBACK) it gives an assert!\r
+ //\r
+ if (mInitializeLock) {\r
+ EfiInitializeLock (&mLock, TPL_NOTIFY);\r
+ mInitializeLock = FALSE;\r
+ }\r
+\r
+ if (Enable != 0) {\r
+ EfiAcquireLock (&mLock);\r
+ } else {\r
+ EfiReleaseLock (&mLock);\r
+ }\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_delay_30 (\r
+ IN UINT64 MicroSeconds\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine with the number of micro seconds when it wants to\r
+ pause.\r
+\r
+Arguments:\r
+ MicroSeconds - number of micro seconds to pause, ususlly multiple of 10\r
+\r
+Returns:\r
+ void\r
+--*/\r
+{\r
+ if (MicroSeconds != 0) {\r
+ gBS->Stall ((UINTN) MicroSeconds);\r
+ }\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_memio_30 (\r
+ IN UINT8 ReadOrWrite,\r
+ IN UINT8 NumBytes,\r
+ IN UINT64 Address,\r
+ IN OUT UINT64 BufferAddr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ This is the IO routine for UNDI. This is not currently being used by UNDI3.0\r
+ because Undi3.0 uses io/mem offsets relative to the beginning of the device\r
+ io/mem address and so it needs to use the PCI_IO_FUNCTION that abstracts the\r
+ start of the device's io/mem addresses. Since SNP cannot retrive the context\r
+ of the undi3.0 interface it cannot use the PCI_IO_FUNCTION that specific for\r
+ that NIC and uses one global IO functions structure, this does not work.\r
+ This however works fine for EFI1.0 Undis because they use absolute addresses\r
+ for io/mem access.\r
+\r
+Arguments:\r
+ ReadOrWrite - indicates read or write, IO or Memory\r
+ NumBytes - number of bytes to read or write\r
+ Address - IO or memory address to read from or write to\r
+ BufferAddr - memory location to read into or that contains the bytes\r
+ to write\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ EFI_PCI_IO_PROTOCOL_WIDTH Width;\r
+\r
+ switch (NumBytes) {\r
+ case 2:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;\r
+ break;\r
+\r
+ case 4:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;\r
+ break;\r
+\r
+ case 8:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;\r
+ break;\r
+\r
+ default:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;\r
+ }\r
+\r
+ switch (ReadOrWrite) {\r
+ case PXE_IO_READ:\r
+ mPciIoFncs->Io.Read (\r
+ mPciIoFncs,\r
+ Width,\r
+ 1, // BAR 1, IO base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_IO_WRITE:\r
+ mPciIoFncs->Io.Write (\r
+ mPciIoFncs,\r
+ Width,\r
+ 1, // BAR 1, IO base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_MEM_READ:\r
+ mPciIoFncs->Mem.Read (\r
+ mPciIoFncs,\r
+ Width,\r
+ 0, // BAR 0, Memory base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_MEM_WRITE:\r
+ mPciIoFncs->Mem.Write (\r
+ mPciIoFncs,\r
+ Width,\r
+ 0, // BAR 0, Memory base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+ }\r
+\r
+ return ;\r
+}\r
+//\r
+// New callbacks for 3.1:\r
+// there won't be a virtual2physical callback for UNDI 3.1 because undi3.1 uses\r
+// the MemMap call to map the required address by itself!\r
+//\r
+VOID\r
+snp_undi32_callback_block (\r
+ IN UINT64 UniqueId,\r
+ IN UINT32 Enable\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI3.1 at undi_start time.\r
+ UNDI call this routine when it wants to have exclusive access to a critical\r
+ section of the code/data\r
+\r
+Arguments:\r
+ UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store\r
+ Undi interface context (Undi does not read or write this variable)\r
+ Enable - non-zero indicates acquire\r
+ zero indicates release\r
+\r
+Returns:\r
+ void\r
+\r
+--*/\r
+{\r
+ SNP_DRIVER *snp;\r
+\r
+ snp = (SNP_DRIVER *) (UINTN) UniqueId;\r
+ //\r
+ // tcpip was calling snp at tpl_notify and when we acquire a lock that was\r
+ // created at a lower level (TPL_CALLBACK) it gives an assert!\r
+ //\r
+ if (Enable != 0) {\r
+ EfiAcquireLock (&snp->lock);\r
+ } else {\r
+ EfiReleaseLock (&snp->lock);\r
+ }\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_delay (\r
+ IN UINT64 UniqueId,\r
+ IN UINT64 MicroSeconds\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine with the number of micro seconds when it wants to\r
+ pause.\r
+\r
+Arguments:\r
+ MicroSeconds - number of micro seconds to pause, ususlly multiple of 10\r
+\r
+Returns:\r
+ void\r
+--*/\r
+{\r
+ if (MicroSeconds != 0) {\r
+ gBS->Stall ((UINTN) MicroSeconds);\r
+ }\r
+}\r
+\r
+/*\r
+ * IO routine for UNDI start CPB.\r
+ */\r
+VOID\r
+snp_undi32_callback_memio (\r
+ UINT64 UniqueId,\r
+ UINT8 ReadOrWrite,\r
+ UINT8 NumBytes,\r
+ UINT64 Address,\r
+ UINT64 BufferAddr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ This is the IO routine for UNDI3.1.\r
+\r
+Arguments:\r
+ ReadOrWrite - indicates read or write, IO or Memory\r
+ NumBytes - number of bytes to read or write\r
+ Address - IO or memory address to read from or write to\r
+ BufferAddr - memory location to read into or that contains the bytes\r
+ to write\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ SNP_DRIVER *snp;\r
+ EFI_PCI_IO_PROTOCOL_WIDTH Width;\r
+\r
+ snp = (SNP_DRIVER *) (UINTN) UniqueId;\r
+\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;\r
+ switch (NumBytes) {\r
+ case 2:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;\r
+ break;\r
+\r
+ case 4:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;\r
+ break;\r
+\r
+ case 8:\r
+ Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;\r
+ break;\r
+ }\r
+\r
+ switch (ReadOrWrite) {\r
+ case PXE_IO_READ:\r
+ snp->IoFncs->Io.Read (\r
+ snp->IoFncs,\r
+ Width,\r
+ snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_IO_WRITE:\r
+ snp->IoFncs->Io.Write (\r
+ snp->IoFncs,\r
+ Width,\r
+ snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_MEM_READ:\r
+ snp->IoFncs->Mem.Read (\r
+ snp->IoFncs,\r
+ Width,\r
+ snp->MemoryBarIndex, // BAR 0, Memory base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+\r
+ case PXE_MEM_WRITE:\r
+ snp->IoFncs->Mem.Write (\r
+ snp->IoFncs,\r
+ Width,\r
+ snp->MemoryBarIndex, // BAR 0, Memory base address\r
+ Address,\r
+ 1, // count\r
+ (VOID *) (UINTN) BufferAddr\r
+ );\r
+ break;\r
+ }\r
+\r
+ return ;\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_map (\r
+ IN UINT64 UniqueId,\r
+ IN UINT64 CpuAddr,\r
+ IN UINT32 NumBytes,\r
+ IN UINT32 Direction,\r
+ IN OUT UINT64 DeviceAddrPtr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine when it has to map a CPU address to a device\r
+ address.\r
+\r
+Arguments:\r
+ UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store\r
+ Undi interface context (Undi does not read or write this variable)\r
+ CpuAddr - Virtual address to be mapped!\r
+ NumBytes - size of memory to be mapped\r
+ Direction - direction of data flow for this memory's usage:\r
+ cpu->device, device->cpu or both ways\r
+ DeviceAddrPtr - pointer to return the mapped device address\r
+\r
+Returns:\r
+ None\r
+\r
+--*/\r
+{\r
+ EFI_PHYSICAL_ADDRESS *DevAddrPtr;\r
+ EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;\r
+ UINTN BuffSize;\r
+ SNP_DRIVER *snp;\r
+ UINTN Index;\r
+ EFI_STATUS Status;\r
+\r
+ BuffSize = (UINTN) NumBytes;\r
+ snp = (SNP_DRIVER *) (UINTN) UniqueId;\r
+ DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;\r
+\r
+ if (CpuAddr == 0) {\r
+ *DevAddrPtr = 0;\r
+ return ;\r
+ }\r
+\r
+ switch (Direction) {\r
+ case TO_AND_FROM_DEVICE:\r
+ DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;\r
+ break;\r
+\r
+ case FROM_DEVICE:\r
+ DirectionFlag = EfiPciIoOperationBusMasterWrite;\r
+ break;\r
+\r
+ case TO_DEVICE:\r
+ DirectionFlag = EfiPciIoOperationBusMasterRead;\r
+ break;\r
+\r
+ default:\r
+ *DevAddrPtr = 0;\r
+ //\r
+ // any non zero indicates error!\r
+ //\r
+ return ;\r
+ }\r
+ //\r
+ // find an unused map_list entry\r
+ //\r
+ for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {\r
+ if (snp->map_list[Index].virt == 0) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index >= MAX_MAP_LENGTH) {\r
+ DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n"));\r
+ *DevAddrPtr = 0;\r
+ return ;\r
+ }\r
+\r
+ snp->map_list[Index].virt = (EFI_PHYSICAL_ADDRESS) CpuAddr;\r
+\r
+ Status = snp->IoFncs->Map (\r
+ snp->IoFncs,\r
+ DirectionFlag,\r
+ (VOID *) (UINTN) CpuAddr,\r
+ &BuffSize,\r
+ DevAddrPtr,\r
+ &(snp->map_list[Index].map_cookie)\r
+ );\r
+ if (Status != EFI_SUCCESS) {\r
+ *DevAddrPtr = 0;\r
+ snp->map_list[Index].virt = 0;\r
+ }\r
+\r
+ return ;\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_unmap (\r
+ IN UINT64 UniqueId,\r
+ IN UINT64 CpuAddr,\r
+ IN UINT32 NumBytes,\r
+ IN UINT32 Direction,\r
+ IN UINT64 DeviceAddr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine when it wants to unmap an address that was previously\r
+ mapped using map callback\r
+\r
+Arguments:\r
+ UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store\r
+ Undi interface context (Undi does not read or write this variable)\r
+ CpuAddr - Virtual address that was mapped!\r
+ NumBytes - size of memory mapped\r
+ Direction- direction of data flow for this memory's usage:\r
+ cpu->device, device->cpu or both ways\r
+ DeviceAddr - the mapped device address\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ SNP_DRIVER *snp;\r
+ UINT16 Index;\r
+\r
+ snp = (SNP_DRIVER *) (UINTN) UniqueId;\r
+\r
+ for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {\r
+ if (snp->map_list[Index].virt == CpuAddr) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index >= MAX_MAP_LENGTH)\r
+ {\r
+ DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));\r
+ return ;\r
+ }\r
+\r
+ snp->IoFncs->Unmap (snp->IoFncs, snp->map_list[Index].map_cookie);\r
+ snp->map_list[Index].virt = 0;\r
+ snp->map_list[Index].map_cookie = NULL;\r
+ return ;\r
+}\r
+\r
+VOID\r
+snp_undi32_callback_sync (\r
+ UINT64 UniqueId,\r
+ UINT64 CpuAddr,\r
+ UINT32 NumBytes,\r
+ UINT32 Direction,\r
+ UINT64 DeviceAddr\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This is a callback routine supplied to UNDI at undi_start time.\r
+ UNDI call this routine when it wants synchronize the virtual buffer contents\r
+ with the mapped buffer contents. The virtual and mapped buffers need not\r
+ correspond to the same physical memory (especially if the virtual address is\r
+ > 4GB). Depending on the direction for which the buffer is mapped, undi will\r
+ need to synchronize their contents whenever it writes to/reads from the buffer\r
+ using either the cpu address or the device address.\r
+\r
+ EFI does not provide a sync call, since virt=physical, we sould just do\r
+ the synchronization ourself here!\r
+\r
+Arguments:\r
+ UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store\r
+ Undi interface context (Undi does not read or write this variable)\r
+ CpuAddr - Virtual address that was mapped!\r
+ NumBytes - size of memory mapped\r
+ Direction- direction of data flow for this memory's usage:\r
+ cpu->device, device->cpu or both ways\r
+ DeviceAddr - the mapped device address\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {\r
+ return ;\r
+\r
+ }\r
+\r
+ switch (Direction) {\r
+ case FROM_DEVICE:\r
+ CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);\r
+ break;\r
+\r
+ case TO_DEVICE:\r
+ CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);\r
+ break;\r
+ }\r
+\r
+ return ;\r
+}\r