forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.c
9980 lines (8511 loc) · 258 KB
/
nvme.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* NVM-Express command line utility.
*
* Copyright (c) 2014-2015, Intel Corporation.
*
* Written by Keith Busch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* This program uses NVMe IOCTLs to run native nvme commands to a device.
*/
#include "config.h"
#include "nvme/tree.h"
#include "nvme/types.h"
#include "util/cleanup.h"
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <dirent.h>
#include <libgen.h>
#include <signal.h>
#include <linux/fs.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#if HAVE_SYS_RANDOM
#include <sys/random.h>
#endif
#include <libnvme.h>
#include "common.h"
#include "nvme.h"
#include "nvme-print.h"
#include "plugin.h"
#include "util/base64.h"
#include "util/crc32.h"
#include "nvme-wrap.h"
#include "util/argconfig.h"
#include "util/suffix.h"
#include "util/logging.h"
#include "fabrics.h"
#define CREATE_CMD
#include "nvme-builtin.h"
#include "malloc.h"
struct feat_cfg {
enum nvme_features_id feature_id;
__u32 namespace_id;
enum nvme_get_features_sel sel;
__u32 cdw11;
__u32 cdw12;
__u8 uuid_index;
__u32 data_len;
bool raw_binary;
bool human_readable;
bool changed;
};
struct passthru_config {
__u8 opcode;
__u8 flags;
__u16 rsvd;
__u32 namespace_id;
__u32 data_len;
__u32 metadata_len;
__u32 cdw2;
__u32 cdw3;
__u32 cdw10;
__u32 cdw11;
__u32 cdw12;
__u32 cdw13;
__u32 cdw14;
__u32 cdw15;
char *input_file;
char *metadata;
bool raw_binary;
bool show_command;
bool dry_run;
bool read;
bool write;
__u8 prefill;
bool latency;
};
struct get_reg_config {
int offset;
bool human_readable;
bool cap;
bool vs;
bool intms;
bool intmc;
bool cc;
bool csts;
bool nssr;
bool aqa;
bool asq;
bool acq;
bool cmbloc;
bool cmbsz;
bool bpinfo;
bool bprsel;
bool bpmbl;
bool cmbmsc;
bool cmbsts;
bool cmbebs;
bool cmbswtp;
bool nssd;
bool crto;
bool pmrcap;
bool pmrctl;
bool pmrsts;
bool pmrebs;
bool pmrswtp;
bool pmrmscl;
bool pmrmscu;
bool fabrics;
};
struct set_reg_config {
int offset;
bool mmio32;
__u64 value;
__u32 intms;
__u32 intmc;
__u32 cc;
__u32 csts;
__u32 nssr;
__u32 aqa;
__u64 asq;
__u64 acq;
__u32 bprsel;
__u64 bpmbl;
__u64 cmbmsc;
__u32 nssd;
__u32 pmrctl;
__u32 pmrmscl;
__u32 pmrmscu;
};
static const char nvme_version_string[] = NVME_VERSION;
static struct plugin builtin = {
.commands = commands,
.name = NULL,
.desc = NULL,
.next = NULL,
.tail = &builtin,
};
static struct program nvme = {
.name = "nvme",
.version = nvme_version_string,
.usage = "<command> [<device>] [<args>]",
.desc = "The '<device>' may be either an NVMe character "
"device (ex: /dev/nvme0), an nvme block device "
"(ex: /dev/nvme0n1), or a mctp address in the form "
"mctp:<net>,<eid>[:ctrl-id]",
.extensions = &builtin,
};
const char *output_format = "Output format: normal|json|binary";
const char *timeout = "timeout value, in milliseconds";
const char *verbose = "Increase output verbosity";
static const char *app_tag = "app tag for end-to-end PI";
static const char *app_tag_mask = "app tag mask for end-to-end PI";
static const char *block_count = "number of blocks (zeroes based) on device to access";
static const char *crkey = "current reservation key";
static const char *csi = "command set identifier";
static const char *buf_len = "buffer len (if) data is sent or received";
static const char *domainid = "Domain Identifier";
static const char *doper = "directive operation";
static const char *dry = "show command instead of sending";
static const char *dspec_w_dtype = "directive specification associated with directive type";
static const char *dtype = "directive type";
static const char *force_unit_access = "force device to commit data before command completes";
static const char *human_readable_directive = "show directive in readable format";
static const char *human_readable_identify = "show identify in readable format";
static const char *human_readable_info = "show info in readable format";
static const char *human_readable_log = "show log in readable format";
static const char *iekey = "ignore existing res. key";
static const char *latency = "output latency statistics";
static const char *lba_format_index = "The index into the LBA Format list\n"
"identifying the LBA Format capabilities that are to be returned";
static const char *limited_retry = "limit media access attempts";
static const char *lsp = "log specific field";
static const char *mos = "management operation specific";
static const char *mo = "management operation";
static const char *namespace_desired = "desired namespace";
static const char *namespace_id_desired = "identifier of desired namespace";
static const char *namespace_id_optional = "optional namespace attached to controller";
static const char *nssf = "NVMe Security Specific Field";
static const char *prinfo = "PI and check field";
static const char *rae = "Retain an Asynchronous Event";
static const char *raw_directive = "show directive in binary format";
static const char *raw_dump = "dump output in binary format";
static const char *raw_identify = "show identify in binary format";
static const char *raw_log = "show log in binary format";
static const char *raw_output = "output in binary format";
static const char *ref_tag = "reference tag for end-to-end PI";
static const char *raw_use = "use binary output";
static const char *rtype = "reservation type";
static const char *secp = "security protocol (cf. SPC-4)";
static const char *spsp = "security-protocol-specific (cf. SPC-4)";
static const char *start_block = "64-bit LBA of first block to access";
static const char *storage_tag = "storage tag for end-to-end PI";
static const char *uuid_index = "UUID index";
static const char *uuid_index_specify = "specify uuid index";
static const char dash[51] = {[0 ... 49] = '=', '\0'};
static const char space[51] = {[0 ... 49] = ' ', '\0'};
static const char *offset = "offset of the requested register";
static const char *intms = "INTMS=0xc register offset";
static const char *intmc = "INTMC=0x10 register offset";
static const char *cc = "CC=0x14 register offset";
static const char *csts = "CSTS=0x1c register offset";
static const char *nssr = "NSSR=0x20 register offset";
static const char *aqa = "AQA=0x24 register offset";
static const char *asq = "ASQ=0x28 register offset";
static const char *acq = "ACQ=0x30 register offset";
static const char *bprsel = "BPRSEL=0x44 register offset";
static const char *bpmbl = "BPMBL=0x48 register offset";
static const char *cmbmsc = "CMBMSC=0x50 register offset";
static const char *nssd = "NSSD=0x64 register offset";
static const char *pmrctl = "PMRCTL=0xe04 register offset";
static const char *pmrmscl = "PMRMSCL=0xe14 register offset";
static const char *pmrmscu = "PMRMSCU=0xe18 register offset";
struct nvme_config nvme_cfg = {
.output_format = "normal",
};
static void *mmap_registers(struct nvme_dev *dev, bool writable);
const char *nvme_strerror(int errnum)
{
if (errnum >= ENVME_CONNECT_RESOLVE)
return nvme_errno_to_string(errnum);
return strerror(errnum);
}
static ssize_t getrandom_bytes(void *buf, size_t buflen)
{
#if HAVE_SYS_RANDOM
return getrandom(buf, buflen, GRND_NONBLOCK);
#else
ssize_t result;
int fd, err = 0;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return fd;
result = read(fd, buf, buflen);
if (result < 0)
err = errno;
close(fd);
errno = err;
return result;
#endif
}
static bool is_chardev(struct nvme_dev *dev)
{
return S_ISCHR(dev->direct.stat.st_mode);
}
static bool is_blkdev(struct nvme_dev *dev)
{
return S_ISBLK(dev->direct.stat.st_mode);
}
static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags)
{
struct nvme_dev *dev;
int err;
dev = calloc(1, sizeof(*dev));
if (!dev)
return -1;
dev->type = NVME_DEV_DIRECT;
dev->name = basename(devstr);
err = open(devstr, flags);
if (err < 0) {
nvme_show_perror(devstr);
goto err_free;
}
dev->direct.fd = err;
err = fstat(dev_fd(dev), &dev->direct.stat);
if (err < 0) {
nvme_show_perror(devstr);
goto err_close;
}
if (!is_chardev(dev) && !is_blkdev(dev)) {
nvme_show_error("%s is not a block or character device", devstr);
errno = ENODEV;
err = -1;
goto err_close;
}
*devp = dev;
return 0;
err_close:
close(dev_fd(dev));
err_free:
free(dev);
return err;
}
static int parse_mi_dev(char *dev, unsigned int *net, uint8_t *eid,
unsigned int *ctrl)
{
int rc;
/* <net>,<eid>:<ctrl-id> form */
rc = sscanf(dev, "mctp:%u,%hhu:%u", net, eid, ctrl);
if (rc == 3)
return 0;
/* <net>,<eid> form, implicit ctrl-id = 0 */
*ctrl = 0;
rc = sscanf(dev, "mctp:%u,%hhu", net, eid);
if (rc == 2)
return 0;
return -1;
}
static int open_dev_mi_mctp(struct nvme_dev **devp, char *devstr)
{
unsigned int net, ctrl_id;
struct nvme_dev *dev;
unsigned char eid;
int rc;
rc = parse_mi_dev(devstr, &net, &eid, &ctrl_id);
if (rc) {
nvme_show_error("invalid device specifier '%s'", devstr);
return rc;
}
dev = calloc(1, sizeof(*dev));
if (!dev)
return -1;
dev->type = NVME_DEV_MI;
dev->name = devstr;
/* todo: verbose argument */
dev->mi.root = nvme_mi_create_root(stderr, LOG_WARNING);
if (!dev->mi.root)
goto err_free;
dev->mi.ep = nvme_mi_open_mctp(dev->mi.root, net, eid);
if (!dev->mi.ep)
goto err_free_root;
dev->mi.ctrl = nvme_mi_init_ctrl(dev->mi.ep, ctrl_id);
if (!dev->mi.ctrl)
goto err_close_ep;
*devp = dev;
return 0;
err_close_ep:
nvme_mi_close(dev->mi.ep);
err_free_root:
nvme_mi_free_root(dev->mi.root);
err_free:
free(dev);
return -1;
}
static int check_arg_dev(int argc, char **argv)
{
if (optind >= argc) {
errno = EINVAL;
nvme_show_perror(argv[0]);
return -EINVAL;
}
return 0;
}
static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags)
{
char *devname;
int ret;
ret = check_arg_dev(argc, argv);
if (ret)
return ret;
devname = argv[optind];
errno = ENXIO;
if (!strncmp(devname, "mctp:", strlen("mctp:")))
ret = open_dev_mi_mctp(dev, devname);
else
ret = open_dev_direct(dev, devname, flags);
return ret ? -errno : 0;
}
static int parse_args(int argc, char *argv[], const char *desc,
struct argconfig_commandline_options *opts)
{
int ret;
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
log_level = map_log_level(nvme_cfg.verbose, false);
nvme_init_default_logging(stderr, log_level, false, false);
return 0;
}
int parse_and_open(struct nvme_dev **dev, int argc, char **argv,
const char *desc,
struct argconfig_commandline_options *opts)
{
int ret;
ret = parse_args(argc, argv, desc, opts);
if (ret)
return ret;
ret = get_dev(dev, argc, argv, O_RDONLY);
if (ret < 0)
argconfig_print_help(desc, opts);
return ret;
}
int open_exclusive(struct nvme_dev **dev, int argc, char **argv,
int ignore_exclusive)
{
int flags = O_RDONLY;
if (!ignore_exclusive)
flags |= O_EXCL;
return get_dev(dev, argc, argv, flags);
}
int validate_output_format(const char *format, nvme_print_flags_t *flags)
{
nvme_print_flags_t f;
if (!format)
return -EINVAL;
if (!strcmp(format, "normal"))
f = NORMAL;
else if (!strcmp(format, "json"))
f = JSON;
else if (!strcmp(format, "binary"))
f = BINARY;
else
return -EINVAL;
*flags = f;
return 0;
}
bool nvme_is_output_format_json(void)
{
nvme_print_flags_t flags;
if (validate_output_format(nvme_cfg.output_format, &flags))
return false;
return flags == JSON;
}
void dev_close(struct nvme_dev *dev)
{
switch (dev->type) {
case NVME_DEV_DIRECT:
close(dev_fd(dev));
break;
case NVME_DEV_MI:
nvme_mi_close(dev->mi.ep);
nvme_mi_free_root(dev->mi.root);
break;
}
free(dev);
}
static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve SMART log for the given device "
"(or optionally a namespace) in either decoded format "
"(default) or binary.";
_cleanup_free_ struct nvme_smart_log *smart_log = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
const char *namespace = "(optional) desired namespace";
nvme_print_flags_t flags;
int err = -1;
struct config {
__u32 namespace_id;
bool raw_binary;
bool human_readable;
};
struct config cfg = {
.namespace_id = NVME_NSID_ALL,
.raw_binary = false,
.human_readable = false,
};
NVME_ARGS(opts,
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw_output),
OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info));
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(nvme_cfg.output_format, &flags);
if (err < 0) {
nvme_show_error("Invalid output format");
return err;
}
if (cfg.raw_binary)
flags = BINARY;
if (cfg.human_readable || argconfig_parse_seen(opts, "verbose"))
flags |= VERBOSE;
smart_log = nvme_alloc(sizeof(*smart_log));
if (!smart_log)
return -ENOMEM;
err = nvme_cli_get_log_smart(dev, cfg.namespace_id, false,
smart_log);
if (!err)
nvme_show_smart_log(smart_log, cfg.namespace_id,
dev->name, flags);
else if (err > 0)
nvme_show_status(err);
else
nvme_show_error("smart log: %s", nvme_strerror(errno));
return err;
}
static int get_ana_log(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
const char *desc = "Retrieve ANA log for the given device in "
"decoded format (default), json or binary.";
const char *groups = "Return ANA groups only.";
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
_cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
_cleanup_free_ struct nvme_ana_log *ana_log = NULL;
size_t max_ana_log_len;
__u32 ana_log_len;
nvme_print_flags_t flags;
int err = -1;
struct config {
bool groups;
};
struct config cfg = {
.groups = false,
};
NVME_ARGS(opts,
OPT_FLAG("groups", 'g', &cfg.groups, groups));
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(nvme_cfg.output_format, &flags);
if (err < 0) {
nvme_show_error("Invalid output format");
return err;
}
ctrl = nvme_alloc(sizeof(*ctrl));
if (!ctrl)
return -ENOMEM;
err = nvme_cli_identify_ctrl(dev, ctrl);
if (err) {
nvme_show_error("ERROR : nvme_identify_ctrl() failed: %s",
nvme_strerror(errno));
return err;
}
max_ana_log_len = nvme_get_ana_log_len_from_id_ctrl(ctrl, cfg.groups);
ana_log_len = max_ana_log_len;
if (ana_log_len < max_ana_log_len) {
nvme_show_error("ANA log length %zu too large", max_ana_log_len);
return -ENOMEM;
}
ana_log = nvme_alloc(ana_log_len);
if (!ana_log)
return -ENOMEM;
err = nvme_cli_get_ana_log_atomic(dev, cfg.groups, true, 10,
ana_log, &ana_log_len);
if (!err)
nvme_show_ana_log(ana_log, dev->name, ana_log_len, flags);
else if (err > 0)
nvme_show_status(err);
else
nvme_show_error("ana-log: %s", nvme_strerror(errno));
return err;
}
static int parse_telemetry_da(struct nvme_dev *dev,
enum nvme_telemetry_da da,
struct nvme_telemetry_log *telem,
size_t *size)
{
_cleanup_free_ struct nvme_id_ctrl *id_ctrl = NULL;
size_t dalb, da1lb = le16_to_cpu(telem->dalb1), da2lb = le16_to_cpu(telem->dalb2),
da3lb = le16_to_cpu(telem->dalb3), da4lb = le32_to_cpu(telem->dalb4);
bool data_area_4_support;
id_ctrl = nvme_alloc(sizeof(*id_ctrl));
if (!id_ctrl)
return -ENOMEM;
if (nvme_cli_identify_ctrl(dev, id_ctrl)) {
perror("identify-ctrl");
return -errno;
}
data_area_4_support = id_ctrl->lpa & 0x40;
switch (da) {
case NVME_TELEMETRY_DA_CTRL_DETERMINE:
if (data_area_4_support)
dalb = da4lb;
else
dalb = da3lb;
break;
case NVME_TELEMETRY_DA_1:
dalb = da1lb;
break;
case NVME_TELEMETRY_DA_2:
dalb = da2lb;
break;
case NVME_TELEMETRY_DA_3:
/* dalb3 >= dalb2 >= dalb1 */
dalb = da3lb;
break;
case NVME_TELEMETRY_DA_4:
if (data_area_4_support) {
dalb = da4lb;
} else {
nvme_show_error(
"Data area 4 unsupported, bit 6 of Log Page Attributes not set");
return -EINVAL;
}
break;
default:
nvme_show_error("Invalid data area parameter - %d", da);
return -EINVAL;
}
if (dalb == 0) {
nvme_show_error("ERROR: No telemetry data block");
return -ENOENT;
}
*size = (dalb + 1) * NVME_LOG_TELEM_BLOCK_SIZE;
return 0;
}
static int get_log_telemetry_ctrl(struct nvme_dev *dev, bool rae, size_t size,
struct nvme_telemetry_log **buf)
{
struct nvme_telemetry_log *log;
int err;
log = nvme_alloc(size);
if (!log)
return -errno;
err = nvme_cli_get_log_telemetry_ctrl(dev, rae, 0, size, log);
if (err) {
free(log);
if (errno)
return -errno;
else
return err;
}
*buf = log;
return 0;
}
static int get_log_telemetry_host(struct nvme_dev *dev, size_t size,
struct nvme_telemetry_log **buf)
{
struct nvme_telemetry_log *log;
int err;
log = nvme_alloc(size);
if (!log)
return -errno;
err = nvme_cli_get_log_telemetry_host(dev, 0, size, log);
if (err) {
free(log);
if (errno)
return -errno;
else
return err;
}
*buf = log;
return 0;
}
static int __create_telemetry_log_host(struct nvme_dev *dev,
enum nvme_telemetry_da da,
size_t *size,
struct nvme_telemetry_log **buf)
{
_cleanup_free_ struct nvme_telemetry_log *log = NULL;
int err;
log = nvme_alloc(sizeof(*log));
if (!log)
return -ENOMEM;
err = nvme_cli_get_log_create_telemetry_host_mcda(dev, da, log);
if (err) {
if (errno)
return -errno;
else
return err;
}
err = parse_telemetry_da(dev, da, log, size);
if (err)
return err;
return get_log_telemetry_host(dev, *size, buf);
}
static int __get_telemetry_log_ctrl(struct nvme_dev *dev,
bool rae,
enum nvme_telemetry_da da,
size_t *size,
struct nvme_telemetry_log **buf)
{
struct nvme_telemetry_log *log;
int err;
log = nvme_alloc(NVME_LOG_TELEM_BLOCK_SIZE);
if (!log)
return -errno;
/*
* set rae = true so it won't clear the current telemetry log in
* controller
*/
err = nvme_cli_get_log_telemetry_ctrl(dev, true, 0,
NVME_LOG_TELEM_BLOCK_SIZE,
log);
if (err)
goto free;
if (!log->ctrlavail) {
if (!rae) {
err = nvme_cli_get_log_telemetry_ctrl(dev, rae, 0,
NVME_LOG_TELEM_BLOCK_SIZE,
log);
goto free;
}
*size = NVME_LOG_TELEM_BLOCK_SIZE;
*buf = log;
printf("Warning: Telemetry Controller-Initiated Data Not Available.\n");
return 0;
}
err = parse_telemetry_da(dev, da, log, size);
if (err)
goto free;
return get_log_telemetry_ctrl(dev, rae, *size, buf);
free:
free(log);
return err;
}
static int __get_telemetry_log_host(struct nvme_dev *dev,
enum nvme_telemetry_da da,
size_t *size,
struct nvme_telemetry_log **buf)
{
_cleanup_free_ struct nvme_telemetry_log *log = NULL;
int err;
log = nvme_alloc(sizeof(*log));
if (!log)
return -errno;
err = nvme_cli_get_log_telemetry_host(dev, 0,
NVME_LOG_TELEM_BLOCK_SIZE,
log);
if (err)
return err;
err = parse_telemetry_da(dev, da, log, size);
if (err)
return err;
return get_log_telemetry_host(dev, *size, buf);
}
static int get_telemetry_log(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
const char *desc = "Retrieve telemetry log and write to binary file";
const char *fname = "File name to save raw binary, includes header";
const char *hgen = "Have the host tell the controller to generate the report";
const char *cgen = "Gather report generated by the controller.";
const char *dgen = "Pick which telemetry data area to report. Default is 3 to fetch areas 1-3. Valid options are 1, 2, 3, 4.";
const char *mcda = "Host-init Maximum Created Data Area. Valid options are 0 ~ 4 "
"If given, This option will override dgen. 0 : controller determines data area";
_cleanup_free_ struct nvme_telemetry_log *log = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
_cleanup_fd_ int output = -1;
int err = 0;
size_t total_size;
__u8 *data_ptr = NULL;
int data_written = 0, data_remaining = 0;
struct config {
char *file_name;
__u32 host_gen;
bool ctrl_init;
int data_area;
bool rae;
__u8 mcda;
};
struct config cfg = {
.file_name = NULL,
.host_gen = 1,
.ctrl_init = false,
.data_area = 3,
.rae = false,
.mcda = 0xff,
};
NVME_ARGS(opts,
OPT_FILE("output-file", 'O', &cfg.file_name, fname),
OPT_UINT("host-generate", 'g', &cfg.host_gen, hgen),
OPT_FLAG("controller-init", 'c', &cfg.ctrl_init, cgen),
OPT_UINT("data-area", 'd', &cfg.data_area, dgen),
OPT_FLAG("rae", 'r', &cfg.rae, rae),
OPT_BYTE("mcda", 'm', &cfg.mcda, mcda));
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
if (!cfg.file_name) {
nvme_show_error("Please provide an output file!");
return -EINVAL;
}
cfg.host_gen = !!cfg.host_gen;
if (cfg.mcda != 0xff) {
if (cfg.ctrl_init || !cfg.host_gen) {
nvme_show_error("mcda allowed for Host-init Creation!");
return -EINVAL;
}
cfg.data_area = cfg.mcda;
}
output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output < 0) {
nvme_show_error("Failed to open output file %s: %s!",
cfg.file_name, strerror(errno));
return output;
}
log = nvme_alloc(sizeof(*log));
if (!log)
return -ENOMEM;
if (cfg.ctrl_init)
err = __get_telemetry_log_ctrl(dev, cfg.rae, cfg.data_area,
&total_size, &log);
else if (cfg.host_gen)
err = __create_telemetry_log_host(dev, cfg.data_area,
&total_size, &log);
else
err = __get_telemetry_log_host(dev, cfg.data_area,
&total_size, &log);
if (err < 0) {
nvme_show_error("get-telemetry-log: %s", nvme_strerror(errno));
return err;
} else if (err > 0) {
nvme_show_status(err);
fprintf(stderr, "Failed to acquire telemetry log %d!\n", err);
return err;
}
data_written = 0;
data_remaining = total_size;
data_ptr = (__u8 *)log;
while (data_remaining) {
data_written = write(output, data_ptr, data_remaining);
if (data_written < 0) {
err = -errno;
nvme_show_error("ERROR: %s: : write failed with error : %s",
__func__, strerror(errno));
break;
} else if (data_written <= data_remaining) {
data_remaining -= data_written;
data_ptr += data_written;
} else {
/* Unexpected overwrite */
fprintf(stderr, "Failure: Unexpected telemetry log overwrite - data_remaining = 0x%x, data_written = 0x%x\n",
data_remaining, data_written);
err = -1;
break;
}
}
if (fsync(output) < 0) {
nvme_show_error("ERROR : %s: : fsync : %s", __func__, strerror(errno));
return -1;
}
return err;
}
static int get_endurance_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieves endurance groups log page and prints the log.";
const char *group_id = "The endurance group identifier";
_cleanup_free_ struct nvme_endurance_group_log *endurance_log = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
nvme_print_flags_t flags;
int err;
struct config {
__u16 group_id;
};
struct config cfg = {
.group_id = 0,
};