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 "block/scsi.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 int scsi_build_sense(uint8_t *buf
, SCSISense sense
)
106 buf
[13] = sense
.ascq
;
111 * Predefined sense codes
114 /* No sense data available */
115 const struct SCSISense sense_code_NO_SENSE
= {
116 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
119 /* LUN not ready, Manual intervention required */
120 const struct SCSISense sense_code_LUN_NOT_READY
= {
121 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
124 /* LUN not ready, Medium not present */
125 const struct SCSISense sense_code_NO_MEDIUM
= {
126 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
129 /* LUN not ready, medium removal prevented */
130 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
131 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
134 /* Hardware error, internal target failure */
135 const struct SCSISense sense_code_TARGET_FAILURE
= {
136 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
139 /* Illegal request, invalid command operation code */
140 const struct SCSISense sense_code_INVALID_OPCODE
= {
141 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
144 /* Illegal request, LBA out of range */
145 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
146 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
149 /* Illegal request, Invalid field in CDB */
150 const struct SCSISense sense_code_INVALID_FIELD
= {
151 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
154 /* Illegal request, Invalid field in parameter list */
155 const struct SCSISense sense_code_INVALID_PARAM
= {
156 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
159 /* Illegal request, Parameter list length error */
160 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
161 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
164 /* Illegal request, LUN not supported */
165 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
166 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
169 /* Illegal request, Saving parameters not supported */
170 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
171 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
174 /* Illegal request, Incompatible medium installed */
175 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
176 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
179 /* Illegal request, medium removal prevented */
180 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
181 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
184 /* Illegal request, Invalid Transfer Tag */
185 const struct SCSISense sense_code_INVALID_TAG
= {
186 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
189 /* Command aborted, I/O process terminated */
190 const struct SCSISense sense_code_IO_ERROR
= {
191 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
194 /* Command aborted, I_T Nexus loss occurred */
195 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
196 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
199 /* Command aborted, Logical Unit failure */
200 const struct SCSISense sense_code_LUN_FAILURE
= {
201 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
204 /* Command aborted, Overlapped Commands Attempted */
205 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
206 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
209 /* Unit attention, Capacity data has changed */
210 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
211 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
214 /* Unit attention, Power on, reset or bus device reset occurred */
215 const struct SCSISense sense_code_RESET
= {
216 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
219 /* Unit attention, No medium */
220 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
221 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
224 /* Unit attention, Medium may have changed */
225 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
226 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
229 /* Unit attention, Reported LUNs data has changed */
230 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
231 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
234 /* Unit attention, Device internal reset */
235 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
236 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
239 /* Data Protection, Write Protected */
240 const struct SCSISense sense_code_WRITE_PROTECTED
= {
241 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
244 /* Data Protection, Space Allocation Failed Write Protect */
245 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
246 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
252 * Convert between fixed and descriptor sense buffers
254 int scsi_convert_sense(uint8_t *in_buf
, int in_len
,
255 uint8_t *buf
, int len
, bool fixed
)
259 if (!fixed
&& len
< 8) {
264 sense
.key
= NO_SENSE
;
268 fixed_in
= (in_buf
[0] & 2) == 0;
270 if (fixed
== fixed_in
) {
271 memcpy(buf
, in_buf
, MIN(len
, in_len
));
272 return MIN(len
, in_len
);
276 sense
.key
= in_buf
[2];
277 sense
.asc
= in_buf
[12];
278 sense
.ascq
= in_buf
[13];
280 sense
.key
= in_buf
[1];
281 sense
.asc
= in_buf
[2];
282 sense
.ascq
= in_buf
[3];
288 /* Return fixed format sense buffer */
293 buf
[13] = sense
.ascq
;
294 return MIN(len
, SCSI_SENSE_LEN
);
296 /* Return descriptor format sense buffer */
305 int scsi_sense_to_errno(int key
, int asc
, int ascq
)
308 case 0x00: /* NO SENSE */
309 case 0x01: /* RECOVERED ERROR */
310 case 0x06: /* UNIT ATTENTION */
311 /* These sense keys are not errors */
313 case 0x0b: /* COMMAND ABORTED */
315 case 0x02: /* NOT READY */
316 case 0x05: /* ILLEGAL REQUEST */
317 case 0x07: /* DATA PROTECTION */
323 switch ((asc
<< 8) | ascq
) {
324 case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
325 case 0x2000: /* INVALID OPERATION CODE */
326 case 0x2400: /* INVALID FIELD IN CDB */
327 case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
329 case 0x2100: /* LBA OUT OF RANGE */
330 case 0x2707: /* SPACE ALLOC FAILED */
332 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
334 case 0x3a00: /* MEDIUM NOT PRESENT */
335 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
336 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
338 case 0x2700: /* WRITE PROTECTED */
340 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
342 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
349 int scsi_sense_buf_to_errno(const uint8_t *sense
, size_t sense_size
)
352 if (sense_size
< 1) {
356 case 0x70: /* Fixed format sense data. */
357 if (sense_size
< 14) {
360 key
= sense
[2] & 0xF;
364 case 0x72: /* Descriptor format sense data. */
365 if (sense_size
< 4) {
368 key
= sense
[1] & 0xF;
376 return scsi_sense_to_errno(key
, asc
, ascq
);
379 const char *scsi_command_name(uint8_t cmd
)
381 static const char *names
[] = {
382 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
383 [ REWIND
] = "REWIND",
384 [ REQUEST_SENSE
] = "REQUEST_SENSE",
385 [ FORMAT_UNIT
] = "FORMAT_UNIT",
386 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
387 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
388 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
389 [ READ_6
] = "READ_6",
390 [ WRITE_6
] = "WRITE_6",
391 [ SET_CAPACITY
] = "SET_CAPACITY",
392 [ READ_REVERSE
] = "READ_REVERSE",
393 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
395 [ INQUIRY
] = "INQUIRY",
396 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
397 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
398 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
399 [ MODE_SELECT
] = "MODE_SELECT",
400 [ RESERVE
] = "RESERVE",
401 [ RELEASE
] = "RELEASE",
404 [ MODE_SENSE
] = "MODE_SENSE",
405 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
406 /* LOAD_UNLOAD and START_STOP use the same operation code */
407 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
408 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
409 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
410 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
411 [ READ_10
] = "READ_10",
412 [ WRITE_10
] = "WRITE_10",
413 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
414 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
415 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
416 [ VERIFY_10
] = "VERIFY_10",
417 [ SEARCH_HIGH
] = "SEARCH_HIGH",
418 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
419 [ SEARCH_LOW
] = "SEARCH_LOW",
420 [ SET_LIMITS
] = "SET_LIMITS",
421 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
422 /* READ_POSITION and PRE_FETCH use the same operation code */
423 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
424 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
425 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
426 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
427 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
428 [ COMPARE
] = "COMPARE",
429 [ COPY_VERIFY
] = "COPY_VERIFY",
430 [ WRITE_BUFFER
] = "WRITE_BUFFER",
431 [ READ_BUFFER
] = "READ_BUFFER",
432 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
433 [ READ_LONG_10
] = "READ_LONG_10",
434 [ WRITE_LONG_10
] = "WRITE_LONG_10",
435 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
436 [ WRITE_SAME_10
] = "WRITE_SAME_10",
438 [ READ_TOC
] = "READ_TOC",
439 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
440 [ SANITIZE
] = "SANITIZE",
441 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
442 [ LOG_SELECT
] = "LOG_SELECT",
443 [ LOG_SENSE
] = "LOG_SENSE",
444 [ MODE_SELECT_10
] = "MODE_SELECT_10",
445 [ RESERVE_10
] = "RESERVE_10",
446 [ RELEASE_10
] = "RELEASE_10",
447 [ MODE_SENSE_10
] = "MODE_SENSE_10",
448 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
449 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
450 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
451 [ EXTENDED_COPY
] = "EXTENDED_COPY",
452 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
453 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
454 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
455 [ READ_16
] = "READ_16",
456 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
457 [ WRITE_16
] = "WRITE_16",
458 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
459 [ VERIFY_16
] = "VERIFY_16",
460 [ PRE_FETCH_16
] = "PRE_FETCH_16",
461 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
462 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
463 [ LOCATE_16
] = "LOCATE_16",
464 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
465 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
466 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
467 [ WRITE_LONG_16
] = "WRITE_LONG_16",
468 [ REPORT_LUNS
] = "REPORT_LUNS",
469 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
470 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
471 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
472 [ READ_12
] = "READ_12",
473 [ WRITE_12
] = "WRITE_12",
474 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
475 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
476 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
477 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
478 [ VERIFY_12
] = "VERIFY_12",
479 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
480 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
481 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
482 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
483 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
484 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
485 [ READ_CD
] = "READ_CD",
486 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
487 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
488 [ RESERVE_TRACK
] = "RESERVE_TRACK",
489 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
490 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
491 [ SET_CD_SPEED
] = "SET_CD_SPEED",
492 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
493 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
494 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
495 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
496 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
499 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
) {
506 int sg_io_sense_from_errno(int errno_value
, struct sg_io_hdr
*io_hdr
,
509 if (errno_value
!= 0) {
510 switch (errno_value
) {
512 return TASK_SET_FULL
;
514 *sense
= SENSE_CODE(TARGET_FAILURE
);
515 return CHECK_CONDITION
;
517 *sense
= SENSE_CODE(IO_ERROR
);
518 return CHECK_CONDITION
;
521 if (io_hdr
->host_status
== SG_ERR_DID_NO_CONNECT
||
522 io_hdr
->host_status
== SG_ERR_DID_BUS_BUSY
||
523 io_hdr
->host_status
== SG_ERR_DID_TIME_OUT
||
524 (io_hdr
->driver_status
& SG_ERR_DRIVER_TIMEOUT
)) {
526 } else if (io_hdr
->host_status
) {
527 *sense
= SENSE_CODE(I_T_NEXUS_LOSS
);
528 return CHECK_CONDITION
;
529 } else if (io_hdr
->status
) {
530 return io_hdr
->status
;
531 } else if (io_hdr
->driver_status
& SG_ERR_DRIVER_SENSE
) {
532 return CHECK_CONDITION
;