CFLAGS ?= -O2 -g -Wall -Werror
override CFLAGS += -std=gnu99
-override CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
+override CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__ -ljson-c
LIBUUID = $(shell $(LD) -o /dev/null -luuid >/dev/null 2>&1; echo $$?)
LIBHUGETLBFS = $(shell $(LD) -o /dev/null -lhugetlbfs >/dev/null 2>&1; echo $$?)
HAVE_SYSTEMD = $(shell pkg-config --exists systemd --atleast-version=232; echo $$?)
NVME_DPKG_VERSION=1~`lsb_release -sc`
-OBJS := print.o lightnvm.o models.o plugin.o
+OBJS := models.o plugin.o
-UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o
+UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/user-types.o
PLUGIN_OBJS := \
plugins/intel/intel-nvme.o \
verify-no-dep: nvme.c nvme.h $(OBJS) NVME-VERSION-FILE
$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ $(OBJS) $(LDFLAGS)
-nvme.o: nvme.c nvme.h print.h util/argconfig.h util/suffix.h lightnvm.h
+nvme.o: nvme.c nvme.h util/argconfig.h util/suffix.h
$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c $< $(LDFLAGS)
-%.o: %.c %.h nvme.h print.h util/argconfig.h
+%.o: %.c %.h nvme.h util/argconfig.h
$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -o $@ -c $< $(LDFLAGS)
-%.o: %.c nvme.h print.h util/argconfig.h
+%.o: %.c nvme.h util/argconfig.h
$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -o $@ -c $< $(LDFLAGS)
doc: $(NVME)
-/*
- * lightnvm.c -- LightNVM NVMe integration.
- *
- * Copyright (c) 2016, CNEX Labs.
- *
- * Written by Matias Bjoerling <matias@cnexlabs.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <string.h>
-#include <errno.h>
-#include <inttypes.h>
-
-#include "lightnvm.h"
-#include "print.h"
-
-static int lnvm_open(void)
-{
- char dev[FILENAME_MAX] = NVM_CTRL_FILE;
- int fd;
-
- fd = open(dev, O_WRONLY);
- if (fd < 0) {
- printf("Failed to open LightNVM mgmt interface\n");
- perror(dev);
- return fd;
- }
-
- return fd;
-}
-
-static void lnvm_close(int fd)
-{
- close(fd);
-}
-
-int lnvm_do_init(char *dev, char *mmtype)
-{
- struct nvm_ioctl_dev_init init;
- int fd, ret;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- memset(&init, 0, sizeof(struct nvm_ioctl_dev_init));
- strncpy(init.dev, dev, DISK_NAME_LEN - 1);
- strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN - 1);
-
- ret = ioctl(fd, NVM_DEV_INIT, &init);
- switch (errno) {
- case EINVAL:
- printf("Initialization failed.\n");
- break;
- case EEXIST:
- printf("Device has already been initialized.\n");
- break;
- case 0:
- break;
- default:
- printf("Unknown error occurred (%d)\n", errno);
- break;
- }
-
- lnvm_close(fd);
-
- return ret;
-}
-
-int lnvm_do_list_devices(void)
-{
- struct nvm_ioctl_get_devices devs;
- int fd, ret, i;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- ret = ioctl(fd, NVM_GET_DEVICES, &devs);
- if (ret)
- return ret;
-
- printf("Number of devices: %u\n", devs.nr_devices);
- printf("%-12s\t%-12s\tVersion\n", "Device", "Block manager");
-
- for (i = 0; i < devs.nr_devices && i < 31; i++) {
- struct nvm_ioctl_device_info *info = &devs.info[i];
-
- printf("%-12s\t%-12s\t(%u,%u,%u)\n", info->devname, info->bmname,
- info->bmversion[0], info->bmversion[1],
- info->bmversion[2]);
- }
-
- lnvm_close(fd);
-
- return 0;
-}
-
-int lnvm_do_info(void)
-{
- struct nvm_ioctl_info c;
- int fd, ret, i;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- memset(&c, 0, sizeof(struct nvm_ioctl_info));
- ret = ioctl(fd, NVM_INFO, &c);
- if (ret)
- return ret;
-
- printf("LightNVM (%u,%u,%u). %u target type(s) registered.\n",
- c.version[0], c.version[1], c.version[2], c.tgtsize);
- printf("Type\tVersion\n");
-
- for (i = 0; i < c.tgtsize; i++) {
- struct nvm_ioctl_info_tgt *tgt = &c.tgts[i];
-
- printf("%s\t(%u,%u,%u)\n",
- tgt->tgtname, tgt->version[0], tgt->version[1],
- tgt->version[2]);
- }
-
- lnvm_close(fd);
- return 0;
-}
-
-int lnvm_do_create_tgt(char *devname, char *tgtname, char *tgttype,
- int lun_begin, int lun_end,
- int over_prov, int flags)
-{
- struct nvm_ioctl_create c;
- int fd, ret;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- strncpy(c.dev, devname, DISK_NAME_LEN - 1);
- strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
- strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX - 1);
- c.flags = flags;
-
- /* Fall back into simple IOCTL version if no extended attributes used */
- if (over_prov != -1) {
- c.conf.type = NVM_CONFIG_TYPE_EXTENDED;
- c.conf.e.lun_begin = lun_begin;
- c.conf.e.lun_end = lun_end;
- c.conf.e.over_prov = over_prov;
- } else {
- c.conf.type = NVM_CONFIG_TYPE_SIMPLE;
- c.conf.s.lun_begin = lun_begin;
- c.conf.s.lun_end = lun_end;
- }
-
- ret = ioctl(fd, NVM_DEV_CREATE, &c);
- if (ret)
- fprintf(stderr, "Creation of target failed. Please see dmesg.\n");
-
- lnvm_close(fd);
- return ret;
-}
-
-int lnvm_do_remove_tgt(char *tgtname)
-{
- struct nvm_ioctl_remove c;
- int fd, ret;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
- c.flags = 0;
-
- ret = ioctl(fd, NVM_DEV_REMOVE, &c);
- if (ret)
- fprintf(stderr, "Remove of target failed. Please see dmesg.\n");
-
- lnvm_close(fd);
- return ret;
-}
-
-int lnvm_do_factory_init(char *devname, int erase_only_marked,
- int clear_host_marks,
- int clear_bb_marks)
-{
- struct nvm_ioctl_dev_factory fact;
- int fd, ret;
-
- fd = lnvm_open();
- if (fd < 0)
- return fd;
-
- memset(&fact, 0, sizeof(struct nvm_ioctl_dev_factory));
-
- strncpy(fact.dev, devname, DISK_NAME_LEN - 1);
- if (erase_only_marked)
- fact.flags |= NVM_FACTORY_ERASE_ONLY_USER;
- if (clear_host_marks)
- fact.flags |= NVM_FACTORY_RESET_HOST_BLKS;
- if (clear_bb_marks)
- fact.flags |= NVM_FACTORY_RESET_GRWN_BBLKS;
-
- ret = ioctl(fd, NVM_DEV_FACTORY, &fact);
- switch (errno) {
- case EINVAL:
- fprintf(stderr, "Factory reset failed.\n");
- break;
- case 0:
- break;
- default:
- fprintf(stderr, "Unknown error occurred (%d)\n", errno);
- break;
- }
-
- lnvm_close(fd);
- return ret;
-}
-
-static void show_lnvm_id_grp(void *t, int human)
-{
- struct nvme_nvm_id12_group *grp = t;
- uint32_t mpos = (uint32_t)le32_to_cpu(grp->mpos);
- uint32_t mccap = (uint32_t)le32_to_cpu(grp->mccap);
-
- printf(" mtype : %d\n", grp->mtype);
- if (human) {
- if (grp->mtype == LNVM_IDFY_GRP_MTYPE_NAND)
- printf(" NAND Flash Memory\n");
- else
- printf(" Reserved\n");
- }
- printf(" fmtype : %d\n", grp->fmtype);
- if (human) {
- if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_SLC)
- printf(" Single bit Level Cell flash (SLC)\n");
- else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_MLC)
- printf(" Two bit Level Cell flash (MLC)\n");
- else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_TLC)
- printf(" Three bit Level Cell flash (TLC)\n");
- else
- printf(" Reserved\n");
- }
- printf(" chnls : %d\n", grp->num_ch);
- printf(" luns : %d\n", grp->num_lun);
- printf(" plns : %d\n", grp->num_pln);
- printf(" blks : %d\n", (uint16_t)le16_to_cpu(grp->num_blk));
- printf(" pgs : %d\n", (uint16_t)le16_to_cpu(grp->num_pg));
- printf(" fpg_sz : %d\n", (uint16_t)le16_to_cpu(grp->fpg_sz));
- printf(" csecs : %d\n", (uint16_t)le16_to_cpu(grp->csecs));
- printf(" sos : %d\n", (uint16_t)le16_to_cpu(grp->sos));
- printf(" trdt : %d\n", (uint32_t)le32_to_cpu(grp->trdt));
- printf(" trdm : %d\n", (uint32_t)le32_to_cpu(grp->trdm));
- printf(" tprt : %d\n", (uint32_t)le32_to_cpu(grp->tprt));
- printf(" tprm : %d\n", (uint32_t)le32_to_cpu(grp->tprm));
- printf(" tbet : %d\n", (uint32_t)le32_to_cpu(grp->tbet));
- printf(" tbem : %d\n", (uint32_t)le32_to_cpu(grp->tbem));
- printf(" mpos : %#x\n", mpos);
- if (human) {
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_RD))
- printf(" [0]: Single plane read\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_RD))
- printf(" [1]: Dual plane read\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_RD))
- printf(" [2]: Quad plane read\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_PRG))
- printf(" [8]: Single plane program\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_PRG))
- printf(" [9]: Dual plane program\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_PRG))
- printf(" [10]: Quad plane program\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_ERS))
- printf(" [16]: Single plane erase\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_ERS))
- printf(" [17]: Dual plane erase\n");
- if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_ERS))
- printf(" [18]: Quad plane erase\n");
- }
- printf(" mccap : %#x\n", mccap);
- if (human) {
- if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SLC))
- printf(" [0]: SLC mode\n");
- if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_CMD_SUSP))
- printf(" [1]: Command suspension\n");
- if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SCRAMBLE))
- printf(" [2]: Scramble\n");
- if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_ENCRYPT))
- printf(" [3]: Encryption\n");
- }
- printf(" cpar : %#x\n", (uint16_t)le16_to_cpu(grp->cpar));
-
-}
-
-static void show_lnvm_ppaf(struct nvme_nvm_addr_format *ppaf)
-{
- printf("ppaf :\n");
- printf(" ch offs : %d ch bits : %d\n",
- ppaf->ch_offset, ppaf->ch_len);
- printf(" lun offs: %d lun bits : %d\n",
- ppaf->lun_offset, ppaf->lun_len);
- printf(" pl offs : %d pl bits : %d\n",
- ppaf->pln_offset, ppaf->pln_len);
- printf(" blk offs: %d blk bits : %d\n",
- ppaf->blk_offset, ppaf->blk_len);
- printf(" pg offs : %d pg bits : %d\n",
- ppaf->pg_offset, ppaf->pg_len);
- printf(" sec offs: %d sec bits : %d\n",
- ppaf->sect_offset, ppaf->sect_len);
-}
-
-static void show_lnvm_id12_ns(void *t, unsigned int flags)
-{
- int i;
- int human = flags & VERBOSE;
- struct nvme_nvm_id12 *id = t;
-
- uint32_t cap = (uint32_t) le32_to_cpu(id->cap);
- uint32_t dom = (uint32_t) le32_to_cpu(id->dom);
- uint32_t cgrps = id->cgrps;
-
- if (id->cgrps > 4) {
- fprintf(stderr, "invalid identify geometry returned\n");
- return;
- }
-
- printf("verid : %#x\n", id->ver_id);
- printf("vmnt : %#x\n", id->vmnt);
- if (human) {
- if (!id->vmnt)
- printf(" Generic/Enable opcodes as found in this spec.");
- else
- printf(" Reserved/Reserved for future opcode configurations");
- }
- printf("\n");
- printf("cgrps : %d\n", id->cgrps);
- printf("cap : %#x\n", cap);
- if (human) {
- if (cap & (1 << LNVM_IDFY_CAP_BAD_BLK_TBL_MGMT))
- printf(" [0]: Bad block table management\n");
- if (cap & (1 << LNVM_IDFY_CAP_HYBRID_CMD_SUPP))
- printf(" [1]: Hybrid command support\n");
- }
- printf("dom : %#x\n", dom);
- if (human) {
- if (dom & (1 << LNVM_IDFY_DOM_HYBRID_MODE))
- printf(" [0]: Hybrid mode (L2P MAP is in device)\n");
- if (dom & (1 << LNVM_IDFY_DOM_ECC_MODE))
- printf(" [1]: Error Code Correction(ECC) mode\n");
- }
- show_lnvm_ppaf(&id->ppaf);
-
- for (i = 0; i < cgrps; i++) {
- printf("grp : %d\n", i);
- show_lnvm_id_grp((void *)&id->groups[i], human);
- }
-}
-
-static void show_lnvm_id20_ns(struct nvme_nvm_id20 *id, unsigned int flags)
-{
- int human = flags & VERBOSE;
- uint32_t mccap = (uint32_t) le32_to_cpu(id->mccap);
-
- printf("ver_major : %#x\n", id->mjr);
- printf("ver_minor : %#x\n", id->mnr);
-
- printf("mccap : %#x\n", mccap);
- if (human) {
- if (mccap & (1 << LNVM_IDFY_CAP_VCOPY))
- printf(" [0]: Vector copy support\n");
- if (mccap & (1 << LNVM_IDFY_CAP_MRESETS))
- printf(" [1]: Multiple resets support\n");
- }
- printf("wit : %d\n", id->wit);
-
- printf("lba format\n");
- printf(" grp len : %d\n", id->lbaf.grp_len);
- printf(" pu len : %d\n", id->lbaf.pu_len);
- printf(" chk len : %d\n", id->lbaf.chk_len);
- printf(" clba len : %d\n", id->lbaf.lba_len);
-
- printf("geometry\n");
- printf(" num_grp : %d\n", le16_to_cpu(id->num_grp));
- printf(" num_pu : %d\n", le16_to_cpu(id->num_pu));
- printf(" num_chk : %d\n", le32_to_cpu(id->num_chk));
- printf(" clba : %d\n", le32_to_cpu(id->clba));
- printf("write req\n");
- printf(" ws_min : %d\n", le32_to_cpu(id->ws_min));
- printf(" ws_opt : %d\n", le32_to_cpu(id->ws_opt));
- printf(" mw_cunits : %d\n", le32_to_cpu(id->mw_cunits));
- printf(" maxoc : %d\n", le32_to_cpu(id->maxoc));
- printf(" maxocpu : %d\n", le32_to_cpu(id->maxocpu));
- printf("perf metrics\n");
- printf(" trdt (ns) : %d\n", le32_to_cpu(id->trdt));
- printf(" trdm (ns) : %d\n", le32_to_cpu(id->trdm));
- printf(" twrt (ns) : %d\n", le32_to_cpu(id->twrt));
- printf(" twrm (ns) : %d\n", le32_to_cpu(id->twrm));
- printf(" tcrst (ns) : %d\n", le32_to_cpu(id->tcrst));
- printf(" tcrsm (ns) : %d\n", le32_to_cpu(id->tcrsm));
-}
-
-static void show_lnvm_id_ns(struct nvme_nvm_id *id, unsigned int flags)
-{
- switch (id->ver_id) {
- case 1:
- show_lnvm_id12_ns((void *) id, flags);
- break;
- case 2:
- show_lnvm_id20_ns((void *) id, flags);
- break;
- default:
- fprintf(stderr, "Version %d not supported.\n",
- id->ver_id);
- }
-}
-
-int lnvm_get_identity(int fd, int nsid, struct nvme_nvm_id *nvm_id)
-{
- struct nvme_passthru_cmd cmd = {
- .opcode = nvme_nvm_admin_identity,
- .nsid = nsid,
- .addr = (__u64)(uintptr_t)nvm_id,
- .data_len = sizeof(struct nvme_nvm_id),
- };
-
- return nvme_submit_admin_passthru(fd, &cmd, NULL);
-}
-
-int lnvm_do_id_ns(int fd, int nsid, unsigned int flags)
-{
- struct nvme_nvm_id nvm_id;
- int err;
-
- err = lnvm_get_identity(fd, nsid, &nvm_id);
- if (!err) {
- if (flags & BINARY)
- d_raw((unsigned char *)&nvm_id, sizeof(nvm_id));
- else
- show_lnvm_id_ns(&nvm_id, flags);
- } else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
- nvme_status_to_string(err), err, nsid);
- return err;
-}
-
-static inline const char *print_chunk_state(__u8 cs)
-{
- switch (cs) {
- case 1 << 0: return "FREE";
- case 1 << 1: return "CLOSED";
- case 1 << 2: return "OPEN";
- case 1 << 3: return "OFFLINE";
- default: return "UNKNOWN";
- }
-}
-
-static inline const char *print_chunk_type(__u8 ct)
-{
- switch (ct & 0xF) {
- case 1 << 0: return "SEQWRITE_REQ";
- case 1 << 1: return "RANDWRITE_ALLOWED";
- default: return "UNKNOWN";
- }
-}
-
-static inline const char *print_chunk_attr(__u8 ct)
-{
- switch (ct & 0xF0) {
- case 1 << 4: return "DEVIATED";
- default: return "NONE";
- }
-}
-
-static void show_lnvm_chunk_log(struct nvme_nvm_chunk_desc *chunk_log,
- __u32 data_len)
-{
- int nr_entry = data_len / sizeof(struct nvme_nvm_chunk_desc);
- int idx;
-
- printf("Total chunks in namespace: %d\n", nr_entry);
- for (idx = 0; idx < nr_entry; idx++) {
- struct nvme_nvm_chunk_desc *desc = &chunk_log[idx];
-
- printf(" [%5d] { ", idx);
- printf("SLBA: 0x%016"PRIx64, le64_to_cpu(desc->slba));
- printf(", WP: 0x%016"PRIx64, le64_to_cpu(desc->wp));
- printf(", CNLB: 0x%016"PRIx64, le64_to_cpu(desc->cnlb));
- printf(", State: %-8s", print_chunk_state(desc->cs));
- printf(", Type: %-20s", print_chunk_type(desc->ct));
- printf(", Attr: %-8s", print_chunk_attr(desc->ct));
- printf(", WLI: %4d }\n", desc->wli);
- }
-}
-
-int lnvm_do_chunk_log(int fd, __u32 nsid, __u32 data_len, void *data,
- unsigned int flags)
-{
- int err;
-
- err = nvme_get_log(fd, NVM_LID_CHUNK_INFO, nsid, 0, 0, 0,
- false, 0, data_len, data);
- if (err > 0) {
- fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
- nvme_status_to_string(err), err, nsid);
-
- goto out;
- } else if (err < 0) {
- err = -errno;
- perror("nvme_get_log");
-
- goto out;
- }
-
- if (flags & BINARY)
- d_raw(data, data_len);
- else
- show_lnvm_chunk_log(data, data_len);
-
-out:
- return err;
-}
-
-static void show_lnvm_bbtbl(struct nvme_nvm_bb_tbl *tbl)
-{
- printf("verid : %#x\n", (uint16_t)le16_to_cpu(tbl->verid));
- printf("tblks : %d\n", (uint32_t)le32_to_cpu(tbl->tblks));
- printf("tfact : %d\n", (uint32_t)le32_to_cpu(tbl->tfact));
- printf("tgrown : %d\n", (uint32_t)le32_to_cpu(tbl->tgrown));
- printf("tdresv : %d\n", (uint32_t)le32_to_cpu(tbl->tdresv));
- printf("thresv : %d\n", (uint32_t)le32_to_cpu(tbl->thresv));
- printf("Use raw output to retrieve table.\n");
-}
-
-static int __lnvm_do_get_bbtbl(int fd, struct nvme_nvm_id12 *id,
- struct ppa_addr ppa,
- unsigned int flags)
-{
- struct nvme_nvm_id12_group *grp = &id->groups[0];
- int bbtblsz = ((uint16_t)le16_to_cpu(grp->num_blk) * grp->num_pln);
- int bufsz = bbtblsz + sizeof(struct nvme_nvm_bb_tbl);
- struct nvme_nvm_bb_tbl *bbtbl;
- int err;
-
- bbtbl = calloc(1, bufsz);
- if (!bbtbl)
- return -ENOMEM;
-
- struct nvme_nvm_getbbtbl cmd = {
- .opcode = nvme_nvm_admin_get_bb_tbl,
- .nsid = cpu_to_le32(1),
- .addr = (__u64)(uintptr_t)bbtbl,
- .data_len = bufsz,
- .ppa = cpu_to_le64(ppa.ppa),
- };
- void *tmp = &cmd;
- struct nvme_passthru_cmd *nvme_cmd = tmp;
-
- err = nvme_submit_admin_passthru(fd, nvme_cmd, NULL);
- if (err > 0) {
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- free(bbtbl);
- return err;
- }
-
- if (flags & BINARY)
- d_raw((unsigned char *)&bbtbl->blk, bbtblsz);
- else {
- printf("LightNVM Bad Block Stats:\n");
- show_lnvm_bbtbl(bbtbl);
- }
-
- free(bbtbl);
- return 0;
-}
-
-int lnvm_do_get_bbtbl(int fd, int nsid, int lunid, int chid, unsigned int flags)
-{
- struct nvme_nvm_id12 nvm_id;
- struct ppa_addr ppa;
- int err;
- void *tmp = &nvm_id;
-
- err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
- if (err) {
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- return err;
- }
-
- if (nvm_id.ver_id != 1) {
- fprintf(stderr, "Get bad block table not supported on version %d\n",
- nvm_id.ver_id);
- return -EINVAL;
- }
-
- if (chid >= nvm_id.groups[0].num_ch ||
- lunid >= nvm_id.groups[0].num_lun) {
- fprintf(stderr, "Out of bound channel id or LUN id\n");
- return -EINVAL;
- }
-
- ppa.ppa = 0;
- ppa.g.lun = lunid;
- ppa.g.ch = chid;
-
- ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
-
- return __lnvm_do_get_bbtbl(fd, &nvm_id, ppa, flags);
-}
-
-static int __lnvm_do_set_bbtbl(int fd, struct ppa_addr ppa, __u8 value)
-{
- int err;
-
- struct nvme_nvm_setbbtbl cmd = {
- .opcode = nvme_nvm_admin_set_bb_tbl,
- .nsid = cpu_to_le32(1),
- .ppa = cpu_to_le64(ppa.ppa),
- .nlb = cpu_to_le16(0),
- .value = value,
- };
- void *tmp = &cmd;
- struct nvme_passthru_cmd *nvme_cmd = tmp;
-
- err = nvme_submit_admin_passthru(fd, nvme_cmd, NULL);
- if (err > 0) {
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- return err;
- }
- return 0;
-}
-
-int lnvm_do_set_bbtbl(int fd, int nsid,
- int chid, int lunid, int plnid, int blkid,
- __u8 value)
-{
- struct nvme_nvm_id12 nvm_id;
- struct ppa_addr ppa;
- int err;
- void *tmp = &nvm_id;
-
- err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
- if (err) {
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- return err;
- }
-
- if (nvm_id.ver_id != 1) {
- fprintf(stderr, "Set bad block table not supported on version %d\n",
- nvm_id.ver_id);
- return -EINVAL;
- }
-
- if (chid >= nvm_id.groups[0].num_ch ||
- lunid >= nvm_id.groups[0].num_lun ||
- plnid >= nvm_id.groups[0].num_pln ||
- blkid >= le16_to_cpu(nvm_id.groups[0].num_blk)) {
- fprintf(stderr, "Out of bound channel id, LUN id, plane id, or"\
- "block id\n");
- return -EINVAL;
- }
-
- ppa.ppa = 0;
- ppa.g.lun = lunid;
- ppa.g.ch = chid;
- ppa.g.pl = plnid;
- ppa.g.blk = blkid;
-
- ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
-
- return __lnvm_do_set_bbtbl(fd, ppa, value);
-}
#include <uuid.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
};
static const char *output_format = "Output format: normal|json|binary";
-//static const char *output_format_no_binary = "Output format: normal|json";
/* Name of file to output log pages in their raw format */
static char *raw;
OPT_STRING("transport", 't', "STR", (char *)&c.transport, nvmf_tport), \
OPT_STRING("traddr", 'a', "STR", (char *)&c.traddr, nvmf_traddr), \
OPT_STRING("trsvcid", 's', "STR", (char *)&c.trsvcid, nvmf_trsvcid), \
- OPT_STRING("host-traddr", 'w', "STR", (char *)&c.host_traddr, nvmf_htraddr), \
+ OPT_STRING("host-traddr", 'w', "STR", (char *)&c.host_traddr,nvmf_htraddr), \
OPT_STRING("hostnqn", 'q', "STR", (char *)&c.hostnqn, nvmf_hostnqn), \
OPT_STRING("hostid", 'I', "STR", (char *)&c.hostid, nvmf_hostid), \
OPT_INT("nr-io-queues", 'i', &c.nr_io_queues, nvmf_nr_io_queues), \
perror(prefix);
else
fprintf(stderr, "%s: nvme status: %s(%#x)\n", prefix,
- nvme_status_to_string(status), status);
+ nvme_status_to_string(status, false), status);
}
+static void nvme_print_object(struct json_object *j)
+{
+ const unsigned long jflags =
+ JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY;
+
+ if (j) {
+ nvme_json_object_print(stdout, j, jflags);
+ json_object_put(j);
+ }
+}
+
+void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_props_to_json(bar, flags));
+}
+
+void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_ns_to_json(ns, flags));
+}
+
+void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_ns_desc_list_to_json(data, flags));
+}
+
+void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_ctrl_to_json(ctrl, flags));
+}
+
+void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_nvm_set_list_to_json(nvmset, flags));
+}
+
+void nvme_show_list_secondary_ctrl(
+ struct nvme_secondary_ctrl_list *sc_list,
+ __u32 count, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_secondary_ctrl_list_to_json(sc_list, flags));
+}
+
+void nvme_show_id_ns_granularity_list(struct nvme_id_ns_granularity_list *glist,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_ns_granularity_list_to_json(glist, flags));
+}
+
+void nvme_show_id_uuid_list(struct nvme_id_uuid_list *uuid_list,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_id_uuid_list_to_json(uuid_list, flags));
+}
+
+void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
+ const char *devname, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_error_log_to_json(err_log, entries, flags));
+}
+
+void nvme_show_resv_report(struct nvme_reservation_status *status, int bytes,
+ __u32 cdw11, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_resv_report_to_json(status, cdw11 & 1, flags));
+}
+
+void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
+ const char *devname, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_fw_slot_log_to_json(fw_log, flags));
+}
+
+void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
+ const char *devname,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_ns_list_to_json(log, flags));
+}
+
+void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
+ unsigned int flags)
+{
+ nvme_print_object(nvme_cmd_effects_log_to_json(effects, flags));
+}
+
+void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
+ __u16 group_id, const char *devname,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_endurance_group_log_to_json(endurance_log, flags));
+}
+
+void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
+ const char *devname, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_smart_log_to_json(smart, flags));
+}
+
+void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
+ enum nvme_print_flags flags, size_t len)
+{
+ nvme_print_object(nvme_ana_log_to_json(ana_log, flags));
+}
+
+void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
+ enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_dev_self_test_log_to_json(self_test, flags));
+}
+
+void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
+ const char *devname, enum nvme_print_flags flags)
+{
+ nvme_print_object(nvme_sanitize_log_to_json(sanitize, flags));
+}
+
+void nvme_directive_show(__u8 dtype, __u8 doper, __u16 spec, __u32 nsid, __u32 result,
+ void *buf, __u32 len, enum nvme_print_flags flags)
+{
+ struct json_object *j = NULL;
+
+ switch (dtype) {
+ case NVME_DIRECTIVE_DTYPE_IDENTIFY:
+ switch (doper) {
+ case NVME_DIRECTIVE_RECEIVE_IDENTIFY_DOPER_PARAM:
+ j = nvme_identify_directives_to_json(buf, flags);
+ break;
+ default:
+ break;
+ }
+ break;
+ case NVME_DIRECTIVE_DTYPE_STREAMS:
+ switch (doper) {
+ case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_PARAM:
+ j = nvme_streams_params_to_json(buf, flags);
+ break;
+ case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_STATUS:
+ j = nvme_streams_status_to_json(buf, flags);
+ break;
+ case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_RESOURCE:
+ j = nvme_streams_allocated_to_json(result, flags);
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (j)
+ nvme_print_object(j);
+ else
+ fprintf(stderr, "Unrecognized dtype:%d doper:%d\n", dtype, doper);
+}
+
+void nvme_feature_show_fields(__u32 fid, unsigned int result, void **buf,
+ unsigned long flags)
+{
+ nvme_print_object(nvme_feature_to_json(fid, result, 0, buf, flags));
+}
+
+void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
+ enum nvme_print_flags flags)
+{
+ nvme_lba_status_desc_list_to_json(list, flags);
+}
static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_smart_log log;
err = nvme_get_features(fd, cfg.feature_id, cfg.namespace_id, cfg.sel, cfg.cdw11,
0, cfg.data_len, buf, &result);
if (!err)
- nvme_feature_show_fields(cfg.feature_id, result, buf);
+ ;//nvme_feature_show_fields(cfg.feature_id, result, buf);
else
nvme_show_status("get-feature", err);
fprintf(stderr, "You are about to format %s, namespace %#x%s.\n",
devicename, cfg.namespace_id,
cfg.namespace_id == NVME_NSID_ALL ? "(ALL namespaces)" : "");
- nvme_show_relatives(devicename);
+ //nvme_show_relatives(devicename);
fprintf(stderr, "WARNING: Format may irrevocably delete this device's data.\n"
"You have 10 seconds to press Ctrl-C to cancel this operation.\n\n"
"Use the force [--force|-f] option to suppress this warning.\n");
err = nvme_set_features(fd, cfg.feature_id, cfg.namespace_id, cfg.value,
cfg.cdw12, cfg.save, 0, 0, cfg.data_len, buf, &result);
if (err)
- nvme_feature_show_fields(cfg.feature_id, result, buf);
+ ;//nvme_feature_show_fields(cfg.feature_id, result, buf);
else
nvme_show_status("set-feature", err);
#include <endian.h>
#include "plugin.h"
-#include "util/json.h"
#include "util/argconfig.h"
+#include "util/user-types.h"
#include <libnvme.h>
-enum nvme_print_flags {
- NORMAL = 0,
- VERBOSE = 1 << 0, /* verbosely decode complex values for humans */
- JSON = 1 << 1, /* display in json format */
- VS = 1 << 2, /* hex dump vendor specific data areas */
- BINARY = 1 << 3, /* binary dump raw bytes */
-};
-
-#define SYS_NVME "/sys/class/nvme"
+#define JSON 0
+#define NORMAL NVME_JSON_TABULAR
+#define BINARY NVME_JSON_BINARY
+#define VERBOSE (NVME_JSON_DECODE_COMPLEX|NVME_JSON_HUMAN)
void register_extension(struct plugin *plugin);
int parse_and_open(int argc, char **argv, const char *desc,
int validate_output_format(char *format);
int __id_ctrl(int argc, char **argv, struct command *cmd,
struct plugin *plugin, void (*vs)(__u8 *vs, struct json_object *root));
-char *nvme_char_from_block(char *block);
-void *mmap_registers(const char *dev);
-
-extern int current_index;
-int scan_namespace_filter(const struct dirent *d);
-int scan_ctrl_paths_filter(const struct dirent *d);
-int scan_ctrls_filter(const struct dirent *d);
-int scan_subsys_filter(const struct dirent *d);
-int scan_dev_filter(const struct dirent *d);
#endif /* _NVME_H */
#include <sys/time.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
exit:
if (err > 0)
- fprintf(stderr, "\nNVMe status:%s(0x%x)\n", nvme_status_to_string(err), err);
+ fprintf(stderr, "\nNVMe status:%s(0x%x)\n", nvme_status_to_string(err, false), err);
return err;
}
#include <sys/stat.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "json.h"
#include "common.h"
#include "nvme.h"
-#include "print.h"
#include "json.h"
#include "plugin.h"
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
d_raw((unsigned char *)&log, sizeof(log));
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
d_raw((unsigned char *)&stats, sizeof(stats));
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
goto close_fd;
if (cfg.raw_binary)
- flags = BINARY;
+ flags = NVME_JSON_BINARY;
err = nvme_get_log(fd, cfg.write ? 0xc2 : 0xc1, NVME_NSID_ALL, 0, 0, 0,
false, 0, sizeof(stats), &stats);
if (!err) {
- if (flags & JSON)
- json_lat_stats(&stats, cfg.write);
- else if (flags & BINARY)
+ if (flags & NVME_JSON_HUMAN)
+ show_lat_stats(&stats, cfg.write);
+ else if (flags & NVME_JSON_BINARY)
d_raw((unsigned char *)&stats, sizeof(stats));
else
- show_lat_stats(&stats, cfg.write);
+ json_lat_stats(&stats, cfg.write);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
close_fd:
close(fd);
out:
if (err > 0) {
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
} else if (err < 0) {
perror("intel log");
err = EIO;
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
+#include <inttypes.h>
+#include <fcntl.h>
#include <unistd.h>
+#include <string.h>
+
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <linux/lightnvm.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
-#include "lightnvm.h"
-
#include "argconfig.h"
#include "suffix.h"
#define CREATE_CMD
#include "lnvm-nvme.h"
+enum nvme_nvm_admin_opcode {
+ nvme_nvm_admin_identity = 0xe2,
+ nvme_nvm_admin_get_bb_tbl = 0xf2,
+ nvme_nvm_admin_set_bb_tbl = 0xf1,
+};
+
+struct nvme_nvm_identity {
+ __u8 opcode;
+ __u8 flags;
+ __le16 command_id;
+ __le32 nsid;
+ __le64 rsvd[2];
+ __le64 prp1;
+ __le64 prp2;
+ __le32 chnl_off;
+ __le32 rsvd11[5];
+};
+
+struct nvme_nvm_setbbtbl {
+ __u8 opcode;
+ __u8 flags;
+ __le16 rsvd1;
+ __le32 nsid;
+ __le32 cdw2;
+ __le32 cdw3;
+ __le64 metadata;
+ __u64 addr;
+ __le32 metadata_len;
+ __le32 data_len;
+ __le64 ppa;
+ __le16 nlb;
+ __u8 value;
+ __u8 rsvd2;
+ __le32 cdw14;
+ __le32 cdw15;
+ __le32 timeout_ms;
+ __le32 result;
+};
+
+struct nvme_nvm_getbbtbl {
+ __u8 opcode;
+ __u8 flags;
+ __le16 rsvd1;
+ __le32 nsid;
+ __le32 cdw2;
+ __le32 cdw3;
+ __u64 metadata;
+ __u64 addr;
+ __u32 metadata_len;
+ __u32 data_len;
+ __le64 ppa;
+ __le32 cdw12;
+ __le32 cdw13;
+ __le32 cdw14;
+ __le32 cdw15;
+ __le32 timeout_ms;
+ __le32 result;
+};
+
+struct nvme_nvm_command {
+ union {
+ struct nvme_nvm_identity identity;
+ struct nvme_nvm_getbbtbl get_bb;
+ };
+};
+
+struct nvme_nvm_completion {
+ __le64 result; /* Used by LightNVM to return ppa completions */
+ __le16 sq_head; /* how much of this queue may be reclaimed */
+ __le16 sq_id; /* submission queue that generated this entry */
+ __le16 command_id; /* of the command which completed */
+ __le16 status; /* did the command fail, and if so, why? */
+};
+
+#define NVME_NVM_LP_MLC_PAIRS 886
+struct nvme_nvm_lp_mlc {
+ __le16 num_pairs;
+ __u8 pairs[NVME_NVM_LP_MLC_PAIRS];
+};
+
+struct nvme_nvm_lp_tbl {
+ __u8 id[8];
+ struct nvme_nvm_lp_mlc mlc;
+};
+
+struct nvme_nvm_id12_group {
+ __u8 mtype;
+ __u8 fmtype;
+ __le16 res16;
+ __u8 num_ch;
+ __u8 num_lun;
+ __u8 num_pln;
+ __u8 rsvd1;
+ __le16 num_blk;
+ __le16 num_pg;
+ __le16 fpg_sz;
+ __le16 csecs;
+ __le16 sos;
+ __le16 rsvd2;
+ __le32 trdt;
+ __le32 trdm;
+ __le32 tprt;
+ __le32 tprm;
+ __le32 tbet;
+ __le32 tbem;
+ __le32 mpos;
+ __le32 mccap;
+ __le16 cpar;
+ __u8 reserved[10];
+ struct nvme_nvm_lp_tbl lptbl;
+} __attribute__((packed));
+
+struct nvme_nvm_addr_format {
+ __u8 ch_offset;
+ __u8 ch_len;
+ __u8 lun_offset;
+ __u8 lun_len;
+ __u8 pln_offset;
+ __u8 pln_len;
+ __u8 blk_offset;
+ __u8 blk_len;
+ __u8 pg_offset;
+ __u8 pg_len;
+ __u8 sect_offset;
+ __u8 sect_len;
+ __u8 res[4];
+} __attribute__((packed));
+
+enum {
+ LNVM_IDFY_CAP_BAD_BLK_TBL_MGMT = 0,
+ LNVM_IDFY_CAP_HYBRID_CMD_SUPP = 1,
+ LNVM_IDFY_CAP_VCOPY = 0,
+ LNVM_IDFY_CAP_MRESETS = 1,
+ LNVM_IDFY_DOM_HYBRID_MODE = 0,
+ LNVM_IDFY_DOM_ECC_MODE = 1,
+ LNVM_IDFY_GRP_MTYPE_NAND = 0,
+ LNVM_IDFY_GRP_FMTYPE_SLC = 0,
+ LNVM_IDFY_GRP_FMTYPE_MLC = 1,
+ LNVM_IDFY_GRP_FMTYPE_TLC = 2,
+ LNVM_IDFY_GRP_MPOS_SNGL_PLN_RD = 0,
+ LNVM_IDFY_GRP_MPOS_DUAL_PLN_RD = 1,
+ LNVM_IDFY_GRP_MPOS_QUAD_PLN_RD = 2,
+ LNVM_IDFY_GRP_MPOS_SNGL_PLN_PRG = 8,
+ LNVM_IDFY_GRP_MPOS_DUAL_PLN_PRG = 9,
+ LNVM_IDFY_GRP_MPOS_QUAD_PLN_PRG = 10,
+ LNVM_IDFY_GRP_MPOS_SNGL_PLN_ERS = 16,
+ LNVM_IDFY_GRP_MPOS_DUAL_PLN_ERS = 17,
+ LNVM_IDFY_GRP_MPOS_QUAD_PLN_ERS = 18,
+ LNVM_IDFY_GRP_MCCAP_SLC = 0,
+ LNVM_IDFY_GRP_MCCAP_CMD_SUSP = 1,
+ LNVM_IDFY_GRP_MCCAP_SCRAMBLE = 2,
+ LNVM_IDFY_GRP_MCCAP_ENCRYPT = 3,
+};
+
+struct nvme_nvm_id12 {
+ __u8 ver_id;
+ __u8 vmnt;
+ __u8 cgrps;
+ __u8 res;
+ __le32 cap;
+ __le32 dom;
+ struct nvme_nvm_addr_format ppaf;
+ __u8 resv[228];
+ struct nvme_nvm_id12_group groups[4];
+} __attribute__((packed));
+
+struct nvme_nvm_id20_addrf {
+ __u8 grp_len;
+ __u8 pu_len;
+ __u8 chk_len;
+ __u8 lba_len;
+ __u8 resv[4];
+} __attribute__((packed));
+
+struct nvme_nvm_id20 {
+ __u8 mjr;
+ __u8 mnr;
+ __u8 resv[6];
+
+ struct nvme_nvm_id20_addrf lbaf;
+
+ __le32 mccap;
+ __u8 resv2[12];
+
+ __u8 wit;
+ __u8 resv3[31];
+
+ /* Geometry */
+ __le16 num_grp;
+ __le16 num_pu;
+ __le32 num_chk;
+ __le32 clba;
+ __u8 resv4[52];
+
+ /* Write data requirements */
+ __le32 ws_min;
+ __le32 ws_opt;
+ __le32 mw_cunits;
+ __le32 maxoc;
+ __le32 maxocpu;
+ __u8 resv5[44];
+
+ /* Performance related metrics */
+ __le32 trdt;
+ __le32 trdm;
+ __le32 twrt;
+ __le32 twrm;
+ __le32 tcrst;
+ __le32 tcrsm;
+ __u8 resv6[40];
+
+ /* Reserved area */
+ __u8 resv7[2816];
+
+ /* Vendor specific */
+ __u8 vs[1024];
+} __attribute__((packed));
+
+struct nvme_nvm_id {
+ __u8 ver_id;
+ __u8 resv[4095];
+} __attribute__((packed));
+
+enum {
+ NVM_LID_CHUNK_INFO = 0xCA,
+};
+
+struct nvme_nvm_chunk_desc {
+ __u8 cs;
+ __u8 ct;
+ __u8 wli;
+ __u8 rsvd_7_3[5];
+ __u64 slba;
+ __u64 cnlb;
+ __u64 wp;
+};
+
+struct nvme_nvm_bb_tbl {
+ __u8 tblid[4];
+ __le16 verid;
+ __le16 revid;
+ __le32 rvsd1;
+ __le32 tblks;
+ __le32 tfact;
+ __le32 tgrown;
+ __le32 tdresv;
+ __le32 thresv;
+ __le32 rsvd2[8];
+ __u8 blk[0];
+};
+
+#define NVM_BLK_BITS (16)
+#define NVM_PG_BITS (16)
+#define NVM_SEC_BITS (8)
+#define NVM_PL_BITS (8)
+#define NVM_LUN_BITS (8)
+#define NVM_CH_BITS (7)
+
+struct ppa_addr {
+ /* Generic structure for all addresses */
+ union {
+ struct {
+ __u64 blk : NVM_BLK_BITS;
+ __u64 pg : NVM_PG_BITS;
+ __u64 sec : NVM_SEC_BITS;
+ __u64 pl : NVM_PL_BITS;
+ __u64 lun : NVM_LUN_BITS;
+ __u64 ch : NVM_CH_BITS;
+ __u64 reserved : 1;
+ } g;
+
+ __u64 ppa;
+ };
+};
+
+static inline struct ppa_addr generic_to_dev_addr(
+ struct nvme_nvm_addr_format *ppaf, struct ppa_addr r)
+{
+ struct ppa_addr l;
+
+ l.ppa = ((__u64)r.g.blk) << ppaf->blk_offset;
+ l.ppa |= ((__u64)r.g.pg) << ppaf->pg_offset;
+ l.ppa |= ((__u64)r.g.sec) << ppaf->sect_offset;
+ l.ppa |= ((__u64)r.g.pl) << ppaf->pln_offset;
+ l.ppa |= ((__u64)r.g.lun) << ppaf->lun_offset;
+ l.ppa |= ((__u64)r.g.ch) << ppaf->ch_offset;
+
+ return l;
+}
+static int lnvm_open(void)
+{
+ char dev[FILENAME_MAX] = NVM_CTRL_FILE;
+ int fd;
+
+ fd = open(dev, O_WRONLY);
+ if (fd < 0) {
+ printf("Failed to open LightNVM mgmt interface\n");
+ perror(dev);
+ return fd;
+ }
+
+ return fd;
+}
+
+static void lnvm_close(int fd)
+{
+ close(fd);
+}
+
+int lnvm_do_init(char *dev, char *mmtype)
+{
+ struct nvm_ioctl_dev_init init;
+ int fd, ret;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ memset(&init, 0, sizeof(struct nvm_ioctl_dev_init));
+ strncpy(init.dev, dev, DISK_NAME_LEN - 1);
+ strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN - 1);
+
+ ret = ioctl(fd, NVM_DEV_INIT, &init);
+ switch (errno) {
+ case EINVAL:
+ printf("Initialization failed.\n");
+ break;
+ case EEXIST:
+ printf("Device has already been initialized.\n");
+ break;
+ case 0:
+ break;
+ default:
+ printf("Unknown error occurred (%d)\n", errno);
+ break;
+ }
+
+ lnvm_close(fd);
+
+ return ret;
+}
+
+int lnvm_do_list_devices(void)
+{
+ struct nvm_ioctl_get_devices devs;
+ int fd, ret, i;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ ret = ioctl(fd, NVM_GET_DEVICES, &devs);
+ if (ret)
+ return ret;
+
+ printf("Number of devices: %u\n", devs.nr_devices);
+ printf("%-12s\t%-12s\tVersion\n", "Device", "Block manager");
+
+ for (i = 0; i < devs.nr_devices && i < 31; i++) {
+ struct nvm_ioctl_device_info *info = &devs.info[i];
+
+ printf("%-12s\t%-12s\t(%u,%u,%u)\n", info->devname, info->bmname,
+ info->bmversion[0], info->bmversion[1],
+ info->bmversion[2]);
+ }
+
+ lnvm_close(fd);
+
+ return 0;
+}
+
+int lnvm_do_info(void)
+{
+ struct nvm_ioctl_info c;
+ int fd, ret, i;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ memset(&c, 0, sizeof(struct nvm_ioctl_info));
+ ret = ioctl(fd, NVM_INFO, &c);
+ if (ret)
+ return ret;
+
+ printf("LightNVM (%u,%u,%u). %u target type(s) registered.\n",
+ c.version[0], c.version[1], c.version[2], c.tgtsize);
+ printf("Type\tVersion\n");
+
+ for (i = 0; i < c.tgtsize; i++) {
+ struct nvm_ioctl_info_tgt *tgt = &c.tgts[i];
+
+ printf("%s\t(%u,%u,%u)\n",
+ tgt->tgtname, tgt->version[0], tgt->version[1],
+ tgt->version[2]);
+ }
+
+ lnvm_close(fd);
+ return 0;
+}
+
+int lnvm_do_create_tgt(char *devname, char *tgtname, char *tgttype,
+ int lun_begin, int lun_end,
+ int over_prov, int flags)
+{
+ struct nvm_ioctl_create c;
+ int fd, ret;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ strncpy(c.dev, devname, DISK_NAME_LEN - 1);
+ strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
+ strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX - 1);
+ c.flags = flags;
+
+ /* Fall back into simple IOCTL version if no extended attributes used */
+ if (over_prov != -1) {
+ c.conf.type = NVM_CONFIG_TYPE_EXTENDED;
+ c.conf.e.lun_begin = lun_begin;
+ c.conf.e.lun_end = lun_end;
+ c.conf.e.over_prov = over_prov;
+ } else {
+ c.conf.type = NVM_CONFIG_TYPE_SIMPLE;
+ c.conf.s.lun_begin = lun_begin;
+ c.conf.s.lun_end = lun_end;
+ }
+
+ ret = ioctl(fd, NVM_DEV_CREATE, &c);
+ if (ret)
+ fprintf(stderr, "Creation of target failed. Please see dmesg.\n");
+
+ lnvm_close(fd);
+ return ret;
+}
+
+int lnvm_do_remove_tgt(char *tgtname)
+{
+ struct nvm_ioctl_remove c;
+ int fd, ret;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1);
+ c.flags = 0;
+
+ ret = ioctl(fd, NVM_DEV_REMOVE, &c);
+ if (ret)
+ fprintf(stderr, "Remove of target failed. Please see dmesg.\n");
+
+ lnvm_close(fd);
+ return ret;
+}
+
+int lnvm_do_factory_init(char *devname, int erase_only_marked,
+ int clear_host_marks,
+ int clear_bb_marks)
+{
+ struct nvm_ioctl_dev_factory fact;
+ int fd, ret;
+
+ fd = lnvm_open();
+ if (fd < 0)
+ return fd;
+
+ memset(&fact, 0, sizeof(struct nvm_ioctl_dev_factory));
+
+ strncpy(fact.dev, devname, DISK_NAME_LEN - 1);
+ if (erase_only_marked)
+ fact.flags |= NVM_FACTORY_ERASE_ONLY_USER;
+ if (clear_host_marks)
+ fact.flags |= NVM_FACTORY_RESET_HOST_BLKS;
+ if (clear_bb_marks)
+ fact.flags |= NVM_FACTORY_RESET_GRWN_BBLKS;
+
+ ret = ioctl(fd, NVM_DEV_FACTORY, &fact);
+ switch (errno) {
+ case EINVAL:
+ fprintf(stderr, "Factory reset failed.\n");
+ break;
+ case 0:
+ break;
+ default:
+ fprintf(stderr, "Unknown error occurred (%d)\n", errno);
+ break;
+ }
+
+ lnvm_close(fd);
+ return ret;
+}
+
+static void show_lnvm_id_grp(void *t, int human)
+{
+ struct nvme_nvm_id12_group *grp = t;
+ uint32_t mpos = (uint32_t)le32_to_cpu(grp->mpos);
+ uint32_t mccap = (uint32_t)le32_to_cpu(grp->mccap);
+
+ printf(" mtype : %d\n", grp->mtype);
+ if (human) {
+ if (grp->mtype == LNVM_IDFY_GRP_MTYPE_NAND)
+ printf(" NAND Flash Memory\n");
+ else
+ printf(" Reserved\n");
+ }
+ printf(" fmtype : %d\n", grp->fmtype);
+ if (human) {
+ if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_SLC)
+ printf(" Single bit Level Cell flash (SLC)\n");
+ else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_MLC)
+ printf(" Two bit Level Cell flash (MLC)\n");
+ else if (grp->fmtype == LNVM_IDFY_GRP_FMTYPE_TLC)
+ printf(" Three bit Level Cell flash (TLC)\n");
+ else
+ printf(" Reserved\n");
+ }
+ printf(" chnls : %d\n", grp->num_ch);
+ printf(" luns : %d\n", grp->num_lun);
+ printf(" plns : %d\n", grp->num_pln);
+ printf(" blks : %d\n", (uint16_t)le16_to_cpu(grp->num_blk));
+ printf(" pgs : %d\n", (uint16_t)le16_to_cpu(grp->num_pg));
+ printf(" fpg_sz : %d\n", (uint16_t)le16_to_cpu(grp->fpg_sz));
+ printf(" csecs : %d\n", (uint16_t)le16_to_cpu(grp->csecs));
+ printf(" sos : %d\n", (uint16_t)le16_to_cpu(grp->sos));
+ printf(" trdt : %d\n", (uint32_t)le32_to_cpu(grp->trdt));
+ printf(" trdm : %d\n", (uint32_t)le32_to_cpu(grp->trdm));
+ printf(" tprt : %d\n", (uint32_t)le32_to_cpu(grp->tprt));
+ printf(" tprm : %d\n", (uint32_t)le32_to_cpu(grp->tprm));
+ printf(" tbet : %d\n", (uint32_t)le32_to_cpu(grp->tbet));
+ printf(" tbem : %d\n", (uint32_t)le32_to_cpu(grp->tbem));
+ printf(" mpos : %#x\n", mpos);
+ if (human) {
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_RD))
+ printf(" [0]: Single plane read\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_RD))
+ printf(" [1]: Dual plane read\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_RD))
+ printf(" [2]: Quad plane read\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_PRG))
+ printf(" [8]: Single plane program\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_PRG))
+ printf(" [9]: Dual plane program\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_PRG))
+ printf(" [10]: Quad plane program\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_SNGL_PLN_ERS))
+ printf(" [16]: Single plane erase\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_DUAL_PLN_ERS))
+ printf(" [17]: Dual plane erase\n");
+ if (mpos & (1 << LNVM_IDFY_GRP_MPOS_QUAD_PLN_ERS))
+ printf(" [18]: Quad plane erase\n");
+ }
+ printf(" mccap : %#x\n", mccap);
+ if (human) {
+ if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SLC))
+ printf(" [0]: SLC mode\n");
+ if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_CMD_SUSP))
+ printf(" [1]: Command suspension\n");
+ if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_SCRAMBLE))
+ printf(" [2]: Scramble\n");
+ if (mccap & (1 << LNVM_IDFY_GRP_MCCAP_ENCRYPT))
+ printf(" [3]: Encryption\n");
+ }
+ printf(" cpar : %#x\n", (uint16_t)le16_to_cpu(grp->cpar));
+
+}
+
+static void show_lnvm_ppaf(struct nvme_nvm_addr_format *ppaf)
+{
+ printf("ppaf :\n");
+ printf(" ch offs : %d ch bits : %d\n",
+ ppaf->ch_offset, ppaf->ch_len);
+ printf(" lun offs: %d lun bits : %d\n",
+ ppaf->lun_offset, ppaf->lun_len);
+ printf(" pl offs : %d pl bits : %d\n",
+ ppaf->pln_offset, ppaf->pln_len);
+ printf(" blk offs: %d blk bits : %d\n",
+ ppaf->blk_offset, ppaf->blk_len);
+ printf(" pg offs : %d pg bits : %d\n",
+ ppaf->pg_offset, ppaf->pg_len);
+ printf(" sec offs: %d sec bits : %d\n",
+ ppaf->sect_offset, ppaf->sect_len);
+}
+
+static void show_lnvm_id12_ns(void *t, unsigned int flags)
+{
+ int i;
+ int human = flags & VERBOSE;
+ struct nvme_nvm_id12 *id = t;
+
+ uint32_t cap = (uint32_t) le32_to_cpu(id->cap);
+ uint32_t dom = (uint32_t) le32_to_cpu(id->dom);
+ uint32_t cgrps = id->cgrps;
+
+ if (id->cgrps > 4) {
+ fprintf(stderr, "invalid identify geometry returned\n");
+ return;
+ }
+
+ printf("verid : %#x\n", id->ver_id);
+ printf("vmnt : %#x\n", id->vmnt);
+ if (human) {
+ if (!id->vmnt)
+ printf(" Generic/Enable opcodes as found in this spec.");
+ else
+ printf(" Reserved/Reserved for future opcode configurations");
+ }
+ printf("\n");
+ printf("cgrps : %d\n", id->cgrps);
+ printf("cap : %#x\n", cap);
+ if (human) {
+ if (cap & (1 << LNVM_IDFY_CAP_BAD_BLK_TBL_MGMT))
+ printf(" [0]: Bad block table management\n");
+ if (cap & (1 << LNVM_IDFY_CAP_HYBRID_CMD_SUPP))
+ printf(" [1]: Hybrid command support\n");
+ }
+ printf("dom : %#x\n", dom);
+ if (human) {
+ if (dom & (1 << LNVM_IDFY_DOM_HYBRID_MODE))
+ printf(" [0]: Hybrid mode (L2P MAP is in device)\n");
+ if (dom & (1 << LNVM_IDFY_DOM_ECC_MODE))
+ printf(" [1]: Error Code Correction(ECC) mode\n");
+ }
+ show_lnvm_ppaf(&id->ppaf);
+
+ for (i = 0; i < cgrps; i++) {
+ printf("grp : %d\n", i);
+ show_lnvm_id_grp((void *)&id->groups[i], human);
+ }
+}
+
+static void show_lnvm_id20_ns(struct nvme_nvm_id20 *id, unsigned int flags)
+{
+ int human = flags & VERBOSE;
+ uint32_t mccap = (uint32_t) le32_to_cpu(id->mccap);
+
+ printf("ver_major : %#x\n", id->mjr);
+ printf("ver_minor : %#x\n", id->mnr);
+
+ printf("mccap : %#x\n", mccap);
+ if (human) {
+ if (mccap & (1 << LNVM_IDFY_CAP_VCOPY))
+ printf(" [0]: Vector copy support\n");
+ if (mccap & (1 << LNVM_IDFY_CAP_MRESETS))
+ printf(" [1]: Multiple resets support\n");
+ }
+ printf("wit : %d\n", id->wit);
+
+ printf("lba format\n");
+ printf(" grp len : %d\n", id->lbaf.grp_len);
+ printf(" pu len : %d\n", id->lbaf.pu_len);
+ printf(" chk len : %d\n", id->lbaf.chk_len);
+ printf(" clba len : %d\n", id->lbaf.lba_len);
+
+ printf("geometry\n");
+ printf(" num_grp : %d\n", le16_to_cpu(id->num_grp));
+ printf(" num_pu : %d\n", le16_to_cpu(id->num_pu));
+ printf(" num_chk : %d\n", le32_to_cpu(id->num_chk));
+ printf(" clba : %d\n", le32_to_cpu(id->clba));
+ printf("write req\n");
+ printf(" ws_min : %d\n", le32_to_cpu(id->ws_min));
+ printf(" ws_opt : %d\n", le32_to_cpu(id->ws_opt));
+ printf(" mw_cunits : %d\n", le32_to_cpu(id->mw_cunits));
+ printf(" maxoc : %d\n", le32_to_cpu(id->maxoc));
+ printf(" maxocpu : %d\n", le32_to_cpu(id->maxocpu));
+ printf("perf metrics\n");
+ printf(" trdt (ns) : %d\n", le32_to_cpu(id->trdt));
+ printf(" trdm (ns) : %d\n", le32_to_cpu(id->trdm));
+ printf(" twrt (ns) : %d\n", le32_to_cpu(id->twrt));
+ printf(" twrm (ns) : %d\n", le32_to_cpu(id->twrm));
+ printf(" tcrst (ns) : %d\n", le32_to_cpu(id->tcrst));
+ printf(" tcrsm (ns) : %d\n", le32_to_cpu(id->tcrsm));
+}
+
+static void show_lnvm_id_ns(struct nvme_nvm_id *id, unsigned int flags)
+{
+ switch (id->ver_id) {
+ case 1:
+ show_lnvm_id12_ns((void *) id, flags);
+ break;
+ case 2:
+ show_lnvm_id20_ns((void *) id, flags);
+ break;
+ default:
+ fprintf(stderr, "Version %d not supported.\n",
+ id->ver_id);
+ }
+}
+
+int lnvm_get_identity(int fd, int nsid, struct nvme_nvm_id *nvm_id)
+{
+ struct nvme_passthru_cmd cmd = {
+ .opcode = nvme_nvm_admin_identity,
+ .nsid = nsid,
+ .addr = (__u64)(uintptr_t)nvm_id,
+ .data_len = sizeof(struct nvme_nvm_id),
+ };
+
+ return nvme_submit_admin_passthru(fd, &cmd, NULL);
+}
+
+int lnvm_do_id_ns(int fd, int nsid, unsigned int flags)
+{
+ struct nvme_nvm_id nvm_id;
+ int err;
+
+ err = lnvm_get_identity(fd, nsid, &nvm_id);
+ if (!err) {
+ if (flags & BINARY)
+ d_raw((unsigned char *)&nvm_id, sizeof(nvm_id));
+ else
+ show_lnvm_id_ns(&nvm_id, flags);
+ } else if (err > 0)
+ fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
+ nvme_status_to_string(err, false), err, nsid);
+ return err;
+}
+
+static inline const char *print_chunk_state(__u8 cs)
+{
+ switch (cs) {
+ case 1 << 0: return "FREE";
+ case 1 << 1: return "CLOSED";
+ case 1 << 2: return "OPEN";
+ case 1 << 3: return "OFFLINE";
+ default: return "UNKNOWN";
+ }
+}
+
+static inline const char *print_chunk_type(__u8 ct)
+{
+ switch (ct & 0xF) {
+ case 1 << 0: return "SEQWRITE_REQ";
+ case 1 << 1: return "RANDWRITE_ALLOWED";
+ default: return "UNKNOWN";
+ }
+}
+
+static inline const char *print_chunk_attr(__u8 ct)
+{
+ switch (ct & 0xF0) {
+ case 1 << 4: return "DEVIATED";
+ default: return "NONE";
+ }
+}
+
+static void show_lnvm_chunk_log(struct nvme_nvm_chunk_desc *chunk_log,
+ __u32 data_len)
+{
+ int nr_entry = data_len / sizeof(struct nvme_nvm_chunk_desc);
+ int idx;
+
+ printf("Total chunks in namespace: %d\n", nr_entry);
+ for (idx = 0; idx < nr_entry; idx++) {
+ struct nvme_nvm_chunk_desc *desc = &chunk_log[idx];
+
+ printf(" [%5d] { ", idx);
+ printf("SLBA: 0x%016"PRIx64, le64_to_cpu(desc->slba));
+ printf(", WP: 0x%016"PRIx64, le64_to_cpu(desc->wp));
+ printf(", CNLB: 0x%016"PRIx64, le64_to_cpu(desc->cnlb));
+ printf(", State: %-8s", print_chunk_state(desc->cs));
+ printf(", Type: %-20s", print_chunk_type(desc->ct));
+ printf(", Attr: %-8s", print_chunk_attr(desc->ct));
+ printf(", WLI: %4d }\n", desc->wli);
+ }
+}
+
+int lnvm_do_chunk_log(int fd, __u32 nsid, __u32 data_len, void *data,
+ unsigned int flags)
+{
+ int err;
+
+ err = nvme_get_log(fd, NVM_LID_CHUNK_INFO, nsid, 0, 0, 0,
+ false, 0, data_len, data);
+ if (err > 0) {
+ fprintf(stderr, "NVMe Status:%s(%x) NSID:%d\n",
+ nvme_status_to_string(err, false), err, nsid);
+
+ goto out;
+ } else if (err < 0) {
+ err = -errno;
+ perror("nvme_get_log");
+
+ goto out;
+ }
+
+ if (flags & BINARY)
+ d_raw(data, data_len);
+ else
+ show_lnvm_chunk_log(data, data_len);
+
+out:
+ return err;
+}
+
+static void show_lnvm_bbtbl(struct nvme_nvm_bb_tbl *tbl)
+{
+ printf("verid : %#x\n", (uint16_t)le16_to_cpu(tbl->verid));
+ printf("tblks : %d\n", (uint32_t)le32_to_cpu(tbl->tblks));
+ printf("tfact : %d\n", (uint32_t)le32_to_cpu(tbl->tfact));
+ printf("tgrown : %d\n", (uint32_t)le32_to_cpu(tbl->tgrown));
+ printf("tdresv : %d\n", (uint32_t)le32_to_cpu(tbl->tdresv));
+ printf("thresv : %d\n", (uint32_t)le32_to_cpu(tbl->thresv));
+ printf("Use raw output to retrieve table.\n");
+}
+
+static int __lnvm_do_get_bbtbl(int fd, struct nvme_nvm_id12 *id,
+ struct ppa_addr ppa,
+ unsigned int flags)
+{
+ struct nvme_nvm_id12_group *grp = &id->groups[0];
+ int bbtblsz = ((uint16_t)le16_to_cpu(grp->num_blk) * grp->num_pln);
+ int bufsz = bbtblsz + sizeof(struct nvme_nvm_bb_tbl);
+ struct nvme_nvm_bb_tbl *bbtbl;
+ int err;
+
+ bbtbl = calloc(1, bufsz);
+ if (!bbtbl)
+ return -ENOMEM;
+
+ struct nvme_nvm_getbbtbl cmd = {
+ .opcode = nvme_nvm_admin_get_bb_tbl,
+ .nsid = cpu_to_le32(1),
+ .addr = (__u64)(uintptr_t)bbtbl,
+ .data_len = bufsz,
+ .ppa = cpu_to_le64(ppa.ppa),
+ };
+ void *tmp = &cmd;
+ struct nvme_passthru_cmd *nvme_cmd = tmp;
+
+ err = nvme_submit_admin_passthru(fd, nvme_cmd, NULL);
+ if (err > 0) {
+ fprintf(stderr, "NVMe Status:%s(%x)\n",
+ nvme_status_to_string(err, false), err);
+ free(bbtbl);
+ return err;
+ }
+
+ if (flags & BINARY)
+ d_raw((unsigned char *)&bbtbl->blk, bbtblsz);
+ else {
+ printf("LightNVM Bad Block Stats:\n");
+ show_lnvm_bbtbl(bbtbl);
+ }
+
+ free(bbtbl);
+ return 0;
+}
+
+int lnvm_do_get_bbtbl(int fd, int nsid, int lunid, int chid, unsigned int flags)
+{
+ struct nvme_nvm_id12 nvm_id;
+ struct ppa_addr ppa;
+ int err;
+ void *tmp = &nvm_id;
+
+ err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
+ if (err) {
+ fprintf(stderr, "NVMe Status:%s(%x)\n",
+ nvme_status_to_string(err, false), err);
+ return err;
+ }
+
+ if (nvm_id.ver_id != 1) {
+ fprintf(stderr, "Get bad block table not supported on version %d\n",
+ nvm_id.ver_id);
+ return -EINVAL;
+ }
+
+ if (chid >= nvm_id.groups[0].num_ch ||
+ lunid >= nvm_id.groups[0].num_lun) {
+ fprintf(stderr, "Out of bound channel id or LUN id\n");
+ return -EINVAL;
+ }
+
+ ppa.ppa = 0;
+ ppa.g.lun = lunid;
+ ppa.g.ch = chid;
+
+ ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
+
+ return __lnvm_do_get_bbtbl(fd, &nvm_id, ppa, flags);
+}
+
+static int __lnvm_do_set_bbtbl(int fd, struct ppa_addr ppa, __u8 value)
+{
+ int err;
+
+ struct nvme_nvm_setbbtbl cmd = {
+ .opcode = nvme_nvm_admin_set_bb_tbl,
+ .nsid = cpu_to_le32(1),
+ .ppa = cpu_to_le64(ppa.ppa),
+ .nlb = cpu_to_le16(0),
+ .value = value,
+ };
+ void *tmp = &cmd;
+ struct nvme_passthru_cmd *nvme_cmd = tmp;
+
+ err = nvme_submit_admin_passthru(fd, nvme_cmd, NULL);
+ if (err > 0) {
+ fprintf(stderr, "NVMe Status:%s(%x)\n",
+ nvme_status_to_string(err, false), err);
+ return err;
+ }
+ return 0;
+}
+
+int lnvm_do_set_bbtbl(int fd, int nsid,
+ int chid, int lunid, int plnid, int blkid,
+ __u8 value)
+{
+ struct nvme_nvm_id12 nvm_id;
+ struct ppa_addr ppa;
+ int err;
+ void *tmp = &nvm_id;
+
+ err = lnvm_get_identity(fd, nsid, (struct nvme_nvm_id *)tmp);
+ if (err) {
+ fprintf(stderr, "NVMe Status:%s(%x)\n",
+ nvme_status_to_string(err, false), err);
+ return err;
+ }
+
+ if (nvm_id.ver_id != 1) {
+ fprintf(stderr, "Set bad block table not supported on version %d\n",
+ nvm_id.ver_id);
+ return -EINVAL;
+ }
+
+ if (chid >= nvm_id.groups[0].num_ch ||
+ lunid >= nvm_id.groups[0].num_lun ||
+ plnid >= nvm_id.groups[0].num_pln ||
+ blkid >= le16_to_cpu(nvm_id.groups[0].num_blk)) {
+ fprintf(stderr, "Out of bound channel id, LUN id, plane id, or"\
+ "block id\n");
+ return -EINVAL;
+ }
+
+ ppa.ppa = 0;
+ ppa.g.lun = lunid;
+ ppa.g.ch = chid;
+ ppa.g.pl = plnid;
+ ppa.g.blk = blkid;
+
+ ppa = generic_to_dev_addr(&nvm_id.ppaf, ppa);
+
+ return __lnvm_do_set_bbtbl(fd, ppa, value);
+}
static int lnvm_init(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Initialize LightNVM device. A LightNVM/Open-Channel SSD"\
#include <unistd.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
};
enum {
- MB_FEAT_POWER_MGMT = 0Xc6,
+ MB_FEAT_POWER_MGMT = 0xc6,
};
#pragma pack(push, 1)
d_raw((unsigned char *)&smart_log, sizeof(smart_log));
}
if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err, false), err);
return err;
}
+#if 0
static char *mb_feature_to_string(int feature)
{
switch (feature) {
default: return "Unknown";
}
}
-
-static int get_additional_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
- const char *desc = "Read operating parameters of the "\
- "specified controller. Operating parameters are grouped "\
- "and identified by Feature Identifiers; each Feature "\
- "Identifier contains one or more attributes that may affect "\
- "behaviour of the feature. Each Feature has three possible "\
- "settings: default, saveable, and current. If a Feature is "\
- "saveable, it may be modified by set-feature. Default values "\
- "are vendor-specific and not changeable. Use set-feature to "\
- "change saveable Features.\n\n"\
- "Available additional feature id:\n"\
- "0xc6: Memblaze power management\n"\
- " (value 0 - 25w, 1 - 20w, 2 - 15w)";
- const char *raw = "show feature in binary format";
- const char *namespace_id = "identifier of desired namespace";
- const char *feature_id = "hexadecimal feature name";
- const char *sel = "[0-3]: curr./default/saved/supp.";
- const char *data_len = "buffer len (if) data is returned";
- const char *cdw11 = "dword 11 for interrupt vector config";
- const char *human_readable = "show infos in readable format";
- int err, fd;
- __u32 result;
- void *buf = NULL;
-
- struct config {
- __u32 namespace_id;
- __u32 feature_id;
- __u8 sel;
- __u32 cdw11;
- __u32 data_len;
- int raw_binary;
- int human_readable;
- };
-
- struct config cfg = {
- .namespace_id = 1,
- .feature_id = 0,
- .sel = 0,
- .cdw11 = 0,
- .data_len = 0,
- };
-
- OPT_ARGS(opts) = {
- OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
- OPT_UINT("feature-id", 'f', &cfg.feature_id, feature_id),
- OPT_BYTE("sel", 's', &cfg.sel, sel),
- OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
- OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11),
- OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable),
- OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
- OPT_END()
- };
-
- fd = parse_and_open(argc, argv, desc, opts);
- if (fd < 0)
- return fd;
-
- if (cfg.sel > 7) {
- fprintf(stderr, "invalid 'select' param:%d\n", cfg.sel);
- return EINVAL;
- }
- if (!cfg.feature_id) {
- fprintf(stderr, "feature-id required param\n");
- return EINVAL;
- }
- if (cfg.data_len) {
- if (posix_memalign(&buf, getpagesize(), cfg.data_len))
- exit(ENOMEM);
- memset(buf, 0, cfg.data_len);
- }
-
- err = nvme_get_features(fd, cfg.feature_id, cfg.namespace_id, cfg.sel, cfg.cdw11,
- 0, cfg.data_len, buf, &result);
- if (!err) {
- printf("get-feature:0x%02x (%s), %s value: %#08x\n", cfg.feature_id,
- mb_feature_to_string(cfg.feature_id),
- nvme_select_to_string(cfg.sel), result);
- if (cfg.human_readable)
- nvme_feature_show_fields(cfg.feature_id, result, buf);
- else {
- if (buf) {
- if (!cfg.raw_binary)
- d(buf, cfg.data_len, 16, 1);
- else
- d_raw(buf, cfg.data_len);
- }
- }
- } else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- if (buf)
- free(buf);
- return err;
-}
-
-static int set_additional_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
- const char *desc = "Modify the saveable or changeable "\
- "current operating parameters of the controller. Operating "\
- "parameters are grouped and identified by Feature "\
- "Identifiers. Feature settings can be applied to the entire "\
- "controller and all associated namespaces, or to only a few "\
- "namespace(s) associated with the controller. Default values "\
- "for each Feature are vendor-specific and may not be modified."\
- "Use get-feature to determine which Features are supported by "\
- "the controller and are saveable/changeable.\n\n"\
- "Available additional feature id:\n"\
- "0xc6: Memblaze power management\n"\
- " (value 0 - 25w, 1 - 20w, 2 - 15w)";
- const char *namespace_id = "desired namespace";
- const char *feature_id = "hex feature name (required)";
- const char *data_len = "buffer length if data required";
- const char *data = "optional file for feature data (default stdin)";
- const char *value = "new value of feature (required)";
- const char *save = "specifies that the controller shall save the attribute";
- int err, fd;
- __u32 result;
- void *buf = NULL;
- int ffd = STDIN_FILENO;
-
- struct config {
- char *file;
- __u32 namespace_id;
- __u32 feature_id;
- __u32 value;
- __u32 data_len;
- int save;
- };
-
- struct config cfg = {
- .file = "",
- .namespace_id = 0,
- .feature_id = 0,
- .value = 0,
- .data_len = 0,
- .save = 0,
- };
-
- OPT_ARGS(opts) = {
- OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
- OPT_UINT("feature-id", 'f', &cfg.feature_id, feature_id),
- OPT_UINT("value", 'v', &cfg.value, value),
- OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
- OPT_FILE("data", 'd', &cfg.file, data),
- OPT_FLAG("save", 's', &cfg.save, save),
- OPT_END()
- };
-
- fd = parse_and_open(argc, argv, desc, opts);
- if (fd < 0)
- return fd;
-
- if (!cfg.feature_id) {
- fprintf(stderr, "feature-id required param\n");
- return EINVAL;
- }
-
- if (cfg.data_len) {
- if (posix_memalign(&buf, getpagesize(), cfg.data_len))
- exit(ENOMEM);
- memset(buf, 0, cfg.data_len);
- }
-
- if (buf) {
- if (strlen(cfg.file)) {
- ffd = open(cfg.file, O_RDONLY);
- if (ffd <= 0) {
- fprintf(stderr, "no firmware file provided\n");
- err = EINVAL;
- goto free;
- }
- }
- if (read(ffd, (void *)buf, cfg.data_len) < 0) {
- fprintf(stderr, "failed to read data buffer from input file\n");
- err = EINVAL;
- goto free;
- }
- }
-
- err = nvme_set_features(fd, cfg.feature_id, cfg.namespace_id, cfg.value,
- 0, cfg.save, 0, 0, cfg.data_len, buf, &result);
- if (err < 0) {
- perror("set-feature");
- goto free;
- }
- if (!err) {
- printf("set-feature:%02x (%s), value:%#08x\n", cfg.feature_id,
- mb_feature_to_string(cfg.feature_id), cfg.value);
- if (buf)
- d(buf, cfg.data_len, 16, 1);
- } else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
-
-free:
- if (buf)
- free(buf);
- return err;
-}
+#endif
PLUGIN(NAME("memblaze", "Memblaze vendor specific extensions"),
COMMAND_LIST(
ENTRY("smart-log-add", "Retrieve Memblaze SMART Log, show it", get_additional_smart_log)
- ENTRY("get-feature-add", "Get Memblaze feature and show the resulting value", get_additional_feature)
- ENTRY("set-feature-add", "Set a Memblaze feature and show the resulting value", set_additional_feature)
)
);
#include <libgen.h>
#include <sys/stat.h>
#include "nvme.h"
-#include "print.h"
#include <sys/ioctl.h>
#include <limits.h>
goto out;
} else if (err != 0) {
fprintf(stderr, "NVME Admin command error:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
goto out;
}
fw_buf += xfer;
#include <sys/stat.h>
#include <ctype.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
#include "suffix.h"
if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
json_print_object(root, NULL);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
cf_err = nvme_get_log(fd, 0xCF, 1, 0, 0, 0, false, 0, sizeof(ExtdSMARTInfo), &logPageCF);
json_vs_pcie_error_log(pcieErrorLog);
} else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err, false), err);
return err;
}
seaget_d_raw((unsigned char *)(&tele_log), sizeof(tele_log), dump_fd);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
seaget_d_raw((unsigned char *)log, blksToGet * 512, dump_fd);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
seaget_d_raw((unsigned char *)(&tele_log), sizeof(tele_log), dump_fd);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
seaget_d_raw((unsigned char *)log, blksToGet * 512, dump_fd);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
seaget_d_raw((unsigned char *)(&tele_log), sizeof(tele_log), dump_fd);
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
} else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
else
perror("log page");
#include "common.h"
#include "nvme.h"
-#include "print.h"
#include "json.h"
#include "plugin.h"
}
else if (err > 0)
fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
-
-static int get_additional_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
- const char *desc = "Read operating parameters of the "\
- "specified controller. Operating parameters are grouped "\
- "and identified by Feature Identifiers; each Feature "\
- "Identifier contains one or more attributes that may affect "\
- "behaviour of the feature. Each Feature has three possible "\
- "settings: default, saveable, and current. If a Feature is "\
- "saveable, it may be modified by set-feature. Default values "\
- "are vendor-specific and not changeable. Use set-feature to "\
- "change saveable Features.\n\n"\
- "Available additional feature id:\n"\
- "0x02: Shannon power management\n";
- const char *raw = "show infos in binary format";
- const char *namespace_id = "identifier of desired namespace";
- const char *feature_id = "hexadecimal feature name";
- const char *sel = "[0-3]: curr./default/saved/supp.";
- const char *data_len = "buffer len (if) data is returned";
- const char *cdw11 = "dword 11 for interrupt vector config";
- const char *human_readable = "show infos in readable format";
- int err, fd;
- __u32 result;
- void *buf = NULL;
-
- struct config {
- __u32 namespace_id;
- __u32 feature_id;
- __u8 sel;
- __u32 cdw11;
- __u32 data_len;
- int raw_binary;
- int human_readable;
- };
-
- struct config cfg = {
- .namespace_id = 1,
- .feature_id = 0,
- .sel = 0,
- .cdw11 = 0,
- .data_len = 0,
- };
-
- OPT_ARGS(opts) = {
- OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
- OPT_UINT("feature-id", 'f', &cfg.feature_id, feature_id),
- OPT_BYTE("sel", 's', &cfg.sel, sel),
- OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
- OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
- OPT_UINT("cdw11", 'c', &cfg.cdw11, cdw11),
- OPT_FLAG("human-readable",'H', &cfg.human_readable, human_readable),
- OPT_END()
- };
-
- fd = parse_and_open(argc, argv, desc, opts);
- if (fd < 0)
- return fd;
-
- if (cfg.sel > 7) {
- fprintf(stderr, "invalid 'select' param:%d\n", cfg.sel);
- close(fd);
- return EINVAL;
- }
- if (!cfg.feature_id) {
- fprintf(stderr, "feature-id required param\n");
- close(fd);
- return EINVAL;
- }
- if (cfg.data_len) {
- if (posix_memalign(&buf, getpagesize(), cfg.data_len))
- {
- close(fd);
- exit(ENOMEM);
- }
- memset(buf, 0, cfg.data_len);
- }
-
- err = nvme_get_features(fd, cfg.feature_id, cfg.namespace_id, cfg.sel, cfg.cdw11, 0,
- cfg.data_len, buf, &result);
- if (!err) {
- printf("get-feature:0x%02x (%s), %s value: %#08x\n", cfg.feature_id,
- nvme_feature_to_string(cfg.feature_id),
- nvme_select_to_string(cfg.sel), result);
- if (cfg.human_readable)
- nvme_feature_show_fields(cfg.feature_id, result, buf);
- else {
- if (buf) {
- if (!cfg.raw_binary)
- d(buf, cfg.data_len, 16, 1);
- else
- d_raw(buf, cfg.data_len);
- }
- }
- } else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
- if (buf)
- free(buf);
- return err;
-}
-
-static int set_additional_feature(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
- const char *desc = "Modify the saveable or changeable "\
- "current operating parameters of the controller. Operating "\
- "parameters are grouped and identified by Feature "\
- "Identifiers. Feature settings can be applied to the entire "\
- "controller and all associated namespaces, or to only a few "\
- "namespace(s) associated with the controller. Default values "\
- "for each Feature are vendor-specific and may not be modified."\
- "Use get-feature to determine which Features are supported by "\
- "the controller and are saveable/changeable.\n\n"\
- "Available additional feature id:\n"\
- "0x02: Shannon power management\n";
- const char *namespace_id = "desired namespace";
- const char *feature_id = "hex feature name (required)";
- const char *data_len = "buffer length if data required";
- const char *data = "optional file for feature data (default stdin)";
- const char *value = "new value of feature (required)";
- const char *save = "specifies that the controller shall save the attribute";
- int err, fd;
- __u32 result;
- void *buf = NULL;
- int ffd = STDIN_FILENO;
-
- struct config {
- char *file;
- __u32 namespace_id;
- __u32 feature_id;
- __u32 value;
- __u32 data_len;
- int save;
- };
-
- struct config cfg = {
- .file = "",
- .namespace_id = 0,
- .feature_id = 0,
- .value = 0,
- .data_len = 0,
- .save = 0,
- };
-
- OPT_ARGS(opts) = {
- OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
- OPT_UINT("feature-id", 'f', &cfg.feature_id, feature_id),
- OPT_UINT("value", 'v', &cfg.value, value),
- OPT_UINT("data-len", 'l', &cfg.data_len, data_len),
- OPT_FILE("data", 'd', &cfg.file, data),
- OPT_FLAG("save", 's', &cfg.save, save),
- OPT_END()
- };
-
- fd = parse_and_open(argc, argv, desc, opts);
- if (fd < 0)
- return fd;
-
- if (!cfg.feature_id) {
- fprintf(stderr, "feature-id required param\n");
- close(fd);
- return EINVAL;
- }
-
- if (cfg.data_len) {
- if (posix_memalign(&buf, getpagesize(), cfg.data_len)){
- fprintf(stderr, "can not allocate feature payload\n");
- close(fd);
- return ENOMEM;
- }
- memset(buf, 0, cfg.data_len);
- }
-
- if (buf) {
- if (strlen(cfg.file)) {
- ffd = open(cfg.file, O_RDONLY);
- if (ffd <= 0) {
- fprintf(stderr, "no firmware file provided\n");
- err = EINVAL;
- goto free;
- }
- }
- err = read(ffd, (void *)buf, cfg.data_len);
- if (err < 0) {
- fprintf(stderr, "failed to read data buffer from input file\n");
- err = EINVAL;
- goto free;
- }
- }
-
- err = nvme_set_features(fd, cfg.feature_id, cfg.namespace_id, cfg.value,
- 0, cfg.save, 0, 0, cfg.data_len, buf, &result);
- if (err < 0) {
- perror("set-feature");
- goto free;
- }
- if (!err) {
- printf("set-feature:%02x (%s), value:%#08x\n", cfg.feature_id,
- nvme_feature_to_string(cfg.feature_id), cfg.value);
- if (buf)
- d(buf, cfg.data_len, 16, 1);
- } else if (err > 0)
- fprintf(stderr, "NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
-
-free:
- if (buf)
- free(buf);
- return err;
-}
-
-static int shannon_id_ctrl(int argc, char **argv, struct command *cmd, struct plugin *plugin)
-{
- return __id_ctrl(argc, argv, cmd, plugin, NULL);
-}
-
-
-
PLUGIN(NAME("shannon", "Shannon vendor specific extensions"),
COMMAND_LIST(
ENTRY("smart-log-add", "Retrieve Shannon SMART Log, show it", get_additional_smart_log)
- ENTRY("get-feature-add", "Get Shannon feature and show the resulting value", get_additional_feature)
- ENTRY("set-feature-add", "Set a Shannon feature and show the resulting value", set_additional_feature)
- ENTRY("id-ctrl", "Shannon NVMe Identify Controller", shannon_id_ctrl)
)
);
#include <stdbool.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
#include "suffix.h"
fprintf(stderr, "%s: couldn't get vendor log 0x%x\n", __func__, cfg.log);
end:
if (err > 0)
- fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(err), err);
+ fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(err, false), err);
return err;
}
fprintf(stderr, "%s: couldn't get fw log \n", __func__);
if (err > 0)
fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__,
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
end:
if (err > 0)
fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__,
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
return err;
}
#include <locale.h>
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "argconfig.h"
#include "suffix.h"
#include "common.h"
#include "nvme.h"
-#include "print.h"
#include "plugin.h"
#include "json.h"
if (ret != 0) {
fprintf(stdout, "ERROR : WDC : Crash dump erase failed\n");
}
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
l->log_size = 0;
ret = -1;
fprintf(stderr, "ERROR : WDC : reading dump length failed\n");
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
if (ret != 0) {
fprintf(stderr, "ERROR : WDC : reading dump length failed\n");
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
}
return ret;
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
if (ret != 0) {
fprintf(stderr, "ERROR : WDC : reading DUI data failed\n");
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
}
return ret;
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
if (ret != 0) {
fprintf(stderr, "ERROR : WDC : reading DUI data V2 failed\n");
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
}
return ret;
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
if (ret != 0) {
fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n",
- __func__, nvme_status_to_string(ret), ret);
+ __func__, nvme_status_to_string(ret, false), ret);
fprintf(stderr, "%s: ERROR : WDC : Get chunk %d, size = 0x%x, offset = 0x%x, addr = 0x%lx\n",
__func__, i, admin_cmd.data_len, curr_data_offset, (long unsigned int)admin_cmd.addr);
break;
}
if (ret == 0) {
- fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
ret = wdc_create_log_file(file, dump_data, dump_length);
}
free(dump_data);
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
if (ret != 0) {
- fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
fprintf(stderr, "%s: ERROR : WDC : Get chunk %d, size = 0x%x, offset = 0x%x, addr = 0x%lx\n",
__func__, i, admin_cmd.data_len, curr_data_offset, (long unsigned int)admin_cmd.addr);
break;
}
if (ret == 0) {
- fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
} else {
- fprintf(stderr, "%s: FAILURE: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: FAILURE: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
fprintf(stderr, "%s: Partial data may have been captured\n", __func__);
snprintf(file + strlen(file), PATH_MAX, "%s", "-PARTIAL");
}
}
} else {
fprintf(stderr, "ERROR : WDC: Get telemetry option feature failed. NVMe Status:%s(%x)\n",
- nvme_status_to_string(err), err);
+ nvme_status_to_string(err, false), err);
err = -EPERM;
goto close_fd;
}
ret = wdc_dump_dui_data(fd, WDC_NVME_CAP_DUI_HEADER_SIZE, 0x00, (__u8 *)log_hdr, last_xfer);
if (ret != 0) {
fprintf(stderr, "%s: ERROR : WDC : Get DUI headers failed\n", __func__);
- fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
goto out;
}
if (ret != 0) {
fprintf(stderr, "%s: ERROR : WDC : Get chunk %d, size = 0x%lx, offset = 0x%lx, addr = 0x%lx\n",
__func__, i, (long unsigned int)total_size, (long unsigned int)curr_data_offset, (long unsigned int)buffer_addr);
- fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
break;
}
if (ret != 0) {
fprintf(stderr, "%s: ERROR : WDC : Get chunk %d, size = 0x%lx, offset = 0x%x, addr = %p\n",
__func__, i, (long unsigned int)log_size, curr_data_offset, buffer_addr);
- fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: ERROR : WDC : NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
break;
}
}
}
- fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret), ret);
+ fprintf(stderr, "%s: NVMe Status:%s(%x)\n", __func__, nvme_status_to_string(ret, false), ret);
if (verbose)
fprintf(stderr, "INFO : WDC : Capture Device Unit Info log, length = 0x%lx\n", (long unsigned int)total_size);
ret = wdc_do_get_sn730_log_len(fd, &full_log_len, SN730_GET_FULL_LOG_LENGTH);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
ret = wdc_do_get_sn730_log_len(fd, &key_log_len, SN730_GET_KEY_LOG_LENGTH);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
ret = wdc_do_get_sn730_log_len(fd, &core_dump_log_len, SN730_GET_COREDUMP_LOG_LENGTH);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
ret = wdc_do_get_sn730_log_len(fd, &extended_log_len, SN730_GET_EXTENDED_LOG_LENGTH);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
/* Get the full log */
ret = get_sn730_log_chunks(fd, full_log_buf, full_log_len, SN730_GET_FULL_LOG_SUBOPCODE);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
/* Get the key log */
ret = get_sn730_log_chunks(fd, key_log_buf, key_log_len, SN730_GET_KEY_LOG_SUBOPCODE);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
/* Get the core dump log */
ret = get_sn730_log_chunks(fd, core_dump_log_buf, core_dump_log_len, SN730_GET_CORE_LOG_SUBOPCODE);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
/* Get the extended log */
ret = get_sn730_log_chunks(fd, extended_log_buf, extended_log_len, SN730_GET_EXTEND_LOG_SUBOPCODE);
if (ret) {
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
goto free_buf;
}
WDC_NVME_SUBCMD_SHIFT) | WDC_NVME_DRIVE_LOG_SIZE_CMD);
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret),
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false),
ret);
if (ret == 0) {
ret = wdc_create_log_file(file, drive_log_data, drive_log_length);
}
fprintf(stderr, "%s", err_str);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
}
}
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
ret = nvme_get_log(fd, WDC_NVME_GET_DEVICE_INFO_LOG_OPCODE, 0xFFFFFFFF, 0, 0, 0,
false, 0, WDC_CA_LOG_BUF_LEN, data);
if (strcmp(format, "json"))
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
if (ret == 0) {
/* parse the data */
ret = nvme_get_log(fd, WDC_NVME_ADD_LOG_OPCODE, 0x01, 0, 0, 0, false, 0,
WDC_ADD_LOG_BUF_LEN, data);
if (strcmp(format, "json"))
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
if (ret == 0) {
l = (struct wdc_log_page_header*)data;
total_subpages = l->num_subpages + WDC_NVME_GET_STAT_PERF_INTERVAL_LIFETIME - 1;
ret = nvme_get_log(fd, WDC_NVME_GET_VU_SMART_LOG_OPCODE, 0xFFFFFFFF, 0,0,0,
false, 0, WDC_NVME_VU_SMART_LOG_LEN, data);
if (strcmp(format, "json"))
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
if (ret == 0) {
/* parse the data */
WDC_NVME_CLEAR_PCIE_CORR_CMD);
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
out:
return ret;
}
WDC_NVME_CLEAR_ASSERT_DUMP_CMD);
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
} else
fprintf(stderr, "INFO : WDC : No Assert Dump Present\n");
false, 0, WDC_FW_ACT_HISTORY_LOG_BUF_LEN, data);
if (strcmp(format, "json"))
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
if (ret == 0) {
/* parse the data */
WDC_NVME_CLEAR_FW_ACT_HIST_CMD);
ret = nvme_submit_admin_passthru(fd, &admin_cmd, NULL);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
out:
return ret;
else
fprintf(stderr, "Controller Option Telemetry Log Page State: Enabled\n");
} else {
- fprintf(stderr, "ERROR : WDC: NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "ERROR : WDC: NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
}
} else {
fprintf(stderr, "ERROR : WDC: unsupported option for this command\n");
if (!ret && logSize)
*logSize = cmd.result;
if( ret != WDC_STATUS_SUCCESS)
- fprintf(stderr, "ERROR : WDC : VUReadSize() failed, status:%s(0x%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "ERROR : WDC : VUReadSize() failed, status:%s(0x%x)\n", nvme_status_to_string(ret, false), ret);
end:
return ret;
ret = nvme_submit_admin_passthru(fd, &cmd, NULL);
if( ret != WDC_STATUS_SUCCESS)
- fprintf(stderr, "ERROR : WDC : VUReadBuffer() failed, status:%s(0x%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "ERROR : WDC : VUReadBuffer() failed, status:%s(0x%x)\n", nvme_status_to_string(ret, false), ret);
end:
return ret;
if (!ret)
printf("New size: %" PRIu64 " GB\n", cfg.size);
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
ret = -1;
}
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
return ret;
}
ret = -1;
}
- fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
+ fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret, false), ret);
close_fd:
close(fd);
#include "util/suffix.h"
#include "common.h"
-static const uint8_t zero_uuid[16] = { 0 };
-static const uint8_t invalid_uuid[16] = {[0 ... 15] = 0xff };
-
-#if 0
-static const char dash[100] = {[0 ... 99] = '-'};
-#endif
-
-static long double int128_to_double(__u8 *data)
-{
- int i;
- long double result = 0;
-
- for (i = 0; i < 16; i++) {
- result *= 256;
- result += data[15 - i];
- }
- return result;
-}
-
-static const char *nvme_ana_state_to_string(enum nvme_ana_state state)
-{
- switch (state) {
- case NVME_ANA_STATE_OPTIMIZED:
- return "optimized";
- case NVME_ANA_STATE_NONOPTIMIZED:
- return "non-optimized";
- case NVME_ANA_STATE_INACCESSIBLE:
- return "inaccessible";
- case NVME_ANA_STATE_PERSISTENT_LOSS:
- return "persistent-loss";
- case NVME_ANA_STATE_CHANGE:
- return "change";
- }
- return "invalid state";
-}
-
-static const char *nvme_cmd_to_string(int admin, __u8 opcode)
-{
- if (admin) {
- switch (opcode) {
- case nvme_admin_delete_sq: return "Delete I/O Submission Queue";
- case nvme_admin_create_sq: return "Create I/O Submission Queue";
- case nvme_admin_get_log_page: return "Get Log Page";
- case nvme_admin_delete_cq: return "Delete I/O Completion Queue";
- case nvme_admin_create_cq: return "Create I/O Completion Queue";
- case nvme_admin_identify: return "Identify";
- case nvme_admin_abort_cmd: return "Abort";
- case nvme_admin_set_features: return "Set Features";
- case nvme_admin_get_features: return "Get Features";
- case nvme_admin_async_event: return "Asynchronous Event Request";
- case nvme_admin_ns_mgmt: return "Namespace Management";
- case nvme_admin_fw_commit: return "Firmware Commit";
- case nvme_admin_fw_download: return "Firmware Image Download";
- case nvme_admin_dev_self_test: return "Device Self-test";
- case nvme_admin_ns_attach: return "Namespace Attachment";
- case nvme_admin_keep_alive: return "Keep Alive";
- case nvme_admin_directive_send: return "Directive Send";
- case nvme_admin_directive_recv: return "Directive Receive";
- case nvme_admin_virtual_mgmt: return "Virtualization Management";
- case nvme_admin_nvme_mi_send: return "NVMEe-MI Send";
- case nvme_admin_nvme_mi_recv: return "NVMEe-MI Receive";
- case nvme_admin_dbbuf: return "Doorbell Buffer Config";
- case nvme_admin_format_nvm: return "Format NVM";
- case nvme_admin_security_send: return "Security Send";
- case nvme_admin_security_recv: return "Security Receive";
- case nvme_admin_sanitize_nvm: return "Sanitize";
- }
- } else {
- switch (opcode) {
- case nvme_cmd_flush: return "Flush";
- case nvme_cmd_write: return "Write";
- case nvme_cmd_read: return "Read";
- case nvme_cmd_write_uncor: return "Write Uncorrectable";
- case nvme_cmd_compare: return "Compare";
- case nvme_cmd_write_zeroes: return "Write Zeroes";
- case nvme_cmd_dsm: return "Dataset Management";
- case nvme_cmd_resv_register: return "Reservation Register";
- case nvme_cmd_resv_report: return "Reservation Report";
- case nvme_cmd_resv_acquire: return "Reservation Acquire";
- case nvme_cmd_resv_release: return "Reservation Release";
- }
- }
-
- return "Unknown";
-}
-
-static const char *get_sanitize_log_sstat_status_str(__u16 status)
-{
- const char *str;
-
- switch (status & NVME_SANITIZE_SSTAT_STATUS_MASK) {
- case NVME_SANITIZE_SSTAT_STATUS_NEVER_SANITIZED:
- str = "NVM Subsystem has never been sanitized.";
- break;
- case NVME_SANITIZE_SSTAT_STATUS_COMPLETE_SUCCESS:
- str = "Most Recent Sanitize Command Completed Successfully.";
- break;
- case NVME_SANITIZE_SSTAT_STATUS_IN_PROGESS:
- str = "Sanitize in Progress.";
- break;
- case NVME_SANITIZE_SSTAT_STATUS_COMPLETED_FAILED:
- str = "Most Recent Sanitize Command Failed.";
- break;
- case NVME_SANITIZE_SSTAT_STATUS_ND_COMPLETE_SUCCESS:
- str = "Most Recent Sanitize Command (No-Deallocate After Sanitize) Completed Successfully.";
- break;
- default:
- str = "Unknown.";
- }
-
- return str;
-}
-
-static void json_nvme_id_ns(struct nvme_id_ns *ns, unsigned int mode)
-{
- char nguid_buf[2 * sizeof(ns->nguid) + 1],
- eui64_buf[2 * sizeof(ns->eui64) + 1];
- char *nguid = nguid_buf, *eui64 = eui64_buf;
- struct json_object *root;
- struct json_array *lbafs;
- int i;
-
- long double nvmcap = int128_to_double(ns->nvmcap);
-
- root = json_create_object();
-
- json_object_add_value_uint(root, "nsze", le64_to_cpu(ns->nsze));
- json_object_add_value_uint(root, "ncap", le64_to_cpu(ns->ncap));
- json_object_add_value_uint(root, "nuse", le64_to_cpu(ns->nuse));
- json_object_add_value_int(root, "nsfeat", ns->nsfeat);
- json_object_add_value_int(root, "nlbaf", ns->nlbaf);
- json_object_add_value_int(root, "flbas", ns->flbas);
- json_object_add_value_int(root, "mc", ns->mc);
- json_object_add_value_int(root, "dpc", ns->dpc);
- json_object_add_value_int(root, "dps", ns->dps);
- json_object_add_value_int(root, "nmic", ns->nmic);
- json_object_add_value_int(root, "rescap", ns->rescap);
- json_object_add_value_int(root, "fpi", ns->fpi);
- json_object_add_value_int(root, "nawun", le16_to_cpu(ns->nawun));
- json_object_add_value_int(root, "nawupf", le16_to_cpu(ns->nawupf));
- json_object_add_value_int(root, "nacwu", le16_to_cpu(ns->nacwu));
- json_object_add_value_int(root, "nabsn", le16_to_cpu(ns->nabsn));
- json_object_add_value_int(root, "nabo", le16_to_cpu(ns->nabo));
- json_object_add_value_int(root, "nabspf", le16_to_cpu(ns->nabspf));
- json_object_add_value_int(root, "noiob", le16_to_cpu(ns->noiob));
- json_object_add_value_float(root, "nvmcap", nvmcap);
- json_object_add_value_int(root, "nsattr", ns->nsattr);
- json_object_add_value_int(root, "nvmsetid", le16_to_cpu(ns->nvmsetid));
-
- if (ns->nsfeat & 0x10) {
- json_object_add_value_int(root, "npwg", le16_to_cpu(ns->npwg));
- json_object_add_value_int(root, "npwa", le16_to_cpu(ns->npwa));
- json_object_add_value_int(root, "npdg", le16_to_cpu(ns->npdg));
- json_object_add_value_int(root, "npda", le16_to_cpu(ns->npda));
- json_object_add_value_int(root, "nows", le16_to_cpu(ns->nows));
- }
-
- json_object_add_value_int(root, "anagrpid", le32_to_cpu(ns->anagrpid));
- json_object_add_value_int(root, "endgid", le16_to_cpu(ns->endgid));
-
- memset(eui64, 0, sizeof(eui64_buf));
- for (i = 0; i < sizeof(ns->eui64); i++)
- eui64 += sprintf(eui64, "%02x", ns->eui64[i]);
-
- memset(nguid, 0, sizeof(nguid_buf));
- for (i = 0; i < sizeof(ns->nguid); i++)
- nguid += sprintf(nguid, "%02x", ns->nguid[i]);
-
- json_object_add_value_string(root, "eui64", eui64_buf);
- json_object_add_value_string(root, "nguid", nguid_buf);
-
- lbafs = json_create_array();
- json_object_add_value_array(root, "lbafs", lbafs);
-
- for (i = 0; i <= ns->nlbaf; i++) {
- struct json_object *lbaf = json_create_object();
-
- json_object_add_value_int(lbaf, "ms",
- le16_to_cpu(ns->lbaf[i].ms));
- json_object_add_value_int(lbaf, "ds", ns->lbaf[i].ds);
- json_object_add_value_int(lbaf, "rp", ns->lbaf[i].rp);
-
- json_array_add_value_object(lbafs, lbaf);
- }
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_nvme_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode,
- void (*vs)(__u8 *vs, struct json_object *root))
-{
- struct json_object *root;
- struct json_array *psds;
-
- long double tnvmcap = int128_to_double(ctrl->tnvmcap);
- long double unvmcap = int128_to_double(ctrl->unvmcap);
-
- char sn[sizeof(ctrl->sn) + 1], mn[sizeof(ctrl->mn) + 1],
- fr[sizeof(ctrl->fr) + 1], subnqn[sizeof(ctrl->subnqn) + 1];
- __u32 ieee = ctrl->ieee[2] << 16 | ctrl->ieee[1] << 8 | ctrl->ieee[0];
-
- int i;
-
- snprintf(sn, sizeof(sn), "%-.*s", (int)sizeof(ctrl->sn), ctrl->sn);
- snprintf(mn, sizeof(mn), "%-.*s", (int)sizeof(ctrl->mn), ctrl->mn);
- snprintf(fr, sizeof(fr), "%-.*s", (int)sizeof(ctrl->fr), ctrl->fr);
- snprintf(subnqn, sizeof(subnqn), "%-.*s", (int)sizeof(ctrl->subnqn), ctrl->subnqn);
-
- root = json_create_object();
-
- json_object_add_value_int(root, "vid", le16_to_cpu(ctrl->vid));
- json_object_add_value_int(root, "ssvid", le16_to_cpu(ctrl->ssvid));
- json_object_add_value_string(root, "sn", sn);
- json_object_add_value_string(root, "mn", mn);
- json_object_add_value_string(root, "fr", fr);
- json_object_add_value_int(root, "rab", ctrl->rab);
- json_object_add_value_int(root, "ieee", ieee);
- json_object_add_value_int(root, "cmic", ctrl->cmic);
- json_object_add_value_int(root, "mdts", ctrl->mdts);
- json_object_add_value_int(root, "cntlid", le16_to_cpu(ctrl->cntlid));
- json_object_add_value_uint(root, "ver", le32_to_cpu(ctrl->ver));
- json_object_add_value_uint(root, "rtd3r", le32_to_cpu(ctrl->rtd3r));
- json_object_add_value_uint(root, "rtd3e", le32_to_cpu(ctrl->rtd3e));
- json_object_add_value_uint(root, "oaes", le32_to_cpu(ctrl->oaes));
- json_object_add_value_int(root, "ctratt", le32_to_cpu(ctrl->ctratt));
- json_object_add_value_int(root, "rrls", le16_to_cpu(ctrl->rrls));
- json_object_add_value_int(root, "crdt1", le16_to_cpu(ctrl->crdt1));
- json_object_add_value_int(root, "crdt2", le16_to_cpu(ctrl->crdt2));
- json_object_add_value_int(root, "crdt3", le16_to_cpu(ctrl->crdt3));
- json_object_add_value_int(root, "oacs", le16_to_cpu(ctrl->oacs));
- json_object_add_value_int(root, "acl", ctrl->acl);
- json_object_add_value_int(root, "aerl", ctrl->aerl);
- json_object_add_value_int(root, "frmw", ctrl->frmw);
- json_object_add_value_int(root, "lpa", ctrl->lpa);
- json_object_add_value_int(root, "elpe", ctrl->elpe);
- json_object_add_value_int(root, "npss", ctrl->npss);
- json_object_add_value_int(root, "avscc", ctrl->avscc);
- json_object_add_value_int(root, "apsta", ctrl->apsta);
- json_object_add_value_int(root, "wctemp", le16_to_cpu(ctrl->wctemp));
- json_object_add_value_int(root, "cctemp", le16_to_cpu(ctrl->cctemp));
- json_object_add_value_int(root, "mtfa", le16_to_cpu(ctrl->mtfa));
- json_object_add_value_uint(root, "hmpre", le32_to_cpu(ctrl->hmpre));
- json_object_add_value_uint(root, "hmmin", le32_to_cpu(ctrl->hmmin));
- json_object_add_value_float(root, "tnvmcap", tnvmcap);
- json_object_add_value_float(root, "unvmcap", unvmcap);
- json_object_add_value_uint(root, "rpmbs", le32_to_cpu(ctrl->rpmbs));
- json_object_add_value_int(root, "edstt", le16_to_cpu(ctrl->edstt));
- json_object_add_value_int(root, "dsto", ctrl->dsto);
- json_object_add_value_int(root, "fwug", ctrl->fwug);
- json_object_add_value_int(root, "kas", le16_to_cpu(ctrl->kas));
- json_object_add_value_int(root, "hctma", le16_to_cpu(ctrl->hctma));
- json_object_add_value_int(root, "mntmt", le16_to_cpu(ctrl->mntmt));
- json_object_add_value_int(root, "mxtmt", le16_to_cpu(ctrl->mxtmt));
- json_object_add_value_int(root, "sanicap", le32_to_cpu(ctrl->sanicap));
- json_object_add_value_int(root, "hmminds", le32_to_cpu(ctrl->hmminds));
- json_object_add_value_int(root, "hmmaxd", le16_to_cpu(ctrl->hmmaxd));
- json_object_add_value_int(root, "nsetidmax",
- le16_to_cpu(ctrl->nsetidmax));
-
- json_object_add_value_int(root, "anatt",ctrl->anatt);
- json_object_add_value_int(root, "anacap", ctrl->anacap);
- json_object_add_value_int(root, "anagrpmax",
- le32_to_cpu(ctrl->anagrpmax));
- json_object_add_value_int(root, "nanagrpid",
- le32_to_cpu(ctrl->nanagrpid));
- json_object_add_value_int(root, "sqes", ctrl->sqes);
- json_object_add_value_int(root, "cqes", ctrl->cqes);
- json_object_add_value_int(root, "maxcmd", le16_to_cpu(ctrl->maxcmd));
- json_object_add_value_uint(root, "nn", le32_to_cpu(ctrl->nn));
- json_object_add_value_int(root, "oncs", le16_to_cpu(ctrl->oncs));
- json_object_add_value_int(root, "fuses", le16_to_cpu(ctrl->fuses));
- json_object_add_value_int(root, "fna", ctrl->fna);
- json_object_add_value_int(root, "vwc", ctrl->vwc);
- json_object_add_value_int(root, "awun", le16_to_cpu(ctrl->awun));
- json_object_add_value_int(root, "awupf", le16_to_cpu(ctrl->awupf));
- json_object_add_value_int(root, "nvscc", ctrl->nvscc);
- json_object_add_value_int(root, "nwpc", ctrl->nwpc);
- json_object_add_value_int(root, "acwu", le16_to_cpu(ctrl->acwu));
- json_object_add_value_int(root, "sgls", le32_to_cpu(ctrl->sgls));
-
- if (strlen(subnqn))
- json_object_add_value_string(root, "subnqn", subnqn);
-
- json_object_add_value_int(root, "ioccsz", le32_to_cpu(ctrl->ioccsz));
- json_object_add_value_int(root, "iorcsz", le32_to_cpu(ctrl->iorcsz));
- json_object_add_value_int(root, "icdoff", le16_to_cpu(ctrl->icdoff));
- json_object_add_value_int(root, "fcatt", ctrl->fcatt);
- json_object_add_value_int(root, "msdbd", ctrl->msdbd);
-
- psds = json_create_array();
- json_object_add_value_array(root, "psds", psds);
-
- for (i = 0; i <= ctrl->npss; i++) {
- struct json_object *psd = json_create_object();
-
- json_object_add_value_int(psd, "mp",
- le16_to_cpu(ctrl->psd[i].mp));
- json_object_add_value_int(psd, "flags", ctrl->psd[i].flags);
- json_object_add_value_uint(psd, "enlat",
- le32_to_cpu(ctrl->psd[i].enlat));
- json_object_add_value_uint(psd, "exlat",
- le32_to_cpu(ctrl->psd[i].exlat));
- json_object_add_value_int(psd, "rrt",
- ctrl->psd[i].rrt);
- json_object_add_value_int(psd, "rrl",
- ctrl->psd[i].rrl);
- json_object_add_value_int(psd, "rwt",
- ctrl->psd[i].rwt);
- json_object_add_value_int(psd, "rwl",
- ctrl->psd[i].rwl);
- json_object_add_value_int(psd, "idlp",
- le16_to_cpu(ctrl->psd[i].idlp));
- json_object_add_value_int(psd, "ips",
- ctrl->psd[i].ips);
- json_object_add_value_int(psd, "apw",
- le16_to_cpu(ctrl->psd[i].apw));
- json_object_add_value_int(psd, "aps",
- ctrl->psd[i].aps);
-
- json_array_add_value_object(psds, psd);
- }
-
- if(vs)
- vs(ctrl->vs, root);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_error_log(struct nvme_error_log_page *err_log, int entries)
-{
- struct json_object *root;
- struct json_array *errors;
- int i;
-
- root = json_create_object();
- errors = json_create_array();
- json_object_add_value_array(root, "errors", errors);
-
- for (i = 0; i < entries; i++) {
- struct json_object *error = json_create_object();
-
- json_object_add_value_uint(error, "error_count",
- le64_to_cpu(err_log[i].error_count));
- json_object_add_value_int(error, "sqid",
- le16_to_cpu(err_log[i].sqid));
- json_object_add_value_int(error, "cmdid",
- le16_to_cpu(err_log[i].cmdid));
- json_object_add_value_int(error, "status_field",
- le16_to_cpu(err_log[i].status_field));
- json_object_add_value_int(error, "parm_error_location",
- le16_to_cpu(err_log[i].parm_error_location));
- json_object_add_value_uint(error, "lba",
- le64_to_cpu(err_log[i].lba));
- json_object_add_value_uint(error, "nsid",
- le32_to_cpu(err_log[i].nsid));
- json_object_add_value_int(error, "vs", err_log[i].vs);
- json_object_add_value_int(error, "trtype", err_log[i].trtype);
- json_object_add_value_uint(error, "cs",
- le64_to_cpu(err_log[i].cs));
- json_object_add_value_int(error, "trtype_spec_info",
- le16_to_cpu(err_log[i].trtype_spec_info));
-
- json_array_add_value_object(errors, error);
- }
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_nvme_resv_report(struct nvme_reservation_status *status,
- int bytes, __u32 cdw11)
-{
- struct json_object *root;
- struct json_array *rcs;
- int i, j, regctl, entries;
-
- regctl = status->regctl[0] | (status->regctl[1] << 8);
-
- root = json_create_object();
-
- json_object_add_value_int(root, "gen", le32_to_cpu(status->gen));
- json_object_add_value_int(root, "rtype", status->rtype);
- json_object_add_value_int(root, "regctl", regctl);
- json_object_add_value_int(root, "ptpls", status->ptpls);
-
- rcs = json_create_array();
- /* check Extended Data Structure bit */
- if ((cdw11 & 0x1) == 0) {
- /*
- * if status buffer was too small, don't loop past the end of
- * the buffer
- */
- entries = (bytes - 24) / 24;
- if (entries < regctl)
- regctl = entries;
-
- json_object_add_value_array(root, "regctls", rcs);
- for (i = 0; i < regctl; i++) {
- struct json_object *rc = json_create_object();
-
- json_object_add_value_int(rc, "cntlid",
- le16_to_cpu(status->regctl_ds[i].cntlid));
- json_object_add_value_int(rc, "rcsts",
- status->regctl_ds[i].rcsts);
- json_object_add_value_uint(rc, "hostid",
- le64_to_cpu(status->regctl_ds[i].hostid));
- json_object_add_value_uint(rc, "rkey",
- le64_to_cpu(status->regctl_ds[i].rkey));
-
- json_array_add_value_object(rcs, rc);
- }
- } else {
- char hostid[33];
-
- /* if status buffer was too small, don't loop past the end of the buffer */
- entries = (bytes - 64) / 64;
- if (entries < regctl)
- regctl = entries;
-
- json_object_add_value_array(root, "regctlext", rcs);
- for (i = 0; i < regctl; i++) {
- struct json_object *rc = json_create_object();
-
- json_object_add_value_int(rc, "cntlid",
- le16_to_cpu(status->regctl_eds[i].cntlid));
- json_object_add_value_int(rc, "rcsts",
- status->regctl_eds[i].rcsts);
- json_object_add_value_uint(rc, "rkey",
- le64_to_cpu(status->regctl_eds[i].rkey));
- for (j = 0; j < 16; j++)
- sprintf(hostid + j * 2, "%02x",
- status->regctl_eds[i].hostid[j]);
-
- json_object_add_value_string(rc, "hostid", hostid);
- json_array_add_value_object(rcs, rc);
- }
- }
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_fw_log(struct nvme_firmware_slot *fw_log, const char *devname)
-{
- struct json_object *root;
- struct json_object *fwsi;
- char fmt[21];
- char str[32];
- int i;
-
- root = json_create_object();
- fwsi = json_create_object();
-
- json_object_add_value_int(fwsi, "Active Firmware Slot (afi)",
- fw_log->afi);
- for (i = 0; i < 7; i++) {
- if (fw_log->frs[i]) {
- snprintf(fmt, sizeof(fmt), "Firmware Rev Slot %d",
- i + 1);
- snprintf(str, sizeof(str), "%-.*s",
- (int)sizeof(fw_log->frs[i]), fw_log->frs[i]);
- json_object_add_value_string(fwsi, fmt, str);
- }
- }
- json_object_add_value_object(root, devname, fwsi);
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_changed_ns_list_log(struct nvme_ns_list *log,
- const char *devname)
-{
- struct json_object *root;
- struct json_object *nsi;
- char fmt[32];
- char str[32];
- __u32 nsid;
- int i;
-
- if (log->ns[0] == cpu_to_le32(0xffffffff))
- return;
-
- root = json_create_object();
- nsi = json_create_object();
-
- json_object_add_value_string(root, "Changed Namespace List Log",
- devname);
-
- for (i = 0; i < NVME_ID_NS_LIST_MAX; i++) {
- nsid = le32_to_cpu(log->ns[i]);
-
- if (nsid == 0)
- break;
-
- snprintf(fmt, sizeof(fmt), "[%4u]", i + 1);
- snprintf(str, sizeof(str), "%#x", nsid);
- json_object_add_value_string(nsi, fmt, str);
- }
-
- json_object_add_value_object(root, devname, nsi);
- json_print_object(root, NULL);
- printf("\n");
-
- json_free_object(root);
-}
-
-static void json_endurance_log(struct nvme_endurance_group_log *endurance_group,
- __u16 group_id)
-{
- struct json_object *root;
-
- long double endurance_estimate =
- int128_to_double(endurance_group->endurance_estimate);
- long double data_units_read =
- int128_to_double(endurance_group->data_units_read);
- long double data_units_written =
- int128_to_double(endurance_group->data_units_written);
- long double media_units_written =
- int128_to_double(endurance_group->media_units_written);
- long double host_read_cmds =
- int128_to_double(endurance_group->host_read_cmds);
- long double host_write_cmds =
- int128_to_double(endurance_group->host_write_cmds);
- long double media_data_integrity_err =
- int128_to_double(endurance_group->media_data_integrity_err);
- long double num_err_info_log_entries =
- int128_to_double(endurance_group->num_err_info_log_entries);
-
- root = json_create_object();
-
- json_object_add_value_int(root, "critical_warning",
- endurance_group->critical_warning);
- json_object_add_value_int(root, "avl_spare",
- endurance_group->avl_spare);
- json_object_add_value_int(root, "avl_spare_threshold",
- endurance_group->avl_spare_threshold);
- json_object_add_value_int(root, "percent_used",
- endurance_group->percent_used);
- json_object_add_value_float(root, "endurance_estimate",
- endurance_estimate);
- json_object_add_value_float(root, "data_units_read", data_units_read);
- json_object_add_value_float(root, "data_units_written",
- data_units_written);
- json_object_add_value_float(root, "mediate_write_commands",
- media_units_written);
- json_object_add_value_float(root, "host_read_cmds", host_read_cmds);
- json_object_add_value_float(root, "host_write_cmds", host_write_cmds);
- json_object_add_value_float(root, "media_data_integrity_err",
- media_data_integrity_err);
- json_object_add_value_float(root, "num_err_info_log_entries",
- num_err_info_log_entries);
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
- enum nvme_print_flags flags)
-{
- int c, human = flags & VERBOSE;
- struct json_object *root;
- char key[21];
-
- unsigned int temperature = ((smart->temperature[1] << 8) |
- smart->temperature[0]);
-
- long double data_units_read = int128_to_double(smart->data_units_read);
- long double data_units_written = int128_to_double(smart->data_units_written);
- long double host_read_commands = int128_to_double(smart->host_reads);
- long double host_write_commands = int128_to_double(smart->host_writes);
- long double controller_busy_time = int128_to_double(smart->ctrl_busy_time);
- long double power_cycles = int128_to_double(smart->power_cycles);
- long double power_on_hours = int128_to_double(smart->power_on_hours);
- long double unsafe_shutdowns = int128_to_double(smart->unsafe_shutdowns);
- long double media_errors = int128_to_double(smart->media_errors);
- long double num_err_log_entries = int128_to_double(smart->num_err_log_entries);
-
- root = json_create_object();
-
- if (human) {
- struct json_object *crt = json_create_object();
-
- json_object_add_value_int(crt, "value", smart->critical_warning);
- json_object_add_value_int(crt, "available_spare", smart->critical_warning & 0x01);
- json_object_add_value_int(crt, "temp_threshold", (smart->critical_warning & 0x02) >> 1);
- json_object_add_value_int(crt, "reliability_degraded", (smart->critical_warning & 0x04) >> 2);
- json_object_add_value_int(crt, "ro", (smart->critical_warning & 0x08) >> 3);
- json_object_add_value_int(crt, "vmbu_failed", (smart->critical_warning & 0x10) >> 4);
- json_object_add_value_int(crt, "pmr_ro", (smart->critical_warning & 0x20) >> 5);
-
- json_object_add_value_object(root, "critical_warning", crt);
- } else
- json_object_add_value_int(root, "critical_warning",
- smart->critical_warning);
-
- json_object_add_value_int(root, "temperature", temperature);
- json_object_add_value_int(root, "avail_spare", smart->avail_spare);
- json_object_add_value_int(root, "spare_thresh", smart->spare_thresh);
- json_object_add_value_int(root, "percent_used", smart->percent_used);
- json_object_add_value_int(root, "endurance_grp_critical_warning_summary",
- smart->endu_grp_crit_warn_sumry);
- json_object_add_value_float(root, "data_units_read", data_units_read);
- json_object_add_value_float(root, "data_units_written",
- data_units_written);
- json_object_add_value_float(root, "host_read_commands",
- host_read_commands);
- json_object_add_value_float(root, "host_write_commands",
- host_write_commands);
- json_object_add_value_float(root, "controller_busy_time",
- controller_busy_time);
- json_object_add_value_float(root, "power_cycles", power_cycles);
- json_object_add_value_float(root, "power_on_hours", power_on_hours);
- json_object_add_value_float(root, "unsafe_shutdowns", unsafe_shutdowns);
- json_object_add_value_float(root, "media_errors", media_errors);
- json_object_add_value_float(root, "num_err_log_entries",
- num_err_log_entries);
- json_object_add_value_uint(root, "warning_temp_time",
- le32_to_cpu(smart->warning_temp_time));
- json_object_add_value_uint(root, "critical_comp_time",
- le32_to_cpu(smart->critical_comp_time));
-
- for (c=0; c < 8; c++) {
- __s32 temp = le16_to_cpu(smart->temp_sensor[c]);
-
- if (temp == 0)
- continue;
- sprintf(key, "temperature_sensor_%d",c+1);
- json_object_add_value_int(root, key, temp);
- }
-
- json_object_add_value_uint(root, "thm_temp1_trans_count",
- le32_to_cpu(smart->thm_temp1_trans_count));
- json_object_add_value_uint(root, "thm_temp2_trans_count",
- le32_to_cpu(smart->thm_temp2_trans_count));
- json_object_add_value_uint(root, "thm_temp1_total_time",
- le32_to_cpu(smart->thm_temp1_total_time));
- json_object_add_value_uint(root, "thm_temp2_total_time",
- le32_to_cpu(smart->thm_temp2_total_time));
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_ana_log(struct nvme_ana_log *ana_log, const char *devname)
-{
- int offset = sizeof(struct nvme_ana_log);
- struct nvme_ana_log *hdr = ana_log;
- struct nvme_ana_group_desc *ana_desc;
- struct json_array *desc_list;
- struct json_array *ns_list;
- struct json_object *desc;
- struct json_object *nsid;
- struct json_object *root;
- size_t nsid_buf_size;
- void *base = ana_log;
- __u32 nr_nsids;
- int i, j;
-
- root = json_create_object();
- json_object_add_value_string(root,
- "Asynchronous Namespace Access Log for NVMe device",
- devname);
- json_object_add_value_uint(root, "chgcnt",
- le64_to_cpu(hdr->chgcnt));
- json_object_add_value_uint(root, "ngrps", le16_to_cpu(hdr->ngrps));
-
- desc_list = json_create_array();
- for (i = 0; i < le16_to_cpu(ana_log->ngrps); i++) {
- desc = json_create_object();
- ana_desc = base + offset;
- nr_nsids = le32_to_cpu(ana_desc->nnsids);
- nsid_buf_size = nr_nsids * sizeof(__le32);
-
- offset += sizeof(*ana_desc);
- json_object_add_value_uint(desc, "grpid",
- le32_to_cpu(ana_desc->grpid));
- json_object_add_value_uint(desc, "nnsids",
- le32_to_cpu(ana_desc->nnsids));
- json_object_add_value_uint(desc, "chgcnt",
- le64_to_cpu(ana_desc->chgcnt));
- json_object_add_value_string(desc, "state",
- nvme_ana_state_to_string(ana_desc->state));
-
- ns_list = json_create_array();
- for (j = 0; j < le32_to_cpu(ana_desc->nnsids); j++) {
- nsid = json_create_object();
- json_object_add_value_uint(nsid, "nsid",
- le32_to_cpu(ana_desc->nsids[j]));
- json_array_add_value_object(ns_list, nsid);
- }
- json_object_add_value_array(desc, "NSIDS", ns_list);
- offset += nsid_buf_size;
- json_array_add_value_object(desc_list, desc);
- }
-
- json_object_add_value_array(root, "ANA DESC LIST ", desc_list);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_self_test_log(struct nvme_self_test_log *self_test)
-{
- struct json_object *valid_attrs;
- struct json_object *root;
- struct json_array *valid;
- int i;
-
- root = json_create_object();
- json_object_add_value_int(root, "Current Device Self-Test Operation",
- self_test->current_operation);
- json_object_add_value_int(root, "Current Device Self-Test Completion",
- self_test->completion);
- valid = json_create_array();
-
- for (i = 0; i < NVME_LOG_ST_MAX_RESULTS; i++) {
- valid_attrs = json_create_object();
- json_object_add_value_int(valid_attrs, "Self test result",
- self_test->result[i].dsts & 0xf);
- if ((self_test->result[i].dsts & 0xf) == 0xf)
- goto add;
- json_object_add_value_int(valid_attrs, "Self test code",
- self_test->result[i].dsts >> 4);
- json_object_add_value_int(valid_attrs, "Segment number",
- self_test->result[i].seg);
- json_object_add_value_int(valid_attrs, "Valid Diagnostic Information",
- self_test->result[i].vdi);
- json_object_add_value_uint(valid_attrs, "Power on hours",
- le64_to_cpu(self_test->result[i].poh));
- if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_NSID)
- json_object_add_value_int(valid_attrs, "Namespace Identifier",
- le32_to_cpu(self_test->result[i].nsid));
- if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_FLBA)
- json_object_add_value_uint(valid_attrs, "Failing LBA",
- le64_to_cpu(self_test->result[i].flba));
- if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_SCT)
- json_object_add_value_int(valid_attrs, "Status Code Type",
- self_test->result[i].sct);
- if (self_test->result[i].vdi & NVME_ST_VALID_DIAG_INFO_SC)
- json_object_add_value_int(valid_attrs, "Status Code",
- self_test->result[i].sc);
- json_object_add_value_int(valid_attrs, "Vendor Specific",
- (self_test->result[i].vs[1] << 8) |
- (self_test->result[i].vs[0]));
-add:
- json_array_add_value_object(valid, valid_attrs);
- }
- json_object_add_value_array(root, "List of Valid Reports", valid);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_effects_log(struct nvme_cmd_effects_log *effects_log)
-{
- struct json_object *root;
- unsigned int opcode;
- char key[128];
- __u32 effect;
-
- root = json_create_object();
-
- for (opcode = 0; opcode < 256; opcode++) {
- sprintf(key, "ACS%d (%s)", opcode,
- nvme_cmd_to_string(1, opcode));
- effect = le32_to_cpu(effects_log->acs[opcode]);
- json_object_add_value_uint(root, key, effect);
- }
-
- for (opcode = 0; opcode < 256; opcode++) {
- sprintf(key, "IOCS%d (%s)", opcode,
- nvme_cmd_to_string(0, opcode));
- effect = le32_to_cpu(effects_log->iocs[opcode]);
- json_object_add_value_uint(root, key, effect);
- }
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_sanitize_log(struct nvme_sanitize_log_page *sanitize_log,
- const char *devname)
-{
- struct json_object *root;
- struct json_object *dev;
- struct json_object *sstat;
- const char *status_str;
- char str[128];
- __u16 status = le16_to_cpu(sanitize_log->sstat);
-
- root = json_create_object();
- dev = json_create_object();
- sstat = json_create_object();
-
- json_object_add_value_int(dev, "sprog",
- le16_to_cpu(sanitize_log->sprog));
- json_object_add_value_int(sstat, "global_erased",
- (status & NVME_SANITIZE_SSTAT_GLOBAL_DATA_ERASED) >> 8);
- json_object_add_value_int(sstat, "no_cmplted_passes",
- (status & NVME_SANITIZE_SSTAT_COMPLETED_PASSES_MASK) >>
- NVME_SANITIZE_SSTAT_COMPLETED_PASSES_SHIFT);
-
- status_str = get_sanitize_log_sstat_status_str(status);
- sprintf(str, "(%d) %s", status & NVME_SANITIZE_SSTAT_STATUS_MASK,
- status_str);
- json_object_add_value_string(sstat, "status", str);
-
- json_object_add_value_object(dev, "sstat", sstat);
- json_object_add_value_uint(dev, "cdw10_info",
- le32_to_cpu(sanitize_log->scdw10));
- json_object_add_value_uint(dev, "time_over_write",
- le32_to_cpu(sanitize_log->eto));
- json_object_add_value_uint(dev, "time_block_erase",
- le32_to_cpu(sanitize_log->etbe));
- json_object_add_value_uint(dev, "time_crypto_erase",
- le32_to_cpu(sanitize_log->etce));
-
- json_object_add_value_uint(dev, "time_over_write_no_dealloc",
- le32_to_cpu(sanitize_log->etond));
- json_object_add_value_uint(dev, "time_block_erase_no_dealloc",
- le32_to_cpu(sanitize_log->etbend));
- json_object_add_value_uint(dev, "time_crypto_erase_no_dealloc",
- le32_to_cpu(sanitize_log->etcend));
-
- json_object_add_value_object(root, devname, dev);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-#if 0
-static void nvme_show_subsystem(struct nvme_subsystem *s)
-{
- int i;
-
- printf("%s - NQN=%s\n", s->name, s->subsysnqn);
- printf("\\\n");
-
- for (i = 0; i < s->nr_ctrls; i++) {
- printf(" +- %s %s %s %s %s\n", s->ctrls[i].name,
- s->ctrls[i].transport,
- s->ctrls[i].address,
- s->ctrls[i].state,
- s->ctrls[i].ana_state ? : "");
- }
-}
-
-static void json_print_nvme_subsystem_list(struct nvme_root *r)
-{
- struct json_object *subsystem_attrs, *path_attrs;
- struct json_array *subsystems, *paths;
- struct json_object *root;
- int i, j;
-
- root = json_create_object();
- subsystems = json_create_array();
-
- for (i = 0; i < r->nr_subsystems; i++) {
- struct nvme_subsystem *s = &r->subsystems[i];
-
- subsystem_attrs = json_create_object();
- json_object_add_value_string(subsystem_attrs,
- "Name", s->name);
- json_object_add_value_string(subsystem_attrs,
- "NQN", s->subsysnqn);
-
- json_array_add_value_object(subsystems, subsystem_attrs);
-
- paths = json_create_array();
- for (j = 0; j < s->nr_ctrls; j++) {
- struct nvme_ctrl *c = &s->ctrls[j];
-
- path_attrs = json_create_object();
- json_object_add_value_string(path_attrs, "Name",
- c->name);
- json_object_add_value_string(path_attrs, "Transport",
- c->transport);
- json_object_add_value_string(path_attrs, "Address",
- c->address);
- json_object_add_value_string(path_attrs, "State",
- c->state);
- if (c->ana_state)
- json_object_add_value_string(path_attrs,
- "ANAState", c->ana_state);
- json_array_add_value_object(paths, path_attrs);
- }
- if (j)
- json_object_add_value_array(subsystem_attrs, "Paths",
- paths);
- }
-
- if (i)
- json_object_add_value_array(root, "Subsystems", subsystems);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-#endif
-
-void nvme_show_subsystem_list(struct nvme_root *r,
- enum nvme_print_flags flags)
-{
-#if 0
- int i;
-
- if (flags & JSON)
- return json_print_nvme_subsystem_list(r);
-
- for (i = 0; i < r->nr_subsystems; i++)
- nvme_show_subsystem(&r->subsystems[i]);
-#endif
-}
-
-static void nvme_show_registers_cap(__u64 cap)
-{
-#if 0
- printf("\tController Memory Buffer Supported (CMBS): The Controller Memory Buffer is %s\n",
- ((cap->rsvd_cmbs_pmrs & 0x02) >> 1) ? "Supported" :
- "Not Supported");
- printf("\tPersistent Memory Region Supported (PMRS): The Persistent Memory Region is %s\n",
- (cap->rsvd_cmbs_pmrs & 0x01) ? "Supported" : "Not Supported");
- printf("\tMemory Page Size Maximum (MPSMAX): %u bytes\n",
- 1 << (12 + ((cap->mpsmax_mpsmin & 0xf0) >> 4)));
- printf("\tMemory Page Size Minimum (MPSMIN): %u bytes\n",
- 1 << (12 + (cap->mpsmax_mpsmin & 0x0f)));
- printf("\tBoot Partition Support (BPS): %s\n",
- (cap->bps_css_nssrs_dstrd & 0x2000) ? "Yes":"No");
- printf("\tCommand Sets Supported (CSS): NVM command set is %s\n",
- (cap->bps_css_nssrs_dstrd & 0x0020) ? "supported":"not supported");
- printf("\tNVM Subsystem Reset Supported (NSSRS): %s\n",
- (cap->bps_css_nssrs_dstrd & 0x0010) ? "Yes":"No");
- printf("\tDoorbell Stride (DSTRD): %u bytes\n",
- 1 << (2 + (cap->bps_css_nssrs_dstrd & 0x000f)));
- printf("\tTimeout (TO): %u ms\n",
- cap->to * 500);
- printf("\tArbitration Mechanism Supported (AMS): Weighted Round Robin with Urgent Priority Class is %s\n",
- (cap->ams_cqr & 0x02) ? "supported":"not supported");
- printf("\tContiguous Queues Required (CQR): %s\n",
- (cap->ams_cqr & 0x01) ? "Yes":"No");
- printf("\tMaximum Queue Entries Supported (MQES): %u\n\n",
- cap->mqes + 1);
-#endif
-}
-
-static void nvme_show_registers_version(__u32 vs)
-{
- printf("\tNVMe specification %d.%d\n\n", (vs & 0xffff0000) >> 16,
- (vs & 0x0000ff00) >> 8);
-}
-
-static void nvme_show_registers_cc_ams (__u8 ams)
-{
- printf("\tArbitration Mechanism Selected (AMS): ");
- switch (ams) {
- case 0:
- printf("Round Robin\n");
- break;
- case 1:
- printf("Weighted Round Robin with Urgent Priority Class\n");
- break;
- case 7:
- printf("Vendor Specific\n");
- break;
- default:
- printf("Reserved\n");
- }
-}
-
-static void nvme_show_registers_cc_shn (__u8 shn)
-{
- printf("\tShutdown Notification (SHN): ");
- switch (shn) {
- case 0:
- printf("No notification; no effect\n");
- break;
- case 1:
- printf("Normal shutdown notification\n");
- break;
- case 2:
- printf("Abrupt shutdown notification\n");
- break;
- default:
- printf("Reserved\n");
- }
-}
-
-static void nvme_show_registers_cc(__u32 cc)
-{
- printf("\tI/O Completion Queue Entry Size (IOCQES): %u bytes\n",
- 1 << ((cc & 0x00f00000) >> NVME_CC_IOCQES_SHIFT));
- printf("\tI/O Submission Queue Entry Size (IOSQES): %u bytes\n",
- 1 << ((cc & 0x000f0000) >> NVME_CC_IOSQES_SHIFT));
- nvme_show_registers_cc_shn((cc & 0x0000c000) >> NVME_CC_SHN_SHIFT);
- nvme_show_registers_cc_ams((cc & 0x00003800) >> NVME_CC_AMS_SHIFT);
- printf("\tMemory Page Size (MPS): %u bytes\n",
- 1 << (12 + ((cc & 0x00000780) >> NVME_CC_MPS_SHIFT)));
- printf("\tI/O Command Sets Selected (CSS): %s\n",
- (cc & 0x00000070) ? "Reserved":"NVM Command Set");
- printf("\tEnable (EN): %s\n\n",
- (cc & 0x00000001) ? "Yes":"No");
-}
-
-static void nvme_show_registers_csts_shst(__u8 shst)
-{
- printf("\tShutdown Status (SHST): ");
- switch (shst) {
- case 0:
- printf("Normal operation (no shutdown has been requested)\n");
- break;
- case 1:
- printf("Shutdown processing occurring\n");
- break;
- case 2:
- printf("Shutdown processing complete\n");
- break;
- default:
- printf("Reserved\n");
- }
-}
-
-static void nvme_show_registers_csts(__u32 csts)
-{
- printf("\tProcessing Paused (PP): %s\n",
- (csts & 0x00000020) ? "Yes":"No");
- printf("\tNVM Subsystem Reset Occurred (NSSRO): %s\n",
- (csts & 0x00000010) ? "Yes":"No");
- nvme_show_registers_csts_shst((csts & 0x0000000c) >> 2);
- printf("\tController Fatal Status (CFS): %s\n",
- (csts & 0x00000002) ? "True":"False");
- printf("\tReady (RDY): %s\n\n",
- (csts & 0x00000001) ? "Yes":"No");
-
-}
-
-static void nvme_show_registers_aqa(__u32 aqa)
-{
- printf("\tAdmin Completion Queue Size (ACQS): %u\n",
- ((aqa & 0x0fff0000) >> 16) + 1);
- printf("\tAdmin Submission Queue Size (ASQS): %u\n\n",
- (aqa & 0x00000fff) + 1);
-
-}
-
-static void nvme_show_registers_cmbloc(__u32 cmbloc, __u32 cmbsz)
-{
- static const char *enforced[] = { "Enforced", "Not Enforced" };
-
- if (cmbsz == 0) {
- printf("\tController Memory Buffer feature is not supported\n\n");
- return;
- }
- printf("\tOffset (OFST): 0x%x (See cmbsz.szu for granularity)\n",
- (cmbloc & 0xfffff000) >> 12);
-
- printf("\tCMB Queue Dword Alignment (CQDA): %d\n",
- (cmbloc & 0x00000100) >> 8);
-
- printf("\tCMB Data Metadata Mixed Memory Support (CDMMMS): %s\n",
- enforced[(cmbloc & 0x00000080) >> 7]);
-
- printf("\tCMB Data Pointer and Command Independent Locations Support (CDPCILS): %s\n",
- enforced[(cmbloc & 0x00000040) >> 6]);
-
- printf("\tCMB Data Pointer Mixed Locations Support (CDPMLS): %s\n",
- enforced[(cmbloc & 0x00000020) >> 5]);
-
- printf("\tCMB Queue Physically Discontiguous Support (CQPDS): %s\n",
- enforced[(cmbloc & 0x00000010) >> 4]);
-
- printf("\tCMB Queue Mixed Memory Support (CQMMS): %s\n",
- enforced[(cmbloc & 0x00000008) >> 3]);
-
- printf("\tBase Indicator Register (BIR): 0x%x\n\n",
- (cmbloc & 0x00000007));
-}
-
-static const char *nvme_register_szu_to_string(__u8 szu)
-{
- switch (szu) {
- case 0: return "4 KB";
- case 1: return "64 KB";
- case 2: return "1 MB";
- case 3: return "16 MB";
- case 4: return "256 MB";
- case 5: return "4 GB";
- case 6: return "64 GB";
- default:return "Reserved";
- }
-}
-
-static void nvme_show_registers_cmbsz(__u32 cmbsz)
-{
- if (cmbsz == 0) {
- printf("\tController Memory Buffer feature is not supported\n\n");
- return;
- }
- printf("\tSize (SZ): %u\n",
- (cmbsz & 0xfffff000) >> 12);
- printf("\tSize Units (SZU): %s\n",
- nvme_register_szu_to_string((cmbsz & 0x00000f00) >> 8));
- printf("\tWrite Data Support (WDS): Write Data and metadata transfer in Controller Memory Buffer is %s\n",
- (cmbsz & 0x00000010) ? "Supported":"Not supported");
- printf("\tRead Data Support (RDS): Read Data and metadata transfer in Controller Memory Buffer is %s\n",
- (cmbsz & 0x00000008) ? "Supported":"Not supported");
- printf("\tPRP SGL List Support (LISTS): PRP/SG Lists in Controller Memory Buffer is %s\n",
- (cmbsz & 0x00000004) ? "Supported":"Not supported");
- printf("\tCompletion Queue Support (CQS): Admin and I/O Completion Queues in Controller Memory Buffer is %s\n",
- (cmbsz & 0x00000002) ? "Supported":"Not supported");
- printf("\tSubmission Queue Support (SQS): Admin and I/O Submission Queues in Controller Memory Buffer is %s\n\n",
- (cmbsz & 0x00000001) ? "Supported":"Not supported");
-}
-
-static void nvme_show_registers_bpinfo_brs(__u8 brs)
-{
- printf("\tBoot Read Status (BRS): ");
- switch (brs) {
- case 0:
- printf("No Boot Partition read operation requested\n");
- break;
- case 1:
- printf("Boot Partition read in progress\n");
- break;
- case 2:
- printf("Boot Partition read completed successfully\n");
- break;
- case 3:
- printf("Error completing Boot Partition read\n");
- break;
- default:
- printf("Invalid\n");
- }
-}
-
-static void nvme_show_registers_bpinfo(__u32 bpinfo)
-{
- if (bpinfo == 0) {
- printf("\tBoot Partition feature is not supported\n\n");
- return;
- }
-
- printf("\tActive Boot Partition ID (ABPID): %u\n",
- (bpinfo & 0x80000000) >> 31);
- nvme_show_registers_bpinfo_brs((bpinfo & 0x03000000) >> 24);
- printf("\tBoot Partition Size (BPSZ): %u\n",
- bpinfo & 0x00007fff);
-}
-
-static void nvme_show_registers_bprsel(__u32 bprsel)
-{
- if (bprsel == 0) {
- printf("\tBoot Partition feature is not supported\n\n");
- return;
- }
-
- printf("\tBoot Partition Identifier (BPID): %u\n",
- (bprsel & 0x80000000) >> 31);
- printf("\tBoot Partition Read Offset (BPROF): %x\n",
- (bprsel & 0x3ffffc00) >> 10);
- printf("\tBoot Partition Read Size (BPRSZ): %x\n",
- bprsel & 0x000003ff);
-}
-
-static void nvme_show_registers_bpmbl(uint64_t bpmbl)
-{
- if (bpmbl == 0) {
- printf("\tBoot Partition feature is not supported\n\n");
- return;
- }
-
- printf("\tBoot Partition Memory Buffer Base Address (BMBBA): %"PRIx64"\n",
- bpmbl);
-}
-
-static void nvme_show_registers_cmbmsc(uint64_t cmbmsc)
-{
- printf("\tController Base Address (CBA) : %" PRIx64 "\n",
- (cmbmsc & 0xfffffffffffff000) >> 12);
- printf("\tController Memory Space Enable (CMSE): %" PRIx64 "\n",
- (cmbmsc & 0x0000000000000002) >> 1);
- printf("\tCapabilities Registers Enabled (CRE) : CMBLOC and "\
- "CMBSZ registers are%senabled\n\n",
- (cmbmsc & 0x0000000000000001) ? " " : " NOT ");
-}
-
-static void nvme_show_registers_cmbsts(__u32 cmbsts)
-{
- printf("\tController Base Address Invalid (CBAI): %x\n\n",
- (cmbsts & 0x00000001));
-}
-
-static void nvme_show_registers_pmrcap(__u32 pmrcap)
-{
- printf("\tController Memory Space Supported (CMSS) : "\
- "Referencing PMR with host supplied addresses is %s\n",
- ((pmrcap & 0x01000000) >> 24) ? "Supported" : "Not Supported");
- printf("\tPersistent Memory Region Timeout (PMRTO): %x\n",
- (pmrcap & 0x00ff0000) >> 16);
- printf("\tPersistent Memory Region Write Barrier Mechanisms(PMRWBM): %x\n",
- (pmrcap & 0x00003c00) >> 10);
- printf("\tPersistent Memory Region Time Units (PMRTU): PMR time unit is %s\n",
- (pmrcap & 0x00000300) >> 8 ? "minutes":"500 milliseconds");
- printf("\tBase Indicator Register (BIR): %x\n",
- (pmrcap & 0x000000e0) >> 5);
- printf("\tWrite Data Support (WDS): Write data to the PMR is %s\n",
- (pmrcap & 0x00000010) ? "supported":"not supported");
- printf("\tRead Data Support (RDS): Read data from the PMR is %s\n",
- (pmrcap & 0x00000008) ? "supported":"not supported");
-}
-
-static void nvme_show_registers_pmrctl(__u32 pmrctl)
-{
- printf("\tEnable (EN): PMR is %s\n", (pmrctl & 0x00000001) ?
- "READY" : "Disabled");
-}
-
-static const char *nvme_register_pmr_hsts_to_string(__u8 hsts)
-{
- switch (hsts) {
- case 0: return "Normal Operation";
- case 1: return "Restore Error";
- case 2: return "Read Only";
- case 3: return "Unreliable";
- default: return "Reserved";
- }
-}
-
-static void nvme_show_registers_pmrsts(__u32 pmrsts, __u32 pmrctl)
-{
- printf("\tController Base Address Invalid (CBAI): %x\n",
- (pmrsts & 0x00001000) >> 12);
- printf("\tHealth Status (HSTS): %s\n",
- nvme_register_pmr_hsts_to_string((pmrsts & 0x00000e00) >> 9));
- printf("\tNot Ready (NRDY): "\
- "The Persistent Memory Region is %s to process "\
- "PCI Express memory read and write requests\n",
- (pmrsts & 0x00000100) == 0 && (pmrctl & 0x00000001) ?
- "READY":"Not Ready");
- printf("\tError (ERR) : %x\n", (pmrsts & 0x000000ff));
-}
-
-static const char *nvme_register_pmr_pmrszu_to_string(__u8 pmrszu)
-{
- switch (pmrszu) {
- case 0: return "Bytes";
- case 1: return "One KB";
- case 2: return "One MB";
- case 3: return "One GB";
- default: return "Reserved";
- }
-}
-
-static void nvme_show_registers_pmrebs(__u32 pmrebs)
-{
- printf("\tPMR Elasticity Buffer Size Base (PMRWBZ): %x\n", (pmrebs & 0xffffff00) >> 8);
- printf("\tRead Bypass Behavior : memory reads not conflicting with memory writes "\
- "in the PMR Elasticity Buffer %s bypass those memory writes\n",
- (pmrebs & 0x00000010) ? "SHALL":"MAY");
- printf("\tPMR Elasticity Buffer Size Units (PMRSZU): %s\n",
- nvme_register_pmr_pmrszu_to_string(pmrebs & 0x0000000f));
-}
-
-static void nvme_show_registers_pmrswtp(__u32 pmrswtp)
-{
- printf("\tPMR Sustained Write Throughput (PMRSWTV): %x\n",
- (pmrswtp & 0xffffff00) >> 8);
- printf("\tPMR Sustained Write Throughput Units (PMRSWTU): %s/second\n",
- nvme_register_pmr_pmrszu_to_string(pmrswtp & 0x0000000f));
-}
-
-static void nvme_show_registers_pmrmsc(uint64_t pmrmsc)
-{
- printf("\tController Base Address (CBA) : %" PRIx64 "\n",
- (pmrmsc & 0xfffffffffffff000) >> 12);
- printf("\tController Memory Space Enable (CMSE : %" PRIx64 "\n\n",
- (pmrmsc & 0x0000000000000001) >> 1);
-}
-
-static inline uint32_t mmio_read32(void *addr)
-{
- __le32 *p = addr;
-
- return le32_to_cpu(*p);
-}
-
-/* Access 64-bit registers as 2 32-bit; Some devices fail 64-bit MMIO. */
-static inline __u64 mmio_read64(void *addr)
-{
- __le32 *p = addr;
-
- return le32_to_cpu(*p) | ((uint64_t)le32_to_cpu(*(p + 1)) << 32);
-}
-
-static void json_ctrl_registers(void *bar)
-{
- uint64_t cap, asq, acq, bpmbl, cmbmsc, pmrmsc;
- uint32_t vs, intms, intmc, cc, csts, nssr, aqa, cmbsz, cmbloc,
- bpinfo, bprsel, cmbsts, pmrcap, pmrctl, pmrsts, pmrebs, pmrswtp;
- struct json_object *root;
-
- cap = mmio_read64(bar + NVME_REG_CAP);
- vs = mmio_read32(bar + NVME_REG_VS);
- intms = mmio_read32(bar + NVME_REG_INTMS);
- intmc = mmio_read32(bar + NVME_REG_INTMC);
- cc = mmio_read32(bar + NVME_REG_CC);
- csts = mmio_read32(bar + NVME_REG_CSTS);
- nssr = mmio_read32(bar + NVME_REG_NSSR);
- aqa = mmio_read32(bar + NVME_REG_AQA);
- asq = mmio_read64(bar + NVME_REG_ASQ);
- acq = mmio_read64(bar + NVME_REG_ACQ);
- cmbloc = mmio_read32(bar + NVME_REG_CMBLOC);
- cmbsz = mmio_read32(bar + NVME_REG_CMBSZ);
- bpinfo = mmio_read32(bar + NVME_REG_BPINFO);
- bprsel = mmio_read32(bar + NVME_REG_BPRSEL);
- bpmbl = mmio_read64(bar + NVME_REG_BPMBL);
- cmbmsc = mmio_read64(bar + NVME_REG_CMBMSC);
- cmbsts = mmio_read32(bar + NVME_REG_CMBSTS);
- pmrcap = mmio_read32(bar + NVME_REG_PMRCAP);
- pmrctl = mmio_read32(bar + NVME_REG_PMRCTL);
- pmrsts = mmio_read32(bar + NVME_REG_PMRSTS);
- pmrebs = mmio_read32(bar + NVME_REG_PMREBS);
- pmrswtp = mmio_read32(bar + NVME_REG_PMRSWTP);
- pmrmsc = mmio_read64(bar + NVME_REG_PMRMSC);
-
- root = json_create_object();
- json_object_add_value_uint(root, "cap", cap);
- json_object_add_value_int(root, "vs", vs);
- json_object_add_value_int(root, "intms", intms);
- json_object_add_value_int(root, "intmc", intmc);
- json_object_add_value_int(root, "cc", cc);
- json_object_add_value_int(root, "csts", csts);
- json_object_add_value_int(root, "nssr", nssr);
- json_object_add_value_int(root, "aqa", aqa);
- json_object_add_value_uint(root, "asq", asq);
- json_object_add_value_uint(root, "acq", acq);
- json_object_add_value_int(root, "cmbloc", cmbloc);
- json_object_add_value_int(root, "cmbsz", cmbsz);
- json_object_add_value_int(root, "bpinfo", bpinfo);
- json_object_add_value_int(root, "bprsel", bprsel);
- json_object_add_value_uint(root, "bpmbl", bpmbl);
- json_object_add_value_uint(root, "cmbmsc", cmbmsc);
- json_object_add_value_int(root, "cmbsts", cmbsts);
- json_object_add_value_int(root, "pmrcap", pmrcap);
- json_object_add_value_int(root, "pmrctl", pmrctl);
- json_object_add_value_int(root, "pmrsts", pmrsts);
- json_object_add_value_int(root, "pmrebs", pmrebs);
- json_object_add_value_int(root, "pmrswtp", pmrswtp);
- json_object_add_value_uint(root, "pmrmsc", pmrmsc);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
-{
- const unsigned int reg_size = 0x50; /* 00h to 4Fh */
- uint64_t cap, asq, acq, bpmbl, cmbmsc, pmrmsc;
- uint32_t vs, intms, intmc, cc, csts, nssr, aqa, cmbsz, cmbloc, bpinfo,
- bprsel, cmbsts, pmrcap, pmrctl, pmrsts, pmrebs, pmrswtp;
- int human = flags & VERBOSE;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)bar, reg_size);
- if (flags & JSON)
- return json_ctrl_registers(bar);
-
- cap = mmio_read64(bar + NVME_REG_CAP);
- vs = mmio_read32(bar + NVME_REG_VS);
- intms = mmio_read32(bar + NVME_REG_INTMS);
- intmc = mmio_read32(bar + NVME_REG_INTMC);
- cc = mmio_read32(bar + NVME_REG_CC);
- csts = mmio_read32(bar + NVME_REG_CSTS);
- nssr = mmio_read32(bar + NVME_REG_NSSR);
- aqa = mmio_read32(bar + NVME_REG_AQA);
- asq = mmio_read64(bar + NVME_REG_ASQ);
- acq = mmio_read64(bar + NVME_REG_ACQ);
- cmbloc = mmio_read32(bar + NVME_REG_CMBLOC);
- cmbsz = mmio_read32(bar + NVME_REG_CMBSZ);
- bpinfo = mmio_read32(bar + NVME_REG_BPINFO);
- bprsel = mmio_read32(bar + NVME_REG_BPRSEL);
- bpmbl = mmio_read64(bar + NVME_REG_BPMBL);
- cmbmsc = mmio_read64(bar + NVME_REG_CMBMSC);
- cmbsts = mmio_read32(bar + NVME_REG_CMBSTS);
- pmrcap = mmio_read32(bar + NVME_REG_PMRCAP);
- pmrctl = mmio_read32(bar + NVME_REG_PMRCTL);
- pmrsts = mmio_read32(bar + NVME_REG_PMRSTS);
- pmrebs = mmio_read32(bar + NVME_REG_PMREBS);
- pmrswtp = mmio_read32(bar + NVME_REG_PMRSWTP);
- pmrmsc = mmio_read64(bar + NVME_REG_PMRMSC);
-
- if (human) {
- if (cap != 0xffffffff) {
- printf("cap : %"PRIx64"\n", cap);
- nvme_show_registers_cap(cap);
- }
- if (vs != 0xffffffff) {
- printf("version : %x\n", vs);
- nvme_show_registers_version(vs);
- }
- if (cc != 0xffffffff) {
- printf("cc : %x\n", cc);
- nvme_show_registers_cc(cc);
- }
- if (csts != 0xffffffff) {
- printf("csts : %x\n", csts);
- nvme_show_registers_csts(csts);
- }
- if (nssr != 0xffffffff) {
- printf("nssr : %x\n", nssr);
- printf("\tNVM Subsystem Reset Control (NSSRC): %u\n\n",
- nssr);
- }
- if (!fabrics) {
- printf("intms : %x\n", intms);
- printf("\tInterrupt Vector Mask Set (IVMS): %x\n\n",
- intms);
-
- printf("intmc : %x\n", intmc);
- printf("\tInterrupt Vector Mask Clear (IVMC): %x\n\n",
- intmc);
- printf("aqa : %x\n", aqa);
- nvme_show_registers_aqa(aqa);
-
- printf("asq : %"PRIx64"\n", asq);
- printf("\tAdmin Submission Queue Base (ASQB): %"PRIx64"\n\n",
- asq);
-
- printf("acq : %"PRIx64"\n", acq);
- printf("\tAdmin Completion Queue Base (ACQB): %"PRIx64"\n\n",
- acq);
-
- printf("cmbloc : %x\n", cmbloc);
- nvme_show_registers_cmbloc(cmbloc, cmbsz);
-
- printf("cmbsz : %x\n", cmbsz);
- nvme_show_registers_cmbsz(cmbsz);
-
- printf("bpinfo : %x\n", bpinfo);
- nvme_show_registers_bpinfo(bpinfo);
-
- printf("bprsel : %x\n", bprsel);
- nvme_show_registers_bprsel(bprsel);
-
- printf("bpmbl : %"PRIx64"\n", bpmbl);
- nvme_show_registers_bpmbl(bpmbl);
-
- printf("cmbmsc : %"PRIx64"\n", cmbmsc);
- nvme_show_registers_cmbmsc(cmbmsc);
-
- printf("cmbsts : %x\n", cmbsts);
- nvme_show_registers_cmbsts(cmbsts);
-
- printf("pmrcap : %x\n", pmrcap);
- nvme_show_registers_pmrcap(pmrcap);
-
- printf("pmrctl : %x\n", pmrctl);
- nvme_show_registers_pmrctl(pmrctl);
-
- printf("pmrsts : %x\n", pmrsts);
- nvme_show_registers_pmrsts(pmrsts, pmrctl);
-
- printf("pmrebs : %x\n", pmrebs);
- nvme_show_registers_pmrebs(pmrebs);
-
- printf("pmrswtp : %x\n", pmrswtp);
- nvme_show_registers_pmrswtp(pmrswtp);
-
- printf("pmrmsc : %"PRIx64"\n", pmrmsc);
- nvme_show_registers_pmrmsc(pmrmsc);
- }
- } else {
- if (cap != 0xffffffff)
- printf("cap : %"PRIx64"\n", cap);
- if (vs != 0xffffffff)
- printf("version : %x\n", vs);
- if (cc != 0xffffffff)
- printf("cc : %x\n", cc);
- if (csts != 0xffffffff)
- printf("csts : %x\n", csts);
- if (nssr != 0xffffffff)
- printf("nssr : %x\n", nssr);
- if (!fabrics) {
- printf("intms : %x\n", intms);
- printf("intmc : %x\n", intmc);
- printf("aqa : %x\n", aqa);
- printf("asq : %"PRIx64"\n", asq);
- printf("acq : %"PRIx64"\n", acq);
- printf("cmbloc : %x\n", cmbloc);
- printf("cmbsz : %x\n", cmbsz);
- printf("bpinfo : %x\n", bpinfo);
- printf("bprsel : %x\n", bprsel);
- printf("bpmbl : %"PRIx64"\n", bpmbl);
- printf("cmbmsc : %"PRIx64"\n", cmbmsc);
- printf("cmbsts : %x\n", cmbsts);
- printf("pmrcap : %x\n", pmrcap);
- printf("pmrctl : %x\n", pmrctl);
- printf("pmrsts : %x\n", pmrsts);
- printf("pmrebs : %x\n", pmrebs);
- printf("pmrswtp : %x\n", pmrswtp);
- printf("pmrmsc : %"PRIx64"\n", pmrmsc);
- }
- }
-}
-
-void nvme_show_single_property(int offset, uint64_t value64, int human)
-{
- uint32_t value32;
-
- if (!human) {
- if (nvme_is_64bit_reg(offset))
- printf("property: 0x%02x (%s), value: %"PRIx64"\n",
- offset, nvme_register_to_string(offset),
- value64);
- else
- printf("property: 0x%02x (%s), value: %x\n", offset,
- nvme_register_to_string(offset),
- (uint32_t) value64);
-
- return;
- }
-
- value32 = (uint32_t) value64;
-
- switch (offset) {
- case NVME_REG_CAP:
- printf("cap : %"PRIx64"\n", value64);
- nvme_show_registers_cap(value64);
- break;
-
- case NVME_REG_VS:
- printf("version : %x\n", value32);
- nvme_show_registers_version(value32);
- break;
-
- case NVME_REG_CC:
- printf("cc : %x\n", value32);
- nvme_show_registers_cc(value32);
- break;
-
- case NVME_REG_CSTS:
- printf("csts : %x\n", value32);
- nvme_show_registers_csts(value32);
- break;
-
- case NVME_REG_NSSR:
- printf("nssr : %x\n", value32);
- printf("\tNVM Subsystem Reset Control (NSSRC): %u\n\n",
- value32);
- break;
-
- default:
- printf("unknown property: 0x%02x (%s), value: %"PRIx64"\n",
- offset, nvme_register_to_string(offset), value64);
- break;
- }
-}
-
-void nvme_show_relatives(const char *name)
-{
-#if 0
- unsigned id, i, nsid = NVME_NSID_ALL;
- char *path = NULL;
- bool block = true;
- int ret;
-
- ret = sscanf(name, "nvme%dn%d", &id, &nsid);
- switch (ret) {
- case 1:
- if (asprintf(&path, "/sys/class/nvme/%s", name) < 0)
- path = NULL;
- block = false;
- break;
- case 2:
- if (asprintf(&path, "/sys/block/%s/device", name) < 0)
- path = NULL;
- break;
- default:
- return;
- }
-
- if (!path)
- return;
-
- if (block) {
- struct nvme_root r = { };
- char *subsysnqn;
- int err;
-
- subsysnqn = get_nvme_subsnqn(path);
- if (!subsysnqn) {
- free(path);
- return;
- }
- err = scan_subsystems(&r, subsysnqn, 0);
- if (err || r.nr_subsystems != 1) {
- free(subsysnqn);
- free(path);
- return;
- }
-
- fprintf(stderr, "Namespace %s has parent controller(s):", name);
- for (i = 0; i < r.subsystems[0].nr_ctrls; i++)
- fprintf(stderr, "%s%s", i ? ", " : "",
- t.subsystems[0].ctrls[i].name);
- fprintf(stderr, "\n\n");
- free(subsysnqn);
- nvme_free_tree(&r);
- } else {
- struct dirent **paths;
- bool comma = false;
- int n, ns, cntlid;
-
- n = scandir(path, &paths, scan_ctrl_paths_filter, alphasort);
- if (n < 0) {
- free(path);
- return;
- }
-
- fprintf(stderr, "Controller %s has child namespace(s):", name);
- for (i = 0; i < n; i++) {
- if (sscanf(paths[i]->d_name, "nvme%dc%dn%d",
- &id, &cntlid, &ns) != 3) {
- if (sscanf(paths[i]->d_name, "nvme%dn%d",
- &id, &ns) != 2) {
- continue;
- }
- }
- fprintf(stderr, "%snvme%dn%d", comma ? ", " : "", id,
- ns);
- comma = true;
- }
- fprintf(stderr, "\n\n");
- free(paths);
- }
- free(path);
-#endif
-}
-
-void d(unsigned char *buf, int len, int width, int group)
-{
- int i, offset = 0, line_done = 0;
- char ascii[32 + 1];
-
- assert(width < sizeof(ascii));
- printf(" ");
- for (i = 0; i <= 15; i++)
- printf("%3x", i);
- for (i = 0; i < len; i++) {
- line_done = 0;
- if (i % width == 0)
- printf( "\n%04x:", offset);
- if (i % group == 0)
- printf( " %02x", buf[i]);
- else
- printf( "%02x", buf[i]);
- ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
- if (((i + 1) % width) == 0) {
- ascii[i % width + 1] = '\0';
- printf( " \"%.*s\"", width, ascii);
- offset += width;
- line_done = 1;
- }
- }
- if (!line_done) {
- unsigned b = width - (i % width);
- ascii[i % width + 1] = '\0';
- printf( " %*s \"%.*s\"",
- 2 * b + b / group + (b % group ? 1 : 0), "",
- width, ascii);
- }
- printf( "\n");
-}
-
-void d_raw(unsigned char *buf, unsigned len)
-{
- unsigned i;
- for (i = 0; i < len; i++)
- putchar(*(buf+i));
-}
-
-#if 0
-static void format(char *formatter, size_t fmt_sz, char *tofmt, size_t tofmtsz)
-{
-
- fmt_sz = snprintf(formatter,fmt_sz, "%-*.*s",
- (int)tofmtsz, (int)tofmtsz, tofmt);
- /* trim() the obnoxious trailing white lines */
- while (fmt_sz) {
- if (formatter[fmt_sz - 1] != ' ' && formatter[fmt_sz - 1] != '\0') {
- formatter[fmt_sz] = '\0';
- break;
- }
- fmt_sz--;
- }
-}
-#endif
-
-static const char *nvme_uuid_to_string(uuid_t uuid)
-{
- /* large enough to hold uuid str (37) + null-termination byte */
- static char uuid_str[40];
-#ifdef LIBUUID
- uuid_unparse_lower(uuid, uuid_str);
-#else
- static const char *hex_digits = "0123456789abcdef";
- char *p = &uuid_str[0];
- int i;
-
- for (i = 0; i < 16; i++) {
- *p++ = hex_digits[(uuid.b[i] & 0xf0) >> 4];
- *p++ = hex_digits[uuid.b[i] & 0x0f];
- if (i == 3 || i == 5 || i == 7 || i == 9)
- *p++ = '-';
- }
- *p = '\0';
-#endif
- return uuid_str;
-}
-
-static void nvme_show_id_ctrl_cmic(__u8 cmic)
-{
- __u8 rsvd = (cmic & 0xF0) >> 4;
- __u8 ana = (cmic & 0x8) >> 3;
- __u8 sriov = (cmic & 0x4) >> 2;
- __u8 mctl = (cmic & 0x2) >> 1;
- __u8 mp = cmic & 0x1;
-
- if (rsvd)
- printf(" [7:4] : %#x\tReserved\n", rsvd);
- printf(" [3:3] : %#x\tANA %ssupported\n", ana, ana ? "" : "not ");
- printf(" [2:2] : %#x\t%s\n", sriov, sriov ? "SR-IOV" : "PCI");
- printf(" [1:1] : %#x\t%s Controller\n",
- mctl, mctl ? "Multi" : "Single");
- printf(" [0:0] : %#x\t%s Port\n", mp, mp ? "Multi" : "Single");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_oaes(__le32 ctrl_oaes)
-{
- __u32 oaes = le32_to_cpu(ctrl_oaes);
- __u32 rsvd0 = (oaes & 0xFFFF8000) >> 15;
- __u32 nace = (oaes & 0x100) >> 8;
- __u32 fan = (oaes & 0x200) >> 9;
- __u32 anacn = (oaes & 800) >> 11;
- __u32 plealcn = (oaes & 0x1000) >> 12;
- __u32 lbasin = (oaes & 0x2000) >> 13;
- __u32 egealpcn = (oaes & 0x4000) >> 14;
- __u32 rsvd1 = oaes & 0xFF;
-
- if (rsvd0)
- printf(" [31:10] : %#x\tReserved\n", rsvd0);
- printf("[14:14] : %#x\tEndurance Group Event Aggregate Log Page"\
- " Change Notice %sSupported\n",
- egealpcn, egealpcn ? "" : "Not ");
- printf("[13:13] : %#x\tLBA Status Information Notices %sSupported\n",
- lbasin, lbasin ? "" : "Not ");
- printf("[12:12] : %#x\tPredictable Latency Event Aggregate Log Change"\
- " Notices %sSupported\n",
- plealcn, plealcn ? "" : "Not ");
- printf("[11:11] : %#x\tAsymmetric Namespace Access Change Notices"\
- " %sSupported\n", anacn, anacn ? "" : "Not ");
- printf(" [9:9] : %#x\tFirmware Activation Notices %sSupported\n",
- fan, fan ? "" : "Not ");
- printf(" [8:8] : %#x\tNamespace Attribute Changed Event %sSupported\n",
- nace, nace ? "" : "Not ");
- if (rsvd1)
- printf(" [7:0] : %#x\tReserved\n", rsvd1);
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_ctratt(__le32 ctrl_ctratt)
-{
- __u32 ctratt = le32_to_cpu(ctrl_ctratt);
- __u32 rsvd = ctratt >> 10;
- __u32 hostid128 = (ctratt & NVME_CTRL_CTRATT_128_ID) >> 0;
- __u32 psp = (ctratt & NVME_CTRL_CTRATT_NON_OP_PSP) >> 1;
- __u32 sets = (ctratt & NVME_CTRL_CTRATT_NVM_SETS) >> 2;
- __u32 rrl = (ctratt & NVME_CTRL_CTRATT_READ_RECV_LVLS) >> 3;
- __u32 eg = (ctratt & NVME_CTRL_CTRATT_ENDURANCE_GROUPS) >> 4;
- __u32 iod = (ctratt & NVME_CTRL_CTRATT_PREDICTABLE_LAT) >> 5;
- __u32 ng = (ctratt & NVME_CTRL_CTRATT_NAMESPACE_GRANULARITY) >> 7;
- __u32 uuidlist = (ctratt & NVME_CTRL_CTRATT_UUID_LIST) >> 9;
- __u32 rsvd6 = (ctratt & 0x00000040) >> 6;
- __u32 rsvd8 = (ctratt & 0x00000100) >> 8;
-
- if (rsvd)
- printf(" [31:10] : %#x\tReserved\n", rsvd);
-
- printf(" [9:9] : %#x\tUUID List %sSupported\n",
- uuidlist, uuidlist ? "" : "Not ");
- if (rsvd8)
- printf(" [8:8] : %#x\tReserved\n", rsvd8);
- printf(" [7:7] : %#x\tNamespace Granularity %sSupported\n",
- ng, ng ? "" : "Not ");
- if (rsvd6)
- printf(" [6:6] : %#x\tReserved\n", rsvd6);
- printf(" [5:5] : %#x\tPredictable Latency Mode %sSupported\n",
- iod, iod ? "" : "Not ");
- printf(" [4:4] : %#x\tEndurance Groups %sSupported\n",
- eg, eg ? "" : "Not ");
- printf(" [3:3] : %#x\tRead Recovery Levels %sSupported\n",
- rrl, rrl ? "" : "Not ");
- printf(" [2:2] : %#x\tNVM Sets %sSupported\n",
- sets, sets ? "" : "Not ");
- printf(" [1:1] : %#x\tNon-Operational Power State Permissive %sSupported\n",
- psp, psp ? "" : "Not ");
- printf(" [0:0] : %#x\t128-bit Host Identifier %sSupported\n",
- hostid128, hostid128 ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_cntrltype(__u8 cntrltype)
-{
- __u8 rsvd = (cntrltype & 0xFC) >> 2;
- __u8 cntrl = cntrltype & 0x3;
-
- static const char *type[] = {
- "Controller type not reported",
- "I/O Controller",
- "Discovery Controller",
- "Administrative Controller"
- };
-
- printf(" [7:2] : %#x\tReserved\n", rsvd);
- printf(" [1:0] : %#x\t%s\n", cntrltype, type[cntrl]);
-}
-
-static void nvme_show_id_ctrl_oacs(__le16 ctrl_oacs)
-{
- __u16 oacs = le16_to_cpu(ctrl_oacs);
- __u16 rsvd = (oacs & 0xFC00) >> 10;
- __u16 glbas = (oacs & 0x200) >> 9;
- __u16 dbc = (oacs & 0x100) >> 8;
- __u16 vir = (oacs & 0x80) >> 7;
- __u16 nmi = (oacs & 0x40) >> 6;
- __u16 dir = (oacs & 0x20) >> 5;
- __u16 sft = (oacs & 0x10) >> 4;
- __u16 nsm = (oacs & 0x8) >> 3;
- __u16 fwc = (oacs & 0x4) >> 2;
- __u16 fmt = (oacs & 0x2) >> 1;
- __u16 sec = oacs & 0x1;
-
- if (rsvd)
- printf(" [15:9] : %#x\tReserved\n", rsvd);
- printf(" [9:9] : %#x\tGet LBA Status Capability %sSupported\n",
- glbas, glbas ? "" : "Not ");
- printf(" [8:8] : %#x\tDoorbell Buffer Config %sSupported\n",
- dbc, dbc ? "" : "Not ");
- printf(" [7:7] : %#x\tVirtualization Management %sSupported\n",
- vir, vir ? "" : "Not ");
- printf(" [6:6] : %#x\tNVMe-MI Send and Receive %sSupported\n",
- nmi, nmi ? "" : "Not ");
- printf(" [5:5] : %#x\tDirectives %sSupported\n",
- dir, dir ? "" : "Not ");
- printf(" [4:4] : %#x\tDevice Self-test %sSupported\n",
- sft, sft ? "" : "Not ");
- printf(" [3:3] : %#x\tNS Management and Attachment %sSupported\n",
- nsm, nsm ? "" : "Not ");
- printf(" [2:2] : %#x\tFW Commit and Download %sSupported\n",
- fwc, fwc ? "" : "Not ");
- printf(" [1:1] : %#x\tFormat NVM %sSupported\n",
- fmt, fmt ? "" : "Not ");
- printf(" [0:0] : %#x\tSecurity Send and Receive %sSupported\n",
- sec, sec ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_frmw(__u8 frmw)
-{
- __u8 rsvd = (frmw & 0xE0) >> 5;
- __u8 fawr = (frmw & 0x10) >> 4;
- __u8 nfws = (frmw & 0xE) >> 1;
- __u8 s1ro = frmw & 0x1;
-
- if (rsvd)
- printf(" [7:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tFirmware Activate Without Reset %sSupported\n",
- fawr, fawr ? "" : "Not ");
- printf(" [3:1] : %#x\tNumber of Firmware Slots\n", nfws);
- printf(" [0:0] : %#x\tFirmware Slot 1 Read%s\n",
- s1ro, s1ro ? "-Only" : "/Write");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_lpa(__u8 lpa)
-{
- __u8 rsvd = (lpa & 0xE0) >> 5;
- __u8 persevnt = (lpa & 0x10) >> 4;
- __u8 telem = (lpa & 0x8) >> 3;
- __u8 ed = (lpa & 0x4) >> 2;
- __u8 celp = (lpa & 0x2) >> 1;
- __u8 smlp = lpa & 0x1;
-
- if (rsvd)
- printf(" [7:4] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tPersistent Event log %sSupported\n",
- persevnt, persevnt ? "" : "Not ");
- printf(" [3:3] : %#x\tTelemetry host/controller initiated log page %sSupported\n",
- telem, telem ? "" : "Not ");
- printf(" [2:2] : %#x\tExtended data for Get Log Page %sSupported\n",
- ed, ed ? "" : "Not ");
- printf(" [1:1] : %#x\tCommand Effects Log Page %sSupported\n",
- celp, celp ? "" : "Not ");
- printf(" [0:0] : %#x\tSMART/Health Log Page per NS %sSupported\n",
- smlp, smlp ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_avscc(__u8 avscc)
-{
- __u8 rsvd = (avscc & 0xFE) >> 1;
- __u8 fmt = avscc & 0x1;
- if (rsvd)
- printf(" [7:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tAdmin Vendor Specific Commands uses %s Format\n",
- fmt, fmt ? "NVMe" : "Vendor Specific");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_apsta(__u8 apsta)
-{
- __u8 rsvd = (apsta & 0xFE) >> 1;
- __u8 apst = apsta & 0x1;
- if (rsvd)
- printf(" [7:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tAutonomous Power State Transitions %sSupported\n",
- apst, apst ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_rpmbs(__le32 ctrl_rpmbs)
-{
- __u32 rpmbs = le32_to_cpu(ctrl_rpmbs);
- __u32 asz = (rpmbs & 0xFF000000) >> 24;
- __u32 tsz = (rpmbs & 0xFF0000) >> 16;
- __u32 rsvd = (rpmbs & 0xFFC0) >> 6;
- __u32 auth = (rpmbs & 0x38) >> 3;
- __u32 rpmb = rpmbs & 0x7;
-
- printf(" [31:24]: %#x\tAccess Size\n", asz);
- printf(" [23:16]: %#x\tTotal Size\n", tsz);
- if (rsvd)
- printf(" [15:6] : %#x\tReserved\n", rsvd);
- printf(" [5:3] : %#x\tAuthentication Method\n", auth);
- printf(" [2:0] : %#x\tNumber of RPMB Units\n", rpmb);
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_hctma(__le16 ctrl_hctma)
-{
- __u16 hctma = le16_to_cpu(ctrl_hctma);
- __u16 rsvd = (hctma & 0xFFFE) >> 1;
- __u16 hctm = hctma & 0x1;
-
- if (rsvd)
- printf(" [15:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tHost Controlled Thermal Management %sSupported\n",
- hctm, hctm ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_sanicap(__le32 ctrl_sanicap)
-{
- __u32 sanicap = le32_to_cpu(ctrl_sanicap);
- __u32 rsvd = (sanicap & 0x1FFFFFF8) >> 3;
- __u32 owr = (sanicap & 0x4) >> 2;
- __u32 ber = (sanicap & 0x2) >> 1;
- __u32 cer = sanicap & 0x1;
- __u32 ndi = (sanicap & 0x20000000) >> 29;
- __u32 nodmmas = (sanicap & 0xC0000000) >> 30;
-
- static const char *modifies_media[] = {
- "Additional media modification after sanitize operation completes successfully is not defined",
- "Media is not additionally modified after sanitize operation completes successfully",
- "Media is additionally modified after sanitize operation completes successfully",
- "Reserved"
- };
-
- printf(" [31:30] : %#x\t%s\n", nodmmas, modifies_media[nodmmas]);
- printf(" [29:29] : %#x\tNo-Deallocate After Sanitize bit in Sanitize command %sSupported\n",
- ndi, ndi ? "Not " : "");
- if (rsvd)
- printf(" [28:3] : %#x\tReserved\n", rsvd);
- printf(" [2:2] : %#x\tOverwrite Sanitize Operation %sSupported\n",
- owr, owr ? "" : "Not ");
- printf(" [1:1] : %#x\tBlock Erase Sanitize Operation %sSupported\n",
- ber, ber ? "" : "Not ");
- printf(" [0:0] : %#x\tCrypto Erase Sanitize Operation %sSupported\n",
- cer, cer ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_anacap(__u8 anacap)
-{
- __u8 nz = (anacap & 0x80) >> 7;
- __u8 grpid_change = (anacap & 0x40) >> 6;
- __u8 rsvd = (anacap & 0x20) >> 5;
- __u8 ana_change = (anacap & 0x10) >> 4;
- __u8 ana_persist_loss = (anacap & 0x08) >> 3;
- __u8 ana_inaccessible = (anacap & 0x04) >> 2;
- __u8 ana_nonopt = (anacap & 0x02) >> 1;
- __u8 ana_opt = (anacap & 0x01);
-
- printf(" [7:7] : %#x\tNon-zero group ID %sSupported\n",
- nz, nz ? "" : "Not ");
- printf(" [6:6] : %#x\tGroup ID does %schange\n",
- grpid_change, grpid_change ? "" : "not ");
- if (rsvd)
- printf(" [5:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tANA Change state %sSupported\n",
- ana_change, ana_change ? "" : "Not ");
- printf(" [3:3] : %#x\tANA Persistent Loss state %sSupported\n",
- ana_persist_loss, ana_persist_loss ? "" : "Not ");
- printf(" [2:2] : %#x\tANA Inaccessible state %sSupported\n",
- ana_inaccessible, ana_inaccessible ? "" : "Not ");
- printf(" [1:1] : %#x\tANA Non-optimized state %sSupported\n",
- ana_nonopt, ana_nonopt ? "" : "Not ");
- printf(" [0:0] : %#x\tANA Optimized state %sSupported\n",
- ana_opt, ana_opt ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_sqes(__u8 sqes)
-{
- __u8 msqes = (sqes & 0xF0) >> 4;
- __u8 rsqes = sqes & 0xF;
- printf(" [7:4] : %#x\tMax SQ Entry Size (%d)\n", msqes, 1 << msqes);
- printf(" [3:0] : %#x\tMin SQ Entry Size (%d)\n", rsqes, 1 << rsqes);
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_cqes(__u8 cqes)
-{
- __u8 mcqes = (cqes & 0xF0) >> 4;
- __u8 rcqes = cqes & 0xF;
- printf(" [7:4] : %#x\tMax CQ Entry Size (%d)\n", mcqes, 1 << mcqes);
- printf(" [3:0] : %#x\tMin CQ Entry Size (%d)\n", rcqes, 1 << rcqes);
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_oncs(__le16 ctrl_oncs)
-{
- __u16 oncs = le16_to_cpu(ctrl_oncs);
- __u16 rsvd = (oncs & 0xFF00) >> 8;
- __u16 vrfy = (oncs & 0x80) >> 7;
- __u16 tmst = (oncs & 0x40) >> 6;
- __u16 resv = (oncs & 0x20) >> 5;
- __u16 save = (oncs & 0x10) >> 4;
- __u16 wzro = (oncs & 0x8) >> 3;
- __u16 dsms = (oncs & 0x4) >> 2;
- __u16 wunc = (oncs & 0x2) >> 1;
- __u16 cmp = oncs & 0x1;
-
- if (rsvd)
- printf(" [15:8] : %#x\tReserved\n", rsvd);
- printf(" [7:7] : %#x\tVerify %sSupported\n",
- vrfy, vrfy ? "" : "Not ");
- printf(" [6:6] : %#x\tTimestamp %sSupported\n",
- tmst, tmst ? "" : "Not ");
- printf(" [5:5] : %#x\tReservations %sSupported\n",
- resv, resv ? "" : "Not ");
- printf(" [4:4] : %#x\tSave and Select %sSupported\n",
- save, save ? "" : "Not ");
- printf(" [3:3] : %#x\tWrite Zeroes %sSupported\n",
- wzro, wzro ? "" : "Not ");
- printf(" [2:2] : %#x\tData Set Management %sSupported\n",
- dsms, dsms ? "" : "Not ");
- printf(" [1:1] : %#x\tWrite Uncorrectable %sSupported\n",
- wunc, wunc ? "" : "Not ");
- printf(" [0:0] : %#x\tCompare %sSupported\n",
- cmp, cmp ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_fuses(__le16 ctrl_fuses)
-{
- __u16 fuses = le16_to_cpu(ctrl_fuses);
- __u16 rsvd = (fuses & 0xFE) >> 1;
- __u16 cmpw = fuses & 0x1;
-
- if (rsvd)
- printf(" [15:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tFused Compare and Write %sSupported\n",
- cmpw, cmpw ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_fna(__u8 fna)
-{
- __u8 rsvd = (fna & 0xF8) >> 3;
- __u8 cese = (fna & 0x4) >> 2;
- __u8 cens = (fna & 0x2) >> 1;
- __u8 fmns = fna & 0x1;
- if (rsvd)
- printf(" [7:3] : %#x\tReserved\n", rsvd);
- printf(" [2:2] : %#x\tCrypto Erase %sSupported as part of Secure Erase\n",
- cese, cese ? "" : "Not ");
- printf(" [1:1] : %#x\tCrypto Erase Applies to %s Namespace(s)\n",
- cens, cens ? "All" : "Single");
- printf(" [0:0] : %#x\tFormat Applies to %s Namespace(s)\n",
- fmns, fmns ? "All" : "Single");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_vwc(__u8 vwc)
-{
- __u8 rsvd = (vwc & 0xF8) >> 3;
- __u8 flush = (vwc & 0x6) >> 1;
- __u8 vwcp = vwc & 0x1;
-
- static const char *flush_behavior[] = {
- "Support for the NSID field set to FFFFFFFFh is not indicated",
- "Reserved",
- "The Flush command does not support NSID set to FFFFFFFFh",
- "The Flush command supports NSID set to FFFFFFFFh"
- };
-
- if (rsvd)
- printf(" [7:3] : %#x\tReserved\n", rsvd);
- printf(" [2:1] : %#x\t%s\n", flush, flush_behavior[flush]);
- printf(" [0:0] : %#x\tVolatile Write Cache %sPresent\n",
- vwcp, vwcp ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_nvscc(__u8 nvscc)
-{
- __u8 rsvd = (nvscc & 0xFE) >> 1;
- __u8 fmt = nvscc & 0x1;
- if (rsvd)
- printf(" [7:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tNVM Vendor Specific Commands uses %s Format\n",
- fmt, fmt ? "NVMe" : "Vendor Specific");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_nwpc(__u8 nwpc)
-{
- __u8 no_wp_wp = (nwpc & 0x01);
- __u8 wp_power_cycle = (nwpc & 0x02) >> 1;
- __u8 wp_permanent = (nwpc & 0x04) >> 2;
- __u8 rsvd = (nwpc & 0xF8) >> 3;
-
- if (rsvd)
- printf(" [7:3] : %#x\tReserved\n", rsvd);
-
- printf(" [2:2] : %#x\tPermanent Write Protect %sSupported\n",
- wp_permanent, wp_permanent ? "" : "Not ");
- printf(" [1:1] : %#x\tWrite Protect Until Power Supply %sSupported\n",
- wp_power_cycle, wp_power_cycle ? "" : "Not ");
- printf(" [0:0] : %#x\tNo Write Protect and Write Protect Namespace %sSupported\n",
- no_wp_wp, no_wp_wp ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_sgls(__le32 ctrl_sgls)
-{
- __u32 sgls = le32_to_cpu(ctrl_sgls);
- __u32 rsvd0 = (sgls & 0xFFC00000) >> 22;
- __u32 trsdbd = (sgls & 0x200000) >> 21;
- __u32 aofdsl = (sgls & 0x100000) >> 20;
- __u32 mpcsd = (sgls & 0x80000) >> 19;
- __u32 sglltb = (sgls & 0x40000) >> 18;
- __u32 bacmdb = (sgls & 0x20000) >> 17;
- __u32 bbs = (sgls & 0x10000) >> 16;
- __u32 rsvd1 = (sgls & 0xFFF8) >> 3;
- __u32 key = (sgls & 0x4) >> 2;
- __u32 sglsp = sgls & 0x3;
-
- if (rsvd0)
- printf(" [31:22]: %#x\tReserved\n", rsvd0);
- if (sglsp || (!sglsp && trsdbd))
- printf(" [21:21]: %#x\tTransport SGL Data Block Descriptor %sSupported\n",
- trsdbd, trsdbd ? "" : "Not ");
- if (sglsp || (!sglsp && aofdsl))
- printf(" [20:20]: %#x\tAddress Offsets %sSupported\n",
- aofdsl, aofdsl ? "" : "Not ");
- if (sglsp || (!sglsp && mpcsd))
- printf(" [19:19]: %#x\tMetadata Pointer Containing "
- "SGL Descriptor is %sSupported\n",
- mpcsd, mpcsd ? "" : "Not ");
- if (sglsp || (!sglsp && sglltb))
- printf(" [18:18]: %#x\tSGL Length Larger than Buffer %sSupported\n",
- sglltb, sglltb ? "" : "Not ");
- if (sglsp || (!sglsp && bacmdb))
- printf(" [17:17]: %#x\tByte-Aligned Contig. MD Buffer %sSupported\n",
- bacmdb, bacmdb ? "" : "Not ");
- if (sglsp || (!sglsp && bbs))
- printf(" [16:16]: %#x\tSGL Bit-Bucket %sSupported\n",
- bbs, bbs ? "" : "Not ");
- if (rsvd1)
- printf(" [15:3] : %#x\tReserved\n", rsvd1);
- if (sglsp || (!sglsp && key))
- printf(" [2:2] : %#x\tKeyed SGL Data Block descriptor %sSupported\n",
- key, key ? "" : "Not ");
- if (sglsp == 0x3)
- printf(" [1:0] : %#x\tReserved\n", sglsp);
- else if (sglsp == 0x2)
- printf(" [1:0] : %#x\tScatter-Gather Lists Supported."
- " Dword alignment required.\n", sglsp);
- else if (sglsp == 0x1)
- printf(" [1:0] : %#x\tScatter-Gather Lists Supported."
- " No Dword alignment required.\n", sglsp);
- else
- printf(" [1:0] : %#x\tScatter-Gather Lists Not Supported\n", sglsp);
- printf("\n");
-}
-
-static void nvme_show_id_ctrl_fcatt(__u8 fcatt)
-{
- __u8 rsvd = (fcatt & 0xFE) >> 1;
- __u8 scm = fcatt & 0x1;
- if (rsvd)
- printf(" [7:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\t%s Controller Model\n",
- scm, scm ? "Static" : "Dynamic");
- printf("\n");
-}
-
-static void nvme_show_id_ns_nsfeat(__u8 nsfeat)
-{
- __u8 rsvd = (nsfeat & 0xE0) >> 5;
- __u8 ioopt = (nsfeat & 0x10) >> 4;
- __u8 dulbe = (nsfeat & 0x4) >> 2;
- __u8 na = (nsfeat & 0x2) >> 1;
- __u8 thin = nsfeat & 0x1;
- if (rsvd)
- printf(" [7:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tNPWG, NPWA, NPDG, NPDA, and NOWS are %sSupported\n",
- ioopt, ioopt ? "" : "Not ");
- printf(" [2:2] : %#x\tDeallocated or Unwritten Logical Block error %sSupported\n",
- dulbe, dulbe ? "" : "Not ");
- printf(" [1:1] : %#x\tNamespace uses %s\n",
- na, na ? "NAWUN, NAWUPF, and NACWU" : "AWUN, AWUPF, and ACWU");
- printf(" [0:0] : %#x\tThin Provisioning %sSupported\n",
- thin, thin ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ns_flbas(__u8 flbas)
-{
- __u8 rsvd = (flbas & 0xE0) >> 5;
- __u8 mdedata = (flbas & 0x10) >> 4;
- __u8 lbaf = flbas & 0xF;
- if (rsvd)
- printf(" [7:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tMetadata Transferred %s\n",
- mdedata, mdedata ? "at End of Data LBA" : "in Separate Contiguous Buffer");
- printf(" [3:0] : %#x\tCurrent LBA Format Selected\n", lbaf);
- printf("\n");
-}
-
-static void nvme_show_id_ns_mc(__u8 mc)
-{
- __u8 rsvd = (mc & 0xFC) >> 2;
- __u8 mdp = (mc & 0x2) >> 1;
- __u8 extdlba = mc & 0x1;
- if (rsvd)
- printf(" [7:2] : %#x\tReserved\n", rsvd);
- printf(" [1:1] : %#x\tMetadata Pointer %sSupported\n",
- mdp, mdp ? "" : "Not ");
- printf(" [0:0] : %#x\tMetadata as Part of Extended Data LBA %sSupported\n",
- extdlba, extdlba ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ns_dpc(__u8 dpc)
-{
- __u8 rsvd = (dpc & 0xE0) >> 5;
- __u8 pil8 = (dpc & 0x10) >> 4;
- __u8 pif8 = (dpc & 0x8) >> 3;
- __u8 pit3 = (dpc & 0x4) >> 2;
- __u8 pit2 = (dpc & 0x2) >> 1;
- __u8 pit1 = dpc & 0x1;
- if (rsvd)
- printf(" [7:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tProtection Information Transferred as Last 8 Bytes of Metadata %sSupported\n",
- pil8, pil8 ? "" : "Not ");
- printf(" [3:3] : %#x\tProtection Information Transferred as First 8 Bytes of Metadata %sSupported\n",
- pif8, pif8 ? "" : "Not ");
- printf(" [2:2] : %#x\tProtection Information Type 3 %sSupported\n",
- pit3, pit3 ? "" : "Not ");
- printf(" [1:1] : %#x\tProtection Information Type 2 %sSupported\n",
- pit2, pit2 ? "" : "Not ");
- printf(" [0:0] : %#x\tProtection Information Type 1 %sSupported\n",
- pit1, pit1 ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ns_dps(__u8 dps)
-{
- __u8 rsvd = (dps & 0xF0) >> 4;
- __u8 pif8 = (dps & 0x8) >> 3;
- __u8 pit = dps & 0x7;
- if (rsvd)
- printf(" [7:4] : %#x\tReserved\n", rsvd);
- printf(" [3:3] : %#x\tProtection Information is Transferred as %s 8 Bytes of Metadata\n",
- pif8, pif8 ? "First" : "Last");
- printf(" [2:0] : %#x\tProtection Information %s\n", pit,
- pit == 3 ? "Type 3 Enabled" :
- pit == 2 ? "Type 2 Enabled" :
- pit == 1 ? "Type 1 Enabled" :
- pit == 0 ? "Disabled" : "Reserved Enabled");
- printf("\n");
-}
-
-static void nvme_show_id_ns_nmic(__u8 nmic)
-{
- __u8 rsvd = (nmic & 0xFE) >> 1;
- __u8 mp = nmic & 0x1;
- if (rsvd)
- printf(" [7:1] : %#x\tReserved\n", rsvd);
- printf(" [0:0] : %#x\tNamespace Multipath %sCapable\n",
- mp, mp ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ns_rescap(__u8 rescap)
-{
- __u8 rsvd = (rescap & 0x80) >> 7;
- __u8 eaar = (rescap & 0x40) >> 6;
- __u8 wear = (rescap & 0x20) >> 5;
- __u8 earo = (rescap & 0x10) >> 4;
- __u8 wero = (rescap & 0x8) >> 3;
- __u8 ea = (rescap & 0x4) >> 2;
- __u8 we = (rescap & 0x2) >> 1;
- __u8 ptpl = rescap & 0x1;
- if (rsvd)
- printf(" [7:7] : %#x\tReserved\n", rsvd);
- printf(" [6:6] : %#x\tExclusive Access - All Registrants %sSupported\n",
- eaar, eaar ? "" : "Not ");
- printf(" [5:5] : %#x\tWrite Exclusive - All Registrants %sSupported\n",
- wear, wear ? "" : "Not ");
- printf(" [4:4] : %#x\tExclusive Access - Registrants Only %sSupported\n",
- earo, earo ? "" : "Not ");
- printf(" [3:3] : %#x\tWrite Exclusive - Registrants Only %sSupported\n",
- wero, wero ? "" : "Not ");
- printf(" [2:2] : %#x\tExclusive Access %sSupported\n",
- ea, ea ? "" : "Not ");
- printf(" [1:1] : %#x\tWrite Exclusive %sSupported\n",
- we, we ? "" : "Not ");
- printf(" [0:0] : %#x\tPersist Through Power Loss %sSupported\n",
- ptpl, ptpl ? "" : "Not ");
- printf("\n");
-}
-
-static void nvme_show_id_ns_fpi(__u8 fpi)
-{
- __u8 fpis = (fpi & 0x80) >> 7;
- __u8 fpii = fpi & 0x7F;
- printf(" [7:7] : %#x\tFormat Progress Indicator %sSupported\n",
- fpis, fpis ? "" : "Not ");
- if (fpis || (!fpis && fpii))
- printf(" [6:0] : %#x\tFormat Progress Indicator (Remaining %d%%)\n",
- fpii, fpii);
- printf("\n");
-}
-
-static void nvme_show_id_ns_dlfeat(__u8 dlfeat)
-{
- __u8 rsvd = (dlfeat & 0xE0) >> 5;
- __u8 guard = (dlfeat & 0x10) >> 4;
- __u8 dwz = (dlfeat & 0x8) >> 3;
- __u8 val = dlfeat & 0x7;
- if (rsvd)
- printf(" [7:5] : %#x\tReserved\n", rsvd);
- printf(" [4:4] : %#x\tGuard Field of Deallocated Logical Blocks is set to %s\n",
- guard, guard ? "CRC of The Value Read" : "0xFFFF");
- printf(" [3:3] : %#x\tDeallocate Bit in the Write Zeroes Command is %sSupported\n",
- dwz, dwz ? "" : "Not ");
- printf(" [2:0] : %#x\tBytes Read From a Deallocated Logical Block and its Metadata are %s\n",
- val, val == 2 ? "0xFF" :
- val == 1 ? "0x00" :
- val == 0 ? "Not Reported" : "Reserved Value");
- printf("\n");
-}
-
-void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
- enum nvme_print_flags flags)
-{
- int human = flags & VERBOSE;
- int vs = flags & VS;
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)ns, sizeof(*ns));
- if (flags & JSON)
- return json_nvme_id_ns(ns, flags);
-
- printf("NVME Identify Namespace %d:\n", nsid);
- printf("nsze : %#"PRIx64"\n", le64_to_cpu(ns->nsze));
- printf("ncap : %#"PRIx64"\n", le64_to_cpu(ns->ncap));
- printf("nuse : %#"PRIx64"\n", le64_to_cpu(ns->nuse));
- printf("nsfeat : %#x\n", ns->nsfeat);
- if (human)
- nvme_show_id_ns_nsfeat(ns->nsfeat);
- printf("nlbaf : %d\n", ns->nlbaf);
- printf("flbas : %#x\n", ns->flbas);
- if (human)
- nvme_show_id_ns_flbas(ns->flbas);
- printf("mc : %#x\n", ns->mc);
- if (human)
- nvme_show_id_ns_mc(ns->mc);
- printf("dpc : %#x\n", ns->dpc);
- if (human)
- nvme_show_id_ns_dpc(ns->dpc);
- printf("dps : %#x\n", ns->dps);
- if (human)
- nvme_show_id_ns_dps(ns->dps);
- printf("nmic : %#x\n", ns->nmic);
- if (human)
- nvme_show_id_ns_nmic(ns->nmic);
- printf("rescap : %#x\n", ns->rescap);
- if (human)
- nvme_show_id_ns_rescap(ns->rescap);
- printf("fpi : %#x\n", ns->fpi);
- if (human)
- nvme_show_id_ns_fpi(ns->fpi);
- printf("dlfeat : %d\n", ns->dlfeat);
- if (human)
- nvme_show_id_ns_dlfeat(ns->dlfeat);
- printf("nawun : %d\n", le16_to_cpu(ns->nawun));
- printf("nawupf : %d\n", le16_to_cpu(ns->nawupf));
- printf("nacwu : %d\n", le16_to_cpu(ns->nacwu));
- printf("nabsn : %d\n", le16_to_cpu(ns->nabsn));
- printf("nabo : %d\n", le16_to_cpu(ns->nabo));
- printf("nabspf : %d\n", le16_to_cpu(ns->nabspf));
- printf("noiob : %d\n", le16_to_cpu(ns->noiob));
- printf("nvmcap : %.0Lf\n", int128_to_double(ns->nvmcap));
- if (ns->nsfeat & 0x10) {
- printf("npwg : %u\n", le16_to_cpu(ns->npwg));
- printf("npwa : %u\n", le16_to_cpu(ns->npwa));
- printf("npdg : %u\n", le16_to_cpu(ns->npdg));
- printf("npda : %u\n", le16_to_cpu(ns->npda));
- printf("nows : %u\n", le16_to_cpu(ns->nows));
- }
- printf("nsattr : %u\n", ns->nsattr);
- printf("nvmsetid: %d\n", le16_to_cpu(ns->nvmsetid));
- printf("anagrpid: %u\n", le32_to_cpu(ns->anagrpid));
- printf("endgid : %d\n", le16_to_cpu(ns->endgid));
-
- printf("nguid : ");
- for (i = 0; i < 16; i++)
- printf("%02x", ns->nguid[i]);
- printf("\n");
-
- printf("eui64 : ");
- for (i = 0; i < 8; i++)
- printf("%02x", ns->eui64[i]);
- printf("\n");
-
- for (i = 0; i <= ns->nlbaf; i++) {
- if (human)
- printf("LBA Format %2d : Metadata Size: %-3d bytes - "
- "Data Size: %-2d bytes - Relative Performance: %#x %s %s\n",
- i, le16_to_cpu(ns->lbaf[i].ms),
- 1 << ns->lbaf[i].ds, ns->lbaf[i].rp,
- ns->lbaf[i].rp == 3 ? "Degraded" :
- ns->lbaf[i].rp == 2 ? "Good" :
- ns->lbaf[i].rp == 1 ? "Better" : "Best",
- i == (ns->flbas & 0xf) ? "(in use)" : "");
- else
- printf("lbaf %2d : ms:%-3d lbads:%-2d rp:%#x %s\n", i,
- le16_to_cpu(ns->lbaf[i].ms), ns->lbaf[i].ds,
- ns->lbaf[i].rp,
- i == (ns->flbas & 0xf) ? "(in use)" : "");
- }
- if (vs) {
- printf("vs[]:\n");
- d(ns->vs, sizeof(ns->vs), 16, 1);
- }
-}
-
-
-static void json_nvme_id_ns_descs(void *data)
-{
- /* large enough to hold uuid str (37) or nguid str (32) + zero byte */
- char json_str[40];
- char *json_str_p;
-
- union {
- __u8 eui64[NVME_NIDT_EUI64_LEN];
- __u8 nguid[NVME_NIDT_NGUID_LEN];
-#ifdef LIBUUID
- uuid_t uuid;
-#endif
- } desc;
-
- struct json_object *root;
- struct json_array *json_array = NULL;
-
- off_t off;
- int pos, len = 0;
- int i;
-
- for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
- struct nvme_ns_id_desc *cur = data + pos;
- const char *nidt_name = NULL;
-
- if (cur->nidl == 0)
- break;
-
- memset(json_str, 0, sizeof(json_str));
- json_str_p = json_str;
- off = pos + sizeof(*cur);
-
- switch (cur->nidt) {
- case NVME_NIDT_EUI64:
- memcpy(desc.eui64, data + off, sizeof(desc.eui64));
- for (i = 0; i < sizeof(desc.eui64); i++)
- json_str_p += sprintf(json_str_p, "%02x", desc.eui64[i]);
- len += sizeof(desc.eui64);
- nidt_name = "eui64";
- break;
-
- case NVME_NIDT_NGUID:
- memcpy(desc.nguid, data + off, sizeof(desc.nguid));
- for (i = 0; i < sizeof(desc.nguid); i++)
- json_str_p += sprintf(json_str_p, "%02x", desc.nguid[i]);
- len += sizeof(desc.nguid);
- nidt_name = "nguid";
- break;
-
-#ifdef LIBUUID
- case NVME_NIDT_UUID:
- memcpy(desc.uuid, data + off, sizeof(desc.uuid));
- uuid_unparse_lower(desc.uuid, json_str);
- len += sizeof(desc.uuid);
- nidt_name = "uuid";
- break;
-#endif
- default:
- /* Skip unnkown types */
- len = cur->nidl;
- break;
- }
-
- if (nidt_name) {
- struct json_object *elem = json_create_object();
-
- json_object_add_value_int(elem, "loc", pos);
- json_object_add_value_int(elem, "nidt", (int)cur->nidt);
- json_object_add_value_int(elem, "nidl", (int)cur->nidl);
- json_object_add_value_string(elem, "type", nidt_name);
- json_object_add_value_string(elem, nidt_name, json_str);
-
- if (!json_array) {
- json_array = json_create_array();
- }
- json_array_add_value_object(json_array, elem);
- }
-
- len += sizeof(*cur);
- }
-
- root = json_create_object();
-
- if (json_array)
- json_object_add_value_array(root, "ns-descs", json_array);
-
- json_print_object(root, NULL);
- printf("\n");
-
- json_free_object(root);
-}
-
-void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
-{
- int pos, len = 0;
- int i;
-#ifdef LIBUUID
- uuid_t uuid;
- char uuid_str[37];
-#endif
- __u8 eui64[8];
- __u8 nguid[16];
-
- if (flags & BINARY)
- return d_raw((unsigned char *)data, 0x1000);
- if (flags & JSON)
- return json_nvme_id_ns_descs(data);
-
- printf("NVME Namespace Identification Descriptors NS %d:\n", nsid);
- for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
- struct nvme_ns_id_desc *cur = data + pos;
-
- if (cur->nidl == 0)
- break;
-
- switch (cur->nidt) {
- case NVME_NIDT_EUI64:
- memcpy(eui64, data + pos + sizeof(*cur), sizeof(eui64));
- printf("eui64 : ");
- for (i = 0; i < 8; i++)
- printf("%02x", eui64[i]);
- printf("\n");
- len += sizeof(eui64);
- break;
- case NVME_NIDT_NGUID:
- memcpy(nguid, data + pos + sizeof(*cur), sizeof(nguid));
- printf("nguid : ");
- for (i = 0; i < 16; i++)
- printf("%02x", nguid[i]);
- printf("\n");
- len += sizeof(nguid);
- break;
-#ifdef LIBUUID
- case NVME_NIDT_UUID:
- memcpy(uuid, data + pos + sizeof(*cur), 16);
- uuid_unparse_lower(uuid, uuid_str);
- printf("uuid : %s\n", uuid_str);
- len += sizeof(uuid);
- break;
-#endif
- default:
- /* Skip unnkown types */
- len = cur->nidl;
- break;
- }
-
- len += sizeof(*cur);
- }
-}
-
-static void print_ps_power_and_scale(__le16 ctr_power, __u8 scale)
-{
- __u16 power = le16_to_cpu(ctr_power);
-
- switch (scale & 0x3) {
- case 0:
- /* Not reported for this power state */
- printf("-");
- break;
-
- case 1:
- /* Units of 0.0001W */
- printf("%01u.%04uW", power / 10000, power % 10000);
- break;
-
- case 2:
- /* Units of 0.01W */
- printf("%01u.%02uW", power / 100, scale % 100);
- break;
-
- default:
- printf("reserved");
- }
-}
-
-static void nvme_show_id_ctrl_power(struct nvme_id_ctrl *ctrl)
-{
- int i;
-
- for (i = 0; i <= ctrl->npss; i++) {
- __u16 max_power = le16_to_cpu(ctrl->psd[i].mp);
-
- printf("ps %4d : mp:", i);
-
- if (ctrl->psd[i].flags & NVME_PSD_FLAGS_MXPS)
- printf("%01u.%04uW ", max_power / 10000, max_power % 10000);
- else
- printf("%01u.%02uW ", max_power / 100, max_power % 100);
-
- if (ctrl->psd[i].flags & NVME_PSD_FLAGS_NOPS)
- printf("non-");
-
- printf("operational enlat:%d exlat:%d rrt:%d rrl:%d\n"
- " rwt:%d rwl:%d idle_power:",
- le32_to_cpu(ctrl->psd[i].enlat),
- le32_to_cpu(ctrl->psd[i].exlat),
- ctrl->psd[i].rrt, ctrl->psd[i].rrl,
- ctrl->psd[i].rwt, ctrl->psd[i].rwl);
- print_ps_power_and_scale(ctrl->psd[i].idlp,
- nvme_psd_power_scale(ctrl->psd[i].ips));
- printf(" apw:");
- print_ps_power_and_scale(ctrl->psd[i].apw,
- nvme_psd_power_scale(ctrl->psd[i].aps));
- printf("\n");
-
- }
-}
-
-void __nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, enum nvme_print_flags flags,
- void (*vendor_show)(__u8 *vs, struct json_object *root))
-{
- int human = flags & VERBOSE, vs = flags & VS;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)ctrl, sizeof(*ctrl));
- else if (flags & JSON)
- return json_nvme_id_ctrl(ctrl, flags, vendor_show);
-
- printf("NVME Identify Controller:\n");
- printf("vid : %#x\n", le16_to_cpu(ctrl->vid));
- printf("ssvid : %#x\n", le16_to_cpu(ctrl->ssvid));
- printf("sn : %-.*s\n", (int)sizeof(ctrl->sn), ctrl->sn);
- printf("mn : %-.*s\n", (int)sizeof(ctrl->mn), ctrl->mn);
- printf("fr : %-.*s\n", (int)sizeof(ctrl->fr), ctrl->fr);
- printf("rab : %d\n", ctrl->rab);
- printf("ieee : %02x%02x%02x\n",
- ctrl->ieee[2], ctrl->ieee[1], ctrl->ieee[0]);
- printf("cmic : %#x\n", ctrl->cmic);
- if (human)
- nvme_show_id_ctrl_cmic(ctrl->cmic);
- printf("mdts : %d\n", ctrl->mdts);
- printf("cntlid : %#x\n", le16_to_cpu(ctrl->cntlid));
- printf("ver : %#x\n", le32_to_cpu(ctrl->ver));
- printf("rtd3r : %#x\n", le32_to_cpu(ctrl->rtd3r));
- printf("rtd3e : %#x\n", le32_to_cpu(ctrl->rtd3e));
- printf("oaes : %#x\n", le32_to_cpu(ctrl->oaes));
- if (human)
- nvme_show_id_ctrl_oaes(ctrl->oaes);
- printf("ctratt : %#x\n", le32_to_cpu(ctrl->ctratt));
- if (human)
- nvme_show_id_ctrl_ctratt(ctrl->ctratt);
- printf("rrls : %#x\n", le16_to_cpu(ctrl->rrls));
- printf("cntrltype : %d\n", ctrl->cntrltype);
- if (human)
- nvme_show_id_ctrl_cntrltype(ctrl->cntrltype);
- printf("fguid : %-.*s\n", (int)sizeof(ctrl->fguid), ctrl->fguid);
- printf("crdt1 : %u\n", le16_to_cpu(ctrl->crdt1));
- printf("crdt2 : %u\n", le16_to_cpu(ctrl->crdt2));
- printf("crdt3 : %u\n", le16_to_cpu(ctrl->crdt3));
-
- printf("oacs : %#x\n", le16_to_cpu(ctrl->oacs));
- if (human)
- nvme_show_id_ctrl_oacs(ctrl->oacs);
- printf("acl : %d\n", ctrl->acl);
- printf("aerl : %d\n", ctrl->aerl);
- printf("frmw : %#x\n", ctrl->frmw);
- if (human)
- nvme_show_id_ctrl_frmw(ctrl->frmw);
- printf("lpa : %#x\n", ctrl->lpa);
- if (human)
- nvme_show_id_ctrl_lpa(ctrl->lpa);
- printf("elpe : %d\n", ctrl->elpe);
- printf("npss : %d\n", ctrl->npss);
- printf("avscc : %#x\n", ctrl->avscc);
- if (human)
- nvme_show_id_ctrl_avscc(ctrl->avscc);
- printf("apsta : %#x\n", ctrl->apsta);
- if (human)
- nvme_show_id_ctrl_apsta(ctrl->apsta);
- printf("wctemp : %d\n", le16_to_cpu(ctrl->wctemp));
- printf("cctemp : %d\n", le16_to_cpu(ctrl->cctemp));
- printf("mtfa : %d\n", le16_to_cpu(ctrl->mtfa));
- printf("hmpre : %d\n", le32_to_cpu(ctrl->hmpre));
- printf("hmmin : %d\n", le32_to_cpu(ctrl->hmmin));
- printf("tnvmcap : %.0Lf\n", int128_to_double(ctrl->tnvmcap));
- printf("unvmcap : %.0Lf\n", int128_to_double(ctrl->unvmcap));
- printf("rpmbs : %#x\n", le32_to_cpu(ctrl->rpmbs));
- if (human)
- nvme_show_id_ctrl_rpmbs(ctrl->rpmbs);
- printf("edstt : %d\n", le16_to_cpu(ctrl->edstt));
- printf("dsto : %d\n", ctrl->dsto);
- printf("fwug : %d\n", ctrl->fwug);
- printf("kas : %d\n", le16_to_cpu(ctrl->kas));
- printf("hctma : %#x\n", le16_to_cpu(ctrl->hctma));
- if (human)
- nvme_show_id_ctrl_hctma(ctrl->hctma);
- printf("mntmt : %d\n", le16_to_cpu(ctrl->mntmt));
- printf("mxtmt : %d\n", le16_to_cpu(ctrl->mxtmt));
- printf("sanicap : %#x\n", le32_to_cpu(ctrl->sanicap));
- if (human)
- nvme_show_id_ctrl_sanicap(ctrl->sanicap);
- printf("hmminds : %d\n", le32_to_cpu(ctrl->hmminds));
- printf("hmmaxd : %d\n", le16_to_cpu(ctrl->hmmaxd));
- printf("nsetidmax : %d\n", le16_to_cpu(ctrl->nsetidmax));
- printf("endgidmax : %d\n", le16_to_cpu(ctrl->endgidmax));
- printf("anatt : %d\n", ctrl->anatt);
- printf("anacap : %d\n", ctrl->anacap);
- if (human)
- nvme_show_id_ctrl_anacap(ctrl->anacap);
- printf("anagrpmax : %d\n", ctrl->anagrpmax);
- printf("nanagrpid : %d\n", le32_to_cpu(ctrl->nanagrpid));
- printf("pels : %d\n", le32_to_cpu(ctrl->pels));
- printf("sqes : %#x\n", ctrl->sqes);
- if (human)
- nvme_show_id_ctrl_sqes(ctrl->sqes);
- printf("cqes : %#x\n", ctrl->cqes);
- if (human)
- nvme_show_id_ctrl_cqes(ctrl->cqes);
- printf("maxcmd : %d\n", le16_to_cpu(ctrl->maxcmd));
- printf("nn : %d\n", le32_to_cpu(ctrl->nn));
- printf("oncs : %#x\n", le16_to_cpu(ctrl->oncs));
- if (human)
- nvme_show_id_ctrl_oncs(ctrl->oncs);
- printf("fuses : %#x\n", le16_to_cpu(ctrl->fuses));
- if (human)
- nvme_show_id_ctrl_fuses(ctrl->fuses);
- printf("fna : %#x\n", ctrl->fna);
- if (human)
- nvme_show_id_ctrl_fna(ctrl->fna);
- printf("vwc : %#x\n", ctrl->vwc);
- if (human)
- nvme_show_id_ctrl_vwc(ctrl->vwc);
- printf("awun : %d\n", le16_to_cpu(ctrl->awun));
- printf("awupf : %d\n", le16_to_cpu(ctrl->awupf));
- printf("nvscc : %d\n", ctrl->nvscc);
- if (human)
- nvme_show_id_ctrl_nvscc(ctrl->nvscc);
- printf("nwpc : %d\n", ctrl->nwpc);
- if (human)
- nvme_show_id_ctrl_nwpc(ctrl->nwpc);
- printf("acwu : %d\n", le16_to_cpu(ctrl->acwu));
- printf("sgls : %#x\n", le32_to_cpu(ctrl->sgls));
- if (human)
- nvme_show_id_ctrl_sgls(ctrl->sgls);
- printf("mnan : %d\n", le32_to_cpu(ctrl->mnan));
- printf("subnqn : %-.*s\n", (int)sizeof(ctrl->subnqn), ctrl->subnqn);
- printf("ioccsz : %d\n", le32_to_cpu(ctrl->ioccsz));
- printf("iorcsz : %d\n", le32_to_cpu(ctrl->iorcsz));
- printf("icdoff : %d\n", le16_to_cpu(ctrl->icdoff));
- printf("fcatt : %#x\n", ctrl->fcatt);
- if (human)
- nvme_show_id_ctrl_fcatt(ctrl->fcatt);
- printf("msdbd : %d\n", ctrl->msdbd);
-
- nvme_show_id_ctrl_power(ctrl);
- if (vendor_show)
- vendor_show(ctrl->vs, NULL);
- else if (vs) {
- printf("vs[]:\n");
- d(ctrl->vs, sizeof(ctrl->vs), 16, 1);
- }
-}
-
-void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode)
-{
- __nvme_show_id_ctrl(ctrl, mode, NULL);
-}
-
-static void json_nvme_id_nvmset(struct nvme_id_nvmset_list *nvmset)
-{
- __u32 nent = nvmset->nid;
- struct json_array *entries;
- struct json_object *root;
- int i;
-
- root = json_create_object();
-
- json_object_add_value_int(root, "nid", nent);
-
- entries = json_create_array();
- for (i = 0; i < nent; i++) {
- struct json_object *entry = json_create_object();
-
- json_object_add_value_int(entry, "nvmset_id",
- le16_to_cpu(nvmset->ent[i].id));
- json_object_add_value_int(entry, "endurance_group_id",
- le16_to_cpu(nvmset->ent[i].endurance_group_id));
- json_object_add_value_int(entry, "random_4k_read_typical",
- le32_to_cpu(nvmset->ent[i].random_4k_read_typical));
- json_object_add_value_int(entry, "optimal_write_size",
- le32_to_cpu(nvmset->ent[i].opt_write_size));
- json_object_add_value_float(entry, "total_nvmset_cap",
- int128_to_double(nvmset->ent[i].total_nvmset_cap));
- json_object_add_value_float(entry, "unalloc_nvmset_cap",
- int128_to_double(nvmset->ent[i].unalloc_nvmset_cap));
- json_array_add_value_object(entries, entry);
- }
-
- json_object_add_value_array(root, "NVMSet", entries);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
- enum nvme_print_flags flags)
-{
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)nvmset, sizeof(*nvmset));
- if (flags & JSON)
- return json_nvme_id_nvmset(nvmset);
-
- printf("NVME Identify NVM Set List %d:\n", nvmset_id);
- printf("nid : %d\n", nvmset->nid);
- printf(".................\n");
- for (i = 0; i < nvmset->nid; i++) {
- printf(" NVM Set Attribute Entry[%2d]\n", i);
- printf(".................\n");
- printf("nvmset_id : %d\n",
- le16_to_cpu(nvmset->ent[i].id));
- printf("enduracne_group_id : %d\n",
- le16_to_cpu(nvmset->ent[i].endurance_group_id));
- printf("random_4k_read_typical : %u\n",
- le32_to_cpu(nvmset->ent[i].random_4k_read_typical));
- printf("optimal_write_size : %u\n",
- le32_to_cpu(nvmset->ent[i].opt_write_size));
- printf("total_nvmset_cap : %.0Lf\n",
- int128_to_double(nvmset->ent[i].total_nvmset_cap));
- printf("unalloc_nvmset_cap : %.0Lf\n",
- int128_to_double(nvmset->ent[i].unalloc_nvmset_cap));
- printf(".................\n");
- }
-}
-
-static void json_nvme_list_secondary_ctrl(const struct nvme_secondary_ctrl_list *sc_list,
- __u32 count)
-{
- const struct nvme_secondary_ctrl *sc_entry = &sc_list->sc_entry[0];
- __u32 nent = min(sc_list->num, count);
- struct json_array *entries;
- struct json_object *root;
- int i;
-
- root = json_create_object();
-
- json_object_add_value_int(root, "num", nent);
-
- entries = json_create_array();
- for (i = 0; i < nent; i++) {
- struct json_object *entry = json_create_object();
-
- json_object_add_value_int(entry,
- "secondary-controller-identifier",
- le16_to_cpu(sc_entry[i].scid));
- json_object_add_value_int(entry,
- "primary-controller-identifier",
- le16_to_cpu(sc_entry[i].pcid));
- json_object_add_value_int(entry, "secondary-controller-state",
- sc_entry[i].scs);
- json_object_add_value_int(entry, "virtual-function-number",
- le16_to_cpu(sc_entry[i].vfn));
- json_object_add_value_int(entry, "num-virtual-queues",
- le16_to_cpu(sc_entry[i].nvq));
- json_object_add_value_int(entry, "num-virtual-interrupts",
- le16_to_cpu(sc_entry[i].nvi));
- json_array_add_value_object(entries, entry);
- }
-
- json_object_add_value_array(root, "secondary-controllers", entries);
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-void nvme_show_list_secondary_ctrl(
- const struct nvme_secondary_ctrl_list *sc_list,
- __u32 count, enum nvme_print_flags flags)
-{
- const struct nvme_secondary_ctrl *sc_entry =
- &sc_list->sc_entry[0];
- static const char * const state_desc[] = { "Offline", "Online" };
-
- __u16 num = sc_list->num;
- __u32 entries = min(num, count);
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)sc_list, sizeof(*sc_list));
- if (flags & JSON)
- return json_nvme_list_secondary_ctrl(sc_list, entries);
-
- printf("Identify Secondary Controller List:\n");
- printf(" NUMID : Number of Identifiers : %d\n", num);
-
- for (i = 0; i < entries; i++) {
- printf(" SCEntry[%-3d]:\n", i);
- printf("................\n");
- printf(" SCID : Secondary Controller Identifier : 0x%.04x\n",
- le16_to_cpu(sc_entry[i].scid));
- printf(" PCID : Primary Controller Identifier : 0x%.04x\n",
- le16_to_cpu(sc_entry[i].pcid));
- printf(" SCS : Secondary Controller State : 0x%.04x (%s)\n",
- sc_entry[i].scs,
- state_desc[sc_entry[i].scs & 0x1]);
- printf(" VFN : Virtual Function Number : 0x%.04x\n",
- le16_to_cpu(sc_entry[i].vfn));
- printf(" NVQ : Num VQ Flex Resources Assigned : 0x%.04x\n",
- le16_to_cpu(sc_entry[i].nvq));
- printf(" NVI : Num VI Flex Resources Assigned : 0x%.04x\n",
- le16_to_cpu(sc_entry[i].nvi));
- }
-}
-
-static void json_nvme_id_ns_granularity_list(
- const struct nvme_id_ns_granularity_list *glist)
-{
- int i;
- struct json_object *root;
- struct json_array *entries;
-
- root = json_create_object();
-
- json_object_add_value_int(root, "attributes", glist->attributes);
- json_object_add_value_int(root, "num-descriptors",
- glist->num_descriptors);
-
- entries = json_create_array();
- for (i = 0; i <= glist->num_descriptors; i++) {
- struct json_object *entry = json_create_object();
-
- json_object_add_value_uint(entry, "namespace-size-granularity",
- le64_to_cpu(glist->entry[i].namespace_size_granularity));
- json_object_add_value_uint(entry, "namespace-capacity-granularity",
- le64_to_cpu(glist->entry[i].namespace_capacity_granularity));
- json_array_add_value_object(entries, entry);
- }
-
- json_object_add_value_array(root, "namespace-granularity-list", entries);
-
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-void nvme_show_id_ns_granularity_list(const struct nvme_id_ns_granularity_list *glist,
- enum nvme_print_flags flags)
-{
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)glist, sizeof(*glist));
- if (flags & JSON)
- return json_nvme_id_ns_granularity_list(glist);
-
- printf("Identify Namespace Granularity List:\n");
- printf(" ATTR : Namespace Granularity Attributes: 0x%x\n",
- glist->attributes);
- printf(" NUMD : Number of Descriptors : %d\n",
- glist->num_descriptors);
-
- /* Number of Descriptors is a 0's based value */
- for (i = 0; i <= glist->num_descriptors; i++) {
- printf("\n Entry[%2d] :\n", i);
- printf("................\n");
- printf(" NSG : Namespace Size Granularity : 0x%"PRIx64"\n",
- le64_to_cpu(glist->entry[i].namespace_size_granularity));
- printf(" NCG : Namespace Capacity Granularity : 0x%"PRIx64"\n",
- le64_to_cpu(glist->entry[i].namespace_capacity_granularity));
- }
-}
-
-static void json_nvme_id_uuid_list(const struct nvme_id_uuid_list *uuid_list)
-{
- struct json_object *root;
- struct json_array *entries;
- int i;
-
- root = json_create_object();
- entries = json_create_array();
- /* The 0th entry is reserved */
- for (i = 1; i < NVME_ID_UUID_LIST_MAX; i++) {
- uuid_t uuid;
- struct json_object *entry = json_create_object();
-
- /* The list is terminated by a zero UUID value */
- if (memcmp(uuid_list->entry[i].uuid, zero_uuid, sizeof(zero_uuid)) == 0)
- break;
- memcpy(&uuid, uuid_list->entry[i].uuid, sizeof(uuid));
- json_object_add_value_int(entry, "association",
- uuid_list->entry[i].header & 0x3);
- json_object_add_value_string(entry, "uuid",
- nvme_uuid_to_string(uuid));
- json_array_add_value_object(entries, entry);
- }
- json_object_add_value_array(root, "UUID-list", entries);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list,
- enum nvme_print_flags flags)
-{
- int i, human = flags & VERBOSE;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)uuid_list, sizeof(*uuid_list));
- if (flags & JSON)
- return json_nvme_id_uuid_list(uuid_list);
-
- /* The 0th entry is reserved */
- printf("NVME Identify UUID:\n");
- for (i = 1; i < NVME_ID_UUID_LIST_MAX; i++) {
- uuid_t uuid;
- char *association = "";
- uint8_t identifier_association = uuid_list->entry[i].header & 0x3;
- /* The list is terminated by a zero UUID value */
- if (memcmp(uuid_list->entry[i].uuid, zero_uuid, sizeof(zero_uuid)) == 0)
- break;
- memcpy(&uuid, uuid_list->entry[i].uuid, sizeof(uuid));
- if (human) {
- switch (identifier_association) {
- case 0x0:
- association = "No association reported";
- break;
- case 0x1:
- association = "associated with PCI Vendor ID";
- break;
- case 0x2:
- association = "associated with PCI Subsystem Vendor ID";
- break;
- default:
- association = "Reserved";
- break;
- }
- }
- printf(" Entry[%3d]\n", i);
- printf(".................\n");
- printf("association : 0x%x %s\n", identifier_association, association);
- printf("UUID : %s", nvme_uuid_to_string(uuid));
- if (memcmp(uuid_list->entry[i].uuid, invalid_uuid,
- sizeof(zero_uuid)) == 0)
- printf(" (Invalid UUID)");
- printf("\n.................\n");
- }
-}
-
-static const char *nvme_trtype_to_string(__u8 trtype)
-{
- switch(trtype) {
- case 0: return "The transport type is not indicated or the error "\
- "is not transport related.";
- case 1: return "RDMA Transport error.";
- case 2: return "Fibre Channel Transport error.";
- case 3: return "TCP Transport error.";
- case 254: return "Intra-host Transport error.";
- default: return "Reserved";
- };
-}
-
-void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
- const char *devname, enum nvme_print_flags flags)
-{
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)err_log,
- entries * sizeof(*err_log));
- else if (flags & JSON)
- return json_error_log(err_log, entries);
-
- printf("Error Log Entries for device:%s entries:%d\n", devname,
- entries);
- printf(".................\n");
- for (i = 0; i < entries; i++) {
- printf(" Entry[%2d] \n", i);
- printf(".................\n");
- printf("error_count : %"PRIu64"\n",
- le64_to_cpu(err_log[i].error_count));
- printf("sqid : %d\n", err_log[i].sqid);
- printf("cmdid : %#x\n", err_log[i].cmdid);
- printf("status_field : %#x(%s)\n", err_log[i].status_field,
- nvme_status_to_string(
- le16_to_cpu(err_log[i].status_field) >> 1));
- printf("parm_err_loc : %#x\n",
- err_log[i].parm_error_location);
- printf("lba : %#"PRIx64"\n",
- le64_to_cpu(err_log[i].lba));
- printf("nsid : %#x\n", err_log[i].nsid);
- printf("vs : %d\n", err_log[i].vs);
- printf("trtype : %s\n",
- nvme_trtype_to_string(err_log[i].trtype));
- printf("cs : %#"PRIx64"\n",
- le64_to_cpu(err_log[i].cs));
- printf("trtype_spec_info: %#x\n", err_log[i].trtype_spec_info);
- printf(".................\n");
- }
-}
-
-void nvme_show_resv_report(struct nvme_reservation_status *status, int bytes,
- __u32 cdw11, enum nvme_print_flags flags)
-{
- int i, j, regctl, entries;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)status, bytes);
- else if (flags & JSON)
- return json_nvme_resv_report(status, bytes, cdw11);
-
- regctl = status->regctl[0] | (status->regctl[1] << 8);
-
- printf("\nNVME Reservation status:\n\n");
- printf("gen : %d\n", le32_to_cpu(status->gen));
- printf("rtype : %d\n", status->rtype);
- printf("regctl : %d\n", regctl);
- printf("ptpls : %d\n", status->ptpls);
-
- /* check Extended Data Structure bit */
- if ((cdw11 & 0x1) == 0) {
- /*
- * if status buffer was too small, don't loop past the end of
- * the buffer
- */
- entries = (bytes - 24) / 24;
- if (entries < regctl)
- regctl = entries;
-
- for (i = 0; i < regctl; i++) {
- printf("regctl[%d] :\n", i);
- printf(" cntlid : %x\n",
- le16_to_cpu(status->regctl_ds[i].cntlid));
- printf(" rcsts : %x\n",
- status->regctl_ds[i].rcsts);
- printf(" hostid : %"PRIx64"\n",
- le64_to_cpu(status->regctl_ds[i].hostid));
- printf(" rkey : %"PRIx64"\n",
- le64_to_cpu(status->regctl_ds[i].rkey));
- }
- } else {
- /* if status buffer was too small, don't loop past the end of the buffer */
- entries = (bytes - 64) / 64;
- if (entries < regctl)
- regctl = entries;
-
- for (i = 0; i < regctl; i++) {
- printf("regctlext[%d] :\n", i);
- printf(" cntlid : %x\n",
- le16_to_cpu(status->regctl_eds[i].cntlid));
- printf(" rcsts : %x\n",
- status->regctl_eds[i].rcsts);
- printf(" rkey : %"PRIx64"\n",
- le64_to_cpu(status->regctl_eds[i].rkey));
- printf(" hostid : ");
- for (j = 0; j < 16; j++)
- printf("%x",
- status->regctl_eds[i].hostid[j]);
- printf("\n");
- }
- }
- printf("\n");
-}
-
-void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
- const char *devname, enum nvme_print_flags flags)
-{
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)fw_log, sizeof(*fw_log));
- if (flags & JSON)
- return json_fw_log(fw_log, devname);
-
- printf("Firmware Log for device:%s\n", devname);
- printf("afi : %#x\n", fw_log->afi);
- for (i = 0; i < 7; i++) {
- if (fw_log->frs[i])
- printf("frs%d : %-.*s\n", i + 1,
- (int)sizeof(fw_log->frs[i]), fw_log->frs[i]);
- }
-}
-
-void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
- const char *devname,
- enum nvme_print_flags flags)
-{
- __u32 nsid;
- int i;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)log, sizeof(*log));
- else if (flags & JSON)
- return json_changed_ns_list_log(log, devname);
-
- if (log->ns[0] != cpu_to_le32(0XFFFFFFFF)) {
- for (i = 0; i < NVME_ID_NS_LIST_MAX; i++) {
- nsid = le32_to_cpu(log->ns[i]);
- if (nsid == 0)
- break;
-
- printf("[%4u]:%#x\n", i, nsid);
- }
- } else
- printf("more than %d ns changed\n",
- NVME_ID_NS_LIST_MAX);
-}
-
-static void nvme_show_effects_log_human(__u32 effect)
-{
- const char *set = "+";
- const char *clr = "-";
-
- printf(" CSUPP+");
- printf(" LBCC%s", (effect & NVME_CMD_EFFECTS_LBCC) ? set : clr);
- printf(" NCC%s", (effect & NVME_CMD_EFFECTS_NCC) ? set : clr);
- printf(" NIC%s", (effect & NVME_CMD_EFFECTS_NIC) ? set : clr);
- printf(" CCC%s", (effect & NVME_CMD_EFFECTS_CCC) ? set : clr);
- printf(" USS%s", (effect & NVME_CMD_EFFECTS_UUID_SEL) ? set : clr);
-
- if ((effect & NVME_CMD_EFFECTS_CSE_MASK) >> 16 == 0)
- printf(" No command restriction\n");
- else if ((effect & NVME_CMD_EFFECTS_CSE_MASK) >> 16 == 1)
- printf(" No other command for same namespace\n");
- else if ((effect & NVME_CMD_EFFECTS_CSE_MASK) >> 16 == 2)
- printf(" No other command for any namespace\n");
- else
- printf(" Reserved CSE\n");
-}
-
-void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
- unsigned int flags)
-{
- int i, human = flags & VERBOSE;
- __u32 effect;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)effects, sizeof(*effects));
- else if (flags & JSON)
- return json_effects_log(effects);
-
- printf("Admin Command Set\n");
- for (i = 0; i < 256; i++) {
- effect = le32_to_cpu(effects->acs[i]);
- if (effect & NVME_CMD_EFFECTS_CSUPP) {
- printf("ACS%-6d[%-32s] %08x", i,
- nvme_cmd_to_string(1, i), effect);
- if (human)
- nvme_show_effects_log_human(effect);
- else
- printf("\n");
- }
- }
- printf("\nNVM Command Set\n");
- for (i = 0; i < 256; i++) {
- effect = le32_to_cpu(effects->iocs[i]);
- if (effect & NVME_CMD_EFFECTS_CSUPP) {
- printf("IOCS%-5d[%-32s] %08x", i,
- nvme_cmd_to_string(0, i), effect);
- if (human)
- nvme_show_effects_log_human(effect);
- else
- printf("\n");
- }
- }
-}
-
-uint64_t int48_to_long(__u8 *data)
-{
- int i;
- uint64_t result = 0;
-
- for (i = 0; i < 6; i++) {
- result *= 256;
- result += data[5 - i];
- }
- return result;
-}
-
-void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
- __u16 group_id, const char *devname,
- enum nvme_print_flags flags)
-{
- if (flags & BINARY)
- return d_raw((unsigned char *)endurance_log,
- sizeof(*endurance_log));
- else if (flags & JSON)
- return json_endurance_log(endurance_log, group_id);
-
- printf("Endurance Group Log for NVME device:%s Group ID:%x\n", devname,
- group_id);
- printf("critical warning : %u\n",
- endurance_log->critical_warning);
- printf("avl_spare : %u\n", endurance_log->avl_spare);
- printf("avl_spare_threshold : %u\n",
- endurance_log->avl_spare_threshold);
- printf("percent_used : %u%%\n", endurance_log->percent_used);
- printf("endurance_estimate : %'.0Lf\n",
- int128_to_double(endurance_log->endurance_estimate));
- printf("data_units_read : %'.0Lf\n",
- int128_to_double(endurance_log->data_units_read));
- printf("data_units_written : %'.0Lf\n",
- int128_to_double(endurance_log->data_units_written));
- printf("media_units_written : %'.0Lf\n",
- int128_to_double(endurance_log->media_units_written));
- printf("host_read_cmds : %'.0Lf\n",
- int128_to_double(endurance_log->host_read_cmds));
- printf("host_write_cmds : %'.0Lf\n",
- int128_to_double(endurance_log->host_write_cmds));
- printf("media_data_integrity_err: %'.0Lf\n",
- int128_to_double(endurance_log->media_data_integrity_err));
- printf("num_err_info_log_entries: %'.0Lf\n",
- int128_to_double(endurance_log->num_err_info_log_entries));
-}
-
-void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
- const char *devname, enum nvme_print_flags flags)
-{
- /* convert temperature from Kelvin to Celsius */
- int temperature = ((smart->temperature[1] << 8) |
- smart->temperature[0]) - 273;
- int i, human = flags & VERBOSE;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)smart, sizeof(*smart));
- else if (flags & JSON)
- return json_smart_log(smart, nsid, flags);
-
- printf("Smart Log for NVME device:%s namespace-id:%x\n", devname, nsid);
- printf("critical_warning : %#x\n",
- smart->critical_warning);
-
- if (human) {
- printf(" Available Spare[0] : %d\n", smart->critical_warning & 0x01);
- printf(" Temp. Threshold[1] : %d\n", (smart->critical_warning & 0x02) >> 1);
- printf(" NVM subsystem Reliability[2] : %d\n", (smart->critical_warning & 0x04) >> 2);
- printf(" Read-only[3] : %d\n", (smart->critical_warning & 0x08) >> 3);
- printf(" Volatile mem. backup failed[4] : %d\n", (smart->critical_warning & 0x10) >> 4);
- printf(" Persistent Mem. RO[5] : %d\n", (smart->critical_warning & 0x20) >> 5);
- }
-
- printf("temperature : %d C\n",
- temperature);
- printf("available_spare : %u%%\n",
- smart->avail_spare);
- printf("available_spare_threshold : %u%%\n",
- smart->spare_thresh);
- printf("percentage_used : %u%%\n",
- smart->percent_used);
- printf("endurance group critical warning summary: %#x\n",
- smart->endu_grp_crit_warn_sumry);
- printf("data_units_read : %'.0Lf\n",
- int128_to_double(smart->data_units_read));
- printf("data_units_written : %'.0Lf\n",
- int128_to_double(smart->data_units_written));
- printf("host_read_commands : %'.0Lf\n",
- int128_to_double(smart->host_reads));
- printf("host_write_commands : %'.0Lf\n",
- int128_to_double(smart->host_writes));
- printf("controller_busy_time : %'.0Lf\n",
- int128_to_double(smart->ctrl_busy_time));
- printf("power_cycles : %'.0Lf\n",
- int128_to_double(smart->power_cycles));
- printf("power_on_hours : %'.0Lf\n",
- int128_to_double(smart->power_on_hours));
- printf("unsafe_shutdowns : %'.0Lf\n",
- int128_to_double(smart->unsafe_shutdowns));
- printf("media_errors : %'.0Lf\n",
- int128_to_double(smart->media_errors));
- printf("num_err_log_entries : %'.0Lf\n",
- int128_to_double(smart->num_err_log_entries));
- printf("Warning Temperature Time : %u\n",
- le32_to_cpu(smart->warning_temp_time));
- printf("Critical Composite Temperature Time : %u\n",
- le32_to_cpu(smart->critical_comp_time));
- for (i = 0; i < 8; i++) {
- __s32 temp = le16_to_cpu(smart->temp_sensor[i]);
-
- if (temp == 0)
- continue;
- printf("Temperature Sensor %d : %d C\n", i + 1,
- temp - 273);
- }
- printf("Thermal Management T1 Trans Count : %u\n",
- le32_to_cpu(smart->thm_temp1_trans_count));
- printf("Thermal Management T2 Trans Count : %u\n",
- le32_to_cpu(smart->thm_temp2_trans_count));
- printf("Thermal Management T1 Total Time : %u\n",
- le32_to_cpu(smart->thm_temp1_total_time));
- printf("Thermal Management T2 Total Time : %u\n",
- le32_to_cpu(smart->thm_temp2_total_time));
-}
-
-void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
- enum nvme_print_flags flags, size_t len)
-{
- int offset = sizeof(struct nvme_ana_log);
- struct nvme_ana_log *hdr = ana_log;
- struct nvme_ana_group_desc *desc;
- size_t nsid_buf_size;
- void *base = ana_log;
- __u32 nr_nsids;
- int i, j;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)ana_log, len);
- else if (flags & JSON)
- return json_ana_log(ana_log, devname);
-
- printf("Asynchronous Namespace Access Log for NVMe device: %s\n",
- devname);
- printf("ANA LOG HEADER :-\n");
- printf("chgcnt : %"PRIu64"\n",
- le64_to_cpu(hdr->chgcnt));
- printf("ngrps : %u\n", le16_to_cpu(hdr->ngrps));
- printf("ANA Log Desc :-\n");
-
- for (i = 0; i < le16_to_cpu(ana_log->ngrps); i++) {
- desc = base + offset;
- nr_nsids = le32_to_cpu(desc->nnsids);
- nsid_buf_size = nr_nsids * sizeof(__le32);
-
- offset += sizeof(*desc);
- printf("grpid : %u\n", le32_to_cpu(desc->grpid));
- printf("nnsids : %u\n", le32_to_cpu(desc->nnsids));
- printf("chgcnt : %"PRIu64"\n",
- le64_to_cpu(desc->chgcnt));
- printf("state : %s\n",
- nvme_ana_state_to_string(desc->state));
- for (j = 0; j < le32_to_cpu(desc->nnsids); j++)
- printf(" nsid : %u\n",
- le32_to_cpu(desc->nsids[j]));
- printf("\n");
- offset += nsid_buf_size;
- }
-}
-
-static void nvme_show_self_test_result(struct nvme_st_result *res,
- enum nvme_print_flags flags)
-{
- static const char *const test_res[] = {
- "Operation completed without error",
- "Operation was aborted by a Device Self-test command",
- "Operation was aborted by a Controller Level Reset",
- "Operation was aborted due to a removal of a namespace from the namespace inventory",
- "Operation was aborted due to the processing of a Format NVM command",
- "A fatal error or unknown test error occurred while the controller was executing the"\
- " device self-test operation and the operation did not complete",
- "Operation completed with a segment that failed and the segment that failed is not known",
- "Operation completed with one or more failed segments and the first segment that failed "\
- "is indicated in the SegmentNumber field",
- "Operation was aborted for unknown reason",
- "Operation was aborted due to a sanitize operation",
- "Reserved",
- [NVME_ST_RESULT_NOT_USED] = "Entry not used (does not contain a result)",
- };
- __u8 op, code;
-
- op = res->dsts & NVME_ST_RESULT_NOT_MASK;
- printf(" Operation Result : %#x", op);
- if (flags & VERBOSE)
- printf(" %s", (op < ARRAY_SIZE(test_res) && test_res[op]) ?
- test_res[op] : test_res[ARRAY_SIZE(test_res) - 1]);
- printf("\n");
- if (op == NVME_ST_RESULT_NOT_USED)
- return;
-
- code = res->dsts >> NVME_ST_CODE_SHIFT;
- printf(" Self Test Code : %x", code);
- if (flags & VERBOSE) {
- switch (code) {
- case NVME_ST_CODE_SHORT:
- printf(" Short device self-test operation");
- break;
- case NVME_ST_CODE_EXTENDED:
- printf(" Extended device self-test operation");
- break;
- case NVME_ST_CODE_VS:
- printf(" Vendor specific");
- break;
- default:
- printf(" Reserved");
- break;
- }
- }
- printf("\n");
-
- if (op == NVME_ST_RESULT_KNOWN_SEG_FAIL)
- printf(" Segment Number : %#x\n", res->seg);
-
- printf(" Valid Diagnostic Information : %#x\n", res->vdi);
- printf(" Power on hours (POH) : %#"PRIx64"\n",
- (uint64_t)le64_to_cpu(res->poh));
-
- if (res->vdi & NVME_ST_VALID_DIAG_INFO_NSID)
- printf(" Namespace Identifier : %#x\n",
- le32_to_cpu(res->nsid));
- if (res->vdi & NVME_ST_VALID_DIAG_INFO_FLBA)
- printf(" Failing LBA : %#"PRIx64"\n",
- (uint64_t)le64_to_cpu(res->flba));
- if (res->vdi & NVME_ST_VALID_DIAG_INFO_SCT)
- printf(" Status Code Type : %#x\n", res->sct);
- if (res->vdi & NVME_ST_VALID_DIAG_INFO_SC) {
- printf(" Status Code : %#x", res->sc);
- if (flags & VERBOSE)
- printf(" %s", nvme_status_to_string(
- (res->sct & 7) << 8 | res->sc));
- printf("\n");
- }
- printf(" Vendor Specific : %#x %#x\n",
- res->vs[0], res->vs[1]);
-}
+static const uint8_t zero_uuid[16] = { 0 };
+static const uint8_t invalid_uuid[16] = {[0 ... 15] = 0xff };
-void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
- enum nvme_print_flags flags)
+static void nvme_print_object(struct json_object *j)
{
- int i;
+ const unsigned long jflags =
+ JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY;
- if (flags & BINARY)
- return d_raw((unsigned char *)self_test, sizeof(*self_test));
- if (flags & JSON)
- return json_self_test_log(self_test);
-
- printf("Device Self Test Log for NVME device:%s\n", devname);
- printf("Current operation : %#x\n", self_test->current_operation);
- printf("Current Completion : %u%%\n", self_test->completion);
- for (i = 0; i < NVME_LOG_ST_MAX_RESULTS; i++) {
- printf("Self Test Result[%d]:\n", i);
- nvme_show_self_test_result(&self_test->result[i], flags);
+ if (j) {
+ nvme_json_object_print(stdout, j, jflags);
+ json_object_put(j);
}
}
-static void nvme_show_sanitize_log_sprog(__u32 sprog)
-{
- double percent;
-
- percent = (((double)sprog * 100) / 0x10000);
- printf("\t(%f%%)\n", percent);
-}
-
-static void nvme_show_sanitize_log_sstat(__u16 sstat)
+void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags)
{
- const char *str = get_sanitize_log_sstat_status_str(sstat);
-
- printf("\t[2:0]\t%s\n", str);
- str = "Number of completed passes if most recent operation was overwrite";
- printf("\t[7:3]\t%s:\t%u\n", str,
- (sstat & NVME_SANITIZE_SSTAT_COMPLETED_PASSES_MASK) >>
- NVME_SANITIZE_SSTAT_COMPLETED_PASSES_SHIFT);
-
- printf("\t [8]\t");
- if (sstat & NVME_SANITIZE_SSTAT_GLOBAL_DATA_ERASED)
- str = "Global Data Erased set: no NS LB in the NVM subsystem "\
- "has been written to and no PMR in the NVM subsystem "\
- "has been enabled";
- else
- str = "Global Data Erased cleared: a NS LB in the NVM "\
- "subsystem has been written to or a PMR in the NVM "\
- "subsystem has been enabled";
- printf("%s\n", str);
+ nvme_print_object(nvme_props_to_json(bar, flags));
}
-static void nvme_show_estimate_sanitize_time(const char *text, uint32_t value)
+void nvme_show_single_property(int offset, uint64_t value64, int human)
{
- printf("%s: %u%s\n", text, value,
- value == 0xffffffff ? " (No time period reported)" : "");
}
-void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
- const char *devname, enum nvme_print_flags flags)
+void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
+ enum nvme_print_flags flags)
{
- int human = flags & VERBOSE;
- __u16 status = le16_to_cpu(sanitize->sstat) & NVME_SANITIZE_SSTAT_STATUS_MASK;
-
- if (flags & BINARY)
- d_raw((unsigned char *)sanitize, sizeof(*sanitize));
- else if (flags & JSON)
- json_sanitize_log(sanitize, devname);
-
- printf("Sanitize Progress (SPROG) : %u",
- le16_to_cpu(sanitize->sprog));
-
- if (human && status == NVME_SANITIZE_SSTAT_STATUS_IN_PROGESS)
- nvme_show_sanitize_log_sprog(le16_to_cpu(sanitize->sprog));
- else
- printf("\n");
-
- printf("Sanitize Status (SSTAT) : %#x\n",
- le16_to_cpu(sanitize->sstat));
- if (human)
- nvme_show_sanitize_log_sstat(le16_to_cpu(sanitize->sstat));
-
- printf("Sanitize Command Dword 10 Information (SCDW10) : %#x\n",
- le32_to_cpu(sanitize->scdw10));
- nvme_show_estimate_sanitize_time("Estimated Time For Overwrite ",
- le32_to_cpu(sanitize->eto));
- nvme_show_estimate_sanitize_time("Estimated Time For Block Erase ",
- le32_to_cpu(sanitize->etbe));
- nvme_show_estimate_sanitize_time("Estimated Time For Crypto Erase ",
- le32_to_cpu(sanitize->etce));
- nvme_show_estimate_sanitize_time("Estimated Time For Overwrite (No-Deallocate) ",
- le32_to_cpu(sanitize->etond));
- nvme_show_estimate_sanitize_time("Estimated Time For Block Erase (No-Deallocate) ",
- le32_to_cpu(sanitize->etbend));
- nvme_show_estimate_sanitize_time("Estimated Time For Crypto Erase (No-Deallocate)",
- le32_to_cpu(sanitize->etcend));
+ nvme_print_object(nvme_id_ns_to_json(ns, mode));
}
-const char *nvme_feature_to_string(int feature)
+void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags)
{
- switch (feature) {
- case NVME_FEAT_FID_ARBITRATION: return "Arbitration";
- case NVME_FEAT_FID_POWER_MGMT: return "Power Management";
- case NVME_FEAT_FID_LBA_RANGE: return "LBA Range Type";
- case NVME_FEAT_FID_TEMP_THRESH: return "Temperature Threshold";
- case NVME_FEAT_FID_ERR_RECOVERY:return "Error Recovery";
- case NVME_FEAT_FID_VOLATILE_WC: return "Volatile Write Cache";
- case NVME_FEAT_FID_NUM_QUEUES: return "Number of Queues";
- case NVME_FEAT_FID_IRQ_COALESCE:return "Interrupt Coalescing";
- case NVME_FEAT_FID_IRQ_CONFIG: return "Interrupt Vector Configuration";
- case NVME_FEAT_FID_WRITE_ATOMIC:return "Write Atomicity Normal";
- case NVME_FEAT_FID_ASYNC_EVENT: return "Async Event Configuration";
- case NVME_FEAT_FID_AUTO_PST: return "Autonomous Power State Transition";
- case NVME_FEAT_FID_HOST_MEM_BUF:return "Host Memory Buffer";
- case NVME_FEAT_FID_KATO: return "Keep Alive Timer";
- case NVME_FEAT_FID_NOPSC: return "Non-Operational Power State Config";
- case NVME_FEAT_FID_RRL: return "Read Recovery Level";
- case NVME_FEAT_FID_PLM_CONFIG: return "Predicatable Latency Mode Config";
- case NVME_FEAT_FID_PLM_WINDOW: return "Predicatable Latency Mode Window";
- case NVME_FEAT_FID_SW_PROGRESS: return "Software Progress";
- case NVME_FEAT_FID_HOST_ID: return "Host Identifier";
- case NVME_FEAT_FID_RESV_MASK: return "Reservation Notification Mask";
- case NVME_FEAT_FID_RESV_PERSIST:return "Reservation Persistence";
- case NVME_FEAT_FID_TIMESTAMP: return "Timestamp";
- case NVME_FEAT_FID_WRITE_PROTECT:return "Namespce Write Protect";
- case NVME_FEAT_FID_HCTM: return "Host Controlled Thermal Management";
- case NVME_FEAT_FID_HOST_BEHAVIOR:return "Host Behavior";
- case NVME_FEAT_FID_SANITIZE: return "Sanitize";
- default: return "Unknown";
- }
+ nvme_print_object(nvme_id_ns_desc_list_to_json(data, flags));
}
-const char *nvme_register_to_string(int reg)
+void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode)
{
- switch (reg) {
- case NVME_REG_CAP: return "Controller Capabilities";
- case NVME_REG_VS: return "Version";
- case NVME_REG_INTMS: return "Interrupt Vector Mask Set";
- case NVME_REG_INTMC: return "Interrupt Vector Mask Clear";
- case NVME_REG_CC: return "Controller Configuration";
- case NVME_REG_CSTS: return "Controller Status";
- case NVME_REG_NSSR: return "NVM Subsystem Reset";
- case NVME_REG_AQA: return "Admin Queue Attributes";
- case NVME_REG_ASQ: return "Admin Submission Queue Base Address";
- case NVME_REG_ACQ: return "Admin Completion Queue Base Address";
- case NVME_REG_CMBLOC: return "Controller Memory Buffer Location";
- case NVME_REG_CMBSZ: return "Controller Memory Buffer Size";
- default: return "Unknown";
- }
+ nvme_print_object(nvme_id_ctrl_to_json(ctrl, mode));
}
-const char *nvme_select_to_string(int sel)
+void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
+ enum nvme_print_flags flags)
{
- switch (sel) {
- case 0: return "Current";
- case 1: return "Default";
- case 2: return "Saved";
- case 3: return "Supported capabilities";
- default: return "Reserved";
- }
+ nvme_print_object(nvme_id_nvm_set_list_to_json(nvmset, flags));
}
-void nvme_show_select_result(__u32 result)
+void nvme_show_list_secondary_ctrl(
+ struct nvme_secondary_ctrl_list *sc_list,
+ __u32 count, enum nvme_print_flags flags)
{
- if (result & 0x1)
- printf(" Feature is saveable\n");
- if (result & 0x2)
- printf(" Feature is per-namespace\n");
- if (result & 0x4)
- printf(" Feature is changeable\n");
+ nvme_print_object(nvme_id_secondary_ctrl_list_to_json(sc_list, flags));
}
-
-static const char *nvme_generic_error_to_string(__u16 sc)
+void nvme_show_id_ns_granularity_list(struct nvme_id_ns_granularity_list *glist,
+ enum nvme_print_flags flags)
{
- switch (sc) {
- case NVME_SC_SUCCESS:
- return "SUCCESS: The command completed successfully";
- case NVME_SC_INVALID_OPCODE:
- return "INVALID_OPCODE: The associated command opcode field is not valid";
- case NVME_SC_INVALID_FIELD:
- return "INVALID_FIELD: A reserved coded value or an unsupported value in a defined field";
- case NVME_SC_CMDID_CONFLICT:
- return "CMDID_CONFLICT: The command identifier is already in use";
- case NVME_SC_DATA_XFER_ERROR:
- return "DATA_XFER_ERROR: Error while trying to transfer the data or metadata";
- case NVME_SC_POWER_LOSS:
- return "POWER_LOSS: Command aborted due to power loss notification";
- case NVME_SC_INTERNAL:
- return "INTERNAL: The command was not completed successfully due to an internal error";
- case NVME_SC_ABORT_REQ:
- return "ABORT_REQ: The command was aborted due to a Command Abort request";
- case NVME_SC_ABORT_QUEUE:
- return "ABORT_QUEUE: The command was aborted due to a Delete I/O Submission Queue request";
- case NVME_SC_FUSED_FAIL:
- return "FUSED_FAIL: The command was aborted due to the other command in a fused operation failing";
- case NVME_SC_FUSED_MISSING:
- return "FUSED_MISSING: The command was aborted due to a Missing Fused Command";
- case NVME_SC_INVALID_NS:
- return "INVALID_NS: The namespace or the format of that namespace is invalid";
- case NVME_SC_CMD_SEQ_ERROR:
- return "CMD_SEQ_ERROR: The command was aborted due to a protocol violation in a multicommand sequence";
- case NVME_SC_SANITIZE_FAILED:
- return "SANITIZE_FAILED: The most recent sanitize operation failed and no recovery actions has been successfully completed";
- case NVME_SC_SANITIZE_IN_PROGRESS:
- return "SANITIZE_IN_PROGRESS: The requested function is prohibited while a sanitize operation is in progress";
- case NVME_SC_LBA_RANGE:
- return "LBA_RANGE: The command references a LBA that exceeds the size of the namespace";
- case NVME_SC_NS_WRITE_PROTECTED:
- return "NS_WRITE_PROTECTED: The command is prohibited while the namespace is write protected by the host.";
- case NVME_SC_CMD_INTERRUPTED:
- return "CMD_INTERRUPTED: Command processing was interrupted and the controller is unable to successfully complete the command. The host should retry the command.";
- case NVME_SC_CAP_EXCEEDED:
- return "CAP_EXCEEDED: The execution of the command has caused the capacity of the namespace to be exceeded";
- case NVME_SC_NS_NOT_READY:
- return "NS_NOT_READY: The namespace is not ready to be accessed as a result of a condition other than a condition that is reported as an Asymmetric Namespace Access condition";
- case NVME_SC_RESERVATION_CONFLICT:
- return "RESERVATION_CONFLICT: The command was aborted due to a conflict with a reservation held on the accessed namespace";
- default:
- return "Unknown";
- };
+ nvme_print_object(nvme_id_ns_granularity_list_to_json(glist, flags));
}
-static const char *nvme_specific_error_to_string(__u16 sc)
+void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list,
+ enum nvme_print_flags flags)
{
- switch (sc) {
- case NVME_SC_CQ_INVALID:
- return "CQ_INVALID: The Completion Queue identifier specified in the command does not exist";
- case NVME_SC_QID_INVALID:
- return "QID_INVALID: The creation of the I/O Completion Queue failed due to an invalid queue identifier specified as part of the command. An invalid queue identifier is one that is currently in use or one that is outside the range supported by the controller";
- case NVME_SC_QUEUE_SIZE:
- return "QUEUE_SIZE: The host attempted to create an I/O Completion Queue with an invalid number of entries";
- case NVME_SC_ABORT_LIMIT:
- return "ABORT_LIMIT: The number of concurrently outstanding Abort commands has exceeded the limit indicated in the Identify Controller data structure";
- case NVME_SC_ABORT_MISSING:
- return "ABORT_MISSING: The abort command is missing";
- case NVME_SC_ASYNC_LIMIT:
- return "ASYNC_LIMIT: The number of concurrently outstanding Asynchronous Event Request commands has been exceeded";
- case NVME_SC_FIRMWARE_SLOT:
- return "FIRMWARE_SLOT: The firmware slot indicated is invalid or read only. This error is indicated if the firmware slot exceeds the number supported";
- case NVME_SC_FIRMWARE_IMAGE:
- return "FIRMWARE_IMAGE: The firmware image specified for activation is invalid and not loaded by the controller";
- case NVME_SC_INVALID_VECTOR:
- return "INVALID_VECTOR: The creation of the I/O Completion Queue failed due to an invalid interrupt vector specified as part of the command";
- case NVME_SC_INVALID_LOG_PAGE:
- return "INVALID_LOG_PAGE: The log page indicated is invalid. This error condition is also returned if a reserved log page is requested";
- case NVME_SC_INVALID_FORMAT:
- return "INVALID_FORMAT: The LBA Format specified is not supported. This may be due to various conditions";
- case NVME_SC_FW_NEEDS_CONV_RESET:
- return "FW_NEEDS_CONVENTIONAL_RESET: The firmware commit was successful, however, activation of the firmware image requires a conventional reset";
- case NVME_SC_INVALID_QUEUE:
- return "INVALID_QUEUE: This error indicates that it is invalid to delete the I/O Completion Queue specified. The typical reason for this error condition is that there is an associated I/O Submission Queue that has not been deleted.";
- case NVME_SC_FEATURE_NOT_SAVEABLE:
- return "FEATURE_NOT_SAVEABLE: The Feature Identifier specified does not support a saveable value";
- case NVME_SC_FEATURE_NOT_CHANGEABLE:
- return "FEATURE_NOT_CHANGEABLE: The Feature Identifier is not able to be changed";
- case NVME_SC_FEATURE_NOT_PER_NS:
- return "FEATURE_NOT_PER_NS: The Feature Identifier specified is not namespace specific. The Feature Identifier settings apply across all namespaces";
- case NVME_SC_FW_NEEDS_SUBSYS_RESET:
- return "FW_NEEDS_SUBSYSTEM_RESET: The firmware commit was successful, however, activation of the firmware image requires an NVM Subsystem";
- case NVME_SC_FW_NEEDS_RESET:
- return "FW_NEEDS_RESET: The firmware commit was successful; however, the image specified does not support being activated without a reset";
- case NVME_SC_FW_NEEDS_MAX_TIME:
- return "FW_NEEDS_MAX_TIME_VIOLATION: The image specified if activated immediately would exceed the Maximum Time for Firmware Activation (MTFA) value reported in Identify Controller. To activate the firmware, the Firmware Commit command needs to be re-issued and the image activated using a reset";
- case NVME_SC_FW_ACTIVATE_PROHIBITED:
- return "FW_ACTIVATION_PROHIBITED: The image specified is being prohibited from activation by the controller for vendor specific reasons";
- case NVME_SC_OVERLAPPING_RANGE:
- return "OVERLAPPING_RANGE: This error is indicated if the firmware image has overlapping ranges";
- case NVME_SC_NS_INSUFFICIENT_CAP:
- return "NS_INSUFFICIENT_CAPACITY: Creating the namespace requires more free space than is currently available. The Command Specific Information field of the Error Information Log specifies the total amount of NVM capacity required to create the namespace in bytes";
- case NVME_SC_NS_ID_UNAVAILABLE:
- return "NS_ID_UNAVAILABLE: The number of namespaces supported has been exceeded";
- case NVME_SC_NS_ALREADY_ATTACHED:
- return "NS_ALREADY_ATTACHED: The controller is already attached to the namespace specified";
- case NVME_SC_NS_IS_PRIVATE:
- return "NS_IS_PRIVATE: The namespace is private and is already attached to one controller";
- case NVME_SC_NS_NOT_ATTACHED:
- return "NS_NOT_ATTACHED: The request to detach the controller could not be completed because the controller is not attached to the namespace";
- case NVME_SC_THIN_PROV_NOT_SUPP:
- return "THIN_PROVISIONING_NOT_SUPPORTED: Thin provisioning is not supported by the controller";
- case NVME_SC_CTRL_LIST_INVALID:
- return "CONTROLLER_LIST_INVALID: The controller list provided is invalid";
- case NVME_SC_BP_WRITE_PROHIBITED:
- return "BOOT PARTITION WRITE PROHIBITED: The command is trying to modify a Boot Partition while it is locked";
- case NVME_SC_BAD_ATTRIBUTES:
- return "BAD_ATTRIBUTES: Bad attributes were given";
- case NVME_SC_PMR_SAN_PROHIBITED:
- return "Sanitize Prohibited While Persistent Memory Region is Enabled: A sanitize operation is prohibited while the Persistent Memory Region is enabled.";
- default:
- return "Unknown";
- };
+ nnvme_print_object(vme_id_uuid_list_to_json(uuid_list, flags));
}
-static const char *nvme_media_error_to_string(__u16 sc)
+void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
+ const char *devname, enum nvme_print_flags flags)
{
- switch (sc) {
- case NVME_SC_WRITE_FAULT:
- return "WRITE_FAULT: The write data could not be committed to the media";
- case NVME_SC_READ_ERROR:
- return "READ_ERROR: The read data could not be recovered from the media";
- case NVME_SC_GUARD_CHECK:
- return "GUARD_CHECK: The command was aborted due to an end-to-end guard check failure";
- case NVME_SC_APPTAG_CHECK:
- return "APPTAG_CHECK: The command was aborted due to an end-to-end application tag check failure";
- case NVME_SC_REFTAG_CHECK:
- return "REFTAG_CHECK: The command was aborted due to an end-to-end reference tag check failure";
- case NVME_SC_COMPARE_FAILED:
- return "COMPARE_FAILED: The command failed due to a miscompare during a Compare command";
- case NVME_SC_ACCESS_DENIED:
- return "ACCESS_DENIED: Access to the namespace and/or LBA range is denied due to lack of access rights";
- case NVME_SC_UNWRITTEN_BLOCK:
- return "UNWRITTEN_BLOCK: The command failed due to an attempt to read from an LBA range containing a deallocated or unwritten logical block";
- default:
- return "Unknown";
- };
+ nnvme_print_object(vme_error_log_to_json(err_log, entries, flags));
}
-static const char *nvme_path_error_to_string(__u16 sc)
+void nvme_show_resv_report(struct nvme_reservation_status *status, int bytes,
+ __u32 cdw11, enum nvme_print_flags flags)
{
- switch (sc) {
- case NVME_SC_ANA_PERSISTENT_LOSS:
- return "ASYMMETRIC_NAMESPACE_ACCESS_PERSISTENT_LOSS: The requested function (e.g., command) is not able to be performed as a result of the relationship between the controller and the namespace being in the ANA Persistent Loss state";
- case NVME_SC_ANA_INACCESSIBLE:
- return "ASYMMETRIC_NAMESPACE_ACCESS_INACCESSIBLE: The requested function (e.g., command) is not able to be performed as a result of the relationship between the controller and the namespace being in the ANA Inaccessible state";
- case NVME_SC_ANA_TRANSITION:
- return "ASYMMETRIC_NAMESPACE_ACCESS_TRANSITION: The requested function (e.g., command) is not able to be performed as a result of the relationship between the controller and the namespace transitioning between Asymmetric Namespace Access states";
- default:
- return "Unknown";
- };
+ nvme_print_object(nvme_resv_report_to_json(status, cdw11 & 1, flags));
}
-const char *nvme_status_to_string(__u32 status)
+void nvme_show_fw_log(struct nvme_firmware_slot *fw_log,
+ const char *devname, enum nvme_print_flags flags)
{
- __u16 sc = nvme_status_code(status);
-
- if (status < 0)
- return strerror(errno);
-
- switch (nvme_status_code_type(status)) {
- case NVME_SCT_GENERIC:
- return nvme_generic_error_to_string(sc);
- case NVME_SCT_CMD_SPECIFIC:
- return nvme_specific_error_to_string(sc);
- case NVME_SCT_MEDIA:
- return nvme_media_error_to_string(sc);
- case NVME_SCT_PATH:
- return nvme_path_error_to_string(sc);
- default:
- return "Unknown";
- }
+ nvme_print_object(nvme_fw_slot_log_to_json(fw_log, flags));
}
-static const char *nvme_feature_lba_type_to_string(__u8 type)
+void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
+ const char *devname,
+ enum nvme_print_flags flags)
{
- switch (type) {
- case 0: return "Reserved";
- case 1: return "Filesystem";
- case 2: return "RAID";
- case 3: return "Cache";
- case 4: return "Page / Swap file";
- default:
- if (type>=0x05 && type<=0x7f)
- return "Reserved";
- else
- return "Vendor Specific";
- }
+ nvme_print_object(nvme_ns_list_to_json(log, flags));
}
-void nvme_show_lba_range(struct nvme_lba_range_type *lbrt, int nr_ranges)
+void nvme_show_effects_log(struct nvme_cmd_effects_log *effects,
+ unsigned int flags)
{
- int i, j;
-
- for (i = 0; i <= nr_ranges; i++) {
- printf("\ttype : %#x - %s\n", lbrt->entry[i].type,
- nvme_feature_lba_type_to_string(lbrt->entry[i].type));
- printf("\tattributes : %#x - %s, %s\n", lbrt->entry[i].attributes,
- (lbrt->entry[i].attributes & 0x0001) ?
- "LBA range may be overwritten" :
- "LBA range should not be overwritten",
- ((lbrt->entry[i].attributes & 0x0002) >> 1) ?
- "LBA range should be hidden from the OS/EFI/BIOS" :
- "LBA range should be visible from the OS/EFI/BIOS");
- printf("\tslba : %#"PRIx64"\n", (uint64_t)(lbrt->entry[i].slba));
- printf("\tnlb : %#"PRIx64"\n", (uint64_t)(lbrt->entry[i].nlb));
- printf("\tguid : ");
- for (j = 0; j < 16; j++)
- printf("%02x", lbrt->entry[i].guid[j]);
- printf("\n");
- }
+ nvme_print_object(nvme_cmd_effects_log_to_json(effects, flags));
}
-
-static const char *nvme_feature_wl_hints_to_string(__u8 wh)
+void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
+ __u16 group_id, const char *devname,
+ enum nvme_print_flags flags)
{
- switch (wh) {
- case 0: return "No Workload";
- case 1: return "Extended Idle Period with a Burst of Random Writes";
- case 2: return "Heavy Sequential Writes";
- default:return "Reserved";
- }
+ nvme_print_object(nvme_endurance_group_log_to_json(endurance_log, flags));
}
-static const char *nvme_feature_temp_type_to_string(__u8 type)
+void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
+ const char *devname, enum nvme_print_flags flags)
{
- switch (type) {
- case 0: return "Over Temperature Threshold";
- case 1: return "Under Temperature Threshold";
- default:return "Reserved";
- }
+ nvme_print_object(nvme_smart_log_to_json(smart, flags));
}
-static const char *nvme_feature_temp_sel_to_string(__u8 sel)
+void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
+ enum nvme_print_flags flags, size_t len)
{
- switch (sel)
- {
- case 0: return "Composite Temperature";
- case 1: return "Temperature Sensor 1";
- case 2: return "Temperature Sensor 2";
- case 3: return "Temperature Sensor 3";
- case 4: return "Temperature Sensor 4";
- case 5: return "Temperature Sensor 5";
- case 6: return "Temperature Sensor 6";
- case 7: return "Temperature Sensor 7";
- case 8: return "Temperature Sensor 8";
- default:return "Reserved";
- }
+ nvme_print_object(nvme_ana_log_to_json(ana_log, flags));
}
-static void nvme_show_auto_pst(struct nvme_feat_auto_pst *apst)
+void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
+ enum nvme_print_flags flags)
{
- int i;
-
- printf( "\tAuto PST Entries");
- printf("\t.................\n");
- for (i = 0; i < 32; i++) {
- __u64 data = le64_to_cpu(apst->apst_entry[i]);
- printf("\tEntry[%2d] \n", i);
- printf("\t.................\n");
- printf("\tIdle Time Prior to Transition (ITPT): %u ms\n",
- (__u32)(data & 0xffffff00) >> 8);
- printf("\tIdle Transition Power State (ITPS): %u\n",
- ((__u32)data & 0x000000f8) >> 3);
- printf("\t.................\n");
- }
+ nvme_print_object(nvme_dev_self_test_log_to_json(self_test, flags));
}
-static void nvme_show_timestamp(struct nvme_timestamp *ts)
+void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
+ const char *devname, enum nvme_print_flags flags)
{
- struct tm *tm;
- char buffer[32];
- time_t timestamp = int48_to_long(ts->timestamp) / 1000;
-
- tm = localtime(×tamp);
- strftime(buffer, sizeof(buffer), "%c %Z", tm);
-
- printf("\tThe timestamp is : %"PRIu64" (%s)\n",
- int48_to_long(ts->timestamp), buffer);
- printf("\t%s\n", (ts->attr & 2) ?
- "The Timestamp field was initialized with a "\
- "Timestamp value using a Set Features command." :
- "The Timestamp field was initialized "\
- "to ‘0’ by a Controller Level Reset.");
- printf("\t%s\n", (ts->attr & 1) ?
- "The controller may have stopped counting during vendor specific "\
- "intervals after the Timestamp value was initialized" :
- "The controller counted time in milliseconds "\
- "continuously since the Timestamp value was initialized.");
+ nvme_print_object(nvme_sanitize_log_to_json(sanitize, flags));
}
-static void nvme_directive_show_fields(__u8 dtype, __u8 doper,
- unsigned int result, unsigned char *buf)
+void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
+ void *buf, __u32 len, enum nvme_print_flags flags)
{
- __u8 *field = buf;
- int count, i;
+ struct json_object *j;
switch (dtype) {
case NVME_DIRECTIVE_DTYPE_IDENTIFY:
switch (doper) {
case NVME_DIRECTIVE_RECEIVE_IDENTIFY_DOPER_PARAM:
- printf("\tDirective support \n");
- printf("\t\tIdentify Directive : %s\n",
- (*field & 0x1) ? "supported":"not supported");
- printf("\t\tStream Directive : %s\n",
- (*field & 0x2) ? "supported":"not supported");
- printf("\tDirective status \n");
- printf("\t\tIdentify Directive : %s\n",
- (*(field + 32) & 0x1) ? "enabled" : "disabled");
- printf("\t\tStream Directive : %s\n",
- (*(field + 32) & 0x2) ? "enabled" : "disabled");
+ j = nvme_identify_directives_to_json(buf, flags);
break;
default:
- fprintf(stderr,
- "invalid directive operations for Identify Directives\n");
+ break;
}
break;
case NVME_DIRECTIVE_DTYPE_STREAMS:
switch (doper) {
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_PARAM:
- printf("\tMax Streams Limit (MSL): %u\n",
- *(__u16 *) field);
- printf("\tNVM Subsystem Streams Available (NSSA): %u\n",
- *(__u16 *) (field + 2));
- printf("\tNVM Subsystem Streams Open (NSSO): %u\n",
- *(__u16 *) (field + 4));
- printf("\tStream Write Size (in unit of LB size) (SWS): %u\n",
- *(__u32 *) (field + 16));
- printf("\tStream Granularity Size (in unit of SWS) (SGS): %u\n",
- *(__u16 *) (field + 20));
- printf("\tNamespece Streams Allocated (NSA): %u\n",
- *(__u16 *) (field + 22));
- printf("\tNamespace Streams Open (NSO): %u\n",
- *(__u16 *) (field + 24));
+ j = nvme_streams_directive_params (buf, flags);
break;
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_STATUS:
- count = *(__u16 *) field;
- printf("\tOpen Stream Count : %u\n", *(__u16 *) field);
- for ( i = 0; i < count; i++ ) {
- printf("\tStream Identifier %.6u : %u\n", i + 1,
- *(__u16 *) (field + ((i + 1) * 2)));
- }
+ j = nvme_streams_status_to_json(buf, flags);
break;
case NVME_DIRECTIVE_RECEIVE_STREAMS_DOPER_RESOURCE:
- printf("\tNamespace Streams Allocated (NSA): %u\n",
- result & 0xffff);
break;
default:
- fprintf(stderr,
- "invalid directive operations for Streams Directives\n");
+ break;
}
break;
default:
- fprintf(stderr, "invalid directive type\n");
break;
}
- return;
-}
-
-void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
- void *buf, __u32 len, enum nvme_print_flags flags)
-{
- if (flags & BINARY) {
- if (buf)
- return d_raw(buf, len);
- return;
- }
-
- printf("dir-receive: type:%#x operation:%#x spec:%#x nsid:%#x result:%#x\n",
- type, oper, spec, nsid, result);
- if (flags & VERBOSE)
- nvme_directive_show_fields(type, oper, result, buf);
- else if (buf)
- d(buf, len, 16, 1);
-}
-
-static const char *nvme_plm_window(__u32 plm)
-{
- switch (plm & 0x7) {
- case 1:
- return "Deterministic Window (DTWIN)";
- case 2:
- return "Non-deterministic Window (NDWIN)";
- default:
- return "Reserved";
- }
-}
-static void nvme_show_plm_config(struct nvme_plm_config *plmcfg)
-{
- printf("\tEnable Event :%04x\n", le16_to_cpu(plmcfg->ee));
- printf("\tDTWIN Reads Threshold :%"PRIu64"\n", le64_to_cpu(plmcfg->dtwinrt));
- printf("\tDTWIN Writes Threshold:%"PRIu64"\n", le64_to_cpu(plmcfg->dtwinwt));
- printf("\tDTWIN Time Threshold :%"PRIu64"\n", le64_to_cpu(plmcfg->dtwintt));
+ if (j)
+ nvme_print_object(j);
+ else
+ fprintf(stderr, "Unrecognized dtype:%d doper:%d\n", dtype, doper);
}
-void nvme_feature_show_fields(__u32 fid, unsigned int result, unsigned char *buf)
+void nvme_feature_show_fields(__u32 fid, unsigned int result, void **buf,
+ unsigned long flags)
{
- __u8 field;
- uint64_t ull;
-
- switch (fid) {
- case NVME_FEAT_FID_ARBITRATION:
- printf("\tHigh Priority Weight (HPW): %u\n", ((result & 0xff000000) >> 24) + 1);
- printf("\tMedium Priority Weight (MPW): %u\n", ((result & 0x00ff0000) >> 16) + 1);
- printf("\tLow Priority Weight (LPW): %u\n", ((result & 0x0000ff00) >> 8) + 1);
- printf("\tArbitration Burst (AB): ");
- if ((result & 0x00000007) == 7)
- printf("No limit\n");
- else
- printf("%u\n", 1 << (result & 0x00000007));
- break;
- case NVME_FEAT_FID_POWER_MGMT:
- field = (result & 0x000000E0) >> 5;
- printf("\tWorkload Hint (WH): %u - %s\n", field, nvme_feature_wl_hints_to_string(field));
- printf("\tPower State (PS): %u\n", result & 0x0000001f);
- break;
- case NVME_FEAT_FID_LBA_RANGE:
- field = result & 0x0000003f;
- printf("\tNumber of LBA Ranges (NUM): %u\n", field + 1);
- nvme_show_lba_range((struct nvme_lba_range_type *)buf, field);
- break;
- case NVME_FEAT_FID_TEMP_THRESH:
- field = (result & 0x00300000) >> 20;
- printf("\tThreshold Type Select (THSEL): %u - %s\n", field, nvme_feature_temp_type_to_string(field));
- field = (result & 0x000f0000) >> 16;
- printf("\tThreshold Temperature Select (TMPSEL): %u - %s\n", field, nvme_feature_temp_sel_to_string(field));
- printf("\tTemperature Threshold (TMPTH): %d C\n", (result & 0x0000ffff) - 273);
- break;
- case NVME_FEAT_FID_ERR_RECOVERY:
- printf("\tDeallocated or Unwritten Logical Block Error Enable (DULBE): %s\n", ((result & 0x00010000) >> 16) ? "Enabled":"Disabled");
- printf("\tTime Limited Error Recovery (TLER): %u ms\n", (result & 0x0000ffff) * 100);
- break;
- case NVME_FEAT_FID_VOLATILE_WC:
- printf("\tVolatile Write Cache Enable (WCE): %s\n", (result & 0x00000001) ? "Enabled":"Disabled");
- break;
- case NVME_FEAT_FID_NUM_QUEUES:
- printf("\tNumber of IO Completion Queues Allocated (NCQA): %u\n", ((result & 0xffff0000) >> 16) + 1);
- printf("\tNumber of IO Submission Queues Allocated (NSQA): %u\n", (result & 0x0000ffff) + 1);
- break;
- case NVME_FEAT_FID_IRQ_COALESCE:
- printf("\tAggregation Time (TIME): %u usec\n", ((result & 0x0000ff00) >> 8) * 100);
- printf("\tAggregation Threshold (THR): %u\n", (result & 0x000000ff) + 1);
- break;
- case NVME_FEAT_FID_IRQ_CONFIG:
- printf("\tCoalescing Disable (CD): %s\n", ((result & 0x00010000) >> 16) ? "True":"False");
- printf("\tInterrupt Vector (IV): %u\n", result & 0x0000ffff);
- break;
- case NVME_FEAT_FID_WRITE_ATOMIC:
- printf("\tDisable Normal (DN): %s\n", (result & 0x00000001) ? "True":"False");
- break;
- case NVME_FEAT_FID_ASYNC_EVENT:
- printf("\tTelemetry Log Notices : %s\n", ((result & 0x00000400) >> 10) ? "Send async event":"Do not send async event");
- printf("\tFirmware Activation Notices : %s\n", ((result & 0x00000200) >> 9) ? "Send async event":"Do not send async event");
- printf("\tNamespace Attribute Notices : %s\n", ((result & 0x00000100) >> 8) ? "Send async event":"Do not send async event");
- printf("\tSMART / Health Critical Warnings: %s\n", (result & 0x000000ff) ? "Send async event":"Do not send async event");
- break;
- case NVME_FEAT_FID_AUTO_PST:
- printf("\tAutonomous Power State Transition Enable (APSTE): %s\n", (result & 0x00000001) ? "Enabled":"Disabled");
- nvme_show_auto_pst((struct nvme_feat_auto_pst *)buf);
- break;
- case NVME_FEAT_FID_HOST_MEM_BUF:
- printf("\tMemory Return (MR): %s\n", ((result & 0x00000002) >> 1) ? "True":"False");
- printf("\tEnable Host Memory (EHM): %s\n", (result & 0x00000001) ? "Enabled":"Disabled");
- break;
- case NVME_FEAT_FID_SW_PROGRESS:
- printf("\tPre-boot Software Load Count (PBSLC): %u\n", result & 0x000000ff);
- break;
- case NVME_FEAT_FID_PLM_CONFIG:
- printf("\tPredictable Latency Window Enabled: %s\n", result & 0x1 ? "True":"False");
- nvme_show_plm_config((struct nvme_plm_config *)buf);
- break;
- case NVME_FEAT_FID_PLM_WINDOW:
- printf("\tWindow Select: %s", nvme_plm_window(result));
- break;
- case NVME_FEAT_FID_HOST_ID:
- ull = buf[7]; ull <<= 8; ull |= buf[6]; ull <<= 8; ull |= buf[5]; ull <<= 8;
- ull |= buf[4]; ull <<= 8; ull |= buf[3]; ull <<= 8; ull |= buf[2]; ull <<= 8;
- ull |= buf[1]; ull <<= 8; ull |= buf[0];
- printf("\tHost Identifier (HOSTID): %" PRIu64 "\n", ull);
- break;
- case NVME_FEAT_FID_RESV_MASK:
- printf("\tMask Reservation Preempted Notification (RESPRE): %s\n", ((result & 0x00000008) >> 3) ? "True":"False");
- printf("\tMask Reservation Released Notification (RESREL): %s\n", ((result & 0x00000004) >> 2) ? "True":"False");
- printf("\tMask Registration Preempted Notification (REGPRE): %s\n", ((result & 0x00000002) >> 1) ? "True":"False");
- break;
- case NVME_FEAT_FID_RESV_PERSIST:
- printf("\tPersist Through Power Loss (PTPL): %s\n", (result & 0x00000001) ? "True":"False");
- break;
- case NVME_FEAT_FID_WRITE_PROTECT:
- printf("\tNamespace Write Protect: %s\n", result != NVME_FEAT_NS_NO_WRITE_PROTECT ? "True" : "False");
- break;
- case NVME_FEAT_FID_TIMESTAMP:
- nvme_show_timestamp((struct nvme_timestamp *)buf);
- break;
- case NVME_FEAT_FID_HCTM:
- printf("\tThermal Management Temperature 1 (TMT1) : %u Kelvin\n", (result >> 16));
- printf("\tThermal Management Temperature 2 (TMT2) : %u Kelvin\n", (result & 0x0000ffff));
- break;
- case NVME_FEAT_FID_KATO:
- printf("\tKeep Alive Timeout (KATO) in milliseconds: %u\n", result);
- break;
- case NVME_FEAT_FID_NOPSC:
- printf("\tNon-Operational Power State Permissive Mode Enable (NOPPME): %s\n", (result & 1) ? "True" : "False");
- break;
- case NVME_FEAT_FID_HOST_BEHAVIOR:
- printf("\tHost Behavior Support: %s\n", (buf[0] & 0x1) ? "True" : "False");
- break;
- }
+ nvme_print_object(nvme_feature_to_json(fid, result, 0, buf, flags));
}
void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
enum nvme_print_flags flags)
{
- int idx;
-
- if (flags & BINARY)
- return d_raw((unsigned char *)list, len);
-
- printf("Number of LBA Status Descriptors(NLSD): %" PRIu64 "\n",
- le64_to_cpu(list->nlsd));
- printf("Completion Condition(CMPC): %u\n", list->cmpc);
-
- switch (list->cmpc) {
- case 1:
- printf("\tCompleted due to transferring the amount of data"\
- " specified in the MNDW field\n");
- break;
- case 2:
- printf("\tCompleted due to having performed the action\n"\
- "\tspecified in the Action Type field over the\n"\
- "\tnumber of logical blocks specified in the\n"\
- "\tRange Length field\n");
- break;
- }
-
- for (idx = 0; idx < list->nlsd; idx++) {
- struct nvme_lba_status_desc *e = &list->descs[idx];
- printf("{ DSLBA: 0x%016"PRIu64", NLB: 0x%08x, Status: 0x%02x }\n",
- le64_to_cpu(e->dslba), le32_to_cpu(e->nlb),
- e->status);
- }
-}
-
-#if 0
-static void nvme_show_list_item(struct nvme_namespace *n)
-{
- long long lba = 1 << n->ns.lbaf[(n->ns.flbas & 0x0f)].ds;
- double nsze = le64_to_cpu(n->ns.nsze) * lba;
- double nuse = le64_to_cpu(n->ns.nuse) * lba;
-
- const char *s_suffix = suffix_si_get(&nsze);
- const char *u_suffix = suffix_si_get(&nuse);
- const char *l_suffix = suffix_binary_get(&lba);
-
- char usage[128];
- char format[128];
-
- sprintf(usage,"%6.2f %2sB / %6.2f %2sB", nuse, u_suffix,
- nsze, s_suffix);
- sprintf(format,"%3.0f %2sB + %2d B", (double)lba, l_suffix,
- le16_to_cpu(n->ns.lbaf[(n->ns.flbas & 0x0f)].ms));
- printf("/dev/%-11s %-*.*s %-*.*s %-9d %-26s %-16s %-.*s\n", n->name,
- (int)sizeof(n->ctrl->id.sn), (int)sizeof(n->ctrl->id.sn), n->ctrl->id.sn,
- (int)sizeof(n->ctrl->id.mn), (int)sizeof(n->ctrl->id.mn), n->ctrl->id.mn,
- n->nsid, usage, format, (int)sizeof(n->ctrl->id.fr), n->ctrl->id.fr);
-}
-
-static void nvme_show_simple_list(struct nvme_root *t)
-{
- int i, j, k;
-
- printf("%-16s %-20s %-40s %-9s %-26s %-16s %-8s\n",
- "Node", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev");
- printf("%-.16s %-.20s %-.40s %-.9s %-.26s %-.16s %-.8s\n", dash, dash,
- dash, dash, dash, dash, dash);
-
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
-
- for (j = 0; j < s->nr_ctrls; j++) {
- struct nvme_ctrl *c = &s->ctrls[j];
-
- for (k = 0; k < c->nr_namespaces; k++) {
- struct nvme_namespace *n = &c->namespaces[k];
- nvme_show_list_item(n);
- }
- }
-
- for (j = 0; j < s->nr_namespaces; j++) {
- struct nvme_namespace *n = &s->namespaces[j];
- nvme_show_list_item(n);
- }
- }
-}
-
-static void nvme_show_details_ns(struct nvme_namespace *n, bool ctrl)
-{
- long long lba = 1 << n->ns.lbaf[(n->ns.flbas & 0x0f)].ds;
- double nsze = le64_to_cpu(n->ns.nsze) * lba;
- double nuse = le64_to_cpu(n->ns.nuse) * lba;
-
- const char *s_suffix = suffix_si_get(&nsze);
- const char *u_suffix = suffix_si_get(&nuse);
- const char *l_suffix = suffix_binary_get(&lba);
-
- char usage[128];
- char format[128];
-
- sprintf(usage,"%6.2f %2sB / %6.2f %2sB", nuse, u_suffix,
- nsze, s_suffix);
- sprintf(format,"%3.0f %2sB + %2d B", (double)lba, l_suffix,
- le16_to_cpu(n->ns.lbaf[(n->ns.flbas & 0x0f)].ms));
-
- printf("%-12s %-8x %-26s %-16s ", n->name, n->nsid, usage, format);
-
- if (ctrl)
- printf("%s", n->ctrl->name);
- else {
- struct nvme_subsystem *s = n->ctrl->subsys;
- int i;
-
- for (i = 0; i < s->nr_ctrls; i++)
- printf("%s%s", i ? ", " : "", s->ctrls[i].name);
- }
- printf("\n");
-}
-
-static void nvme_show_detailed_list(struct nvme_root *t)
-{
- int i, j, k;
-
- printf("NVM Express Subsystems\n\n");
- printf("%-16s %-96s %-.16s\n", "Subsystem", "Subsystem-NQN", "Controllers");
- printf("%-.16s %-.96s %-.16s\n", dash, dash, dash);
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
-
- printf("%-16s %-96s ", s->name, s->subsysnqn);
- for (j = 0; j < s->nr_ctrls; j++) {
- struct nvme_ctrl *c = &s->ctrls[j];
-
- printf("%s%s", j ? ", " : "", c->name);
- }
- printf("\n");
- };
-
- printf("\nNVM Express Controllers\n\n");
- printf("%-8s %-20s %-40s %-8s %-6s %-14s %-12s %-16s\n", "Device",
- "SN", "MN", "FR", "TxPort", "Address", "Subsystem", "Namespaces");
- printf("%-.8s %-.20s %-.40s %-.8s %-.6s %-.14s %-.12s %-.16s\n", dash, dash,
- dash, dash, dash, dash, dash, dash);
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
-
- for (j = 0; j < s->nr_ctrls; j++) {
- bool comma = false;
- struct nvme_ctrl *c = &s->ctrls[j];
-
- printf("%-8s %-.20s %-.40s %-.8s %-6s %-14s %-12s ",
- c->name, c->id.sn, c->id.mn, c->id.fr,
- c->transport, c->address, s->name);
-
- for (k = 0; k < c->nr_namespaces; k++) {
- struct nvme_namespace *n = &c->namespaces[k];
- printf("%s%s", comma ? ", " : "", n->name);
- comma = true;
- }
- for (k = 0; k < s->nr_namespaces; k++) {
- struct nvme_namespace *n = &s->namespaces[k];
- printf("%s%s", comma ? ", " : "", n->name);
- comma = true;
- }
- printf("\n");
- }
- }
-
- printf("\nNVM Express Namespaces\n\n");
- printf("%-12s %-8s %-26s %-16s %-16s\n", "Device", "NSID", "Usage", "Format", "Controllers");
- printf("%-.12s %-.8s %-.26s %-.16s %-.16s\n", dash, dash, dash, dash, dash);
-
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
-
- for (j = 0; j < s->nr_ctrls; j++) {
- struct nvme_ctrl *c = &s->ctrls[j];
-
- for (k = 0; k < c->nr_namespaces; k++) {
- struct nvme_namespace *n = &c->namespaces[k];
- nvme_show_details_ns(n, true);
- }
- }
- for (j = 0; j < s->nr_namespaces; j++) {
- struct nvme_namespace *n = &s->namespaces[j];
- nvme_show_details_ns(n, false);
- }
- }
-}
-
-static void json_detail_ns(struct nvme_namespace *n, struct json_object *ns_attrs)
-{
- long long lba;
- double nsze, nuse;
-
- lba = 1 << n->ns.lbaf[(n->ns.flbas & 0x0f)].ds;
- nsze = le64_to_cpu(n->ns.nsze) * lba;
- nuse = le64_to_cpu(n->ns.nuse) * lba;
-
- json_object_add_value_string(ns_attrs, "NameSpace", n->name);
- json_object_add_value_uint(ns_attrs, "NSID", n->nsid);
-
- json_object_add_value_uint(ns_attrs, "UsedBytes", nuse);
- json_object_add_value_uint(ns_attrs, "MaximumLBA",
- le64_to_cpu(n->ns.nsze));
- json_object_add_value_uint(ns_attrs, "PhysicalSize", nsze);
- json_object_add_value_uint(ns_attrs, "SectorSize", lba);
-}
-
-static void json_detail_list(struct nvme_root *t)
-{
- int i, j, k;
- struct json_object *root;
- struct json_array *devices;
- char formatter[41] = { 0 };
-
- root = json_create_object();
- devices = json_create_array();
-
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
- struct json_object *subsys_attrs;
- struct json_array *namespaces, *ctrls;
-
- subsys_attrs = json_create_object();
- json_object_add_value_string(subsys_attrs, "Subsystem", s->name);
- json_object_add_value_string(subsys_attrs, "SubsystemNQN", s->subsysnqn);
-
- ctrls = json_create_array();
- json_object_add_value_array(subsys_attrs, "Controllers", ctrls);
- for (j = 0; j < s->nr_ctrls; j++) {
- struct json_object *ctrl_attrs = json_create_object();
- struct nvme_ctrl *c = &s->ctrls[j];
- struct json_array *namespaces;
-
- json_object_add_value_string(ctrl_attrs, "Controller", c->name);
- json_object_add_value_string(ctrl_attrs, "Transport", c->transport);
- json_object_add_value_string(ctrl_attrs, "Address", c->address);
- json_object_add_value_string(ctrl_attrs, "State", c->state);
-
- format(formatter, sizeof(formatter), c->id.fr, sizeof(c->id.fr));
- json_object_add_value_string(ctrl_attrs, "Firmware", formatter);
-
- format(formatter, sizeof(formatter), c->id.mn, sizeof(c->id.mn));
- json_object_add_value_string(ctrl_attrs, "ModelNumber", formatter);
-
- format(formatter, sizeof(formatter), c->id.sn, sizeof(c->id.sn));
- json_object_add_value_string(ctrl_attrs, "SerialNumber", formatter);
-
- namespaces = json_create_array();
-
- for (k = 0; k < c->nr_namespaces; k++) {
- struct json_object *ns_attrs = json_create_object();
- struct nvme_namespace *n = &c->namespaces[k];
-
- json_detail_ns(n, ns_attrs);
- json_array_add_value_object(namespaces, ns_attrs);
- }
- if (k)
- json_object_add_value_array(ctrl_attrs, "Namespaces", namespaces);
- else
- json_free_array(namespaces);
-
- json_array_add_value_object(ctrls, ctrl_attrs);
- }
-
- namespaces = json_create_array();
- for (k = 0; k < s->nr_namespaces; k++) {
- struct json_object *ns_attrs = json_create_object();
- struct nvme_namespace *n = &s->namespaces[k];
-
- json_detail_ns(n, ns_attrs);
- json_array_add_value_object(namespaces, ns_attrs);
- }
- if (k)
- json_object_add_value_array(subsys_attrs, "Namespaces", namespaces);
- else
- json_free_array(namespaces);
-
- json_array_add_value_object(devices, subsys_attrs);
- }
-
- json_object_add_value_array(root, "Devices", devices);
- json_print_object(root, NULL);
- printf("\n");
- json_free_object(root);
-}
-
-static void json_simple_ns(struct nvme_namespace *n, struct json_array *devices)
-{
- struct json_object *device_attrs;
- char formatter[41] = { 0 };
- double nsze, nuse;
- int index = -1;
- long long lba;
- char *devnode;
-
- if (asprintf(&devnode, "/dev/%s", n->name) < 0)
- return;
-
- device_attrs = json_create_object();
- json_object_add_value_int(device_attrs, "NameSpace", n->nsid);
-
- json_object_add_value_string(device_attrs, "DevicePath", devnode);
- free(devnode);
-
- format(formatter, sizeof(formatter),
- n->ctrl->id.fr,
- sizeof(n->ctrl->id.fr));
-
- json_object_add_value_string(device_attrs, "Firmware", formatter);
-
- if (sscanf(n->ctrl->name, "nvme%d", &index) == 1)
- json_object_add_value_int(device_attrs, "Index", index);
-
- format(formatter, sizeof(formatter),
- n->ctrl->id.mn,
- sizeof(n->ctrl->id.mn));
-
- json_object_add_value_string(device_attrs, "ModelNumber", formatter);
-
- if (index >= 0) {
- char *product = nvme_product_name(index);
-
- json_object_add_value_string(device_attrs, "ProductName", product);
- free((void*)product);
- }
-
- format(formatter, sizeof(formatter),
- n->ctrl->id.sn,
- sizeof(n->ctrl->id.sn));
-
- json_object_add_value_string(device_attrs, "SerialNumber", formatter);
-
- lba = 1 << n->ns.lbaf[(n->ns.flbas & 0x0f)].ds;
- nsze = le64_to_cpu(n->ns.nsze) * lba;
- nuse = le64_to_cpu(n->ns.nuse) * lba;
-
- json_object_add_value_uint(device_attrs, "UsedBytes", nuse);
- json_object_add_value_uint(device_attrs, "MaximumLBA",
- le64_to_cpu(n->ns.nsze));
- json_object_add_value_uint(device_attrs, "PhysicalSize", nsze);
- json_object_add_value_uint(device_attrs, "SectorSize", lba);
-
- json_array_add_value_object(devices, device_attrs);
-}
-
-static void json_simple_list(struct nvme_root *t)
-{
- struct json_object *root;
- struct json_array *devices;
- int i, j, k;
-
- root = json_create_object();
- devices = json_create_array();
- for (i = 0; i < t->nr_subsystems; i++) {
- struct nvme_subsystem *s = &t->subsystems[i];
-
- for (j = 0; j < s->nr_ctrls; j++) {
- struct nvme_ctrl *c = &s->ctrls[j];
-
- for (k = 0; k < c->nr_namespaces; k++) {
- struct nvme_namespace *n = &c->namespaces[k];
- json_simple_ns(n, devices);
- }
- }
-
- for (j = 0; j < s->nr_namespaces; j++) {
- struct nvme_namespace *n = &s->namespaces[j];
- json_simple_ns(n, devices);
- }
- }
- json_object_add_value_array(root, "Devices", devices);
- json_print_object(root, NULL);
-}
-
-static void json_print_list_items(struct nvme_root *t,
- enum nvme_print_flags flags)
-{
- if (flags & VERBOSE)
- json_detail_list(t);
- else
- json_simple_list(t);
-}
-#endif
-
-void nvme_show_list_items(struct nvme_root *t, enum nvme_print_flags flags)
-{
-#if 0
- if (flags & JSON)
- json_print_list_items(t, flags);
- else if (flags & VERBOSE)
- nvme_show_detailed_list(t);
- else
- nvme_show_simple_list(t);
-#endif
+ nvme_lba_status_desc_list_to_json(list, flags);
}
+++ /dev/null
-#ifndef NVME_PRINT_H
-#define NVME_PRINT_H
-
-#include "nvme.h"
-#include "util/json.h"
-#include <inttypes.h>
-
-void d(unsigned char *buf, int len, int width, int group);
-void d_raw(unsigned char *buf, unsigned len);
-uint64_t int48_to_long(__u8 *data);
-
-void nvme_show_relatives(const char *name);
-
-void __nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode,
- void (*vendor_show)(__u8 *vs, struct json_object *root));
-void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, unsigned int mode);
-void nvme_show_id_ns(struct nvme_id_ns *ns, unsigned int nsid,
- enum nvme_print_flags flags);
-void nvme_show_resv_report(struct nvme_reservation_status *status, int bytes, __u32 cdw11,
- enum nvme_print_flags flags);
-void nvme_show_lba_range(struct nvme_lba_range_type *lbrt, int nr_ranges);
-void nvme_show_error_log(struct nvme_error_log_page *err_log, int entries,
- const char *devname, enum nvme_print_flags flags);
-void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
- const char *devname, enum nvme_print_flags flags);
-void nvme_show_ana_log(struct nvme_ana_log *ana_log, const char *devname,
- enum nvme_print_flags flags, size_t len);
-void nvme_show_self_test_log(struct nvme_self_test_log *self_test, const char *devname,
- enum nvme_print_flags flags);
-void nvme_show_fw_log(struct nvme_firmware_slot *fw_log, const char *devname,
- enum nvme_print_flags flags);
-void nvme_show_effects_log(struct nvme_cmd_effects_log *effects, unsigned int flags);
-void nvme_show_changed_ns_list_log(struct nvme_ns_list *log,
- const char *devname, enum nvme_print_flags flags);
-void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
- __u16 group_id, const char *devname, enum nvme_print_flags flags);
-void nvme_show_sanitize_log(struct nvme_sanitize_log_page *sanitize,
- const char *devname, enum nvme_print_flags flags);
-void nvme_show_ctrl_registers(void *bar, bool fabrics, enum nvme_print_flags flags);
-void nvme_show_single_property(int offset, uint64_t prop, int human);
-void nvme_show_id_ns_descs(void *data, unsigned nsid, enum nvme_print_flags flags);
-void nvme_show_lba_status(struct nvme_lba_status *list, unsigned long len,
- enum nvme_print_flags flags);
-void nvme_show_list_items(struct nvme_root *r, enum nvme_print_flags flags);
-void nvme_show_id_nvmset(struct nvme_id_nvmset_list *nvmset, unsigned nvmset_id,
- enum nvme_print_flags flags);
-void nvme_show_list_secondary_ctrl(const struct nvme_secondary_ctrl_list *sc_list,
- __u32 count, enum nvme_print_flags flags);
-void nvme_show_id_ns_granularity_list(const struct nvme_id_ns_granularity_list *glist,
- enum nvme_print_flags flags);
-void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list,
- enum nvme_print_flags flags);
-
-void nvme_feature_show_fields(__u32 fid, unsigned int result, unsigned char *buf);
-void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
- void *buf, __u32 len, enum nvme_print_flags flags);
-void nvme_show_select_result(__u32 result);
-
-const char *nvme_status_to_string(__u32 status);
-const char *nvme_select_to_string(int sel);
-const char *nvme_feature_to_string(int feature);
-const char *nvme_register_to_string(int reg);
-
-#endif
--- /dev/null
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/param.h>
+#include <json-c/json.h>
+#include <uuid/uuid.h>
+
+#include "user-types.h"
+
+static const char dash[101] = {[0 ... 99] = '-'};
+
+void d(unsigned char *buf, int len, int width, int group)
+{
+ int i, offset = 0;
+ char ascii[32 + 1];
+ bool line_done = false;
+
+ printf(" ");
+ for (i = 0; i <= 15; i++)
+ printf("%3x", i);
+
+ for (i = 0; i < len; i++) {
+ line_done = false;
+ if (i % width == 0)
+ printf( "\n%04x:", offset);
+ if (i % group == 0)
+ printf( " %02x", buf[i]);
+ else
+ printf( "%02x", buf[i]);
+ ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
+ if (((i + 1) % width) == 0) {
+ ascii[i % width + 1] = '\0';
+ printf( " \"%.*s\"", width, ascii);
+ offset += width;
+ line_done = true;
+ }
+ }
+
+ if (!line_done) {
+ unsigned b = width - (i % width);
+
+ ascii[i % width + 1] = '\0';
+ printf( " %*s \"%.*s\"",
+ 2 * b + b / group + (b % group ? 1 : 0), "",
+ width, ascii);
+ }
+ printf( "\n");
+}
+
+void d_raw(unsigned char *buf, unsigned len)
+{
+ unsigned i;
+ for (i = 0; i < len; i++)
+ putchar(*(buf+i));
+}
+
+#define ARRAY_SIZE(x) sizeof(x) / sizeof(*x)
+#define ARGSTR(s, i) arg_str(s, ARRAY_SIZE(s), i)
+
+static const char *arg_str(const char * const *strings,
+ size_t array_size, size_t idx)
+{
+ if (idx < array_size && strings[idx])
+ return strings[idx];
+ return "unrecognized";
+}
+
+static const char * const features_sels[] = {
+ [NVME_GET_FEATURES_SEL_CURRENT] = "current",
+ [NVME_GET_FEATURES_SEL_DEFAULT] = "default",
+ [NVME_GET_FEATURES_SEL_SAVED] = "saved",
+};
+
+const char *nvme_get_feature_select_to_string(__u8 sel)
+{
+ return ARGSTR(features_sels, sel);
+}
+
+static const char * const cntrltypes[] = {
+ [NVME_CTRL_CNTRLTYPE_IO] = "io",
+ [NVME_CTRL_CNTRLTYPE_DISCOVERY] = "discovery",
+ [NVME_CTRL_CNTRLTYPE_ADMIN] = "administrative",
+};
+
+const char *nvme_id_ctrl_cntrltype_str(__u8 cntrltype)
+{
+ return ARGSTR(cntrltypes, cntrltype);
+}
+
+static const char * const rbtypes[] = {
+ [NVME_NS_DLFEAT_RB_NR] = "not-reported",
+ [NVME_NS_DLFEAT_RB_ALL_0S] = "all-0s",
+ [NVME_NS_DLFEAT_RB_ALL_FS] = "all-fs",
+};
+
+const char *id_ns_dlfeat_rb_str(__u8 rb)
+{
+ return ARGSTR(rbtypes, rb);
+}
+
+static const char * const dpstypes[] = {
+ [NVME_NS_DPS_PI_NONE] = "none",
+ [NVME_NS_DPS_PI_TYPE1] = "type1",
+ [NVME_NS_DPS_PI_TYPE2] = "type2",
+ [NVME_NS_DPS_PI_TYPE3] = "type3",
+};
+
+const char *nvme_id_ns_dps_str(__u8 dps)
+{
+ return ARGSTR(dpstypes, dps);
+}
+
+static const char * const rptypes[] = {
+ [NVME_LBAF_RP_BEST] = "best",
+ [NVME_LBAF_RP_BETTER] = "better",
+ [NVME_LBAF_RP_GOOD] = "good",
+ [NVME_LBAF_RP_DEGRADED] = "degraded",
+};
+
+const char *nvme_id_ns_lbaf_rp_str(__u8 rp)
+{
+ return ARGSTR(rptypes, rp);
+}
+
+static const char * const featuretypes[] = {
+ [NVME_FEAT_FID_ARBITRATION] = "Arbitration",
+ [NVME_FEAT_FID_POWER_MGMT] = "Power Management",
+ [NVME_FEAT_FID_LBA_RANGE] = "LBA Range Type",
+ [NVME_FEAT_FID_TEMP_THRESH] = "Temperature Threshold",
+ [NVME_FEAT_FID_ERR_RECOVERY] = "Error Recovery",
+ [NVME_FEAT_FID_VOLATILE_WC] = "Volatile Write Cache",
+ [NVME_FEAT_FID_NUM_QUEUES] = "Number of Queues",
+ [NVME_FEAT_FID_IRQ_COALESCE] = "Interrupt Coalescing",
+ [NVME_FEAT_FID_IRQ_CONFIG] = "Interrupt Vector Configuration",
+ [NVME_FEAT_FID_WRITE_ATOMIC] = "Write Atomicity Normal",
+ [NVME_FEAT_FID_ASYNC_EVENT] = "Async Event Configuration",
+ [NVME_FEAT_FID_AUTO_PST] = "Autonomous Power State Transition",
+ [NVME_FEAT_FID_HOST_MEM_BUF] = "Host Memory Buffer",
+ [NVME_FEAT_FID_TIMESTAMP] = "Timestamp",
+ [NVME_FEAT_FID_KATO] = "Keep Alive Timer",
+ [NVME_FEAT_FID_HCTM] = "Host Controlled Thermal Management",
+ [NVME_FEAT_FID_NOPSC] = "Non-Operational Power State Config",
+ [NVME_FEAT_FID_RRL] = "Read Recovery Level Config",
+ [NVME_FEAT_FID_PLM_CONFIG] = "Predicatable Latency Mode Config",
+ [NVME_FEAT_FID_PLM_WINDOW] = "Predicatable Latency Mode Window",
+ [NVME_FEAT_FID_LBA_STS_INTERVAL] = "LBA Status Infomration Report Interval",
+ [NVME_FEAT_FID_HOST_BEHAVIOR] = "Host Behavior Support",
+ [NVME_FEAT_FID_SANITIZE] = "Sanitize Config",
+ [NVME_FEAT_FID_ENDURANCE_EVT_CFG] = "Endurance Group Event Configuration",
+ [NVME_FEAT_FID_SW_PROGRESS] = "Software Progress",
+ [NVME_FEAT_FID_HOST_ID] = "Host Identifier",
+ [NVME_FEAT_FID_RESV_MASK] = "Reservation Notification Mask",
+ [NVME_FEAT_FID_RESV_PERSIST] = "Reservation Persistence",
+ [NVME_FEAT_FID_WRITE_PROTECT] = "Namespce Write Protection Config",
+};
+
+const char *nvme_feature_str(__u8 fid)
+{
+ return ARGSTR(featuretypes, fid);
+}
+
+static const char * const nidttypes[] = {
+ [NVME_NIDT_EUI64] = "eui64",
+ [NVME_NIDT_NGUID] = "nguid",
+ [NVME_NIDT_UUID] = "uuid",
+};
+
+const char *nvme_id_nsdesc_nidt_str(__u8 nidt)
+{
+ return ARGSTR(nidttypes, nidt);
+}
+
+static const char * const associationtypes[] = {
+ [NVME_ID_UUID_ASSOCIATION_NONE] = "none",
+ [NVME_ID_UUID_ASSOCIATION_VENDOR] = "vendor",
+ [NVME_ID_UUID_ASSOCIATION_SUBSYSTEM_VENDOR] = "subsystem-vendor",
+};
+
+const char *nvme_id_uuid_assoc_str(__u8 assoc)
+{
+ return ARGSTR(associationtypes, assoc);
+}
+
+static const char * const trtypes[] = {
+ [NVMF_TRTYPE_UNSPECIFIED] = "unspecified",
+ [NVMF_TRTYPE_RDMA] = "rdma",
+ [NVMF_TRTYPE_FC] = "fc",
+ [NVMF_TRTYPE_TCP] = "tcp",
+ [NVMF_TRTYPE_LOOP] = "loop",
+};
+
+const char *nvmf_trtype_str(__u8 trtype)
+{
+ return ARGSTR(trtypes, trtype);
+}
+
+static const char * const adrfams[] = {
+ [NVMF_ADDR_FAMILY_PCI] = "pci",
+ [NVMF_ADDR_FAMILY_IP4] = "ipv4",
+ [NVMF_ADDR_FAMILY_IP6] = "ipv6",
+ [NVMF_ADDR_FAMILY_IB] = "infiniband",
+ [NVMF_ADDR_FAMILY_FC] = "fibre-channel",
+};
+
+const char *nvmf_adrfam_str(__u8 adrfam)
+{
+ return ARGSTR(adrfams, adrfam);
+}
+
+static const char * const subtypes[] = {
+ [NVME_NQN_DISC] = "discovery subsystem",
+ [NVME_NQN_NVME] = "nvme subsystem",
+};
+
+const char *nvmf_subtype_str(__u8 subtype)
+{
+ return ARGSTR(subtypes, subtype);
+}
+
+static const char * const treqs[] = {
+ [NVMF_TREQ_NOT_SPECIFIED] = "not specified",
+ [NVMF_TREQ_REQUIRED] = "required",
+ [NVMF_TREQ_NOT_REQUIRED] = "not required",
+ [NVMF_TREQ_DISABLE_SQFLOW] = "not specified, sq flow control disable supported",
+};
+
+const char *nvmf_treq_str(__u8 treq)
+{
+ return ARGSTR(treqs, treq);
+}
+
+static const char * const sectypes[] = {
+ [NVMF_TCP_SECTYPE_NONE] = "none",
+ [NVMF_TCP_SECTYPE_TLS] = "tls",
+};
+
+const char *nvmf_sectype_str(__u8 sectype)
+{
+ return ARGSTR(sectypes, sectype);
+}
+
+static const char * const prtypes[] = {
+ [NVMF_RDMA_PRTYPE_NOT_SPECIFIED] = "not specified",
+ [NVMF_RDMA_PRTYPE_IB] = "infiniband",
+ [NVMF_RDMA_PRTYPE_ROCE] = "roce",
+ [NVMF_RDMA_PRTYPE_ROCEV2] = "roce-v2",
+ [NVMF_RDMA_PRTYPE_IWARP] = "iwarp",
+};
+
+const char *nvmf_prtype_str(__u8 prtype)
+{
+ return ARGSTR(prtypes, prtype);
+}
+
+static const char * const qptypes[] = {
+ [NVMF_RDMA_QPTYPE_CONNECTED] = "connected",
+ [NVMF_RDMA_QPTYPE_DATAGRAM] = "datagram",
+};
+
+const char *nvmf_qptype_str(__u8 qptype)
+{
+ return ARGSTR(qptypes, qptype);
+}
+
+static const char * const cms[] = {
+ [NVMF_RDMA_CMS_RDMA_CM] = "rdma-cm",
+};
+
+const char *nvmf_cms_str(__u8 cm)
+{
+ return ARGSTR(cms, cm);
+}
+
+static const char * const generic_status[] = {
+ [NVME_SC_SUCCESS] = "Successful Completion: The command completed without error",
+ [NVME_SC_INVALID_OPCODE] = "Invalid Command Opcode: A reserved coded value or an unsupported value in the command opcode field",
+ [NVME_SC_INVALID_FIELD] = "Invalid Field in Command: A reserved coded value or an unsupported value in a defined field",
+ [NVME_SC_CMDID_CONFLICT] = "Command ID Conflict: The command identifier is already in use",
+ [NVME_SC_DATA_XFER_ERROR] = "Data Transfer Error: Transferring the data or metadata associated with a command experienced an error",
+ [NVME_SC_POWER_LOSS] = "Commands Aborted due to Power Loss Notification: Indicates that the command was aborted due to a power loss notification",
+ [NVME_SC_INTERNAL] = "Internal Error: The command was not completed successfully due to an internal error",
+ [NVME_SC_ABORT_REQ] = "Command Abort Requested: The command was aborted due to an Abort command",
+ [NVME_SC_ABORT_QUEUE] = "Command Aborted due to SQ Deletion: The command was aborted due to a Delete I/O Submission Queue",
+ [NVME_SC_FUSED_FAIL] = "Command Aborted due to Failed Fused Command: The command was aborted due to the other command in a fused operation failing",
+ [NVME_SC_FUSED_MISSING] = "Command Aborted due to Missing Fused Command: The fused command was aborted due to the adjacent submission queue entry not containing a fused command",
+ [NVME_SC_INVALID_NS] = "Invalid Namespace or Format: The namespace or the format of that namespace is invalid",
+ [NVME_SC_CMD_SEQ_ERROR] = "Command Sequence Error: The command was aborted due to a protocol violation in a multi- command sequence",
+ [NVME_SC_SGL_INVALID_LAST] = "Invalid SGL Segment Descriptor: The command includes an invalid SGL Last Segment or SGL Segment descriptor",
+ [NVME_SC_SGL_INVALID_COUNT] = "Invalid Number of SGL Descriptors: There is an SGL Last Segment descriptor or an SGL Segment descriptor in a location other than the last descriptor of a segment based on the length indicated",
+ [NVME_SC_SGL_INVALID_DATA] = "Data SGL Length Invalid: The length of a Data SGL is too short or too long and the controller does not support SGL transfers longer than the amount of data to be transferred",
+ [NVME_SC_SGL_INVALID_METADATA] = "Metadata SGL Length Invalid: The length of a Metadata SGL is too short or too long and the controller does not support SGL transfers longer than the amount of data to be transferred",
+ [NVME_SC_SGL_INVALID_TYPE] = "SGL Descriptor Type Invalid: The type of an SGL Descriptor is a type that is not supported by the controller",
+ [NVME_SC_CMB_INVALID_USE] = "Invalid Use of Controller Memory Buffer: The attempted use of the Controller Memory Buffer is not supported by the controller",
+ [NVME_SC_PRP_INVALID_OFFSET] = "PRP Offset Invalid: The Offset field for a PRP entry is invalid",
+ [NVME_SC_AWU_EXCEEDED] = "Atomic Write Unit Exceeded: The length specified exceeds the atomic write unit size",
+ [NVME_SC_OP_DENIED] = "Operation Denied: The command was denied due to lack of access rights",
+ [NVME_SC_SGL_INVALID_OFFSET] = "SGL Offset Invalid: The offset specified in a descriptor is invalid",
+ [NVME_SC_HOSTID_FORMAT] = "Host Identifier Inconsistent Format: The NVM subsystem detected the simultaneous use of 64- bit and 128-bit Host Identifier values on different controllers",
+ [NVME_SC_KAT_EXPIRED] = "Keep Alive Timer Expired: The Keep Alive Timer expired",
+ [NVME_SC_KAT_INVALID] = "Keep Alive Timeout Invalid: The Keep Alive Timeout value specified is invalid",
+ [NVME_SC_CMD_ABORTED_PREMEPT] = "Command Aborted due to Preempt and Abort: The command was aborted due to a Reservation Acquire command",
+ [NVME_SC_SANITIZE_FAILED] = "Sanitize Failed: The most recent sanitize operation failed and no recovery action has been successfully completed",
+ [NVME_SC_SANITIZE_IN_PROGRESS] = "Sanitize In Progress: The requested function is prohibited while a sanitize operation is in progress",
+ [NVME_SC_SGL_INVALID_GRANULARITY] = "SGL Data Block Granularity Invalid: The Address alignment or Length granularity for an SGL Data Block descriptor is invalid",
+ [NVME_SC_CMD_IN_CMBQ_NOT_SUPP] = "Command Not Supported for Queue in CMB: The controller does not support Submission Queue in the Controller Memory Buffer or Completion Queue in the Controller Memory Buffer",
+ [NVME_SC_NS_WRITE_PROTECTED] = "Namespace is Write Protected: The command is prohibited while the namespace is write protected",
+ [NVME_SC_CMD_INTERRUPTED] = "Command Interrupted: Command processing was interrupted and the controller is unable to successfully complete the command",
+ [NVME_SC_TRAN_TPORT_ERROR] = "Transient Transport Error: A transient transport error was detected",
+ [NVME_SC_LBA_RANGE] = "LBA Out of Range: The command references an LBA that exceeds the size of the namespace",
+ [NVME_SC_CAP_EXCEEDED] = "Capacity Exceeded: Execution of the command has caused the capacity of the namespace to be exceeded",
+ [NVME_SC_NS_NOT_READY] = "Namespace Not Ready: The namespace is not ready to be accessed",
+ [NVME_SC_RESERVATION_CONFLICT] = "Reservation Conflict: The command was aborted due to a conflict with a reservation held on the accessed namespace",
+ [NVME_SC_FORMAT_IN_PROGRESS] = "Format In Progress: A Format NVM command is in progress on the namespace",
+};
+
+static const char * const cmd_spec_status[] = {
+ [NVME_SC_CQ_INVALID] = "Completion Queue Invalid: The Completion Queue identifier specified in the command does not exist",
+ [NVME_SC_QID_INVALID] = "Invalid Queue Identifier: The creation of the I/O Completion Queue failed due to an invalid queue identifier specified as part of the command",
+ [NVME_SC_QUEUE_SIZE] = "Invalid Queue Size: The host attempted to create an I/O Completion Queue with an invalid number of entries",
+ [NVME_SC_ABORT_LIMIT] = "Abort Command Limit Exceeded: The number of concurrently outstanding Abort commands has exceeded the limit indicated in the Identify Controller data structure",
+ [NVME_SC_ASYNC_LIMIT] = "Asynchronous Event Request Limit Exceeded: The number of concurrently outstanding Asynchronous Event Request commands has been exceeded",
+ [NVME_SC_FIRMWARE_SLOT] = "Invalid Firmware Slot: The firmware slot indicated is invalid or read only",
+ [NVME_SC_FIRMWARE_IMAGE] = "Invalid Firmware Image: The firmware image specified for activation is invalid and not loaded by the controller",
+ [NVME_SC_INVALID_VECTOR] = "Invalid Interrupt Vector: The creation of the I/O Completion Queue failed due to an invalid interrupt vector specified as part of the command",
+ [NVME_SC_INVALID_LOG_PAGE] = "Invalid Log Page: The log page indicated is invalid",
+ [NVME_SC_INVALID_FORMAT] = "Invalid Format: The LBA Format specified is not supported",
+ [NVME_SC_FW_NEEDS_CONV_RESET] = "Firmware Activation Requires Conventional Reset: The firmware commit was successful, however, activation of the firmware image requires a conventional reset",
+ [NVME_SC_INVALID_QUEUE] = "Invalid Queue Deletion: Invalid I/O Completion Queue specified to delete",
+ [NVME_SC_FEATURE_NOT_SAVEABLE] = "Feature Identifier Not Saveable: The Feature Identifier specified does not support a saveable value",
+ [NVME_SC_FEATURE_NOT_CHANGEABLE] = "Feature Not Changeable: The Feature Identifier is not able to be changed",
+ [NVME_SC_FEATURE_NOT_PER_NS] = "Feature Not Namespace Specific: The Feature Identifier specified is not namespace specific",
+ [NVME_SC_FW_NEEDS_SUBSYS_RESET] = "Firmware Activation Requires NVM Subsystem Reset: The firmware commit was successful, however, activation of the firmware image requires an NVM Subsystem",
+ [NVME_SC_FW_NEEDS_RESET] = "Firmware Activation Requires Controller Level Reset: The firmware commit was successful; however, the image specified does not support being activated without a reset",
+ [NVME_SC_FW_NEEDS_MAX_TIME] = "Firmware Activation Requires Maximum Time Violation: The image specified if activated immediately would exceed the Maximum Time for Firmware Activation (MTFA) value reported in Identify Controller",
+ [NVME_SC_FW_ACTIVATE_PROHIBITED] = "Firmware Activation Prohibited: The image specified is being prohibited from activation by the controller for vendor specific reasons",
+ [NVME_SC_OVERLAPPING_RANGE] = "Overlapping Range: The downloaded firmware image has overlapping ranges",
+ [NVME_SC_NS_INSUFFICIENT_CAP] = "Namespace Insufficient Capacity: Creating the namespace requires more free space than is currently available",
+ [NVME_SC_NS_ID_UNAVAILABLE] = "Namespace Identifier Unavailable: The number of namespaces supported has been exceeded",
+ [NVME_SC_NS_ALREADY_ATTACHED] = "Namespace Already Attached: The controller is already attached to the namespace specified",
+ [NVME_SC_NS_IS_PRIVATE] = "Namespace Is Private: The namespace is private and is already attached to one controller",
+ [NVME_SC_NS_NOT_ATTACHED] = "Namespace Not Attached: The request to detach the controller could not be completed because the controller is not attached to the namespace",
+ [NVME_SC_THIN_PROV_NOT_SUPP] = "Thin Provisioning Not Supported: Thin provisioning is not supported by the controller",
+ [NVME_SC_CTRL_LIST_INVALID] = "Controller List Invalid: The controller list provided contains invalid controller ids",
+ [NVME_SC_SELF_TEST_IN_PROGRESS] = "Device Self-test In Progress",
+ [NVME_SC_BP_WRITE_PROHIBITED] = "Boot Partition Write Prohibited: The command tried to modify a locked Boot Partition",
+ [NVME_SC_INVALID_CTRL_ID] = "Invalid Controller Identifier: An invalid controller id was specified",
+ [NVME_SC_INVALID_SEC_CTRL_STATE] = "Invalid Secondary Controller State: The requested secondary controller action is invalid based on the secondary and primary controllers current states",
+ [NVME_SC_INVALID_CTRL_RESOURCES] = "Invalid Number of Controller Resources: The specified number of Flexible Resources is invalid",
+ [NVME_SC_INVALID_RESOURCE_ID] = "Invalid Resource Identifier: At least one of the specified resource identifiers was invalid",
+ [NVME_SC_PMR_SAN_PROHIBITED] = "Sanitize Prohibited While Persistent Memory Region is Enabled",
+ [NVME_SC_ANA_GROUP_ID_INVALID] = "ANA Group Identifier Invalid",
+ [NVME_SC_ANA_ATTACH_FAILED] = "ANA Attach Failed: The command's specified ANA Group Identifier is not supported",
+};
+
+static const char * const nvm_status[] = {
+ [NVME_SC_BAD_ATTRIBUTES] = "Conflicting Attributes: The attributes specified in the command are conflicting",
+ [NVME_SC_INVALID_PI] = "Invalid Protection Information: The command's Protection Information Field settings are invalid for the namespace's Protection Information format",
+ [NVME_SC_READ_ONLY] = "Attempted Write to Read Only Range: The LBA range specified contains read-only blocks",
+};
+
+static const char * const nvmf_status[] = {
+ [NVME_SC_CONNECT_FORMAT] = "Incompatible Format: The NVM subsystem does not support the record format specified by the host",
+ [NVME_SC_CONNECT_CTRL_BUSY] = "Controller Busy: The controller is already associated with a host",
+ [NVME_SC_CONNECT_INVALID_PARAM] = "Connect Invalid Parameters: One or more of the command parameters",
+ [NVME_SC_CONNECT_RESTART_DISC] = "Connect Restart Discovery: The NVM subsystem requested is not available",
+ [NVME_SC_CONNECT_INVALID_HOST] = "Connect Invalid Host: The host is not allowed to establish an association to either any controller in the NVM subsystem or the specified controller",
+ [NVME_SC_DISCONNECT_INVALID_QTYPE] = "Invalid Queue Type: The command was sent on the wrong queue type",
+ [NVME_SC_DISCOVERY_RESTART] = "Discover Restart: The snapshot of the records is now invalid or out of date",
+ [NVME_SC_AUTH_REQUIRED] = "Authentication Required: NVMe in-band authentication is required and the queue has not yet been authenticated",
+};
+
+static const char * const media_status[] = {
+ [NVME_SC_WRITE_FAULT] = "Write Fault: The write data could not be committed to the media",
+ [NVME_SC_READ_ERROR] = "Unrecovered Read Error: The read data could not be recovered from the media",
+ [NVME_SC_GUARD_CHECK] = "End-to-end Guard Check Error: The command was aborted due to an end-to-end guard check failure",
+ [NVME_SC_APPTAG_CHECK] = "End-to-end Application Tag Check Error: The command was aborted due to an end-to-end application tag check failure",
+ [NVME_SC_REFTAG_CHECK] = "End-to-end Reference Tag Check Error: The command was aborted due to an end-to-end reference tag check failure",
+ [NVME_SC_COMPARE_FAILED] = "Compare Failure: The command failed due to a miscompare during a Compare command",
+ [NVME_SC_ACCESS_DENIED] = "Access Denied: Access to the namespace and/or LBA range is denied due to lack of access rights",
+ [NVME_SC_UNWRITTEN_BLOCK] = "Deallocated or Unwritten Logical Block: The command failed due to an attempt to read from or verify an LBA range containing a deallocated or unwritten logical block",
+};
+
+static const char * const path_status[] = {
+ [NVME_SC_ANA_INTERNAL_PATH_ERROR] = "Internal Path Error: An internal error specific to the controller processing the commmand prevented completion",
+ [NVME_SC_ANA_PERSISTENT_LOSS] = "Asymmetric Access Persistent Loss: The controller is in a persistent loss state with the requested namespace",
+ [NVME_SC_ANA_INACCESSIBLE] = "Asymmetric Access Inaccessible: The controller is in an inaccessible state with the requested namespace",
+ [NVME_SC_ANA_TRANSITION] = "Asymmetric Access Transition: The controller is currently transitioning states with the requested namespace",
+ [NVME_SC_CTRL_PATH_ERROR] = "Controller Pathing Error: A pathing error was detected by the controller",
+ [NVME_SC_HOST_PATH_ERROR] = "Host Pathing Error: A pathing error was detected by the host",
+ [NVME_SC_CMD_ABORTED_BY_HOST] = "Command Aborted By Host: The command was aborted as a result of host action",
+};
+
+const char *nvme_status_to_string(int status, bool fabrics)
+{
+ const char *s = NULL;
+ __u16 sc, sct;
+
+ if (status < 0)
+ return strerror(errno);
+
+ sc = status & NVME_SC_MASK;
+ sct = status & NVME_SCT_MASK;
+
+ switch (sct) {
+ case NVME_SCT_GENERIC:
+ s = ARGSTR(generic_status, sc);
+ break;
+ case NVME_SCT_CMD_SPECIFIC:
+ if (sc < ARRAY_SIZE(cmd_spec_status))
+ s = ARGSTR(cmd_spec_status, sc);
+ else if (fabrics)
+ s = ARGSTR(nvmf_status, sc);
+ else
+ s = ARGSTR(nvm_status, sc);
+ break;
+ case NVME_SCT_MEDIA:
+ s = ARGSTR(media_status, sc);
+ break;
+ case NVME_SCT_PATH:
+ s = ARGSTR(path_status, sc);
+ break;
+ case NVME_SCT_VS:
+ s = "Vendor Specific Status";
+ break;
+ default:
+ s = "Unknown status";
+ break;
+ }
+
+ return s;
+}
+
+static const char *iec[] = {
+ "B",
+ "KiB",
+ "MiB",
+ "GiB",
+ "TiB",
+ "PiB",
+ "EiB",
+ "ZiB",
+ "YiB",
+};
+
+static const char *jedec[] = {
+ "B",
+ "KB",
+ "MB",
+ "GB",
+ "TB",
+ "PB",
+ "EB",
+ "ZB",
+ "YB",
+};
+
+static void util_split(uint64_t v, uint16_t *idx, uint16_t *major,
+ uint16_t *minor, uint16_t *jmajor, uint16_t *jminor)
+{
+ uint64_t lower = 0, upper = v, mag = 1;
+ int i, j;
+
+ for (i = 0; i < ARRAY_SIZE(iec) - 1 && upper > 1024; i++)
+ upper >>= 10;
+ if (i)
+ lower = (((v - (upper << (10 * i))) >> ((i - 1) * 10)) * 100) >> 10;;
+
+ *major = upper;
+ *minor = lower;
+ *idx = i;
+
+ if (!jmajor)
+ return;
+
+ for (j = 0; j < i; j++)
+ mag *= 1000;
+ upper = v / mag;
+
+ if (j)
+ lower = (v % mag) / (mag / 1000);
+ *jmajor = upper;
+ *jminor = lower;
+}
+
+static int __display_human_size128(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint16_t i = 0, major = 0, minor = 0, jmajor = 0, jminor = 0, bias = 0;
+ uint8_t *s = (uint8_t *)json_object_get_string(o);
+ uint64_t upper = 0, lower = 0, v;
+ int j;
+
+ for (j = 7; j >= 0; j--) {
+ lower = lower * 256 + s[j];
+ upper = upper * 256 + s[j + 8];
+ }
+
+ if (upper) {
+ if (upper >= (1 << 16)) {
+ v = upper >> 6;
+ bias = 7;
+ } else if (upper >= (1 << 6)) {
+ v = (upper << 4) | (lower >> 60);
+ bias = 6;
+ } else {
+ v = (upper << 14) | (lower >> 50);
+ bias = 5;
+ }
+ } else
+ v = lower;
+
+ util_split(v, &i, &major, &minor, &jmajor, &jminor);
+ return sprintbuf(p, "%u.%02u %s (%lu.%03lu %s)", major, minor,
+ iec[i + bias], jmajor, jminor, jedec[i + bias]);
+}
+
+static int __display_human_size128_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ __display_human_size128(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int __display_human_size(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint16_t i = 0, major = 0, minor = 0, jmajor = 0, jminor = 0;
+ uint64_t v = json_object_get_int64(o);
+
+ util_split(v, &i, &major, &minor, &jmajor, &jminor);
+ return sprintbuf(p, "%u.%02u %s (%lu.%03lu %s)", major, minor, iec[i],
+ jmajor, jminor, jedec[i]);
+}
+
+static int __display_human_size_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ __display_human_size(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int _display_human_size(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint64_t v = json_object_get_int64(o);
+ uint16_t i = 0, major = 0, minor = 0;
+
+ util_split(v, &i, &major, &minor, NULL, NULL);
+ if (minor)
+ return sprintbuf(p, "%u.%02u %s", major, minor, iec[i]);
+ return sprintbuf(p, "%u %s", major, iec[i]);
+}
+
+static int _display_human_size_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ _display_human_size(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_human_size(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint16_t i = 0, major = 0, minor = 0;
+ uint64_t v = json_object_get_int64(o);
+
+ util_split(v, &i, &major, &minor, NULL, NULL);
+ return sprintbuf(p, "%u %s", major, iec[i]);
+}
+
+static int display_human_size_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_human_size(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_binary(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ void *s = (void *)json_object_get_string(o);
+ int len = json_object_get_string_len(o);
+
+ d_raw(s, len);
+ return 0;
+}
+
+static int display_hex_array(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint8_t *s = (uint8_t *)json_object_get_string(o);
+ int i, len = json_object_get_string_len(o);
+
+ for (i = 0; i < len; i++)
+ sprintbuf(p, "%02x", s[i]);
+ return 0;
+}
+
+static int display_hex_array_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_hex_array(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static void util_wrap_string(struct printbuf *pb, const char *s, int indent)
+{
+ const int width = 76;
+ const char *c, *t;
+ char *p, *wrap = malloc(strlen(s) * 4);
+ int next_space = -1;
+ int last_line = indent;
+
+ p = wrap;
+ for (c = s; *c != 0; c++) {
+ if (*c == '\n')
+ goto new_line;
+
+ if (*c == ' ' || next_space < 0) {
+ next_space = 0;
+ for (t = c + 1; *t != 0 && *t != ' '; t++)
+ next_space++;
+
+ if (((int)(c - s) + indent + next_space) >
+ (last_line - indent + width)) {
+new_line:
+ if (*(c + 1) == 0)
+ continue;
+ last_line = (int) (c-s) + indent;
+ p += sprintf(p, "\n%-*s", indent, "");
+ continue;
+ }
+ }
+ p += sprintf(p, "%c", *c);
+ }
+ p += sprintf(p, "\n");
+ printbuf_memappend(pb, wrap, strlen(wrap));
+ free(wrap);
+}
+
+static int util_count_child_primitive_objects(struct json_object *j)
+{
+ int i = 0;
+
+ json_object_object_foreach(j, key, val) {
+ (void)key;
+
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ i++;
+ break;
+ default:
+ break;
+ }
+ }
+
+ return i;
+}
+
+static void util_set_column_widths(struct json_object *j, int *widths)
+{
+ int i = 0;
+
+ json_object_object_foreach(j, key, val) {
+ (void)key;
+
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ if (strlen(json_object_to_json_string(val)) + 2 > widths[i]) {
+ widths[i] =
+ strlen(json_object_to_json_string(val)) + 2;
+ }
+ i++;
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+static void util_init_column_widths(struct json_object *j, int *widths)
+{
+ int i = 0;
+
+ json_object_object_foreach(j, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ break;
+ default:
+ continue;;
+ }
+
+ widths[i++] = strlen(key);
+ }
+}
+
+static void util_print_column_widths(struct json_object *j, int *widths,
+ int indent, struct printbuf *p)
+{
+ int i = 0;
+
+ json_object_object_foreach(j, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ break;
+ default:
+ continue;
+ }
+
+ sprintbuf(p, "%-*s%*s", i ? 1 : 0, "", widths[i], key);
+ i++;
+ }
+ printbuf_memappend(p, "\n", 1);
+
+ i = 0;
+ sprintbuf(p, "%-.*s : ", indent, dash);
+ json_object_object_foreach(j, _key, _val) {
+ (void)_key;
+
+ switch (json_object_get_type(_val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ break;
+ default:
+ continue;
+ }
+
+ sprintbuf(p, "%-*s%-.*s", i ? 1 : 0, "", widths[i], dash);
+ i++;
+ }
+ printbuf_memappend(p, "\n", 1);
+}
+
+static void util_print_values(struct json_object *j, int *widths,
+ struct printbuf *p)
+{
+ int i = 0;
+
+ json_object_object_foreach(j, key, val) {
+ (void)key;
+
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ break;
+ default:
+ continue;
+ }
+
+ sprintbuf(p, "%-*s%*s", i ? 1 : 0, "", widths[i], json_object_to_json_string(val));
+ i++;
+ }
+ printbuf_memappend(p, "\n", 1);
+}
+
+static void nvme_print_compact_json_array(struct printbuf *p, struct json_object *o, char *key, int space)
+{
+ size_t i, len = json_object_array_length(o);
+ int *widths, indent = strlen(key);
+ struct json_object *jso;
+
+ if (!len)
+ return;
+
+ jso = json_object_array_get_idx(o, 0);
+ if (json_object_get_type(jso) != json_type_object)
+ return;
+
+ i = util_count_child_primitive_objects(jso);
+ if (!i)
+ return;
+
+ widths = calloc(i, sizeof(*widths));
+ if (!widths)
+ return;
+
+ widths = calloc(i, sizeof(*widths));
+ util_init_column_widths(jso, widths);
+ for (i = 0; i < len; i++) {
+ jso = json_object_array_get_idx(o, i);
+ util_set_column_widths(jso, widths);
+ }
+
+ sprintbuf(p, "%-*s : ", space, key);
+ jso = json_object_array_get_idx(o, 0);
+ util_print_column_widths(jso, widths, indent, p);
+ for (i = 0; i < len; i++) {
+ jso = json_object_array_get_idx(o, i);
+ sprintbuf(p, "%*d : ", indent, i);
+ util_print_values(jso, widths, p);
+ }
+}
+
+int display_compact_object(struct json_object *jso, struct printbuf *p,
+ int level, int flags)
+{
+ int ret, l = 0, i = 0;
+ char *buf;
+
+ json_object_object_foreach(jso, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ sprintbuf(p, "%s%s:%s", i ? " " : "", key,
+ json_object_to_json_string(val));
+ i++;
+ break;
+ case json_type_object:
+ sprintbuf(p, "\n%-*s:", l, key);
+ l++;
+ ret = asprintf(&buf, "%*s%s", l * 2, "",
+ json_object_to_json_string(val));
+ l--;
+ if (ret < 0 || !buf)
+ break;
+ util_wrap_string(p, buf, (l + 1) * 2);
+ free(buf);
+ break;
+ case json_type_array:
+ nvme_print_compact_json_array(p, val, key, l + 2);
+ break;
+ default:
+ break;
+ }
+ }
+ printbuf_memappend(p, "\n", 1);
+
+ return 0;
+}
+
+int display_compact_object_str(struct json_object *jso, struct printbuf *p,
+ int level, int flags)
+{
+ int i = 0;
+
+ json_object_object_foreach(jso, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ sprintbuf(p, "%s\"%s\":%s", i ? ", " : "", key,
+ json_object_to_json_string(val));
+ i++;
+ break;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+static void nvme_print_json_array(struct printbuf *p, struct json_object *o, char *key, int space)
+{
+ size_t i, len = json_object_array_length(o);
+ char *buf;
+
+ for (i = 0; i < len; i++) {
+ int ret;
+
+ sprintbuf(p, "%-*s%3lu : ", space - 6, key, i);
+ ret = asprintf(&buf, "%s", json_object_to_json_string(
+ json_object_array_get_idx(o, i)));
+ if (ret < 0 || !buf)
+ break;
+ util_wrap_string(p, buf, space);
+ free(buf);
+ }
+}
+
+static int l = 0;
+
+int display_tabular(struct json_object *jso, struct printbuf *p,
+ int level, int flags)
+{
+ int ret, len = 0;
+ char *buf;
+
+ json_object_object_foreach(jso, tkey, tval) {
+ (void)tkey;
+
+ if (strlen(tkey) + 1 > len)
+ len = strlen(tkey) + 1;
+ }
+
+ json_object_object_foreach(jso, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ sprintbuf(p, "%-*s: %s\n", len, key,
+ json_object_to_json_string(val));
+ break;
+ case json_type_object:
+ sprintbuf(p, "%-*s:\n", len, key);
+ l++;
+ ret = asprintf(&buf, "%*s%s", l * 2, "",
+ json_object_to_json_string(val));
+ l--;
+ if (ret < 0 || !buf)
+ break;
+ util_wrap_string(p, buf, (l + 1) * 2);
+ free(buf);
+ break;
+ case json_type_array:
+ nvme_print_json_array(p, val, key, len + 2);
+ break;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+int display_tree(struct json_object *jso, struct printbuf *p,
+ int level, int flags)
+{
+ static char char_stack[16];
+ int j, i = 0;
+
+ if (l == 0)
+ printbuf_memappend(p, ".\n", 2);
+ if (l >= sizeof(char_stack))
+ return 0;
+
+ json_object_object_foreach(jso, tkey, tval) {
+ (void)tkey;
+
+ switch (json_object_get_type(tval)) {
+ case json_type_object:
+ i++;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (i > 1)
+ char_stack[l] = '|';
+ else
+ char_stack[l] = ' ';
+
+ printf("%s %d child objects\n", __func__, i);
+ json_object_object_foreach(jso, key, val) {
+ switch (json_object_get_type(val)) {
+ case json_type_boolean:
+ case json_type_double:
+ case json_type_int:
+ case json_type_string:
+ sprintbuf(p, " %s:%s%s", key,
+ json_object_to_json_string(val),
+ (i > 1) ? " " : "");
+ break;
+ case json_type_object:
+ for (j = 0; j < l; j++)
+ sprintbuf(p, "%c ", char_stack[j]);
+
+ l++;
+ sprintbuf(p, "%c-- %s - %s\n", (i > 1) ? '|' : '`',
+ key, json_object_to_json_string(val));
+ l--;
+ break;
+ case json_type_array:
+ l++;
+ sprintbuf(p, "%c-- %s - %s\n", (i > 1) ? '|' : '`',
+ key, json_object_to_json_string(val));
+ l--;
+ break;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+static int display_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ const char *s = json_object_get_string(o);
+ return printbuf_memappend(p, s, strlen(s));
+}
+
+static int display_int128(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint8_t *s = (uint8_t *)json_object_get_string(o);
+ long double result = 0;
+ char buf[40];
+ int i;
+
+ for (i = 15; i >= 0; i--)
+ result = result * 256 + s[i];
+
+ snprintf(buf, sizeof(buf), "%.0Lf", result);
+ return printbuf_memappend(p, buf, strlen(buf));
+}
+
+static int display_0x(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ return sprintbuf(p, "%#llx", json_object_get_int64(o));
+}
+
+static int display_0x_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_0x(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_hex(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ return sprintbuf(p, "%llx", json_object_get_int64(o));
+}
+
+static int display_hex_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_hex(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_percent(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ return sprintbuf(p, "%u%%", json_object_get_int(o));
+}
+
+static int display_percent_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_percent(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_temp_k(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint16_t k = json_object_get_int(o);
+ return sprintbuf(p, "%uC (%.2fF %uK)", k - 273,
+ ((k - 273.15) * 9.0 / 5.0) + 32, k);
+}
+
+static int display_temp_k_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_temp_k(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static const char *tsuffix[] = {
+ "µsec",
+ "msec",
+ "sec",
+};
+
+static int display_time_us(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint64_t d = 1, t, v = json_object_get_int64(o);
+ int i = 0;
+
+ t = v;
+ for (i = 0; i < 2 && (t / d) >= 1000; i++)
+ d *= 1000;
+
+ t /= d;
+ if (v % d)
+ return sprintbuf(p, "%u.%u %s", t, v % d, tsuffix[i]);
+ else
+ return sprintbuf(p, "%u %s", t, tsuffix[i]);
+}
+
+static int display_time_us_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_time_us(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_time_s(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint64_t m, h, d, v = json_object_get_int64(o);
+
+ if (!v) {
+ printbuf_memappend(p, "0", 1);
+ return 0;
+ }
+
+ d = v / (24 * 60 * 60);
+ v %= (24 * 60 * 60);
+
+ h = v / (60 * 60);
+ v %= (60 * 60);
+
+ m = v / 60;
+ v %= 60;
+
+ if (d)
+ sprintbuf(p, "%d day%s ", d, d > 1 ? "s" : "");
+ if (h)
+ sprintbuf(p, "%d hour%s ", h, h > 1 ? "s" : "");
+ if (m)
+ sprintbuf(p, "%d minute%s ", m, m > 1 ? "s" : "");
+ if (v)
+ sprintbuf(p, "%d second%s", v, v > 1 ? "s" : "");
+
+ return 0;
+}
+
+static int display_time_s_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_time_s(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_hu_watts(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint64_t v = json_object_get_int64(o);
+
+ if (v % 1000)
+ return sprintbuf(p, "%u.%04uW", v / 10000, v % 10000);
+ return sprintbuf(p, "%u.%02uW", v / 10000, (v % 10000) / 100);
+}
+
+static int display_hu_watts_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_hu_watts(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 0;
+}
+
+static int display_bool_terse(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ return sprintbuf(p, "%c", json_object_get_boolean(o) ? '+' : '-');
+}
+
+static int display_uuid(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ char buf[40];
+ uuid_t uuid;
+
+ memcpy((void *)uuid, json_object_get_string(o),
+ sizeof(uuid_t));
+ uuid_unparse(uuid, buf);
+ return printbuf_memappend(p, buf, strlen(buf));
+}
+
+static int display_uuid_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_uuid(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 1;
+}
+
+static int display_oui(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ uint8_t *value = (uint8_t *)json_object_get_string(o);
+ int i, len = json_object_get_string_len(o);
+
+ for (i = 0; i < len; i++)
+ sprintbuf(p, "%s%02x", i ? "-" : "", value[i]);
+ return 1;
+}
+
+static int display_oui_str(struct json_object *o, struct printbuf *p,
+ int l, int f)
+{
+ printbuf_memappend(p, "\"", 1);
+ display_oui(o, p, l, f);
+ printbuf_memappend(p, "\"", 1);
+
+ return 1;
+}
+
+static inline void fail_and_notify(void *o)
+{
+ if (o)
+ return;
+ fprintf(stderr,
+ "Allocation of memory for json object failed, aborting\n");
+ abort();
+}
+
+struct json_object *nvme_json_new_str_len(const char *v, int l)
+{
+ struct json_object *o = json_object_new_string_len(v, l);
+ fail_and_notify(o);
+ return o;
+}
+
+struct json_object *nvme_json_new_str_len_flags(const void *v, int l, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_str_len(v, l);
+ if (flags & NVME_JSON_BINARY)
+ json_object_set_serializer(o, display_binary, NULL, NULL);
+ else if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_str(const char *v, unsigned long flags)
+{
+ struct json_object *o = json_object_new_string(v);
+ fail_and_notify(o);
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_int128(uint8_t *v)
+{
+ struct json_object *o = nvme_json_new_str_len((const char *)v, 16);
+ json_object_set_serializer(o, display_int128, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_int64(uint64_t v)
+{
+ struct json_object *o = json_object_new_int64(v);
+ fail_and_notify(o);
+ return o;
+}
+
+struct json_object *nvme_json_new_int(uint32_t v)
+{
+ struct json_object *o = json_object_new_int(v);
+ fail_and_notify(o);
+ return o;
+}
+
+struct json_object *nvme_json_new_bool(bool v)
+{
+ struct json_object *o = json_object_new_boolean(v);
+ fail_and_notify(o);
+ return o;
+}
+
+struct json_object *nvme_json_new_object(unsigned long flags)
+{
+ struct json_object *o = json_object_new_object();
+ fail_and_notify(o);
+
+ if (flags & NVME_JSON_COMPACT) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_compact_object, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_compact_object_str, NULL, NULL);
+ } else if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_tabular, NULL, NULL);
+
+ return o;
+}
+
+struct json_object *nvme_json_new_array()
+{
+ struct json_object *o = json_object_new_array();
+ fail_and_notify(o);
+ return o;
+}
+
+struct json_object *nvme_json_new_storage_128(uint8_t *v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int128(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, __display_human_size128, NULL, NULL);
+ else
+ json_object_set_serializer(o, __display_human_size128_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_storage(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, __display_human_size, NULL, NULL);
+ else
+ json_object_set_serializer(o, __display_human_size_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_size(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, _display_human_size, NULL, NULL);
+ else
+ json_object_set_serializer(o, _display_human_size_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_memory(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_human_size, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_human_size_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_hex_array(uint8_t *v, uint32_t l)
+{
+ struct json_object *o = nvme_json_new_str_len((const char *)v, l);
+ json_object_set_serializer(o, display_hex_array_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_hex(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_hex, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_hex_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_0x(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (v && (flags & NVME_JSON_HUMAN)) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_0x, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_0x_str, NULL, NULL);
+ }
+ return o;
+}
+
+struct json_object *nvme_json_new_percent(uint8_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int(v);
+ if (flags & NVME_JSON_HUMAN) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_percent, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_percent_str, NULL, NULL);
+ }
+ return o;
+}
+
+struct json_object *nvme_json_new_temp(uint16_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int(v);
+ if (flags & NVME_JSON_HUMAN) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_temp_k, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_temp_k_str, NULL, NULL);
+ }
+ return o;
+}
+
+struct json_object *nvme_json_new_time_us(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_time_us, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_time_us_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_time_s(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_time_s, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_time_s_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_hecto_uwatts(uint64_t v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_int64(v);
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_hu_watts, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_hu_watts_str, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_json_new_uuid(uint8_t *v, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_str_len((const char *)v, 16);
+
+ if (flags & NVME_JSON_HUMAN) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_uuid, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_uuid_str, NULL, NULL);
+ } else {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_hex_array, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_hex_array_str, NULL, NULL);
+ }
+ return o;
+}
+
+struct json_object *nvme_json_new_oui(uint8_t *v, int len, unsigned long flags)
+{
+ struct json_object *o = nvme_json_new_str_len((const char *)v, len);
+ if (flags & NVME_JSON_HUMAN) {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_oui, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_oui_str, NULL, NULL);
+ } else {
+ if (flags & NVME_JSON_TABULAR)
+ json_object_set_serializer(o, display_hex_array, NULL, NULL);
+ else
+ json_object_set_serializer(o, display_hex_array_str, NULL, NULL);
+ }
+ return o;
+}
+
+struct json_object *nvme_json_new_bool_terse(bool v)
+{
+ struct json_object *o = nvme_json_new_bool(v);
+ json_object_set_serializer(o, display_bool_terse, NULL, NULL);
+ return o;
+}
+
+struct json_object *nvme_identify_directives_to_json(
+ struct nvme_id_directives *idd, unsigned long flags)
+{
+ struct json_object *jidd, *js, *je;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(idd, sizeof(*idd), flags);
+
+ jidd = nvme_json_new_object(flags);
+ js = nvme_json_new_object(flags);
+ je = nvme_json_new_object(flags);
+
+ nvme_json_add_bool(js, "id", bit_set(idd->supported, NVME_ID_DIR_ID_BIT));
+ nvme_json_add_bool(js, "sd", bit_set(idd->supported, NVME_ID_DIR_SD_BIT));
+ json_object_object_add(jidd, "supported", js);
+
+ nvme_json_add_bool(je, "id", bit_set(idd->enabled, NVME_ID_DIR_ID_BIT));
+ nvme_json_add_bool(je, "sd", bit_set(idd->enabled, NVME_ID_DIR_SD_BIT));
+ json_object_object_add(jidd, "enabled", je);
+
+ return jidd;
+}
+
+struct json_object *nvme_streams_status_to_json(
+ struct nvme_streams_directive_status *sds, unsigned long flags)
+{
+ struct json_object *jsds, *jsids;
+ int i, psid = -1, osc = le16_to_cpu(sds->osc);
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(sds, sizeof(*sds), flags);
+
+ jsds = nvme_json_new_object(flags);
+ if (!jsds)
+ return NULL;
+
+ jsids = json_object_new_array();
+ if (!jsids) {
+ json_object_put(jsds);
+ return NULL;
+ }
+
+ nvme_json_add_int(jsds, "osc", osc);
+ for (i = 0; i < osc; i++) {
+ struct json_object *jsid;
+ __u16 sid;
+
+ sid = le16_to_cpu(sds->sid[i]);
+ if ((int)sid <= psid)
+ break;
+
+ psid = sid;
+ jsid = json_object_new_int(sid);
+ if (!jsid)
+ break;
+
+ json_object_array_add(jsids, jsid);
+ }
+ json_object_object_add(jsds, "sids", jsids);
+
+ return jsds;
+}
+
+struct json_object *nvme_streams_params_to_json(
+ struct nvme_streams_directive_params *sdp, unsigned long flags)
+{
+ struct json_object *jsdp;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(sdp, sizeof(*sdp), flags);
+
+ jsdp = nvme_json_new_object(flags);
+ if (!jsdp)
+ return NULL;
+
+ nvme_json_add_le16(jsdp, "msl", sdp->msl);
+ nvme_json_add_le16(jsdp, "nssa", sdp->nssa);
+ nvme_json_add_le16(jsdp, "nsso", sdp->nsso);
+ nvme_json_add_int(jsdp, "nssc", sdp->nssc);
+ nvme_json_add_le32(jsdp, "sws", sdp->sws);
+ nvme_json_add_le16(jsdp, "sgs", sdp->sgs);
+ nvme_json_add_le16(jsdp, "nsa", sdp->nsa);
+ nvme_json_add_le16(jsdp, "nso", sdp->nso);
+
+ return jsdp;
+}
+
+struct json_object *nvme_streams_allocated_to_json(__u16 nsa, unsigned long flags)
+{
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(&nsa, sizeof(nsa), flags);
+ return nvme_json_new_int(nsa);
+}
+
+static json_object *nvme_feat_simple_to_json(__u8 fid, __u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_json_new_object(flags);
+ nvme_json_add_int(jf, "feature-id", fid);
+ nvme_json_add_str(jf, "feature", nvme_feature_str(fid), flags);
+ nvme_json_add_int(jf, "value", value);
+
+ return jf;
+}
+
+static json_object *nvme_feat_arbitration_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_ARBITRATION, value, flags);
+ nvme_json_add_int(jf, "ab", NVME_FEAT_ARB_BURST(value));
+ nvme_json_add_int(jf, "lpw", NVME_FEAT_ARB_LPW(value));
+ nvme_json_add_int(jf, "mpw", NVME_FEAT_ARB_MPW(value));
+ nvme_json_add_int(jf, "hpw", NVME_FEAT_ARB_HPW(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_power_mgmt_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_POWER_MGMT, value, flags);
+ nvme_json_add_int(jf, "ps", NVME_FEAT_PM_PS(value));
+ nvme_json_add_int(jf, "wh", NVME_FEAT_PM_WH(value));
+
+ return jf;
+}
+
+static json_object *nvme_lba_range_to_json(struct nvme_lba_range_type_entry *lbar,
+ unsigned long flags)
+{
+ struct json_object *jlbar;
+
+ jlbar = nvme_json_new_object(flags);
+
+ nvme_json_add_int(jlbar, "type", lbar->type);
+ nvme_json_add_int(jlbar, "attrs", lbar->attributes);
+ nvme_json_add_le64(jlbar, "slba", lbar->slba);
+ nvme_json_add_le64(jlbar, "nlb", lbar->nlb);
+ nvme_json_add_hex_array(jlbar, "guid", (void *)lbar->guid, sizeof(lbar->guid));
+
+ return jlbar;
+}
+
+static json_object *nvme_feat_lba_range_to_json(__u32 value,
+ struct nvme_lba_range_type *lbar, unsigned long flags)
+{
+ struct json_object *jf, *jlbars;
+ int i, num = NVME_FEAT_LBAR_NR(value);
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_LBA_RANGE, value, flags);
+ nvme_json_add_int(jf, "num", num);
+
+ jlbars = nvme_json_new_array();
+ for (i = 0; i <= num; i++) {
+ struct json_object *jlbar;
+
+ jlbar = nvme_lba_range_to_json(&lbar->entry[i], flags);
+ if (!jlbar)
+ break;
+
+ json_object_array_add(jlbars, jlbar);
+ }
+ json_object_object_add(jf, "ranges", jlbars);
+
+ return jf;
+}
+
+static json_object *nvme_feat_temp_thresh_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_TEMP_THRESH, value, flags);
+ nvme_json_add_int(jf, "tmpth", NVME_FEAT_TT_TMPTH(value));
+ nvme_json_add_int(jf, "tmpsel", NVME_FEAT_TT_TMPSEL(value));
+ nvme_json_add_int(jf, "thsel", NVME_FEAT_TT_THSEL(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_err_recovery_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_ERR_RECOVERY, value, flags);
+ nvme_json_add_int(jf, "tler", NVME_FEAT_ER_TLER(value));
+ nvme_json_add_bool(jf, "dulbe", NVME_FEAT_ER_DULBE(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_volatile_wc_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_VOLATILE_WC, value, flags);
+ nvme_json_add_bool(jf, "wce", NVME_FEAT_VWC_WCE(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_num_queues_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_NUM_QUEUES, value, flags);
+ nvme_json_add_int(jf, "nsqr", NVME_FEAT_NRQS_NSQR(value));
+ nvme_json_add_int(jf, "ncqr", NVME_FEAT_NRQS_NCQR(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_irq_coalesce_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_IRQ_COALESCE, value, flags);
+ nvme_json_add_int(jf, "thr", NVME_FEAT_ICOAL_THR(value));
+ nvme_json_add_int(jf, "time", NVME_FEAT_ICOAL_TIME(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_irq_config_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_IRQ_CONFIG, value, flags);
+ nvme_json_add_int(jf, "iv", NVME_FEAT_ICFG_IV(value));
+ nvme_json_add_bool(jf, "cd", NVME_FEAT_ICFG_CD(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_write_atomic_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_WRITE_ATOMIC, value, flags);
+ nvme_json_add_bool(jf, "dn", NVME_FEAT_WA_DN(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_async_event_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_ASYNC_EVENT, value, flags);
+ nvme_json_add_int(jf, "smart", NVME_FEAT_AE_SMART(value));
+ nvme_json_add_bool(jf, "nan", NVME_FEAT_AE_NAN(value));
+ nvme_json_add_bool(jf, "fw", NVME_FEAT_AE_FW(value));
+ nvme_json_add_bool(jf, "telem", NVME_FEAT_AE_TELEM(value));
+ nvme_json_add_bool(jf, "ana", NVME_FEAT_AE_ANA(value));
+ nvme_json_add_bool(jf, "pla", NVME_FEAT_AE_PLA(value));
+ nvme_json_add_bool(jf, "lbas", NVME_FEAT_AE_LBAS(value));
+ nvme_json_add_bool(jf, "ega", NVME_FEAT_AE_EGA(value));
+
+ return jf;
+}
+
+static json_object *nvme_apst_to_json(__u64 value, unsigned long flags)
+{
+ struct json_object *japst;
+
+ japst = nvme_json_new_object(flags);
+ nvme_json_add_int(japst, "itps", (value & NVME_APST_ENTRY_ITPS_MASK) >>
+ NVME_APST_ENTRY_ITPS_SHIFT);
+ nvme_json_add_int(japst, "itpt", (value & NVME_APST_ENTRY_ITPT_MASK) >>
+ NVME_APST_ENTRY_ITPT_SHIFT);
+
+ return japst;
+}
+
+static json_object *nvme_feat_auto_pst_to_json(__u32 value,
+ struct nvme_feat_auto_pst *apst, unsigned long flags)
+{
+ struct json_object *jf, *japsts;
+ int i;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_AUTO_PST, value, flags);
+ nvme_json_add_bool(jf, "apste", NVME_FEAT_APST_APSTE(value));
+
+ japsts = nvme_json_new_array();
+ for (i = 0; i < 32; i++) {
+ struct json_object *japst;
+
+ japst = nvme_apst_to_json(le64_to_cpu(apst->apst_entry[i]), flags);
+ if (!japst)
+ break;
+
+ json_object_array_add(japsts, japst);
+ }
+ json_object_object_add(jf, "entries", japsts);
+
+ return jf;
+}
+
+static json_object *nvme_feat_host_mem_buf_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_HOST_MEM_BUF, value, flags);
+ nvme_json_add_bool(jf, "ehm", NVME_FEAT_HMEM_EHM(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_timestamp_to_json(__u32 value,
+ struct nvme_timestamp *ts, unsigned long flags)
+{
+ int timestamp = unalign_int(ts->timestamp, sizeof(ts->timestamp));
+ struct json_object *jf, *jtimestamp;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_TIMESTAMP, value, flags);
+ jtimestamp = nvme_json_new_object(flags);
+
+ nvme_json_add_int(jtimestamp, "timestamp", timestamp);
+ nvme_json_add_int(jtimestamp, "sync", ts->attr & 1);
+ nvme_json_add_int(jtimestamp, "origin", (ts->attr >> 1) & 0x7);
+
+ return jf;
+}
+
+static json_object *nvme_feat_kato_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_KATO, value, flags);
+
+ return jf;
+}
+
+static json_object *nvme_feat_hctm_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_HCTM, value, flags);
+ nvme_json_add_int(jf, "tmt2", NVME_FEAT_HCTM_TMT2(value));
+ nvme_json_add_int(jf, "tmt1", NVME_FEAT_HCTM_TMT1(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_nopsc_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_NOPSC, value, flags);
+ nvme_json_add_bool(jf, "noppme", NVME_FEAT_NOPS_NOPPME(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_rrl_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_RRL, value, flags);
+ nvme_json_add_int(jf, "rrl", NVME_FEAT_RRL_RRL(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_plm_config_to_json(__u32 value,
+ struct nvme_plm_config *plm, unsigned long flags)
+{
+ struct json_object *jf, *jplm;
+
+ jplm = nvme_json_new_object(flags);
+ nvme_json_add_le16(jplm, "ee", plm->ee);
+ nvme_json_add_le64(jplm, "dtwinrt", plm->dtwinrt);
+ nvme_json_add_le64(jplm, "dwtinwt", plm->dtwinwt);
+ nvme_json_add_le64(jplm, "dwtintt", plm->dtwintt);
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_PLM_CONFIG, value, flags);
+ nvme_json_add_bool(jf, "plme", NVME_FEAT_PLM_PLME(value));
+ json_object_object_add(jf, "plmcfg", jplm);
+
+ return jf;
+}
+
+static json_object *nvme_feat_plm_window_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_PLM_WINDOW, value, flags);
+ nvme_json_add_int(jf, "ws", NVME_FEAT_PLMW_WS(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_lba_sts_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_LBA_STS_INTERVAL, value, flags);
+ nvme_json_add_int(jf, "lsiri", NVME_FEAT_LBAS_LSIRI(value));
+ nvme_json_add_int(jf, "lsipi", NVME_FEAT_LBAS_LSIPI(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_host_behavior_to_json(__u32 value,
+ struct nvme_feat_host_behavior *host, unsigned long flags)
+{
+ struct json_object *jf, *jhost;
+
+ jhost = nvme_json_new_object(flags);
+ nvme_json_add_le16(jhost, "acre", host->acre);
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_HOST_BEHAVIOR, value, flags);
+ json_object_object_add(jf, "hbs", jhost);
+
+ return jf;
+}
+
+static json_object *nvme_feat_sanitize_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_SANITIZE, value, flags);
+ nvme_json_add_bool(jf, "nodrm", NVME_FEAT_SC_NODRM(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_endurance_evt_cfg_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_ENDURANCE_EVT_CFG, value, flags);
+ nvme_json_add_int(jf, "endgid", NVME_FEAT_EG_ENDGID(value));
+ nvme_json_add_int(jf, "endgcw", NVME_FEAT_EG_EGCW(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_sw_progress_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_SW_PROGRESS, value, flags);
+ nvme_json_add_int(jf, "pbslc", NVME_FEAT_SPM_PBSLC(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_host_id_to_json(__u32 value,
+ uint8_t *hostid, unsigned long flags)
+{
+ bool exhid = NVME_FEAT_HOSTID_EXHID(value);
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_HOST_ID, value, flags);
+ nvme_json_add_bool(jf, "exhid", exhid);
+
+ if (exhid)
+ nvme_json_add_int128(jf, "hostid", (void *)hostid);
+ else
+ nvme_json_add_int64(jf, "hostid", read64(hostid));
+
+ return jf;
+}
+
+static json_object *nvme_feat_resv_mask_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_RESV_MASK, value, flags);
+ nvme_json_add_bool(jf, "regpre", NVME_FEAT_RM_REGPRE(value));
+ nvme_json_add_bool(jf, "resrel", NVME_FEAT_RM_RESREL(value));
+ nvme_json_add_bool(jf, "respre", NVME_FEAT_RM_RESPRE(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_resv_persist_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_RESV_PERSIST, value, flags);
+ nvme_json_add_bool(jf, "ptpl", NVME_FEAT_RP_PTPL(value));
+
+ return jf;
+}
+
+static json_object *nvme_feat_write_protect_to_json(__u32 value,
+ unsigned long flags)
+{
+ struct json_object *jf;
+
+ jf = nvme_feat_simple_to_json(NVME_FEAT_FID_WRITE_PROTECT, value, flags);
+ nvme_json_add_int(jf, "wps", NVME_FEAT_WP_WPS(value));
+
+ return jf;
+}
+
+struct json_object *nvme_feature_to_json(__u8 fid, __u32 value, unsigned len,
+ void *data, unsigned long flags)
+{
+ if (!(flags & NVME_JSON_DECODE_COMPLEX))
+ return nvme_feat_simple_to_json(fid, value, flags);
+
+ switch (fid) {
+ case NVME_FEAT_FID_ARBITRATION:
+ return nvme_feat_arbitration_to_json(value, flags);
+ case NVME_FEAT_FID_POWER_MGMT:
+ return nvme_feat_power_mgmt_to_json(value, flags);
+ case NVME_FEAT_FID_LBA_RANGE:
+ return nvme_feat_lba_range_to_json(value, data, flags);
+ case NVME_FEAT_FID_TEMP_THRESH:
+ return nvme_feat_temp_thresh_to_json(value, flags);
+ case NVME_FEAT_FID_ERR_RECOVERY:
+ return nvme_feat_err_recovery_to_json(value, flags);
+ case NVME_FEAT_FID_VOLATILE_WC:
+ return nvme_feat_volatile_wc_to_json(value, flags);
+ case NVME_FEAT_FID_NUM_QUEUES:
+ return nvme_feat_num_queues_to_json(value, flags);
+ case NVME_FEAT_FID_IRQ_COALESCE:
+ return nvme_feat_irq_coalesce_to_json(value, flags);
+ case NVME_FEAT_FID_IRQ_CONFIG:
+ return nvme_feat_irq_config_to_json(value, flags);
+ case NVME_FEAT_FID_WRITE_ATOMIC:
+ return nvme_feat_write_atomic_to_json(value, flags);
+ case NVME_FEAT_FID_ASYNC_EVENT:
+ return nvme_feat_async_event_to_json(value, flags);
+ case NVME_FEAT_FID_AUTO_PST:
+ return nvme_feat_auto_pst_to_json(value, data, flags);
+ case NVME_FEAT_FID_HOST_MEM_BUF:
+ return nvme_feat_host_mem_buf_to_json(value, flags);
+ case NVME_FEAT_FID_TIMESTAMP:
+ return nvme_feat_timestamp_to_json(value, data, flags);
+ case NVME_FEAT_FID_KATO:
+ return nvme_feat_kato_to_json(value, flags);
+ case NVME_FEAT_FID_HCTM:
+ return nvme_feat_hctm_to_json(value, flags);
+ case NVME_FEAT_FID_NOPSC:
+ return nvme_feat_nopsc_to_json(value, flags);
+ case NVME_FEAT_FID_RRL:
+ return nvme_feat_rrl_to_json(value, flags);
+ case NVME_FEAT_FID_PLM_CONFIG:
+ return nvme_feat_plm_config_to_json(value, data, flags);
+ case NVME_FEAT_FID_PLM_WINDOW:
+ return nvme_feat_plm_window_to_json(value, flags);
+ case NVME_FEAT_FID_LBA_STS_INTERVAL:
+ return nvme_feat_lba_sts_to_json(value, flags);
+ case NVME_FEAT_FID_HOST_BEHAVIOR:
+ return nvme_feat_host_behavior_to_json(value, data, flags);
+ case NVME_FEAT_FID_SANITIZE:
+ return nvme_feat_sanitize_to_json(value, flags);
+ case NVME_FEAT_FID_ENDURANCE_EVT_CFG:
+ return nvme_feat_endurance_evt_cfg_to_json(value, flags);
+ case NVME_FEAT_FID_SW_PROGRESS:
+ return nvme_feat_sw_progress_to_json(value, flags);
+ case NVME_FEAT_FID_HOST_ID:
+ return nvme_feat_host_id_to_json(value, data, flags);
+ case NVME_FEAT_FID_RESV_MASK:
+ return nvme_feat_resv_mask_to_json(value, flags);
+ case NVME_FEAT_FID_RESV_PERSIST:
+ return nvme_feat_resv_persist_to_json(value, flags);
+ case NVME_FEAT_FID_WRITE_PROTECT:
+ return nvme_feat_write_protect_to_json(value, flags);
+ default:
+ return NULL;
+ }
+}
+
+static void nvme_json_add_id_ctrl_psd_human(struct json_object *j,
+ struct nvme_id_psd *psd, unsigned long flags)
+{
+ struct json_object *jpsd = nvme_json_new_object(flags);
+
+ bool mxps = is_set(psd->flags, NVME_PSD_FLAGS_MXPS);
+ uint8_t ips = nvme_psd_power_scale(psd->ips);
+ uint8_t aps = nvme_psd_power_scale(psd->aps);
+ uint16_t mp = le16_to_cpu(psd->mp);
+ uint16_t idlp = le16_to_cpu(psd->idlp);
+ uint16_t actp = le16_to_cpu(psd->actp);
+
+ switch (ips) {
+ case 1:
+ break;
+ case 2:
+ idlp *= 100;
+ break;
+ default:
+ idlp = 0;
+ break;
+ }
+
+ switch (aps) {
+ case 1:
+ break;
+ case 2:
+ actp *= 100;
+ break;
+ default:
+ actp = 0;
+ break;
+ }
+
+ if (!mxps)
+ mp *= 100;
+
+ nvme_json_add_power(jpsd, "mp", mp, flags);
+ nvme_json_add_bool(jpsd, "mxps", mxps);
+ nvme_json_add_flag(jpsd, "nops", psd->flags, NVME_PSD_FLAGS_NOPS);
+ nvme_json_add_time_us_flags(jpsd, "enlat", le32_to_cpu(psd->enlat), flags);
+ nvme_json_add_time_us_flags(jpsd, "exlat", le32_to_cpu(psd->exlat), flags);
+ nvme_json_add_int(jpsd, "rrt", psd->rrt);
+ nvme_json_add_int(jpsd, "rrl", psd->rrl);
+ nvme_json_add_int(jpsd, "rwt", psd->rwt);
+ nvme_json_add_int(jpsd, "rwl", psd->rwl);
+
+ if (ips)
+ nvme_json_add_power(jpsd, "idlp", idlp, flags);
+ else
+ nvme_json_add_bool(jpsd, "idlp", false);
+ nvme_json_add_int(jpsd, "ips", ips);
+
+ if (aps)
+ nvme_json_add_power(jpsd, "actp", actp, flags);
+ else
+ nvme_json_add_bool(jpsd, "actp", false);
+ nvme_json_add_int(jpsd, "apw", psd->apw);
+ nvme_json_add_int(jpsd, "aps", aps);
+
+ json_object_array_add(j, jpsd);
+}
+
+static void nvme_id_ctrl_psd_to_json(struct json_object *j,
+ struct nvme_id_psd *psd, unsigned long flags)
+{
+ struct json_object *jpsd = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jpsd, "mp", psd->mp);
+ nvme_json_add_flag_flags(jpsd, "mxps", psd->flags, NVME_PSD_FLAGS_MXPS, flags);
+ nvme_json_add_flag_flags(jpsd, "nops", psd->flags, NVME_PSD_FLAGS_NOPS, flags);
+ nvme_json_add_le32(jpsd, "enlat", psd->enlat);
+ nvme_json_add_le32(jpsd, "exlat", psd->exlat);
+ nvme_json_add_int(jpsd, "rrt", psd->rrt);
+ nvme_json_add_int(jpsd, "rrl", psd->rrl);
+ nvme_json_add_int(jpsd, "rwt", psd->rwt);
+ nvme_json_add_int(jpsd, "rwl", psd->rwl);
+ nvme_json_add_le16(jpsd, "idlp", psd->idlp);
+ nvme_json_add_int(jpsd, "ips", nvme_psd_power_scale(psd->ips));
+ nvme_json_add_le16(jpsd, "actp", psd->actp);
+ nvme_json_add_int(jpsd, "apw", psd->apw);
+ nvme_json_add_int(jpsd, "aps", nvme_psd_power_scale(psd->aps));
+
+ json_object_array_add(j, jpsd);
+}
+
+static void nvme_json_add_id_ctrl_psd(struct json_object *j,
+ struct nvme_id_psd *psd, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ return nvme_json_add_id_ctrl_psd_human(j, psd, flags);
+ return nvme_id_ctrl_psd_to_json(j, psd, flags);
+}
+
+static void nvme_json_add_id_ctrl_ieee(struct json_object *j, const char *n,
+ __u8 *ieee, unsigned long flags)
+{
+ /*
+ * See nvme specification 1.4 section 7.10.3 for why this byte swapping
+ * is done.
+ */
+ uint8_t i[3] = { ieee[2], ieee[1], ieee[0] };
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_oui(j, n, i, 3, flags);
+ else
+ nvme_json_add_int(j, n, nvme_ieee_to_int(ieee));
+}
+
+static void nvme_json_add_id_ctrl_cmic(struct json_object *j, const char *n,
+ __u8 cmic, unsigned long flags)
+{
+ struct json_object *jcmic;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, cmic, flags);
+ return;
+ }
+
+ jcmic = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jcmic, "value", cmic, flags);
+ nvme_json_add_flag_flags(jcmic, "mport", cmic, NVME_CTRL_CMIC_MULTI_PORT, flags);
+ nvme_json_add_flag_flags(jcmic, "mctrl", cmic, NVME_CTRL_CMIC_MULTI_CTRL, flags);
+ nvme_json_add_flag_flags(jcmic, "virtual", cmic, NVME_CTRL_CMIC_MULTI_SRIOV, flags);
+ nvme_json_add_flag_flags(jcmic, "anarep", cmic, NVME_CTRL_CMIC_MULTI_ANA_REPORTING, flags);
+
+ json_object_object_add(j, n, jcmic);
+}
+
+static void nvme_json_add_id_ctrl_mdts(struct json_object *j, const char *n,
+ __u8 mdts, unsigned long flags)
+{
+ uint64_t m = mdts ? (1ULL << (12ULL + mdts)) : 0;
+
+ if (!(flags & NVME_JSON_HUMAN))
+ nvme_json_add_int(j, n, mdts);
+ else if (m)
+ nvme_json_add_memory(j, n, m, flags);
+ else
+ nvme_json_add_str(j, n, "No Limit", flags);
+}
+
+static void nvme_json_add_id_ctrl_ver(struct json_object *j, const char *n,
+ __u32 ver, unsigned long flags)
+{
+ char buf[16];
+
+ if (!(flags & NVME_JSON_HUMAN)) {
+ nvme_json_add_int(j, n, ver);
+ return;
+ }
+
+ if (NVME_TERTIARY(ver))
+ sprintf(buf, "%u.%u.%u", NVME_MAJOR(ver), NVME_MINOR(ver),
+ NVME_TERTIARY(ver));
+ else
+ sprintf(buf, "%u.%u", NVME_MAJOR(ver), NVME_MINOR(ver));
+ nvme_json_add_str(j, n, buf, flags);
+}
+
+static void nvme_json_add_id_ctrl_oaes(struct json_object *j, const char *n,
+ __u32 oaes, unsigned long flags)
+{
+ struct json_object *joaes;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, oaes, flags);
+ return;
+ }
+
+ joaes = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(joaes, "value", oaes, flags);
+ nvme_json_add_flag_flags(joaes, "nsattr", oaes, NVME_CTRL_OAES_NA, flags);
+ nvme_json_add_flag_flags(joaes, "frmwa", oaes, NVME_CTRL_OAES_FA, flags);
+ nvme_json_add_flag_flags(joaes, "anachg", oaes, NVME_CTRL_OAES_ANA, flags);
+ nvme_json_add_flag_flags(joaes, "ple", oaes, NVME_CTRL_OAES_PLEA, flags);
+ nvme_json_add_flag_flags(joaes, "lbasts", oaes, NVME_CTRL_OAES_LBAS, flags);
+ nvme_json_add_flag_flags(joaes, "ege", oaes, NVME_CTRL_OAES_EGE, flags);
+
+ json_object_object_add(j, n, joaes);
+}
+
+static void nvme_json_add_id_ctrl_ctratt(struct json_object *j, const char *n,
+ __u32 ctratt, unsigned long flags)
+{
+ struct json_object *jctratt;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, ctratt, flags);
+ return;
+ }
+
+ jctratt = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jctratt, "value", ctratt, flags);
+ nvme_json_add_flag_flags(jctratt, "hostid-128", ctratt, NVME_CTRL_CTRATT_128_ID, flags);
+ nvme_json_add_flag_flags(jctratt, "nopspm", ctratt, NVME_CTRL_CTRATT_NON_OP_PSP, flags);
+ nvme_json_add_flag_flags(jctratt, "nvmsets", ctratt, NVME_CTRL_CTRATT_NVM_SETS, flags);
+ nvme_json_add_flag_flags(jctratt, "rrl", ctratt, NVME_CTRL_CTRATT_READ_RECV_LVLS, flags);
+ nvme_json_add_flag_flags(jctratt, "eg", ctratt, NVME_CTRL_CTRATT_ENDURANCE_GROUPS, flags);
+ nvme_json_add_flag_flags(jctratt, "plm", ctratt, NVME_CTRL_CTRATT_PREDICTABLE_LAT, flags);
+ nvme_json_add_flag_flags(jctratt, "tbkas", ctratt, NVME_CTRL_CTRATT_TBKAS, flags);
+ nvme_json_add_flag_flags(jctratt, "ng", ctratt, NVME_CTRL_CTRATT_NAMESPACE_GRANULARITY, flags);
+ nvme_json_add_flag_flags(jctratt, "sqa", ctratt, NVME_CTRL_CTRATT_SQ_ASSOCIATIONS, flags);
+ nvme_json_add_flag_flags(jctratt, "uuid", ctratt, NVME_CTRL_CTRATT_UUID_LIST, flags);
+
+ json_object_object_add(j, n, jctratt);
+}
+
+static void nvme_json_add_id_ctrl_cntrltype(struct json_object *j, const char *n,
+ __u8 cntrltype, unsigned long flags)
+{
+ const char *type;
+
+ if (!(flags & NVME_JSON_HUMAN)) {
+ nvme_json_add_int(j, n, cntrltype);
+ return;
+ }
+
+ type = nvme_id_ctrl_cntrltype_str(cntrltype);
+ nvme_json_add_str(j, n, type, flags);
+}
+static void nvme_json_add_id_ctrl_oacs(struct json_object *j, const char *n,
+ __u16 oacs, unsigned long flags)
+{
+ struct json_object *joacs;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, oacs, flags);
+ return;
+ }
+
+ joacs = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(joacs, "value", oacs, flags);
+ nvme_json_add_flag_flags(joacs, "security", oacs, NVME_CTRL_OACS_SECURITY, flags);
+ nvme_json_add_flag_flags(joacs, "format-nvm", oacs, NVME_CTRL_OACS_FORMAT, flags);
+ nvme_json_add_flag_flags(joacs, "firmware", oacs, NVME_CTRL_OACS_FW, flags);
+ nvme_json_add_flag_flags(joacs, "ns-mgmt", oacs, NVME_CTRL_OACS_NS_MGMT, flags);
+ nvme_json_add_flag_flags(joacs, "dev-self-test", oacs, NVME_CTRL_OACS_SELF_TEST, flags);
+ nvme_json_add_flag_flags(joacs, "directives", oacs, NVME_CTRL_OACS_DIRECTIVES, flags);
+ nvme_json_add_flag_flags(joacs, "nvme-mi", oacs, NVME_CTRL_OACS_NVME_MI, flags);
+ nvme_json_add_flag_flags(joacs, "virt-mgmt", oacs, NVME_CTRL_OACS_VIRT_MGMT, flags);
+ nvme_json_add_flag_flags(joacs, "doorbell-buf-cfg", oacs, NVME_CTRL_OACS_DBBUF_CFG, flags);
+ nvme_json_add_flag_flags(joacs, "get-lba-status", oacs, NVME_CTRL_OACS_LBA_STATUS, flags);
+
+ json_object_object_add(j, n, joacs);
+}
+
+static void nvme_json_add_id_ctrl_frmw(struct json_object *j, const char *n,
+ __u8 frmw, unsigned long flags)
+{
+ struct json_object *jfrmw;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, frmw, flags);
+ return;
+ }
+
+ jfrmw = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jfrmw, "value", frmw, flags);
+ nvme_json_add_flag_flags(jfrmw, "first-slot-ro", frmw, NVME_CTRL_FRMW_1ST_RO, flags);
+ nvme_json_add_int(jfrmw, "num-slots", (frmw & NVME_CTRL_FRMW_NR_SLOTS) >> 1);
+ nvme_json_add_flag_flags(jfrmw, "activate-no-reset", frmw, NVME_CTRL_FRMW_FW_ACT_NO_RESET, flags);
+
+ json_object_object_add(j, n, jfrmw);
+}
+
+static void nvme_json_add_id_ctrl_lpa(struct json_object *j, const char *n,
+ __u8 lpa, unsigned long flags)
+{
+ struct json_object *jlpa;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, lpa, flags);
+ return;
+ }
+
+ jlpa = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jlpa, "value", lpa, flags);
+ nvme_json_add_flag_flags(jlpa, "smart-per-namespace", lpa, NVME_CTRL_LPA_SMART_PER_NS, flags);
+ nvme_json_add_flag_flags(jlpa, "command-effects", lpa, NVME_CTRL_LPA_CMD_EFFECTS, flags);
+ nvme_json_add_flag_flags(jlpa, "extended", lpa, NVME_CTRL_LPA_EXTENDED, flags);
+ nvme_json_add_flag_flags(jlpa, "telemetry", lpa, NVME_CTRL_LPA_TELEMETRY, flags);
+ nvme_json_add_flag_flags(jlpa, "persistent-event", lpa, NVME_CTRL_LPA_PERSETENT_EVENT, flags);
+
+ json_object_object_add(j, n, jlpa);
+}
+
+static void nvme_json_add_id_ctrl_avscc(struct json_object *j, const char *n,
+ __u8 avscc, unsigned long flags)
+{
+ struct json_object *javscc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, avscc, flags);
+ return;
+ }
+
+ javscc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(javscc, "value", avscc, flags);
+ nvme_json_add_flag_flags(javscc, "avs-format", avscc, NVME_CTRL_AVSCC_AVS, flags);
+
+ json_object_object_add(j, n, javscc);
+}
+
+static void nvme_json_add_id_ctrl_apsta(struct json_object *j, const char *n,
+ __u8 apsta, unsigned long flags)
+{
+ struct json_object *japsta;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, apsta, flags);
+ return;
+ }
+
+ japsta = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(japsta, "value", apsta, flags);
+ nvme_json_add_flag_flags(japsta, "apst", apsta, NVME_CTRL_APSTA_APST, flags);
+
+ json_object_object_add(j, n, japsta);
+}
+
+static void nvme_json_add_id_ctrl_4k_mem(struct json_object *j, const char *n,
+ __u32 size, unsigned long flags)
+{
+ uint64_t m = size * 4096;
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_memory(j, n, m, flags);
+ else
+ nvme_json_add_int(j, n, size);
+}
+
+static void nvme_json_add_id_ctrl_64k_mem(struct json_object *j, const char *n,
+ __u32 size, unsigned long flags)
+{
+ uint64_t m = size * 64 * 1024ULL;
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_memory(j, n, m, flags);
+ else
+ nvme_json_add_int(j, n, size);
+}
+
+static void nvme_json_add_id_ctrl_16_mem(struct json_object *j, const char *n,
+ __u32 size, unsigned long flags)
+{
+ uint64_t m = size * 16;
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_memory(j, n, m, flags);
+ else
+ nvme_json_add_int(j, n, size);
+}
+
+static void nvme_json_add_id_ctrl_rpmbs(struct json_object *j, const char *n,
+ __u32 rpmbs, unsigned long flags)
+{
+ struct json_object *jrpmbs;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, rpmbs, flags);
+ return;
+ }
+
+ jrpmbs = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jrpmbs, "value", rpmbs, flags);
+ nvme_json_add_int(jrpmbs, "number-of-units", rpmbs & NVME_CTRL_RPMBS_NR_UNITS);
+ nvme_json_add_int(jrpmbs, "authentication-method", (rpmbs & NVME_CTRL_RPMBS_AUTH_METHOD) >> 3);
+ nvme_json_add_int(jrpmbs, "total-size", (rpmbs & NVME_CTRL_RPMBS_TOTAL_SIZE) >> 16);
+ nvme_json_add_int(jrpmbs, "access-size", (rpmbs & NVME_CTRL_RPMBS_ACCESS_SIZE) >> 24);
+
+ json_object_object_add(j, n, jrpmbs);
+}
+
+static void nvme_json_add_id_ctrl_dsto(struct json_object *j, const char *n,
+ __u8 dsto, unsigned long flags)
+{
+ struct json_object *jdsto;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, dsto, flags);
+ return;
+ }
+
+ jdsto = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jdsto, "value", dsto, flags);
+ nvme_json_add_flag_flags(jdsto, "one-dst", dsto, NVME_CTRL_DSTO_ONE_DST, flags);
+
+ json_object_object_add(j, n, jdsto);
+}
+
+static void nvme_json_add_id_ctrl_hctma(struct json_object *j, const char *n,
+ __u32 hctma, unsigned long flags)
+{
+ struct json_object *jhctma;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, hctma, flags);
+ return;
+ }
+
+ jhctma = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jhctma, "value", hctma, flags);
+ nvme_json_add_flag_flags(jhctma, "hctm", hctma, NVME_CTRL_HCTMA_HCTM, flags);
+
+ json_object_object_add(j, n, jhctma);
+}
+
+static void nvme_json_add_id_ctrl_sanicap(struct json_object *j, const char *n,
+ __u32 sanicap, unsigned long flags)
+{
+ struct json_object *jsanicap;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, sanicap, flags);
+ return;
+ }
+
+ jsanicap = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jsanicap, "value", sanicap, flags);
+ nvme_json_add_flag_flags(jsanicap, "ces", sanicap, NVME_CTRL_SANICAP_CES, flags);
+ nvme_json_add_flag_flags(jsanicap, "bes", sanicap, NVME_CTRL_SANICAP_BES, flags);
+ nvme_json_add_flag_flags(jsanicap, "ows", sanicap, NVME_CTRL_SANICAP_OWS, flags);
+ nvme_json_add_flag_flags(jsanicap, "ndi", sanicap, NVME_CTRL_SANICAP_NDI, flags);
+ nvme_json_add_int(jsanicap, "nodmmas", (sanicap & NVME_CTRL_SANICAP_NODMMAS) >> 30);
+
+ json_object_object_add(j, n, jsanicap);
+}
+
+static void nvme_json_add_id_ctrl_anacap(struct json_object *j, const char *n,
+ __u8 anacap, unsigned long flags)
+{
+ struct json_object *janacap;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, anacap, flags);
+ return;
+ }
+
+ janacap = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(janacap, "value", anacap, flags);
+ nvme_json_add_flag_flags(janacap, "optimal", anacap, NVME_CTRL_ANACAP_OPT, flags);
+ nvme_json_add_flag_flags(janacap, "non-optimal", anacap, NVME_CTRL_ANACAP_NON_OPT, flags);
+ nvme_json_add_flag_flags(janacap, "inaccessible", anacap, NVME_CTRL_ANACAP_INACCESSIBLE, flags);
+ nvme_json_add_flag_flags(janacap, "persistent-loss", anacap, NVME_CTRL_ANACAP_PERSISTENT_LOSS, flags);
+ nvme_json_add_flag_flags(janacap, "change", anacap, NVME_CTRL_ANACAP_CHANGE, flags);
+ nvme_json_add_flag_flags(janacap, "grpid-not-changable", anacap, NVME_CTRL_ANACAP_GRPID_NO_CHG, flags);
+ nvme_json_add_flag_flags(janacap, "grpid-ns-mgmt", anacap, NVME_CTRL_ANACAP_GRPID_MGMT, flags);
+
+ json_object_object_add(j, n, janacap);
+}
+
+static void nvme_json_add_id_ctrl_sqes(struct json_object *j, const char *n,
+ __u8 sqes, unsigned long flags)
+{
+ struct json_object *jsqes;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, sqes, flags);
+ return;
+ }
+
+ jsqes = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jsqes, "value", sqes, flags);
+ nvme_json_add_int(jsqes, "sqes-min", sqes & NVME_CTRL_SQES_MIN);
+ nvme_json_add_int(jsqes, "sqes-max", (sqes & NVME_CTRL_SQES_MAX) >> 4);
+
+ json_object_object_add(j, n, jsqes);
+}
+
+static void nvme_json_add_id_ctrl_cqes(struct json_object *j, const char *n,
+ __u8 cqes, unsigned long flags)
+{
+ struct json_object *jcqes;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, cqes, flags);
+ return;
+ }
+
+ jcqes = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jcqes, "value", cqes, flags);
+ nvme_json_add_int(jcqes, "cqes-min", cqes & NVME_CTRL_CQES_MIN);
+ nvme_json_add_int(jcqes, "cqes-max", (cqes & NVME_CTRL_CQES_MAX) >> 4);
+
+ json_object_object_add(j, n, jcqes);
+}
+
+static void nvme_json_add_id_ctrl_oncs(struct json_object *j, const char *n,
+ __u16 oncs, unsigned long flags)
+{
+ struct json_object *joncs;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, oncs, flags);
+ return;
+ }
+
+ joncs = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(joncs, "value", oncs, flags);
+ nvme_json_add_flag_flags(joncs, "compare", oncs, NVME_CTRL_ONCS_COMPARE, flags);
+ nvme_json_add_flag_flags(joncs, "write-uncorrectable", oncs, NVME_CTRL_ONCS_WRITE_UNCORRECTABLE, flags);
+ nvme_json_add_flag_flags(joncs, "data-set-mgmt", oncs, NVME_CTRL_ONCS_DSM, flags);
+ nvme_json_add_flag_flags(joncs, "write-zeroes", oncs, NVME_CTRL_ONCS_WRITE_ZEROES, flags);
+ nvme_json_add_flag_flags(joncs, "save-features", oncs, NVME_CTRL_ONCS_SAVE_FEATURES, flags);
+ nvme_json_add_flag_flags(joncs, "reservations", oncs, NVME_CTRL_ONCS_RESERVATIONS, flags);
+ nvme_json_add_flag_flags(joncs, "timestamp", oncs, NVME_CTRL_ONCS_TIMESTAMP, flags);
+ nvme_json_add_flag_flags(joncs, "verify", oncs, NVME_CTRL_ONCS_VERIFY, flags);
+
+ json_object_object_add(j, n, joncs);
+}
+
+static void nvme_json_add_id_ctrl_fuses(struct json_object *j, const char *n,
+ __u16 fuses, unsigned long flags)
+{
+ struct json_object *jfuses;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, fuses, flags);
+ return;
+ }
+
+ jfuses = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jfuses, "value", fuses, flags);
+ nvme_json_add_flag_flags(jfuses, "compare", fuses, NVME_CTRL_FUSES_COMPARE_AND_WRITE, flags);
+
+ json_object_object_add(j, n, jfuses);
+}
+
+static void nvme_json_add_id_ctrl_fna(struct json_object *j, const char *n,
+ __u8 fna, unsigned long flags)
+{
+ struct json_object *jfna;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, fna, flags);
+ return;
+ }
+
+ jfna = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jfna, "value", fna, flags);
+ nvme_json_add_flag_flags(jfna, "format-all-ns", fna, NVME_CTRL_FNA_FMT_ALL_NAMESPACES, flags);
+ nvme_json_add_flag_flags(jfna, "secure-erase-all-ns", fna, NVME_CTRL_FNA_SEC_ALL_NAMESPACES, flags);
+ nvme_json_add_flag_flags(jfna, "crypto-erasens", fna, NVME_CTRL_FNA_CRYPTO_ERASE, flags);
+
+ json_object_object_add(j, n, jfna);
+}
+
+static void nvme_json_add_id_ctrl_vwc(struct json_object *j, const char *n,
+ __u8 vwc, unsigned long flags)
+{
+ struct json_object *jvwc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, vwc, flags);
+ return;
+ }
+
+ jvwc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jvwc, "value", vwc, flags);
+ nvme_json_add_flag_flags(jvwc, "present", vwc, NVME_CTRL_VWC_PRESENT, flags);
+ nvme_json_add_flag_flags(jvwc, "flush-behavior", vwc, NVME_CTRL_VWC_FLUSH, flags);
+
+ json_object_object_add(j, n, jvwc);
+}
+
+static void nvme_json_add_id_ctrl_nvscc(struct json_object *j, const char *n,
+ __u8 nvscc, unsigned long flags)
+{
+ struct json_object *jnvscc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, nvscc, flags);
+ return;
+ }
+
+ jnvscc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jnvscc, "value", nvscc, flags);
+ nvme_json_add_flag_flags(jnvscc, "nvmvs-format", nvscc, NVME_CTRL_NVSCC_FMT, flags);
+
+ json_object_object_add(j, n, jnvscc);
+}
+
+static void nvme_json_add_id_ctrl_nwpc(struct json_object *j, const char *n,
+ __u8 nwpc, unsigned long flags)
+{
+ struct json_object *jnwpc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, nwpc, flags);
+ return;
+ }
+
+ jnwpc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jnwpc, "value", nwpc, flags);
+ nvme_json_add_flag_flags(jnwpc, "wp", nwpc, NVME_CTRL_NWPC_WRITE_PROTECT, flags);
+ nvme_json_add_flag_flags(jnwpc, "wpupc", nwpc, NVME_CTRL_NWPC_WRITE_PROTECT_POWER_CYCLE, flags);
+ nvme_json_add_flag_flags(jnwpc, "pwp", nwpc, NVME_CTRL_NWPC_WRITE_PROTECT_PERMANENT, flags);
+
+ json_object_object_add(j, n, jnwpc);
+}
+
+static void nvme_json_add_id_ctrl_sgls(struct json_object *j, const char *n,
+ __u32 sgls, unsigned long flags)
+{
+ struct json_object *jsgls;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, sgls, flags);
+ return;
+ }
+
+ jsgls = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jsgls, "value", sgls, flags);
+ nvme_json_add_int(jsgls, "supports", sgls & NVME_CTRL_SGLS_SUPPORTED);
+ nvme_json_add_flag_flags(jsgls, "keyed", sgls, NVME_CTRL_SGLS_KEYED, flags);
+ nvme_json_add_flag_flags(jsgls, "bitbucket", sgls, NVME_CTRL_SGLS_BIT_BUCKET, flags);
+ nvme_json_add_flag_flags(jsgls, "aligned", sgls, NVME_CTRL_SGLS_MPTR_BYTE_ALIGNED, flags);
+ nvme_json_add_flag_flags(jsgls, "oversize", sgls, NVME_CTRL_SGLS_OVERSIZE, flags);
+ nvme_json_add_flag_flags(jsgls, "mptrsgl", sgls, NVME_CTRL_SGLS_MPTR_SGL, flags);
+ nvme_json_add_flag_flags(jsgls, "offets", sgls, NVME_CTRL_SGLS_OFFSET, flags);
+ nvme_json_add_flag_flags(jsgls, "tportdesc", sgls, NVME_CTRL_SGLS_TPORT, flags);
+
+ json_object_object_add(j, n, jsgls);
+}
+
+static void nvme_json_add_id_ctrl_fcatt(struct json_object *j, const char *n,
+ __u8 fcatt, unsigned long flags)
+{
+ struct json_object *jfcatt;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, fcatt, flags);
+ return;
+ }
+
+ jfcatt = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jfcatt, "value", fcatt, flags);
+ nvme_json_add_flag_flags(jfcatt, "dynamic-subsystem", fcatt, NVME_CTRL_FCATT_DYNAMIC, flags);
+
+ json_object_object_add(j, n, jfcatt);
+}
+
+static void nvme_json_add_id_ctrl_ofcs(struct json_object *j, const char *n,
+ __u8 ofcs, unsigned long flags)
+{
+ struct json_object *jofcs;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, ofcs, flags);
+ return;
+ }
+
+ jofcs = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jofcs, "value", ofcs, flags);
+ nvme_json_add_flag_flags(jofcs, "disconnect-support", ofcs, NVME_CTRL_OFCS_DISCONNECT, flags);
+
+ json_object_object_add(j, n, jofcs);
+}
+
+struct json_object *nvme_id_ctrl_to_json(
+ struct nvme_id_ctrl *id, unsigned long flags)
+{
+ struct json_object *jctrl, *jpsds;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(id, sizeof(*id), flags);
+
+ jctrl = nvme_json_new_object(flags);
+
+ nvme_json_add_hex_le16_flags(jctrl, "vid", id->vid, flags);
+ nvme_json_add_hex_le16_flags(jctrl, "ssvid", id->ssvid, flags);
+ nvme_json_add_str_flags(jctrl, "sn", id->sn, sizeof(id->sn), flags);
+ nvme_json_add_str_flags(jctrl, "mn", id->mn, sizeof(id->mn), flags);
+ nvme_json_add_str_flags(jctrl, "fr", id->fr, sizeof(id->fr), flags);
+ nvme_json_add_int(jctrl, "rab", id->rab);
+ nvme_json_add_id_ctrl_ieee(jctrl, "ieee", id->ieee, flags);
+ nvme_json_add_id_ctrl_cmic(jctrl, "cmic", id->cmic, flags);
+ nvme_json_add_id_ctrl_mdts(jctrl, "mdts", id->mdts, flags);
+ nvme_json_add_le16(jctrl, "cntlid", id->cntlid);
+ nvme_json_add_id_ctrl_ver(jctrl, "ver", le32_to_cpu(id->ver), flags);
+ nvme_json_add_time_us_flags(jctrl, "rtd3r", le32_to_cpu(id->rtd3r), flags);
+ nvme_json_add_time_us_flags(jctrl, "rtd3e", le32_to_cpu(id->rtd3e), flags);
+ nvme_json_add_id_ctrl_oaes(jctrl, "oaes", le32_to_cpu(id->oaes), flags);
+ nvme_json_add_id_ctrl_ctratt(jctrl, "ctratt", le32_to_cpu(id->ctratt), flags);
+ nvme_json_add_0x(jctrl, "rrls", le16_to_cpu(id->rrls), flags);
+ nvme_json_add_id_ctrl_cntrltype(jctrl, "cntrltype", id->cntrltype, flags);
+ nvme_json_add_uuid(jctrl, "fguid", id->fguid, flags);
+ nvme_json_add_time_100us_flags(jctrl, "crdt1", le16_to_cpu(id->crdt1), flags);
+ nvme_json_add_time_100us_flags(jctrl, "crdt2", le16_to_cpu(id->crdt2), flags);
+ nvme_json_add_time_100us_flags(jctrl, "crdt3", le16_to_cpu(id->crdt3), flags);
+ nvme_json_add_0x(jctrl, "nvmsr", id->nvmsr, flags);
+ nvme_json_add_0x(jctrl, "vwci", id->vwci, flags);
+ nvme_json_add_0x(jctrl, "mec", id->mec, flags);
+ nvme_json_add_id_ctrl_oacs(jctrl, "oacs", le16_to_cpu(id->oacs), flags);
+ nvme_json_add_int(jctrl, "acl", id->acl);
+ nvme_json_add_int(jctrl, "aerl", id->aerl);
+ nvme_json_add_id_ctrl_frmw(jctrl, "frmw", id->frmw, flags);
+ nvme_json_add_id_ctrl_lpa(jctrl, "lpa", id->lpa, flags);
+ nvme_json_add_int(jctrl, "elpe", id->elpe);
+ nvme_json_add_int(jctrl, "npss", id->npss);
+ nvme_json_add_id_ctrl_avscc(jctrl, "avscc", id->avscc, flags);
+ nvme_json_add_id_ctrl_apsta(jctrl, "apsta", id->apsta, flags);
+ nvme_json_add_temp(jctrl, "wctemp", id->wctemp, flags);
+ nvme_json_add_temp(jctrl, "cctemp", id->cctemp, flags);
+ nvme_json_add_time_100us_flags(jctrl, "mtfa", le32_to_cpu(id->mtfa), flags);
+ nvme_json_add_id_ctrl_4k_mem(jctrl, "hmpre", le32_to_cpu(id->hmpre), flags);
+ nvme_json_add_id_ctrl_4k_mem(jctrl, "hmmin", le32_to_cpu(id->hmmin), flags);
+ nvme_json_add_storage_128_flags(jctrl, "tnvmcap", id->tnvmcap, flags);
+ nvme_json_add_storage_128_flags(jctrl, "unvmcap", id->unvmcap, flags);
+ nvme_json_add_id_ctrl_rpmbs(jctrl, "rpmbs", le32_to_cpu(id->rpmbs), flags);
+ nvme_json_add_time_m_flags(jctrl, "edstt", le16_to_cpu(id->edstt), flags);
+ nvme_json_add_id_ctrl_dsto(jctrl, "dsto", id->dsto, flags);
+ nvme_json_add_id_ctrl_4k_mem(jctrl, "fwug", id->fwug, flags);
+ nvme_json_add_time_100us_flags(jctrl, "kas", le16_to_cpu(id->kas), flags);
+ nvme_json_add_id_ctrl_hctma(jctrl, "hctma", le16_to_cpu(id->hctma), flags);
+ nvme_json_add_temp(jctrl, "mntmt", le16_to_cpu(id->mntmt), flags);
+ nvme_json_add_temp(jctrl, "mxtmt", le16_to_cpu(id->mxtmt), flags);
+ nvme_json_add_id_ctrl_sanicap(jctrl, "sanicap", le32_to_cpu(id->sanicap), flags);
+ nvme_json_add_id_ctrl_4k_mem(jctrl, "hmminds", le32_to_cpu(id->hmminds), flags);
+ nvme_json_add_le16(jctrl, "hmmaxd", id->hmmaxd);
+ nvme_json_add_le16(jctrl, "nsetidmax", id->nsetidmax);
+ nvme_json_add_le16(jctrl, "endgidmax", id->endgidmax);
+ nvme_json_add_time_s_flags(jctrl, "anatt", id->anatt, flags);
+ nvme_json_add_id_ctrl_anacap(jctrl, "anacap", id->anacap, flags);
+ nvme_json_add_le32(jctrl, "anagrpmax", id->anagrpmax);
+ nvme_json_add_le32(jctrl, "nanagrpid", id->nanagrpid);
+ nvme_json_add_id_ctrl_64k_mem(jctrl, "pels", le32_to_cpu(id->pels), flags);
+ nvme_json_add_id_ctrl_sqes(jctrl, "sqes", id->sqes, flags);
+ nvme_json_add_id_ctrl_cqes(jctrl, "cqes", id->cqes, flags);
+ nvme_json_add_le16(jctrl, "maxcmd", id->maxcmd);
+ nvme_json_add_le32(jctrl, "nn", id->nn);
+ nvme_json_add_id_ctrl_oncs(jctrl, "oncs", le16_to_cpu(id->oncs), flags);
+ nvme_json_add_id_ctrl_fuses(jctrl, "fuses", le16_to_cpu(id->fuses), flags);
+ nvme_json_add_id_ctrl_fna(jctrl, "fna", id->fna, flags);
+ nvme_json_add_id_ctrl_vwc(jctrl, "vwc", id->vwc, flags);
+ nvme_json_add_le16(jctrl, "awun", id->awun);
+ nvme_json_add_le16(jctrl, "awupf", id->awupf);
+ nvme_json_add_id_ctrl_nvscc(jctrl, "nvscc", id->nvscc, flags);
+ nvme_json_add_id_ctrl_nwpc(jctrl, "nwpc", id->nwpc, flags);
+ nvme_json_add_le16(jctrl, "acwu", id->acwu);
+ nvme_json_add_id_ctrl_sgls(jctrl, "sgls", le32_to_cpu(id->sgls), flags);
+ nvme_json_add_le32(jctrl, "mnan", id->mnan);
+ nvme_json_add_str_flags(jctrl, "subnqn", id->subnqn, strnlen(id->subnqn, sizeof(id->subnqn)), flags);
+ nvme_json_add_id_ctrl_16_mem(jctrl, "ioccsz", le32_to_cpu(id->ioccsz), flags);
+ nvme_json_add_id_ctrl_16_mem(jctrl, "iorcsz", le32_to_cpu(id->iorcsz), flags);
+ nvme_json_add_id_ctrl_16_mem(jctrl, "icdoff", le16_to_cpu(id->icdoff), flags);
+ nvme_json_add_id_ctrl_fcatt(jctrl, "fcatt", id->fcatt, flags);
+ nvme_json_add_int(jctrl, "msdbd", id->msdbd);
+ nvme_json_add_id_ctrl_ofcs(jctrl, "ofcs", le16_to_cpu(id->ofcs), flags);
+
+ jpsds = nvme_json_new_array();
+ for (i = 0; i <= id->npss; i++)
+ nvme_json_add_id_ctrl_psd(jpsds, &id->psd[i], flags | NVME_JSON_COMPACT);
+ json_object_object_add(jctrl, "psd", jpsds);
+
+ return jctrl;
+}
+
+static void nvme_json_add_id_ns_lbaf(struct json_object *j,
+ struct nvme_lbaf *lbaf, bool in_use, unsigned long flags)
+{
+ struct json_object *jlbaf = nvme_json_new_object(flags);
+
+ nvme_json_add_size_flags(jlbaf, "ms", le16_to_cpu(lbaf->ms), flags);
+
+ if (flags & NVME_JSON_HUMAN) {
+ nvme_json_add_size_flags(jlbaf, "lbads", 1 << lbaf->ds, flags);
+ nvme_json_add_str(jlbaf, "rp",
+ nvme_id_ns_lbaf_rp_str(lbaf->rp & NVME_LBAF_RP_MASK),
+ flags);
+ } else {
+ nvme_json_add_int(jlbaf, "lbads", lbaf->ds);
+ nvme_json_add_int(jlbaf, "rp", lbaf->rp);
+ }
+
+ nvme_json_add_bool(jlbaf, "in-use", in_use);
+
+ json_object_array_add(j, jlbaf);
+}
+
+static void nvme_json_add_id_ns_nsfeat(struct json_object *j, const char *n, __u8 nsfeat, unsigned long flags)
+{
+ struct json_object *jnsfeat;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, nsfeat, flags);
+ return;
+ }
+
+ jnsfeat = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jnsfeat, "value", nsfeat, flags);
+ nvme_json_add_flag_flags(jnsfeat, "thin-provisioning", nsfeat, NVME_NS_FEAT_THIN, flags);
+ nvme_json_add_flag_flags(jnsfeat, "ns-atomics", nsfeat, NVME_NS_FEAT_NATOMIC, flags);
+ nvme_json_add_flag_flags(jnsfeat, "unwritten-blk-err", nsfeat, NVME_NS_FEAT_DULBE, flags);
+ nvme_json_add_flag_flags(jnsfeat, "id-reuse", nsfeat, NVME_NS_FEAT_ID_REUSE, flags);
+ nvme_json_add_flag_flags(jnsfeat, "preferred-access", nsfeat, NVME_NS_FEAT_IO_OPT, flags);
+
+ json_object_object_add(j, n, jnsfeat);
+}
+
+static void nvme_json_add_id_ns_flbas(struct json_object *j, const char *n, __u8 flbas, unsigned long flags)
+{
+ struct json_object *jflbas;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, flbas, flags);
+ return;
+ }
+
+ jflbas = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jflbas, "value", flbas, flags);
+ nvme_json_add_int(jflbas, "lba-index", flbas & NVME_NS_FLBAS_LBA_MASK);
+ nvme_json_add_flag_flags(jflbas, "extended-metadata", flbas, NVME_NS_FLBAS_META_EXT, flags);
+
+ json_object_object_add(j, n, jflbas);
+}
+
+static void nvme_json_add_id_ns_mc(struct json_object *j, const char *n, __u8 mc, unsigned long flags)
+{
+ struct json_object *jmc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, mc, flags);
+ return;
+ }
+
+ jmc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jmc, "value", mc, flags);
+ nvme_json_add_flag_flags(jmc, "extended", mc, NVME_NS_MC_EXTENDED, flags);
+ nvme_json_add_flag_flags(jmc, "separate", mc, NVME_NS_MC_SEPARATE, flags);
+
+ json_object_object_add(j, n, jmc);
+}
+
+static void nvme_json_add_id_ns_dpc(struct json_object *j, const char *n, __u8 dpc, unsigned long flags)
+{
+ struct json_object *jdpc;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, dpc, flags);
+ return;
+ }
+
+ jdpc = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jdpc, "value", dpc, flags);
+ nvme_json_add_flag_flags(jdpc, "type1", dpc, NVME_NS_DPC_PI_TYPE1, flags);
+ nvme_json_add_flag_flags(jdpc, "type2", dpc, NVME_NS_DPC_PI_TYPE2, flags);
+ nvme_json_add_flag_flags(jdpc, "type3", dpc, NVME_NS_DPC_PI_TYPE3, flags);
+ nvme_json_add_flag_flags(jdpc, "first", dpc, NVME_NS_DPC_PI_FIRST, flags);
+ nvme_json_add_flag_flags(jdpc, "last", dpc, NVME_NS_DPC_PI_LAST, flags);
+
+ json_object_object_add(j, n, jdpc);
+}
+
+static void nvme_json_add_id_ns_dps(struct json_object *j, const char *n, __u8 dps, unsigned long flags)
+{
+ struct json_object *jdps;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, dps, flags);
+ return;
+ }
+
+ jdps = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jdps, "value", dps, flags);
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(jdps, "pi",
+ nvme_id_ns_dps_str(dps & NVME_NS_DPS_PI_MASK),
+ flags);
+ else
+ nvme_json_add_int(jdps, "pi", dps & NVME_NS_DPS_PI_MASK);
+
+ nvme_json_add_flag_flags(jdps, "first", dps, NVME_NS_DPS_PI_FIRST, flags);
+
+ json_object_object_add(j, n, jdps);
+}
+
+static void nvme_json_add_id_ns_nmic(struct json_object *j, const char *n, __u8 nmic, unsigned long flags)
+{
+ struct json_object *jnmic;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, nmic, flags);
+ return;
+ }
+
+ jnmic = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jnmic, "value", nmic, flags);
+ nvme_json_add_flag_flags(jnmic, "shared", nmic, NVME_NS_NMIC_SHARED, flags);
+
+ json_object_object_add(j, n, jnmic);
+}
+
+static void nvme_json_add_id_ns_rescap(struct json_object *j, const char *n, __u8 rescap, unsigned long flags)
+{
+ struct json_object *jrescap;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, rescap, flags);
+ return;
+ }
+
+ jrescap = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jrescap, "value", rescap, flags);
+ nvme_json_add_flag_flags(jrescap, "ptpl", rescap, NVME_NS_RESCAP_PTPL, flags);
+ nvme_json_add_flag_flags(jrescap, "we", rescap, NVME_NS_RESCAP_WE, flags);
+ nvme_json_add_flag_flags(jrescap, "ea", rescap, NVME_NS_RESCAP_EA, flags);
+ nvme_json_add_flag_flags(jrescap, "wero", rescap, NVME_NS_RESCAP_WERO, flags);
+ nvme_json_add_flag_flags(jrescap, "earo", rescap, NVME_NS_RESCAP_EARO, flags);
+ nvme_json_add_flag_flags(jrescap, "wear", rescap, NVME_NS_RESCAP_WEAR, flags);
+ nvme_json_add_flag_flags(jrescap, "eaar", rescap, NVME_NS_RESCAP_EAAR, flags);
+ nvme_json_add_flag_flags(jrescap, "iek13", rescap, NVME_NS_RESCAP_IEK_13, flags);
+
+ json_object_object_add(j, n, jrescap);
+}
+
+static void nvme_json_add_id_ns_fpi(struct json_object *j, const char *n, __u8 fpi, unsigned long flags)
+{
+ struct json_object *jfpi;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, fpi, flags);
+ return;
+ }
+
+ jfpi = nvme_json_new_object(flags);
+ nvme_json_add_0x(jfpi, "value", fpi, flags);
+ nvme_json_add_percent(jfpi, "remaining", fpi & NVME_NS_FPI_REMAINING, flags);
+ nvme_json_add_flag_flags(jfpi, "supported", fpi, NVME_NS_FPI_SUPPORTED, flags);
+
+ json_object_object_add(j, n, jfpi);
+}
+
+static void nvme_json_add_id_ns_dlfeat(struct json_object *j, const char *n, __u8 dlfeat, unsigned long flags)
+{
+ struct json_object *jdlfeat;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, dlfeat, flags);
+ return;
+ }
+
+ jdlfeat = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jdlfeat, "value", dlfeat, flags);
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(jdlfeat, "read-behavior",
+ id_ns_dlfeat_rb_str(dlfeat & NVME_NS_DLFEAT_RB), flags);
+ else
+ nvme_json_add_int(jdlfeat, "read-behavior", dlfeat & NVME_NS_DLFEAT_RB);
+
+ nvme_json_add_flag_flags(jdlfeat, "write-zeroes", dlfeat, NVME_NS_DLFEAT_WRITE_ZEROES, flags);
+ nvme_json_add_flag_flags(jdlfeat, "crc-guard", dlfeat, NVME_NS_DLFEAT_CRC_GUARD, flags);
+
+ json_object_object_add(j, n, jdlfeat);
+}
+
+static void nvme_json_add_id_ns_nsattr(struct json_object *j, const char *n, __u8 nsattr, unsigned long flags)
+{
+ struct json_object *jnsattr;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, n, nsattr, flags);
+ return;
+ }
+
+ jnsattr = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jnsattr, "value", nsattr, flags);
+ nvme_json_add_flag_flags(jnsattr, "write-protected", nsattr, NVME_NS_NSATTR_WRITE_PROTECTED, flags);
+
+ json_object_object_add(j, n, jnsattr);
+}
+
+static void nvme_json_add_blocks(struct json_object *j, const char *n, __u64 blocks,
+ uint32_t bs, unsigned long flags)
+{
+ uint64_t size = blocks * bs;
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_size(j, n, size, flags);
+ else
+ nvme_json_add_int(j, n, blocks);
+}
+
+struct json_object *nvme_id_ns_to_json(struct nvme_id_ns *ns,
+ unsigned long flags)
+{
+ uint8_t lbaf = ns->flbas & NVME_NS_FLBAS_LBA_MASK;
+ uint32_t bs = 1 << ns->lbaf[lbaf].ds;
+ struct json_object *jns, *jlbafs;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(ns, sizeof(*ns), flags);
+
+ jns = nvme_json_new_object(flags);
+
+ if (flags & NVME_JSON_HUMAN) {
+ nvme_json_add_storage_flags(jns, "nsze", bs * le64_to_cpu(ns->nsze), flags);
+ nvme_json_add_storage_flags(jns, "ncap", bs * le64_to_cpu(ns->ncap), flags);
+ nvme_json_add_storage_flags(jns, "nuse", bs * le64_to_cpu(ns->nuse), flags);
+ } else {
+ nvme_json_add_le64(jns, "nsze", ns->nsze);
+ nvme_json_add_le64(jns, "ncap", ns->ncap);
+ nvme_json_add_le64(jns, "nuse", ns->nuse);
+ }
+
+ nvme_json_add_id_ns_nsfeat(jns, "nsfeat", ns->nsfeat, flags);
+ nvme_json_add_int(jns, "nlbaf", ns->nlbaf);
+ nvme_json_add_id_ns_flbas(jns, "flbas", ns->flbas, flags);
+ nvme_json_add_id_ns_mc(jns, "mc", ns->mc, flags);
+ nvme_json_add_id_ns_dpc(jns, "dpc", ns->dpc, flags);
+ nvme_json_add_id_ns_dps(jns, "dps", ns->dps, flags);
+ nvme_json_add_id_ns_nmic(jns, "nmic", ns->nmic, flags);
+ nvme_json_add_id_ns_rescap(jns, "rescap", ns->rescap, flags);
+ nvme_json_add_id_ns_fpi(jns, "fpi", ns->fpi, flags);
+ nvme_json_add_id_ns_dlfeat(jns, "dlfeat", ns->dlfeat, flags);
+ nvme_json_add_blocks(jns, "nawun", le16_to_cpu(ns->nawun), bs, flags);
+ nvme_json_add_blocks(jns, "nawupf", le16_to_cpu(ns->nawupf), bs, flags);
+ nvme_json_add_blocks(jns, "nacwu", le16_to_cpu(ns->nacwu), bs, flags);
+ nvme_json_add_blocks(jns, "nabsn", le16_to_cpu(ns->nabsn), bs, flags);
+ nvme_json_add_blocks(jns, "nabo", le16_to_cpu(ns->nabo), bs, flags);
+ nvme_json_add_blocks(jns, "nabspf", le16_to_cpu(ns->nabspf), bs, flags);
+ nvme_json_add_blocks(jns, "noiob", le16_to_cpu(ns->noiob), bs, flags);
+ nvme_json_add_storage_128_flags(jns, "nvmcap", ns->nvmcap, flags);
+ nvme_json_add_blocks(jns, "npwg", le16_to_cpu(ns->npwg), bs, flags);
+ nvme_json_add_blocks(jns, "npwa", le16_to_cpu(ns->npwa), bs, flags);
+ nvme_json_add_blocks(jns, "npdg", le16_to_cpu(ns->npdg), bs, flags);
+ nvme_json_add_blocks(jns, "npda", le16_to_cpu(ns->npda), bs, flags);
+ nvme_json_add_blocks(jns, "nows", le16_to_cpu(ns->nows), bs, flags);
+ nvme_json_add_le32(jns, "anagrpid", ns->anagrpid);
+ nvme_json_add_id_ns_nsattr(jns, "nsattr", ns->nsattr, flags);
+ nvme_json_add_le16(jns, "nvmsetid", ns->nvmsetid);
+ nvme_json_add_le16(jns, "endgid", ns->endgid);
+ nvme_json_add_uuid(jns, "nguid", ns->nguid, flags);
+ nvme_json_add_oui(jns, "eui64", ns->eui64, 8, flags);
+
+ jlbafs = nvme_json_new_array();
+ for (i = 0; i <= ns->nlbaf; i++)
+ nvme_json_add_id_ns_lbaf(jlbafs, &ns->lbaf[i], i == lbaf, flags);
+ json_object_object_add(jns, "lbaf", jlbafs);
+
+ return jns;
+}
+
+static struct json_object *nvme_id_ns_desc_to_json(
+ struct nvme_ns_id_desc *desc, unsigned long flags)
+{
+ struct json_object *jdesc = nvme_json_new_object(flags);
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(jdesc, "nidt", nvme_id_nsdesc_nidt_str(desc->nidt), flags);
+ else
+ nvme_json_add_int(jdesc, "nidt", desc->nidt);
+
+ nvme_json_add_int(jdesc, "nidl", desc->nidl);
+ nvme_json_add_hex_array(jdesc, "nid", desc->nid, desc->nidl);
+
+ return jdesc;
+}
+
+struct json_object *nvme_id_ns_desc_list_to_json(void *list,
+ unsigned long flags)
+{
+ struct json_object *jlist, *jdescs;
+ struct nvme_ns_id_desc *cur;
+ void *p = list;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(list, sizeof(*list), flags);
+
+ jlist = nvme_json_new_object(flags);
+ jdescs = nvme_json_new_array();
+
+ cur = p;
+ while (cur->nidl && p - list < 0x1000) {
+ json_object_array_add(jdescs,
+ nvme_id_ns_desc_to_json(cur, flags));
+ p += cur->nidl + sizeof(*cur);
+ cur = p;
+ }
+ json_object_object_add(jlist, "ns-id-descriptors", jdescs);
+
+ return jlist;
+}
+
+static struct json_object *nvme_id_ns_granularity_desc_to_json(
+ struct nvme_id_ns_granularity_desc *gran, unsigned long flags)
+{
+ struct json_object *jgran = nvme_json_new_object(flags);
+
+ nvme_json_add_le64(jgran, "nszeg", gran->namespace_size_granularity);
+ nvme_json_add_le64(jgran, "ncapg", gran->namespace_capacity_granularity);
+
+ return jgran;
+}
+
+struct json_object *nvme_id_ns_granularity_list_to_json(
+ struct nvme_id_ns_granularity_list *glist, unsigned long flags)
+{
+ struct json_object *jglist, *jgrans;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(glist, sizeof(*glist), flags);
+
+ jglist = nvme_json_new_object(flags);
+
+ nvme_json_add_le32(jglist, "attributes", glist->attributes);
+ nvme_json_add_int(jglist, "ndesc", glist->num_descriptors);
+
+ jgrans = nvme_json_new_array();
+ for (i = 0; i <= MIN(glist->num_descriptors, 15); i++)
+ json_object_array_add(jgrans,
+ nvme_id_ns_granularity_desc_to_json(&glist->entry[i],
+ flags));
+ json_object_object_add(jglist, "ng-descriptors", jgrans);
+
+ return jglist;
+}
+
+static json_object *nvme_nvmset_attr_to_json(
+ struct nvme_nvmset_attr *attr, unsigned long flags)
+{
+ struct json_object *jattr = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jattr, "nvmsetid", attr->id);
+ nvme_json_add_le16(jattr, "egi", attr->endurance_group_id);
+ nvme_json_add_le32(jattr, "r4krt", attr->random_4k_read_typical);
+ nvme_json_add_le32(jattr, "ows", attr->opt_write_size);
+ nvme_json_add_int128(jattr, "tnvmsetcap", attr->total_nvmset_cap);
+ nvme_json_add_int128(jattr, "unvmsetcap", attr->unalloc_nvmset_cap);
+
+ return jattr;
+}
+
+struct json_object *nvme_id_nvm_set_list_to_json(
+ struct nvme_id_nvmset_list *nvmset, unsigned long flags)
+{
+ struct json_object *jnvmset, *jattrs;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(nvmset, sizeof(*nvmset), flags);
+
+ jnvmset = nvme_json_new_object(flags);
+
+ nvme_json_add_int(jnvmset, "nids", nvmset->nid);
+
+ jattrs = nvme_json_new_array();
+ for (i = 0; i < nvmset->nid; i++)
+ json_object_array_add(jnvmset,
+ nvme_nvmset_attr_to_json(&nvmset->ent[i], flags));
+ json_object_object_add(jnvmset, "entry", jattrs);
+
+ return jnvmset;
+}
+
+struct json_object *nvme_id_primary_ctrl_cap_to_json(
+ struct nvme_primary_ctrl_cap *cap, unsigned long flags)
+{
+ struct json_object *jpri;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(cap, sizeof(*cap), flags);
+
+ jpri = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jpri, "cntlid", cap->cntlid);
+ nvme_json_add_le16(jpri, "portid", cap->portid);
+ nvme_json_add_int(jpri, "crt", cap->crt);
+ nvme_json_add_le32(jpri, "vqfrt", cap->vqfrt);
+ nvme_json_add_le32(jpri, "vqrfa", cap->vqrfa);
+ nvme_json_add_le16(jpri, "vqrfap", cap->vqrfap);
+ nvme_json_add_le16(jpri, "vqprt", cap->vqprt);
+ nvme_json_add_le16(jpri, "vqfrsm", cap->vqfrsm);
+ nvme_json_add_le16(jpri, "vqgran", cap->vqgran);
+ nvme_json_add_le32(jpri, "vifrt", cap->vifrt);
+ nvme_json_add_le32(jpri, "virfa", cap->virfa);
+ nvme_json_add_le16(jpri, "virfap", cap->virfap);
+ nvme_json_add_le16(jpri, "viprt", cap->viprt);
+ nvme_json_add_le16(jpri, "vifrsm", cap->vifrsm);
+ nvme_json_add_le16(jpri, "vigran", cap->vigran);
+
+ return jpri;
+}
+
+static struct json_object *nvme_secondary_ctrl_entry_to_json(
+ struct nvme_secondary_ctrl *ent, unsigned int flags)
+{
+ struct json_object *jsec = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jsec, "scid", ent->scid);
+ nvme_json_add_le16(jsec, "pcid", ent->pcid);
+ nvme_json_add_int(jsec, "scs", ent->scs);
+ nvme_json_add_le16(jsec, "vfn", ent->vfn);
+ nvme_json_add_le16(jsec, "nvq", ent->nvq);
+ nvme_json_add_le16(jsec, "nvi", ent->nvi);
+
+ return jsec;
+}
+
+struct json_object *nvme_id_secondary_ctrl_list_to_json(
+ struct nvme_secondary_ctrl_list *list, unsigned long flags)
+{
+ struct json_object *jlist, *jsecs;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(list, sizeof(*list), flags);
+
+ jlist = nvme_json_new_object(flags);
+ jsecs = nvme_json_new_array();
+
+ nvme_json_add_int(jlist, "nids", list->num);
+ for (i = 0; i < MIN(list->num, 127); i++)
+ json_object_array_add(jsecs,
+ nvme_secondary_ctrl_entry_to_json(&list->sc_entry[i],
+ flags));
+ json_object_object_add(jlist, "sc-entrys", jsecs);
+ return jlist;
+}
+
+static struct json_object *nvme_id_uuid_to_json(
+ struct nvme_id_uuid_list_entry *desc, unsigned long flags)
+{
+ struct json_object *juuid = nvme_json_new_object(flags);
+ __u8 assoc = desc->header & NVME_ID_UUID_HDR_ASSOCIATION_MASK;
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(juuid, "nidt", nvme_id_uuid_assoc_str(assoc), flags);
+ else
+ nvme_json_add_int(juuid, "association", assoc);
+ nvme_json_add_hex_array(juuid, "uuid", desc->uuid, 16);
+
+ return juuid;
+}
+
+struct json_object *nvme_id_uuid_list_to_json(
+ struct nvme_id_uuid_list *list, unsigned long flags)
+{
+ struct json_object *jlist, *juuids;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(list, sizeof(*list), flags);
+
+ jlist = nvme_json_new_object(flags);
+ juuids = nvme_json_new_array();
+
+ for (i = 0; i < NVME_ID_UUID_LIST_MAX; i++)
+ json_object_array_add(juuids,
+ nvme_id_uuid_to_json(&list->entry[i], flags));
+ json_object_object_add(jlist, "uuids", juuids);
+
+ return jlist;
+}
+
+struct json_object *nvme_ns_list_to_json(
+ struct nvme_ns_list *list, unsigned long flags)
+{
+ struct json_object *jlist, *jarray;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(list, sizeof(*list), flags);
+
+ jlist = nvme_json_new_object(flags);
+ jarray = nvme_json_new_array();
+ for (i = 0; i < 1024; i++) {
+ struct json_object *jnsid;
+ __u32 nsid;
+
+ nsid = le32_to_cpu(list->ns[i]);
+ if (!nsid)
+ break;
+
+ jnsid = nvme_json_new_int(nsid);
+ json_object_array_add(jarray, jnsid);
+ }
+ json_object_object_add(jlist, "nsids", jarray);
+
+ return jlist;
+}
+
+struct json_object *nvme_ctrl_list_to_json(
+ struct nvme_ctrl_list *list, unsigned long flags)
+{
+ struct json_object *jlist, *jarray;
+ __u16 num_ids;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(list, sizeof(*list), flags);
+
+ jlist = nvme_json_new_object(flags);
+ jarray = nvme_json_new_array();
+
+ num_ids = le16_to_cpu(list->num);
+ nvme_json_add_int(jlist, "nids", num_ids);
+
+ for (i = 0; i < MIN(num_ids, 2047); i++)
+ json_object_array_add(jarray, nvme_json_new_int(
+ le16_to_cpu(list->identifier[i])));
+
+ json_object_object_add(jlist, "cntlids", jarray);
+
+ return jlist;
+}
+
+static struct json_object *nvme_lbas_desc_to_json(struct nvme_lba_status_desc *lbasd)
+{
+ struct json_object *jlbasd;
+
+ jlbasd = nvme_json_new_object(0);
+
+ nvme_json_add_le64(jlbasd, "dslba", lbasd->dslba);
+ nvme_json_add_le32(jlbasd, "nlb", lbasd->nlb);
+ nvme_json_add_int(jlbasd, "status", lbasd->status);
+
+ return jlbasd;
+}
+
+struct json_object *nvme_lba_status_desc_list_to_json(
+ struct nvme_lba_status *lbas, unsigned long flags)
+{
+ struct json_object *jlbas, *jlbasds;
+ __u64 i, nlsd = le64_to_cpu(lbas->nlsd);
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(lbas,
+ sizeof(*lbas) + sizeof(lbas->descs[0]) * nlsd,
+ flags);
+
+ jlbas = nvme_json_new_object(flags);
+
+ nvme_json_add_int64(jlbas, "nlds", nlsd);
+ nvme_json_add_int(jlbas, "cmpc", lbas->cmpc);
+
+ jlbasds = nvme_json_new_array();
+ for (i = 0; i < nlsd; i++) {
+ struct json_object *jlbasd;
+
+ jlbasd = nvme_lbas_desc_to_json(&lbas->descs[i]);
+ json_object_array_add(jlbasds, jlbasd);
+ }
+ json_object_object_add(jlbas, "entries", jlbasds);
+
+ return jlbas;
+}
+
+static int nvme_ana_size(struct nvme_ana_log *ana)
+{
+ int i, offset = 0, ngroups = le16_to_cpu(ana->ngrps);
+ int ret = sizeof(*ana) + sizeof(ana->descs[0]) * ngroups;
+ void *base = ana;
+
+ for (i = 0; i < ngroups; i++) {
+ struct nvme_ana_group_desc *desc = base + offset;
+ int nnsids = le32_to_cpu(desc->nnsids);
+
+ ret += nnsids * sizeof(desc->nsids[0]);
+ }
+ return ret;
+}
+
+static struct json_object *nvme_ana_desc_to_json(
+ struct nvme_ana_group_desc *desc, int *offset, unsigned long flags)
+{
+ struct json_object *jdesc, *jnsids;
+ int j, nnsids;
+
+ jdesc = nvme_json_new_object(flags);
+ nnsids = le32_to_cpu(desc->nnsids);
+ nvme_json_add_le32(jdesc, "anagid", desc->grpid);
+ nvme_json_add_int(jdesc, "nnsids", nnsids);
+ nvme_json_add_le64(jdesc, "cc", desc->chgcnt);
+ nvme_json_add_int(jdesc, "state", desc->state);
+
+ jnsids = nvme_json_new_array();
+ for (j = 0; j < nnsids; j++) {
+ __u32 nsid = le32_to_cpu(desc->nsids[j]);
+ json_object_array_add(jnsids, nvme_json_new_int(nsid));
+ }
+ json_object_object_add(jdesc, "nsids", jnsids);
+ *offset += nnsids * sizeof(__u32);
+
+ return jdesc;
+}
+
+struct json_object *nvme_ana_log_to_json(
+ struct nvme_ana_log *ana, unsigned long flags)
+{
+ struct json_object *jana, *jdescs;
+ int i, offset = 0, ngroups = le16_to_cpu(ana->ngrps);
+ void *base = &ana->descs;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(ana, nvme_ana_size(ana), flags);
+
+ jana = nvme_json_new_object(flags);
+ nvme_json_add_le64(jana, "cc", ana->chgcnt);
+ nvme_json_add_int(jana, "nagd", ngroups);
+
+ jdescs = nvme_json_new_array();
+ for (i = 0; i < ngroups; i++) {
+ struct nvme_ana_group_desc *desc = base + offset;
+
+ json_object_array_add(jdescs,
+ nvme_ana_desc_to_json(desc, &offset, flags));
+ }
+ json_object_object_add(jana, "anagds", jdescs);
+
+ return jana;
+}
+
+static struct json_object *nvme_disc_entry_to_json(
+ struct nvmf_disc_log_entry *e, unsigned long flags)
+{
+ struct json_object *jentry = nvme_json_new_object(flags);
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(jentry, "trtype", nvmf_trtype_str(e->trtype), flags);
+ else
+ nvme_json_add_int(jentry, "trtype", e->trtype);
+
+ nvme_json_add_int(jentry, "adrfam", e->adrfam);
+ nvme_json_add_int(jentry, "subtype", e->subtype);
+ nvme_json_add_int(jentry, "treq", e->treq);
+ nvme_json_add_le16(jentry, "portid", e->portid);
+ nvme_json_add_le16(jentry, "cntlid", e->cntlid);
+ nvme_json_add_le16(jentry, "asqsz", e->asqsz);
+ nvme_json_add_str_flags(jentry, "trsvcid", e->trsvcid,
+ strnlen(e->trsvcid, sizeof(e->trsvcid)), flags);
+ nvme_json_add_str_flags(jentry, "subnqn", e->subnqn,
+ strnlen(e->subnqn, sizeof(e->subnqn)), flags);
+ nvme_json_add_str_flags(jentry, "traddr", e->traddr,
+ strnlen(e->traddr, sizeof(e->traddr)), flags);
+ nvme_json_add_hex_array(jentry, "tsas", (void *)e->tsas.common, 16);
+
+ return jentry;
+}
+
+struct json_object *nvme_discovery_log_to_json(
+ struct nvmf_discovery_log *log, unsigned long flags)
+{
+ struct json_object *jlog, *jentries;
+ int i, numrec = le64_to_cpu(log->numrec);
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(log,
+ sizeof(*log) + sizeof(log->entries[0]) * numrec,
+ flags);
+
+ jlog = nvme_json_new_object(flags);
+ nvme_json_add_le64(jlog, "genctr", log->genctr);
+ nvme_json_add_int(jlog, "numrec", numrec);
+ nvme_json_add_le16(jlog, "recfmt", log->recfmt);
+
+ jentries = nvme_json_new_array();
+ for (i = 0; i < numrec; i++) {
+ struct nvmf_disc_log_entry *e = &log->entries[i];
+ struct json_object *jentry;
+
+ jentry = nvme_disc_entry_to_json(e, flags);
+ if (!jentry)
+ break;
+
+ json_object_array_add(jentries, jentry);
+ }
+ json_object_object_add(jlog, "entries", jentries);
+
+ return jlog;
+}
+
+struct json_object *nvme_cmd_effects_log_to_json(
+ struct nvme_cmd_effects_log *effects, unsigned long flags)
+{
+ struct json_object *jeffects;
+ __u32 effect;
+ char key[8];
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(effects, sizeof(*effects), flags);
+
+ jeffects = nvme_json_new_object(flags);
+ for (i = 0; i < 255; i++) {
+ effect = le32_to_cpu(effects->acs[i]);
+ if (!(effect & NVME_CMD_EFFECTS_CSUPP))
+ continue;
+ sprintf(key, "acs%d", i);
+ nvme_json_add_int(jeffects, key, effect);
+ }
+
+ for (i = 0; i < 255; i++) {
+ effect = le32_to_cpu(effects->acs[i]);
+ if (!(effect & NVME_CMD_EFFECTS_CSUPP))
+ continue;
+ sprintf(key, "iocs%d", i);
+ nvme_json_add_le32(jeffects, key, effects->iocs[i]);
+ }
+
+ return jeffects;
+}
+
+struct json_object *nvme_ege_aggregate_log(
+ struct nvme_eg_event_aggregate_log *eglog, unsigned long flags)
+{
+ struct json_object *jeglog, *jegs;
+ int i, nents = le64_to_cpu(eglog->nr_entries);
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(eglog,
+ sizeof(*eglog) + sizeof(eglog->egids[0]) * nents,
+ flags);
+
+ jeglog = nvme_json_new_object(flags);
+ nvme_json_add_int(jeglog, "nents", nents);
+
+ jegs = nvme_json_new_array();
+ for (i = 0; i < nents; i++) {
+ int egid = le16_to_cpu(eglog->egids[i]);
+
+ if (!egid)
+ break;
+ json_object_array_add(jegs, nvme_json_new_int(egid));
+ }
+ json_object_object_add(jeglog, "cntlids", jegs);
+
+ return jeglog;
+}
+
+struct json_object *nvme_endurance_group_log_to_json(
+ struct nvme_endurance_group_log *eg, unsigned long flags)
+{
+ struct json_object *jeg;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(eg, sizeof(*eg), flags);
+
+ jeg = nvme_json_new_object(flags);
+ nvme_json_add_int(jeg, "cw", eg->critical_warning);
+ nvme_json_add_int(jeg, "ap", eg->avl_spare);
+ nvme_json_add_int(jeg, "apt", eg->avl_spare_threshold);
+ nvme_json_add_int(jeg, "pu", eg->percent_used);
+ nvme_json_add_int128(jeg, "ee", eg->endurance_estimate);
+ nvme_json_add_int128(jeg, "dur", eg->data_units_read);
+ nvme_json_add_int128(jeg, "duw", eg->data_units_written);
+ nvme_json_add_int128(jeg, "mwc", eg->media_units_written);
+ nvme_json_add_int128(jeg, "hrc", eg->host_read_cmds);
+ nvme_json_add_int128(jeg, "hwc", eg->host_write_cmds);
+ nvme_json_add_int128(jeg, "mdie", eg->media_data_integrity_err);
+ nvme_json_add_int128(jeg, "nele", eg->num_err_info_log_entries);
+
+ return jeg;
+}
+
+static struct json_object *nvme_error_to_json(
+ struct nvme_error_log_page *log, unsigned long flags)
+{
+ struct json_object *jerror = nvme_json_new_object(flags);
+
+ nvme_json_add_le64(jerror, "error-count", log->error_count);
+ nvme_json_add_le16(jerror, "sqid", log->sqid);
+ nvme_json_add_le16(jerror, "cmdid", log->cmdid);
+ nvme_json_add_hex_le16_flags(jerror, "status-field", log->status_field, flags);
+ nvme_json_add_hex_le16_flags(jerror, "err-loc", log->parm_error_location, flags);
+ nvme_json_add_le64(jerror, "lba", log->lba);
+ nvme_json_add_le32(jerror, "nsid", log->nsid);
+ nvme_json_add_int(jerror, "vsia", log->vs);
+
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_str(jerror, "trtype", nvmf_trtype_str(log->trtype), flags);
+ else
+ nvme_json_add_int(jerror, "trtype", log->trtype);
+ nvme_json_add_le64(jerror, "csi", log->cs);
+ nvme_json_add_le16(jerror, "ttsi", log->trtype_spec_info);
+
+ return jerror;
+}
+
+struct json_object *nvme_error_log_to_json(
+ struct nvme_error_log_page *log, int entries, unsigned long flags)
+{
+ struct json_object *jlog, *jerrors;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(log, sizeof(*log) * entries,
+ flags);
+
+ flags |= NVME_JSON_COMPACT;
+ jlog = nvme_json_new_object(flags);
+ jerrors = nvme_json_new_array();
+ for (i = 0; i < entries; i++)
+ json_object_array_add(jerrors,
+ nvme_error_to_json(log + i, flags));
+ json_object_object_add(jlog, "error", jerrors);
+
+ return jlog;
+}
+
+struct json_object *nvme_fw_slot_log_to_json(
+ struct nvme_firmware_slot *fw, unsigned long flags)
+{
+ struct json_object *jfw = nvme_json_new_object(flags);
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(fw, sizeof(*fw), flags);
+
+ nvme_json_add_int(jfw, "afi", fw->afi);
+ for (i = 0; i < 7; i++) {
+ char key[5] = { 0 }; /* frsX\0 */
+
+ if (!strcmp(fw->frs[i], "") ||
+ !fw->frs[i][0])
+ continue;
+
+ sprintf(key, "frs%d", i + 1);
+ nvme_json_add_str_flags(jfw, key, fw->frs[i],
+ sizeof(fw->frs[i]), 0);
+ }
+ return jfw;
+}
+
+static struct json_object *nvme_lba_status_lba_rd_to_json(
+ struct nvme_lba_rd *rd, unsigned long flags)
+{
+ struct json_object *jrd = nvme_json_new_object(flags);
+
+ nvme_json_add_le64(jrd, "rslba", rd->rslba);
+ nvme_json_add_le32(jrd, "rnlb", rd->rnlb);
+
+ return jrd;
+}
+
+static struct json_object *nvme_lba_status_log_ns_element_to_json(
+ struct nvme_lbas_ns_element *element, int *offset,
+ unsigned long flags)
+{
+ int i, neid = le32_to_cpu(element->neid);
+ struct json_object *jelem, *jrds;
+
+ jelem = nvme_json_new_object(flags);
+ nvme_json_add_int(jelem, "neid", neid);
+ nvme_json_add_le32(jelem, "nrld", element->nrld);
+ nvme_json_add_int(jelem, "ratype", element->ratype);
+
+ jrds = nvme_json_new_array();
+ for (i = 0; i < neid; i++)
+ json_object_array_add(jrds,
+ nvme_lba_status_lba_rd_to_json(&element->lba_rd[i],
+ flags));
+ json_object_object_add(jelem, "lbards", jrds);
+
+ *offset += sizeof(*element) + neid * sizeof(element->lba_rd[0]);
+ return jelem;
+}
+
+struct json_object *nvme_lba_status_log_to_json(
+ struct nvme_lba_status_log *lbas, unsigned long flags)
+{
+ struct json_object *jlbas, *jelems;
+ int offset = 0, lslplen = le32_to_cpu(lbas->lslplen);
+ void *base = &lbas->elements;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(base, lslplen, flags);
+
+ jlbas = nvme_json_new_object(flags);
+ nvme_json_add_int(jlbas, "lslplen", lslplen);
+ nvme_json_add_le32(jlbas, "nlslne", lbas->nlslne);
+ nvme_json_add_le32(jlbas, "estulb", lbas->estulb);
+ nvme_json_add_le16(jlbas, "lsgc", lbas->lsgc);
+
+ jelems = nvme_json_new_array();
+ while (offset < lslplen - sizeof(*lbas)) {
+ struct nvme_lbas_ns_element *element = base + offset;
+
+ json_object_array_add(jelems,
+ nvme_lba_status_log_ns_element_to_json(element,
+ &offset, flags));
+ }
+ json_object_object_add(jlbas, "jelems", jelems);
+
+ return jlbas;
+}
+
+
+struct json_object *nvme_aggr_predictable_lat_log_to_json(
+ struct nvme_aggregate_predictable_lat_event *pl, unsigned long flags)
+{
+ __u64 num_entries = le64_to_cpu(pl->num_entries);
+ struct json_object *jpl, *jentries;
+ int i;
+
+ jpl = nvme_json_new_object(flags);
+ nvme_json_add_int64(jpl, "nents", num_entries);
+
+ jentries = nvme_json_new_array();
+ for (i = 0; i < num_entries; i++)
+ json_object_array_add(jentries,
+ nvme_json_new_int(le16_to_cpu(pl->entries[i])));
+ json_object_object_add(jpl, "entries", jentries);
+
+ return jpl;
+}
+
+struct json_object *nvme_nvmset_predictable_lat_log_to_json(
+ struct nvme_nvmset_predictable_lat_log *pl, unsigned long flags)
+{
+ struct json_object *jpl;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(pl, sizeof(*pl), flags);
+
+ jpl = nvme_json_new_object(flags);
+ nvme_json_add_int(jpl, "status", pl->status);
+ nvme_json_add_le16(jpl, "event_type", pl->event_type);
+ nvme_json_add_le64(jpl, "dtwin_rt", pl->dtwin_rt);
+ nvme_json_add_le64(jpl, "dtwin_wt", pl->dtwin_wt);
+ nvme_json_add_le64(jpl, "dtwin_tmax", pl->dtwin_tmax);
+ nvme_json_add_le64(jpl, "dtwin_tmin_hi", pl->dtwin_tmin_hi);
+ nvme_json_add_le64(jpl, "dtwin_tmin_lo", pl->dtwin_tmin_lo);
+ nvme_json_add_le64(jpl, "dtwin_re", pl->dtwin_re);
+ nvme_json_add_le64(jpl, "dtwin_we", pl->dtwin_we);
+ nvme_json_add_le64(jpl, "dtwin_te", pl->dtwin_te);
+
+ return jpl;
+}
+
+
+struct json_object *nvme_resv_notify_log_to_json(
+ struct nvme_resv_notification_log *resv, unsigned long flags)
+{
+ struct json_object *jresv;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(resv, sizeof(*resv), flags);
+
+ jresv = nvme_json_new_object(flags);
+ nvme_json_add_le64(jresv, "lpc", resv->lpc);
+ nvme_json_add_int(jresv, "rnlpt", resv->rnlpt);
+ nvme_json_add_int(jresv, "nalp", resv->nalp);
+ nvme_json_add_le32(jresv, "nsid", resv->nsid);
+
+ return jresv;
+}
+
+struct json_object *nvme_sanitize_log_to_json(
+ struct nvme_sanitize_log_page *san, unsigned long flags)
+{
+ struct json_object *jsan;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(san, sizeof(*san), flags);
+
+ jsan = nvme_json_new_object(flags);
+ nvme_json_add_le16(jsan, "sprog", san->sprog);
+ nvme_json_add_le16(jsan, "sstat", san->sstat);
+ nvme_json_add_le32(jsan, "scdw10", san->scdw10);
+ nvme_json_add_le32(jsan, "eto", san->eto);
+ nvme_json_add_le32(jsan, "etbe", san->etbe);
+ nvme_json_add_le32(jsan, "etce", san->etce);
+ nvme_json_add_le32(jsan, "etond", san->etond);
+ nvme_json_add_le32(jsan, "etbend", san->etbend);
+ nvme_json_add_le32(jsan, "etcend", san->etcend);
+
+ return jsan;
+}
+
+static struct json_object *nvme_self_test_to_json(
+ struct nvme_st_result *str, unsigned long flags)
+{
+ struct json_object *jstr;
+ __u8 op = str->dsts & NVME_ST_RESULT_MASK;
+
+ jstr = nvme_json_new_object(flags);
+ nvme_json_add_int(jstr, "dstop", op);
+
+ if (op == NVME_ST_RESULT_NOT_USED)
+ return jstr;
+
+ nvme_json_add_int(jstr, "stc", str->dsts >> NVME_ST_CODE_SHIFT);
+ nvme_json_add_int(jstr, "seg", str->seg);
+ nvme_json_add_int(jstr, "vid", str->vdi);
+ nvme_json_add_le64(jstr, "poh", str->poh);
+ if (str->vdi & NVME_ST_VALID_DIAG_INFO_NSID)
+ nvme_json_add_le32(jstr, "nsid", str->nsid);
+ if (str->vdi & NVME_ST_VALID_DIAG_INFO_FLBA)
+ nvme_json_add_le64(jstr, "flba", str->flba);
+ if (str->vdi & NVME_ST_VALID_DIAG_INFO_SCT)
+ nvme_json_add_int(jstr, "sct", str->sct);
+ if (str->vdi & NVME_ST_VALID_DIAG_INFO_SC)
+ nvme_json_add_int(jstr, "sc", str->sc);
+ nvme_json_add_int(jstr, "vs", (str->vs[1] << 8) | (str->vs[0]));
+
+ return jstr;
+}
+
+struct json_object *nvme_dev_self_test_log_to_json(
+ struct nvme_self_test_log *st, unsigned long flags)
+{
+ struct json_object *jst, *jstrs;
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(st, sizeof(*st), flags);
+
+ jst = nvme_json_new_object(flags);
+ jstrs = nvme_json_new_array();
+
+ nvme_json_add_int(jst, "current-operation", st->current_operation);
+ nvme_json_add_int(jst, "completion", st->completion);
+
+ for (i = 0; i < NVME_LOG_ST_MAX_RESULTS; i++)
+ json_object_array_add(jstrs,
+ nvme_self_test_to_json(&st->result[i], flags));
+ json_object_object_add(jst, "nsids", jstrs);
+
+ return jst;
+}
+
+static void nvme_json_add_smart_cw(struct json_object *j, const char *n,
+ __u8 cw, unsigned long flags)
+{
+ struct json_object *jcw;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, "cw", cw, flags);
+ return;
+ }
+
+ jcw = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jcw, "value", cw, flags);
+ nvme_json_add_flag_flags(jcw, "spare", cw, NVME_SMART_CRIT_SPARE, flags);
+ nvme_json_add_flag_flags(jcw, "temperature", cw, NVME_SMART_CRIT_TEMPERATURE, flags);
+ nvme_json_add_flag_flags(jcw, "degraded", cw, NVME_SMART_CRIT_DEGRADED, flags);
+ nvme_json_add_flag_flags(jcw, "media", cw, NVME_SMART_CRIT_MEDIA, flags);
+ nvme_json_add_flag_flags(jcw, "memory-backup", cw, NVME_SMART_CRIT_VOLATILE_MEMORY, flags);
+ nvme_json_add_flag_flags(jcw, "pmr-ro", cw, NVME_SMART_CRIT_PMR_RO, flags);
+
+ json_object_object_add(j, n, jcw);
+}
+
+static void nvme_json_add_smart_egcw(struct json_object *j, const char *n,
+ __u8 egcw, unsigned long flags)
+{
+ struct json_object *jegcw;
+
+ if (!(flags & NVME_JSON_DECODE_COMPLEX)) {
+ nvme_json_add_0x(j, "egcw", egcw, flags);
+ return;
+ }
+
+ jegcw = nvme_json_new_object(flags);
+
+ nvme_json_add_0x(jegcw, "value", egcw, flags);
+ nvme_json_add_flag_flags(jegcw, "spare", egcw, NVME_SMART_EGCW_SPARE, flags);
+ nvme_json_add_flag_flags(jegcw, "degraded", egcw, NVME_SMART_EGCW_DEGRADED, flags);
+ nvme_json_add_flag_flags(jegcw, "read-only", egcw, NVME_SMART_EGCW_RO, flags);
+
+ json_object_object_add(j, n, jegcw);
+}
+
+struct json_object *nvme_smart_log_to_json(
+ struct nvme_smart_log *smart, unsigned long flags)
+{
+ __u32 temp = unalign_int(smart->temperature, sizeof(smart->temperature));
+ struct json_object *jsmart = nvme_json_new_object(flags);
+ int i;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(smart, sizeof(*smart), flags);
+
+ nvme_json_add_smart_cw(jsmart, "critical-warning", smart->critical_warning, flags);
+ nvme_json_add_temp(jsmart, "composite-temp", temp, flags);
+ nvme_json_add_percent(jsmart, "available-spare", smart->avail_spare, flags);
+ nvme_json_add_percent(jsmart, "spare-threshold", smart->spare_thresh, flags);
+ nvme_json_add_percent(jsmart, "percent-used", smart->percent_used, flags);
+ nvme_json_add_smart_egcw(jsmart, "endgrp-crit-warning", smart->endu_grp_crit_warn_sumry, flags);
+ nvme_json_add_int128(jsmart, "data-units-read", smart->data_units_read);
+ nvme_json_add_int128(jsmart, "data-units-written", smart->data_units_written);
+ nvme_json_add_int128(jsmart, "host-reads", smart->host_reads);
+ nvme_json_add_int128(jsmart, "host-writes", smart->host_writes);
+ nvme_json_add_int128(jsmart, "ctrl-busy-time", smart->ctrl_busy_time);
+ nvme_json_add_int128(jsmart, "power-cycles", smart->power_cycles);
+ nvme_json_add_int128(jsmart, "power-on-hours", smart->power_on_hours);
+ nvme_json_add_int128(jsmart, "unsafe-shutdowns", smart->unsafe_shutdowns);
+ nvme_json_add_int128(jsmart, "media-errors", smart->media_errors);
+ nvme_json_add_int128(jsmart, "error-log-entries", smart->num_err_log_entries);
+ nvme_json_add_time_m_flags(jsmart, "warning-temp-time", smart->warning_temp_time, flags);
+ nvme_json_add_time_m_flags(jsmart, "crit-comp-temp-time", smart->critical_comp_time, flags);
+
+ for (i = 0; i < 8; i++) {
+ __u32 t = le16_to_cpu(smart->temp_sensor[i]);
+ char key[4] = {};
+
+ if (t == 0)
+ continue;
+ sprintf(key, "ts%d", i + 1);
+ nvme_json_add_temp(jsmart, key, t, flags);
+ }
+
+ nvme_json_add_le32(jsmart, "therm-mgmt-t1-trans-cnt", smart->thm_temp1_trans_count);
+ nvme_json_add_le32(jsmart, "therm-mgmt-t2-trans-cnt", smart->thm_temp2_trans_count);
+ nvme_json_add_le32(jsmart, "therm-mgmt-t1-total-time", smart->thm_temp1_total_time);
+ nvme_json_add_le32(jsmart, "therm-mgmt-t2-total-time", smart->thm_temp2_total_time);
+
+ return jsmart;
+}
+
+struct json_object *nvme_telemetry_log_to_json(
+ struct nvme_telemetry_log *telem, unsigned long flags)
+{
+ struct json_object *jtelem;
+ int size = sizeof(*telem) + NVME_LOG_TELEM_BLOCK_SIZE *
+ le16_to_cpu(telem->dalb3);
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(telem, size, flags);
+
+ jtelem = nvme_json_new_object(flags);
+ nvme_json_add_int(jtelem, "lpi", telem->lpi);
+ nvme_json_add_hex(jtelem, "ieee", nvme_ieee_to_int(telem->ieee), flags);
+ nvme_json_add_le16(jtelem, "da1lb", telem->dalb1);
+ nvme_json_add_le16(jtelem, "da2lb", telem->dalb2);
+ nvme_json_add_le16(jtelem, "da3lb", telem->dalb3);
+ nvme_json_add_int(jtelem, "tcida", telem->ctrlavail);
+ nvme_json_add_int(jtelem, "tcidgn", telem->ctrldgn);
+ nvme_json_add_hex_array(jtelem, "rid", telem->rsnident, sizeof(telem->rsnident));
+
+ return jtelem;
+}
+
+struct json_object *nvme_persistent_event_log_to_json(
+ struct nvme_persistent_event_log *pel, unsigned long flags)
+{
+ struct json_object *jpel;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(pel, sizeof(*pel), flags);
+
+ jpel = nvme_json_new_object(flags);
+ nvme_json_add_int(jpel, "lid", pel->lid);
+ nvme_json_add_le32(jpel, "ttl", pel->ttl);
+ nvme_json_add_int(jpel, "rv", pel->rv);
+ nvme_json_add_le16(jpel, "lht", pel->lht);
+ nvme_json_add_le64(jpel, "ts", pel->ts);
+ nvme_json_add_int128(jpel, "poh", pel->poh);
+ nvme_json_add_le64(jpel, "pcc", pel->pcc);
+ nvme_json_add_hex(jpel, "vid", le16_to_cpu(pel->vid), flags);
+ nvme_json_add_hex(jpel, "ssvid", le16_to_cpu(pel->ssvid), flags);
+ nvme_json_add_str_flags(jpel, "sn", pel->sn, sizeof(pel->sn), 0);
+ nvme_json_add_str_flags(jpel, "mn", pel->mn, sizeof(pel->mn), 0);
+ nvme_json_add_str_flags(jpel, "subnqn", pel->subnqn, sizeof(pel->subnqn), 0);
+ nvme_json_add_hex_array(jpel, "seb", pel->seb, sizeof(pel->seb));
+
+ return jpel;
+}
+
+struct json_object *nvme_props_to_json(void *regs, unsigned long flags)
+{
+ struct json_object *jregs;
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(regs, 0x1000, flags);
+
+ jregs = nvme_json_new_object(flags);
+ nvme_json_add_le64_ptr(jregs, "cap", regs + NVME_REG_CAP);
+ nvme_json_add_le32_ptr(jregs, "vs", regs + NVME_REG_VS);
+ nvme_json_add_le32_ptr(jregs, "intms", regs + NVME_REG_INTMS);
+ nvme_json_add_le32_ptr(jregs, "intmc", regs + NVME_REG_INTMC);
+ nvme_json_add_le32_ptr(jregs, "cc", regs + NVME_REG_CC);
+ nvme_json_add_le32_ptr(jregs, "csts", regs + NVME_REG_CSTS);
+ nvme_json_add_le32_ptr(jregs, "nssr", regs + NVME_REG_NSSR);
+ nvme_json_add_le32_ptr(jregs, "aqa", regs + NVME_REG_AQA);
+ nvme_json_add_le64_ptr(jregs, "asq", regs + NVME_REG_ASQ);
+ nvme_json_add_le64_ptr(jregs, "acq", regs + NVME_REG_ACQ);
+ nvme_json_add_le32_ptr(jregs, "cmbloc", regs + NVME_REG_CMBLOC);
+ nvme_json_add_le32_ptr(jregs, "cmbsz", regs + NVME_REG_CMBSZ);
+ nvme_json_add_le32_ptr(jregs, "bpinfo", regs + NVME_REG_BPINFO);
+ nvme_json_add_le32_ptr(jregs, "bprsel", regs + NVME_REG_BPRSEL);
+ nvme_json_add_le64_ptr(jregs, "bpmbl", regs + NVME_REG_BPMBL);
+ nvme_json_add_le64_ptr(jregs, "cmbmsc", regs + NVME_REG_CMBMSC);
+ nvme_json_add_le32_ptr(jregs, "cmbsts", regs + NVME_REG_CMBSTS);
+ nvme_json_add_le32_ptr(jregs, "pmrcap", regs + NVME_REG_PMRCAP);
+ nvme_json_add_le32_ptr(jregs, "pmrctl", regs + NVME_REG_PMRCTL);
+ nvme_json_add_le32_ptr(jregs, "pmrsts", regs + NVME_REG_PMRSTS);
+ nvme_json_add_le32_ptr(jregs, "pmrebs", regs + NVME_REG_PMREBS);
+ nvme_json_add_le32_ptr(jregs, "pmrswtp", regs + NVME_REG_PMRSWTP);
+ nvme_json_add_le64_ptr(jregs, "pmrmsc", regs + NVME_REG_PMRMSC);
+
+ return jregs;
+}
+
+static struct json_object *nvme_resv_ctrl_to_json(
+ struct nvme_registered_ctrl *regctl,
+ unsigned long flags)
+{
+ struct json_object *jrc;
+
+ jrc = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jrc, "cntlid", regctl->cntlid);
+ nvme_json_add_int(jrc, "rcsts", regctl->rcsts);
+ nvme_json_add_le64(jrc, "hostid", regctl->hostid);
+ nvme_json_add_le64(jrc, "rkey", regctl->rkey);
+
+ return jrc;
+}
+
+static struct json_object *nvme_resv_ctrl_ext_to_json(
+ struct nvme_registered_ctrl_ext *regctl,
+ unsigned long flags)
+{
+ struct json_object *jrc;
+
+ jrc = nvme_json_new_object(flags);
+
+ nvme_json_add_le16(jrc, "cntlid", regctl->cntlid);
+ nvme_json_add_int(jrc, "rcsts", regctl->rcsts);
+ nvme_json_add_le64(jrc, "rkey", regctl->rkey);
+ nvme_json_add_int128(jrc, "hostid", regctl->hostid);
+
+ return jrc;
+}
+
+struct json_object *nvme_resv_report_to_json(
+ struct nvme_reservation_status *status, bool ext,
+ unsigned long flags)
+{
+ int i, regctl = status->regctl[0] | (status->regctl[1] << 8);
+ struct json_object *jrs, *jrcs;
+ int size = sizeof(*status) - sizeof(status->rsvd24) +
+ regctl * (ext ? sizeof(status->regctl_eds[0]) :
+ sizeof(status->regctl_ds[0]));
+
+ if (flags & NVME_JSON_BINARY)
+ return nvme_json_new_str_len_flags(status, size, flags);
+
+ jrs = nvme_json_new_object(flags);
+
+ nvme_json_add_le32(jrs, "gen", status->gen);
+ nvme_json_add_int(jrs, "rtype", status->rtype);
+ nvme_json_add_int(jrs, "regctl", regctl);
+ nvme_json_add_int(jrs, "ptpls", status->ptpls);
+
+ jrcs = nvme_json_new_array();
+ for (i = 0; i < regctl; i++) {
+ struct json_object *jrc;
+
+ if (ext)
+ jrc = nvme_resv_ctrl_ext_to_json(
+ &status->regctl_eds[i], flags);
+ else
+ jrc = nvme_resv_ctrl_to_json(
+ &status->regctl_ds[i], flags);
+
+ json_object_array_add(jrcs, jrc);
+ }
+ json_object_object_add(jrs, "nsids", jrcs);
+
+ return NULL;
+}
--- /dev/null
+#ifndef _JSON_UTIL_H
+#define _JSON_UTIL_H
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <json-c/json.h>
+
+#include "libnvme.h"
+#include "nvme.h"
+
+enum nvme_print_flags {
+ _NVME_JSON_HUMAN,
+ _NVME_JSON_HIDE_UNSUPPORTED,
+ _NVME_JSON_DECODE_COMPLEX,
+ _NVME_JSON_TABULAR,
+ _NVME_JSON_COMPACT,
+ _NVME_JSON_BINARY,
+ _NVME_JSON_TREE,
+
+ NVME_JSON_HUMAN = 1 << _NVME_JSON_HUMAN,
+ NVME_JSON_HIDE_UNSUPPORTED = 1 << _NVME_JSON_HIDE_UNSUPPORTED,
+ NVME_JSON_DECODE_COMPLEX = 1 << _NVME_JSON_DECODE_COMPLEX,
+ NVME_JSON_TABULAR = 1 << _NVME_JSON_TABULAR,
+ NVME_JSON_COMPACT = 1 << _NVME_JSON_COMPACT,
+ NVME_JSON_BINARY = 1 << _NVME_JSON_BINARY,
+ NVME_JSON_TREE = 1 << _NVME_JSON_TREE,
+};
+
+struct json_object *nvme_identify_directives_to_json(
+ struct nvme_id_directives *idd, unsigned long flags);
+
+struct json_object *nvme_streams_status_to_json(
+ struct nvme_streams_directive_status *sds, unsigned long flags);
+
+struct json_object *nvme_streams_params_to_json(
+ struct nvme_streams_directive_params *sdp, unsigned long flags);
+
+struct json_object *nvme_streams_allocated_to_json(__u16 nsa, unsigned long flags);
+
+struct json_object *nvme_endurance_group_log_to_json(
+ struct nvme_endurance_group_log *eg, unsigned long flags);
+
+struct json_object *nvme_telemetry_log_to_json(
+ struct nvme_telemetry_log *telem, unsigned long flags);
+
+struct json_object *nvme_dev_self_test_log_to_json(
+ struct nvme_self_test_log *st, unsigned long flags);
+
+struct json_object *nvme_cmd_effects_log_to_json(
+ struct nvme_cmd_effects_log *effects, unsigned long flags);
+
+struct json_object *nvme_fw_slot_log_to_json(
+ struct nvme_firmware_slot *fw, unsigned long flags);
+
+struct json_object *nvme_smart_log_to_json(
+ struct nvme_smart_log *smart, unsigned long flags);
+
+struct json_object *nvme_identify_to_json(void *id, __u8 cns,
+ unsigned long flags);
+
+struct json_object *nvme_feature_to_json(__u8 fid, __u32 value, unsigned len,
+ void *data, unsigned long flags);
+
+struct json_object *nvme_props_to_json(void *regs, unsigned long flags);
+
+const char *nvme_feature_to_string(int feature);
+
+struct json_object *nvme_ctrl_list_to_json(
+ struct nvme_ctrl_list *list, unsigned long flags);
+
+struct json_object *nvme_lba_status_desc_list_to_json(
+ struct nvme_lba_status *lbas, unsigned long flags);
+
+struct json_object *nvme_id_ns_to_json(struct nvme_id_ns *id_ns,
+ unsigned long flags);
+
+struct json_object *nvme_id_ctrl_to_json(
+ struct nvme_id_ctrl *id_ctrl, unsigned long flags);
+
+struct json_object *nvme_id_ns_desc_list_to_json(void *list,
+ unsigned long flags);
+
+struct json_object *nvme_id_ns_granularity_list_to_json(
+ struct nvme_id_ns_granularity_list *glist, unsigned long flags);
+
+struct json_object *nvme_id_nvm_set_list_to_json(
+ struct nvme_id_nvmset_list *nvmset, unsigned long flags);
+
+struct json_object *nvme_id_primary_ctrl_cap_to_json(
+ struct nvme_primary_ctrl_cap *cap, unsigned long flags);
+
+struct json_object *nvme_id_secondary_ctrl_list_to_json(
+ struct nvme_secondary_ctrl_list *list, unsigned long flags);
+
+struct json_object *nvme_id_uuid_list_to_json(
+ struct nvme_id_uuid_list *list, unsigned long flags);
+
+struct json_object *nvme_ana_log_to_json(
+ struct nvme_ana_log *ana, unsigned long flags);
+
+struct json_object *nvme_discovery_log_to_json(
+ struct nvmf_discovery_log *log, unsigned long flags);
+
+struct json_object *nvme_resv_notify_log_to_json(
+ struct nvme_resv_notification_log *resv, unsigned long flags);
+
+struct json_object *nvme_nvmset_predictable_lat_log_to_json(
+ struct nvme_nvmset_predictable_lat_log *pl, unsigned long flags);
+
+struct json_object *nvme_aggr_predictable_lat_log_to_json(
+ struct nvme_aggregate_predictable_lat_event *pl, unsigned long flags);
+
+struct json_object *nvme_sanitize_log_to_json(
+ struct nvme_sanitize_log_page *san, unsigned long flags);
+
+struct json_object *nvme_lba_status_log_to_json(
+ struct nvme_lba_status_log *lbas, unsigned long flags);
+
+struct json_object *nvme_ege_aggregate_log(
+ struct nvme_eg_event_aggregate_log *eglog, unsigned long flags);
+
+struct json_object *nvme_persistent_event_log_to_json(
+ struct nvme_persistent_event_log *pel, unsigned long flags);
+
+struct json_object *nvme_error_log_to_json(
+ struct nvme_error_log_page *log, int entries, unsigned long flags);
+
+struct json_object *nvme_ns_list_to_json(
+ struct nvme_ns_list *list, unsigned long flags);
+
+struct json_object *nvme_resv_report_to_json(
+ struct nvme_reservation_status *status, bool ext,
+ unsigned long flags);
+
+struct json_object *nvme_json_new_str_len(const char *v, int len);
+struct json_object *nvme_json_new_str_len_flags(const void *v, int len, unsigned long flags);
+struct json_object *nvme_json_new_str(const char *v, unsigned long flags);
+struct json_object *nvme_json_new_int128(uint8_t *v);
+struct json_object *nvme_json_new_int64(uint64_t v);
+struct json_object *nvme_json_new_int(uint32_t v);
+struct json_object *nvme_json_new_bool(bool v);
+struct json_object *nvme_json_new_object(unsigned long flags);
+struct json_object *nvme_json_new_array();
+
+struct json_object *nvme_json_new_storage_128(uint8_t *v, unsigned long flags);
+struct json_object *nvme_json_new_storage(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_size(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_memory(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_hex_array(uint8_t *v, uint32_t len);
+struct json_object *nvme_json_new_hex(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_0x(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_percent(uint8_t v, unsigned long flags);
+struct json_object *nvme_json_new_temp(uint16_t v, unsigned long flags);
+struct json_object *nvme_json_new_time_us(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_time_s(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_hecto_uwatts(uint64_t v, unsigned long flags);
+struct json_object *nvme_json_new_uuid(uint8_t *v, unsigned long flags);
+struct json_object *nvme_json_new_oui(uint8_t *v, int len, unsigned long flags);
+struct json_object *nvme_json_new_bool_terse(bool v);
+
+#define bit_set(a, b) !!(a[b / 8] & (b % 8))
+#define is_set(v, f) !!(v & f)
+
+static inline __u64 unalign_int(uint8_t *data, int len)
+{
+ __u32 ret = 0;
+ int i;
+
+ for (i = len - 1; i >= 0; i--)
+ ret = ret * 256 + data[i];
+ return ret;
+}
+
+static inline uint64_t int48_to_long(__u8 *data)
+{
+ return (uint64_t)unalign_int((uint8_t *)data, 6);
+}
+
+static inline __u64 read64(void *addr)
+{
+ __le32 *p = addr;
+ return le32_to_cpu(*p) | ((__u64)le32_to_cpu(*(p + 1)) << 32);
+}
+
+static inline __u32 read32(void *addr)
+{
+ __le32 *p = addr;
+ return le32_to_cpu(*p);
+}
+
+static inline void nvme_json_add_str_len(struct json_object *j, const char *n,
+ const char *v, int l, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_str_len_flags(v, l, flags));
+}
+
+static inline void nvme_json_add_str(struct json_object *j, const char *n,
+ const char *v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_str(v, flags));
+}
+
+static inline void nvme_json_add_int128(struct json_object *j, const char *n,
+ uint8_t *v)
+{
+ json_object_object_add(j, n, nvme_json_new_int128(v));
+}
+
+static inline void nvme_json_add_int64(struct json_object *j, const char *n,
+ uint64_t v)
+{
+ json_object_object_add(j, n, nvme_json_new_int64(v));
+}
+
+static inline void nvme_json_add_int(struct json_object *j, const char *n,
+ uint32_t v)
+{
+ json_object_object_add(j, n, nvme_json_new_int(v));
+}
+
+static inline void nvme_json_add_bool(struct json_object *j, const char *n,
+ bool v)
+{
+ json_object_object_add(j, n, nvme_json_new_bool(v));
+}
+
+static inline void nvme_json_add_flag(struct json_object *j, const char *n,
+ uint64_t v, int f)
+{
+ nvme_json_add_bool(j, n, is_set(v, f));
+}
+
+static inline void nvme_json_add_le64_ptr(struct json_object *j, const char *n,
+ void *addr)
+{
+ __u64 v = read64(addr);
+ nvme_json_add_int64(j, n, v);
+}
+
+static inline void nvme_json_add_le32_ptr(struct json_object *j, const char *n,
+ void *addr)
+{
+ __u32 v = read32(addr);
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_add_storage_128(struct json_object *j, const char *n,
+ uint8_t *v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_storage_128(v, flags));
+}
+
+static inline void nvme_json_add_storage(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_storage(v, flags));
+}
+
+static inline void nvme_json_add_size(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_size(v, flags));
+}
+
+static inline void nvme_json_add_memory(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_memory(v, flags));
+}
+
+static inline void nvme_json_add_hex_array(struct json_object *j, const char *n,
+ uint8_t *v, uint32_t l)
+{
+ json_object_object_add(j, n, nvme_json_new_hex_array(v, l));
+}
+
+static inline void nvme_json_add_0x(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_0x(v, flags));
+}
+
+static inline void nvme_json_add_hex(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_hex(v, flags));
+}
+
+static inline void nvme_json_add_percent(struct json_object *j, const char *n,
+ uint8_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_percent(v, flags));
+}
+
+static inline void nvme_json_add_temp(struct json_object *j, const char *n,
+ uint16_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_temp(v, flags));
+}
+
+static inline void nvme_json_add_time_us(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_time_us(v, flags));
+}
+
+static inline void nvme_json_add_time_100us(struct json_object *j, const char *n,
+ uint64_t v)
+{
+ nvme_json_add_time_us(j, n, 100 * v, 0);
+}
+
+static inline void nvme_json_add_time_s(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_time_s(v, flags));
+}
+
+static inline void nvme_json_add_power(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_hecto_uwatts(v, flags));
+}
+
+static inline void nvme_json_add_uuid(struct json_object *j, const char *n,
+ uint8_t *v, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_uuid(v, flags));
+}
+
+static inline void nvme_json_add_oui(struct json_object *j, const char *n,
+ uint8_t *v, int l, unsigned long flags)
+{
+ json_object_object_add(j, n, nvme_json_new_oui(v, l, flags));
+}
+
+static inline void nvme_json_add_le64(struct json_object *j,
+ const char *n, __le64 v)
+{
+ nvme_json_add_int64(j, n, le64_to_cpu(v));
+}
+
+static inline void nvme_json_add_le32(struct json_object *j,
+ const char *n, __le32 v)
+{
+ nvme_json_add_int(j, n, le32_to_cpu(v));
+}
+
+static inline void nvme_json_add_le16(struct json_object *j,
+ const char *n, __le16 v)
+{
+ nvme_json_add_int(j, n, le16_to_cpu(v));
+}
+
+static inline void nvme_json_add_hex_le64(struct json_object *j,
+ const char *n, __le32 v, unsigned long flags)
+{
+ nvme_json_add_hex(j, n, le64_to_cpu(v), flags);
+}
+
+static inline void nvme_json_add_hex_le32(struct json_object *j,
+ const char *n, __le32 v, unsigned long flags)
+{
+ nvme_json_add_hex(j, n, le32_to_cpu(v), flags);
+}
+
+static inline void nvme_json_add_hex_le16(struct json_object *j,
+ const char *n, __le16 v, unsigned long flags)
+{
+ nvme_json_add_hex(j, n, le16_to_cpu(v), flags);
+}
+
+static void chomp(char *s, int l)
+{
+ while (l && (s[l] == '\0' || s[l] == ' '))
+ s[l--] = '\0';
+}
+
+static inline void nvme_json_add_str_flags(struct json_object *j, const char *n,
+ const char *v, int l, unsigned long flags)
+{
+ char buf[l + 1];
+
+ if (!(flags & NVME_JSON_HUMAN)) {
+ nvme_json_add_str_len(j, n, v, l, flags);
+ return;
+ }
+
+ snprintf(buf, sizeof(buf), "%-.*s", l, v);
+ chomp(buf, l);
+
+ nvme_json_add_str(j, n, buf, flags);
+}
+
+static inline void nvme_json_add_storage_128_flags(struct json_object *j, const char *n,
+ uint8_t *v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_storage_128(j, n, v, flags);
+ else
+ nvme_json_add_int128(j, n, v);
+}
+
+static inline void nvme_json_add_storage_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_storage(j, n, v, flags);
+ else
+ nvme_json_add_int64(j, n, v);
+}
+
+static inline void nvme_json_add_size_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_size(j, n, v, flags);
+ else
+ nvme_json_add_int64(j, n, v);
+}
+
+static inline void nvme_json_add_hex_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_hex(j, n, v, flags);
+ else
+ nvme_json_add_int64(j, n, v);
+}
+
+static inline void nvme_json_add_time_us_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_time_us(j, n, v, flags);
+ else
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_add_time_100us_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_time_us(j, n, 100 * v, flags);
+ else
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_add_time_m_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_time_s(j, n, 60 * v, flags);
+ else
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_add_time_s_flags(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_time_s(j, n, v, flags);
+ else
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_add_hex_le16_flags(struct json_object *j,
+ const char *n, __le16 v,
+ unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_hex_le16(j, n, v, flags);
+ else
+ nvme_json_add_le16(j, n, v);
+}
+
+static inline void nvme_json_add_hex_le32_flags(struct json_object *j,
+ const char *n, __le16 v,
+ unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_hex_le32(j, n, v, flags);
+ else
+ nvme_json_add_le32(j, n, v);
+}
+
+static inline void nvme_json_add_hex_le64_flags(struct json_object *j,
+ const char *n, __le16 v,
+ unsigned long flags)
+{
+ if (flags & NVME_JSON_HUMAN)
+ nvme_json_add_hex_le64(j, n, v, flags);
+ else
+ nvme_json_add_le64(j, n, v);
+}
+
+static inline int nvme_ieee_to_int(__u8 ieee[])
+{
+ /* nvme defines the ieee byte order order this way */
+ return ieee[2] << 16 | ieee[1] << 8 | ieee[0];
+}
+
+static inline void nvme_json_add_flag_flags(struct json_object *j, const char *n,
+ uint64_t v, int f, unsigned long flags)
+{
+ if (flags & NVME_JSON_TABULAR && flags & NVME_JSON_COMPACT)
+ json_object_object_add(j, n, nvme_json_new_bool_terse(is_set(v, f)));
+ else
+ nvme_json_add_bool(j, n, is_set(v, f));
+}
+
+static inline void nvme_json_add_object(struct json_object *j, const char *n,
+ struct json_object *o)
+{
+ json_object_object_add(j, n, o);
+}
+
+static inline void nvme_json_add_not_zero(struct json_object *j, const char *n,
+ uint64_t v, unsigned long flags)
+{
+ if (v || !(flags & NVME_JSON_HIDE_UNSUPPORTED))
+ nvme_json_add_int(j, n, v);
+}
+
+static inline void nvme_json_object_print(FILE *f, struct json_object *j,
+ unsigned long jflags)
+{
+ if (j)
+ fprintf(f, "%s", json_object_to_json_string_ext(j, jflags));
+}
+
+const char *nvme_status_to_string(int status, bool fabrics);
+const char *nvme_get_feature_select_to_string(__u8 sel);
+void d(unsigned char *buf, int len, int width, int group);
+void d_raw(unsigned char *buf, unsigned len);
+
+#endif /* _JSON_UTIL_H */