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, Parameter list length error */
201 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
202 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
205 /* Illegal request, LUN not supported */
206 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
207 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
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
215 /* Illegal request, Incompatible medium installed */
216 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
217 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
220 /* Illegal request, medium removal prevented */
221 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
222 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
225 /* Illegal request, Invalid Transfer Tag */
226 const struct SCSISense sense_code_INVALID_TAG
= {
227 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
230 /* Command aborted, I/O process terminated */
231 const struct SCSISense sense_code_IO_ERROR
= {
232 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
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
240 /* Command aborted, Logical Unit failure */
241 const struct SCSISense sense_code_LUN_FAILURE
= {
242 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
245 /* Command aborted, Overlapped Commands Attempted */
246 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
247 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
250 /* Command aborted, LUN Communication Failure */
251 const struct SCSISense sense_code_LUN_COMM_FAILURE
= {
252 .key
= ABORTED_COMMAND
, .asc
= 0x08, .ascq
= 0x00
255 /* Medium Error, Unrecovered read error */
256 const struct SCSISense sense_code_READ_ERROR
= {
257 .key
= MEDIUM_ERROR
, .asc
= 0x11, .ascq
= 0x00
260 /* Not ready, Cause not reportable */
261 const struct SCSISense sense_code_NOT_READY
= {
262 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x00
265 /* Unit attention, Capacity data has changed */
266 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
267 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
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
275 /* Unit attention, SCSI bus reset */
276 const struct SCSISense sense_code_SCSI_BUS_RESET
= {
277 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x02
280 /* Unit attention, No medium */
281 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
282 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
285 /* Unit attention, Medium may have changed */
286 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
287 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
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
295 /* Unit attention, Device internal reset */
296 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
297 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
300 /* Data Protection, Write Protected */
301 const struct SCSISense sense_code_WRITE_PROTECTED
= {
302 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
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
313 * Convert between fixed and descriptor sense buffers
315 int scsi_convert_sense(uint8_t *in_buf
, int in_len
,
316 uint8_t *buf
, int len
, bool fixed
)
322 return scsi_build_sense_buf(buf
, len
, SENSE_CODE(NO_SENSE
), fixed
);
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
);
330 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
331 return scsi_build_sense_buf(buf
, len
, sense
, fixed
);
335 static bool scsi_sense_is_guest_recoverable(int key
, int asc
, int ascq
)
339 case RECOVERED_ERROR
:
341 case ABORTED_COMMAND
:
344 case ILLEGAL_REQUEST
:
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 */
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 */
364 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
365 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
372 int scsi_sense_to_errno(int key
, int asc
, int ascq
)
376 case RECOVERED_ERROR
:
379 case ABORTED_COMMAND
: /* COMMAND ABORTED */
382 case ILLEGAL_REQUEST
:
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 */
395 case 0x2100: /* LBA OUT OF RANGE */
396 case 0x2707: /* SPACE ALLOC FAILED */
398 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
400 case 0x3a00: /* MEDIUM NOT PRESENT */
401 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
402 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
404 case 0x2700: /* WRITE PROTECTED */
406 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
408 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
415 int scsi_sense_buf_to_errno(const uint8_t *in_buf
, size_t in_len
)
422 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
423 return scsi_sense_to_errno(sense
.key
, sense
.asc
, sense
.ascq
);
426 bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf
, size_t in_len
)
433 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
434 return scsi_sense_is_guest_recoverable(sense
.key
, sense
.asc
, sense
.ascq
);
437 const char *scsi_command_name(uint8_t cmd
)
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",
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",
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",
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",
557 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
) {
564 int sg_io_sense_from_errno(int errno_value
, struct sg_io_hdr
*io_hdr
,
567 if (errno_value
!= 0) {
568 switch (errno_value
) {
570 return TASK_SET_FULL
;
572 *sense
= SENSE_CODE(TARGET_FAILURE
);
573 return CHECK_CONDITION
;
575 *sense
= SENSE_CODE(IO_ERROR
);
576 return CHECK_CONDITION
;
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
)) {
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
;