forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmds.c
More file actions
616 lines (528 loc) · 14.2 KB
/
cmds.c
File metadata and controls
616 lines (528 loc) · 14.2 KB
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
*
* Authors: Keith Busch <[email protected]>
* Chaitanya Kulkarni <[email protected]>
*/
#include <ccan/endian/endian.h>
#include <ccan/minmax/minmax.h>
#include <libnvme.h>
#include "cleanup.h"
#include "private.h"
int nvme_fw_download_seq(struct nvme_transport_handle *hdl, bool ish,
__u32 size, __u32 xfer, __u32 offset, void *buf)
{
struct nvme_passthru_cmd cmd;
void *data = buf;
int err = 0;
if (ish && nvme_transport_handle_is_mi(hdl))
nvme_init_mi_cmd_flags(&cmd, ish);
while (size > 0) {
__u32 chunk = min(xfer, size);
err = nvme_init_fw_download(&cmd, data, chunk, offset);
if (err)
break;
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
break;
data += chunk;
size -= chunk;
offset += chunk;
}
return err;
}
int nvme_set_etdas(struct nvme_transport_handle *hdl, bool *changed)
{
struct nvme_feat_host_behavior da4;
struct nvme_passthru_cmd cmd;
int err;
nvme_init_get_features_host_behavior(&cmd, 0, &da4);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
return err;
if (da4.etdas) {
*changed = false;
return 0;
}
da4.etdas = 1;
nvme_init_set_features_host_behavior(&cmd, false, &da4);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
return err;
*changed = true;
return 0;
}
int nvme_clear_etdas(struct nvme_transport_handle *hdl, bool *changed)
{
struct nvme_feat_host_behavior da4;
struct nvme_passthru_cmd cmd;
int err;
nvme_init_get_features_host_behavior(&cmd, 0, &da4);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
return err;
if (!da4.etdas) {
*changed = false;
return 0;
}
da4.etdas = 0;
nvme_init_set_features_host_behavior(&cmd, false, &da4);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
return err;
*changed = true;
return 0;
}
int nvme_get_uuid_list(struct nvme_transport_handle *hdl,
struct nvme_id_uuid_list *uuid_list)
{
struct nvme_passthru_cmd cmd;
struct nvme_id_ctrl ctrl;
int err;
memset(&ctrl, 0, sizeof(struct nvme_id_ctrl));
nvme_init_identify_ctrl(&cmd, &ctrl);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err) {
nvme_msg(hdl->ctx, LOG_ERR,
"ERROR: nvme_identify_ctrl() failed 0x%x\n", err);
return err;
}
if ((ctrl.ctratt & NVME_CTRL_CTRATT_UUID_LIST) ==
NVME_CTRL_CTRATT_UUID_LIST) {
nvme_init_identify_uuid_list(&cmd, uuid_list);
err = nvme_submit_admin_passthru(hdl, &cmd);
}
return err;
}
int nvme_get_telemetry_max(struct nvme_transport_handle *hdl,
enum nvme_telemetry_da *da, size_t *data_tx)
{
_cleanup_free_ struct nvme_id_ctrl *id_ctrl = NULL;
struct nvme_passthru_cmd cmd;
int err;
id_ctrl = __nvme_alloc(sizeof(*id_ctrl));
if (!id_ctrl)
return -ENOMEM;
nvme_init_identify_ctrl(&cmd, id_ctrl);
err = nvme_submit_admin_passthru(hdl, &cmd);
if (err)
return err;
if (data_tx) {
*data_tx = id_ctrl->mdts;
if (id_ctrl->mdts) {
/*
* assuming CAP.MPSMIN is zero minimum Memory Page Size
* is at least 4096 bytes
*/
*data_tx = (1 << id_ctrl->mdts) * 4096;
}
}
if (da) {
if (id_ctrl->lpa & 0x8)
*da = NVME_TELEMETRY_DA_3;
if (id_ctrl->lpa & 0x40)
*da = NVME_TELEMETRY_DA_4;
}
return err;
}
int nvme_get_telemetry_log(struct nvme_transport_handle *hdl, bool create,
bool ctrl, bool rae, size_t max_data_tx,
enum nvme_telemetry_da da, struct nvme_telemetry_log **buf,
size_t *size)
{
static const __u32 xfer = NVME_LOG_TELEM_BLOCK_SIZE;
struct nvme_telemetry_log *telem;
struct nvme_passthru_cmd cmd;
_cleanup_free_ void *log = NULL;
void *tmp;
int err;
size_t dalb;
*size = 0;
log = __nvme_alloc(xfer);
if (!log)
return -ENOMEM;
if (ctrl) {
nvme_init_get_log_telemetry_ctrl(&cmd, 0, log, xfer);
err = nvme_get_log(hdl, &cmd, true, xfer);
} else {
if (create) {
nvme_init_get_log_create_telemetry_host_mcda(&cmd,
da, log);
err = nvme_get_log(hdl, &cmd, false, xfer);
} else {
nvme_init_get_log_telemetry_host(&cmd, 0, log, xfer);
err = nvme_get_log(hdl, &cmd, false, xfer);
}
}
if (err)
return err;
telem = log;
if (ctrl && !telem->ctrlavail) {
*buf = log;
log = NULL;
*size = xfer;
return 0;
}
switch (da) {
case NVME_TELEMETRY_DA_1:
dalb = le16_to_cpu(telem->dalb1);
break;
case NVME_TELEMETRY_DA_2:
dalb = le16_to_cpu(telem->dalb2);
break;
case NVME_TELEMETRY_DA_3:
/* dalb3 >= dalb2 >= dalb1 */
dalb = le16_to_cpu(telem->dalb3);
break;
case NVME_TELEMETRY_DA_4:
dalb = le32_to_cpu(telem->dalb4);
break;
default:
return -EINVAL;
}
if (dalb == 0)
return -ENOENT;
*size = (dalb + 1) * xfer;
tmp = __nvme_realloc(log, *size);
if (!tmp)
return -ENOMEM;
log = tmp;
if (ctrl)
nvme_init_get_log_telemetry_ctrl(&cmd, 0, log, *size);
else
nvme_init_get_log_telemetry_host(&cmd, 0, log, *size);
err = nvme_get_log(hdl, &cmd, rae, max_data_tx);
if (err)
return err;
*buf = log;
log = NULL;
return 0;
}
static int nvme_check_get_telemetry_log(struct nvme_transport_handle *hdl,
bool create, bool ctrl, bool rae,
struct nvme_telemetry_log **log, enum nvme_telemetry_da da,
size_t *size)
{
enum nvme_telemetry_da max_da = 0;
int err;
err = nvme_get_telemetry_max(hdl, &max_da, NULL);
if (err)
return err;
if (da > max_da)
return -ENOENT;
return nvme_get_telemetry_log(hdl, create, ctrl, rae, 4096, da,
log, size);
}
int nvme_get_ctrl_telemetry(struct nvme_transport_handle *hdl, bool rae,
struct nvme_telemetry_log **log,
enum nvme_telemetry_da da, size_t *size)
{
return nvme_check_get_telemetry_log(hdl, false, true, rae, log,
da, size);
}
int nvme_get_host_telemetry(struct nvme_transport_handle *hdl,
struct nvme_telemetry_log **log,
enum nvme_telemetry_da da, size_t *size)
{
return nvme_check_get_telemetry_log(hdl, false, false, false, log,
da, size);
}
int nvme_get_new_host_telemetry(struct nvme_transport_handle *hdl,
struct nvme_telemetry_log **log,
enum nvme_telemetry_da da, size_t *size)
{
return nvme_check_get_telemetry_log(hdl, true, false, false, log,
da, size);
}
int nvme_get_lba_status_log(struct nvme_transport_handle *hdl, bool rae,
struct nvme_lba_status_log **log)
{
_cleanup_free_ struct nvme_lba_status_log *buf = NULL;
struct nvme_passthru_cmd cmd;
__u32 size;
void *tmp;
int err;
buf = malloc(sizeof(*buf));
if (!buf)
return -ENOMEM;
nvme_init_get_log_lba_status(&cmd, 0, log, sizeof(*buf));
err = nvme_get_log(hdl, &cmd, true, sizeof(*buf));
if (err) {
*log = NULL;
return err;
}
size = le32_to_cpu(buf->lslplen);
if (!size) {
*log = buf;
buf = NULL;
return 0;
}
tmp = realloc(buf, size);
if (!tmp) {
*log = NULL;
return -ENOMEM;
}
buf = tmp;
nvme_init_get_log_lba_status(&cmd, 0, buf, size);
err = nvme_get_log(hdl, &cmd, rae, NVME_LOG_PAGE_PDU_SIZE);
if (err) {
*log = NULL;
return err;
}
*log = buf;
buf = NULL;
return 0;
}
static int nvme_ns_attachment(struct nvme_transport_handle *hdl, bool ish,
__u32 nsid, __u16 num_ctrls, __u16 *ctrlist, bool attach)
{
struct nvme_ctrl_list cntlist = { 0 };
struct nvme_passthru_cmd cmd;
nvme_init_ctrl_list(&cntlist, num_ctrls, ctrlist);
if (ish && nvme_transport_handle_is_mi(hdl))
nvme_init_mi_cmd_flags(&cmd, ish);
if (attach)
nvme_init_ns_attach_ctrls(&cmd, nsid, &cntlist);
else
nvme_init_ns_detach_ctrls(&cmd, nsid, &cntlist);
return nvme_submit_admin_passthru(hdl, &cmd);
}
int nvme_namespace_attach_ctrls(struct nvme_transport_handle *hdl, bool ish,
__u32 nsid, __u16 num_ctrls, __u16 *ctrlist)
{
return nvme_ns_attachment(hdl, ish, nsid, num_ctrls, ctrlist, true);
}
int nvme_namespace_detach_ctrls(struct nvme_transport_handle *hdl, bool ish,
__u32 nsid, __u16 num_ctrls, __u16 *ctrlist)
{
return nvme_ns_attachment(hdl, ish, nsid, num_ctrls, ctrlist, false);
}
size_t nvme_get_ana_log_len_from_id_ctrl(const struct nvme_id_ctrl *id_ctrl,
bool rgo)
{
__u32 nanagrpid = le32_to_cpu(id_ctrl->nanagrpid);
size_t size = sizeof(struct nvme_ana_log) +
nanagrpid * sizeof(struct nvme_ana_group_desc);
return rgo ? size : size + le32_to_cpu(id_ctrl->mnan) * sizeof(__le32);
}
int nvme_get_ana_log_len(struct nvme_transport_handle *hdl, size_t *analen)
{
_cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
struct nvme_passthru_cmd cmd;
int ret;
ctrl = __nvme_alloc(sizeof(*ctrl));
if (!ctrl)
return -ENOMEM;
nvme_init_identify_ctrl(&cmd, ctrl);
ret = nvme_submit_admin_passthru(hdl, &cmd);
if (ret)
return ret;
*analen = nvme_get_ana_log_len_from_id_ctrl(ctrl, false);
return 0;
}
int nvme_get_logical_block_size(struct nvme_transport_handle *hdl,
__u32 nsid, int *blksize)
{
_cleanup_free_ struct nvme_id_ns *ns = NULL;
struct nvme_passthru_cmd cmd;
__u8 flbas;
int ret;
ns = __nvme_alloc(sizeof(*ns));
if (!ns)
return -ENOMEM;
nvme_init_identify_ns(&cmd, nsid, ns);
ret = nvme_submit_admin_passthru(hdl, &cmd);
if (ret)
return ret;
nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas);
*blksize = 1 << ns->lbaf[flbas].ds;
return 0;
}
static inline void nvme_init_copy_range_elbt(__u8 *elbt, __u64 eilbrt)
{
int i;
for (i = 0; i < 8; i++)
elbt[9 - i] = (eilbrt >> (8 * i)) & 0xff;
elbt[1] = 0;
elbt[0] = 0;
}
void nvme_init_copy_range(struct nvme_copy_range *copy, __u16 *nlbs,
__u64 *slbas, __u32 *eilbrts, __u32 *elbatms,
__u32 *elbats, __u16 nr)
{
int i;
for (i = 0; i < nr; i++) {
copy[i].nlb = cpu_to_le16(nlbs[i]);
copy[i].slba = cpu_to_le64(slbas[i]);
copy[i].eilbrt = cpu_to_le32(eilbrts[i]);
copy[i].elbatm = cpu_to_le16(elbatms[i]);
copy[i].elbat = cpu_to_le16(elbats[i]);
}
}
void nvme_init_copy_range_f1(struct nvme_copy_range_f1 *copy, __u16 *nlbs,
__u64 *slbas, __u64 *eilbrts, __u32 *elbatms,
__u32 *elbats, __u16 nr)
{
int i;
for (i = 0; i < nr; i++) {
copy[i].nlb = cpu_to_le16(nlbs[i]);
copy[i].slba = cpu_to_le64(slbas[i]);
copy[i].elbatm = cpu_to_le16(elbatms[i]);
copy[i].elbat = cpu_to_le16(elbats[i]);
nvme_init_copy_range_elbt(copy[i].elbt, eilbrts[i]);
}
}
void nvme_init_copy_range_f2(struct nvme_copy_range_f2 *copy, __u32 *snsids,
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
__u32 *eilbrts, __u32 *elbatms, __u32 *elbats,
__u16 nr)
{
int i;
for (i = 0; i < nr; i++) {
copy[i].snsid = cpu_to_le32(snsids[i]);
copy[i].nlb = cpu_to_le16(nlbs[i]);
copy[i].slba = cpu_to_le64(slbas[i]);
copy[i].sopt = cpu_to_le16(sopts[i]);
copy[i].eilbrt = cpu_to_le32(eilbrts[i]);
copy[i].elbatm = cpu_to_le16(elbatms[i]);
copy[i].elbat = cpu_to_le16(elbats[i]);
}
}
void nvme_init_copy_range_f3(struct nvme_copy_range_f3 *copy, __u32 *snsids,
__u16 *nlbs, __u64 *slbas, __u16 *sopts,
__u64 *eilbrts, __u32 *elbatms, __u32 *elbats,
__u16 nr)
{
int i;
for (i = 0; i < nr; i++) {
copy[i].snsid = cpu_to_le32(snsids[i]);
copy[i].nlb = cpu_to_le16(nlbs[i]);
copy[i].slba = cpu_to_le64(slbas[i]);
copy[i].sopt = cpu_to_le16(sopts[i]);
copy[i].elbatm = cpu_to_le16(elbatms[i]);
copy[i].elbat = cpu_to_le16(elbats[i]);
nvme_init_copy_range_elbt(copy[i].elbt, eilbrts[i]);
}
}
void nvme_init_dsm_range(struct nvme_dsm_range *dsm, __u32 *ctx_attrs,
__u32 *llbas, __u64 *slbas, __u16 nr_ranges)
{
int i;
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]);
}
}
void nvme_init_ctrl_list(struct nvme_ctrl_list *cntlist, __u16 num_ctrls,
__u16 *ctrlist)
{
int i;
cntlist->num = cpu_to_le16(num_ctrls);
for (i = 0; i < num_ctrls; i++)
cntlist->identifier[i] = cpu_to_le16(ctrlist[i]);
}
int nvme_get_feature_length(int fid, __u32 cdw11, enum nvme_data_tfr dir,
__u32 *len)
{
switch (fid) {
case NVME_FEAT_FID_LBA_RANGE:
*len = sizeof(struct nvme_lba_range_type);
break;
case NVME_FEAT_FID_AUTO_PST:
*len = sizeof(struct nvme_feat_auto_pst);
break;
case NVME_FEAT_FID_PLM_CONFIG:
*len = sizeof(struct nvme_plm_config);
break;
case NVME_FEAT_FID_TIMESTAMP:
*len = sizeof(struct nvme_timestamp);
break;
case NVME_FEAT_FID_HOST_BEHAVIOR:
*len = sizeof(struct nvme_feat_host_behavior);
break;
case NVME_FEAT_FID_HOST_ID:
*len = (cdw11 & 0x1) ? 16 : 8;
break;
case NVME_FEAT_FID_HOST_MEM_BUF:
if (dir == NVME_DATA_TFR_HOST_TO_CTRL) {
*len = 0;
break;
}
*len = sizeof(struct nvme_host_mem_buf_attrs);
break;
case NVME_FEAT_FID_ARBITRATION:
case NVME_FEAT_FID_POWER_MGMT:
case NVME_FEAT_FID_TEMP_THRESH:
case NVME_FEAT_FID_ERR_RECOVERY:
case NVME_FEAT_FID_VOLATILE_WC:
case NVME_FEAT_FID_NUM_QUEUES:
case NVME_FEAT_FID_IRQ_COALESCE:
case NVME_FEAT_FID_IRQ_CONFIG:
case NVME_FEAT_FID_WRITE_ATOMIC:
case NVME_FEAT_FID_ASYNC_EVENT:
case NVME_FEAT_FID_KATO:
case NVME_FEAT_FID_HCTM:
case NVME_FEAT_FID_NOPSC:
case NVME_FEAT_FID_RRL:
case NVME_FEAT_FID_PLM_WINDOW:
case NVME_FEAT_FID_LBA_STS_INTERVAL:
case NVME_FEAT_FID_SANITIZE:
case NVME_FEAT_FID_ENDURANCE_EVT_CFG:
case NVME_FEAT_FID_SW_PROGRESS:
case NVME_FEAT_FID_RESV_MASK:
case NVME_FEAT_FID_RESV_PERSIST:
case NVME_FEAT_FID_WRITE_PROTECT:
case NVME_FEAT_FID_POWER_LIMIT:
case NVME_FEAT_FID_POWER_MEASUREMENT:
*len = 0;
break;
case NVME_FEAT_FID_ENH_CTRL_METADATA:
case NVME_FEAT_FID_CTRL_METADATA:
case NVME_FEAT_FID_NS_METADATA:
*len = sizeof(struct nvme_host_metadata);
break;
case NVME_FEAT_FID_PERF_CHARACTERISTICS:
*len = sizeof(struct nvme_perf_characteristics);
break;
case NVME_FEAT_FID_FDP_EVENTS:
*len = NVME_FEAT_FDPE_NOET_MASK *
sizeof(struct nvme_fdp_supported_event_desc);
break;
default:
return -EINVAL;
}
return 0;
}
int nvme_get_directive_receive_length(enum nvme_directive_dtype dtype,
enum nvme_directive_receive_doper doper, __u32 *len)
{
switch (dtype) {
case NVME_DIRECTIVE_DTYPE_IDENTIFY:
switch (doper) {
case NVME_DIRECTIVE_RECEIVE_IDENTIFY_DOPER_PARAM:
*len = sizeof(struct nvme_id_directives);
return 0;
default:
return -EINVAL;
}
case NVME_DIRECTIVE_DTYPE_STREAMS:
switch (doper) {
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_PARAM:
*len = sizeof(struct nvme_streams_directive_params);
return 0;
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_STATUS:
*len = (128 * 1024) * sizeof(__le16);
return 0;
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_RESOURCE:
*len = 0;
return 0;
default:
return -EINVAL;
}
default:
return -EINVAL;
}
}