/** Set up the Simple Network Protocol fields, the Simple Network Mode fields, and the Exit Boot Services Event of the virtio-net driver instance. This function may only be called by VirtioNetDriverBindingStart(). @param[in,out] Dev The VNET_DEV driver instance being created for the virtio-net device. @return Status codes from the CreateEvent() boot service or the VirtioNetGetFeatures() function. @retval EFI_SUCCESS Configuration successful. */ STATIC EFI_STATUS EFIAPI VirtioNetSnpPopulate ( IN OUT VNET_DEV *Dev ) { EFI_STATUS Status; // // We set up a function here that is asynchronously callable by an // external application to check if there are any packets available for // reception. The least urgent task priority level we can specify for such a // "software interrupt" is TPL_CALLBACK. // // TPL_CALLBACK is also the maximum TPL an SNP implementation is allowed to // run at (see 6.1 Event, Timer, and Task Priority Services in the UEFI // Specification 2.3.1+errC). // // Since we raise our TPL to TPL_CALLBACK in every single function that // accesses the device, and the external application also queues its interest // for received packets at the same TPL_CALLBACK, in effect the // VirtioNetIsPacketAvailable() function will never interrupt any // device-accessing driver function, it will be scheduled in isolation. // // TPL_CALLBACK (which basically this entire driver runs at) is allowed // for "[l]ong term operations (such as file system operations and disk // I/O)". Because none of our functions block, we'd satisfy an even stronger // requirement. // Status = gBS->CreateEvent (EVT_NOTIFY_WAIT, TPL_CALLBACK, &VirtioNetIsPacketAvailable, Dev, &Dev->Snp.WaitForPacket); if (EFI_ERROR (Status)) { return Status; } Dev->Snp.Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; Dev->Snp.Start = &VirtioNetStart; Dev->Snp.Stop = &VirtioNetStop; Dev->Snp.Initialize = &VirtioNetInitialize; Dev->Snp.Reset = &VirtioNetReset; Dev->Snp.Shutdown = &VirtioNetShutdown; Dev->Snp.ReceiveFilters = &VirtioNetReceiveFilters; Dev->Snp.StationAddress = &VirtioNetStationAddress; Dev->Snp.Statistics = &VirtioNetStatistics; Dev->Snp.MCastIpToMac = &VirtioNetMcastIpToMac; Dev->Snp.NvData = &VirtioNetNvData; Dev->Snp.GetStatus = &VirtioNetGetStatus; Dev->Snp.Transmit = &VirtioNetTransmit; Dev->Snp.Receive = &VirtioNetReceive; Dev->Snp.Mode = &Dev->Snm; Dev->Snm.State = EfiSimpleNetworkStopped; Dev->Snm.HwAddressSize = SIZE_OF_VNET (Mac); Dev->Snm.MediaHeaderSize = SIZE_OF_VNET (Mac) + // dst MAC SIZE_OF_VNET (Mac) + // src MAC 2; // Ethertype Dev->Snm.MaxPacketSize = 1500; Dev->Snm.NvRamSize = 0; Dev->Snm.NvRamAccessSize = 0; Dev->Snm.ReceiveFilterMask = RECEIVE_FILTERS_NO_MCAST; Dev->Snm.ReceiveFilterSetting = RECEIVE_FILTERS_NO_MCAST; Dev->Snm.MaxMCastFilterCount = 0; Dev->Snm.MCastFilterCount = 0; Dev->Snm.IfType = 1; // ethernet Dev->Snm.MacAddressChangeable = FALSE; Dev->Snm.MultipleTxSupported = TRUE; ASSERT (SIZE_OF_VNET (Mac) <= sizeof (EFI_MAC_ADDRESS)); Status = VirtioNetGetFeatures (Dev, &Dev->Snm.CurrentAddress, &Dev->Snm.MediaPresentSupported, &Dev->Snm.MediaPresent); if (EFI_ERROR (Status)) { goto CloseWaitForPacket; } CopyMem (&Dev->Snm.PermanentAddress, &Dev->Snm.CurrentAddress, SIZE_OF_VNET (Mac)); SetMem (&Dev->Snm.BroadcastAddress, SIZE_OF_VNET (Mac), 0xFF); // // VirtioNetExitBoot() is queued by ExitBootServices(); its purpose is to // cancel any pending virtio requests. The TPL_CALLBACK reasoning is // identical to the one above. There's one difference: this kind of // event is "globally visible", which means it can be signalled as soon as // we create it. We haven't raised our TPL here, hence VirtioNetExitBoot() // could be entered immediately. VirtioNetExitBoot() checks Dev->Snm.State, // so we're safe. // Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, &VirtioNetExitBoot, Dev, &Dev->ExitBoot); if (EFI_ERROR (Status)) { goto CloseWaitForPacket; } return EFI_SUCCESS; CloseWaitForPacket: gBS->CloseEvent (Dev->Snp.WaitForPacket); return Status; }
/* 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; }
/* 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; }