Esempio n. 1
0
/*
  Temporarily enable then reset the virtio-net device in order to retrieve
  configuration values needed by Simple Network Protocol and Simple Network
  Mode fields.

  Only VirtioNetSnpPopulate() may call this function.

  If the function fails for any reason, the virtio-net device is moved to
  VSTAT_FAILED instead of being reset. This serves only informative purposes
  for the host side.

  param[in,out] Dev                 The VNET_DEV structure being created for
                                    the virtio-net device.
  param[out] MacAddress             MAC address configured by the host.
  param[out] MediaPresentSupported  Link status is made available by the host.
  param[out] MediaPresent           If link status is made available by the
                                    host, the current link status is stored in
                                    *MediaPresent. Otherwise MediaPresent is
                                    unused.

  @retval EFI_UNSUPPORTED           The host doesn't supply a MAC address.
  @return                           Status codes from VirtIo protocol members.
  @retval EFI_SUCCESS               Configuration values retrieved.
*/
STATIC
EFI_STATUS
EFIAPI
VirtioNetGetFeatures (
  IN OUT  VNET_DEV        *Dev,
  OUT     EFI_MAC_ADDRESS *MacAddress,
  OUT     BOOLEAN         *MediaPresentSupported,
  OUT     BOOLEAN         *MediaPresent
  )
{
  EFI_STATUS Status;
  UINT8      NextDevStat;
  UINT32     Features;
  UINTN      MacIdx;
  UINT16     LinkStatus;

  //
  // Interrogate the device for features (virtio-0.9.5, 2.2.1 Device
  // Initialization Sequence), but don't complete setting it up.
  //
  NextDevStat = 0;             // step 1 -- reset device
  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  NextDevStat |= VSTAT_ACK;    // step 2 -- acknowledge device presence
  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  //
  // step 4a -- retrieve and validate features
  //
  Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  //
  // get MAC address byte-wise
  //
  if ((Features & VIRTIO_NET_F_MAC) == 0) {
    Status = EFI_UNSUPPORTED;
    goto YieldDevice;
  }
  for (MacIdx = 0; MacIdx < SIZE_OF_VNET (Mac); ++MacIdx) {
    Status = Dev->VirtIo->ReadDevice (Dev->VirtIo,
                            OFFSET_OF_VNET (Mac) + MacIdx, // Offset
                            1,                             // FieldSize
                            1,                             // BufferSize
                            &MacAddress->Addr[MacIdx]      // Buffer
                            );
    if (EFI_ERROR (Status)) {
      goto YieldDevice;
    }
  }

  //
  // check if link status is reported, and if so, what the link status is
  //
  if ((Features & VIRTIO_NET_F_STATUS) == 0) {
    *MediaPresentSupported = FALSE;
  }
  else {
    *MediaPresentSupported = TRUE;
    Status = VIRTIO_CFG_READ (Dev, LinkStatus, &LinkStatus);
    if (EFI_ERROR (Status)) {
      goto YieldDevice;
    }
    *MediaPresent = !!(LinkStatus & VIRTIO_NET_S_LINK_UP);
  }

YieldDevice:
  Dev->VirtIo->SetDeviceStatus (Dev->VirtIo,
    EFI_ERROR (Status) ? VSTAT_FAILED : 0);

  return Status;
}
Esempio n. 2
0
/*
  Temporarily enable then reset the virtio-net device in order to retrieve
  configuration values needed by Simple Network Protocol and Simple Network
  Mode fields.

  Only VirtioNetSnpPopulate() may call this function.

  If the function fails for any reason, the virtio-net device is moved to
  VSTAT_FAILED instead of being reset. This serves only informative purposes
  for the host side.

  param[in,out] Dev                 The VNET_DEV structure being created for
                                    the virtio-net device.
  param[out] MacAddress             MAC address configured by the host.
  param[out] MediaPresentSupported  Link status is made available by the host.
  param[out] MediaPresent           If link status is made available by the
                                    host, the current link status is stored in
                                    *MediaPresent. Otherwise MediaPresent is
                                    unused.

  @retval EFI_UNSUPPORTED           The host doesn't supply a MAC address.
  @return                           Status codes from Dev->PciIo->Io.Read(),
                                    VIRTIO_CFG_READ() and VIRTIO_CFG_WRITE().
  @retval EFI_SUCCESS               Configuration values retrieved.
*/
STATIC
EFI_STATUS
EFIAPI
VirtioNetGetFeatures (
  IN OUT  VNET_DEV        *Dev,
  OUT     EFI_MAC_ADDRESS *MacAddress,
  OUT     BOOLEAN         *MediaPresentSupported,
  OUT     BOOLEAN         *MediaPresent
  )
{
  EFI_STATUS Status;
  UINT8      NextDevStat;
  UINT32     Features;
  UINT16     LinkStatus;

  //
  // Interrogate the device for features (virtio-0.9.5, 2.2.1 Device
  // Initialization Sequence), but don't complete setting it up.
  //
  NextDevStat = 0;             // step 1 -- reset device
  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  NextDevStat |= VSTAT_ACK;    // step 2 -- acknowledge device presence
  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  //
  // step 4a -- retrieve and validate features
  //
  Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);
  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  //
  // get MAC address byte-wise
  //
  if ((Features & VIRTIO_NET_F_MAC) == 0) {
    Status = EFI_UNSUPPORTED;
    goto YieldDevice;
  }
  Status = Dev->PciIo->Io.Read (Dev->PciIo,           // PciIo
                            EfiPciIoWidthUint8,       // Width
                            PCI_BAR_IDX0,             // BarIndex
                            OFFSET_OF_VNET (VhdrMac), // Offset
                            SIZE_OF_VNET (VhdrMac),   // Count
                            MacAddress                // Buffer
                            );

  if (EFI_ERROR (Status)) {
    goto YieldDevice;
  }

  //
  // check if link status is reported, and if so, what the link status is
  //
  if ((Features & VIRTIO_NET_F_STATUS) == 0) {
    *MediaPresentSupported = FALSE;
  }
  else {
    *MediaPresentSupported = TRUE;
    Status = VIRTIO_CFG_READ (Dev, VhdrLinkStatus, &LinkStatus);
    if (EFI_ERROR (Status)) {
      goto YieldDevice;
    }
    *MediaPresent = !!(LinkStatus & VIRTIO_NET_S_LINK_UP);
  }

YieldDevice:
  VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus,
    EFI_ERROR (Status) ? VSTAT_FAILED : 0);

  return Status;
}