-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathinnogrit-nvme.c
More file actions
369 lines (323 loc) · 9.14 KB
/
innogrit-nvme.c
File metadata and controls
369 lines (323 loc) · 9.14 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#include "typedef.h"
#include "util/cleanup.h"
#define CREATE_CMD
#include "innogrit-nvme.h"
static int nvme_vucmd(struct nvme_transport_handle *hdl, unsigned char opcode,
unsigned int cdw12, unsigned int cdw13,
unsigned int cdw14, unsigned int cdw15, char *data,
int data_len)
{
struct nvme_passthru_cmd cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = opcode;
cmd.cdw2 = IGVSC_SIG;
cmd.cdw10 = data_len / 4;
cmd.cdw12 = cdw12;
cmd.cdw13 = cdw13;
cmd.cdw14 = cdw14;
cmd.cdw15 = cdw15;
cmd.nsid = 0xffffffff;
cmd.addr = (__u64)(__u64)(uintptr_t)data;
cmd.data_len = data_len;
return nvme_submit_admin_passthru(hdl, &cmd);
}
static int getlogpage(struct nvme_transport_handle *hdl, unsigned char ilogid,
unsigned char ilsp, char *data, int data_len,
__u64 *result)
{
struct nvme_passthru_cmd cmd;
nvme_init_get_log(&cmd, NVME_NSID_ALL, ilogid, NVME_CSI_NVM,
data, data_len);
cmd.cdw10 |= NVME_FIELD_ENCODE(ilsp,
NVME_LOG_CDW10_LSP_SHIFT,
NVME_LOG_CDW10_LSP_MASK);
return nvme_get_log(hdl, &cmd, true, NVME_LOG_PAGE_PDU_SIZE);
}
static int getvsctype(struct nvme_transport_handle *hdl)
{
unsigned char ilogid;
char data[4096];
struct drvinfo_t *pdrvinfo = (struct drvinfo_t *)data;
memset(data, 0, 4096);
// pdrvinfo by getlogpage
for (ilogid = 0xe1; ilogid < 0xe2; ilogid++) {
getlogpage(hdl, ilogid, 0, data, 4096, NULL);
if (pdrvinfo->signature == 0x5A)
return 1;
}
//pdrvinfo by vucmd
nvme_vucmd(hdl, 0xfe, 0x82, 0X03, 0x00, 0, (char *)data, 4096);
if (pdrvinfo->signature == 0x5A)
return 1;
return 0;
}
static int getvsc_eventlog(struct nvme_transport_handle *hdl, FILE *fp)
{
unsigned int errcnt, rxlen, start_flag;
int ivsctype = getvsctype(hdl);
struct evlg_flush_hdr *pevlog;
unsigned long long end_flag;
char data[4096];
int ret = -1;
pevlog = (struct evlg_flush_hdr *)data;
start_flag = 0;
rxlen = 0;
errcnt = 0;
while (1) {
memset(data, 0, 4096);
if (ivsctype == 0) {
ret = nvme_vucmd(hdl, NVME_VSC_GET_EVENT_LOG, 0, 0,
(SRB_SIGNATURE >> 32),
(SRB_SIGNATURE & 0xFFFFFFFF),
(char *)data, 4096);
} else {
ret = nvme_vucmd(hdl, NVME_VSC_TYPE1_GET, 0x60, 0,
0, 0, (char *)data, 4096);
}
if (ret == -1) {
printf("(error)\n");
return IG_ERROR;
}
if (pevlog->signature == EVLOG_SIG) {
errcnt = 0;
} else {
errcnt++;
if (errcnt > 16) {
printf("(invalid data error)\n");
return IG_ERROR;
}
}
if (start_flag == 1) {
end_flag = *(unsigned long long *)&data[4096 - 32];
if (end_flag == 0xffffffff00000000)
break;
fwrite(data, 1, 4096, fp);
rxlen += 4096;
printf("\rget eventlog : %d.%d MB ", rxlen / SIZE_MB,
(rxlen % SIZE_MB) * 100 / SIZE_MB);
} else if (errcnt == 0) {
printf("get eventlog by vsc command\n");
start_flag = 1;
fwrite(data, 1, 4096, fp);
rxlen += 4096;
}
}
printf("\n");
return IG_SUCCESS;
}
int getlogpage_eventlog(struct nvme_transport_handle *hdl, FILE *fp)
{
unsigned int i, total_size;
char data[4096];
__u64 result;
int ret = 0;
result = 0;
ret = getlogpage(hdl, 0xcb, 0x01, data, 4096, NULL);
if (ret)
return IG_UNSUPPORT;
ret = getlogpage(hdl, 0xcb, 0x02, data, 4096, &result);
if ((ret) || (result == 0))
return IG_UNSUPPORT;
total_size = result * 4096;
printf("total eventlog : %d.%d MB\n", total_size / SIZE_MB,
(total_size % SIZE_MB) * 100 / SIZE_MB);
for (i = 0; i <= total_size; i += 4096) {
ret = getlogpage(hdl, 0xcb, 0x00, data, 4096, NULL);
printf("\rget eventlog : %d.%d MB ", i / SIZE_MB,
(i % SIZE_MB) * 100 / SIZE_MB);
if (ret) {
printf("(error)\n");
return IG_ERROR;
}
fwrite(data, 1, 4096, fp);
}
printf("\n");
return IG_SUCCESS;
}
static int innogrit_geteventlog(int argc, char **argv,
struct command *command,
struct plugin *plugin)
{
const char *desc = "Recrieve event log for the given device ";
_cleanup_nvme_transport_handle_ struct nvme_transport_handle *hdl = NULL;
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
_cleanup_file_ FILE *fp = NULL;
char currentdir[128], filename[512];
struct tm *logtime;
time_t timep;
int ret = -1;
NVME_ARGS(opts);
ret = parse_and_open(&ctx, &hdl, argc, argv, desc, opts);
if (ret)
return ret;
if (getcwd(currentdir, 128) == NULL)
return -1;
time(&timep);
logtime = localtime(&timep);
sprintf(filename, "%s/eventlog_%02d%02d-%02d%02d%02d.eraw", currentdir,
logtime->tm_mon + 1, logtime->tm_mday, logtime->tm_hour,
logtime->tm_min, logtime->tm_sec);
printf("output eventlog file : %s\n", filename);
fp = fopen(filename, "a+");
getvsctype(hdl);
ret = getlogpage_eventlog(hdl, fp);
if (ret == IG_UNSUPPORT)
ret = getvsc_eventlog(hdl, fp);
chmod(filename, 0666);
return ret;
}
static int innogrit_vsc_getcdump(int argc, char **argv, struct command *acmd,
struct plugin *plugin)
{
const char *desc = "Recrieve cdump data for the given device ";
char currentdir[128], filename[512], fname[128];
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
_cleanup_nvme_transport_handle_ struct nvme_transport_handle *hdl = NULL;
unsigned int itotal, icur, ivsctype;
unsigned int ipackcount, ipackindex;
unsigned char busevsc = false;
struct cdumpinfo cdumpinfo;
struct tm *logtime;
FILE *fp = NULL;
char data[4096];
char fwvera[32];
time_t timep;
int ret = -1;
NVME_ARGS(opts);
ret = parse_and_open(&ctx, &hdl, argc, argv, desc, opts);
if (ret)
return ret;
ivsctype = getvsctype(hdl);
if (getcwd(currentdir, 128) == NULL)
return -1;
time(&timep);
logtime = localtime(&timep);
ipackindex = 0;
memset(data, 0, 4096);
if (ivsctype == 0) {
ret = nvme_vucmd(hdl, NVME_VSC_GET, VSC_FN_GET_CDUMP, 0x00,
(SRB_SIGNATURE >> 32), (SRB_SIGNATURE & 0xFFFFFFFF),
(char *)data, 4096);
} else {
ret = nvme_vucmd(hdl, NVME_VSC_TYPE1_GET, 0x82, 0x00,
0, 0, (char *)data, 4096);
}
if (ret == 0) {
memcpy(&cdumpinfo, &data[3072], sizeof(cdumpinfo));
if (cdumpinfo.sig == 0x5a5b5c5d) {
busevsc = true;
ipackcount = cdumpinfo.ipackcount;
if (ipackcount == 0) {
itotal = 0;
} else {
itotal = cdumpinfo.cdumppack[ipackindex].ilenth;
memset(fwvera, 0, sizeof(fwvera));
memcpy(fwvera, cdumpinfo.cdumppack[ipackindex].fwver, 8);
sprintf(fname, "cdump_%02d%02d-%02d%02d%02d_%d_%s.cdp",
logtime->tm_mon+1, logtime->tm_mday, logtime->tm_hour,
logtime->tm_min, logtime->tm_sec, ipackindex, fwvera);
sprintf(filename, "%s/%s", currentdir, fname);
if (fp != NULL)
fclose(fp);
fp = fopen(filename, "a+");
}
}
}
if (busevsc == false) {
memset(data, 0, 4096);
ret = nvme_get_nsid_log(hdl, NVME_NSID_ALL,true, 0x07,
data, 4096);
if (ret != 0)
return ret;
ipackcount = 1;
memcpy(&itotal, &data[4092], 4);
sprintf(fname, "cdump_%02d%02d-%02d%02d%02d.cdp", logtime->tm_mon+1,
logtime->tm_mday, logtime->tm_hour, logtime->tm_min, logtime->tm_sec);
sprintf(filename, "%s/%s", currentdir, fname);
if (fp != NULL)
fclose(fp);
fp = fopen(filename, "a+");
}
if (itotal == 0) {
printf("no cdump data\n");
return 0;
}
while (ipackindex < ipackcount) {
memset(data, 0, 4096);
strcpy((char *)data, "cdumpstart");
fwrite(data, 1, strlen((char *)data), fp);
for (icur = 0; icur < itotal; icur += 4096) {
memset(data, 0, 4096);
if (busevsc) {
if (ivsctype == 0) {
ret = nvme_vucmd(hdl, NVME_VSC_GET,
VSC_FN_GET_CDUMP, 0x00,
(SRB_SIGNATURE >> 32),
(SRB_SIGNATURE & 0xFFFFFFFF),
(char *)data, 4096);
} else {
ret = nvme_vucmd(hdl, NVME_VSC_TYPE1_GET,
0x82, 0x00, 0, 0, (char *)data, 4096);
}
} else {
ret = nvme_get_nsid_log(hdl, NVME_NSID_ALL, true,
0x07, data, 4096);
}
if (ret != 0)
return ret;
fwrite(data, 1, 4096, fp);
printf("\rWait for dump data %d%%" XCLEAN_LINE,
((icur + 4096) * 100 / itotal));
}
memset(data, 0, 4096);
strcpy((char *)data, "cdumpend");
fwrite(data, 1, strlen((char *)data), fp);
printf("\r%s\n", fname);
ipackindex++;
if (ipackindex != ipackcount) {
memset(data, 0, 4096);
if (busevsc) {
if (ivsctype == 0) {
ret = nvme_vucmd(hdl, NVME_VSC_GET,
VSC_FN_GET_CDUMP, 0x00,
(SRB_SIGNATURE >> 32),
(SRB_SIGNATURE & 0xFFFFFFFF),
(char *)data, 4096);
} else {
ret = nvme_vucmd(hdl, NVME_VSC_TYPE1_GET,
0x82, 0x00, 0, 0, (char *)data, 4096);
}
} else {
ret = nvme_get_nsid_log(hdl, NVME_NSID_ALL, true,
0x07, data, 4096);
}
if (ret != 0)
return ret;
itotal = cdumpinfo.cdumppack[ipackindex].ilenth;
memset(fwvera, 0, sizeof(fwvera));
memcpy(fwvera, cdumpinfo.cdumppack[ipackindex].fwver, 8);
sprintf(fname, "cdump_%02d%02d-%02d%02d%02d_%d_%s.cdp", logtime->tm_mon+1,
logtime->tm_mday, logtime->tm_hour, logtime->tm_min, logtime->tm_sec,
ipackindex, fwvera);
if (fp != NULL)
fclose(fp);
fp = fopen(filename, "a+");
}
}
printf("\n");
if (fp != NULL)
fclose(fp);
return ret;
}