]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Callback.c
5ee191aeea35e63fb502cd62233f42f5a3a6d9bb
[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 - 2011, Intel Corporation. All rights reserved.<BR>
7 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 EFIAPI
53 SnpUndi32CallbackV2p30 (
54 IN UINT64 CpuAddr,
55 IN OUT UINT64 DeviceAddrPtr
56 )
57 {
58 V2P *V2p;
59 //
60 // Do nothing if virtual address is zero or physical pointer is NULL.
61 // No need to map if the virtual address is within 4GB limit since
62 // EFI uses identical mapping
63 //
64 if ((CpuAddr == 0) || (DeviceAddrPtr == 0)) {
65 DEBUG ((EFI_D_NET, "\nv2p: Null virtual address or physical pointer.\n"));
66 return ;
67 }
68
69 if (CpuAddr < FOUR_GIGABYTES) {
70 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
71 return ;
72 }
73 //
74 // SNP creates a vaddr tp paddr mapping at the time of calling undi with any
75 // big address, this callback routine just looks up in the v2p list and
76 // returns the physical address for any given virtual address.
77 //
78 if (FindV2p (&V2p, (VOID *) (UINTN) CpuAddr) != EFI_SUCCESS) {
79 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
80 } else {
81 *(UINT64 *) (UINTN) DeviceAddrPtr = V2p->PhysicalAddress;
82 }
83 }
84
85 /**
86 Acquire or release a lock of an exclusive access to a critical section of the
87 code/data.
88
89 This is a callback routine supplied to UNDI at undi_start time.
90
91 @param Enable Non-zero indicates acquire; Zero indicates release.
92
93 **/
94 VOID
95 EFIAPI
96 SnpUndi32CallbackBlock30 (
97 IN UINT32 Enable
98 )
99 {
100 //
101 // tcpip was calling snp at tpl_notify and if we acquire a lock that was
102 // created at a lower level (TPL_CALLBACK) it gives an assert!
103 //
104 if (mInitializeLock) {
105 EfiInitializeLock (&mLock, TPL_NOTIFY);
106 mInitializeLock = FALSE;
107 }
108
109 if (Enable != 0) {
110 EfiAcquireLock (&mLock);
111 } else {
112 EfiReleaseLock (&mLock);
113 }
114 }
115
116 /**
117 Delay MicroSeconds of micro seconds.
118
119 This is a callback routine supplied to UNDI at undi_start time.
120
121 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
122
123 **/
124 VOID
125 EFIAPI
126 SnpUndi32CallbackDelay30 (
127 IN UINT64 MicroSeconds
128 )
129 {
130 if (MicroSeconds != 0) {
131 gBS->Stall ((UINTN) MicroSeconds);
132 }
133 }
134
135 /**
136 IO routine for UNDI.
137
138 This is a callback routine supplied to UNDI at undi_start time. This is not
139 currently being used by UNDI3.0 because Undi3.0 uses io/mem offsets relative
140 to the beginning of the device io/mem address and so it needs to use the
141 PCI_IO_FUNCTION that abstracts the start of the device's io/mem addresses.
142 Since SNP cannot retrive the context of the undi3.0 interface it cannot use
143 the PCI_IO_FUNCTION that specific for that NIC and uses one global IO
144 functions structure, this does not work. This however works fine for EFI1.0
145 Undis because they use absolute addresses for io/mem access.
146
147 @param ReadOrWrite Indicates read or write, IO or Memory.
148 @param NumBytes Number of bytes to read or write.
149 @param Address IO or memory address to read from or write to.
150 @param BufferAddr Memory location to read into or that contains the bytes to
151 write.
152
153 **/
154 VOID
155 EFIAPI
156 SnpUndi32CallbackMemio30 (
157 IN UINT8 ReadOrWrite,
158 IN UINT8 NumBytes,
159 IN UINT64 Address,
160 IN OUT UINT64 BufferAddr
161 )
162 {
163 EFI_PCI_IO_PROTOCOL_WIDTH Width;
164
165 switch (NumBytes) {
166 case 2:
167 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
168 break;
169
170 case 4:
171 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
172 break;
173
174 case 8:
175 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
176 break;
177
178 default:
179 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
180 }
181
182 switch (ReadOrWrite) {
183 case PXE_IO_READ:
184 mPciIo->Io.Read (
185 mPciIo,
186 Width,
187 1, // BAR 1, IO base address
188 Address,
189 1, // count
190 (VOID *) (UINTN) BufferAddr
191 );
192 break;
193
194 case PXE_IO_WRITE:
195 mPciIo->Io.Write (
196 mPciIo,
197 Width,
198 1, // BAR 1, IO base address
199 Address,
200 1, // count
201 (VOID *) (UINTN) BufferAddr
202 );
203 break;
204
205 case PXE_MEM_READ:
206 mPciIo->Mem.Read (
207 mPciIo,
208 Width,
209 0, // BAR 0, Memory base address
210 Address,
211 1, // count
212 (VOID *) (UINTN) BufferAddr
213 );
214 break;
215
216 case PXE_MEM_WRITE:
217 mPciIo->Mem.Write (
218 mPciIo,
219 Width,
220 0, // BAR 0, Memory base address
221 Address,
222 1, // count
223 (VOID *) (UINTN) BufferAddr
224 );
225 break;
226 }
227
228 return ;
229 }
230
231 /**
232 Acquire or release a lock of the exclusive access to a critical section of the
233 code/data.
234
235 This is a callback routine supplied to UNDI3.1 at undi_start time.
236 New callbacks for 3.1: there won't be a virtual2physical callback for UNDI 3.1
237 because undi3.1 uses the MemMap call to map the required address by itself!
238
239 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
240 store Undi interface context (Undi does not read or write
241 this variable).
242 @param Enable Non-zero indicates acquire; Zero indicates release.
243
244 **/
245 VOID
246 EFIAPI
247 SnpUndi32CallbackBlock (
248 IN UINT64 UniqueId,
249 IN UINT32 Enable
250 )
251 {
252 SNP_DRIVER *Snp;
253
254 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
255 //
256 // tcpip was calling snp at tpl_notify and when we acquire a lock that was
257 // created at a lower level (TPL_CALLBACK) it gives an assert!
258 //
259 if (Enable != 0) {
260 EfiAcquireLock (&Snp->Lock);
261 } else {
262 EfiReleaseLock (&Snp->Lock);
263 }
264 }
265
266 /**
267 Delay MicroSeconds of micro seconds.
268
269 This is a callback routine supplied to UNDI at undi_start time.
270
271 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
272 store Undi interface context (Undi does not read or write
273 this variable).
274 @param MicroSeconds Number of micro seconds to pause, ususlly multiple of 10.
275
276 **/
277 VOID
278 EFIAPI
279 SnpUndi32CallbackDelay (
280 IN UINT64 UniqueId,
281 IN UINT64 MicroSeconds
282 )
283 {
284 if (MicroSeconds != 0) {
285 gBS->Stall ((UINTN) MicroSeconds);
286 }
287 }
288
289 /**
290 IO routine for UNDI3.1.
291
292 This is a callback routine supplied to UNDI at undi_start time.
293
294 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this
295 to store Undi interface context (Undi does not read or
296 write this variable).
297 @param ReadOrWrite Indicates read or write, IO or Memory.
298 @param NumBytes Number of bytes to read or write.
299 @param MemOrPortAddr IO or memory address to read from or write to.
300 @param BufferPtr Memory location to read into or that contains the bytes
301 to write.
302
303 **/
304 VOID
305 EFIAPI
306 SnpUndi32CallbackMemio (
307 IN UINT64 UniqueId,
308 IN UINT8 ReadOrWrite,
309 IN UINT8 NumBytes,
310 IN UINT64 MemOrPortAddr,
311 IN OUT UINT64 BufferPtr
312 )
313 {
314 SNP_DRIVER *Snp;
315 EFI_PCI_IO_PROTOCOL_WIDTH Width;
316
317 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
318
319 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
320 switch (NumBytes) {
321 case 2:
322 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
323 break;
324
325 case 4:
326 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
327 break;
328
329 case 8:
330 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
331 break;
332 }
333
334 switch (ReadOrWrite) {
335 case PXE_IO_READ:
336 Snp->PciIo->Io.Read (
337 Snp->PciIo,
338 Width,
339 Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
340 MemOrPortAddr,
341 1, // count
342 (VOID *) (UINTN) BufferPtr
343 );
344 break;
345
346 case PXE_IO_WRITE:
347 Snp->PciIo->Io.Write (
348 Snp->PciIo,
349 Width,
350 Snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
351 MemOrPortAddr,
352 1, // count
353 (VOID *) (UINTN) BufferPtr
354 );
355 break;
356
357 case PXE_MEM_READ:
358 Snp->PciIo->Mem.Read (
359 Snp->PciIo,
360 Width,
361 Snp->MemoryBarIndex, // BAR 0, Memory base address
362 MemOrPortAddr,
363 1, // count
364 (VOID *) (UINTN) BufferPtr
365 );
366 break;
367
368 case PXE_MEM_WRITE:
369 Snp->PciIo->Mem.Write (
370 Snp->PciIo,
371 Width,
372 Snp->MemoryBarIndex, // BAR 0, Memory base address
373 MemOrPortAddr,
374 1, // count
375 (VOID *) (UINTN) BufferPtr
376 );
377 break;
378 }
379
380 return ;
381 }
382
383 /**
384 Map a CPU address to a device address.
385
386 This is a callback routine supplied to UNDI at undi_start time.
387
388 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
389 store Undi interface context (Undi does not read or write
390 this variable).
391 @param CpuAddr Virtual address to be mapped.
392 @param NumBytes Size of memory to be mapped.
393 @param Direction Direction of data flow for this memory's usage:
394 cpu->device, device->cpu or both ways.
395 @param DeviceAddrPtr Pointer to return the mapped device address.
396
397 **/
398 VOID
399 EFIAPI
400 SnpUndi32CallbackMap (
401 IN UINT64 UniqueId,
402 IN UINT64 CpuAddr,
403 IN UINT32 NumBytes,
404 IN UINT32 Direction,
405 IN OUT UINT64 DeviceAddrPtr
406 )
407 {
408 EFI_PHYSICAL_ADDRESS *DevAddrPtr;
409 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;
410 UINTN BuffSize;
411 SNP_DRIVER *Snp;
412 UINTN Index;
413 EFI_STATUS Status;
414
415 BuffSize = (UINTN) NumBytes;
416 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
417 DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;
418
419 if (CpuAddr == 0) {
420 *DevAddrPtr = 0;
421 return ;
422 }
423
424 switch (Direction) {
425 case TO_AND_FROM_DEVICE:
426 DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;
427 break;
428
429 case FROM_DEVICE:
430 DirectionFlag = EfiPciIoOperationBusMasterWrite;
431 break;
432
433 case TO_DEVICE:
434 DirectionFlag = EfiPciIoOperationBusMasterRead;
435 break;
436
437 default:
438 *DevAddrPtr = 0;
439 //
440 // any non zero indicates error!
441 //
442 return ;
443 }
444 //
445 // find an unused map_list entry
446 //
447 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
448 if (Snp->MapList[Index].VirtualAddress == 0) {
449 break;
450 }
451 }
452
453 if (Index >= MAX_MAP_LENGTH) {
454 DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n"));
455 *DevAddrPtr = 0;
456 return ;
457 }
458
459 Snp->MapList[Index].VirtualAddress = (EFI_PHYSICAL_ADDRESS) CpuAddr;
460
461 Status = Snp->PciIo->Map (
462 Snp->PciIo,
463 DirectionFlag,
464 (VOID *) (UINTN) CpuAddr,
465 &BuffSize,
466 DevAddrPtr,
467 &(Snp->MapList[Index].MapCookie)
468 );
469 if (Status != EFI_SUCCESS) {
470 *DevAddrPtr = 0;
471 Snp->MapList[Index].VirtualAddress = 0;
472 }
473
474 return ;
475 }
476
477 /**
478 Unmap an address that was previously mapped using map callback.
479
480 This is a callback routine supplied to UNDI at undi_start time.
481
482 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
483 store. Undi interface context (Undi does not read or write
484 this variable).
485 @param CpuAddr Virtual address that was mapped.
486 @param NumBytes Size of memory mapped.
487 @param Direction Direction of data flow for this memory's usage:
488 cpu->device, device->cpu or both ways.
489 @param DeviceAddr The mapped device address.
490
491 **/
492 VOID
493 EFIAPI
494 SnpUndi32CallbackUnmap (
495 IN UINT64 UniqueId,
496 IN UINT64 CpuAddr,
497 IN UINT32 NumBytes,
498 IN UINT32 Direction,
499 IN UINT64 DeviceAddr
500 )
501 {
502 SNP_DRIVER *Snp;
503 UINT16 Index;
504
505 Snp = (SNP_DRIVER *) (UINTN) UniqueId;
506
507 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
508 if (Snp->MapList[Index].VirtualAddress == CpuAddr) {
509 break;
510 }
511 }
512
513 if (Index >= MAX_MAP_LENGTH) {
514 DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));
515 return ;
516 }
517
518 Snp->PciIo->Unmap (Snp->PciIo, Snp->MapList[Index].MapCookie);
519 Snp->MapList[Index].VirtualAddress = 0;
520 Snp->MapList[Index].MapCookie = NULL;
521 return ;
522 }
523
524 /**
525 Synchronize the virtual buffer contents with the mapped buffer contents.
526
527 This is a callback routine supplied to UNDI at undi_start time. The virtual
528 and mapped buffers need not correspond to the same physical memory (especially
529 if the virtual address is > 4GB). Depending on the direction for which the
530 buffer is mapped, undi will need to synchronize their contents whenever it
531 writes to/reads from the buffer using either the cpu address or the device
532 address.
533 EFI does not provide a sync call since virt=physical, we should just do the
534 synchronization ourselves here.
535
536 @param UniqueId This was supplied to UNDI at Undi_Start, SNP uses this to
537 store Undi interface context (Undi does not read or write
538 this variable).
539 @param CpuAddr Virtual address that was mapped.
540 @param NumBytes Size of memory mapped.
541 @param Direction Direction of data flow for this memory's usage:
542 cpu->device, device->cpu or both ways.
543 @param DeviceAddr The mapped device address.
544
545 **/
546 VOID
547 EFIAPI
548 SnpUndi32CallbackSync (
549 IN UINT64 UniqueId,
550 IN UINT64 CpuAddr,
551 IN UINT32 NumBytes,
552 IN UINT32 Direction,
553 IN UINT64 DeviceAddr
554 )
555 {
556 if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {
557 return ;
558
559 }
560
561 switch (Direction) {
562 case FROM_DEVICE:
563 CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);
564 break;
565
566 case TO_DEVICE:
567 CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);
568 break;
569 }
570
571 return ;
572 }