2 Implementation of Managed Network Protocol public services.
4 Copyright (c) 2005 - 2007, Intel Corporation.<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
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.
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/BaseLib.h>
22 Returns the operational parameters for the current MNP child driver. May also
23 support returning the underlying SNP driver mode data.
25 The GetModeData() function is used to read the current mode data (operational
26 parameters) from the MNP or the underlying SNP.
28 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
29 @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type
30 EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related
32 @param[out] SnpModeData Pointer to storage for SNP operational parameters. This
33 feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE
34 is defined in the EFI_SIMPLE_NETWORK_PROTOCOL.
36 @retval EFI_SUCCESS The operation completed successfully.
37 @retval EFI_INVALID_PARAMETER This is NULL.
38 @retval EFI_UNSUPPORTED The requested feature is unsupported in this
40 @retval EFI_NOT_STARTED This MNP child driver instance has not been
41 configured. The default values are returned in
42 MnpConfigData if it is not NULL.
43 @retval Other The mode data could not be read.
49 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
50 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
,
51 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
54 MNP_INSTANCE_DATA
*Instance
;
55 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
61 return EFI_INVALID_PARAMETER
;
64 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
66 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
68 if (MnpConfigData
!= NULL
) {
70 // Copy the instance configuration data.
72 CopyMem (MnpConfigData
, &Instance
->ConfigData
, sizeof (*MnpConfigData
));
75 if (SnpModeData
!= NULL
) {
77 // Copy the underlayer Snp mode data.
79 Snp
= Instance
->MnpServiceData
->Snp
;
80 CopyMem (SnpModeData
, Snp
->Mode
, sizeof (*SnpModeData
));
83 if (!Instance
->Configured
) {
84 Status
= EFI_NOT_STARTED
;
89 gBS
->RestoreTPL (OldTpl
);
96 Sets or clears the operational parameters for the MNP child driver.
98 The Configure() function is used to set, change, or reset the operational
99 parameters for the MNP child driver instance. Until the operational parameters
100 have been set, no network traffic can be sent or received by this MNP child
101 driver instance. Once the operational parameters have been reset, no more
102 traffic can be sent or received until the operational parameters have been set
104 Each MNP child driver instance can be started and stopped independently of
105 each other by setting or resetting their receive filter settings with the
106 Configure() function.
107 After any successful call to Configure(), the MNP child driver instance is
108 started. The internal periodic timer (if supported) is enabled. Data can be
109 transmitted and may be received if the receive filters have also been enabled.
110 Note: If multiple MNP child driver instances will receive the same packet
111 because of overlapping receive filter settings, then the first MNP child
112 driver instance will receive the original packet and additional instances will
113 receive copies of the original packet.
114 Note: Warning: Receive filter settings that overlap will consume extra
115 processor and/or DMA resources and degrade system and network performance.
117 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
118 @param[in] MnpConfigData Pointer to configuration data that will be assigned
119 to the MNP child driver instance. If NULL, the MNP
120 child driver instance is reset to startup defaults
121 and all pending transmit and receive requests are
122 flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is
123 defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData().
125 @retval EFI_SUCCESS The operation completed successfully.
126 @retval EFI_INVALID_PARAMETER One or more of the following conditions is
129 * MnpConfigData.ProtocolTypeFilter is not
131 The operational data for the MNP child driver
132 instance is unchanged.
133 @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory)
134 could not be allocated.
135 The MNP child driver instance has been reset to
137 @retval EFI_UNSUPPORTED The requested feature is unsupported in
138 this [MNP] implementation. The operational data
139 for the MNP child driver instance is unchanged.
140 @retval EFI_DEVICE_ERROR An unexpected network or system error
141 occurred. The MNP child driver instance has
142 been reset to startup defaults.
143 @retval Other The MNP child driver instance has been reset to
150 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
151 IN EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData OPTIONAL
154 MNP_INSTANCE_DATA
*Instance
;
158 if ((This
== NULL
) ||
159 ((MnpConfigData
!= NULL
) &&
160 (MnpConfigData
->ProtocolTypeFilter
> 0) &&
161 (MnpConfigData
->ProtocolTypeFilter
<= 1500))) {
163 return EFI_INVALID_PARAMETER
;
166 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
168 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
170 if ((MnpConfigData
== NULL
) && (!Instance
->Configured
)) {
172 // If the instance is not configured and a reset is requested, just return.
174 Status
= EFI_SUCCESS
;
179 // Configure the instance.
181 Status
= MnpConfigureInstance (Instance
, MnpConfigData
);
184 gBS
->RestoreTPL (OldTpl
);
191 Translates an IP multicast address to a hardware (MAC) multicast address. This
192 function may be unsupported in some MNP implementations.
194 The McastIpToMac() function translates an IP multicast address to a hardware
195 (MAC) multicast address. This function may be implemented by calling the
196 underlying EFI_SIMPLE_NETWORK.MCastIpToMac() function, which may also be
197 unsupported in some MNP implementations.
199 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
200 @param[in] Ipv6Flag Set to TRUE to if IpAddress is an IPv6 multicast address.
201 Set to FALSE if IpAddress is an IPv4 multicast address.
202 @param[in] IpAddress Pointer to the multicast IP address (in network byte order)
204 @param[out] MacAddress Pointer to the resulting multicast MAC address.
206 @retval EFI_SUCCESS The operation completed successfully.
207 @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE:
210 * IpAddress is not a valid multicast IP
212 * MacAddress is NULL.
213 @retval EFI_NOT_STARTED This MNP child driver instance has not been
215 @retval EFI_UNSUPPORTED The requested feature is unsupported in this
217 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
218 @retval Other The address could not be converted.
223 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
225 IN EFI_IP_ADDRESS
*IpAddress
,
226 OUT EFI_MAC_ADDRESS
*MacAddress
230 MNP_INSTANCE_DATA
*Instance
;
231 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
234 if ((This
== NULL
) || (IpAddress
== NULL
) || (MacAddress
== NULL
)) {
236 return EFI_INVALID_PARAMETER
;
241 // Currently IPv6 isn't supported.
243 return EFI_UNSUPPORTED
;
246 if (!IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress
))) {
248 // The IPv4 address passed in is not a multicast address.
250 return EFI_INVALID_PARAMETER
;
253 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
255 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
257 if (!Instance
->Configured
) {
259 Status
= EFI_NOT_STARTED
;
263 Snp
= Instance
->MnpServiceData
->Snp
;
264 ASSERT (Snp
!= NULL
);
266 if (Snp
->Mode
->IfType
== NET_IFTYPE_ETHERNET
) {
268 // Translate the IPv4 address into a multicast MAC address if the NIC is an
271 MacAddress
->Addr
[0] = 0x01;
272 MacAddress
->Addr
[1] = 0x00;
273 MacAddress
->Addr
[2] = 0x5E;
274 MacAddress
->Addr
[3] = (UINT8
) (IpAddress
->v4
.Addr
[1] & 0x7F);
275 MacAddress
->Addr
[4] = IpAddress
->v4
.Addr
[2];
276 MacAddress
->Addr
[5] = IpAddress
->v4
.Addr
[3];
278 Status
= EFI_SUCCESS
;
281 // Invoke Snp to translate the multicast IP address.
283 Status
= Snp
->MCastIpToMac (
292 gBS
->RestoreTPL (OldTpl
);
298 Enables and disables receive filters for multicast address. This function may
299 be unsupported in some MNP implementations.
301 The Groups() function only adds and removes multicast MAC addresses from the
302 filter list. The MNP driver does not transmit or process Internet Group
303 Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is
304 NULL, then all joined groups are left.
306 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
307 @param[in] JoinFlag Set to TRUE to join this multicast group.
308 Set to FALSE to leave this multicast group.
309 @param[in] MacAddress Pointer to the multicast MAC group (address) to join or
312 @retval EFI_SUCCESS The requested operation completed successfully.
313 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
315 * JoinFlag is TRUE and MacAddress is NULL.
316 * MacAddress is not a valid multicast MAC
318 * The MNP multicast group settings are
320 @retval EFI_NOT_STARTED This MNP child driver instance has not been
322 @retval EFI_ALREADY_STARTED The supplied multicast group is already joined.
323 @retval EFI_NOT_FOUND The supplied multicast group is not joined.
324 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
325 The MNP child driver instance has been reset to
327 @retval EFI_UNSUPPORTED The requested feature is unsupported in this MNP
329 @retval Other The requested operation could not be completed.
330 The MNP multicast group settings are unchanged.
336 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
338 IN EFI_MAC_ADDRESS
*MacAddress OPTIONAL
341 MNP_INSTANCE_DATA
*Instance
;
342 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
343 MNP_GROUP_CONTROL_BLOCK
*GroupCtrlBlk
;
344 MNP_GROUP_ADDRESS
*GroupAddress
;
345 LIST_ENTRY
*ListEntry
;
346 BOOLEAN AddressExist
;
350 if (This
== NULL
|| (JoinFlag
&& (MacAddress
== NULL
))) {
352 // This is NULL, or it's a join operation but MacAddress is NULL.
354 return EFI_INVALID_PARAMETER
;
357 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
358 SnpMode
= Instance
->MnpServiceData
->Snp
->Mode
;
360 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
362 if (!Instance
->Configured
) {
364 Status
= EFI_NOT_STARTED
;
368 if ((!Instance
->ConfigData
.EnableMulticastReceive
) ||
369 ((MacAddress
!= NULL
) && !NET_MAC_IS_MULTICAST (MacAddress
, &SnpMode
->BroadcastAddress
, SnpMode
->HwAddressSize
))) {
371 // The instance isn't configured to do mulitcast receive. OR
372 // the passed in MacAddress is not a mutlticast mac address.
374 Status
= EFI_INVALID_PARAMETER
;
378 Status
= EFI_SUCCESS
;
379 AddressExist
= FALSE
;
382 if (MacAddress
!= NULL
) {
384 // Search the instance's GroupCtrlBlkList to find the specific address.
386 NET_LIST_FOR_EACH (ListEntry
, &Instance
->GroupCtrlBlkList
) {
388 GroupCtrlBlk
= NET_LIST_USER_STRUCT (
390 MNP_GROUP_CONTROL_BLOCK
,
393 GroupAddress
= GroupCtrlBlk
->GroupAddress
;
394 if (0 == CompareMem (
396 &GroupAddress
->Address
,
397 SnpMode
->HwAddressSize
400 // There is already the same multicast mac address configured.
407 if (JoinFlag
&& AddressExist
) {
409 // The multicast mac address to join already exists.
411 Status
= EFI_ALREADY_STARTED
;
414 if (!JoinFlag
&& !AddressExist
) {
416 // The multicast mac address to leave doesn't exist in this instance.
418 Status
= EFI_NOT_FOUND
;
421 if (EFI_ERROR (Status
)) {
424 } else if (IsListEmpty (&Instance
->GroupCtrlBlkList
)) {
426 // The MacAddress is NULL and there is no configured multicast mac address,
433 // OK, it is time to take action.
435 Status
= MnpGroupOp (Instance
, JoinFlag
, MacAddress
, GroupCtrlBlk
);
438 gBS
->RestoreTPL (OldTpl
);
444 Places asynchronous outgoing data packets into the transmit queue.
446 The Transmit() function places a completion token into the transmit packet
447 queue. This function is always asynchronous.
448 The caller must fill in the Token.Event and Token.TxData fields in the
449 completion token, and these fields cannot be NULL. When the transmit operation
450 completes, the MNP updates the Token.Status field and the Token.Event is
452 Note: There may be a performance penalty if the packet needs to be
453 defragmented before it can be transmitted by the network device. Systems in
454 which performance is critical should review the requirements and features of
455 the underlying communications device and drivers.
458 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
459 @param[in] Token Pointer to a token associated with the transmit data
460 descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN is
461 defined in "Related Definitions" below.
463 @retval EFI_SUCCESS The transmit completion token was cached.
464 @retval EFI_NOT_STARTED This MNP child driver instance has not been
466 @retval EFI_INVALID_PARAMETER One or more of the following conditions is
470 * Token.Event is NULL.
471 * Token.TxData is NULL.
472 * Token.TxData.DestinationAddress is not
473 NULL and Token.TxData.HeaderLength is zero.
474 * Token.TxData.FragmentCount is zero.
475 * (Token.TxData.HeaderLength +
476 Token.TxData.DataLength) is not equal to the
478 Token.TxData.FragmentTable[].FragmentLength
481 Token.TxData.FragmentTable[].FragmentLength
484 Token.TxData.FragmentTable[].FragmentBufferfields
486 * Token.TxData.DataLength is greater than MTU.
487 @retval EFI_ACCESS_DENIED The transmit completion token is already in the
489 @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a
490 lack of system resources (usually memory).
491 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
492 The MNP child driver instance has been reset to
494 @retval EFI_NOT_READY The transmit request could not be queued because
495 the transmit queue is full.
501 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
502 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
506 MNP_INSTANCE_DATA
*Instance
;
507 MNP_SERVICE_DATA
*MnpServiceData
;
512 if ((This
== NULL
) || (Token
== NULL
)) {
514 return EFI_INVALID_PARAMETER
;
517 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
519 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
521 if (!Instance
->Configured
) {
523 Status
= EFI_NOT_STARTED
;
527 if (!MnpIsValidTxToken (Instance
, Token
)) {
529 // The Token is invalid.
531 Status
= EFI_INVALID_PARAMETER
;
535 MnpServiceData
= Instance
->MnpServiceData
;
536 NET_CHECK_SIGNATURE (MnpServiceData
, MNP_SERVICE_DATA_SIGNATURE
);
539 // Build the tx packet
541 MnpBuildTxPacket (MnpServiceData
, Token
->Packet
.TxData
, &PktBuf
, &PktLen
);
544 // OK, send the packet synchronously.
546 Status
= MnpSyncSendPacket (MnpServiceData
, PktBuf
, PktLen
, Token
);
549 gBS
->RestoreTPL (OldTpl
);
556 Places an asynchronous receiving request into the receiving queue.
558 The Receive() function places a completion token into the receive packet
559 queue. This function is always asynchronous.
560 The caller must fill in the Token.Event field in the completion token, and
561 this field cannot be NULL. When the receive operation completes, the MNP
562 updates the Token.Status and Token.RxData fields and the Token.Event is
565 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
566 @param[in] Token Pointer to a token associated with the receive
567 data descriptor. Type
568 EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in
569 EFI_MANAGED_NETWORK_PROTOCOL.Transmit().
571 @retval EFI_SUCCESS The receive completion token was cached.
572 @retval EFI_NOT_STARTED This MNP child driver instance has not been
574 @retval EFI_INVALID_PARAMETER One or more of the following conditions is
578 * Token.Event is NULL
579 @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a
580 lack of system resources (usually memory).
581 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
582 The MNP child driver instance has been reset to
584 @retval EFI_ACCESS_DENIED The receive completion token was already in the
586 @retval EFI_NOT_READY The receive request could not be queued because
587 the receive queue is full.
593 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
594 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token
598 MNP_INSTANCE_DATA
*Instance
;
601 if ((This
== NULL
) || (Token
== NULL
) || (Token
->Event
== NULL
)) {
603 return EFI_INVALID_PARAMETER
;
606 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
608 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
610 if (!Instance
->Configured
) {
612 Status
= EFI_NOT_STARTED
;
617 // Check whether this token(event) is already in the rx token queue.
619 Status
= NetMapIterate (&Instance
->RxTokenMap
, MnpTokenExist
, (VOID
*) Token
);
620 if (EFI_ERROR (Status
)) {
626 // Insert the Token into the RxTokenMap.
628 Status
= NetMapInsertTail (&Instance
->RxTokenMap
, (VOID
*) Token
, NULL
);
630 if (!EFI_ERROR (Status
)) {
632 // Try to deliver any buffered packets.
634 Status
= MnpInstanceDeliverPacket (Instance
);
637 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
639 NetLibDispatchDpc ();
643 gBS
->RestoreTPL (OldTpl
);
649 Aborts an asynchronous transmit or receive request.
651 The Cancel() function is used to abort a pending transmit or receive request.
652 If the token is in the transmit or receive request queues, after calling this
653 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
654 signaled. If the token is not in one of the queues, which usually means that
655 the asynchronous operation has completed, this function will not signal the
656 token and EFI_NOT_FOUND is returned.
658 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
659 @param[in] Token Pointer to a token that has been issued by
660 EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or
661 EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all pending
664 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
665 Token.Event was signaled. When Token is NULL,
666 all pending requests were aborted and their
667 events were signaled.
668 @retval EFI_NOT_STARTED This MNP child driver instance has not been
670 @retval EFI_INVALID_PARAMETER This is NULL.
671 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O
672 request was not found in the transmit or
673 receive queue. It has either completed or was
674 not issued by Transmit() and Receive().
680 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
,
681 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN
*Token OPTIONAL
685 MNP_INSTANCE_DATA
*Instance
;
690 return EFI_INVALID_PARAMETER
;
693 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
695 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
697 if (!Instance
->Configured
) {
699 Status
= EFI_NOT_STARTED
;
704 // Iterate the RxTokenMap to cancel the specified Token.
706 Status
= NetMapIterate (&Instance
->RxTokenMap
, MnpCancelTokens
, (VOID
*) Token
);
710 Status
= (Status
== EFI_ABORTED
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
714 // Dispatch the DPC queued by the NotifyFunction of the cancled token's events.
716 NetLibDispatchDpc ();
719 gBS
->RestoreTPL (OldTpl
);
725 Polls for incoming data packets and processes outgoing data packets.
727 The Poll() function can be used by network drivers and applications to
728 increase the rate that data packets are moved between the communications
729 device and the transmit and receive queues.
730 Normally, a periodic timer event internally calls the Poll() function. But, in
731 some systems, the periodic timer event may not call Poll() fast enough to
732 transmit and/or receive all data packets without missing packets. Drivers and
733 applications that are experiencing packet loss should try calling the Poll()
736 @param[in] This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
738 @retval EFI_SUCCESS Incoming or outgoing data was processed.
739 @retval EFI_NOT_STARTED This MNP child driver instance has not been
741 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The
742 MNP child driver instance has been reset to startup
744 @retval EFI_NOT_READY No incoming or outgoing data was processed. Consider
745 increasing the polling rate.
746 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive
747 queue. Consider increasing the polling rate.
753 IN EFI_MANAGED_NETWORK_PROTOCOL
*This
757 MNP_INSTANCE_DATA
*Instance
;
761 return EFI_INVALID_PARAMETER
;
764 Instance
= MNP_INSTANCE_DATA_FROM_THIS (This
);
766 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
768 if (!Instance
->Configured
) {
769 Status
= EFI_NOT_STARTED
;
774 // Try to receive packets.
776 Status
= MnpReceivePacket (Instance
->MnpServiceData
);
778 NetLibDispatchDpc ();
781 gBS
->RestoreTPL (OldTpl
);