]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/power.c
Copy googletest 1.8.1 from ^/vendor/google/googletest/1.8.1 to .../contrib/googletest
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / power.c
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 <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "nvmecontrol.h"
42
43 _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
44                "nvme_power_state size wrong");
45
46 #define POWER_USAGE                                                            \
47         "power [-l] [-p new-state [-w workload-hint]] <controller id>\n"
48
49 static void
50 power_list_one(int i, struct nvme_power_state *nps)
51 {
52         int mpower, apower, ipower;
53         uint8_t mps, nops, aps, apw;
54
55         mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) &
56                 NVME_PWR_ST_MPS_MASK;
57         nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) &
58                 NVME_PWR_ST_NOPS_MASK;
59         apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) &
60                 NVME_PWR_ST_APW_MASK;
61         aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) &
62                 NVME_PWR_ST_APS_MASK;
63
64         mpower = nps->mp;
65         if (mps == 0)
66                 mpower *= 100;
67         ipower = nps->idlp;
68         if (nps->ips == 1)
69                 ipower *= 100;
70         apower = nps->actp;
71         if (aps == 1)
72                 apower *= 100;
73         printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n",
74                i, mpower / 10000, mpower % 10000,
75                nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000,
76                nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl,
77                nps->rwt, nps->rwl, ipower / 10000, ipower % 10000,
78                apower / 10000, apower % 10000, apw);
79 }
80
81 static void
82 power_list(struct nvme_controller_data *cdata)
83 {
84         int i;
85
86         printf("\nPower States Supported: %d\n\n", cdata->npss + 1);
87         printf(" #   Max pwr  Enter Lat  Exit Lat RT RL WT WL Idle Pwr  Act Pwr Workloadd\n");
88         printf("--  --------  --------- --------- -- -- -- -- -------- -------- --\n");
89         for (i = 0; i <= cdata->npss; i++)
90                 power_list_one(i, &cdata->power_state[i]);
91 }
92
93 static void
94 power_set(int fd, int power_val, int workload, int perm)
95 {
96         struct nvme_pt_command  pt;
97         uint32_t p;
98
99         p = perm ? (1u << 31) : 0;
100         memset(&pt, 0, sizeof(pt));
101         pt.cmd.opc = NVME_OPC_SET_FEATURES;
102         pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p);
103         pt.cmd.cdw11 = htole32(power_val | (workload << 5));
104
105         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
106                 err(1, "set feature power mgmt request failed");
107
108         if (nvme_completion_is_error(&pt.cpl))
109                 errx(1, "set feature power mgmt request returned error");
110 }
111
112 static void
113 power_show(int fd)
114 {
115         struct nvme_pt_command  pt;
116
117         memset(&pt, 0, sizeof(pt));
118         pt.cmd.opc = NVME_OPC_GET_FEATURES;
119         pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT);
120
121         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
122                 err(1, "set feature power mgmt request failed");
123
124         if (nvme_completion_is_error(&pt.cpl))
125                 errx(1, "set feature power mgmt request returned error");
126
127         printf("Current Power Mode is %d\n", pt.cpl.cdw0);
128 }
129
130 static void
131 power(const struct nvme_function *nf, int argc, char *argv[])
132 {
133         struct nvme_controller_data     cdata;
134         int                             ch, listflag = 0, powerflag = 0, power_val = 0, fd;
135         int                             workload = 0;
136         char                            *end;
137
138         while ((ch = getopt(argc, argv, "lp:w:")) != -1) {
139                 switch ((char)ch) {
140                 case 'l':
141                         listflag = 1;
142                         break;
143                 case 'p':
144                         powerflag = 1;
145                         power_val = strtol(optarg, &end, 0);
146                         if (*end != '\0') {
147                                 fprintf(stderr, "Invalid power state number: %s\n", optarg);
148                                 usage(nf);
149                         }
150                         break;
151                 case 'w':
152                         workload = strtol(optarg, &end, 0);
153                         if (*end != '\0') {
154                                 fprintf(stderr, "Invalid workload hint: %s\n", optarg);
155                                 usage(nf);
156                         }
157                         break;
158                 default:
159                         usage(nf);
160                 }
161         }
162
163         /* Check that a controller was specified. */
164         if (optind >= argc)
165                 usage(nf);
166
167         if (listflag && powerflag) {
168                 fprintf(stderr, "Can't set power and list power states\n");
169                 usage(nf);
170         }
171
172         open_dev(argv[optind], &fd, 1, 1);
173         read_controller_data(fd, &cdata);
174
175         if (listflag) {
176                 power_list(&cdata);
177                 goto out;
178         }
179
180         if (powerflag) {
181                 power_set(fd, power_val, workload, 0);
182                 goto out;
183         }
184         power_show(fd);
185
186 out:
187         close(fd);
188         exit(0);
189 }
190
191 NVME_COMMAND(top, power, power, POWER_USAGE);