Home | History | Annotate | Line # | Download | only in nvmectl
      1 /*	$NetBSD: logpage.c,v 1.11 2023/02/02 08:21:32 mlelstv Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2013 EMC Corp.
      7  * All rights reserved.
      8  *
      9  * Copyright (C) 2012-2013 Intel Corporation
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: logpage.c,v 1.11 2023/02/02 08:21:32 mlelstv Exp $");
     37 #if 0
     38 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 329824 2018-02-22 13:32:31Z wma $");
     39 #endif
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/ioccom.h>
     44 #include <sys/endian.h>
     45 
     46 #include <ctype.h>
     47 #include <err.h>
     48 #include <fcntl.h>
     49 #include <stdbool.h>
     50 #include <stddef.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "nvmectl.h"
     57 
     58 #define DEFAULT_SIZE	(4096)
     59 #define MAX_FW_SLOTS	(7)
     60 
     61 typedef void (*print_fn_t)(const struct nvm_identify_controller *cdata, void *buf,
     62     uint32_t size);
     63 
     64 struct kv_name {
     65 	uint32_t key;
     66 	const char *name;
     67 };
     68 
     69 static const char *
     70 kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key)
     71 {
     72 	static char bad[32];
     73 	size_t i;
     74 
     75 	for (i = 0; i < kv_count; i++, kv++)
     76 		if (kv->key == key)
     77 			return kv->name;
     78 	snprintf(bad, sizeof(bad), "Attribute %#x", key);
     79 	return bad;
     80 }
     81 
     82 static void
     83 print_log_hex(const struct nvm_identify_controller *cdata __unused, void *data,
     84     uint32_t length)
     85 {
     86 	print_hex(data, length);
     87 }
     88 
     89 static void
     90 print_bin(const struct nvm_identify_controller *cdata __unused, void *data,
     91     uint32_t length)
     92 {
     93 	write(STDOUT_FILENO, data, length);
     94 }
     95 
     96 static void *
     97 get_log_buffer(uint32_t size)
     98 {
     99 	void	*buf;
    100 
    101 	if ((buf = malloc(size)) == NULL)
    102 		errx(1, "unable to malloc %u bytes", size);
    103 
    104 	memset(buf, 0, size);
    105 	return (buf);
    106 }
    107 
    108 void
    109 read_logpage(int fd, uint8_t log_page, int nsid, void *payload,
    110     uint32_t payload_size)
    111 {
    112 	struct nvme_pt_command	pt;
    113 
    114 	memset(&pt, 0, sizeof(pt));
    115 	pt.cmd.opcode = NVM_ADMIN_GET_LOG_PG;
    116 	pt.cmd.nsid = nsid;
    117 	pt.cmd.cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16;
    118 	pt.cmd.cdw10 |= log_page;
    119 	pt.buf = payload;
    120 	pt.len = payload_size;
    121 	pt.is_read = 1;
    122 
    123 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
    124 		err(1, "get log page request failed");
    125 
    126 	if (nvme_completion_is_error(&pt.cpl))
    127 		errx(1, "get log page request returned error 0x%x/0x%02x",
    128 			(uint8_t)__SHIFTOUT(pt.cpl.flags, NVME_CQE_SCT_MASK),
    129 			(uint8_t)__SHIFTOUT(pt.cpl.flags, NVME_CQE_SC_MASK));
    130 }
    131 
    132 static void
    133 nvme_error_information_entry_swapbytes(struct nvme_error_information_entry *e)
    134 {
    135 #if _BYTE_ORDER != _LITTLE_ENDIAN
    136 	e->error_count = le64toh(e->error_count);
    137 	e->sqid = le16toh(e->sqid);
    138 	e->cid = le16toh(e->cid);
    139 	e->status = le16toh(e->status);
    140 	e->error_location = le16toh(e->error_location);
    141 	e->lba = le64toh(e->lba);
    142 	e->nsid = le32toh(e->nsid);
    143 	e->command_specific = le64toh(e->command_specific);
    144 #endif
    145 }
    146 
    147 static void
    148 print_log_error(const struct nvm_identify_controller *cdata __unused, void *buf,
    149     uint32_t size)
    150 {
    151 	int					i, nentries;
    152 	struct nvme_error_information_entry	*entry = buf;
    153 
    154 	/* Convert data to host endian */
    155 	nvme_error_information_entry_swapbytes(entry);
    156 
    157 	printf("Error Information Log\n");
    158 	printf("=====================\n");
    159 
    160 	if (entry->error_count == 0) {
    161 		printf("No error entries found\n");
    162 		return;
    163 	}
    164 
    165 	nentries = size/sizeof(struct nvme_error_information_entry);
    166 	for (i = 0; i < nentries; i++, entry++) {
    167 		if (entry->error_count == 0)
    168 			break;
    169 
    170 		printf("Entry %02d\n", i + 1);
    171 		printf("=========\n");
    172 		printf(" Error count:           %ju\n", entry->error_count);
    173 		printf(" Submission queue ID:   %u\n", entry->sqid);
    174 		printf(" Command ID:            %u\n", entry->cid);
    175 		/* TODO: Export nvme_status_string structures from kernel? */
    176 		printf(" Status:\n");
    177 		printf("  Phase tag:            %d\n",
    178 		    (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_PHASE));
    179 		printf("  Status code:          %d\n",
    180 		    (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_SC_MASK));
    181 		printf("  Status code type:     %d\n",
    182 		    (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_SCT_MASK));
    183 		printf("  More:                 %d\n",
    184 		    (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_M));
    185 		printf("  DNR:                  %d\n",
    186 		    (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_DNR));
    187 		printf(" Error location:        %u\n", entry->error_location);
    188 		printf(" LBA:                   %ju\n", entry->lba);
    189 		printf(" Namespace ID:          %u\n", entry->nsid);
    190 		printf(" Vendor specific info:  %u\n", entry->vendor_specific);
    191 		printf(" Command specific info: %ju\n",
    192 		    entry->command_specific);
    193 	}
    194 }
    195 
    196 static void
    197 print_temp(uint16_t t)
    198 {
    199 	printf("%u K, %2.2f C, %3.2f F\n", t, (float)t - 273.15,
    200 	    (float)t * 9 / 5 - 459.67);
    201 }
    202 
    203 static void
    204 nvme_health_information_page_swapbytes(struct nvme_health_information_page *e)
    205 {
    206 #if _BYTE_ORDER != _LITTLE_ENDIAN
    207 	u_int i;
    208 
    209 	e->composite_temperature = le16toh(e->composite_temperature);
    210 	nvme_le128toh(e->data_units_read);
    211 	nvme_le128toh(e->data_units_written);
    212 	nvme_le128toh(e->host_read_commands);
    213 	nvme_le128toh(e->host_write_commands);
    214 	nvme_le128toh(e->controller_busy_time);
    215 	nvme_le128toh(e->power_cycles);
    216 	nvme_le128toh(e->power_on_hours);
    217 	nvme_le128toh(e->unsafe_shutdowns);
    218 	nvme_le128toh(e->media_errors);
    219 	nvme_le128toh(e->num_error_info_log_entries);
    220 	e->warning_temp_time = le32toh(e->warning_temp_time);
    221 	e->error_temp_time = le32toh(e->error_temp_time);
    222 	for (i = 0; i < __arraycount(e->temp_sensor); i++)
    223 		e->temp_sensor[i] = le16toh(e->temp_sensor[i]);
    224 #endif
    225 }
    226 
    227 static void
    228 print_log_health(const struct nvm_identify_controller *cdata __unused, void *buf,
    229     uint32_t size __unused)
    230 {
    231 	struct nvme_health_information_page *health = buf;
    232 	u_int i;
    233 
    234 	/* Convert data to host endian */
    235 	nvme_health_information_page_swapbytes(health);
    236 
    237 	printf("SMART/Health Information Log\n");
    238 	printf("============================\n");
    239 
    240 	printf("Critical Warning State:         0x%02x\n",
    241 	    health->critical_warning);
    242 	printf(" Available spare:               %d\n",
    243 	    (uint8_t)__SHIFTOUT(health->critical_warning,
    244 	      NVME_HEALTH_PAGE_CW_AVAIL_SPARE));
    245 	printf(" Temperature:                   %d\n",
    246 	    (uint8_t)__SHIFTOUT(health->critical_warning,
    247 	      NVME_HEALTH_PAGE_CW_TEMPERTURE));
    248 	printf(" Device reliability:            %d\n",
    249 	    (uint8_t)__SHIFTOUT(health->critical_warning,
    250 	      NVME_HEALTH_PAGE_CW_DEVICE_RELIABLITY));
    251 	printf(" Read only:                     %d\n",
    252 	    (uint8_t)__SHIFTOUT(health->critical_warning,
    253 	      NVME_HEALTH_PAGE_CW_READ_ONLY));
    254 	printf(" Volatile memory backup:        %d\n",
    255 	    (uint8_t)__SHIFTOUT(health->critical_warning,
    256 	      NVME_HEALTH_PAGE_CW_VOLATILE_MEMORY_BACKUP));
    257 	printf("Temperature:                    ");
    258 	print_temp(health->composite_temperature);
    259 	printf("Available spare:                %u\n",
    260 	    health->available_spare);
    261 	printf("Available spare threshold:      %u\n",
    262 	    health->available_spare_threshold);
    263 	printf("Percentage used:                %u\n",
    264 	    health->percentage_used);
    265 
    266 	print_bignum1("Data units read:", health->data_units_read, "", "B", 512000);
    267 	print_bignum1("Data units written:", health->data_units_written,
    268 	    "", "B", 512000);
    269 	print_bignum("Host read commands:", health->host_read_commands, "");
    270 	print_bignum("Host write commands:", health->host_write_commands, "");
    271 	print_bignum("Controller busy time (minutes):", health->controller_busy_time,
    272 	    "");
    273 	print_bignum("Power cycles:", health->power_cycles, "");
    274 	print_bignum("Power on hours:", health->power_on_hours, "");
    275 	print_bignum("Unsafe shutdowns:", health->unsafe_shutdowns, "");
    276 	print_bignum("Media errors:", health->media_errors, "");
    277 	print_bignum("No. error info log entries:",
    278 	    health->num_error_info_log_entries, "");
    279 
    280 	printf("Warning Temp Composite Time:    %d\n", health->warning_temp_time);
    281 	printf("Error Temp Composite Time:      %d\n", health->error_temp_time);
    282 	for (i = 0; i < __arraycount(health->temp_sensor); i++) {
    283 		if (health->temp_sensor[i] == 0)
    284 			continue;
    285 		printf("Temperature Sensor %d:           ", i + 1);
    286 		print_temp(health->temp_sensor[i]);
    287 	}
    288 }
    289 
    290 static void
    291 nvme_firmware_page_swapbytes(struct nvme_firmware_page *e)
    292 {
    293 #if _BYTE_ORDER != _LITTLE_ENDIAN
    294 	u_int i;
    295 
    296 	for (i = 0; i < __arraycount(e->revision); i++)
    297 		e->revision[i] = le64toh(e->revision[i]);
    298 #endif
    299 }
    300 
    301 static void
    302 print_log_firmware(const struct nvm_identify_controller *cdata, void *buf,
    303     uint32_t size __unused)
    304 {
    305 	u_int				i, slots;
    306 	const char			*status;
    307 	struct nvme_firmware_page	*fw = buf;
    308 
    309 	/* Convert data to host endian */
    310 	nvme_firmware_page_swapbytes(fw);
    311 
    312 	printf("Firmware Slot Log\n");
    313 	printf("=================\n");
    314 
    315 	if (!(cdata->oacs & NVME_ID_CTRLR_OACS_FW))
    316 		slots = 1;
    317 	else
    318 		slots = MIN(__SHIFTOUT(cdata->frmw, NVME_ID_CTRLR_FRMW_NSLOT),
    319 		    MAX_FW_SLOTS);
    320 
    321 	for (i = 0; i < slots; i++) {
    322 		printf("Slot %d: ", i + 1);
    323 		if (__SHIFTOUT(fw->afi, NVME_FW_PAGE_AFI_SLOT) == i + 1)
    324 			status = "  Active";
    325 		else
    326 			status = "Inactive";
    327 
    328 		if (fw->revision[i] == 0LLU)
    329 			printf("Empty\n");
    330 		else
    331 			if (isprint(*(uint8_t *)&fw->revision[i]))
    332 				printf("[%s] %.8s\n", status,
    333 				    (char *)&fw->revision[i]);
    334 			else
    335 				printf("[%s] %016jx\n", status,
    336 				    fw->revision[i]);
    337 	}
    338 }
    339 
    340 /*
    341  * Intel specific log pages from
    342  * http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/ssd-dc-p3700-spec.pdf
    343  *
    344  * Though the version as of this date has a typo for the size of log page 0xca,
    345  * offset 147: it is only 1 byte, not 6.
    346  */
    347 static void
    348 intel_log_temp_stats_swapbytes(struct intel_log_temp_stats *e)
    349 {
    350 #if _BYTE_ORDER != _LITTLE_ENDIAN
    351 	e->current = le64toh(e->current);
    352 	e->overtemp_flag_last = le64toh(e->overtemp_flag_last);
    353 	e->overtemp_flag_life = le64toh(e->overtemp_flag_life);
    354 	e->max_temp = le64toh(e->max_temp);
    355 	e->min_temp = le64toh(e->min_temp);
    356 	e->max_oper_temp = le64toh(e->max_oper_temp);
    357 	e->min_oper_temp = le64toh(e->min_oper_temp);
    358 	e->est_offset = le64toh(e->est_offset);
    359 #endif
    360 }
    361 
    362 static void
    363 print_intel_temp_stats(const struct nvm_identify_controller *cdata __unused,
    364     void *buf, uint32_t size __unused)
    365 {
    366 	struct intel_log_temp_stats	*temp = buf;
    367 
    368 	/* Convert data to host endian */
    369 	intel_log_temp_stats_swapbytes(temp);
    370 
    371 	printf("Intel Temperature Log\n");
    372 	printf("=====================\n");
    373 
    374 	printf("Current:                        ");
    375 	print_temp(temp->current);
    376 	printf("Overtemp Last Flags             %#jx\n",
    377 	    (uintmax_t)temp->overtemp_flag_last);
    378 	printf("Overtemp Lifetime Flags         %#jx\n",
    379 	    (uintmax_t)temp->overtemp_flag_life);
    380 	printf("Max Temperature                 ");
    381 	print_temp(temp->max_temp);
    382 	printf("Min Temperature                 ");
    383 	print_temp(temp->min_temp);
    384 	printf("Max Operating Temperature       ");
    385 	print_temp(temp->max_oper_temp);
    386 	printf("Min Operating Temperature       ");
    387 	print_temp(temp->min_oper_temp);
    388 	printf("Estimated Temperature Offset:   %ju C/K\n",
    389 	    (uintmax_t)temp->est_offset);
    390 }
    391 
    392 /*
    393  * Format from Table 22, section 5.7 IO Command Latency Statistics.
    394  * Read and write stats pages have identical encoding.
    395  */
    396 static void
    397 print_intel_read_write_lat_log(const struct nvm_identify_controller *cdata __unused,
    398     void *buf, uint32_t size __unused)
    399 {
    400 	const char *walker = buf;
    401 	int i;
    402 
    403 	printf("Major:                         %d\n", le16dec(walker + 0));
    404 	printf("Minor:                         %d\n", le16dec(walker + 2));
    405 	for (i = 0; i < 32; i++)
    406 		printf("%4dus-%4dus:                 %ju\n", i * 32, (i + 1) * 32,
    407 		    (uintmax_t)le32dec(walker + 4 + i * 4));
    408 	for (i = 1; i < 32; i++)
    409 		printf("%4dms-%4dms:                 %ju\n", i, i + 1,
    410 		    (uintmax_t)le32dec(walker + 132 + i * 4));
    411 	for (i = 1; i < 32; i++)
    412 		printf("%4dms-%4dms:                 %ju\n", i * 32, (i + 1) * 32,
    413 		    (uintmax_t)le32dec(walker + 256 + i * 4));
    414 }
    415 
    416 static void
    417 print_intel_read_lat_log(const struct nvm_identify_controller *cdata, void *buf,
    418     uint32_t size)
    419 {
    420 
    421 	printf("Intel Read Latency Log\n");
    422 	printf("======================\n");
    423 	print_intel_read_write_lat_log(cdata, buf, size);
    424 }
    425 
    426 static void
    427 print_intel_write_lat_log(const struct nvm_identify_controller *cdata, void *buf,
    428     uint32_t size)
    429 {
    430 
    431 	printf("Intel Write Latency Log\n");
    432 	printf("=======================\n");
    433 	print_intel_read_write_lat_log(cdata, buf, size);
    434 }
    435 
    436 /*
    437  * Table 19. 5.4 SMART Attributes.
    438  * Samsung also implements this and some extra data not documented.
    439  */
    440 static void
    441 print_intel_add_smart(const struct nvm_identify_controller *cdata __unused,
    442     void *buf, uint32_t size __unused)
    443 {
    444 	uint8_t *walker = buf;
    445 	uint8_t *end = walker + 150;
    446 	const char *name;
    447 	uint64_t raw;
    448 	uint8_t normalized;
    449 
    450 	static struct kv_name kv[] = {
    451 		{ 0xab, "Program Fail Count" },
    452 		{ 0xac, "Erase Fail Count" },
    453 		{ 0xad, "Wear Leveling Count" },
    454 		{ 0xb8, "End to End Error Count" },
    455 		{ 0xc7, "CRC Error Count" },
    456 		{ 0xe2, "Timed: Media Wear" },
    457 		{ 0xe3, "Timed: Host Read %" },
    458 		{ 0xe4, "Timed: Elapsed Time" },
    459 		{ 0xea, "Thermal Throttle Status" },
    460 		{ 0xf0, "Retry Buffer Overflows" },
    461 		{ 0xf3, "PLL Lock Loss Count" },
    462 		{ 0xf4, "NAND Bytes Written" },
    463 		{ 0xf5, "Host Bytes Written" },
    464 	};
    465 
    466 	printf("Additional SMART Data Log\n");
    467 	printf("=========================\n");
    468 	/*
    469 	 * walker[0] = Key
    470 	 * walker[1,2] = reserved
    471 	 * walker[3] = Normalized Value
    472 	 * walker[4] = reserved
    473 	 * walker[5..10] = Little Endian Raw value
    474 	 *	(or other represenations)
    475 	 * walker[11] = reserved
    476 	 */
    477 	while (walker < end) {
    478 		name = kv_lookup(kv, __arraycount(kv), *walker);
    479 		normalized = walker[3];
    480 		raw = le48dec(walker + 5);
    481 		switch (*walker){
    482 		case 0:
    483 			break;
    484 		case 0xad:
    485 			printf("%-32s: %3d min: %u max: %u ave: %u\n", name,
    486 			    normalized, le16dec(walker + 5), le16dec(walker + 7),
    487 			    le16dec(walker + 9));
    488 			break;
    489 		case 0xe2:
    490 			printf("%-32s: %3d %.3f%%\n", name, normalized, raw / 1024.0);
    491 			break;
    492 		case 0xea:
    493 			printf("%-32s: %3d %d%% %d times\n", name, normalized,
    494 			    walker[5], le32dec(walker+6));
    495 			break;
    496 		default:
    497 			printf("%-32s: %3d %ju\n", name, normalized, (uintmax_t)raw);
    498 			break;
    499 		}
    500 		walker += 12;
    501 	}
    502 }
    503 
    504 /*
    505  * HGST's 0xc1 page. This is a grab bag of additional data. Please see
    506  * https://www.hgst.com/sites/default/files/resources/US_SN150_ProdManual.pdf
    507  * https://www.hgst.com/sites/default/files/resources/US_SN100_ProdManual.pdf
    508  * Appendix A for details
    509  */
    510 
    511 typedef void (*subprint_fn_t)(void *buf, uint16_t subtype, uint8_t res,
    512     uint32_t size);
    513 
    514 struct subpage_print {
    515 	uint16_t key;
    516 	subprint_fn_t fn;
    517 };
    518 
    519 static void print_hgst_info_write_errors(void *, uint16_t, uint8_t, uint32_t);
    520 static void print_hgst_info_read_errors(void *, uint16_t, uint8_t, uint32_t);
    521 static void print_hgst_info_verify_errors(void *, uint16_t, uint8_t, uint32_t);
    522 static void print_hgst_info_self_test(void *, uint16_t, uint8_t, uint32_t);
    523 static void print_hgst_info_background_scan(void *, uint16_t, uint8_t, uint32_t);
    524 static void print_hgst_info_erase_errors(void *, uint16_t, uint8_t, uint32_t);
    525 static void print_hgst_info_erase_counts(void *, uint16_t, uint8_t, uint32_t);
    526 static void print_hgst_info_temp_history(void *, uint16_t, uint8_t, uint32_t);
    527 static void print_hgst_info_ssd_perf(void *, uint16_t, uint8_t, uint32_t);
    528 static void print_hgst_info_firmware_load(void *, uint16_t, uint8_t, uint32_t);
    529 
    530 static struct subpage_print hgst_subpage[] = {
    531 	{ 0x02, print_hgst_info_write_errors },
    532 	{ 0x03, print_hgst_info_read_errors },
    533 	{ 0x05, print_hgst_info_verify_errors },
    534 	{ 0x10, print_hgst_info_self_test },
    535 	{ 0x15, print_hgst_info_background_scan },
    536 	{ 0x30, print_hgst_info_erase_errors },
    537 	{ 0x31, print_hgst_info_erase_counts },
    538 	{ 0x32, print_hgst_info_temp_history },
    539 	{ 0x37, print_hgst_info_ssd_perf },
    540 	{ 0x38, print_hgst_info_firmware_load },
    541 };
    542 
    543 /* Print a subpage that is basically just key value pairs */
    544 static void
    545 print_hgst_info_subpage_gen(void *buf, uint16_t subtype __unused, uint32_t size,
    546     const struct kv_name *kv, size_t kv_count)
    547 {
    548 	uint8_t *wsp, *esp;
    549 	uint16_t ptype;
    550 	uint8_t plen;
    551 	uint64_t param;
    552 	int i;
    553 
    554 	wsp = buf;
    555 	esp = wsp + size;
    556 	while (wsp < esp) {
    557 		ptype = le16dec(wsp);
    558 		wsp += 2;
    559 		wsp++;			/* Flags, just ignore */
    560 		plen = *wsp++;
    561 		param = 0;
    562 		for (i = 0; i < plen; i++)
    563 			param |= (uint64_t)*wsp++ << (i * 8);
    564 		printf("  %-30s: %jd\n", kv_lookup(kv, kv_count, ptype),
    565 		    (uintmax_t)param);
    566 	}
    567 }
    568 
    569 static void
    570 print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res __unused,
    571     uint32_t size)
    572 {
    573 	static const struct kv_name kv[] = {
    574 		{ 0x0000, "Corrected Without Delay" },
    575 		{ 0x0001, "Corrected Maybe Delayed" },
    576 		{ 0x0002, "Re-Writes" },
    577 		{ 0x0003, "Errors Corrected" },
    578 		{ 0x0004, "Correct Algorithm Used" },
    579 		{ 0x0005, "Bytes Processed" },
    580 		{ 0x0006, "Uncorrected Errors" },
    581 		{ 0x8000, "Flash Write Commands" },
    582 		{ 0x8001, "HGST Special" },
    583 	};
    584 
    585 	printf("Write Errors Subpage:\n");
    586 	print_hgst_info_subpage_gen(buf, subtype, size, kv, __arraycount(kv));
    587 }
    588 
    589 static void
    590 print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res __unused,
    591     uint32_t size)
    592 {
    593 	static const struct kv_name kv[] = {
    594 		{ 0x0000, "Corrected Without Delay" },
    595 		{ 0x0001, "Corrected Maybe Delayed" },
    596 		{ 0x0002, "Re-Reads" },
    597 		{ 0x0003, "Errors Corrected" },
    598 		{ 0x0004, "Correct Algorithm Used" },
    599 		{ 0x0005, "Bytes Processed" },
    600 		{ 0x0006, "Uncorrected Errors" },
    601 		{ 0x8000, "Flash Read Commands" },
    602 		{ 0x8001, "XOR Recovered" },
    603 		{ 0x8002, "Total Corrected Bits" },
    604 	};
    605 
    606 	printf("Read Errors Subpage:\n");
    607 	print_hgst_info_subpage_gen(buf, subtype, size, kv, __arraycount(kv));
    608 }
    609 
    610 static void
    611 print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res __unused,
    612     uint32_t size)
    613 {
    614 	static const struct kv_name kv[] = {
    615 		{ 0x0000, "Corrected Without Delay" },
    616 		{ 0x0001, "Corrected Maybe Delayed" },
    617 		{ 0x0002, "Re-Reads" },
    618 		{ 0x0003, "Errors Corrected" },
    619 		{ 0x0004, "Correct Algorithm Used" },
    620 		{ 0x0005, "Bytes Processed" },
    621 		{ 0x0006, "Uncorrected Errors" },
    622 		{ 0x8000, "Commands Processed" },
    623 	};
    624 
    625 	printf("Verify Errors Subpage:\n");
    626 	print_hgst_info_subpage_gen(buf, subtype, size, kv, __arraycount(kv));
    627 }
    628 
    629 static void
    630 print_hgst_info_self_test(void *buf, uint16_t subtype __unused, uint8_t res __unused,
    631     uint32_t size)
    632 {
    633 	size_t i;
    634 	uint8_t *walker = buf;
    635 	uint16_t code, hrs;
    636 	uint32_t lba;
    637 
    638 	printf("Self Test Subpage:\n");
    639 	for (i = 0; i < size / 20; i++) {	/* Each entry is 20 bytes */
    640 		code = le16dec(walker);
    641 		walker += 2;
    642 		walker++;			/* Ignore fixed flags */
    643 		if (*walker == 0)		/* Last entry is zero length */
    644 			break;
    645 		if (*walker++ != 0x10) {
    646 			printf("Bad length for self test report\n");
    647 			return;
    648 		}
    649 		printf("  %-30s: %d\n", "Recent Test", code);
    650 		printf("    %-28s: %#x\n", "Self-Test Results", *walker & 0xf);
    651 		printf("    %-28s: %#x\n", "Self-Test Code", (*walker >> 5) & 0x7);
    652 		walker++;
    653 		printf("    %-28s: %#x\n", "Self-Test Number", *walker++);
    654 		hrs = le16dec(walker);
    655 		walker += 2;
    656 		lba = le32dec(walker);
    657 		walker += 4;
    658 		printf("    %-28s: %u\n", "Total Power On Hrs", hrs);
    659 		printf("    %-28s: %#jx (%jd)\n", "LBA", (uintmax_t)lba,
    660 		    (uintmax_t)lba);
    661 		printf("    %-28s: %#x\n", "Sense Key", *walker++ & 0xf);
    662 		printf("    %-28s: %#x\n", "Additional Sense Code", *walker++);
    663 		printf("    %-28s: %#x\n", "Additional Sense Qualifier", *walker++);
    664 		printf("    %-28s: %#x\n", "Vendor Specific Detail", *walker++);
    665 	}
    666 }
    667 
    668 static void
    669 print_hgst_info_background_scan(void *buf, uint16_t subtype __unused,
    670     uint8_t res __unused, uint32_t size)
    671 {
    672 	uint8_t *walker = buf;
    673 	uint8_t status;
    674 	uint16_t code, nscan, progress;
    675 	uint32_t pom, nand;
    676 
    677 	printf("Background Media Scan Subpage:\n");
    678 	/* Decode the header */
    679 	code = le16dec(walker);
    680 	walker += 2;
    681 	walker++;			/* Ignore fixed flags */
    682 	if (*walker++ != 0x10) {
    683 		printf("Bad length for background scan header\n");
    684 		return;
    685 	}
    686 	if (code != 0) {
    687 		printf("Expected code 0, found code %#x\n", code);
    688 		return;
    689 	}
    690 	pom = le32dec(walker);
    691 	walker += 4;
    692 	walker++;			/* Reserved */
    693 	status = *walker++;
    694 	nscan = le16dec(walker);
    695 	walker += 2;
    696 	progress = le16dec(walker);
    697 	walker += 2;
    698 	walker += 6;			/* Reserved */
    699 	printf("  %-30s: %d\n", "Power On Minutes", pom);
    700 	printf("  %-30s: %x (%s)\n", "BMS Status", status,
    701 	    status == 0 ? "idle" : (status == 1 ? "active" :
    702 	      (status == 8 ? "suspended" : "unknown")));
    703 	printf("  %-30s: %d\n", "Number of BMS", nscan);
    704 	printf("  %-30s: %d\n", "Progress Current BMS", progress);
    705 	/* Report retirements */
    706 	if (walker - (uint8_t *)buf != 20) {
    707 		printf("Coding error, offset not 20\n");
    708 		return;
    709 	}
    710 	size -= 20;
    711 	printf("  %-30s: %d\n", "BMS retirements", size / 0x18);
    712 	while (size > 0) {
    713 		code = le16dec(walker);
    714 		walker += 2;
    715 		walker++;
    716 		if (*walker++ != 0x14) {
    717 			printf("Bad length parameter\n");
    718 			return;
    719 		}
    720 		pom = le32dec(walker);
    721 		walker += 4;
    722 		/*
    723 		 * Spec sheet says the following are hard coded, if true, just
    724 		 * print the NAND retirement.
    725 		 */
    726 		if (walker[0] == 0x41 &&
    727 		    walker[1] == 0x0b &&
    728 		    walker[2] == 0x01 &&
    729 		    walker[3] == 0x00 &&
    730 		    walker[4] == 0x00 &&
    731 		    walker[5] == 0x00 &&
    732 		    walker[6] == 0x00 &&
    733 		    walker[7] == 0x00) {
    734 			walker += 8;
    735 			walker += 4;	/* Skip reserved */
    736 			nand = le32dec(walker);
    737 			walker += 4;
    738 			printf("  %-30s: %d\n", "Retirement number", code);
    739 			printf("    %-28s: %#x\n", "NAND (C/T)BBBPPP", nand);
    740 		} else {
    741 			printf("Parameter %#x entry corrupt\n", code);
    742 			walker += 16;
    743 		}
    744 	}
    745 }
    746 
    747 static void
    748 print_hgst_info_erase_errors(void *buf, uint16_t subtype __unused,
    749     uint8_t res __unused, uint32_t size)
    750 {
    751 	static const struct kv_name kv[] = {
    752 		{ 0x0000, "Corrected Without Delay" },
    753 		{ 0x0001, "Corrected Maybe Delayed" },
    754 		{ 0x0002, "Re-Erase" },
    755 		{ 0x0003, "Errors Corrected" },
    756 		{ 0x0004, "Correct Algorithm Used" },
    757 		{ 0x0005, "Bytes Processed" },
    758 		{ 0x0006, "Uncorrected Errors" },
    759 		{ 0x8000, "Flash Erase Commands" },
    760 		{ 0x8001, "Mfg Defect Count" },
    761 		{ 0x8002, "Grown Defect Count" },
    762 		{ 0x8003, "Erase Count -- User" },
    763 		{ 0x8004, "Erase Count -- System" },
    764 	};
    765 
    766 	printf("Erase Errors Subpage:\n");
    767 	print_hgst_info_subpage_gen(buf, subtype, size, kv, __arraycount(kv));
    768 }
    769 
    770 static void
    771 print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res __unused,
    772     uint32_t size)
    773 {
    774 	/* My drive doesn't export this -- so not coding up */
    775 	printf("XXX: Erase counts subpage: %p, %#x %d\n", buf, subtype, size);
    776 }
    777 
    778 static void
    779 print_hgst_info_temp_history(void *buf, uint16_t subtype __unused,
    780     uint8_t res __unused, uint32_t size __unused)
    781 {
    782 	uint8_t *walker = buf;
    783 	uint32_t min;
    784 
    785 	printf("Temperature History:\n");
    786 	printf("  %-30s: %d C\n", "Current Temperature", *walker++);
    787 	printf("  %-30s: %d C\n", "Reference Temperature", *walker++);
    788 	printf("  %-30s: %d C\n", "Maximum Temperature", *walker++);
    789 	printf("  %-30s: %d C\n", "Minimum Temperature", *walker++);
    790 	min = le32dec(walker);
    791 	walker += 4;
    792 	printf("  %-30s: %d:%02d:00\n", "Max Temperature Time", min / 60, min % 60);
    793 	min = le32dec(walker);
    794 	walker += 4;
    795 	printf("  %-30s: %d:%02d:00\n", "Over Temperature Duration", min / 60,
    796 	    min % 60);
    797 	min = le32dec(walker);
    798 	walker += 4;
    799 	printf("  %-30s: %d:%02d:00\n", "Min Temperature Time", min / 60, min % 60);
    800 }
    801 
    802 static void
    803 print_hgst_info_ssd_perf(void *buf, uint16_t subtype __unused, uint8_t res,
    804     uint32_t size __unused)
    805 {
    806 	uint8_t *walker = buf;
    807 	uint64_t val;
    808 
    809 	printf("SSD Performance Subpage Type %d:\n", res);
    810 	val = le64dec(walker);
    811 	walker += 8;
    812 	printf("  %-30s: %ju\n", "Host Read Commands", val);
    813 	val = le64dec(walker);
    814 	walker += 8;
    815 	printf("  %-30s: %ju\n", "Host Read Blocks", val);
    816 	val = le64dec(walker);
    817 	walker += 8;
    818 	printf("  %-30s: %ju\n", "Host Cache Read Hits Commands", val);
    819 	val = le64dec(walker);
    820 	walker += 8;
    821 	printf("  %-30s: %ju\n", "Host Cache Read Hits Blocks", val);
    822 	val = le64dec(walker);
    823 	walker += 8;
    824 	printf("  %-30s: %ju\n", "Host Read Commands Stalled", val);
    825 	val = le64dec(walker);
    826 	walker += 8;
    827 	printf("  %-30s: %ju\n", "Host Write Commands", val);
    828 	val = le64dec(walker);
    829 	walker += 8;
    830 	printf("  %-30s: %ju\n", "Host Write Blocks", val);
    831 	val = le64dec(walker);
    832 	walker += 8;
    833 	printf("  %-30s: %ju\n", "Host Write Odd Start Commands", val);
    834 	val = le64dec(walker);
    835 	walker += 8;
    836 	printf("  %-30s: %ju\n", "Host Write Odd End Commands", val);
    837 	val = le64dec(walker);
    838 	walker += 8;
    839 	printf("  %-30s: %ju\n", "Host Write Commands Stalled", val);
    840 	val = le64dec(walker);
    841 	walker += 8;
    842 	printf("  %-30s: %ju\n", "NAND Read Commands", val);
    843 	val = le64dec(walker);
    844 	walker += 8;
    845 	printf("  %-30s: %ju\n", "NAND Read Blocks", val);
    846 	val = le64dec(walker);
    847 	walker += 8;
    848 	printf("  %-30s: %ju\n", "NAND Write Commands", val);
    849 	val = le64dec(walker);
    850 	walker += 8;
    851 	printf("  %-30s: %ju\n", "NAND Write Blocks", val);
    852 	val = le64dec(walker);
    853 	walker += 8;
    854 	printf("  %-30s: %ju\n", "NAND Read Before Writes", val);
    855 }
    856 
    857 static void
    858 print_hgst_info_firmware_load(void *buf, uint16_t subtype __unused,
    859     uint8_t res __unused, uint32_t size __unused)
    860 {
    861 	uint8_t *walker = buf;
    862 
    863 	printf("Firmware Load Subpage:\n");
    864 	printf("  %-30s: %d\n", "Firmware Downloads", le32dec(walker));
    865 }
    866 
    867 static void
    868 kv_indirect(void *buf, uint32_t subtype, uint8_t res, uint32_t size,
    869     struct subpage_print *sp, size_t nsp)
    870 {
    871 	size_t i;
    872 
    873 	for (i = 0; i < nsp; i++, sp++) {
    874 		if (sp->key == subtype) {
    875 			sp->fn(buf, subtype, res, size);
    876 			return;
    877 		}
    878 	}
    879 	printf("No handler for page type %x\n", subtype);
    880 }
    881 
    882 static void
    883 print_hgst_info_log(const struct nvm_identify_controller *cdata __unused, void *buf,
    884     uint32_t size __unused)
    885 {
    886 	uint8_t	*walker, *end, *subpage;
    887 	int pages __unused;
    888 	uint16_t len;
    889 	uint8_t subtype, res;
    890 
    891 	printf("HGST Extra Info Log\n");
    892 	printf("===================\n");
    893 
    894 	walker = buf;
    895 	pages = *walker++;
    896 	walker++;
    897 	len = le16dec(walker);
    898 	walker += 2;
    899 	end = walker + len;		/* Length is exclusive of this header */
    900 
    901 	while (walker < end) {
    902 		subpage = walker + 4;
    903 		subtype = *walker++ & 0x3f;	/* subtype */
    904 		res = *walker++;		/* Reserved */
    905 		len = le16dec(walker);
    906 		walker += len + 2;		/* Length, not incl header */
    907 		if (walker > end) {
    908 			printf("Ooops! Off the end of the list\n");
    909 			break;
    910 		}
    911 		kv_indirect(subpage, subtype, res, len, hgst_subpage,
    912 		    __arraycount(hgst_subpage));
    913 	}
    914 }
    915 
    916 /*
    917  * Table of log page printer / sizing.
    918  *
    919  * This includes Intel specific pages that are widely implemented.
    920  * Make sure you keep all the pages of one vendor together so -v help
    921  * lists all the vendors pages.
    922  */
    923 static struct logpage_function {
    924 	uint8_t		log_page;
    925 	const char     *vendor;
    926 	const char     *name;
    927 	print_fn_t	print_fn;
    928 	size_t		size;
    929 } logfuncs[] = {
    930 	{NVME_LOG_ERROR,		NULL,	"Drive Error Log",
    931 	 print_log_error,		0},
    932 	{NVME_LOG_HEALTH_INFORMATION,	NULL,	"Health/SMART Data",
    933 	 print_log_health,		sizeof(struct nvme_health_information_page)},
    934 	{NVME_LOG_FIRMWARE_SLOT,	NULL,	"Firmware Information",
    935 	 print_log_firmware,		sizeof(struct nvme_firmware_page)},
    936 	{HGST_INFO_LOG,			"hgst",	"Detailed Health/SMART",
    937 	 print_hgst_info_log,		DEFAULT_SIZE},
    938 	{HGST_INFO_LOG,			"wds",	"Detailed Health/SMART",
    939 	 print_hgst_info_log,		DEFAULT_SIZE},
    940 	{INTEL_LOG_TEMP_STATS,		"intel", "Temperature Stats",
    941 	 print_intel_temp_stats,	sizeof(struct intel_log_temp_stats)},
    942 	{INTEL_LOG_READ_LAT_LOG,	"intel", "Read Latencies",
    943 	 print_intel_read_lat_log,	DEFAULT_SIZE},
    944 	{INTEL_LOG_WRITE_LAT_LOG,	"intel", "Write Latencies",
    945 	 print_intel_write_lat_log,	DEFAULT_SIZE},
    946 	{INTEL_LOG_ADD_SMART,		"intel", "Extra Health/SMART Data",
    947 	 print_intel_add_smart,		DEFAULT_SIZE},
    948 	{INTEL_LOG_ADD_SMART,		"samsung", "Extra Health/SMART Data",
    949 	 print_intel_add_smart,		DEFAULT_SIZE},
    950 
    951 	{0, NULL, NULL, NULL, 0},
    952 };
    953 
    954 __dead static void
    955 logpage_usage(void)
    956 {
    957 	fprintf(stderr, "usage:\n");
    958 	fprintf(stderr, "\t%s " LOGPAGE_USAGE, getprogname());
    959 	exit(1);
    960 }
    961 
    962 __dead static void
    963 logpage_help(void)
    964 {
    965 	struct logpage_function		*f;
    966 	const char 			*v;
    967 
    968 	fprintf(stderr, "\n");
    969 	fprintf(stderr, "%-8s %-10s %s\n", "Page", "Vendor","Page Name");
    970 	fprintf(stderr, "-------- ---------- ----------\n");
    971 	for (f = logfuncs; f->log_page > 0; f++) {
    972 		v = f->vendor == NULL ? "-" : f->vendor;
    973 		fprintf(stderr, "0x%02x     %-10s %s\n", f->log_page, v, f->name);
    974 	}
    975 
    976 	exit(1);
    977 }
    978 
    979 void
    980 logpage(int argc, char *argv[])
    981 {
    982 	int				fd, nsid;
    983 	int				log_page = 0, pageflag = false;
    984 	int				binflag = false, hexflag = false, ns_specified;
    985 	int				ch;
    986 	char				*p;
    987 	char				cname[64];
    988 	uint32_t			size;
    989 	void				*buf;
    990 	const char 			*vendor = NULL;
    991 	struct logpage_function		*f;
    992 	struct nvm_identify_controller	cdata;
    993 	print_fn_t			print_fn;
    994 
    995 	while ((ch = getopt(argc, argv, "bp:xv:")) != -1) {
    996 		switch (ch) {
    997 		case 'b':
    998 			binflag = true;
    999 			break;
   1000 		case 'p':
   1001 			if (strcmp(optarg, "help") == 0)
   1002 				logpage_help();
   1003 
   1004 			/* TODO: Add human-readable ASCII page IDs */
   1005 			log_page = strtol(optarg, &p, 0);
   1006 			if (p != NULL && *p != '\0') {
   1007 				fprintf(stderr,
   1008 				    "\"%s\" not valid log page id.\n",
   1009 				    optarg);
   1010 				logpage_usage();
   1011 			}
   1012 			pageflag = true;
   1013 			break;
   1014 		case 'x':
   1015 			hexflag = true;
   1016 			break;
   1017 		case 'v':
   1018 			if (strcmp(optarg, "help") == 0)
   1019 				logpage_help();
   1020 			vendor = optarg;
   1021 			break;
   1022 		}
   1023 	}
   1024 
   1025 	if (!pageflag) {
   1026 		printf("Missing page_id (-p).\n");
   1027 		logpage_usage();
   1028 	}
   1029 
   1030 	/* Check that a controller and/or namespace was specified. */
   1031 	if (optind >= argc)
   1032 		logpage_usage();
   1033 
   1034 	if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) {
   1035 		ns_specified = true;
   1036 		parse_ns_str(argv[optind], cname, &nsid);
   1037 		open_dev(cname, &fd, 1, 1);
   1038 	} else {
   1039 		ns_specified = false;
   1040 		nsid = 0xffffffff;
   1041 		open_dev(argv[optind], &fd, 1, 1);
   1042 	}
   1043 
   1044 	read_controller_data(fd, &cdata);
   1045 
   1046 	/*
   1047 	 * The log page attributes indicate whether or not the controller
   1048 	 * supports the SMART/Health information log page on a per
   1049 	 * namespace basis.
   1050 	 */
   1051 	if (ns_specified) {
   1052 		if (log_page != NVME_LOG_HEALTH_INFORMATION)
   1053 			errx(1, "log page %d valid only at controller level",
   1054 			    log_page);
   1055 		if (!(cdata.lpa & NVME_ID_CTRLR_LPA_NS_SMART))
   1056 			errx(1,
   1057 			    "controller does not support per namespace "
   1058 			    "smart/health information");
   1059 	}
   1060 
   1061 	print_fn = print_log_hex;
   1062 	size = DEFAULT_SIZE;
   1063 	if (binflag)
   1064 		print_fn = print_bin;
   1065 	if (!binflag && !hexflag) {
   1066 		/*
   1067 		 * See if there is a pretty print function for the specified log
   1068 		 * page.  If one isn't found, we just revert to the default
   1069 		 * (print_hex). If there was a vendor specified bt the user, and
   1070 		 * the page is vendor specific, don't match the print function
   1071 		 * unless the vendors match.
   1072 		 */
   1073 		for (f = logfuncs; f->log_page > 0; f++) {
   1074 			if (f->vendor != NULL && vendor != NULL &&
   1075 			    strcmp(f->vendor, vendor) != 0)
   1076 				continue;
   1077 			if (log_page != f->log_page)
   1078 				continue;
   1079 			print_fn = f->print_fn;
   1080 			size = f->size;
   1081 			break;
   1082 		}
   1083 	}
   1084 
   1085 	if (log_page == NVME_LOG_ERROR) {
   1086 		size = sizeof(struct nvme_error_information_entry);
   1087 		size *= (cdata.elpe + 1);
   1088 	}
   1089 
   1090 	/* Read the log page */
   1091 	buf = get_log_buffer(size);
   1092 	read_logpage(fd, log_page, nsid, buf, size);
   1093 	print_fn(&cdata, buf, size);
   1094 
   1095 	close(fd);
   1096 	exit(0);
   1097 }
   1098