Skip to content

Commit 9ea737e

Browse files
author
Kelvin Cao
committed
Merge branch 'devel'
2 parents 86c3933 + 75e4d16 commit 9ea737e

17 files changed

+1178
-199
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ a.out
77
NVME-VERSION-FILE
88

99
cscope.*
10+
tags
1011

1112
/debian/files
1213
/debian/nvme-cli.debhelper.log

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CFLAGS ?= -O2 -g -Wall -Werror
22
CFLAGS += -std=gnu99
33
CPPFLAGS += -D_GNU_SOURCE -D__CHECK_ENDIAN__
44
LIBUUID = $(shell $(LD) -o /dev/null -luuid >/dev/null 2>&1; echo $$?)
5-
NVME = nvme
5+
NVME = switchtec-nvme
66
INSTALL ?= install
77
DESTDIR =
88
PREFIX ?= /usr/local
@@ -34,15 +34,16 @@ NVME_DPKG_VERSION=1~`lsb_release -sc`
3434
OBJS := argconfig.o suffix.o parser.o nvme-print.o nvme-ioctl.o \
3535
nvme-lightnvm.o fabrics.o json.o plugin.o intel-nvme.o \
3636
lnvm-nvme.o memblaze-nvme.o wdc-nvme.o wdc-utils.o nvme-models.o \
37-
huawei-nvme.o netapp-nvme.o toshiba-nvme.o
37+
huawei-nvme.o netapp-nvme.o toshiba-nvme.o switchtec-nvme-device.o \
38+
rc-nvme-device.o switchtec-nvme.o
3839

39-
nvme: nvme.c nvme.h $(OBJS) NVME-VERSION-FILE
40-
$(CC) $(CPPFLAGS) $(CFLAGS) nvme.c -o $(NVME) $(OBJS) $(LDFLAGS)
40+
$(NVME): nvme.c nvme.h $(OBJS) NVME-VERSION-FILE
41+
$(CC) $(CPPFLAGS) $(CFLAGS) nvme.c -o $(NVME) $(OBJS) $(LDFLAGS) -lswitchtec
4142

42-
nvme.o: nvme.c nvme.h nvme-print.h nvme-ioctl.h argconfig.h suffix.h nvme-lightnvm.h fabrics.h
43+
nvme.o: nvme.c nvme.h nvme-device.h rc-nvme-device.h pax-nvme-device.h nvme-print.h nvme-ioctl.h argconfig.h suffix.h nvme-lightnvm.h fabrics.h
4344
$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
4445

45-
%.o: %.c %.h nvme.h linux/nvme_ioctl.h nvme-ioctl.h nvme-print.h argconfig.h
46+
%.o: %.c %.h nvme.h linux/nvme_ioctl.h nvme-ioctl.h nvme-print.h argconfig.h nvme-device.h
4647
$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
4748

4849
doc: $(NVME)

README.md

Lines changed: 190 additions & 158 deletions
Large diffs are not rendered by default.

