From 5dfd5f7e7cc16d0edb746b957b8c5496500a6e86 Mon Sep 17 00:00:00 2001 From: Wren Turkal Date: Mon, 27 Jul 2020 22:49:39 -0700 Subject: [PATCH] Fixed PCI bus scan to scan bus 0xFF. Previously, the PCI bus scan was skipping bus 0xFF. Now it does not. I used a match expression to make sure that all cases are accounted for. I also changed the PCI dev scan and PCI func scan to use a match expression in a similar way to make sure all cases are account. While this is functionally the same as before, the match expression will not allow unhandled cases and should be easier to read and make it harder to introduce bugs. Signed-off-by: Wren Turkal --- pcid/src/pci/bus.rs | 21 +++++++++++---------- pcid/src/pci/dev.rs | 21 +++++++++++---------- pcid/src/pci/mod.rs | 23 ++++++++++++----------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/pcid/src/pci/bus.rs b/pcid/src/pci/bus.rs index 7678fad2fc..4c2a931f3f 100644 --- a/pcid/src/pci/bus.rs +++ b/pcid/src/pci/bus.rs @@ -20,7 +20,7 @@ impl<'pci> PciBus<'pci> { pub struct PciBusIter<'pci> { bus: &'pci PciBus<'pci>, - num: u32 + num: u8 } impl<'pci> PciBusIter<'pci> { @@ -35,15 +35,16 @@ impl<'pci> PciBusIter<'pci> { impl<'pci> Iterator for PciBusIter<'pci> { type Item = PciDev<'pci>; fn next(&mut self) -> Option { - if self.num < 32 { - let dev = PciDev { - bus: self.bus, - num: self.num as u8 - }; - self.num += 1; - Some(dev) - } else { - None + match self.num { + dev_num if dev_num < 32 => { + let dev = PciDev { + bus: self.bus, + num: self.num + }; + self.num += 1; + Some(dev) + }, + _ => None, } } } diff --git a/pcid/src/pci/dev.rs b/pcid/src/pci/dev.rs index 7cb7aa2926..ca2e505bb4 100644 --- a/pcid/src/pci/dev.rs +++ b/pcid/src/pci/dev.rs @@ -20,7 +20,7 @@ impl<'pci> PciDev<'pci> { pub struct PciDevIter<'pci> { dev: &'pci PciDev<'pci>, - num: u32 + num: u8 } impl<'pci> PciDevIter<'pci> { @@ -35,15 +35,16 @@ impl<'pci> PciDevIter<'pci> { impl<'pci> Iterator for PciDevIter<'pci> { type Item = PciFunc<'pci>; fn next(&mut self) -> Option { - if self.num < 8 { - let func = PciFunc { - dev: self.dev, - num: self.num as u8 - }; - self.num += 1; - Some(func) - } else { - None + match self.num { + func_num if func_num < 8 => { + let func = PciFunc { + dev: self.dev, + num: self.num + }; + self.num += 1; + Some(func) + }, + _ => None, } } } diff --git a/pcid/src/pci/mod.rs b/pcid/src/pci/mod.rs index 3307c857c2..6bc80ebb0e 100644 --- a/pcid/src/pci/mod.rs +++ b/pcid/src/pci/mod.rs @@ -106,14 +106,14 @@ impl CfgAccess for Pci { pub struct PciIter<'pci> { pci: &'pci dyn CfgAccess, - num: u32 + num: Option } impl<'pci> PciIter<'pci> { pub fn new(pci: &'pci dyn CfgAccess) -> Self { PciIter { pci, - num: 0 + num: Some(0) } } } @@ -121,15 +121,16 @@ impl<'pci> PciIter<'pci> { impl<'pci> Iterator for PciIter<'pci> { type Item = PciBus<'pci>; fn next(&mut self) -> Option { - if self.num < 255 { /* TODO: Do not ignore 0xFF bus */ - let bus = PciBus { - pci: self.pci, - num: self.num as u8 - }; - self.num += 1; - Some(bus) - } else { - None + match self.num { + Some(bus_num) => { + let bus = PciBus { + pci: self.pci, + num: bus_num + }; + self.num = bus_num.checked_add(1); + Some(bus) + }, + None => None, } } }