]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/NetLib: Add NetLibDetectMediaWaitTimeout() API to support EFI_NOT_READY...
authorfanwang2 <fan.wang@intel.com>
Wed, 6 Dec 2017 05:00:21 +0000 (13:00 +0800)
committerJiaxin Wu <jiaxin.wu@intel.com>
Wed, 6 Dec 2017 05:10:05 +0000 (13:10 +0800)
In wireless connection, connecting state needs to be cared more
about. ECR 1772 redefined the state EFI_NOT_READY to represent
connecting state and can be retrieved from Aip protocol. This
patch adds a new API to check media state at a specified time
interval when network is connecting until the connection process
finishes or timeout.

V2:
  * Return error status code directly when Aip protocol falied to detect
    media rather than wait for another time's check.
  * Set media state default value to EFI_SUCCESS since some platforms may
    not support retrieving media state from Aip protocol.

Cc: Fu Siyuan <siyuan.fu@intel.com>
Cc: Ye Ting <ting.ye@intel.com>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Wang Fan <fan.wang@intel.com>
Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>
Reviewed-by: Fu Siyuan <siyuan.fu@intel.com>
MdeModulePkg/Include/Library/NetLib.h
MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
MdeModulePkg/Library/DxeNetLib/DxeNetLib.inf

index b9df46c50f63f73671654e246bdb37d8caf8d603..7862df9d43adf5b5cda7c9f88c0b3ace45fc322c 100644 (file)
@@ -95,6 +95,12 @@ typedef UINT16          TCP_PORTNO;
 #define  DNS_CLASS_HS          4\r
 #define  DNS_CLASS_ANY         255\r
 \r
+//\r
+// Number of 100ns units time Interval for network media state detect\r
+//\r
+#define MEDIA_STATE_DETECT_TIME_INTERVAL  1000000U\r
+\r
+\r
 #pragma pack(1)\r
 \r
 //\r
@@ -1247,6 +1253,40 @@ NetLibDetectMedia (
   OUT BOOLEAN               *MediaPresent\r
   );\r
 \r
