xref: /openbmc/qemu/scsi/utils.c (revision 6f03770d)
1 /*
2  *  SCSI helpers
3  *
4  *  Copyright 2017 Red Hat, Inc.
5  *
6  *  Authors:
7  *   Fam Zheng <famz@redhat.com>
8  *   Paolo Bonzini <pbonzini@redhat.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  */
15 
16 #include "qemu/osdep.h"
17 #include "scsi/constants.h"
18 #include "scsi/utils.h"
19 #include "qemu/bswap.h"
20 
21 uint32_t scsi_data_cdb_xfer(uint8_t *buf)
22 {
23     if ((buf[0] >> 5) == 0 && buf[4] == 0) {
24         return 256;
25     } else {
26         return scsi_cdb_xfer(buf);
27     }
28 }
29 
30 uint32_t scsi_cdb_xfer(uint8_t *buf)
31 {
32     switch (buf[0] >> 5) {
33     case 0:
34         return buf[4];
35     case 1:
36     case 2:
37         return lduw_be_p(&buf[7]);
38     case 4:
39         return ldl_be_p(&buf[10]) & 0xffffffffULL;
40     case 5:
41         return ldl_be_p(&buf[6]) & 0xffffffffULL;
42     default:
43         return -1;
44     }
45 }
46 
47 uint64_t scsi_cmd_lba(SCSICommand *cmd)
48 {
49     uint8_t *buf = cmd->buf;
50     uint64_t lba;
51 
52     switch (buf[0] >> 5) {
53     case 0:
54         lba = ldl_be_p(&buf[0]) & 0x1fffff;
55         break;
56     case 1:
57     case 2:
58     case 5:
59         lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
60         break;
61     case 4:
62         lba = ldq_be_p(&buf[2]);
63         break;
64     default:
65         lba = -1;
66 
67     }
68     return lba;
69 }
70 
71 int scsi_cdb_length(uint8_t *buf)
72 {
73     int cdb_len;
74 
75     switch (buf[0] >> 5) {
76     case 0:
77         cdb_len = 6;
78         break;
79     case 1:
80     case 2:
81         cdb_len = 10;
82         break;
83     case 4:
84         cdb_len = 16;
85         break;
86     case 5:
87         cdb_len = 12;
88         break;
89     default:
90         cdb_len = -1;
91     }
92     return cdb_len;
93 }
94 
95 SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len)
96 {
97     bool fixed_in;
98     SCSISense sense;
99 
100     assert(in_len > 0);
101     fixed_in = (in_buf[0] & 2) == 0;
102     if (fixed_in) {
103         if (in_len < 14) {
104             return SENSE_CODE(IO_ERROR);
105         }
106         sense.key = in_buf[2];
107         sense.asc = in_buf[12];
108         sense.ascq = in_buf[13];
109     } else {
110         if (in_len < 4) {
111             return SENSE_CODE(IO_ERROR);
112         }
113         sense.key = in_buf[1];
114         sense.asc = in_buf[2];
115         sense.ascq = in_buf[3];
116     }
117 
118     return sense;
119 }
120 
121 int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense,
122                          bool fixed_sense)
123 {
124     int len;
125     uint8_t buf[SCSI_SENSE_LEN] = { 0 };
126 
127     if (fixed_sense) {
128         buf[0] = 0x70;
129         buf[2] = sense.key;
130         buf[7] = 10;
131         buf[12] = sense.asc;
132         buf[13] = sense.ascq;
133         len = 18;
134     } else {
135         buf[0] = 0x72;
136         buf[1] = sense.key;
137         buf[2] = sense.asc;
138         buf[3] = sense.ascq;
139         len = 8;
140     }
141     len = MIN(len, size);
142     memcpy(out_buf, buf, len);
143     return len;
144 }
145 
146 int scsi_build_sense(uint8_t *buf, SCSISense sense)
147 {
148     return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true);
149 }
150 
151 /*
152  * Predefined sense codes
153  */
154 
155 /* No sense data available */
156 const struct SCSISense sense_code_NO_SENSE = {
157     .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
158 };
159 
160 /* LUN not ready, Manual intervention required */
161 const struct SCSISense sense_code_LUN_NOT_READY = {
162     .key = NOT_READY, .asc = 0x04, .ascq = 0x03
163 };
164 
165 /* LUN not ready, Medium not present */
166 const struct SCSISense sense_code_NO_MEDIUM = {
167     .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
168 };
169 
170 /* LUN not ready, medium removal prevented */
171 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
172     .key = NOT_READY, .asc = 0x53, .ascq = 0x02
173 };
174 
175 /* Hardware error, internal target failure */
176 const struct SCSISense sense_code_TARGET_FAILURE = {
177     .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
178 };
179 
180 /* Illegal request, invalid command operation code */
181 const struct SCSISense sense_code_INVALID_OPCODE = {
182     .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
183 };
184 
185 /* Illegal request, LBA out of range */
186 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
187     .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
188 };
189 
190 /* Illegal request, Invalid field in CDB */
191 const struct SCSISense sense_code_INVALID_FIELD = {
192     .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
193 };
194 
195 /* Illegal request, Invalid field in parameter list */
196 const struct SCSISense sense_code_INVALID_PARAM = {
197     .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
198 };
199 
200 /* Illegal request, Invalid value in parameter list */
201 const struct SCSISense sense_code_INVALID_PARAM_VALUE = {
202     .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x01
203 };
204 
205 /* Illegal request, Parameter list length error */
206 const struct SCSISense sense_code_INVALID_PARAM_LEN = {
207     .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
208 };
209 
210 /* Illegal request, LUN not supported */
211 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
212     .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
213 };
214 
215 /* Illegal request, Saving parameters not supported */
216 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
217     .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
218 };
219 
220 /* Illegal request, Incompatible medium installed */
221 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
222     .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
223 };
224 
225 /* Illegal request, medium removal prevented */
226 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
227     .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
228 };
229 
230 /* Illegal request, Invalid Transfer Tag */
231 const struct SCSISense sense_code_INVALID_TAG = {
232     .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
233 };
234 
235 /* Command aborted, I/O process terminated */
236 const struct SCSISense sense_code_IO_ERROR = {
237     .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
238 };
239 
240 /* Command aborted, I_T Nexus loss occurred */
241 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
242     .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
243 };
244 
245 /* Command aborted, Logical Unit failure */
246 const struct SCSISense sense_code_LUN_FAILURE = {
247     .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
248 };
249 
250 /* Command aborted, Overlapped Commands Attempted */
251 const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
252     .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
253 };
254 
255 /* Command aborted, LUN Communication Failure */
256 const struct SCSISense sense_code_LUN_COMM_FAILURE = {
257     .key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
258 };
259 
260 /* Medium Error, Unrecovered read error */
261 const struct SCSISense sense_code_READ_ERROR = {
262     .key = MEDIUM_ERROR, .asc = 0x11, .ascq = 0x00
263 };
264 
265 /* Not ready, Cause not reportable */
266 const struct SCSISense sense_code_NOT_READY = {
267     .key = NOT_READY, .asc = 0x04, .ascq = 0x00
268 };
269 
270 /* Unit attention, Capacity data has changed */
271 const struct SCSISense sense_code_CAPACITY_CHANGED = {
272     .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
273 };
274 
275 /* Unit attention, Power on, reset or bus device reset occurred */
276 const struct SCSISense sense_code_RESET = {
277     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
278 };
279 
280 /* Unit attention, SCSI bus reset */
281 const struct SCSISense sense_code_SCSI_BUS_RESET = {
282     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x02
283 };
284 
285 /* Unit attention, No medium */
286 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
287     .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
288 };
289 
290 /* Unit attention, Medium may have changed */
291 const struct SCSISense sense_code_MEDIUM_CHANGED = {
292     .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
293 };
294 
295 /* Unit attention, Reported LUNs data has changed */
296 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
297     .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
298 };
299 
300 /* Unit attention, Device internal reset */
301 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
302     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
303 };
304 
305 /* Data Protection, Write Protected */
306 const struct SCSISense sense_code_WRITE_PROTECTED = {
307     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
308 };
309 
310 /* Data Protection, Space Allocation Failed Write Protect */
311 const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
312     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
313 };
314 
315 /*
316  * scsi_convert_sense
317  *
318  * Convert between fixed and descriptor sense buffers
319  */
320 int scsi_convert_sense(uint8_t *in_buf, int in_len,
321                        uint8_t *buf, int len, bool fixed)
322 {
323     SCSISense sense;
324     bool fixed_in;
325 
326     if (in_len == 0) {
327         return scsi_build_sense_buf(buf, len, SENSE_CODE(NO_SENSE), fixed);
328     }
329 
330     fixed_in = (in_buf[0] & 2) == 0;
331     if (fixed == fixed_in) {
332         memcpy(buf, in_buf, MIN(len, in_len));
333         return MIN(len, in_len);
334     } else {
335         sense = scsi_parse_sense_buf(in_buf, in_len);
336         return scsi_build_sense_buf(buf, len, sense, fixed);
337     }
338 }
339 
340 static bool scsi_sense_is_guest_recoverable(int key, int asc, int ascq)
341 {
342     switch (key) {
343     case NO_SENSE:
344     case RECOVERED_ERROR:
345     case UNIT_ATTENTION:
346     case ABORTED_COMMAND:
347         return true;
348     case NOT_READY:
349     case ILLEGAL_REQUEST:
350     case DATA_PROTECT:
351         /* Parse ASCQ */
352         break;
353     default:
354         return false;
355     }
356 
357     switch ((asc << 8) | ascq) {
358     case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
359     case 0x2000: /* INVALID OPERATION CODE */
360     case 0x2400: /* INVALID FIELD IN CDB */
361     case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
362     case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
363 
364     case 0x2104: /* UNALIGNED WRITE COMMAND */
365     case 0x2105: /* WRITE BOUNDARY VIOLATION */
366     case 0x2106: /* ATTEMPT TO READ INVALID DATA */
367     case 0x550e: /* INSUFFICIENT ZONE RESOURCES */
368 
369     case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
370     case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
371         return true;
372     default:
373         return false;
374     }
375 }
376 
377 int scsi_sense_to_errno(int key, int asc, int ascq)
378 {
379     switch (key) {
380     case NO_SENSE:
381     case RECOVERED_ERROR:
382     case UNIT_ATTENTION:
383         return EAGAIN;
384     case ABORTED_COMMAND: /* COMMAND ABORTED */
385         return ECANCELED;
386     case NOT_READY:
387     case ILLEGAL_REQUEST:
388     case DATA_PROTECT:
389         /* Parse ASCQ */
390         break;
391     default:
392         return EIO;
393     }
394     switch ((asc << 8) | ascq) {
395     case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
396     case 0x2000: /* INVALID OPERATION CODE */
397     case 0x2400: /* INVALID FIELD IN CDB */
398     case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
399         return EINVAL;
400     case 0x2100: /* LBA OUT OF RANGE */
401     case 0x2707: /* SPACE ALLOC FAILED */
402         return ENOSPC;
403     case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
404         return ENOTSUP;
405     case 0x3a00: /* MEDIUM NOT PRESENT */
406     case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
407     case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
408         return ENOMEDIUM;
409     case 0x2700: /* WRITE PROTECTED */
410         return EACCES;
411     case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
412         return EINPROGRESS;
413     case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
414         return ENOTCONN;
415     default:
416         return EIO;
417     }
418 }
419 
420 int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len)
421 {
422     SCSISense sense;
423     if (in_len < 1) {
424         return EIO;
425     }
426 
427     sense = scsi_parse_sense_buf(in_buf, in_len);
428     return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq);
429 }
430 
431 bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf, size_t in_len)
432 {
433     SCSISense sense;
434     if (in_len < 1) {
435         return false;
436     }
437 
438     sense = scsi_parse_sense_buf(in_buf, in_len);
439     return scsi_sense_is_guest_recoverable(sense.key, sense.asc, sense.ascq);
440 }
441 
442 const char *scsi_command_name(uint8_t cmd)
443 {
444     static const char *names[] = {
445         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
446         [ REWIND                   ] = "REWIND",
447         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
448         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
449         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
450         [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
451         /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
452         [ READ_6                   ] = "READ_6",
453         [ WRITE_6                  ] = "WRITE_6",
454         [ SET_CAPACITY             ] = "SET_CAPACITY",
455         [ READ_REVERSE             ] = "READ_REVERSE",
456         [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
457         [ SPACE                    ] = "SPACE",
458         [ INQUIRY                  ] = "INQUIRY",
459         [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
460         [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
461         [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
462         [ MODE_SELECT              ] = "MODE_SELECT",
463         [ RESERVE                  ] = "RESERVE",
464         [ RELEASE                  ] = "RELEASE",
465         [ COPY                     ] = "COPY",
466         [ ERASE                    ] = "ERASE",
467         [ MODE_SENSE               ] = "MODE_SENSE",
468         [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
469         /* LOAD_UNLOAD and START_STOP use the same operation code */
470         [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
471         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
472         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
473         [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
474         [ READ_10                  ] = "READ_10",
475         [ WRITE_10                 ] = "WRITE_10",
476         [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
477         /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
478         [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
479         [ VERIFY_10                ] = "VERIFY_10",
480         [ SEARCH_HIGH              ] = "SEARCH_HIGH",
481         [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
482         [ SEARCH_LOW               ] = "SEARCH_LOW",
483         [ SET_LIMITS               ] = "SET_LIMITS",
484         [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
485         /* READ_POSITION and PRE_FETCH use the same operation code */
486         [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
487         [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
488         [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
489         /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
490         [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
491         [ COMPARE                  ] = "COMPARE",
492         [ COPY_VERIFY              ] = "COPY_VERIFY",
493         [ WRITE_BUFFER             ] = "WRITE_BUFFER",
494         [ READ_BUFFER              ] = "READ_BUFFER",
495         [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
496         [ READ_LONG_10             ] = "READ_LONG_10",
497         [ WRITE_LONG_10            ] = "WRITE_LONG_10",
498         [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
499         [ WRITE_SAME_10            ] = "WRITE_SAME_10",
500         [ UNMAP                    ] = "UNMAP",
501         [ READ_TOC                 ] = "READ_TOC",
502         [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
503         [ SANITIZE                 ] = "SANITIZE",
504         [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
505         [ LOG_SELECT               ] = "LOG_SELECT",
506         [ LOG_SENSE                ] = "LOG_SENSE",
507         [ MODE_SELECT_10           ] = "MODE_SELECT_10",
508         [ RESERVE_10               ] = "RESERVE_10",
509         [ RELEASE_10               ] = "RELEASE_10",
510         [ MODE_SENSE_10            ] = "MODE_SENSE_10",
511         [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
512         [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
513         [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
514         [ EXTENDED_COPY            ] = "EXTENDED_COPY",
515         [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
516         [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
517         [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
518         [ READ_16                  ] = "READ_16",
519         [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
520         [ WRITE_16                 ] = "WRITE_16",
521         [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
522         [ VERIFY_16                ] = "VERIFY_16",
523         [ PRE_FETCH_16             ] = "PRE_FETCH_16",
524         [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
525         /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
526         [ LOCATE_16                ] = "LOCATE_16",
527         [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
528         /* ERASE_16 and WRITE_SAME_16 use the same operation code */
529         [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
530         [ WRITE_LONG_16            ] = "WRITE_LONG_16",
531         [ REPORT_LUNS              ] = "REPORT_LUNS",
532         [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
533         [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
534         [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
535         [ READ_12                  ] = "READ_12",
536         [ WRITE_12                 ] = "WRITE_12",
537         [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
538         /* ERASE_12 and GET_PERFORMANCE use the same operation code */
539         [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
540         [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
541         [ VERIFY_12                ] = "VERIFY_12",
542         [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
543         [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
544         [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
545         [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
546         [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
547         /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
548         [ READ_CD                  ] = "READ_CD",
549         [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
550         [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
551         [ RESERVE_TRACK            ] = "RESERVE_TRACK",
552         [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
553         [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
554         [ SET_CD_SPEED             ] = "SET_CD_SPEED",
555         [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
556         [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
557         [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
558         [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
559         [ READ_DISC_INFORMATION    ] = "READ_DISC_INFORMATION",
560     };
561 
562     if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
563         return "*UNKNOWN*";
564     }
565     return names[cmd];
566 }
567 
568 int scsi_sense_from_errno(int errno_value, SCSISense *sense)
569 {
570     switch (errno_value) {
571     case 0:
572         return GOOD;
573     case EDOM:
574         return TASK_SET_FULL;
575 #ifdef CONFIG_LINUX
576         /* These errno mapping are specific to Linux.  For more information:
577          * - scsi_decide_disposition in drivers/scsi/scsi_error.c
578          * - scsi_result_to_blk_status in drivers/scsi/scsi_lib.c
579          * - blk_errors[] in block/blk-core.c
580          */
581     case EBADE:
582         return RESERVATION_CONFLICT;
583     case ENODATA:
584         *sense = SENSE_CODE(READ_ERROR);
585         return CHECK_CONDITION;
586     case EREMOTEIO:
587         *sense = SENSE_CODE(LUN_COMM_FAILURE);
588         return CHECK_CONDITION;
589 #endif
590     case ENOMEDIUM:
591         *sense = SENSE_CODE(NO_MEDIUM);
592         return CHECK_CONDITION;
593     case ENOMEM:
594         *sense = SENSE_CODE(TARGET_FAILURE);
595         return CHECK_CONDITION;
596     case EINVAL:
597         *sense = SENSE_CODE(INVALID_FIELD);
598         return CHECK_CONDITION;
599     case ENOSPC:
600         *sense = SENSE_CODE(SPACE_ALLOC_FAILED);
601         return CHECK_CONDITION;
602     default:
603         *sense = SENSE_CODE(IO_ERROR);
604         return CHECK_CONDITION;
605     }
606 }
607 
608 #ifdef CONFIG_LINUX
609 int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
610                            SCSISense *sense)
611 {
612     if (errno_value != 0) {
613         return scsi_sense_from_errno(errno_value, sense);
614     } else {
615         if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
616             io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
617             io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
618             (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
619             return BUSY;
620         } else if (io_hdr->host_status) {
621             *sense = SENSE_CODE(I_T_NEXUS_LOSS);
622             return CHECK_CONDITION;
623         } else if (io_hdr->status) {
624             return io_hdr->status;
625         } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
626             return CHECK_CONDITION;
627         } else {
628             return GOOD;
629         }
630     }
631 }
632 #endif
633