| 18 | } |
| 19 | |
| 20 | auto VirtioDriver::Probe(DeviceNode& node) -> Expected<void> { |
| 21 | if (node.mmio_size == 0) { |
| 22 | klog::Err("VirtioDriver: FDT reg property missing size for node '{}'", |
| 23 | node.name); |
| 24 | return std::unexpected(Error(ErrorCode::kInvalidArgument)); |
| 25 | } |
| 26 | auto ctx = mmio_helper::Prepare(node, node.mmio_size); |
| 27 | if (!ctx) { |
| 28 | return std::unexpected(ctx.error()); |
| 29 | } |
| 30 | |
| 31 | etl::io_port_ro<uint32_t> magic_reg{reinterpret_cast<void*>(ctx->base)}; |
| 32 | if (magic_reg.read() != virtio::kMmioMagicValue) { |
| 33 | return std::unexpected(Error(ErrorCode::kNotSupported)); |
| 34 | } |
| 35 | |
| 36 | // 读取 device_id |
| 37 | etl::io_port_ro<uint32_t> device_id_reg{reinterpret_cast<void*>( |
| 38 | ctx->base + |
| 39 | std::to_underlying(virtio::MmioTransport::MmioReg::kDeviceId))}; |
| 40 | const auto device_id = static_cast<DeviceId>(device_id_reg.read()); |
| 41 | |
| 42 | switch (device_id) { |
| 43 | case DeviceId::kBlock: { |
| 44 | if (blk_device_count_ >= kMaxBlkDevices) { |
| 45 | klog::Warn( |
| 46 | "VirtioDriver: blk device pool full, device at {:#x} skipped", |
| 47 | ctx->base); |
| 48 | return std::unexpected(Error(ErrorCode::kOutOfMemory)); |
| 49 | } |
| 50 | const size_t idx = blk_device_count_; |
| 51 | |
| 52 | // 分配 DMA buffer |
| 53 | dma_buffers_[idx] = kstd::make_unique<IoBuffer>(kMinDmaBufferSize); |
| 54 | if (!dma_buffers_[idx] || !dma_buffers_[idx]->IsValid() || |
| 55 | dma_buffers_[idx]->GetBuffer().size() < kMinDmaBufferSize) { |
| 56 | klog::Err("VirtioDriver: failed to allocate DMA buffer at {:#x}", |
| 57 | ctx->base); |
| 58 | return std::unexpected(Error(ErrorCode::kOutOfMemory)); |
| 59 | } |
| 60 | |
| 61 | // Allocate slot DMA buffer |
| 62 | auto [slot_size, slot_align] = |
| 63 | virtio::blk::VirtioBlk<>::GetRequiredSlotMemSize(); |
| 64 | slot_buffers_[idx] = kstd::make_unique<IoBuffer>(slot_size, slot_align); |
| 65 | if (!slot_buffers_[idx] || !slot_buffers_[idx]->IsValid()) { |
| 66 | klog::Err("VirtioDriver: failed to allocate slot DMA buffer at {:#x}", |
| 67 | ctx->base); |
| 68 | dma_buffers_[idx].reset(); |
| 69 | return std::unexpected(Error(ErrorCode::kOutOfMemory)); |
| 70 | } |
| 71 | |
| 72 | uint64_t extra_features = |
| 73 | static_cast<uint64_t>(virtio::blk::BlkFeatureBit::kSegMax) | |
| 74 | static_cast<uint64_t>(virtio::blk::BlkFeatureBit::kSizeMax) | |
| 75 | static_cast<uint64_t>(virtio::blk::BlkFeatureBit::kBlkSize) | |
| 76 | static_cast<uint64_t>(virtio::blk::BlkFeatureBit::kFlush) | |
| 77 | static_cast<uint64_t>(virtio::blk::BlkFeatureBit::kGeometry); |
nothing calls this directly
no test coverage detected