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