2025-02-16 12:16:19 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2025-02-16 11:09:01 +01:00
|
|
|
/*
|
|
|
|
* Definitions for the NVM Express interface
|
|
|
|
* Copyright (c) 2011-2014, Intel Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NVME_H
|
|
|
|
#define _NVME_H
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2025-02-16 12:18:36 +01:00
|
|
|
#include <stdio.h>
|
2025-02-16 11:09:01 +01:00
|
|
|
#include <endian.h>
|
2025-02-16 11:31:10 +01:00
|
|
|
#include <sys/time.h>
|
2025-02-16 12:18:36 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <libnvme-mi.h>
|
2025-02-16 11:09:01 +01:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "util/json.h"
|
2025-02-16 12:25:41 +01:00
|
|
|
#include "util/mem.h"
|
2025-02-16 11:09:01 +01:00
|
|
|
#include "util/argconfig.h"
|
2025-02-16 12:24:54 +01:00
|
|
|
#include "util/cleanup.h"
|
2025-02-16 11:09:01 +01:00
|
|
|
|
|
|
|
enum nvme_print_flags {
|
2025-02-16 12:27:38 +01:00
|
|
|
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 */
|
2025-02-16 11:09:01 +01:00
|
|
|
};
|
|
|
|
|
2025-02-16 12:27:38 +01:00
|
|
|
typedef uint32_t nvme_print_flags_t;
|
|
|
|
|
2025-02-16 12:18:36 +01:00
|
|
|
enum nvme_cli_topo_ranking {
|
|
|
|
NVME_CLI_TOPO_NAMESPACE,
|
|
|
|
NVME_CLI_TOPO_CTRL,
|
|
|
|
};
|
|
|
|
|
2025-02-16 11:09:01 +01:00
|
|
|
#define SYS_NVME "/sys/class/nvme"
|
|
|
|
|
2025-02-16 12:18:36 +01:00
|
|
|
enum nvme_dev_type {
|
|
|
|
NVME_DEV_DIRECT,
|
|
|
|
NVME_DEV_MI,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nvme_dev {
|
|
|
|
enum nvme_dev_type type;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int fd;
|
|
|
|
struct stat stat;
|
|
|
|
} direct;
|
|
|
|
struct {
|
|
|
|
nvme_root_t root;
|
|
|
|
nvme_mi_ep_t ep;
|
|
|
|
nvme_mi_ctrl_t ctrl;
|
|
|
|
} mi;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define dev_fd(d) __dev_fd(d, __func__, __LINE__)
|
|
|
|
|
2025-02-16 12:27:38 +01:00
|
|
|
struct nvme_config {
|
|
|
|
char *output_format;
|
|
|
|
int verbose;
|
|
|
|
__u32 timeout;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the ordering of the arguments matters, as the argument parser uses the first match, thus any
|
|
|
|
* command which defines -t shorthand will match first.
|
|
|
|
*/
|
|
|
|
#define NVME_ARGS(n, ...) \
|
|
|
|
struct argconfig_commandline_options n[] = { \
|
|
|
|
OPT_INCR("verbose", 'v', &nvme_cfg.verbose, verbose), \
|
|
|
|
OPT_FMT("output-format", 'o', &nvme_cfg.output_format, output_format), \
|
|
|
|
##__VA_ARGS__, \
|
|
|
|
OPT_UINT("timeout", 't', &nvme_cfg.timeout, timeout), \
|
|
|
|
OPT_END() \
|
|
|
|
}
|
|
|
|
|
2025-02-16 12:18:36 +01:00
|
|
|
static inline int __dev_fd(struct nvme_dev *dev, const char *func, int line)
|
|
|
|
{
|
|
|
|
if (dev->type != NVME_DEV_DIRECT) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"warning: %s:%d not a direct transport!\n",
|
|
|
|
func, line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return dev->direct.fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline nvme_mi_ep_t dev_mi_ep(struct nvme_dev *dev)
|
|
|
|
{
|
|
|
|
if (dev->type != NVME_DEV_MI) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"warning: not a MI transport!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return dev->mi.ep;
|
|
|
|
}
|
|
|
|
|
2025-02-16 11:09:01 +01:00
|
|
|
void register_extension(struct plugin *plugin);
|
2025-02-16 12:18:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parse_and_open - parses arguments and opens the NVMe device, populating @dev
|
|
|
|
*/
|
|
|
|
int parse_and_open(struct nvme_dev **dev, int argc, char **argv, const char *desc,
|
2025-02-16 12:23:16 +01:00
|
|
|
struct argconfig_commandline_options *clo);
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:18:36 +01:00
|
|
|
void dev_close(struct nvme_dev *dev);
|
|
|
|
|
2025-02-16 12:25:41 +01:00
|
|
|
static inline DEFINE_CLEANUP_FUNC(
|
|
|
|
cleanup_nvme_dev, struct nvme_dev *, dev_close)
|
2025-02-16 12:24:54 +01:00
|
|
|
#define _cleanup_nvme_dev_ __cleanup__(cleanup_nvme_dev)
|
|
|
|
|
2025-02-16 11:31:10 +01:00
|
|
|
extern const char *output_format;
|
2025-02-16 12:27:38 +01:00
|
|
|
extern const char *timeout;
|
|
|
|
extern const char *verbose;
|
|
|
|
extern struct nvme_config nvme_cfg;
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:27:38 +01:00
|
|
|
int validate_output_format(const char *format, nvme_print_flags_t *flags);
|
2025-02-16 12:24:54 +01:00
|
|
|
bool nvme_is_output_format_json(void);
|
2025-02-16 11:09:01 +01:00
|
|
|
int __id_ctrl(int argc, char **argv, struct command *cmd,
|
2025-02-16 12:15:45 +01:00
|
|
|
struct plugin *plugin, void (*vs)(uint8_t *vs, struct json_object *root));
|
2025-02-16 11:09:01 +01:00
|
|
|
|
2025-02-16 12:15:45 +01:00
|
|
|
const char *nvme_strerror(int errnum);
|
2025-02-16 11:31:10 +01:00
|
|
|
|
|
|
|
unsigned long long elapsed_utime(struct timeval start_time,
|
|
|
|
struct timeval end_time);
|
2025-02-16 12:11:43 +01:00
|
|
|
|
2025-02-16 12:15:45 +01:00
|
|
|
/* nvme-print.c */
|
|
|
|
const char *nvme_select_to_string(int sel);
|
|
|
|
|
|
|
|
void d(unsigned char *buf, int len, int width, int group);
|
|
|
|
void d_raw(unsigned char *buf, unsigned len);
|
|
|
|
uint64_t int48_to_long(uint8_t *data);
|
|
|
|
|
2025-02-16 12:26:52 +01:00
|
|
|
int get_reg_size(int offset);
|
|
|
|
bool nvme_is_ctrl_reg(int offset);
|
2025-02-16 11:09:01 +01:00
|
|
|
#endif /* _NVME_H */
|