xref: /openbmc/qemu/scsi/utils.c (revision 083fab02)
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 /* Command aborted, LUN Communication Failure */
210 const struct SCSISense sense_code_LUN_COMM_FAILURE = {
211     .key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
212 };
213 
214 /* Unit attention, Capacity data has changed */
215 const struct SCSISense sense_code_CAPACITY_CHANGED = {
216     .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
217 };
218 
219 /* Unit attention, Power on, reset or bus device reset occurred */
220 const struct SCSISense sense_code_RESET = {
221     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
222 };
223 
224 /* Unit attention, SCSI bus reset */
225 const struct SCSISense sense_code_SCSI_BUS_RESET = {
226     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x02
227 };
228 
229 /* Unit attention, No medium */
230 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
231     .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
232 };
233 
234 /* Unit attention, Medium may have changed */
235 const struct SCSISense sense_code_MEDIUM_CHANGED = {
236     .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
237 };
238 
239 /* Unit attention, Reported LUNs data has changed */
240 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
241     .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
242 };
243 
244 /* Unit attention, Device internal reset */
245 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
246     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
247 };
248 
249 /* Data Protection, Write Protected */
250 const struct SCSISense sense_code_WRITE_PROTECTED = {
251     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
252 };
253 
254 /* Data Protection, Space Allocation Failed Write Protect */
255 const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
256     .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
257 };
258 
259 /*
260  * scsi_convert_sense
261  *
262  * Convert between fixed and descriptor sense buffers
263  */
264 int scsi_convert_sense(uint8_t *in_buf, int in_len,
265                        uint8_t *buf, int len, bool fixed)
266 {
267     bool fixed_in;
268     SCSISense sense;
269     if (!fixed && len < 8) {
270         return 0;
271     }
272 
273     if (in_len == 0) {
274         sense.key = NO_SENSE;
275         sense.asc = 0;
276         sense.ascq = 0;
277     } else {
278         fixed_in = (in_buf[0] & 2) == 0;
279 
280         if (fixed == fixed_in) {
281             memcpy(buf, in_buf, MIN(len, in_len));
282             return MIN(len, in_len);
283         }
284 
285         if (fixed_in) {
286             sense.key = in_buf[2];
287             sense.asc = in_buf[12];
288             sense.ascq = in_buf[13];
289         } else {
290             sense.key = in_buf[1];
291             sense.asc = in_buf[2];
292             sense.ascq = in_buf[3];
293         }
294     }
295 
296     memset(buf, 0, len);
297     if (fixed) {
298         /* Return fixed format sense buffer */
299         buf[0] = 0x70;
300         buf[2] = sense.key;
301         buf[7] = 10;
302         buf[12] = sense.asc;
303         buf[13] = sense.ascq;
304         return MIN(len, SCSI_SENSE_LEN);
305     } else {
306         /* Return descriptor format sense buffer */
307         buf[0] = 0x72;
308         buf[1] = sense.key;
309         buf[2] = sense.asc;
310         buf[3] = sense.ascq;
311         return 8;
312     }
313 }
314 
315 int scsi_sense_to_errno(int key, int asc, int ascq)
316 {
317     switch (key) {
318     case 0x00: /* NO SENSE */
319     case 0x01: /* RECOVERED ERROR */
320     case 0x06: /* UNIT ATTENTION */
321         /* These sense keys are not errors */
322         return 0;
323     case 0x0b: /* COMMAND ABORTED */
324         return ECANCELED;
325     case 0x02: /* NOT READY */
326     case 0x05: /* ILLEGAL REQUEST */
327     case 0x07: /* DATA PROTECTION */
328         /* Parse ASCQ */
329         break;
330     default:
331         return EIO;
332     }
333     switch ((asc << 8) | ascq) {
334     case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
335     case 0x2000: /* INVALID OPERATION CODE */
336     case 0x2400: /* INVALID FIELD IN CDB */
337     case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
338         return EINVAL;
339     case 0x2100: /* LBA OUT OF RANGE */
340     case 0x2707: /* SPACE ALLOC FAILED */
341         return ENOSPC;
342     case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
343         return ENOTSUP;
344     case 0x3a00: /* MEDIUM NOT PRESENT */
345     case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
346     case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
347         return ENOMEDIUM;
348     case 0x2700: /* WRITE PROTECTED */
349         return EACCES;
350     case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
351         return EAGAIN;
352     case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
353         return ENOTCONN;
354     default:
355         return EIO;
356     }
357 }
358 
359 int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size)
360 {
361     int key, asc, ascq;
362     if (sense_size < 1) {
363         return EIO;
364     }
365     switch (sense[0]) {
366     case 0x70: /* Fixed format sense data. */
367         if (sense_size < 14) {
368             return EIO;
369         }
370         key = sense[2] & 0xF;
371         asc = sense[12];
372         ascq = sense[13];
373         break;
374     case 0x72: /* Descriptor format sense data. */
375         if (sense_size < 4) {
376             return EIO;
377         }
378         key = sense[1] & 0xF;
379         asc = sense[2];
380         ascq = sense[3];
381         break;
382     default:
383         return EIO;
384         break;
385     }
386     return scsi_sense_to_errno(key, asc, ascq);
387 }
388 
389 const char *scsi_command_name(uint8_t cmd)
390 {
391     static const char *names[] = {
392         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
393         [ REWIND                   ] = "REWIND",
394         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
395         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
396         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
397         [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
398         /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
399         [ READ_6                   ] = "READ_6",
400         [ WRITE_6                  ] = "WRITE_6",
401         [ SET_CAPACITY             ] = "SET_CAPACITY",
402         [ READ_REVERSE             ] = "READ_REVERSE",
403         [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
404         [ SPACE                    ] = "SPACE",
405         [ INQUIRY                  ] = "INQUIRY",
406         [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
407         [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
408         [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
409         [ MODE_SELECT              ] = "MODE_SELECT",
410         [ RESERVE                  ] = "RESERVE",
411         [ RELEASE                  ] = "RELEASE",
412         [ COPY                     ] = "COPY",
413         [ ERASE                    ] = "ERASE",
414         [ MODE_SENSE               ] = "MODE_SENSE",
415         [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
416         /* LOAD_UNLOAD and START_STOP use the same operation code */
417         [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
418         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
419         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
420         [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
421         [ READ_10                  ] = "READ_10",
422         [ WRITE_10                 ] = "WRITE_10",
423         [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
424         /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
425         [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
426         [ VERIFY_10                ] = "VERIFY_10",
427         [ SEARCH_HIGH              ] = "SEARCH_HIGH",
428         [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
429         [ SEARCH_LOW               ] = "SEARCH_LOW",
430         [ SET_LIMITS               ] = "SET_LIMITS",
431         [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
432         /* READ_POSITION and PRE_FETCH use the same operation code */
433         [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
434         [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
435         [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
436         /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
437         [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
438         [ COMPARE                  ] = "COMPARE",
439         [ COPY_VERIFY              ] = "COPY_VERIFY",
440         [ WRITE_BUFFER             ] = "WRITE_BUFFER",
441         [ READ_BUFFER              ] = "READ_BUFFER",
442         [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
443         [ READ_LONG_10             ] = "READ_LONG_10",
444         [ WRITE_LONG_10            ] = "WRITE_LONG_10",
445         [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
446         [ WRITE_SAME_10            ] = "WRITE_SAME_10",
447         [ UNMAP                    ] = "UNMAP",
448         [ READ_TOC                 ] = "READ_TOC",
449         [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
450         [ SANITIZE                 ] = "SANITIZE",
451         [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
452         [ LOG_SELECT               ] = "LOG_SELECT",
453         [ LOG_SENSE                ] = "LOG_SENSE",
454         [ MODE_SELECT_10           ] = "MODE_SELECT_10",
455         [ RESERVE_10               ] = "RESERVE_10",
456         [ RELEASE_10               ] = "RELEASE_10",
457         [ MODE_SENSE_10            ] = "MODE_SENSE_10",
458         [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
459         [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
460         [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
461         [ EXTENDED_COPY            ] = "EXTENDED_COPY",
462         [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
463         [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
464         [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
465         [ READ_16                  ] = "READ_16",
466         [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
467         [ WRITE_16                 ] = "WRITE_16",
468         [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
469         [ VERIFY_16                ] = "VERIFY_16",
470         [ PRE_FETCH_16             ] = "PRE_FETCH_16",
471         [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
472         /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
473         [ LOCATE_16                ] = "LOCATE_16",
474         [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
475         /* ERASE_16 and WRITE_SAME_16 use the same operation code */
476         [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
477         [ WRITE_LONG_16            ] = "WRITE_LONG_16",
478         [ REPORT_LUNS              ] = "REPORT_LUNS",
479         [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
480         [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
481         [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
482         [ READ_12                  ] = "READ_12",
483         [ WRITE_12                 ] = "WRITE_12",
484         [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
485         /* ERASE_12 and GET_PERFORMANCE use the same operation code */
486         [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
487         [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
488         [ VERIFY_12                ] = "VERIFY_12",
489         [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
490         [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
491         [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
492         [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
493         [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
494         /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
495         [ READ_CD                  ] = "READ_CD",
496         [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
497         [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
498         [ RESERVE_TRACK            ] = "RESERVE_TRACK",
499         [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
500         [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
501         [ SET_CD_SPEED             ] = "SET_CD_SPEED",
502         [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
503         [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
504         [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
505         [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
506         [ READ_DISC_INFORMATION    ] = "READ_DISC_INFORMATION",
507     };
508 
509     if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
510         return "*UNKNOWN*";
511     }
512     return names[cmd];
513 }
514 
515 #ifdef CONFIG_LINUX
516 int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
517                            SCSISense *sense)
518 {
519     if (errno_value != 0) {
520         switch (errno_value) {
521         case EDOM:
522             return TASK_SET_FULL;
523         case ENOMEM:
524             *sense = SENSE_CODE(TARGET_FAILURE);
525             return CHECK_CONDITION;
526         default:
527             *sense = SENSE_CODE(IO_ERROR);
528             return CHECK_CONDITION;
529         }
530     } else {
531         if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
532             io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
533             io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
534             (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
535             return BUSY;
536         } else if (io_hdr->host_status) {
537             *sense = SENSE_CODE(I_T_NEXUS_LOSS);
538             return CHECK_CONDITION;
539         } else if (io_hdr->status) {
540             return io_hdr->status;
541         } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
542             return CHECK_CONDITION;
543         } else {
544             return GOOD;
545         }
546     }
547 }
548 #endif
549