xref: /freebsd/sbin/nvmecontrol/modules/intel/intel.c (revision 315ee00f)
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/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/ioccom.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/endian.h>
46 
47 #include "nvmecontrol.h"
48 
49 /*
50  * Intel specific log pages from
51  * http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/ssd-dc-p3700-spec.pdf
52  *
53  * Though the version as of this date has a typo for the size of log page 0xca,
54  * offset 147: it is only 1 byte, not 6.
55  */
56 static void
57 print_intel_temp_stats(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
58 {
59 	struct intel_log_temp_stats	*temp = buf;
60 
61 	printf("Intel Temperature Log\n");
62 	printf("=====================\n");
63 
64 	printf("Current:                        ");
65 	print_temp_C(temp->current);
66 	printf("Overtemp Last Flags             %#jx\n", (uintmax_t)temp->overtemp_flag_last);
67 	printf("Overtemp Lifetime Flags         %#jx\n", (uintmax_t)temp->overtemp_flag_life);
68 	printf("Max Temperature                 ");
69 	print_temp_C(temp->max_temp);
70 	printf("Min Temperature                 ");
71 	print_temp_C(temp->min_temp);
72 	printf("Max Operating Temperature       ");
73 	print_temp_C(temp->max_oper_temp);
74 	printf("Min Operating Temperature       ");
75 	print_temp_C(temp->min_oper_temp);
76 	printf("Estimated Temperature Offset:   %ju C/K\n", (uintmax_t)temp->est_offset);
77 }
78 
79 /*
80  * Format from Table 22, section 5.7 IO Command Latency Statistics.
81  * Read and write stats pages have identical encoding.
82  */
83 static void
84 print_intel_read_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
85 {
86 	const char *walker = buf;
87 	int i;
88 
89 	printf("Major:                         %d\n", le16dec(walker + 0));
90 	printf("Minor:                         %d\n", le16dec(walker + 2));
91 	for (i = 0; i < 32; i++)
92 		printf("%4dus-%4dus:                 %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 4 + i * 4));
93 	for (i = 1; i < 32; i++)
94 		printf("%4dms-%4dms:                 %ju\n", i, i + 1, (uintmax_t)le32dec(walker + 132 + i * 4));
95 	for (i = 1; i < 32; i++)
96 		printf("%4dms-%4dms:                 %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 256 + i * 4));
97 }
98 
99 static void
100 print_intel_read_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
101 {
102 
103 	printf("Intel Read Latency Log\n");
104 	printf("======================\n");
105 	print_intel_read_write_lat_log(cdata, buf, size);
106 }
107 
108 static void
109 print_intel_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
110 {
111 
112 	printf("Intel Write Latency Log\n");
113 	printf("=======================\n");
114 	print_intel_read_write_lat_log(cdata, buf, size);
115 }
116 
117 /*
118  * Table 19. 5.4 SMART Attributes. Others also implement this and some extra data not documented.
119  */
120 void
121 print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
122 {
123 	uint8_t *walker = buf;
124 	uint8_t *end = walker + 150;
125 	const char *name;
126 	uint64_t raw;
127 	uint8_t normalized;
128 
129 	static struct kv_name kv[] =
130 	{
131 		{ 0xab, "Program Fail Count" },
132 		{ 0xac, "Erase Fail Count" },
133 		{ 0xad, "Wear Leveling Count" },
134 		{ 0xb8, "End to End Error Count" },
135 		{ 0xc7, "CRC Error Count" },
136 		{ 0xe2, "Timed: Media Wear" },
137 		{ 0xe3, "Timed: Host Read %" },
138 		{ 0xe4, "Timed: Elapsed Time" },
139 		{ 0xea, "Thermal Throttle Status" },
140 		{ 0xf0, "Retry Buffer Overflows" },
141 		{ 0xf3, "PLL Lock Loss Count" },
142 		{ 0xf4, "NAND Bytes Written" },
143 		{ 0xf5, "Host Bytes Written" },
144 	};
145 
146 	printf("Additional SMART Data Log\n");
147 	printf("=========================\n");
148 	/*
149 	 * walker[0] = Key
150 	 * walker[1,2] = reserved
151 	 * walker[3] = Normalized Value
152 	 * walker[4] = reserved
153 	 * walker[5..10] = Little Endian Raw value
154 	 *	(or other represenations)
155 	 * walker[11] = reserved
156 	 */
157 	while (walker < end) {
158 		name = kv_lookup(kv, nitems(kv), *walker);
159 		normalized = walker[3];
160 		raw = le48dec(walker + 5);
161 		switch (*walker){
162 		case 0:
163 			break;
164 		case 0xad:
165 			printf("%-32s: %3d min: %u max: %u ave: %u\n", name, normalized,
166 			    le16dec(walker + 5), le16dec(walker + 7), le16dec(walker + 9));
167 			break;
168 		case 0xe2:
169 			printf("%-32s: %3d %.3f%%\n", name, normalized, raw / 1024.0);
170 			break;
171 		case 0xea:
172 			printf("%-32s: %3d %d%% %d times\n", name, normalized, walker[5], le32dec(walker+6));
173 			break;
174 		default:
175 			printf("%-32s: %3d %ju\n", name, normalized, (uintmax_t)raw);
176 			break;
177 		}
178 		walker += 12;
179 	}
180 }
181 
182 NVME_LOGPAGE(intel_temp,
183     INTEL_LOG_TEMP_STATS,		"intel", "Temperature Stats",
184     print_intel_temp_stats,		sizeof(struct intel_log_temp_stats));
185 NVME_LOGPAGE(intel_rlat,
186     INTEL_LOG_READ_LAT_LOG,		"intel", "Read Latencies",
187     print_intel_read_lat_log,		DEFAULT_SIZE);
188 NVME_LOGPAGE(intel_wlat,
189     INTEL_LOG_WRITE_LAT_LOG,		"intel", "Write Latencies",
190     print_intel_write_lat_log,		DEFAULT_SIZE);
191 NVME_LOGPAGE(intel_smart,
192     INTEL_LOG_ADD_SMART,		"intel", "Extra Health/SMART Data",
193     print_intel_add_smart,		DEFAULT_SIZE);
194