nvmecontrol.c revision 253436
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: head/sbin/nvmecontrol/nvmecontrol.c 253436 2013-07-17 23:14:41Z jimharris $"); 29 30#include <sys/param.h> 31#include <sys/ioccom.h> 32#include <sys/stat.h> 33 34#include <ctype.h> 35#include <err.h> 36#include <errno.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 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 {"logpage", logpage, LOGPAGE_USAGE}, 59 {"firmware", firmware, FIRMWARE_USAGE}, 60 {NULL, NULL, NULL}, 61}; 62 63static void 64usage(void) 65{ 66 struct nvme_function *f; 67 68 f = funcs; 69 fprintf(stderr, "usage:\n"); 70 while (f->name != NULL) { 71 fprintf(stderr, "%s", f->usage); 72 f++; 73 } 74 exit(1); 75} 76 77static void 78print_bytes(void *data, uint32_t length) 79{ 80 uint32_t i, j; 81 uint8_t *p, *end; 82 83 end = (uint8_t *)data + length; 84 85 for (i = 0; i < length; i++) { 86 p = (uint8_t *)data + (i*16); 87 printf("%03x: ", i*16); 88 for (j = 0; j < 16 && p < end; j++) 89 printf("%02x ", *p++); 90 if (p >= end) 91 break; 92 printf("\n"); 93 } 94 printf("\n"); 95} 96 97static void 98print_dwords(void *data, uint32_t length) 99{ 100 uint32_t *p; 101 uint32_t i, j; 102 103 p = (uint32_t *)data; 104 length /= sizeof(uint32_t); 105 106 for (i = 0; i < length; i+=8) { 107 printf("%03x: ", i*4); 108 for (j = 0; j < 8; j++) 109 printf("%08x ", p[i+j]); 110 printf("\n"); 111 } 112 113 printf("\n"); 114} 115 116void 117print_hex(void *data, uint32_t length) 118{ 119 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0) 120 print_dwords(data, length); 121 else 122 print_bytes(data, length); 123} 124 125void 126read_controller_data(int fd, struct nvme_controller_data *cdata) 127{ 128 struct nvme_pt_command pt; 129 130 memset(&pt, 0, sizeof(pt)); 131 pt.cmd.opc = NVME_OPC_IDENTIFY; 132 pt.cmd.cdw10 = 1; 133 pt.buf = cdata; 134 pt.len = sizeof(*cdata); 135 pt.is_read = 1; 136 137 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 138 err(1, "identify request failed"); 139 140 if (nvme_completion_is_error(&pt.cpl)) 141 errx(1, "identify request returned error"); 142} 143 144void 145read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata) 146{ 147 struct nvme_pt_command pt; 148 149 memset(&pt, 0, sizeof(pt)); 150 pt.cmd.opc = NVME_OPC_IDENTIFY; 151 pt.cmd.nsid = nsid; 152 pt.buf = nsdata; 153 pt.len = sizeof(*nsdata); 154 pt.is_read = 1; 155 156 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 157 err(1, "identify request failed"); 158 159 if (nvme_completion_is_error(&pt.cpl)) 160 errx(1, "identify request returned error"); 161} 162 163int 164open_dev(const char *str, int *fd, int show_error, int exit_on_error) 165{ 166 struct stat devstat; 167 char full_path[64]; 168 169 *fd = 0; 170 171 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) { 172 if (show_error) 173 warnx("controller/namespace ids must begin with '%s'", 174 NVME_CTRLR_PREFIX); 175 if (exit_on_error) 176 exit(1); 177 else 178 return (1); 179 } 180 181 snprintf(full_path, sizeof(full_path), "/dev/%s", str); 182 if (stat(full_path, &devstat) != 0) { 183 if (show_error) 184 warn("could not stat %s", full_path); 185 if (exit_on_error) 186 exit(1); 187 else 188 return (1); 189 } 190 191 *fd = open(full_path, O_RDWR); 192 if (*fd < 0) { 193 if (show_error) 194 warn("could not open %s", full_path); 195 if (exit_on_error) 196 exit(1); 197 else 198 return (1); 199 } 200 201 return (0); 202} 203 204void 205parse_ns_str(const char *ns_str, char *ctrlr_str, int *nsid) 206{ 207 char *nsloc; 208 209 /* 210 * Pull the namespace id from the string. +2 skips past the "ns" part 211 * of the string. Don't search past 10 characters into the string, 212 * otherwise we know it is malformed. 213 */ 214 nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10); 215 if (nsloc != NULL) 216 *nsid = strtol(nsloc + 2, NULL, 10); 217 if (nsloc == NULL || (*nsid == 0 && errno != 0)) 218 errx(1, "invalid namespace ID '%s'", ns_str); 219 220 /* 221 * The controller string will include only the nvmX part of the 222 * nvmeXnsY string. 223 */ 224 snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str); 225} 226 227int 228main(int argc, char *argv[]) 229{ 230 struct nvme_function *f; 231 232 if (argc < 2) 233 usage(); 234 235 f = funcs; 236 while (f->name != NULL) { 237 if (strcmp(argv[1], f->name) == 0) 238 f->fn(argc-1, &argv[1]); 239 f++; 240 } 241 242 usage(); 243 244 return (0); 245} 246