2 * Copyright (c) 2016 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 #include <sys/param.h>
28 #include <sys/ioccom.h>
41 #include "nvmecontrol.h"
43 _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
44 "nvme_power_state size wrong");
46 #define POWER_NONE 0xffffffffu
48 static struct options {
61 power_list_one(int i, struct nvme_power_state *nps)
63 int mpower, apower, ipower;
64 uint8_t mps, nops, aps, apw;
66 mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) &
68 nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) &
69 NVME_PWR_ST_NOPS_MASK;
70 apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) &
72 aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) &
84 printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
85 i, mpower / 10000, mpower % 10000,
86 nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
87 nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
88 nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
89 apower / 10000, apower % 10000, apw);
93 power_list(struct nvme_controller_data *cdata)
97 printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
98 printf(" # Max pwr Enter Lat Exit Lat RT RL WT WL Idle Pwr Act Pwr Workloadd\n");
99 printf("-- -------- --------- --------- -- -- -- -- -------- -------- --\n");
100 for (i = 0; i <= cdata->npss; i++)
101 power_list_one(i, &cdata->power_state[i]);
105 power_set(int fd, int power_val, int workload, int perm)
107 struct nvme_pt_command pt;
110 p = perm ? (1u << 31) : 0;
111 memset(&pt, 0, sizeof(pt));
112 pt.cmd.opc = NVME_OPC_SET_FEATURES;
113 pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
114 pt.cmd.cdw11 = htole32(power_val | (workload << 5));
116 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
117 err(EX_IOERR, "set feature power mgmt request failed");
119 if (nvme_completion_is_error(&pt.cpl))
120 errx(EX_IOERR, "set feature power mgmt request returned error");
126 struct nvme_pt_command pt;
128 memset(&pt, 0, sizeof(pt));
129 pt.cmd.opc = NVME_OPC_GET_FEATURES;
130 pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
132 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
133 err(EX_IOERR, "set feature power mgmt request failed");
135 if (nvme_completion_is_error(&pt.cpl))
136 errx(EX_IOERR, "set feature power mgmt request returned error");
138 printf("Current Power State is %d\n", pt.cpl.cdw0 & 0x1F);
139 printf("Current Workload Hint is %d\n", pt.cpl.cdw0 >> 5);
143 power(const struct cmd *f, int argc, char *argv[])
145 struct nvme_controller_data cdata;
150 if (arg_parse(argc, argv, f))
153 if (opt.list && opt.power != POWER_NONE) {
154 fprintf(stderr, "Can't set power and list power states\n");
155 arg_help(argc, argv, f);
158 open_dev(opt.dev, &fd, 1, 1);
159 get_nsid(fd, &path, &nsid);
162 open_dev(path, &fd, 1, 1);
167 if (read_controller_data(fd, &cdata))
168 errx(EX_IOERR, "Identify request failed");
173 if (opt.power != POWER_NONE) {
174 power_set(fd, opt.power, opt.workload, 0);
184 static const struct opts power_opts[] = {
185 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
186 OPT("list", 'l', arg_none, opt, list,
187 "List the valid power states"),
188 OPT("power", 'p', arg_uint32, opt, power,
189 "Set the power state"),
190 OPT("workload", 'w', arg_uint32, opt, workload,
191 "Set the workload hint"),
192 { NULL, 0, arg_none, NULL, NULL }
196 static const struct args power_args[] = {
197 { arg_string, &opt.dev, "controller-id|namespace-id" },
198 { arg_none, NULL, NULL },
201 static struct cmd power_cmd = {
204 .descr = "Manage power states for the drive",
205 .ctx_size = sizeof(opt),
210 CMD_COMMAND(power_cmd);