nvmecontrol.c revision 253298
1166886Srrs/*- 2166886Srrs * Copyright (C) 2012-2013 Intel Corporation 3166886Srrs * All rights reserved. 4166886Srrs * 5166886Srrs * Redistribution and use in source and binary forms, with or without 6166886Srrs * modification, are permitted provided that the following conditions 7166886Srrs * are met: 8166886Srrs * 1. Redistributions of source code must retain the above copyright 9166886Srrs * notice, this list of conditions and the following disclaimer. 10166886Srrs * 2. Redistributions in binary form must reproduce the above copyright 11166886Srrs * notice, this list of conditions and the following disclaimer in the 12251067Semaste * documentation and/or other materials provided with the distribution. 13166886Srrs * 14166886Srrs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15166886Srrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16166886Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17166886Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18166886Srrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19166886Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20166886Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21166886Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22166886Srrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23166886Srrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24166886Srrs * SUCH DAMAGE. 25166886Srrs */ 26166886Srrs 27166886Srrs#include <sys/cdefs.h> 28166886Srrs__FBSDID("$FreeBSD: stable/9/sbin/nvmecontrol/nvmecontrol.c 253298 2013-07-12 22:12:48Z jimharris $"); 29166886Srrs 30166886Srrs#include <sys/param.h> 31223225Stuexen#include <sys/ioccom.h> 32166886Srrs#include <sys/stat.h> 33166886Srrs 34166886Srrs#include <ctype.h> 35166886Srrs#include <err.h> 36172320Sbrueffer#include <fcntl.h> 37166886Srrs#include <stdbool.h> 38166886Srrs#include <stddef.h> 39166886Srrs#include <stdio.h> 40166886Srrs#include <stdlib.h> 41166886Srrs#include <string.h> 42167017Srrs#include <unistd.h> 43166886Srrs 44202176Sbrueffer#include "nvmecontrol.h" 45166886Srrs 46166886Srrstypedef void (*nvme_fn_t)(int argc, char *argv[]); 47166886Srrs 48166886Srrsstatic struct nvme_function { 49233648Seadler const char *name; 50166886Srrs nvme_fn_t fn; 51166886Srrs const char *usage; 52170919Sbrueffer} funcs[] = { 53170919Sbrueffer {"devlist", devlist, DEVLIST_USAGE}, 54170919Sbrueffer {"identify", identify, IDENTIFY_USAGE}, 55170919Sbrueffer {"perftest", perftest, PERFTEST_USAGE}, 56166886Srrs {"reset", reset, RESET_USAGE}, 57170919Sbrueffer {"logpage", logpage, LOGPAGE_USAGE}, 58170919Sbrueffer {"firmware", firmware, FIRMWARE_USAGE}, 59170919Sbrueffer {NULL, NULL, NULL}, 60166886Srrs}; 61166886Srrs 62170919Sbruefferstatic void 63170919Sbruefferusage(void) 64170919Sbrueffer{ 65170919Sbrueffer struct nvme_function *f; 66170919Sbrueffer 67166886Srrs f = funcs; 68166886Srrs fprintf(stderr, "usage:\n"); 69233648Seadler while (f->name != NULL) { 70166886Srrs fprintf(stderr, "%s", f->usage); 71166886Srrs f++; 72166886Srrs } 73166886Srrs exit(1); 74166886Srrs} 75223225Stuexen 76166886Srrsstatic void 77223225Stuexenprint_bytes(void *data, uint32_t length) 78223225Stuexen{ 79223225Stuexen uint32_t i, j; 80223225Stuexen uint8_t *p, *end; 81223225Stuexen 82223225Stuexen end = (uint8_t *)data + length; 83223225Stuexen 84223225Stuexen for (i = 0; i < length; i++) { 85223225Stuexen p = (uint8_t *)data + (i*16); 86223225Stuexen printf("%03x: ", i*16); 87223225Stuexen for (j = 0; j < 16 && p < end; j++) 88223225Stuexen printf("%02x ", *p++); 89223225Stuexen if (p >= end) 90223225Stuexen break; 91223225Stuexen printf("\n"); 92223225Stuexen } 93223225Stuexen printf("\n"); 94223225Stuexen} 95166886Srrs 96166886Srrsstatic void 97166886Srrsprint_dwords(void *data, uint32_t length) 98166886Srrs{ 99166886Srrs uint32_t *p; 100166886Srrs uint32_t i, j; 101166886Srrs 102203322Sbrucec p = (uint32_t *)data; 103203322Sbrucec length /= sizeof(uint32_t); 104166886Srrs 105166886Srrs for (i = 0; i < length; i+=8) { 106166886Srrs printf("%03x: ", i*4); 107202176Sbrueffer for (j = 0; j < 8; j++) 108166886Srrs printf("%08x ", p[i+j]); 109166886Srrs printf("\n"); 110166886Srrs } 111166886Srrs 112166886Srrs printf("\n"); 113166886Srrs} 114166886Srrs 115166886Srrsvoid 116166886Srrsprint_hex(void *data, uint32_t length) 117166886Srrs{ 118166886Srrs if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0) 119166886Srrs print_dwords(data, length); 120166886Srrs else 121166886Srrs print_bytes(data, length); 122166886Srrs} 123166886Srrs 124166886Srrsvoid 125166886Srrsread_controller_data(int fd, struct nvme_controller_data *cdata) 126166886Srrs{ 127166886Srrs struct nvme_pt_command pt; 128166886Srrs 129170919Sbrueffer memset(&pt, 0, sizeof(pt)); 130223225Stuexen pt.cmd.opc = NVME_OPC_IDENTIFY; 131223225Stuexen pt.cmd.cdw10 = 1; 132223225Stuexen pt.buf = cdata; 133223225Stuexen pt.len = sizeof(*cdata); 134223225Stuexen pt.is_read = 1; 135223225Stuexen 136223225Stuexen if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 137223225Stuexen err(1, "identify request failed"); 138223225Stuexen 139223225Stuexen if (nvme_completion_is_error(&pt.cpl)) 140223225Stuexen errx(1, "identify request returned error"); 141} 142 143void 144read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata) 145{ 146 struct nvme_pt_command pt; 147 148 memset(&pt, 0, sizeof(pt)); 149 pt.cmd.opc = NVME_OPC_IDENTIFY; 150 pt.cmd.nsid = nsid; 151 pt.buf = nsdata; 152 pt.len = sizeof(*nsdata); 153 pt.is_read = 1; 154 155 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 156 err(1, "identify request failed"); 157 158 if (nvme_completion_is_error(&pt.cpl)) 159 errx(1, "identify request returned error"); 160} 161 162int 163open_dev(const char *str, int *fd, int show_error, int exit_on_error) 164{ 165 struct stat devstat; 166 char full_path[64]; 167 168 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) { 169 if (show_error) 170 warnx("controller/namespace ids must begin with '%s'", 171 NVME_CTRLR_PREFIX); 172 if (exit_on_error) 173 exit(1); 174 else 175 return (1); 176 } 177 178 snprintf(full_path, sizeof(full_path), "/dev/%s", str); 179 if (stat(full_path, &devstat) != 0) { 180 if (show_error) 181 warn("could not stat %s", full_path); 182 if (exit_on_error) 183 exit(1); 184 else 185 return (1); 186 } 187 188 *fd = open(full_path, O_RDWR); 189 if (*fd < 0) { 190 if (show_error) 191 warn("could not open %s", full_path); 192 if (exit_on_error) 193 exit(1); 194 else 195 return (1); 196 } 197 198 return (0); 199} 200 201int 202main(int argc, char *argv[]) 203{ 204 struct nvme_function *f; 205 206 if (argc < 2) 207 usage(); 208 209 f = funcs; 210 while (f->name != NULL) { 211 if (strcmp(argv[1], f->name) == 0) 212 f->fn(argc-1, &argv[1]); 213 f++; 214 } 215 216 usage(); 217 218 return (0); 219} 220 221