]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/Snp.c
Changed ID into Id.
[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 - 2009, Intel Corporation. <BR>
5 All rights reserved. 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) {
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 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_STATION_ADDR_SETTABLE) != 0) {
585 Snp->Mode.MacAddressChangeable = TRUE;
586 } else {
587 Snp->Mode.MacAddressChangeable = FALSE;
588 }
589
590 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_MULTI_FRAME_SUPPORTED) != 0) {
591 Snp->Mode.MultipleTxSupported = TRUE;
592 } else {
593 Snp->Mode.MultipleTxSupported = FALSE;
594 }
595
596 Snp->Mode.ReceiveFilterMask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
597
598 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) != 0) {
599 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
600
601 }
602
603 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED) != 0) {
604 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
605
606 }
607
608 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED) != 0) {
609 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
610
611 }
612
613 if ((Pxe->hw.Implementation & PXE_ROMID_IMP_FILTERED_MULTICAST_RX_SUPPORTED) != 0) {
614 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
615
616 }
617
618 if (Pxe->hw.Implementation & PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED) {
619 Snp->Mode.ReceiveFilterMask |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
620
621 }
622
623 Snp->Mode.ReceiveFilterSetting = 0;
624
625 //
626 // need to get the station address to save in the mode structure. we need to
627 // initialize the UNDI first for this.
628 //
629 Snp->TxRxBufferSize = Snp->InitInfo.MemoryRequired;
630 Status = PxeInit (Snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);
631
632 if (EFI_ERROR (Status)) {
633 PxeStop (Snp);
634 goto Error_DeleteSNP;
635 }
636
637 Status = PxeGetStnAddr (Snp);
638
639 if (Status != EFI_SUCCESS) {
640 DEBUG ((EFI_D_ERROR, "\nSnp->undi.get_station_addr() failed.\n"));
641 PxeShutdown (Snp);
642 PxeStop (Snp);
643 goto Error_DeleteSNP;
644 }
645
646 Snp->Mode.MediaPresent = FALSE;
647
648 //
649 // We should not leave UNDI started and initialized here. this DriverStart()
650 // routine must only find and attach the SNP interface to UNDI layer that it
651 // finds on the given handle!
652 // The UNDI layer will be started when upper layers call Snp->start.
653 // How ever, this DriverStart() must fill up the snp mode structure which
654 // contains the MAC address of the NIC. For this reason we started and
655 // initialized UNDI here, now we are done, do a shutdown and stop of the
656 // UNDI interface!
657 //
658 PxeShutdown (Snp);
659 PxeStop (Snp);
660
661 //
662 // Create EXIT_BOOT_SERIVES Event
663 //
664 Status = gBS->CreateEventEx (
665 EVT_NOTIFY_SIGNAL,
666 TPL_NOTIFY,
667 SnpNotifyExitBootServices,
668 Snp,
669 &gEfiEventExitBootServicesGuid,
670 &Snp->ExitBootServicesEvent
671 );
672 if (EFI_ERROR (Status)) {
673 goto Error_DeleteSNP;
674 }
675
676 //
677 // add SNP to the undi handle
678 //
679 Status = gBS->InstallProtocolInterface (
680 &Controller,
681 &gEfiSimpleNetworkProtocolGuid,
682 EFI_NATIVE_INTERFACE,
683 &(Snp->Snp)
684 );
685
686 if (!EFI_ERROR (Status)) {
687 return Status;
688 }
689
690 Status = mPciIo->FreeBuffer (
691 mPciIo,
692 SNP_MEM_PAGES (4096),
693 Snp->Cpb
694 );
695
696 Error_DeleteSNP:
697
698 mPciIo->FreeBuffer (
699 mPciIo,
700 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
701 Snp
702 );
703 NiiError:
704 gBS->CloseProtocol (
705 Controller,
706 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
707 This->DriverBindingHandle,
708 Controller
709 );
710
711 gBS->CloseProtocol (
712 Controller,
713 &gEfiDevicePathProtocolGuid,
714 This->DriverBindingHandle,
715 Controller
716 );
717
718 return Status;
719 }
720
721 /**
722 Stop this driver on ControllerHandle. This service is called by the
723 EFI boot service DisconnectController(). In order to
724 make drivers as small as possible, there are a few calling
725 restrictions for this service. DisconnectController()
726 must follow these calling restrictions. If any other agent wishes
727 to call Stop() it must also follow these calling restrictions.
728
729 @param This Protocol instance pointer.
730 @param ControllerHandle Handle of device to stop driver on
731 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
732 children is zero stop the entire bus driver.
733 @param ChildHandleBuffer List of Child Handles to Stop.
734
735 @retval EFI_SUCCESS This driver is removed ControllerHandle
736 @retval other This driver was not removed from this device
737
738 **/
739 EFI_STATUS
740 EFIAPI
741 SimpleNetworkDriverStop (
742 IN EFI_DRIVER_BINDING_PROTOCOL *This,
743 IN EFI_HANDLE Controller,
744 IN UINTN NumberOfChildren,
745 IN EFI_HANDLE *ChildHandleBuffer
746 )
747 {
748 EFI_STATUS Status;
749 EFI_SIMPLE_NETWORK_PROTOCOL *SnpProtocol;
750 SNP_DRIVER *Snp;
751
752 //
753 // Get our context back.
754 //
755 Status = gBS->OpenProtocol (
756 Controller,
757 &gEfiSimpleNetworkProtocolGuid,
758 (VOID **) &SnpProtocol,
759 This->DriverBindingHandle,
760 Controller,
761 EFI_OPEN_PROTOCOL_GET_PROTOCOL
762 );
763
764 if (EFI_ERROR (Status)) {
765 return EFI_UNSUPPORTED;
766 }
767
768 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (SnpProtocol);
769
770 Status = gBS->UninstallProtocolInterface (
771 Controller,
772 &gEfiSimpleNetworkProtocolGuid,
773 &Snp->Snp
774 );
775
776 if (EFI_ERROR (Status)) {
777 return Status;
778 }
779
780 //
781 // Close EXIT_BOOT_SERIVES Event
782 //
783 gBS->CloseEvent (Snp->ExitBootServicesEvent);
784
785 Status = gBS->CloseProtocol (
786 Controller,
787 &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
788 This->DriverBindingHandle,
789 Controller
790 );
791
792 Status = gBS->CloseProtocol (
793 Controller,
794 &gEfiDevicePathProtocolGuid,
795 This->DriverBindingHandle,
796 Controller
797 );
798
799 PxeShutdown (Snp);
800 PxeStop (Snp);
801
802 mPciIo->FreeBuffer (
803 mPciIo,
804 SNP_MEM_PAGES (4096),
805 Snp->Cpb
806 );
807
808 mPciIo->FreeBuffer (
809 mPciIo,
810 SNP_MEM_PAGES (sizeof (SNP_DRIVER)),
811 Snp
812 );
813
814 return Status;
815 }
816
817 //
818 // Simple Network Protocol Driver Global Variables
819 //
820 EFI_DRIVER_BINDING_PROTOCOL mSimpleNetworkDriverBinding = {
821 SimpleNetworkDriverSupported,
822 SimpleNetworkDriverStart,
823 SimpleNetworkDriverStop,
824 0xa,
825 NULL,
826 NULL
827 };
828
829
830 /**
831 This routine maps the given CPU address to a Device address. It creates a
832 an entry in the map list with the virtual and physical addresses and the
833 un map cookie.
834
835 @param V2p pointer to return a map list node pointer.
836 @param Type the direction in which the data flows from the given
837 virtual address device->cpu or cpu->device or both
838 ways.
839 @param VirtualAddress virtual address (or CPU address) to be mapped.
840 @param BufferSize size of the buffer to be mapped.
841
842 @retval EFI_SUCEESS routine has completed the mapping.
843 @retval EFI_INVALID_PARAMETER invalid parameter.
844 @retval EFI_OUT_OF_RESOURCES out of resource.
845 @retval other error as indicated.
846
847 **/
848 EFI_STATUS
849 AddV2P (
850 IN OUT V2P **V2p,
851 EFI_PCI_IO_PROTOCOL_OPERATION Type,
852 VOID *VirtualAddress,
853 UINTN BufferSize
854 )
855 {
856 EFI_STATUS Status;
857
858 if ((V2p == NULL) || (VirtualAddress == NULL) || (BufferSize == 0)) {
859 return EFI_INVALID_PARAMETER;
860 }
861
862 *V2p = AllocatePool (sizeof (V2P));
863 if (*V2p != NULL) {
864 return EFI_OUT_OF_RESOURCES;
865 }
866
867 Status = mPciIo->Map (
868 mPciIo,
869 Type,
870 VirtualAddress,
871 &BufferSize,
872 &(*V2p)->PhysicalAddress,
873 &(*V2p)->Unmap
874 );
875 if (Status != EFI_SUCCESS) {
876 FreePool (*V2p);
877 return Status;
878 }
879 (*V2p)->VirtualAddress = VirtualAddress;
880 (*V2p)->BufferSize = BufferSize;
881 (*V2p)->Next = mV2p;
882 mV2p = *V2p;
883
884 return EFI_SUCCESS;
885 }
886
887
888 /**
889 This routine searches the linked list of mapped address nodes (for undi3.0
890 interface) to find the node that corresponds to the given virtual address and
891 returns a pointer to that node.
892
893 @param V2p pointer to return a map list node pointer.
894 @param VirtualAddr virtual address (or CPU address) to be searched in
895 the map list
896
897 @retval EFI_SUCEESS A match was found.
898 @retval Other A match cannot be found.
899
900 **/
901 EFI_STATUS
902 FindV2p (
903 V2P **V2p,
904 VOID *VirtualAddr
905 )
906 {
907 V2P *Ptr;
908
909 if (V2p == NULL || VirtualAddr == NULL) {
910 return EFI_INVALID_PARAMETER;
911 }
912
913 for (Ptr = mV2p; Ptr != NULL; Ptr = Ptr->Next) {
914 if (Ptr->VirtualAddress == VirtualAddr) {
915 *V2p = Ptr;
916 return EFI_SUCCESS;
917 }
918 }
919
920 return EFI_NOT_FOUND;
921 }
922
923
924 /**
925 Unmap the given virtual address and free the memory allocated for the map list
926 node corresponding to that address.
927
928 @param VirtualAddress virtual address (or CPU address) to be unmapped.
929
930 @retval EFI_SUCEESS Successfully unmapped.
931 @retval Other Other errors as indicated.
932
933 **/
934 EFI_STATUS
935 DelV2p (
936 VOID *VirtualAddress
937 )
938 {
939 V2P *Current;
940 V2P *Next;
941 EFI_STATUS Status;
942
943 if (VirtualAddress == NULL) {
944 return EFI_INVALID_PARAMETER;
945 }
946
947 if (mV2p == NULL) {
948 return EFI_NOT_FOUND;
949 }
950 //
951 // Is our node at the head of the list??
952 //
953 if ((Current = mV2p)->VirtualAddress == VirtualAddress) {
954 mV2p = mV2p->Next;
955
956 Status = mPciIo->Unmap (mPciIo, Current->Unmap);
957
958 FreePool (Current);
959
960 if (EFI_ERROR (Status)) {
961 DEBUG ((EFI_D_ERROR, "Unmap failed with status = %r\n", Status));
962 }
963 return Status;
964 }
965
966 for (; Current->Next != NULL; Current = Next) {
967 if ((Next = Current->Next)->VirtualAddress == VirtualAddress) {
968 Current->Next = Next->Next;
969 Status = mPciIo->Unmap (mPciIo, Next->Unmap);
970 FreePool (Next);
971
972 if (EFI_ERROR (Status)) {
973 DEBUG ((EFI_D_ERROR, "Unmap failed with status = %r\n", Status));
974 }
975 return Status;
976 }
977 }
978
979 return EFI_NOT_FOUND;
980 }
981
982 /**
983 The SNP driver entry point.
984
985 @param ImageHandle The driver image handle.
986 @param SystemTable The system table.
987
988 @retval EFI_SUCEESS Initialization routine has found UNDI hardware,
989 loaded it's ROM, and installed a notify event for
990 the Network Indentifier Interface Protocol
991 successfully.
992 @retval Other Return value from HandleProtocol for
993 DeviceIoProtocol or LoadedImageProtocol
994
995 **/
996 EFI_STATUS
997 EFIAPI
998 InitializeSnpNiiDriver (
999 IN EFI_HANDLE ImageHandle,
1000 IN EFI_SYSTEM_TABLE *SystemTable
1001 )
1002 {
1003 return EfiLibInstallDriverBindingComponentName2 (
1004 ImageHandle,
1005 SystemTable,
1006 &mSimpleNetworkDriverBinding,
1007 NULL,
1008 &gSimpleNetworkComponentName,
1009 &gSimpleNetworkComponentName2
1010 );
1011 }