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
|
/*
* lightnvm.c -- LightNVM NVMe integration.
*
* Copyright (c) 2016, CNEX Labs.
*
* Written by Matias Bjoerling <matias@cnexlabs.com>
*
* 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include "nvme-lightnvm.h"
#include "nvme-print.h"
#include "nvme-ioctl.h"
static int lnvm_open(void)
{
char dev[FILENAME_MAX] = NVM_CTRL_FILE;
int fd;
fd = open(dev, O_WRONLY);
if (fd < 0) {
printf("Failed to open LightNVM mgmt interface\n");
perror(dev);
return fd;
}
return fd;
}
static void lnvm_close(int fd)
{
close(fd);
}
int lnvm_do_init(char *dev, char *mmtype)
{
struct nvm_ioctl_dev_init init;
int fd, ret;
fd = lnvm_open();
if (fd < 0)
return fd;
memset(&init, 0, sizeof(struct nvm_ioctl_dev_init));
strncpy(init.dev, dev, DISK_NAME_LEN - 1);
strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN - 1);
ret = ioctl(fd, NVM_DEV_INIT, &init);
switch (errno) {
case EINVAL:
printf("Initialization failed.\n");
break;
case EEXIST:
printf("Device has already been initialized.\n");
break;
case 0:
break;
default:
printf("Unknown error occurred (%d)\n", errno);
break;
}
lnvm_close(fd);
return ret;
}
int lnvm_do_list_devices(void)
{
struct nvm_ioctl_get_devices devs;
int fd, ret, i;
fd = lnvm_open();
if (fd < 0)
return fd;
ret = ioctl(fd, NVM_GET_DEVICES, &devs);
if (ret) {
lnvm_close(fd);
return ret;
}
printf("Number of devices: %u\n", devs.nr_devices);
printf("%-12s\t%-12s\tVersion\n", "Device", "Block manager");
for (i = 0; i < devs.nr_devices && i < 31; i++) {
struct nvm_ioctl_device_info *info = &devs.info[i];
printf("%-12s\t%-12s\t(%u,%u,%u)\n", info->devname, info->bmname,
info->bmversion[0], info->bmversion[1],
info->bmversion[2]);
}
lnvm_close(fd);
return 0;
}
int lnvm_do_info(void)
{
struct nvm_ioctl_info c;
int fd, ret, i;
fd = lnvm_open();
if (fd < 0)
return fd;
memset(&c, 0, sizeof(struct nvm_ioctl_info));
ret = ioctl(fd, NVM_INFO, &c);
if (ret) {
lnvm_close(fd);
return ret;
}
printf("LightNVM (%u,%u,%u). %u target type(s) registered.\n",
c.version[0], c.version[1], c.version[2], c.tgtsize);
printf("Type\tVersion\n");
for (i = 0; i < c.tgtsize; i++) {
struct nvm_ioctl_info_tgt *tgt = &c.tgts[i];
printf("%s\t(%u,%u,%u)\n",
tgt->tgtname, tgt->version[0], tgt->version[1],
tgt->version[2]);
}
lnvm_close(fd);
return 0;
}
int lnvm_do_create_tgt(char *devname, char *tgtname, char *tgttype,
int lun_begin, int lun_end,
int over_prov, int flags)
{
struct nvm_ioctl_create c;
int fd, ret;
fd = lnvm_open();
if (fd < 0)
return fd;
strncpy(c.dev, devname, DISK_NAME_LEN - 1);
strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX - 1);
c.flags = flags;
/* Fall back into simple IOCTL version if no extended attributes used */
if (over_prov != -1) {
c.conf.type = NVM_CONFIG_TYPE_EXTENDED;
c.conf.e.lun_begin = lun_begin;
c.conf.e.lun_end = lun_end;
c.conf.e.over_prov = over_prov;
} else {
c.conf.type = NVM_CONFIG_TYPE_SIMPLE;
c.conf.s.lun_begin = lun_begin;
c.conf.s.lun_end = lun_end;
}
ret = ioctl(fd, NVM_DEV_CREATE, &c);
if (ret)
fprintf(stderr, "Creation of target failed. Please see dmesg.\n");
lnvm_close(fd);
return ret;
}
int lnvm_do_remove_tgt(char *tgtname)
{
struct nvm_ioctl_remove c;
int fd, ret;
fd = lnvm_open();
if (fd < 0)
return fd;
strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
c.flags = 0;
ret = ioctl(fd, NVM_DEV_REMOVE, &c);
if (ret)
fprintf(stderr, "Remove of target failed. Please see dmesg.\n");
lnvm_close(fd);
return ret;
}
int lnvm_do_factory_init(char *devname, int erase_only_marked,
int clear_host_marks,
int clear_bb_marks)
{
struct nvm_ioctl_dev_factory fact;
int fd, ret;
fd = lnvm_open();
if (fd < 0)
return fd;
memset(&fact, 0, sizeof(struct nvm_ioctl_dev_factory));
strncpy(fact.dev, devname, DISK_NAME_LEN - 1);
if (erase_only_marked)
fact.flags |= NVM_FACTORY_ERASE_ONLY_USER;
if (clear_host_marks)
fact.flags |= NVM_FACTORY_RESET_HOST_BLKS;
if (clear_bb_marks)
fact.flags |= NVM_FACTORY_RESET_GRWN_BBLKS;
ret = ioctl(fd, NVM_DEV_FACTORY, &fact);
switch (errno) {
case EINVAL:
fprintf(stderr, "Factory reset failed.\n");
break;
case 0:
break;
default:
fprintf(stderr, "Unknown error occurred (%d)\n", errno);
break;
}
lnvm_close(fd);
return ret;
}
static void show_lnvm_id_grp(void *t, int human)
{
struct nvme_nvm_id12_group *grp = t;
uint32_t mpos = (uint32_t)le32_to_cpu(grp->mpos);
uint32_t mccap = (uint32_t)le32_to_cpu(grp->mccap);
printf(" mtype : %d\n", grp->mtype);
if (human) {
if (grp->mtype == LNVM_IDFY_GRP_MTYPE_NAND)
printf(" NAND Flash Memory\n");
else
printf(" Reserved\n");
}
printf(" fmtype : %d\n", grp->fmtype);
if (human) {
if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_SLC)
printf(" Single bit Level Cell flash (SLC)\n");
else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_MLC)
printf(" Two bit Level Cell flash (MLC)\n");
else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_TLC)
printf(" Three bit Level Cell flash (TLC)\n");
else
printf(" Reserved\n");
}
printf(" chnls : %d\n", grp->num_ch);
printf(" luns : %d\n", grp->num_lun);
printf(" plns : %d\n", grp->num_pln);
printf(" blks : %d\n", (uint16_t)le16_to_cpu(grp->num_blk));
printf(" pgs : %d\n", (uint16_t)le16_to_cpu(grp->num_pg));
printf(" fpg_sz : %d\n", (uint16_t)le16_to_cpu(grp->fpg_sz));
printf(" csecs : %d\n", (uint16_t)le16_to_cpu(grp->csecs));
printf(" sos : %d\n", (uint16_t)le16_to_cpu(grp->sos));
printf(" trdt : %d\n", (uint32_t)le32_to_cpu(grp->trdt));
printf(" trdm : %d\n", (uint32_t)le32_to_cpu(grp->trdm));
printf(" tprt : %d\n", (uint32_t)le32_to_cpu(grp->tprt));
printf(" tprm : %d\n", (uint32_t)le32_to_cpu(grp->tprm));
printf(" tbet : %d\n", (uint32_t)le32_to_cpu(grp->tbet));
printf(" tbem : %d\n", (uint32_t)le32_to_cpu(grp->tbem));
printf(" mpos : %#x\n", mpos);
if (human) {
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_RD))
printf(" [0]: Single plane read\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_RD))
printf(" [1]: Dual plane read\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_RD))
printf(" [2]: Quad plane read\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_PRG))
printf(" [8]: Single plane program\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_PRG))
printf(" [9]: Dual plane program\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_PRG))
printf(" [10]: Quad plane program\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_ERS))
printf(" [16]: Single plane erase\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_ERS))
printf(" [17]: Dual plane erase\n");
if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_ERS))
printf(" [18]: Quad plane erase\n");
}
printf(" mccap : %#x\n", mccap);
if (human) {
if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SLC))
printf(" [0]: SLC mode\n");
if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_CMD_SUSP))
printf(" [1]: Command suspension\n");
if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SCRAMBLE))
printf(" [2]: Scramble\n");
if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_ENCRYPT))
printf(" [3]: Encryption\n");
}
printf(" cpar : %#x\n", (uint16_t)le16_to_cpu(grp->cpar));
}
static void show_lnvm_ppaf(struct nvme_nvm_addr_format *ppaf)
{
printf("ppaf :\n");
printf(" ch offs : %d ch bits : %d\n",
ppaf->ch_offset, ppaf->ch_len);
printf(" lun offs: %d lun bits : %d\n",
ppaf->lun_offset, ppaf->lun_len);
printf(" pl offs : %d pl bits : %d\n",
ppaf->pln_offset, ppaf->pln_len);
printf(" blk offs: %d blk bits : %d\n",
ppaf->blk_offset, ppaf->blk_len);
printf(" pg offs : %d pg bits : %d\n",
ppaf->pg_offset, ppaf->pg_len);
printf(" sec offs: %d sec bits : %d\n",
ppaf->sect_offset, ppaf->sect_len);
}
static void show_lnvm_id12_ns(void *t, unsigned int flags)
{
int i;
int human = flags & VERBOSE;
struct nvme_nvm_id12 *id = t;
uint32_t cap = (uint32_t) le32_to_cpu(id->cap);
uint32_t dom = (uint32_t) le32_to_cpu(id->dom);
uint32_t cgrps = id->cgrps;
if (id->cgrps > 4) {
fprintf(stderr, "invalid identify geometry returned\n");
return;
}
printf("verid : %#x\n", id->ver_id);
printf("vmnt : %#x\n", id->vmnt);
if (human) {
if (!id->vmnt)
printf(" Generic/Enable opcodes as found in this spec.");
else
printf(" Reserved/Reserved for future opcode configurations");
}
printf("\n");
printf("cgrps : %d\n", id->cgrps);
printf("cap : %#x\n", cap);
if (human) {
if (cap & (1 << LNVM_IDFY_CAP_BAD_BLK_TBL_MGMT))
printf(" [0]: Bad block table management\n");
if (cap & (1 << LNVM_IDFY_CAP_HYBRID_CMD_SUPP))
printf(" [1]: Hybrid command support\n");
}
printf("dom : %#x\n", dom);
if (human) {
if (dom & (1 << LNVM_IDFY_DOM_HYBRID_MODE))
printf(" [0]: Hybrid mode (L2P MAP is in device)\n");
if (dom & (1 << LNVM_IDFY_DOM_ECC_MODE))
printf(" [1]: Error Code Correction(ECC) mode\n");
}
show_lnvm_ppaf(&id->ppaf);
for (i = 0; i < cgrps; i++) {
printf("grp : %d\n", i);
show_lnvm_id_grp((void *)&id->groups[i], human);
}
}
static void show_lnvm_id20_ns(struct nvme_nvm_id20 *id, unsigned int flags)
{
int human = flags & VERBOSE;
uint32_t mccap = (uint32_t) le32_to_cpu(id->mccap);
printf("ver_major : %#x\n", id->mjr);
printf("ver_minor : %#x\n", id->mnr);
printf("mccap : %#x\n", mccap);
if (human) {
if (mccap & (1 << LNVM_IDFY_CAP_VCOPY))
printf(" [0]: Vector copy support\n");
if (mccap & (1 << LNVM_IDFY_CAP_MRESETS))
printf(" [1]: Multiple resets support\n");
}
printf("wit : %d\n", id->wit);
printf("lba format\n");
printf(" grp len : %d\n", id->lbaf.grp_len);
printf(" pu len : %d\n", id->lbaf.pu_len);
printf(" chk len : %d\n", id->lbaf.chk_len);
printf(" clba len : %d\n", id->lbaf.lba_len);
printf("geometry\n");
printf(" num_grp : %d\n", le16_to_cpu(id->num_grp));
printf(" num_pu : %d\n", le16_to_cpu(id->num_pu));
printf(" num_chk : %d\n", le32_to_cpu(id->num_chk));
printf(" clba : %d\n", le32_to_cpu(id->clba));
printf("write req\n");
printf(" ws_min : %d\n", le32_to_cpu(id->ws_min));
printf(" ws_opt : %d\n", le32_to_cpu(id->ws_opt));
printf(" mw_cunits : %d\n", le32_to_cpu(id->mw_cunits));
printf(" maxoc : %d\n", le32_to_cpu(id->maxoc));
printf(" maxocpu : %d\n", le32_to_cpu(id->maxocpu));
printf("perf metrics\n");
printf(" trdt (ns) : %d\n", le32_to_cpu(id->trdt));
printf(" trdm (ns) : %d\n", le32_to_cpu(id->trdm));
printf(" twrt (ns) : %d\n", le32_to_cpu(id->twrt));
printf(" twrm (ns) : %d\n", le32_to_cpu(id->twrm));
printf(" tcrst (ns) : %d\n", le32_to_cpu(id->tcrst));
printf(" tcrsm (ns) : %d\n", le32_to_cpu(id->tcrsm));
}
static void show_lnvm_id_ns(struct nvme_nvm_id *id, unsigned int flags)
{
switch (id->ver_id) {
case 1:
show_lnvm_id12_ns((void *) id, flags);
break;
case 2:
show_lnvm_id20_ns((void *) id, flags);
break;
default:
fprintf(stderr, "Version %d not supported.\n",
id->ver_id);
}
}
int lnvm_get_identity(int fd, int nsid, struct nvme_nvm_id *nvm_id)
{
struct nvme_admin_cmd cmd = {
.opcode = nvme_nvm_admin_identity,
.nsid = nsid,
.addr = (__u64)(uintptr_t)nvm_id,
.data_len = sizeof(struct nvme_nvm_id),
};
return nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}
int lnvm_do_id_ns(int fd, int nsid, unsigned int flags)
{
struct nvme_nvm_id nvm_id;
int err;
err = lnvm_get_identity(fd, nsid, &nvm_id);
if (!err) {
if (flags & BINARY)
d_raw((unsigned char *)&nvm_id, sizeof(nvm_id));
else
show_lnvm_id_ns(&nvm_id, flags);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, nsid);
return err;
}
static inline const char *print_chunk_state(__u8 cs)
{
switch (cs) {
case 1 << 0: return "FREE";
case 1 << 1: return "CLOSED";
case 1 << 2: return "OPEN";
case 1 << 3: return "OFFLINE";
default: return "UNKNOWN";
}
}
static inline const char *print_chunk_type(__u8 ct)
{
switch (ct & 0xF) {
case 1 << 0: return "SEQWRITE_REQ";
case 1 << 1: return "RANDWRITE_ALLOWED";
default: return "UNKNOWN";
}
}
static inline const char *print_chunk_attr(__u8 ct)
{
switch (ct & 0xF0) {
case 1 << 4: return "DEVIATED";
default: return "NONE";
}
}
static void show_lnvm_chunk_log(struct nvme_nvm_chunk_desc *chunk_log,
__u32 data_len)
{
int nr_entry = data_len / sizeof(struct nvme_nvm_chunk_desc);
int idx;
printf("Total chunks in namespace: %d\n", nr_entry);
for (idx = 0; idx < nr_entry; idx++) {
struct nvme_nvm_chunk_desc *desc = &chunk_log[idx];
printf(" [%5d] { ", idx);
printf("SLBA: 0x%016"PRIx64, le64_to_cpu(desc->slba));
printf(", WP: 0x%016"PRIx64, le64_to_cpu(desc->wp));
printf(", CNLB: 0x%016"PRIx64, le64_to_cpu(desc->cnlb));
printf(", State: %-8s", print_chunk_state(desc->cs));
printf(", Type: %-20s", print_chunk_type(desc->ct));
printf(", Attr: %-8s", print_chunk_attr(desc->ct));
printf(", WLI: %4d }\n", desc->wli);
}
}
int lnvm_do_chunk_log(int fd, __u32 nsid, __u32 data_len, void *data,
unsigned int flags)
{
int err;
err = nvme_get_log13(fd, nsid, NVM_LID_CHUNK_INFO, 0, 0, 0,
false, data_len, data);
if (err > 0) {
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, nsid);
goto out;
} else if (err < 0) {
err = -errno;
perror("nvme_get_log13");
goto out;
}
if (flags & BINARY)
d_raw(data, data_len);
else
show_lnvm_chunk_log(data, data_len);
out:
return err;
}
static void show_lnvm_bbtbl(struct nvme_nvm_bb_tbl *tbl)
{
printf("verid : %#x\n", (uint16_t)le16_to_cpu(tbl->verid));
printf("tblks : %d\n", (uint32_t)le32_to_cpu(tbl->tblks));
printf("tfact : %d\n", (uint32_t)le32_to_cpu(tbl->tfact));
printf("tgrown : %d\n", (uint32_t)le32_to_cpu(tbl->tgrown));
printf("tdresv : %d\n", (uint32_t)le32_to_cpu(tbl->tdresv));
printf("thresv : %d\n", (uint32_t)le32_to_cpu(tbl->thresv));
printf("Use raw output to retrieve table.\n");
}
static int __lnvm_do_get_bbtbl(int fd, struct nvme_nvm_id12 *id,
struct ppa_addr ppa,
unsigned int flags)
{
struct nvme_nvm_id12_group *grp = &id->groups[0];
int bbtblsz = ((uint16_t)le16_to_cpu(grp->num_blk) * grp->num_pln);
int bufsz = bbtblsz + sizeof(struct nvme_nvm_bb_tbl);
struct nvme_nvm_bb_tbl *bbtbl;
int err;
bbtbl = calloc(1, bufsz);
if (!bbtbl)
return -ENOMEM;
struct nvme_nvm_getbbtbl cmd = {
.opcode = nvme_nvm_admin_get_bb_tbl,
.nsid = cpu_to_le32(1),
.addr = (__u64)(uintptr_t)bbtbl,
.data_len = bufsz,
.ppa = cpu_to_le64(ppa.ppa),
};
void *tmp = &cmd;
struct nvme_passthru_cmd *nvme_cmd = tmp;
err = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, nvme_cmd);
if (err > 0) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
free(bbtbl);
return err;
}
if (flags & BINARY)
d_raw((unsigned char *)&bbtbl->blk, bbtblsz);
else {
printf("LightNVM Bad Block Stats:\n");
show_lnvm_bbtbl(bbtbl);
}
free(bbtbl);
return 0;
}
int lnvm_do_get_bbtbl(int fd, int nsid, int lunid, int chid, unsigned int flags)
{
struct nvme_nvm_id12 nvm_id;
struct ppa_addr ppa;
int err;
void *tmp = &nvm_id;
err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
if (err) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
if (nvm_id.ver_id != 1) {
fprintf(stderr, "Get bad block table not supported on version %d\n",
nvm_id.ver_id);
return -EINVAL;
}
if (chid >= nvm_id.groups[0].num_ch ||
lunid >= nvm_id.groups[0].num_lun) {
fprintf(stderr, "Out of bound channel id or LUN id\n");
return -EINVAL;
}
ppa.ppa = 0;
ppa.g.lun = lunid;
ppa.g.ch = chid;
ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
return __lnvm_do_get_bbtbl(fd, &nvm_id, ppa, flags);
}
static int __lnvm_do_set_bbtbl(int fd, struct ppa_addr ppa, __u8 value)
{
int err;
struct nvme_nvm_setbbtbl cmd = {
.opcode = nvme_nvm_admin_set_bb_tbl,
.nsid = cpu_to_le32(1),
.ppa = cpu_to_le64(ppa.ppa),
.nlb = cpu_to_le16(0),
.value = value,
};
void *tmp = &cmd;
struct nvme_passthru_cmd *nvme_cmd = tmp;
err = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, nvme_cmd);
if (err > 0) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
return 0;
}
int lnvm_do_set_bbtbl(int fd, int nsid,
int chid, int lunid, int plnid, int blkid,
__u8 value)
{
struct nvme_nvm_id12 nvm_id;
struct ppa_addr ppa;
int err;
void *tmp = &nvm_id;
err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
if (err) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
if (nvm_id.ver_id != 1) {
fprintf(stderr, "Set bad block table not supported on version %d\n",
nvm_id.ver_id);
return -EINVAL;
}
if (chid >= nvm_id.groups[0].num_ch ||
lunid >= nvm_id.groups[0].num_lun ||
plnid >= nvm_id.groups[0].num_pln ||
blkid >= le16_to_cpu(nvm_id.groups[0].num_blk)) {
fprintf(stderr, "Out of bound channel id, LUN id, plane id, or"\
"block id\n");
return -EINVAL;
}
ppa.ppa = 0;
ppa.g.lun = lunid;
ppa.g.ch = chid;
ppa.g.pl = plnid;
ppa.g.blk = blkid;
ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
return __lnvm_do_set_bbtbl(fd, ppa, value);
}
|