1 /** @file
2   Implementation of Managed Network Protocol public services.
3 
4 Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include "MnpImpl.h"
10 
11 /**
12   Returns the operational parameters for the current MNP child driver. May also
13   support returning the underlying SNP driver mode data.
14 
15   The GetModeData() function is used to read the current mode data (operational
16   parameters) from the MNP or the underlying SNP.
17 
18   @param[in]  This          Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
19   @param[out] MnpConfigData Pointer to storage for MNP operational parameters. Type
20                             EFI_MANAGED_NETWORK_CONFIG_DATA is defined in "Related
21                             Definitions" below.
22   @param[out] SnpModeData   Pointer to storage for SNP operational parameters. This
23                             feature may be unsupported. Type EFI_SIMPLE_NETWORK_MODE
24                             is defined in the EFI_SIMPLE_NETWORK_PROTOCOL.
25 
26   @retval EFI_SUCCESS           The operation completed successfully.
27   @retval EFI_INVALID_PARAMETER This is NULL.
28   @retval EFI_UNSUPPORTED       The requested feature is unsupported in this
29                                 MNP implementation.
30   @retval EFI_NOT_STARTED       This MNP child driver instance has not been
31                                 configured. The default values are returned in
32                                 MnpConfigData if it is not NULL.
33   @retval Others                The mode data could not be read.
34 
35 **/
36 EFI_STATUS
37 EFIAPI
MnpGetModeData(IN EFI_MANAGED_NETWORK_PROTOCOL * This,OUT EFI_MANAGED_NETWORK_CONFIG_DATA * MnpConfigData OPTIONAL,OUT EFI_SIMPLE_NETWORK_MODE * SnpModeData OPTIONAL)38 MnpGetModeData (
39   IN     EFI_MANAGED_NETWORK_PROTOCOL      *This,
40      OUT EFI_MANAGED_NETWORK_CONFIG_DATA   *MnpConfigData OPTIONAL,
41      OUT EFI_SIMPLE_NETWORK_MODE           *SnpModeData OPTIONAL
42   )
43 {
44   MNP_INSTANCE_DATA           *Instance;
45   EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
46   EFI_TPL                     OldTpl;
47   EFI_STATUS                  Status;
48   UINT32                      InterruptStatus;
49 
50   if (This == NULL) {
51     return EFI_INVALID_PARAMETER;
52   }
53 
54   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
55 
56   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
57 
58   if (MnpConfigData != NULL) {
59     //
60     // Copy the instance configuration data.
61     //
62     CopyMem (MnpConfigData, &Instance->ConfigData, sizeof (*MnpConfigData));
63   }
64 
65   if (SnpModeData != NULL) {
66     //
67     // Copy the underlayer Snp mode data.
68     //
69     Snp = Instance->MnpServiceData->MnpDeviceData->Snp;
70 
71     //
72     // Upon successful return of GetStatus(), the Snp->Mode->MediaPresent
73     // will be updated to reflect any change of media status
74     //
75     Snp->GetStatus (Snp, &InterruptStatus, NULL);
76     CopyMem (SnpModeData, Snp->Mode, sizeof (*SnpModeData));
77   }
78 
79   if (!Instance->Configured) {
80     Status = EFI_NOT_STARTED;
81   } else {
82     Status = EFI_SUCCESS;
83   }
84 
85   gBS->RestoreTPL (OldTpl);
86 
87   return Status;
88 }
89 
90 
91 /**
92   Sets or clears the operational parameters for the MNP child driver.
93 
94   The Configure() function is used to set, change, or reset the operational
95   parameters for the MNP child driver instance. Until the operational parameters
96   have been set, no network traffic can be sent or received by this MNP child
97   driver instance. Once the operational parameters have been reset, no more
98   traffic can be sent or received until the operational parameters have been set
99   again.
100   Each MNP child driver instance can be started and stopped independently of
101   each other by setting or resetting their receive filter settings with the
102   Configure() function.
103   After any successful call to Configure(), the MNP child driver instance is
104   started. The internal periodic timer (if supported) is enabled. Data can be
105   transmitted and may be received if the receive filters have also been enabled.
106   Note: If multiple MNP child driver instances will receive the same packet
107   because of overlapping receive filter settings, then the first MNP child
108   driver instance will receive the original packet and additional instances will
109   receive copies of the original packet.
110   Note: Warning: Receive filter settings that overlap will consume extra
111   processor and/or DMA resources and degrade system and network performance.
112 
113   @param[in]  This           Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
114   @param[in]  MnpConfigData  Pointer to configuration data that will be assigned
115                              to the MNP child driver instance. If NULL, the MNP
116                              child driver instance is reset to startup defaults
117                              and all pending transmit and receive requests are
118                              flushed. Type EFI_MANAGED_NETWORK_CONFIG_DATA is
119                              defined in EFI_MANAGED_NETWORK_PROTOCOL.GetModeData().
120 
121   @retval EFI_SUCCESS            The operation completed successfully.
122   @retval EFI_INVALID_PARAMETER  One or more of the following conditions is
123                                  TRUE:
124                                  * This is NULL.
125                                  * MnpConfigData.ProtocolTypeFilter is not
126                                    valid.
127                                  The operational data for the MNP child driver
128                                  instance is unchanged.
129   @retval EFI_OUT_OF_RESOURCES   Required system resources (usually memory)
130                                  could not be allocated.
131                                  The MNP child driver instance has been reset to
132                                  startup defaults.
133   @retval EFI_UNSUPPORTED        The requested feature is unsupported in
134                                  this [MNP] implementation. The operational data
135                                  for the MNP child driver instance is unchanged.
136   @retval EFI_DEVICE_ERROR       An unexpected network or system error
137                                  occurred. The MNP child driver instance has
138                                  been reset to startup defaults.
139   @retval Others                 The MNP child driver instance has been reset to
140                                  startup defaults.
141 
142 **/
143 EFI_STATUS
144 EFIAPI
MnpConfigure(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN EFI_MANAGED_NETWORK_CONFIG_DATA * MnpConfigData OPTIONAL)145 MnpConfigure (
146   IN EFI_MANAGED_NETWORK_PROTOCOL        *This,
147   IN EFI_MANAGED_NETWORK_CONFIG_DATA     *MnpConfigData OPTIONAL
148   )
149 {
150   MNP_INSTANCE_DATA  *Instance;
151   EFI_TPL            OldTpl;
152   EFI_STATUS         Status;
153 
154   if ((This == NULL) ||
155       ((MnpConfigData != NULL) &&
156        (MnpConfigData->ProtocolTypeFilter > 0) &&
157        (MnpConfigData->ProtocolTypeFilter <= 1500))
158      ) {
159     return EFI_INVALID_PARAMETER;
160   }
161 
162   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
163 
164   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
165 
166   if ((MnpConfigData == NULL) && (!Instance->Configured)) {
167     //
168     // If the instance is not configured and a reset is requested, just return.
169     //
170     Status = EFI_SUCCESS;
171     goto ON_EXIT;
172   }
173 
174   //
175   // Configure the instance.
176   //
177   Status = MnpConfigureInstance (Instance, MnpConfigData);
178 
179 ON_EXIT:
180   gBS->RestoreTPL (OldTpl);
181 
182   return Status;
183 }
184 
185 
186 /**
187   Translates an IP multicast address to a hardware (MAC) multicast address. This
188   function may be unsupported in some MNP implementations.
189 
190   The McastIpToMac() function translates an IP multicast address to a hardware
191   (MAC) multicast address. This function may be implemented by calling the
192   underlying EFI_SIMPLE_NETWORK. MCastIpToMac() function, which may also be
193   unsupported in some MNP implementations.
194 
195   @param[in]  This        Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
196   @param[in]  Ipv6Flag    Set to TRUE to if IpAddress is an IPv6 multicast address.
197                           Set to FALSE if IpAddress is an IPv4 multicast address.
198   @param[in]  IpAddress   Pointer to the multicast IP address (in network byte
199                           order) to convert.
200   @param[out] MacAddress  Pointer to the resulting multicast MAC address.
201 
202   @retval EFI_SUCCESS           The operation completed successfully.
203   @retval EFI_INVALID_PARAMETER One of the following conditions is TRUE:
204                                  * This is NULL.
205                                  * IpAddress is NULL.
206                                  * IpAddress is not a valid multicast IP
207                                    address.
208                                  * MacAddress is NULL.
209   @retval EFI_NOT_STARTED       This MNP child driver instance has not been
210                                 configured.
211   @retval EFI_UNSUPPORTED       The requested feature is unsupported in this
212                                 MNP implementation.
213   @retval EFI_DEVICE_ERROR      An unexpected network or system error occurred.
214   @retval Others                The address could not be converted.
215 **/
216 EFI_STATUS
217 EFIAPI
MnpMcastIpToMac(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN BOOLEAN Ipv6Flag,IN EFI_IP_ADDRESS * IpAddress,OUT EFI_MAC_ADDRESS * MacAddress)218 MnpMcastIpToMac (
219   IN     EFI_MANAGED_NETWORK_PROTOCOL    *This,
220   IN     BOOLEAN                         Ipv6Flag,
221   IN     EFI_IP_ADDRESS                  *IpAddress,
222      OUT EFI_MAC_ADDRESS                 *MacAddress
223   )
224 {
225   EFI_STATUS                  Status;
226   MNP_INSTANCE_DATA           *Instance;
227   EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
228   EFI_TPL                     OldTpl;
229   EFI_IPv6_ADDRESS            *Ip6Address;
230 
231   if ((This == NULL) || (IpAddress == NULL) || (MacAddress == NULL)) {
232     return EFI_INVALID_PARAMETER;
233   }
234 
235   Ip6Address = &IpAddress->v6;
236 
237   if ((Ipv6Flag && !IP6_IS_MULTICAST (Ip6Address)) ||
238       (!Ipv6Flag && !IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress)))
239       ) {
240     //
241     // The IP address passed in is not a multicast address.
242     //
243     return EFI_INVALID_PARAMETER;
244   }
245 
246   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
247 
248   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
249 
250   if (!Instance->Configured) {
251 
252     Status = EFI_NOT_STARTED;
253     goto ON_EXIT;
254   }
255 
256   Snp = Instance->MnpServiceData->MnpDeviceData->Snp;
257   ASSERT (Snp != NULL);
258 
259   ZeroMem (MacAddress, sizeof (EFI_MAC_ADDRESS));
260 
261   if (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) {
262     if (!Ipv6Flag) {
263       //
264       // Translate the IPv4 address into a multicast MAC address if the NIC is an
265       // ethernet NIC according to RFC1112..
266       //
267       MacAddress->Addr[0] = 0x01;
268       MacAddress->Addr[1] = 0x00;
269       MacAddress->Addr[2] = 0x5E;
270       MacAddress->Addr[3] = (UINT8) (IpAddress->v4.Addr[1] & 0x7F);
271       MacAddress->Addr[4] = IpAddress->v4.Addr[2];
272       MacAddress->Addr[5] = IpAddress->v4.Addr[3];
273     } else {
274       //
275       // Translate the IPv6 address into a multicast MAC address if the NIC is an
276       // ethernet NIC according to RFC2464.
277       //
278 
279       MacAddress->Addr[0] = 0x33;
280       MacAddress->Addr[1] = 0x33;
281       MacAddress->Addr[2] = Ip6Address->Addr[12];
282       MacAddress->Addr[3] = Ip6Address->Addr[13];
283       MacAddress->Addr[4] = Ip6Address->Addr[14];
284       MacAddress->Addr[5] = Ip6Address->Addr[15];
285     }
286 
287     Status = EFI_SUCCESS;
288   } else {
289     //
290     // Invoke Snp to translate the multicast IP address.
291     //
292     Status = Snp->MCastIpToMac (
293                     Snp,
294                     Ipv6Flag,
295                     IpAddress,
296                     MacAddress
297                     );
298   }
299 
300 ON_EXIT:
301   gBS->RestoreTPL (OldTpl);
302 
303   return Status;
304 }
305 
306 /**
307   Enables and disables receive filters for multicast address. This function may
308   be unsupported in some MNP implementations.
309 
310   The Groups() function only adds and removes multicast MAC addresses from the
311   filter list. The MNP driver does not transmit or process Internet Group
312   Management Protocol (IGMP) packets. If JoinFlag is FALSE and MacAddress is
313   NULL, then all joined groups are left.
314 
315   @param[in]  This        Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
316   @param[in]  JoinFlag    Set to TRUE to join this multicast group.
317                           Set to FALSE to leave this multicast group.
318   @param[in]  MacAddress  Pointer to the multicast MAC group (address) to join or
319                           leave.
320 
321   @retval EFI_SUCCESS           The requested operation completed successfully.
322   @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
323                                 * This is NULL.
324                                 * JoinFlag is TRUE and MacAddress is NULL.
325                                 * MacAddress is not a valid multicast MAC
326                                   address.
327                                 * The MNP multicast group settings are
328                                   unchanged.
329   @retval EFI_NOT_STARTED       This MNP child driver instance has not been
330                                 configured.
331   @retval EFI_ALREADY_STARTED   The supplied multicast group is already joined.
332   @retval EFI_NOT_FOUND         The supplied multicast group is not joined.
333   @retval EFI_DEVICE_ERROR      An unexpected network or system error occurred.
334                                 The MNP child driver instance has been reset to
335                                 startup defaults.
336   @retval EFI_UNSUPPORTED       The requested feature is unsupported in this MNP
337                                 implementation.
338   @retval Others                The requested operation could not be completed.
339                                 The MNP multicast group settings are unchanged.
340 
341 **/
342 EFI_STATUS
343 EFIAPI
MnpGroups(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN BOOLEAN JoinFlag,IN EFI_MAC_ADDRESS * MacAddress OPTIONAL)344 MnpGroups (
345   IN EFI_MANAGED_NETWORK_PROTOCOL    *This,
346   IN BOOLEAN                         JoinFlag,
347   IN EFI_MAC_ADDRESS                 *MacAddress OPTIONAL
348   )
349 {
350   MNP_INSTANCE_DATA       *Instance;
351   EFI_SIMPLE_NETWORK_MODE *SnpMode;
352   MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk;
353   MNP_GROUP_ADDRESS       *GroupAddress;
354   LIST_ENTRY              *ListEntry;
355   BOOLEAN                 AddressExist;
356   EFI_TPL                 OldTpl;
357   EFI_STATUS              Status;
358 
359   if (This == NULL || (JoinFlag && (MacAddress == NULL))) {
360     //
361     // This is NULL, or it's a join operation but MacAddress is NULL.
362     //
363     return EFI_INVALID_PARAMETER;
364   }
365 
366   Instance  = MNP_INSTANCE_DATA_FROM_THIS (This);
367   SnpMode   = Instance->MnpServiceData->MnpDeviceData->Snp->Mode;
368 
369   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
370 
371   if (!Instance->Configured) {
372     Status = EFI_NOT_STARTED;
373     goto ON_EXIT;
374   }
375 
376   if ((!Instance->ConfigData.EnableMulticastReceive) ||
377     ((MacAddress != NULL) && !NET_MAC_IS_MULTICAST (MacAddress, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize))) {
378     //
379     // The instance isn't configured to do mulitcast receive. OR
380     // the passed in MacAddress is not a mutlticast mac address.
381     //
382     Status = EFI_INVALID_PARAMETER;
383     goto ON_EXIT;
384   }
385 
386   Status       = EFI_SUCCESS;
387   AddressExist = FALSE;
388   GroupCtrlBlk = NULL;
389 
390   if (MacAddress != NULL) {
391     //
392     // Search the instance's GroupCtrlBlkList to find the specific address.
393     //
394     NET_LIST_FOR_EACH (ListEntry, &Instance->GroupCtrlBlkList) {
395 
396       GroupCtrlBlk = NET_LIST_USER_STRUCT (
397                       ListEntry,
398                       MNP_GROUP_CONTROL_BLOCK,
399                       CtrlBlkEntry
400                       );
401       GroupAddress = GroupCtrlBlk->GroupAddress;
402       if (0 == CompareMem (
403                 MacAddress,
404                 &GroupAddress->Address,
405                 SnpMode->HwAddressSize
406                 )) {
407         //
408         // There is already the same multicast mac address configured.
409         //
410         AddressExist = TRUE;
411         break;
412       }
413     }
414 
415     if (JoinFlag && AddressExist) {
416       //
417       // The multicast mac address to join already exists.
418       //
419       Status = EFI_ALREADY_STARTED;
420     }
421 
422     if (!JoinFlag && !AddressExist) {
423       //
424       // The multicast mac address to leave doesn't exist in this instance.
425       //
426       Status = EFI_NOT_FOUND;
427     }
428 
429     if (EFI_ERROR (Status)) {
430       goto ON_EXIT;
431     }
432   } else if (IsListEmpty (&Instance->GroupCtrlBlkList)) {
433     //
434     // The MacAddress is NULL and there is no configured multicast mac address,
435     // just return.
436     //
437     goto ON_EXIT;
438   }
439 
440   //
441   // OK, it is time to take action.
442   //
443   Status = MnpGroupOp (Instance, JoinFlag, MacAddress, GroupCtrlBlk);
444 
445 ON_EXIT:
446   gBS->RestoreTPL (OldTpl);
447 
448   return Status;
449 }
450 
451 /**
452   Places asynchronous outgoing data packets into the transmit queue.
453 
454   The Transmit() function places a completion token into the transmit packet
455   queue. This function is always asynchronous.
456   The caller must fill in the Token.Event and Token.TxData fields in the
457   completion token, and these fields cannot be NULL. When the transmit operation
458   completes, the MNP updates the Token.Status field and the Token.Event is
459   signaled.
460   Note: There may be a performance penalty if the packet needs to be
461   defragmented before it can be transmitted by the network device. Systems in
462   which performance is critical should review the requirements and features of
463   the underlying communications device and drivers.
464 
465 
466   @param[in]  This    Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
467   @param[in]  Token   Pointer to a token associated with the transmit data
468                       descriptor. Type EFI_MANAGED_NETWORK_COMPLETION_TOKEN
469                       is defined in "Related Definitions" below.
470 
471   @retval EFI_SUCCESS            The transmit completion token was cached.
472   @retval EFI_NOT_STARTED        This MNP child driver instance has not been
473                                  configured.
474   @retval EFI_INVALID_PARAMETER  One or more of the following conditions is
475                                  TRUE:
476                                  * This is NULL.
477                                  * Token is NULL.
478                                  * Token.Event is NULL.
479                                  * Token.TxData is NULL.
480                                  * Token.TxData.DestinationAddress is not
481                                    NULL and Token.TxData.HeaderLength is zero.
482                                  * Token.TxData.FragmentCount is zero.
483                                  * (Token.TxData.HeaderLength +
484                                    Token.TxData.DataLength) is not equal to the
485                                    sum of the
486                                    Token.TxData.FragmentTable[].FragmentLength
487                                    fields.
488                                  * One or more of the
489                                    Token.TxData.FragmentTable[].FragmentLength
490                                    fields is zero.
491                                  * One or more of the
492                                    Token.TxData.FragmentTable[].FragmentBufferfields
493                                    is NULL.
494                                  * Token.TxData.DataLength is greater than MTU.
495   @retval EFI_ACCESS_DENIED      The transmit completion token is already in the
496                                  transmit queue.
497   @retval EFI_OUT_OF_RESOURCES   The transmit data could not be queued due to a
498                                  lack of system resources (usually memory).
499   @retval EFI_DEVICE_ERROR       An unexpected system or network error occurred.
500                                  The MNP child driver instance has been reset to
501                                  startup defaults.
502   @retval EFI_NOT_READY          The transmit request could not be queued because
503                                  the transmit queue is full.
504 
505 **/
506 EFI_STATUS
507 EFIAPI
MnpTransmit(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN * Token)508 MnpTransmit (
509   IN EFI_MANAGED_NETWORK_PROTOCOL            *This,
510   IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN    *Token
511   )
512 {
513   EFI_STATUS        Status;
514   MNP_INSTANCE_DATA *Instance;
515   MNP_SERVICE_DATA  *MnpServiceData;
516   UINT8             *PktBuf;
517   UINT32            PktLen;
518   EFI_TPL           OldTpl;
519 
520   if ((This == NULL) || (Token == NULL)) {
521     return EFI_INVALID_PARAMETER;
522   }
523 
524   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
525 
526   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
527 
528   if (!Instance->Configured) {
529 
530     Status = EFI_NOT_STARTED;
531     goto ON_EXIT;
532   }
533 
534   if (!MnpIsValidTxToken (Instance, Token)) {
535     //
536     // The Token is invalid.
537     //
538     Status = EFI_INVALID_PARAMETER;
539     goto ON_EXIT;
540   }
541 
542   MnpServiceData = Instance->MnpServiceData;
543   NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
544 
545   //
546   // Build the tx packet
547   //
548   Status = MnpBuildTxPacket (MnpServiceData, Token->Packet.TxData, &PktBuf, &PktLen);
549   if (EFI_ERROR (Status)) {
550     goto ON_EXIT;
551   }
552 
553   //
554   //  OK, send the packet synchronously.
555   //
556   Status = MnpSyncSendPacket (MnpServiceData, PktBuf, PktLen, Token);
557 
558 ON_EXIT:
559   gBS->RestoreTPL (OldTpl);
560 
561   return Status;
562 }
563 
564 
565 /**
566   Places an asynchronous receiving request into the receiving queue.
567 
568   The Receive() function places a completion token into the receive packet
569   queue. This function is always asynchronous.
570   The caller must fill in the Token.Event field in the completion token, and
571   this field cannot be NULL. When the receive operation completes, the MNP
572   updates the Token.Status and Token.RxData fields and the Token.Event is
573   signaled.
574 
575   @param[in]  This      Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
576   @param[in]  Token     Pointer to a token associated with the receive
577                         data descriptor. Type
578                         EFI_MANAGED_NETWORK_COMPLETION_TOKEN is defined in
579                         EFI_MANAGED_NETWORK_PROTOCOL.Transmit().
580 
581   @retval EFI_SUCCESS            The receive completion token was cached.
582   @retval EFI_NOT_STARTED        This MNP child driver instance has not been
583                                  configured.
584   @retval EFI_INVALID_PARAMETER  One or more of the following conditions is
585                                  TRUE:
586                                  * This is NULL.
587                                  * Token is NULL.
588                                  * Token.Event is NULL
589   @retval EFI_OUT_OF_RESOURCES   The transmit data could not be queued due to a
590                                  lack of system resources (usually memory).
591   @retval EFI_DEVICE_ERROR       An unexpected system or network error occurred.
592                                  The MNP child driver instance has been reset to
593                                  startup defaults.
594   @retval EFI_ACCESS_DENIED      The receive completion token was already in the
595                                  receive queue.
596   @retval EFI_NOT_READY          The receive request could not be queued because
597                                  the receive queue is full.
598 
599 **/
600 EFI_STATUS
601 EFIAPI
MnpReceive(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN * Token)602 MnpReceive (
603   IN EFI_MANAGED_NETWORK_PROTOCOL            *This,
604   IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN    *Token
605   )
606 {
607   EFI_STATUS         Status;
608   MNP_INSTANCE_DATA  *Instance;
609   EFI_TPL            OldTpl;
610 
611   if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
612     return EFI_INVALID_PARAMETER;
613   }
614 
615   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
616 
617   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
618 
619   if (!Instance->Configured) {
620     Status = EFI_NOT_STARTED;
621     goto ON_EXIT;
622   }
623 
624   //
625   // Check whether this token(event) is already in the rx token queue.
626   //
627   Status = NetMapIterate (&Instance->RxTokenMap, MnpTokenExist, (VOID *) Token);
628   if (EFI_ERROR (Status)) {
629     goto ON_EXIT;
630   }
631 
632   //
633   // Insert the Token into the RxTokenMap.
634   //
635   Status = NetMapInsertTail (&Instance->RxTokenMap, (VOID *) Token, NULL);
636   if (!EFI_ERROR (Status)) {
637     //
638     // Try to deliver any buffered packets.
639     //
640     Status = MnpInstanceDeliverPacket (Instance);
641 
642     //
643     // Dispatch the DPC queued by the NotifyFunction of Token->Event.
644     //
645     DispatchDpc ();
646   }
647 
648 ON_EXIT:
649   gBS->RestoreTPL (OldTpl);
650 
651   return Status;
652 }
653 
654 /**
655   Aborts an asynchronous transmit or receive request.
656 
657   The Cancel() function is used to abort a pending transmit or receive request.
658   If the token is in the transmit or receive request queues, after calling this
659   function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
660   signaled. If the token is not in one of the queues, which usually means that
661   the asynchronous operation has completed, this function will not signal the
662   token and EFI_NOT_FOUND is returned.
663 
664   @param[in]  This     Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
665   @param[in]  Token    Pointer to a token that has been issued by
666                        EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or
667                        EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL, all
668                        pending tokens are aborted.
669 
670   @retval EFI_SUCCESS            The asynchronous I/O request was aborted and
671                                  Token.Event was signaled. When Token is NULL,
672                                  all pending requests were aborted and their
673                                  events were signaled.
674   @retval EFI_NOT_STARTED        This MNP child driver instance has not been
675                                  configured.
676   @retval EFI_INVALID_PARAMETER  This is NULL.
677   @retval EFI_NOT_FOUND          When Token is not NULL, the asynchronous I/O
678                                  request was not found in the transmit or
679                                  receive queue. It has either completed or was
680                                  not issued by Transmit() and Receive().
681 
682 **/
683 EFI_STATUS
684 EFIAPI
MnpCancel(IN EFI_MANAGED_NETWORK_PROTOCOL * This,IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN * Token OPTIONAL)685 MnpCancel (
686   IN EFI_MANAGED_NETWORK_PROTOCOL            *This,
687   IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN    *Token OPTIONAL
688   )
689 {
690   EFI_STATUS         Status;
691   MNP_INSTANCE_DATA  *Instance;
692   EFI_TPL            OldTpl;
693 
694   if (This == NULL) {
695     return EFI_INVALID_PARAMETER;
696   }
697 
698   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
699 
700   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
701 
702   if (!Instance->Configured) {
703     Status = EFI_NOT_STARTED;
704     goto ON_EXIT;
705   }
706 
707   //
708   // Iterate the RxTokenMap to cancel the specified Token.
709   //
710   Status = NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, (VOID *) Token);
711   if (Token != NULL) {
712     Status = (Status == EFI_ABORTED) ? EFI_SUCCESS : EFI_NOT_FOUND;
713   }
714 
715   //
716   // Dispatch the DPC queued by the NotifyFunction of the cancled token's events.
717   //
718   DispatchDpc ();
719 
720 ON_EXIT:
721   gBS->RestoreTPL (OldTpl);
722 
723   return Status;
724 }
725 
726 /**
727   Polls for incoming data packets and processes outgoing data packets.
728 
729   The Poll() function can be used by network drivers and applications to
730   increase the rate that data packets are moved between the communications
731   device and the transmit and receive queues.
732   Normally, a periodic timer event internally calls the Poll() function. But, in
733   some systems, the periodic timer event may not call Poll() fast enough to
734   transmit and/or receive all data packets without missing packets. Drivers and
735   applications that are experiencing packet loss should try calling the Poll()
736   function more often.
737 
738   @param[in]  This         Pointer to the EFI_MANAGED_NETWORK_PROTOCOL instance.
739 
740   @retval EFI_SUCCESS      Incoming or outgoing data was processed.
741   @retval EFI_NOT_STARTED  This MNP child driver instance has not been
742                            configured.
743   @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The
744                            MNP child driver instance has been reset to startup
745                            defaults.
746   @retval EFI_NOT_READY    No incoming or outgoing data was processed. Consider
747                            increasing the polling rate.
748   @retval EFI_TIMEOUT      Data was dropped out of the transmit and/or receive
749                            queue. Consider increasing the polling rate.
750 
751 **/
752 EFI_STATUS
753 EFIAPI
MnpPoll(IN EFI_MANAGED_NETWORK_PROTOCOL * This)754 MnpPoll (
755   IN EFI_MANAGED_NETWORK_PROTOCOL    *This
756   )
757 {
758   EFI_STATUS         Status;
759   MNP_INSTANCE_DATA  *Instance;
760   EFI_TPL            OldTpl;
761 
762   if (This == NULL) {
763     return EFI_INVALID_PARAMETER;
764   }
765 
766   Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
767 
768   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
769 
770   if (!Instance->Configured) {
771     Status = EFI_NOT_STARTED;
772     goto ON_EXIT;
773   }
774 
775   //
776   // Try to receive packets.
777   //
778   Status = MnpReceivePacket (Instance->MnpServiceData->MnpDeviceData);
779 
780   //
781   // Dispatch the DPC queued by the NotifyFunction of rx token's events.
782   //
783   DispatchDpc ();
784 
785 ON_EXIT:
786   gBS->RestoreTPL (OldTpl);
787 
788   return Status;
789 }
790