1 /* $NetBSD: logpage.c,v 1.3 2017/02/13 11:16:46 nonaka Exp $ */ 2 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 #ifndef lint 34 __RCSID("$NetBSD: logpage.c,v 1.3 2017/02/13 11:16:46 nonaka Exp $"); 35 #if 0 36 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 285796 2015-07-22 16:10:29Z jimharris $"); 37 #endif 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/ioccom.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <fcntl.h> 46 #include <stdbool.h> 47 #include <stddef.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "nvmectl.h" 54 #include "bn.h" 55 56 #define DEFAULT_SIZE (4096) 57 #define MAX_FW_SLOTS (7) 58 59 typedef void (*print_fn_t)(void *buf, uint32_t size); 60 61 static void * 62 get_log_buffer(uint32_t size) 63 { 64 void *buf; 65 66 if ((buf = malloc(size)) == NULL) 67 errx(1, "unable to malloc %u bytes", size); 68 69 memset(buf, 0, size); 70 return (buf); 71 } 72 73 void 74 read_logpage(int fd, uint8_t log_page, int nsid, void *payload, 75 uint32_t payload_size) 76 { 77 struct nvme_pt_command pt; 78 79 memset(&pt, 0, sizeof(pt)); 80 pt.cmd.opcode = NVM_ADMIN_GET_LOG_PG; 81 pt.cmd.nsid = nsid; 82 pt.cmd.cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16; 83 pt.cmd.cdw10 |= log_page; 84 pt.buf = payload; 85 pt.len = payload_size; 86 pt.is_read = 1; 87 88 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 89 err(1, "get log page request failed"); 90 91 if (nvme_completion_is_error(&pt.cpl)) 92 errx(1, "get log page request returned error"); 93 } 94 95 static void 96 print_log_error(void *buf, uint32_t size) 97 { 98 int i, nentries; 99 struct nvme_error_information_entry *entry = buf; 100 101 printf("Error Information Log\n"); 102 printf("=====================\n"); 103 104 if (entry->error_count == 0) { 105 printf("No error entries found\n"); 106 return; 107 } 108 109 nentries = size/sizeof(struct nvme_error_information_entry); 110 for (i = 0; i < nentries; i++, entry++) { 111 if (entry->error_count == 0) 112 break; 113 114 printf("Entry %02d\n", i + 1); 115 printf("=========\n"); 116 printf(" Error count: %ju\n", entry->error_count); 117 printf(" Submission queue ID: %u\n", entry->sqid); 118 printf(" Command ID: %u\n", entry->cid); 119 /* TODO: Export nvme_status_string structures from kernel? */ 120 printf(" Status:\n"); 121 printf(" Phase tag: %d\n", 122 (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_PHASE)); 123 printf(" Status code: %d\n", 124 (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_SC_MASK)); 125 printf(" Status code type: %d\n", 126 (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_SCT_MASK)); 127 printf(" More: %d\n", 128 (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_M)); 129 printf(" DNR: %d\n", 130 (uint16_t)__SHIFTOUT(entry->status, NVME_CQE_DNR)); 131 printf(" Error location: %u\n", entry->error_location); 132 printf(" LBA: %ju\n", entry->lba); 133 printf(" Namespace ID: %u\n", entry->nsid); 134 printf(" Vendor specific info: %u\n", entry->vendor_specific); 135 printf(" Command specific info: %ju\n", 136 entry->command_specific); 137 } 138 } 139 140 #define METRIX_PREFIX_BUFSIZ 17 141 #define NO_METRIX_PREFIX_BUFSIZ 42 142 143 static void 144 print_bignum(const char *title, uint64_t v[2], const char *suffix) 145 { 146 char buf[64]; 147 uint8_t tmp[16]; 148 uint64_t l = le64toh(v[0]); 149 uint64_t h = le64toh(v[1]); 150 151 tmp[ 0] = (h >> 56) & 0xff; 152 tmp[ 1] = (h >> 48) & 0xff; 153 tmp[ 2] = (h >> 40) & 0xff; 154 tmp[ 3] = (h >> 32) & 0xff; 155 tmp[ 4] = (h >> 24) & 0xff; 156 tmp[ 5] = (h >> 16) & 0xff; 157 tmp[ 6] = (h >> 8) & 0xff; 158 tmp[ 7] = h & 0xff; 159 tmp[ 8] = (l >> 56) & 0xff; 160 tmp[ 9] = (l >> 48) & 0xff; 161 tmp[10] = (l >> 40) & 0xff; 162 tmp[11] = (l >> 32) & 0xff; 163 tmp[12] = (l >> 24) & 0xff; 164 tmp[13] = (l >> 16) & 0xff; 165 tmp[14] = (l >> 8) & 0xff; 166 tmp[15] = l & 0xff; 167 168 buf[0] = '\0'; 169 BIGNUM *bn = BN_bin2bn(tmp, __arraycount(tmp), NULL); 170 if (bn != NULL) { 171 humanize_bignum(buf, METRIX_PREFIX_BUFSIZ + strlen(suffix), 172 bn, suffix, HN_AUTOSCALE, HN_DECIMAL); 173 BN_free(bn); 174 } 175 if (buf[0] == '\0') 176 snprintf(buf, sizeof(buf), "0x%016jx%016jx", h, l); 177 printf("%-31s %s\n", title, buf); 178 } 179 180 static void 181 print_log_health(void *buf, uint32_t size __unused) 182 { 183 struct nvme_health_information_page *health = buf; 184 float composite_temperature = health->composite_temperature; 185 186 printf("SMART/Health Information Log\n"); 187 printf("============================\n"); 188 189 printf("Critical Warning State: 0x%02x\n", 190 health->critical_warning); 191 printf(" Available spare: %d\n", 192 (uint8_t)__SHIFTOUT(health->critical_warning, 193 NVME_HEALTH_PAGE_CW_AVAIL_SPARE)); 194 printf(" Temperature: %d\n", 195 (uint8_t)__SHIFTOUT(health->critical_warning, 196 NVME_HEALTH_PAGE_CW_TEMPERTURE)); 197 printf(" Device reliability: %d\n", 198 (uint8_t)__SHIFTOUT(health->critical_warning, 199 NVME_HEALTH_PAGE_CW_DEVICE_RELIABLITY)); 200 printf(" Read only: %d\n", 201 (uint8_t)__SHIFTOUT(health->critical_warning, 202 NVME_HEALTH_PAGE_CW_READ_ONLY)); 203 printf(" Volatile memory backup: %d\n", 204 (uint8_t)__SHIFTOUT(health->critical_warning, 205 NVME_HEALTH_PAGE_CW_VOLATILE_MEMORY_BACKUP)); 206 printf("Temperature: %u K, %2.2f C, %3.2f F\n", 207 health->composite_temperature, 208 composite_temperature - (float)273.15, 209 (composite_temperature * (float)9/5) - (float)459.67); 210 printf("Available spare: %u\n", 211 health->available_spare); 212 printf("Available spare threshold: %u\n", 213 health->available_spare_threshold); 214 printf("Percentage used: %u\n", 215 health->percentage_used); 216 217 print_bignum("Data units (512 byte) read:", 218 health->data_units_read, ""); 219 print_bignum("Data units (512 byte) written:", 220 health->data_units_written, ""); 221 print_bignum("Host read commands:", 222 health->host_read_commands, ""); 223 print_bignum("Host write commands:", 224 health->host_write_commands, ""); 225 print_bignum("Controller busy time (minutes):", 226 health->controller_busy_time, ""); 227 print_bignum("Power cycles:", 228 health->power_cycles, ""); 229 print_bignum("Power on hours:", 230 health->power_on_hours, ""); 231 print_bignum("Unsafe shutdowns:", 232 health->unsafe_shutdowns, ""); 233 print_bignum("Media errors:", 234 health->media_errors, ""); 235 print_bignum("No. error info log entries:", 236 health->num_error_info_log_entries, ""); 237 } 238 239 static void 240 print_log_firmware(void *buf, uint32_t size __unused) 241 { 242 u_int i; 243 const char *status; 244 struct nvme_firmware_page *fw = buf; 245 246 printf("Firmware Slot Log\n"); 247 printf("=================\n"); 248 249 for (i = 0; i < MAX_FW_SLOTS; i++) { 250 printf("Slot %d: ", i + 1); 251 if (__SHIFTOUT(fw->afi, NVME_FW_PAGE_AFI_SLOT) == i + 1) 252 status = " Active"; 253 else 254 status = "Inactive"; 255 256 if (fw->revision[i] == 0LLU) 257 printf("Empty\n"); 258 else 259 if (isprint(*(uint8_t *)&fw->revision[i])) 260 printf("[%s] %.8s\n", status, 261 (char *)&fw->revision[i]); 262 else 263 printf("[%s] %016jx\n", status, 264 fw->revision[i]); 265 } 266 } 267 268 static struct logpage_function { 269 uint8_t log_page; 270 print_fn_t fn; 271 } logfuncs[] = { 272 {NVME_LOG_ERROR, print_log_error }, 273 {NVME_LOG_HEALTH_INFORMATION, print_log_health }, 274 {NVME_LOG_FIRMWARE_SLOT, print_log_firmware }, 275 {0, NULL }, 276 }; 277 278 __dead static void 279 logpage_usage(void) 280 { 281 fprintf(stderr, "usage:\n"); 282 fprintf(stderr, LOGPAGE_USAGE); 283 exit(1); 284 } 285 286 void 287 logpage(int argc, char *argv[]) 288 { 289 int fd, nsid; 290 int log_page = 0, pageflag = false; 291 int hexflag = false, ns_specified; 292 int ch; 293 char *p; 294 char cname[64]; 295 uint32_t size; 296 void *buf; 297 struct logpage_function *f; 298 struct nvm_identify_controller cdata; 299 print_fn_t print_fn; 300 301 while ((ch = getopt(argc, argv, "p:x")) != -1) { 302 switch (ch) { 303 case 'p': 304 /* TODO: Add human-readable ASCII page IDs */ 305 log_page = strtol(optarg, &p, 0); 306 if (p != NULL && *p != '\0') { 307 fprintf(stderr, 308 "\"%s\" not valid log page id.\n", 309 optarg); 310 logpage_usage(); 311 /* TODO: Define valid log page id ranges in nvme.h? */ 312 } else if (log_page == 0 || 313 (log_page >= 0x04 && log_page <= 0x7F) || 314 (log_page >= 0x80 && log_page <= 0xBF)) { 315 fprintf(stderr, 316 "\"%s\" not valid log page id.\n", 317 optarg); 318 logpage_usage(); 319 } 320 pageflag = true; 321 break; 322 case 'x': 323 hexflag = true; 324 break; 325 } 326 } 327 328 if (!pageflag) { 329 printf("Missing page_id (-p).\n"); 330 logpage_usage(); 331 } 332 333 /* Check that a controller and/or namespace was specified. */ 334 if (optind >= argc) 335 logpage_usage(); 336 337 if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) { 338 ns_specified = true; 339 parse_ns_str(argv[optind], cname, &nsid); 340 open_dev(cname, &fd, 1, 1); 341 } else { 342 ns_specified = false; 343 nsid = 0xffffffff; 344 open_dev(argv[optind], &fd, 1, 1); 345 } 346 347 read_controller_data(fd, &cdata); 348 349 /* 350 * The log page attribtues indicate whether or not the controller 351 * supports the SMART/Health information log page on a per 352 * namespace basis. 353 */ 354 if (ns_specified) { 355 if (log_page != NVME_LOG_HEALTH_INFORMATION) 356 errx(1, "log page %d valid only at controller level", 357 log_page); 358 if (!(cdata.lpa & NVME_ID_CTRLR_LPA_NS_SMART)) 359 errx(1, 360 "controller does not support per namespace " 361 "smart/health information"); 362 } 363 364 print_fn = print_hex; 365 if (!hexflag) { 366 /* 367 * See if there is a pretty print function for the 368 * specified log page. If one isn't found, we 369 * just revert to the default (print_hex). 370 */ 371 f = logfuncs; 372 while (f->log_page > 0) { 373 if (log_page == f->log_page) { 374 print_fn = f->fn; 375 break; 376 } 377 f++; 378 } 379 } 380 381 /* Read the log page */ 382 switch (log_page) { 383 case NVME_LOG_ERROR: 384 size = sizeof(struct nvme_error_information_entry); 385 size *= (cdata.elpe + 1); 386 break; 387 case NVME_LOG_HEALTH_INFORMATION: 388 size = sizeof(struct nvme_health_information_page); 389 break; 390 case NVME_LOG_FIRMWARE_SLOT: 391 size = sizeof(struct nvme_firmware_page); 392 break; 393 default: 394 size = DEFAULT_SIZE; 395 break; 396 } 397 398 buf = get_log_buffer(size); 399 read_logpage(fd, log_page, nsid, buf, size); 400 print_fn(buf, size); 401 402 close(fd); 403 exit(0); 404 } 405