Deleted Added
full compact
logpage.c (252302) logpage.c (253109)
1/*-
2 * Copyright (c) 2013 EMC Corp.
3 * All rights reserved.
4 *
5 * Copyright (C) 2012-2013 Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2013 EMC Corp.
3 * All rights reserved.
4 *
5 * Copyright (C) 2012-2013 Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 252302 2013-06-27 10:42:09Z glebius $");
31__FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 253109 2013-07-09 21:14:15Z jimharris $");
32
33#include <sys/param.h>
34#include <sys/ioccom.h>
35
36#include <ctype.h>
32
33#include <sys/param.h>
34#include <sys/ioccom.h>
35
36#include <ctype.h>
37#include <err.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <stdbool.h>
40#include <stddef.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <stdbool.h>
41#include <stddef.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
44#include <sysexits.h>
45#include <unistd.h>
46
47#include "nvmecontrol.h"
48
49#define DEFAULT_SIZE (4096)
50#define MAX_FW_SLOTS (7)
51
52typedef void (*print_fn_t)(void *buf, uint32_t size);
53
54static void *
45#include <unistd.h>
46
47#include "nvmecontrol.h"
48
49#define DEFAULT_SIZE (4096)
50#define MAX_FW_SLOTS (7)
51
52typedef void (*print_fn_t)(void *buf, uint32_t size);
53
54static void *
55get_log_buffer(size_t size)
55get_log_buffer(uint32_t size)
56{
57 void *buf;
58
56{
57 void *buf;
58
59 if ((buf = malloc(size)) == NULL) {
60 fprintf(stderr, "Unable to malloc %zd bytes\n", size);
61 exit(EX_IOERR);
62 }
59 if ((buf = malloc(size)) == NULL)
60 errx(1, "unable to malloc %u bytes", size);
61
63 memset(buf, 0, size);
64 return (buf);
65}
66
67void
68read_logpage(int fd, uint8_t log_page, int nsid, void *payload,
69 uint32_t payload_size)
70{
71 struct nvme_pt_command pt;
72
73 memset(&pt, 0, sizeof(pt));
74 pt.cmd.opc = NVME_OPC_GET_LOG_PAGE;
75 pt.cmd.nsid = nsid;
76 pt.cmd.cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16;
77 pt.cmd.cdw10 |= log_page;
78 pt.buf = payload;
79 pt.len = payload_size;
80 pt.is_read = 1;
81
62 memset(buf, 0, size);
63 return (buf);
64}
65
66void
67read_logpage(int fd, uint8_t log_page, int nsid, void *payload,
68 uint32_t payload_size)
69{
70 struct nvme_pt_command pt;
71
72 memset(&pt, 0, sizeof(pt));
73 pt.cmd.opc = NVME_OPC_GET_LOG_PAGE;
74 pt.cmd.nsid = nsid;
75 pt.cmd.cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16;
76 pt.cmd.cdw10 |= log_page;
77 pt.buf = payload;
78 pt.len = payload_size;
79 pt.is_read = 1;
80
82 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
83 printf("Get log page request failed. errno=%d (%s)\n",
84 errno, strerror(errno));
85 exit(EX_IOERR);
86 }
81 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
82 err(1, "get log page request failed");
87
83
88 if (nvme_completion_is_error(&pt.cpl)) {
89 printf("Passthrough command returned error.\n");
90 exit(EX_IOERR);
91 }
84 if (nvme_completion_is_error(&pt.cpl))
85 errx(1, "get log page request returned error");
92}
93
94static void
95print_log_error(void *buf, uint32_t size)
96{
97 int i, nentries;
98 struct nvme_error_information_entry *entry = buf;
99 struct nvme_status *status;

--- 137 unchanged lines hidden (view full) ---

237 {0, NULL },
238};
239
240static void
241logpage_usage(void)
242{
243 fprintf(stderr, "usage:\n");
244 fprintf(stderr, LOGPAGE_USAGE);
86}
87
88static void
89print_log_error(void *buf, uint32_t size)
90{
91 int i, nentries;
92 struct nvme_error_information_entry *entry = buf;
93 struct nvme_status *status;

--- 137 unchanged lines hidden (view full) ---

231 {0, NULL },
232};
233
234static void
235logpage_usage(void)
236{
237 fprintf(stderr, "usage:\n");
238 fprintf(stderr, LOGPAGE_USAGE);
245 exit(EX_USAGE);
239 exit(1);
246}
247
248void
249logpage(int argc, char *argv[])
250{
251 int fd, nsid, len;
252 int log_page = 0, pageflag = false;
253 int hexflag = false;
254 int allow_ns = false;
255 char ch, *p, *nsloc = NULL;
256 char *cname = NULL;
240}
241
242void
243logpage(int argc, char *argv[])
244{
245 int fd, nsid, len;
246 int log_page = 0, pageflag = false;
247 int hexflag = false;
248 int allow_ns = false;
249 char ch, *p, *nsloc = NULL;
250 char *cname = NULL;
257 size_t size;
251 uint32_t size;
258 void *buf;
259 struct logpage_function *f;
260 struct nvme_controller_data cdata;
261 print_fn_t print_fn;
262
263 while ((ch = getopt(argc, argv, "p:x")) != -1) {
264 switch (ch) {
265 case 'p':

--- 42 unchanged lines hidden (view full) ---

308 read_controller_data(fd, &cdata);
309
310 if (log_page == NVME_LOG_HEALTH_INFORMATION && cdata.lpa.ns_smart != 0)
311 allow_ns = true;
312
313 /* If a namespace id was specified, validate it's use */
314 if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) {
315 if (!allow_ns) {
252 void *buf;
253 struct logpage_function *f;
254 struct nvme_controller_data cdata;
255 print_fn_t print_fn;
256
257 while ((ch = getopt(argc, argv, "p:x")) != -1) {
258 switch (ch) {
259 case 'p':

--- 42 unchanged lines hidden (view full) ---

302 read_controller_data(fd, &cdata);
303
304 if (log_page == NVME_LOG_HEALTH_INFORMATION && cdata.lpa.ns_smart != 0)
305 allow_ns = true;
306
307 /* If a namespace id was specified, validate it's use */
308 if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) {
309 if (!allow_ns) {
316 if (log_page != NVME_LOG_HEALTH_INFORMATION) {
317 fprintf(stderr,
318 "Namespace ID not valid for log page %d.\n",
310 if (log_page != NVME_LOG_HEALTH_INFORMATION)
311 errx(1,
312 "log page %d valid only at controller level",
319 log_page);
313 log_page);
320 } else if (cdata.lpa.ns_smart == 0) {
321 fprintf(stderr,
322 "Controller does not support per "
323 "namespace SMART/Health information.\n");
324 }
325 close(fd);
326 exit(EX_IOERR);
314 else if (cdata.lpa.ns_smart == 0)
315 errx(1,
316 "controller does not support per "
317 "namespace smart/health information");
327 }
328 nsloc = strnstr(argv[optind], NVME_NS_PREFIX, 10);
329 if (nsloc != NULL)
330 nsid = strtol(nsloc + 2, NULL, 10);
318 }
319 nsloc = strnstr(argv[optind], NVME_NS_PREFIX, 10);
320 if (nsloc != NULL)
321 nsid = strtol(nsloc + 2, NULL, 10);
331 if (nsloc == NULL || (nsid == 0 && errno != 0)) {
332 fprintf(stderr,
333 "Invalid namespace ID %s.\n",
334 argv[optind]);
335 close(fd);
336 exit(EX_IOERR);
337 }
322 if (nsloc == NULL || (nsid == 0 && errno != 0))
323 errx(1, "invalid namespace id '%s'", argv[optind]);
338
339 /*
340 * User is asking for per namespace log page information
341 * so close the controller and open up the namespace.
342 */
343 close(fd);
344 open_dev(argv[optind], &fd, 1, 1);
345 } else

--- 33 unchanged lines hidden (view full) ---

379 break;
380 }
381
382 buf = get_log_buffer(size);
383 read_logpage(fd, log_page, nsid, buf, size);
384 print_fn(buf, size);
385
386 close(fd);
324
325 /*
326 * User is asking for per namespace log page information
327 * so close the controller and open up the namespace.
328 */
329 close(fd);
330 open_dev(argv[optind], &fd, 1, 1);
331 } else

--- 33 unchanged lines hidden (view full) ---

365 break;
366 }
367
368 buf = get_log_buffer(size);
369 read_logpage(fd, log_page, nsid, buf, size);
370 print_fn(buf, size);
371
372 close(fd);
387 exit(EX_OK);
373 exit(0);
388}
374}