]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - nvmeprint.cpp
Closes #831504
[mirror_smartmontools-debian.git] / nvmeprint.cpp
1 /*
2 * nvmeprint.cpp
3 *
4 * Home page of code is: http://www.smartmontools.org
5 *
6 * Copyright (C) 2016 Christian Franke
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 #include "config.h"
19 #include "nvmeprint.h"
20
21 const char * nvmeprint_cvsid = "$Id: nvmeprint.cpp 4311 2016-04-27 21:03:01Z chrfranke $"
22 NVMEPRINT_H_CVSID;
23
24 #include "int64.h"
25 #include "utility.h"
26 #include "dev_interface.h"
27 #include "nvmecmds.h"
28 #include "atacmds.h" // dont_print_serial_number
29 #include "scsicmds.h" // dStrHex()
30 #include "smartctl.h"
31
32 using namespace smartmontools;
33
34 // Return true if 128 bit LE integer is != 0.
35 static bool le128_is_non_zero(const unsigned char (& val)[16])
36 {
37 for (int i = 0; i < 16; i++) {
38 if (val[i])
39 return true;
40 }
41 return false;
42 }
43
44 // Format 128 bit integer for printing.
45 // Add value with SI prefixes if BYTES_PER_UNIT is specified.
46 static const char * le128_to_str(char (& str)[64], uint64_t hi, uint64_t lo, unsigned bytes_per_unit)
47 {
48 if (!hi) {
49 // Up to 64-bit, print exact value
50 format_with_thousands_sep(str, sizeof(str)-16, lo);
51
52 if (lo && bytes_per_unit && lo < 0xffffffffffffffffULL / bytes_per_unit) {
53 int i = strlen(str);
54 str[i++] = ' '; str[i++] = '[';
55 format_capacity(str+i, (int)sizeof(str)-i-1, lo * bytes_per_unit);
56 i = strlen(str);
57 str[i++] = ']'; str[i] = 0;
58 }
59 }
60 else {
61 // More than 64-bit, print approximate value, prepend ~ flag
62 snprintf(str, sizeof(str), "~%.0f",
63 hi * (0xffffffffffffffffULL + 1.0) + lo);
64 }
65
66 return str;
67 }
68
69 // Format 128 bit LE integer for printing.
70 // Add value with SI prefixes if BYTES_PER_UNIT is specified.
71 static const char * le128_to_str(char (& str)[64], const unsigned char (& val)[16],
72 unsigned bytes_per_unit = 0)
73 {
74 uint64_t hi = val[15];
75 for (int i = 15-1; i >= 8; i--) {
76 hi <<= 8; hi += val[i];
77 }
78 uint64_t lo = val[7];
79 for (int i = 7-1; i >= 0; i--) {
80 lo <<= 8; lo += val[i];
81 }
82 return le128_to_str(str, hi, lo, bytes_per_unit);
83 }
84
85 // Format capacity specified as 64bit LBA count for printing.
86 static const char * lbacap_to_str(char (& str)[64], uint64_t lba_cnt, int lba_bits)
87 {
88 return le128_to_str(str, (lba_cnt >> (64 - lba_bits)), (lba_cnt << lba_bits), 1);
89 }
90
91 // Format a Kelvin temperature value in Celsius.
92 static const char * kelvin_to_str(char (& str)[64], int k)
93 {
94 if (!k) // unsupported?
95 str[0] = '-', str[1] = 0;
96 else
97 snprintf(str, sizeof(str), "%d Celsius", k - 273);
98 return str;
99 }
100
101 static inline unsigned le16_to_uint(const unsigned char (& val)[2])
102 {
103 return ((val[1] << 8) | val[0]);
104 }
105
106 static void print_drive_info(const nvme_id_ctrl & id_ctrl, const nvme_id_ns & id_ns,
107 unsigned nsid, bool show_all)
108 {
109 char buf[64];
110 pout("Model Number: %s\n", format_char_array(buf, id_ctrl.mn));
111 if (!dont_print_serial_number)
112 pout("Serial Number: %s\n", format_char_array(buf, id_ctrl.sn));
113 pout("Firmware Version: %s\n", format_char_array(buf, id_ctrl.fr));
114
115 // Vendor and Subsystem IDs are usually equal
116 if (show_all || id_ctrl.vid != id_ctrl.ssvid) {
117 pout("PCI Vendor ID: 0x%04x\n", id_ctrl.vid);
118 pout("PCI Vendor Subsystem ID: 0x%04x\n", id_ctrl.ssvid);
119 }
120 else {
121 pout("PCI Vendor/Subsystem ID: 0x%04x\n", id_ctrl.vid);
122 }
123
124 pout("IEEE OUI Identifier: 0x%02x%02x%02x\n",
125 id_ctrl.ieee[2], id_ctrl.ieee[1], id_ctrl.ieee[0]);
126
127 // Capacity info is optional for devices without namespace management
128 if (show_all || le128_is_non_zero(id_ctrl.tnvmcap) || le128_is_non_zero(id_ctrl.unvmcap)) {
129 pout("Total NVM Capacity: %s\n", le128_to_str(buf, id_ctrl.tnvmcap, 1));
130 pout("Unallocated NVM Capacity: %s\n", le128_to_str(buf, id_ctrl.unvmcap, 1));
131 }
132
133 pout("Controller ID: %d\n", id_ctrl.cntlid);
134
135 // Print namespace info if available
136 pout("Number of Namespaces: %u\n", id_ctrl.nn);
137
138 if (nsid && id_ns.nsze) {
139 const char * align = &(" "[nsid < 10 ? 0 : (nsid < 100 ? 1 : 2)]);
140 int fmt_lba_bits = id_ns.lbaf[id_ns.flbas & 0xf].ds;
141
142 // Size and Capacity are equal if thin provisioning is not supported
143 if (show_all || id_ns.ncap != id_ns.nsze || (id_ns.nsfeat & 0x01)) {
144 pout("Namespace %u Size: %s%s\n", nsid, align,
145 lbacap_to_str(buf, id_ns.nsze, fmt_lba_bits));
146 pout("Namespace %u Capacity: %s%s\n", nsid, align,
147 lbacap_to_str(buf, id_ns.ncap, fmt_lba_bits));
148 }
149 else {
150 pout("Namespace %u Size/Capacity: %s%s\n", nsid, align,
151 lbacap_to_str(buf, id_ns.nsze, fmt_lba_bits));
152 }
153 // Utilization may be always equal to Capacity if thin provisioning is not supported
154 if (show_all || id_ns.nuse != id_ns.ncap || (id_ns.nsfeat & 0x01))
155 pout("Namespace %u Utilization: %s%s\n", nsid, align,
156 lbacap_to_str(buf, id_ns.nuse, fmt_lba_bits));
157
158 pout("Namespace %u Formatted LBA Size: %s%u\n", nsid, align, (1U << fmt_lba_bits));
159 }
160
161 char td[DATEANDEPOCHLEN]; dateandtimezone(td);
162 pout("Local Time is: %s\n", td);
163 }
164
165 // Format scaled power value.
166 static const char * format_power(char (& str)[16], unsigned power, unsigned scale)
167 {
168 switch (scale & 0x3) {
169 case 0: // not reported
170 str[0] = '-'; str[1] = ' '; str[2] = 0; break;
171 case 1: // 0.0001W
172 snprintf(str, sizeof(str), "%u.%04uW", power / 10000, power % 10000); break;
173 case 2: // 0.01W
174 snprintf(str, sizeof(str), "%u.%02uW", power / 100, power % 100); break;
175 default: // reserved
176 str[0] = '?'; str[1] = 0; break;
177 }
178 return str;
179 }
180
181 static void print_drive_capabilities(const nvme_id_ctrl & id_ctrl, const nvme_id_ns & id_ns,
182 unsigned nsid, bool show_all)
183 {
184 pout("Firmware Updates (0x%02x): %d Slot%s%s%s\n", id_ctrl.frmw,
185 ((id_ctrl.frmw >> 1) & 0x7), (((id_ctrl.frmw >> 1) & 0x7) != 1 ? "s" : ""),
186 ((id_ctrl.frmw & 0x01) ? ", Slot 1 R/O" : ""),
187 ((id_ctrl.frmw & 0x10) ? ", no Reset required" : ""));
188
189 if (show_all || id_ctrl.oacs)
190 pout("Optional Admin Commands (0x%04x): %s%s%s%s%s%s\n", id_ctrl.oacs,
191 (!id_ctrl.oacs ? " -" : ""),
192 ((id_ctrl.oacs & 0x0001) ? " Security" : ""),
193 ((id_ctrl.oacs & 0x0002) ? " Format" : ""),
194 ((id_ctrl.oacs & 0x0004) ? " Frmw_DL" : ""),
195 ((id_ctrl.oacs & 0x0008) ? " NS_Mngmt" : ""),
196 ((id_ctrl.oacs & ~0x000f) ? " *Other*" : ""));
197
198 if (show_all || id_ctrl.oncs)
199 pout("Optional NVM Commands (0x%04x): %s%s%s%s%s%s%s%s\n", id_ctrl.oncs,
200 (!id_ctrl.oncs ? " -" : ""),
201 ((id_ctrl.oncs & 0x0001) ? " Comp" : ""),
202 ((id_ctrl.oncs & 0x0002) ? " Wr_Unc" : ""),
203 ((id_ctrl.oncs & 0x0004) ? " DS_Mngmt" : ""),
204 ((id_ctrl.oncs & 0x0008) ? " Wr_Zero" : ""),
205 ((id_ctrl.oncs & 0x0010) ? " Sav/Sel_Feat" : ""),
206 ((id_ctrl.oncs & 0x0020) ? " Resv" : ""),
207 ((id_ctrl.oncs & ~0x003f) ? " *Other*" : ""));
208
209 if (id_ctrl.mdts)
210 pout("Maximum Data Transfer Size: %u Pages\n", (1U << id_ctrl.mdts));
211 else if (show_all)
212 pout("Maximum Data Transfer Size: -\n");
213
214 // Temperature thresholds are optional
215 char buf[64];
216 if (show_all || id_ctrl.wctemp)
217 pout("Warning Comp. Temp. Threshold: %s\n", kelvin_to_str(buf, id_ctrl.wctemp));
218 if (show_all || id_ctrl.cctemp)
219 pout("Critical Comp. Temp. Threshold: %s\n", kelvin_to_str(buf, id_ctrl.cctemp));
220
221 if (nsid && (show_all || id_ns.nsfeat)) {
222 const char * align = &(" "[nsid < 10 ? 0 : (nsid < 100 ? 1 : 2)]);
223 pout("Namespace %u Features (0x%02x): %s%s%s%s%s%s\n", nsid, id_ns.nsfeat, align,
224 (!id_ns.nsfeat ? " -" : ""),
225 ((id_ns.nsfeat & 0x01) ? " Thin_Prov" : ""),
226 ((id_ns.nsfeat & 0x02) ? " NA_Fields" : ""),
227 ((id_ns.nsfeat & 0x04) ? " Dea/Unw_Error" : ""),
228 ((id_ns.nsfeat & ~0x07) ? " *Other*" : ""));
229 }
230
231 // Print Power States
232 pout("\nSupported Power States\n");
233 pout("St Op Max Active Idle RL RT WL WT Ent_Lat Ex_Lat\n");
234 for (int i = 0; i <= id_ctrl.npss /* 1-based */ && i < 32; i++) {
235 char p1[16], p2[16], p3[16];
236 const nvme_id_power_state & ps = id_ctrl.psd[i];
237 pout("%2d %c %9s %8s %8s %3d %2d %2d %2d %8u %7u\n", i,
238 ((ps.flags & 0x02) ? '-' : '+'),
239 format_power(p1, ps.max_power, ((ps.flags & 0x01) ? 1 : 2)),
240 format_power(p2, ps.active_power, ps.active_work_scale),
241 format_power(p3, ps.idle_power, ps.idle_scale),
242 ps.read_lat & 0x1f, ps.read_tput & 0x1f,
243 ps.write_lat & 0x1f, ps.write_tput & 0x1f,
244 ps.entry_lat, ps.exit_lat);
245 }
246
247 // Print LBA sizes
248 if (nsid && id_ns.lbaf[0].ds) {
249 pout("\nSupported LBA Sizes (NSID 0x%x)\n", nsid);
250 pout("Id Fmt Data Metadt Rel_Perf\n");
251 for (int i = 0; i <= id_ns.nlbaf /* 1-based */ && i < 16; i++) {
252 const nvme_lbaf & lba = id_ns.lbaf[i];
253 pout("%2d %c %7u %7d %9d\n", i, (i == id_ns.flbas ? '+' : '-'),
254 (1U << lba.ds), lba.ms, lba.rp);
255 }
256 }
257 }
258
259 static void print_critical_warning(unsigned char w)
260 {
261 pout("SMART overall-health self-assessment test result: %s\n",
262 (!w ? "PASSED" : "FAILED!"));
263
264 if (w) {
265 if (w & 0x01)
266 pout("- available spare has fallen below threshold\n");
267 if (w & 0x02)
268 pout("- temperature is above or below threshold\n");
269 if (w & 0x04)
270 pout("- NVM subsystem reliability has been degraded\n");
271 if (w & 0x08)
272 pout("- media has been placed in read only mode\n");
273 if (w & 0x10)
274 pout("- volatile memory backup device has failed\n");
275 if (w & ~0x1f)
276 pout("- unknown critical warning(s) (0x%02x)\n", w & ~0x1f);
277 }
278
279 pout("\n");
280 }
281
282 static void print_smart_log(const nvme_smart_log & smart_log, unsigned nsid,
283 const nvme_id_ctrl & id_ctrl, bool show_all)
284 {
285 char buf[64];
286 pout("SMART/Health Information (NVMe Log 0x02, NSID 0x%x)\n", nsid);
287 pout("Critical Warning: 0x%02x\n", smart_log.critical_warning);
288 pout("Temperature: %s\n",
289 kelvin_to_str(buf, le16_to_uint(smart_log.temperature)));
290 pout("Available Spare: %u%%\n", smart_log.avail_spare);
291 pout("Available Spare Threshold: %u%%\n", smart_log.spare_thresh);
292 pout("Percentage Used: %u%%\n", smart_log.percent_used);
293 pout("Data Units Read: %s\n", le128_to_str(buf, smart_log.data_units_read, 1000*512));
294 pout("Data Units Written: %s\n", le128_to_str(buf, smart_log.data_units_written, 1000*512));
295 pout("Host Read Commands: %s\n", le128_to_str(buf, smart_log.host_reads));
296 pout("Host Write Commands: %s\n", le128_to_str(buf, smart_log.host_writes));
297 pout("Controller Busy Time: %s\n", le128_to_str(buf, smart_log.ctrl_busy_time));
298 pout("Power Cycles: %s\n", le128_to_str(buf, smart_log.power_cycles));
299 pout("Power On Hours: %s\n", le128_to_str(buf, smart_log.power_on_hours));
300 pout("Unsafe Shutdowns: %s\n", le128_to_str(buf, smart_log.unsafe_shutdowns));
301 pout("Media and Data Integrity Errors: %s\n", le128_to_str(buf, smart_log.media_errors));
302 pout("Error Information Log Entries: %s\n", le128_to_str(buf, smart_log.num_err_log_entries));
303
304 // Temperature thresholds are optional
305 if (show_all || id_ctrl.wctemp || smart_log.warning_temp_time)
306 pout("Warning Comp. Temperature Time: %d\n", smart_log.warning_temp_time);
307 if (show_all || id_ctrl.cctemp || smart_log.critical_comp_time)
308 pout("Critical Comp. Temperature Time: %d\n", smart_log.critical_comp_time);
309
310 // Temperature sensors are optional
311 for (int i = 0; i < 8; i++) {
312 if (show_all || smart_log.temp_sensor[i])
313 pout("Temperature Sensor %d: %s\n", i + 1,
314 kelvin_to_str(buf, smart_log.temp_sensor[i]));
315 }
316 pout("\n");
317 }
318
319 static void print_error_log(const nvme_error_log_page * error_log,
320 unsigned num_entries, unsigned print_entries)
321 {
322 pout("Error Information (NVMe Log 0x01, max %u entries)\n", num_entries);
323
324 unsigned cnt = 0;
325 for (unsigned i = 0; i < num_entries; i++) {
326 const nvme_error_log_page & e = error_log[i];
327 if (!e.error_count)
328 continue; // unused or invalid entry
329 if (++cnt > print_entries)
330 continue;
331
332 if (cnt == 1)
333 pout("Num ErrCount SQId CmdId Status PELoc LBA NSID VS\n");
334
335 char sq[16] = "-", cm[16] = "-", st[16] = "-", pe[16] = "-";
336 char lb[32] = "-", ns[16] = "-", vs[8] = "-";
337 if (e.sqid != 0xffff)
338 snprintf(sq, sizeof(sq), "%d", e.sqid);
339 if (e.cmdid != 0xffff)
340 snprintf(cm, sizeof(cm), "0x%04x", e.cmdid);
341 if (e.status_field != 0xffff)
342 snprintf(st, sizeof(st), "0x%04x", e.status_field);
343 if (e.parm_error_location != 0xffff)
344 snprintf(pe, sizeof(pe), "0x%03x", e.parm_error_location);
345 if (e.lba != 0xffffffffffffffffULL)
346 snprintf(lb, sizeof(lb), "%" PRIu64, e.lba);
347 if (e.nsid != 0xffffffffU)
348 snprintf(ns, sizeof(ns), "%u", e.nsid);
349 if (e.vs != 0x00)
350 snprintf(vs, sizeof(vs), "0x%02x", e.vs);
351
352 pout("%3u %10" PRIu64 " %5s %7s %7s %6s %12s %5s %5s\n",
353 i, e.error_count, sq, cm, st, pe, lb, ns, vs);
354 }
355
356 if (!cnt)
357 pout("No Errors Logged\n");
358 else if (cnt > print_entries)
359 pout("... (%u entries not shown)\n", cnt - print_entries);
360 pout("\n");
361 }
362
363 int nvmePrintMain(nvme_device * device, const nvme_print_options & options)
364 {
365 if (!( options.drive_info || options.drive_capabilities
366 || options.smart_check_status || options.smart_vendor_attrib
367 || options.error_log_entries || options.log_page_size )) {
368 pout("NVMe device successfully opened\n\n"
369 "Use 'smartctl -a' (or '-x') to print SMART (and more) information\n\n");
370 return 0;
371 }
372
373 // Show unset optional values only if debugging is enabled
374 bool show_all = (nvme_debugmode > 0);
375
376 // Read Identify Controller always
377 nvme_id_ctrl id_ctrl;
378 if (!nvme_read_id_ctrl(device, id_ctrl)) {
379 pout("Read NVMe Identify Controller failed: %s\n", device->get_errmsg());
380 return FAILID;
381 }
382
383 // Print Identify Controller/Namespace info
384 if (options.drive_info || options.drive_capabilities) {
385 pout("=== START OF INFORMATION SECTION ===\n");
386 nvme_id_ns id_ns; memset(&id_ns, 0, sizeof(id_ns));
387
388 unsigned nsid = device->get_nsid();
389 if (nsid == 0xffffffffU) {
390 // Broadcast namespace
391 if (id_ctrl.nn == 1) {
392 // No namespace management, get size from single namespace
393 nsid = 1;
394 if (!nvme_read_id_ns(device, nsid, id_ns))
395 nsid = 0;
396 }
397 }
398 else {
399 // Identify current namespace
400 if (!nvme_read_id_ns(device, nsid, id_ns)) {
401 pout("Read NVMe Identify Namespace 0x%x failed: %s\n", nsid, device->get_errmsg());
402 return FAILID;
403 }
404 }
405
406 if (options.drive_info)
407 print_drive_info(id_ctrl, id_ns, nsid, show_all);
408 if (options.drive_capabilities)
409 print_drive_capabilities(id_ctrl, id_ns, nsid, show_all);
410 pout("\n");
411 }
412
413 if ( options.smart_check_status || options.smart_vendor_attrib
414 || options.error_log_entries)
415 pout("=== START OF SMART DATA SECTION ===\n");
416
417 // Print SMART Status and SMART/Health Information
418 int retval = 0;
419 if (options.smart_check_status || options.smart_vendor_attrib) {
420 nvme_smart_log smart_log;
421 if (!nvme_read_smart_log(device, smart_log)) {
422 pout("Read NVMe SMART/Health Information failed: %s\n\n", device->get_errmsg());
423 return FAILSMART;
424 }
425
426 if (options.smart_check_status) {
427 print_critical_warning(smart_log.critical_warning);
428 if (smart_log.critical_warning)
429 retval |= FAILSTATUS;
430 }
431
432 if (options.smart_vendor_attrib) {
433 print_smart_log(smart_log, device->get_nsid(), id_ctrl, show_all);
434 }
435 }
436
437 // Print Error Information Log
438 if (options.error_log_entries) {
439 unsigned num_entries = id_ctrl.elpe + 1; // 0-based value
440 raw_buffer error_log_buf(num_entries * sizeof(nvme_error_log_page));
441 nvme_error_log_page * error_log =
442 reinterpret_cast<nvme_error_log_page *>(error_log_buf.data());
443
444 if (!nvme_read_error_log(device, error_log, num_entries)) {
445 pout("Read Error Information Log failed: %s\n\n", device->get_errmsg());
446 return retval | FAILSMART;
447 }
448
449 print_error_log(error_log, num_entries, options.error_log_entries);
450 }
451
452 // Dump log page
453 if (options.log_page_size) {
454 // Align size to dword boundary
455 unsigned size = ((options.log_page_size + 4-1) / 4) * 4;
456 raw_buffer log_buf(size);
457
458 if (!nvme_read_log_page(device, options.log_page, log_buf.data(), size)) {
459 pout("Read NVMe Log 0x%02x failed: %s\n\n", options.log_page, device->get_errmsg());
460 return retval | FAILSMART;
461 }
462
463 pout("NVMe Log 0x%02x (0x%04x bytes)\n", options.log_page, size);
464 dStrHex(log_buf.data(), size, 0);
465 pout("\n");
466 }
467
468 return retval;
469 }