| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/** |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2020 Western Digital Corporation or its affiliates. |
| 5 |
* |
| 6 |
* Authors: Keith Busch <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Prints the values of the nvme register map. Use the nvme controller resource |
| 11 |
* for your pci device found in /sys/class/nvme/nvmeX/device/resource0 |
| 12 |
*/ |
| 13 |
|
| 14 |
#define __SANE_USERSPACE_TYPES__ |
| 15 |
|
| 16 |
#include <fcntl.h> |
| 17 |
#include <inttypes.h> |
| 18 |
#include <libnvme.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdint.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include <sys/mman.h> |
| 25 |
|
| 26 |
#include <ccan/endian/endian.h> |
| 27 |
|
| 28 |
static inline uint32_t nvme_mmio_read32(volatile void *addr) |
| 29 |
{ |
| 30 |
uint32_t *p = (__le32 *)addr; |
| 31 |
|
| 32 |
return le32_to_cpu(*p); |
| 33 |
} |
| 34 |
|
| 35 |
static inline uint64_t nvme_mmio_read64(volatile void *addr) |
| 36 |
{ |
| 37 |
volatile __u32 *p = (__u32 *)addr; |
| 38 |
uint32_t low, high; |
| 39 |
|
| 40 |
low = nvme_mmio_read32(p); |
| 41 |
high = nvme_mmio_read32(p + 1); |
| 42 |
|
| 43 |
return low + ((uint64_t)high << 32); |
| 44 |
} |
| 45 |
|
| 46 |
void nvme_print_registers(void *regs) |
| 47 |
{ |
| 48 |
__u64 cap = nvme_mmio_read64(regs + NVME_REG_CAP); |
| 49 |
__u32 vs = nvme_mmio_read32(regs + NVME_REG_VS); |
| 50 |
__u32 intms = nvme_mmio_read32(regs + NVME_REG_INTMS); |
| 51 |
__u32 intmc = nvme_mmio_read32(regs + NVME_REG_INTMC); |
| 52 |
__u32 cc = nvme_mmio_read32(regs + NVME_REG_CC); |
| 53 |
__u32 csts = nvme_mmio_read32(regs + NVME_REG_CSTS); |
| 54 |
__u32 nssr = nvme_mmio_read32(regs + NVME_REG_NSSR); |
| 55 |
__u32 aqa = nvme_mmio_read32(regs + NVME_REG_AQA); |
| 56 |
__u64 asq = nvme_mmio_read64(regs + NVME_REG_ASQ); |
| 57 |
__u64 acq = nvme_mmio_read64(regs + NVME_REG_ACQ); |
| 58 |
__u32 cmbloc = nvme_mmio_read32(regs + NVME_REG_CMBLOC); |
| 59 |
__u32 cmbsz = nvme_mmio_read32(regs + NVME_REG_CMBSZ); |
| 60 |
__u32 bpinfo = nvme_mmio_read32(regs + NVME_REG_BPINFO); |
| 61 |
__u32 bprsel = nvme_mmio_read32(regs + NVME_REG_BPRSEL); |
| 62 |
__u64 bpmbl = nvme_mmio_read64(regs + NVME_REG_BPMBL); |
| 63 |
__u64 cmbmsc = nvme_mmio_read64(regs + NVME_REG_CMBMSC); |
| 64 |
__u32 cmbsts = nvme_mmio_read32(regs + NVME_REG_CMBSTS); |
| 65 |
__u32 pmrcap = nvme_mmio_read32(regs + NVME_REG_PMRCAP); |
| 66 |
__u32 pmrctl = nvme_mmio_read32(regs + NVME_REG_PMRCTL); |
| 67 |
__u32 pmrsts = nvme_mmio_read32(regs + NVME_REG_PMRSTS); |
| 68 |
__u32 pmrebs = nvme_mmio_read32(regs + NVME_REG_PMREBS); |
| 69 |
__u32 pmrswtp = nvme_mmio_read32(regs + NVME_REG_PMRSWTP); |
| 70 |
__u64 pmrmsc = nvme_mmio_read32(regs + NVME_REG_PMRMSCL) | |
| 71 |
(__u64)nvme_mmio_read64(regs + NVME_REG_PMRMSCU) << 32; |
| 72 |
|
| 73 |
printf("%-10s : %llx\n", "CAP", cap); |
| 74 |
printf(" %-8s : %llx\n", "MQES", NVME_CAP_MQES(cap)); |
| 75 |
printf(" %-8s : %llx\n", "CQRS", NVME_CAP_CQR(cap)); |
| 76 |
printf(" %-8s : %llx\n", "AMS", NVME_CAP_AMS(cap)); |
| 77 |
printf(" %-8s : %llx\n", "TO", NVME_CAP_TO(cap)); |
| 78 |
printf(" %-8s : %llx\n", "DSTRD", NVME_CAP_DSTRD(cap)); |
| 79 |
printf(" %-8s : %llx\n", "NSSRC", NVME_CAP_NSSRC(cap)); |
| 80 |
printf(" %-8s : %llx\n", "CSS", NVME_CAP_CSS(cap)); |
| 81 |
printf(" %-8s : %llx\n", "BPS", NVME_CAP_BPS(cap)); |
| 82 |
printf(" %-8s : %llx\n", "CPS", NVME_CAP_CPS(cap)); |
| 83 |
printf(" %-8s : %llx\n", "MPSMIN", NVME_CAP_MPSMIN(cap)); |
| 84 |
printf(" %-8s : %llx\n", "MPSMAX", NVME_CAP_MPSMAX(cap)); |
| 85 |
printf(" %-8s : %llx\n", "PMRS", NVME_CAP_PMRS(cap)); |
| 86 |
printf(" %-8s : %llx\n", "CMBS", NVME_CAP_CMBS(cap)); |
| 87 |
printf(" %-8s : %llx\n", "NSSS", NVME_CAP_NSSS(cap)); |
| 88 |
printf(" %-8s : %llx\n", "CRMS", NVME_CAP_CRMS(cap)); |
| 89 |
|
| 90 |
printf("%-10s : %x\n", "VS", vs); |
| 91 |
printf(" %-8s : %x\n", "MJR", NVME_VS_TER(vs)); |
| 92 |
printf(" %-8s : %x\n", "MNR", NVME_VS_MNR(vs)); |
| 93 |
printf(" %-8s : %x\n", "TER", NVME_VS_MJR(vs)); |
| 94 |
|
| 95 |
printf("%-10s : %x\n", "INTMS", intms); |
| 96 |
printf("%-10s : %x\n", "INTMC", intmc); |
| 97 |
|
| 98 |
printf("%-10s : %x\n", "CC", cc); |
| 99 |
printf(" %-8s : %x\n", "EN", NVME_CC_EN(cc)); |
| 100 |
printf(" %-8s : %x\n", "CSS", NVME_CC_CSS(cc)); |
| 101 |
printf(" %-8s : %x\n", "MPS", NVME_CC_MPS(cc)); |
| 102 |
printf(" %-8s : %x\n", "AMS", NVME_CC_AMS(cc)); |
| 103 |
printf(" %-8s : %x\n", "SHN", NVME_CC_SHN(cc)); |
| 104 |
printf(" %-8s : %x\n", "IOSQES", NVME_CC_IOSQES(cc)); |
| 105 |
printf(" %-8s : %x\n", "IOCQES", NVME_CC_IOCQES(cc)); |
| 106 |
|
| 107 |
printf("%-10s : %x\n", "CSTS", csts); |
| 108 |
printf(" %-8s : %x\n", "RDY", NVME_CSTS_RDY(csts)); |
| 109 |
printf(" %-8s : %x\n", "CFS", NVME_CSTS_CFS(csts)); |
| 110 |
printf(" %-8s : %x\n", "SHST", NVME_CSTS_SHST(csts)); |
| 111 |
printf(" %-8s : %x\n", "NSSRO", NVME_CSTS_NSSRO(csts)); |
| 112 |
printf(" %-8s : %x\n", "PP", NVME_CSTS_PP(csts)); |
| 113 |
printf(" %-8s : %x\n", "ST", NVME_CSTS_ST(csts)); |
| 114 |
|
| 115 |
printf("%-10s : %x\n", "NSSR", nssr); |
| 116 |
|
| 117 |
printf("%-10s : %x\n", "AQA", aqa); |
| 118 |
printf(" %-8s : %x\n", "ASQS", NVME_AQA_ASQS(aqa)); |
| 119 |
printf(" %-8s : %x\n", "ACQS", NVME_AQA_ACQS(aqa)); |
| 120 |
|
| 121 |
printf("%-10s : %llx\n", "ASQ", asq); |
| 122 |
printf("%-10s : %llx\n", "ACQ", acq); |
| 123 |
|
| 124 |
printf("%-10s : %x\n", "CMBLOC", cmbloc); |
| 125 |
printf(" %-8s : %x\n", "BIR", NVME_CMBLOC_BIR(cmbloc)); |
| 126 |
printf(" %-8s : %x\n", "CQMMS", NVME_CMBLOC_CQMMS(cmbloc)); |
| 127 |
printf(" %-8s : %x\n", "CQPDS", NVME_CMBLOC_CQPDS(cmbloc)); |
| 128 |
printf(" %-8s : %x\n", "CDPLMS", NVME_CMBLOC_CDPLMS(cmbloc)); |
| 129 |
printf(" %-8s : %x\n", "CDPCILS", NVME_CMBLOC_CDPCILS(cmbloc)); |
| 130 |
printf(" %-8s : %x\n", "CDMMMS", NVME_CMBLOC_CDMMMS(cmbloc)); |
| 131 |
printf(" %-8s : %x\n", "CQDA", NVME_CMBLOC_CQDA(cmbloc)); |
| 132 |
printf(" %-8s : %x\n", "OFST", NVME_CMBLOC_OFST(cmbloc)); |
| 133 |
|
| 134 |
printf("%-10s : %x\n", "CMBSZ", cmbsz); |
| 135 |
printf(" %-8s : %x\n", "SQS", NVME_CMBSZ_SQS(cmbsz)); |
| 136 |
printf(" %-8s : %x\n", "CQS", NVME_CMBSZ_CQS(cmbsz)); |
| 137 |
printf(" %-8s : %x\n", "LISTS", NVME_CMBSZ_LISTS(cmbsz)); |
| 138 |
printf(" %-8s : %x\n", "RDS", NVME_CMBSZ_RDS(cmbsz)); |
| 139 |
printf(" %-8s : %x\n", "WDS", NVME_CMBSZ_WDS(cmbsz)); |
| 140 |
printf(" %-8s : %x\n", "SZU", NVME_CMBSZ_SZU(cmbsz)); |
| 141 |
printf(" %-8s : %x\n", "SZ", NVME_CMBSZ_SZ(cmbsz)); |
| 142 |
printf(" %-8s : %llx\n", "bytes", nvme_cmb_size(cmbsz)); |
| 143 |
|
| 144 |
printf("%-10s : %x\n", "BPINFO", bpinfo); |
| 145 |
printf(" %-8s : %x\n", "BPSZ", NVME_BPINFO_BPSZ(bpinfo)); |
| 146 |
printf(" %-8s : %x\n", "BRS", NVME_BPINFO_BRS(bpinfo)); |
| 147 |
printf(" %-8s : %x\n", "ABPID", NVME_BPINFO_ABPID(bpinfo)); |
| 148 |
|
| 149 |
printf("%-10s : %x\n", "BPRSEL", bprsel); |
| 150 |
printf(" %-8s : %x\n", "BPRSZ", NVME_BPRSEL_BPRSZ(bprsel)); |
| 151 |
printf(" %-8s : %x\n", "BPROF", NVME_BPRSEL_BPROF(bprsel)); |
| 152 |
printf(" %-8s : %x\n", "BPID", NVME_BPRSEL_BPID(bprsel)); |
| 153 |
|
| 154 |
printf("%-10s : %llx\n", "BPMBL", bpmbl); |
| 155 |
|
| 156 |
printf("%-10s : %llx\n", "CMBMSC", cmbmsc); |
| 157 |
printf(" %-8s : %llx\n", "CRE", NVME_CMBMSC_CRE(cmbmsc)); |
| 158 |
printf(" %-8s : %llx\n", "CMSE", NVME_CMBMSC_CMSE(cmbmsc)); |
| 159 |
printf(" %-8s : %llx\n", "CBA", NVME_CMBMSC_CBA(cmbmsc)); |
| 160 |
|
| 161 |
printf("%-10s : %x\n", "CMBSTS", cmbsts); |
| 162 |
printf(" %-8s : %x\n", "CBAI", NVME_CMBSTS_CBAI(cmbsts)); |
| 163 |
|
| 164 |
printf("%-10s : %x\n", "PMRCAP", pmrcap); |
| 165 |
printf(" %-8s : %x\n", "RDS", NVME_PMRCAP_RDS(pmrcap)); |
| 166 |
printf(" %-8s : %x\n", "WDS", NVME_PMRCAP_WDS(pmrcap)); |
| 167 |
printf(" %-8s : %x\n", "BIR", NVME_PMRCAP_BIR(pmrcap)); |
| 168 |
printf(" %-8s : %x\n", "PMRTU", NVME_PMRCAP_PMRTU(pmrcap)); |
| 169 |
printf(" %-8s : %x\n", "PMRWBM", NVME_PMRCAP_PMRWBM(pmrcap)); |
| 170 |
printf(" %-8s : %x\n", "PMRTO", NVME_PMRCAP_PMRTO(pmrcap)); |
| 171 |
printf(" %-8s : %x\n", "CMSS", NVME_PMRCAP_CMSS(pmrcap)); |
| 172 |
|
| 173 |
printf("%-10s : %x\n", "PMRCTL", pmrctl); |
| 174 |
printf(" %-8s : %x\n", "EN", NVME_PMRCTL_EN(pmrctl)); |
| 175 |
|
| 176 |
printf("%-10s : %x\n", "PMRSTS", pmrsts); |
| 177 |
printf(" %-8s : %x\n", "ERR", NVME_PMRSTS_ERR(pmrsts)); |
| 178 |
printf(" %-8s : %x\n", "NRDY", NVME_PMRSTS_NRDY(pmrsts)); |
| 179 |
printf(" %-8s : %x\n", "HSTS", NVME_PMRSTS_HSTS(pmrsts)); |
| 180 |
printf(" %-8s : %x\n", "CBAI", NVME_PMRSTS_CBAI(pmrsts)); |
| 181 |
|
| 182 |
printf("%-10s : %x\n", "PMREBS", pmrebs); |
| 183 |
printf(" %-8s : %x\n", "PMRSZU", NVME_PMREBS_PMRSZU(pmrebs)); |
| 184 |
printf(" %-8s : %x\n", "RBB", NVME_PMREBS_RBB(pmrebs)); |
| 185 |
printf(" %-8s : %x\n", "PMRWBZ", NVME_PMREBS_PMRWBZ(pmrebs)); |
| 186 |
printf(" %-8s : %llx\n", "bytes", nvme_pmr_size(pmrebs)); |
| 187 |
|
| 188 |
printf("%-10s : %x\n", "PMRSWTP", pmrswtp); |
| 189 |
printf(" %-8s : %x\n", "PMRSWTU", NVME_PMRSWTP_PMRSWTU(pmrswtp)); |
| 190 |
printf(" %-8s : %x\n", "PMRSWTV", NVME_PMRSWTP_PMRSWTV(pmrswtp)); |
| 191 |
printf(" %-8s : %llx\n", "tput", nvme_pmr_throughput(pmrswtp)); |
| 192 |
|
| 193 |
printf("%-10s : %llx\n", "PMRMSC", pmrmsc); |
| 194 |
printf(" %-8s : %llx\n", "CMSE", NVME_PMRMSC_CMSE(pmrmsc)); |
| 195 |
printf(" %-8s : %llx\n", "CBA", NVME_PMRMSC_CBA(pmrmsc)); |
| 196 |
} |
| 197 |
|
| 198 |
int main(int argc, char **argv) |
| 199 |
{ |
| 200 |
int ret, fd; |
| 201 |
char *path; |
| 202 |
void *regs; |
| 203 |
|
| 204 |
if (argc != 2) { |
| 205 |
fprintf(stderr, "%s nvme<X>\n", argv[0]); |
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
ret = asprintf(&path, "/sys/class/nvme/%s/device/resource0", argv[1]); |
| 210 |
if (ret < 0) |
| 211 |
return 0; |
| 212 |
|
| 213 |
printf("open %s\n", path); |
| 214 |
fd = open(path, O_RDONLY | O_SYNC); |
| 215 |
if (fd < 0) { |
| 216 |
fprintf(stderr, "failed to open %s\n", path); |
| 217 |
free(path); |
| 218 |
return 1; |
| 219 |
} |
| 220 |
|
| 221 |
regs = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED, fd, 0); |
| 222 |
if (regs == MAP_FAILED) { |
| 223 |
fprintf(stderr, "failed to map device BAR\n"); |
| 224 |
fprintf(stderr, "did your kernel enable CONFIG_IO_STRICT_DEVMEM?\n"); |
| 225 |
free(path); |
| 226 |
close(fd); |
| 227 |
return 1; |
| 228 |
} |
| 229 |
|
| 230 |
nvme_print_registers(regs); |
| 231 |
munmap(regs, getpagesize()); |
| 232 |
free(path); |
| 233 |
close(fd); |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|