power.c revision 1.3
1/* $NetBSD: power.c,v 1.3 2018/03/17 11:07:26 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.3 2018/03/17 11:07:26 jdolecek Exp $"); 32#if 0 33__FBSDID("$FreeBSD: head/sbin/nvmecontrol/power.c 296672 2016-03-11 17:25:18Z dim $"); 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 52power_usage(void) 53{ 54 fprintf(stderr, "usage:\n"); 55 fprintf(stderr, "\t%s " POWER_USAGE, getprogname()); 56 exit(1); 57} 58 59static void 60power_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 86static void 87power_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 98static void 99power_set(int fd, int power_val, int workload, int perm) 100{ 101 struct nvme_pt_command pt; 102 uint32_t p; 103 104 p = perm ? (1u << 31) : 0; 105 memset(&pt, 0, sizeof(pt)); 106 pt.cmd.opcode = NVM_ADMIN_SET_FEATURES; 107 pt.cmd.cdw10 = NVME_FEAT_POWER_MANAGEMENT | p; 108 pt.cmd.cdw11 = power_val | (workload << 5); 109 110 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 111 err(1, "set feature power mgmt request failed"); 112 113 if (nvme_completion_is_error(&pt.cpl)) 114 errx(1, "set feature power mgmt request returned error"); 115} 116 117static void 118power_show(int fd) 119{ 120 struct nvme_pt_command pt; 121 122 memset(&pt, 0, sizeof(pt)); 123 pt.cmd.opcode = NVM_ADMIN_GET_FEATURES; 124 pt.cmd.cdw10 = NVME_FEAT_POWER_MANAGEMENT; 125 126 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 127 err(1, "set feature power mgmt request failed"); 128 129 if (nvme_completion_is_error(&pt.cpl)) 130 errx(1, "set feature power mgmt request returned error"); 131 132 printf("Current Power Mode is %d\n", pt.cpl.cdw0); 133} 134 135void 136power(int argc, char *argv[]) 137{ 138 struct nvm_identify_controller cdata; 139 int ch, listflag = 0, powerflag = 0, power_val = 0, fd; 140 int workload = 0; 141 char *end; 142 143 while ((ch = getopt(argc, argv, "lp:w:")) != -1) { 144 switch (ch) { 145 case 'l': 146 listflag = 1; 147 break; 148 case 'p': 149 powerflag = 1; 150 power_val = strtol(optarg, &end, 0); 151 if (*end != '\0') { 152 fprintf(stderr, "Invalid power state number: %s\n", optarg); 153 power_usage(); 154 } 155 break; 156 case 'w': 157 workload = strtol(optarg, &end, 0); 158 if (*end != '\0') { 159 fprintf(stderr, "Invalid workload hint: %s\n", optarg); 160 power_usage(); 161 } 162 break; 163 default: 164 power_usage(); 165 } 166 } 167 168 /* Check that a controller was specified. */ 169 if (optind >= argc) 170 power_usage(); 171 172 if (listflag && powerflag) { 173 fprintf(stderr, "Can't set power and list power states\n"); 174 power_usage(); 175 } 176 177 open_dev(argv[optind], &fd, 1, 1); 178 read_controller_data(fd, &cdata); 179 180 if (listflag) { 181 power_list(&cdata); 182 goto out; 183 } 184 185 if (powerflag) { 186 power_set(fd, power_val, workload, 0); 187 goto out; 188 } 189 power_show(fd); 190 191out: 192 close(fd); 193 exit(0); 194} 195