-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnvme_util.cpp
259 lines (224 loc) · 6.92 KB
/
nvme_util.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "nvme_util.h"
#include <fcntl.h>
#include "linux_nvme_ioctl.h"
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#define O_RDONLY 00
#ifndef O_NONBLOCK
# define O_NONBLOCK 04000
#endif
nvme_Device::nvme_Device()
{
}
nvme_Device::nvme_Device(const char * dev_name, const char * req_type, unsigned nsid)
{
nvmeopts.drive_info = true;
nvmeopts.smart_check_status = true;
m_nsid = nsid;
m_info.dev_name = dev_name;
m_info.info_name = dev_name;
m_info.dev_type = "nvme";
m_info.req_type = req_type;
m_fd = -1;
m_flags = O_RDONLY | O_NONBLOCK;
m_retry_flags = -1;
}
int nvme_Device::myOpen()
{
m_fd = ::open(m_info.dev_name.c_str(), m_flags);
if (m_fd < 0 && errno == EROFS && m_retry_flags != -1)
// Retry
m_fd = ::open(m_info.dev_name.c_str(), m_retry_flags);
if (m_fd < 0) {
//QMessageBox::information(NULL, "Error", "open fail", NULL);
// if (errno == EBUSY && (m_flags & O_EXCL))
// // device is locked
// return set_err(EBUSY,
// "The requested controller is used exclusively by another process!\n"
// "(e.g. smartctl or smartd)\n"
// "Please quit the impeding process or try again later...");
// return set_err((errno==ENOENT || errno==ENOTDIR) ? ENODEV : errno);
return m_fd;
}
if (m_fd >= 0) {
// sets FD_CLOEXEC on the opened device file descriptor. The
// descriptor is otherwise leaked to other applications (mail
// sender) which may be considered a security risk and may result
// in AVC messages on SELinux-enabled systems.
if (-1 == fcntl(m_fd, F_SETFD, FD_CLOEXEC))
{
// TODO: Provide an error printing routine in class smart_interface
// QMessageBox::information(NULL, "Error", "fcntl(set FD_CLOEXEC) failed", NULL);
//qDebug()<<"fcntl(set FD_CLOEXEC) failed";
//pout("fcntl(set FD_CLOEXEC) failed, errno=%d [%s]\n", errno, strerror(errno));
}
}
if (!m_nsid) {
// Use actual NSID (/dev/nvmeXnN) if available,
// else use broadcast namespace (/dev/nvmeX)
int nsid = ioctl(m_fd, NVME_IOCTL_ID, (void*)0);
m_nsid = nsid;
}
return m_fd;
}
void nvme_Device::swap2(char *location){
char tmp=*location;
*location=*(location+1);
*(location+1)=tmp;
return;
}
// swap four bytes. Point to low address
void nvme_Device::swap4(char *location){
char tmp=*location;
*location=*(location+3);
*(location+3)=tmp;
swap2(location+1);
return;
}
// swap eight bytes. Points to low address
void nvme_Device::swap8(char *location){
char tmp=*location;
*location=*(location+7);
*(location+7)=tmp;
tmp=*(location+1);
*(location+1)=*(location+6);
*(location+6)=tmp;
swap4(location+2);
return;
}
bool nvme_Device::nvme_read_id_ctrl(nvme_id_ctrl & id_ctrl)
{
if (!nvme_read_identify(0, 0x01, &id_ctrl, sizeof(id_ctrl)))
return false;
if (isbigendian()) {
swapx(&id_ctrl.vid);
swapx(&id_ctrl.ssvid);
swapx(&id_ctrl.cntlid);
swapx(&id_ctrl.oacs);
swapx(&id_ctrl.wctemp);
swapx(&id_ctrl.cctemp);
swapx(&id_ctrl.mtfa);
swapx(&id_ctrl.hmpre);
swapx(&id_ctrl.hmmin);
swapx(&id_ctrl.rpmbs);
swapx(&id_ctrl.nn);
swapx(&id_ctrl.oncs);
swapx(&id_ctrl.fuses);
swapx(&id_ctrl.awun);
swapx(&id_ctrl.awupf);
swapx(&id_ctrl.acwu);
swapx(&id_ctrl.sgls);
for (int i = 0; i < 32; i++) {
swapx(&id_ctrl.psd[i].max_power);
swapx(&id_ctrl.psd[i].entry_lat);
swapx(&id_ctrl.psd[i].exit_lat);
swapx(&id_ctrl.psd[i].idle_power);
swapx(&id_ctrl.psd[i].active_power);
}
}
return true;
}
bool nvme_Device::nvme_read_smart_log(nvme_smart_log & smart_log)
{
if (!nvme_read_log_page(0x02, &smart_log, sizeof(smart_log)))
return false;
if (isbigendian()) {
swapx(&smart_log.warning_temp_time);
swapx(&smart_log.critical_comp_time);
for (int i = 0; i < 8; i++)
swapx(&smart_log.temp_sensor[i]);
}
return true;
}
bool nvme_Device::nvme_read_id_ns(unsigned nsid, nvme_id_ns & id_ns)
{
if (!nvme_read_identify(nsid, 0x00, &id_ns, sizeof(id_ns)))
return false;
if (isbigendian()) {
swapx(&id_ns.nsze);
swapx(&id_ns.ncap);
swapx(&id_ns.nuse);
swapx(&id_ns.nawun);
swapx(&id_ns.nawupf);
swapx(&id_ns.nacwu);
swapx(&id_ns.nabsn);
swapx(&id_ns.nabo);
swapx(&id_ns.nabspf);
for (int i = 0; i < 16; i++)
swapx(&id_ns.lbaf[i].ms);
}
return true;
}
bool nvme_Device::nvme_pass_through(const nvme_cmd_in & in)
{
nvme_cmd_out out;
nvme_passthru_cmd pt;
memset(&pt, 0, sizeof(pt));
pt.opcode = in.opcode;
pt.nsid = in.nsid;
pt.addr = (uint64_t)in.buffer;
pt.data_len = in.size;
pt.cdw10 = in.cdw10;
pt.cdw11 = in.cdw11;
pt.cdw12 = in.cdw12;
pt.cdw13 = in.cdw13;
pt.cdw14 = in.cdw14;
pt.cdw15 = in.cdw15;
// Kernel default for NVMe admin commands is 60 seconds
// pt.timeout_ms = 60 * 1000;
int status = ioctl(m_fd, NVME_IOCTL_ADMIN_CMD, &pt);
if (status < 0)
//return set_err(errno, "NVME_IOCTL_ADMIN_CMD: %s", strerror(errno));
return false;
else if (status > 0)
//return set_nvme_err(out, status);
return false;
else
return true;
}
bool nvme_Device::nvme_read_WAI( void * data, unsigned size)
{
memset(data, 0, size);
nvme_cmd_in in;
in.set_data_in(nvme_admin_get_log_page, data, size);
in.nsid = 0x00;
in.cdw10 = 0x3ff04c6;
return nvme_pass_through(in);
}
bool nvme_Device::nvme_read_badblock( void * data, unsigned size)
{
memset(data, 0, size);
nvme_cmd_in in;
in.set_data_in(nvme_read_bad_block, data, size);
in.nsid = 0x00;
in.cdw10 = 0x400;
in.cdw12 = 0x5A;
return nvme_pass_through(in);
}
bool nvme_Device::nvme_read_identify(unsigned nsid,
unsigned char cns, void * data, unsigned size)
{
memset(data, 0, size);
nvme_cmd_in in;
in.set_data_in(nvme_admin_identify, data, size);
in.nsid = nsid;
in.cdw10 = cns;
return nvme_pass_through(in);
}
bool nvme_Device::nvme_read_log_page(unsigned char lid, void * data, unsigned size)
{
if (!(4 <= size && size <= 0x4000 && (size % 4) == 0))
{
//QMessageBox::information(NULL, "Error", "invalid opcode", NULL);
// qDebug() << "invalid opcode";
return false;
}
//throw std::logic_error("nvme_read_log_page(): invalid size");
memset(data, 0, size);
nvme_cmd_in in;
in.set_data_in(nvme_admin_get_log_page, data, size);
in.nsid = m_nsid;
in.cdw10 = lid | (((size / 4) - 1) << 16);
return nvme_pass_through(in);
}