xref: /freebsd/sbin/nvmecontrol/power.c (revision 77a1348b3c1cfe8547be49a121b56299a1e18b69)
1 /*-
2  * Copyright (c) 2016 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/ioccom.h>
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.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
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 = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) &
68 		NVME_PWR_ST_MPS_MASK;
69 	nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) &
70 		NVME_PWR_ST_NOPS_MASK;
71 	apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) &
72 		NVME_PWR_ST_APW_MASK;
73 	aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) &
74 		NVME_PWR_ST_APS_MASK;
75 
76 	mpower = nps->mp;
77 	if (mps == 0)
78 		mpower *= 100;
79 	ipower = nps->idlp;
80 	if (nps->ips == 1)
81 		ipower *= 100;
82 	apower = nps->actp;
83 	if (aps == 1)
84 		apower *= 100;
85 	printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
86 	       i, mpower / 10000, mpower % 10000,
87 	       nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
88 	       nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
89 	       nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
90 	       apower / 10000, apower % 10000, apw);
91 }
92 
93 static void
94 power_list(struct nvme_controller_data *cdata)
95 {
96 	int i;
97 
98 	printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
99 	printf(" #   Max pwr  Enter Lat  Exit Lat RT RL WT WL Idle Pwr  Act Pwr Workloadd\n");
100 	printf("--  --------  --------- --------- -- -- -- -- -------- -------- --\n");
101 	for (i = 0; i <= cdata->npss; i++)
102 		power_list_one(i, &cdata->power_state[i]);
103 }
104 
105 static void
106 power_set(int fd, int power_val, int workload, int perm)
107 {
108 	struct nvme_pt_command	pt;
109 	uint32_t p;
110 
111 	p = perm ? (1u << 31) : 0;
112 	memset(&pt, 0, sizeof(pt));
113 	pt.cmd.opc = NVME_OPC_SET_FEATURES;
114 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
115 	pt.cmd.cdw11 = htole32(power_val | (workload << 5));
116 
117 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
118 		err(1, "set feature power mgmt request failed");
119 
120 	if (nvme_completion_is_error(&pt.cpl))
121 		errx(1, "set feature power mgmt request returned error");
122 }
123 
124 static void
125 power_show(int fd)
126 {
127 	struct nvme_pt_command	pt;
128 
129 	memset(&pt, 0, sizeof(pt));
130 	pt.cmd.opc = NVME_OPC_GET_FEATURES;
131 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
132 
133 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
134 		err(1, "set feature power mgmt request failed");
135 
136 	if (nvme_completion_is_error(&pt.cpl))
137 		errx(1, "set feature power mgmt request returned error");
138 
139 	printf("Current Power Mode is %d\n", pt.cpl.cdw0);
140 }
141 
142 static void
143 power(const struct cmd *f, int argc, char *argv[])
144 {
145 	struct nvme_controller_data	cdata;
146 	int				fd;
147 
148 	if (arg_parse(argc, argv, f))
149 		return;
150 
151 	if (opt.list && opt.power != POWER_NONE) {
152 		fprintf(stderr, "Can't set power and list power states\n");
153 		arg_help(argc, argv, f);
154 	}
155 
156 	open_dev(opt.dev, &fd, 1, 1);
157 
158 	if (opt.list) {
159 		read_controller_data(fd, &cdata);
160 		power_list(&cdata);
161 		goto out;
162 	}
163 
164 	if (opt.power != POWER_NONE) {
165 		power_set(fd, opt.power, opt.workload, 0);
166 		goto out;
167 	}
168 	power_show(fd);
169 
170 out:
171 	close(fd);
172 	exit(0);
173 }
174 
175 static const struct opts power_opts[] = {
176 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
177 	OPT("list", 'l', arg_none, opt, list,
178 	    "List the valid power states"),
179 	OPT("power", 'p', arg_uint32, opt, power,
180 	    "Set the power state"),
181 	OPT("workload", 'w', arg_uint32, opt, workload,
182 	    "Set the workload"),
183 	{ NULL, 0, arg_none, NULL, NULL }
184 };
185 #undef OPT
186 
187 static const struct args power_args[] = {
188 	{ arg_string, &opt.dev, "controller-id" },
189 	{ arg_none, NULL, NULL },
190 };
191 
192 static struct cmd power_cmd = {
193 	.name = "power",
194 	.fn = power,
195 	.descr = "Manage power states for the drive",
196 	.ctx_size = sizeof(opt),
197 	.opts = power_opts,
198 	.args = power_args,
199 };
200 
201 CMD_COMMAND(power_cmd);
202