nvmecontrol.c revision 253109
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 253109 2013-07-09 21:14:15Z 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 <fcntl.h> 37#include <stdbool.h> 38#include <stddef.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43 44#include "nvmecontrol.h" 45 46typedef void (*nvme_fn_t)(int argc, char *argv[]); 47 48static struct nvme_function { 49 const char *name; 50 nvme_fn_t fn; 51 const char *usage; 52} funcs[] = { 53 {"devlist", devlist, DEVLIST_USAGE}, 54 {"identify", identify, IDENTIFY_USAGE}, 55 {"perftest", perftest, PERFTEST_USAGE}, 56 {"reset", reset, RESET_USAGE}, 57 {"logpage", logpage, LOGPAGE_USAGE}, 58 {"firmware", firmware, FIRMWARE_USAGE}, 59 {NULL, NULL, NULL}, 60}; 61 62static void 63usage(void) 64{ 65 struct nvme_function *f; 66 67 f = funcs; 68 fprintf(stderr, "usage:\n"); 69 while (f->name != NULL) { 70 fprintf(stderr, "%s", f->usage); 71 f++; 72 } 73 exit(1); 74} 75 76static void 77print_bytes(void *data, uint32_t length) 78{ 79 uint32_t i, j; 80 uint8_t *p, *end; 81 82 end = (uint8_t *)data + length; 83 84 for (i = 0; i < length; i++) { 85 p = (uint8_t *)data + (i*16); 86 printf("%03x: ", i*16); 87 for (j = 0; j < 16 && p < end; j++) 88 printf("%02x ", *p++); 89 if (p >= end) 90 break; 91 printf("\n"); 92 } 93 printf("\n"); 94} 95 96static void 97print_dwords(void *data, uint32_t length) 98{ 99 uint32_t *p; 100 uint32_t i, j; 101 102 p = (uint32_t *)data; 103 length /= sizeof(uint32_t); 104 105 for (i = 0; i < length; i+=8) { 106 printf("%03x: ", i*4); 107 for (j = 0; j < 8; j++) 108 printf("%08x ", p[i+j]); 109 printf("\n"); 110 } 111 112 printf("\n"); 113} 114 115void 116print_hex(void *data, uint32_t length) 117{ 118 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0) 119 print_dwords(data, length); 120 else 121 print_bytes(data, length); 122} 123 124void 125read_controller_data(int fd, struct nvme_controller_data *cdata) 126{ 127 struct nvme_pt_command pt; 128 129 memset(&pt, 0, sizeof(pt)); 130 pt.cmd.opc = NVME_OPC_IDENTIFY; 131 pt.cmd.cdw10 = 1; 132 pt.buf = cdata; 133 pt.len = sizeof(*cdata); 134 pt.is_read = 1; 135 136 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 137 err(1, "identify request failed"); 138 139 if (nvme_completion_is_error(&pt.cpl)) 140 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