xref: /freebsd/sbin/nvmecontrol/power.c (revision 85732ac8)
1 /*-
2  * Copyright (c) 2016 Netflix, Inc
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <fcntl.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_USAGE							       \
48 	"power [-l] [-p new-state [-w workload-hint]] <controller id>\n"
49 
50 static void
51 power_list_one(int i, struct nvme_power_state *nps)
52 {
53 	int mpower, apower, ipower;
54 	uint8_t mps, nops, aps, apw;
55 
56 	mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) &
57 		NVME_PWR_ST_MPS_MASK;
58 	nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) &
59 		NVME_PWR_ST_NOPS_MASK;
60 	apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) &
61 		NVME_PWR_ST_APW_MASK;
62 	aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) &
63 		NVME_PWR_ST_APS_MASK;
64 
65 	mpower = nps->mp;
66 	if (mps == 0)
67 		mpower *= 100;
68 	ipower = nps->idlp;
69 	if (nps->ips == 1)
70 		ipower *= 100;
71 	apower = nps->actp;
72 	if (aps == 1)
73 		apower *= 100;
74 	printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
75 	       i, mpower / 10000, mpower % 10000,
76 	       nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
77 	       nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
78 	       nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
79 	       apower / 10000, apower % 10000, apw);
80 }
81 
82 static void
83 power_list(struct nvme_controller_data *cdata)
84 {
85 	int i;
86 
87 	printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
88 	printf(" #   Max pwr  Enter Lat  Exit Lat RT RL WT WL Idle Pwr  Act Pwr Workloadd\n");
89 	printf("--  --------  --------- --------- -- -- -- -- -------- -------- --\n");
90 	for (i = 0; i <= cdata->npss; i++)
91 		power_list_one(i, &cdata->power_state[i]);
92 }
93 
94 static void
95 power_set(int fd, int power_val, int workload, int perm)
96 {
97 	struct nvme_pt_command	pt;
98 	uint32_t p;
99 
100 	p = perm ? (1u << 31) : 0;
101 	memset(&pt, 0, sizeof(pt));
102 	pt.cmd.opc = NVME_OPC_SET_FEATURES;
103 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
104 	pt.cmd.cdw11 = htole32(power_val | (workload << 5));
105 
106 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
107 		err(1, "set feature power mgmt request failed");
108 
109 	if (nvme_completion_is_error(&pt.cpl))
110 		errx(1, "set feature power mgmt request returned error");
111 }
112 
113 static void
114 power_show(int fd)
115 {
116 	struct nvme_pt_command	pt;
117 
118 	memset(&pt, 0, sizeof(pt));
119 	pt.cmd.opc = NVME_OPC_GET_FEATURES;
120 	pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
121 
122 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
123 		err(1, "set feature power mgmt request failed");
124 
125 	if (nvme_completion_is_error(&pt.cpl))
126 		errx(1, "set feature power mgmt request returned error");
127 
128 	printf("Current Power Mode is %d\n", pt.cpl.cdw0);
129 }
130 
131 static void
132 power(const struct nvme_function *nf, int argc, char *argv[])
133 {
134 	struct nvme_controller_data	cdata;
135 	int				ch, listflag = 0, powerflag = 0, power_val = 0, fd;
136 	int				workload = 0;
137 	char				*end;
138 
139 	while ((ch = getopt(argc, argv, "lp:w:")) != -1) {
140 		switch ((char)ch) {
141 		case 'l':
142 			listflag = 1;
143 			break;
144 		case 'p':
145 			powerflag = 1;
146 			power_val = strtol(optarg, &end, 0);
147 			if (*end != '\0') {
148 				fprintf(stderr, "Invalid power state number: %s\n", optarg);
149 				usage(nf);
150 			}
151 			break;
152 		case 'w':
153 			workload = strtol(optarg, &end, 0);
154 			if (*end != '\0') {
155 				fprintf(stderr, "Invalid workload hint: %s\n", optarg);
156 				usage(nf);
157 			}
158 			break;
159 		default:
160 			usage(nf);
161 		}
162 	}
163 
164 	/* Check that a controller was specified. */
165 	if (optind >= argc)
166 		usage(nf);
167 
168 	if (listflag && powerflag) {
169 		fprintf(stderr, "Can't set power and list power states\n");
170 		usage(nf);
171 	}
172 
173 	open_dev(argv[optind], &fd, 1, 1);
174 	read_controller_data(fd, &cdata);
175 
176 	if (listflag) {
177 		power_list(&cdata);
178 		goto out;
179 	}
180 
181 	if (powerflag) {
182 		power_set(fd, power_val, workload, 0);
183 		goto out;
184 	}
185 	power_show(fd);
186 
187 out:
188 	close(fd);
189 	exit(0);
190 }
191 
192 NVME_COMMAND(top, power, power, POWER_USAGE);
193