Debian Codegraph @

src/plugins/nvme/nvme-info.c

raw
1 /*
2 * Copyright (C) 2014-2021 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Tomas Bzatek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <malloc.h>
29
30 #include <libnvme.h>
31
32 #include <blockdev/utils.h>
33 #include <check_deps.h>
34 #include "nvme.h"
35 #include "nvme-private.h"
36
37
38 /**
39 * bd_nvme_controller_info_free: (skip)
40 * @info: (nullable): %BDNVMEControllerInfo to free
41 *
42 * Frees @info.
43 */
44 void bd_nvme_controller_info_free (BDNVMEControllerInfo *info) {
45 if (info == NULL)
46 return;
47
48 g_free (info->fguid);
49 g_free (info->subsysnqn);
50 g_free (info->model_number);
51 g_free (info->serial_number);
52 g_free (info->firmware_ver);
53 g_free (info->nvme_ver);
54 g_free (info);
55 }
56
57 /**
58 * bd_nvme_controller_info_copy: (skip)
59 * @info: (nullable): %BDNVMEControllerInfo to copy
60 *
61 * Creates a new copy of @info.
62 */
63 BDNVMEControllerInfo * bd_nvme_controller_info_copy (BDNVMEControllerInfo *info) {
64 BDNVMEControllerInfo *new_info;
65
66 if (info == NULL)
67 return NULL;
68
69 new_info = g_new0 (BDNVMEControllerInfo, 1);
70 memcpy (new_info, info, sizeof (BDNVMEControllerInfo));
71 new_info->fguid = g_strdup (info->fguid);
72 new_info->subsysnqn = g_strdup (info->subsysnqn);
73 new_info->model_number = g_strdup (info->model_number);
74 new_info->serial_number = g_strdup (info->serial_number);
75 new_info->firmware_ver = g_strdup (info->firmware_ver);
76 new_info->nvme_ver = g_strdup (info->nvme_ver);
77
78 return new_info;
79 }
80
81 /**
82 * bd_nvme_lba_format_free: (skip)
83 * @fmt: (nullable): %BDNVMELBAFormat to free
84 *
85 * Frees @fmt.
86 */
87 void bd_nvme_lba_format_free (BDNVMELBAFormat *fmt) {
88 g_free (fmt);
89 }
90
91 /**
92 * bd_nvme_lba_format_copy: (skip)
93 * @fmt: (nullable): %BDNVMELBAFormat to copy
94 *
95 * Creates a new copy of @fmt.
96 */
97 BDNVMELBAFormat * bd_nvme_lba_format_copy (BDNVMELBAFormat *fmt) {
98 BDNVMELBAFormat *new_fmt;
99
100 if (fmt == NULL)
101 return NULL;
102
103 new_fmt = g_new0 (BDNVMELBAFormat, 1);
104 new_fmt->data_size = fmt->data_size;
105 new_fmt->metadata_size = fmt->metadata_size;
106 new_fmt->relative_performance = fmt->relative_performance;
107
108 return new_fmt;
109 }
110
111 /**
112 * bd_nvme_namespace_info_free: (skip)
113 * @info: (nullable): %BDNVMENamespaceInfo to free
114 *
115 * Frees @info.
116 */
117 void bd_nvme_namespace_info_free (BDNVMENamespaceInfo *info) {
118 BDNVMELBAFormat **lba_formats;
119
120 if (info == NULL)
121 return;
122
123 g_free (info->eui64);
124 g_free (info->uuid);
125 g_free (info->nguid);
126
127 for (lba_formats = info->lba_formats; lba_formats && *lba_formats; lba_formats++)
128 bd_nvme_lba_format_free (*lba_formats);
129 g_free (info->lba_formats);
130 g_free (info);
131 }
132
133 /**
134 * bd_nvme_namespace_info_copy: (skip)
135 * @info: (nullable): %BDNVMENamespaceInfo to copy
136 *
137 * Creates a new copy of @info.
138 */
139 BDNVMENamespaceInfo * bd_nvme_namespace_info_copy (BDNVMENamespaceInfo *info) {
140 BDNVMENamespaceInfo *new_info;
141 BDNVMELBAFormat **lba_formats;
142 GPtrArray *ptr_array;
143
144 if (info == NULL)
145 return NULL;
146
147 new_info = g_new0 (BDNVMENamespaceInfo, 1);
148 memcpy (new_info, info, sizeof (BDNVMENamespaceInfo));
149 new_info->eui64 = g_strdup (info->eui64);
150 new_info->uuid = g_strdup (info->uuid);
151 new_info->nguid = g_strdup (info->nguid);
152
153 ptr_array = g_ptr_array_new ();
154 for (lba_formats = info->lba_formats; lba_formats && *lba_formats; lba_formats++)
155 g_ptr_array_add (ptr_array, bd_nvme_lba_format_copy (*lba_formats));
156 g_ptr_array_add (ptr_array, NULL);
157 new_info->lba_formats = (BDNVMELBAFormat **) g_ptr_array_free (ptr_array, FALSE);
158
159 return new_info;
160 }
161
162 /**
163 * bd_nvme_smart_log_free: (skip)
164 * @log: (nullable): %BDNVMESmartLog to free
165 *
166 * Frees @log.
167 */
168 void bd_nvme_smart_log_free (BDNVMESmartLog *log) {
169 g_free (log);
170 }
171
172 /**
173 * bd_nvme_smart_log_copy: (skip)
174 * @log: (nullable): %BDNVMESmartLog to copy
175 *
176 * Creates a new copy of @log.
177 */
178 BDNVMESmartLog * bd_nvme_smart_log_copy (BDNVMESmartLog *log) {
179 BDNVMESmartLog *new_log;
180
181 if (log == NULL)
182 return NULL;
183
184 new_log = g_new0 (BDNVMESmartLog, 1);
185 memcpy (new_log, log, sizeof (BDNVMESmartLog));
186
187 return new_log;
188 }
189
190 /**
191 * bd_nvme_error_log_entry_free: (skip)
192 * @entry: (nullable): %BDNVMEErrorLogEntry to free
193 *
194 * Frees @entry.
195 */
196 void bd_nvme_error_log_entry_free (BDNVMEErrorLogEntry *entry) {
197 if (entry == NULL)
198 return;
199
200 if (entry->command_error)
201 g_error_free (entry->command_error);
202 g_free (entry);
203 }
204
205 /**
206 * bd_nvme_error_log_entry_copy: (skip)
207 * @entry: (nullable): %BDNVMEErrorLogEntry to copy
208 *
209 * Creates a new copy of @entry.
210 */
211 BDNVMEErrorLogEntry * bd_nvme_error_log_entry_copy (BDNVMEErrorLogEntry *entry) {
212 BDNVMEErrorLogEntry *new_entry;
213
214 if (entry == NULL)
215 return NULL;
216
217 new_entry = g_new0 (BDNVMEErrorLogEntry, 1);
218 memcpy (new_entry, entry, sizeof (BDNVMEErrorLogEntry));
219 if (entry->command_error)
220 new_entry->command_error = g_error_copy (entry->command_error);
221
222 return new_entry;
223 }
224
225 /**
226 * bd_nvme_self_test_log_entry_free: (skip)
227 * @entry: (nullable): %BDNVMESelfTestLogEntry to free
228 *
229 * Frees @entry.
230 */
231 void bd_nvme_self_test_log_entry_free (BDNVMESelfTestLogEntry *entry) {
232 if (entry == NULL)
233 return;
234
235 if (entry->status_code_error)
236 g_error_free (entry->status_code_error);
237 g_free (entry);
238 }
239
240 /**
241 * bd_nvme_self_test_log_entry_copy: (skip)
242 * @entry: (nullable): %BDNVMESelfTestLogEntry to copy
243 *
244 * Creates a new copy of @entry.
245 */
246 BDNVMESelfTestLogEntry * bd_nvme_self_test_log_entry_copy (BDNVMESelfTestLogEntry *entry) {
247 BDNVMESelfTestLogEntry *new_entry;
248
249 if (entry == NULL)
250 return NULL;
251
252 new_entry = g_new0 (BDNVMESelfTestLogEntry, 1);
253 memcpy (new_entry, entry, sizeof (BDNVMESelfTestLogEntry));
254 if (entry->status_code_error)
255 new_entry->status_code_error = g_error_copy (entry->status_code_error);
256
257 return new_entry;
258 }
259
260 /**
261 * bd_nvme_self_test_result_to_string:
262 * @result: A %BDNVMESelfTestResult.
263 * @error: (out) (optional): place to store error (if any)
264 *
265 * Returns: (transfer none): A string representation of @result for use as an identifier string
266 * or %NULL when the code is unknown.
267 */
268 const gchar * bd_nvme_self_test_result_to_string (BDNVMESelfTestResult result, GError **error) {
269 static const gchar * const str[] = {
270 [BD_NVME_SELF_TEST_RESULT_NO_ERROR] = "success",
271 [BD_NVME_SELF_TEST_RESULT_ABORTED] = "aborted",
272 [BD_NVME_SELF_TEST_RESULT_CTRL_RESET] = "ctrl_reset",
273 [BD_NVME_SELF_TEST_RESULT_NS_REMOVED] = "ns_removed",
274 [BD_NVME_SELF_TEST_RESULT_ABORTED_FORMAT] = "aborted_format",
275 [BD_NVME_SELF_TEST_RESULT_FATAL_ERROR] = "fatal_error",
276 [BD_NVME_SELF_TEST_RESULT_UNKNOWN_SEG_FAIL] = "unknown_seg_fail",
277 [BD_NVME_SELF_TEST_RESULT_KNOWN_SEG_FAIL] = "known_seg_fail",
278 [BD_NVME_SELF_TEST_RESULT_ABORTED_UNKNOWN] = "aborted_unknown",
279 [BD_NVME_SELF_TEST_RESULT_ABORTED_SANITIZE] = "aborted_sanitize"
280 };
281
282 if (result >= G_N_ELEMENTS (str)) {
283 g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT,
284 "Invalid result code %d", result);
285 return NULL;
286 }
287
288 return str[result];
289 }
290
291 /**
292 * bd_nvme_self_test_log_free: (skip)
293 * @log: (nullable): %BDNVMESelfTestLog to free
294 *
295 * Frees @log.
296 */
297 void bd_nvme_self_test_log_free (BDNVMESelfTestLog *log) {
298 BDNVMESelfTestLogEntry **entries;
299
300 if (log == NULL)
301 return;
302
303 for (entries = log->entries; entries && *entries; entries++)
304 bd_nvme_self_test_log_entry_free (*entries);
305 g_free (log->entries);
306 g_free (log);
307 }
308
309 /**
310 * bd_nvme_self_test_log_copy: (skip)
311 * @log: (nullable): %BDNVMESelfTestLog to copy
312 *
313 * Creates a new copy of @log.
314 */
315 BDNVMESelfTestLog * bd_nvme_self_test_log_copy (BDNVMESelfTestLog *log) {
316 BDNVMESelfTestLog *new_log;
317 BDNVMESelfTestLogEntry **entries;
318 GPtrArray *ptr_array;
319
320 if (log == NULL)
321 return NULL;
322
323 new_log = g_new0 (BDNVMESelfTestLog, 1);
324 memcpy (new_log, log, sizeof (BDNVMESelfTestLog));
325
326 ptr_array = g_ptr_array_new ();
327 for (entries = log->entries; entries && *entries; entries++)
328 g_ptr_array_add (ptr_array, bd_nvme_self_test_log_entry_copy (*entries));
329 g_ptr_array_add (ptr_array, NULL);
330 new_log->entries = (BDNVMESelfTestLogEntry **) g_ptr_array_free (ptr_array, FALSE);
331
332 return new_log;
333 }
334
335
336 /**
337 * bd_nvme_sanitize_log_free: (skip)
338 * @log: (nullable): %BDNVMESanitizeLog to free
339 *
340 * Frees @log.
341 */
342 void bd_nvme_sanitize_log_free (BDNVMESanitizeLog *log) {
343 if (log == NULL)
344 return;
345
346 g_free (log);
347 }
348
349 /**
350 * bd_nvme_sanitize_log_copy: (skip)
351 * @log: (nullable): %BDNVMESanitizeLog to copy
352 *
353 * Creates a new copy of @log.
354 */
355 BDNVMESanitizeLog * bd_nvme_sanitize_log_copy (BDNVMESanitizeLog *log) {
356 BDNVMESanitizeLog *new_log;
357
358 if (log == NULL)
359 return NULL;
360
361 new_log = g_new0 (BDNVMESanitizeLog, 1);
362 memcpy (new_log, log, sizeof (BDNVMESanitizeLog));
363
364 return new_log;
365 }
366
367
368 /* can't use real __int128 due to gobject-introspection */
369 static guint64 int128_to_guint64 (__u8 data[16])
370 {
371 int i;
372 __u8 d[16];
373 guint64 result = 0;
374
375 /* endianness conversion */
376 #if G_BYTE_ORDER == G_BIG_ENDIAN
377 for (i = 0; i < 16; i++)
378 d[i] = data[15 - i];
379 #else
380 memcpy (d, data, sizeof (d));
381 #endif
382
383 /* FIXME: would overflow */
384 /* https://github.com/linux-nvme/libnvme/issues/475 */
385 for (i = 0; i < 16; i++) {
386 result *= 256;
387 result += d[15 - i];
388 }
389 return result;
390 }
391
392 gint _open_dev (const gchar *device, GError **error) {
393 int fd;
394
395 fd = open (device, O_RDONLY);
396 if (fd < 0) {
397 _nvme_status_to_error (-1, FALSE, error);
398 g_prefix_error (error, "Failed to open device '%s': ", device);
399 return -1;
400 }
401
402 return fd;
403 }
404
405 /* backported from nvme-cli: https://github.com/linux-nvme/nvme-cli/pull/2051 */
406 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
407
408 void *_nvme_alloc (size_t len, GError **error)
409 {
410 size_t _len = ROUND_UP (len, 0x1000);
411 void *p;
412
413 if (posix_memalign ((void *) &p, getpagesize (), _len)) {
414 g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED,
415 "Memory allocation failed: %m");
416 return NULL;
417 }
418
419 memset (p, 0, _len);
420 return p;
421 }
422
423 static gchar *decode_nvme_rev (guint32 ver) {
424 guint16 mjr;
425 guint8 mnr, ter = 0;
426
427 mjr = ver >> 16;
428 mnr = (ver >> 8) & 0xFF;
429 /* 'ter' is only valid for >= 1.2.1 */
430 if (mjr >= 2 || mnr >= 2)
431 ter = ver & 0xFF;
432
433 if (mjr == 0 && mnr == 0)
434 return NULL;
435 if (ter == 0)
436 return g_strdup_printf ("%u.%u", mjr, mnr);
437 else
438 return g_strdup_printf ("%u.%u.%u", mjr, mnr, ter);
439 }
440
441 static gchar *_uuid_to_str (unsigned char uuid[NVME_UUID_LEN]) {
442 gchar uuid_buf[NVME_UUID_LEN_STRING] = ZERO_INIT;
443
444 if (nvme_uuid_to_string (uuid, uuid_buf) == 0)
445 return g_strdup (uuid_buf);
446 return NULL;
447 }
448
449 static gboolean _nvme_a_is_zero (const __u8 a[], int len) {
450 int i;
451
452 for (i = 0; i < len; i++)
453 if (a[i] > 0)
454 return FALSE;
455 return TRUE;
456 }
457
458 /**
459 * bd_nvme_get_controller_info:
460 * @device: a NVMe controller device (e.g. `/dev/nvme0`)
461 * @error: (out) (nullable): place to store error (if any)
462 *
463 * Retrieves information about the NVMe controller (the Identify Controller command)
464 * as specified by the @device block device path.
465 *
466 * Returns: (transfer full): information about given controller or %NULL in case of an error (with @error set).
467 *
468 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
469 */
470 BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError **error) {
471 int ret;
472 int fd;
473 struct nvme_id_ctrl *ctrl_id;
474 BDNVMEControllerInfo *info;
475
476 /* open the block device */
477 fd = _open_dev (device, error);
478 if (fd < 0)
479 return NULL;
480
481 ctrl_id = _nvme_alloc (sizeof (struct nvme_id_ctrl), error);
482 if (!ctrl_id) {
483 close (fd);
484 return NULL;
485 }
486 /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
487 ret = nvme_identify_ctrl (fd, ctrl_id);
488 if (ret != 0) {
489 _nvme_status_to_error (ret, FALSE, error);
490 g_prefix_error (error, "NVMe Identify Controller command error: ");
491 close (fd);
492 free (ctrl_id);
493 return NULL;
494 }
495 close (fd);
496
497 info = g_new0 (BDNVMEControllerInfo, 1);
498 if ((ctrl_id->cmic & NVME_CTRL_CMIC_MULTI_PORT) == NVME_CTRL_CMIC_MULTI_PORT)
499 info->features |= BD_NVME_CTRL_FEAT_MULTIPORT;
500 if ((ctrl_id->cmic & NVME_CTRL_CMIC_MULTI_CTRL) == NVME_CTRL_CMIC_MULTI_CTRL)
501 info->features |= BD_NVME_CTRL_FEAT_MULTICTRL;
502 if ((ctrl_id->cmic & NVME_CTRL_CMIC_MULTI_SRIOV) == NVME_CTRL_CMIC_MULTI_SRIOV)
503 info->features |= BD_NVME_CTRL_FEAT_SRIOV;
504 if ((ctrl_id->cmic & NVME_CTRL_CMIC_MULTI_ANA_REPORTING) == NVME_CTRL_CMIC_MULTI_ANA_REPORTING)
505 info->features |= BD_NVME_CTRL_FEAT_ANA_REPORTING;
506 if ((ctrl_id->nvmsr & NVME_CTRL_NVMSR_NVMESD) == NVME_CTRL_NVMSR_NVMESD)
507 info->features |= BD_NVME_CTRL_FEAT_STORAGE_DEVICE;
508 if ((ctrl_id->nvmsr & NVME_CTRL_NVMSR_NVMEE) == NVME_CTRL_NVMSR_NVMEE)
509 info->features |= BD_NVME_CTRL_FEAT_ENCLOSURE;
510 if ((ctrl_id->mec & NVME_CTRL_MEC_PCIEME) == NVME_CTRL_MEC_PCIEME)
511 info->features |= BD_NVME_CTRL_FEAT_MGMT_PCIE;
512 if ((ctrl_id->mec & NVME_CTRL_MEC_SMBUSME) == NVME_CTRL_MEC_SMBUSME)
513 info->features |= BD_NVME_CTRL_FEAT_MGMT_SMBUS;
514 info->pci_vendor_id = GUINT16_FROM_LE (ctrl_id->vid);
515 info->pci_subsys_vendor_id = GUINT16_FROM_LE (ctrl_id->ssvid);
516 info->ctrl_id = GUINT16_FROM_LE (ctrl_id->cntlid);
517 if (!_nvme_a_is_zero (ctrl_id->fguid, sizeof (ctrl_id->fguid)))
518 info->fguid = _uuid_to_str (ctrl_id->fguid);
519 info->model_number = g_strndup (ctrl_id->mn, sizeof (ctrl_id->mn));
520 g_strstrip (info->model_number);
521 info->serial_number = g_strndup (ctrl_id->sn, sizeof (ctrl_id->sn));
522 g_strstrip (info->serial_number);
523 info->firmware_ver = g_strndup (ctrl_id->fr, sizeof (ctrl_id->fr));
524 g_strstrip (info->firmware_ver);
525 info->nvme_ver = decode_nvme_rev (GUINT32_FROM_LE (ctrl_id->ver));
526 /* TODO: vwci: VPD Write Cycle Information */
527 if ((ctrl_id->oacs & NVME_CTRL_OACS_FORMAT) == NVME_CTRL_OACS_FORMAT)
528 info->features |= BD_NVME_CTRL_FEAT_FORMAT;
529 if ((ctrl_id->oacs & NVME_CTRL_OACS_NS_MGMT) == NVME_CTRL_OACS_NS_MGMT)
530 info->features |= BD_NVME_CTRL_FEAT_NS_MGMT;
531 if ((ctrl_id->oacs & NVME_CTRL_OACS_SELF_TEST) == NVME_CTRL_OACS_SELF_TEST)
532 info->features |= BD_NVME_CTRL_FEAT_SELFTEST;
533 switch (ctrl_id->cntrltype) {
534 case NVME_CTRL_CNTRLTYPE_IO:
535 info->controller_type = BD_NVME_CTRL_TYPE_IO;
536 break;
537 case NVME_CTRL_CNTRLTYPE_DISCOVERY:
538 info->controller_type = BD_NVME_CTRL_TYPE_DISCOVERY;
539 break;
540 case NVME_CTRL_CNTRLTYPE_ADMIN:
541 info->controller_type = BD_NVME_CTRL_TYPE_ADMIN;
542 break;
543 default:
544 info->controller_type = BD_NVME_CTRL_TYPE_UNKNOWN;
545 }
546 info->hmb_pref_size = GUINT32_FROM_LE (ctrl_id->hmpre) * 4096LL;
547 info->hmb_min_size = GUINT32_FROM_LE (ctrl_id->hmmin) * 4096LL;
548 info->size_total = int128_to_guint64 (ctrl_id->tnvmcap);
549 info->size_unalloc = int128_to_guint64 (ctrl_id->unvmcap);
550 info->selftest_ext_time = GUINT16_FROM_LE (ctrl_id->edstt);
551 /* TODO: lpa: Log Page Attributes - NVME_CTRL_LPA_PERSETENT_EVENT: Persistent Event log */
552 if ((ctrl_id->dsto & NVME_CTRL_DSTO_ONE_DST) == NVME_CTRL_DSTO_ONE_DST)
553 info->features |= BD_NVME_CTRL_FEAT_SELFTEST_SINGLE;
554 if ((ctrl_id->sanicap & NVME_CTRL_SANICAP_CES) == NVME_CTRL_SANICAP_CES)
555 info->features |= BD_NVME_CTRL_FEAT_SANITIZE_CRYPTO;
556 if ((ctrl_id->sanicap & NVME_CTRL_SANICAP_BES) == NVME_CTRL_SANICAP_BES)
557 info->features |= BD_NVME_CTRL_FEAT_SANITIZE_BLOCK;
558 if ((ctrl_id->sanicap & NVME_CTRL_SANICAP_OWS) == NVME_CTRL_SANICAP_OWS)
559 info->features |= BD_NVME_CTRL_FEAT_SANITIZE_OVERWRITE;
560 /* struct nvme_id_ctrl.nn: If the &struct nvme_id_ctrl.mnan field is cleared to 0h,
561 * then the struct nvme_id_ctrl.nn field also indicates the maximum number of namespaces
562 * supported by the NVM subsystem.
563 */
564 info->num_namespaces = GUINT32_FROM_LE (ctrl_id->mnan) == 0 ? GUINT32_FROM_LE (ctrl_id->nn) : GUINT32_FROM_LE (ctrl_id->mnan);
565 if ((ctrl_id->fna & NVME_CTRL_FNA_FMT_ALL_NAMESPACES) == NVME_CTRL_FNA_FMT_ALL_NAMESPACES)
566 info->features |= BD_NVME_CTRL_FEAT_FORMAT_ALL_NS;
567 if ((ctrl_id->fna & NVME_CTRL_FNA_SEC_ALL_NAMESPACES) == NVME_CTRL_FNA_SEC_ALL_NAMESPACES)
568 info->features |= BD_NVME_CTRL_FEAT_SECURE_ERASE_ALL_NS;
569 if ((ctrl_id->fna & NVME_CTRL_FNA_CRYPTO_ERASE) == NVME_CTRL_FNA_CRYPTO_ERASE)
570 info->features |= BD_NVME_CTRL_FEAT_SECURE_ERASE_CRYPTO;
571 /* TODO: enum nvme_id_ctrl_oncs: NVME_CTRL_ONCS_WRITE_UNCORRECTABLE, NVME_CTRL_ONCS_WRITE_ZEROES... */
572 /* TODO: nwpc: Namespace Write Protection Capabilities */
573 info->subsysnqn = g_strndup (ctrl_id->subnqn, sizeof (ctrl_id->subnqn));
574 g_strstrip (info->subsysnqn);
575
576 free (ctrl_id);
577 return info;
578 }
579
580
581 /**
582 * bd_nvme_get_namespace_info:
583 * @device: a NVMe namespace device (e.g. `/dev/nvme0n1`)
584 * @error: (out) (nullable): place to store error (if any)
585 *
586 * Retrieves information about the NVMe namespace (the Identify Namespace command)
587 * as specified by the @device block device path.
588 *
589 * Returns: (transfer full): information about given namespace or %NULL in case of an error (with @error set).
590 *
591 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
592 */
593 BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **error) {
594 int ret;
595 int ret_ctrl;
596 int ret_desc = -1;
597 int ret_ns_ind = -1;
598 int fd;
599 __u32 nsid = 0;
600 struct nvme_id_ctrl *ctrl_id;
601 struct nvme_id_ns *ns_info;
602 struct nvme_id_independent_id_ns *ns_info_ind = NULL;
603 struct nvme_ns_id_desc *descs = NULL;
604 guint8 flbas;
605 guint i;
606 guint j;
607 guint len;
608 BDNVMENamespaceInfo *info;
609 GPtrArray *ptr_array;
610
611 /* open the block device */
612 fd = _open_dev (device, error);
613 if (fd < 0)
614 return NULL;
615
616 /* get Namespace Identifier (NSID) for the @device (NVME_IOCTL_ID) */
617 ret = nvme_get_nsid (fd, &nsid);
618 if (ret != 0) {
619 _nvme_status_to_error (ret, FALSE, error);
620 g_prefix_error (error, "Error getting Namespace Identifier (NSID): ");
621 close (fd);
622 return NULL;
623 }
624
625 /* send the NVME_IDENTIFY_CNS_NS ioctl */
626 ns_info = _nvme_alloc (sizeof (struct nvme_id_ns), error);
627 if (!ns_info) {
628 close (fd);
629 return NULL;
630 }
631 ret = nvme_identify_ns (fd, nsid, ns_info);
632 if (ret != 0) {
633 _nvme_status_to_error (ret, FALSE, error);
634 g_prefix_error (error, "NVMe Identify Namespace command error: ");
635 close (fd);
636 free (ns_info);
637 return NULL;
638 }
639
640 /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
641 ctrl_id = _nvme_alloc (sizeof (struct nvme_id_ctrl), error);
642 if (!ctrl_id) {
643 close (fd);
644 free (ns_info);
645 return NULL;
646 }
647 ret_ctrl = nvme_identify_ctrl (fd, ctrl_id);
648
649 /* send the NVME_IDENTIFY_CNS_NS_DESC_LIST ioctl, NVMe 1.3 */
650 if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id->ver) >= 0x10300) {
651 descs = _nvme_alloc (NVME_IDENTIFY_DATA_SIZE, NULL);
652 if (descs != NULL)
653 ret_desc = nvme_identify_ns_descs (fd, nsid, descs);
654 }
655
656 /* send the NVME_IDENTIFY_CNS_CSI_INDEPENDENT_ID_NS ioctl, NVMe 2.0 */
657 if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id->ver) >= 0x20000) {
658 ns_info_ind = _nvme_alloc (sizeof (struct nvme_id_independent_id_ns), NULL);
659 if (ns_info_ind != NULL)
660 ret_ns_ind = nvme_identify_independent_identify_ns (fd, nsid, ns_info_ind);
661 }
662 close (fd);
663
664 info = g_new0 (BDNVMENamespaceInfo, 1);
665 info->nsid = nsid;
666 info->nsize = GUINT64_FROM_LE (ns_info->nsze);
667 info->ncap = GUINT64_FROM_LE (ns_info->ncap);
668 info->nuse = GUINT64_FROM_LE (ns_info->nuse);
669 if ((ns_info->nsfeat & NVME_NS_FEAT_THIN) == NVME_NS_FEAT_THIN)
670 info->features |= BD_NVME_NS_FEAT_THIN;
671 if ((ns_info->nmic & NVME_NS_NMIC_SHARED) == NVME_NS_NMIC_SHARED)
672 info->features |= BD_NVME_NS_FEAT_MULTIPATH_SHARED;
673 if ((ns_info->fpi & NVME_NS_FPI_SUPPORTED) == NVME_NS_FPI_SUPPORTED)
674 info->features |= BD_NVME_NS_FEAT_FORMAT_PROGRESS;
675 info->format_progress_remaining = ns_info->fpi & NVME_NS_FPI_REMAINING;
676 /* TODO: what the ns_info->nvmcap really stands for? */
677 info->write_protected = (ns_info->nsattr & NVME_NS_NSATTR_WRITE_PROTECTED) == NVME_NS_NSATTR_WRITE_PROTECTED;
678
679 if (ret_desc == 0) {
680 for (i = 0; i < NVME_IDENTIFY_DATA_SIZE; i += len) {
681 struct nvme_ns_id_desc *d = descs + i;
682
683 if (!d->nidl)
684 break;
685 len = d->nidl + sizeof (*d);
686
687 switch (d->nidt) {
688 case NVME_NIDT_EUI64:
689 g_free (info->eui64);
690 info->eui64 = g_malloc0 (d->nidl * 2 + 1);
691 for (j = 0; j < d->nidl; j++)
692 snprintf (info->eui64 + j * 2, 3, "%02x", d->nid[j]);
693 break;
694 case NVME_NIDT_NGUID:
695 g_free (info->nguid);
696 info->nguid = g_malloc0 (d->nidl * 2 + 1);
697 for (j = 0; j < d->nidl; j++)
698 snprintf (info->nguid + j * 2, 3, "%02x", d->nid[j]);
699 break;
700 case NVME_NIDT_UUID:
701 g_free (info->uuid);
702 info->uuid = _uuid_to_str (d->nid);
703 break;
704 case NVME_NIDT_CSI:
705 /* unused */
706 break;
707 }
708 }
709 }
710
711 if (info->nguid == NULL && !_nvme_a_is_zero (ns_info->nguid, sizeof (ns_info->nguid))) {
712 info->nguid = g_malloc0 (sizeof (ns_info->nguid) * 2 + 1);
713 for (i = 0; i < sizeof (ns_info->nguid); i++)
714 snprintf (info->nguid + i * 2, 3, "%02x", ns_info->nguid[i]);
715 }
716 if (info->eui64 == NULL && !_nvme_a_is_zero (ns_info->eui64, sizeof (ns_info->eui64))) {
717 info->eui64 = g_malloc0 (sizeof (ns_info->eui64) * 2 + 1);
718 for (i = 0; i < sizeof (ns_info->eui64); i++)
719 snprintf (info->eui64 + i * 2, 3, "%02x", ns_info->eui64[i]);
720 }
721 if (ret_ns_ind == 0) {
722 if ((ns_info_ind->nsfeat & 1 << 4) == 1 << 4)
723 info->features |= BD_NVME_NS_FEAT_ROTATIONAL;
724 }
725
726 /* translate the LBA Format array */
727 ptr_array = g_ptr_array_new ();
728 nvme_id_ns_flbas_to_lbaf_inuse (ns_info->flbas, &flbas);
729 for (i = 0; i <= ns_info->nlbaf + ns_info->nulbaf; i++) {
730 BDNVMELBAFormat *lbaf = g_new0 (BDNVMELBAFormat, 1);
731 lbaf->data_size = 1 << ns_info->lbaf[i].ds;
732 lbaf->metadata_size = GUINT16_FROM_LE (ns_info->lbaf[i].ms);
733 lbaf->relative_performance = ns_info->lbaf[i].rp + 1;
734 g_ptr_array_add (ptr_array, lbaf);
735 if (i == flbas) {
736 info->current_lba_format.data_size = lbaf->data_size;
737 info->current_lba_format.metadata_size = lbaf->metadata_size;
738 info->current_lba_format.relative_performance = lbaf->relative_performance;
739 }
740 }
741 g_ptr_array_add (ptr_array, NULL); /* trailing NULL element */
742 info->lba_formats = (BDNVMELBAFormat **) g_ptr_array_free (ptr_array, FALSE);
743
744 free (ctrl_id);
745 free (ns_info);
746 free (ns_info_ind);
747 free (descs);
748 return info;
749 }
750
751
752 /**
753 * bd_nvme_get_smart_log:
754 * @device: a NVMe controller device (e.g. `/dev/nvme0`)
755 * @error: (out) (nullable): place to store error (if any)
756 *
757 * Retrieves drive SMART and general health information (Log Identifier `02h`).
758 * The information provided is over the life of the controller and is retained across power cycles.
759 *
760 * Returns: (transfer full): health log data or %NULL in case of an error (with @error set).
761 *
762 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
763 */
764 BDNVMESmartLog * bd_nvme_get_smart_log (const gchar *device, GError **error) {
765 int ret;
766 int ret_identify;
767 int fd;
768 struct nvme_id_ctrl *ctrl_id;
769 struct nvme_smart_log *smart_log;
770 BDNVMESmartLog *log;
771 guint i;
772
773 /* open the block device */
774 fd = _open_dev (device, error);
775 if (fd < 0)
776 return NULL;
777
778 /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
779 ctrl_id = _nvme_alloc (sizeof (struct nvme_id_ctrl), error);
780 if (!ctrl_id) {
781 close (fd);
782 return NULL;
783 }
784 ret_identify = nvme_identify_ctrl (fd, ctrl_id);
785 if (ret_identify != 0) {
786 _nvme_status_to_error (ret_identify, FALSE, error);
787 g_prefix_error (error, "NVMe Identify Controller command error: ");
788 close (fd);
789 free (ctrl_id);
790 return NULL;
791 }
792
793 /* send the NVME_LOG_LID_SMART ioctl */
794 smart_log = _nvme_alloc (sizeof (struct nvme_smart_log), error);
795 if (!smart_log) {
796 close (fd);
797 free (ctrl_id);
798 return NULL;
799 }
800 ret = nvme_get_log_smart (fd, NVME_NSID_ALL, FALSE /* rae */, smart_log);
801 if (ret != 0) {
802 _nvme_status_to_error (ret, FALSE, error);
803 g_prefix_error (error, "NVMe Get Log Page - SMART / Health Information Log command error: ");
804 close (fd);
805 free (ctrl_id);
806 free (smart_log);
807 return NULL;
808 }
809 close (fd);
810
811 log = g_new0 (BDNVMESmartLog, 1);
812 if ((smart_log->critical_warning & NVME_SMART_CRIT_SPARE) == NVME_SMART_CRIT_SPARE)
813 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_SPARE;
814 if ((smart_log->critical_warning & NVME_SMART_CRIT_TEMPERATURE) == NVME_SMART_CRIT_TEMPERATURE)
815 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_TEMPERATURE;
816 if ((smart_log->critical_warning & NVME_SMART_CRIT_DEGRADED) == NVME_SMART_CRIT_DEGRADED)
817 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_DEGRADED;
818 if ((smart_log->critical_warning & NVME_SMART_CRIT_MEDIA) == NVME_SMART_CRIT_MEDIA)
819 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_READONLY;
820 if ((smart_log->critical_warning & NVME_SMART_CRIT_VOLATILE_MEMORY) == NVME_SMART_CRIT_VOLATILE_MEMORY)
821 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_VOLATILE_MEM;
822 if ((smart_log->critical_warning & NVME_SMART_CRIT_PMR_RO) == NVME_SMART_CRIT_PMR_RO)
823 log->critical_warning |= BD_NVME_SMART_CRITICAL_WARNING_PMR_READONLY;
824 log->avail_spare = smart_log->avail_spare;
825 log->spare_thresh = smart_log->spare_thresh;
826 log->percent_used = smart_log->percent_used;
827 log->total_data_read = int128_to_guint64 (smart_log->data_units_read) * 1000 * 512;
828 log->total_data_written = int128_to_guint64 (smart_log->data_units_written) * 1000 * 512;
829 log->ctrl_busy_time = int128_to_guint64 (smart_log->ctrl_busy_time);
830 log->power_cycles = int128_to_guint64 (smart_log->power_cycles);
831 log->power_on_hours = int128_to_guint64 (smart_log->power_on_hours);
832 log->unsafe_shutdowns = int128_to_guint64 (smart_log->unsafe_shutdowns);
833 log->media_errors = int128_to_guint64 (smart_log->media_errors);
834 log->num_err_log_entries = int128_to_guint64 (smart_log->num_err_log_entries);
835
836 log->temperature = (smart_log->temperature[1] << 8) | smart_log->temperature[0];
837 for (i = 0; i < G_N_ELEMENTS (smart_log->temp_sensor); i++)
838 log->temp_sensors[i] = GUINT16_FROM_LE (smart_log->temp_sensor[i]);
839 log->warning_temp_time = GUINT32_FROM_LE (smart_log->warning_temp_time);
840 log->critical_temp_time = GUINT32_FROM_LE (smart_log->critical_comp_time);
841
842 if (ret_identify == 0) {
843 log->wctemp = GUINT16_FROM_LE (ctrl_id->wctemp);
844 log->cctemp = GUINT16_FROM_LE (ctrl_id->cctemp);
845 }
846
847 /* FIXME: intentionally not providing Host Controlled Thermal Management attributes
848 * at the moment (an optional NVMe feature), along with intentionally not providing
849 * Power State attributes. Subject to re-evaluation in the future.
850 */
851
852 free (ctrl_id);
853 free (smart_log);
854 return log;
855 }
856
857
858 /**
859 * bd_nvme_get_error_log_entries:
860 * @device: a NVMe controller device (e.g. `/dev/nvme0`)
861 * @error: (out) (nullable): place to store error (if any)
862 *
863 * Retrieves Error Information Log (Log Identifier `01h`) entries, used to describe
864 * extended error information for a command that completed with error or to report
865 * an error that is not specific to a particular command. This log is global to the
866 * controller. The ordering of the entries is based on the time when the error
867 * occurred, with the most recent error being returned as the first log entry.
868 * As the number of entries is typically limited by the drive implementation, only
869 * most recent entries are provided.
870 *
871 * Returns: (transfer full) (array zero-terminated=1): null-terminated list
872 * of error entries or %NULL in case of an error (with @error set).
873 *
874 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
875 */
876 BDNVMEErrorLogEntry ** bd_nvme_get_error_log_entries (const gchar *device, GError **error) {
877 int ret;
878 int fd;
879 guint elpe;
880 struct nvme_id_ctrl *ctrl_id;
881 struct nvme_error_log_page *err_log;
882 GPtrArray *ptr_array;
883 guint i;
884
885 /* open the block device */
886 fd = _open_dev (device, error);
887 if (fd < 0)
888 return NULL;
889
890 /* find out the maximum number of error log entries as reported by the controller */
891 ctrl_id = _nvme_alloc (sizeof (struct nvme_id_ctrl), error);
892 if (!ctrl_id) {
893 close (fd);
894 return NULL;
895 }
896 ret = nvme_identify_ctrl (fd, ctrl_id);
897 if (ret != 0) {
898 _nvme_status_to_error (ret, FALSE, error);
899 g_prefix_error (error, "NVMe Identify Controller command error: ");
900 close (fd);
901 free (ctrl_id);
902 return NULL;
903 }
904
905 elpe = ctrl_id->elpe + 1;
906 free (ctrl_id);
907
908 /* send the NVME_LOG_LID_ERROR ioctl */
909 err_log = _nvme_alloc (sizeof (struct nvme_error_log_page) * elpe, error);
910 if (!err_log) {
911 close (fd);
912 return NULL;
913 }
914 ret = nvme_get_log_error (fd, elpe, FALSE /* rae */, err_log);
915 if (ret != 0) {
916 _nvme_status_to_error (ret, FALSE, error);
917 g_prefix_error (error, "NVMe Get Log Page - Error Information Log Entry command error: ");
918 close (fd);
919 free (err_log);
920 return NULL;
921 }
922 close (fd);
923
924 /* parse the log */
925 ptr_array = g_ptr_array_new ();
926 for (i = 0; i < elpe; i++) {
927 if (GUINT64_FROM_LE (err_log[i].error_count) > 0) {
928 BDNVMEErrorLogEntry *entry;
929
930 entry = g_new0 (BDNVMEErrorLogEntry, 1);
931 entry->error_count = GUINT64_FROM_LE (err_log[i].error_count);
932 entry->command_id = err_log[i].cmdid;
933 entry->command_specific = GUINT64_FROM_LE (err_log[i].cs);
934 entry->command_status = GUINT16_FROM_LE (err_log[i].status_field) >> 1;
935 _nvme_status_to_error (GUINT16_FROM_LE (err_log[i].status_field) >> 1, FALSE, &entry->command_error);
936 entry->lba = GUINT64_FROM_LE (err_log[i].lba);
937 entry->nsid = err_log[i].nsid;
938 entry->transport_type = err_log[i].trtype;
939 /* not providing Transport Type Specific Information here on purpose */
940
941 g_ptr_array_add (ptr_array, entry);
942 }
943 }
944 g_ptr_array_add (ptr_array, NULL); /* trailing NULL element */
945 free (err_log);
946
947 return (BDNVMEErrorLogEntry **) g_ptr_array_free (ptr_array, FALSE);
948 }
949
950
951 /**
952 * bd_nvme_get_self_test_log:
953 * @device: a NVMe controller device (e.g. `/dev/nvme0`)
954 * @error: (out) (nullable): place to store error (if any)
955 *
956 * Retrieves drive self-test log (Log Identifier `06h`). Provides the status of a self-test operation
957 * in progress and the percentage complete of that operation, along with the results of the last
958 * 20 device self-test operations.
959 *
960 * Returns: (transfer full): self-test log data or %NULL in case of an error (with @error set).
961 *
962 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
963 */
964 BDNVMESelfTestLog * bd_nvme_get_self_test_log (const gchar *device, GError **error) {
965 int ret;
966 int fd;
967 struct nvme_self_test_log *self_test_log;
968 BDNVMESelfTestLog *log;
969 GPtrArray *ptr_array;
970 guint i;
971
972 /* open the block device */
973 fd = _open_dev (device, error);
974 if (fd < 0)
975 return NULL;
976
977 /* send the NVME_LOG_LID_DEVICE_SELF_TEST ioctl */
978 self_test_log = _nvme_alloc (sizeof (struct nvme_self_test_log), error);
979 if (!self_test_log) {
980 close (fd);
981 return NULL;
982 }
983 ret = nvme_get_log_device_self_test (fd, self_test_log);
984 if (ret != 0) {
985 _nvme_status_to_error (ret, FALSE, error);
986 g_prefix_error (error, "NVMe Get Log Page - Device Self-test Log command error: ");
987 close (fd);
988 free (self_test_log);
989 return NULL;
990 }
991 close (fd);
992
993 log = g_new0 (BDNVMESelfTestLog, 1);
994 switch (self_test_log->current_operation & NVME_ST_CURR_OP_MASK) {
995 case NVME_ST_CURR_OP_NOT_RUNNING:
996 log->current_operation = BD_NVME_SELF_TEST_ACTION_NOT_RUNNING;
997 break;
998 case NVME_ST_CURR_OP_SHORT:
999 log->current_operation = BD_NVME_SELF_TEST_ACTION_SHORT;
1000 break;
1001 case NVME_ST_CURR_OP_EXTENDED:
1002 log->current_operation = BD_NVME_SELF_TEST_ACTION_EXTENDED;
1003 break;
1004 case NVME_ST_CURR_OP_VS:
1005 case NVME_ST_CURR_OP_RESERVED:
1006 default:
1007 log->current_operation = BD_NVME_SELF_TEST_ACTION_VENDOR_SPECIFIC;
1008 }
1009 if ((self_test_log->current_operation & NVME_ST_CURR_OP_MASK) > 0)
1010 log->current_operation_completion = self_test_log->completion & NVME_ST_CURR_OP_CMPL_MASK;
1011
1012 ptr_array = g_ptr_array_new ();
1013 for (i = 0; i < NVME_LOG_ST_MAX_RESULTS; i++) {
1014 BDNVMESelfTestLogEntry *entry;
1015 guint8 dsts;
1016 guint8 code;
1017
1018 dsts = self_test_log->result[i].dsts & NVME_ST_RESULT_MASK;
1019 code = self_test_log->result[i].dsts >> NVME_ST_CODE_SHIFT;
1020 if (dsts == NVME_ST_RESULT_NOT_USED)
1021 continue;
1022
1023 entry = g_new0 (BDNVMESelfTestLogEntry, 1);
1024 switch (dsts) {
1025 case NVME_ST_RESULT_NO_ERR:
1026 entry->result = BD_NVME_SELF_TEST_RESULT_NO_ERROR;
1027 break;
1028 case NVME_ST_RESULT_ABORTED:
1029 entry->result = BD_NVME_SELF_TEST_RESULT_ABORTED;
1030 break;
1031 case NVME_ST_RESULT_CLR:
1032 entry->result = BD_NVME_SELF_TEST_RESULT_CTRL_RESET;
1033 break;
1034 case NVME_ST_RESULT_NS_REMOVED:
1035 entry->result = BD_NVME_SELF_TEST_RESULT_NS_REMOVED;
1036 break;
1037 case NVME_ST_RESULT_ABORTED_FORMAT:
1038 entry->result = BD_NVME_SELF_TEST_RESULT_ABORTED_FORMAT;
1039 break;
1040 case NVME_ST_RESULT_FATAL_ERR:
1041 entry->result = BD_NVME_SELF_TEST_RESULT_FATAL_ERROR;
1042 break;
1043 case NVME_ST_RESULT_UNKNOWN_SEG_FAIL:
1044 entry->result = BD_NVME_SELF_TEST_RESULT_UNKNOWN_SEG_FAIL;
1045 break;
1046 case NVME_ST_RESULT_KNOWN_SEG_FAIL:
1047 entry->result = BD_NVME_SELF_TEST_RESULT_KNOWN_SEG_FAIL;
1048 break;
1049 case NVME_ST_RESULT_ABORTED_UNKNOWN:
1050 entry->result = BD_NVME_SELF_TEST_RESULT_ABORTED_UNKNOWN;
1051 break;
1052 case NVME_ST_RESULT_ABORTED_SANITIZE:
1053 entry->result = BD_NVME_SELF_TEST_RESULT_ABORTED_SANITIZE;
1054 break;
1055 default:
1056 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Unhandled self-test log entry result code: %d", dsts);
1057 g_free (entry);
1058 continue;
1059 }
1060 switch (code) {
1061 case NVME_ST_CODE_SHORT:
1062 entry->action = BD_NVME_SELF_TEST_ACTION_SHORT;
1063 break;
1064 case NVME_ST_CODE_EXTENDED:
1065 entry->action = BD_NVME_SELF_TEST_ACTION_EXTENDED;
1066 break;
1067 case NVME_ST_CODE_VS:
1068 case NVME_ST_CODE_RESERVED:
1069 entry->action = BD_NVME_SELF_TEST_ACTION_VENDOR_SPECIFIC;
1070 break;
1071 default:
1072 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Unhandled self-test log entry action code: %d", code);
1073 entry->action = BD_NVME_SELF_TEST_ACTION_VENDOR_SPECIFIC;
1074 }
1075 entry->segment = self_test_log->result[i].seg;
1076 entry->power_on_hours = GUINT64_FROM_LE (self_test_log->result[i].poh);
1077 if (self_test_log->result[i].vdi & NVME_ST_VALID_DIAG_INFO_NSID)
1078 entry->nsid = GUINT32_FROM_LE (self_test_log->result[i].nsid);
1079 if (self_test_log->result[i].vdi & NVME_ST_VALID_DIAG_INFO_FLBA)
1080 entry->failing_lba = GUINT64_FROM_LE (self_test_log->result[i].flba);
1081 if ((self_test_log->result[i].vdi & NVME_ST_VALID_DIAG_INFO_SC) &&
1082 (self_test_log->result[i].vdi & NVME_ST_VALID_DIAG_INFO_SCT))
1083 _nvme_status_to_error ((self_test_log->result[i].sct & 7) << 8 | self_test_log->result[i].sc,
1084 FALSE, &entry->status_code_error);
1085
1086 g_ptr_array_add (ptr_array, entry);
1087 }
1088 g_ptr_array_add (ptr_array, NULL);
1089 log->entries = (BDNVMESelfTestLogEntry **) g_ptr_array_free (ptr_array, FALSE);
1090 free (self_test_log);
1091
1092 return log;
1093 }
1094
1095
1096 /**
1097 * bd_nvme_get_sanitize_log:
1098 * @device: a NVMe controller device (e.g. `/dev/nvme0`)
1099 * @error: (out) (nullable): place to store error (if any)
1100 *
1101 * Retrieves the drive sanitize status log (Log Identifier `81h`) that includes information
1102 * about the most recent sanitize operation and the sanitize operation time estimates.
1103 *
1104 * As advised in the NVMe specification whitepaper the host should limit polling
1105 * to retrieve progress of a running sanitize operations (e.g. to at most once every
1106 * several minutes) to avoid interfering with the progress of the sanitize operation itself.
1107 *
1108 * Returns: (transfer full): sanitize log data or %NULL in case of an error (with @error set).
1109 *
1110 * Tech category: %BD_NVME_TECH_NVME-%BD_NVME_TECH_MODE_INFO
1111 */
1112 BDNVMESanitizeLog * bd_nvme_get_sanitize_log (const gchar *device, GError **error) {
1113 int ret;
1114 int fd;
1115 struct nvme_sanitize_log_page *sanitize_log;
1116 BDNVMESanitizeLog *log;
1117 __u16 sstat;
1118
1119 /* open the block device */
1120 fd = _open_dev (device, error);
1121 if (fd < 0)
1122 return NULL;
1123
1124 /* send the NVME_LOG_LID_SANITIZE ioctl */
1125 sanitize_log = _nvme_alloc (sizeof (struct nvme_sanitize_log_page), error);
1126 if (!sanitize_log) {
1127 close (fd);
1128 return NULL;
1129 }
1130 ret = nvme_get_log_sanitize (fd, FALSE /* rae */, sanitize_log);
1131 if (ret != 0) {
1132 _nvme_status_to_error (ret, FALSE, error);
1133 g_prefix_error (error, "NVMe Get Log Page - Sanitize Status Log command error: ");
1134 close (fd);
1135 free (sanitize_log);
1136 return NULL;
1137 }
1138 close (fd);
1139
1140 log = g_new0 (BDNVMESanitizeLog, 1);
1141 log->sanitize_progress = 0;
1142 sstat = GUINT16_FROM_LE (sanitize_log->sstat);
1143 if ((sstat & NVME_SANITIZE_SSTAT_STATUS_MASK) == NVME_SANITIZE_SSTAT_STATUS_IN_PROGESS)
1144 log->sanitize_progress = ((gdouble) GUINT16_FROM_LE (sanitize_log->sprog) * 100) / 0x10000;
1145 log->global_data_erased = sstat & NVME_SANITIZE_SSTAT_GLOBAL_DATA_ERASED;
1146 log->overwrite_passes = (sstat >> NVME_SANITIZE_SSTAT_COMPLETED_PASSES_SHIFT) & NVME_SANITIZE_SSTAT_COMPLETED_PASSES_MASK;
1147
1148 switch (sstat & NVME_SANITIZE_SSTAT_STATUS_MASK) {
1149 case NVME_SANITIZE_SSTAT_STATUS_COMPLETE_SUCCESS:
1150 log->sanitize_status = BD_NVME_SANITIZE_STATUS_SUCCESS;
1151 break;
1152 case NVME_SANITIZE_SSTAT_STATUS_IN_PROGESS:
1153 log->sanitize_status = BD_NVME_SANITIZE_STATUS_IN_PROGRESS;
1154 break;
1155 case NVME_SANITIZE_SSTAT_STATUS_COMPLETED_FAILED:
1156 log->sanitize_status = BD_NVME_SANITIZE_STATUS_FAILED;
1157 break;
1158 case NVME_SANITIZE_SSTAT_STATUS_ND_COMPLETE_SUCCESS:
1159 log->sanitize_status = BD_NVME_SANITIZE_STATUS_SUCCESS_NO_DEALLOC;
1160 break;
1161 case NVME_SANITIZE_SSTAT_STATUS_NEVER_SANITIZED:
1162 default:
1163 log->sanitize_status = BD_NVME_SANITIZE_STATUS_NEVER_SANITIZED;
1164 break;
1165 }
1166
1167 log->time_for_overwrite = (GUINT32_FROM_LE (sanitize_log->eto) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->eto);
1168 log->time_for_block_erase = (GUINT32_FROM_LE (sanitize_log->etbe) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->etbe);
1169 log->time_for_crypto_erase = (GUINT32_FROM_LE (sanitize_log->etce) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->etce);
1170 log->time_for_overwrite_nd = (GUINT32_FROM_LE (sanitize_log->etond) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->etond);
1171 log->time_for_block_erase_nd = (GUINT32_FROM_LE (sanitize_log->etbend) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->etbend);
1172 log->time_for_crypto_erase_nd = (GUINT32_FROM_LE (sanitize_log->etcend) == 0xffffffff) ? -1 : (gint64) GUINT32_FROM_LE (sanitize_log->etcend);
1173
1174 free (sanitize_log);
1175 return log;
1176 }