sbin/hammer: Fix bug in get_buffer_data()
[dragonfly.git] / sbin / nvmectl / subs.c
1 /*
2  * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include "nvmectl.h"
36
37 const char *
38 format_number(uint64_t value)
39 {
40         static char buf[8];
41
42         humanize_number(buf, sizeof(buf), value, "",
43                          HN_AUTOSCALE, HN_DIVISOR_1000 | HN_NOSPACE);
44
45         return buf;
46 }
47
48 const char *
49 status_to_str(uint16_t status)
50 {
51         static char buf[64];
52         int code = NVME_COMQ_STATUS_CODE_GET(status);
53         const char *msg = NULL;
54
55         switch(NVME_COMQ_STATUS_TYPE_GET(status)) {
56         case NVME_STATUS_TYPE_GENERIC:
57                 switch(code) {
58                 case NVME_CODE_SUCCESS:
59                         msg = "SUCCESS";
60                         break;
61                 case NVME_CODE_BADOP:
62                         msg = "Bad Op";
63                         break;
64                 case NVME_CODE_BADFIELD:
65                         msg = "Bad Field";
66                         break;
67                 case NVME_CODE_IDCONFLICT:
68                         msg = "ID Conflict";
69                         break;
70                 case NVME_CODE_BADXFER:
71                         msg = "Bad XFer";
72                         break;
73                 case NVME_CODE_ABORTED_PWRLOSS:
74                         msg = "Abort on Power Loss";
75                         break;
76                 case NVME_CODE_INTERNAL:
77                         msg = "Internal Error";
78                         break;
79                 case NVME_CODE_ABORTED_ONREQ:
80                         msg = "Abort on Request";
81                         break;
82                 case NVME_CODE_ABORTED_SQDEL:
83                         msg = "Abort on SubQ Deletion";
84                         break;
85                 case NVME_CODE_ABORTED_FUSEFAIL:
86                         msg = "Abort on Fuse Failed";
87                         break;
88                 case NVME_CODE_ABORTED_FUSEMISSING:
89                         msg = "Abort on Fuse Missing";
90                         break;
91                 case NVME_CODE_BADNAMESPACE:
92                         msg = "Bad Namespace";
93                         break;
94                 case NVME_CODE_SEQERROR:
95                         msg = "Seq Error";
96                         break;
97                 case NVME_CODE_BADSGLSEG:
98                         msg = "Bad SGL Segment";
99                         break;
100                 case NVME_CODE_BADSGLCNT:
101                         msg = "Bad SGL Count";
102                         break;
103                 case NVME_CODE_BADSGLLEN:
104                         msg = "Bad SGL Length";
105                         break;
106                 case NVME_CODE_BADSGLMLEN:
107                         msg = "Bad SGL MLength";
108                         break;
109                 case NVME_CODE_BADSGLTYPE:
110                         msg = "Bad SGL Type";
111                         break;
112                 case NVME_CODE_BADMEMBUFUSE:
113                         msg = "Bad Memory Buffer Usage";
114                         break;
115                 case NVME_CODE_BADPRPOFF:
116                         msg = "Bad PRP Offset";
117                         break;
118                 case NVME_CODE_ATOMICWUOVFL:
119                         msg = "Atomic Write Overflow";
120                         break;
121                 case NVME_CODE_LBA_RANGE:
122                         msg = "LBA Out of Range";
123                         break;
124                 case NVME_CODE_CAP_EXCEEDED:
125                         msg = "Capacity Exceeded";
126                         break;
127                 case NVME_CODE_NAM_NOT_READY:
128                         msg = "Namespace not Ready";
129                         break;
130                 case NVME_CODE_RSV_CONFLICT:
131                         msg = "Reservation Conflict";
132                         break;
133                 case NVME_CODE_FMT_IN_PROG:
134                         msg = "Format in Progress";
135                         break;
136                 default:
137                         snprintf(buf, sizeof(buf), "generic(0x%02x)", code);
138                         break;
139                 }
140                 break;
141         case NVME_STATUS_TYPE_SPECIFIC:
142                 switch(code) {
143                 case NVME_CSSCODE_BADCOMQ:
144                         msg = "Bad Completion Queue";
145                         break;
146                 case NVME_CSSCODE_BADQID:
147                         msg = "Bad Queue ID";
148                         break;
149                 case NVME_CSSCODE_BADQSIZE:
150                         msg = "Bad Queue Size";
151                         break;
152                 case NVME_CSSCODE_ABORTLIM:
153                         msg = "Too many Aborts";
154                         break;
155                 case NVME_CSSCODE_RESERVED04:
156                         msg = "reserved04";
157                         break;
158                 case NVME_CSSCODE_ASYNCEVENTLIM:
159                         msg = "Too many Async Events";
160                         break;
161                 case NVME_CSSCODE_BADFWSLOT:
162                         msg = "Bad Firmware Slot";
163                         break;
164                 case NVME_CSSCODE_BADFWIMAGE:
165                         msg = "Bad Firmware Image";
166                         break;
167                 case NVME_CSSCODE_BADINTRVECT:
168                         msg = "Bad Interrupt Vector";
169                         break;
170                 case NVME_CSSCODE_BADLOGPAGE:
171                         msg = "Unsupported Log Page";
172                         break;
173                 case NVME_CSSCODE_BADFORMAT:
174                         msg = "Bad Format Command";
175                         break;
176                 case NVME_CSSCODE_FW_NEEDSCONVRESET:
177                         msg = "Firmware Activation Needs Conventional Reset";
178                         break;
179                 case NVME_CSSCODE_BADQDELETE:
180                         msg = "Bad Queue Delete";
181                         break;
182                 case NVME_CSSCODE_FEAT_NOT_SAVEABLE:
183                         msg = "Feature not Saveable";
184                         break;
185                 case NVME_CSSCODE_FEAT_NOT_CHGABLE:
186                         msg = "Feature not Changeable";
187                         break;
188                 case NVME_CSSCODE_FEAT_NOT_NSSPEC:
189                         msg = "Feature not Namespace-Specific";
190                         break;
191                 case NVME_CSSCODE_FW_NEEDSSUBRESET:
192                         msg = "Firmware Activation Needs Subsystem Reset";
193                         break;
194                 case NVME_CSSCODE_FW_NEEDSRESET:
195                         msg = "Firmware Activation Needs Reset";
196                         break;
197                 case NVME_CSSCODE_FW_NEEDSMAXTVIOLATE:
198                         msg = "Firmware Activation Requires "
199                                 "Maximum Time Violation";
200                         break;
201                 case NVME_CSSCODE_FW_PROHIBITED:
202                         msg = "Firmware Activation Prohibited";
203                         break;
204                 case NVME_CSSCODE_RANGE_OVERLAP:
205                         msg = "Overlapping Range";
206                         break;
207                 case NVME_CSSCODE_NAM_INSUFF_CAP:
208                         msg = "Insufficient Capacity";
209                         break;
210                 case NVME_CSSCODE_NAM_ID_UNAVAIL:
211                         msg = "NSID is not Available";
212                         break;
213                 case NVME_CSSCODE_RESERVED17:
214                         msg = "reserved17";
215                         break;
216                 case NVME_CSSCODE_NAM_ALREADY_ATT:
217                         msg = "NSID Already Attached";
218                         break;
219                 case NVME_CSSCODE_NAM_IS_PRIVATE:
220                         msg = "NSID is Private";
221                         break;
222                 case NVME_CSSCODE_NAM_NOT_ATT:
223                         msg = "NSID Not Attached";
224                         break;
225                 case NVME_CSSCODE_NO_THIN_PROVISION:
226                         msg = "This Provisioning not Supported";
227                         break;
228                 case NVME_CSSCODE_CTLR_LIST_INVALID:
229                         msg = "Controller List Invalid";
230                         break;
231                 case NVME_CSSCODE_ATTR_CONFLICT:
232                         msg = "Conflicting Attributes";
233                         break;
234                 case NVME_CSSCODE_BADPROTINFO:
235                         msg = "Invalid Prortection Information";
236                         break;
237                 case NVME_CSSCODE_WRITE_TO_RDONLY:
238                         msg = "Attempted Write to Read Only Range";
239                         break;
240                 default:
241                         snprintf(buf, sizeof(buf), "specific(0x%02x)", code);
242                         break;
243                 }
244                 break;
245         case NVME_STATUS_TYPE_MEDIA:
246                 switch(code) {
247                 case NVME_MEDCODE_WRITE_FAULT:
248                         msg = "Write Fault";
249                         break;
250                 case NVME_MEDCODE_UNRECOV_READ_ERROR:
251                         msg = "Unrecoverable Read Error";
252                         break;
253                 case NVME_MEDCODE_ETOE_GUARD_CHK:
254                         msg = "End-to-End Guard Check Fail";
255                         break;
256                 case NVME_MEDCODE_ETOE_APPTAG_CHK:
257                         msg = "End-to-End Application Tag Check Error";
258                         break;
259                 case NVME_MEDCODE_ETOE_REFTAG_CHK:
260                         msg = "End-to-End Reference Tag Check Error";
261                         break;
262                 case NVME_MEDCODE_COMPARE_FAILURE:
263                         msg = "Compare Failure";
264                         break;
265                 case NVME_MEDCODE_ACCESS_DENIED:
266                         msg = "Access Denied";
267                         break;
268                 case NVME_MEDCODE_UNALLOCATED:
269                         msg = "Deallocated or Unwritten Logical Block";
270                         break;
271                 default:
272                         snprintf(buf, sizeof(buf), "media(0x%02x)", code);
273                         break;
274                 }
275                 break;
276         case NVME_STATUS_TYPE_3:
277         case NVME_STATUS_TYPE_4:
278         case NVME_STATUS_TYPE_5:
279         case NVME_STATUS_TYPE_6:
280                 buf[0] = 0;
281                 break;
282         case NVME_STATUS_TYPE_VENDOR:
283                 snprintf(buf, sizeof(buf), "vendor(0x%02x)", code);
284                 break;
285         }
286         if (msg)
287                 snprintf(buf, sizeof(buf), "%s", msg);
288
289         return buf;
290 }