forked from mirror/smartmontools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvmecmds.cpp
248 lines (212 loc) · 6.71 KB
/
nvmecmds.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
/*
* nvmecmds.cpp
*
* Home page of code is: http://www.smartmontools.org
*
* Copyright (C) 2016 Christian Franke
*
* 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, or (at your option)
* any later version.
*
* You should have received a copy of the GNU General Public License
* (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "nvmecmds.h"
const char * nvmecmds_cvsid = "$Id$"
NVMECMDS_H_CVSID;
#include "dev_interface.h"
#include "atacmds.h" // swapx(), ASSERT_*(), dont_print_serial_number
#include "scsicmds.h" // dStrHex()
#include "utility.h"
using namespace smartmontools;
// Check nvme_* struct sizes
ASSERT_SIZEOF_STRUCT(nvme_id_ctrl, 4096);
ASSERT_SIZEOF_STRUCT(nvme_id_ns, 4096);
ASSERT_SIZEOF_STRUCT(nvme_error_log_page, 64);
ASSERT_SIZEOF_STRUCT(nvme_smart_log, 512);
// Print NVMe debug messages?
unsigned char nvme_debugmode = 0;
// Dump up to 4096 bytes, do not dump trailing zero bytes.
// TODO: Handle this by new unified function in utility.cpp
static void debug_hex_dump(const void * data, unsigned size)
{
const unsigned char * p = (const unsigned char *)data;
const unsigned limit = 4096; // sizeof(nvme_id_ctrl)
unsigned sz = (size <= limit ? size : limit);
while (sz > 0x10 && !p[sz-1])
sz--;
if (sz < size) {
if (sz & 0x0f)
sz = (sz & ~0x0f) + 0x10;
sz += 0x10;
if (sz > size)
sz = size;
}
dStrHex(p, sz, 0);
if (sz < size)
pout(" ...\n");
}
// Call NVMe pass-through and print debug info if requested.
static bool nvme_pass_through(nvme_device * device, const nvme_cmd_in & in,
nvme_cmd_out & out)
{
int64_t start_usec = -1;
if (nvme_debugmode) {
pout(" [NVMe call: opcode=0x%02x, size=0x%04x, nsid=0x%08x, cdw10=0x%08x",
in.opcode, in.size, in.nsid, in.cdw10);
if (in.cdw11 || in.cdw12 || in.cdw13 || in.cdw14 || in.cdw15)
pout(",\n cdw1x=0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x",
in.cdw11, in.cdw12, in.cdw13, in.cdw14, in.cdw15);
pout("]\n");
start_usec = smi()->get_timer_usec();
}
bool ok = device->nvme_pass_through(in, out);
if ( dont_print_serial_number && ok
&& in.opcode == nvme_admin_identify && in.cdw10 == 0x01) {
// Invalidate serial number
nvme_id_ctrl & id_ctrl = *reinterpret_cast<nvme_id_ctrl *>(in.buffer);
memset(id_ctrl.sn, 'X', sizeof(id_ctrl.sn));
}
if (nvme_debugmode) {
if (start_usec >= 0) {
int64_t duration_usec = smi()->get_timer_usec() - start_usec;
if (duration_usec >= 500)
pout(" [Duration: %.3fs]\n", duration_usec / 1000000.0);
}
if (!ok) {
pout(" [NVMe call failed: ");
if (out.status_valid)
pout("NVMe Status=0x%04x", out.status);
else
pout("%s", device->get_errmsg());
}
else {
pout(" [NVMe call succeeded: result=0x%08x", out.result);
if (nvme_debugmode > 1 && in.direction() == nvme_cmd_in::data_in) {
pout("\n");
debug_hex_dump(in.buffer, in.size);
pout(" ");
}
}
pout("]\n");
}
return ok;
}
// Call NVMe pass-through and print debug info if requested.
// Version without output parameters.
static bool nvme_pass_through(nvme_device * device, const nvme_cmd_in & in)
{
nvme_cmd_out out;
return nvme_pass_through(device, in, out);
}
// Read NVMe identify info with controller/namespace field CNS.
static bool nvme_read_identify(nvme_device * device, 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(device, in);
}
// Read NVMe Identify Controller data structure.
bool nvme_read_id_ctrl(nvme_device * device, nvme_id_ctrl & id_ctrl)
{
if (!nvme_read_identify(device, 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;
}
// Read NVMe Identify Namespace data structure for namespace NSID.
bool nvme_read_id_ns(nvme_device * device, unsigned nsid, nvme_id_ns & id_ns)
{
if (!nvme_read_identify(device, 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;
}
// Read NVMe log page with identifier LID.
bool nvme_read_log_page(nvme_device * device, unsigned char lid, void * data, unsigned size)
{
if (!(4 <= size && size <= 0x4000 && (size % 4) == 0))
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 = device->get_nsid();
in.cdw10 = lid | (((size / 4) - 1) << 16);
return nvme_pass_through(device, in);
}
// Read NVMe Error Information Log.
bool nvme_read_error_log(nvme_device * device, nvme_error_log_page * error_log, unsigned num_entries)
{
if (!nvme_read_log_page(device, 0x01, error_log, num_entries * sizeof(*error_log)))
return false;
if (isbigendian()) {
for (unsigned i = 0; i < num_entries; i++) {
swapx(&error_log[i].error_count);
swapx(&error_log[i].sqid);
swapx(&error_log[i].cmdid);
swapx(&error_log[i].status_field);
swapx(&error_log[i].parm_error_location);
swapx(&error_log[i].lba);
swapx(&error_log[i].nsid);
}
}
return true;
}
// Read NVMe SMART/Health Information log.
bool nvme_read_smart_log(nvme_device * device, nvme_smart_log & smart_log)
{
if (!nvme_read_log_page(device, 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;
}