forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme-lightnvm.c
463 lines (388 loc) · 11 KB
/
nvme-lightnvm.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
/*
* lightnvm.c -- LightNVM NVMe integration.
*
* Copyright (c) 2016, CNEX Labs.
*
* Written by Matias Bjoerling <[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.
*/
#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);
strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN);
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)
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)
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 flags)
{
struct nvm_ioctl_create c;
int fd, ret;
fd = lnvm_open();
if (fd < 0)
return fd;
strncpy(c.dev, devname, DISK_NAME_LEN);
strncpy(c.tgtname, tgtname, DISK_NAME_LEN);
strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX);
c.flags = 0;
c.conf.type = 0;
c.conf.s.lun_begin = lun_begin;
c.conf.s.lun_end = lun_end;
c.flags = flags;
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);
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);
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(struct nvme_nvm_id_group *grp)
{
printf(" mtype : %d\n", grp->mtype);
printf(" fmtype : %d\n", grp->fmtype);
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", (uint32_t)le32_to_cpu(grp->mpos));
printf(" mccap : %#x\n", (uint32_t)le32_to_cpu(grp->mccap));
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_id_ns(struct nvme_nvm_id *id)
{
int i;
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);
printf("cgrps : %d\n", id->cgrps);
printf("cap : %#x\n", (uint32_t)le32_to_cpu(id->cap));
printf("dom : %#x\n", (uint32_t)le32_to_cpu(id->dom));
show_lnvm_ppaf(&id->ppaf);
for (i = 0; i < id->cgrps; i++) {
printf("grp : %d\n", i);
show_lnvm_id_grp(&id->groups[i]);
}
}
static 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 = 0x1000,
};
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 & RAW)
d_raw((unsigned char *)&nvm_id, sizeof(nvm_id));
else {
printf("LightNVM Identify Geometry (%d):\n", nsid);
show_lnvm_id_ns(&nvm_id);
}
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
nvme_status_to_string(err), err, nsid);
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_id *id,
struct ppa_addr ppa,
unsigned int flags)
{
struct nvme_nvm_id_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 = cpu_to_le32(bufsz),
.ppa = cpu_to_le64(ppa.ppa),
};
err = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD,
(struct nvme_passthru_cmd *)&cmd);
if (err > 0) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
free(bbtbl);
return err;
}
if (flags & RAW)
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_id nvm_id;
struct ppa_addr ppa;
int err;
err = lnvm_get_identity(fd, nsid, &nvm_id);
if (err) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
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,
};
err = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD,
(struct nvme_passthru_cmd *)&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_id nvm_id;
struct ppa_addr ppa;
int err;
err = lnvm_get_identity(fd, nsid, &nvm_id);
if (err) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
nvme_status_to_string(err), err);
return err;
}
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);
}