Skip to content

Commit

Permalink
Merge pull request #1534 from hermit-os/all-features
Browse files Browse the repository at this point in the history
fix: clippy all features
  • Loading branch information
mkroening authored Jan 2, 2025
2 parents 1042193 + 0f81173 commit 4b1549a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 69 deletions.
6 changes: 3 additions & 3 deletions src/arch/riscv64/kernel/devicetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use core::ptr::NonNull;

use fdt::Fdt;
#[cfg(all(feature = "tcp", feature = "gem-net"))]
#[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))]
use memory_addresses::VirtAddr;
use memory_addresses::{AddrRange, PhysAddr};
#[cfg(all(feature = "tcp", not(feature = "pci")))]
Expand All @@ -15,7 +15,7 @@ use crate::arch::riscv64::kernel::interrupts::init_plic;
#[cfg(all(feature = "tcp", not(feature = "pci")))]
use crate::arch::riscv64::kernel::mmio::MmioDriver;
use crate::arch::riscv64::mm::paging;
#[cfg(feature = "gem-net")]
#[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))]
use crate::drivers::net::gem;
#[cfg(all(feature = "tcp", not(feature = "pci"), not(feature = "gem-net")))]
use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver};
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn init_drivers() {
}

// Init GEM
#[cfg(all(feature = "tcp", feature = "gem-net"))]
#[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))]
if let Some(gem_node) = fdt.find_compatible(&["sifive,fu540-c000-gem"]) {
debug!("Found Ethernet controller");

Expand Down
3 changes: 1 addition & 2 deletions src/arch/x86_64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::collections::BTreeMap;
use core::arch::asm;
use core::ptr;
use core::sync::atomic::{AtomicU64, Ordering};

use ahash::RandomState;
Expand Down Expand Up @@ -53,7 +52,7 @@ pub(crate) fn enable_and_wait() {

#[cfg(not(feature = "idle-poll"))]
if crate::processor::supports_mwait() {
let addr = ptr::from_ref(core_scheduler().get_priority_bitmap()).cast::<u8>();
let addr = core::ptr::from_ref(core_scheduler().get_priority_bitmap()).cast::<u8>();

unsafe {
if crate::processor::supports_clflush() {
Expand Down
16 changes: 8 additions & 8 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub(crate) type InterruptHandlerQueue = VecDeque<fn()>;
pub mod error {
use core::fmt;

#[cfg(feature = "gem-net")]
#[cfg(all(target_arch = "riscv64", feature = "gem-net"))]
use crate::drivers::net::gem::GEMError;
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
use crate::drivers::net::rtl8139::RTL8139Error;
#[cfg(any(
all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")),
Expand All @@ -51,9 +51,9 @@ pub mod error {
feature = "vsock"
))]
InitVirtioDevFail(VirtioError),
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
InitRTL8139DevFail(RTL8139Error),
#[cfg(feature = "gem-net")]
#[cfg(all(target_arch = "riscv64", feature = "gem-net"))]
InitGEMDevFail(GEMError),
}

Expand All @@ -68,14 +68,14 @@ pub mod error {
}
}

#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
impl From<RTL8139Error> for DriverError {
fn from(err: RTL8139Error) -> Self {
DriverError::InitRTL8139DevFail(err)
}
}

#[cfg(feature = "gem-net")]
#[cfg(all(target_arch = "riscv64", feature = "gem-net"))]
impl From<GEMError> for DriverError {
fn from(err: GEMError) -> Self {
DriverError::InitGEMDevFail(err)
Expand All @@ -94,11 +94,11 @@ pub mod error {
DriverError::InitVirtioDevFail(ref err) => {
write!(f, "Virtio driver failed: {err:?}")
}
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
DriverError::InitRTL8139DevFail(ref err) => {
write!(f, "RTL8139 driver failed: {err:?}")
}
#[cfg(feature = "gem-net")]
#[cfg(all(target_arch = "riscv64", feature = "gem-net"))]
DriverError::InitGEMDevFail(ref err) => {
write!(f, "GEM driver failed: {err:?}")
}
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(all(target_arch = "riscv64", feature = "gem-net"))]
pub mod gem;
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
pub mod rtl8139;
#[cfg(not(feature = "rtl8139"))]
#[cfg(not(all(target_arch = "x86_64", feature = "rtl8139")))]
pub mod virtio;

use smoltcp::phy::ChecksumCapabilities;
Expand Down
60 changes: 30 additions & 30 deletions src/drivers/net/rtl8139.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,38 +270,38 @@ impl NetworkDriver for RTL8139Driver {
fn receive_packet(&mut self) -> Option<(RxToken, TxToken)> {
let cmd = unsafe { inb(self.iobase + CR) };

if (cmd & CR_BUFE) != CR_BUFE {
let header = self.rx_peek_u16();
self.advance_rxpos(mem::size_of::<u16>());
if (cmd & CR_BUFE) == CR_BUFE {
return None;
}

if header & ISR_ROK == ISR_ROK {
let length = self.rx_peek_u16() - 4; // copy packet (but not the CRC)
let pos = (self.rxpos + mem::size_of::<u16>()) % RX_BUF_LEN;

// do we reach the end of the receive buffers?
// in this case, we contact the two slices to one vec
let vec_data = if pos + length as usize > RX_BUF_LEN {
let first = &self.rxbuffer[pos..RX_BUF_LEN];
let second = &self.rxbuffer[..length as usize - first.len()];
[first, second].concat()
} else {
(self.rxbuffer[pos..][..length.into()]).to_vec()
};

self.consume_current_buffer();

Some((RxToken::new(vec_data), TxToken::new()))
} else {
warn!(
"RTL8192: invalid header {:#x}, rx_pos {}\n",
header, self.rxpos
);
let header = self.rx_peek_u16();
self.advance_rxpos(mem::size_of::<u16>());

None
}
} else {
None
if header & ISR_ROK != ISR_ROK {
warn!(
"RTL8192: invalid header {:#x}, rx_pos {}\n",
header, self.rxpos
);

return None;
}

let length = self.rx_peek_u16() - 4; // copy packet (but not the CRC)
let pos = (self.rxpos + mem::size_of::<u16>()) % RX_BUF_LEN;

// do we reach the end of the receive buffers?
// in this case, we contact the two slices to one vec
let vec_data = if pos + length as usize > RX_BUF_LEN {
let first = &self.rxbuffer[pos..RX_BUF_LEN];
let second = &self.rxbuffer[..length as usize - first.len()];
[first, second].concat()
} else {
(self.rxbuffer[pos..][..length.into()]).to_vec()
};

self.consume_current_buffer();

Some((RxToken::new(vec_data), TxToken::new()))
}

fn set_polling_mode(&mut self, value: bool) {
Expand Down Expand Up @@ -431,7 +431,7 @@ pub(crate) fn init_device(

for i in 0..MAX_BARS {
if let Some(Bar::Io { port }) = device.get_bar(i.try_into().unwrap()) {
iobase = Some(port)
iobase = Some(port);
}
}

Expand Down
75 changes: 59 additions & 16 deletions src/drivers/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,29 @@ use pci_types::{
use crate::arch::pci::PciConfigRegion;
#[cfg(feature = "fuse")]
use crate::drivers::fs::virtio_fs::VirtioFsDriver;
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
use crate::drivers::net::rtl8139::{self, RTL8139Driver};
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
use crate::drivers::net::virtio::VirtioNetDriver;
#[cfg(any(feature = "tcp", feature = "udp"))]
use crate::drivers::net::NetworkDriver;
#[cfg(any(
all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")),
all(
any(feature = "tcp", feature = "udp"),
not(all(target_arch = "x86_64", feature = "rtl8139"))
),
feature = "fuse",
feature = "vsock"
))]
use crate::drivers::virtio::transport::pci as pci_virtio;
#[cfg(any(
all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")),
all(
any(feature = "tcp", feature = "udp"),
not(all(target_arch = "x86_64", feature = "rtl8139"))
),
feature = "fuse",
feature = "vsock"
))]
Expand Down Expand Up @@ -321,14 +330,24 @@ pub(crate) enum PciDriver {
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
#[cfg(feature = "vsock")]
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
#[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))]
#[cfg(all(
target_arch = "x86_64",
feature = "rtl8139",
any(feature = "tcp", feature = "udp")
))]
RTL8139Net(InterruptTicketMutex<RTL8139Driver>),
}

