/** @file
Implementation of Managed Network Protocol private services.
Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions
of the BSD License which accompanies this distribution. The full
text of the license may be found at<BR>
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "MnpImpl.h"
#include "MnpVlan.h"
};
};
10000000,
10000000,
0,
};
/**
Add Count of net buffers to MnpDeviceData->FreeNbufQue. The length of the net
buffer is specified by MnpDeviceData->BufferLength.
@param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
@param[in] Count Number of NET_BUFFERs to add.
@retval EFI_SUCCESS The specified amount of NET_BUFs are allocated
and added to MnpDeviceData->FreeNbufQue.
@retval EFI_OUT_OF_RESOURCES Failed to allocate a NET_BUF structure.
**/
)
{
break;
}
if (MnpDeviceData->PaddingSize > 0) {
//
// Pad padding bytes before the media header
//
}
}
return Status;
}
/**
Allocate a free NET_BUF from MnpDeviceData->FreeNbufQue. If there is none
in the queue, first try to allocate some and add them into the queue, then
fetch the NET_BUF from the updated FreeNbufQue.
@param[in, out] MnpDeviceData Pointer to the MNP_DEVICE_DATA.
@return Pointer to the allocated free NET_BUF structure, if NULL the
operation is failed.
**/
NET_BUF *
)
{
//
// Check whether there are available buffers, or else try to add some.
//
if (FreeNbufQue->BufNum == 0) {
DEBUG (
"MnpAllocNbuf: The maximum NET_BUF size is reached for MNP driver instance %p.\n",
);
goto ON_EXIT;
}
DEBUG (
"MnpAllocNbuf: Failed to add NET_BUFs into the FreeNbufQue, %r.\n",
);
//
// Don't return NULL, perhaps MnpAddFreeNbuf does add some NET_BUFs but
// the amount is less than MNP_NET_BUFFER_INCREASEMENT.
//
}
}
//
// Increase the RefCnt.
//
NET_GET_REF (Nbuf);
}
return Nbuf;
}
/**
Try to reclaim the Nbuf into the buffer pool.
@param[in, out] MnpDeviceData Pointer to the mnp device context data.
@param[in, out] Nbuf Pointer to the NET_BUF to free.
**/
)
{
NET_PUT_REF (Nbuf);
//
// Trim all buffer contained in the Nbuf, then append it to the NbufQue.
//
//
// There is space reserved for vlan tag in the head, reclaim it
//
}
}
}
/**
Initialize the mnp device context data.
@param[in, out] MnpDeviceData Pointer to the mnp device context data.
@param[in] ImageHandle The driver image handle.
@param[in] ControllerHandle Handle of device to bind driver to.
@retval EFI_SUCCESS The mnp service context is initialized.
@retval EFI_UNSUPPORTED ControllerHandle does not support Simple Network Protocol.
@retval Others Other errors as indicated.
**/
)
{
//
// Copy the MNP Protocol interfaces from the template.
//
CopyMem (&MnpDeviceData->VlanConfig, &mVlanConfigProtocolTemplate, sizeof (EFI_VLAN_CONFIG_PROTOCOL));
//
// Open the Simple Network protocol.
//
);
return EFI_UNSUPPORTED;
}
//
// Get MTU from Snp.
//
//
// Initialize the lists.
//
//
// Get the buffer length used to allocate NET_BUF to hold data received
// from SNP. Do this before fill the FreeNetBufQue.
//
//
MnpDeviceData->BufferLength = SnpMode->MediaHeaderSize + NET_VLAN_TAG_LEN + SnpMode->MaxPacketSize + NET_ETHER_FCS_SIZE;
//
// Make sure the protocol headers immediately following the media header
// 4-byte aligned, and also preserve additional space for VLAN tag
//
//
// Initialize MAC string which will be used as VLAN configuration variable name
//
goto ERROR;
}
//
// Initialize the FreeNetBufQue and pre-allocate some NET_BUFs.
//
goto ERROR;
}
//
// Get one NET_BUF from the FreeNbufQue for rx cache.
//
);
//
// Allocate buffer pool for tx.
//
goto ERROR;
}
//
// Create the system poll timer.
//
);
goto ERROR;
}
//
// Create the timer for packet timeout check.
//
);
goto ERROR;
}
//
// Create the timer for media detection.
//
);
goto ERROR;
}
//
// Create the timer for tx timeout check.
//
NULL,
NULL,
);
}
//
// Free the dynamic allocated resources if necessary.
//
}
}
}
}
}
}
}
//
// Close the Simple Network Protocol.
//
gBS->CloseProtocol (
);
}
return Status;
}
/**
Destroy the MNP device context data.
@param[in, out] MnpDeviceData Pointer to the mnp device context data.
@param[in] ImageHandle The driver image handle.
**/
)
{
//
// Free Vlan Config variable name string
//
}
//
// The GroupAddressList must be empty.
//
//
// Close the event.
//
//
// Free the tx buffer.
//
//
// Free the RxNbufCache.
//
//
// Flush the FreeNbufQue.
//
//
// Close the Simple Network Protocol.
//
gBS->CloseProtocol (
);
}
/**
Create mnp service context data.
@param[in] MnpDeviceData Pointer to the mnp device context data.
@param[in] VlanId The VLAN ID.
@param[in] Priority The VLAN priority. If VlanId is 0,
Priority is ignored.
@return A pointer to MNP_SERVICE_DATA or NULL if failed to create MNP service context.
**/
)
{
//
// Initialize the Mnp Service Data.
//
if (MnpServiceData == NULL) {
DEBUG ((EFI_D_ERROR, "MnpCreateServiceData: Faild to allocate memory for the new Mnp Service Data.\n"));
return NULL;
}
//
// Add to MNP service list
//
//
// Copy the ServiceBinding structure.
//
CopyMem (&MnpServiceData->ServiceBinding, &mMnpServiceBindingProtocol, sizeof (EFI_SERVICE_BINDING_PROTOCOL));
//
// Initialize the lists.
//
if (VlanId != 0) {
//
// Create VLAN child handle
//
);
if (MnpServiceHandle == NULL) {
return NULL;
}
//
// Open VLAN Config Protocol by child
//
(VOID **) &VlanConfig,
);
goto Exit;
}
//
// Reduce MTU for VLAN device
//
} else {
//
//
}
//
// Install the MNP Service Binding Protocol
//
);
Exit:
}
return MnpServiceData;
}
/**
Destroy the MNP service context data.
@param[in, out] MnpServiceData Pointer to the mnp service context data.
@retval EFI_SUCCESS The mnp service context is destroyed.
@retval Others Errors as indicated.
**/
)
{
//
// Uninstall the MNP Service Binding Protocol
//
);
return Status;
}
if (MnpServiceData->VlanId != 0) {
//
// Close VlanConfig Protocol opened by VLAN child handle
//
);
return Status;
}
//
// Uninstall Device Path Protocol to destroy the VLAN child handle
//
);
return Status;
}
}
}
//
// Remove from MnpDeviceData service list
//
return Status;
}
/**
Destroy all child of the MNP service data.
@param[in, out] MnpServiceData Pointer to the mnp service context data.
@retval EFI_SUCCESS All child are destroyed.
@retval Others Failed to destroy all child.
**/
)
{
//
// Don't use NetListRemoveHead here, the remove opreration will be done
// in ServiceBindingDestroyChild.
//
);
return Status;
}
}
return EFI_SUCCESS;
}
/**
Find the MNP Service Data for given VLAN ID.
@param[in] MnpDeviceData Pointer to the mnp device context data.
@param[in] VlanId The VLAN ID.
@return A pointer to MNP_SERVICE_DATA or NULL if not found.
**/
)
{
//
// Check VLAN ID of each Mnp Service Data
//
return MnpServiceData;
}
}
return NULL;
}
/**
Initialize the mnp instance context data.
@param[in] MnpServiceData Pointer to the mnp service context data.
@param[in, out] Instance Pointer to the mnp instance context data
to initialize.
**/
)
{
//
// Set the signature.
//
//
// Copy the MNP Protocol interfaces from the template.
//
//
// Copy the default config data.
//
//
// Initialize the lists.
//
//
// Initialize the RxToken Map.
//
//
// Save the MnpServiceData info.
//
}
/**
Check whether the token specified by Arg matches the token in Item.
@param[in] Map Pointer to the NET_MAP.
@param[in] Item Pointer to the NET_MAP_ITEM.
@param[in] Arg Pointer to the Arg, it's a pointer to the token to
check.
@retval EFI_SUCCESS The token specified by Arg is different from the
token in Item.
@retval EFI_ACCESS_DENIED The token specified by Arg is the same as that in
Item.
**/
)
{
//
// The token is the same either the two tokens equals or the Events in
// the two tokens are the same.
//
return EFI_ACCESS_DENIED;
}
return EFI_SUCCESS;
}
/**
Cancel the token specified by Arg if it matches the token in Item.
@param[in, out] Map Pointer to the NET_MAP.
@param[in, out] Item Pointer to the NET_MAP_ITEM.
@param[in] Arg Pointer to the Arg, it's a pointer to the
token to cancel.
@retval EFI_SUCCESS The Arg is NULL, and the token in Item is cancelled,
or the Arg isn't NULL, and the token in Item is
different from the Arg.
@retval EFI_ABORTED The Arg isn't NULL, the token in Item mathces the
Arg, and the token is cancelled.
**/
)
{
//
// The token in Item is not the token specified by Arg.
//
return EFI_SUCCESS;
}
//
// Remove the item from the map.
//
//
// Cancel this token with status set to EFI_ABORTED.
//
//
// Only abort the token specified by Arg if Arg isn't NULL.
//
return EFI_ABORTED;
}
return EFI_SUCCESS;
}
/**
Start and initialize the simple network.
@param[in] Snp Pointer to the simple network protocol.
@retval EFI_SUCCESS The simple network protocol is started.
@retval Others Other errors as indicated.
**/
)
{
//
// Start the simple network.
//
//
// Initialize the simple network.
//
}
return Status;
}
/**
Stop the simple network.
@param[in] Snp Pointer to the simple network protocol.
@retval EFI_SUCCESS The simple network is stopped.
@retval Others Other errors as indicated.
**/
)
{
//
// Shut down the simple network.
//
//
// Stop the simple network.
//
}
return Status;
}
/**
Start the managed network, this function is called when one instance is configured
or reconfigured.
@param[in, out] MnpServiceData Pointer to the mnp service context data.
@param[in] IsConfigUpdate The instance is reconfigured or it's the first
time the instanced is configured.
@param[in] EnableSystemPoll Enable the system polling or not.
@retval EFI_SUCCESS The managed network is started and some
configuration is updated.
@retval Others Other errors as indicated.
**/
MnpStart (
)
{
if (!IsConfigUpdate) {
//
// If it's not a configuration update, increase the configured children number.
//
//
// It's the first configured child, start the simple network.
//
goto ErrorExit;
}
//
// Start the timeout timer.
//
);
DEBUG (
"MnpStart, gBS->SetTimer for TimeoutCheckTimer %r.\n",
);
goto ErrorExit;
}
//
// Start the media detection timer.
//
);
DEBUG (
"MnpStart, gBS->SetTimer for MediaDetectTimer %r.\n",
);
goto ErrorExit;
}
}
}
//
// The EnableSystemPoll differs with the current state, disable or enable
// the system poll.
//
goto ErrorExit;
}
}
//
// Change the receive filters if need.
//
return Status;
}
/**
Stop the managed network.
@param[in, out] MnpServiceData Pointer to the mnp service context data.
@retval EFI_SUCCESS The managed network is stopped.
@retval Others Other errors as indicated.
**/
MnpStop (
)
{
//
// Configure the receive filters.
//
//
// Decrease the children number.
//
if (MnpDeviceData->ConfiguredChildrenNumber > 0) {
//
// If there are other configured chilren, return and keep the timers and
// simple network unchanged.
//
return EFI_SUCCESS;
}
//
// No configured children now.
//
if (MnpDeviceData->EnableSystemPoll) {
//
// The system poll in on, cancel the poll timer.
//
}
//
// Cancel the timeout timer.
//
//
// Cancel the media detect timer.
//
//
// Stop the simple network.
//
return Status;
}
/**
Flush the instance's received data.
@param[in, out] Instance Pointer to the mnp instance context data.
**/
)
{
//
// Remove all the Wraps.
//
//
// Recycle the RxDataWrap.
//
}
}
/**
Configure the Instance using ConfigData.
@param[in, out] Instance Pointer to the mnp instance context data.
@param[in] ConfigData Pointer to the configuration data used to configure
the isntance.
@retval EFI_SUCCESS The Instance is configured.
@retval EFI_UNSUPPORTED EnableReceiveTimestamps is on and the
implementation doesn't support it.
@retval Others Other errors as indicated.
**/
)
{
//
// Don't support timestamp.
//
return EFI_UNSUPPORTED;
}
if (NewConfigData == NULL) {
//
// Restore back the default config data if a reset of this instance
// is required.
//
}
//
// Reset the instance's receive filter.
//
Instance->ReceiveFilter = 0;
//
// Clear the receive counters according to the old ConfigData.
//
if (OldConfigData->EnableUnicastReceive) {
}
if (OldConfigData->EnableMulticastReceive) {
}
if (OldConfigData->EnableBroadcastReceive) {
}
}
//
// Set the receive filter counters and the receive filter of the
// instance according to the new ConfigData.
//
if (NewConfigData->EnableUnicastReceive) {
}
if (NewConfigData->EnableMulticastReceive) {
}
if (NewConfigData->EnableBroadcastReceive) {
}
}
if (OldConfigData->FlushQueuesOnReset) {
}
if (ConfigData == NULL) {
}
if (!NewConfigData->EnableMulticastReceive) {
}
//
// Save the new configuration data.
//
if (Instance->Configured) {
//
// The instance is configured, start the Mnp.
//
);
} else {
//
// The instance is changed to the unconfigured state, stop the Mnp.
//
}
return Status;
}
/**
Configure the Snp receive filters according to the instances' receive filter
settings.
@param[in] MnpDeviceData Pointer to the mnp device context data.
@retval EFI_SUCCESS The receive filters is configured.
@retval EFI_OUT_OF_RESOURCES The receive filters can't be configured due
to lack of memory resource.
**/
)
{
//
// Initialize the enable filter and disable filter.
//
EnableFilterBits = 0;
if (MnpDeviceData->UnicastCount != 0) {
//
// Enable unicast if any instance wants to receive unicast.
//
}
if (MnpDeviceData->BroadcastCount != 0) {
//
// Enable broadcast if any instance wants to receive broadcast.
//
}
MCastFilter = NULL;
MCastFilterCnt = 0;
//
// There are instances configured to receive multicast and already some group
// addresses are joined.
//
//
// The joind group address is less than simple network's maximum count.
// Just configure the snp to do the multicast filtering.
//
//
// Allocate pool for the mulicast addresses.
//
if (MCastFilter == NULL) {
DEBUG ((EFI_D_ERROR, "MnpConfigReceiveFilters: Failed to allocate memory resource for MCastFilter.\n"));
return EFI_OUT_OF_RESOURCES;
}
//
// Fill the multicast HW address buffer.
//
Index = 0;
Index++;
}
} else {
//
// The maximum multicast is reached, set the filter to be promiscuous
// multicast.
//
} else {
//
// Either MULTICAST or PROMISCUOUS_MULTICAST is not supported by Snp,
// set the NIC to be promiscuous although this will tremendously degrade
// the performance.
//
}
}
}
if (MnpDeviceData->PromiscuousCount != 0) {
//
// Enable promiscuous if any instance wants to receive promiscuous.
//
}
//
// Set the disable filter.
//
//
// Configure the receive filters of SNP.
//
Snp,
);
DEBUG (
"MnpConfigReceiveFilters: Snp->ReceiveFilters failed, %r.\n",
);
}
);
if (MCastFilter != NULL) {
//
// Free the buffer used to hold the group addresses.
//
}
return Status;
}
/**
Add a group address control block which controls the MacAddress for
this instance.
@param[in, out] Instance Pointer to the mnp instance context data.
@param[in, out] CtrlBlk Pointer to the group address control block.
@param[in, out] GroupAddress Pointer to the group adress.
@param[in] MacAddress Pointer to the mac address.
@param[in] HwAddressSize The hardware address size.
@retval EFI_SUCCESS The group address control block is added.
@retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
**/
)
{
if (GroupAddress == NULL) {
//
// Allocate a new GroupAddress to be added into MNP's GroupAddressList.
//
if (GroupAddress == NULL) {
return EFI_OUT_OF_RESOURCES;
}
GroupAddress->RefCnt = 0;
);
}
//
// Increase the RefCnt.
//
GroupAddress->RefCnt++;
//
// Add the CtrlBlk into the instance's GroupCtrlBlkList.
//
return EFI_SUCCESS;
}
/**
Delete a group control block from the instance. If the controlled group address's
reference count reaches zero, the group address is removed too.
@param[in] Instance Pointer to the instance context data.
@param[in] CtrlBlk Pointer to the group control block to delete.
@return The group address controlled by the control block is no longer used or not.
**/
)
{
//
// Remove and free the CtrlBlk.
//
//
// Count down the RefCnt.
//
GroupAddress->RefCnt--;
if (GroupAddress->RefCnt == 0) {
//
// Free this GroupAddress entry if no instance uses it.
//
return TRUE;
}
return FALSE;
}
/**
Do the group operations for this instance.
@param[in, out] Instance Pointer to the instance context data.
@param[in] JoinFlag Set to TRUE to join a group. Set to TRUE to
@param[in] MacAddress Pointer to the group address to join or leave.
@param[in] CtrlBlk Pointer to the group control block if JoinFlag
is FALSE.
@retval EFI_SUCCESS The group operation finished.
@retval EFI_OUT_OF_RESOURCES Failed due to lack of memory resources.
@retval Others Other errors as indicated.
**/
)
{
if (JoinFlag) {
//
// A new gropu address is to be added.
//
GroupAddress = NULL;
//
// Allocate memory for the control block.
//
if (NewCtrlBlk == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Check whether the MacAddress is already joined by other instances.
//
AddressExist = TRUE;
break;
}
}
if (!AddressExist) {
GroupAddress = NULL;
}
//
// Add the GroupAddress for this instance.
//
);
return Status;
}
NeedUpdate = TRUE;
} else {
if (MacAddress != NULL) {
//
// Leave the specific multicast mac address.
//
} else {
//
// Leave all multicast mac addresses.
//
NeedUpdate = FALSE;
);
//
// Update is required if the group address left is no longer used
// by other instances.
//
}
}
}
if (NeedUpdate) {
//
// Reconfigure the receive filters if necessary.
//
}
return Status;
}