blob: f9d36c6bf2927c5b899979bfc56d7a78114540f3 [file] [log] [blame]
Andrew Walbran19690632022-12-07 16:41:30 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Andrew Walbran0a8dac72022-12-21 13:49:06 +000015//! Functions to scan the PCI bus for VirtIO devices.
Andrew Walbran19690632022-12-07 16:41:30 +000016
Andrew Walbran848decf2022-12-15 14:39:38 +000017use super::hal::HalImpl;
Andrew Walbran730375d2022-12-21 14:04:34 +000018use crate::{entry::RebootReason, memory::MemoryTracker};
19use fdtpci::{PciError, PciInfo};
Andrew Walbran848decf2022-12-15 14:39:38 +000020use log::{debug, error, info};
21use virtio_drivers::{
22 device::blk::VirtIOBlk,
23 transport::{
24 pci::{bus::PciRoot, virtio_device_type, PciTransport},
25 DeviceType, Transport,
26 },
27};
Andrew Walbran19690632022-12-07 16:41:30 +000028
Andrew Walbran730375d2022-12-21 14:04:34 +000029/// Maps the CAM and BAR range in the page table and MMIO guard.
30pub fn map_mmio(pci_info: &PciInfo, memory: &mut MemoryTracker) -> Result<(), RebootReason> {
31 memory.map_mmio_range(pci_info.cam_range.clone()).map_err(|e| {
32 error!("Failed to map PCI CAM: {}", e);
33 RebootReason::InternalError
34 })?;
Andrew Walbran19690632022-12-07 16:41:30 +000035
Andrew Walbran730375d2022-12-21 14:04:34 +000036 memory
37 .map_mmio_range(pci_info.bar_range.start as usize..pci_info.bar_range.end as usize)
38 .map_err(|e| {
39 error!("Failed to map PCI MMIO range: {}", e);
Andrew Walbran0d8b54d2022-12-08 16:32:33 +000040 RebootReason::InternalError
41 })?;
42
Andrew Walbran730375d2022-12-21 14:04:34 +000043 Ok(())
Andrew Walbran19690632022-12-07 16:41:30 +000044}
Andrew Walbrand1d03182022-12-09 18:20:01 +000045
Andrew Walbran0a8dac72022-12-21 13:49:06 +000046/// Finds VirtIO PCI devices.
47pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> {
Andrew Walbrand1d03182022-12-09 18:20:01 +000048 for (device_function, info) in pci_root.enumerate_bus(0) {
49 let (status, command) = pci_root.get_status_command(device_function);
50 debug!(
51 "Found PCI device {} at {}, status {:?} command {:?}",
52 info, device_function, status, command
53 );
54 if let Some(virtio_type) = virtio_device_type(&info) {
55 debug!(" VirtIO {:?}", virtio_type);
Andrew Walbran848decf2022-12-15 14:39:38 +000056 let mut transport = PciTransport::new::<HalImpl>(pci_root, device_function).unwrap();
57 info!(
58 "Detected virtio PCI device with device type {:?}, features {:#018x}",
59 transport.device_type(),
60 transport.read_device_features(),
61 );
62 if virtio_type == DeviceType::Block {
63 let mut blk =
64 VirtIOBlk::<HalImpl, _>::new(transport).expect("failed to create blk driver");
65 info!("Found {} KiB block device.", blk.capacity() * 512 / 1024);
66 let mut data = [0; 512];
67 blk.read_block(0, &mut data).expect("Failed to read block device");
68 }
Andrew Walbrand1d03182022-12-09 18:20:01 +000069 }
70 }
71
72 Ok(())
73}