1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016 Netflix, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/ioccom.h>
30
31 #include <ctype.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41
42 #include "nvmecontrol.h"
43
44 _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
45 "nvme_power_state size wrong");
46
47 #define POWER_NONE 0xffffffffu
48
49 static struct options {
50 bool list;
51 uint32_t power;
52 uint32_t workload;
53 const char *dev;
54 } opt = {
55 .list = false,
56 .power = POWER_NONE,
57 .workload = 0,
58 .dev = NULL,
59 };
60
61 static void
power_list_one(int i,struct nvme_power_state * nps)62 power_list_one(int i, struct nvme_power_state *nps)
63 {
64 int mpower, apower, ipower;
65 uint8_t mps, nops, aps, apw;
66
67 mps = NVMEV(NVME_PWR_ST_MPS, nps->mps_nops);
68 nops = NVMEV(NVME_PWR_ST_NOPS, nps->mps_nops);
69 apw = NVMEV(NVME_PWR_ST_APW, nps->apw_aps);
70 aps = NVMEV(NVME_PWR_ST_APS, nps->apw_aps);
71
72 mpower = nps->mp;
73 if (mps == 0)
74 mpower *= 100;
75 ipower = nps->idlp;
76 if (nps->ips == 1)
77 ipower *= 100;
78 apower = nps->actp;
79 if (aps == 1)
80 apower *= 100;
81 printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
82 i, mpower / 10000, mpower % 10000,
83 nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
84 nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
85 nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
86 apower / 10000, apower % 10000, apw);
87 }
88
89 static void
power_list(struct nvme_controller_data * cdata)90 power_list(struct nvme_controller_data *cdata)
91 {
92 int i;
93
94 printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
95 printf(" # Max pwr Enter Lat Exit Lat RT RL WT WL Idle Pwr Act Pwr Workload\n");
96 printf("-- -------- --------- --------- -- -- -- -- -------- -------- --\n");
97 for (i = 0; i <= cdata->npss; i++)
98 power_list_one(i, &cdata->power_state[i]);
99 }
100
101 static void
power_set(int fd,int power_val,int workload,int perm)102 power_set(int fd, int power_val, int workload, int perm)
103 {
104 struct nvme_pt_command pt;
105 uint32_t p;
106
107 p = perm ? (1u << 31) : 0;
108 memset(&pt, 0, sizeof(pt));
109 pt.cmd.opc = NVME_OPC_SET_FEATURES;
110 pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
111 pt.cmd.cdw11 = htole32(power_val | (workload << 5));
112
113 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
114 err(EX_IOERR, "set feature power mgmt request failed");
115
116 if (nvme_completion_is_error(&pt.cpl))
117 errx(EX_IOERR, "set feature power mgmt request returned error");
118 }
119
120 static void
power_show(int fd)121 power_show(int fd)
122 {
123 struct nvme_pt_command pt;
124
125 memset(&pt, 0, sizeof(pt));
126 pt.cmd.opc = NVME_OPC_GET_FEATURES;
127 pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
128
129 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
130 err(EX_IOERR, "set feature power mgmt request failed");
131
132 if (nvme_completion_is_error(&pt.cpl))
133 errx(EX_IOERR, "set feature power mgmt request returned error");
134
135 printf("Current Power State is %d\n", pt.cpl.cdw0 & 0x1F);
136 printf("Current Workload Hint is %d\n", pt.cpl.cdw0 >> 5);
137 }
138
139 static void
power(const struct cmd * f,int argc,char * argv[])140 power(const struct cmd *f, int argc, char *argv[])
141 {
142 struct nvme_controller_data cdata;
143 int fd;
144 char *path;
145 uint32_t nsid;
146
147 if (arg_parse(argc, argv, f))
148 return;
149
150 if (opt.list && opt.power != POWER_NONE) {
151 fprintf(stderr, "Can't set power and list power states\n");
152 arg_help(argc, argv, f);
153 }
154
155 open_dev(opt.dev, &fd, 1, 1);
156 get_nsid(fd, &path, &nsid);
157 if (nsid != 0) {
158 close(fd);
159 open_dev(path, &fd, 1, 1);
160 }
161 free(path);
162
163 if (opt.list) {
164 if (read_controller_data(fd, &cdata))
165 errx(EX_IOERR, "Identify request failed");
166 power_list(&cdata);
167 goto out;
168 }
169
170 if (opt.power != POWER_NONE) {
171 power_set(fd, opt.power, opt.workload, 0);
172 goto out;
173 }
174 power_show(fd);
175
176 out:
177 close(fd);
178 exit(0);
179 }
180
181 static const struct opts power_opts[] = {
182 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
183 OPT("list", 'l', arg_none, opt, list,
184 "List the valid power states"),
185 OPT("power", 'p', arg_uint32, opt, power,
186 "Set the power state"),
187 OPT("workload", 'w', arg_uint32, opt, workload,
188 "Set the workload hint"),
189 { NULL, 0, arg_none, NULL, NULL }
190 };
191 #undef OPT
192
193 static const struct args power_args[] = {
194 { arg_string, &opt.dev, "controller-id|namespace-id" },
195 { arg_none, NULL, NULL },
196 };
197
198 static struct cmd power_cmd = {
199 .name = "power",
200 .fn = power,
201 .descr = "Manage power states for the drive",
202 .ctx_size = sizeof(opt),
203 .opts = power_opts,
204 .args = power_args,
205 };
206
207 CMD_COMMAND(power_cmd);
208