xref: /openbmc/qemu/scsi/utils.c (revision c51a3f5d)
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, Parameter list length error */
201 const struct SCSISense sense_code_INVALID_PARAM_LEN = {
202     .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
203 };
204 
205 /* Illegal request, LUN not supported */
206 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
207     .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
208 };
209 
210 /* Illegal request, Saving parameters not supported */
211 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
212     .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
213 };
214 
215 /* Illegal request, Incompatible medium installed */
216 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
217     .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
218 };
219 
220 /* Illegal request, medium removal prevented */
221 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
222     .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
223 };
224 
225 /* Illegal request, Invalid Transfer Tag */
226 const struct SCSISense sense_code_INVALID_TAG = {
227     .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
228 };
229 
230 /* Command aborted, I/O process terminated */
231 const struct SCSISense sense_code_IO_ERROR = {
232     .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
233 };
234 
235 /* Command aborted, I_T Nexus loss occurred */
236 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
237     .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
238 };
239 
240 /* Command aborted, Logical Unit failure */
241 const struct SCSISense sense_code_LUN_FAILURE = {
242     .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
243 };
244 
245 /* Command aborted, Overlapped Commands Attempted */
246 const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
247     .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
248 };
249 
250 /* Command aborted, LUN Communication Failure */
251 const struct SCSISense sense_code_LUN_COMM_FAILURE = {
252     .key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
253 };
254 
255 /* Medium Error, Unrecovered read error */
256 const struct SCSISense sense_code_READ_ERROR = {
257     .key = MEDIUM_ERROR, .asc = 0x11, .ascq = 0x00
258 };
259 
260 /* Not ready, Cause not reportable */
261 const struct SCSISense sense_code_NOT_READY = {
262     .key = NOT_READY, .asc = 0x04, .ascq = 0x00
263 };
264 
265 /* Unit attention, Capacity data has changed */
266 const struct SCSISense sense_code_CAPACITY_CHANGED = {
267     .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
268 };
269 
270 /* Unit attention, Power on, reset or bus device reset occurred */
271 const struct SCSISense sense_code_RESET = {
272     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
273 };
274 
275 /* Unit attention, SCSI bus reset */
276 const struct SCSISense sense_code_SCSI_BUS_RESET = {
277     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x02
278 };
279 
280 /* Unit attention, No medium */
281 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
282     .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
283 };
284 
285 /* Unit attention, Medium may have changed */
286 const struct SCSISense sense_code_MEDIUM_CHANGED = {
287     .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
288 };
289 
290 /* Unit attention, Reported LUNs data has changed */
291 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
292     .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
293 };
294 
295 /* Unit attention, Device internal reset */
296 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
297     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
298 };
299 
300 /* Data Protection, Write Protected */
301 const struct SCSISense sense_code_WRITE_PROTECTED = {
302     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
303 };
304 
305 /* Data Protection, Space Allocation Failed Write Protect */
306 const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
307     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
308 };
309 
310 /*
311  * scsi_convert_sense
312  *
313  * Convert between fixed and descriptor sense buffers
314  */
315 int scsi_convert_sense(uint8_t *in_buf, int in_len,
316                        uint8_t *buf, int len, bool fixed)
317 {
318     SCSISense sense;
319     bool fixed_in;
320 
321     if (in_len == 0) {
322         return scsi_build_sense_buf(buf, len, SENSE_CODE(NO_SENSE), fixed);
323     }
324 
325     fixed_in = (in_buf[0] & 2) == 0;
326     if (fixed == fixed_in) {
327         memcpy(buf, in_buf, MIN(len, in_len));
328         return MIN(len, in_len);
329     } else {
330         sense = scsi_parse_sense_buf(in_buf, in_len);
331         return scsi_build_sense_buf(buf, len, sense, fixed);
332     }
333 }
334 
335 static bool scsi_sense_is_guest_recoverable(int key, int asc, int ascq)
336 {
337     switch (key) {
338     case NO_SENSE:
339     case RECOVERED_ERROR:
340     case UNIT_ATTENTION:
341     case ABORTED_COMMAND:
342         return true;
343     case NOT_READY:
344     case ILLEGAL_REQUEST:
345     case DATA_PROTECT:
346         /* Parse ASCQ */
347         break;
348     default:
349         return false;
350     }
351 
352     switch ((asc << 8) | ascq) {
353     case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
354     case 0x2000: /* INVALID OPERATION CODE */
355     case 0x2400: /* INVALID FIELD IN CDB */
356     case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
357     case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
358 
359     case 0x2104: /* UNALIGNED WRITE COMMAND */
360     case 0x2105: /* WRITE BOUNDARY VIOLATION */
361     case 0x2106: /* ATTEMPT TO READ INVALID DATA */
362     case 0x550e: /* INSUFFICIENT ZONE RESOURCES */
363 
364     case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
365     case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
366         return true;
367     default:
368         return false;
369     }
370 }
371 
372 int scsi_sense_to_errno(int key, int asc, int ascq)
373 {
374     switch (key) {
375     case NO_SENSE:
376     case RECOVERED_ERROR:
377     case UNIT_ATTENTION:
378         return EAGAIN;
379     case ABORTED_COMMAND: /* COMMAND ABORTED */
380         return ECANCELED;
381     case NOT_READY:
382     case ILLEGAL_REQUEST:
383     case DATA_PROTECT:
384         /* Parse ASCQ */
385         break;
386     default:
387         return EIO;
388     }
389     switch ((asc << 8) | ascq) {
390     case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
391     case 0x2000: /* INVALID OPERATION CODE */
392     case 0x2400: /* INVALID FIELD IN CDB */
393     case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
394         return EINVAL;
395     case 0x2100: /* LBA OUT OF RANGE */
396     case 0x2707: /* SPACE ALLOC FAILED */
397         return ENOSPC;
398     case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
399         return ENOTSUP;
400     case 0x3a00: /* MEDIUM NOT PRESENT */
401     case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
402     case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
403         return ENOMEDIUM;
404     case 0x2700: /* WRITE PROTECTED */
405         return EACCES;
406     case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
407         return EINPROGRESS;
408     case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
409         return ENOTCONN;
410     default:
411         return EIO;
412     }
413 }
414 
415 int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len)
416 {
417     SCSISense sense;
418     if (in_len < 1) {
419         return EIO;
420     }
421 
422     sense = scsi_parse_sense_buf(in_buf, in_len);
423     return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq);
424 }
425 
426 bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf, size_t in_len)
427 {
428     SCSISense sense;
429     if (in_len < 1) {
430         return false;
431     }
432 
433     sense = scsi_parse_sense_buf(in_buf, in_len);
434     return scsi_sense_is_guest_recoverable(sense.key, sense.asc, sense.ascq);
435 }
436 
437 const char *scsi_command_name(uint8_t cmd)
438 {
439     static const char *names[] = {
440         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
441         [ REWIND                   ] = "REWIND",
442         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
443         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
444         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
445         [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
446         /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
447         [ READ_6                   ] = "READ_6",
448         [ WRITE_6                  ] = "WRITE_6",
449         [ SET_CAPACITY             ] = "SET_CAPACITY",
450         [ READ_REVERSE             ] = "READ_REVERSE",
451         [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
452         [ SPACE                    ] = "SPACE",
453         [ INQUIRY                  ] = "INQUIRY",
454         [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
455         [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
456         [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
457         [ MODE_SELECT              ] = "MODE_SELECT",
458         [ RESERVE                  ] = "RESERVE",
459         [ RELEASE                  ] = "RELEASE",
460         [ COPY                     ] = "COPY",
461         [ ERASE                    ] = "ERASE",
462         [ MODE_SENSE               ] = "MODE_SENSE",
463         [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
464         /* LOAD_UNLOAD and START_STOP use the same operation code */
465         [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
466         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
467         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
468         [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
469         [ READ_10                  ] = "READ_10",
470         [ WRITE_10                 ] = "WRITE_10",
471         [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
472         /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
473         [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
474         [ VERIFY_10                ] = "VERIFY_10",
475         [ SEARCH_HIGH              ] = "SEARCH_HIGH",
476         [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
477         [ SEARCH_LOW               ] = "SEARCH_LOW",
478         [ SET_LIMITS               ] = "SET_LIMITS",
479         [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
480         /* READ_POSITION and PRE_FETCH use the same operation code */
481         [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
482         [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
483         [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
484         /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
485         [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
486         [ COMPARE                  ] = "COMPARE",
487         [ COPY_VERIFY              ] = "COPY_VERIFY",
488         [ WRITE_BUFFER             ] = "WRITE_BUFFER",
489         [ READ_BUFFER              ] = "READ_BUFFER",
490         [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
491         [ READ_LONG_10             ] = "READ_LONG_10",
492         [ WRITE_LONG_10            ] = "WRITE_LONG_10",
493         [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
494         [ WRITE_SAME_10            ] = "WRITE_SAME_10",
495         [ UNMAP                    ] = "UNMAP",
496         [ READ_TOC                 ] = "READ_TOC",
497         [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
498         [ SANITIZE                 ] = "SANITIZE",
499         [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
500         [ LOG_SELECT               ] = "LOG_SELECT",
501         [ LOG_SENSE                ] = "LOG_SENSE",
502         [ MODE_SELECT_10           ] = "MODE_SELECT_10",
503         [ RESERVE_10               ] = "RESERVE_10",
504         [ RELEASE_10               ] = "RELEASE_10",
505         [ MODE_SENSE_10            ] = "MODE_SENSE_10",
506         [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
507         [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
508         [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
509         [ EXTENDED_COPY            ] = "EXTENDED_COPY",
510         [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
511         [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
512         [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
513         [ READ_16                  ] = "READ_16",
514         [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
515         [ WRITE_16                 ] = "WRITE_16",
516         [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
517         [ VERIFY_16                ] = "VERIFY_16",
518         [ PRE_FETCH_16             ] = "PRE_FETCH_16",
519         [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
520         /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
521         [ LOCATE_16                ] = "LOCATE_16",
522         [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
523         /* ERASE_16 and WRITE_SAME_16 use the same operation code */
524         [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
525         [ WRITE_LONG_16            ] = "WRITE_LONG_16",
526         [ REPORT_LUNS              ] = "REPORT_LUNS",
527         [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
528         [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
529         [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
530         [ READ_12                  ] = "READ_12",
531         [ WRITE_12                 ] = "WRITE_12",
532         [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
533         /* ERASE_12 and GET_PERFORMANCE use the same operation code */
534         [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
535         [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
536         [ VERIFY_12                ] = "VERIFY_12",
537         [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
538         [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
539         [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
540         [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
541         [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
542         /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
543         [ READ_CD                  ] = "READ_CD",
544         [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
545         [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
546         [ RESERVE_TRACK            ] = "RESERVE_TRACK",
547         [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
548         [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
549         [ SET_CD_SPEED             ] = "SET_CD_SPEED",
550         [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
551         [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
552         [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
553         [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
554         [ READ_DISC_INFORMATION    ] = "READ_DISC_INFORMATION",
555     };
556 
557     if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
558         return "*UNKNOWN*";
559     }
560     return names[cmd];
561 }
562 
563 #ifdef CONFIG_LINUX
564 int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
565                            SCSISense *sense)
566 {
567     if (errno_value != 0) {
568         switch (errno_value) {
569         case EDOM:
570             return TASK_SET_FULL;
571         case ENOMEM:
572             *sense = SENSE_CODE(TARGET_FAILURE);
573             return CHECK_CONDITION;
574         default:
575             *sense = SENSE_CODE(IO_ERROR);
576             return CHECK_CONDITION;
577         }
578     } else {
579         if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
580             io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
581             io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
582             (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
583             return BUSY;
584         } else if (io_hdr->host_status) {
585             *sense = SENSE_CODE(I_T_NEXUS_LOSS);
586             return CHECK_CONDITION;
587         } else if (io_hdr->status) {
588             return io_hdr->status;
589         } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
590             return CHECK_CONDITION;
591         } else {
592             return GOOD;
593         }
594     }
595 }
596 #endif
597