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) {
38 return lduw_be_p(&buf
[7]);
41 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
44 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
51 uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
53 uint8_t *buf
= cmd
->buf
;
56 switch (buf
[0] >> 5) {
58 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
63 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
66 lba
= ldq_be_p(&buf
[2]);
75 int scsi_cdb_length(uint8_t *buf
)
79 switch (buf
[0] >> 5) {
99 SCSISense
scsi_parse_sense_buf(const uint8_t *in_buf
, int in_len
)
105 fixed_in
= (in_buf
[0] & 2) == 0;
108 return SENSE_CODE(IO_ERROR
);
110 sense
.key
= in_buf
[2];
111 sense
.asc
= in_buf
[12];
112 sense
.ascq
= in_buf
[13];
115 return SENSE_CODE(IO_ERROR
);
117 sense
.key
= in_buf
[1];
118 sense
.asc
= in_buf
[2];
119 sense
.ascq
= in_buf
[3];
125 int scsi_build_sense_buf(uint8_t *out_buf
, size_t size
, SCSISense sense
,
129 uint8_t buf
[SCSI_SENSE_LEN
] = { 0 };
136 buf
[13] = sense
.ascq
;
145 len
= MIN(len
, size
);
146 memcpy(out_buf
, buf
, len
);
150 int scsi_build_sense(uint8_t *buf
, SCSISense sense
)
152 return scsi_build_sense_buf(buf
, SCSI_SENSE_LEN
, sense
, true);
156 * Predefined sense codes
159 /* No sense data available */
160 const struct SCSISense sense_code_NO_SENSE
= {
161 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
164 /* LUN not ready, Manual intervention required */
165 const struct SCSISense sense_code_LUN_NOT_READY
= {
166 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
169 /* LUN not ready, Medium not present */
170 const struct SCSISense sense_code_NO_MEDIUM
= {
171 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
174 /* LUN not ready, medium removal prevented */
175 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
176 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
179 /* Hardware error, internal target failure */
180 const struct SCSISense sense_code_TARGET_FAILURE
= {
181 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
184 /* Illegal request, invalid command operation code */
185 const struct SCSISense sense_code_INVALID_OPCODE
= {
186 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
189 /* Illegal request, LBA out of range */
190 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
191 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
194 /* Illegal request, Invalid field in CDB */
195 const struct SCSISense sense_code_INVALID_FIELD
= {
196 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
199 /* Illegal request, Invalid field in parameter list */
200 const struct SCSISense sense_code_INVALID_PARAM
= {
201 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
204 /* Illegal request, Parameter list length error */
205 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
206 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
209 /* Illegal request, LUN not supported */
210 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
211 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
214 /* Illegal request, Saving parameters not supported */
215 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
216 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
219 /* Illegal request, Incompatible medium installed */
220 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
221 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
224 /* Illegal request, medium removal prevented */
225 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
226 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
229 /* Illegal request, Invalid Transfer Tag */
230 const struct SCSISense sense_code_INVALID_TAG
= {
231 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
234 /* Command aborted, I/O process terminated */
235 const struct SCSISense sense_code_IO_ERROR
= {
236 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
239 /* Command aborted, I_T Nexus loss occurred */
240 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
241 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
244 /* Command aborted, Logical Unit failure */
245 const struct SCSISense sense_code_LUN_FAILURE
= {
246 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
249 /* Command aborted, Overlapped Commands Attempted */
250 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
251 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
254 /* Command aborted, LUN Communication Failure */
255 const struct SCSISense sense_code_LUN_COMM_FAILURE
= {
256 .key
= ABORTED_COMMAND
, .asc
= 0x08, .ascq
= 0x00
259 /* Medium Error, Unrecovered read error */
260 const struct SCSISense sense_code_READ_ERROR
= {
261 .key
= MEDIUM_ERROR
, .asc
= 0x11, .ascq
= 0x00
264 /* Not ready, Cause not reportable */
265 const struct SCSISense sense_code_NOT_READY
= {
266 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x00
269 /* Unit attention, Capacity data has changed */
270 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
271 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
274 /* Unit attention, Power on, reset or bus device reset occurred */
275 const struct SCSISense sense_code_RESET
= {
276 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
279 /* Unit attention, SCSI bus reset */
280 const struct SCSISense sense_code_SCSI_BUS_RESET
= {
281 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x02
284 /* Unit attention, No medium */
285 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
286 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
289 /* Unit attention, Medium may have changed */
290 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
291 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
294 /* Unit attention, Reported LUNs data has changed */
295 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
296 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
299 /* Unit attention, Device internal reset */
300 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
301 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
304 /* Data Protection, Write Protected */
305 const struct SCSISense sense_code_WRITE_PROTECTED
= {
306 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
309 /* Data Protection, Space Allocation Failed Write Protect */
310 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
311 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
317 * Convert between fixed and descriptor sense buffers
319 int scsi_convert_sense(uint8_t *in_buf
, int in_len
,
320 uint8_t *buf
, int len
, bool fixed
)
326 return scsi_build_sense_buf(buf
, len
, SENSE_CODE(NO_SENSE
), fixed
);
329 fixed_in
= (in_buf
[0] & 2) == 0;
330 if (fixed
== fixed_in
) {
331 memcpy(buf
, in_buf
, MIN(len
, in_len
));
332 return MIN(len
, in_len
);
334 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
335 return scsi_build_sense_buf(buf
, len
, sense
, fixed
);
339 static bool scsi_sense_is_guest_recoverable(int key
, int asc
, int ascq
)
343 case RECOVERED_ERROR
:
345 case ABORTED_COMMAND
:
348 case ILLEGAL_REQUEST
:
356 switch ((asc
<< 8) | ascq
) {
357 case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
358 case 0x2000: /* INVALID OPERATION CODE */
359 case 0x2400: /* INVALID FIELD IN CDB */
360 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
361 case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
363 case 0x2104: /* UNALIGNED WRITE COMMAND */
364 case 0x2105: /* WRITE BOUNDARY VIOLATION */
365 case 0x2106: /* ATTEMPT TO READ INVALID DATA */
366 case 0x550e: /* INSUFFICIENT ZONE RESOURCES */
368 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
369 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
376 int scsi_sense_to_errno(int key
, int asc
, int ascq
)
380 case RECOVERED_ERROR
:
383 case ABORTED_COMMAND
: /* COMMAND ABORTED */
386 case ILLEGAL_REQUEST
:
393 switch ((asc
<< 8) | ascq
) {
394 case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
395 case 0x2000: /* INVALID OPERATION CODE */
396 case 0x2400: /* INVALID FIELD IN CDB */
397 case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
399 case 0x2100: /* LBA OUT OF RANGE */
400 case 0x2707: /* SPACE ALLOC FAILED */
402 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
404 case 0x3a00: /* MEDIUM NOT PRESENT */
405 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
406 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
408 case 0x2700: /* WRITE PROTECTED */
410 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
412 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
419 int scsi_sense_buf_to_errno(const uint8_t *in_buf
, size_t in_len
)
426 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
427 return scsi_sense_to_errno(sense
.key
, sense
.asc
, sense
.ascq
);
430 bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf
, size_t in_len
)
437 sense
= scsi_parse_sense_buf(in_buf
, in_len
);
438 return scsi_sense_is_guest_recoverable(sense
.key
, sense
.asc
, sense
.ascq
);
441 const char *scsi_command_name(uint8_t cmd
)
443 static const char *names
[] = {
444 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
445 [ REWIND
] = "REWIND",
446 [ REQUEST_SENSE
] = "REQUEST_SENSE",
447 [ FORMAT_UNIT
] = "FORMAT_UNIT",
448 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
449 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
450 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
451 [ READ_6
] = "READ_6",
452 [ WRITE_6
] = "WRITE_6",
453 [ SET_CAPACITY
] = "SET_CAPACITY",
454 [ READ_REVERSE
] = "READ_REVERSE",
455 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
457 [ INQUIRY
] = "INQUIRY",
458 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
459 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
460 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
461 [ MODE_SELECT
] = "MODE_SELECT",
462 [ RESERVE
] = "RESERVE",
463 [ RELEASE
] = "RELEASE",
466 [ MODE_SENSE
] = "MODE_SENSE",
467 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
468 /* LOAD_UNLOAD and START_STOP use the same operation code */
469 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
470 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
471 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
472 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
473 [ READ_10
] = "READ_10",
474 [ WRITE_10
] = "WRITE_10",
475 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
476 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
477 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
478 [ VERIFY_10
] = "VERIFY_10",
479 [ SEARCH_HIGH
] = "SEARCH_HIGH",
480 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
481 [ SEARCH_LOW
] = "SEARCH_LOW",
482 [ SET_LIMITS
] = "SET_LIMITS",
483 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
484 /* READ_POSITION and PRE_FETCH use the same operation code */
485 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
486 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
487 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
488 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
489 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
490 [ COMPARE
] = "COMPARE",
491 [ COPY_VERIFY
] = "COPY_VERIFY",
492 [ WRITE_BUFFER
] = "WRITE_BUFFER",
493 [ READ_BUFFER
] = "READ_BUFFER",
494 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
495 [ READ_LONG_10
] = "READ_LONG_10",
496 [ WRITE_LONG_10
] = "WRITE_LONG_10",
497 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
498 [ WRITE_SAME_10
] = "WRITE_SAME_10",
500 [ READ_TOC
] = "READ_TOC",
501 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
502 [ SANITIZE
] = "SANITIZE",
503 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
504 [ LOG_SELECT
] = "LOG_SELECT",
505 [ LOG_SENSE
] = "LOG_SENSE",
506 [ MODE_SELECT_10
] = "MODE_SELECT_10",
507 [ RESERVE_10
] = "RESERVE_10",
508 [ RELEASE_10
] = "RELEASE_10",
509 [ MODE_SENSE_10
] = "MODE_SENSE_10",
510 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
511 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
512 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
513 [ EXTENDED_COPY
] = "EXTENDED_COPY",
514 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
515 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
516 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
517 [ READ_16
] = "READ_16",
518 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
519 [ WRITE_16
] = "WRITE_16",
520 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
521 [ VERIFY_16
] = "VERIFY_16",
522 [ PRE_FETCH_16
] = "PRE_FETCH_16",
523 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
524 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
525 [ LOCATE_16
] = "LOCATE_16",
526 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
527 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
528 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
529 [ WRITE_LONG_16
] = "WRITE_LONG_16",
530 [ REPORT_LUNS
] = "REPORT_LUNS",
531 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
532 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
533 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
534 [ READ_12
] = "READ_12",
535 [ WRITE_12
] = "WRITE_12",
536 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
537 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
538 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
539 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
540 [ VERIFY_12
] = "VERIFY_12",
541 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
542 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
543 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
544 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
545 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
546 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
547 [ READ_CD
] = "READ_CD",
548 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
549 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
550 [ RESERVE_TRACK
] = "RESERVE_TRACK",
551 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
552 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
553 [ SET_CD_SPEED
] = "SET_CD_SPEED",
554 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
555 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
556 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
557 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
558 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
561 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
) {
568 int sg_io_sense_from_errno(int errno_value
, struct sg_io_hdr
*io_hdr
,
571 if (errno_value
!= 0) {
572 switch (errno_value
) {
574 return TASK_SET_FULL
;
576 *sense
= SENSE_CODE(TARGET_FAILURE
);
577 return CHECK_CONDITION
;
579 *sense
= SENSE_CODE(IO_ERROR
);
580 return CHECK_CONDITION
;
583 if (io_hdr
->host_status
== SG_ERR_DID_NO_CONNECT
||
584 io_hdr
->host_status
== SG_ERR_DID_BUS_BUSY
||
585 io_hdr
->host_status
== SG_ERR_DID_TIME_OUT
||
586 (io_hdr
->driver_status
& SG_ERR_DRIVER_TIMEOUT
)) {
588 } else if (io_hdr
->host_status
) {
589 *sense
= SENSE_CODE(I_T_NEXUS_LOSS
);
590 return CHECK_CONDITION
;
591 } else if (io_hdr
->status
) {
592 return io_hdr
->status
;
593 } else if (io_hdr
->driver_status
& SG_ERR_DRIVER_SENSE
) {
594 return CHECK_CONDITION
;