-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunvmed-cmds.c
2399 lines (2037 loc) · 63.2 KB
/
unvmed-cmds.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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/msg.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <ccan/str/str.h>
#include <nvme/types.h>
#include <vfn/pci.h>
#include <vfn/nvme.h>
#include "libunvmed.h"
#include "unvme.h"
#include "unvmed.h"
#include "app/perf/perf.h"
#include "argtable3/argtable3.h"
static pthread_spinlock_t __arglock;
static void __attribute__((constructor)) unvmed_cmds_init(void)
{
pthread_spin_init(&__arglock, 0);
}
#define unvme_parse_args_locked(argc, argv, argtable, help, end, desc) \
do { \
int nerror; \
\
pthread_spin_lock(&__arglock); \
nerror = arg_parse(argc - 1, &argv[1], argtable); \
pthread_spin_unlock(&__arglock); \
\
if (arg_boolv(help)) { \
unvme_print_help(__stdout, argv[1], desc, argtable); \
\
arg_freetable(argtable, sizeof(argtable) / sizeof(*argtable)); \
return 0; \
} \
\
if (nerror > 0) { \
arg_print_errors(__stdout, end, "unvme"); \
unvme_print_help(__stdout, argv[1], desc, argtable); \
\
arg_freetable(argtable, sizeof(argtable) / sizeof(*argtable)); \
return EINVAL; \
} \
\
} while (0)
extern __thread struct unvme_msg *__msg;
__thread jmp_buf *__jump = NULL;
/*
* Overrided exit() function which should be called by the external apps.
*/
void exit(int status)
{
if (__msg) {
unvmed_log_err("job (pid=%d) has been terminated (err=%d)",
unvme_msg_pid(__msg), status);
}
if (__jump)
longjmp(*__jump, EINTR);
unvme_exit_job(status);
pthread_exit(NULL);
}
static void unvme_pr_cqe(struct nvme_cqe *cqe)
{
uint32_t sct, sc;
sct = (cqe->sfp >> 9) & 0x7;
sc = (cqe->sfp >> 1) & 0xff;
unvme_pr_err("CQ entry: sqid=%#x, cid=%#x, dw0=%#x, dw1=%#x, sct=%#x, sc=%#x\n",
cqe->sqid, cqe->cid, cqe->dw0, cqe->dw1, sct, sc);
}
static void unvme_pr_cqe_status(int status)
{
uint8_t type = (status >> 8) & 0x7;
uint8_t code = status & 0xff;
unvme_pr_err("CQE status: type=%#x, code=%#x\n", type, code);
}
static inline void __unvme_cmd_pr(const char *format, void *buf, size_t len,
void (*pr)(void *))
{
if (streq(format, "binary"))
unvme_pr_raw(buf, len);
else if (streq(format, "normal"))
pr(buf);
else
assert(false);
}
static void unvme_cmd_pr_raw(void *buf, size_t len)
{
__unvme_cmd_pr("binary", buf, len, NULL);
}
static bool __is_nvme_device(const char *bdf)
{
unsigned long long class;
__unvme_free char *path = NULL;
char buf[32];
int ret;
int fd;
ret = asprintf(&path, "/sys/bus/pci/devices/%s/class", bdf);
assert(ret > 0);
fd = open(path, O_RDONLY);
if (fd < 0)
return false;
ret = read(fd, buf, sizeof(buf));
if (ret < 0) {
close(fd);
return false;
}
buf[ret] = '\0';
class = strtoull(buf, NULL, 16);
return ((class >> 8) & 0xffff) == 0x108;
}
static int unvmed_pci_bind(const char *bdf, const char *target)
{
char *path = "/sys/module/vfio_pci";
unsigned long long vendor, device;
struct stat st;
if (stat(path, &st) || !S_ISDIR(st.st_mode)) {
unvme_pr_err("'%s' kernel module is not loaded. "
"Try 'modprobe %s'.\n", target, target);
errno = ENOENT;
return -1;
}
if (pci_device_info_get_ull(bdf, "vendor", &vendor)) {
perror("pci_device_info_get_ull");
return -1;
}
if (pci_device_info_get_ull(bdf, "device", &device)) {
perror("pci_device_info_get_ull");
return -1;
}
pci_unbind(bdf);
if (pci_driver_new_id(target, vendor, device)) {
if (pci_bind(bdf, target)) {
perror("pci_bind");
return -1;
}
}
return 0;
}
static int unvmed_pci_unbind(const char *bdf)
{
return pci_unbind(bdf);
}
int unvme_list(int argc, char *argv[], struct unvme_msg *msg)
{
struct dirent *entry;
char *bdf;
DIR *dfd;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Print NVMe PCI device(s) in the current system as PCI BDF\n"
"(Bus-Device-Function) format. It requires unvmed to figure\n"
"out whether each device is attached to unvmed service or not.";
void *argtable[] = {
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
dfd = opendir("/sys/bus/pci/devices");
if (!dfd) {
unvme_pr_err("failed to open sysfs\n");
ret = ENOENT;
goto out;
}
unvme_pr("NVMe device \tAttached\tDriver \n");
unvme_pr("---------------\t--------\t------------\n");
while ((entry = readdir(dfd))) {
if (streq(entry->d_name, ".") || streq(entry->d_name, ".."))
continue;
bdf = entry->d_name;
if (!__is_nvme_device(bdf))
continue;
unvme_pr("%-15s\t%-8s\t%-12s\n", bdf,
unvmed_get(bdf) ? "yes" : "no",
pci_get_driver(bdf));
}
closedir(dfd);
out:
unvme_free_args(argtable);
return ret;
}
int unvme_add(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_int *nrioqs;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Register a given target <device> to unvmed service by binding\n"
"it to 'vfio-pci' kernel driver to support user-space I/O.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
nrioqs = arg_int0("q", "nr-ioqs", "<n>", "[O] Maximum number of "
"I/O queues to create (defaults: # of cpu)"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
/* Set default argument values prior to parsing */
arg_intv(nrioqs) = get_nprocs();
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (u)
goto out;
if (arg_intv(nrioqs) < 1) {
unvme_pr_err("'--nr-ioqs=' should be greater than 0\n");
ret = EINVAL;
goto out;
}
if (unvmed_pci_bind(arg_strv(dev), "vfio-pci")) {
unvme_pr_err("failed to bind PCI device to vfio-pci driver\n");
ret = errno;
goto out;
}
if (!unvmed_init_ctrl(arg_strv(dev), arg_intv(nrioqs))) {
unvme_pr_err("failed to initialize unvme\n");
ret = errno;
goto out;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_del(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_lit *nvme;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Delete a given target <device> from unvmed service by unbinding\n"
"it from 'vfio-pci' kernel driver";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
nvme = arg_lit0("n", "nvme", "Bind back to nvme kernel driver from vfio-pci driver"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (u)
unvmed_free_ctrl(u);
if (unvmed_pci_unbind(arg_strv(dev))) {
unvme_pr_err("failed to unbind PCI device from vfio-pci driver\n");
ret = errno;
}
/*
* If --nvme is not given, keep the device being binded to vfio-pci
* kernel driver, otherwise, bind it to nvme kernel drive back.
*/
if (arg_boolv(nvme)) {
if (unvmed_pci_bind(arg_strv(dev), "nvme")) {
unvme_pr_err("failed to bind PCI device to nvme driver\n");
ret = errno;
}
}
unvme_free_args(argtable);
return ret;
}
int unvme_show_regs(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Show NVMe controller properties (registers in MMIO space).";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
unvme_pr_show_regs(u);
out:
unvme_free_args(argtable);
return ret;
}
int unvme_status(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Show status of a given target <device> in unvmed.\n"
"The status includes CC, CSTS and Submission/Completion Queue.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
unvme_pr_status(u);
out:
unvme_free_args(argtable);
return ret;
}
int unvme_enable(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_lit *no_adminq;
struct arg_int *iosqes;
struct arg_int *iocqes;
struct arg_int *mps;
struct arg_int *css;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Enable NVMe controller along with setting up admin queues.\n"
"It will wait for CSTS.RDY to be 1 after setting CC.EN to 1.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
no_adminq = arg_lit0(NULL, "no-adminq", "[O] Enable device without creating admin queues"),
iosqes = arg_int0("s", "iosqes", "<n>", "[O] I/O Sumission Queue Entry Size (2^n) (default: 6)"),
iocqes = arg_int0("c", "iocqes", "<n>", "[O] I/O Completion Queue Entry Size (2^n) (default: 4)"),
mps = arg_int0("m", "mps", "<n>", "[O] Memory Page Size (2^(12+n)) (default: 0)"),
css = arg_int0("i", "css", "<n>", "[O] I/O Command Set Selected (default: 0)"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
/* Set default argument values prior to parsing */
arg_intv(iosqes) = 6;
arg_intv(iocqes) = 4;
arg_intv(mps) = 0;
arg_intv(css) = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
if (arg_intv(iosqes) > 0xf) {
unvme_pr_err("invalid -s|--iosqes\n");
ret = EINVAL;
goto out;
}
if (arg_intv(iocqes) > 0xf) {
unvme_pr_err("invalid -c|--iocqes\n");
ret = EINVAL;
goto out;
}
if (arg_intv(mps) > 0xf) {
unvme_pr_err("invalid -m|--mps\n");
ret = EINVAL;
goto out;
}
if (arg_intv(css) > 0x7) {
unvme_pr_err("invalid -i|--css\n");
ret = EINVAL;
goto out;
}
if (!arg_boolv(no_adminq) && unvmed_create_adminq(u)) {
unvme_pr_err("failed to create adminq\n");
ret = errno;
goto out;
}
if (unvmed_enable_ctrl(u, arg_intv(iosqes), arg_intv(iocqes),
arg_intv(mps), arg_intv(css))) {
unvme_pr_err("failed to enable controller\n");
ret = errno;
goto out;
}
if (arg_boolv(no_adminq)) {
unvme_pr_err("Warning: Admin queues are not created. "
"The following admin commands will not work\n");
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_cmb(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_lit *enable;
struct arg_lit *disable;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Enable CMB (controller memory buffer) in the controller.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
enable = arg_lit0("e", "enable", "Enable CMB"),
disable = arg_lit0("d", "disable", "Disable CMB"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
if (arg_boolv(enable) && arg_boolv(disable)) {
unvme_pr_err("invalid, given --enable and --disable both\n");
ret = EINVAL;
goto out;
}
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
if (arg_boolv(enable)) {
if (unvmed_cmb_init(u)) {
unvme_pr_err("failed to initialize CMB\n");
ret = EINVAL;
goto out;
}
} else if (arg_boolv(disable)) {
unvmed_cmb_free(u);
} else {
unvme_pr_err("either --enable or --disable should be given\n");
ret = EINVAL;
goto out;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_create_iocq(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_int *qid;
struct arg_int *qsize;
struct arg_int *vector;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit a Create I/O Completion Queue admin command to the target\n"
"<device>. DMA address of the queue will be allocated and configured\n"
"by libvfn.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
qid = arg_int1("q", "qid", "<n>", "[M] Completion Queue ID to create"),
qsize = arg_int1("z", "qsize", "<n>", "[M] Queue size (1-based)"),
vector = arg_int0("v", "vector", "<n>", "[O] Interrupt vector (defaults: -1)"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
struct unvme_sq *usq;
int ret = 0;
/* Set default argument values prior to parsing */
arg_intv(vector) = -1;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, 0);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
if (arg_intv(vector) < 0)
arg_intv(vector) = -1;
if (unvmed_cq_enabled(u, arg_intv(qid))) {
unvme_pr_err("failed to create cq (qid=%u) (exists)\n", arg_intv(qid));
ret = EEXIST;
goto out;
}
if (unvmed_create_cq(u, arg_intv(qid), arg_intv(qsize), arg_intv(vector))) {
unvme_pr_err("failed to create cq\n");
ret = errno;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_delete_iocq(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_int *qid;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit a Delete I/O Completion Queue admin command to the target <device>.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
qid = arg_int1("q", "qid", "<n>", "[M] Completion Queue ID to delete"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
struct unvme_sq *usq;
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, 0);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
if (!unvmed_cq_enabled(u, arg_intv(qid))) {
unvme_pr_err("failed to delete cq (qid=%u) (not exists)\n", arg_intv(qid));
ret = ENOMEDIUM;
goto out;
}
if (unvmed_delete_cq(u, arg_intv(qid))) {
unvme_pr_err("failed to delete iocq\n");
ret = errno;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_create_iosq(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_int *qid;
struct arg_int *qsize;
struct arg_int *cqid;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit a Create I/O Submission Queue admin command to the target\n"
"<device>. DMA address of the queue will be allocated and configured\n"
"by libvfn.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
qid = arg_int1("q", "qid", "<n>", "[M] Submission Queue ID to create"),
qsize = arg_int1("z", "qsize", "<n>", "[M] Queue size (1-based)"),
cqid = arg_int1("c", "cqid", "<n>", "[M] Completion Queue ID"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
struct unvme_sq *usq, *targetq;
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, 0);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
targetq = unvmed_sq_find(u, arg_intv(qid));
if (unvmed_sq_enabled(targetq)) {
unvme_pr_err("failed to create iosq (qid=%u) (exists)\n", arg_intv(qid));
ret = EEXIST;
goto out;
}
if (unvmed_create_sq(u, arg_intv(qid), arg_intv(qsize), arg_intv(cqid))) {
unvme_pr_err("failed to create iosq\n");
ret = errno;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_delete_iosq(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_int *qid;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit a Delete I/O Submission Queue admin command\n"
"to the target <device>.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
qid = arg_int1("q", "qid", "<n>", "[M] Submission Queue ID to delete"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
struct unvme_sq *usq, *targetq;
int ret = 0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, 0);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
targetq = unvmed_sq_find(u, arg_intv(qid));
if (unvmed_sq_enabled(targetq)) {
unvme_pr_err("failed to delete iosq (qid=%u) (not exists)\n", arg_intv(qid));
ret = ENOMEDIUM;
goto out;
}
if (unvmed_delete_sq(u, arg_intv(qid))) {
unvme_pr_err("failed to delete iosq\n");
ret = errno;
}
out:
unvme_free_args(argtable);
return ret;
}
int unvme_id_ns(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_dbl *nsid;
struct arg_int *prp1_offset;
struct arg_str *format;
struct arg_lit *nodb;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit an Identify Namespace admin command to the target <device>.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
nsid = arg_dbl1("n", "nsid", "<n>", "[M] Namespace ID"),
prp1_offset = arg_int0(NULL, "prp1-offset", "<n>", "[O] PRP1 offset < CC.MPS (default: 0x0)"),
format = arg_str0("o", "output-format", "[normal|binary]", "[O] Output format: [normal|binary] (defaults: normal)"),
nodb = arg_lit0("N", "nodb", "[O] Don't update tail doorbell of the submission queue"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
const size_t size = NVME_IDENTIFY_DATA_SIZE;
const uint16_t sqid = 0;
struct unvme_cmd *cmd;
struct unvme_sq *usq = NULL;
ssize_t len;
void *buf = NULL;
unsigned long flags = 0;
struct iovec iov;
int ret;
/* Set default argument values prior to parsing */
arg_strv(format) = "normal";
arg_intv(prp1_offset) = 0x0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, sqid);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
if (arg_intv(prp1_offset) >= getpagesize()) {
unvme_pr_err("invalid --prp1-offset\n");
ret = EINVAL;
goto out;
}
if (arg_boolv(nodb))
flags |= UNVMED_CMD_F_NODB;
/* allocate a buffer with a consieration of --prp1-offset=<n> */
len = pgmap(&buf, size + (getpagesize() - arg_intv(prp1_offset)));
if (len < 0) {
unvme_pr_err("failed to allocate user data buffer\n");
ret = errno;
goto out;
}
cmd = unvmed_alloc_cmd(u, usq, buf, len);
if (!cmd) {
unvme_pr_err("failed to allocate a command instance\n");
pgunmap(buf, len);
ret = errno;
goto out;
}
buf += arg_intv(prp1_offset);
iov = (struct iovec) {
.iov_base = buf,
.iov_len = size,
};
ret = unvmed_id_ns(u, cmd, arg_dblv(nsid), &iov, 1, flags);
if (arg_boolv(nodb)) {
cmd->buf.flags = UNVME_CMD_BUF_F_VA_UNMAP |
UNVME_CMD_BUF_F_IOVA_UNMAP;
goto out;
}
if (!ret) {
__unvme_cmd_pr(arg_strv(format), buf, size, unvme_pr_id_ns);
if (unvmed_init_ns(u, arg_dblv(nsid), buf)) {
unvme_pr_err("failed to initialize ns instance\n");
ret = errno;
}
} else if (ret > 0)
unvme_pr_cqe_status(ret);
else
unvme_pr_err("failed to identify namespace\n");
unvmed_cmd_free(cmd);
pgunmap(buf - arg_intv(prp1_offset), len);
out:
unvme_free_args(argtable);
return ret;
}
int unvme_id_ctrl(int argc, char *argv[], struct unvme_msg *msg)
{
const char *desc =
"Submit an Identify Controller admin command to the target <device>.";
struct unvme *u;
struct arg_rex *dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf");
struct arg_int *prp1_offset = arg_int0(NULL, "prp1-offset", "<n>", "[O] PRP1 offset < CC.MPS (default: 0x0)");
struct arg_lit *vendor_specific = arg_lit0("V", "vendor-specific", "[O] Dump binary vendor field");
struct arg_lit *binary = arg_lit0("b", "raw-binary", "[O] Show identify in binary format");
struct arg_lit *nodb = arg_lit0("N", "nodb", "[O] Don't update tail doorbell of the submission queue");
struct arg_lit *help = arg_lit0("h", "help", "Show help message");
struct arg_end *end = arg_end(UNVME_ARG_MAX_ERROR);
void *argtable[] = {dev, prp1_offset, vendor_specific, binary, nodb, help, end};
const size_t size = NVME_IDENTIFY_DATA_SIZE;
const uint16_t sqid = 0;
struct unvme_cmd *cmd;
struct unvme_sq *usq = NULL;
ssize_t len;
void *buf = NULL;
unsigned long flags = 0;
struct iovec iov;
int ret;
/* Set default argument values prior to parsing */
arg_intv(prp1_offset) = 0x0;
unvme_parse_args_locked(argc, argv, argtable, help, end, desc);
u = unvmed_get(arg_strv(dev));
if (!u) {
unvme_pr_err("%s is not added to unvmed\n", arg_strv(dev));
ret = ENODEV;
goto out;
}
usq = unvmed_sq_find(u, sqid);
if (!usq || !unvmed_sq_enabled(usq)) {
unvme_pr_err("failed to get admin sq\n");
ret = ENOMEDIUM;
goto out;
}
if (arg_intv(prp1_offset) >= getpagesize()) {
unvme_pr_err("invalid --prp1-offset\n");
ret = EINVAL;
goto out;
}
if (arg_boolv(nodb))
flags |= UNVMED_CMD_F_NODB;
/* allocate a buffer with a consieration of --prp1-offset=<n> */
len = pgmap(&buf, size + (getpagesize() - arg_intv(prp1_offset)));
if (len < 0) {
unvme_pr_err("failed to allocate user data buffer\n");
ret = errno;
goto out;
}
cmd = unvmed_alloc_cmd(u, usq, buf, len);
if (!cmd) {
unvme_pr_err("failed to allocate a command instance\n");
pgunmap(buf, len);
ret = errno;
goto out;
}
buf += arg_intv(prp1_offset);
iov = (struct iovec) {
.iov_base = buf,
.iov_len = size,
};
ret = unvmed_id_ctrl(u, cmd, &iov, 1, flags);
if (arg_boolv(nodb)) {
cmd->buf.flags = UNVME_CMD_BUF_F_VA_UNMAP |
UNVME_CMD_BUF_F_IOVA_UNMAP;
goto out;
}
if (!ret) {
unvmed_init_id_ctrl(u, buf);
__unvme_cmd_pr(arg_boolv(binary) ? "binary" : "normal", buf, size, unvme_pr_id_ctrl);
} else if (ret > 0)
unvme_pr_cqe_status(ret);
else
unvme_pr_err("failed to identify namespace\n");
unvmed_cmd_free(cmd);
pgunmap(buf - arg_intv(prp1_offset), len);
out:
unvme_free_args(argtable);
return ret;
}
int unvme_id_active_nslist(int argc, char *argv[], struct unvme_msg *msg)
{
struct unvme *u;
struct arg_rex *dev;
struct arg_dbl *nsid;
struct arg_int *prp1_offset;
struct arg_str *format;
struct arg_lit *help;
struct arg_end *end;
const char *desc =
"Submit an Identify Active Namespace List admin command to the target <device>.";
void *argtable[] = {
dev = arg_rex1(NULL, NULL, UNVME_BDF_PATTERN, "<device>", 0, "[M] Device bdf"),
nsid = arg_dbl0("n", "nsid", "<n>", "[O] Starting NSID for retrieving active NS with NSID greater than this (default: 0)"),
prp1_offset = arg_int0(NULL, "prp1-offset", "<n>", "[O] PRP1 offset < CC.MPS (default: 0x0)"),
format = arg_str0("o", "output-format", "[normal|binary]", "[O] Output format: [normal|binary] (defaults: normal)"),
help = arg_lit0("h", "help", "Show help message"),
end = arg_end(UNVME_ARG_MAX_ERROR),
};
const size_t size = NVME_IDENTIFY_DATA_SIZE;
const uint16_t sqid = 0;
struct unvme_cmd *cmd;