xref: /freebsd/sbin/nvmecontrol/modules/micron/micron.c (revision 72170a4174e0ba50e349918667973a158a94ed8f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Wanpeng Qian <wanpengqian@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/ioccom.h>
33 
34 #include <ctype.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/endian.h>
44 
45 #include "nvmecontrol.h"
46 
47 static void
print_micron_unique_smart(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size __unused)48 print_micron_unique_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
49 {
50 	uint8_t *walker = buf;
51 	uint8_t *end = walker + 150;
52 	const char *name;
53 	uint64_t raw;
54 	uint8_t normalized;
55 
56 	static struct kv_name kv[] =
57 	{
58 		{ 0xf9, "NAND Writes 1GiB" },
59 		{ 0xfa, "NAND Reads 1GiB" },
60 		{ 0xea, "Thermal Throttle Status" },
61 		{ 0xe7, "Temperature" },
62 		{ 0xe8, "Power Consumption" },
63 		{ 0xaf, "Power Loss Protection" },
64 	};
65 
66 	printf("Vendor Unique SMART Information\n");
67 	printf("=========================\n");
68 	/*
69 	 * walker[0] = Key
70 	 * walker[1,2] = reserved
71 	 * walker[3] = Normalized Value
72 	 * walker[4] = reserved
73 	 * walker[5..10] = Little Endian Raw value
74 	 *	(or other represenations)
75 	 * walker[11] = reserved
76 	 */
77 	while (walker < end) {
78 		name = kv_lookup(kv, nitems(kv), *walker);
79 		normalized = walker[3];
80 		raw = le48dec(walker + 5);
81 		switch (*walker){
82 		case 0:
83 			break;
84 		case 0xf9:
85 			/* FALLTHOUGH */
86 		case 0xfa:
87 			printf("%2X %-24s: %ju GiB\n", *walker, name, (uintmax_t)raw);
88 			break;
89 		case 0xea:
90 			printf("%2X %-24s:", *walker, name);
91 			if (*(walker + 5) == 0)
92 				printf(" inactive\n");
93 			if (*(walker + 5) == 1)
94 				printf(" active, total throttling time %u mins\n", le32dec(walker + 6));
95 			break;
96 		case 0xe7:
97 			printf("%2X %-24s: max ", *walker, name);
98 			print_temp_C(le16dec(walker + 5));
99 			printf("                           : min ");
100 			print_temp_C(le16dec(walker + 7));
101 			printf("                           : cur ");
102 			print_temp_C(le16dec(walker + 9));
103 			break;
104 		case 0xe8:
105 			printf("%2X %-24s: max %u W, min %u W, ave %u W\n",
106 			    *walker, name, le16dec(walker + 5), le16dec(walker + 7), le16dec(walker + 9));
107 			break;
108 		case 0xaf:
109 			printf("%2X %-24s:", *walker, name);
110 			if (normalized == 100)
111 				printf(" success");
112 			if (normalized == 0)
113 				printf(" failed");
114 			printf(" %3d\n", normalized);
115 			break;
116 		default:
117 			printf("%2X %-24s: %3d %ju\n",
118 			    *walker, name, normalized, (uintmax_t)raw);
119 			break;
120 		}
121 		walker += 12;
122 	}
123 }
124 
125 #define MICRON_LOG_UNIQUE_SMART	0xca
126 
127 NVME_LOGPAGE(micron_smart,
128     MICRON_LOG_UNIQUE_SMART,		"micron", "Vendor Unique SMART Information",
129     print_micron_unique_smart,		DEFAULT_SIZE);
130