From 1e0ebb760f0e3e9efd86909ea8c3eab7b7bb5bc6 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 11 May 2020 17:04:57 +0200 Subject: [PATCH] pci: Allow specific PCI b/d/f to be reserved In order to let the PciBus user choose where a device should be placed on the bus, a new function get_device_id() is introduced. This will be helpful in the context of snapshot/restore as the caller will be able to place the PCI devices on the same slot they were placed before the snapshot was taken. Signed-off-by: Sebastien Boeuf --- pci/src/bus.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pci/src/bus.rs b/pci/src/bus.rs index 7cad62d8a..c29f5bd85 100644 --- a/pci/src/bus.rs +++ b/pci/src/bus.rs @@ -34,6 +34,8 @@ pub enum PciRootError { NoPciDeviceSlotAvailable, /// Invalid PCI device identifier provided. InvalidPciDeviceSlot(usize), + /// Valid PCI device identifier but already used. + AlreadyInUsePciDeviceSlot(usize), } pub type Result = std::result::Result; @@ -155,6 +157,19 @@ impl PciBus { Err(PciRootError::NoPciDeviceSlotAvailable) } + pub fn get_device_id(&mut self, id: usize) -> Result<()> { + if id < NUM_DEVICE_IDS { + if !self.device_ids[id] { + self.device_ids[id] = true; + Ok(()) + } else { + Err(PciRootError::AlreadyInUsePciDeviceSlot(id)) + } + } else { + Err(PciRootError::InvalidPciDeviceSlot(id)) + } + } + pub fn put_device_id(&mut self, id: usize) -> Result<()> { if id < NUM_DEVICE_IDS { self.device_ids[id] = false;