root / hw / scsi-disk.c @ aa5dbdc1
History | View | Annotate | Download (33.2 kB)
1 |
/*
|
---|---|
2 |
* SCSI Device emulation
|
3 |
*
|
4 |
* Copyright (c) 2006 CodeSourcery.
|
5 |
* Based on code by Fabrice Bellard
|
6 |
*
|
7 |
* Written by Paul Brook
|
8 |
*
|
9 |
* This code is licenced under the LGPL.
|
10 |
*
|
11 |
* Note that this file only handles the SCSI architecture model and device
|
12 |
* commands. Emulation of interface/link layer protocols is handled by
|
13 |
* the host adapter emulator.
|
14 |
*/
|
15 |
|
16 |
#include <qemu-common.h> |
17 |
#include <sysemu.h> |
18 |
//#define DEBUG_SCSI
|
19 |
|
20 |
#ifdef DEBUG_SCSI
|
21 |
#define DPRINTF(fmt, ...) \
|
22 |
do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0) |
23 |
#else
|
24 |
#define DPRINTF(fmt, ...) do {} while(0) |
25 |
#endif
|
26 |
|
27 |
#define BADF(fmt, ...) \
|
28 |
do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0) |
29 |
|
30 |
#include "qemu-common.h" |
31 |
#include "block.h" |
32 |
#include "scsi.h" |
33 |
#include "scsi-defs.h" |
34 |
|
35 |
#define SCSI_DMA_BUF_SIZE 131072 |
36 |
#define SCSI_MAX_INQUIRY_LEN 256 |
37 |
|
38 |
#define SCSI_REQ_STATUS_RETRY 0x01 |
39 |
|
40 |
typedef struct SCSIDiskState SCSIDiskState; |
41 |
|
42 |
typedef struct SCSIDiskReq { |
43 |
SCSIRequest req; |
44 |
/* ??? We should probably keep track of whether the data transfer is
|
45 |
a read or a write. Currently we rely on the host getting it right. */
|
46 |
/* Both sector and sector_count are in terms of qemu 512 byte blocks. */
|
47 |
uint64_t sector; |
48 |
uint32_t sector_count; |
49 |
struct iovec iov;
|
50 |
QEMUIOVector qiov; |
51 |
uint32_t status; |
52 |
} SCSIDiskReq; |
53 |
|
54 |
struct SCSIDiskState
|
55 |
{ |
56 |
SCSIDevice qdev; |
57 |
/* The qemu block layer uses a fixed 512 byte sector size.
|
58 |
This is the number of 512 byte blocks in a single scsi sector. */
|
59 |
int cluster_size;
|
60 |
uint64_t max_lba; |
61 |
char drive_serial_str[21]; |
62 |
QEMUBH *bh; |
63 |
}; |
64 |
|
65 |
static SCSIDiskReq *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun)
|
66 |
{ |
67 |
SCSIRequest *req; |
68 |
SCSIDiskReq *r; |
69 |
|
70 |
req = scsi_req_alloc(sizeof(SCSIDiskReq), d, tag, lun);
|
71 |
r = DO_UPCAST(SCSIDiskReq, req, req); |
72 |
r->iov.iov_base = qemu_memalign(512, SCSI_DMA_BUF_SIZE);
|
73 |
return r;
|
74 |
} |
75 |
|
76 |
static void scsi_remove_request(SCSIDiskReq *r) |
77 |
{ |
78 |
qemu_free(r->iov.iov_base); |
79 |
scsi_req_free(&r->req); |
80 |
} |
81 |
|
82 |
static SCSIDiskReq *scsi_find_request(SCSIDiskState *s, uint32_t tag)
|
83 |
{ |
84 |
return DO_UPCAST(SCSIDiskReq, req, scsi_req_find(&s->qdev, tag));
|
85 |
} |
86 |
|
87 |
static void scsi_req_set_status(SCSIRequest *req, int status, int sense_code) |
88 |
{ |
89 |
req->status = status; |
90 |
scsi_dev_set_sense(req->dev, sense_code); |
91 |
} |
92 |
|
93 |
/* Helper function for command completion. */
|
94 |
static void scsi_command_complete(SCSIDiskReq *r, int status, int sense) |
95 |
{ |
96 |
DPRINTF("Command complete tag=0x%x status=%d sense=%d\n",
|
97 |
r->req.tag, status, sense); |
98 |
scsi_req_set_status(&r->req, status, sense); |
99 |
scsi_req_complete(&r->req); |
100 |
scsi_remove_request(r); |
101 |
} |
102 |
|
103 |
/* Cancel a pending data transfer. */
|
104 |
static void scsi_cancel_io(SCSIDevice *d, uint32_t tag) |
105 |
{ |
106 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
107 |
SCSIDiskReq *r; |
108 |
DPRINTF("Cancel tag=0x%x\n", tag);
|
109 |
r = scsi_find_request(s, tag); |
110 |
if (r) {
|
111 |
if (r->req.aiocb)
|
112 |
bdrv_aio_cancel(r->req.aiocb); |
113 |
r->req.aiocb = NULL;
|
114 |
scsi_remove_request(r); |
115 |
} |
116 |
} |
117 |
|
118 |
static void scsi_read_complete(void * opaque, int ret) |
119 |
{ |
120 |
SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
121 |
|
122 |
if (ret) {
|
123 |
DPRINTF("IO error\n");
|
124 |
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, 0);
|
125 |
scsi_command_complete(r, CHECK_CONDITION, NO_SENSE); |
126 |
return;
|
127 |
} |
128 |
DPRINTF("Data ready tag=0x%x len=%" PRId64 "\n", r->req.tag, r->iov.iov_len); |
129 |
|
130 |
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len); |
131 |
} |
132 |
|
133 |
/* Read more data from scsi device into buffer. */
|
134 |
static void scsi_read_data(SCSIDevice *d, uint32_t tag) |
135 |
{ |
136 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
137 |
SCSIDiskReq *r; |
138 |
uint32_t n; |
139 |
|
140 |
r = scsi_find_request(s, tag); |
141 |
if (!r) {
|
142 |
BADF("Bad read tag 0x%x\n", tag);
|
143 |
/* ??? This is the wrong error. */
|
144 |
scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR); |
145 |
return;
|
146 |
} |
147 |
if (r->sector_count == (uint32_t)-1) { |
148 |
DPRINTF("Read buf_len=%" PRId64 "\n", r->iov.iov_len); |
149 |
r->sector_count = 0;
|
150 |
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, r->iov.iov_len); |
151 |
return;
|
152 |
} |
153 |
DPRINTF("Read sector_count=%d\n", r->sector_count);
|
154 |
if (r->sector_count == 0) { |
155 |
scsi_command_complete(r, GOOD, NO_SENSE); |
156 |
return;
|
157 |
} |
158 |
|
159 |
n = r->sector_count; |
160 |
if (n > SCSI_DMA_BUF_SIZE / 512) |
161 |
n = SCSI_DMA_BUF_SIZE / 512;
|
162 |
|
163 |
r->iov.iov_len = n * 512;
|
164 |
qemu_iovec_init_external(&r->qiov, &r->iov, 1);
|
165 |
r->req.aiocb = bdrv_aio_readv(s->qdev.dinfo->bdrv, r->sector, &r->qiov, n, |
166 |
scsi_read_complete, r); |
167 |
if (r->req.aiocb == NULL) |
168 |
scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR); |
169 |
r->sector += n; |
170 |
r->sector_count -= n; |
171 |
} |
172 |
|
173 |
static int scsi_handle_write_error(SCSIDiskReq *r, int error) |
174 |
{ |
175 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
176 |
BlockInterfaceErrorAction action = drive_get_onerror(s->qdev.dinfo->bdrv); |
177 |
|
178 |
if (action == BLOCK_ERR_IGNORE)
|
179 |
return 0; |
180 |
|
181 |
if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
|
182 |
|| action == BLOCK_ERR_STOP_ANY) { |
183 |
r->status |= SCSI_REQ_STATUS_RETRY; |
184 |
vm_stop(0);
|
185 |
} else {
|
186 |
scsi_command_complete(r, CHECK_CONDITION, |
187 |
HARDWARE_ERROR); |
188 |
} |
189 |
|
190 |
return 1; |
191 |
} |
192 |
|
193 |
static void scsi_write_complete(void * opaque, int ret) |
194 |
{ |
195 |
SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
196 |
uint32_t len; |
197 |
uint32_t n; |
198 |
|
199 |
r->req.aiocb = NULL;
|
200 |
|
201 |
if (ret) {
|
202 |
if (scsi_handle_write_error(r, -ret))
|
203 |
return;
|
204 |
} |
205 |
|
206 |
n = r->iov.iov_len / 512;
|
207 |
r->sector += n; |
208 |
r->sector_count -= n; |
209 |
if (r->sector_count == 0) { |
210 |
scsi_command_complete(r, GOOD, NO_SENSE); |
211 |
} else {
|
212 |
len = r->sector_count * 512;
|
213 |
if (len > SCSI_DMA_BUF_SIZE) {
|
214 |
len = SCSI_DMA_BUF_SIZE; |
215 |
} |
216 |
r->iov.iov_len = len; |
217 |
DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, len);
|
218 |
r->req.bus->complete(r->req.bus, SCSI_REASON_DATA, r->req.tag, len); |
219 |
} |
220 |
} |
221 |
|
222 |
static void scsi_write_request(SCSIDiskReq *r) |
223 |
{ |
224 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
225 |
uint32_t n; |
226 |
|
227 |
n = r->iov.iov_len / 512;
|
228 |
if (n) {
|
229 |
qemu_iovec_init_external(&r->qiov, &r->iov, 1);
|
230 |
r->req.aiocb = bdrv_aio_writev(s->qdev.dinfo->bdrv, r->sector, &r->qiov, n, |
231 |
scsi_write_complete, r); |
232 |
if (r->req.aiocb == NULL) |
233 |
scsi_command_complete(r, CHECK_CONDITION, |
234 |
HARDWARE_ERROR); |
235 |
} else {
|
236 |
/* Invoke completion routine to fetch data from host. */
|
237 |
scsi_write_complete(r, 0);
|
238 |
} |
239 |
} |
240 |
|
241 |
/* Write data to a scsi device. Returns nonzero on failure.
|
242 |
The transfer may complete asynchronously. */
|
243 |
static int scsi_write_data(SCSIDevice *d, uint32_t tag) |
244 |
{ |
245 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
246 |
SCSIDiskReq *r; |
247 |
|
248 |
DPRINTF("Write data tag=0x%x\n", tag);
|
249 |
r = scsi_find_request(s, tag); |
250 |
if (!r) {
|
251 |
BADF("Bad write tag 0x%x\n", tag);
|
252 |
scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR); |
253 |
return 1; |
254 |
} |
255 |
|
256 |
if (r->req.aiocb)
|
257 |
BADF("Data transfer already in progress\n");
|
258 |
|
259 |
scsi_write_request(r); |
260 |
|
261 |
return 0; |
262 |
} |
263 |
|
264 |
static void scsi_dma_restart_bh(void *opaque) |
265 |
{ |
266 |
SCSIDiskState *s = opaque; |
267 |
SCSIRequest *req; |
268 |
SCSIDiskReq *r; |
269 |
|
270 |
qemu_bh_delete(s->bh); |
271 |
s->bh = NULL;
|
272 |
|
273 |
QTAILQ_FOREACH(req, &s->qdev.requests, next) { |
274 |
r = DO_UPCAST(SCSIDiskReq, req, req); |
275 |
if (r->status & SCSI_REQ_STATUS_RETRY) {
|
276 |
r->status &= ~SCSI_REQ_STATUS_RETRY; |
277 |
scsi_write_request(r); |
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
static void scsi_dma_restart_cb(void *opaque, int running, int reason) |
283 |
{ |
284 |
SCSIDiskState *s = opaque; |
285 |
|
286 |
if (!running)
|
287 |
return;
|
288 |
|
289 |
if (!s->bh) {
|
290 |
s->bh = qemu_bh_new(scsi_dma_restart_bh, s); |
291 |
qemu_bh_schedule(s->bh); |
292 |
} |
293 |
} |
294 |
|
295 |
/* Return a pointer to the data buffer. */
|
296 |
static uint8_t *scsi_get_buf(SCSIDevice *d, uint32_t tag)
|
297 |
{ |
298 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
299 |
SCSIDiskReq *r; |
300 |
|
301 |
r = scsi_find_request(s, tag); |
302 |
if (!r) {
|
303 |
BADF("Bad buffer tag 0x%x\n", tag);
|
304 |
return NULL; |
305 |
} |
306 |
return (uint8_t *)r->iov.iov_base;
|
307 |
} |
308 |
|
309 |
static int scsi_disk_emulate_command(SCSIRequest *req, uint8_t *outbuf) |
310 |
{ |
311 |
BlockDriverState *bdrv = req->dev->dinfo->bdrv; |
312 |
int buflen = 0; |
313 |
|
314 |
switch (req->cmd.buf[0]) { |
315 |
case TEST_UNIT_READY:
|
316 |
if (!bdrv_is_inserted(bdrv))
|
317 |
goto not_ready;
|
318 |
break;
|
319 |
default:
|
320 |
goto illegal_request;
|
321 |
} |
322 |
scsi_req_set_status(req, GOOD, NO_SENSE); |
323 |
return buflen;
|
324 |
|
325 |
not_ready:
|
326 |
scsi_req_set_status(req, CHECK_CONDITION, NOT_READY); |
327 |
return 0; |
328 |
|
329 |
illegal_request:
|
330 |
scsi_req_set_status(req, CHECK_CONDITION, ILLEGAL_REQUEST); |
331 |
return 0; |
332 |
} |
333 |
|
334 |
/* Execute a scsi command. Returns the length of the data expected by the
|
335 |
command. This will be Positive for data transfers from the device
|
336 |
(eg. disk reads), negative for transfers to the device (eg. disk writes),
|
337 |
and zero if the command does not transfer any data. */
|
338 |
|
339 |
static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
|
340 |
uint8_t *buf, int lun)
|
341 |
{ |
342 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
343 |
uint64_t nb_sectors; |
344 |
uint64_t lba; |
345 |
uint32_t len; |
346 |
int cmdlen;
|
347 |
int is_write;
|
348 |
uint8_t command; |
349 |
uint8_t *outbuf; |
350 |
SCSIDiskReq *r; |
351 |
int rc;
|
352 |
|
353 |
command = buf[0];
|
354 |
r = scsi_find_request(s, tag); |
355 |
if (r) {
|
356 |
BADF("Tag 0x%x already in use\n", tag);
|
357 |
scsi_cancel_io(d, tag); |
358 |
} |
359 |
/* ??? Tags are not unique for different luns. We only implement a
|
360 |
single lun, so this should not matter. */
|
361 |
r = scsi_new_request(d, tag, lun); |
362 |
outbuf = (uint8_t *)r->iov.iov_base; |
363 |
is_write = 0;
|
364 |
DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]); |
365 |
switch (command >> 5) { |
366 |
case 0: |
367 |
lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) | |
368 |
(((uint64_t) buf[1] & 0x1f) << 16); |
369 |
len = buf[4];
|
370 |
cmdlen = 6;
|
371 |
break;
|
372 |
case 1: |
373 |
case 2: |
374 |
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) | |
375 |
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24); |
376 |
len = buf[8] | (buf[7] << 8); |
377 |
cmdlen = 10;
|
378 |
break;
|
379 |
case 4: |
380 |
lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) | |
381 |
((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) | |
382 |
((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) | |
383 |
((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56); |
384 |
len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24); |
385 |
cmdlen = 16;
|
386 |
break;
|
387 |
case 5: |
388 |
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) | |
389 |
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24); |
390 |
len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24); |
391 |
cmdlen = 12;
|
392 |
break;
|
393 |
default:
|
394 |
BADF("Unsupported command length, command %x\n", command);
|
395 |
goto fail;
|
396 |
} |
397 |
#ifdef DEBUG_SCSI
|
398 |
{ |
399 |
int i;
|
400 |
for (i = 1; i < cmdlen; i++) { |
401 |
printf(" 0x%02x", buf[i]);
|
402 |
} |
403 |
printf("\n");
|
404 |
} |
405 |
#endif
|
406 |
|
407 |
if (scsi_req_parse(&r->req, buf) != 0) { |
408 |
BADF("Unsupported command length, command %x\n", command);
|
409 |
goto fail;
|
410 |
} |
411 |
assert(r->req.cmd.len == cmdlen); |
412 |
assert(r->req.cmd.lba == lba); |
413 |
|
414 |
if (lun || buf[1] >> 5) { |
415 |
/* Only LUN 0 supported. */
|
416 |
DPRINTF("Unimplemented LUN %d\n", lun ? lun : buf[1] >> 5); |
417 |
if (command != REQUEST_SENSE && command != INQUIRY)
|
418 |
goto fail;
|
419 |
} |
420 |
switch (command) {
|
421 |
case TEST_UNIT_READY:
|
422 |
rc = scsi_disk_emulate_command(&r->req, outbuf); |
423 |
if (rc > 0) { |
424 |
r->iov.iov_len = rc; |
425 |
} else {
|
426 |
scsi_req_complete(&r->req); |
427 |
scsi_remove_request(r); |
428 |
} |
429 |
return rc;
|
430 |
case REQUEST_SENSE:
|
431 |
DPRINTF("Request Sense (len %d)\n", len);
|
432 |
if (len < 4) |
433 |
goto fail;
|
434 |
memset(outbuf, 0, 4); |
435 |
r->iov.iov_len = 4;
|
436 |
if (s->qdev.sense.key == NOT_READY && len >= 18) { |
437 |
memset(outbuf, 0, 18); |
438 |
r->iov.iov_len = 18;
|
439 |
outbuf[7] = 10; |
440 |
/* asc 0x3a, ascq 0: Medium not present */
|
441 |
outbuf[12] = 0x3a; |
442 |
outbuf[13] = 0; |
443 |
} |
444 |
outbuf[0] = 0xf0; |
445 |
outbuf[1] = 0; |
446 |
outbuf[2] = s->qdev.sense.key;
|
447 |
scsi_dev_clear_sense(&s->qdev); |
448 |
break;
|
449 |
case INQUIRY:
|
450 |
DPRINTF("Inquiry (len %d)\n", len);
|
451 |
if (buf[1] & 0x2) { |
452 |
/* Command support data - optional, not implemented */
|
453 |
BADF("optional INQUIRY command support request not implemented\n");
|
454 |
goto fail;
|
455 |
} |
456 |
else if (buf[1] & 0x1) { |
457 |
/* Vital product data */
|
458 |
uint8_t page_code = buf[2];
|
459 |
if (len < 4) { |
460 |
BADF("Error: Inquiry (EVPD[%02X]) buffer size %d is "
|
461 |
"less than 4\n", page_code, len);
|
462 |
goto fail;
|
463 |
} |
464 |
|
465 |
switch (page_code) {
|
466 |
case 0x00: |
467 |
{ |
468 |
/* Supported page codes, mandatory */
|
469 |
DPRINTF("Inquiry EVPD[Supported pages] "
|
470 |
"buffer size %d\n", len);
|
471 |
|
472 |
r->iov.iov_len = 0;
|
473 |
|
474 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
|
475 |
outbuf[r->iov.iov_len++] = 5;
|
476 |
} else {
|
477 |
outbuf[r->iov.iov_len++] = 0;
|
478 |
} |
479 |
|
480 |
outbuf[r->iov.iov_len++] = 0x00; // this page |
481 |
outbuf[r->iov.iov_len++] = 0x00;
|
482 |
outbuf[r->iov.iov_len++] = 3; // number of pages |
483 |
outbuf[r->iov.iov_len++] = 0x00; // list of supported pages (this page) |
484 |
outbuf[r->iov.iov_len++] = 0x80; // unit serial number |
485 |
outbuf[r->iov.iov_len++] = 0x83; // device identification |
486 |
} |
487 |
break;
|
488 |
case 0x80: |
489 |
{ |
490 |
int l;
|
491 |
|
492 |
/* Device serial number, optional */
|
493 |
if (len < 4) { |
494 |
BADF("Error: EVPD[Serial number] Inquiry buffer "
|
495 |
"size %d too small, %d needed\n", len, 4); |
496 |
goto fail;
|
497 |
} |
498 |
|
499 |
DPRINTF("Inquiry EVPD[Serial number] buffer size %d\n", len);
|
500 |
l = MIN(len, strlen(s->drive_serial_str)); |
501 |
|
502 |
r->iov.iov_len = 0;
|
503 |
|
504 |
/* Supported page codes */
|
505 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
|
506 |
outbuf[r->iov.iov_len++] = 5;
|
507 |
} else {
|
508 |
outbuf[r->iov.iov_len++] = 0;
|
509 |
} |
510 |
|
511 |
outbuf[r->iov.iov_len++] = 0x80; // this page |
512 |
outbuf[r->iov.iov_len++] = 0x00;
|
513 |
outbuf[r->iov.iov_len++] = l; |
514 |
memcpy(&outbuf[r->iov.iov_len], s->drive_serial_str, l); |
515 |
r->iov.iov_len += l; |
516 |
} |
517 |
|
518 |
break;
|
519 |
case 0x83: |
520 |
{ |
521 |
/* Device identification page, mandatory */
|
522 |
int max_len = 255 - 8; |
523 |
int id_len = strlen(bdrv_get_device_name(s->qdev.dinfo->bdrv));
|
524 |
if (id_len > max_len)
|
525 |
id_len = max_len; |
526 |
|
527 |
DPRINTF("Inquiry EVPD[Device identification] "
|
528 |
"buffer size %d\n", len);
|
529 |
r->iov.iov_len = 0;
|
530 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
|
531 |
outbuf[r->iov.iov_len++] = 5;
|
532 |
} else {
|
533 |
outbuf[r->iov.iov_len++] = 0;
|
534 |
} |
535 |
|
536 |
outbuf[r->iov.iov_len++] = 0x83; // this page |
537 |
outbuf[r->iov.iov_len++] = 0x00;
|
538 |
outbuf[r->iov.iov_len++] = 3 + id_len;
|
539 |
|
540 |
outbuf[r->iov.iov_len++] = 0x2; // ASCII |
541 |
outbuf[r->iov.iov_len++] = 0; // not officially assigned |
542 |
outbuf[r->iov.iov_len++] = 0; // reserved |
543 |
outbuf[r->iov.iov_len++] = id_len; // length of data following
|
544 |
|
545 |
memcpy(&outbuf[r->iov.iov_len], |
546 |
bdrv_get_device_name(s->qdev.dinfo->bdrv), id_len); |
547 |
r->iov.iov_len += id_len; |
548 |
} |
549 |
break;
|
550 |
default:
|
551 |
BADF("Error: unsupported Inquiry (EVPD[%02X]) "
|
552 |
"buffer size %d\n", page_code, len);
|
553 |
goto fail;
|
554 |
} |
555 |
/* done with EVPD */
|
556 |
break;
|
557 |
} |
558 |
else {
|
559 |
/* Standard INQUIRY data */
|
560 |
if (buf[2] != 0) { |
561 |
BADF("Error: Inquiry (STANDARD) page or code "
|
562 |
"is non-zero [%02X]\n", buf[2]); |
563 |
goto fail;
|
564 |
} |
565 |
|
566 |
/* PAGE CODE == 0 */
|
567 |
if (len < 5) { |
568 |
BADF("Error: Inquiry (STANDARD) buffer size %d "
|
569 |
"is less than 5\n", len);
|
570 |
goto fail;
|
571 |
} |
572 |
|
573 |
if (len < 36) { |
574 |
BADF("Error: Inquiry (STANDARD) buffer size %d "
|
575 |
"is less than 36 (TODO: only 5 required)\n", len);
|
576 |
} |
577 |
} |
578 |
|
579 |
if(len > SCSI_MAX_INQUIRY_LEN)
|
580 |
len = SCSI_MAX_INQUIRY_LEN; |
581 |
|
582 |
memset(outbuf, 0, len);
|
583 |
|
584 |
if (lun || buf[1] >> 5) { |
585 |
outbuf[0] = 0x7f; /* LUN not supported */ |
586 |
} else if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) { |
587 |
outbuf[0] = 5; |
588 |
outbuf[1] = 0x80; |
589 |
memcpy(&outbuf[16], "QEMU CD-ROM ", 16); |
590 |
} else {
|
591 |
outbuf[0] = 0; |
592 |
memcpy(&outbuf[16], "QEMU HARDDISK ", 16); |
593 |
} |
594 |
memcpy(&outbuf[8], "QEMU ", 8); |
595 |
memcpy(&outbuf[32], QEMU_VERSION, 4); |
596 |
/* Identify device as SCSI-3 rev 1.
|
597 |
Some later commands are also implemented. */
|
598 |
outbuf[2] = 3; |
599 |
outbuf[3] = 2; /* Format 2 */ |
600 |
outbuf[4] = len - 5; /* Additional Length = (Len - 1) - 4 */ |
601 |
/* Sync data transfer and TCQ. */
|
602 |
outbuf[7] = 0x10 | (r->req.bus->tcq ? 0x02 : 0); |
603 |
r->iov.iov_len = len; |
604 |
break;
|
605 |
case RESERVE:
|
606 |
DPRINTF("Reserve(6)\n");
|
607 |
if (buf[1] & 1) |
608 |
goto fail;
|
609 |
break;
|
610 |
case RELEASE:
|
611 |
DPRINTF("Release(6)\n");
|
612 |
if (buf[1] & 1) |
613 |
goto fail;
|
614 |
break;
|
615 |
case MODE_SENSE:
|
616 |
case MODE_SENSE_10:
|
617 |
{ |
618 |
uint8_t *p; |
619 |
int page;
|
620 |
int dbd;
|
621 |
|
622 |
dbd = buf[1] & 0x8; |
623 |
page = buf[2] & 0x3f; |
624 |
DPRINTF("Mode Sense (page %d, len %d)\n", page, len);
|
625 |
p = outbuf; |
626 |
memset(p, 0, 4); |
627 |
outbuf[1] = 0; /* Default media type. */ |
628 |
outbuf[3] = 0; /* Block descriptor length. */ |
629 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM ||
|
630 |
bdrv_is_read_only(s->qdev.dinfo->bdrv)) { |
631 |
outbuf[2] = 0x80; /* Readonly. */ |
632 |
} |
633 |
p += 4;
|
634 |
bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors); |
635 |
if ((~dbd) & nb_sectors) {
|
636 |
nb_sectors /= s->cluster_size; |
637 |
nb_sectors--; |
638 |
if (nb_sectors > 0xffffff) |
639 |
nb_sectors = 0xffffff;
|
640 |
outbuf[3] = 8; /* Block descriptor length */ |
641 |
p[0] = 0; /* media density code */ |
642 |
p[1] = (nb_sectors >> 16) & 0xff; |
643 |
p[2] = (nb_sectors >> 8) & 0xff; |
644 |
p[3] = nb_sectors & 0xff; |
645 |
p[4] = 0; /* reserved */ |
646 |
p[5] = 0; /* bytes 5-7 are the sector size in bytes */ |
647 |
p[6] = s->cluster_size * 2; |
648 |
p[7] = 0; |
649 |
p += 8;
|
650 |
} |
651 |
|
652 |
if (page == 4) { |
653 |
int cylinders, heads, secs;
|
654 |
|
655 |
/* Rigid disk device geometry page. */
|
656 |
p[0] = 4; |
657 |
p[1] = 0x16; |
658 |
/* if a geometry hint is available, use it */
|
659 |
bdrv_get_geometry_hint(s->qdev.dinfo->bdrv, &cylinders, &heads, &secs); |
660 |
p[2] = (cylinders >> 16) & 0xff; |
661 |
p[3] = (cylinders >> 8) & 0xff; |
662 |
p[4] = cylinders & 0xff; |
663 |
p[5] = heads & 0xff; |
664 |
/* Write precomp start cylinder, disabled */
|
665 |
p[6] = (cylinders >> 16) & 0xff; |
666 |
p[7] = (cylinders >> 8) & 0xff; |
667 |
p[8] = cylinders & 0xff; |
668 |
/* Reduced current start cylinder, disabled */
|
669 |
p[9] = (cylinders >> 16) & 0xff; |
670 |
p[10] = (cylinders >> 8) & 0xff; |
671 |
p[11] = cylinders & 0xff; |
672 |
/* Device step rate [ns], 200ns */
|
673 |
p[12] = 0; |
674 |
p[13] = 200; |
675 |
/* Landing zone cylinder */
|
676 |
p[14] = 0xff; |
677 |
p[15] = 0xff; |
678 |
p[16] = 0xff; |
679 |
/* Medium rotation rate [rpm], 5400 rpm */
|
680 |
p[20] = (5400 >> 8) & 0xff; |
681 |
p[21] = 5400 & 0xff; |
682 |
p += 0x16;
|
683 |
} else if (page == 5) { |
684 |
int cylinders, heads, secs;
|
685 |
|
686 |
/* Flexible disk device geometry page. */
|
687 |
p[0] = 5; |
688 |
p[1] = 0x1e; |
689 |
/* Transfer rate [kbit/s], 5Mbit/s */
|
690 |
p[2] = 5000 >> 8; |
691 |
p[3] = 5000 & 0xff; |
692 |
/* if a geometry hint is available, use it */
|
693 |
bdrv_get_geometry_hint(s->qdev.dinfo->bdrv, &cylinders, &heads, &secs); |
694 |
p[4] = heads & 0xff; |
695 |
p[5] = secs & 0xff; |
696 |
p[6] = s->cluster_size * 2; |
697 |
p[8] = (cylinders >> 8) & 0xff; |
698 |
p[9] = cylinders & 0xff; |
699 |
/* Write precomp start cylinder, disabled */
|
700 |
p[10] = (cylinders >> 8) & 0xff; |
701 |
p[11] = cylinders & 0xff; |
702 |
/* Reduced current start cylinder, disabled */
|
703 |
p[12] = (cylinders >> 8) & 0xff; |
704 |
p[13] = cylinders & 0xff; |
705 |
/* Device step rate [100us], 100us */
|
706 |
p[14] = 0; |
707 |
p[15] = 1; |
708 |
/* Device step pulse width [us], 1us */
|
709 |
p[16] = 1; |
710 |
/* Device head settle delay [100us], 100us */
|
711 |
p[17] = 0; |
712 |
p[18] = 1; |
713 |
/* Motor on delay [0.1s], 0.1s */
|
714 |
p[19] = 1; |
715 |
/* Motor off delay [0.1s], 0.1s */
|
716 |
p[20] = 1; |
717 |
/* Medium rotation rate [rpm], 5400 rpm */
|
718 |
p[28] = (5400 >> 8) & 0xff; |
719 |
p[29] = 5400 & 0xff; |
720 |
p += 0x1e;
|
721 |
} else if ((page == 8 || page == 0x3f)) { |
722 |
/* Caching page. */
|
723 |
memset(p,0,20); |
724 |
p[0] = 8; |
725 |
p[1] = 0x12; |
726 |
if (bdrv_enable_write_cache(s->qdev.dinfo->bdrv)) {
|
727 |
p[2] = 4; /* WCE */ |
728 |
} |
729 |
p += 20;
|
730 |
} |
731 |
if ((page == 0x3f || page == 0x2a) |
732 |
&& (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM)) { |
733 |
/* CD Capabilities and Mechanical Status page. */
|
734 |
p[0] = 0x2a; |
735 |
p[1] = 0x14; |
736 |
p[2] = 3; // CD-R & CD-RW read |
737 |
p[3] = 0; // Writing not supported |
738 |
p[4] = 0x7f; /* Audio, composite, digital out, |
739 |
mode 2 form 1&2, multi session */
|
740 |
p[5] = 0xff; /* CD DA, DA accurate, RW supported, |
741 |
RW corrected, C2 errors, ISRC,
|
742 |
UPC, Bar code */
|
743 |
p[6] = 0x2d | (bdrv_is_locked(s->qdev.dinfo->bdrv)? 2 : 0); |
744 |
/* Locking supported, jumper present, eject, tray */
|
745 |
p[7] = 0; /* no volume & mute control, no |
746 |
changer */
|
747 |
p[8] = (50 * 176) >> 8; // 50x read speed |
748 |
p[9] = (50 * 176) & 0xff; |
749 |
p[10] = 0 >> 8; // No volume |
750 |
p[11] = 0 & 0xff; |
751 |
p[12] = 2048 >> 8; // 2M buffer |
752 |
p[13] = 2048 & 0xff; |
753 |
p[14] = (16 * 176) >> 8; // 16x read speed current |
754 |
p[15] = (16 * 176) & 0xff; |
755 |
p[18] = (16 * 176) >> 8; // 16x write speed |
756 |
p[19] = (16 * 176) & 0xff; |
757 |
p[20] = (16 * 176) >> 8; // 16x write speed current |
758 |
p[21] = (16 * 176) & 0xff; |
759 |
p += 22;
|
760 |
} |
761 |
r->iov.iov_len = p - outbuf; |
762 |
outbuf[0] = r->iov.iov_len - 4; |
763 |
if (r->iov.iov_len > len)
|
764 |
r->iov.iov_len = len; |
765 |
} |
766 |
break;
|
767 |
case START_STOP:
|
768 |
DPRINTF("Start Stop Unit\n");
|
769 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM &&
|
770 |
(buf[4] & 2)) |
771 |
/* load/eject medium */
|
772 |
bdrv_eject(s->qdev.dinfo->bdrv, !(buf[4] & 1)); |
773 |
break;
|
774 |
case ALLOW_MEDIUM_REMOVAL:
|
775 |
DPRINTF("Prevent Allow Medium Removal (prevent = %d)\n", buf[4] & 3); |
776 |
bdrv_set_locked(s->qdev.dinfo->bdrv, buf[4] & 1); |
777 |
break;
|
778 |
case READ_CAPACITY:
|
779 |
DPRINTF("Read Capacity\n");
|
780 |
/* The normal LEN field for this command is zero. */
|
781 |
memset(outbuf, 0, 8); |
782 |
bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors); |
783 |
nb_sectors /= s->cluster_size; |
784 |
/* Returned value is the address of the last sector. */
|
785 |
if (nb_sectors) {
|
786 |
nb_sectors--; |
787 |
/* Remember the new size for read/write sanity checking. */
|
788 |
s->max_lba = nb_sectors; |
789 |
/* Clip to 2TB, instead of returning capacity modulo 2TB. */
|
790 |
if (nb_sectors > UINT32_MAX)
|
791 |
nb_sectors = UINT32_MAX; |
792 |
outbuf[0] = (nb_sectors >> 24) & 0xff; |
793 |
outbuf[1] = (nb_sectors >> 16) & 0xff; |
794 |
outbuf[2] = (nb_sectors >> 8) & 0xff; |
795 |
outbuf[3] = nb_sectors & 0xff; |
796 |
outbuf[4] = 0; |
797 |
outbuf[5] = 0; |
798 |
outbuf[6] = s->cluster_size * 2; |
799 |
outbuf[7] = 0; |
800 |
r->iov.iov_len = 8;
|
801 |
} else {
|
802 |
scsi_command_complete(r, CHECK_CONDITION, NOT_READY); |
803 |
return 0; |
804 |
} |
805 |
break;
|
806 |
case READ_6:
|
807 |
case READ_10:
|
808 |
case 0x88: |
809 |
DPRINTF("Read (sector %" PRId64 ", count %d)\n", lba, len); |
810 |
if (lba > s->max_lba)
|
811 |
goto illegal_lba;
|
812 |
r->sector = lba * s->cluster_size; |
813 |
r->sector_count = len * s->cluster_size; |
814 |
break;
|
815 |
case WRITE_6:
|
816 |
case WRITE_10:
|
817 |
case 0x8a: |
818 |
DPRINTF("Write (sector %" PRId64 ", count %d)\n", lba, len); |
819 |
if (lba > s->max_lba)
|
820 |
goto illegal_lba;
|
821 |
r->sector = lba * s->cluster_size; |
822 |
r->sector_count = len * s->cluster_size; |
823 |
is_write = 1;
|
824 |
break;
|
825 |
case SYNCHRONIZE_CACHE:
|
826 |
DPRINTF("Synchronise cache (sector %" PRId64 ", count %d)\n", lba, len); |
827 |
bdrv_flush(s->qdev.dinfo->bdrv); |
828 |
break;
|
829 |
case READ_TOC:
|
830 |
{ |
831 |
int start_track, format, msf, toclen;
|
832 |
|
833 |
msf = buf[1] & 2; |
834 |
format = buf[2] & 0xf; |
835 |
start_track = buf[6];
|
836 |
bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors); |
837 |
DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1); |
838 |
nb_sectors /= s->cluster_size; |
839 |
switch(format) {
|
840 |
case 0: |
841 |
toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track); |
842 |
break;
|
843 |
case 1: |
844 |
/* multi session : only a single session defined */
|
845 |
toclen = 12;
|
846 |
memset(outbuf, 0, 12); |
847 |
outbuf[1] = 0x0a; |
848 |
outbuf[2] = 0x01; |
849 |
outbuf[3] = 0x01; |
850 |
break;
|
851 |
case 2: |
852 |
toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track); |
853 |
break;
|
854 |
default:
|
855 |
goto error_cmd;
|
856 |
} |
857 |
if (toclen > 0) { |
858 |
if (len > toclen)
|
859 |
len = toclen; |
860 |
r->iov.iov_len = len; |
861 |
break;
|
862 |
} |
863 |
error_cmd:
|
864 |
DPRINTF("Read TOC error\n");
|
865 |
goto fail;
|
866 |
} |
867 |
case 0x46: |
868 |
DPRINTF("Get Configuration (rt %d, maxlen %d)\n", buf[1] & 3, len); |
869 |
memset(outbuf, 0, 8); |
870 |
/* ??? This should probably return much more information. For now
|
871 |
just return the basic header indicating the CD-ROM profile. */
|
872 |
outbuf[7] = 8; // CD-ROM |
873 |
r->iov.iov_len = 8;
|
874 |
break;
|
875 |
case RESERVE_10:
|
876 |
DPRINTF("Reserve(10)\n");
|
877 |
if (buf[1] & 3) |
878 |
goto fail;
|
879 |
break;
|
880 |
case RELEASE_10:
|
881 |
DPRINTF("Release(10)\n");
|
882 |
if (buf[1] & 3) |
883 |
goto fail;
|
884 |
break;
|
885 |
case 0x9e: |
886 |
/* Service Action In subcommands. */
|
887 |
if ((buf[1] & 31) == 0x10) { |
888 |
DPRINTF("SAI READ CAPACITY(16)\n");
|
889 |
memset(outbuf, 0, len);
|
890 |
bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors); |
891 |
nb_sectors /= s->cluster_size; |
892 |
/* Returned value is the address of the last sector. */
|
893 |
if (nb_sectors) {
|
894 |
nb_sectors--; |
895 |
/* Remember the new size for read/write sanity checking. */
|
896 |
s->max_lba = nb_sectors; |
897 |
outbuf[0] = (nb_sectors >> 56) & 0xff; |
898 |
outbuf[1] = (nb_sectors >> 48) & 0xff; |
899 |
outbuf[2] = (nb_sectors >> 40) & 0xff; |
900 |
outbuf[3] = (nb_sectors >> 32) & 0xff; |
901 |
outbuf[4] = (nb_sectors >> 24) & 0xff; |
902 |
outbuf[5] = (nb_sectors >> 16) & 0xff; |
903 |
outbuf[6] = (nb_sectors >> 8) & 0xff; |
904 |
outbuf[7] = nb_sectors & 0xff; |
905 |
outbuf[8] = 0; |
906 |
outbuf[9] = 0; |
907 |
outbuf[10] = s->cluster_size * 2; |
908 |
outbuf[11] = 0; |
909 |
/* Protection, exponent and lowest lba field left blank. */
|
910 |
r->iov.iov_len = len; |
911 |
} else {
|
912 |
scsi_command_complete(r, CHECK_CONDITION, NOT_READY); |
913 |
return 0; |
914 |
} |
915 |
break;
|
916 |
} |
917 |
DPRINTF("Unsupported Service Action In\n");
|
918 |
goto fail;
|
919 |
case 0xa0: |
920 |
DPRINTF("Report LUNs (len %d)\n", len);
|
921 |
if (len < 16) |
922 |
goto fail;
|
923 |
memset(outbuf, 0, 16); |
924 |
outbuf[3] = 8; |
925 |
r->iov.iov_len = 16;
|
926 |
break;
|
927 |
case VERIFY:
|
928 |
DPRINTF("Verify (sector %" PRId64 ", count %d)\n", lba, len); |
929 |
break;
|
930 |
default:
|
931 |
DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]); |
932 |
fail:
|
933 |
scsi_command_complete(r, CHECK_CONDITION, ILLEGAL_REQUEST); |
934 |
return 0; |
935 |
illegal_lba:
|
936 |
scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR); |
937 |
return 0; |
938 |
} |
939 |
if (r->sector_count == 0 && r->iov.iov_len == 0) { |
940 |
scsi_command_complete(r, GOOD, NO_SENSE); |
941 |
} |
942 |
len = r->sector_count * 512 + r->iov.iov_len;
|
943 |
if (is_write) {
|
944 |
return -len;
|
945 |
} else {
|
946 |
if (!r->sector_count)
|
947 |
r->sector_count = -1;
|
948 |
return len;
|
949 |
} |
950 |
} |
951 |
|
952 |
static void scsi_destroy(SCSIDevice *dev) |
953 |
{ |
954 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
955 |
SCSIDiskReq *r; |
956 |
|
957 |
while (!QTAILQ_EMPTY(&s->qdev.requests)) {
|
958 |
r = DO_UPCAST(SCSIDiskReq, req, QTAILQ_FIRST(&s->qdev.requests)); |
959 |
scsi_remove_request(r); |
960 |
} |
961 |
drive_uninit(s->qdev.dinfo); |
962 |
} |
963 |
|
964 |
static int scsi_disk_initfn(SCSIDevice *dev) |
965 |
{ |
966 |
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
967 |
uint64_t nb_sectors; |
968 |
|
969 |
if (!s->qdev.dinfo || !s->qdev.dinfo->bdrv) {
|
970 |
qemu_error("scsi-disk: drive property not set\n");
|
971 |
return -1; |
972 |
} |
973 |
|
974 |
if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
|
975 |
s->cluster_size = 4;
|
976 |
} else {
|
977 |
s->cluster_size = 1;
|
978 |
} |
979 |
s->qdev.blocksize = 512 * s->cluster_size;
|
980 |
s->qdev.type = TYPE_DISK; |
981 |
bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors); |
982 |
nb_sectors /= s->cluster_size; |
983 |
if (nb_sectors)
|
984 |
nb_sectors--; |
985 |
s->max_lba = nb_sectors; |
986 |
strncpy(s->drive_serial_str, drive_get_serial(s->qdev.dinfo->bdrv), |
987 |
sizeof(s->drive_serial_str));
|
988 |
if (strlen(s->drive_serial_str) == 0) |
989 |
pstrcpy(s->drive_serial_str, sizeof(s->drive_serial_str), "0"); |
990 |
qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s); |
991 |
return 0; |
992 |
} |
993 |
|
994 |
static SCSIDeviceInfo scsi_disk_info = {
|
995 |
.qdev.name = "scsi-disk",
|
996 |
.qdev.desc = "virtual scsi disk or cdrom",
|
997 |
.qdev.size = sizeof(SCSIDiskState),
|
998 |
.init = scsi_disk_initfn, |
999 |
.destroy = scsi_destroy, |
1000 |
.send_command = scsi_send_command, |
1001 |
.read_data = scsi_read_data, |
1002 |
.write_data = scsi_write_data, |
1003 |
.cancel_io = scsi_cancel_io, |
1004 |
.get_buf = scsi_get_buf, |
1005 |
.qdev.props = (Property[]) { |
1006 |
DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.dinfo),
|
1007 |
DEFINE_PROP_END_OF_LIST(), |
1008 |
}, |
1009 |
}; |
1010 |
|
1011 |
static void scsi_disk_register_devices(void) |
1012 |
{ |
1013 |
scsi_qdev_register(&scsi_disk_info); |
1014 |
} |
1015 |
device_init(scsi_disk_register_devices) |