Skip to content

Commit a7f06ef

Browse files
committed
nvme: mi: Convert AdminFormatNvmConfiguration to deku bits
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
1 parent 9d75665 commit a7f06ef

4 files changed

Lines changed: 32 additions & 51 deletions

File tree

src/nvme.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -154,50 +154,41 @@ struct ControllerListRequest {
154154
}
155155

156156
// Base v2.1, 5.1.10, Figure 189, SES
157-
#[derive(Debug)]
157+
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
158+
#[deku(
159+
bits = "bits.0",
160+
ctx = "endian: Endian, bits: BitSize",
161+
endian = "endian",
162+
id_type = "u8"
163+
)]
158164
#[repr(u8)]
159165
enum SecureEraseSettings {
160166
NoOperation = 0b000,
161167
UserDataErase = 0b001,
162168
CryptographicErase = 0b010,
163169
}
164-
unsafe impl Discriminant<u8> for SecureEraseSettings {}
165-
166-
impl TryFrom<u32> for SecureEraseSettings {
167-
type Error = ();
168-
169-
fn try_from(value: u32) -> Result<Self, Self::Error> {
170-
match value {
171-
0b000 => Ok(Self::NoOperation),
172-
0b001 => Ok(Self::UserDataErase),
173-
0b010 => Ok(Self::CryptographicErase),
174-
_ => Err(()),
175-
}
176-
}
177-
}
178170

179171
// Base v2.1, 5.1.10, Figure 189
180-
#[derive(Debug)]
181-
#[expect(dead_code)]
172+
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
173+
#[deku(ctx = "endian: Endian", endian = "endian")]
182174
struct AdminFormatNvmConfiguration {
183-
lbafi: u8,
184-
mset: bool,
175+
#[deku(bits = "3")]
185176
pi: u8,
186-
pil: bool,
177+
#[deku(bits = "1")]
178+
mset: bool,
179+
#[deku(bits = "4")]
180+
lbafl: u8,
181+
#[deku(bits = "2", pad_bits_before = "2")]
182+
lbafu: u8,
183+
#[deku(bits = "3")]
187184
ses: SecureEraseSettings,
185+
#[deku(bits = "1", pad_bytes_after = "2")]
186+
pil: bool,
188187
}
189188

190-
impl TryFrom<u32> for AdminFormatNvmConfiguration {
191-
type Error = ();
192-
193-
fn try_from(value: u32) -> Result<Self, Self::Error> {
194-
Ok(Self {
195-
lbafi: ((((value >> 12) & 0x3) << 4) | (value & 0xf)) as u8,
196-
mset: ((value >> 4) & 1) == 1,
197-
pi: ((value >> 5) & 0x3) as u8,
198-
pil: ((value >> 6) & 1) == 1,
199-
ses: TryFrom::try_from((value >> 9) & 0x7)?,
200-
})
189+
impl AdminFormatNvmConfiguration {
190+
fn lbafi(&self) -> u8 {
191+
self.lbafu << 4 | self.lbafl
201192
}
202193
}
203194

src/nvme/mi.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use flagset::{FlagSet, flags};
99
use log::debug;
1010

1111
use crate::nvme::{
12-
AdminNamespaceAttachmentSelect, AdminNamespaceManagementSelect, ControllerListRequest,
12+
AdminFormatNvmConfiguration, AdminNamespaceAttachmentSelect, AdminNamespaceManagementSelect,
13+
ControllerListRequest,
1314
};
1415
use crate::wire::{WireFlagSet, WireVec};
1516
use crate::{CommandEffectError, Discriminant, Encode, MAX_CONTROLLERS};
@@ -901,7 +902,7 @@ struct AdminFormatNvmRequest {
901902
dlen: u32,
902903
#[deku(seek_from_current = "8")]
903904
#[deku(pad_bytes_after = "20")]
904-
config: u32,
905+
config: AdminFormatNvmConfiguration,
905906
}
906907

907908
// MI v2.0, 6, Figure 136

src/nvme/mi/dev.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use crate::{
1717
CommandEffect, CommandEffectError, Controller, ControllerError, ControllerType, Discriminant,
1818
MAX_CONTROLLERS, MAX_NAMESPACES, NamespaceId, NamespaceIdDisposition, SubsystemError,
1919
nvme::{
20-
AdminFormatNvmConfiguration, AdminGetLogPageLidRequestType,
21-
AdminGetLogPageSupportedLogPagesResponse, AdminIdentifyActiveNamespaceIdListResponse,
22-
AdminIdentifyAllocatedNamespaceIdListResponse, AdminIdentifyCnsRequestType,
23-
AdminIdentifyControllerResponse,
20+
AdminGetLogPageLidRequestType, AdminGetLogPageSupportedLogPagesResponse,
21+
AdminIdentifyActiveNamespaceIdListResponse, AdminIdentifyAllocatedNamespaceIdListResponse,
22+
AdminIdentifyCnsRequestType, AdminIdentifyControllerResponse,
2423
AdminIdentifyNamespaceIdentificationDescriptorListResponse,
2524
AdminIdentifyNvmIdentifyNamespaceResponse, AdminIoCqeGenericCommandStatus,
2625
AdminIoCqeStatusType, AdminSanitizeConfiguration, ControllerListResponse,
@@ -2016,19 +2015,8 @@ impl RequestHandler for AdminFormatNvmRequest {
20162015
.await;
20172016
};
20182017

2019-
let Ok(config) = TryInto::<AdminFormatNvmConfiguration>::try_into(self.config) else {
2020-
debug!("Invalid configuration for Admin Format NVM");
2021-
return admin_send_status(
2022-
resp,
2023-
AdminIoCqeStatusType::GenericCommandStatus(
2024-
AdminIoCqeGenericCommandStatus::InvalidFieldInCommand,
2025-
),
2026-
)
2027-
.await;
2028-
};
2029-
2030-
if config.lbafi != 0 {
2031-
debug!("Unsupported LBA format index: {}", config.lbafi);
2018+
if self.config.lbafi() != 0 {
2019+
debug!("Unsupported LBA format index: {}", self.config.lbafi());
20322020
return admin_send_status(
20332021
resp,
20342022
AdminIoCqeStatusType::GenericCommandStatus(

tests/admin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4990,7 +4990,8 @@ mod format_nvm {
49904990
0x7a, 0x05, 0xbd, 0xb8
49914991
];
49924992

4993-
let resp = ExpectedRespChannel::new(&RESP_ADMIN_STATUS_INVALID_FIELD);
4993+
// FIXME: Poor error response code for the problem at hand
4994+
let resp = ExpectedRespChannel::new(&RESP_INVALID_COMMAND_SIZE);
49944995
smol::block_on(async {
49954996
mep.handle_async(&mut subsys, &REQ, MsgIC(true), resp, async |_| Ok(()))
49964997
.await

0 commit comments

Comments
 (0)