xref: /freebsd/sbin/nvmecontrol/power.c (revision 6ac0f711ad9ecd9ac1525787bb08002c85c03cde)
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/param.h>
27038659e7SWarner Losh #include <sys/ioccom.h>
28038659e7SWarner Losh 
29038659e7SWarner Losh #include <ctype.h>
30038659e7SWarner Losh #include <err.h>
31038659e7SWarner Losh #include <fcntl.h>
32f634b4c1SWarner Losh #include <stdbool.h>
33038659e7SWarner Losh #include <stddef.h>
34038659e7SWarner Losh #include <stdio.h>
35038659e7SWarner Losh #include <stdlib.h>
36038659e7SWarner Losh #include <string.h>
375dc463f9SAlexander Motin #include <sysexits.h>
38038659e7SWarner Losh #include <unistd.h>
39038659e7SWarner Losh 
40038659e7SWarner Losh #include "nvmecontrol.h"
41038659e7SWarner Losh 
42038659e7SWarner Losh _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
43038659e7SWarner Losh 	       "nvme_power_state size wrong");
44038659e7SWarner Losh 
45f634b4c1SWarner Losh #define POWER_NONE 0xffffffffu
46f634b4c1SWarner Losh 
47f634b4c1SWarner Losh static struct options {
48f634b4c1SWarner Losh 	bool		list;
49f634b4c1SWarner Losh 	uint32_t	power;
50f634b4c1SWarner Losh 	uint32_t	workload;
51f634b4c1SWarner Losh 	const char	*dev;
52f634b4c1SWarner Losh } opt = {
53f634b4c1SWarner Losh 	.list = false,
54f634b4c1SWarner Losh 	.power = POWER_NONE,
55f634b4c1SWarner Losh 	.workload = 0,
56f634b4c1SWarner Losh 	.dev = NULL,
57f634b4c1SWarner Losh };
58a13a291aSWarner Losh 
59038659e7SWarner Losh static void
60038659e7SWarner Losh power_list_one(int i, struct nvme_power_state *nps)
61038659e7SWarner Losh {
62038659e7SWarner Losh 	int mpower, apower, ipower;
630d787e9bSWojciech Macek 	uint8_t mps, nops, aps, apw;
640d787e9bSWojciech Macek 
65fba73a40SJohn Baldwin 	mps = NVMEV(NVME_PWR_ST_MPS, nps->mps_nops);
66fba73a40SJohn Baldwin 	nops = NVMEV(NVME_PWR_ST_NOPS, nps->mps_nops);
67fba73a40SJohn Baldwin 	apw = NVMEV(NVME_PWR_ST_APW, nps->apw_aps);
68fba73a40SJohn Baldwin 	aps = NVMEV(NVME_PWR_ST_APS, nps->apw_aps);
69038659e7SWarner Losh 
70038659e7SWarner Losh 	mpower = nps->mp;
710d787e9bSWojciech Macek 	if (mps == 0)
72038659e7SWarner Losh 		mpower *= 100;
73038659e7SWarner Losh 	ipower = nps->idlp;
74038659e7SWarner Losh 	if (nps->ips == 1)
75038659e7SWarner Losh 		ipower *= 100;
76038659e7SWarner Losh 	apower = nps->actp;
770d787e9bSWojciech Macek 	if (aps == 1)
78038659e7SWarner Losh 		apower *= 100;
79038659e7SWarner Losh 	printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
80038659e7SWarner Losh 	       i, mpower / 10000, mpower % 10000,
810d787e9bSWojciech Macek 	       nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
82038659e7SWarner Losh 	       nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
83038659e7SWarner Losh 	       nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
840d787e9bSWojciech Macek 	       apower / 10000, apower % 10000, apw);
85038659e7SWarner Losh }
86038659e7SWarner Losh 
87038659e7SWarner Losh static void
88038659e7SWarner Losh power_list(struct nvme_controller_data *cdata)
89038659e7SWarner Losh {
90038659e7SWarner Losh 	int i;
91038659e7SWarner Losh 
92038659e7SWarner Losh 	printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
93*6ac0f711SAlexander Motin 	printf(" #   Max pwr  Enter Lat  Exit Lat RT RL WT WL Idle Pwr  Act Pwr Workload\n");
94038659e7SWarner Losh 	printf("--  --------  --------- --------- -- -- -- -- -------- -------- --\n");
95038659e7SWarner Losh 	for (i = 0; i <= cdata->npss; i++)
96038659e7SWarner Losh 		power_list_one(i, &cdata->power_state[i]);
97038659e7SWarner Losh }
98038659e7SWarner Losh 
99038659e7SWarner Losh static void
100b130d07fSDimitry Andric power_set(int fd, int power_val, int workload, int perm)
101038659e7SWarner Losh {
102038659e7SWarner Losh 	struct nvme_pt_command	pt;
103038659e7SWarner Losh 	uint32_t p;
104038659e7SWarner Losh 
105038659e7SWarner Losh 	p = perm ? (1u << 31) : 0;
106038659e7SWarner Losh 	memset(&pt, 0, sizeof(pt));
1079544e6dcSChuck Tuffli 	pt.cmd.opc = NVME_OPC_SET_FEATURES;
1080d787e9bSWojciech Macek 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
1090d787e9bSWojciech Macek 	pt.cmd.cdw11 = htole32(power_val | (workload << 5));
110038659e7SWarner Losh 
111038659e7SWarner Losh 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
1125dc463f9SAlexander Motin 		err(EX_IOERR, "set feature power mgmt request failed");
113038659e7SWarner Losh 
114038659e7SWarner Losh 	if (nvme_completion_is_error(&pt.cpl))
1155dc463f9SAlexander Motin 		errx(EX_IOERR, "set feature power mgmt request returned error");
116038659e7SWarner Losh }
117038659e7SWarner Losh 
118038659e7SWarner Losh static void
119038659e7SWarner Losh power_show(int fd)
120038659e7SWarner Losh {
121038659e7SWarner Losh 	struct nvme_pt_command	pt;
122038659e7SWarner Losh 
123038659e7SWarner Losh 	memset(&pt, 0, sizeof(pt));
1249544e6dcSChuck Tuffli 	pt.cmd.opc = NVME_OPC_GET_FEATURES;
1250d787e9bSWojciech Macek 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
126038659e7SWarner Losh 
127038659e7SWarner Losh 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
1285dc463f9SAlexander Motin 		err(EX_IOERR, "set feature power mgmt request failed");
129038659e7SWarner Losh 
130038659e7SWarner Losh 	if (nvme_completion_is_error(&pt.cpl))
1315dc463f9SAlexander Motin 		errx(EX_IOERR, "set feature power mgmt request returned error");
132038659e7SWarner Losh 
133f409f11bSAlexander Motin 	printf("Current Power State is %d\n", pt.cpl.cdw0 & 0x1F);
134f409f11bSAlexander Motin 	printf("Current Workload Hint is %d\n", pt.cpl.cdw0 >> 5);
135038659e7SWarner Losh }
136038659e7SWarner Losh 
137a13a291aSWarner Losh static void
138f634b4c1SWarner Losh power(const struct cmd *f, int argc, char *argv[])
139038659e7SWarner Losh {
140038659e7SWarner Losh 	struct nvme_controller_data	cdata;
141f634b4c1SWarner Losh 	int				fd;
1425458a1c8SAlexander Motin 	char				*path;
1435458a1c8SAlexander Motin 	uint32_t			nsid;
144038659e7SWarner Losh 
1456995fb5eSDavid Bright 	if (arg_parse(argc, argv, f))
1466995fb5eSDavid Bright 		return;
147038659e7SWarner Losh 
148f634b4c1SWarner Losh 	if (opt.list && opt.power != POWER_NONE) {
149038659e7SWarner Losh 		fprintf(stderr, "Can't set power and list power states\n");
150f634b4c1SWarner Losh 		arg_help(argc, argv, f);
151038659e7SWarner Losh 	}
152038659e7SWarner Losh 
153f634b4c1SWarner Losh 	open_dev(opt.dev, &fd, 1, 1);
1545458a1c8SAlexander Motin 	get_nsid(fd, &path, &nsid);
1555458a1c8SAlexander Motin 	if (nsid != 0) {
1565458a1c8SAlexander Motin 		close(fd);
1575458a1c8SAlexander Motin 		open_dev(path, &fd, 1, 1);
1585458a1c8SAlexander Motin 	}
1595458a1c8SAlexander Motin 	free(path);
160038659e7SWarner Losh 
161f634b4c1SWarner Losh 	if (opt.list) {
1625dc463f9SAlexander Motin 		if (read_controller_data(fd, &cdata))
1635dc463f9SAlexander Motin 			errx(EX_IOERR, "Identify request failed");
164038659e7SWarner Losh 		power_list(&cdata);
165038659e7SWarner Losh 		goto out;
166038659e7SWarner Losh 	}
167038659e7SWarner Losh 
168f634b4c1SWarner Losh 	if (opt.power != POWER_NONE) {
169f634b4c1SWarner Losh 		power_set(fd, opt.power, opt.workload, 0);
170038659e7SWarner Losh 		goto out;
171038659e7SWarner Losh 	}
172038659e7SWarner Losh 	power_show(fd);
173038659e7SWarner Losh 
174038659e7SWarner Losh out:
175038659e7SWarner Losh 	close(fd);
176038659e7SWarner Losh 	exit(0);
177038659e7SWarner Losh }
178a13a291aSWarner Losh 
179f634b4c1SWarner Losh static const struct opts power_opts[] = {
180f634b4c1SWarner Losh #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
181f634b4c1SWarner Losh 	OPT("list", 'l', arg_none, opt, list,
182f634b4c1SWarner Losh 	    "List the valid power states"),
183f634b4c1SWarner Losh 	OPT("power", 'p', arg_uint32, opt, power,
184f634b4c1SWarner Losh 	    "Set the power state"),
185f634b4c1SWarner Losh 	OPT("workload", 'w', arg_uint32, opt, workload,
186f409f11bSAlexander Motin 	    "Set the workload hint"),
187f634b4c1SWarner Losh 	{ NULL, 0, arg_none, NULL, NULL }
188f634b4c1SWarner Losh };
189f634b4c1SWarner Losh #undef OPT
190f634b4c1SWarner Losh 
191f634b4c1SWarner Losh static const struct args power_args[] = {
1925458a1c8SAlexander Motin 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
193f634b4c1SWarner Losh 	{ arg_none, NULL, NULL },
194f634b4c1SWarner Losh };
195f634b4c1SWarner Losh 
196f634b4c1SWarner Losh static struct cmd power_cmd = {
197f634b4c1SWarner Losh 	.name = "power",
198f634b4c1SWarner Losh 	.fn = power,
199f634b4c1SWarner Losh 	.descr = "Manage power states for the drive",
200f634b4c1SWarner Losh 	.ctx_size = sizeof(opt),
201f634b4c1SWarner Losh 	.opts = power_opts,
202f634b4c1SWarner Losh 	.args = power_args,
203f634b4c1SWarner Losh };
204f634b4c1SWarner Losh 
205f634b4c1SWarner Losh CMD_COMMAND(power_cmd);
206