]>
Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
c4a7d208 | 2 | This file contains the callback routines for undi3.1.\r |
fb0b259e | 3 | the callback routines for Undi3.1 have an extra parameter UniqueId which\r |
4 | stores the interface context for the NIC that snp is trying to talk.\r | |
5 | \r | |
c4a7d208 | 6 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 7 | This program and the accompanying materials\r |
5e7e4fad | 8 | are licensed and made available under the terms and conditions of the BSD License\r |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
fb0b259e | 15 | **/\r |
5e7e4fad | 16 | \r |
17 | #include "Snp.h"\r | |
18 | \r | |
4cda7726 | 19 | /**\r |
9c9f5859 | 20 | Acquire or release a lock of the exclusive access to a critical section of the\r |
21 | code/data.\r | |
22 | \r | |
4cda7726 | 23 | This is a callback routine supplied to UNDI3.1 at undi_start time.\r |
f3816027 | 24 | New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1\r |
9c9f5859 | 25 | because undi3.1 uses the MemMap call to map the required address by itself!\r |
4cda7726 | 26 | \r |
9c9f5859 | 27 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r |
f3816027 | 28 | store Undi interface context (Undi does not read or write\r |
29 | this variable).\r | |
9c9f5859 | 30 | @param Enable Non-zero indicates acquire; Zero indicates release.\r |
f3816027 | 31 | \r |
4cda7726 | 32 | **/\r |
5e7e4fad | 33 | VOID\r |
9c9f5859 | 34 | EFIAPI\r |
4cda7726 | 35 | SnpUndi32CallbackBlock (\r |
5e7e4fad | 36 | IN UINT64 UniqueId,\r |
37 | IN UINT32 Enable\r | |
38 | )\r | |
5e7e4fad | 39 | {\r |
4cda7726 | 40 | SNP_DRIVER *Snp;\r |
5e7e4fad | 41 | \r |
4cda7726 | 42 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r |
5e7e4fad | 43 | //\r |
44 | // tcpip was calling snp at tpl_notify and when we acquire a lock that was\r | |
45 | // created at a lower level (TPL_CALLBACK) it gives an assert!\r | |
46 | //\r | |
47 | if (Enable != 0) {\r | |
4cda7726 | 48 | EfiAcquireLock (&Snp->Lock);\r |
5e7e4fad | 49 | } else {\r |
4cda7726 | 50 | EfiReleaseLock (&Snp->Lock);\r |
5e7e4fad | 51 | }\r |
52 | }\r | |
53 | \r | |
4cda7726 | 54 | /**\r |
f3816027 | 55 | Delay MicroSeconds of micro seconds.\r |
9c9f5859 | 56 | \r |
4cda7726 | 57 | This is a callback routine supplied to UNDI at undi_start time.\r |
4cda7726 | 58 | \r |
59 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r | |
f3816027 | 60 | store Undi interface context (Undi does not read or write\r |
61 | this variable).\r | |
9c9f5859 | 62 | @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.\r |
f3816027 | 63 | \r |
4cda7726 | 64 | **/\r |
5e7e4fad | 65 | VOID\r |
9c9f5859 | 66 | EFIAPI\r |
4cda7726 | 67 | SnpUndi32CallbackDelay (\r |
5e7e4fad | 68 | IN UINT64 UniqueId,\r |
69 | IN UINT64 MicroSeconds\r | |
70 | )\r | |
5e7e4fad | 71 | {\r |
72 | if (MicroSeconds != 0) {\r | |
73 | gBS->Stall ((UINTN) MicroSeconds);\r | |
74 | }\r | |
75 | }\r | |
76 | \r | |
4cda7726 | 77 | /**\r |
9c9f5859 | 78 | IO routine for UNDI3.1.\r |
79 | \r | |
4cda7726 | 80 | This is a callback routine supplied to UNDI at undi_start time.\r |
9c9f5859 | 81 | \r |
82 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this\r | |
83 | to store Undi interface context (Undi does not read or\r | |
84 | write this variable).\r | |
f3816027 | 85 | @param ReadOrWrite Indicates read or write, IO or Memory.\r |
86 | @param NumBytes Number of bytes to read or write.\r | |
4cda7726 | 87 | @param MemOrPortAddr IO or memory address to read from or write to.\r |
f3816027 | 88 | @param BufferPtr Memory location to read into or that contains the bytes\r |
9c9f5859 | 89 | to write.\r |
f3816027 | 90 | \r |
4cda7726 | 91 | **/\r |
5e7e4fad | 92 | VOID\r |
9c9f5859 | 93 | EFIAPI\r |
4cda7726 | 94 | SnpUndi32CallbackMemio (\r |
95 | IN UINT64 UniqueId,\r | |
96 | IN UINT8 ReadOrWrite,\r | |
97 | IN UINT8 NumBytes,\r | |
98 | IN UINT64 MemOrPortAddr,\r | |
99 | IN OUT UINT64 BufferPtr\r | |
5e7e4fad | 100 | )\r |
5e7e4fad | 101 | {\r |
4cda7726 | 102 | SNP_DRIVER *Snp;\r |
5e7e4fad | 103 | EFI_PCI_IO_PROTOCOL_WIDTH Width;\r |
104 | \r | |
4cda7726 | 105 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r |
5e7e4fad | 106 | \r |
107 | Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;\r | |
108 | switch (NumBytes) {\r | |
109 | case 2:\r | |
110 | Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;\r | |
111 | break;\r | |
112 | \r | |
113 | case 4:\r | |
114 | Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;\r | |
115 | break;\r | |
116 | \r | |
117 | case 8:\r | |
118 | Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;\r | |
119 | break;\r | |
120 | }\r | |
121 | \r | |
122 | switch (ReadOrWrite) {\r | |
123 | case PXE_IO_READ:\r | |
4cda7726 | 124 | Snp->PciIo->Io.Read (\r |
125 | Snp->PciIo,\r | |
126 | Width,\r | |
127 | Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address\r | |
128 | MemOrPortAddr,\r | |
129 | 1, // count\r | |
130 | (VOID *) (UINTN) BufferPtr\r | |
131 | );\r | |
5e7e4fad | 132 | break;\r |
133 | \r | |
134 | case PXE_IO_WRITE:\r | |
4cda7726 | 135 | Snp->PciIo->Io.Write (\r |
136 | Snp->PciIo,\r | |
137 | Width,\r | |
138 | Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address\r | |
139 | MemOrPortAddr,\r | |
140 | 1, // count\r | |
141 | (VOID *) (UINTN) BufferPtr\r | |
142 | );\r | |
5e7e4fad | 143 | break;\r |
144 | \r | |
145 | case PXE_MEM_READ:\r | |
4cda7726 | 146 | Snp->PciIo->Mem.Read (\r |
147 | Snp->PciIo,\r | |
5e7e4fad | 148 | Width,\r |
4cda7726 | 149 | Snp->MemoryBarIndex, // BAR 0, Memory base address\r |
150 | MemOrPortAddr,\r | |
5e7e4fad | 151 | 1, // count\r |
4cda7726 | 152 | (VOID *) (UINTN) BufferPtr\r |
5e7e4fad | 153 | );\r |
154 | break;\r | |
155 | \r | |
156 | case PXE_MEM_WRITE:\r | |
4cda7726 | 157 | Snp->PciIo->Mem.Write (\r |
158 | Snp->PciIo,\r | |
5e7e4fad | 159 | Width,\r |
4cda7726 | 160 | Snp->MemoryBarIndex, // BAR 0, Memory base address\r |
161 | MemOrPortAddr,\r | |
5e7e4fad | 162 | 1, // count\r |
4cda7726 | 163 | (VOID *) (UINTN) BufferPtr\r |
5e7e4fad | 164 | );\r |
165 | break;\r | |
166 | }\r | |
167 | \r | |
168 | return ;\r | |
169 | }\r | |
170 | \r | |
4cda7726 | 171 | /**\r |
9c9f5859 | 172 | Map a CPU address to a device address.\r |
f3816027 | 173 | \r |
4cda7726 | 174 | This is a callback routine supplied to UNDI at undi_start time.\r |
4cda7726 | 175 | \r |
f3816027 | 176 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r |
177 | store Undi interface context (Undi does not read or write\r | |
178 | this variable).\r | |
179 | @param CpuAddr Virtual address to be mapped.\r | |
180 | @param NumBytes Size of memory to be mapped.\r | |
181 | @param Direction Direction of data flow for this memory's usage:\r | |
182 | cpu->device, device->cpu or both ways.\r | |
183 | @param DeviceAddrPtr Pointer to return the mapped device address.\r | |
4cda7726 | 184 | \r |
185 | **/\r | |
5e7e4fad | 186 | VOID\r |
9c9f5859 | 187 | EFIAPI\r |
4cda7726 | 188 | SnpUndi32CallbackMap (\r |
5e7e4fad | 189 | IN UINT64 UniqueId,\r |
190 | IN UINT64 CpuAddr,\r | |
191 | IN UINT32 NumBytes,\r | |
192 | IN UINT32 Direction,\r | |
193 | IN OUT UINT64 DeviceAddrPtr\r | |
194 | )\r | |
5e7e4fad | 195 | {\r |
196 | EFI_PHYSICAL_ADDRESS *DevAddrPtr;\r | |
197 | EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;\r | |
198 | UINTN BuffSize;\r | |
4cda7726 | 199 | SNP_DRIVER *Snp;\r |
5e7e4fad | 200 | UINTN Index;\r |
201 | EFI_STATUS Status;\r | |
202 | \r | |
203 | BuffSize = (UINTN) NumBytes;\r | |
4cda7726 | 204 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r |
5e7e4fad | 205 | DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;\r |
206 | \r | |
207 | if (CpuAddr == 0) {\r | |
208 | *DevAddrPtr = 0;\r | |
209 | return ;\r | |
210 | }\r | |
211 | \r | |
212 | switch (Direction) {\r | |
213 | case TO_AND_FROM_DEVICE:\r | |
214 | DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;\r | |
215 | break;\r | |
216 | \r | |
217 | case FROM_DEVICE:\r | |
218 | DirectionFlag = EfiPciIoOperationBusMasterWrite;\r | |
219 | break;\r | |
220 | \r | |
221 | case TO_DEVICE:\r | |
222 | DirectionFlag = EfiPciIoOperationBusMasterRead;\r | |
223 | break;\r | |
224 | \r | |
225 | default:\r | |
226 | *DevAddrPtr = 0;\r | |
227 | //\r | |
228 | // any non zero indicates error!\r | |
229 | //\r | |
230 | return ;\r | |
231 | }\r | |
232 | //\r | |
233 | // find an unused map_list entry\r | |
234 | //\r | |
235 | for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {\r | |
4cda7726 | 236 | if (Snp->MapList[Index].VirtualAddress == 0) {\r |
5e7e4fad | 237 | break;\r |
238 | }\r | |
239 | }\r | |
240 | \r | |
241 | if (Index >= MAX_MAP_LENGTH) {\r | |
242 | DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n"));\r | |
243 | *DevAddrPtr = 0;\r | |
244 | return ;\r | |
245 | }\r | |
246 | \r | |
4cda7726 | 247 | Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS) CpuAddr;\r |
5e7e4fad | 248 | \r |
4cda7726 | 249 | Status = Snp->PciIo->Map (\r |
250 | Snp->PciIo,\r | |
251 | DirectionFlag,\r | |
252 | (VOID *) (UINTN) CpuAddr,\r | |
253 | &BuffSize,\r | |
254 | DevAddrPtr,\r | |
255 | &(Snp->MapList[Index].MapCookie)\r | |
256 | );\r | |
5e7e4fad | 257 | if (Status != EFI_SUCCESS) {\r |
4cda7726 | 258 | *DevAddrPtr = 0;\r |
259 | Snp->MapList[Index].VirtualAddress = 0;\r | |
5e7e4fad | 260 | }\r |
261 | \r | |
262 | return ;\r | |
263 | }\r | |
264 | \r | |
4cda7726 | 265 | /**\r |
9c9f5859 | 266 | Unmap an address that was previously mapped using map callback.\r |
267 | \r | |
4cda7726 | 268 | This is a callback routine supplied to UNDI at undi_start time.\r |
4cda7726 | 269 | \r |
9c9f5859 | 270 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r |
f3816027 | 271 | store. Undi interface context (Undi does not read or write\r |
272 | this variable).\r | |
273 | @param CpuAddr Virtual address that was mapped.\r | |
274 | @param NumBytes Size of memory mapped.\r | |
9c9f5859 | 275 | @param Direction Direction of data flow for this memory's usage:\r |
f3816027 | 276 | cpu->device, device->cpu or both ways.\r |
277 | @param DeviceAddr The mapped device address.\r | |
4cda7726 | 278 | \r |
279 | **/\r | |
5e7e4fad | 280 | VOID\r |
9c9f5859 | 281 | EFIAPI\r |
4cda7726 | 282 | SnpUndi32CallbackUnmap (\r |
5e7e4fad | 283 | IN UINT64 UniqueId,\r |
284 | IN UINT64 CpuAddr,\r | |
285 | IN UINT32 NumBytes,\r | |
286 | IN UINT32 Direction,\r | |
287 | IN UINT64 DeviceAddr\r | |
288 | )\r | |
5e7e4fad | 289 | {\r |
4cda7726 | 290 | SNP_DRIVER *Snp;\r |
5e7e4fad | 291 | UINT16 Index;\r |
292 | \r | |
4cda7726 | 293 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r |
5e7e4fad | 294 | \r |
295 | for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {\r | |
4cda7726 | 296 | if (Snp->MapList[Index].VirtualAddress == CpuAddr) {\r |
5e7e4fad | 297 | break;\r |
298 | }\r | |
299 | }\r | |
300 | \r | |
f3816027 | 301 | if (Index >= MAX_MAP_LENGTH) {\r |
5e7e4fad | 302 | DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));\r |
303 | return ;\r | |
304 | }\r | |
305 | \r | |
4cda7726 | 306 | Snp->PciIo->Unmap (Snp->PciIo, Snp->MapList[Index].MapCookie);\r |
307 | Snp->MapList[Index].VirtualAddress = 0;\r | |
308 | Snp->MapList[Index].MapCookie = NULL;\r | |
5e7e4fad | 309 | return ;\r |
310 | }\r | |
311 | \r | |
9c9f5859 | 312 | /**\r |
313 | Synchronize the virtual buffer contents with the mapped buffer contents.\r | |
314 | \r | |
f3816027 | 315 | This is a callback routine supplied to UNDI at undi_start time. The virtual\r |
9c9f5859 | 316 | and mapped buffers need not correspond to the same physical memory (especially\r |
317 | if the virtual address is > 4GB). Depending on the direction for which the\r | |
318 | buffer is mapped, undi will need to synchronize their contents whenever it\r | |
319 | writes to/reads from the buffer using either the cpu address or the device\r | |
320 | address.\r | |
321 | EFI does not provide a sync call since virt=physical, we should just do the\r | |
322 | synchronization ourselves here.\r | |
323 | \r | |
324 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r | |
f3816027 | 325 | store Undi interface context (Undi does not read or write\r |
326 | this variable).\r | |
327 | @param CpuAddr Virtual address that was mapped.\r | |
328 | @param NumBytes Size of memory mapped.\r | |
329 | @param Direction Direction of data flow for this memory's usage:\r | |
4cda7726 | 330 | cpu->device, device->cpu or both ways.\r |
f3816027 | 331 | @param DeviceAddr The mapped device address.\r |
4cda7726 | 332 | \r |
333 | **/\r | |
5e7e4fad | 334 | VOID\r |
9c9f5859 | 335 | EFIAPI\r |
4cda7726 | 336 | SnpUndi32CallbackSync (\r |
337 | IN UINT64 UniqueId,\r | |
338 | IN UINT64 CpuAddr,\r | |
339 | IN UINT32 NumBytes,\r | |
340 | IN UINT32 Direction,\r | |
341 | IN UINT64 DeviceAddr\r | |
5e7e4fad | 342 | )\r |
5e7e4fad | 343 | {\r |
344 | if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {\r | |
345 | return ;\r | |
346 | \r | |
347 | }\r | |
348 | \r | |
349 | switch (Direction) {\r | |
350 | case FROM_DEVICE:\r | |
351 | CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);\r | |
352 | break;\r | |
353 | \r | |
354 | case TO_DEVICE:\r | |
355 | CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);\r | |
356 | break;\r | |
357 | }\r | |
358 | \r | |
359 | return ;\r | |
360 | }\r |