]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Snp.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Snp.c
1 /** @file
2 Implementation of driver entry point and driver binding protocol.
3
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed
6 and made available under the terms and conditions of the BSD License which
7 accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Snp.h"
16
17 //
18 // Module global variables needed to support undi 3.0 interface
19 //
20 EFI_PCI_IO_PROTOCOL *mPciIo;
21 V2P *mV2p = NULL; // undi3.0 map_list head
22 // End Global variables
23 //
24
25 /**
26 One notified function to stop UNDI device when gBS->ExitBootServices() called.
27
28 @param Event Pointer to this event
29 @param Context Event hanlder private data
30
31 **/
32 VOID
33 EFIAPI
34 SnpNotifyExitBootServices (
35 EFI_EVENT Event,
36 VOID *Context
37 )
38 {
39 SNP_DRIVER *Snp;
40
41 Snp = (SNP_DRIVER *)Context;
42
43 //
44 // Shutdown and stop UNDI driver
45 //
46 PxeShutdown (Snp);
47 PxeStop (Snp);
48 }
49
50 /**
51 Send command to UNDI. It does nothing currently.
52
53 @param Cdb command to be sent to UNDI.
54
55 @retval EFI_INVALID_PARAMETER The command is 0.
56 @retval EFI_UNSUPPORTED Default return status because it's not
57 supported currently.
58
59 **/
60 EFI_STATUS
61 IssueHwUndiCommand (
62 UINT64 Cdb
63 )
64 {
65 DEBUG ((EFI_D_ERROR, "\nIssueHwUndiCommand() - This should not be called!"));
66
67 if (Cdb == 0) {
68 return EFI_INVALID_PARAMETER;
69
70 }
71 //
72 // %%TBD - For now, nothing is done.
73 //
74 return EFI_UNSUPPORTED;
75 }
76
77
78 /**
79 Compute 8-bit checksum of a buffer.
80
81 @param Buffer Pointer to buffer.
82 @param Length Length of buffer in bytes.
83
84 @return 8-bit checksum of all bytes in buffer, or zero if ptr is NULL or len
85 is zero.
86
87 **/
88 UINT8
89 Calc8BitCksum (
90 VOID *Buffer,
91 UINTN Length
92 )
93 {
94 UINT8 *Ptr;
95 UINT8 Cksum;
96
97 Ptr = Buffer;
98 Cksum = 0;
99
100 if (Ptr == NULL || Length == 0) {
101 return 0;
102 }
103
104 while (Length-- != 0) {
105 Cksum = (UINT8) (Cksum + *Ptr++);
106 }
107
108 return Cksum;
109 }
110
111 /**
112 Test to see if this driver supports ControllerHandle. This service
113 is called by the EFI boot service ConnectController(). In
114 order to make drivers as small as possible, there are a few calling
115 restrictions for this service. ConnectController() must
116 follow these calling restrictions. If any other agent wishes to call
117 Supported() it must also follow these calling restrictions.
118
119 @param This Protocol instance pointer.
120 @param ControllerHandle Handle of device to test.
121 @param RemainingDevicePath Optional parameter use to pick a specific child
122 device to start.
123
124 @retval EFI_SUCCESS This driver supports this device.
125 @retval EFI_ALREADY_STARTED This driver is already running on this device.
126 @retval other This driver does not support this device.
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 SimpleNetworkDriverSupported (
132 IN EFI_DRIVER_BINDING_PROTOCOL *This,
133 IN EFI_HANDLE Controller,
134 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
135 )
136 {
137 EFI_STATUS Status;
138 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *NiiProtocol;
139 PXE_UNDI *Pxe;
140
141 Status = gBS->OpenProtocol (
142 Controller,
143 &gEfiDevicePathProtocolGuid,
144 NULL,
145 This->DriverBindingHandle,
146 Controller,
147 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
148 );
149 if (EFI_ERROR (Status)) {
150 return Status;
151 }
152
153 Status = gBS->OpenProtocol (
154 Controller,
155 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
156 (VOID **) &NiiProtocol,
157 This->DriverBindingHandle,
158 Controller,
159 EFI_OPEN_PROTOCOL_BY_DRIVER
160 );
161
162 if (EFI_ERROR (Status)) {
163 if (Status == EFI_ALREADY_STARTED) {
164 DEBUG ((EFI_D_INFO, "Support(): Already Started. on handle %p\n", Controller));
165 }
166 return Status;
167 }
168
169 DEBUG ((EFI_D_INFO, "Support(): UNDI3.1 found on handle %p\n", Controller));
170
171 //
172 // check the version, we don't want to connect to the undi16
173 //
174 if (NiiProtocol->Type != EfiNetworkInterfaceUndi) {
175 Status = EFI_UNSUPPORTED;
176 goto Done;
177 }
178 //
179 // Check to see if !PXE structure is valid. Paragraph alignment of !PXE structure is required.
180 //
181 if ((NiiProtocol->Id & 0x0F) != 0) {
182 DEBUG ((EFI_D_NET, "\n!PXE structure is not paragraph aligned.\n"));
183 Status = EFI_UNSUPPORTED;
184 goto Done;
185 }
186
187 Pxe = (PXE_UNDI *) (UINTN) (NiiProtocol->Id);
188
189 //
190 // Verify !PXE revisions.
191 //
192 if (Pxe->hw.Signature != PXE_ROMID_SIGNATURE) {
193 DEBUG ((EFI_D_NET, "\n!PXE signature is not valid.\n"));
194 Status = EFI_UNSUPPORTED;
195 goto Done;
196 }
197
198 if (Pxe->hw.Rev < PXE_ROMID_REV) {
199 DEBUG ((EFI_D_NET, "\n!PXE.Rev is not supported.\n"));
200 Status = EFI_UNSUPPORTED;
201 goto Done;
202 }
203
204 if (Pxe->hw.MajorVer < PXE_ROMID_MAJORVER) {
205
206 DEBUG ((EFI_D_NET, "\n!PXE.MajorVer is not supported.\n"));
207 Status = EFI_UNSUPPORTED;
208 goto Done;
209
210 } else if (Pxe->hw.MajorVer == PXE_ROMID_MAJORVER && Pxe->hw.MinorVer < PXE_ROMID_MINORVER) {
211 DEBUG ((EFI_D_NET, "\n!PXE.MinorVer is not supported."));
212 Status = EFI_UNSUPPORTED;
213 goto Done;
214 }
215 //
216 // Do S/W UNDI specific checks.
217 //
218 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) == 0) {
219 if (Pxe->sw.EntryPoint < Pxe->sw.Len) {
220 DEBUG ((EFI_D_NET, "\n!PXE S/W entry point is not valid."));
221 Status = EFI_UNSUPPORTED;
222 goto Done;
223 }
224
225 if (Pxe->sw.BusCnt == 0) {
226 DEBUG ((EFI_D_NET, "\n!PXE.BusCnt is zero."));
227 Status = EFI_UNSUPPORTED;
228 goto Done;
229 }
230 }
231
232 Status = EFI_SUCCESS;
233 DEBUG ((EFI_D_INFO, "Support(): supported on %p\n", Controller));
234
235 Done:
236 gBS->CloseProtocol (
237 Controller,
238 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
239 This->DriverBindingHandle,
240 Controller
241 );
242
243 return Status;
244 }
245
246 /**
247 Start this driver on ControllerHandle. This service is called by the
248 EFI boot service ConnectController(). In order to make
249 drivers as small as possible, there are a few calling restrictions for
250 this service. ConnectController() must follow these
251 calling restrictions. If any other agent wishes to call Start() it
252 must also follow these calling restrictions.
253
254 @param This Protocol instance pointer.
255 @param ControllerHandle Handle of device to bind driver to.
256 @param RemainingDevicePath Optional parameter use to pick a specific child
257 device to start.
258
259 @retval EFI_SUCCESS This driver is added to ControllerHandle
260 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
261 @retval other This driver does not support this device
262
263 **/
264 EFI_STATUS
265 EFIAPI
266 SimpleNetworkDriverStart (
267 IN EFI_DRIVER_BINDING_PROTOCOL *This,
268 IN EFI_HANDLE Controller,
269 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
270 )
271 {
272 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii;
273 EFI_DEVICE_PATH_PROTOCOL *NiiDevicePath;
274 EFI_STATUS Status;
275 PXE_UNDI *Pxe;
276 SNP_DRIVER *Snp;
277 VOID *Address;
278 EFI_HANDLE Handle;
279 PXE_PCI_CONFIG_INFO ConfigInfo;
280 PCI_TYPE00 *ConfigHeader;
281 UINT32 *TempBar;
282 UINT8 BarIndex;
283 PXE_STATFLAGS InitStatFlags;
284
285 DEBUG ((EFI_D_NET, "\nSnpNotifyNetworkInterfaceIdentifier() "));
286
287 Status = gBS->OpenProtocol (
288 Controller,
289 &gEfiDevicePathProtocolGuid,
290 (VOID **) &NiiDevicePath,
291 This->DriverBindingHandle,
292 Controller,
293 EFI_OPEN_PROTOCOL_BY_DRIVER
294 );
295
296 if (EFI_ERROR (Status)) {
297 return Status;
298 }
299
300 Status = gBS->LocateDevicePath (
301 &gEfiPciIoProtocolGuid,
302 &NiiDevicePath,
303 &Handle
304 );
305
306 if (EFI_ERROR (Status)) {
307 return Status;
308 }
309
310 Status = gBS->OpenProtocol (
311 Handle,
312 &gEfiPciIoProtocolGuid,
313 (VOID **) &mPciIo,
314 This->DriverBindingHandle,
315 Controller,
316 EFI_OPEN_PROTOCOL_GET_PROTOCOL
317 );
318 if (EFI_ERROR (Status)) {
319 return Status;
320 }
321 //
322 // Get the NII interface.
323 //
324 Status = gBS->OpenProtocol (
325 Controller,
326 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
327 (VOID **) &Nii,
328 This->DriverBindingHandle,
329 Controller,
330 EFI_OPEN_PROTOCOL_BY_DRIVER
331 );
332 if (EFI_ERROR (Status)) {
333 gBS->CloseProtocol (
334 Controller,
335 &gEfiDevicePathProtocolGuid,
336 This->DriverBindingHandle,
337 Controller
338 );
339 return Status;
340 }
341
342 DEBUG ((EFI_D_INFO, "Start(): UNDI3.1 found\n"));
343
344 Pxe = (PXE_UNDI *) (UINTN) (Nii->Id);
345
346 if (Calc8BitCksum (Pxe, Pxe->hw.Len) != 0) {
347 DEBUG ((EFI_D_NET, "\n!PXE checksum is not correct.\n"));
348 goto NiiError;
349 }
350
351 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) {
352 //
353 // We can get any packets.
354 //
355 } else if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) {
356 //
357 // We need to be able to get broadcast packets for DHCP.
358 // If we do not have promiscuous support, we must at least have
359 // broadcast support or we cannot do DHCP!
360 //
361 } else {
362 DEBUG ((EFI_D_NET, "\nUNDI does not have promiscuous or broadcast support."));
363 goto NiiError;
364 }
365 //
366 // OK, we like this UNDI, and we know snp is not already there on this handle
367 // Allocate and initialize a new simple network protocol structure.
368 //
369 Status = mPciIo->AllocateBuffer (
370 mPciIo,
371 AllocateAnyPages,
372 EfiBootServicesData,
373 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
374 &Address,
375 0
376 );
377
378 if (Status != EFI_SUCCESS) {
379 DEBUG ((EFI_D_NET, "\nCould not allocate SNP_DRIVER structure.\n"));
380 goto NiiError;
381 }
382
383 Snp = (SNP_DRIVER *) (UINTN) Address;
384
385 ZeroMem (Snp, sizeof (SNP_DRIVER));
386
387 Snp->PciIo = mPciIo;
388 Snp->Signature = SNP_DRIVER_SIGNATURE;
389
390 EfiInitializeLock (&Snp->Lock, TPL_NOTIFY);
391
392 Snp->Snp.Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
393 Snp->Snp.Start = SnpUndi32Start;
394 Snp->Snp.Stop = SnpUndi32Stop;
395 Snp->Snp.Initialize = SnpUndi32Initialize;
396 Snp->Snp.Reset = SnpUndi32Reset;
397 Snp->Snp.Shutdown = SnpUndi32Shutdown;
398 Snp->Snp.ReceiveFilters = SnpUndi32ReceiveFilters;
399 Snp->Snp.StationAddress = SnpUndi32StationAddress;
400 Snp->Snp.Statistics = SnpUndi32Statistics;
401 Snp->Snp.MCastIpToMac = SnpUndi32McastIpToMac;
402 Snp->Snp.NvData = SnpUndi32NvData;
403 Snp->Snp.GetStatus = SnpUndi32GetStatus;
404 Snp->Snp.Transmit = SnpUndi32Transmit;
405 Snp->Snp.Receive = SnpUndi32Receive;
406 Snp->Snp.WaitForPacket = NULL;
407
408 Snp->Snp.Mode = &Snp->Mode;
409
410 Snp->TxRxBufferSize = 0;
411 Snp->TxRxBuffer = NULL;
412
413 Snp->IfNum = Nii->IfNum;
414
415 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) != 0) {
416 Snp->IsSwUndi = FALSE;
417 Snp->IssueUndi32Command = &IssueHwUndiCommand;
418 } else {
419 Snp->IsSwUndi = TRUE;
420
421 if ((Pxe->sw.Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR) != 0) {
422 Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) Pxe->sw.EntryPoint;
423 } else {
424 Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) ((UINT8) (UINTN) Pxe + Pxe->sw.EntryPoint);
425 }
426 }
427 //
428 // Allocate a global CPB and DB buffer for this UNDI interface.
429 // we do this because:
430 //
431 // -UNDI 3.0 wants all the addresses passed to it (even the cpb and db) to be
432 // within 2GB limit, create them here and map them so that when undi calls
433 // v2p callback to check if the physical address is < 2gb, we will pass.
434 //
435 // -This is not a requirement for 3.1 or later UNDIs but the code looks
436 // simpler if we use the same cpb, db variables for both old and new undi
437 // interfaces from all the SNP interface calls (we don't map the buffers
438 // for the newer undi interfaces though)
439 // .
440 // -it is OK to allocate one global set of CPB, DB pair for each UNDI
441 // interface as EFI does not multi-task and so SNP will not be re-entered!
442 //
443 Status = mPciIo->AllocateBuffer (
444 mPciIo,
445 AllocateAnyPages,
446 EfiBootServicesData,
447 SNP_MEM_PAGES (4096),
448 &Address,
449 0
450 );
451
452 if (Status != EFI_SUCCESS) {
453 DEBUG ((EFI_D_NET, "\nCould not allocate CPB and DB structures.\n"));
454 goto Error_DeleteSNP;
455 }
456
457 Snp->Cpb = (VOID *) (UINTN) Address;
458 Snp->Db = (VOID *) ((UINTN) Address + 2048);
459
460 //
461 // PxeStart call is going to give the callback functions to UNDI, these callback
462 // functions use the BarIndex values from the snp structure, so these must be initialized
463 // with default values before doing a PxeStart. The correct values can be obtained after
464 // getting the config information from UNDI
465 //
466 Snp->MemoryBarIndex = 0;
467 Snp->IoBarIndex = 1;
468
469 //
470 // we need the undi init information many times in this snp code, just get it
471 // once here and store it in the snp driver structure. to get Init Info
472 // from UNDI we have to start undi first.
473 //
474 Status = PxeStart (Snp);
475
476 if (Status != EFI_SUCCESS) {
477 goto Error_DeleteSNP;
478 }
479
480 Snp->Cdb.OpCode = PXE_OPCODE_GET_INIT_INFO;
481 Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED;
482
483 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
484 Snp->Cdb.CPBaddr = PXE_DBADDR_NOT_USED;
485
486 Snp->Cdb.DBsize = sizeof Snp->InitInfo;
487 Snp->Cdb.DBaddr = (UINT64)(UINTN) &Snp->InitInfo;
488
489 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
490 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
491
492 Snp->Cdb.IFnum = Snp->IfNum;
493 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
494
495 DEBUG ((EFI_D_NET, "\nSnp->undi.get_init_info() "));
496
497 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
498
499 //
500 // Save the INIT Stat Code...
501 //
502 InitStatFlags = Snp->Cdb.StatFlags;
503
504 if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
505 DEBUG ((EFI_D_NET, "\nSnp->undi.init_info() %xh:%xh\n", Snp->Cdb.StatFlags, Snp->Cdb.StatCode));
506 PxeStop (Snp);
507 goto Error_DeleteSNP;
508 }
509
510 Snp->Cdb.OpCode = PXE_OPCODE_GET_CONFIG_INFO;
511 Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED;
512
513 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
514 Snp->Cdb.CPBaddr = PXE_DBADDR_NOT_USED;
515
516 Snp->Cdb.DBsize = sizeof ConfigInfo;
517 Snp->Cdb.DBaddr = (UINT64)(UINTN) &ConfigInfo;
518
519 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
520 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
521
522 Snp->Cdb.IFnum = Snp->IfNum;
523 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
524
525 DEBUG ((EFI_D_NET, "\nSnp->undi.get_config_info() "));
526
527 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
528
529 if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
530 DEBUG ((EFI_D_NET, "\nSnp->undi.config_info() %xh:%xh\n", Snp->Cdb.StatFlags, Snp->Cdb.StatCode));
531 PxeStop (Snp);
532 goto Error_DeleteSNP;
533 }
534 //
535 // Find the correct BAR to do IO.
536 //
537 //
538 // Enumerate through the PCI BARs for the device to determine which one is
539 // the IO BAR. Save the index of the BAR into the adapter info structure.
540 // for regular 32bit BARs, 0 is memory mapped, 1 is io mapped
541 //
542 ConfigHeader = (PCI_TYPE00 *) &ConfigInfo.Config.Byte[0];
543 TempBar = (UINT32 *) &ConfigHeader->Device.Bar[0];
544 for (BarIndex = 0; BarIndex <= 5; BarIndex++) {
545 if ((*TempBar & PCI_BAR_MEM_MASK) == PCI_BAR_MEM_64BIT) {
546 //
547 // This is a 64-bit memory bar, skip this and the
548 // next bar as well.
549 //
550 TempBar++;
551 }
552
553 if ((*TempBar & PCI_BAR_IO_MASK) == PCI_BAR_IO_MODE) {
554 Snp->IoBarIndex = BarIndex;
555 break;
556 }
557
558 TempBar++;
559 }
560
561 //
562 // Initialize simple network protocol mode structure
563 //
564 Snp->Mode.State = EfiSimpleNetworkStopped;
565 Snp->Mode.HwAddressSize = Snp->InitInfo.HWaddrLen;
566 Snp->Mode.MediaHeaderSize = Snp->InitInfo.MediaHeaderLen;
567 Snp->Mode.MaxPacketSize = Snp->InitInfo.FrameDataLen;
568 Snp->Mode.NvRamAccessSize = Snp->InitInfo.NvWidth;
569 Snp->Mode.NvRamSize = Snp->InitInfo.NvCount * Snp->Mode.NvRamAccessSize;
570 Snp->Mode.IfType = Snp->InitInfo.IFtype;
571 Snp->Mode.MaxMCastFilterCount = Snp->InitInfo.MCastFilterCnt;
572 Snp->Mode.MCastFilterCount = 0;
573
574 switch (InitStatFlags & PXE_STATFLAGS_CABLE_DETECT_MASK) {
575 case PXE_STATFLAGS_CABLE_DETECT_SUPPORTED:
576 Snp->Mode.MediaPresentSupported = TRUE;
577 break;
578
579 case PXE_STATFLAGS_CABLE_DETECT_NOT_SUPPORTED:
580 default:
581 Snp->Mode.MediaPresentSupported = FALSE;
582 }
583
584 switch (InitStatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_MASK) {
585 case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED:
586 Snp->MediaStatusSupported = TRUE;
587 break;
588
589 case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_NOT_SUPPORTED:
590 default:
591 Snp->MediaStatusSupported = FALSE;
592 }
593
594 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE) != 0) {
595 Snp->Mode.MacAddressChangeable = TRUE;
596 } else {
597 Snp->Mode.MacAddressChangeable = FALSE;
598 }
599
600 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_MULTI_FRAME_SUPPORTED) != 0) {
601 Snp->Mode.MultipleTxSupported = TRUE;
602 } else {
603 Snp->Mode.MultipleTxSupported = FALSE;
604 }
605
606 Snp->Mode.ReceiveFilterMask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
607
608 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) {
609 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
610
611 }
612
613 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) {
614 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
615
616 }
617
618 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) {
619 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
620
621 }
622
623 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_FILTERED_MULTICAST_RX_SUPPORTED) != 0) {
624 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
625
626 }
627
628 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) {
629 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
630
631 }
632
633 Snp->Mode.ReceiveFilterSetting = 0;
634
635 //
636 // need to get the station address to save in the mode structure. we need to
637 // initialize the UNDI first for this.
638 //
639 Snp->TxRxBufferSize = Snp->InitInfo.MemoryRequired;
640 Status = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);
641
642 if (EFI_ERROR (Status)) {
643 PxeStop (Snp);
644 goto Error_DeleteSNP;
645 }
646
647 Status = PxeGetStnAddr (Snp);
648
649 if (Status != EFI_SUCCESS) {
650 DEBUG ((EFI_D_ERROR, "\nSnp->undi.get_station_addr() failed.\n"));
651 PxeShutdown (Snp);
652 PxeStop (Snp);
653 goto Error_DeleteSNP;
654 }
655
656 Snp->Mode.MediaPresent = FALSE;
657
658 //
659 // We should not leave UNDI started and initialized here. this DriverStart()
660 // routine must only find and attach the SNP interface to UNDI layer that it
661 // finds on the given handle!
662 // The UNDI layer will be started when upper layers call Snp->start.
663 // How ever, this DriverStart() must fill up the snp mode structure which
664 // contains the MAC address of the NIC. For this reason we started and
665 // initialized UNDI here, now we are done, do a shutdown and stop of the
666 // UNDI interface!
667 //
668 PxeShutdown (Snp);
669 PxeStop (Snp);
670
671 //
672 // Create EXIT_BOOT_SERIVES Event
673 //
674 Status = gBS->CreateEventEx (
675 EVT_NOTIFY_SIGNAL,
676 TPL_NOTIFY,
677 SnpNotifyExitBootServices,
678 Snp,
679 &gEfiEventExitBootServicesGuid,
680 &Snp->ExitBootServicesEvent
681 );
682 if (EFI_ERROR (Status)) {
683 goto Error_DeleteSNP;
684 }
685
686 //
687 // add SNP to the undi handle
688 //
689 Status = gBS->InstallProtocolInterface (
690 &Controller,
691 &gEfiSimpleNetworkProtocolGuid,
692 EFI_NATIVE_INTERFACE,
693 &(Snp->Snp)
694 );
695
696 if (!EFI_ERROR (Status)) {
697 return Status;
698 }
699
700 Status = mPciIo->FreeBuffer (
701 mPciIo,
702 SNP_MEM_PAGES (4096),
703 Snp->Cpb
704 );
705
706 Error_DeleteSNP:
707
708 mPciIo->FreeBuffer (
709 mPciIo,
710 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
711 Snp
712 );
713 NiiError:
714 gBS->CloseProtocol (
715 Controller,
716 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
717 This->DriverBindingHandle,
718 Controller
719 );
720
721 gBS->CloseProtocol (
722 Controller,
723 &gEfiDevicePathProtocolGuid,
724 This->DriverBindingHandle,
725 Controller
726 );
727
728 return Status;
729 }
730
731 /**
732 Stop this driver on ControllerHandle. This service is called by the
733 EFI boot service DisconnectController(). In order to
734 make drivers as small as possible, there are a few calling
735 restrictions for this service. DisconnectController()
736 must follow these calling restrictions. If any other agent wishes
737 to call Stop() it must also follow these calling restrictions.
738
739 @param This Protocol instance pointer.
740 @param ControllerHandle Handle of device to stop driver on
741 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
742 children is zero stop the entire bus driver.
743 @param ChildHandleBuffer List of Child Handles to Stop.
744
745 @retval EFI_SUCCESS This driver is removed ControllerHandle
746 @retval other This driver was not removed from this device
747
748 **/
749 EFI_STATUS
750 EFIAPI
751 SimpleNetworkDriverStop (
752 IN EFI_DRIVER_BINDING_PROTOCOL *This,
753 IN EFI_HANDLE Controller,
754 IN UINTN NumberOfChildren,
755 IN EFI_HANDLE *ChildHandleBuffer
756 )
757 {
758 EFI_STATUS Status;
759 EFI_SIMPLE_NETWORK_PROTOCOL *SnpProtocol;
760 SNP_DRIVER *Snp;
761
762 //
763 // Get our context back.
764 //
765 Status = gBS->OpenProtocol (
766 Controller,
767 &gEfiSimpleNetworkProtocolGuid,
768 (VOID **) &SnpProtocol,
769 This->DriverBindingHandle,
770 Controller,
771 EFI_OPEN_PROTOCOL_GET_PROTOCOL
772 );
773
774 if (EFI_ERROR (Status)) {
775 return EFI_UNSUPPORTED;
776 }
777
778 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (SnpProtocol);
779
780 Status = gBS->UninstallProtocolInterface (
781 Controller,
782 &gEfiSimpleNetworkProtocolGuid,
783 &Snp->Snp
784 );
785
786 if (EFI_ERROR (Status)) {
787 return Status;
788 }
789
790 //
791 // Close EXIT_BOOT_SERIVES Event
792 //
793 gBS->CloseEvent (Snp->ExitBootServicesEvent);
794
795 Status = gBS->CloseProtocol (
796 Controller,
797 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
798 This->DriverBindingHandle,
799 Controller
800 );
801
802 Status = gBS->CloseProtocol (
803 Controller,
804 &gEfiDevicePathProtocolGuid,
805 This->DriverBindingHandle,
806 Controller
807 );
808
809 PxeShutdown (Snp);
810 PxeStop (Snp);
811
812 mPciIo->FreeBuffer (
813 mPciIo,
814 SNP_MEM_PAGES (4096),
815 Snp->Cpb
816 );
817
818 mPciIo->FreeBuffer (
819 mPciIo,
820 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
821 Snp
822 );
823
824 return Status;
825 }
826
827 //
828 // Simple Network Protocol Driver Global Variables
829 //
830 EFI_DRIVER_BINDING_PROTOCOL mSimpleNetworkDriverBinding = {
831 SimpleNetworkDriverSupported,
832 SimpleNetworkDriverStart,
833 SimpleNetworkDriverStop,
834 0xa,
835 NULL,
836 NULL
837 };
838
839
840 /**
841 This routine maps the given CPU address to a Device address. It creates a
842 an entry in the map list with the virtual and physical addresses and the
843 un map cookie.
844
845 @param V2p pointer to return a map list node pointer.
846 @param Type the direction in which the data flows from the given
847 virtual address device->cpu or cpu->device or both
848 ways.
849 @param VirtualAddress virtual address (or CPU address) to be mapped.
850 @param BufferSize size of the buffer to be mapped.
851
852 @retval EFI_SUCEESS routine has completed the mapping.
853 @retval EFI_INVALID_PARAMETER invalid parameter.
854 @retval EFI_OUT_OF_RESOURCES out of resource.
855 @retval other error as indicated.
856
857 **/
858 EFI_STATUS
859 AddV2P (
860 IN OUT V2P **V2p,
861 EFI_PCI_IO_PROTOCOL_OPERATION Type,
862 VOID *VirtualAddress,
863 UINTN BufferSize
864 )
865 {
866 EFI_STATUS Status;
867
868 if ((V2p == NULL) || (VirtualAddress == NULL) || (BufferSize == 0)) {
869 return EFI_INVALID_PARAMETER;
870 }
871
872 *V2p = AllocatePool (sizeof (V2P));
873 if (*V2p == NULL) {
874 return EFI_OUT_OF_RESOURCES;
875 }
876
877 Status = mPciIo->Map (
878 mPciIo,
879 Type,
880 VirtualAddress,
881 &BufferSize,
882 &(*V2p)->PhysicalAddress,
883 &(*V2p)->Unmap
884 );
885 if (Status != EFI_SUCCESS) {
886 FreePool (*V2p);
887 return Status;
888 }
889 (*V2p)->VirtualAddress = VirtualAddress;
890 (*V2p)->BufferSize = BufferSize;
891 (*V2p)->Next = mV2p;
892 mV2p = *V2p;
893
894 return EFI_SUCCESS;
895 }
896
897
898 /**
899 This routine searches the linked list of mapped address nodes (for undi3.0
900 interface) to find the node that corresponds to the given virtual address and
901 returns a pointer to that node.
902
903 @param V2p pointer to return a map list node pointer.
904 @param VirtualAddr virtual address (or CPU address) to be searched in
905 the map list
906
907 @retval EFI_SUCEESS A match was found.
908 @retval Other A match cannot be found.
909
910 **/
911 EFI_STATUS
912 FindV2p (
913 V2P **V2p,
914 VOID *VirtualAddr
915 )
916 {
917 V2P *Ptr;
918
919 if (V2p == NULL || VirtualAddr == NULL) {
920 return EFI_INVALID_PARAMETER;
921 }
922
923 for (Ptr = mV2p; Ptr != NULL; Ptr = Ptr->Next) {
924 if (Ptr->VirtualAddress == VirtualAddr) {
925 *V2p = Ptr;
926 return EFI_SUCCESS;
927 }
928 }
929
930 return EFI_NOT_FOUND;
931 }
932
933
934 /**
935 Unmap the given virtual address and free the memory allocated for the map list
936 node corresponding to that address.
937
938 @param VirtualAddress virtual address (or CPU address) to be unmapped.
939
940 @retval EFI_SUCEESS Successfully unmapped.
941 @retval Other Other errors as indicated.
942
943 **/
944 EFI_STATUS
945 DelV2p (
946 VOID *VirtualAddress
947 )
948 {
949 V2P *Current;
950 V2P *Next;
951 EFI_STATUS Status;
952
953 if (VirtualAddress == NULL) {
954 return EFI_INVALID_PARAMETER;
955 }
956
957 if (mV2p == NULL) {
958 return EFI_NOT_FOUND;
959 }
960 //
961 // Is our node at the head of the list??
962 //
963 if ((Current = mV2p)->VirtualAddress == VirtualAddress) {
964 mV2p = mV2p->Next;
965
966 Status = mPciIo->Unmap (mPciIo, Current->Unmap);
967
968 FreePool (Current);
969
970 if (EFI_ERROR (Status)) {
971 DEBUG ((EFI_D_ERROR, "Unmap failed with status = %r\n", Status));
972 }
973 return Status;
974 }
975
976 for (; Current->Next != NULL; Current = Next) {
977 if ((Next = Current->Next)->VirtualAddress == VirtualAddress) {
978 Current->Next = Next->Next;
979 Status = mPciIo->Unmap (mPciIo, Next->Unmap);
980 FreePool (Next);
981
982 if (EFI_ERROR (Status)) {
983 DEBUG ((EFI_D_ERROR, "Unmap failed with status = %r\n", Status));
984 }
985 return Status;
986 }
987 }
988
989 return EFI_NOT_FOUND;
990 }
991
992 /**
993 The SNP driver entry point.
994
995 @param ImageHandle The driver image handle.
996 @param SystemTable The system table.
997
998 @retval EFI_SUCEESS Initialization routine has found UNDI hardware,
999 loaded it's ROM, and installed a notify event for
1000 the Network Indentifier Interface Protocol
1001 successfully.
1002 @retval Other Return value from HandleProtocol for
1003 DeviceIoProtocol or LoadedImageProtocol
1004
1005 **/
1006 EFI_STATUS
1007 EFIAPI
1008 InitializeSnpNiiDriver (
1009 IN EFI_HANDLE ImageHandle,
1010 IN EFI_SYSTEM_TABLE *SystemTable
1011 )
1012 {
1013 return EfiLibInstallDriverBindingComponentName2 (
1014 ImageHandle,
1015 SystemTable,
1016 &mSimpleNetworkDriverBinding,
1017 NULL,
1018 &gSimpleNetworkComponentName,
1019 &gSimpleNetworkComponentName2
1020 );
1021 }