libnvme 0.9.0

Safe, idiomatic Rust bindings for the Linux libnvme C library
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Get Features methods.

use libnvme_sys::{
    nvme_get_features_arbitration, nvme_get_features_async_event, nvme_get_features_auto_pst,
    nvme_get_features_endurance_event_cfg, nvme_get_features_err_recovery, nvme_get_features_hctm,
    nvme_get_features_host_behavior, nvme_get_features_host_id, nvme_get_features_host_mem_buf,
    nvme_get_features_iocs_profile, nvme_get_features_irq_coalesce, nvme_get_features_irq_config,
    nvme_get_features_kato, nvme_get_features_lba_range, nvme_get_features_lba_sts_interval,
    nvme_get_features_nopsc, nvme_get_features_num_queues, nvme_get_features_plm_config,
    nvme_get_features_plm_window, nvme_get_features_power_mgmt, nvme_get_features_resv_mask,
    nvme_get_features_resv_persist, nvme_get_features_rrl, nvme_get_features_sanitize,
    nvme_get_features_sw_progress, nvme_get_features_temp_thresh, nvme_get_features_timestamp,
    nvme_get_features_volatile_wc, nvme_get_features_write_atomic, nvme_get_features_write_protect,
};

#[cfg(has_err_recovery2)]
use libnvme_sys::nvme_get_features_err_recovery2;
#[cfg(has_host_mem_buf2)]
use libnvme_sys::nvme_get_features_host_mem_buf2;
#[cfg(has_lba_range2)]
use libnvme_sys::nvme_get_features_lba_range2;
#[cfg(has_resv_mask2)]
use libnvme_sys::nvme_get_features_resv_mask2;
#[cfg(has_resv_persist2)]
use libnvme_sys::nvme_get_features_resv_persist2;
#[cfg(has_temp_thresh2)]
use libnvme_sys::nvme_get_features_temp_thresh2;

use super::types::{AutoPst, HostBehavior, HostMemBufAttrs, LbaRangeType, PlmConfig, Timestamp};
use super::Features;
use crate::admin::FeatureSelect;
use crate::error::check_ret;
use crate::{Error, Result};

