]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | This file contains the callback routines for undi3.1.\r | |
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 | |
6 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
7 | This program and the accompanying materials\r | |
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 | |
15 | **/\r | |
16 | \r | |
17 | #include "Snp.h"\r | |
18 | \r | |
19 | /**\r | |
20 | Acquire or release a lock of the exclusive access to a critical section of the\r | |
21 | code/data.\r | |
22 | \r | |
23 | This is a callback routine supplied to UNDI3.1 at undi_start time.\r | |
24 | New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1\r | |
25 | because undi3.1 uses the MemMap call to map the required address by itself!\r | |
26 | \r | |
27 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r | |
28 | store Undi interface context (Undi does not read or write\r | |
29 | this variable).\r | |
30 | @param Enable Non-zero indicates acquire; Zero indicates release.\r | |
31 | \r | |
32 | **/\r | |
33 | VOID\r | |
34 | EFIAPI\r | |
35 | SnpUndi32CallbackBlock (\r | |
36 | IN UINT64 UniqueId,\r | |
37 | IN UINT32 Enable\r | |
38 | )\r | |
39 | {\r | |
40 | SNP_DRIVER *Snp;\r | |
41 | \r | |
42 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r | |
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 | |
48 | EfiAcquireLock (&Snp->Lock);\r | |
49 | } else {\r | |
50 | EfiReleaseLock (&Snp->Lock);\r | |
51 | }\r | |
52 | }\r | |
53 | \r | |
54 | /**\r | |
55 | Delay MicroSeconds of micro seconds.\r | |
56 | \r | |
57 | This is a callback routine supplied to UNDI at undi_start time.\r | |
58 | \r | |
59 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r | |
60 | store Undi interface context (Undi does not read or write\r | |
61 | this variable).\r | |
62 | @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.\r | |
63 | \r | |
64 | **/\r | |
65 | VOID\r | |
66 | EFIAPI\r | |
67 | SnpUndi32CallbackDelay (\r | |
68 | IN UINT64 UniqueId,\r | |
69 | IN UINT64 MicroSeconds\r | |
70 | )\r | |
71 | {\r | |
72 | if (MicroSeconds != 0) {\r | |
73 | gBS->Stall ((UINTN) MicroSeconds);\r | |
74 | }\r | |
75 | }\r | |
76 | \r | |
77 | /**\r | |
78 | IO routine for UNDI3.1.\r | |
79 | \r | |
80 | This is a callback routine supplied to UNDI at undi_start time.\r | |
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 | |
85 | @param ReadOrWrite Indicates read or write, IO or Memory.\r | |
86 | @param NumBytes Number of bytes to read or write.\r | |
87 | @param MemOrPortAddr IO or memory address to read from or write to.\r | |
88 | @param BufferPtr Memory location to read into or that contains the bytes\r | |
89 | to write.\r | |
90 | \r | |
91 | **/\r | |
92 | VOID\r | |
93 | EFIAPI\r | |
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 | |
100 | )\r | |
101 | {\r | |
102 | SNP_DRIVER *Snp;\r | |
103 | EFI_PCI_IO_PROTOCOL_WIDTH Width;\r | |
104 | \r | |
105 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r | |
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 | |
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 | |
132 | break;\r | |
133 | \r | |
134 | case PXE_IO_WRITE:\r | |
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 | |
143 | break;\r | |
144 | \r | |
145 | case PXE_MEM_READ:\r | |
146 | Snp->PciIo->Mem.Read (\r | |
147 | Snp->PciIo,\r | |
148 | Width,\r | |
149 | Snp->MemoryBarIndex, // BAR 0, Memory base address\r | |
150 | MemOrPortAddr,\r | |
151 | 1, // count\r | |
152 | (VOID *) (UINTN) BufferPtr\r | |
153 | );\r | |
154 | break;\r | |
155 | \r | |
156 | case PXE_MEM_WRITE:\r | |
157 | Snp->PciIo->Mem.Write (\r | |
158 | Snp->PciIo,\r | |
159 | Width,\r | |
160 | Snp->MemoryBarIndex, // BAR 0, Memory base address\r | |
161 | MemOrPortAddr,\r | |
162 | 1, // count\r | |
163 | (VOID *) (UINTN) BufferPtr\r | |
164 | );\r | |
165 | break;\r | |
166 | }\r | |
167 | \r | |
168 | return ;\r | |
169 | }\r | |
170 | \r | |
171 | /**\r | |
172 | Map a CPU address to a device address.\r | |
173 | \r | |
174 | This is a callback routine supplied to UNDI at undi_start time.\r | |
175 | \r | |
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 | |
184 | \r | |
185 | **/\r | |
186 | VOID\r | |
187 | EFIAPI\r | |
188 | SnpUndi32CallbackMap (\r | |
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 | |
195 | {\r | |
196 | EFI_PHYSICAL_ADDRESS *DevAddrPtr;\r | |
197 | EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;\r | |
198 | UINTN BuffSize;\r | |
199 | SNP_DRIVER *Snp;\r | |
200 | UINTN Index;\r | |
201 | EFI_STATUS Status;\r | |
202 | \r | |
203 | BuffSize = (UINTN) NumBytes;\r | |
204 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r | |
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 | |
236 | if (Snp->MapList[Index].VirtualAddress == 0) {\r | |
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 | |
247 | Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS) CpuAddr;\r | |
248 | \r | |
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 | |
257 | if (Status != EFI_SUCCESS) {\r | |
258 | *DevAddrPtr = 0;\r | |
259 | Snp->MapList[Index].VirtualAddress = 0;\r | |
260 | }\r | |
261 | \r | |
262 | return ;\r | |
263 | }\r | |
264 | \r | |
265 | /**\r | |
266 | Unmap an address that was previously mapped using map callback.\r | |
267 | \r | |
268 | This is a callback routine supplied to UNDI at undi_start time.\r | |
269 | \r | |
270 | @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to\r | |
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 | |
275 | @param Direction Direction of data flow for this memory's usage:\r | |
276 | cpu->device, device->cpu or both ways.\r | |
277 | @param DeviceAddr The mapped device address.\r | |
278 | \r | |
279 | **/\r | |
280 | VOID\r | |
281 | EFIAPI\r | |
282 | SnpUndi32CallbackUnmap (\r | |
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 | |
289 | {\r | |
290 | SNP_DRIVER *Snp;\r | |
291 | UINT16 Index;\r | |
292 | \r | |
293 | Snp = (SNP_DRIVER *) (UINTN) UniqueId;\r | |
294 | \r | |
295 | for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {\r | |
296 | if (Snp->MapList[Index].VirtualAddress == CpuAddr) {\r | |
297 | break;\r | |
298 | }\r | |
299 | }\r | |
300 | \r | |
301 | if (Index >= MAX_MAP_LENGTH) {\r | |
302 | DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));\r | |
303 | return ;\r | |
304 | }\r | |
305 | \r | |
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 | |
309 | return ;\r | |
310 | }\r | |
311 | \r | |
312 | /**\r | |
313 | Synchronize the virtual buffer contents with the mapped buffer contents.\r | |
314 | \r | |
315 | This is a callback routine supplied to UNDI at undi_start time. The virtual\r | |
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 | |
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 | |
330 | cpu->device, device->cpu or both ways.\r | |
331 | @param DeviceAddr The mapped device address.\r | |
332 | \r | |
333 | **/\r | |
334 | VOID\r | |
335 | EFIAPI\r | |
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 | |
342 | )\r | |
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 |