README_nvme_cli.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# nvme-cli
2+
NVM-Express user space tooling for Linux.
3+
4+
To install, run:
5+
6+
# make && make install
7+
8+
If not sure how to use, find the top-level documentation with:
9+
10+
# man nvme
11+
12+
Or find a short summary with:
13+
14+
# nvme help
15+
16+
## Distro Support
17+
18+
### Fedora
19+
20+
nvme-cli is available in Fedora 23 and up. Install it with your favorite
21+
package manager. For example:
22+
23+
$ sudo dnf install nvme-cli
24+
25+
### Ubuntu
26+
27+
nvme-cli is supported in the Universe package sources for Xenial for
28+
many architectures. For a complete list try running:
29+
```
30+
rmadison nvme-cli
31+
nvme-cli | 0.3-1 | xenial/universe | source, amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
32+
```
33+
A Debian based package for nvme-cli is currently maintained as a
34+
Ubuntu PPA. Right now there is support for Trusty, Vivid and Wiley. To
35+
install nvme-cli using this approach please perform the following
36+
steps:
37+
1. Add the sbates PPA to your sources. One way to do this is to run
38+
```
39+
sudo add-apt-repository ppa:sbates
40+
```
41+
2. Perform an update of your repository list:
42+
```
43+
sudo apt-get update
44+
```
45+
3. Get nvme-cli!
46+
```
47+
sudo apt-get install nvme-cli
48+
```
49+
4. Test the code.
50+
```
51+
sudo nvme list
52+
```
53+
In the case of no NVMe devices you will see
54+
```
55+
No NVMe devices detected.
56+
```
57+
otherwise you will see information about each NVMe device installed
58+
in the system.
59+
60+
### Alpine Linux
61+
62+
nvme-cli is tested on Alpine Linux 3.3. Install it using:
63+
64+
# akp update && apk add nvme-cli nvme-cli-doc
65+
66+
if you just use the device you're after, it will work flawless.
67+
```
68+
# nvme smart-log /dev/nvme0
69+
Smart Log for NVME device:/dev/nvme0 namespace-id:ffffffff
70+
critical_warning : 0
71+
temperature : 49 C
72+
available_spare : 100%
73+
```
74+
75+
### openSUSE Tumbleweed
76+
77+
nvme-cli is available in openSUSE Tumbleweed. You can install it using zypper.
78+
For example:
79+
80+
$ sudo zypper install nvme-cli
81+
82+
### Arch Linux
83+
84+
Install from AUR, e.g.:
85+
```
86+
$ yaourt -S nvme-cli-git
87+
```
88+
89+
### Nix(OS)
90+
91+
The attribute is named `nvme-cli` and can e.g. be installed with:
92+
```
93+
$ nix-env -f '<nixpkgs>' -iA nvme-cli
94+
```
95+
96+
### Other Distros
97+
98+
TBD
99+
100+
## Developers
101+
102+
You may wish to add a new command or possibly an entirely new plug-in
103+
for some special extension outside the spec.
104+
105+
This project provides macros that help generate the code for you. If
106+
you're interested in how that works, it is very similar to how trace
107+
events are created by Linux kernel's 'ftrace' component.
108+
109+
### Add command to existing built-in
110+
111+
The first thing to do is define a new command entry in the command
112+
list. This is declared in nvme-builtin.h. Simply append a new "ENTRY" into
113+
the list. The ENTRY normally takes three arguments: the "name" of the
114+
subcommand (this is what the user will type at the command line to invoke
115+
your command), a short help description of what your command does, and the
116+
name of the function callback that you're going to write. Additionally,
117+
You can declare an alias name of subcommand with fourth argument, if needed.
118+
119+
After the ENTRY is defined, you need to implement the callback. It takes
120+
four arguments: argc, argv, the command structure associated with the
121+
callback, and the plug-in structure that contains that command. The
122+
prototype looks like this:
123+
124+
```
125+
int f(int argc, char **argv, struct command *cmd, struct plugin *plugin);
126+
```
127+
128+
The argc and argv are adjusted from the command line arguments to start
129+
after the sub-command. So if the command line is "nvme foo --option=bar",
130+
the argc is 1 and argv starts at "--option".
131+
132+
You can then define argument parsing for your sub-command's specific
133+
options then do some command specific action in your callback.
134+
135+
### Add a new plugin
136+
137+
The nvme-cli provides macros to make define a new plug-in simpler. You
138+
can certainly do all this by hand if you want, but it should be easier
139+
to get going using the macros. To start, first create a header file
140+
to define your plugin. This is where you will give your plugin a name,
141+
description, and define all the sub-commands your plugin implements.
142+
143+
There is a very important order on how to define the plugin. The following
144+
is a basic example on how to start this:
145+
146+
File: foo-plugin.h
147+
```
148+
#undef CMD_INC_FILE
149+
#define CMD_INC_FILE foo-plugin
150+
151+
#if !defined(FOO) || defined(CMD_HEADER_MULTI_READ)
152+
#define FOO
153+
154+
#include "cmd.h"
155+
156+
PLUGIN(NAME("foo", "Foo plugin"),
157+
COMMAND_LIST(
158+
ENTRY("bar", "foo bar", bar)
159+
ENTRY("baz", "foo baz", baz)
160+
ENTRY("qux", "foo quz", qux)
161+
)
162+
);
163+
164+
#endif
165+
166+
#include "define_cmd.h"
167+
```
168+
169+
In order to have the compiler generate the plugin through the xmacro
170+
expansion, you need to include this header in your source file, with
171+
pre-defining macro directive to create the commands.
172+
173+
To get started from the above example, we just need to define "CREATE_CMD"
174+
and include the header:
175+
176+
File: foo-plugin.c
177+
```
178+
#define CREATE_CMD
179+
#include "foo-plugin.h"
180+
```
181+
182+
After that, you just need to implement the functions you defined in each
183+
ENTRY, then append the object file name to the Makefile's "OBJS".

fabrics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ int discover(const char *desc, int argc, char **argv, bool connect)
841841
}
842842
}
843843

