]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Callback.c
Copied remotely
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Callback.c
1 /*++
2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 Module name:
12 callback.c
13
14 Abstract:
15 This file contains two sets of callback routines for undi3.0 and undi3.1.
16 the callback routines for Undi3.1 have an extra parameter UniqueId which
17 stores the interface context for the NIC that snp is trying to talk..
18
19 --*/
20
21
22 #include "Snp.h"
23
24 //
25 // Global variables
26 // these 2 global variables are used only for 3.0 undi. we could not place
27 // them in the snp structure because we will not know which snp structure
28 // in the callback context!
29 //
30 STATIC BOOLEAN mInitializeLock = TRUE;
31 STATIC EFI_LOCK mLock;
32
33 //
34 // End Global variables
35 //
36 extern EFI_PCI_IO_PROTOCOL *mPciIoFncs;
37
38 VOID
39 snp_undi32_callback_v2p_30 (
40 IN UINT64 CpuAddr,
41 IN OUT UINT64 DeviceAddrPtr
42 )
43 /*++
44
45 Routine Description:
46 This is a callback routine supplied to UNDI at undi_start time.
47 UNDI call this routine with a virtual or CPU address that SNP provided
48 to convert it to a physical or device address. Since EFI uses the identical
49 mapping, this routine returns the physical address same as the virtual address
50 for most of the addresses. an address above 4GB cannot generally be used as a
51 device address, it needs to be mapped to a lower physical address. This routine
52 does not call the map routine itself, but it assumes that the mapping was done
53 at the time of providing the address to UNDI. This routine just looks up the
54 address in a map table (which is the v2p structure chain)
55
56 Arguments:
57 CpuAddr - virtual address of a buffer
58 DeviceAddrPtr - pointer to the physical address
59
60 Returns:
61 void - The DeviceAddrPtr will contain 0 in case of any error
62
63 --*/
64 {
65 struct s_v2p *v2p;
66 //
67 // Do nothing if virtual address is zero or physical pointer is NULL.
68 // No need to map if the virtual address is within 4GB limit since
69 // EFI uses identical mapping
70 //
71 if ((CpuAddr == 0) || (DeviceAddrPtr == 0)) {
72 DEBUG ((EFI_D_ERROR, "\nv2p: Null virtual address or physical pointer.\n"));
73 return ;
74 }
75
76 if (CpuAddr < FOUR_GIGABYTES) {
77 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
78 return ;
79 }
80 //
81 // SNP creates a vaddr tp paddr mapping at the time of calling undi with any
82 // big address, this callback routine just looks up in the v2p list and
83 // returns the physical address for any given virtual address.
84 //
85 if (find_v2p (&v2p, (VOID *) (UINTN) CpuAddr) != EFI_SUCCESS) {
86 *(UINT64 *) (UINTN) DeviceAddrPtr = CpuAddr;
87 } else {
88 *(UINT64 *) (UINTN) DeviceAddrPtr = v2p->paddr;
89 }
90 }
91
92 VOID
93 snp_undi32_callback_block_30 (
94 IN UINT32 Enable
95 )
96 /*++
97
98 Routine Description:
99 This is a callback routine supplied to UNDI at undi_start time.
100 UNDI call this routine when it wants to have exclusive access to a critical
101 section of the code/data
102
103 Arguments:
104 Enable - non-zero indicates acquire
105 zero indicates release
106
107 Returns:
108 void
109 --*/
110 {
111 //
112 // tcpip was calling snp at tpl_notify and if we acquire a lock that was
113 // created at a lower level (TPL_CALLBACK) it gives an assert!
114 //
115 if (mInitializeLock) {
116 EfiInitializeLock (&mLock, TPL_NOTIFY);
117 mInitializeLock = FALSE;
118 }
119
120 if (Enable != 0) {
121 EfiAcquireLock (&mLock);
122 } else {
123 EfiReleaseLock (&mLock);
124 }
125 }
126
127 VOID
128 snp_undi32_callback_delay_30 (
129 IN UINT64 MicroSeconds
130 )
131 /*++
132
133 Routine Description:
134 This is a callback routine supplied to UNDI at undi_start time.
135 UNDI call this routine with the number of micro seconds when it wants to
136 pause.
137
138 Arguments:
139 MicroSeconds - number of micro seconds to pause, ususlly multiple of 10
140
141 Returns:
142 void
143 --*/
144 {
145 if (MicroSeconds != 0) {
146 gBS->Stall ((UINTN) MicroSeconds);
147 }
148 }
149
150 VOID
151 snp_undi32_callback_memio_30 (
152 IN UINT8 ReadOrWrite,
153 IN UINT8 NumBytes,
154 IN UINT64 Address,
155 IN OUT UINT64 BufferAddr
156 )
157 /*++
158
159 Routine Description:
160 This is a callback routine supplied to UNDI at undi_start time.
161 This is the IO routine for UNDI. This is not currently being used by UNDI3.0
162 because Undi3.0 uses io/mem offsets relative to the beginning of the device
163 io/mem address and so it needs to use the PCI_IO_FUNCTION that abstracts the
164 start of the device's io/mem addresses. Since SNP cannot retrive the context
165 of the undi3.0 interface it cannot use the PCI_IO_FUNCTION that specific for
166 that NIC and uses one global IO functions structure, this does not work.
167 This however works fine for EFI1.0 Undis because they use absolute addresses
168 for io/mem access.
169
170 Arguments:
171 ReadOrWrite - indicates read or write, IO or Memory
172 NumBytes - number of bytes to read or write
173 Address - IO or memory address to read from or write to
174 BufferAddr - memory location to read into or that contains the bytes
175 to write
176
177 Returns:
178
179 --*/
180 {
181 EFI_PCI_IO_PROTOCOL_WIDTH Width;
182
183 switch (NumBytes) {
184 case 2:
185 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
186 break;
187
188 case 4:
189 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
190 break;
191
192 case 8:
193 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
194 break;
195
196 default:
197 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
198 }
199
200 switch (ReadOrWrite) {
201 case PXE_IO_READ:
202 mPciIoFncs->Io.Read (
203 mPciIoFncs,
204 Width,
205 1, // BAR 1, IO base address
206 Address,
207 1, // count
208 (VOID *) (UINTN) BufferAddr
209 );
210 break;
211
212 case PXE_IO_WRITE:
213 mPciIoFncs->Io.Write (
214 mPciIoFncs,
215 Width,
216 1, // BAR 1, IO base address
217 Address,
218 1, // count
219 (VOID *) (UINTN) BufferAddr
220 );
221 break;
222
223 case PXE_MEM_READ:
224 mPciIoFncs->Mem.Read (
225 mPciIoFncs,
226 Width,
227 0, // BAR 0, Memory base address
228 Address,
229 1, // count
230 (VOID *) (UINTN) BufferAddr
231 );
232 break;
233
234 case PXE_MEM_WRITE:
235 mPciIoFncs->Mem.Write (
236 mPciIoFncs,
237 Width,
238 0, // BAR 0, Memory base address
239 Address,
240 1, // count
241 (VOID *) (UINTN) BufferAddr
242 );
243 break;
244 }
245
246 return ;
247 }
248 //
249 // New callbacks for 3.1:
250 // there won't be a virtual2physical callback for UNDI 3.1 because undi3.1 uses
251 // the MemMap call to map the required address by itself!
252 //
253 VOID
254 snp_undi32_callback_block (
255 IN UINT64 UniqueId,
256 IN UINT32 Enable
257 )
258 /*++
259
260 Routine Description:
261 This is a callback routine supplied to UNDI3.1 at undi_start time.
262 UNDI call this routine when it wants to have exclusive access to a critical
263 section of the code/data
264
265 Arguments:
266 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
267 Undi interface context (Undi does not read or write this variable)
268 Enable - non-zero indicates acquire
269 zero indicates release
270
271 Returns:
272 void
273
274 --*/
275 {
276 SNP_DRIVER *snp;
277
278 snp = (SNP_DRIVER *) (UINTN) UniqueId;
279 //
280 // tcpip was calling snp at tpl_notify and when we acquire a lock that was
281 // created at a lower level (TPL_CALLBACK) it gives an assert!
282 //
283 if (Enable != 0) {
284 EfiAcquireLock (&snp->lock);
285 } else {
286 EfiReleaseLock (&snp->lock);
287 }
288 }
289
290 VOID
291 snp_undi32_callback_delay (
292 IN UINT64 UniqueId,
293 IN UINT64 MicroSeconds
294 )
295 /*++
296
297 Routine Description:
298 This is a callback routine supplied to UNDI at undi_start time.
299 UNDI call this routine with the number of micro seconds when it wants to
300 pause.
301
302 Arguments:
303 MicroSeconds - number of micro seconds to pause, ususlly multiple of 10
304
305 Returns:
306 void
307 --*/
308 {
309 if (MicroSeconds != 0) {
310 gBS->Stall ((UINTN) MicroSeconds);
311 }
312 }
313
314 /*
315 * IO routine for UNDI start CPB.
316 */
317 VOID
318 snp_undi32_callback_memio (
319 UINT64 UniqueId,
320 UINT8 ReadOrWrite,
321 UINT8 NumBytes,
322 UINT64 Address,
323 UINT64 BufferAddr
324 )
325 /*++
326
327 Routine Description:
328 This is a callback routine supplied to UNDI at undi_start time.
329 This is the IO routine for UNDI3.1.
330
331 Arguments:
332 ReadOrWrite - indicates read or write, IO or Memory
333 NumBytes - number of bytes to read or write
334 Address - IO or memory address to read from or write to
335 BufferAddr - memory location to read into or that contains the bytes
336 to write
337
338 Returns:
339
340 --*/
341 {
342 SNP_DRIVER *snp;
343 EFI_PCI_IO_PROTOCOL_WIDTH Width;
344
345 snp = (SNP_DRIVER *) (UINTN) UniqueId;
346
347 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 0;
348 switch (NumBytes) {
349 case 2:
350 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 1;
351 break;
352
353 case 4:
354 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 2;
355 break;
356
357 case 8:
358 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) 3;
359 break;
360 }
361
362 switch (ReadOrWrite) {
363 case PXE_IO_READ:
364 snp->IoFncs->Io.Read (
365 snp->IoFncs,
366 Width,
367 snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
368 Address,
369 1, // count
370 (VOID *) (UINTN) BufferAddr
371 );
372 break;
373
374 case PXE_IO_WRITE:
375 snp->IoFncs->Io.Write (
376 snp->IoFncs,
377 Width,
378 snp->IoBarIndex, // BAR 1 (for 32bit regs), IO base address
379 Address,
380 1, // count
381 (VOID *) (UINTN) BufferAddr
382 );
383 break;
384
385 case PXE_MEM_READ:
386 snp->IoFncs->Mem.Read (
387 snp->IoFncs,
388 Width,
389 snp->MemoryBarIndex, // BAR 0, Memory base address
390 Address,
391 1, // count
392 (VOID *) (UINTN) BufferAddr
393 );
394 break;
395
396 case PXE_MEM_WRITE:
397 snp->IoFncs->Mem.Write (
398 snp->IoFncs,
399 Width,
400 snp->MemoryBarIndex, // BAR 0, Memory base address
401 Address,
402 1, // count
403 (VOID *) (UINTN) BufferAddr
404 );
405 break;
406 }
407
408 return ;
409 }
410
411 VOID
412 snp_undi32_callback_map (
413 IN UINT64 UniqueId,
414 IN UINT64 CpuAddr,
415 IN UINT32 NumBytes,
416 IN UINT32 Direction,
417 IN OUT UINT64 DeviceAddrPtr
418 )
419 /*++
420
421 Routine Description:
422 This is a callback routine supplied to UNDI at undi_start time.
423 UNDI call this routine when it has to map a CPU address to a device
424 address.
425
426 Arguments:
427 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
428 Undi interface context (Undi does not read or write this variable)
429 CpuAddr - Virtual address to be mapped!
430 NumBytes - size of memory to be mapped
431 Direction - direction of data flow for this memory's usage:
432 cpu->device, device->cpu or both ways
433 DeviceAddrPtr - pointer to return the mapped device address
434
435 Returns:
436 None
437
438 --*/
439 {
440 EFI_PHYSICAL_ADDRESS *DevAddrPtr;
441 EFI_PCI_IO_PROTOCOL_OPERATION DirectionFlag;
442 UINTN BuffSize;
443 SNP_DRIVER *snp;
444 UINTN Index;
445 EFI_STATUS Status;
446
447 BuffSize = (UINTN) NumBytes;
448 snp = (SNP_DRIVER *) (UINTN) UniqueId;
449 DevAddrPtr = (EFI_PHYSICAL_ADDRESS *) (UINTN) DeviceAddrPtr;
450
451 if (CpuAddr == 0) {
452 *DevAddrPtr = 0;
453 return ;
454 }
455
456 switch (Direction) {
457 case TO_AND_FROM_DEVICE:
458 DirectionFlag = EfiPciIoOperationBusMasterCommonBuffer;
459 break;
460
461 case FROM_DEVICE:
462 DirectionFlag = EfiPciIoOperationBusMasterWrite;
463 break;
464
465 case TO_DEVICE:
466 DirectionFlag = EfiPciIoOperationBusMasterRead;
467 break;
468
469 default:
470 *DevAddrPtr = 0;
471 //
472 // any non zero indicates error!
473 //
474 return ;
475 }
476 //
477 // find an unused map_list entry
478 //
479 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
480 if (snp->map_list[Index].virt == 0) {
481 break;
482 }
483 }
484
485 if (Index >= MAX_MAP_LENGTH) {
486 DEBUG ((EFI_D_INFO, "SNP maplist is FULL\n"));
487 *DevAddrPtr = 0;
488 return ;
489 }
490
491 snp->map_list[Index].virt = (EFI_PHYSICAL_ADDRESS) CpuAddr;
492
493 Status = snp->IoFncs->Map (
494 snp->IoFncs,
495 DirectionFlag,
496 (VOID *) (UINTN) CpuAddr,
497 &BuffSize,
498 DevAddrPtr,
499 &(snp->map_list[Index].map_cookie)
500 );
501 if (Status != EFI_SUCCESS) {
502 *DevAddrPtr = 0;
503 snp->map_list[Index].virt = 0;
504 }
505
506 return ;
507 }
508
509 VOID
510 snp_undi32_callback_unmap (
511 IN UINT64 UniqueId,
512 IN UINT64 CpuAddr,
513 IN UINT32 NumBytes,
514 IN UINT32 Direction,
515 IN UINT64 DeviceAddr
516 )
517 /*++
518
519 Routine Description:
520 This is a callback routine supplied to UNDI at undi_start time.
521 UNDI call this routine when it wants to unmap an address that was previously
522 mapped using map callback
523
524 Arguments:
525 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
526 Undi interface context (Undi does not read or write this variable)
527 CpuAddr - Virtual address that was mapped!
528 NumBytes - size of memory mapped
529 Direction- direction of data flow for this memory's usage:
530 cpu->device, device->cpu or both ways
531 DeviceAddr - the mapped device address
532
533 Returns:
534
535 --*/
536 {
537 SNP_DRIVER *snp;
538 UINT16 Index;
539
540 snp = (SNP_DRIVER *) (UINTN) UniqueId;
541
542 for (Index = 0; Index < MAX_MAP_LENGTH; Index++) {
543 if (snp->map_list[Index].virt == CpuAddr) {
544 break;
545 }
546 }
547
548 if (Index >= MAX_MAP_LENGTH)
549 {
550 DEBUG ((EFI_D_ERROR, "SNP could not find a mapping, failed to unmap.\n"));
551 return ;
552 }
553
554 snp->IoFncs->Unmap (snp->IoFncs, snp->map_list[Index].map_cookie);
555 snp->map_list[Index].virt = 0;
556 snp->map_list[Index].map_cookie = NULL;
557 return ;
558 }
559
560 VOID
561 snp_undi32_callback_sync (
562 UINT64 UniqueId,
563 UINT64 CpuAddr,
564 UINT32 NumBytes,
565 UINT32 Direction,
566 UINT64 DeviceAddr
567 )
568 /*++
569
570 Routine Description:
571 This is a callback routine supplied to UNDI at undi_start time.
572 UNDI call this routine when it wants synchronize the virtual buffer contents
573 with the mapped buffer contents. The virtual and mapped buffers need not
574 correspond to the same physical memory (especially if the virtual address is
575 > 4GB). Depending on the direction for which the buffer is mapped, undi will
576 need to synchronize their contents whenever it writes to/reads from the buffer
577 using either the cpu address or the device address.
578
579 EFI does not provide a sync call, since virt=physical, we sould just do
580 the synchronization ourself here!
581
582 Arguments:
583 UniqueId - This was supplied to UNDI at Undi_Start, SNP uses this to store
584 Undi interface context (Undi does not read or write this variable)
585 CpuAddr - Virtual address that was mapped!
586 NumBytes - size of memory mapped
587 Direction- direction of data flow for this memory's usage:
588 cpu->device, device->cpu or both ways
589 DeviceAddr - the mapped device address
590
591 Returns:
592
593 --*/
594 {
595 if ((CpuAddr == 0) || (DeviceAddr == 0) || (NumBytes == 0)) {
596 return ;
597
598 }
599
600 switch (Direction) {
601 case FROM_DEVICE:
602 CopyMem ((UINT8 *) (UINTN) CpuAddr, (UINT8 *) (UINTN) DeviceAddr, NumBytes);
603 break;
604
605 case TO_DEVICE:
606 CopyMem ((UINT8 *) (UINTN) DeviceAddr, (UINT8 *) (UINTN) CpuAddr, NumBytes);
607 break;
608 }
609
610 return ;
611 }