1 /* 2 * SCSI helpers 3 * 4 * Copyright 2017 Red Hat, Inc. 5 * 6 * Authors: 7 * Fam Zheng <famz@redhat.com> 8 * Paolo Bonzini <pbonzini@redhat.com> 9 * 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. 14 */ 15 16 #include "qemu/osdep.h" 17 #include "scsi/constants.h" 18 #include "scsi/utils.h" 19 #include "qemu/bswap.h" 20 21 uint32_t scsi_data_cdb_xfer(uint8_t *buf) 22 { 23 if ((buf[0] >> 5) == 0 && buf[4] == 0) { 24 return 256; 25 } else { 26 return scsi_cdb_xfer(buf); 27 } 28 } 29 30 uint32_t scsi_cdb_xfer(uint8_t *buf) 31 { 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; 48 } 49 } 50 51 uint64_t scsi_cmd_lba(SCSICommand *cmd) 52 { 53 uint8_t *buf = cmd->buf; 54 uint64_t lba; 55 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; 70 71 } 72 return lba; 73 } 74 75 int scsi_cdb_length(uint8_t *buf) 76 { 77 int cdb_len; 78 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; 95 } 96 return cdb_len; 97 } 98 99 int scsi_build_sense(uint8_t *buf, SCSISense sense) 100 { 101 memset(buf, 0, 18); 102 buf[0] = 0x70; 103 buf[2] = sense.key; 104 buf[7] = 10; 105 buf[12] = sense.asc; 106 buf[13] = sense.ascq; 107 return 18; 108 } 109 110 /* 111 * Predefined sense codes 112 */ 113 114 /* No sense data available */ 115 const struct SCSISense sense_code_NO_SENSE = { 116 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00 117 }; 118 119 /* LUN not ready, Manual intervention required */ 120 const struct SCSISense sense_code_LUN_NOT_READY = { 121 .key = NOT_READY, .asc = 0x04, .ascq = 0x03 122 }; 123 124 /* LUN not ready, Medium not present */ 125 const struct SCSISense sense_code_NO_MEDIUM = { 126 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00 127 }; 128 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 132 }; 133 134 /* Hardware error, internal target failure */ 135 const struct SCSISense sense_code_TARGET_FAILURE = { 136 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00 137 }; 138 139 /* Illegal request, invalid command operation code */ 140 const struct SCSISense sense_code_INVALID_OPCODE = { 141 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00 142 }; 143 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 147 }; 148 149 /* Illegal request, Invalid field in CDB */ 150 const struct SCSISense sense_code_INVALID_FIELD = { 151 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00 152 }; 153 154 /* Illegal request, Invalid field in parameter list */ 155 const struct SCSISense sense_code_INVALID_PARAM = { 156 .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00 157 }; 158 159 /* Illegal request, Parameter list length error */ 160 const struct SCSISense sense_code_INVALID_PARAM_LEN = { 161 .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00 162 }; 163 164 /* Illegal request, LUN not supported */ 165 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = { 166 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00 167 }; 168 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 172 }; 173 174 /* Illegal request, Incompatible medium installed */ 175 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = { 176 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00 177 }; 178 179 /* Illegal request, medium removal prevented */ 180 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = { 181 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02 182 }; 183 184 /* Illegal request, Invalid Transfer Tag */ 185 const struct SCSISense sense_code_INVALID_TAG = { 186 .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01 187 }; 188 189 /* Command aborted, I/O process terminated */ 190 const struct SCSISense sense_code_IO_ERROR = { 191 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06 192 }; 193 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 197 }; 198 199 /* Command aborted, Logical Unit failure */ 200 const struct SCSISense sense_code_LUN_FAILURE = { 201 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01 202 }; 203 204 /* Command aborted, Overlapped Commands Attempted */ 205 const struct SCSISense sense_code_OVERLAPPED_COMMANDS = { 206 .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00 207 }; 208 209 /* Unit attention, Capacity data has changed */ 210 const struct SCSISense sense_code_CAPACITY_CHANGED = { 211 .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09 212 }; 213 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 217 }; 218 219 /* Unit attention, No medium */ 220 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = { 221 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00 222 }; 223 224 /* Unit attention, Medium may have changed */ 225 const struct SCSISense sense_code_MEDIUM_CHANGED = { 226 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00 227 }; 228 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 232 }; 233 234 /* Unit attention, Device internal reset */ 235 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = { 236 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04 237 }; 238 239 /* Data Protection, Write Protected */ 240 const struct SCSISense sense_code_WRITE_PROTECTED = { 241 .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00 242 }; 243 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 247 }; 248 249 /* 250 * scsi_convert_sense 251 * 252 * Convert between fixed and descriptor sense buffers 253 */ 254 int scsi_convert_sense(uint8_t *in_buf, int in_len, 255 uint8_t *buf, int len, bool fixed) 256 { 257 bool fixed_in; 258 SCSISense sense; 259 if (!fixed && len < 8) { 260 return 0; 261 } 262 263 if (in_len == 0) { 264 sense.key = NO_SENSE; 265 sense.asc = 0; 266 sense.ascq = 0; 267 } else { 268 fixed_in = (in_buf[0] & 2) == 0; 269 270 if (fixed == fixed_in) { 271 memcpy(buf, in_buf, MIN(len, in_len)); 272 return MIN(len, in_len); 273 } 274 275 if (fixed_in) { 276 sense.key = in_buf[2]; 277 sense.asc = in_buf[12]; 278 sense.ascq = in_buf[13]; 279 } else { 280 sense.key = in_buf[1]; 281 sense.asc = in_buf[2]; 282 sense.ascq = in_buf[3]; 283 } 284 } 285 286 memset(buf, 0, len); 287 if (fixed) { 288 /* Return fixed format sense buffer */ 289 buf[0] = 0x70; 290 buf[2] = sense.key; 291 buf[7] = 10; 292 buf[12] = sense.asc; 293 buf[13] = sense.ascq; 294 return MIN(len, SCSI_SENSE_LEN); 295 } else { 296 /* Return descriptor format sense buffer */ 297 buf[0] = 0x72; 298 buf[1] = sense.key; 299 buf[2] = sense.asc; 300 buf[3] = sense.ascq; 301 return 8; 302 } 303 } 304 305 int scsi_sense_to_errno(int key, int asc, int ascq) 306 { 307 switch (key) { 308 case 0x00: /* NO SENSE */ 309 case 0x01: /* RECOVERED ERROR */ 310 case 0x06: /* UNIT ATTENTION */ 311 /* These sense keys are not errors */ 312 return 0; 313 case 0x0b: /* COMMAND ABORTED */ 314 return ECANCELED; 315 case 0x02: /* NOT READY */ 316 case 0x05: /* ILLEGAL REQUEST */ 317 case 0x07: /* DATA PROTECTION */ 318 /* Parse ASCQ */ 319 break; 320 default: 321 return EIO; 322 } 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 */ 328 return EINVAL; 329 case 0x2100: /* LBA OUT OF RANGE */ 330 case 0x2707: /* SPACE ALLOC FAILED */ 331 return ENOSPC; 332 case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */ 333 return ENOTSUP; 334 case 0x3a00: /* MEDIUM NOT PRESENT */ 335 case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */ 336 case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */ 337 return ENOMEDIUM; 338 case 0x2700: /* WRITE PROTECTED */ 339 return EACCES; 340 case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */ 341 return EAGAIN; 342 case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */ 343 return ENOTCONN; 344 default: 345 return EIO; 346 } 347 } 348 349 int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size) 350 { 351 int key, asc, ascq; 352 if (sense_size < 1) { 353 return EIO; 354 } 355 switch (sense[0]) { 356 case 0x70: /* Fixed format sense data. */ 357 if (sense_size < 14) { 358 return EIO; 359 } 360 key = sense[2] & 0xF; 361 asc = sense[12]; 362 ascq = sense[13]; 363 break; 364 case 0x72: /* Descriptor format sense data. */ 365 if (sense_size < 4) { 366 return EIO; 367 } 368 key = sense[1] & 0xF; 369 asc = sense[2]; 370 ascq = sense[3]; 371 break; 372 default: 373 return EIO; 374 break; 375 } 376 return scsi_sense_to_errno(key, asc, ascq); 377 } 378 379 const char *scsi_command_name(uint8_t cmd) 380 { 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", 394 [ SPACE ] = "SPACE", 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", 402 [ COPY ] = "COPY", 403 [ ERASE ] = "ERASE", 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", 437 [ UNMAP ] = "UNMAP", 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", 497 }; 498 499 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) { 500 return "*UNKNOWN*"; 501 } 502 return names[cmd]; 503 } 504 505 #ifdef CONFIG_LINUX 506 int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr, 507 SCSISense *sense) 508 { 509 if (errno_value != 0) { 510 switch (errno_value) { 511 case EDOM: 512 return TASK_SET_FULL; 513 case ENOMEM: 514 *sense = SENSE_CODE(TARGET_FAILURE); 515 return CHECK_CONDITION; 516 default: 517 *sense = SENSE_CODE(IO_ERROR); 518 return CHECK_CONDITION; 519 } 520 } else { 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)) { 525 return BUSY; 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; 533 } else { 534 return GOOD; 535 } 536 } 537 } 538 #endif 539