844-
int connect(const char *desc, int argc, char **argv)
844+
int _connect(const char *desc, int argc, char **argv)
845845
{
846846
char argstr[BUF_SIZE];
847847
int instance, ret;

fabrics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _DISCOVER_H
33

44
extern int discover(const char *desc, int argc, char **argv, bool connect);
5-
extern int connect(const char *desc, int argc, char **argv);
5+
extern int _connect(const char *desc, int argc, char **argv);
66
extern int disconnect(const char *desc, int argc, char **argv);
77

88
#endif

nvme-device.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef _NVME_DEVICE_H
2+
#define _NVME_DEVICE_H
3+
4+
#include "nvme-ioctl.h"
5+
6+
struct nvme_device_ops {
7+
int (*nvme_submit_admin_passthru)(int fd, struct nvme_passthru_cmd *cmd);
8+
int (*nvme_get_nsid)(int fd);
9+
int (*nvme_io)(int fd, struct nvme_user_io *io);
10+
int (*nvme_subsystem_reset)(int fd);
11+
int (*nvme_reset_controller)(int fd);
12+
int (*nvme_ns_rescan)(int fd);
13+
int (*nvme_submit_passthru)(int fd, int ioctl_cmd, struct nvme_passthru_cmd *cmd);
14+
int (*nvme_submit_io_passthru)(int fd, struct nvme_passthru_cmd *cmd);
15+
int (*is_blk)(void);
16+
};
17+
18+
#define NVME_DEVICE_TYPE_RC 0
19+
#define NVME_DEVICE_TYPE_PAX 1
20+
21+
struct nvme_device {
22+
int type;
23+
struct nvme_device_ops *ops;
24+
};
25+
26+
extern struct nvme_device *global_device;
27+
28+
#endif

nvme-ioctl.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#include <linux/types.h>
2121

2222
#include "nvme-ioctl.h"
23+
#include "nvme-device.h"
24+
25+
int is_blk(void)
26+
{
27+
return global_device->ops->is_blk();
28+
}
2329

2430
static int nvme_verify_chr(int fd)
2531
{
@@ -45,7 +51,7 @@ int nvme_subsystem_reset(int fd)
4551
ret = nvme_verify_chr(fd);
4652
if (ret)
4753
return ret;
48-
return ioctl(fd, NVME_IOCTL_SUBSYS_RESET);
54+
return global_device->ops->nvme_subsystem_reset(fd);
4955
}
5056

5157
int nvme_reset_controller(int fd)
@@ -55,7 +61,7 @@ int nvme_reset_controller(int fd)
5561
ret = nvme_verify_chr(fd);
5662
if (ret)
5763
return ret;
58-
return ioctl(fd, NVME_IOCTL_RESET);
64+
return global_device->ops->nvme_reset_controller(fd);
5965
}
6066

6167
int nvme_ns_rescan(int fd)
@@ -65,7 +71,7 @@ int nvme_ns_rescan(int fd)
6571
ret = nvme_verify_chr(fd);
6672
if (ret)
6773
return ret;
68-
return ioctl(fd, NVME_IOCTL_RESCAN);
74+
return global_device->ops->nvme_ns_rescan(fd);
6975
}
7076

7177
int nvme_get_nsid(int fd)
@@ -76,27 +82,27 @@ int nvme_get_nsid(int fd)
7682
if (err < 0)
7783
return err;
7884

79-
if (!S_ISBLK(nvme_stat.st_mode)) {
85+
if (!global_device->ops->is_blk()) {
8086
fprintf(stderr,
8187
"Error: requesting namespace-id from non-block device\n");
8288
return -ENOTBLK;
8389
}
84-
return ioctl(fd, NVME_IOCTL_ID);
90+
return global_device->ops->nvme_get_nsid(fd);
8591
}
8692

8793
int nvme_submit_passthru(int fd, int ioctl_cmd, struct nvme_passthru_cmd *cmd)
8894
{
89-
return ioctl(fd, ioctl_cmd, cmd);
95+
return global_device->ops->nvme_submit_passthru(fd, ioctl_cmd, cmd);
9096
}
9197

92-
static int nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
98+
int nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd)
9399
{
94-
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, cmd);
100+
return global_device->ops->nvme_submit_admin_passthru(fd, cmd);
95101
}
96102

97103
static int nvme_submit_io_passthru(int fd, struct nvme_passthru_cmd *cmd)
98104
{
99-
return ioctl(fd, NVME_IOCTL_IO_CMD, cmd);
105+
return global_device->ops->nvme_submit_io_passthru(fd, cmd);
100106
}
101107

102108
int nvme_passthru(int fd, int ioctl_cmd, __u8 opcode, __u8 flags, __u16 rsvd,
@@ -151,7 +157,7 @@ int nvme_io(int fd, __u8 opcode, __u64 slba, __u16 nblocks, __u16 control,
151157
.appmask = appmask,
152158
.apptag = apptag,
153159
};
154-
return ioctl(fd, NVME_IOCTL_SUBMIT_IO, &io);
160+
return global_device->ops->nvme_io(fd, &io);
155161
}
156162

157163
int nvme_read(int fd, __u64 slba, __u16 nblocks, __u16 control, __u32 dsmgmt,

nvme-ioctl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,6 @@ int nvme_set_property(int fd, int offset, int value);
135135
int nvme_sanitize(int fd, __u8 sanact, __u8 ause, __u8 owpass, __u8 oipbp,
136136
__u8 no_dealloc, __u32 ovrpat);
137137

138+
int is_blk(void);
139+
138140
#endif /* _NVME_LIB_H */

0 commit comments

Comments
 (0)