4 * Copyright 2017 Red Hat, Inc.
7 * Fam Zheng <famz@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
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)
16 #include "qemu/osdep.h"
17 #include "scsi/constants.h"
18 #include "scsi/utils.h"
19 #include "qemu/bswap.h"
21 uint32_t scsi_data_cdb_xfer(uint8_t *buf
)
23 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
26 return scsi_cdb_xfer(buf
);
30 uint32_t scsi_cdb_xfer(uint8_t *buf
)
32 switch (buf
[0] >> 5) {
37 return lduw_be_p(&buf
[7]);
39 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
41 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
47 uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
49 uint8_t *buf
= cmd
->buf
;
52 switch (buf
[0] >> 5) {
54 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
59 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
62 lba
= ldq_be_p(&buf
[2]);
71 int scsi_cdb_length(uint8_t *buf
)
75 switch (buf
[0] >> 5) {
95 SCSISense
scsi_parse_sense_buf(const uint8_t *in_buf
, int in_len
)
101 fixed_in
= (in_buf
[0] & 2) == 0;
104 return SENSE_CODE(IO_ERROR
);
106 sense
.key
= in_buf
[2];
107 sense
.asc
= in_buf
[12];
108 sense
.ascq
= in_buf
[13];
111 return SENSE_CODE(IO_ERROR
);
113 sense
.key
= in_buf
[1];
114 sense
.asc
= in_buf
[2];
115 sense
.ascq
= in_buf
[3];
121 int scsi_build_sense_buf(uint8_t *out_buf
, size_t size
, SCSISense sense
,
125 uint8_t buf
[SCSI_SENSE_LEN
] = { 0 };
132 buf
[13] = sense
.ascq
;
141 len
= MIN(len
, size
);
142 memcpy(out_buf
, buf
, len
);
146 int scsi_build_sense(uint8_t *buf
, SCSISense sense
)
148 return scsi_build_sense_buf(buf
, SCSI_SENSE_LEN
, sense
, true);
152 * Predefined sense codes
155 /* No sense data available */
156 const struct SCSISense sense_code_NO_SENSE
= {
157 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
160 /* LUN not ready, Manual intervention required */
161 const struct SCSISense sense_code_LUN_NOT_READY
= {
162 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
165 /* LUN not ready, Medium not present */
166 const struct SCSISense sense_code_NO_MEDIUM
= {
167 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
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
175 /* Hardware error, internal target failure */
176 const struct SCSISense sense_code_TARGET_FAILURE
= {
177 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
180 /* Illegal request, invalid command operation code */
181 const struct SCSISense sense_code_INVALID_OPCODE
= {
182 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
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
190 /* Illegal request, Invalid field in CDB */
191 const struct SCSISense sense_code_INVALID_FIELD
= {
192 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
195 /* Illegal request, Invalid field in parameter list */
196 const struct SCSISense sense_code_INVALID_PARAM
= {
197 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
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
205 /* Illegal request, Parameter list length error */
206 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
207 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
210 /* Illegal request, LUN not supported */
211 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
212 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
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
220 /* Illegal request, Incompatible medium installed */
221 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
222 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
225 /* Illegal request, medium removal prevented */
226 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
227 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
230 /* Illegal request, Invalid Transfer Tag */
231 const struct SCSISense sense_code_INVALID_TAG
= {
232 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
235 /* Command aborted, I/O process terminated */
236 const struct SCSISense sense_code_IO_ERROR
= {
237 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
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
245 /* Command aborted, Logical Unit failure */
246 const struct SCSISense sense_code_LUN_FAILURE
= {
247 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
250 /* Command aborted, Overlapped Commands Attempted */
251 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
252 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
255 /* Command aborted, LUN Communication Failure */
256 const struct SCSISense sense_code_LUN_COMM_FAILURE
= {
257 .key
= ABORTED_COMMAND
, .asc
= 0x08, .ascq
= 0x00
260 /* Medium Error, Unrecovered read error */
261 const struct SCSISense sense_code_READ_ERROR
= {
262 .key
= MEDIUM_ERROR
, .asc
= 0x11, .ascq
= 0x00
265 /* Not ready, Cause not reportable */
266 const struct SCSISense sense_code_NOT_READY
= {
267 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x00
270 /* Unit attention, Capacity data has changed */
271 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
272 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
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
280 /* Unit attention, SCSI bus reset */
281 const struct SCSISense sense_code_SCSI_BUS_RESET
= {
282 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x02
285 /* Unit attention, No medium */
286 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
287 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
290 /* Unit attention, Medium may have changed */
291 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
292 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
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
300 /* Unit attention, Device internal reset */
301 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
302 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
305 /* Data Protection, Write Protected */
306 const struct SCSISense sense_code_WRITE_PROTECTED
= {
307 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
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
318 * Convert between fixed and descriptor sense buffers
320 int scsi_convert_sense(uint8_t *in_buf
, int in_len
,
321 uint8_t *buf
, int len
, bool fixed
)
327 return scsi_build_sense_buf(buf
, len
, SENSE_CODE(NO_SENSE
), fixed
);
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
);
335 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
336 return scsi_build_sense_buf(buf
, len
, sense
, fixed
);
340 static bool scsi_sense_is_guest_recoverable(int key
, int asc
, int ascq
)
344 case RECOVERED_ERROR
:
346 case ABORTED_COMMAND
:
349 case ILLEGAL_REQUEST
:
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 */
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 */
369 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
370 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
377 int scsi_sense_to_errno(int key
, int asc
, int ascq
)
381 case RECOVERED_ERROR
:
384 case ABORTED_COMMAND
: /* COMMAND ABORTED */
387 case ILLEGAL_REQUEST
:
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 */
400 case 0x2100: /* LBA OUT OF RANGE */
401 case 0x2707: /* SPACE ALLOC FAILED */
403 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
405 case 0x3a00: /* MEDIUM NOT PRESENT */
406 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
407 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
409 case 0x2700: /* WRITE PROTECTED */
411 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
413 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
420 int scsi_sense_buf_to_errno(const uint8_t *in_buf
, size_t in_len
)
427 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
428 return scsi_sense_to_errno(sense
.key
, sense
.asc
, sense
.ascq
);
431 bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf
, size_t in_len
)
438 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
439 return scsi_sense_is_guest_recoverable(sense
.key
, sense
.asc
, sense
.ascq
);
442 const char *scsi_command_name(uint8_t cmd
)
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",
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",
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",
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",
562 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
) {
569 int sg_io_sense_from_errno(int errno_value
, struct sg_io_hdr
*io_hdr
,
572 if (errno_value
!= 0) {
573 switch (errno_value
) {
575 return TASK_SET_FULL
;
577 *sense
= SENSE_CODE(TARGET_FAILURE
);
578 return CHECK_CONDITION
;
580 *sense
= SENSE_CODE(IO_ERROR
);
581 return CHECK_CONDITION
;
584 if (io_hdr
->host_status
== SG_ERR_DID_NO_CONNECT
||
585 io_hdr
->host_status
== SG_ERR_DID_BUS_BUSY
||
586 io_hdr
->host_status
== SG_ERR_DID_TIME_OUT
||
587 (io_hdr
->driver_status
& SG_ERR_DRIVER_TIMEOUT
)) {
589 } else if (io_hdr
->host_status
) {
590 *sense
= SENSE_CODE(I_T_NEXUS_LOSS
);
591 return CHECK_CONDITION
;
592 } else if (io_hdr
->status
) {
593 return io_hdr
->status
;
594 } else if (io_hdr
->driver_status
& SG_ERR_DRIVER_SENSE
) {
595 return CHECK_CONDITION
;