nvmecontrol.c revision 252669
1/*- 2 * Copyright (C) 2012-2013 Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/9/sbin/nvmecontrol/nvmecontrol.c 252669 2013-07-04 00:03:30Z jimharris $"); 29 30#include <sys/param.h> 31#include <sys/ioccom.h> 32#include <sys/stat.h> 33 34#include <ctype.h> 35#include <errno.h> 36#include <fcntl.h> 37#include <stdbool.h> 38#include <stddef.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <sysexits.h> 43#include <unistd.h> 44 45#include "nvmecontrol.h" 46 47typedef void (*nvme_fn_t)(int argc, char *argv[]); 48 49static struct nvme_function { 50 const char *name; 51 nvme_fn_t fn; 52 const char *usage; 53} funcs[] = { 54 {"devlist", devlist, DEVLIST_USAGE}, 55 {"identify", identify, IDENTIFY_USAGE}, 56 {"perftest", perftest, PERFTEST_USAGE}, 57 {"reset", reset, RESET_USAGE}, 58 {NULL, NULL, NULL}, 59}; 60 61static void 62usage(void) 63{ 64 struct nvme_function *f; 65 66 f = funcs; 67 fprintf(stderr, "usage:\n"); 68 while (f->name != NULL) { 69 fprintf(stderr, "%s", f->usage); 70 f++; 71 } 72 exit(EX_USAGE); 73} 74 75static void 76print_bytes(void *data, uint32_t length) 77{ 78 uint32_t i, j; 79 uint8_t *p, *end; 80 81 end = (uint8_t *)data + length; 82 83 for (i = 0; i < length; i++) { 84 p = (uint8_t *)data + (i*16); 85 printf("%03x: ", i*16); 86 for (j = 0; j < 16 && p < end; j++) 87 printf("%02x ", *p++); 88 if (p >= end) 89 break; 90 printf("\n"); 91 } 92 printf("\n"); 93} 94 95static void 96print_dwords(void *data, uint32_t length) 97{ 98 uint32_t *p; 99 uint32_t i, j; 100 101 p = (uint32_t *)data; 102 length /= sizeof(uint32_t); 103 104 for (i = 0; i < length; i+=8) { 105 printf("%03x: ", i*4); 106 for (j = 0; j < 8; j++) 107 printf("%08x ", p[i+j]); 108 printf("\n"); 109 } 110 111 printf("\n"); 112} 113 114void 115print_hex(void *data, uint32_t length) 116{ 117 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0) 118 print_dwords(data, length); 119 else 120 print_bytes(data, length); 121} 122 123void 124read_controller_data(int fd, struct nvme_controller_data *cdata) 125{ 126 struct nvme_pt_command pt; 127 128 memset(&pt, 0, sizeof(pt)); 129 pt.cmd.opc = NVME_OPC_IDENTIFY; 130 pt.cmd.cdw10 = 1; 131 pt.buf = cdata; 132 pt.len = sizeof(*cdata); 133 pt.is_read = 1; 134 135 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) { 136 printf("Identify request failed. errno=%d (%s)\n", 137 errno, strerror(errno)); 138 exit(EX_IOERR); 139 } 140 141 if (nvme_completion_is_error(&pt.cpl)) { 142 printf("Passthrough command returned error.\n"); 143 exit(EX_IOERR); 144 } 145} 146 147void 148read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata) 149{ 150 struct nvme_pt_command pt; 151 152 memset(&pt, 0, sizeof(pt)); 153 pt.cmd.opc = NVME_OPC_IDENTIFY; 154 pt.cmd.nsid = nsid; 155 pt.buf = nsdata; 156 pt.len = sizeof(*nsdata); 157 pt.is_read = 1; 158 159 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) { 160 printf("Identify request failed. errno=%d (%s)\n", 161 errno, strerror(errno)); 162 exit(EX_IOERR); 163 } 164 165 if (nvme_completion_is_error(&pt.cpl)) { 166 printf("Passthrough command returned error.\n"); 167 exit(EX_IOERR); 168 } 169} 170 171int 172open_dev(const char *str, int *fd, int show_error, int exit_on_error) 173{ 174 struct stat devstat; 175 char full_path[64]; 176 177 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) { 178 if (show_error) 179 fprintf(stderr, 180 "Controller/namespace IDs must begin with '%s'.\n", 181 NVME_CTRLR_PREFIX); 182 if (exit_on_error) 183 exit(EX_USAGE); 184 else 185 return (EX_USAGE); 186 } 187 188 snprintf(full_path, sizeof(full_path), "/dev/%s", str); 189 if (stat(full_path, &devstat) != 0) { 190 if (show_error) 191 fprintf(stderr, "Could not stat %s. errno=%d (%s)\n", 192 full_path, errno, strerror(errno)); 193 if (exit_on_error) 194 exit(EX_NOINPUT); 195 else 196 return (EX_NOINPUT); 197 } 198 199 *fd = open(full_path, O_RDWR); 200 if (*fd < 0) { 201 if (show_error) 202 fprintf(stderr, "Could not open %s. errno=%d (%s)\n", 203 full_path, errno, strerror(errno)); 204 if (exit_on_error) 205 exit(EX_NOPERM); 206 else 207 return (EX_NOPERM); 208 } 209 210 return (EX_OK); 211} 212 213int 214main(int argc, char *argv[]) 215{ 216 struct nvme_function *f; 217 218 if (argc < 2) 219 usage(); 220 221 f = funcs; 222 while (f->name != NULL) { 223 if (strcmp(argv[1], f->name) == 0) 224 f->fn(argc-1, &argv[1]); 225 f++; 226 } 227 228 usage(); 229 230 return (0); 231} 232 233