impl PciDriver {
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<VirtioNetDriver>> {
#[allow(unreachable_patterns)]
match self {
Expand All @@ -337,7 +356,11 @@ impl PciDriver {
}
}

#[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))]
#[cfg(all(
target_arch = "x86_64",
feature = "rtl8139",
any(feature = "tcp", feature = "udp")
))]
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<RTL8139Driver>> {
#[allow(unreachable_patterns)]
match self {
Expand Down Expand Up @@ -379,7 +402,11 @@ impl PciDriver {

(irq_number, vsock_handler)
}
#[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))]
#[cfg(all(
target_arch = "x86_64",
feature = "rtl8139",
any(feature = "tcp", feature = "udp")
))]
Self::RTL8139Net(drv) => {
fn rtl8139_handler() {
if let Some(driver) = get_network_driver() {
Expand All @@ -391,7 +418,10 @@ impl PciDriver {

(irq_number, rtl8139_handler)
}
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
Self::VirtioNet(drv) => {
fn network_handler() {
if let Some(driver) = get_network_driver() {
Expand Down Expand Up @@ -440,15 +470,22 @@ pub(crate) fn get_interrupt_handlers() -> HashMap<InterruptLine, InterruptHandle
handlers
}

#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<VirtioNetDriver>> {
PCI_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_network_driver())
}

#[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))]
#[cfg(all(
target_arch = "x86_64",
feature = "rtl8139",
any(feature = "tcp", feature = "udp")
))]
pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<RTL8139Driver>> {
PCI_DRIVERS
.get()?
Expand Down Expand Up @@ -485,12 +522,18 @@ pub(crate) fn init() {
);

#[cfg(any(
all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")),
all(
any(feature = "tcp", feature = "udp"),
not(all(target_arch = "x86_64", feature = "rtl8139"))
),
feature = "fuse",
feature = "vsock"
))]
match pci_virtio::init_device(adapter) {
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
Ok(VirtioDriver::Network(drv)) => {
register_driver(PciDriver::VirtioNet(InterruptTicketMutex::new(drv)));
}
Expand All @@ -507,7 +550,7 @@ pub(crate) fn init() {
}

// Searching for Realtek RTL8139, which is supported by Qemu
#[cfg(feature = "rtl8139")]
#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))]
for adapter in PCI_DEVICES.finalize().iter().filter(|x| {
let (vendor_id, device_id) = x.id();
vendor_id == 0x10ec && (0x8138..=0x8139).contains(&device_id)
Expand All @@ -518,7 +561,7 @@ pub(crate) fn init() {
);

if let Ok(drv) = rtl8139::init_device(adapter) {
register_driver(PciDriver::RTL8139Net(InterruptTicketMutex::new(drv)))
register_driver(PciDriver::RTL8139Net(InterruptTicketMutex::new(drv)));
}
}
});
Expand Down
12 changes: 9 additions & 3 deletions src/drivers/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ pub mod error {

#[cfg(feature = "fuse")]
pub use crate::drivers::fs::virtio_fs::error::VirtioFsError;
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
pub use crate::drivers::net::virtio::error::VirtioNetError;
#[cfg(feature = "pci")]
use crate::drivers::pci::error::PciError;
Expand All @@ -29,7 +32,10 @@ pub mod error {
#[cfg(feature = "pci")]
NoNotifCfg(u16),
DevNotSupported(u16),
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(
not(all(target_arch = "x86_64", feature = "rtl8139")),
any(feature = "tcp", feature = "udp")
))]
NetDriver(VirtioNetError),
#[cfg(feature = "fuse")]
FsDriver(VirtioFsError),
Expand Down Expand Up @@ -58,7 +64,7 @@ pub mod error {
#[cfg(feature = "pci")]
VirtioError::NoNotifCfg(id) => write!(f, "Virtio driver failed, for device {id:x}, due to a missing or malformed notification config!"),
VirtioError::DevNotSupported(id) => write!(f, "Device with id {id:#x} not supported."),
#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
#[cfg(all(not(all(target_arch = "x86_64", feature = "rtl8139")), any(feature = "tcp", feature = "udp")))]
VirtioError::NetDriver(net_error) => match net_error {
#[cfg(feature = "pci")]
VirtioNetError::NoDevCfg(id) => write!(f, "Virtio network driver failed, for device {id:x}, due to a missing or malformed device config!"),
Expand Down
Loading

0 comments on commit 4b1549a

Please sign in to comment.