xref: /freebsd/sbin/nvmecontrol/power.c (revision f409f11bc556e6da4c4cdb9f20a2ba3b0977fb32)
1038659e7SWarner Losh /*-
252467047SWarner Losh  * Copyright (c) 2016 Netflix, Inc.
3038659e7SWarner Losh  *
4038659e7SWarner Losh  * Redistribution and use in source and binary forms, with or without
5038659e7SWarner Losh  * modification, are permitted provided that the following conditions
6038659e7SWarner Losh  * are met:
7038659e7SWarner Losh  * 1. Redistributions of source code must retain the above copyright
8038659e7SWarner Losh  *    notice, this list of conditions and the following disclaimer.
9038659e7SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
10038659e7SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
11038659e7SWarner Losh  *    documentation and/or other materials provided with the distribution.
12038659e7SWarner Losh  *
13038659e7SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14038659e7SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15038659e7SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16038659e7SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17038659e7SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18038659e7SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19038659e7SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20038659e7SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21038659e7SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22038659e7SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23038659e7SWarner Losh  * SUCH DAMAGE.
24038659e7SWarner Losh  */
25038659e7SWarner Losh 
26038659e7SWarner Losh #include <sys/cdefs.h>
27038659e7SWarner Losh __FBSDID("$FreeBSD$");
28038659e7SWarner Losh 
29038659e7SWarner Losh #include <sys/param.h>
30038659e7SWarner Losh #include <sys/ioccom.h>
31038659e7SWarner Losh 
32038659e7SWarner Losh #include <ctype.h>
33038659e7SWarner Losh #include <err.h>
34038659e7SWarner Losh #include <fcntl.h>
35f634b4c1SWarner Losh #include <stdbool.h>
36038659e7SWarner Losh #include <stddef.h>
37038659e7SWarner Losh #include <stdio.h>
38038659e7SWarner Losh #include <stdlib.h>
39038659e7SWarner Losh #include <string.h>
405dc463f9SAlexander Motin #include <sysexits.h>
41038659e7SWarner Losh #include <unistd.h>
42038659e7SWarner Losh 
43038659e7SWarner Losh #include "nvmecontrol.h"
44038659e7SWarner Losh 
45038659e7SWarner Losh _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
46038659e7SWarner Losh 	       "nvme_power_state size wrong");
47038659e7SWarner Losh 
48f634b4c1SWarner Losh #define POWER_NONE 0xffffffffu
49f634b4c1SWarner Losh 
50f634b4c1SWarner Losh static struct options {
51f634b4c1SWarner Losh 	bool		list;
52f634b4c1SWarner Losh 	uint32_t	power;
53f634b4c1SWarner Losh 	uint32_t	workload;
54f634b4c1SWarner Losh 	const char	*dev;
55f634b4c1SWarner Losh } opt = {
56f634b4c1SWarner Losh 	.list = false,
57f634b4c1SWarner Losh 	.power = POWER_NONE,
58f634b4c1SWarner Losh 	.workload = 0,
59f634b4c1SWarner Losh 	.dev = NULL,
60f634b4c1SWarner Losh };
61a13a291aSWarner Losh 
62038659e7SWarner Losh static void
63038659e7SWarner Losh power_list_one(int i, struct nvme_power_state *nps)
64038659e7SWarner Losh {
65038659e7SWarner Losh 	int mpower, apower, ipower;
660d787e9bSWojciech Macek 	uint8_t mps, nops, aps, apw;
670d787e9bSWojciech Macek 
680d787e9bSWojciech Macek 	mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) &
690d787e9bSWojciech Macek 		NVME_PWR_ST_MPS_MASK;
700d787e9bSWojciech Macek 	nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) &
710d787e9bSWojciech Macek 		NVME_PWR_ST_NOPS_MASK;
720d787e9bSWojciech Macek 	apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) &
730d787e9bSWojciech Macek 		NVME_PWR_ST_APW_MASK;
740d787e9bSWojciech Macek 	aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) &
750d787e9bSWojciech Macek 		NVME_PWR_ST_APS_MASK;
76038659e7SWarner Losh 
77038659e7SWarner Losh 	mpower = nps->mp;
780d787e9bSWojciech Macek 	if (mps == 0)
79038659e7SWarner Losh 		mpower *= 100;
80038659e7SWarner Losh 	ipower = nps->idlp;
81038659e7SWarner Losh 	if (nps->ips == 1)
82038659e7SWarner Losh 		ipower *= 100;
83038659e7SWarner Losh 	apower = nps->actp;
840d787e9bSWojciech Macek 	if (aps == 1)
85038659e7SWarner Losh 		apower *= 100;
86038659e7SWarner Losh 	printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
87038659e7SWarner Losh 	       i, mpower / 10000, mpower % 10000,
880d787e9bSWojciech Macek 	       nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
89038659e7SWarner Losh 	       nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
90038659e7SWarner Losh 	       nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
910d787e9bSWojciech Macek 	       apower / 10000, apower % 10000, apw);
92038659e7SWarner Losh }
93038659e7SWarner Losh 
94038659e7SWarner Losh static void
95038659e7SWarner Losh power_list(struct nvme_controller_data *cdata)
96038659e7SWarner Losh {
97038659e7SWarner Losh 	int i;
98038659e7SWarner Losh 
99038659e7SWarner Losh 	printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
100038659e7SWarner Losh 	printf(" #   Max pwr  Enter Lat  Exit Lat RT RL WT WL Idle Pwr  Act Pwr Workloadd\n");
101038659e7SWarner Losh 	printf("--  --------  --------- --------- -- -- -- -- -------- -------- --\n");
102038659e7SWarner Losh 	for (i = 0; i <= cdata->npss; i++)
103038659e7SWarner Losh 		power_list_one(i, &cdata->power_state[i]);
104038659e7SWarner Losh }
105038659e7SWarner Losh 
106038659e7SWarner Losh static void
107b130d07fSDimitry Andric power_set(int fd, int power_val, int workload, int perm)
108038659e7SWarner Losh {
109038659e7SWarner Losh 	struct nvme_pt_command	pt;
110038659e7SWarner Losh 	uint32_t p;
111038659e7SWarner Losh 
112038659e7SWarner Losh 	p = perm ? (1u << 31) : 0;
113038659e7SWarner Losh 	memset(&pt, 0, sizeof(pt));
1149544e6dcSChuck Tuffli 	pt.cmd.opc = NVME_OPC_SET_FEATURES;
1150d787e9bSWojciech Macek 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
1160d787e9bSWojciech Macek 	pt.cmd.cdw11 = htole32(power_val | (workload << 5));
117038659e7SWarner Losh 
118038659e7SWarner Losh 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
1195dc463f9SAlexander Motin 		err(EX_IOERR, "set feature power mgmt request failed");
120038659e7SWarner Losh 
121038659e7SWarner Losh 	if (nvme_completion_is_error(&pt.cpl))
1225dc463f9SAlexander Motin 		errx(EX_IOERR, "set feature power mgmt request returned error");
123038659e7SWarner Losh }
124038659e7SWarner Losh 
125038659e7SWarner Losh static void
126038659e7SWarner Losh power_show(int fd)
127038659e7SWarner Losh {
128038659e7SWarner Losh 	struct nvme_pt_command	pt;
129038659e7SWarner Losh 
130038659e7SWarner Losh 	memset(&pt, 0, sizeof(pt));
1319544e6dcSChuck Tuffli 	pt.cmd.opc = NVME_OPC_GET_FEATURES;
1320d787e9bSWojciech Macek 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
133038659e7SWarner Losh 
134038659e7SWarner Losh 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
1355dc463f9SAlexander Motin 		err(EX_IOERR, "set feature power mgmt request failed");
136038659e7SWarner Losh 
137038659e7SWarner Losh 	if (nvme_completion_is_error(&pt.cpl))
1385dc463f9SAlexander Motin 		errx(EX_IOERR, "set feature power mgmt request returned error");
139038659e7SWarner Losh 
140*f409f11bSAlexander Motin 	printf("Current Power State is %d\n", pt.cpl.cdw0 & 0x1F);
141*f409f11bSAlexander Motin 	printf("Current Workload Hint is %d\n", pt.cpl.cdw0 >> 5);
142038659e7SWarner Losh }
143038659e7SWarner Losh 
144a13a291aSWarner Losh static void
145f634b4c1SWarner Losh power(const struct cmd *f, int argc, char *argv[])
146038659e7SWarner Losh {
147038659e7SWarner Losh 	struct nvme_controller_data	cdata;
148f634b4c1SWarner Losh 	int				fd;
1495458a1c8SAlexander Motin 	char				*path;
1505458a1c8SAlexander Motin 	uint32_t			nsid;
151038659e7SWarner Losh 
1526995fb5eSDavid Bright 	if (arg_parse(argc, argv, f))
1536995fb5eSDavid Bright 		return;
154038659e7SWarner Losh 
155f634b4c1SWarner Losh 	if (opt.list && opt.power != POWER_NONE) {
156038659e7SWarner Losh 		fprintf(stderr, "Can't set power and list power states\n");
157f634b4c1SWarner Losh 		arg_help(argc, argv, f);
158038659e7SWarner Losh 	}
159038659e7SWarner Losh 
160f634b4c1SWarner Losh 	open_dev(opt.dev, &fd, 1, 1);
1615458a1c8SAlexander Motin 	get_nsid(fd, &path, &nsid);
1625458a1c8SAlexander Motin 	if (nsid != 0) {
1635458a1c8SAlexander Motin 		close(fd);
1645458a1c8SAlexander Motin 		open_dev(path, &fd, 1, 1);
1655458a1c8SAlexander Motin 	}
1665458a1c8SAlexander Motin 	free(path);
167038659e7SWarner Losh 
168f634b4c1SWarner Losh 	if (opt.list) {
1695dc463f9SAlexander Motin 		if (read_controller_data(fd, &cdata))
1705dc463f9SAlexander Motin 			errx(EX_IOERR, "Identify request failed");
171038659e7SWarner Losh 		power_list(&cdata);
172038659e7SWarner Losh 		goto out;
173038659e7SWarner Losh 	}
174038659e7SWarner Losh 
175f634b4c1SWarner Losh 	if (opt.power != POWER_NONE) {
176f634b4c1SWarner Losh 		power_set(fd, opt.power, opt.workload, 0);
177038659e7SWarner Losh 		goto out;
178038659e7SWarner Losh 	}
179038659e7SWarner Losh 	power_show(fd);
180038659e7SWarner Losh 
181038659e7SWarner Losh out:
182038659e7SWarner Losh 	close(fd);
183038659e7SWarner Losh 	exit(0);
184038659e7SWarner Losh }
185a13a291aSWarner Losh 
186f634b4c1SWarner Losh static const struct opts power_opts[] = {
187f634b4c1SWarner Losh #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
188f634b4c1SWarner Losh 	OPT("list", 'l', arg_none, opt, list,
189f634b4c1SWarner Losh 	    "List the valid power states"),
190f634b4c1SWarner Losh 	OPT("power", 'p', arg_uint32, opt, power,
191f634b4c1SWarner Losh 	    "Set the power state"),
192f634b4c1SWarner Losh 	OPT("workload", 'w', arg_uint32, opt, workload,
193*f409f11bSAlexander Motin 	    "Set the workload hint"),
194f634b4c1SWarner Losh 	{ NULL, 0, arg_none, NULL, NULL }
195f634b4c1SWarner Losh };
196f634b4c1SWarner Losh #undef OPT
197f634b4c1SWarner Losh 
198f634b4c1SWarner Losh static const struct args power_args[] = {
1995458a1c8SAlexander Motin 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
200f634b4c1SWarner Losh 	{ arg_none, NULL, NULL },
201f634b4c1SWarner Losh };
202f634b4c1SWarner Losh 
203f634b4c1SWarner Losh static struct cmd power_cmd = {
204f634b4c1SWarner Losh 	.name = "power",
205f634b4c1SWarner Losh 	.fn = power,
206f634b4c1SWarner Losh 	.descr = "Manage power states for the drive",
207f634b4c1SWarner Losh 	.ctx_size = sizeof(opt),
208f634b4c1SWarner Losh 	.opts = power_opts,
209f634b4c1SWarner Losh 	.args = power_args,
210f634b4c1SWarner Losh };
211f634b4c1SWarner Losh 
212f634b4c1SWarner Losh CMD_COMMAND(power_cmd);
213