1038659e7SWarner Losh /*- 252467047SWarner Losh * Copyright (c) 2016 Netflix, Inc. 3038659e7SWarner Losh * 4038659e7SWarner Losh * Redistribution and use in source and binary forms, with or without 5038659e7SWarner Losh * modification, are permitted provided that the following conditions 6038659e7SWarner Losh * are met: 7038659e7SWarner Losh * 1. Redistributions of source code must retain the above copyright 8038659e7SWarner Losh * notice, this list of conditions and the following disclaimer. 9038659e7SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 10038659e7SWarner Losh * notice, this list of conditions and the following disclaimer in the 11038659e7SWarner Losh * documentation and/or other materials provided with the distribution. 12038659e7SWarner Losh * 13038659e7SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14038659e7SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15038659e7SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16038659e7SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17038659e7SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18038659e7SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19038659e7SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20038659e7SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21038659e7SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22038659e7SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23038659e7SWarner Losh * SUCH DAMAGE. 24038659e7SWarner Losh */ 25038659e7SWarner Losh 26038659e7SWarner Losh #include <sys/cdefs.h> 27038659e7SWarner Losh __FBSDID("$FreeBSD$"); 28038659e7SWarner Losh 29038659e7SWarner Losh #include <sys/param.h> 30038659e7SWarner Losh #include <sys/ioccom.h> 31038659e7SWarner Losh 32038659e7SWarner Losh #include <ctype.h> 33038659e7SWarner Losh #include <err.h> 34038659e7SWarner Losh #include <fcntl.h> 35f634b4c1SWarner Losh #include <stdbool.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 47f634b4c1SWarner Losh #define POWER_NONE 0xffffffffu 48f634b4c1SWarner Losh 49f634b4c1SWarner Losh static struct options { 50f634b4c1SWarner Losh bool list; 51f634b4c1SWarner Losh uint32_t power; 52f634b4c1SWarner Losh uint32_t workload; 53f634b4c1SWarner Losh const char *dev; 54f634b4c1SWarner Losh } opt = { 55f634b4c1SWarner Losh .list = false, 56f634b4c1SWarner Losh .power = POWER_NONE, 57f634b4c1SWarner Losh .workload = 0, 58f634b4c1SWarner Losh .dev = NULL, 59f634b4c1SWarner Losh }; 60a13a291aSWarner Losh 61038659e7SWarner Losh static void 62038659e7SWarner Losh power_list_one(int i, struct nvme_power_state *nps) 63038659e7SWarner Losh { 64038659e7SWarner Losh int mpower, apower, ipower; 650d787e9bSWojciech Macek uint8_t mps, nops, aps, apw; 660d787e9bSWojciech Macek 670d787e9bSWojciech Macek mps = (nps->mps_nops >> NVME_PWR_ST_MPS_SHIFT) & 680d787e9bSWojciech Macek NVME_PWR_ST_MPS_MASK; 690d787e9bSWojciech Macek nops = (nps->mps_nops >> NVME_PWR_ST_NOPS_SHIFT) & 700d787e9bSWojciech Macek NVME_PWR_ST_NOPS_MASK; 710d787e9bSWojciech Macek apw = (nps->apw_aps >> NVME_PWR_ST_APW_SHIFT) & 720d787e9bSWojciech Macek NVME_PWR_ST_APW_MASK; 730d787e9bSWojciech Macek aps = (nps->apw_aps >> NVME_PWR_ST_APS_SHIFT) & 740d787e9bSWojciech Macek NVME_PWR_ST_APS_MASK; 75038659e7SWarner Losh 76038659e7SWarner Losh mpower = nps->mp; 770d787e9bSWojciech Macek if (mps == 0) 78038659e7SWarner Losh mpower *= 100; 79038659e7SWarner Losh ipower = nps->idlp; 80038659e7SWarner Losh if (nps->ips == 1) 81038659e7SWarner Losh ipower *= 100; 82038659e7SWarner Losh apower = nps->actp; 830d787e9bSWojciech Macek if (aps == 1) 84038659e7SWarner Losh apower *= 100; 85038659e7SWarner Losh printf("%2d: %2d.%04dW%c %3d.%03dms %3d.%03dms %2d %2d %2d %2d %2d.%04dW %2d.%04dW %d\n", 86038659e7SWarner Losh i, mpower / 10000, mpower % 10000, 870d787e9bSWojciech Macek nops ? '*' : ' ', nps->enlat / 1000, nps->enlat % 1000, 88038659e7SWarner Losh nps->exlat / 1000, nps->exlat % 1000, nps->rrt, nps->rrl, 89038659e7SWarner Losh nps->rwt, nps->rwl, ipower / 10000, ipower % 10000, 900d787e9bSWojciech Macek apower / 10000, apower % 10000, apw); 91038659e7SWarner Losh } 92038659e7SWarner Losh 93038659e7SWarner Losh static void 94038659e7SWarner Losh power_list(struct nvme_controller_data *cdata) 95038659e7SWarner Losh { 96038659e7SWarner Losh int i; 97038659e7SWarner Losh 98038659e7SWarner Losh printf("\nPower States Supported: %d\n\n", cdata->npss + 1); 99038659e7SWarner Losh printf(" # Max pwr Enter Lat Exit Lat RT RL WT WL Idle Pwr Act Pwr Workloadd\n"); 100038659e7SWarner Losh printf("-- -------- --------- --------- -- -- -- -- -------- -------- --\n"); 101038659e7SWarner Losh for (i = 0; i <= cdata->npss; i++) 102038659e7SWarner Losh power_list_one(i, &cdata->power_state[i]); 103038659e7SWarner Losh } 104038659e7SWarner Losh 105038659e7SWarner Losh static void 106b130d07fSDimitry Andric power_set(int fd, int power_val, int workload, int perm) 107038659e7SWarner Losh { 108038659e7SWarner Losh struct nvme_pt_command pt; 109038659e7SWarner Losh uint32_t p; 110038659e7SWarner Losh 111038659e7SWarner Losh p = perm ? (1u << 31) : 0; 112038659e7SWarner Losh memset(&pt, 0, sizeof(pt)); 1139544e6dcSChuck Tuffli pt.cmd.opc = NVME_OPC_SET_FEATURES; 1140d787e9bSWojciech Macek pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT | p); 1150d787e9bSWojciech Macek pt.cmd.cdw11 = htole32(power_val | (workload << 5)); 116038659e7SWarner Losh 117038659e7SWarner Losh if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 118038659e7SWarner Losh err(1, "set feature power mgmt request failed"); 119038659e7SWarner Losh 120038659e7SWarner Losh if (nvme_completion_is_error(&pt.cpl)) 121038659e7SWarner Losh errx(1, "set feature power mgmt request returned error"); 122038659e7SWarner Losh } 123038659e7SWarner Losh 124038659e7SWarner Losh static void 125038659e7SWarner Losh power_show(int fd) 126038659e7SWarner Losh { 127038659e7SWarner Losh struct nvme_pt_command pt; 128038659e7SWarner Losh 129038659e7SWarner Losh memset(&pt, 0, sizeof(pt)); 1309544e6dcSChuck Tuffli pt.cmd.opc = NVME_OPC_GET_FEATURES; 1310d787e9bSWojciech Macek pt.cmd.cdw10 = htole32(NVME_FEAT_POWER_MANAGEMENT); 132038659e7SWarner Losh 133038659e7SWarner Losh if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 134038659e7SWarner Losh err(1, "set feature power mgmt request failed"); 135038659e7SWarner Losh 136038659e7SWarner Losh if (nvme_completion_is_error(&pt.cpl)) 137038659e7SWarner Losh errx(1, "set feature power mgmt request returned error"); 138038659e7SWarner Losh 139038659e7SWarner Losh printf("Current Power Mode is %d\n", pt.cpl.cdw0); 140038659e7SWarner Losh } 141038659e7SWarner Losh 142a13a291aSWarner Losh static void 143f634b4c1SWarner Losh power(const struct cmd *f, int argc, char *argv[]) 144038659e7SWarner Losh { 145038659e7SWarner Losh struct nvme_controller_data cdata; 146f634b4c1SWarner Losh int fd; 147*5458a1c8SAlexander Motin char *path; 148*5458a1c8SAlexander Motin uint32_t nsid; 149038659e7SWarner Losh 1506995fb5eSDavid Bright if (arg_parse(argc, argv, f)) 1516995fb5eSDavid Bright return; 152038659e7SWarner Losh 153f634b4c1SWarner Losh if (opt.list && opt.power != POWER_NONE) { 154038659e7SWarner Losh fprintf(stderr, "Can't set power and list power states\n"); 155f634b4c1SWarner Losh arg_help(argc, argv, f); 156038659e7SWarner Losh } 157038659e7SWarner Losh 158f634b4c1SWarner Losh open_dev(opt.dev, &fd, 1, 1); 159*5458a1c8SAlexander Motin get_nsid(fd, &path, &nsid); 160*5458a1c8SAlexander Motin if (nsid != 0) { 161*5458a1c8SAlexander Motin close(fd); 162*5458a1c8SAlexander Motin open_dev(path, &fd, 1, 1); 163*5458a1c8SAlexander Motin } 164*5458a1c8SAlexander Motin free(path); 165038659e7SWarner Losh 166f634b4c1SWarner Losh if (opt.list) { 167f634b4c1SWarner Losh read_controller_data(fd, &cdata); 168038659e7SWarner Losh power_list(&cdata); 169038659e7SWarner Losh goto out; 170038659e7SWarner Losh } 171038659e7SWarner Losh 172f634b4c1SWarner Losh if (opt.power != POWER_NONE) { 173f634b4c1SWarner Losh power_set(fd, opt.power, opt.workload, 0); 174038659e7SWarner Losh goto out; 175038659e7SWarner Losh } 176038659e7SWarner Losh power_show(fd); 177038659e7SWarner Losh 178038659e7SWarner Losh out: 179038659e7SWarner Losh close(fd); 180038659e7SWarner Losh exit(0); 181038659e7SWarner Losh } 182a13a291aSWarner Losh 183f634b4c1SWarner Losh static const struct opts power_opts[] = { 184f634b4c1SWarner Losh #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 185f634b4c1SWarner Losh OPT("list", 'l', arg_none, opt, list, 186f634b4c1SWarner Losh "List the valid power states"), 187f634b4c1SWarner Losh OPT("power", 'p', arg_uint32, opt, power, 188f634b4c1SWarner Losh "Set the power state"), 189f634b4c1SWarner Losh OPT("workload", 'w', arg_uint32, opt, workload, 190f634b4c1SWarner Losh "Set the workload"), 191f634b4c1SWarner Losh { NULL, 0, arg_none, NULL, NULL } 192f634b4c1SWarner Losh }; 193f634b4c1SWarner Losh #undef OPT 194f634b4c1SWarner Losh 195f634b4c1SWarner Losh static const struct args power_args[] = { 196*5458a1c8SAlexander Motin { arg_string, &opt.dev, "controller-id|namespace-id" }, 197f634b4c1SWarner Losh { arg_none, NULL, NULL }, 198f634b4c1SWarner Losh }; 199f634b4c1SWarner Losh 200f634b4c1SWarner Losh static struct cmd power_cmd = { 201f634b4c1SWarner Losh .name = "power", 202f634b4c1SWarner Losh .fn = power, 203f634b4c1SWarner Losh .descr = "Manage power states for the drive", 204f634b4c1SWarner Losh .ctx_size = sizeof(opt), 205f634b4c1SWarner Losh .opts = power_opts, 206f634b4c1SWarner Losh .args = power_args, 207f634b4c1SWarner Losh }; 208f634b4c1SWarner Losh 209f634b4c1SWarner Losh CMD_COMMAND(power_cmd); 210