Skip to content

Commit

Permalink
add nvm format and completely clearing namespace, use reference inste…
Browse files Browse the repository at this point in the history
…ad of copy in completion polling
  • Loading branch information
bootreer committed Mar 25, 2024
1 parent 5a6127c commit 00e5ad4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,46 @@ impl NvmeCommand {
cdw15: 0,
}
}

pub(crate) fn format_nvm(c_id: u16, ns_id: u32) -> Self {
Self {
opcode: 0x80,
flags: 0,
c_id,
ns_id,
_rsvd: 0,
md_ptr: 0,
d_ptr: [0, 0],
cdw10: 1 << 9,
// TODO: dealloc and prinfo bits
cdw11: 0,
cdw12: 0,
cdw13: 0,
cdw14: 0,
cdw15: 0,

}

}

// not supported by samsung
pub fn write_zeroes(c_id: u16, ns_id: u32, slba: u64, nlb: u16, deac: bool) -> Self {
Self {
opcode: 8,
flags: 0,
c_id,
ns_id,
_rsvd: 0,
md_ptr: 0,
d_ptr: [0, 0],
cdw10: slba as u32,
// TODO: dealloc and prinfo bits
cdw11: (slba >> 32) as u32,
cdw12: ((deac as u32) << 25) | nlb as u32,
cdw13: 0,
cdw14: 0,
cdw15: 0,
}

}
}
18 changes: 15 additions & 3 deletions src/nvme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ impl NvmeQueuePair {
unsafe {
std::ptr::write_volatile(self.comp_queue.doorbell as *mut u32, tail as u32);
}

let status = c_entry.status >> 1;
self.sub_queue.head = c_entry.sq_head as usize;
let status = c_entry.status >> 1;
if status != 0 {
eprintln!(
"Status: 0x{:x}, Status Code 0x{:x}, Status Code Type: 0x{:x}",
Expand All @@ -177,6 +176,7 @@ impl NvmeQueuePair {
unsafe {
std::ptr::write_volatile(self.comp_queue.doorbell as *mut u32, tail as u32);
}
self.sub_queue.head = c_entry.sq_head as usize;
let status = c_entry.status >> 1;
if status != 0 {
eprintln!(
Expand All @@ -187,7 +187,6 @@ impl NvmeQueuePair {
);
eprintln!("{:?}", c_entry);
}
self.sub_queue.head = c_entry.sq_head as usize;
return Some(());
}
None
Expand Down Expand Up @@ -731,6 +730,19 @@ impl NvmeDevice {
Ok(entry)
}

pub fn clear_namespace(
&mut self,
ns_id: Option<u32>
) {
let ns_id = if let Some(ns_id) = ns_id {
assert!(self.namespaces.contains_key(&ns_id));
ns_id
} else {
0xFFFF_FFFF
};
self.submit_and_complete_admin(| c_id, _ | { NvmeCommand::format_nvm(c_id, ns_id) } );
}

/// Sets Queue `qid` Tail Doorbell to `val`
fn write_reg_idx(&self, reg: NvmeArrayRegs, qid: u16, val: u32) {
match reg {
Expand Down
5 changes: 3 additions & 2 deletions src/queues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ impl NvmeCompQueue {
})
}

#[inline(always)]
pub fn complete(&mut self) -> Option<(usize, NvmeCompletion, usize)> {
let entry: NvmeCompletion = self.commands[self.head];
let entry = &self.commands[self.head];

if ((entry.status & 1) == 1) == self.phase {
let prev = self.head;
self.head = (self.head + 1) % self.len;
if self.head == 0 {
self.phase = !self.phase;
}
Some((self.head, entry, prev))
Some((self.head, entry.clone(), prev))
} else {
None
}
Expand Down

0 comments on commit 00e5ad4

Please sign in to comment.