Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 1 | // 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 Walbran | 0a8dac7 | 2022-12-21 13:49:06 +0000 | [diff] [blame] | 15 | //! Functions to scan the PCI bus for VirtIO devices. |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame^] | 17 | use super::hal::HalImpl; |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 18 | use crate::{entry::RebootReason, memory::MemoryTracker}; |
| 19 | use fdtpci::{PciError, PciInfo}; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame^] | 20 | use log::{debug, error, info}; |
| 21 | use virtio_drivers::{ |
| 22 | device::blk::VirtIOBlk, |
| 23 | transport::{ |
| 24 | pci::{bus::PciRoot, virtio_device_type, PciTransport}, |
| 25 | DeviceType, Transport, |
| 26 | }, |
| 27 | }; |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 28 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 29 | /// Maps the CAM and BAR range in the page table and MMIO guard. |
| 30 | pub 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 Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 35 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 36 | 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 Walbran | 0d8b54d | 2022-12-08 16:32:33 +0000 | [diff] [blame] | 40 | RebootReason::InternalError |
| 41 | })?; |
| 42 | |
Andrew Walbran | 730375d | 2022-12-21 14:04:34 +0000 | [diff] [blame] | 43 | Ok(()) |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 44 | } |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 45 | |
Andrew Walbran | 0a8dac7 | 2022-12-21 13:49:06 +0000 | [diff] [blame] | 46 | /// Finds VirtIO PCI devices. |
| 47 | pub fn find_virtio_devices(pci_root: &mut PciRoot) -> Result<(), PciError> { |
Andrew Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 48 | 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 Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame^] | 56 | 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 Walbran | d1d0318 | 2022-12-09 18:20:01 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | Ok(()) |
| 73 | } |