-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnvme_passthru.rs
357 lines (323 loc) · 9.26 KB
/
nvme_passthru.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use alloc::vec::Vec;
use core::mem::MaybeUninit;
use bitflags::bitflags;
use core::{
fmt::{Debug, Display, Formatter},
ptr::null_mut,
slice,
};
use uefi::{
data_types::unsafe_guid,
newtype_enum,
proto::{device_path::DevicePath, Protocol},
Event, Status,
};
#[unsafe_guid("52c78312-8edc-4233-98f2-1a1aa5e388a5")]
#[derive(Protocol)]
#[repr(C)]
pub struct NvmExpressPassthru {
mode: *const Mode,
pass_thru: unsafe extern "efiapi" fn(
this: &NvmExpressPassthru,
namespace_id: u32,
packet: &mut CommandPacket,
event: Event,
) -> Status,
get_next_namespace:
unsafe extern "efiapi" fn(this: &NvmExpressPassthru, namespace_id: &mut u32) -> Status,
build_device_path: unsafe extern "efiapi" fn(
this: &NvmExpressPassthru,
namespace_id: u32,
device_path: &mut *mut DevicePath,
) -> Status,
get_namespace: unsafe extern "efiapi" fn(
this: &NvmExpressPassthru,
device_path: &DevicePath,
namespace_id: &mut u32,
) -> Status,
}
impl NvmExpressPassthru {
pub fn mode(&self) -> &Mode {
unsafe { &*self.mode }
}
pub unsafe fn send<'a, 'b: 'a>(
&'a mut self,
target: SendTarget,
packet: &mut CommandPacket<'b>,
) -> uefi::Result<NvmeCompletion> {
self.send_async(target, packet, core::mem::zeroed())
}
pub unsafe fn send_async<'a, 'b: 'a>(
&'a mut self,
target: SendTarget,
mut packet: &mut CommandPacket<'b>,
event: Event,
) -> uefi::Result<NvmeCompletion> {
let id = match target {
SendTarget::Controller => 0,
SendTarget::Namespace(id) => id.to_u32(),
SendTarget::AllNamespaces => 0xFFFFFFFF,
};
let mut completion = Default::default();
packet.completion = Some(&mut completion);
(self.pass_thru)(self, id, packet, event).into_with_val(|| completion)
}
// shorthand assuming there is always at least one namespace on the NVMe controller
pub fn first_namespace(&self) -> uefi::Result<NamespaceId> {
let mut namespace_id = 0xFFFFFFFF;
unsafe { (self.get_next_namespace)(self, &mut namespace_id) }
.into_with_val(|| NamespaceId(namespace_id))
}
pub fn list_namespaces(&self) -> uefi::Result<Vec<NamespaceId>> {
let mut namespace_id = 0xFFFFFFFF;
let mut result = Vec::new();
loop {
let status = unsafe { (self.get_next_namespace)(self, &mut namespace_id) };
if status.is_success() {
result.push(NamespaceId(namespace_id));
} else if status == Status::NOT_FOUND {
break Status::SUCCESS.into_with_val(|| result);
} else {
break Err(status.into());
}
}
}
pub fn build_device_path(&self, namespace_id: NamespaceId) -> uefi::Result<&mut DevicePath> {
let mut device_path = null_mut();
unsafe { (self.build_device_path)(self, namespace_id.to_u32(), &mut device_path) }
.into_with_val(|| unsafe { device_path.as_mut() }.unwrap())
}
pub fn get_namespace(&self, device_path: &DevicePath) -> uefi::Result<NamespaceId> {
let mut namespace_id = 0;
unsafe { (self.get_namespace)(self, device_path, &mut namespace_id) }
.into_with_val(|| unsafe { NamespaceId::new(namespace_id) })
}
}
#[derive(Debug)]
pub enum SendTarget {
Controller,
Namespace(NamespaceId),
AllNamespaces,
}
newtype_enum! {
#[must_use]
pub enum QueueType: u8 => {
ADMIN = 0,
IO = 1,
}
}
bitflags! {
#[repr(transparent)]
pub struct Attributes: u32 {
const PHYSICAL = 0x01;
const LOGICAL = 0x02;
const NONBLOCKIO = 0x04;
const CMD_SET_NVM = 0x08;
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Version {
data: [u8; 4],
}
impl Version {
pub fn major(&self) -> u16 {
(self.data[0] as u16) << 8 | self.data[1] as u16
}
pub fn minor(&self) -> u8 {
self.data[2]
}
pub fn tertiary(&self) -> u8 {
self.data[3]
}
}
impl Debug for Version {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
f.debug_struct("Version")
.field("major", &self.major())
.field("minor", &self.major())
.field("tertiary", &self.tertiary())
.finish()
}
}
impl Display for Version {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
write!(f, "{}.{}.{}", self.major(), self.minor(), self.tertiary())
}
}
#[derive(Debug)]
#[repr(C)]
pub struct Mode {
pub attributes: Attributes,
pub io_align: u32,
pub version: Version,
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone)]
#[repr(transparent)]
pub struct NamespaceId(u32);
impl NamespaceId {
/// # Safety
/// This contructor allows to construct arbitrary unchecked NamespaceIds.
///
/// This is to ensure that only safe way to get them is to actually query the device,
/// as well as because all-zero and all-one bit patterns are not allowed to be NamespaceIds.
pub const unsafe fn new(id: u32) -> NamespaceId {
NamespaceId(id)
}
pub const fn to_u32(self) -> u32 {
self.0
}
}
pub const NVME_GENERIC_TIMEOUT: u64 = 5_000_000;
bitflags! {
#[repr(transparent)]
struct CdwValidityFlags: u8 {
const CDW_2 = 0x01;
const CDW_3 = 0x02;
const CDW_10 = 0x04;
const CDW_11 = 0x08;
const CDW_12 = 0x10;
const CDW_13 = 0x20;
const CDW_14 = 0x40;
const CDW_15 = 0x80;
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
struct Cdw0 {
opcode: u8, // UINT32 Opcode:8;
fused: u8, // UINT32 FusedOperation:2;
_res1: u8, //
_res2: u8, // UINT32 Reserved:22;
}
#[repr(C)]
#[derive(Debug)]
pub struct Command {
cdw_0: Cdw0,
flags: CdwValidityFlags,
pub nsid: u32,
pub cdw_2: u32,
pub cdw_3: u32,
pub cdw_10: u32,
pub cdw_11: u32,
pub cdw_12: u32,
pub cdw_13: u32,
pub cdw_14: u32,
pub cdw_15: u32,
}
macro_rules! cdws {
($($name:ident: $flag:ident;)*) => {
$(
pub const fn $name(mut self, $name: u32) -> Self {
self.$name = $name;
self.flags.bits |= CdwValidityFlags::$flag.bits;
self
}
)*
};
}
impl Command {
pub const fn new(opcode: u8) -> Command {
Command {
cdw_0: Cdw0 {
opcode,
fused: 0,
_res1: 0,
_res2: 0,
},
flags: CdwValidityFlags::empty(),
nsid: 0,
cdw_2: 0,
cdw_3: 0,
cdw_10: 0,
cdw_11: 0,
cdw_12: 0,
cdw_13: 0,
cdw_14: 0,
cdw_15: 0,
}
}
pub const fn fused_first(mut self) -> Self {
self.cdw_0.fused |= 0b10000000;
self
}
pub const fn fused_second(mut self) -> Self {
self.cdw_0.fused |= 0b01000000;
self
}
pub const fn ns(mut self, namespace: NamespaceId) -> Self {
self.nsid = namespace.to_u32();
self
}
cdws! {
cdw_2: CDW_2;
cdw_3: CDW_3;
cdw_10: CDW_10;
cdw_11: CDW_11;
cdw_12: CDW_12;
cdw_13: CDW_13;
cdw_14: CDW_14;
cdw_15: CDW_15;
}
}
#[repr(C)]
#[derive(Debug, Default, Clone)]
pub struct NvmeCompletion {
pub dw_0: u32,
pub dw_1: u32,
pub dw_2: u32,
pub dw_3: u32,
}
#[repr(C)]
pub struct CommandPacket<'a> {
pub timeout: u64,
transfer_buffer: *mut MaybeUninit<u8>,
transfer_length: u32,
metadata_buffer: *mut MaybeUninit<u8>,
metadata_length: u32,
pub queue_type: QueueType,
pub cmd: &'a Command,
pub completion: Option<*mut NvmeCompletion>,
}
impl Debug for CommandPacket<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("CommandPacket")
.field("timeout", &self.timeout)
.field("transfer_buffer", unsafe {
&slice::from_raw_parts_mut(self.transfer_buffer, self.transfer_length as usize)
})
.field("metadata_buffer", unsafe {
&slice::from_raw_parts_mut(self.metadata_buffer, self.metadata_length as usize)
})
.field("queue_type", &self.queue_type)
.field("cmd", &self.cmd)
// .field("completion", &self.completion)
.finish()
}
}
impl<'a> CommandPacket<'a> {
pub fn new(
timeout: u64,
transfer_buffer: Option<&mut [MaybeUninit<u8>]>,
metadata_buffer: Option<&mut [MaybeUninit<u8>]>,
queue_type: QueueType,
cmd: &'a Command,
) -> Self {
let (transfer_buffer, transfer_length) = transfer_buffer
.map(|it| (it.as_mut_ptr(), it.len() as u32))
.unwrap_or((null_mut(), 0));
let (metadata_buffer, metadata_length) = metadata_buffer
.map(|it| (it.as_mut_ptr(), it.len() as u32))
.unwrap_or((null_mut(), 0));
Self {
timeout,
transfer_buffer,
transfer_length,
metadata_buffer,
metadata_length,
queue_type,
cmd,
completion: None,
}
}
}