1 /* $NetBSD: power.c,v 1.6 2020/09/27 18:17:35 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 2016 Netflix, Inc 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: power.c,v 1.6 2020/09/27 18:17:35 jdolecek Exp $"); 32 #if 0 33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/power.c 329824 2018-02-22 13:32:31Z wma $"); 34 #endif 35 #endif 36 37 #include <sys/param.h> 38 #include <sys/ioccom.h> 39 40 #include <ctype.h> 41 #include <err.h> 42 #include <fcntl.h> 43 #include <stddef.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include "nvmectl.h" 50 51 __dead static void 52 power_usage(void) 53 { 54 fprintf(stderr, "usage:\n"); 55 fprintf(stderr, "\t%s " POWER_USAGE, getprogname()); 56 exit(1); 57 } 58 59 static void 60 power_list_one(int i, struct nvm_identify_psd *psd) 61 { 62 int mpower, apower, ipower; 63 64 mpower = psd->mp; 65 if (!(psd->flags & NVME_PSD_MPS)) 66 mpower *= 100; 67 ipower = psd->idlp; 68 if (__SHIFTOUT(psd->ips, NVME_PSD_IPS_MASK) == 1) 69 ipower *= 100; 70 apower = psd->actp; 71 if (__SHIFTOUT(psd->ap, NVME_PSD_APS_MASK) == 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 (psd->flags & NVME_PSD_NOPS) ? '*' : ' ', 76 psd->enlat / 1000, psd->enlat % 1000, 77 psd->exlat / 1000, psd->exlat % 1000, 78 (uint8_t)(psd->rrt & NVME_PSD_RRT_MASK), 79 (uint8_t)(psd->rrl & NVME_PSD_RRL_MASK), 80 (uint8_t)(psd->rwt & NVME_PSD_RWT_MASK), 81 (uint8_t)(psd->rwl & NVME_PSD_RWL_MASK), 82 ipower / 10000, ipower % 10000, apower / 10000, apower % 10000, 83 (uint16_t)__SHIFTOUT(psd->ap, NVME_PSD_APW_MASK)); 84 } 85 86 static void 87 power_list(struct nvm_identify_controller *cdata) 88 { 89 int i; 90 91 printf("\nPower States Supported: %d\n\n", cdata->npss + 1); 92 printf(" # Max pwr Enter Lat Exit Lat RT RL WT WL Idle Pwr Act Pwr Workloadd\n"); 93 printf("-- -------- --------- --------- -- -- -- -- -------- -------- --\n"); 94 for (i = 0; i <= cdata->npss; i++) 95 power_list_one(i, &cdata->psd[i]); 96 } 97 98 static void 99 power_set(int fd, int power_val, int workload, int saveflag) 100 { 101 struct nvme_pt_command pt; 102 103 memset(&pt, 0, sizeof(pt)); 104 pt.cmd.opcode = NVM_ADMIN_SET_FEATURES; 105 pt.cmd.cdw10 = NVM_FEAT_POWER_MANAGEMENT 106 | (saveflag ? NVM_SET_FEATURES_SV : 0); 107 pt.cmd.cdw11 = power_val | (workload << 5); 108 109 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 110 err(1, "set feature power mgmt request failed"); 111 112 if (nvme_completion_is_error(&pt.cpl)) 113 errx(1, "set feature power mgmt request returned error"); 114 } 115 116 static void 117 power_show(int fd) 118 { 119 struct nvme_pt_command pt; 120 121 memset(&pt, 0, sizeof(pt)); 122 pt.cmd.opcode = NVM_ADMIN_GET_FEATURES; 123 pt.cmd.cdw10 = NVM_FEAT_POWER_MANAGEMENT; 124 125 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 126 err(1, "set feature power mgmt request failed"); 127 128 if (nvme_completion_is_error(&pt.cpl)) 129 errx(1, "set feature power mgmt request returned error"); 130 131 printf("Current Power State is %d, Workload Hint %d\n", 132 pt.cpl.cdw0 & ((1 << 5) - 1), 133 pt.cpl.cdw0 >> 5); 134 } 135 136 void 137 power(int argc, char *argv[]) 138 { 139 struct nvm_identify_controller cdata; 140 int ch, listflag = 0, powerflag = 0, power_val = 0, fd, saveflag = 0; 141 int workload = 0; 142 char *end; 143 144 while ((ch = getopt(argc, argv, "lsp:w:")) != -1) { 145 switch (ch) { 146 case 'l': 147 listflag = 1; 148 break; 149 case 's': 150 saveflag = 1; 151 break; 152 case 'p': 153 powerflag = 1; 154 power_val = strtol(optarg, &end, 0); 155 if (*end != '\0') { 156 fprintf(stderr, "Invalid power state number: %s\n", optarg); 157 power_usage(); 158 } 159 break; 160 case 'w': 161 workload = strtol(optarg, &end, 0); 162 if (*end != '\0') { 163 fprintf(stderr, "Invalid workload hint: %s\n", optarg); 164 power_usage(); 165 } 166 break; 167 default: 168 power_usage(); 169 } 170 } 171 172 /* Check that a controller was specified. */ 173 if (optind >= argc) 174 power_usage(); 175 176 if (listflag && powerflag) { 177 fprintf(stderr, "Can't set power and list power states\n"); 178 power_usage(); 179 } 180 181 open_dev(argv[optind], &fd, 1, 1); 182 read_controller_data(fd, &cdata); 183 184 if (listflag) { 185 power_list(&cdata); 186 goto out; 187 } 188 189 if (powerflag) { 190 power_set(fd, power_val, workload, saveflag); 191 goto out; 192 } 193 power_show(fd); 194 195 out: 196 close(fd); 197 exit(0); 198 } 199