Home | History | Annotate | Line # | Download | only in nvmectl
wdc.c revision 1.1.10.2
      1  1.1.10.2  pgoyette /*	$NetBSD: wdc.c,v 1.1.10.2 2018/04/22 07:20:16 pgoyette Exp $	*/
      2       1.1    nonaka 
      3       1.1    nonaka /*-
      4       1.1    nonaka  * Copyright (c) 2017 Netflix, Inc
      5       1.1    nonaka  * All rights reserved.
      6       1.1    nonaka  *
      7       1.1    nonaka  * Redistribution and use in source and binary forms, with or without
      8       1.1    nonaka  * modification, are permitted provided that the following conditions
      9       1.1    nonaka  * are met:
     10       1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     11       1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     12       1.1    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    nonaka  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    nonaka  *    documentation and/or other materials provided with the distribution.
     15       1.1    nonaka  *
     16       1.1    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1    nonaka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1    nonaka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1    nonaka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1    nonaka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1    nonaka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1    nonaka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1    nonaka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1    nonaka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1    nonaka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1    nonaka  * SUCH DAMAGE.
     27       1.1    nonaka  */
     28       1.1    nonaka 
     29       1.1    nonaka #include <sys/cdefs.h>
     30       1.1    nonaka #ifndef lint
     31  1.1.10.2  pgoyette __RCSID("$NetBSD: wdc.c,v 1.1.10.2 2018/04/22 07:20:16 pgoyette Exp $");
     32       1.1    nonaka #if 0
     33  1.1.10.2  pgoyette __FBSDID("$FreeBSD: head/sbin/nvmecontrol/wdc.c 329824 2018-02-22 13:32:31Z wma $");
     34       1.1    nonaka #endif
     35       1.1    nonaka #endif
     36       1.1    nonaka 
     37       1.1    nonaka #include <sys/param.h>
     38       1.1    nonaka #include <sys/ioccom.h>
     39       1.1    nonaka #include <sys/endian.h>
     40       1.1    nonaka 
     41       1.1    nonaka #include <ctype.h>
     42       1.1    nonaka #include <err.h>
     43       1.1    nonaka #include <fcntl.h>
     44       1.1    nonaka #include <stddef.h>
     45       1.1    nonaka #include <stdio.h>
     46       1.1    nonaka #include <stdlib.h>
     47       1.1    nonaka #include <string.h>
     48       1.1    nonaka #include <unistd.h>
     49       1.1    nonaka 
     50       1.1    nonaka #include "nvmectl.h"
     51       1.1    nonaka 
     52       1.1    nonaka #define WDC_NVME_TOC_SIZE	8
     53       1.1    nonaka 
     54       1.1    nonaka #define WDC_NVME_CAP_DIAG_OPCODE	0xe6
     55       1.1    nonaka #define WDC_NVME_CAP_DIAG_CMD		0x0000
     56       1.1    nonaka 
     57       1.1    nonaka static void wdc_cap_diag(int argc, char *argv[]);
     58       1.1    nonaka 
     59  1.1.10.1  pgoyette #define WDC_CAP_DIAG_USAGE	"wdc cap-diag [-o path-template]\n"
     60       1.1    nonaka 
     61  1.1.10.1  pgoyette static const struct nvme_function wdc_funcs[] = {
     62       1.1    nonaka 	{"cap-diag",		wdc_cap_diag,		WDC_CAP_DIAG_USAGE},
     63       1.1    nonaka 	{NULL,			NULL,			NULL},
     64       1.1    nonaka };
     65       1.1    nonaka 
     66       1.1    nonaka static void
     67       1.1    nonaka wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix)
     68       1.1    nonaka {
     69       1.1    nonaka 	struct nvm_identify_controller	cdata;
     70       1.1    nonaka 	char sn[sizeof(cdata.sn) + 1];
     71       1.1    nonaka 	char *walker;
     72       1.1    nonaka 
     73       1.1    nonaka 	len -= strlen(buf);
     74       1.1    nonaka 	buf += strlen(buf);
     75       1.1    nonaka 	read_controller_data(fd, &cdata);
     76       1.1    nonaka 	memcpy(sn, cdata.sn, sizeof(cdata.sn));
     77       1.1    nonaka 	walker = sn + sizeof(cdata.sn) - 1;
     78       1.1    nonaka 	while (walker > sn && *walker == ' ')
     79       1.1    nonaka 		walker--;
     80       1.1    nonaka 	*++walker = '\0';
     81       1.1    nonaka 	snprintf(buf, len, "%s%s.bin", sn, suffix);
     82       1.1    nonaka }
     83       1.1    nonaka 
     84       1.1    nonaka static void
     85       1.1    nonaka wdc_get_data(int fd, uint32_t opcode, uint32_t len, uint32_t off, uint32_t cmd,
     86       1.1    nonaka     uint8_t *buffer, size_t buflen)
     87       1.1    nonaka {
     88       1.1    nonaka 	struct nvme_pt_command	pt;
     89       1.1    nonaka 
     90       1.1    nonaka 	memset(&pt, 0, sizeof(pt));
     91       1.1    nonaka 	pt.cmd.opcode = opcode;
     92       1.1    nonaka 	pt.cmd.cdw10 = len / sizeof(uint32_t);	/* - 1 like all the others ??? */
     93       1.1    nonaka 	pt.cmd.cdw11 = off / sizeof(uint32_t);
     94       1.1    nonaka 	pt.cmd.cdw12 = cmd;
     95       1.1    nonaka 	pt.buf = buffer;
     96       1.1    nonaka 	pt.len = buflen;
     97       1.1    nonaka 	pt.is_read = 1;
     98       1.1    nonaka //	printf("opcode %#x cdw10(len) %#x cdw11(offset?) %#x cdw12(cmd/sub) %#x buflen %zd\n",
     99       1.1    nonaka //	    (int)opcode, (int)cdw10, (int)cdw11, (int)cdw12, buflen);
    100       1.1    nonaka 
    101       1.1    nonaka 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
    102       1.1    nonaka 		err(1, "wdc_get_data request failed");
    103       1.1    nonaka 	if (nvme_completion_is_error(&pt.cpl))
    104       1.1    nonaka 		errx(1, "wdc_get_data request returned error");
    105       1.1    nonaka }
    106       1.1    nonaka 
    107       1.1    nonaka static void
    108       1.1    nonaka wdc_do_dump(int fd, char *tmpl, const char *suffix, uint32_t opcode,
    109  1.1.10.2  pgoyette     uint32_t cmd, int len_off)
    110       1.1    nonaka {
    111  1.1.10.2  pgoyette 	int first;
    112       1.1    nonaka 	int fd2;
    113       1.1    nonaka 	uint8_t *buf;
    114       1.1    nonaka 	uint32_t len, offset;
    115  1.1.10.2  pgoyette 	size_t resid;
    116       1.1    nonaka 	long page_size;
    117       1.1    nonaka 
    118       1.1    nonaka 	page_size = sysconf(_SC_PAGESIZE);
    119       1.1    nonaka 	if (page_size <= 0)
    120       1.1    nonaka 		page_size = 4096;
    121       1.1    nonaka 
    122       1.1    nonaka 	wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
    123       1.1    nonaka 
    124       1.1    nonaka 	/* XXX overwrite protection? */
    125  1.1.10.2  pgoyette 	fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    126       1.1    nonaka 	if (fd2 < 0)
    127       1.1    nonaka 		err(1, "open %s", tmpl);
    128       1.1    nonaka 	buf = aligned_alloc(page_size, NVME_MAX_XFER_SIZE);
    129       1.1    nonaka 	if (buf == NULL)
    130       1.1    nonaka 		errx(1, "Can't get buffer to read dump");
    131  1.1.10.2  pgoyette 	offset = 0;
    132  1.1.10.2  pgoyette 	len = NVME_MAX_XFER_SIZE;
    133  1.1.10.2  pgoyette 	first = 1;
    134  1.1.10.2  pgoyette 
    135  1.1.10.2  pgoyette 	do {
    136       1.1    nonaka 		resid = len > NVME_MAX_XFER_SIZE ? NVME_MAX_XFER_SIZE : len;
    137       1.1    nonaka 		wdc_get_data(fd, opcode, resid, offset, cmd, buf, resid);
    138  1.1.10.2  pgoyette 		if (first) {
    139  1.1.10.2  pgoyette 			len = be32dec(buf + len_off);
    140  1.1.10.2  pgoyette 			if (len == 0)
    141  1.1.10.2  pgoyette 				errx(1, "No data for %s", suffix);
    142  1.1.10.2  pgoyette 			if (memcmp("E6LG", buf, 4) != 0)
    143  1.1.10.2  pgoyette 				printf("Expected header of E6LG, found '%4.4s' instead\n",
    144  1.1.10.2  pgoyette 				    buf);
    145  1.1.10.2  pgoyette 			printf("Dumping %d bytes of version %d.%d log to %s\n", len,
    146  1.1.10.2  pgoyette 			    buf[8], buf[9], tmpl);
    147  1.1.10.2  pgoyette 			/*
    148  1.1.10.2  pgoyette 			 * Adjust amount to dump if total dump < 1MB,
    149  1.1.10.2  pgoyette 			 * though it likely doesn't matter to the WDC
    150  1.1.10.2  pgoyette 			 * analysis tools.
    151  1.1.10.2  pgoyette 			 */
    152  1.1.10.2  pgoyette 			if (resid > len)
    153  1.1.10.2  pgoyette 				resid = len;
    154  1.1.10.2  pgoyette 			first = 0;
    155  1.1.10.2  pgoyette 		}
    156  1.1.10.2  pgoyette 		if (write(fd2, buf, resid) != (ssize_t)resid)
    157       1.1    nonaka 			err(1, "write");
    158       1.1    nonaka 		offset += resid;
    159       1.1    nonaka 		len -= resid;
    160  1.1.10.2  pgoyette 	} while (len > 0);
    161       1.1    nonaka 	free(buf);
    162       1.1    nonaka 	close(fd2);
    163       1.1    nonaka }
    164       1.1    nonaka 
    165       1.1    nonaka __dead static void
    166       1.1    nonaka wdc_cap_diag_usage(void)
    167       1.1    nonaka {
    168       1.1    nonaka 	fprintf(stderr, "usage:\n");
    169  1.1.10.1  pgoyette 	fprintf(stderr, "\t%s " WDC_CAP_DIAG_USAGE, getprogname());
    170       1.1    nonaka 	exit(1);
    171       1.1    nonaka }
    172       1.1    nonaka 
    173       1.1    nonaka __dead static void
    174       1.1    nonaka wdc_cap_diag(int argc, char *argv[])
    175       1.1    nonaka {
    176       1.1    nonaka 	char path_tmpl[MAXPATHLEN];
    177       1.1    nonaka 	int ch, fd;
    178       1.1    nonaka 
    179       1.1    nonaka 	path_tmpl[0] = '\0';
    180       1.1    nonaka 	while ((ch = getopt(argc, argv, "o:")) != -1) {
    181       1.1    nonaka 		switch ((char)ch) {
    182       1.1    nonaka 		case 'o':
    183       1.1    nonaka 			strlcpy(path_tmpl, optarg, MAXPATHLEN);
    184       1.1    nonaka 			break;
    185       1.1    nonaka 		default:
    186       1.1    nonaka 			wdc_cap_diag_usage();
    187       1.1    nonaka 		}
    188       1.1    nonaka 	}
    189       1.1    nonaka 	/* Check that a controller was specified. */
    190       1.1    nonaka 	if (optind >= argc)
    191       1.1    nonaka 		wdc_cap_diag_usage();
    192       1.1    nonaka 	open_dev(argv[optind], &fd, 1, 1);
    193       1.1    nonaka 
    194       1.1    nonaka 	wdc_do_dump(fd, path_tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE,
    195  1.1.10.2  pgoyette 	    WDC_NVME_CAP_DIAG_CMD, 4);
    196       1.1    nonaka 
    197       1.1    nonaka 	close(fd);
    198       1.1    nonaka 
    199       1.1    nonaka 	exit(1);
    200       1.1    nonaka }
    201       1.1    nonaka 
    202       1.1    nonaka void
    203       1.1    nonaka wdc(int argc, char *argv[])
    204       1.1    nonaka {
    205       1.1    nonaka 
    206       1.1    nonaka 	dispatch(argc, argv, wdc_funcs);
    207       1.1    nonaka }
    208