1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu/osdep.h"
17#include "scsi/constants.h"
18#include "scsi/utils.h"
19#include "qemu/bswap.h"
20
21uint32_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
30uint32_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
51uint64_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
75int 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
99SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len)
100{
101 bool fixed_in;
102 SCSISense sense;
103
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);
109 }
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);
116 }
117 sense.key = in_buf[1];
118 sense.asc = in_buf[2];
119 sense.ascq = in_buf[3];
120 }
121
122 return sense;
123}
124
125int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense,
126 bool fixed_sense)
127{
128 int len;
129 uint8_t buf[SCSI_SENSE_LEN] = { 0 };
130
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;
144 }
145 len = MIN(len, size);
146 memcpy(out_buf, buf, len);
147 return len;
148}
149
150int scsi_build_sense(uint8_t *buf, SCSISense sense)
151{
152 return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true);
153}
154
155
156
157
158
159
160const struct SCSISense sense_code_NO_SENSE = {
161 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
162};
163
164
165const struct SCSISense sense_code_LUN_NOT_READY = {
166 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
167};
168
169
170const struct SCSISense sense_code_NO_MEDIUM = {
171 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
172};
173
174
175const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
176 .key = NOT_READY, .asc = 0x53, .ascq = 0x02
177};
178
179
180const struct SCSISense sense_code_TARGET_FAILURE = {
181 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
182};
183
184
185const struct SCSISense sense_code_INVALID_OPCODE = {
186 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
187};
188
189
190const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
191 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
192};
193
194
195const struct SCSISense sense_code_INVALID_FIELD = {
196 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
197};
198
199
200const struct SCSISense sense_code_INVALID_PARAM = {
201 .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
202};
203
204
205const struct SCSISense sense_code_INVALID_PARAM_LEN = {
206 .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
207};
208
209
210const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
211 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
212};
213
214
215const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
216 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
217};
218
219
220const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
221 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
222};
223
224
225const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
226 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
227};
228
229
230const struct SCSISense sense_code_INVALID_TAG = {
231 .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
232};
233
234
235const struct SCSISense sense_code_IO_ERROR = {
236 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
237};
238
239
240const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
241 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
242};
243
244
245const struct SCSISense sense_code_LUN_FAILURE = {
246 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
247};
248
249
250const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
251 .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
252};
253
254
255const struct SCSISense sense_code_LUN_COMM_FAILURE = {
256 .key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
257};
258
259
260const struct SCSISense sense_code_READ_ERROR = {
261 .key = MEDIUM_ERROR, .asc = 0x11, .ascq = 0x00
262};
263
264
265const struct SCSISense sense_code_NOT_READY = {
266 .key = NOT_READY, .asc = 0x04, .ascq = 0x00
267};
268
269
270const struct SCSISense sense_code_CAPACITY_CHANGED = {
271 .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
272};
273
274
275const struct SCSISense sense_code_RESET = {
276 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
277};
278
279
280const struct SCSISense sense_code_SCSI_BUS_RESET = {
281 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x02
282};
283
284
285const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
286 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
287};
288
289
290const struct SCSISense sense_code_MEDIUM_CHANGED = {
291 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
292};
293
294
295const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
296 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
297};
298
299
300const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
301 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
302};
303
304
305const struct SCSISense sense_code_WRITE_PROTECTED = {
306 .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
307};
308
309
310const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
311 .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
312};
313
314
315
316
317
318
319int scsi_convert_sense(uint8_t *in_buf, int in_len,
320 uint8_t *buf, int len, bool fixed)
321{
322 SCSISense sense;
323 bool fixed_in;
324
325 if (in_len == 0) {
326 return scsi_build_sense_buf(buf, len, SENSE_CODE(NO_SENSE), fixed);
327 }
328
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);
336 }
337}
338
339static bool scsi_sense_is_guest_recoverable(int key, int asc, int ascq)
340{
341 switch (key) {
342 case NO_SENSE:
343 case RECOVERED_ERROR:
344 case UNIT_ATTENTION:
345 case ABORTED_COMMAND:
346 return true;
347 case NOT_READY:
348 case ILLEGAL_REQUEST:
349 case DATA_PROTECT:
350
351 break;
352 default:
353 return false;
354 }
355
356 switch ((asc << 8) | ascq) {
357 case 0x1a00:
358 case 0x2000:
359 case 0x2400:
360 case 0x2500:
361 case 0x2600:
362
363 case 0x2104:
364 case 0x2105:
365 case 0x2106:
366 case 0x550e:
367
368 case 0x0401:
369 case 0x0402:
370 return true;
371 default:
372 return false;
373 }
374}
375
376int scsi_sense_to_errno(int key, int asc, int ascq)
377{
378 switch (key) {
379 case NO_SENSE:
380 case RECOVERED_ERROR:
381 case UNIT_ATTENTION:
382 return EAGAIN;
383 case ABORTED_COMMAND:
384 return ECANCELED;
385 case NOT_READY:
386 case ILLEGAL_REQUEST:
387 case DATA_PROTECT:
388
389 break;
390 default:
391 return EIO;
392 }
393 switch ((asc << 8) | ascq) {
394 case 0x1a00:
395 case 0x2000:
396 case 0x2400:
397 case 0x2600:
398 return EINVAL;
399 case 0x2100:
400 case 0x2707:
401 return ENOSPC;
402 case 0x2500:
403 return ENOTSUP;
404 case 0x3a00:
405 case 0x3a01:
406 case 0x3a02:
407 return ENOMEDIUM;
408 case 0x2700:
409 return EACCES;
410 case 0x0401:
411 return EINPROGRESS;
412 case 0x0402:
413 return ENOTCONN;
414 default:
415 return EIO;
416 }
417}
418
419int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len)
420{
421 SCSISense sense;
422 if (in_len < 1) {
423 return EIO;
424 }
425
426 sense = scsi_parse_sense_buf(in_buf, in_len);
427 return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq);
428}
429
430bool scsi_sense_buf_is_guest_recoverable(const uint8_t *in_buf, size_t in_len)
431{
432 SCSISense sense;
433 if (in_len < 1) {
434 return false;
435 }
436
437 sense = scsi_parse_sense_buf(in_buf, in_len);
438 return scsi_sense_is_guest_recoverable(sense.key, sense.asc, sense.ascq);
439}
440
441const char *scsi_command_name(uint8_t cmd)
442{
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
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",
456 [ SPACE ] = "SPACE",
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",
464 [ COPY ] = "COPY",
465 [ ERASE ] = "ERASE",
466 [ MODE_SENSE ] = "MODE_SENSE",
467 [ START_STOP ] = "START_STOP/LOAD_UNLOAD",
468
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
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
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
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",
499 [ UNMAP ] = "UNMAP",
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
525 [ LOCATE_16 ] = "LOCATE_16",
526 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
527
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
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
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",
559 };
560
561 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
562 return "*UNKNOWN*";
563 }
564 return names[cmd];
565}
566
567#ifdef CONFIG_LINUX
568int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
569 SCSISense *sense)
570{
571 if (errno_value != 0) {
572 switch (errno_value) {
573 case EDOM:
574 return TASK_SET_FULL;
575 case ENOMEM:
576 *sense = SENSE_CODE(TARGET_FAILURE);
577 return CHECK_CONDITION;
578 default:
579 *sense = SENSE_CODE(IO_ERROR);
580 return CHECK_CONDITION;
581 }
582 } else {
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)) {
587 return BUSY;
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;
595 } else {
596 return GOOD;
597 }
598 }
599}
600#endif
601