+/**\r
+\r
+  Detect media state for a network device. This routine will wait for a period of time at \r
+  a specified checking interval when a certain network is under connecting until connection \r
+  process finishes or timeout. If Aip protocol is supported by low layer drivers, three kinds\r
+  of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents\r
+  connected state, connecting state and no media state respectively. When function detects \r
+  the current state is EFI_NOT_READY, it will loop to wait for next time's check until state \r
+  turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will \r
+  call NetLibDetectMedia() and return state directly.\r
+\r
+  @param[in]   ServiceHandle    The handle where network service binding protocols are\r
+                                installed on.\r
+  @param[in]   Timeout          The maximum number of 100ns units to wait when network\r
+                                is connecting. Zero value means detect once and return\r
+                                immediately.\r
+  @param[out]  MediaState       The pointer to the detected media state.\r
+\r
+  @retval EFI_SUCCESS           Media detection success.\r
+  @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or \r
+                                MediaState pointer is NULL.\r
+  @retval EFI_DEVICE_ERROR      A device error occurred.\r
+  @retval EFI_TIMEOUT           Network is connecting but timeout.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibDetectMediaWaitTimeout (\r
+  IN  EFI_HANDLE            ServiceHandle,\r
+  IN  UINT64                Timeout,\r
+  OUT EFI_STATUS            *MediaState\r
+  );\r
+\r
+\r
 /**\r
   Create an IPv4 device path node.\r
 \r
index b8544b89ab8d3f5bee7b5e94c1eb12f615b170c2..1bfa33d58d1a07b479b737d6d9a8a504082e9801 100644 (file)
@@ -19,6 +19,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/DriverBinding.h>\r
 #include <Protocol/ServiceBinding.h>\r
 #include <Protocol/SimpleNetwork.h>\r
+#include <Protocol/AdapterInformation.h>\r
 #include <Protocol/ManagedNetwork.h>\r
 #include <Protocol/Ip4Config2.h>\r
 #include <Protocol/ComponentName.h>\r
@@ -2502,6 +2503,168 @@ Exit:
   return Status;\r
 }\r
 \r
+/**\r
+\r
+  Detect media state for a network device. This routine will wait for a period of time at \r
+  a specified checking interval when a certain network is under connecting until connection \r
+  process finishs or timeout. If Aip protocol is supported by low layer drivers, three kinds\r
+  of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents\r
+  connected state, connecting state and no media state respectively. When function detects \r
+  the current state is EFI_NOT_READY, it will loop to wait for next time's check until state \r
+  turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will \r
+  call NetLibDetectMedia() and return state directly.\r
+\r
+  @param[in]   ServiceHandle    The handle where network service binding protocols are\r
+                                installed on.\r
+  @param[in]   Timeout          The maximum number of 100ns units to wait when network\r
+                                is connecting. Zero value means detect once and return\r
+                                immediately.\r
+  @param[out]  MediaState       The pointer to the detected media state.\r
+\r
+  @retval EFI_SUCCESS           Media detection success.\r
+  @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or \r
+                                MediaState pointer is NULL.\r
+  @retval EFI_DEVICE_ERROR      A device error occurred.\r
+  @retval EFI_TIMEOUT           Network is connecting but timeout.\r
+\r
+**/\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+NetLibDetectMediaWaitTimeout (\r
+  IN  EFI_HANDLE            ServiceHandle,\r
+  IN  UINT64                Timeout,\r
+  OUT EFI_STATUS            *MediaState\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_HANDLE                        SnpHandle;\r
+  EFI_SIMPLE_NETWORK_PROTOCOL       *Snp;\r
+  EFI_ADAPTER_INFORMATION_PROTOCOL  *Aip;\r
+  EFI_ADAPTER_INFO_MEDIA_STATE      *MediaInfo;\r
+  BOOLEAN                           MediaPresent;\r
+  UINTN                             DataSize;\r
+  EFI_STATUS                        TimerStatus;\r
+  EFI_EVENT                         Timer;\r
+  UINT64                            TimeRemained;\r
+\r
+  if (MediaState == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  *MediaState = EFI_SUCCESS;\r
+  MediaInfo   = NULL;\r
+\r
+  //\r
+  // Get SNP handle\r
+  //\r
+  Snp = NULL;\r
+  SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp);\r
+  if (SnpHandle == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = gBS->HandleProtocol (\r
+                  SnpHandle,\r
+                  &gEfiAdapterInformationProtocolGuid,\r
+                  (VOID *) &Aip\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+\r
+    MediaPresent = TRUE;\r
+    Status = NetLibDetectMedia (ServiceHandle, &MediaPresent);\r
+    if (!EFI_ERROR (Status)) {\r
+      if (MediaPresent == TRUE) {\r
+        *MediaState = EFI_SUCCESS;\r
+      } else {\r
+        *MediaState = EFI_NO_MEDIA;\r
+      }\r
+    }\r
+\r
+    //\r
+    // NetLibDetectMedia doesn't support EFI_NOT_READY status, return now!\r
+    //\r
+    return Status;\r
+  }\r
+\r
+  Status = Aip->GetInformation (\r
+                  Aip,\r
+                  &gEfiAdapterInfoMediaStateGuid,\r
+                  (VOID **) &MediaInfo,\r
+                  &DataSize\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+\r
+    *MediaState = MediaInfo->MediaState;\r
+    FreePool (MediaInfo);\r
+    if (*MediaState != EFI_NOT_READY || Timeout < MEDIA_STATE_DETECT_TIME_INTERVAL) {\r
+\r
+      return EFI_SUCCESS;\r
+    }\r
+  } else {\r
+\r
+    if (MediaInfo != NULL) {\r
+      FreePool (MediaInfo);\r
+    }\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Loop to check media state \r
+  //\r
+\r
+  Timer        = NULL;\r
+  TimeRemained = Timeout;\r
+  Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  do {\r
+    Status = gBS->SetTimer (\r
+                    Timer,\r
+                    TimerRelative,\r
+                    MEDIA_STATE_DETECT_TIME_INTERVAL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->CloseEvent(Timer);\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    do {\r
+      TimerStatus = gBS->CheckEvent (Timer);\r
+      if (!EFI_ERROR (TimerStatus)) {\r
+\r
+        TimeRemained -= MEDIA_STATE_DETECT_TIME_INTERVAL;\r
+        Status = Aip->GetInformation (\r
+                        Aip,\r
+                        &gEfiAdapterInfoMediaStateGuid,\r
+                        (VOID **) &MediaInfo,\r
+                        &DataSize\r
+                        );\r
+        if (!EFI_ERROR (Status)) {\r
+\r
+          *MediaState = MediaInfo->MediaState;\r
+          FreePool (MediaInfo);\r
+        } else {\r
+\r
+          if (MediaInfo != NULL) {\r
+            FreePool (MediaInfo);\r
+          }\r
+          gBS->CloseEvent(Timer);\r
+          return Status;\r
+        }\r
+      }\r
+    } while (TimerStatus == EFI_NOT_READY);\r
+  } while (*MediaState == EFI_NOT_READY && TimeRemained >= MEDIA_STATE_DETECT_TIME_INTERVAL);\r
+\r
+  gBS->CloseEvent(Timer);\r
+  if (*MediaState == EFI_NOT_READY && TimeRemained < MEDIA_STATE_DETECT_TIME_INTERVAL) {\r
+    return EFI_TIMEOUT;\r
+  } else {\r
+    return EFI_SUCCESS;\r
+  }\r
+}\r
+\r
 /**\r
   Check the default address used by the IPv4 driver is static or dynamic (acquired\r
   from DHCP).\r
index 1ff3a4fe556a5cc75c75aea0bf027da0f6d9f946..ad0727c42f5b01a9df2bb7aa44e813a8bd88bb7e 100644 (file)
@@ -54,6 +54,7 @@
 [Guids]\r
   gEfiSmbiosTableGuid                           ## SOMETIMES_CONSUMES  ## SystemTable\r
   gEfiSmbios3TableGuid                          ## SOMETIMES_CONSUMES  ## SystemTable\r
+  gEfiAdapterInfoMediaStateGuid                 ## SOMETIMES_CONSUMES\r
 \r
 \r
 [Protocols]\r
@@ -63,3 +64,4 @@
   gEfiIp4Config2ProtocolGuid                    ## SOMETIMES_CONSUMES\r
   gEfiComponentNameProtocolGuid                 ## SOMETIMES_CONSUMES\r
   gEfiComponentName2ProtocolGuid                ## SOMETIMES_CONSUMES\r
+  gEfiAdapterInformationProtocolGuid            ## SOMETIMES_CONSUMES
\ No newline at end of file