]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpConfig.c
115185afe246b4150796659a7ad5dd115f8d6260
[mirror_edk2.git] / MdeModulePkg / Universal / Network / MnpDxe / MnpConfig.c
1 /** @file
2 Implementation of Managed Network Protocol private services.
3
4 Copyright (c) 2005 - 2010, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The full
8 text of the license may be found at<BR>
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "MnpImpl.h"
17 #include "MnpVlan.h"
18
19 EFI_SERVICE_BINDING_PROTOCOL mMnpServiceBindingProtocol = {
20 MnpServiceBindingCreateChild,
21 MnpServiceBindingDestroyChild
22 };
23
24 EFI_MANAGED_NETWORK_PROTOCOL mMnpProtocolTemplate = {
25 MnpGetModeData,
26 MnpConfigure,
27 MnpMcastIpToMac,
28 MnpGroups,
29 MnpTransmit,
30 MnpReceive,
31 MnpCancel,
32 MnpPoll
33 };
34
35 EFI_MANAGED_NETWORK_CONFIG_DATA mMnpDefaultConfigData = {
36 10000000,
37 10000000,
38 0,
39 FALSE,
40 FALSE,
41 FALSE,
42 FALSE,
43 FALSE,
44 FALSE,
45 FALSE
46 };
47
48
49 /**
50 Add Count of net buffers to MnpDeviceData->FreeNbufQue. The length of the net
51 buffer is specified by MnpDeviceData->BufferLength.
52
53 @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
54 @param[in] Count Number of NET_BUFFERs to add.
55
56 @retval EFI_SUCCESS The specified amount of NET_BUFs are allocated
57 and added to MnpDeviceData->FreeNbufQue.
58 @retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
59
60 **/
61 EFI_STATUS
62 MnpAddFreeNbuf (
63 IN OUT MNP_DEVICE_DATA *MnpDeviceData,
64 IN UINTN Count
65 )
66 {
67 EFI_STATUS Status;
68 UINTN Index;
69 NET_BUF *Nbuf;
70
71 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
72 ASSERT ((Count > 0) && (MnpDeviceData->BufferLength > 0));
73
74 Status = EFI_SUCCESS;
75 for (Index = 0; Index < Count; Index++) {
76 Nbuf = NetbufAlloc (MnpDeviceData->BufferLength + MnpDeviceData->PaddingSize);
77 if (Nbuf == NULL) {
78 DEBUG ((EFI_D_ERROR, "MnpAddFreeNbuf: NetBufAlloc failed.\n"));
79
80 Status = EFI_OUT_OF_RESOURCES;
81 break;
82 }
83
84 if (MnpDeviceData->PaddingSize > 0) {
85 //
86 // Pad padding bytes before the media header
87 //
88 NetbufAllocSpace (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_TAIL);
89 NetbufTrim (Nbuf, MnpDeviceData->PaddingSize, NET_BUF_HEAD);
90 }
91
92 NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf);
93 }
94
95 MnpDeviceData->NbufCnt += Index;
96 return Status;
97 }
98
99
100 /**
101 Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none
102 in the queue, first try to allocate some and add them into the queue, then
103 fetch the NET_BUF from the updated FreeNbufQue.
104
105 @param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
106
107 @return Pointer to the allocated free NET_BUF structure, if NULL the
108 operation is failed.
109
110 **/
111 NET_BUF *
112 MnpAllocNbuf (
113 IN OUT MNP_DEVICE_DATA *MnpDeviceData
114 )
115 {
116 EFI_STATUS Status;
117 NET_BUF_QUEUE *FreeNbufQue;
118 NET_BUF *Nbuf;
119 EFI_TPL OldTpl;
120
121 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
122
123 FreeNbufQue = &MnpDeviceData->FreeNbufQue;
124 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
125
126 //
127 // Check whether there are available buffers, or else try to add some.
128 //
129 if (FreeNbufQue->BufNum == 0) {
130 if ((MnpDeviceData->NbufCnt + MNP_NET_BUFFER_INCREASEMENT) > MNP_MAX_NET_BUFFER_NUM) {
131 DEBUG (
132 (EFI_D_ERROR,
133 "MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
134 MnpDeviceData)
135 );
136
137 Nbuf = NULL;
138 goto ON_EXIT;
139 }
140
141 Status = MnpAddFreeNbuf (MnpDeviceData, MNP_NET_BUFFER_INCREASEMENT);
142 if (EFI_ERROR (Status)) {
143 DEBUG (
144 (EFI_D_ERROR,
145 "MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
146 Status)
147 );
148
149 //
150 // Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
151 // the amount is less than MNP_NET_BUFFER_INCREASEMENT.
152 //
153 }
154 }
155
156 Nbuf = NetbufQueRemove (FreeNbufQue);
157
158 //
159 // Increase the RefCnt.
160 //
161 if (Nbuf != NULL) {
162 NET_GET_REF (Nbuf);
163 }
164
165 ON_EXIT:
166 gBS->RestoreTPL (OldTpl);
167
168 return Nbuf;
169 }
170
171
172 /**
173 Try to reclaim the Nbuf into the buffer pool.
174
175 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
176 @param[in, out] Nbuf Pointer to the NET_BUF to free.
177
178 **/
179 VOID
180 MnpFreeNbuf (
181 IN OUT MNP_DEVICE_DATA *MnpDeviceData,
182 IN OUT NET_BUF *Nbuf
183 )
184 {
185 EFI_TPL OldTpl;
186
187 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
188 ASSERT (Nbuf->RefCnt > 1);
189
190 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
191
192 NET_PUT_REF (Nbuf);
193
194 if (Nbuf->RefCnt == 1) {
195 //
196 // Trim all buffer contained in the Nbuf, then append it to the NbufQue.
197 //
198 NetbufTrim (Nbuf, Nbuf->TotalSize, NET_BUF_TAIL);
199
200 if (NetbufAllocSpace (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_HEAD) != NULL) {
201 //
202 // There is space reserved for vlan tag in the head, reclaim it
203 //
204 NetbufTrim (Nbuf, NET_VLAN_TAG_LEN, NET_BUF_TAIL);
205 }
206
207 NetbufQueAppend (&MnpDeviceData->FreeNbufQue, Nbuf);
208 }
209
210 gBS->RestoreTPL (OldTpl);
211 }
212
213
214 /**
215 Initialize the mnp device context data.
216
217 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
218 @param[in] ImageHandle The driver image handle.
219 @param[in] ControllerHandle Handle of device to bind driver to.
220
221 @retval EFI_SUCCESS The mnp service context is initialized.
222 @retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol.
223 @retval Others Other errors as indicated.
224
225 **/
226 EFI_STATUS
227 MnpInitializeDeviceData (
228 IN OUT MNP_DEVICE_DATA *MnpDeviceData,
229 IN EFI_HANDLE ImageHandle,
230 IN EFI_HANDLE ControllerHandle
231 )
232 {
233 EFI_STATUS Status;
234 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
235 EFI_SIMPLE_NETWORK_MODE *SnpMode;
236
237 MnpDeviceData->Signature = MNP_DEVICE_DATA_SIGNATURE;
238 MnpDeviceData->ImageHandle = ImageHandle;
239 MnpDeviceData->ControllerHandle = ControllerHandle;
240
241 //
242 // Copy the MNP Protocol interfaces from the template.
243 //
244 CopyMem (&MnpDeviceData->VlanConfig, &mVlanConfigProtocolTemplate, sizeof (EFI_VLAN_CONFIG_PROTOCOL));
245
246 //
247 // Open the Simple Network protocol.
248 //
249 Status = gBS->OpenProtocol (
250 ControllerHandle,
251 &gEfiSimpleNetworkProtocolGuid,
252 (VOID **) &Snp,
253 ImageHandle,
254 ControllerHandle,
255 EFI_OPEN_PROTOCOL_BY_DRIVER
256 );
257 if (EFI_ERROR (Status)) {
258 return EFI_UNSUPPORTED;
259 }
260
261 //
262 // Get MTU from Snp.
263 //
264 SnpMode = Snp->Mode;
265 MnpDeviceData->Snp = Snp;
266
267 //
268 // Initialize the lists.
269 //
270 InitializeListHead (&MnpDeviceData->ServiceList);
271 InitializeListHead (&MnpDeviceData->GroupAddressList);
272
273 //
274 // Get the buffer length used to allocate NET_BUF to hold data received
275 // from SNP. Do this before fill the FreeNetBufQue.
276 //
277 //
278 MnpDeviceData->BufferLength = SnpMode->MediaHeaderSize + NET_VLAN_TAG_LEN + SnpMode->MaxPacketSize + NET_ETHER_FCS_SIZE;
279
280 //
281 // Make sure the protocol headers immediately following the media header
282 // 4-byte aligned, and also preserve additional space for VLAN tag
283 //
284 MnpDeviceData->PaddingSize = ((4 - SnpMode->MediaHeaderSize) & 0x3) + NET_VLAN_TAG_LEN;
285
286 //
287 // Initialize MAC string which will be used as VLAN configuration variable name
288 //
289 Status = NetLibGetMacString (ControllerHandle, ImageHandle, &MnpDeviceData->MacString);
290 if (EFI_ERROR (Status)) {
291 goto ERROR;
292 }
293
294 //
295 // Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
296 //
297 NetbufQueInit (&MnpDeviceData->FreeNbufQue);
298 Status = MnpAddFreeNbuf (MnpDeviceData, MNP_INIT_NET_BUFFER_NUM);
299 if (EFI_ERROR (Status)) {
300 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: MnpAddFreeNbuf failed, %r.\n", Status));
301
302 goto ERROR;
303 }
304
305 //
306 // Get one NET_BUF from the FreeNbufQue for rx cache.
307 //
308 MnpDeviceData->RxNbufCache = MnpAllocNbuf (MnpDeviceData);
309 NetbufAllocSpace (
310 MnpDeviceData->RxNbufCache,
311 MnpDeviceData->BufferLength,
312 NET_BUF_TAIL
313 );
314
315 //
316 // Allocate buffer pool for tx.
317 //
318 MnpDeviceData->TxBuf = AllocatePool (MnpDeviceData->BufferLength);
319 if (MnpDeviceData->TxBuf == NULL) {
320 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: AllocatePool failed.\n"));
321
322 Status = EFI_OUT_OF_RESOURCES;
323 goto ERROR;
324 }
325
326 //
327 // Create the system poll timer.
328 //
329 Status = gBS->CreateEvent (
330 EVT_NOTIFY_SIGNAL | EVT_TIMER,
331 TPL_CALLBACK,
332 MnpSystemPoll,
333 MnpDeviceData,
334 &MnpDeviceData->PollTimer
335 );
336 if (EFI_ERROR (Status)) {
337 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for poll timer failed.\n"));
338
339 goto ERROR;
340 }
341
342 //
343 // Create the timer for packet timeout check.
344 //
345 Status = gBS->CreateEvent (
346 EVT_NOTIFY_SIGNAL | EVT_TIMER,
347 TPL_CALLBACK,
348 MnpCheckPacketTimeout,
349 MnpDeviceData,
350 &MnpDeviceData->TimeoutCheckTimer
351 );
352 if (EFI_ERROR (Status)) {
353 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for packet timeout check failed.\n"));
354
355 goto ERROR;
356 }
357
358 //
359 // Create the timer for media detection.
360 //
361 Status = gBS->CreateEvent (
362 EVT_NOTIFY_SIGNAL | EVT_TIMER,
363 TPL_CALLBACK,
364 MnpCheckMediaStatus,
365 MnpDeviceData,
366 &MnpDeviceData->MediaDetectTimer
367 );
368 if (EFI_ERROR (Status)) {
369 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for media detection failed.\n"));
370
371 goto ERROR;
372 }
373
374 //
375 // Create the timer for tx timeout check.
376 //
377 Status = gBS->CreateEvent (
378 EVT_TIMER,
379 TPL_CALLBACK,
380 NULL,
381 NULL,
382 &MnpDeviceData->TxTimeoutEvent
383 );
384 if (EFI_ERROR (Status)) {
385 DEBUG ((EFI_D_ERROR, "MnpInitializeDeviceData: CreateEvent for tx timeout event failed.\n"));
386 }
387
388 ERROR:
389 if (EFI_ERROR (Status)) {
390 //
391 // Free the dynamic allocated resources if necessary.
392 //
393 if (MnpDeviceData->MacString != NULL) {
394 FreePool (MnpDeviceData->MacString);
395 }
396
397 if (MnpDeviceData->TimeoutCheckTimer != NULL) {
398 gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer);
399 }
400
401 if (MnpDeviceData->MediaDetectTimer != NULL) {
402 gBS->CloseEvent (MnpDeviceData->MediaDetectTimer);
403 }
404
405 if (MnpDeviceData->PollTimer != NULL) {
406 gBS->CloseEvent (MnpDeviceData->PollTimer);
407 }
408
409 if (MnpDeviceData->TxBuf != NULL) {
410 FreePool (MnpDeviceData->TxBuf);
411 }
412
413 if (MnpDeviceData->RxNbufCache != NULL) {
414 MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache);
415 }
416
417 if (MnpDeviceData->FreeNbufQue.BufNum != 0) {
418 NetbufQueFlush (&MnpDeviceData->FreeNbufQue);
419 }
420
421 //
422 // Close the Simple Network Protocol.
423 //
424 gBS->CloseProtocol (
425 ControllerHandle,
426 &gEfiSimpleNetworkProtocolGuid,
427 ImageHandle,
428 ControllerHandle
429 );
430 }
431
432 return Status;
433 }
434
435
436 /**
437 Destroy the MNP device context data.
438
439 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
440 @param[in] ImageHandle The driver image handle.
441
442 **/
443 VOID
444 MnpDestroyDeviceData (
445 IN OUT MNP_DEVICE_DATA *MnpDeviceData,
446 IN EFI_HANDLE ImageHandle
447 )
448 {
449 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
450
451 //
452 // Free Vlan Config variable name string
453 //
454 if (MnpDeviceData->MacString != NULL) {
455 FreePool (MnpDeviceData->MacString);
456 }
457
458 //
459 // The GroupAddressList must be empty.
460 //
461 ASSERT (IsListEmpty (&MnpDeviceData->GroupAddressList));
462
463 //
464 // Close the event.
465 //
466 gBS->CloseEvent (MnpDeviceData->TxTimeoutEvent);
467 gBS->CloseEvent (MnpDeviceData->TimeoutCheckTimer);
468 gBS->CloseEvent (MnpDeviceData->MediaDetectTimer);
469 gBS->CloseEvent (MnpDeviceData->PollTimer);
470
471 //
472 // Free the tx buffer.
473 //
474 FreePool (MnpDeviceData->TxBuf);
475
476 //
477 // Free the RxNbufCache.
478 //
479 MnpFreeNbuf (MnpDeviceData, MnpDeviceData->RxNbufCache);
480
481 //
482 // Flush the FreeNbufQue.
483 //
484 MnpDeviceData->NbufCnt -= MnpDeviceData->FreeNbufQue.BufNum;
485 NetbufQueFlush (&MnpDeviceData->FreeNbufQue);
486
487 //
488 // Close the Simple Network Protocol.
489 //
490 gBS->CloseProtocol (
491 MnpDeviceData->ControllerHandle,
492 &gEfiSimpleNetworkProtocolGuid,
493 ImageHandle,
494 MnpDeviceData->ControllerHandle
495 );
496 }
497
498
499 /**
500 Create mnp service context data.
501
502 @param[in] MnpDeviceData Pointer to the mnp device context data.
503 @param[in] VlanId The VLAN ID.
504 @param[in] Priority The VLAN priority. If VlanId is 0,
505 Priority is ignored.
506
507 @return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context.
508
509 **/
510 MNP_SERVICE_DATA *
511 MnpCreateServiceData (
512 IN MNP_DEVICE_DATA *MnpDeviceData,
513 IN UINT16 VlanId,
514 IN UINT8 Priority OPTIONAL
515 )
516 {
517 EFI_HANDLE MnpServiceHandle;
518 MNP_SERVICE_DATA *MnpServiceData;
519 EFI_STATUS Status;
520 EFI_SIMPLE_NETWORK_MODE *SnpMode;
521 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
522
523 //
524 // Initialize the Mnp Service Data.
525 //
526 MnpServiceData = AllocateZeroPool (sizeof (MNP_SERVICE_DATA));
527 if (MnpServiceData == NULL) {
528 DEBUG ((EFI_D_ERROR, "MnpCreateServiceData: Faild to allocate memory for the new Mnp Service Data.\n"));
529
530 return NULL;
531 }
532
533 //
534 // Add to MNP service list
535 //
536 InsertTailList (&MnpDeviceData->ServiceList, &MnpServiceData->Link);
537
538 MnpServiceData->Signature = MNP_SERVICE_DATA_SIGNATURE;
539 MnpServiceData->MnpDeviceData = MnpDeviceData;
540
541 //
542 // Copy the ServiceBinding structure.
543 //
544 CopyMem (&MnpServiceData->ServiceBinding, &mMnpServiceBindingProtocol, sizeof (EFI_SERVICE_BINDING_PROTOCOL));
545
546 //
547 // Initialize the lists.
548 //
549 InitializeListHead (&MnpServiceData->ChildrenList);
550
551 SnpMode = MnpDeviceData->Snp->Mode;
552 if (VlanId != 0) {
553 //
554 // Create VLAN child handle
555 //
556 MnpServiceHandle = MnpCreateVlanChild (
557 MnpDeviceData->ImageHandle,
558 MnpDeviceData->ControllerHandle,
559 VlanId,
560 &MnpServiceData->DevicePath
561 );
562 if (MnpServiceHandle == NULL) {
563 DEBUG ((EFI_D_ERROR, "MnpCreateServiceData: Faild to create child handle.\n"));
564
565 return NULL;
566 }
567
568 //
569 // Open VLAN Config Protocol by child
570 //
571 Status = gBS->OpenProtocol (
572 MnpDeviceData->ControllerHandle,
573 &gEfiVlanConfigProtocolGuid,
574 (VOID **) &VlanConfig,
575 MnpDeviceData->ImageHandle,
576 MnpServiceHandle,
577 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
578 );
579 if (EFI_ERROR (Status)) {
580 goto Exit;
581 }
582
583 //
584 // Reduce MTU for VLAN device
585 //
586 MnpServiceData->Mtu = SnpMode->MaxPacketSize - NET_VLAN_TAG_LEN;
587 } else {
588 //
589 // VlanId set to 0 means rx/tx untagged frame
590 //
591 MnpServiceHandle = MnpDeviceData->ControllerHandle;
592 MnpServiceData->Mtu = SnpMode->MaxPacketSize;
593 }
594
595 MnpServiceData->ServiceHandle = MnpServiceHandle;
596 MnpServiceData->VlanId = VlanId;
597 MnpServiceData->Priority = Priority;
598
599 //
600 // Install the MNP Service Binding Protocol
601 //
602 Status = gBS->InstallMultipleProtocolInterfaces (
603 &MnpServiceHandle,
604 &gEfiManagedNetworkServiceBindingProtocolGuid,
605 &MnpServiceData->ServiceBinding,
606 NULL
607 );
608
609 Exit:
610 if (EFI_ERROR (Status)) {
611 MnpDestroyServiceData (MnpServiceData);
612 MnpServiceData = NULL;
613 }
614
615 return MnpServiceData;
616 }
617
618 /**
619 Destroy the MNP service context data.
620
621 @param[in, out] MnpServiceData Pointer to the mnp service context data.
622
623 @retval EFI_SUCCESS The mnp service context is destroyed.
624 @retval Others Errors as indicated.
625
626 **/
627 EFI_STATUS
628 MnpDestroyServiceData (
629 IN OUT MNP_SERVICE_DATA *MnpServiceData
630 )
631 {
632 EFI_STATUS Status;
633
634 //
635 // Uninstall the MNP Service Binding Protocol
636 //
637 Status = gBS->UninstallMultipleProtocolInterfaces (
638 MnpServiceData->ServiceHandle,
639 &gEfiManagedNetworkServiceBindingProtocolGuid,
640 &MnpServiceData->ServiceBinding,
641 NULL
642 );
643 if (EFI_ERROR (Status)) {
644 return Status;
645 }
646
647 if (MnpServiceData->VlanId != 0) {
648 //
649 // Close VlanConfig Protocol opened by VLAN child handle
650 //
651 Status = gBS->CloseProtocol (
652 MnpServiceData->MnpDeviceData->ControllerHandle,
653 &gEfiVlanConfigProtocolGuid,
654 MnpServiceData->MnpDeviceData->ImageHandle,
655 MnpServiceData->ServiceHandle
656 );
657 if (EFI_ERROR (Status)) {
658 return Status;
659 }
660
661 //
662 // Uninstall Device Path Protocol to destroy the VLAN child handle
663 //
664 Status = gBS->UninstallMultipleProtocolInterfaces (
665 MnpServiceData->ServiceHandle,
666 &gEfiDevicePathProtocolGuid,
667 MnpServiceData->DevicePath,
668 NULL
669 );
670 if (EFI_ERROR (Status)) {
671 return Status;
672 }
673
674 if (MnpServiceData->DevicePath != NULL) {
675 FreePool (MnpServiceData->DevicePath);
676 }
677 }
678
679 //
680 // Remove from MnpDeviceData service list
681 //
682 RemoveEntryList (&MnpServiceData->Link);
683
684 FreePool (MnpServiceData);
685
686 return Status;
687 }
688
689 /**
690 Destroy all child of the MNP service data.
691
692 @param[in, out] MnpServiceData Pointer to the mnp service context data.
693
694 @retval EFI_SUCCESS All child are destroyed.
695 @retval Others Failed to destroy all child.
696
697 **/
698 EFI_STATUS
699 MnpDestroyServiceChild (
700 IN OUT MNP_SERVICE_DATA *MnpServiceData
701 )
702 {
703 EFI_STATUS Status;
704 MNP_INSTANCE_DATA *Instance;
705 EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
706
707 ServiceBinding = &MnpServiceData->ServiceBinding;
708 while (!IsListEmpty (&MnpServiceData->ChildrenList)) {
709 //
710 // Don't use NetListRemoveHead here, the remove opreration will be done
711 // in ServiceBindingDestroyChild.
712 //
713 Instance = NET_LIST_HEAD (
714 &MnpServiceData->ChildrenList,
715 MNP_INSTANCE_DATA,
716 InstEntry
717 );
718
719 Status = ServiceBinding->DestroyChild (ServiceBinding, Instance->Handle);
720 if (EFI_ERROR (Status)) {
721 return Status;
722 }
723 }
724
725 return EFI_SUCCESS;
726 }
727
728 /**
729 Find the MNP Service Data for given VLAN ID.
730
731 @param[in] MnpDeviceData Pointer to the mnp device context data.
732 @param[in] VlanId The VLAN ID.
733
734 @return A pointer to MNP_SERVICE_DATA or NULL if not found.
735
736 **/
737 MNP_SERVICE_DATA *
738 MnpFindServiceData (
739 IN MNP_DEVICE_DATA *MnpDeviceData,
740 IN UINT16 VlanId
741 )
742 {
743 LIST_ENTRY *Entry;
744 MNP_SERVICE_DATA *MnpServiceData;
745
746 NET_LIST_FOR_EACH (Entry, &MnpDeviceData->ServiceList) {
747 //
748 // Check VLAN ID of each Mnp Service Data
749 //
750 MnpServiceData = MNP_SERVICE_DATA_FROM_LINK (Entry);
751 if (MnpServiceData->VlanId == VlanId) {
752 return MnpServiceData;
753 }
754 }
755
756 return NULL;
757 }
758
759 /**
760 Initialize the mnp instance context data.
761
762 @param[in] MnpServiceData Pointer to the mnp service context data.
763 @param[in, out] Instance Pointer to the mnp instance context data
764 to initialize.
765
766 **/
767 VOID
768 MnpInitializeInstanceData (
769 IN MNP_SERVICE_DATA *MnpServiceData,
770 IN OUT MNP_INSTANCE_DATA *Instance
771 )
772 {
773 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
774 ASSERT (Instance != NULL);
775
776 //
777 // Set the signature.
778 //
779 Instance->Signature = MNP_INSTANCE_DATA_SIGNATURE;
780
781 //
782 // Copy the MNP Protocol interfaces from the template.
783 //
784 CopyMem (&Instance->ManagedNetwork, &mMnpProtocolTemplate, sizeof (Instance->ManagedNetwork));
785
786 //
787 // Copy the default config data.
788 //
789 CopyMem (&Instance->ConfigData, &mMnpDefaultConfigData, sizeof (Instance->ConfigData));
790
791 //
792 // Initialize the lists.
793 //
794 InitializeListHead (&Instance->GroupCtrlBlkList);
795 InitializeListHead (&Instance->RcvdPacketQueue);
796 InitializeListHead (&Instance->RxDeliveredPacketQueue);
797
798 //
799 // Initialize the RxToken Map.
800 //
801 NetMapInit (&Instance->RxTokenMap);
802
803 //
804 // Save the MnpServiceData info.
805 //
806 Instance->MnpServiceData = MnpServiceData;
807 }
808
809
810 /**
811 Check whether the token specified by Arg matches the token in Item.
812
813 @param[in] Map Pointer to the NET_MAP.
814 @param[in] Item Pointer to the NET_MAP_ITEM.
815 @param[in] Arg Pointer to the Arg, it's a pointer to the token to
816 check.
817
818 @retval EFI_SUCCESS The token specified by Arg is different from the
819 token in Item.
820 @retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
821 Item.
822
823 **/
824 EFI_STATUS
825 MnpTokenExist (
826 IN NET_MAP *Map,
827 IN NET_MAP_ITEM *Item,
828 IN VOID *Arg
829 )
830 {
831 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token;
832 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenInItem;
833
834 Token = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Arg;
835 TokenInItem = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key;
836
837 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
838 //
839 // The token is the same either the two tokens equals or the Events in
840 // the two tokens are the same.
841 //
842 return EFI_ACCESS_DENIED;
843 }
844
845 return EFI_SUCCESS;
846 }
847
848 /**
849 Cancel the token specified by Arg if it matches the token in Item.
850
851 @param[in, out] Map Pointer to the NET_MAP.
852 @param[in, out] Item Pointer to the NET_MAP_ITEM.
853 @param[in] Arg Pointer to the Arg, it's a pointer to the
854 token to cancel.
855
856 @retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled,
857 or the Arg isn't NULL, and the token in Item is
858 different from the Arg.
859 @retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
860 Arg, and the token is cancelled.
861
862 **/
863 EFI_STATUS
864 MnpCancelTokens (
865 IN OUT NET_MAP *Map,
866 IN OUT NET_MAP_ITEM *Item,
867 IN VOID *Arg
868 )
869 {
870 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *TokenToCancel;
871
872 if ((Arg != NULL) && (Item->Key != Arg)) {
873 //
874 // The token in Item is not the token specified by Arg.
875 //
876 return EFI_SUCCESS;
877 }
878
879 TokenToCancel = (EFI_MANAGED_NETWORK_COMPLETION_TOKEN *) Item->Key;
880
881 //
882 // Remove the item from the map.
883 //
884 NetMapRemoveItem (Map, Item, NULL);
885
886 //
887 // Cancel this token with status set to EFI_ABORTED.
888 //
889 TokenToCancel->Status = EFI_ABORTED;
890 gBS->SignalEvent (TokenToCancel->Event);
891
892 if (Arg != NULL) {
893 //
894 // Only abort the token specified by Arg if Arg isn't NULL.
895 //
896 return EFI_ABORTED;
897 }
898
899 return EFI_SUCCESS;
900 }
901
902
903 /**
904 Start and initialize the simple network.
905
906 @param[in] Snp Pointer to the simple network protocol.
907
908 @retval EFI_SUCCESS The simple network protocol is started.
909 @retval Others Other errors as indicated.
910
911 **/
912 EFI_STATUS
913 MnpStartSnp (
914 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
915 )
916 {
917 EFI_STATUS Status;
918
919 ASSERT (Snp != NULL);
920
921 //
922 // Start the simple network.
923 //
924 Status = Snp->Start (Snp);
925
926 if (!EFI_ERROR (Status)) {
927 //
928 // Initialize the simple network.
929 //
930 Status = Snp->Initialize (Snp, 0, 0);
931 }
932
933 return Status;
934 }
935
936
937 /**
938 Stop the simple network.
939
940 @param[in] Snp Pointer to the simple network protocol.
941
942 @retval EFI_SUCCESS The simple network is stopped.
943 @retval Others Other errors as indicated.
944
945 **/
946 EFI_STATUS
947 MnpStopSnp (
948 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
949 )
950 {
951 EFI_STATUS Status;
952
953 ASSERT (Snp != NULL);
954
955 //
956 // Shut down the simple network.
957 //
958 Status = Snp->Shutdown (Snp);
959 if (!EFI_ERROR (Status)) {
960 //
961 // Stop the simple network.
962 //
963 Status = Snp->Stop (Snp);
964 }
965
966 return Status;
967 }
968
969
970 /**
971 Start the managed network, this function is called when one instance is configured
972 or reconfigured.
973
974 @param[in, out] MnpServiceData Pointer to the mnp service context data.
975 @param[in] IsConfigUpdate The instance is reconfigured or it's the first
976 time the instanced is configured.
977 @param[in] EnableSystemPoll Enable the system polling or not.
978
979 @retval EFI_SUCCESS The managed network is started and some
980 configuration is updated.
981 @retval Others Other errors as indicated.
982
983 **/
984 EFI_STATUS
985 MnpStart (
986 IN OUT MNP_SERVICE_DATA *MnpServiceData,
987 IN BOOLEAN IsConfigUpdate,
988 IN BOOLEAN EnableSystemPoll
989 )
990 {
991 EFI_STATUS Status;
992 EFI_TIMER_DELAY TimerOpType;
993 MNP_DEVICE_DATA *MnpDeviceData;
994
995 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
996
997 Status = EFI_SUCCESS;
998 MnpDeviceData = MnpServiceData->MnpDeviceData;
999
1000 if (!IsConfigUpdate) {
1001 //
1002 // If it's not a configuration update, increase the configured children number.
1003 //
1004 MnpDeviceData->ConfiguredChildrenNumber++;
1005
1006 if (MnpDeviceData->ConfiguredChildrenNumber == 1) {
1007 //
1008 // It's the first configured child, start the simple network.
1009 //
1010 Status = MnpStartSnp (MnpDeviceData->Snp);
1011 if (EFI_ERROR (Status)) {
1012 DEBUG ((EFI_D_ERROR, "MnpStart: MnpStartSnp failed, %r.\n", Status));
1013
1014 goto ErrorExit;
1015 }
1016
1017 //
1018 // Start the timeout timer.
1019 //
1020 Status = gBS->SetTimer (
1021 MnpDeviceData->TimeoutCheckTimer,
1022 TimerPeriodic,
1023 MNP_TIMEOUT_CHECK_INTERVAL
1024 );
1025 if (EFI_ERROR (Status)) {
1026 DEBUG (
1027 (EFI_D_ERROR,
1028 "MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
1029 Status)
1030 );
1031
1032 goto ErrorExit;
1033 }
1034
1035 //
1036 // Start the media detection timer.
1037 //
1038 Status = gBS->SetTimer (
1039 MnpDeviceData->MediaDetectTimer,
1040 TimerPeriodic,
1041 MNP_MEDIA_DETECT_INTERVAL
1042 );
1043 if (EFI_ERROR (Status)) {
1044 DEBUG (
1045 (EFI_D_ERROR,
1046 "MnpStart, gBS->SetTimer for MediaDetectTimer %r.\n",
1047 Status)
1048 );
1049
1050 goto ErrorExit;
1051 }
1052 }
1053 }
1054
1055 if (MnpDeviceData->EnableSystemPoll ^ EnableSystemPoll) {
1056 //
1057 // The EnableSystemPoll differs with the current state, disable or enable
1058 // the system poll.
1059 //
1060 TimerOpType = EnableSystemPoll ? TimerPeriodic : TimerCancel;
1061
1062 Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerOpType, MNP_SYS_POLL_INTERVAL);
1063 if (EFI_ERROR (Status)) {
1064 DEBUG ((EFI_D_ERROR, "MnpStart: gBS->SetTimer for PollTimer failed, %r.\n", Status));
1065
1066 goto ErrorExit;
1067 }
1068
1069 MnpDeviceData->EnableSystemPoll = EnableSystemPoll;
1070 }
1071
1072 //
1073 // Change the receive filters if need.
1074 //
1075 Status = MnpConfigReceiveFilters (MnpDeviceData);
1076
1077 ErrorExit:
1078 return Status;
1079 }
1080
1081
1082 /**
1083 Stop the managed network.
1084
1085 @param[in, out] MnpServiceData Pointer to the mnp service context data.
1086
1087 @retval EFI_SUCCESS The managed network is stopped.
1088 @retval Others Other errors as indicated.
1089
1090 **/
1091 EFI_STATUS
1092 MnpStop (
1093 IN OUT MNP_SERVICE_DATA *MnpServiceData
1094 )
1095 {
1096 EFI_STATUS Status;
1097 MNP_DEVICE_DATA *MnpDeviceData;
1098
1099 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1100 MnpDeviceData = MnpServiceData->MnpDeviceData;
1101 ASSERT (MnpDeviceData->ConfiguredChildrenNumber > 0);
1102
1103 //
1104 // Configure the receive filters.
1105 //
1106 MnpConfigReceiveFilters (MnpDeviceData);
1107
1108 //
1109 // Decrease the children number.
1110 //
1111 MnpDeviceData->ConfiguredChildrenNumber--;
1112
1113 if (MnpDeviceData->ConfiguredChildrenNumber > 0) {
1114 //
1115 // If there are other configured chilren, return and keep the timers and
1116 // simple network unchanged.
1117 //
1118 return EFI_SUCCESS;
1119 }
1120
1121 //
1122 // No configured children now.
1123 //
1124 if (MnpDeviceData->EnableSystemPoll) {
1125 //
1126 // The system poll in on, cancel the poll timer.
1127 //
1128 Status = gBS->SetTimer (MnpDeviceData->PollTimer, TimerCancel, 0);
1129 MnpDeviceData->EnableSystemPoll = FALSE;
1130 }
1131
1132 //
1133 // Cancel the timeout timer.
1134 //
1135 Status = gBS->SetTimer (MnpDeviceData->TimeoutCheckTimer, TimerCancel, 0);
1136
1137 //
1138 // Cancel the media detect timer.
1139 //
1140 Status = gBS->SetTimer (MnpDeviceData->MediaDetectTimer, TimerCancel, 0);
1141
1142 //
1143 // Stop the simple network.
1144 //
1145 Status = MnpStopSnp (MnpDeviceData->Snp);
1146 return Status;
1147 }
1148
1149
1150 /**
1151 Flush the instance's received data.
1152
1153 @param[in, out] Instance Pointer to the mnp instance context data.
1154
1155 **/
1156 VOID
1157 MnpFlushRcvdDataQueue (
1158 IN OUT MNP_INSTANCE_DATA *Instance
1159 )
1160 {
1161 EFI_TPL OldTpl;
1162 MNP_RXDATA_WRAP *RxDataWrap;
1163
1164 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1165
1166 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1167
1168 while (!IsListEmpty (&Instance->RcvdPacketQueue)) {
1169 //
1170 // Remove all the Wraps.
1171 //
1172 RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);
1173
1174 //
1175 // Recycle the RxDataWrap.
1176 //
1177 MnpRecycleRxData (NULL, (VOID *) RxDataWrap);
1178 Instance->RcvdPacketQueueSize--;
1179 }
1180
1181 ASSERT (Instance->RcvdPacketQueueSize == 0);
1182
1183 gBS->RestoreTPL (OldTpl);
1184 }
1185
1186
1187 /**
1188 Configure the Instance using ConfigData.
1189
1190 @param[in, out] Instance Pointer to the mnp instance context data.
1191 @param[in] ConfigData Pointer to the configuration data used to configure
1192 the isntance.
1193
1194 @retval EFI_SUCCESS The Instance is configured.
1195 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
1196 implementation doesn't support it.
1197 @retval Others Other errors as indicated.
1198
1199 **/
1200 EFI_STATUS
1201 MnpConfigureInstance (
1202 IN OUT MNP_INSTANCE_DATA *Instance,
1203 IN EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData OPTIONAL
1204 )
1205 {
1206 EFI_STATUS Status;
1207 MNP_SERVICE_DATA *MnpServiceData;
1208 MNP_DEVICE_DATA *MnpDeviceData;
1209 EFI_MANAGED_NETWORK_CONFIG_DATA *OldConfigData;
1210 EFI_MANAGED_NETWORK_CONFIG_DATA *NewConfigData;
1211 BOOLEAN IsConfigUpdate;
1212
1213 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1214
1215 if ((ConfigData != NULL) && ConfigData->EnableReceiveTimestamps) {
1216 //
1217 // Don't support timestamp.
1218 //
1219 return EFI_UNSUPPORTED;
1220 }
1221
1222 Status = EFI_SUCCESS;
1223
1224 MnpServiceData = Instance->MnpServiceData;
1225 MnpDeviceData = MnpServiceData->MnpDeviceData;
1226 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
1227
1228 IsConfigUpdate = (BOOLEAN) ((Instance->Configured) && (ConfigData != NULL));
1229
1230 OldConfigData = &Instance->ConfigData;
1231 NewConfigData = ConfigData;
1232 if (NewConfigData == NULL) {
1233 //
1234 // Restore back the default config data if a reset of this instance
1235 // is required.
1236 //
1237 NewConfigData = &mMnpDefaultConfigData;
1238 }
1239
1240 //
1241 // Reset the instance's receive filter.
1242 //
1243 Instance->ReceiveFilter = 0;
1244
1245 //
1246 // Clear the receive counters according to the old ConfigData.
1247 //
1248 if (OldConfigData->EnableUnicastReceive) {
1249 MnpDeviceData->UnicastCount--;
1250 }
1251
1252 if (OldConfigData->EnableMulticastReceive) {
1253 MnpDeviceData->MulticastCount--;
1254 }
1255
1256 if (OldConfigData->EnableBroadcastReceive) {
1257 MnpDeviceData->BroadcastCount--;
1258 }
1259
1260 if (OldConfigData->EnablePromiscuousReceive) {
1261 MnpDeviceData->PromiscuousCount--;
1262 }
1263
1264 //
1265 // Set the receive filter counters and the receive filter of the
1266 // instance according to the new ConfigData.
1267 //
1268 if (NewConfigData->EnableUnicastReceive) {
1269 MnpDeviceData->UnicastCount++;
1270 Instance->ReceiveFilter |= MNP_RECEIVE_UNICAST;
1271 }
1272
1273 if (NewConfigData->EnableMulticastReceive) {
1274 MnpDeviceData->MulticastCount++;
1275 }
1276
1277 if (NewConfigData->EnableBroadcastReceive) {
1278 MnpDeviceData->BroadcastCount++;
1279 Instance->ReceiveFilter |= MNP_RECEIVE_BROADCAST;
1280 }
1281
1282 if (NewConfigData->EnablePromiscuousReceive) {
1283 MnpDeviceData->PromiscuousCount++;
1284 }
1285
1286 if (OldConfigData->FlushQueuesOnReset) {
1287 MnpFlushRcvdDataQueue (Instance);
1288 }
1289
1290 if (ConfigData == NULL) {
1291 Instance->ManagedNetwork.Cancel (&Instance->ManagedNetwork, NULL);
1292 }
1293
1294 if (!NewConfigData->EnableMulticastReceive) {
1295 MnpGroupOp (Instance, FALSE, NULL, NULL);
1296 }
1297
1298 //
1299 // Save the new configuration data.
1300 //
1301 CopyMem (OldConfigData, NewConfigData, sizeof (*OldConfigData));
1302
1303 Instance->Configured = (BOOLEAN) (ConfigData != NULL);
1304 if (Instance->Configured) {
1305 //
1306 // The instance is configured, start the Mnp.
1307 //
1308 Status = MnpStart (
1309 MnpServiceData,
1310 IsConfigUpdate,
1311 (BOOLEAN) !NewConfigData->DisableBackgroundPolling
1312 );
1313 } else {
1314 //
1315 // The instance is changed to the unconfigured state, stop the Mnp.
1316 //
1317 Status = MnpStop (MnpServiceData);
1318 }
1319
1320 return Status;
1321 }
1322
1323 /**
1324 Configure the Snp receive filters according to the instances' receive filter
1325 settings.
1326
1327 @param[in] MnpDeviceData Pointer to the mnp device context data.
1328
1329 @retval EFI_SUCCESS The receive filters is configured.
1330 @retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due
1331 to lack of memory resource.
1332
1333 **/
1334 EFI_STATUS
1335 MnpConfigReceiveFilters (
1336 IN MNP_DEVICE_DATA *MnpDeviceData
1337 )
1338 {
1339 EFI_STATUS Status;
1340 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1341 EFI_MAC_ADDRESS *MCastFilter;
1342 UINT32 MCastFilterCnt;
1343 UINT32 EnableFilterBits;
1344 UINT32 DisableFilterBits;
1345 BOOLEAN ResetMCastFilters;
1346 LIST_ENTRY *Entry;
1347 UINT32 Index;
1348 MNP_GROUP_ADDRESS *GroupAddress;
1349
1350 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
1351
1352 Snp = MnpDeviceData->Snp;
1353
1354 //
1355 // Initialize the enable filter and disable filter.
1356 //
1357 EnableFilterBits = 0;
1358 DisableFilterBits = Snp->Mode->ReceiveFilterMask;
1359
1360 if (MnpDeviceData->UnicastCount != 0) {
1361 //
1362 // Enable unicast if any instance wants to receive unicast.
1363 //
1364 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
1365 }
1366
1367 if (MnpDeviceData->BroadcastCount != 0) {
1368 //
1369 // Enable broadcast if any instance wants to receive broadcast.
1370 //
1371 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
1372 }
1373
1374 MCastFilter = NULL;
1375 MCastFilterCnt = 0;
1376 ResetMCastFilters = TRUE;
1377
1378 if ((MnpDeviceData->MulticastCount != 0) && (MnpDeviceData->GroupAddressCount != 0)) {
1379 //
1380 // There are instances configured to receive multicast and already some group
1381 // addresses are joined.
1382 //
1383
1384 ResetMCastFilters = FALSE;
1385
1386 if (MnpDeviceData->GroupAddressCount <= Snp->Mode->MaxMCastFilterCount) {
1387 //
1388 // The joind group address is less than simple network's maximum count.
1389 // Just configure the snp to do the multicast filtering.
1390 //
1391
1392 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
1393
1394 //
1395 // Allocate pool for the mulicast addresses.
1396 //
1397 MCastFilterCnt = MnpDeviceData->GroupAddressCount;
1398 MCastFilter = AllocatePool (sizeof (EFI_MAC_ADDRESS) * MCastFilterCnt);
1399 if (MCastFilter == NULL) {
1400 DEBUG ((EFI_D_ERROR, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
1401
1402 return EFI_OUT_OF_RESOURCES;
1403 }
1404
1405 //
1406 // Fill the multicast HW address buffer.
1407 //
1408 Index = 0;
1409 NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) {
1410
1411 GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
1412 CopyMem (MCastFilter + Index, &GroupAddress->Address, sizeof (*(MCastFilter + Index)));
1413 Index++;
1414
1415 ASSERT (Index <= MCastFilterCnt);
1416 }
1417 } else {
1418 //
1419 // The maximum multicast is reached, set the filter to be promiscuous
1420 // multicast.
1421 //
1422
1423 if ((Snp->Mode->ReceiveFilterMask & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
1424 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
1425 } else {
1426 //
1427 // Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
1428 // set the NIC to be promiscuous although this will tremendously degrade
1429 // the performance.
1430 //
1431 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
1432 }
1433 }
1434 }
1435
1436 if (MnpDeviceData->PromiscuousCount != 0) {
1437 //
1438 // Enable promiscuous if any instance wants to receive promiscuous.
1439 //
1440 EnableFilterBits |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
1441 }
1442
1443 //
1444 // Set the disable filter.
1445 //
1446 DisableFilterBits ^= EnableFilterBits;
1447
1448 //
1449 // Configure the receive filters of SNP.
1450 //
1451 Status = Snp->ReceiveFilters (
1452 Snp,
1453 EnableFilterBits,
1454 DisableFilterBits,
1455 ResetMCastFilters,
1456 MCastFilterCnt,
1457 MCastFilter
1458 );
1459 DEBUG_CODE (
1460 if (EFI_ERROR (Status)) {
1461 DEBUG (
1462 (EFI_D_ERROR,
1463 "MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
1464 Status)
1465 );
1466 }
1467 );
1468
1469 if (MCastFilter != NULL) {
1470 //
1471 // Free the buffer used to hold the group addresses.
1472 //
1473 FreePool (MCastFilter);
1474 }
1475
1476 return Status;
1477 }
1478
1479
1480 /**
1481 Add a group address control block which controls the MacAddress for
1482 this instance.
1483
1484 @param[in, out] Instance Pointer to the mnp instance context data.
1485 @param[in, out] CtrlBlk Pointer to the group address control block.
1486 @param[in, out] GroupAddress Pointer to the group adress.
1487 @param[in] MacAddress Pointer to the mac address.
1488 @param[in] HwAddressSize The hardware address size.
1489
1490 @retval EFI_SUCCESS The group address control block is added.
1491 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1492
1493 **/
1494 EFI_STATUS
1495 MnpGroupOpAddCtrlBlk (
1496 IN OUT MNP_INSTANCE_DATA *Instance,
1497 IN OUT MNP_GROUP_CONTROL_BLOCK *CtrlBlk,
1498 IN OUT MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
1499 IN EFI_MAC_ADDRESS *MacAddress,
1500 IN UINT32 HwAddressSize
1501 )
1502 {
1503 MNP_DEVICE_DATA *MnpDeviceData;
1504
1505 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1506
1507 MnpDeviceData = Instance->MnpServiceData->MnpDeviceData;
1508 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
1509
1510 if (GroupAddress == NULL) {
1511 ASSERT (MacAddress != NULL);
1512
1513 //
1514 // Allocate a new GroupAddress to be added into MNP's GroupAddressList.
1515 //
1516 GroupAddress = AllocatePool (sizeof (MNP_GROUP_ADDRESS));
1517 if (GroupAddress == NULL) {
1518
1519 DEBUG ((EFI_D_ERROR, "MnpGroupOpFormCtrlBlk: Failed to allocate memory resource.\n"));
1520
1521 return EFI_OUT_OF_RESOURCES;
1522 }
1523
1524 CopyMem (&GroupAddress->Address, MacAddress, sizeof (GroupAddress->Address));
1525 GroupAddress->RefCnt = 0;
1526 InsertTailList (
1527 &MnpDeviceData->GroupAddressList,
1528 &GroupAddress->AddrEntry
1529 );
1530 MnpDeviceData->GroupAddressCount++;
1531 }
1532
1533 //
1534 // Increase the RefCnt.
1535 //
1536 GroupAddress->RefCnt++;
1537
1538 //
1539 // Add the CtrlBlk into the instance's GroupCtrlBlkList.
1540 //
1541 CtrlBlk->GroupAddress = GroupAddress;
1542 InsertTailList (&Instance->GroupCtrlBlkList, &CtrlBlk->CtrlBlkEntry);
1543
1544 return EFI_SUCCESS;
1545 }
1546
1547
1548 /**
1549 Delete a group control block from the instance. If the controlled group address's
1550 reference count reaches zero, the group address is removed too.
1551
1552 @param[in] Instance Pointer to the instance context data.
1553 @param[in] CtrlBlk Pointer to the group control block to delete.
1554
1555 @return The group address controlled by the control block is no longer used or not.
1556
1557 **/
1558 BOOLEAN
1559 MnpGroupOpDelCtrlBlk (
1560 IN MNP_INSTANCE_DATA *Instance,
1561 IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk
1562 )
1563 {
1564 MNP_DEVICE_DATA *MnpDeviceData;
1565 MNP_GROUP_ADDRESS *GroupAddress;
1566
1567 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1568
1569 MnpDeviceData = Instance->MnpServiceData->MnpDeviceData;
1570 NET_CHECK_SIGNATURE (MnpDeviceData, MNP_DEVICE_DATA_SIGNATURE);
1571
1572 //
1573 // Remove and free the CtrlBlk.
1574 //
1575 GroupAddress = CtrlBlk->GroupAddress;
1576 RemoveEntryList (&CtrlBlk->CtrlBlkEntry);
1577 FreePool (CtrlBlk);
1578
1579 ASSERT (GroupAddress->RefCnt > 0);
1580
1581 //
1582 // Count down the RefCnt.
1583 //
1584 GroupAddress->RefCnt--;
1585
1586 if (GroupAddress->RefCnt == 0) {
1587 //
1588 // Free this GroupAddress entry if no instance uses it.
1589 //
1590 MnpDeviceData->GroupAddressCount--;
1591 RemoveEntryList (&GroupAddress->AddrEntry);
1592 FreePool (GroupAddress);
1593
1594 return TRUE;
1595 }
1596
1597 return FALSE;
1598 }
1599
1600
1601 /**
1602 Do the group operations for this instance.
1603
1604 @param[in, out] Instance Pointer to the instance context data.
1605 @param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to
1606 leave a group/groups.
1607 @param[in] MacAddress Pointer to the group address to join or leave.
1608 @param[in] CtrlBlk Pointer to the group control block if JoinFlag
1609 is FALSE.
1610
1611 @retval EFI_SUCCESS The group operation finished.
1612 @retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
1613 @retval Others Other errors as indicated.
1614
1615 **/
1616 EFI_STATUS
1617 MnpGroupOp (
1618 IN OUT MNP_INSTANCE_DATA *Instance,
1619 IN BOOLEAN JoinFlag,
1620 IN EFI_MAC_ADDRESS *MacAddress OPTIONAL,
1621 IN MNP_GROUP_CONTROL_BLOCK *CtrlBlk OPTIONAL
1622 )
1623 {
1624 MNP_DEVICE_DATA *MnpDeviceData;
1625 LIST_ENTRY *Entry;
1626 LIST_ENTRY *NextEntry;
1627 MNP_GROUP_ADDRESS *GroupAddress;
1628 EFI_SIMPLE_NETWORK_MODE *SnpMode;
1629 MNP_GROUP_CONTROL_BLOCK *NewCtrlBlk;
1630 EFI_STATUS Status;
1631 BOOLEAN AddressExist;
1632 BOOLEAN NeedUpdate;
1633
1634 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1635
1636 MnpDeviceData = Instance->MnpServiceData->MnpDeviceData;
1637 SnpMode = MnpDeviceData->Snp->Mode;
1638
1639 if (JoinFlag) {
1640 //
1641 // A new gropu address is to be added.
1642 //
1643 GroupAddress = NULL;
1644 AddressExist = FALSE;
1645
1646 //
1647 // Allocate memory for the control block.
1648 //
1649 NewCtrlBlk = AllocatePool (sizeof (MNP_GROUP_CONTROL_BLOCK));
1650 if (NewCtrlBlk == NULL) {
1651 DEBUG ((EFI_D_ERROR, "MnpGroupOp: Failed to allocate memory resource.\n"));
1652
1653 return EFI_OUT_OF_RESOURCES;
1654 }
1655
1656 NET_LIST_FOR_EACH (Entry, &MnpDeviceData->GroupAddressList) {
1657 //
1658 // Check whether the MacAddress is already joined by other instances.
1659 //
1660 GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
1661 if (CompareMem (MacAddress, &GroupAddress->Address, SnpMode->HwAddressSize) == 0) {
1662 AddressExist = TRUE;
1663 break;
1664 }
1665 }
1666
1667 if (!AddressExist) {
1668 GroupAddress = NULL;
1669 }
1670
1671 //
1672 // Add the GroupAddress for this instance.
1673 //
1674 Status = MnpGroupOpAddCtrlBlk (
1675 Instance,
1676 NewCtrlBlk,
1677 GroupAddress,
1678 MacAddress,
1679 SnpMode->HwAddressSize
1680 );
1681 if (EFI_ERROR (Status)) {
1682 return Status;
1683 }
1684
1685 NeedUpdate = TRUE;
1686 } else {
1687 if (MacAddress != NULL) {
1688 ASSERT (CtrlBlk != NULL);
1689
1690 //
1691 // Leave the specific multicast mac address.
1692 //
1693 NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, CtrlBlk);
1694 } else {
1695 //
1696 // Leave all multicast mac addresses.
1697 //
1698 NeedUpdate = FALSE;
1699
1700 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Instance->GroupCtrlBlkList) {
1701
1702 NewCtrlBlk = NET_LIST_USER_STRUCT (
1703 Entry,
1704 MNP_GROUP_CONTROL_BLOCK,
1705 CtrlBlkEntry
1706 );
1707 //
1708 // Update is required if the group address left is no longer used
1709 // by other instances.
1710 //
1711 NeedUpdate = MnpGroupOpDelCtrlBlk (Instance, NewCtrlBlk);
1712 }
1713 }
1714 }
1715
1716 Status = EFI_SUCCESS;
1717
1718 if (NeedUpdate) {
1719 //
1720 // Reconfigure the receive filters if necessary.
1721 //
1722 Status = MnpConfigReceiveFilters (MnpDeviceData);
1723 }
1724
1725 return Status;
1726 }