forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nvme-ioctl.c
900 lines (764 loc) · 22.1 KB
/
nvme-ioctl.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
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <endian.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <linux/types.h>
#include "nvme-ioctl.h"
#include "spdk-nvme.h"
static int nvme_verify_chr(int fd)
{
static struct stat nvme_stat;
int err = nvme_spdk_is_valid_fd(fd);
if (err == 0) {
return 0;
}
err = fstat(fd, &nvme_stat);
if (err < 0) {
perror("fstat");
return errno;
}
if (!S_ISCHR(nvme_stat.st_mode)) {
fprintf(stderr,
"Error: requesting reset on non-controller handle\n");
return ENOTBLK;
}
return 0;
}
int nvme_subsystem_reset(int fd)
{
int ret;
ret = nvme_verify_chr(fd);
if (ret)
return ret;
return ioctl(fd, NVME_IOCTL_SUBSYS_RESET);
}
int nvme_reset_controller(int fd)
{
int ret;
ret = nvme_verify_chr(fd);
if (ret)
return ret;
if (nvme_spdk_is_valid_fd(fd) == 0) {
return spdk_nvme_ctrlr_reset(nvme_spdk_get_ctrlr_by_fd(fd));
}
return ioctl(fd, NVME_IOCTL_RESET);
}
int nvme_ns_rescan(int fd)
{
int ret;
ret = nvme_verify_chr(fd);
if (ret)
return ret;
return ioctl(fd, NVME_IOCTL_RESCAN);
}
int nvme_get_nsid(int fd)
{
if (nvme_spdk_is_valid_fd(fd) == 0) {
return nvme_spdk_get_nsid(fd);
}
static struct stat nvme_stat;
int err = fstat(fd, &nvme_stat);
if (err < 0)
return err;
if (!S_ISBLK(nvme_stat.st_mode)) {
fprintf(stderr,
"Error: requesting namespace-id from non-block device\n");
errno = ENOTBLK;
return -errno;
}
return ioctl(fd, NVME_IOCTL_ID);
}
int nvme_submit_passthru(int fd, __u64 ioctl_cmd, struct nvme_passthru_cmd *cmd)
{
if (ioctl_cmd == NVME_IOCTL_ADMIN_CMD) {
if (nvme_spdk_is_valid_fd(fd) == 0) {
return nvme_spdk_submit_cmd_passthru(fd, cmd, true);
}
}
return ioctl(fd, ioctl_cmd, cmd);
}
static int nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
{
if (nvme_spdk_is_valid_fd(fd) == 0) {
return nvme_spdk_submit_cmd_passthru(fd, cmd, true);
}
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, cmd);
}
static int nvme_submit_io_passthru(int fd, struct nvme_passthru_cmd *cmd)
{
if (g_spdk_enabled) {
switch (cmd->opcode) {
case nvme_cmd_flush:
case nvme_cmd_write_zeroes:
case nvme_cmd_write_uncor:
case nvme_cmd_dsm:
case nvme_cmd_resv_report:
case nvme_cmd_resv_register:
case nvme_cmd_resv_acquire:
case nvme_cmd_resv_release:
return nvme_spdk_submit_cmd_passthru(fd, cmd, false);
default:
fprintf(stderr,
"Error: unsupported opcode %d\n", cmd->opcode);
break;
}
}
return ioctl(fd, NVME_IOCTL_IO_CMD, cmd);
}
int nvme_passthru(int fd, int ioctl_cmd, __u8 opcode, __u8 flags, __u16 rsvd,
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10, __u32 cdw11,
__u32 cdw12, __u32 cdw13, __u32 cdw14, __u32 cdw15,
__u32 data_len, void *data, __u32 metadata_len,
void *metadata, __u32 timeout_ms, __u32 *result)
{
struct nvme_passthru_cmd cmd = {
.opcode = opcode,
.flags = flags,
.rsvd1 = rsvd,
.nsid = nsid,
.cdw2 = cdw2,
.cdw3 = cdw3,
.metadata = (__u64)(uintptr_t) metadata,
.addr = (__u64)(uintptr_t) data,
.metadata_len = metadata_len,
.data_len = data_len,
.cdw10 = cdw10,
.cdw11 = cdw11,
.cdw12 = cdw12,
.cdw13 = cdw13,
.cdw14 = cdw14,
.cdw15 = cdw15,
.timeout_ms = timeout_ms,
.result = 0,
};
int err;
err = nvme_submit_passthru(fd, ioctl_cmd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_io(int fd, __u8 opcode, __u64 slba, __u16 nblocks, __u16 control,
__u32 dsmgmt, __u32 reftag, __u16 apptag, __u16 appmask, void *data,
void *metadata)
{
struct nvme_user_io io = {
.opcode = opcode,
.flags = 0,
.control = control,
.nblocks = nblocks,
.rsvd = 0,
.metadata = (__u64)(uintptr_t) metadata,
.addr = (__u64)(uintptr_t) data,
.slba = slba,
.dsmgmt = dsmgmt,
.reftag = reftag,
.appmask = appmask,
.apptag = apptag,
};
if (nvme_spdk_is_valid_fd(fd) == 0) {
return nvme_spdk_io(fd, &io);
}
return ioctl(fd, NVME_IOCTL_SUBMIT_IO, &io);
}
int nvme_read(int fd, __u64 slba, __u16 nblocks, __u16 control, __u32 dsmgmt,
__u32 reftag, __u16 apptag, __u16 appmask, void *data,
void *metadata)
{
return nvme_io(fd, nvme_cmd_read, slba, nblocks, control, dsmgmt,
reftag, apptag, appmask, data, metadata);
}
int nvme_write(int fd, __u64 slba, __u16 nblocks, __u16 control, __u32 dsmgmt,
__u32 reftag, __u16 apptag, __u16 appmask, void *data,
void *metadata)
{
return nvme_io(fd, nvme_cmd_write, slba, nblocks, control, dsmgmt,
reftag, apptag, appmask, data, metadata);
}
int nvme_compare(int fd, __u64 slba, __u16 nblocks, __u16 control, __u32 dsmgmt,
__u32 reftag, __u16 apptag, __u16 appmask, void *data,
void *metadata)
{
return nvme_io(fd, nvme_cmd_compare, slba, nblocks, control, dsmgmt,
reftag, apptag, appmask, data, metadata);
}
int nvme_passthru_io(int fd, __u8 opcode, __u8 flags, __u16 rsvd,
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10,
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14,
__u32 cdw15, __u32 data_len, void *data,
__u32 metadata_len, void *metadata, __u32 timeout_ms)
{
return nvme_passthru(fd, NVME_IOCTL_IO_CMD, opcode, flags, rsvd, nsid,
cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, cdw14,
cdw15, data_len, data, metadata_len, metadata,
timeout_ms, NULL);
}
int nvme_write_zeros(int fd, __u32 nsid, __u64 slba, __u16 nlb,
__u16 control, __u32 reftag, __u16 apptag, __u16 appmask)
{
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_write_zeroes,
.nsid = nsid,
.cdw10 = slba & 0xffffffff,
.cdw11 = slba >> 32,
.cdw12 = nlb | (control << 16),
.cdw14 = reftag,
.cdw15 = apptag | (appmask << 16),
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_write_uncorrectable(int fd, __u32 nsid, __u64 slba, __u16 nlb)
{
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_write_uncor,
.nsid = nsid,
.cdw10 = slba & 0xffffffff,
.cdw11 = slba >> 32,
.cdw12 = nlb,
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_flush(int fd, __u32 nsid)
{
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_flush,
.nsid = nsid,
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_dsm(int fd, __u32 nsid, __u32 cdw11, struct nvme_dsm_range *dsm,
__u16 nr_ranges)
{
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_dsm,
.nsid = nsid,
.addr = (__u64)(uintptr_t) dsm,
.data_len = nr_ranges * sizeof(*dsm),
.cdw10 = nr_ranges - 1,
.cdw11 = cdw11,
};
return nvme_submit_io_passthru(fd, &cmd);
}
struct nvme_dsm_range *nvme_setup_dsm_range(__u32 *ctx_attrs, __u32 *llbas,
__u64 *slbas, __u16 nr_ranges)
{
int i;
struct nvme_dsm_range *dsm = malloc(nr_ranges * sizeof(*dsm));
if (!dsm)
return NULL;
for (i = 0; i < nr_ranges; i++) {
dsm[i].cattr = cpu_to_le32(ctx_attrs[i]);
dsm[i].nlb = cpu_to_le32(llbas[i]);
dsm[i].slba = cpu_to_le64(slbas[i]);
}
return dsm;
}
int nvme_resv_acquire(int fd, __u32 nsid, __u8 rtype, __u8 racqa,
bool iekey, __u64 crkey, __u64 nrkey)
{
__le64 payload[2] = { cpu_to_le64(crkey), cpu_to_le64(nrkey) };
__u32 cdw10 = racqa | (iekey ? 1 << 3 : 0) | rtype << 8;
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_resv_acquire,
.nsid = nsid,
.cdw10 = cdw10,
.addr = (__u64)(uintptr_t) (payload),
.data_len = sizeof(payload),
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_resv_register(int fd, __u32 nsid, __u8 rrega, __u8 cptpl,
bool iekey, __u64 crkey, __u64 nrkey)
{
__le64 payload[2] = { cpu_to_le64(crkey), cpu_to_le64(nrkey) };
__u32 cdw10 = rrega | (iekey ? 1 << 3 : 0) | cptpl << 30;
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_resv_register,
.nsid = nsid,
.cdw10 = cdw10,
.addr = (__u64)(uintptr_t) (payload),
.data_len = sizeof(payload),
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_resv_release(int fd, __u32 nsid, __u8 rtype, __u8 rrela,
bool iekey, __u64 crkey)
{
__le64 payload[1] = { cpu_to_le64(crkey) };
__u32 cdw10 = rrela | (iekey ? 1 << 3 : 0) | rtype << 8;
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_resv_release,
.nsid = nsid,
.cdw10 = cdw10,
.addr = (__u64)(uintptr_t) (payload),
.data_len = sizeof(payload),
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_resv_report(int fd, __u32 nsid, __u32 numd, __u32 cdw11, void *data)
{
struct nvme_passthru_cmd cmd = {
.opcode = nvme_cmd_resv_report,
.nsid = nsid,
.cdw10 = numd,
.cdw11 = cdw11,
.addr = (__u64)(uintptr_t) data,
.data_len = (numd + 1) << 2,
};
return nvme_submit_io_passthru(fd, &cmd);
}
int nvme_passthru_admin(int fd, __u8 opcode, __u8 flags, __u16 rsvd,
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10,
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14,
__u32 cdw15, __u32 data_len, void *data,
__u32 metadata_len, void *metadata, __u32 timeout_ms)
{
return nvme_passthru(fd, NVME_IOCTL_ADMIN_CMD, opcode, flags, rsvd,
nsid, cdw2, cdw3, cdw10, cdw11, cdw12, cdw13,
cdw14, cdw15, data_len, data, metadata_len,
metadata, timeout_ms, NULL);
}
int nvme_identify13(int fd, __u32 nsid, __u32 cdw10, __u32 cdw11, void *data)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_identify,
.nsid = nsid,
.addr = (__u64)(uintptr_t) data,
.data_len = NVME_IDENTIFY_DATA_SIZE,
.cdw10 = cdw10,
.cdw11 = cdw11,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_identify(int fd, __u32 nsid, __u32 cdw10, void *data)
{
return nvme_identify13(fd, nsid, cdw10, 0, data);
}
int nvme_identify_ctrl(int fd, void *data)
{
return nvme_identify(fd, 0, 1, data);
}
int nvme_identify_ns(int fd, __u32 nsid, bool present, void *data)
{
int cns = present ? NVME_ID_CNS_NS_PRESENT : NVME_ID_CNS_NS;
return nvme_identify(fd, nsid, cns, data);
}
int nvme_identify_ns_list(int fd, __u32 nsid, bool all, void *data)
{
int cns = all ? NVME_ID_CNS_NS_PRESENT_LIST : NVME_ID_CNS_NS_ACTIVE_LIST;
return nvme_identify(fd, nsid, cns, data);
}
int nvme_identify_ctrl_list(int fd, __u32 nsid, __u16 cntid, void *data)
{
int cns = nsid ? NVME_ID_CNS_CTRL_NS_LIST : NVME_ID_CNS_CTRL_LIST;
return nvme_identify(fd, nsid, (cntid << 16) | cns, data);
}
int nvme_identify_ns_descs(int fd, __u32 nsid, void *data)
{
return nvme_identify(fd, nsid, NVME_ID_CNS_NS_DESC_LIST, data);
}
int nvme_identify_nvmset(int fd, __u16 nvmset_id, void *data)
{
return nvme_identify13(fd, 0, NVME_ID_CNS_NVMSET_LIST, nvmset_id, data);
}
int nvme_get_log13(int fd, __u32 nsid, __u8 log_id, __u8 lsp, __u64 lpo,
__u16 lsi, bool rae, __u32 data_len, void *data)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_get_log_page,
.nsid = nsid,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
};
__u32 numd = (data_len >> 2) - 1;
__u16 numdu = numd >> 16, numdl = numd & 0xffff;
cmd.cdw10 = log_id | (numdl << 16) | (rae ? 1 << 15 : 0);
if (lsp)
cmd.cdw10 |= lsp << 8;
cmd.cdw11 = numdu | (lsi << 16);
cmd.cdw12 = lpo;
cmd.cdw13 = (lpo >> 32);
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_get_log(int fd, __u32 nsid, __u8 log_id, __u32 data_len, void *data)
{
return nvme_get_log13(fd, nsid, log_id, NVME_NO_LOG_LSP, NVME_NO_LOG_LPO,
0, 0, data_len, data);
}
int nvme_get_telemetry_log(int fd, void *lp, int generate_report,
int ctrl_init, size_t log_page_size, __u64 offset)
{
if (ctrl_init)
return nvme_get_log13(fd, NVME_NSID_ALL, NVME_LOG_TELEMETRY_CTRL,
NVME_NO_LOG_LSP, offset,
0, 1, log_page_size, lp);
if (generate_report)
return nvme_get_log13(fd, NVME_NSID_ALL, NVME_LOG_TELEMETRY_HOST,
NVME_TELEM_LSP_CREATE, offset,
0, 1, log_page_size, lp);
else
return nvme_get_log13(fd, NVME_NSID_ALL, NVME_LOG_TELEMETRY_HOST,
NVME_NO_LOG_LSP, offset,
0, 1, log_page_size, lp);
}
int nvme_fw_log(int fd, struct nvme_firmware_log_page *fw_log)
{
return nvme_get_log(fd, NVME_NSID_ALL, NVME_LOG_FW_SLOT, sizeof(*fw_log), fw_log);
}
int nvme_changed_ns_list_log(int fd, struct nvme_changed_ns_list_log *changed_ns_list_log)
{
return nvme_get_log(fd, 0, NVME_LOG_CHANGED_NS, sizeof(changed_ns_list_log->log),
changed_ns_list_log->log);
}
int nvme_error_log(int fd, int entries, struct nvme_error_log_page *err_log)
{
return nvme_get_log(fd, NVME_NSID_ALL, NVME_LOG_ERROR, entries * sizeof(*err_log), err_log);
}
int nvme_endurance_log(int fd, __u16 group_id, struct nvme_endurance_group_log *endurance_log)
{
return nvme_get_log13(fd, 0, NVME_LOG_ENDURANCE_GROUP, 0, 0, group_id, 0,
sizeof(*endurance_log), endurance_log);
}
int nvme_smart_log(int fd, __u32 nsid, struct nvme_smart_log *smart_log)
{
return nvme_get_log(fd, nsid, NVME_LOG_SMART, sizeof(*smart_log), smart_log);
}
int nvme_self_test_log(int fd, struct nvme_self_test_log *self_test_log)
{
return nvme_get_log(fd, NVME_NSID_ALL, NVME_LOG_DEVICE_SELF_TEST,
sizeof(*self_test_log), self_test_log);
}
int nvme_effects_log(int fd, struct nvme_effects_log_page *effects_log)
{
return nvme_get_log(fd, 0, NVME_LOG_CMD_EFFECTS, sizeof(*effects_log), effects_log);
}
int nvme_discovery_log(int fd, struct nvmf_disc_rsp_page_hdr *log, __u32 size)
{
return nvme_get_log(fd, 0, NVME_LOG_DISC, size, log);
}
int nvme_sanitize_log(int fd, struct nvme_sanitize_log_page *sanitize_log)
{
return nvme_get_log(fd, 0, NVME_LOG_SANITIZE, sizeof(*sanitize_log), sanitize_log);
}
int nvme_feature(int fd, __u8 opcode, __u32 nsid, __u32 cdw10, __u32 cdw11,
__u32 cdw12, __u32 data_len, void *data, __u32 *result)
{
struct nvme_admin_cmd cmd = {
.opcode = opcode,
.nsid = nsid,
.cdw10 = cdw10,
.cdw11 = cdw11,
.cdw12 = cdw12,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_set_feature(int fd, __u32 nsid, __u8 fid, __u32 value, __u32 cdw12,
bool save, __u32 data_len, void *data, __u32 *result)
{
__u32 cdw10 = fid | (save ? 1 << 31 : 0);
return nvme_feature(fd, nvme_admin_set_features, nsid, cdw10, value,
cdw12, data_len, data, result);
}
int nvme_property(int fd, __u8 fctype, __le32 off, __le64 *value, __u8 attrib)
{
int err;
struct nvme_admin_cmd cmd = {
.opcode = nvme_fabrics_command,
.cdw10 = attrib,
.cdw11 = off,
};
if (!value) {
errno = EINVAL;
return -errno;
}
if (fctype == nvme_fabrics_type_property_get){
cmd.nsid = nvme_fabrics_type_property_get;
} else if(fctype == nvme_fabrics_type_property_set) {
cmd.nsid = nvme_fabrics_type_property_set;
cmd.cdw12 = *value;
} else {
errno = EINVAL;
return -errno;
}
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && fctype == nvme_fabrics_type_property_get)
*value = cpu_to_le64(cmd.result);
return err;
}
static int get_property_helper(int fd, int offset, void *value, int *advance)
{
__le64 value64;
int err = -EINVAL;
switch (offset) {
case NVME_REG_CAP:
case NVME_REG_ASQ:
case NVME_REG_ACQ:
*advance = 8;
break;
default:
*advance = 4;
}
if (!value)
return err;
err = nvme_property(fd, nvme_fabrics_type_property_get,
cpu_to_le32(offset), &value64, (*advance == 8));
if (!err) {
if (*advance == 8)
*((uint64_t *)value) = le64_to_cpu(value64);
else
*((uint32_t *)value) = le32_to_cpu(value64);
}
return err;
}
int nvme_get_property(int fd, int offset, uint64_t *value)
{
int advance;
return get_property_helper(fd, offset, value, &advance);
}
int nvme_get_properties(int fd, void **pbar)
{
int offset, advance;
int err, ret = -EINVAL;
int size = getpagesize();
*pbar = malloc(size);
if (!*pbar)
return ret;
memset(*pbar, 0xff, size);
for (offset = NVME_REG_CAP; offset <= NVME_REG_CMBSZ; offset += advance) {
err = get_property_helper(fd, offset, *pbar + offset, &advance);
if (!err)
ret = 0;
}
return ret;
}
int nvme_set_property(int fd, int offset, int value)
{
__le64 val = cpu_to_le64(value);
__le32 off = cpu_to_le32(offset);
bool is64bit;
switch (off) {
case NVME_REG_CAP:
case NVME_REG_ASQ:
case NVME_REG_ACQ:
is64bit = true;
break;
default:
is64bit = false;
}
return nvme_property(fd, nvme_fabrics_type_property_set,
off, &val, is64bit ? 1: 0);
}
int nvme_get_feature(int fd, __u32 nsid, __u8 fid, __u8 sel, __u32 cdw11,
__u32 data_len, void *data, __u32 *result)
{
__u32 cdw10 = fid | sel << 8;
return nvme_feature(fd, nvme_admin_get_features, nsid, cdw10, cdw11,
0, data_len, data, result);
}
int nvme_format(int fd, __u32 nsid, __u8 lbaf, __u8 ses, __u8 pi,
__u8 pil, __u8 ms, __u32 timeout)
{
__u32 cdw10 = lbaf | ms << 4 | pi << 5 | pil << 8 | ses << 9;
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_format_nvm,
.nsid = nsid,
.cdw10 = cdw10,
.timeout_ms = timeout,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_ns_create(int fd, __u64 nsze, __u64 ncap, __u8 flbas,
__u8 dps, __u8 nmic, __u32 *result)
{
struct nvme_id_ns ns = {
.nsze = cpu_to_le64(nsze),
.ncap = cpu_to_le64(ncap),
.flbas = flbas,
.dps = dps,
.nmic = nmic,
};
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_ns_mgmt,
.addr = (__u64)(uintptr_t) ((void *)&ns),
.cdw10 = 0,
.data_len = 0x1000,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_ns_delete(int fd, __u32 nsid)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_ns_mgmt,
.nsid = nsid,
.cdw10 = 1,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_ns_attachment(int fd, __u32 nsid, __u16 num_ctrls, __u16 *ctrlist,
bool attach)
{
int i;
__u8 buf[0x1000];
struct nvme_controller_list *cntlist =
(struct nvme_controller_list *)buf;
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_ns_attach,
.nsid = nsid,
.addr = (__u64)(uintptr_t) cntlist,
.cdw10 = attach ? 0 : 1,
.data_len = 0x1000,
};
memset(buf, 0, sizeof(buf));
cntlist->num = cpu_to_le16(num_ctrls);
for (i = 0; i < num_ctrls; i++)
cntlist->identifier[i] = cpu_to_le16(ctrlist[i]);
if (nvme_spdk_is_valid_fd(fd) == 0) {
if (num_ctrls == 0) {
struct spdk_nvme_ctrlr *ctrlr = nvme_spdk_get_ctrlr_by_fd(fd);
const struct spdk_nvme_ctrlr_data *cdata = spdk_nvme_ctrlr_get_data(ctrlr);
cntlist->identifier[0] = cdata->cntlid;
cntlist->num = 1;
}
}
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_ns_attach_ctrls(int fd, __u32 nsid, __u16 num_ctrls, __u16 *ctrlist)
{
return nvme_ns_attachment(fd, nsid, num_ctrls, ctrlist, true);
}
int nvme_ns_detach_ctrls(int fd, __u32 nsid, __u16 num_ctrls, __u16 *ctrlist)
{
return nvme_ns_attachment(fd, nsid, num_ctrls, ctrlist, false);
}
int nvme_fw_download(int fd, __u32 offset, __u32 data_len, void *data)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_download_fw,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
.cdw10 = (data_len >> 2) - 1,
.cdw11 = offset >> 2,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_fw_commit(int fd, __u8 slot, __u8 action, __u8 bpid)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_activate_fw,
.cdw10 = (bpid << 31) | (action << 3) | slot,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_sec_send(int fd, __u32 nsid, __u8 nssf, __u16 spsp,
__u8 secp, __u32 tl, __u32 data_len, void *data, __u32 *result)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_security_send,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
.nsid = nsid,
.cdw10 = secp << 24 | spsp << 8 | nssf,
.cdw11 = tl,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_sec_recv(int fd, __u32 nsid, __u8 nssf, __u16 spsp,
__u8 secp, __u32 al, __u32 data_len, void *data, __u32 *result)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_security_recv,
.nsid = nsid,
.cdw10 = secp << 24 | spsp << 8 | nssf,
.cdw11 = al,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_dir_send(int fd, __u32 nsid, __u16 dspec, __u8 dtype, __u8 doper,
__u32 data_len, __u32 dw12, void *data, __u32 *result)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_directive_send,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
.nsid = nsid,
.cdw10 = data_len? (data_len >> 2) - 1 : 0,
.cdw11 = dspec << 16 | dtype << 8 | doper,
.cdw12 = dw12,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_dir_recv(int fd, __u32 nsid, __u16 dspec, __u8 dtype, __u8 doper,
__u32 data_len, __u32 dw12, void *data, __u32 *result)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_directive_recv,
.addr = (__u64)(uintptr_t) data,
.data_len = data_len,
.nsid = nsid,
.cdw10 = data_len? (data_len >> 2) - 1 : 0,
.cdw11 = dspec << 16 | dtype << 8 | doper,
.cdw12 = dw12,
};
int err;
err = nvme_submit_admin_passthru(fd, &cmd);
if (!err && result)
*result = cmd.result;
return err;
}
int nvme_sanitize(int fd, __u8 sanact, __u8 ause, __u8 owpass, __u8 oipbp,
__u8 no_dealloc, __u32 ovrpat)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_sanitize_nvm,
.cdw10 = no_dealloc << 9 | oipbp << 8 |
owpass << NVME_SANITIZE_OWPASS_SHIFT |
ause << 3 | sanact,
.cdw11 = ovrpat,
};
return nvme_submit_admin_passthru(fd, &cmd);
}
int nvme_self_test_start(int fd, __u32 nsid, __u32 cdw10)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_dev_self_test,
.nsid = nsid,
.cdw10 = cdw10,
};
return nvme_submit_admin_passthru(fd, &cmd);
}