Skip to content

Commit e4ffd65

Browse files
ikegami-tigaw
authored andcommitted
plugins: add NVMe feature extension
This extension is for adding each features commands to get and set. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent ea0761b commit e4ffd65

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

plugins/feat/feat-nvme.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include "nvme.h"
3+
#include "plugin.h"
4+
#include "nvme-print.h"
5+
6+
#define CREATE_CMD
7+
#include "feat-nvme.h"
8+
9+
static const char *power_mgmt_feat = "power management feature";
10+
static const char *sel = "[0-3]: current/default/saved/supported";
11+
static const char *save = "Specifies that the controller shall save the attribute";
12+
13+
static int power_mgmt_get(struct nvme_dev *dev, const __u8 fid, __u8 sel)
14+
{
15+
__u32 result;
16+
int err;
17+
18+
struct nvme_get_features_args args = {
19+
.args_size = sizeof(args),
20+
.fd = dev_fd(dev),
21+
.fid = fid,
22+
.sel = sel,
23+
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
24+
.result = &result,
25+
};
26+
27+
err = nvme_get_features(&args);
28+
if (!err) {
29+
if (NVME_CHECK(sel, GET_FEATURES_SEL, SUPPORTED))
30+
nvme_show_select_result(fid, result);
31+
else
32+
nvme_feature_show_fields(fid, result, NULL);
33+
} else {
34+
nvme_show_error("Get %s", power_mgmt_feat);
35+
}
36+
37+
return err;
38+
}
39+
40+
static int power_mgmt_set(struct nvme_dev *dev, const __u8 fid, __u8 ps, __u8 wh, bool save)
41+
{
42+
__u32 result;
43+
int err;
44+
45+
struct nvme_set_features_args args = {
46+
.args_size = sizeof(args),
47+
.fd = dev_fd(dev),
48+
.fid = fid,
49+
.cdw11 = NVME_SET(ps, FEAT_PWRMGMT_PS) | NVME_SET(wh, FEAT_PWRMGMT_WH),
50+
.save = save,
51+
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
52+
.result = &result,
53+
};
54+
55+
err = nvme_set_features(&args);
56+
57+
nvme_show_init();
58+
59+
if (err > 0) {
60+
nvme_show_status(err);
61+
} else if (err < 0) {
62+
nvme_show_perror("Set %s", power_mgmt_feat);
63+
} else {
64+
nvme_show_result("Set %s: 0x%04x (%s)", power_mgmt_feat, args.cdw11,
65+
save ? "Save" : "Not save");
66+
nvme_feature_show_fields(fid, args.cdw11, NULL);
67+
}
68+
69+
nvme_show_finish();
70+
71+
return err;
72+
}
73+
74+
static int feat_power_mgmt(int argc, char **argv, struct command *cmd, struct plugin *plugin)
75+
{
76+
const char *ps = "power state";
77+
const char *wh = "workload hint";
78+
const __u8 fid = NVME_FEAT_FID_POWER_MGMT;
79+
80+
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
81+
int err;
82+
83+
struct config {
84+
__u8 ps;
85+
__u8 wh;
86+
bool save;
87+
__u8 sel;
88+
};
89+
90+
struct config cfg = { 0 };
91+
92+
NVME_ARGS(opts,
93+
OPT_BYTE("ps", 'p', &cfg.ps, ps),
94+
OPT_BYTE("wh", 'w', &cfg.wh, wh),
95+
OPT_FLAG("save", 's', &cfg.save, save),
96+
OPT_BYTE("sel", 'S', &cfg.sel, sel));
97+
98+
err = parse_and_open(&dev, argc, argv, POWER_MGMT_DESC, opts);
99+
if (err)
100+
return err;
101+
102+
if (argconfig_parse_seen(opts, "ps"))
103+
err = power_mgmt_set(dev, fid, cfg.ps, cfg.wh, cfg.save);
104+
else
105+
err = power_mgmt_get(dev, fid, cfg.sel);
106+
107+
return err;
108+
}

plugins/feat/feat-nvme.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#include "cmd.h"
3+
4+
#undef CMD_INC_FILE
5+
#define CMD_INC_FILE plugins/feat/feat-nvme
6+
7+
#include "define_cmd.h"
8+
9+
#if !defined(FEAT_NVME) || defined(CMD_HEADER_MULTI_READ)
10+
#define FEAT_NVME
11+
12+
#define FEAT_PLUGIN_VERSION "1.0"
13+
#define POWER_MGMT_DESC "Get and set power management feature"
14+
15+
PLUGIN(NAME("feat", "NVMe feature extensions", FEAT_PLUGIN_VERSION),
16+
COMMAND_LIST(
17+
ENTRY("power-mgmt", POWER_MGMT_DESC, feat_power_mgmt)
18+
)
19+
);
20+
#endif /* !FEAT_NVME || CMD_HEADER_MULTI_READ */
21+
22+
#ifndef FEAT_NVME_H
23+
#define FEAT_NVME_H
24+
25+
#endif /* FEAT_NVME_H */

plugins/feat/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sources += [
2+
'plugins/feat/feat-nvme.c',
3+
]

plugins/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sources += [
3030
'plugins/zns/zns.c',
3131
]
3232

33+
subdir('feat')
3334
subdir('lm')
3435
subdir('ocp')
3536

0 commit comments

Comments
 (0)