]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Snp.c
1. Use the PciIo->GetBarAttributes to find the logical bar index of the memory mapped...
[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 - 2014, 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 One notified function to stop UNDI device when gBS->ExitBootServices() called.
19
20 @param Event Pointer to this event
21 @param Context Event handler private data
22
23 **/
24 VOID
25 EFIAPI
26 SnpNotifyExitBootServices (
27 EFI_EVENT Event,
28 VOID *Context
29 )
30 {
31 SNP_DRIVER *Snp;
32
33 Snp = (SNP_DRIVER *)Context;
34
35 //
36 // Shutdown and stop UNDI driver
37 //
38 PxeShutdown (Snp);
39 PxeStop (Snp);
40 }
41
42 /**
43 Send command to UNDI. It does nothing currently.
44
45 @param Cdb command to be sent to UNDI.
46
47 @retval EFI_INVALID_PARAMETER The command is 0.
48 @retval EFI_UNSUPPORTED Default return status because it's not
49 supported currently.
50
51 **/
52 EFI_STATUS
53 EFIAPI
54 IssueHwUndiCommand (
55 UINT64 Cdb
56 )
57 {
58 DEBUG ((EFI_D_ERROR, "\nIssueHwUndiCommand() - This should not be called!"));
59
60 if (Cdb == 0) {
61 return EFI_INVALID_PARAMETER;
62
63 }
64 //
65 // %%TBD - For now, nothing is done.
66 //
67 return EFI_UNSUPPORTED;
68 }
69
70
71 /**
72 Compute 8-bit checksum of a buffer.
73
74 @param Buffer Pointer to buffer.
75 @param Length Length of buffer in bytes.
76
77 @return 8-bit checksum of all bytes in buffer, or zero if ptr is NULL or len
78 is zero.
79
80 **/
81 UINT8
82 Calc8BitCksum (
83 VOID *Buffer,
84 UINTN Length
85 )
86 {
87 UINT8 *Ptr;
88 UINT8 Cksum;
89
90 Ptr = Buffer;
91 Cksum = 0;
92
93 if (Ptr == NULL || Length == 0) {
94 return 0;
95 }
96
97 while (Length-- != 0) {
98 Cksum = (UINT8) (Cksum + *Ptr++);
99 }
100
101 return Cksum;
102 }
103
104 /**
105 Test to see if this driver supports ControllerHandle. This service
106 is called by the EFI boot service ConnectController(). In
107 order to make drivers as small as possible, there are a few calling
108 restrictions for this service. ConnectController() must
109 follow these calling restrictions. If any other agent wishes to call
110 Supported() it must also follow these calling restrictions.
111
112 @param This Protocol instance pointer.
113 @param ControllerHandle Handle of device to test.
114 @param RemainingDevicePath Optional parameter use to pick a specific child
115 device to start.
116
117 @retval EFI_SUCCESS This driver supports this device.
118 @retval EFI_ALREADY_STARTED This driver is already running on this device.
119 @retval other This driver does not support this device.
120
121 **/
122 EFI_STATUS
123 EFIAPI
124 SimpleNetworkDriverSupported (
125 IN EFI_DRIVER_BINDING_PROTOCOL *This,
126 IN EFI_HANDLE Controller,
127 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
128 )
129 {
130 EFI_STATUS Status;
131 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *NiiProtocol;
132 PXE_UNDI *Pxe;
133
134 Status = gBS->OpenProtocol (
135 Controller,
136 &gEfiDevicePathProtocolGuid,
137 NULL,
138 This->DriverBindingHandle,
139 Controller,
140 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
141 );
142 if (EFI_ERROR (Status)) {
143 return Status;
144 }
145
146 Status = gBS->OpenProtocol (
147 Controller,
148 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
149 (VOID **) &NiiProtocol,
150 This->DriverBindingHandle,
151 Controller,
152 EFI_OPEN_PROTOCOL_BY_DRIVER
153 );
154
155 if (EFI_ERROR (Status)) {
156 if (Status == EFI_ALREADY_STARTED) {
157 DEBUG ((EFI_D_INFO, "Support(): Already Started. on handle %p\n", Controller));
158 }
159 return Status;
160 }
161
162 DEBUG ((EFI_D_INFO, "Support(): UNDI3.1 found on handle %p\n", Controller));
163
164 //
165 // check the version, we don't want to connect to the undi16
166 //
167 if (NiiProtocol->Type != EfiNetworkInterfaceUndi) {
168 Status = EFI_UNSUPPORTED;
169 goto Done;
170 }
171 //
172 // Check to see if !PXE structure is valid. Paragraph alignment of !PXE structure is required.
173 //
174 if ((NiiProtocol->Id & 0x0F) != 0) {
175 DEBUG ((EFI_D_NET, "\n!PXE structure is not paragraph aligned.\n"));
176 Status = EFI_UNSUPPORTED;
177 goto Done;
178 }
179
180 Pxe = (PXE_UNDI *) (UINTN) (NiiProtocol->Id);
181
182 //
183 // Verify !PXE revisions.
184 //
185 if (Pxe->hw.Signature != PXE_ROMID_SIGNATURE) {
186 DEBUG ((EFI_D_NET, "\n!PXE signature is not valid.\n"));
187 Status = EFI_UNSUPPORTED;
188 goto Done;
189 }
190
191 if (Pxe->hw.Rev < PXE_ROMID_REV) {
192 DEBUG ((EFI_D_NET, "\n!PXE.Rev is not supported.\n"));
193 Status = EFI_UNSUPPORTED;
194 goto Done;
195 }
196
197 if (Pxe->hw.MajorVer < PXE_ROMID_MAJORVER) {
198
199 DEBUG ((EFI_D_NET, "\n!PXE.MajorVer is not supported.\n"));
200 Status = EFI_UNSUPPORTED;
201 goto Done;
202
203 } else if (Pxe->hw.MajorVer == PXE_ROMID_MAJORVER && Pxe->hw.MinorVer < PXE_ROMID_MINORVER) {
204 DEBUG ((EFI_D_NET, "\n!PXE.MinorVer is not supported."));
205 Status = EFI_UNSUPPORTED;
206 goto Done;
207 }
208 //
209 // Do S/W UNDI specific checks.
210 //
211 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) == 0) {
212 if (Pxe->sw.EntryPoint < Pxe->sw.Len) {
213 DEBUG ((EFI_D_NET, "\n!PXE S/W entry point is not valid."));
214 Status = EFI_UNSUPPORTED;
215 goto Done;
216 }
217
218 if (Pxe->sw.BusCnt == 0) {
219 DEBUG ((EFI_D_NET, "\n!PXE.BusCnt is zero."));
220 Status = EFI_UNSUPPORTED;
221 goto Done;
222 }
223 }
224
225 Status = EFI_SUCCESS;
226 DEBUG ((EFI_D_INFO, "Support(): supported on %p\n", Controller));
227
228 Done:
229 gBS->CloseProtocol (
230 Controller,
231 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
232 This->DriverBindingHandle,
233 Controller
234 );
235
236 return Status;
237 }
238
239 /**
240 Start this driver on ControllerHandle. This service is called by the
241 EFI boot service ConnectController(). In order to make
242 drivers as small as possible, there are a few calling restrictions for
243 this service. ConnectController() must follow these
244 calling restrictions. If any other agent wishes to call Start() it
245 must also follow these calling restrictions.
246
247 @param This Protocol instance pointer.
248 @param ControllerHandle Handle of device to bind driver to.
249 @param RemainingDevicePath Optional parameter use to pick a specific child
250 device to start.
251
252 @retval EFI_SUCCESS This driver is added to ControllerHandle
253 @retval EFI_DEVICE_ERROR This driver could not be started due to a device error
254 @retval other This driver does not support this device
255
256 **/
257 EFI_STATUS
258 EFIAPI
259 SimpleNetworkDriverStart (
260 IN EFI_DRIVER_BINDING_PROTOCOL *This,
261 IN EFI_HANDLE Controller,
262 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
263 )
264 {
265 EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii;
266 EFI_DEVICE_PATH_PROTOCOL *NiiDevicePath;
267 EFI_STATUS Status;
268 PXE_UNDI *Pxe;
269 SNP_DRIVER *Snp;
270 VOID *Address;
271 EFI_HANDLE Handle;
272 UINT8 BarIndex;
273 PXE_STATFLAGS InitStatFlags;
274 EFI_PCI_IO_PROTOCOL *PciIo;
275 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
276
277 DEBUG ((EFI_D_NET, "\nSnpNotifyNetworkInterfaceIdentifier() "));
278
279 Status = gBS->OpenProtocol (
280 Controller,
281 &gEfiDevicePathProtocolGuid,
282 (VOID **) &NiiDevicePath,
283 This->DriverBindingHandle,
284 Controller,
285 EFI_OPEN_PROTOCOL_BY_DRIVER
286 );
287
288 if (EFI_ERROR (Status)) {
289 return Status;
290 }
291
292 Status = gBS->LocateDevicePath (
293 &gEfiPciIoProtocolGuid,
294 &NiiDevicePath,
295 &Handle
296 );
297
298 if (EFI_ERROR (Status)) {
299 return Status;
300 }
301
302 Status = gBS->OpenProtocol (
303 Handle,
304 &gEfiPciIoProtocolGuid,
305 (VOID **) &PciIo,
306 This->DriverBindingHandle,
307 Controller,
308 EFI_OPEN_PROTOCOL_GET_PROTOCOL
309 );
310 if (EFI_ERROR (Status)) {
311 return Status;
312 }
313 //
314 // Get the NII interface.
315 //
316 Status = gBS->OpenProtocol (
317 Controller,
318 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
319 (VOID **) &Nii,
320 This->DriverBindingHandle,
321 Controller,
322 EFI_OPEN_PROTOCOL_BY_DRIVER
323 );
324 if (EFI_ERROR (Status)) {
325 gBS->CloseProtocol (
326 Controller,
327 &gEfiDevicePathProtocolGuid,
328 This->DriverBindingHandle,
329 Controller
330 );
331 return Status;
332 }
333
334 DEBUG ((EFI_D_INFO, "Start(): UNDI3.1 found\n"));
335
336 Pxe = (PXE_UNDI *) (UINTN) (Nii->Id);
337
338 if (Calc8BitCksum (Pxe, Pxe->hw.Len) != 0) {
339 DEBUG ((EFI_D_NET, "\n!PXE checksum is not correct.\n"));
340 goto NiiError;
341 }
342
343 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) {
344 //
345 // We can get any packets.
346 //
347 } else if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) {
348 //
349 // We need to be able to get broadcast packets for DHCP.
350 // If we do not have promiscuous support, we must at least have
351 // broadcast support or we cannot do DHCP!
352 //
353 } else {
354 DEBUG ((EFI_D_NET, "\nUNDI does not have promiscuous or broadcast support."));
355 goto NiiError;
356 }
357 //
358 // OK, we like this UNDI, and we know snp is not already there on this handle
359 // Allocate and initialize a new simple network protocol structure.
360 //
361 Status = PciIo->AllocateBuffer (
362 PciIo,
363 AllocateAnyPages,
364 EfiBootServicesData,
365 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
366 &Address,
367 0
368 );
369
370 if (Status != EFI_SUCCESS) {
371 DEBUG ((EFI_D_NET, "\nCould not allocate SNP_DRIVER structure.\n"));
372 goto NiiError;
373 }
374
375 Snp = (SNP_DRIVER *) (UINTN) Address;
376
377 ZeroMem (Snp, sizeof (SNP_DRIVER));
378
379 Snp->PciIo = PciIo;
380 Snp->Signature = SNP_DRIVER_SIGNATURE;
381
382 EfiInitializeLock (&Snp->Lock, TPL_NOTIFY);
383
384 Snp->Snp.Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
385 Snp->Snp.Start = SnpUndi32Start;
386 Snp->Snp.Stop = SnpUndi32Stop;
387 Snp->Snp.Initialize = SnpUndi32Initialize;
388 Snp->Snp.Reset = SnpUndi32Reset;
389 Snp->Snp.Shutdown = SnpUndi32Shutdown;
390 Snp->Snp.ReceiveFilters = SnpUndi32ReceiveFilters;
391 Snp->Snp.StationAddress = SnpUndi32StationAddress;
392 Snp->Snp.Statistics = SnpUndi32Statistics;
393 Snp->Snp.MCastIpToMac = SnpUndi32McastIpToMac;
394 Snp->Snp.NvData = SnpUndi32NvData;
395 Snp->Snp.GetStatus = SnpUndi32GetStatus;
396 Snp->Snp.Transmit = SnpUndi32Transmit;
397 Snp->Snp.Receive = SnpUndi32Receive;
398 Snp->Snp.WaitForPacket = NULL;
399
400 Snp->Snp.Mode = &Snp->Mode;
401
402 Snp->TxRxBufferSize = 0;
403 Snp->TxRxBuffer = NULL;
404
405 if (Nii->Revision >= EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION) {
406 Snp->IfNum = Nii->IfNum;
407
408 } else {
409 Snp->IfNum = (UINT8) (Nii->IfNum & 0xFF);
410 }
411
412 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_HW_UNDI) != 0) {
413 Snp->IsSwUndi = FALSE;
414 Snp->IssueUndi32Command = &IssueHwUndiCommand;
415 } else {
416 Snp->IsSwUndi = TRUE;
417
418 if ((Pxe->sw.Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR) != 0) {
419 Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) Pxe->sw.EntryPoint;
420 } else {
421 Snp->IssueUndi32Command = (ISSUE_UNDI32_COMMAND) (UINTN) ((UINT8) (UINTN) Pxe + Pxe->sw.EntryPoint);
422 }
423 }
424 //
425 // Allocate a global CPB and DB buffer for this UNDI interface.
426 // we do this because:
427 //
428 // -UNDI 3.0 wants all the addresses passed to it (even the cpb and db) to be
429 // within 2GB limit, create them here and map them so that when undi calls
430 // v2p callback to check if the physical address is < 2gb, we will pass.
431 //
432 // -This is not a requirement for 3.1 or later UNDIs but the code looks
433 // simpler if we use the same cpb, db variables for both old and new undi
434 // interfaces from all the SNP interface calls (we don't map the buffers
435 // for the newer undi interfaces though)
436 // .
437 // -it is OK to allocate one global set of CPB, DB pair for each UNDI
438 // interface as EFI does not multi-task and so SNP will not be re-entered!
439 //
440 Status = PciIo->AllocateBuffer (
441 PciIo,
442 AllocateAnyPages,
443 EfiBootServicesData,
444 SNP_MEM_PAGES (4096),
445 &Address,
446 0
447 );
448
449 if (Status != EFI_SUCCESS) {
450 DEBUG ((EFI_D_NET, "\nCould not allocate CPB and DB structures.\n"));
451 goto Error_DeleteSNP;
452 }
453
454 Snp->Cpb = (VOID *) (UINTN) Address;
455 Snp->Db = (VOID *) ((UINTN) Address + 2048);
456
457 //
458 // Find the correct memory and io bar.
459 //
460 Snp->MemoryBarIndex = PCI_MAX_BAR;
461 Snp->IoBarIndex = PCI_MAX_BAR;
462 for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) {
463 Status = PciIo->GetBarAttributes (
464 PciIo,
465 BarIndex,
466 NULL,
467 (VOID**) &BarDesc
468 );
469 if (Status == EFI_UNSUPPORTED) {
470 continue;
471 } else if (EFI_ERROR (Status)) {
472 goto Error_DeleteSNP;
473 }
474
475 if (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
476 Snp->MemoryBarIndex = BarIndex;
477 } else if (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_IO) {
478 Snp->IoBarIndex = BarIndex;
479 }
480
481 FreePool (BarDesc);
482 }
483 if ((Snp->MemoryBarIndex == PCI_MAX_BAR) || (Snp->IoBarIndex == PCI_MAX_BAR)) {
484 goto Error_DeleteSNP;
485 }
486
487 Status = PxeStart (Snp);
488
489 if (Status != EFI_SUCCESS) {
490 goto Error_DeleteSNP;
491 }
492
493 Snp->Cdb.OpCode = PXE_OPCODE_GET_INIT_INFO;
494 Snp->Cdb.OpFlags = PXE_OPFLAGS_NOT_USED;
495
496 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
497 Snp->Cdb.CPBaddr = PXE_DBADDR_NOT_USED;
498
499 Snp->Cdb.DBsize = (UINT16) sizeof (Snp->InitInfo);
500 Snp->Cdb.DBaddr = (UINT64)(UINTN) (&Snp->InitInfo);
501
502 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
503 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
504
505 Snp->Cdb.IFnum = Snp->IfNum;
506 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
507
508 DEBUG ((EFI_D_NET, "\nSnp->undi.get_init_info() "));
509
510 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
511
512 //
513 // Save the INIT Stat Code...
514 //
515 InitStatFlags = Snp->Cdb.StatFlags;
516
517 if (Snp->Cdb.StatCode != PXE_STATCODE_SUCCESS) {
518 DEBUG ((EFI_D_NET, "\nSnp->undi.init_info() %xh:%xh\n", Snp->Cdb.StatFlags, Snp->Cdb.StatCode));
519 PxeStop (Snp);
520 goto Error_DeleteSNP;
521 }
522
523 //
524 // Initialize simple network protocol mode structure
525 //
526 Snp->Mode.State = EfiSimpleNetworkStopped;
527 Snp->Mode.HwAddressSize = Snp->InitInfo.HWaddrLen;
528 Snp->Mode.MediaHeaderSize = Snp->InitInfo.MediaHeaderLen;
529 Snp->Mode.MaxPacketSize = Snp->InitInfo.FrameDataLen;
530 Snp->Mode.NvRamAccessSize = Snp->InitInfo.NvWidth;
531 Snp->Mode.NvRamSize = Snp->InitInfo.NvCount * Snp->Mode.NvRamAccessSize;
532 Snp->Mode.IfType = Snp->InitInfo.IFtype;
533 Snp->Mode.MaxMCastFilterCount = Snp->InitInfo.MCastFilterCnt;
534 Snp->Mode.MCastFilterCount = 0;
535
536 switch (InitStatFlags & PXE_STATFLAGS_CABLE_DETECT_MASK) {
537 case PXE_STATFLAGS_CABLE_DETECT_SUPPORTED:
538 Snp->Mode.MediaPresentSupported = TRUE;
539 break;
540
541 case PXE_STATFLAGS_CABLE_DETECT_NOT_SUPPORTED:
542 default:
543 Snp->Mode.MediaPresentSupported = FALSE;
544 }
545
546 switch (InitStatFlags & PXE_STATFLAGS_GET_STATUS_NO_MEDIA_MASK) {
547 case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED:
548 Snp->MediaStatusSupported = TRUE;
549 break;
550
551 case PXE_STATFLAGS_GET_STATUS_NO_MEDIA_NOT_SUPPORTED:
552 default:
553 Snp->MediaStatusSupported = FALSE;
554 }
555
556 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE) != 0) {
557 Snp->Mode.MacAddressChangeable = TRUE;
558 } else {
559 Snp->Mode.MacAddressChangeable = FALSE;
560 }
561
562 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_MULTI_FRAME_SUPPORTED) != 0) {
563 Snp->Mode.MultipleTxSupported = TRUE;
564 } else {
565 Snp->Mode.MultipleTxSupported = FALSE;
566 }
567
568 Snp->Mode.ReceiveFilterMask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
569
570 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) {
571 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
572
573 }
574
575 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) {
576 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
577
578 }
579
580 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) {
581 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
582
583 }
584
585 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_FILTERED_MULTICAST_RX_SUPPORTED) != 0) {
586 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
587
588 }
589
590 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) {
591 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
592
593 }
594
595 Snp->Mode.ReceiveFilterSetting = 0;
596
597 //
598 // need to get the station address to save in the mode structure. we need to
599 // initialize the UNDI first for this.
600 //
601 Snp->TxRxBufferSize = Snp->InitInfo.MemoryRequired;
602 Status = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);
603
604 if (EFI_ERROR (Status)) {
605 PxeStop (Snp);
606 goto Error_DeleteSNP;
607 }
608
609 Status = PxeGetStnAddr (Snp);
610
611 if (Status != EFI_SUCCESS) {
612 DEBUG ((EFI_D_ERROR, "\nSnp->undi.get_station_addr() failed.\n"));
613 PxeShutdown (Snp);
614 PxeStop (Snp);
615 goto Error_DeleteSNP;
616 }
617
618 Snp->Mode.MediaPresent = FALSE;
619
620 //
621 // We should not leave UNDI started and initialized here. this DriverStart()
622 // routine must only find and attach the SNP interface to UNDI layer that it
623 // finds on the given handle!
624 // The UNDI layer will be started when upper layers call Snp->start.
625 // How ever, this DriverStart() must fill up the snp mode structure which
626 // contains the MAC address of the NIC. For this reason we started and
627 // initialized UNDI here, now we are done, do a shutdown and stop of the
628 // UNDI interface!
629 //
630 PxeShutdown (Snp);
631 PxeStop (Snp);
632
633 //
634 // Create EXIT_BOOT_SERIVES Event
635 //
636 Status = gBS->CreateEventEx (
637 EVT_NOTIFY_SIGNAL,
638 TPL_NOTIFY,
639 SnpNotifyExitBootServices,
640 Snp,
641 &gEfiEventExitBootServicesGuid,
642 &Snp->ExitBootServicesEvent
643 );
644 if (EFI_ERROR (Status)) {
645 goto Error_DeleteSNP;
646 }
647
648 //
649 // add SNP to the undi handle
650 //
651 Status = gBS->InstallProtocolInterface (
652 &Controller,
653 &gEfiSimpleNetworkProtocolGuid,
654 EFI_NATIVE_INTERFACE,
655 &(Snp->Snp)
656 );
657
658 if (!EFI_ERROR (Status)) {
659 return Status;
660 }
661
662 PciIo->FreeBuffer (
663 PciIo,
664 SNP_MEM_PAGES (4096),
665 Snp->Cpb
666 );
667
668 Error_DeleteSNP:
669
670 PciIo->FreeBuffer (
671 PciIo,
672 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
673 Snp
674 );
675 NiiError:
676 gBS->CloseProtocol (
677 Controller,
678 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
679 This->DriverBindingHandle,
680 Controller
681 );
682
683 gBS->CloseProtocol (
684 Controller,
685 &gEfiDevicePathProtocolGuid,
686 This->DriverBindingHandle,
687 Controller
688 );
689
690 //
691 // If we got here that means we are in error state.
692 //
693 if (!EFI_ERROR (Status)) {
694 Status = EFI_DEVICE_ERROR;
695 }
696
697 return Status;
698 }
699
700 /**
701 Stop this driver on ControllerHandle. This service is called by the
702 EFI boot service DisconnectController(). In order to
703 make drivers as small as possible, there are a few calling
704 restrictions for this service. DisconnectController()
705 must follow these calling restrictions. If any other agent wishes
706 to call Stop() it must also follow these calling restrictions.
707
708 @param This Protocol instance pointer.
709 @param ControllerHandle Handle of device to stop driver on
710 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
711 children is zero stop the entire bus driver.
712 @param ChildHandleBuffer List of Child Handles to Stop.
713
714 @retval EFI_SUCCESS This driver is removed ControllerHandle
715 @retval other This driver was not removed from this device
716
717 **/
718 EFI_STATUS
719 EFIAPI
720 SimpleNetworkDriverStop (
721 IN EFI_DRIVER_BINDING_PROTOCOL *This,
722 IN EFI_HANDLE Controller,
723 IN UINTN NumberOfChildren,
724 IN EFI_HANDLE *ChildHandleBuffer
725 )
726 {
727 EFI_STATUS Status;
728 EFI_SIMPLE_NETWORK_PROTOCOL *SnpProtocol;
729 SNP_DRIVER *Snp;
730 EFI_PCI_IO_PROTOCOL *PciIo;
731
732 //
733 // Get our context back.
734 //
735 Status = gBS->OpenProtocol (
736 Controller,
737 &gEfiSimpleNetworkProtocolGuid,
738 (VOID **) &SnpProtocol,
739 This->DriverBindingHandle,
740 Controller,
741 EFI_OPEN_PROTOCOL_GET_PROTOCOL
742 );
743
744 if (EFI_ERROR (Status)) {
745 return EFI_UNSUPPORTED;
746 }
747
748 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (SnpProtocol);
749
750 Status = gBS->UninstallProtocolInterface (
751 Controller,
752 &gEfiSimpleNetworkProtocolGuid,
753 &Snp->Snp
754 );
755
756 if (EFI_ERROR (Status)) {
757 return Status;
758 }
759
760 //
761 // Close EXIT_BOOT_SERIVES Event
762 //
763 gBS->CloseEvent (Snp->ExitBootServicesEvent);
764
765 Status = gBS->CloseProtocol (
766 Controller,
767 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
768 This->DriverBindingHandle,
769 Controller
770 );
771
772 Status = gBS->CloseProtocol (
773 Controller,
774 &gEfiDevicePathProtocolGuid,
775 This->DriverBindingHandle,
776 Controller
777 );
778
779 PxeShutdown (Snp);
780 PxeStop (Snp);
781
782 PciIo = Snp->PciIo;
783 PciIo->FreeBuffer (
784 PciIo,
785 SNP_MEM_PAGES (4096),
786 Snp->Cpb
787 );
788
789 PciIo->FreeBuffer (
790 PciIo,
791 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
792 Snp
793 );
794
795 return Status;
796 }
797
798 //
799 // Simple Network Protocol Driver Global Variables
800 //
801 EFI_DRIVER_BINDING_PROTOCOL gSimpleNetworkDriverBinding = {
802 SimpleNetworkDriverSupported,
803 SimpleNetworkDriverStart,
804 SimpleNetworkDriverStop,
805 0xa,
806 NULL,
807 NULL
808 };
809
810 /**
811 The SNP driver entry point.
812
813 @param ImageHandle The driver image handle.
814 @param SystemTable The system table.
815
816 @retval EFI_SUCEESS Initialization routine has found UNDI hardware,
817 loaded it's ROM, and installed a notify event for
818 the Network Indentifier Interface Protocol
819 successfully.
820 @retval Other Return value from HandleProtocol for
821 DeviceIoProtocol or LoadedImageProtocol
822
823 **/
824 EFI_STATUS
825 EFIAPI
826 InitializeSnpNiiDriver (
827 IN EFI_HANDLE ImageHandle,
828 IN EFI_SYSTEM_TABLE *SystemTable
829 )
830 {
831 return EfiLibInstallDriverBindingComponentName2 (
832 ImageHandle,
833 SystemTable,
834 &gSimpleNetworkDriverBinding,
835 ImageHandle,
836 &gSimpleNetworkComponentName,
837 &gSimpleNetworkComponentName2
838 );
839 }