xref: /freebsd/sbin/nvmecontrol/modules/intel/intel.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 EMC Corp.
5  * All rights reserved.
6  *
7  * Copyright (C) 2012-2013 Intel Corporation
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
34 
35 #include <ctype.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/endian.h>
45 
46 #include "nvmecontrol.h"
47 
48 /*
49  * Intel specific log pages from
50  * http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/ssd-dc-p3700-spec.pdf
51  *
52  * Though the version as of this date has a typo for the size of log page 0xca,
53  * offset 147: it is only 1 byte, not 6.
54  */
55 static void
56 print_intel_temp_stats(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
57 {
58 	struct intel_log_temp_stats	*temp = buf;
59 
60 	printf("Intel Temperature Log\n");
61 	printf("=====================\n");
62 
63 	printf("Current:                        ");
64 	print_temp_C(temp->current);
65 	printf("Overtemp Last Flags             %#jx\n", (uintmax_t)temp->overtemp_flag_last);
66 	printf("Overtemp Lifetime Flags         %#jx\n", (uintmax_t)temp->overtemp_flag_life);
67 	printf("Max Temperature                 ");
68 	print_temp_C(temp->max_temp);
69 	printf("Min Temperature                 ");
70 	print_temp_C(temp->min_temp);
71 	printf("Max Operating Temperature       ");
72 	print_temp_C(temp->max_oper_temp);
73 	printf("Min Operating Temperature       ");
74 	print_temp_C(temp->min_oper_temp);
75 	printf("Estimated Temperature Offset:   %ju C/K\n", (uintmax_t)temp->est_offset);
76 }
77 
78 /*
79  * Format from Table 22, section 5.7 IO Command Latency Statistics.
80  * Read and write stats pages have identical encoding.
81  */
82 static void
83 print_intel_read_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
84 {
85 	const char *walker = buf;
86 	int i;
87 
88 	printf("Major:                         %d\n", le16dec(walker + 0));
89 	printf("Minor:                         %d\n", le16dec(walker + 2));
90 	for (i = 0; i < 32; i++)
91 		printf("%4dus-%4dus:                 %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 4 + i * 4));
92 	for (i = 1; i < 32; i++)
93 		printf("%4dms-%4dms:                 %ju\n", i, i + 1, (uintmax_t)le32dec(walker + 132 + i * 4));
94 	for (i = 1; i < 32; i++)
95 		printf("%4dms-%4dms:                 %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 256 + i * 4));
96 }
97 
98 static void
99 print_intel_read_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
100 {
101 
102 	printf("Intel Read Latency Log\n");
103 	printf("======================\n");
104 	print_intel_read_write_lat_log(cdata, buf, size);
105 }
106 
107 static void
108 print_intel_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
109 {
110 
111 	printf("Intel Write Latency Log\n");
112 	printf("=======================\n");
113 	print_intel_read_write_lat_log(cdata, buf, size);
114 }
115 
116 /*
117  * Table 19. 5.4 SMART Attributes. Others also implement this and some extra data not documented.
118  */
119 void
120 print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
121 {
122 	uint8_t *walker = buf;
123 	uint8_t *end = walker + 150;
124 	const char *name;
125 	uint64_t raw;
126 	uint8_t normalized;
127 
128 	static struct kv_name kv[] =
129 	{
130 		{ 0xab, "Program Fail Count" },
131 		{ 0xac, "Erase Fail Count" },
132 		{ 0xad, "Wear Leveling Count" },
133 		{ 0xb8, "End to End Error Count" },
134 		{ 0xc7, "CRC Error Count" },
135 		{ 0xe2, "Timed: Media Wear" },
136 		{ 0xe3, "Timed: Host Read %" },
137 		{ 0xe4, "Timed: Elapsed Time" },
138 		{ 0xea, "Thermal Throttle Status" },
139 		{ 0xf0, "Retry Buffer Overflows" },
140 		{ 0xf3, "PLL Lock Loss Count" },
141 		{ 0xf4, "NAND Bytes Written" },
142 		{ 0xf5, "Host Bytes Written" },
143 	};
144 
145 	printf("Additional SMART Data Log\n");
146 	printf("=========================\n");
147 	/*
148 	 * walker[0] = Key
149 	 * walker[1,2] = reserved
150 	 * walker[3] = Normalized Value
151 	 * walker[4] = reserved
152 	 * walker[5..10] = Little Endian Raw value
153 	 *	(or other represenations)
154 	 * walker[11] = reserved
155 	 */
156 	while (walker < end) {
157 		name = kv_lookup(kv, nitems(kv), *walker);
158 		normalized = walker[3];
159 		raw = le48dec(walker + 5);
160 		switch (*walker){
161 		case 0:
162 			break;
163 		case 0xad:
164 			printf("%-32s: %3d min: %u max: %u ave: %u\n", name, normalized,
165 			    le16dec(walker + 5), le16dec(walker + 7), le16dec(walker + 9));
166 			break;
167 		case 0xe2:
168 			printf("%-32s: %3d %.3f%%\n", name, normalized, raw / 1024.0);
169 			break;
170 		case 0xea:
171 			printf("%-32s: %3d %d%% %d times\n", name, normalized, walker[5], le32dec(walker+6));
172 			break;
173 		default:
174 			printf("%-32s: %3d %ju\n", name, normalized, (uintmax_t)raw);
175 			break;
176 		}
177 		walker += 12;
178 	}
179 }
180 
181 NVME_LOGPAGE(intel_temp,
182     INTEL_LOG_TEMP_STATS,		"intel", "Temperature Stats",
183     print_intel_temp_stats,		sizeof(struct intel_log_temp_stats));
184 NVME_LOGPAGE(intel_rlat,
185     INTEL_LOG_READ_LAT_LOG,		"intel", "Read Latencies",
186     print_intel_read_lat_log,		DEFAULT_SIZE);
187 NVME_LOGPAGE(intel_wlat,
188     INTEL_LOG_WRITE_LAT_LOG,		"intel", "Write Latencies",
189     print_intel_write_lat_log,		DEFAULT_SIZE);
190 NVME_LOGPAGE(intel_smart,
191     INTEL_LOG_ADD_SMART,		"intel", "Extra Health/SMART Data",
192     print_intel_add_smart,		DEFAULT_SIZE);
193