]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Callback.c
fc018265c792551f0d18d525634f8126c9701757
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Callback.c
1 /** @file
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.
5
6 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
7 All rights reserved. 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
11
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.
14
15 **/
16
17 #include "Snp.h"
18
19 //
20 // Global variables
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!
24 //
25 BOOLEAN mInitializeLock = TRUE;
26 EFI_LOCK mLock;
27
28 //
29 // End Global variables
30 //
31 extern EFI_PCI_IO_PROTOCOL *mPciIo;
32
33 /**
34 Convert a virtual or CPU address provided by SNP to a physical or device
35 address.
36
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
44 structure chain).
45
46 @param CpuAddr Virtual address.
47 @param DeviceAddrPtr Pointer to the physical address, or 0 in case of any
48 error.
49
50 **/
51 VOID
52 SnpUndi32CallbackV2p30 (
53 IN UINT64 CpuAddr,
54 IN OUT UINT64 DeviceAddrPtr
55 )
56 {
57 V2P *V2p;
58 //
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
62 //
63 if ((CpuAddr == 0) || (DeviceAddrPtr == 0)) {
64 DEBUG ((EFI_D_INFO | EFI_D_NET, "\nv2p: Null virtual address or physical pointer.\n"));
65 return ;
66 }
67
68 if (CpuAddr < FOUR_GIGABYTES) {
69 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
70 return ;
71 }
72 //
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.
76 //
77 if (FindV2p (&V2p, (VOID *) (UINTN) CpuAddr) != EFI_SUCCESS) {
78 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
79 } else {
80 *(UINT64 *) (UINTN) DeviceAddrPtr = V2p->PhysicalAddress;
81 }
82 }
83
84 /**
85 Acquire or release a lock of an exclusive access to a critical section of the
86 code/data.
87
88 This is a callback routine supplied to UNDI at undi_start time.
89
90 @param Enable Non-zero indicates acquire; Zero indicates release.
91
92 **/
93 VOID
94 SnpUndi32CallbackBlock30 (
95 IN UINT32 Enable
96 )
97 {
98 //
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!
101 //
102 if (mInitializeLock) {
103 EfiInitializeLock (&mLock, TPL_NOTIFY);
104 mInitializeLock = FALSE;
105 }
106
107 if (Enable != 0) {
108 EfiAcquireLock (&mLock);
109 } else {
110 EfiReleaseLock (&mLock);
111 }
112 }
113
114 /**
115 Delay MicroSeconds of micro seconds.
116
117 This is a callback routine supplied to UNDI at undi_start time.
118
119 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
120
121 **/
122 VOID
123 SnpUndi32CallbackDelay30 (
124 IN UINT64 MicroSeconds
125 )
126 {
127 if (MicroSeconds != 0) {
128 gBS->Stall ((UINTN) MicroSeconds);
129 }
130 }
131
132 /**
133 IO routine for UNDI.
134
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.
143
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
148 write.
149
150 **/
151 VOID
152 SnpUndi32CallbackMemio30 (
153 IN UINT8 ReadOrWrite,
154 IN UINT8 NumBytes,
155 IN UINT64 Address,
156 IN OUT UINT64 BufferAddr
157 )
158 {
159 EFI_PCI_IO_PROTOCOL_WIDTH Width;
160
161 switch (NumBytes) {
162 case 2:
163 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
164 break;
165
166 case 4:
167 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
168 break;
169
170 case 8:
171 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
172 break;
173
174 default:
175 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
176 }
177
178 switch (ReadOrWrite) {
179 case PXE_IO_READ:
180 mPciIo->Io.Read (
181 mPciIo,
182 Width,
183 1, // BAR 1, IO base address
184 Address,
185 1, // count
186 (VOID *) (UINTN) BufferAddr
187 );
188 break;
189
190 case PXE_IO_WRITE:
191 mPciIo->Io.Write (
192 mPciIo,
193 Width,
194 1, // BAR 1, IO base address
195 Address,
196 1, // count
197 (VOID *) (UINTN) BufferAddr
198 );
199 break;
200
201 case PXE_MEM_READ:
202 mPciIo->Mem.Read (
203 mPciIo,
204 Width,
205 0, // BAR 0, Memory base address
206 Address,
207 1, // count
208 (VOID *) (UINTN) BufferAddr
209 );
210 break;
211
212 case PXE_MEM_WRITE:
213 mPciIo->Mem.Write (
214 mPciIo,
215 Width,
216 0, // BAR 0, Memory base address
217 Address,
218 1, // count
219 (VOID *) (UINTN) BufferAddr
220 );
221 break;
222 }
223
224 return ;
225 }
226
227 /**
228 Acquire or release a lock of the exclusive access to a critical section of the
229 code/data.
230
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!
234
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
237 this variable).
238 @param Enable Non-zero indicates acquire; Zero indicates release.
239
240 **/
241 VOID
242 SnpUndi32CallbackBlock (
243 IN UINT64 UniqueId,
244 IN UINT32 Enable
245 )
246 {
247 SNP_DRIVER *Snp;
248
249 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
250 //
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!
253 //
254 if (Enable != 0) {
255 EfiAcquireLock (&Snp->Lock);
256 } else {
257 EfiReleaseLock (&Snp->Lock);
258 }
259 }
260
261 /**
262 Delay MicroSeconds of micro seconds.
263
264 This is a callback routine supplied to UNDI at undi_start time.
265
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
268 this variable).
269 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
270
271 **/
272 VOID
273 SnpUndi32CallbackDelay (
274 IN UINT64 UniqueId,
275 IN UINT64 MicroSeconds
276 )
277 {
278 if (MicroSeconds != 0) {
279 gBS->Stall ((UINTN) MicroSeconds);
280 }
281 }
282
283 /**
284 IO routine for UNDI3.1.
285
286 This is a callback routine supplied to UNDI at undi_start time.
287
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
295 to write.
296
297 **/
298 VOID
299 SnpUndi32CallbackMemio (
300 IN UINT64 UniqueId,
301 IN UINT8 ReadOrWrite,
302 IN UINT8 NumBytes,
303 IN UINT64 MemOrPortAddr,
304 IN OUT UINT64 BufferPtr
305 )
306 {
307 SNP_DRIVER *Snp;
308 EFI_PCI_IO_PROTOCOL_WIDTH Width;
309
310 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
311
312 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
313 switch (NumBytes) {
314 case 2:
315 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
316 break;
317
318 case 4:
319 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
320 break;
321
322 case 8:
323 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
324 break;
325 }
326
327 switch (ReadOrWrite) {
328 case PXE_IO_READ:
329 Snp->PciIo->Io.Read (
330 Snp->PciIo,
331 Width,
332 Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
333 MemOrPortAddr,
334 1, // count
335 (VOID *) (UINTN) BufferPtr
336 );
337 break;
338
339 case PXE_IO_WRITE:
340 Snp->PciIo->Io.Write (
341 Snp->PciIo,
342 Width,
343 Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
344 MemOrPortAddr,
345 1, // count
346 (VOID *) (UINTN) BufferPtr
347 );
348 break;
349
350 case PXE_MEM_READ:
351 Snp->PciIo->Mem.Read (
352 Snp->PciIo,
353 Width,
354 Snp->MemoryBarIndex, // BAR 0, Memory base address
355 MemOrPortAddr,
356 1, // count
357 (VOID *) (UINTN) BufferPtr
358 );
359 break;
360
361 case PXE_MEM_WRITE:
362 Snp->PciIo->Mem.Write (
363 Snp->PciIo,
364 Width,
365 Snp->MemoryBarIndex, // BAR 0, Memory base address
366 MemOrPortAddr,
367 1, // count
368 (VOID *) (UINTN) BufferPtr
369 );
370 break;
371 }
372
373 return ;
374 }
375
376 /**
377 Map a CPU address to a device address.
378
379 This is a callback routine supplied to UNDI at undi_start time.
380
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
383 this variable).
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.
389
390 **/
391 VOID
392 SnpUndi32CallbackMap (
393 IN UINT64 UniqueId,
394 IN UINT64 CpuAddr,
395 IN UINT32 NumBytes,
396 IN UINT32 Direction,
397 IN OUT UINT64 DeviceAddrPtr
398 )
399 {
400 EFI_PHYSICAL_ADDRESS *DevAddrPtr;
401 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;
402 UINTN BuffSize;
403 SNP_DRIVER *Snp;
404 UINTN Index;
405 EFI_STATUS Status;
406
407 BuffSize = (UINTN) NumBytes;
408 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
409 DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;
410
411 if (CpuAddr == 0) {
412 *DevAddrPtr = 0;
413 return ;
414 }
415
416 switch (Direction) {
417 case TO_AND_FROM_DEVICE:
418 DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;
419 break;
420
421 case FROM_DEVICE:
422 DirectionFlag = EfiPciIoOperationBusMasterWrite;
423 break;
424
425 case TO_DEVICE:
426 DirectionFlag = EfiPciIoOperationBusMasterRead;
427 break;
428
429 default:
430 *DevAddrPtr = 0;
431 //
432 // any non zero indicates error!
433 //
434 return ;
435 }
436 //
437 // find an unused map_list entry
438 //
439 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
440 if (Snp->MapList[Index].VirtualAddress == 0) {
441 break;
442 }
443 }
444
445 if (Index >= MAX_MAP_LENGTH) {
446 DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n"));
447 *DevAddrPtr = 0;
448 return ;
449 }
450
451 Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS) CpuAddr;
452
453 Status = Snp->PciIo->Map (
454 Snp->PciIo,
455 DirectionFlag,
456 (VOID *) (UINTN) CpuAddr,
457 &BuffSize,
458 DevAddrPtr,
459 &(Snp->MapList[Index].MapCookie)
460 );
461 if (Status != EFI_SUCCESS) {
462 *DevAddrPtr = 0;
463 Snp->MapList[Index].VirtualAddress = 0;
464 }
465
466 return ;
467 }
468
469 /**
470 Unmap an address that was previously mapped using map callback.
471
472 This is a callback routine supplied to UNDI at undi_start time.
473
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
476 this variable).
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.
482
483 **/
484 VOID
485 SnpUndi32CallbackUnmap (
486 IN UINT64 UniqueId,
487 IN UINT64 CpuAddr,
488 IN UINT32 NumBytes,
489 IN UINT32 Direction,
490 IN UINT64 DeviceAddr
491 )
492 {
493 SNP_DRIVER *Snp;
494 UINT16 Index;
495
496 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
497
498 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
499 if (Snp->MapList[Index].VirtualAddress == CpuAddr) {
500 break;
501 }
502 }
503
504 if (Index >= MAX_MAP_LENGTH) {
505 DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));
506 return ;
507 }
508
509 Snp->PciIo->Unmap (Snp->PciIo, Snp->MapList[Index].MapCookie);
510 Snp->MapList[Index].VirtualAddress = 0;
511 Snp->MapList[Index].MapCookie = NULL;
512 return ;
513 }
514
515 /**
516 Synchronize the virtual buffer contents with the mapped buffer contents.
517
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
523 address.
524 EFI does not provide a sync call since virt=physical, we should just do the
525 synchronization ourselves here.
526
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
529 this variable).
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.
535
536 **/
537 VOID
538 SnpUndi32CallbackSync (
539 IN UINT64 UniqueId,
540 IN UINT64 CpuAddr,
541 IN UINT32 NumBytes,
542 IN UINT32 Direction,
543 IN UINT64 DeviceAddr
544 )
545 {
546 if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {
547 return ;
548
549 }
550
551 switch (Direction) {
552 case FROM_DEVICE:
553 CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);
554 break;
555
556 case TO_DEVICE:
557 CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);
558 break;
559 }
560
561 return ;
562 }