sii3112: Remove unneeded exit function
[qemu.git] / scsi / utils.c
blob87385229554b175e6bbc4994f8b918a74f659d24
1 /*
2 * SCSI helpers
4 * Copyright 2017 Red Hat, Inc.
6 * Authors:
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)
13 * any later version.
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) {
24 return 256;
25 } else {
26 return scsi_cdb_xfer(buf);
30 uint32_t scsi_cdb_xfer(uint8_t *buf)
32 switch (buf[0] >> 5) {
33 case 0:
34 return buf[4];
35 break;
36 case 1:
37 case 2:
38 return lduw_be_p(&buf[7]);
39 break;
40 case 4:
41 return ldl_be_p(&buf[10]) & 0xffffffffULL;
42 break;
43 case 5:
44 return ldl_be_p(&buf[6]) & 0xffffffffULL;
45 break;
46 default:
47 return -1;
51 uint64_t scsi_cmd_lba(SCSICommand *cmd)
53 uint8_t *buf = cmd->buf;
54 uint64_t lba;
56 switch (buf[0] >> 5) {
57 case 0:
58 lba = ldl_be_p(&buf[0]) & 0x1fffff;
59 break;
60 case 1:
61 case 2:
62 case 5:
63 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
64 break;
65 case 4:
66 lba = ldq_be_p(&buf[2]);
67 break;
68 default:
69 lba = -1;
72 return lba;
75 int scsi_cdb_length(uint8_t *buf)
77 int cdb_len;
79 switch (buf[0] >> 5) {
80 case 0:
81 cdb_len = 6;
82 break;
83 case 1:
84 case 2:
85 cdb_len = 10;
86 break;
87 case 4:
88 cdb_len = 16;
89 break;
90 case 5:
91 cdb_len = 12;
92 break;
93 default:
94 cdb_len = -1;
96 return cdb_len;
99 SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len)
101 bool fixed_in;
102 SCSISense sense;
104 assert(in_len > 0);
105 fixed_in = (in_buf[0] & 2) == 0;
106 if (fixed_in) {
107 if (in_len < 14) {
108 return SENSE_CODE(IO_ERROR);
110 sense.key = in_buf[2];
111 sense.asc = in_buf[12];
112 sense.ascq = in_buf[13];
113 } else {
114 if (in_len < 4) {
115 return SENSE_CODE(IO_ERROR);
117 sense.key = in_buf[1];
118 sense.asc = in_buf[2];
119 sense.ascq = in_buf[3];
122 return sense;
125 int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense,
126 bool fixed_sense)
128 int len;
129 uint8_t buf[SCSI_SENSE_LEN] = { 0 };
131 if (fixed_sense) {
132 buf[0] = 0x70;
133 buf[2] = sense.key;
134 buf[7] = 10;
135 buf[12] = sense.asc;
136 buf[13] = sense.ascq;
137 len = 18;
138 } else {
139 buf[0] = 0x72;
140 buf[1] = sense.key;
141 buf[2] = sense.asc;
142 buf[3] = sense.ascq;
143 len = 8;
145 len = MIN(len, size);
146 memcpy(out_buf, buf, len);
147 return 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
315 * scsi_convert_sense
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)
322 SCSISense sense;
323 bool fixed_in;
325 if (in_len == 0) {
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);
333 } else {
334 sense = scsi_parse_sense_buf(in_buf, in_len);
335 return scsi_build_sense_buf(buf, len, sense, fixed);
339 int scsi_sense_to_errno(int key, int asc, int ascq)
341 switch (key) {
342 case NO_SENSE:
343 case RECOVERED_ERROR:
344 case UNIT_ATTENTION:
345 /* These sense keys are not errors */
346 return 0;
347 case ABORTED_COMMAND: /* COMMAND ABORTED */
348 return ECANCELED;
349 case NOT_READY:
350 case ILLEGAL_REQUEST:
351 case DATA_PROTECT:
352 /* Parse ASCQ */
353 break;
354 default:
355 return EIO;
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 0x2600: /* INVALID FIELD IN PARAMETER LIST */
362 return EINVAL;
363 case 0x2100: /* LBA OUT OF RANGE */
364 case 0x2707: /* SPACE ALLOC FAILED */
365 return ENOSPC;
366 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
367 return ENOTSUP;
368 case 0x3a00: /* MEDIUM NOT PRESENT */
369 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
370 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
371 return ENOMEDIUM;
372 case 0x2700: /* WRITE PROTECTED */
373 return EACCES;
374 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
375 return EAGAIN;
376 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
377 return ENOTCONN;
378 default:
379 return EIO;
383 int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len)
385 SCSISense sense;
386 if (in_len < 1) {
387 return EIO;
390 sense = scsi_parse_sense_buf(in_buf, in_len);
391 return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq);
394 const char *scsi_command_name(uint8_t cmd)
396 static const char *names[] = {
397 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
398 [ REWIND ] = "REWIND",
399 [ REQUEST_SENSE ] = "REQUEST_SENSE",
400 [ FORMAT_UNIT ] = "FORMAT_UNIT",
401 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
402 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
403 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
404 [ READ_6 ] = "READ_6",
405 [ WRITE_6 ] = "WRITE_6",
406 [ SET_CAPACITY ] = "SET_CAPACITY",
407 [ READ_REVERSE ] = "READ_REVERSE",
408 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
409 [ SPACE ] = "SPACE",
410 [ INQUIRY ] = "INQUIRY",
411 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
412 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
413 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
414 [ MODE_SELECT ] = "MODE_SELECT",
415 [ RESERVE ] = "RESERVE",
416 [ RELEASE ] = "RELEASE",
417 [ COPY ] = "COPY",
418 [ ERASE ] = "ERASE",
419 [ MODE_SENSE ] = "MODE_SENSE",
420 [ START_STOP ] = "START_STOP/LOAD_UNLOAD",
421 /* LOAD_UNLOAD and START_STOP use the same operation code */
422 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
423 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
424 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
425 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
426 [ READ_10 ] = "READ_10",
427 [ WRITE_10 ] = "WRITE_10",
428 [ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT",
429 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
430 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
431 [ VERIFY_10 ] = "VERIFY_10",
432 [ SEARCH_HIGH ] = "SEARCH_HIGH",
433 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
434 [ SEARCH_LOW ] = "SEARCH_LOW",
435 [ SET_LIMITS ] = "SET_LIMITS",
436 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
437 /* READ_POSITION and PRE_FETCH use the same operation code */
438 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
439 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
440 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
441 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
442 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
443 [ COMPARE ] = "COMPARE",
444 [ COPY_VERIFY ] = "COPY_VERIFY",
445 [ WRITE_BUFFER ] = "WRITE_BUFFER",
446 [ READ_BUFFER ] = "READ_BUFFER",
447 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
448 [ READ_LONG_10 ] = "READ_LONG_10",
449 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
450 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
451 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
452 [ UNMAP ] = "UNMAP",
453 [ READ_TOC ] = "READ_TOC",
454 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
455 [ SANITIZE ] = "SANITIZE",
456 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
457 [ LOG_SELECT ] = "LOG_SELECT",
458 [ LOG_SENSE ] = "LOG_SENSE",
459 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
460 [ RESERVE_10 ] = "RESERVE_10",
461 [ RELEASE_10 ] = "RELEASE_10",
462 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
463 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
464 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
465 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
466 [ EXTENDED_COPY ] = "EXTENDED_COPY",
467 [ ATA_PASSTHROUGH_16 ] = "ATA_PASSTHROUGH_16",
468 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
469 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
470 [ READ_16 ] = "READ_16",
471 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
472 [ WRITE_16 ] = "WRITE_16",
473 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
474 [ VERIFY_16 ] = "VERIFY_16",
475 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
476 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
477 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
478 [ LOCATE_16 ] = "LOCATE_16",
479 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
480 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
481 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
482 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
483 [ REPORT_LUNS ] = "REPORT_LUNS",
484 [ ATA_PASSTHROUGH_12 ] = "BLANK/ATA_PASSTHROUGH_12",
485 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
486 [ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM",
487 [ READ_12 ] = "READ_12",
488 [ WRITE_12 ] = "WRITE_12",
489 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
490 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
491 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
492 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
493 [ VERIFY_12 ] = "VERIFY_12",
494 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
495 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
496 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
497 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
498 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
499 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
500 [ READ_CD ] = "READ_CD",
501 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
502 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
503 [ RESERVE_TRACK ] = "RESERVE_TRACK",
504 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
505 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
506 [ SET_CD_SPEED ] = "SET_CD_SPEED",
507 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
508 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
509 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
510 [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
511 [ READ_DISC_INFORMATION ] = "READ_DISC_INFORMATION",
514 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
515 return "*UNKNOWN*";
517 return names[cmd];
520 #ifdef CONFIG_LINUX
521 int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
522 SCSISense *sense)
524 if (errno_value != 0) {
525 switch (errno_value) {
526 case EDOM:
527 return TASK_SET_FULL;
528 case ENOMEM:
529 *sense = SENSE_CODE(TARGET_FAILURE);
530 return CHECK_CONDITION;
531 default:
532 *sense = SENSE_CODE(IO_ERROR);
533 return CHECK_CONDITION;
535 } else {
536 if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
537 io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
538 io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
539 (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
540 return BUSY;
541 } else if (io_hdr->host_status) {
542 *sense = SENSE_CODE(I_T_NEXUS_LOSS);
543 return CHECK_CONDITION;
544 } else if (io_hdr->status) {
545 return io_hdr->status;
546 } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
547 return CHECK_CONDITION;
548 } else {
549 return GOOD;
553 #endif