1038659e7SWarner Losh /*- 2038659e7SWarner Losh * Copyright (c) 2016 Netflix, Inc 3038659e7SWarner Losh * All rights reserved. 4038659e7SWarner Losh * 5038659e7SWarner Losh * Redistribution and use in source and binary forms, with or without 6038659e7SWarner Losh * modification, are permitted provided that the following conditions 7038659e7SWarner Losh * are met: 8038659e7SWarner Losh * 1. Redistributions of source code must retain the above copyright 9038659e7SWarner Losh * notice, this list of conditions and the following disclaimer. 10038659e7SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 11038659e7SWarner Losh * notice, this list of conditions and the following disclaimer in the 12038659e7SWarner Losh * documentation and/or other materials provided with the distribution. 13038659e7SWarner Losh * 14038659e7SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15038659e7SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16038659e7SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17038659e7SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18038659e7SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19038659e7SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20038659e7SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21038659e7SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22038659e7SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23038659e7SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24038659e7SWarner Losh * SUCH DAMAGE. 25038659e7SWarner Losh */ 26038659e7SWarner Losh 27038659e7SWarner Losh #include <sys/cdefs.h> 28038659e7SWarner Losh __FBSDID("$FreeBSD$"); 29038659e7SWarner Losh 30038659e7SWarner Losh #include <sys/param.h> 31038659e7SWarner Losh #include <sys/ioccom.h> 32038659e7SWarner Losh 33038659e7SWarner Losh #include <ctype.h> 34038659e7SWarner Losh #include <err.h> 35038659e7SWarner Losh #include <fcntl.h> 36038659e7SWarner Losh #include <stddef.h> 37038659e7SWarner Losh #include <stdio.h> 38038659e7SWarner Losh #include <stdlib.h> 39038659e7SWarner Losh #include <string.h> 40038659e7SWarner Losh #include <unistd.h> 41038659e7SWarner Losh 42038659e7SWarner Losh #include "nvmecontrol.h" 43038659e7SWarner Losh 44038659e7SWarner Losh _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY, 45038659e7SWarner Losh "nvme_power_state size wrong"); 46038659e7SWarner Losh 47038659e7SWarner Losh static void 48038659e7SWarner Losh power_usage(void) 49038659e7SWarner Losh { 50038659e7SWarner Losh fprintf(stderr, "usage:\n"); 51038659e7SWarner Losh fprintf(stderr, POWER_USAGE); 52038659e7SWarner Losh exit(1); 53038659e7SWarner Losh } 54038659e7SWarner Losh 55038659e7SWarner Losh static void 56038659e7SWarner Losh power_list_one(int i, struct nvme_power_state *nps) 57038659e7SWarner Losh { 58038659e7SWarner Losh int mpower, apower, ipower; 59*0d787e9bSWojciech Macek uint8_t mps, nops, aps, apw; 60*0d787e9bSWojciech Macek 61*0d787e9bSWojciech Macek mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) & 62*0d787e9bSWojciech Macek NVME_PWR_ST_MPS_MASK; 63*0d787e9bSWojciech Macek nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) & 64*0d787e9bSWojciech Macek NVME_PWR_ST_NOPS_MASK; 65*0d787e9bSWojciech Macek apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) & 66*0d787e9bSWojciech Macek NVME_PWR_ST_APW_MASK; 67*0d787e9bSWojciech Macek aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) & 68*0d787e9bSWojciech Macek NVME_PWR_ST_APS_MASK; 69038659e7SWarner Losh 70038659e7SWarner Losh mpower = nps->mp; 71*0d787e9bSWojciech 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; 77*0d787e9bSWojciech 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, 81*0d787e9bSWojciech 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, 84*0d787e9bSWojciech 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); 93038659e7SWarner Losh printf(" # Max pwr Enter Lat Exit Lat RT RL WT WL Idle Pwr Act Pwr Workloadd\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)); 107*0d787e9bSWojciech Macek pt.cmd.opc_fuse = NVME_CMD_SET_OPC(NVME_OPC_SET_FEATURES); 108*0d787e9bSWojciech Macek pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p); 109*0d787e9bSWojciech Macek pt.cmd.cdw11 = htole32(power_val | (workload << 5)); 110038659e7SWarner Losh 111038659e7SWarner Losh if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 112038659e7SWarner Losh err(1, "set feature power mgmt request failed"); 113038659e7SWarner Losh 114038659e7SWarner Losh if (nvme_completion_is_error(&pt.cpl)) 115038659e7SWarner Losh errx(1, "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)); 124*0d787e9bSWojciech Macek pt.cmd.opc_fuse = NVME_CMD_SET_OPC(NVME_OPC_GET_FEATURES); 125*0d787e9bSWojciech Macek pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT); 126038659e7SWarner Losh 127038659e7SWarner Losh if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 128038659e7SWarner Losh err(1, "set feature power mgmt request failed"); 129038659e7SWarner Losh 130038659e7SWarner Losh if (nvme_completion_is_error(&pt.cpl)) 131038659e7SWarner Losh errx(1, "set feature power mgmt request returned error"); 132038659e7SWarner Losh 133038659e7SWarner Losh printf("Current Power Mode is %d\n", pt.cpl.cdw0); 134038659e7SWarner Losh } 135038659e7SWarner Losh 136038659e7SWarner Losh void 137038659e7SWarner Losh power(int argc, char *argv[]) 138038659e7SWarner Losh { 139038659e7SWarner Losh struct nvme_controller_data cdata; 140b130d07fSDimitry Andric int ch, listflag = 0, powerflag = 0, power_val = 0, fd; 141038659e7SWarner Losh int workload = 0; 142038659e7SWarner Losh char *end; 143038659e7SWarner Losh 144038659e7SWarner Losh while ((ch = getopt(argc, argv, "lp:w:")) != -1) { 145038659e7SWarner Losh switch ((char)ch) { 146038659e7SWarner Losh case 'l': 147038659e7SWarner Losh listflag = 1; 148038659e7SWarner Losh break; 149038659e7SWarner Losh case 'p': 150038659e7SWarner Losh powerflag = 1; 151b130d07fSDimitry Andric power_val = strtol(optarg, &end, 0); 152038659e7SWarner Losh if (*end != '\0') { 153038659e7SWarner Losh fprintf(stderr, "Invalid power state number: %s\n", optarg); 154038659e7SWarner Losh power_usage(); 155038659e7SWarner Losh } 156038659e7SWarner Losh break; 157038659e7SWarner Losh case 'w': 158038659e7SWarner Losh workload = strtol(optarg, &end, 0); 159038659e7SWarner Losh if (*end != '\0') { 160038659e7SWarner Losh fprintf(stderr, "Invalid workload hint: %s\n", optarg); 161038659e7SWarner Losh power_usage(); 162038659e7SWarner Losh } 163038659e7SWarner Losh break; 164038659e7SWarner Losh default: 165038659e7SWarner Losh power_usage(); 166038659e7SWarner Losh } 167038659e7SWarner Losh } 168038659e7SWarner Losh 169038659e7SWarner Losh /* Check that a controller was specified. */ 170038659e7SWarner Losh if (optind >= argc) 171038659e7SWarner Losh power_usage(); 172038659e7SWarner Losh 173038659e7SWarner Losh if (listflag && powerflag) { 174038659e7SWarner Losh fprintf(stderr, "Can't set power and list power states\n"); 175038659e7SWarner Losh power_usage(); 176038659e7SWarner Losh } 177038659e7SWarner Losh 178038659e7SWarner Losh open_dev(argv[optind], &fd, 1, 1); 179038659e7SWarner Losh read_controller_data(fd, &cdata); 180038659e7SWarner Losh 181038659e7SWarner Losh if (listflag) { 182038659e7SWarner Losh power_list(&cdata); 183038659e7SWarner Losh goto out; 184038659e7SWarner Losh } 185038659e7SWarner Losh 186038659e7SWarner Losh if (powerflag) { 187b130d07fSDimitry Andric power_set(fd, power_val, workload, 0); 188038659e7SWarner Losh goto out; 189038659e7SWarner Losh } 190038659e7SWarner Losh power_show(fd); 191038659e7SWarner Losh 192038659e7SWarner Losh out: 193038659e7SWarner Losh close(fd); 194038659e7SWarner Losh exit(0); 195038659e7SWarner Losh } 196