use super::nvme_log::{HealthLog, IdentifyController};
use super::traits::Error;
use super::windows_device::Device;
use windows::Win32::Foundation::ERROR_INVALID_PARAMETER;
use windows::Win32::System::Ioctl::{
NVMeDataTypeFeature, NVMeDataTypeIdentify, NVMeDataTypeLogPage, PropertyStandardQuery,
ProtocolTypeNvme, StorageDeviceProtocolSpecificProperty, IOCTL_STORAGE_QUERY_PROPERTY,
STORAGE_PROPERTY_QUERY, STORAGE_PROTOCOL_DATA_DESCRIPTOR, STORAGE_PROTOCOL_SPECIFIC_DATA,
};
use windows::Win32::System::IO::DeviceIoControl;
const NVME_LOG_PAGE_HEALTH: u32 = 0x02;
const NVME_IDENTIFY_CNS_CONTROLLER: u32 = 0x01;
const NVME_FEATURE_POWER_MANAGEMENT: u32 = 0x02;
const HEALTH_LOG_LEN: usize = 512;
const IDENTIFY_LEN: usize = 4096;
pub(crate) struct NvmeData {
pub identify: Option<IdentifyController>,
pub health: Option<HealthLog>,
pub power_state: Option<u8>,
}
impl Device {
fn query(
&self,
data_type: i32,
request_value: u32,
data_len: usize,
) -> Result<ProtocolReply, Error> {
let header = std::mem::size_of::<STORAGE_PROPERTY_QUERY>()
+ std::mem::size_of::<STORAGE_PROTOCOL_SPECIFIC_DATA>();
let mut buf = vec![0u8; header + data_len];
unsafe {
let query = buf.as_mut_ptr() as *mut STORAGE_PROPERTY_QUERY;
(*query).PropertyId = StorageDeviceProtocolSpecificProperty;
(*query).QueryType = PropertyStandardQuery;
let protocol = std::ptr::addr_of_mut!((*query).AdditionalParameters)
as *mut STORAGE_PROTOCOL_SPECIFIC_DATA;
(*protocol).ProtocolType = ProtocolTypeNvme;
(*protocol).DataType = data_type as u32;
(*protocol).ProtocolDataRequestValue = request_value;
(*protocol).ProtocolDataRequestSubValue = 0;
(*protocol).ProtocolDataOffset =
std::mem::size_of::<STORAGE_PROTOCOL_SPECIFIC_DATA>() as u32;
(*protocol).ProtocolDataLength = data_len as u32;
let mut returned = 0u32;
DeviceIoControl(
self.0,
IOCTL_STORAGE_QUERY_PROPERTY,
Some(buf.as_ptr() as *const _),
buf.len() as u32,
Some(buf.as_mut_ptr() as *mut _),
buf.len() as u32,
Some(&mut returned),
None,
)
.map_err(|e| {
if e.code() == ERROR_INVALID_PARAMETER.to_hresult() {
Error::NotSupported
} else {
Error::QueryFailed(format!("NVMe query: {e}"))
}
})?;
let descriptor = buf.as_ptr() as *const STORAGE_PROTOCOL_DATA_DESCRIPTOR;
let embedded = std::ptr::addr_of!((*descriptor).ProtocolSpecificData) as usize
- buf.as_ptr() as usize;
let offset = (*descriptor).ProtocolSpecificData.ProtocolDataOffset as usize;
let length = (*descriptor).ProtocolSpecificData.ProtocolDataLength as usize;
let fixed = (*descriptor).ProtocolSpecificData.FixedProtocolReturnData;
let start = embedded.saturating_add(offset);
let end = start.saturating_add(length);
let data = if length == 0 || end > buf.len() {
Vec::new()
} else {
buf[start..end].to_vec()
};
Ok(ProtocolReply { data, fixed })
}
}
fn feature(&self, feature_id: u32) -> Result<u32, Error> {
self.query(NVMeDataTypeFeature.0, feature_id, IDENTIFY_LEN)
.map(|reply| reply.fixed)
}
}
struct ProtocolReply {
data: Vec<u8>,
fixed: u32,
}
pub(crate) fn query(index: u32) -> Result<NvmeData, Error> {
let device = Device::open(index)?;
let identify = device.query(
NVMeDataTypeIdentify.0,
NVME_IDENTIFY_CNS_CONTROLLER,
IDENTIFY_LEN,
);
if matches!(identify, Err(Error::NotSupported)) {
return Err(Error::NotSupported);
}
let health = device.query(NVMeDataTypeLogPage.0, NVME_LOG_PAGE_HEALTH, HEALTH_LOG_LEN);
let power_state = device
.feature(NVME_FEATURE_POWER_MANAGEMENT)
.ok()
.map(|cdw0| (cdw0 & 0x1F) as u8);
Ok(NvmeData {
identify: identify
.ok()
.and_then(|r| IdentifyController::parse(&r.data)),
health: health.ok().and_then(|r| HealthLog::parse(&r.data)),
power_state,
})
}