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
print_intel_temp_stats(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size __unused)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(letoh(temp->current));
65 printf("Overtemp Last Flags %#jx\n",
66 (uintmax_t)letoh(temp->overtemp_flag_last));
67 printf("Overtemp Lifetime Flags %#jx\n",
68 (uintmax_t)letoh(temp->overtemp_flag_life));
69 printf("Max Temperature ");
70 print_temp_C(letoh(temp->max_temp));
71 printf("Min Temperature ");
72 print_temp_C(letoh(temp->min_temp));
73 printf("Max Operating Temperature ");
74 print_temp_C(letoh(temp->max_oper_temp));
75 printf("Min Operating Temperature ");
76 print_temp_C(letoh(temp->min_oper_temp));
77 printf("Estimated Temperature Offset: %ju C/K\n",
78 (uintmax_t)letoh(temp->est_offset));
79 }
80
81 /*
82 * Format from Table 22, section 5.7 IO Command Latency Statistics.
83 * Read and write stats pages have identical encoding.
84 */
85 static void
print_intel_read_write_lat_log(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size __unused)86 print_intel_read_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
87 {
88 const char *walker = buf;
89 int i;
90
91 printf("Major: %d\n", le16dec(walker + 0));
92 printf("Minor: %d\n", le16dec(walker + 2));
93 for (i = 0; i < 32; i++)
94 printf("%4dus-%4dus: %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 4 + i * 4));
95 for (i = 1; i < 32; i++)
96 printf("%4dms-%4dms: %ju\n", i, i + 1, (uintmax_t)le32dec(walker + 132 + i * 4));
97 for (i = 1; i < 32; i++)
98 printf("%4dms-%4dms: %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 256 + i * 4));
99 }
100
101 static void
print_intel_read_lat_log(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size)102 print_intel_read_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
103 {
104
105 printf("Intel Read Latency Log\n");
106 printf("======================\n");
107 print_intel_read_write_lat_log(cdata, buf, size);
108 }
109
110 static void
print_intel_write_lat_log(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size)111 print_intel_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
112 {
113
114 printf("Intel Write Latency Log\n");
115 printf("=======================\n");
116 print_intel_read_write_lat_log(cdata, buf, size);
117 }
118
119 /*
120 * Table 19. 5.4 SMART Attributes. Others also implement this and some extra data not documented.
121 * Note: different models implement the same key values to mean different things. To fix that,
122 * we'd need to index this to a vendor/device values.
123 */
124 void
print_intel_add_smart(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size __unused)125 print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
126 {
127 uint8_t *walker = buf;
128 uint8_t *end = walker + 150;
129 const char *name;
130 uint64_t raw;
131 uint8_t normalized;
132
133 static struct kv_name kv[] =
134 {
135 { 0xab, "Program Fail Count" },
136 { 0xac, "Erase Fail Count" },
137 { 0xad, "Wear Leveling Count" },
138 { 0xb8, "End to End Error Count" },
139 { 0xc7, "CRC Error Count" },
140 { 0xe2, "Timed: Media Wear" },
141 { 0xe3, "Timed: Host Read %" },
142 { 0xe4, "Timed: Elapsed Time" },
143 { 0xe7, "Lifetime Temperature" },
144 { 0xe8, "Power" },
145 { 0xea, "Thermal Throttle Status" },
146 { 0xf0, "Retry Buffer Overflows" },
147 { 0xf3, "PLL Lock Loss Count" },
148 { 0xf4, "NAND Bytes Written" },
149 { 0xf5, "Host Bytes Written" },
150 { 0xf9, "NAND GiB Written" },
151 { 0xfa, "NAND GiB Read" },
152 };
153
154 printf("Additional SMART Data Log\n");
155 printf("=========================\n");
156 /*
157 * walker[0] = Key
158 * walker[1,2] = reserved
159 * walker[3] = Normalized Value
160 * walker[4] = reserved
161 * walker[5..10] = Little Endian Raw value
162 * (or other represenations)
163 * walker[11] = reserved
164 */
165 while (walker < end) {
166 name = kv_lookup(kv, nitems(kv), *walker);
167 normalized = walker[3];
168 raw = le48dec(walker + 5);
169 switch (*walker){
170 case 0:
171 break;
172 case 0xad:
173 printf("%-32s: %3d min: %u max: %u ave: %u\n", name, normalized,
174 le16dec(walker + 5), le16dec(walker + 7), le16dec(walker + 9));
175 break;
176 case 0xe2:
177 printf("%-32s: %3d %.3f%%\n", name, normalized, raw / 1024.0);
178 break;
179 case 0xe7:
180 printf("%-32s: %3d %#jx max: %dK min: %dK cur: %dK\n", name, normalized,
181 (uintmax_t)raw, le16dec(walker+5), le16dec(walker+7), le16dec(walker+9));
182 break;
183 case 0xe8:
184 printf("%-32s: %3d %#jx max: %dW min: %dW cur: %dW\n", name, normalized,
185 (uintmax_t)raw, le16dec(walker+5), le16dec(walker+7), le16dec(walker+9));
186 break;
187 case 0xea:
188 printf("%-32s: %3d %d%% %d times\n", name, normalized, walker[5], le32dec(walker+6));
189 break;
190 default:
191 printf("%-32s: %3d %ju %#jx\n", name, normalized, (uintmax_t)raw, (uintmax_t)raw);
192 break;
193 }
194 walker += 12;
195 }
196 }
197
198 NVME_LOGPAGE(intel_temp,
199 INTEL_LOG_TEMP_STATS, "intel", "Temperature Stats",
200 print_intel_temp_stats, sizeof(struct intel_log_temp_stats));
201 NVME_LOGPAGE(intel_rlat,
202 INTEL_LOG_READ_LAT_LOG, "intel", "Read Latencies",
203 print_intel_read_lat_log, DEFAULT_SIZE);
204 NVME_LOGPAGE(intel_wlat,
205 INTEL_LOG_WRITE_LAT_LOG, "intel", "Write Latencies",
206 print_intel_write_lat_log, DEFAULT_SIZE);
207 NVME_LOGPAGE(intel_smart, /* Note: Samsung and Micron also use this */
208 INTEL_LOG_ADD_SMART, "intel", "Extra Health/SMART Data",
209 print_intel_add_smart, DEFAULT_SIZE);
210