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