impl Features<'_, '_> {
    /// Get Features — Arbitration (FID 01h).
    pub fn get_arbitration(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is a valid file descriptor for this controller; result is
        // a valid &mut u32 alive for the call; sel is a plain integer.
        let ret = unsafe { nvme_get_features_arbitration(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Power Management (FID 02h).
    pub fn get_power_mgmt(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is a valid file descriptor for this controller; result is
        // a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_power_mgmt(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — LBA Range Type (FID 03h).
    pub fn get_lba_range(&self, sel: FeatureSelect, data: &mut LbaRangeType) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; data points to a caller-owned LbaRangeType
        // alive for the call; result is a valid &mut u32.
        let ret =
            unsafe { nvme_get_features_lba_range(fd, sel.as_raw(), data as *mut _, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — LBA Range Type (FID 03h), NSID-scoped extended form.
    #[cfg(has_lba_range2)]
    pub fn get_lba_range2(
        &self,
        sel: FeatureSelect,
        nsid: u32,
        data: &mut LbaRangeType,
    ) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; data points to a caller-owned LbaRangeType
        // alive for the call; result is a valid &mut u32.
        let ret = unsafe {
            nvme_get_features_lba_range2(fd, sel.as_raw(), nsid, data as *mut _, &mut result)
        };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Temperature Threshold (FID 04h).
    pub fn get_temp_thresh(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_temp_thresh(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Temperature Threshold (FID 04h), extended form.
    #[cfg(has_temp_thresh2)]
    pub fn get_temp_thresh2(&self, sel: FeatureSelect, tmpsel: u8, thsel: u32) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret =
            unsafe { nvme_get_features_temp_thresh2(fd, sel.as_raw(), tmpsel, thsel, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Error Recovery (FID 05h).
    pub fn get_err_recovery(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_err_recovery(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Error Recovery (FID 05h), NSID-scoped extended form.
    #[cfg(has_err_recovery2)]
    pub fn get_err_recovery2(&self, sel: FeatureSelect, nsid: u32) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_err_recovery2(fd, sel.as_raw(), nsid, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Volatile Write Cache (FID 06h).
    pub fn get_volatile_wc(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_volatile_wc(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Number of Queues (FID 07h).
    pub fn get_num_queues(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_num_queues(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Interrupt Coalescing (FID 08h).
    pub fn get_irq_coalesce(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_irq_coalesce(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Interrupt Vector Configuration (FID 09h).
    pub fn get_irq_config(&self, sel: FeatureSelect, iv: u16) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_irq_config(fd, sel.as_raw(), iv, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Write Atomicity Normal (FID 0Ah).
    pub fn get_write_atomic(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_write_atomic(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Async Event Configuration (FID 0Bh).
    pub fn get_async_event(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_async_event(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Auto Power State Transition (FID 0Ch).
    pub fn get_auto_pst(&self, sel: FeatureSelect, apst: &mut AutoPst) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; apst points to a caller-owned AutoPst alive
        // for the call; result is a valid &mut u32.
        let ret =
            unsafe { nvme_get_features_auto_pst(fd, sel.as_raw(), apst as *mut _, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Host Memory Buffer (FID 0Dh).
    pub fn get_host_mem_buf(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_host_mem_buf(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Host Memory Buffer (FID 0Dh), extended form returning
    /// the full attributes struct.
    #[cfg(has_host_mem_buf2)]
    pub fn get_host_mem_buf2(
        &self,
        sel: FeatureSelect,
        attrs: &mut HostMemBufAttrs,
    ) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; attrs points to a caller-owned HostMemBufAttrs
        // alive for the call; result is a valid &mut u32.
        let ret = unsafe {
            nvme_get_features_host_mem_buf2(fd, sel.as_raw(), attrs as *mut _, &mut result)
        };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Timestamp (FID 0Eh).
    pub fn get_timestamp(&self, sel: FeatureSelect, ts: &mut Timestamp) -> Result<()> {
        let fd = self.fd()?;
        // SAFETY: fd is valid; ts points to a caller-owned Timestamp alive
        // for the call.
        let ret = unsafe { nvme_get_features_timestamp(fd, sel.as_raw(), ts as *mut _) };
        check_ret(ret)
    }

    /// Get Features — Keep Alive Timer (FID 0Fh).
    pub fn get_kato(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_kato(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Host Controlled Thermal Management (FID 10h).
    pub fn get_hctm(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_hctm(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Non-Operational Power State Config (FID 11h).
    pub fn get_nopsc(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_nopsc(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Read Recovery Level (FID 12h).
    pub fn get_rrl(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_rrl(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Predictable Latency Mode Config (FID 13h).
    pub fn get_plm_config(
        &self,
        sel: FeatureSelect,
        nvmsetid: u16,
        data: &mut PlmConfig,
    ) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; data points to a caller-owned PlmConfig alive
        // for the call; result is a valid &mut u32.
        let ret = unsafe {
            nvme_get_features_plm_config(fd, sel.as_raw(), nvmsetid, data as *mut _, &mut result)
        };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Predictable Latency Mode Window (FID 14h).
    pub fn get_plm_window(&self, sel: FeatureSelect, nvmsetid: u16) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_plm_window(fd, sel.as_raw(), nvmsetid, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — LBA Status Information Report Interval (FID 15h).
    pub fn get_lba_sts_interval(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_lba_sts_interval(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Host Behavior Support (FID 16h).
    pub fn get_host_behavior(&self, sel: FeatureSelect, data: &mut HostBehavior) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; data points to a caller-owned HostBehavior alive
        // for the call; result is a valid &mut u32.
        let ret = unsafe {
            nvme_get_features_host_behavior(fd, sel.as_raw(), data as *mut _, &mut result)
        };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Sanitize Config (FID 17h).
    pub fn get_sanitize(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_sanitize(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Endurance Group Event Configuration (FID 18h).
    pub fn get_endurance_event_cfg(&self, sel: FeatureSelect, endgid: u16) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret =
            unsafe { nvme_get_features_endurance_event_cfg(fd, sel.as_raw(), endgid, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Software Progress Marker (FID 80h).
    pub fn get_sw_progress(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_sw_progress(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Host Identifier (FID 81h).
    ///
    /// `hostid` must be exactly 8 bytes when `exhid` is `false`, or 16 bytes
    /// when `exhid` is `true`.
    pub fn get_host_id(&self, sel: FeatureSelect, exhid: bool, hostid: &mut [u8]) -> Result<()> {
        let expected = if exhid { 16 } else { 8 };
        if hostid.len() != expected {
            return Err(Error::Os(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "hostid buffer must be 8 bytes (exhid=false) or 16 bytes (exhid=true)",
            )));
        }
        let fd = self.fd()?;
        // SAFETY: fd is valid; hostid is a caller-owned mutable slice of the
        // exact required length (checked above), alive for the call.
        let ret = unsafe {
            nvme_get_features_host_id(
                fd,
                sel.as_raw(),
                exhid,
                hostid.len() as u32,
                hostid.as_mut_ptr(),
            )
        };
        check_ret(ret)
    }

    /// Get Features — Reservation Notification Mask (FID 82h).
    pub fn get_resv_mask(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_resv_mask(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Reservation Notification Mask (FID 82h), NSID-scoped
    /// extended form.
    #[cfg(has_resv_mask2)]
    pub fn get_resv_mask2(&self, sel: FeatureSelect, nsid: u32) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_resv_mask2(fd, sel.as_raw(), nsid, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Reservation Persistence (FID 83h).
    pub fn get_resv_persist(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_resv_persist(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Reservation Persistence (FID 83h), NSID-scoped extended
    /// form.
    #[cfg(has_resv_persist2)]
    pub fn get_resv_persist2(&self, sel: FeatureSelect, nsid: u32) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_resv_persist2(fd, sel.as_raw(), nsid, &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — Namespace Write Protection (FID 84h).
    pub fn get_write_protect(&self, sel: FeatureSelect, nsid: u32) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_write_protect(fd, nsid, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Get Features — IO Command Set Profile (FID 19h, NVMe 2.0).
    pub fn get_iocs_profile(&self, sel: FeatureSelect) -> Result<u32> {
        let fd = self.fd()?;
        let mut result = 0u32;
        // SAFETY: fd is valid; result is a valid &mut u32 alive for the call.
        let ret = unsafe { nvme_get_features_iocs_profile(fd, sel.as_raw(), &mut result) };
        check_ret(ret)?;
        Ok(result)
    }
}