nvmecontrol.c (252302) | nvmecontrol.c (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 --- 11 unchanged lines hidden (view full) --- 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> | 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 --- 11 unchanged lines hidden (view full) --- 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 252302 2013-06-27 10:42:09Z glebius $"); | 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> | 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> | 35#include |
36#include <fcntl.h> 37#include <stdbool.h> 38#include <stddef.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.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; --- 15 unchanged lines hidden (view full) --- 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 } | 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; --- 15 unchanged lines hidden (view full) --- 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 } |
74 exit(EX_USAGE); | 73 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 --- 46 unchanged lines hidden (view full) --- 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 | 74} 75 76static void 77print_bytes(void *data, uint32_t length) 78{ 79 uint32_t i, j; 80 uint8_t *p, *end; 81 --- 46 unchanged lines hidden (view full) --- 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 |
137 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) { 138 printf("Identify request failed. errno=%d (%s)\n", 139 errno, strerror(errno)); 140 exit(EX_IOERR); 141 } | 136 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 137 err(1, "identify request failed"); |
142 | 138 |
143 if (nvme_completion_is_error(&pt.cpl)) { 144 printf("Passthrough command returned error.\n"); 145 exit(EX_IOERR); 146 } | 139 if (nvme_completion_is_error(&pt.cpl)) 140 errx(1, "identify request returned error"); |
147} 148 149void 150read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata) 151{ 152 struct nvme_pt_command pt; 153 154 memset(&pt, 0, sizeof(pt)); 155 pt.cmd.opc = NVME_OPC_IDENTIFY; 156 pt.cmd.nsid = nsid; 157 pt.buf = nsdata; 158 pt.len = sizeof(*nsdata); 159 pt.is_read = 1; 160 | 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 |
161 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) { 162 printf("Identify request failed. errno=%d (%s)\n", 163 errno, strerror(errno)); 164 exit(EX_IOERR); 165 } | 155 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 156 err(1, "identify request failed"); |
166 | 157 |
167 if (nvme_completion_is_error(&pt.cpl)) { 168 printf("Passthrough command returned error.\n"); 169 exit(EX_IOERR); 170 } | 158 if (nvme_completion_is_error(&pt.cpl)) 159 errx(1, "identify request returned error"); |
171} 172 173int 174open_dev(const char *str, int *fd, int show_error, int exit_on_error) 175{ 176 struct stat devstat; 177 char full_path[64]; 178 179 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) { 180 if (show_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) |
181 fprintf(stderr, 182 "Controller/namespace IDs must begin with '%s'.\n", | 170 warnx("controller/namespace ids must begin with '%s'", |
183 NVME_CTRLR_PREFIX); 184 if (exit_on_error) | 171 NVME_CTRLR_PREFIX); 172 if (exit_on_error) |
185 exit(EX_USAGE); | 173 exit(1); |
186 else | 174 else |
187 return (EX_USAGE); | 175 return (1); |
188 } 189 190 snprintf(full_path, sizeof(full_path), "/dev/%s", str); 191 if (stat(full_path, &devstat) != 0) { 192 if (show_error) | 176 } 177 178 snprintf(full_path, sizeof(full_path), "/dev/%s", str); 179 if (stat(full_path, &devstat) != 0) { 180 if (show_error) |
193 fprintf(stderr, "Could not stat %s. errno=%d (%s)\n", 194 full_path, errno, strerror(errno)); | 181 warn("could not stat %s", full_path); |
195 if (exit_on_error) | 182 if (exit_on_error) |
196 exit(EX_NOINPUT); | 183 exit(1); |
197 else | 184 else |
198 return (EX_NOINPUT); | 185 return (1); |
199 } 200 201 *fd = open(full_path, O_RDWR); 202 if (*fd < 0) { 203 if (show_error) | 186 } 187 188 *fd = open(full_path, O_RDWR); 189 if (*fd < 0) { 190 if (show_error) |
204 fprintf(stderr, "Could not open %s. errno=%d (%s)\n", 205 full_path, errno, strerror(errno)); | 191 warn("could not open %s", full_path); |
206 if (exit_on_error) | 192 if (exit_on_error) |
207 exit(EX_NOPERM); | 193 exit(1); |
208 else | 194 else |
209 return (EX_NOPERM); | 195 return (1); |
210 } 211 | 196 } 197 |
212 return (EX_OK); | 198 return (0); |
213} 214 215int 216main(int argc, char *argv[]) 217{ 218 struct nvme_function *f; 219 220 if (argc < 2) --- 13 unchanged lines hidden --- | 199} 200 201int 202main(int argc, char *argv[]) 203{ 204 struct nvme_function *f; 205 206 if (argc < 2) --- 13 unchanged lines hidden --- |