forked from nvmedirect/nvmed_info
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvmed_info_utils.c
More file actions
201 lines (167 loc) · 4.1 KB
/
nvmed_info_utils.c
File metadata and controls
201 lines (167 loc) · 4.1 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "nvme_hdr.h"
#include "nvmed.h"
#include "lib_nvmed.h"
#include "nvmed_info.h"
#define BYTES_PER_LINE (16)
char *nvme_sc[] = {
/* 00h */ "Successful Completion",
/* 01h */ "Invalid Command Opcode",
/* 02h */ "Invalid Field in Command",
/* 03h */ "Command ID Conflict",
/* 04h */ "Data Transfer Error",
/* 05h */ "Commands Aborted due to Power Loss Notification",
/* 06h */ "Internal Error",
/* 07h */ "Command Abort Requested",
/* 08h */ "Command Aborted due to SQ Deletion",
/* 09h */ "Command Aborted due to Failed Fused Command",
/* 0Ah */ "Command Aborted due to Missing Fused Command",
/* 0Bh */ "Invalid Namespace or Format",
/* 0Ch */ "Command Sequence Error",
/* 0Dh */ "Invalid SGL Segment Descriptor",
/* 0Eh */ "Invalid Number of SQL Descriptors",
/* 0Fh */ "Data SGL Length Invalid",
/* 10h */ "Metadata SGL Lengh Invalid",
/* 11h */ "SGL Descriptor Type Invalid",
/* 12h */ "Invalid Use of Controller Memory Buffer",
/* 13h */ "PRP Offset Invalid",
/* 14h */ "Atomic Write Unit Exceeded",
};
int nvmed_info_admin_command (NVMED *nvmed, struct nvme_admin_cmd *cmd)
{
int rc;
rc = ioctl (dev_fd, NVME_IOCTL_ADMIN_CMD, cmd);
if (rc < 0) {
printf("ioctl() failed, rc = %d.\n", rc);
return -1;
}
else if (rc > 0) {
if (rc < (int) (sizeof(nvme_sc)/sizeof(char *)))
printf ("NVMe Error %d (Opcode %02x): %s\n", rc, cmd->opcode, nvme_sc[rc]);
else
printf ("NVMe Error %d (Opcode %02x)\n", rc, cmd->opcode);
return -1;
}
return 0;
}
struct nvmed_info_cmd *cmd_lookup (struct nvmed_info_cmd *list, char *str)
{
struct nvmed_info_cmd *c = list;
if (c == NULL || str == NULL)
return NULL;
while (c->cmd_name) {
if (!strncmp(str, c->cmd_name, c->len))
return c;
c++;
}
return NULL;
}
int cmd_help (char *invalid_cmd, char *cmd_name, struct nvmed_info_cmd *c)
{
if (invalid_cmd)
printf("Invalid command \"%s\"\n", invalid_cmd);
printf("%s\n", cmd_name);
while (c->cmd_name) {
printf("\t%-12s\t%s\n", c->cmd_name, c->cmd_help);
c++;
}
return -1;
}
#if 0
void *alloc_buffer (int bytes)
{
void *p;
bytes = ((bytes - 1) & ~PAGESIZE) + PAGESIZE;
p = mmap (0, bytes, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (p == NULL)
{
perror ("mmap");
return p;
}
memset (p, 0, bytes);
return p;
}
int dealloc_buffer (void *p, int bytes)
{
return munmap (p, bytes);
}
#endif
void print_bytes (__u8 *p, int n)
{
int i, j;
int col;
for (i = 0; i < n; i += BYTES_PER_LINE)
{
col = ((n - i) >= BYTES_PER_LINE)? BYTES_PER_LINE : (n - i);
printf ("[%04x] %04d: ", i, i);
for (j = 0; j < col; j++)
printf ("%02x ", p[i+j]);
for (j = col; j < BYTES_PER_LINE; j++)
printf (" ");
printf (" ");
for (j = 0; j < col; j++)
printf ("%1c", (isprint (p[i+j]))? p[i+j] : '.');
for (j = col; j < BYTES_PER_LINE; j++)
printf (" ");
printf ("\n");
}
}
void print_something (enum print_format format, __u8 *p, int offset, int end, char *title, char *unit)
{
int i, j;
int col;
char s[80];
__u64 value;
for (i = offset; i < end; i += 4)
{
if (i == offset)
printf ("%04d:%04d ", offset, end);
else
printf (" ");
col = ((end + 1 - i) < 4)? (end + 1 - i) : 4;
for (j = 0; j < col; j++)
printf ("%02x ", p[i+j]);
for (j = col; j < 4; j++)
printf (" ");
printf (" ");
switch (format)
{
case FORMAT_STRING:
if (i == offset)
{
strncpy (s, (char *) &p[offset], end + 1 - offset);
s[end + 1 - offset] = '\0';
printf ("%s: %s", title, s);
}
break;
case FORMAT_ID:
if (i == offset)
printf ("%s:", title);
if (((i == offset) && (end + 1 - offset) <= 4) || (i == offset + 4))
{
for (j = end; j >= offset; j--)
{
printf ("%02x", p[j]);
if (j != offset)
printf ("-");
}
}
break;
case FORMAT_VALUE:
if (i == offset)
{
value = 0;
for (j = end; j >= offset; j--)
value = (value << 8) + p[j];
printf ("%s: %llu %s", title, value, unit);
}
break;
default:
printf ("UNKNOWN FORMAT");
}
printf ("\n");
}
}