-
Notifications
You must be signed in to change notification settings - Fork 10
/
nvme_linux.go
180 lines (152 loc) · 4.24 KB
/
nvme_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package smart
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
// https:// nvmexpress.org/wp-content/uploads/NVM-Express-Base-Specification-2.0b-2021.12.18-Ratified.pdf
// https:// nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0b-2021.12.18-Ratified.pdf
// include/uapi/linux/nvme_ioctl.h
var nvmeIoctlAdmin64Cmd = iowr('N', 0x47, unsafe.Sizeof(nvmePassthruCmd64{}))
type nvmePassthruCmd64 struct {
opcode uint8
flags uint8
_ uint16
nsid uint32
cdw2 uint32
cdw3 uint32
metadata uint64
addr uint64
metadataLen uint32
dataLen uint32
cdw10 uint32
cdw11 uint32
cdw12 uint32
cdw13 uint32
cdw14 uint32
cdw15 uint32
timeoutMs uint32
_ uint32
result uint64
}
var nvmeIoctlAdminCmd = iowr('N', 0x41, unsafe.Sizeof(nvmePassthruCmd{}))
type nvmePassthruCmd struct {
opcode uint8
flags uint8
_ uint16
nsid uint32
cdw2 uint32
cdw3 uint32
metadata uint64
addr uint64
metadataLen uint32
dataLen uint32
cdw10 uint32
cdw11 uint32
cdw12 uint32
cdw13 uint32
cdw14 uint32
cdw15 uint32
timeoutMs uint32
result uint32
}
type NVMeDevice struct {
fd int
}
func OpenNVMe(name string) (*NVMeDevice, error) {
fd, err := unix.Open(name, unix.O_RDONLY, 0o600)
if err != nil {
return nil, err
}
dev := NVMeDevice{
fd: fd,
}
return &dev, nil
}
func (d *NVMeDevice) Close() error {
return unix.Close(d.fd)
}
func (d *NVMeDevice) ReadSMART() (*NvmeSMARTLog, error) {
buf := make([]byte, 512)
if err := nvmeReadLogPage(d.fd, nvmeLogSmartInformation, buf); err != nil {
return nil, err
}
var sl NvmeSMARTLog
if err := binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &sl); err != nil {
return nil, err
}
return &sl, nil
}
func nvmeReadLogPage(fd int, logID uint8, buf []byte) error {
bufLen := len(buf)
if (bufLen < 4) || (bufLen > 0x4000) || (bufLen%4 != 0) {
return fmt.Errorf("invalid buffer size")
}
cmd64 := nvmePassthruCmd64{
opcode: nvmeAdminGetLogPage,
nsid: 0xffffffff, // controller-level SMART info
addr: uint64(uintptr(unsafe.Pointer(&buf[0]))),
dataLen: uint32(bufLen),
cdw10: uint32(logID) | (((uint32(bufLen) / 4) - 1) << 16),
}
err := ioctl(uintptr(fd), nvmeIoctlAdmin64Cmd, uintptr(unsafe.Pointer(&cmd64)))
if err != syscall.ENOTTY {
return err
}
// fallback to legacy 32bit struct
cmd := nvmePassthruCmd{
opcode: nvmeAdminGetLogPage,
nsid: 0xffffffff, // controller-level SMART info
addr: uint64(uintptr(unsafe.Pointer(&buf[0]))),
dataLen: uint32(bufLen),
cdw10: uint32(logID) | (((uint32(bufLen) / 4) - 1) << 16),
}
return ioctl(uintptr(fd), nvmeIoctlAdminCmd, uintptr(unsafe.Pointer(&cmd)))
}
func (d *NVMeDevice) readIdentifyData(nsid, cns int, data []byte) error {
cmd64 := nvmePassthruCmd64{
opcode: nvmeAdminIdentify,
nsid: uint32(nsid),
addr: uint64(uintptr(unsafe.Pointer(&data[0]))),
dataLen: uint32(len(data)),
cdw10: uint32(cns),
}
err := ioctl(uintptr(d.fd), nvmeIoctlAdmin64Cmd, uintptr(unsafe.Pointer(&cmd64)))
if err != syscall.ENOTTY {
return err
}
// fallback to legacy 32bit struct
cmd := nvmePassthruCmd{
opcode: nvmeAdminIdentify,
nsid: uint32(nsid),
addr: uint64(uintptr(unsafe.Pointer(&data[0]))),
dataLen: uint32(len(data)),
cdw10: uint32(cns),
}
return ioctl(uintptr(d.fd), nvmeIoctlAdminCmd, uintptr(unsafe.Pointer(&cmd)))
}
func (d *NVMeDevice) readControllerIdentifyData() (*NvmeIdentController, error) {
buf := make([]byte, 4096)
if err := d.readIdentifyData(0, 1, buf); err != nil {
return nil, err
}
var controller NvmeIdentController
if err := binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &controller); err != nil {
return nil, err
}
return &controller, nil
}
func (d *NVMeDevice) readNamespaceIdentifyData(nsid int) (*NvmeIdentNamespace, error) {
buf := make([]byte, 4096)
if err := d.readIdentifyData(nsid, 0, buf); err != nil {
return nil, err
}
var namespace NvmeIdentNamespace
if err := binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &namespace); err != nil {
return nil, err
}
return &namespace, nil
}