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
|
// SPDX-License-Identifier: GPL-2.0-or-later
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <linux/fs.h>
#include <sys/stat.h>
#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#define CREATE_CMD
#include "fdp.h"
static int fdp_configs(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
const char *desc = "Get Flexible Data Placement Configurations";
const char *egid = "Endurance group identifier";
const char *human_readable = "show log in readable format";
const char *raw = "use binary output";
nvme_print_flags_t flags;
struct nvme_dev *dev;
struct nvme_fdp_config_log hdr;
void *log = NULL;
int err;
struct config {
__u16 egid;
char *output_format;
bool human_readable;
bool raw_binary;
};
struct config cfg = {
.egid = 0,
.output_format = "normal",
.raw_binary = false,
};
OPT_ARGS(opts) = {
OPT_UINT("endgrp-id", 'e', &cfg.egid, egid),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(cfg.output_format, &flags);
if (err < 0)
goto out;
if (cfg.raw_binary)
flags = BINARY;
if (cfg.human_readable)
flags |= VERBOSE;
if (!cfg.egid) {
fprintf(stderr, "endurance group identifier required\n");
err = -EINVAL;
goto out;
}
err = nvme_get_log_fdp_configurations(dev->direct.fd, cfg.egid, 0,
sizeof(hdr), &hdr);
if (err) {
nvme_show_status(errno);
goto out;
}
log = malloc(hdr.size);
if (!log) {
err = -ENOMEM;
goto out;
}
err = nvme_get_log_fdp_configurations(dev->direct.fd, cfg.egid, 0,
hdr.size, log);
if (err) {
nvme_show_status(errno);
goto out;
}
nvme_show_fdp_configs(log, hdr.size, flags);
out:
dev_close(dev);
free(log);
return err;
}
static int fdp_usage(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Get Flexible Data Placement Reclaim Unit Handle Usage";
const char *egid = "Endurance group identifier";
const char *raw = "use binary output";
nvme_print_flags_t flags;
struct nvme_dev *dev;
struct nvme_fdp_ruhu_log hdr;
size_t len;
void *log = NULL;
int err;
struct config {
__u16 egid;
char *output_format;
bool raw_binary;
};
struct config cfg = {
.egid = 0,
.output_format = "normal",
.raw_binary = false,
};
OPT_ARGS(opts) = {
OPT_UINT("endgrp-id", 'e', &cfg.egid, egid),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(cfg.output_format, &flags);
if (err < 0)
goto out;
if (cfg.raw_binary)
flags = BINARY;
err = nvme_get_log_reclaim_unit_handle_usage(dev->direct.fd, cfg.egid,
0, sizeof(hdr), &hdr);
if (err) {
nvme_show_status(err);
goto out;
}
len = sizeof(hdr) + le16_to_cpu(hdr.nruh) * sizeof(struct nvme_fdp_ruhu_desc);
log = malloc(len);
if (!log) {
err = -ENOMEM;
goto out;
}
err = nvme_get_log_reclaim_unit_handle_usage(dev->direct.fd, cfg.egid,
0, len, log);
if (err) {
nvme_show_status(err);
goto out;
}
nvme_show_fdp_usage(log, len, flags);
out:
dev_close(dev);
free(log);
return err;
}
static int fdp_stats(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Get Flexible Data Placement Statistics";
const char *egid = "Endurance group identifier";
const char *raw = "use binary output";
nvme_print_flags_t flags;
struct nvme_dev *dev;
struct nvme_fdp_stats_log stats;
int err;
struct config {
__u16 egid;
char *output_format;
bool raw_binary;
};
struct config cfg = {
.egid = 0,
.output_format = "normal",
.raw_binary = false,
};
OPT_ARGS(opts) = {
OPT_UINT("endgrp-id", 'e', &cfg.egid, egid),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(cfg.output_format, &flags);
if (err < 0)
goto out;
if (cfg.raw_binary)
flags = BINARY;
if (!cfg.egid) {
fprintf(stderr, "endurance group identifier required\n");
err = -EINVAL;
goto out;
}
memset(&stats, 0x0, sizeof(stats));
err = nvme_get_log_fdp_stats(dev->direct.fd, cfg.egid, 0, sizeof(stats), &stats);
if (err) {
nvme_show_status(err);
goto out;
}
nvme_show_fdp_stats(&stats, flags);
out:
dev_close(dev);
return err;
}
static int fdp_events(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Get Flexible Data Placement Events";
const char *egid = "Endurance group identifier";
const char *host_events = "Get host events";
const char *raw = "use binary output";
nvme_print_flags_t flags;
struct nvme_dev *dev;
struct nvme_fdp_events_log events;
int err;
struct config {
__u16 egid;
bool host_events;
char *output_format;
bool raw_binary;
};
struct config cfg = {
.egid = 0,
.host_events = false,
.output_format = "normal",
.raw_binary = false,
};
OPT_ARGS(opts) = {
OPT_UINT("endgrp-id", 'e', &cfg.egid, egid),
OPT_FLAG("host-events", 'E', &cfg.host_events, host_events),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(cfg.output_format, &flags);
if (err < 0)
goto out;
if (cfg.raw_binary)
flags = BINARY;
if (!cfg.egid) {
fprintf(stderr, "endurance group identifier required\n");
err = -EINVAL;
goto out;
}
memset(&events, 0x0, sizeof(events));
err = nvme_get_log_fdp_events(dev->direct.fd, cfg.egid,
cfg.host_events, 0, sizeof(events), &events);
if (err) {
nvme_show_status(err);
goto out;
}
nvme_show_fdp_events(&events, flags);
out:
dev_close(dev);
return err;
}
static int fdp_status(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Reclaim Unit Handle Status";
const char *namespace_id = "Namespace identifier";
const char *raw = "use binary output";
nvme_print_flags_t flags;
struct nvme_dev *dev;
struct nvme_fdp_ruh_status hdr;
size_t len;
void *buf = NULL;
int err = -1;
struct config {
__u32 namespace_id;
char *output_format;
bool raw_binary;
};
struct config cfg = {
.output_format = "normal",
.raw_binary = false,
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
err = validate_output_format(cfg.output_format, &flags);
if (err < 0)
goto out;
if (cfg.raw_binary)
flags = BINARY;
if (!cfg.namespace_id) {
err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id);
if (err < 0) {
perror("get-namespace-id");
goto out;
}
}
err = nvme_fdp_reclaim_unit_handle_status(dev_fd(dev),
cfg.namespace_id, sizeof(hdr), &hdr);
if (err) {
nvme_show_status(err);
goto out;
}
len = sizeof(struct nvme_fdp_ruh_status) +
le16_to_cpu(hdr.nruhsd) * sizeof(struct nvme_fdp_ruh_status_desc);
buf = malloc(len);
if (!buf) {
err = -ENOMEM;
goto out;
}
err = nvme_fdp_reclaim_unit_handle_status(dev_fd(dev),
cfg.namespace_id, len, buf);
if (err) {
nvme_show_status(err);
goto out;
}
nvme_show_fdp_ruh_status(buf, len, flags);
out:
free(buf);
dev_close(dev);
return err;
}
static int fdp_update(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Reclaim Unit Handle Update";
const char *namespace_id = "Namespace identifier";
const char *_pids = "Comma-separated list of placement identifiers to update";
struct nvme_dev *dev;
unsigned short pids[256];
__u16 buf[256];
int npids;
int err = -1;
struct config {
__u32 namespace_id;
char *pids;
};
struct config cfg = {
.pids = "",
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_LIST("pids", 'p', &cfg.pids, _pids),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
npids = argconfig_parse_comma_sep_array_short(cfg.pids, pids, ARRAY_SIZE(pids));
if (npids < 0) {
perror("could not parse pids");
err = -EINVAL;
goto out;
} else if (npids == 0) {
fprintf(stderr, "no placement identifiers set\n");
err = -EINVAL;
goto out;
}
if (!cfg.namespace_id) {
err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id);
if (err < 0) {
perror("get-namespace-id");
goto out;
}
}
for (unsigned int i = 0; i < npids; i++)
buf[i] = cpu_to_le16(pids[i]);
err = nvme_fdp_reclaim_unit_handle_update(dev_fd(dev), cfg.namespace_id, npids, buf);
if (err) {
nvme_show_status(err);
goto out;
}
printf("update: Success\n");
out:
dev_close(dev);
return err;
}
static int fdp_set_events(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Enable or disable FDP events";
const char *namespace_id = "Namespace identifier";
const char *enable = "Enable/disable event";
const char *event_types = "Comma-separated list of event types";
const char *ph = "Placement Handle";
const char *save = "specifies that the controller shall save the attribute";
struct nvme_dev *dev;
int err = -1;
unsigned short evts[255];
int nev;
__u8 buf[255];
struct config {
__u32 namespace_id;
__u16 ph;
char *event_types;
bool enable;
bool save;
};
struct config cfg = {
.enable = false,
.save = false,
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
OPT_SHRT("placement-handle", 'p', &cfg.ph, ph),
OPT_FLAG("enable", 'e', &cfg.enable, enable),
OPT_FLAG("save", 's', &cfg.save, save),
OPT_LIST("event-types", 't', &cfg.event_types, event_types),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
nev = argconfig_parse_comma_sep_array_short(cfg.event_types, evts, ARRAY_SIZE(evts));
if (nev < 0) {
perror("could not parse event types");
err = -EINVAL;
goto out;
} else if (nev == 0) {
fprintf(stderr, "no event types set\n");
err = -EINVAL;
goto out;
} else if (nev > 255) {
fprintf(stderr, "too many event types (max 255)\n");
err = -EINVAL;
goto out;
}
if (!cfg.namespace_id) {
err = nvme_get_nsid(dev_fd(dev), &cfg.namespace_id);
if (err < 0) {
if (errno != ENOTTY) {
fprintf(stderr, "get-namespace-id: %s\n", nvme_strerror(errno));
goto out;
}
cfg.namespace_id = NVME_NSID_ALL;
}
}
for (unsigned int i = 0; i < nev; i++)
buf[i] = (__u8)evts[i];
struct nvme_set_features_args args = {
.args_size = sizeof(args),
.fd = dev_fd(dev),
.fid = NVME_FEAT_FID_FDP_EVENTS,
.save = cfg.save,
.nsid = cfg.namespace_id,
.cdw11 = (nev << 16) | cfg.ph,
.cdw12 = cfg.enable ? 0x1 : 0x0,
.data_len = sizeof(buf),
.data = buf,
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
.result = NULL,
};
err = nvme_set_features(&args);
if (err) {
nvme_show_status(err);
goto out;
}
printf("set-events: Success\n");
out:
dev_close(dev);
return err;
}
static int fdp_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Show, enable or disable FDP configuration";
const char *enable_conf_idx = "FDP configuration index to enable";
const char *endurance_group = "Endurance group ID";
const char *disable = "Disable current FDP configuration";
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
int err = -1;
__u32 result;
bool enabling_conf_idx = false;
struct nvme_set_features_args setf_args = {
.args_size = sizeof(setf_args),
.fd = -1,
.fid = NVME_FEAT_FID_FDP,
.save = 1,
.nsid = NVME_NSID_ALL,
.data_len = 0,
.data = NULL,
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
};
struct config {
bool disable;
__u8 fdpcidx;
__u16 endgid;
};
struct config cfg = {
.disable = false,
.fdpcidx = 0,
.endgid = 0,
};
OPT_ARGS(opts) = {
OPT_SHRT("endgrp-id", 'e', &cfg.endgid, endurance_group),
OPT_BYTE("enable-conf-idx", 'c', &cfg.fdpcidx, enable_conf_idx),
OPT_FLAG("disable", 'd', &cfg.disable, disable),
OPT_INCR("verbose", 'v', &nvme_cfg.verbose, verbose),
OPT_END()
};
err = parse_and_open(&dev, argc, argv, desc, opts);
if (err)
return err;
enabling_conf_idx = argconfig_parse_seen(opts, "enable-conf-idx");
if (enabling_conf_idx && cfg.disable) {
nvme_show_error("Cannot enable and disable at the same time");
return -EINVAL;
}
if (!enabling_conf_idx && !cfg.disable) {
struct nvme_get_features_args getf_args = {
.args_size = sizeof(getf_args),
.fd = dev_fd(dev),
.fid = NVME_FEAT_FID_FDP,
.nsid = NVME_NSID_ALL,
.sel = NVME_GET_FEATURES_SEL_CURRENT,
.cdw11 = cfg.endgid,
.uuidx = 0,
.data_len = 0,
.data = NULL,
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
.result = &result,
};
nvme_show_result("Endurance Group : %d", cfg.endgid);
err = nvme_get_features(&getf_args);
if (err) {
nvme_show_status(err);
return err;
}
nvme_show_result("Flexible Direct Placement Enable (FDPE) : %s",
(result & 0x1) ? "Yes" : "No");
nvme_show_result("Flexible Direct Placement Configuration Index : %u",
(result >> 8) & 0xf);
return err;
}
setf_args.fd = dev_fd(dev);
setf_args.cdw11 = cfg.endgid;
setf_args.cdw12 = cfg.fdpcidx << 8 | (!cfg.disable);
err = nvme_set_features(&setf_args);
if (err) {
nvme_show_status(err);
return err;
}
nvme_show_result("Success %s Endurance Group: %d, FDP configuration index: %d",
(cfg.disable) ? "disabling" : "enabling", cfg.endgid, cfg.fdpcidx);
return err;
}
|