root / hw / scsi-bus.c @ 40723a99
History | View | Annotate | Download (47.4 kB)
1 |
#include "hw.h" |
---|---|
2 |
#include "qemu-error.h" |
3 |
#include "scsi.h" |
4 |
#include "scsi-defs.h" |
5 |
#include "qdev.h" |
6 |
#include "blockdev.h" |
7 |
#include "trace.h" |
8 |
#include "dma.h" |
9 |
|
10 |
static char *scsibus_get_dev_path(DeviceState *dev); |
11 |
static char *scsibus_get_fw_dev_path(DeviceState *dev); |
12 |
static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf); |
13 |
static void scsi_req_dequeue(SCSIRequest *req); |
14 |
|
15 |
static Property scsi_props[] = {
|
16 |
DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), |
17 |
DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), |
18 |
DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), |
19 |
DEFINE_PROP_END_OF_LIST(), |
20 |
}; |
21 |
|
22 |
static void scsi_bus_class_init(ObjectClass *klass, void *data) |
23 |
{ |
24 |
BusClass *k = BUS_CLASS(klass); |
25 |
|
26 |
k->get_dev_path = scsibus_get_dev_path; |
27 |
k->get_fw_dev_path = scsibus_get_fw_dev_path; |
28 |
} |
29 |
|
30 |
static const TypeInfo scsi_bus_info = { |
31 |
.name = TYPE_SCSI_BUS, |
32 |
.parent = TYPE_BUS, |
33 |
.instance_size = sizeof(SCSIBus),
|
34 |
.class_init = scsi_bus_class_init, |
35 |
}; |
36 |
static int next_scsi_bus; |
37 |
|
38 |
static int scsi_device_init(SCSIDevice *s) |
39 |
{ |
40 |
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); |
41 |
if (sc->init) {
|
42 |
return sc->init(s);
|
43 |
} |
44 |
return 0; |
45 |
} |
46 |
|
47 |
static void scsi_device_destroy(SCSIDevice *s) |
48 |
{ |
49 |
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); |
50 |
if (sc->destroy) {
|
51 |
sc->destroy(s); |
52 |
} |
53 |
} |
54 |
|
55 |
static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
|
56 |
uint8_t *buf, void *hba_private)
|
57 |
{ |
58 |
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); |
59 |
if (sc->alloc_req) {
|
60 |
return sc->alloc_req(s, tag, lun, buf, hba_private);
|
61 |
} |
62 |
|
63 |
return NULL; |
64 |
} |
65 |
|
66 |
static void scsi_device_unit_attention_reported(SCSIDevice *s) |
67 |
{ |
68 |
SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); |
69 |
if (sc->unit_attention_reported) {
|
70 |
sc->unit_attention_reported(s); |
71 |
} |
72 |
} |
73 |
|
74 |
/* Create a scsi bus, and attach devices to it. */
|
75 |
void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info) |
76 |
{ |
77 |
qbus_create_inplace(&bus->qbus, TYPE_SCSI_BUS, host, NULL);
|
78 |
bus->busnr = next_scsi_bus++; |
79 |
bus->info = info; |
80 |
bus->qbus.allow_hotplug = 1;
|
81 |
} |
82 |
|
83 |
static void scsi_dma_restart_bh(void *opaque) |
84 |
{ |
85 |
SCSIDevice *s = opaque; |
86 |
SCSIRequest *req, *next; |
87 |
|
88 |
qemu_bh_delete(s->bh); |
89 |
s->bh = NULL;
|
90 |
|
91 |
QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { |
92 |
scsi_req_ref(req); |
93 |
if (req->retry) {
|
94 |
req->retry = false;
|
95 |
switch (req->cmd.mode) {
|
96 |
case SCSI_XFER_FROM_DEV:
|
97 |
case SCSI_XFER_TO_DEV:
|
98 |
scsi_req_continue(req); |
99 |
break;
|
100 |
case SCSI_XFER_NONE:
|
101 |
assert(!req->sg); |
102 |
scsi_req_dequeue(req); |
103 |
scsi_req_enqueue(req); |
104 |
break;
|
105 |
} |
106 |
} |
107 |
scsi_req_unref(req); |
108 |
} |
109 |
} |
110 |
|
111 |
void scsi_req_retry(SCSIRequest *req)
|
112 |
{ |
113 |
/* No need to save a reference, because scsi_dma_restart_bh just
|
114 |
* looks at the request list. */
|
115 |
req->retry = true;
|
116 |
} |
117 |
|
118 |
static void scsi_dma_restart_cb(void *opaque, int running, RunState state) |
119 |
{ |
120 |
SCSIDevice *s = opaque; |
121 |
|
122 |
if (!running) {
|
123 |
return;
|
124 |
} |
125 |
if (!s->bh) {
|
126 |
s->bh = qemu_bh_new(scsi_dma_restart_bh, s); |
127 |
qemu_bh_schedule(s->bh); |
128 |
} |
129 |
} |
130 |
|
131 |
static int scsi_qdev_init(DeviceState *qdev) |
132 |
{ |
133 |
SCSIDevice *dev = SCSI_DEVICE(qdev); |
134 |
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); |
135 |
SCSIDevice *d; |
136 |
int rc = -1; |
137 |
|
138 |
if (dev->channel > bus->info->max_channel) {
|
139 |
error_report("bad scsi channel id: %d", dev->channel);
|
140 |
goto err;
|
141 |
} |
142 |
if (dev->id != -1 && dev->id > bus->info->max_target) { |
143 |
error_report("bad scsi device id: %d", dev->id);
|
144 |
goto err;
|
145 |
} |
146 |
if (dev->lun != -1 && dev->lun > bus->info->max_lun) { |
147 |
error_report("bad scsi device lun: %d", dev->lun);
|
148 |
goto err;
|
149 |
} |
150 |
|
151 |
if (dev->id == -1) { |
152 |
int id = -1; |
153 |
if (dev->lun == -1) { |
154 |
dev->lun = 0;
|
155 |
} |
156 |
do {
|
157 |
d = scsi_device_find(bus, dev->channel, ++id, dev->lun); |
158 |
} while (d && d->lun == dev->lun && id < bus->info->max_target);
|
159 |
if (d && d->lun == dev->lun) {
|
160 |
error_report("no free target");
|
161 |
goto err;
|
162 |
} |
163 |
dev->id = id; |
164 |
} else if (dev->lun == -1) { |
165 |
int lun = -1; |
166 |
do {
|
167 |
d = scsi_device_find(bus, dev->channel, dev->id, ++lun); |
168 |
} while (d && d->lun == lun && lun < bus->info->max_lun);
|
169 |
if (d && d->lun == lun) {
|
170 |
error_report("no free lun");
|
171 |
goto err;
|
172 |
} |
173 |
dev->lun = lun; |
174 |
} else {
|
175 |
d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); |
176 |
assert(d); |
177 |
if (d->lun == dev->lun && dev != d) {
|
178 |
qdev_free(&d->qdev); |
179 |
} |
180 |
} |
181 |
|
182 |
QTAILQ_INIT(&dev->requests); |
183 |
rc = scsi_device_init(dev); |
184 |
if (rc == 0) { |
185 |
dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, |
186 |
dev); |
187 |
} |
188 |
|
189 |
err:
|
190 |
return rc;
|
191 |
} |
192 |
|
193 |
static int scsi_qdev_exit(DeviceState *qdev) |
194 |
{ |
195 |
SCSIDevice *dev = SCSI_DEVICE(qdev); |
196 |
|
197 |
if (dev->vmsentry) {
|
198 |
qemu_del_vm_change_state_handler(dev->vmsentry); |
199 |
} |
200 |
scsi_device_destroy(dev); |
201 |
return 0; |
202 |
} |
203 |
|
204 |
/* handle legacy '-drive if=scsi,...' cmd line args */
|
205 |
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, |
206 |
int unit, bool removable, int bootindex) |
207 |
{ |
208 |
const char *driver; |
209 |
DeviceState *dev; |
210 |
|
211 |
driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk"; |
212 |
dev = qdev_create(&bus->qbus, driver); |
213 |
qdev_prop_set_uint32(dev, "scsi-id", unit);
|
214 |
if (bootindex >= 0) { |
215 |
qdev_prop_set_int32(dev, "bootindex", bootindex);
|
216 |
} |
217 |
if (object_property_find(OBJECT(dev), "removable", NULL)) { |
218 |
qdev_prop_set_bit(dev, "removable", removable);
|
219 |
} |
220 |
if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) { |
221 |
qdev_free(dev); |
222 |
return NULL; |
223 |
} |
224 |
if (qdev_init(dev) < 0) |
225 |
return NULL; |
226 |
return SCSI_DEVICE(dev);
|
227 |
} |
228 |
|
229 |
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
|
230 |
{ |
231 |
Location loc; |
232 |
DriveInfo *dinfo; |
233 |
int res = 0, unit; |
234 |
|
235 |
loc_push_none(&loc); |
236 |
for (unit = 0; unit <= bus->info->max_target; unit++) { |
237 |
dinfo = drive_get(IF_SCSI, bus->busnr, unit); |
238 |
if (dinfo == NULL) { |
239 |
continue;
|
240 |
} |
241 |
qemu_opts_loc_restore(dinfo->opts); |
242 |
if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) { |
243 |
res = -1;
|
244 |
break;
|
245 |
} |
246 |
} |
247 |
loc_pop(&loc); |
248 |
return res;
|
249 |
} |
250 |
|
251 |
static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
|
252 |
{ |
253 |
scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); |
254 |
scsi_req_complete(req, CHECK_CONDITION); |
255 |
return 0; |
256 |
} |
257 |
|
258 |
static const struct SCSIReqOps reqops_invalid_field = { |
259 |
.size = sizeof(SCSIRequest),
|
260 |
.send_command = scsi_invalid_field |
261 |
}; |
262 |
|
263 |
/* SCSIReqOps implementation for invalid commands. */
|
264 |
|
265 |
static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
|
266 |
{ |
267 |
scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); |
268 |
scsi_req_complete(req, CHECK_CONDITION); |
269 |
return 0; |
270 |
} |
271 |
|
272 |
static const struct SCSIReqOps reqops_invalid_opcode = { |
273 |
.size = sizeof(SCSIRequest),
|
274 |
.send_command = scsi_invalid_command |
275 |
}; |
276 |
|
277 |
/* SCSIReqOps implementation for unit attention conditions. */
|
278 |
|
279 |
static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
|
280 |
{ |
281 |
if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
|
282 |
scsi_req_build_sense(req, req->dev->unit_attention); |
283 |
} else if (req->bus->unit_attention.key == UNIT_ATTENTION) { |
284 |
scsi_req_build_sense(req, req->bus->unit_attention); |
285 |
} |
286 |
scsi_req_complete(req, CHECK_CONDITION); |
287 |
return 0; |
288 |
} |
289 |
|
290 |
static const struct SCSIReqOps reqops_unit_attention = { |
291 |
.size = sizeof(SCSIRequest),
|
292 |
.send_command = scsi_unit_attention |
293 |
}; |
294 |
|
295 |
/* SCSIReqOps implementation for REPORT LUNS and for commands sent to
|
296 |
an invalid LUN. */
|
297 |
|
298 |
typedef struct SCSITargetReq SCSITargetReq; |
299 |
|
300 |
struct SCSITargetReq {
|
301 |
SCSIRequest req; |
302 |
int len;
|
303 |
uint8_t buf[2056];
|
304 |
}; |
305 |
|
306 |
static void store_lun(uint8_t *outbuf, int lun) |
307 |
{ |
308 |
if (lun < 256) { |
309 |
outbuf[1] = lun;
|
310 |
return;
|
311 |
} |
312 |
outbuf[1] = (lun & 255); |
313 |
outbuf[0] = (lun >> 8) | 0x40; |
314 |
} |
315 |
|
316 |
static bool scsi_target_emulate_report_luns(SCSITargetReq *r) |
317 |
{ |
318 |
BusChild *kid; |
319 |
int i, len, n;
|
320 |
int channel, id;
|
321 |
bool found_lun0;
|
322 |
|
323 |
if (r->req.cmd.xfer < 16) { |
324 |
return false; |
325 |
} |
326 |
if (r->req.cmd.buf[2] > 2) { |
327 |
return false; |
328 |
} |
329 |
channel = r->req.dev->channel; |
330 |
id = r->req.dev->id; |
331 |
found_lun0 = false;
|
332 |
n = 0;
|
333 |
QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
334 |
DeviceState *qdev = kid->child; |
335 |
SCSIDevice *dev = SCSI_DEVICE(qdev); |
336 |
|
337 |
if (dev->channel == channel && dev->id == id) {
|
338 |
if (dev->lun == 0) { |
339 |
found_lun0 = true;
|
340 |
} |
341 |
n += 8;
|
342 |
} |
343 |
} |
344 |
if (!found_lun0) {
|
345 |
n += 8;
|
346 |
} |
347 |
len = MIN(n + 8, r->req.cmd.xfer & ~7); |
348 |
if (len > sizeof(r->buf)) { |
349 |
/* TODO: > 256 LUNs? */
|
350 |
return false; |
351 |
} |
352 |
|
353 |
memset(r->buf, 0, len);
|
354 |
stl_be_p(&r->buf, n); |
355 |
i = found_lun0 ? 8 : 16; |
356 |
QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
357 |
DeviceState *qdev = kid->child; |
358 |
SCSIDevice *dev = SCSI_DEVICE(qdev); |
359 |
|
360 |
if (dev->channel == channel && dev->id == id) {
|
361 |
store_lun(&r->buf[i], dev->lun); |
362 |
i += 8;
|
363 |
} |
364 |
} |
365 |
assert(i == n + 8);
|
366 |
r->len = len; |
367 |
return true; |
368 |
} |
369 |
|
370 |
static bool scsi_target_emulate_inquiry(SCSITargetReq *r) |
371 |
{ |
372 |
assert(r->req.dev->lun != r->req.lun); |
373 |
if (r->req.cmd.buf[1] & 0x2) { |
374 |
/* Command support data - optional, not implemented */
|
375 |
return false; |
376 |
} |
377 |
|
378 |
if (r->req.cmd.buf[1] & 0x1) { |
379 |
/* Vital product data */
|
380 |
uint8_t page_code = r->req.cmd.buf[2];
|
381 |
r->buf[r->len++] = page_code ; /* this page */
|
382 |
r->buf[r->len++] = 0x00;
|
383 |
|
384 |
switch (page_code) {
|
385 |
case 0x00: /* Supported page codes, mandatory */ |
386 |
{ |
387 |
int pages;
|
388 |
pages = r->len++; |
389 |
r->buf[r->len++] = 0x00; /* list of supported pages (this page) */ |
390 |
r->buf[pages] = r->len - pages - 1; /* number of pages */ |
391 |
break;
|
392 |
} |
393 |
default:
|
394 |
return false; |
395 |
} |
396 |
/* done with EVPD */
|
397 |
assert(r->len < sizeof(r->buf));
|
398 |
r->len = MIN(r->req.cmd.xfer, r->len); |
399 |
return true; |
400 |
} |
401 |
|
402 |
/* Standard INQUIRY data */
|
403 |
if (r->req.cmd.buf[2] != 0) { |
404 |
return false; |
405 |
} |
406 |
|
407 |
/* PAGE CODE == 0 */
|
408 |
r->len = MIN(r->req.cmd.xfer, 36);
|
409 |
memset(r->buf, 0, r->len);
|
410 |
if (r->req.lun != 0) { |
411 |
r->buf[0] = TYPE_NO_LUN;
|
412 |
} else {
|
413 |
r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
|
414 |
r->buf[2] = 5; /* Version */ |
415 |
r->buf[3] = 2 | 0x10; /* HiSup, response data format */ |
416 |
r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */ |
417 |
r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */ |
418 |
memcpy(&r->buf[8], "QEMU ", 8); |
419 |
memcpy(&r->buf[16], "QEMU TARGET ", 16); |
420 |
pstrcpy((char *) &r->buf[32], 4, qemu_get_version()); |
421 |
} |
422 |
return true; |
423 |
} |
424 |
|
425 |
static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
|
426 |
{ |
427 |
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); |
428 |
|
429 |
switch (buf[0]) { |
430 |
case REPORT_LUNS:
|
431 |
if (!scsi_target_emulate_report_luns(r)) {
|
432 |
goto illegal_request;
|
433 |
} |
434 |
break;
|
435 |
case INQUIRY:
|
436 |
if (!scsi_target_emulate_inquiry(r)) {
|
437 |
goto illegal_request;
|
438 |
} |
439 |
break;
|
440 |
case REQUEST_SENSE:
|
441 |
r->len = scsi_device_get_sense(r->req.dev, r->buf, |
442 |
MIN(req->cmd.xfer, sizeof r->buf),
|
443 |
(req->cmd.buf[1] & 1) == 0); |
444 |
if (r->req.dev->sense_is_ua) {
|
445 |
scsi_device_unit_attention_reported(req->dev); |
446 |
r->req.dev->sense_len = 0;
|
447 |
r->req.dev->sense_is_ua = false;
|
448 |
} |
449 |
break;
|
450 |
default:
|
451 |
scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED)); |
452 |
scsi_req_complete(req, CHECK_CONDITION); |
453 |
return 0; |
454 |
illegal_request:
|
455 |
scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); |
456 |
scsi_req_complete(req, CHECK_CONDITION); |
457 |
return 0; |
458 |
} |
459 |
|
460 |
if (!r->len) {
|
461 |
scsi_req_complete(req, GOOD); |
462 |
} |
463 |
return r->len;
|
464 |
} |
465 |
|
466 |
static void scsi_target_read_data(SCSIRequest *req) |
467 |
{ |
468 |
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); |
469 |
uint32_t n; |
470 |
|
471 |
n = r->len; |
472 |
if (n > 0) { |
473 |
r->len = 0;
|
474 |
scsi_req_data(&r->req, n); |
475 |
} else {
|
476 |
scsi_req_complete(&r->req, GOOD); |
477 |
} |
478 |
} |
479 |
|
480 |
static uint8_t *scsi_target_get_buf(SCSIRequest *req)
|
481 |
{ |
482 |
SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); |
483 |
|
484 |
return r->buf;
|
485 |
} |
486 |
|
487 |
static const struct SCSIReqOps reqops_target_command = { |
488 |
.size = sizeof(SCSITargetReq),
|
489 |
.send_command = scsi_target_send_command, |
490 |
.read_data = scsi_target_read_data, |
491 |
.get_buf = scsi_target_get_buf, |
492 |
}; |
493 |
|
494 |
|
495 |
SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
|
496 |
uint32_t tag, uint32_t lun, void *hba_private)
|
497 |
{ |
498 |
SCSIRequest *req; |
499 |
|
500 |
req = g_malloc0(reqops->size); |
501 |
req->refcount = 1;
|
502 |
req->bus = scsi_bus_from_device(d); |
503 |
req->dev = d; |
504 |
req->tag = tag; |
505 |
req->lun = lun; |
506 |
req->hba_private = hba_private; |
507 |
req->status = -1;
|
508 |
req->sense_len = 0;
|
509 |
req->ops = reqops; |
510 |
trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); |
511 |
return req;
|
512 |
} |
513 |
|
514 |
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, |
515 |
uint8_t *buf, void *hba_private)
|
516 |
{ |
517 |
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); |
518 |
SCSIRequest *req; |
519 |
SCSICommand cmd; |
520 |
|
521 |
if (scsi_req_parse(&cmd, d, buf) != 0) { |
522 |
trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
|
523 |
req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private); |
524 |
} else {
|
525 |
trace_scsi_req_parsed(d->id, lun, tag, buf[0],
|
526 |
cmd.mode, cmd.xfer); |
527 |
if (cmd.lba != -1) { |
528 |
trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
|
529 |
cmd.lba); |
530 |
} |
531 |
|
532 |
if (cmd.xfer > INT32_MAX) {
|
533 |
req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private); |
534 |
} else if ((d->unit_attention.key == UNIT_ATTENTION || |
535 |
bus->unit_attention.key == UNIT_ATTENTION) && |
536 |
(buf[0] != INQUIRY &&
|
537 |
buf[0] != REPORT_LUNS &&
|
538 |
buf[0] != GET_CONFIGURATION &&
|
539 |
buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
|
540 |
|
541 |
/*
|
542 |
* If we already have a pending unit attention condition,
|
543 |
* report this one before triggering another one.
|
544 |
*/
|
545 |
!(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
|
546 |
req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun, |
547 |
hba_private); |
548 |
} else if (lun != d->lun || |
549 |
buf[0] == REPORT_LUNS ||
|
550 |
(buf[0] == REQUEST_SENSE && d->sense_len)) {
|
551 |
req = scsi_req_alloc(&reqops_target_command, d, tag, lun, |
552 |
hba_private); |
553 |
} else {
|
554 |
req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); |
555 |
} |
556 |
} |
557 |
|
558 |
req->cmd = cmd; |
559 |
req->resid = req->cmd.xfer; |
560 |
|
561 |
switch (buf[0]) { |
562 |
case INQUIRY:
|
563 |
trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]); |
564 |
break;
|
565 |
case TEST_UNIT_READY:
|
566 |
trace_scsi_test_unit_ready(d->id, lun, tag); |
567 |
break;
|
568 |
case REPORT_LUNS:
|
569 |
trace_scsi_report_luns(d->id, lun, tag); |
570 |
break;
|
571 |
case REQUEST_SENSE:
|
572 |
trace_scsi_request_sense(d->id, lun, tag); |
573 |
break;
|
574 |
default:
|
575 |
break;
|
576 |
} |
577 |
|
578 |
return req;
|
579 |
} |
580 |
|
581 |
uint8_t *scsi_req_get_buf(SCSIRequest *req) |
582 |
{ |
583 |
return req->ops->get_buf(req);
|
584 |
} |
585 |
|
586 |
static void scsi_clear_unit_attention(SCSIRequest *req) |
587 |
{ |
588 |
SCSISense *ua; |
589 |
if (req->dev->unit_attention.key != UNIT_ATTENTION &&
|
590 |
req->bus->unit_attention.key != UNIT_ATTENTION) { |
591 |
return;
|
592 |
} |
593 |
|
594 |
/*
|
595 |
* If an INQUIRY command enters the enabled command state,
|
596 |
* the device server shall [not] clear any unit attention condition;
|
597 |
* See also MMC-6, paragraphs 6.5 and 6.6.2.
|
598 |
*/
|
599 |
if (req->cmd.buf[0] == INQUIRY || |
600 |
req->cmd.buf[0] == GET_CONFIGURATION ||
|
601 |
req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
|
602 |
return;
|
603 |
} |
604 |
|
605 |
if (req->dev->unit_attention.key == UNIT_ATTENTION) {
|
606 |
ua = &req->dev->unit_attention; |
607 |
} else {
|
608 |
ua = &req->bus->unit_attention; |
609 |
} |
610 |
|
611 |
/*
|
612 |
* If a REPORT LUNS command enters the enabled command state, [...]
|
613 |
* the device server shall clear any pending unit attention condition
|
614 |
* with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
|
615 |
*/
|
616 |
if (req->cmd.buf[0] == REPORT_LUNS && |
617 |
!(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc && |
618 |
ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) { |
619 |
return;
|
620 |
} |
621 |
|
622 |
*ua = SENSE_CODE(NO_SENSE); |
623 |
} |
624 |
|
625 |
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) |
626 |
{ |
627 |
int ret;
|
628 |
|
629 |
assert(len >= 14);
|
630 |
if (!req->sense_len) {
|
631 |
return 0; |
632 |
} |
633 |
|
634 |
ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
|
635 |
|
636 |
/*
|
637 |
* FIXME: clearing unit attention conditions upon autosense should be done
|
638 |
* only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
|
639 |
* (SAM-5, 5.14).
|
640 |
*
|
641 |
* We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
|
642 |
* 10b for HBAs that do not support it (do not call scsi_req_get_sense).
|
643 |
* Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
|
644 |
*/
|
645 |
if (req->dev->sense_is_ua) {
|
646 |
scsi_device_unit_attention_reported(req->dev); |
647 |
req->dev->sense_len = 0;
|
648 |
req->dev->sense_is_ua = false;
|
649 |
} |
650 |
return ret;
|
651 |
} |
652 |
|
653 |
int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed) |
654 |
{ |
655 |
return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
|
656 |
} |
657 |
|
658 |
void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
|
659 |
{ |
660 |
trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag, |
661 |
sense.key, sense.asc, sense.ascq); |
662 |
memset(req->sense, 0, 18); |
663 |
req->sense[0] = 0x70; |
664 |
req->sense[2] = sense.key;
|
665 |
req->sense[7] = 10; |
666 |
req->sense[12] = sense.asc;
|
667 |
req->sense[13] = sense.ascq;
|
668 |
req->sense_len = 18;
|
669 |
} |
670 |
|
671 |
static void scsi_req_enqueue_internal(SCSIRequest *req) |
672 |
{ |
673 |
assert(!req->enqueued); |
674 |
scsi_req_ref(req); |
675 |
if (req->bus->info->get_sg_list) {
|
676 |
req->sg = req->bus->info->get_sg_list(req); |
677 |
} else {
|
678 |
req->sg = NULL;
|
679 |
} |
680 |
req->enqueued = true;
|
681 |
QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); |
682 |
} |
683 |
|
684 |
int32_t scsi_req_enqueue(SCSIRequest *req) |
685 |
{ |
686 |
int32_t rc; |
687 |
|
688 |
assert(!req->retry); |
689 |
scsi_req_enqueue_internal(req); |
690 |
scsi_req_ref(req); |
691 |
rc = req->ops->send_command(req, req->cmd.buf); |
692 |
scsi_req_unref(req); |
693 |
return rc;
|
694 |
} |
695 |
|
696 |
static void scsi_req_dequeue(SCSIRequest *req) |
697 |
{ |
698 |
trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); |
699 |
req->retry = false;
|
700 |
if (req->enqueued) {
|
701 |
QTAILQ_REMOVE(&req->dev->requests, req, next); |
702 |
req->enqueued = false;
|
703 |
scsi_req_unref(req); |
704 |
} |
705 |
} |
706 |
|
707 |
static int scsi_get_performance_length(int num_desc, int type, int data_type) |
708 |
{ |
709 |
/* MMC-6, paragraph 6.7. */
|
710 |
switch (type) {
|
711 |
case 0: |
712 |
if ((data_type & 3) == 0) { |
713 |
/* Each descriptor is as in Table 295 - Nominal performance. */
|
714 |
return 16 * num_desc + 8; |
715 |
} else {
|
716 |
/* Each descriptor is as in Table 296 - Exceptions. */
|
717 |
return 6 * num_desc + 8; |
718 |
} |
719 |
case 1: |
720 |
case 4: |
721 |
case 5: |
722 |
return 8 * num_desc + 8; |
723 |
case 2: |
724 |
return 2048 * num_desc + 8; |
725 |
case 3: |
726 |
return 16 * num_desc + 8; |
727 |
default:
|
728 |
return 8; |
729 |
} |
730 |
} |
731 |
|
732 |
static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
733 |
{ |
734 |
switch (buf[0] >> 5) { |
735 |
case 0: |
736 |
cmd->xfer = buf[4];
|
737 |
break;
|
738 |
case 1: |
739 |
case 2: |
740 |
cmd->xfer = lduw_be_p(&buf[7]);
|
741 |
break;
|
742 |
case 4: |
743 |
cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL; |
744 |
break;
|
745 |
case 5: |
746 |
cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL; |
747 |
break;
|
748 |
default:
|
749 |
return -1; |
750 |
} |
751 |
|
752 |
switch (buf[0]) { |
753 |
case TEST_UNIT_READY:
|
754 |
case REWIND:
|
755 |
case START_STOP:
|
756 |
case SET_CAPACITY:
|
757 |
case WRITE_FILEMARKS:
|
758 |
case WRITE_FILEMARKS_16:
|
759 |
case SPACE:
|
760 |
case RESERVE:
|
761 |
case RELEASE:
|
762 |
case ERASE:
|
763 |
case ALLOW_MEDIUM_REMOVAL:
|
764 |
case VERIFY_10:
|
765 |
case SEEK_10:
|
766 |
case SYNCHRONIZE_CACHE:
|
767 |
case SYNCHRONIZE_CACHE_16:
|
768 |
case LOCATE_16:
|
769 |
case LOCK_UNLOCK_CACHE:
|
770 |
case SET_CD_SPEED:
|
771 |
case SET_LIMITS:
|
772 |
case WRITE_LONG_10:
|
773 |
case UPDATE_BLOCK:
|
774 |
case RESERVE_TRACK:
|
775 |
case SET_READ_AHEAD:
|
776 |
case PRE_FETCH:
|
777 |
case PRE_FETCH_16:
|
778 |
case ALLOW_OVERWRITE:
|
779 |
cmd->xfer = 0;
|
780 |
break;
|
781 |
case MODE_SENSE:
|
782 |
break;
|
783 |
case WRITE_SAME_10:
|
784 |
case WRITE_SAME_16:
|
785 |
cmd->xfer = dev->blocksize; |
786 |
break;
|
787 |
case READ_CAPACITY_10:
|
788 |
cmd->xfer = 8;
|
789 |
break;
|
790 |
case READ_BLOCK_LIMITS:
|
791 |
cmd->xfer = 6;
|
792 |
break;
|
793 |
case SEND_VOLUME_TAG:
|
794 |
/* GPCMD_SET_STREAMING from multimedia commands. */
|
795 |
if (dev->type == TYPE_ROM) {
|
796 |
cmd->xfer = buf[10] | (buf[9] << 8); |
797 |
} else {
|
798 |
cmd->xfer = buf[9] | (buf[8] << 8); |
799 |
} |
800 |
break;
|
801 |
case WRITE_6:
|
802 |
/* length 0 means 256 blocks */
|
803 |
if (cmd->xfer == 0) { |
804 |
cmd->xfer = 256;
|
805 |
} |
806 |
case WRITE_10:
|
807 |
case WRITE_VERIFY_10:
|
808 |
case WRITE_12:
|
809 |
case WRITE_VERIFY_12:
|
810 |
case WRITE_16:
|
811 |
case WRITE_VERIFY_16:
|
812 |
cmd->xfer *= dev->blocksize; |
813 |
break;
|
814 |
case READ_6:
|
815 |
case READ_REVERSE:
|
816 |
/* length 0 means 256 blocks */
|
817 |
if (cmd->xfer == 0) { |
818 |
cmd->xfer = 256;
|
819 |
} |
820 |
case READ_10:
|
821 |
case RECOVER_BUFFERED_DATA:
|
822 |
case READ_12:
|
823 |
case READ_16:
|
824 |
cmd->xfer *= dev->blocksize; |
825 |
break;
|
826 |
case FORMAT_UNIT:
|
827 |
/* MMC mandates the parameter list to be 12-bytes long. Parameters
|
828 |
* for block devices are restricted to the header right now. */
|
829 |
if (dev->type == TYPE_ROM && (buf[1] & 16)) { |
830 |
cmd->xfer = 12;
|
831 |
} else {
|
832 |
cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4); |
833 |
} |
834 |
break;
|
835 |
case INQUIRY:
|
836 |
case RECEIVE_DIAGNOSTIC:
|
837 |
case SEND_DIAGNOSTIC:
|
838 |
cmd->xfer = buf[4] | (buf[3] << 8); |
839 |
break;
|
840 |
case READ_CD:
|
841 |
case READ_BUFFER:
|
842 |
case WRITE_BUFFER:
|
843 |
case SEND_CUE_SHEET:
|
844 |
cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); |
845 |
break;
|
846 |
case PERSISTENT_RESERVE_OUT:
|
847 |
cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL; |
848 |
break;
|
849 |
case ERASE_12:
|
850 |
if (dev->type == TYPE_ROM) {
|
851 |
/* MMC command GET PERFORMANCE. */
|
852 |
cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8), |
853 |
buf[10], buf[1] & 0x1f); |
854 |
} |
855 |
break;
|
856 |
case MECHANISM_STATUS:
|
857 |
case READ_DVD_STRUCTURE:
|
858 |
case SEND_DVD_STRUCTURE:
|
859 |
case MAINTENANCE_OUT:
|
860 |
case MAINTENANCE_IN:
|
861 |
if (dev->type == TYPE_ROM) {
|
862 |
/* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
|
863 |
cmd->xfer = buf[9] | (buf[8] << 8); |
864 |
} |
865 |
break;
|
866 |
} |
867 |
return 0; |
868 |
} |
869 |
|
870 |
static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
871 |
{ |
872 |
switch (buf[0]) { |
873 |
/* stream commands */
|
874 |
case ERASE_12:
|
875 |
case ERASE_16:
|
876 |
cmd->xfer = 0;
|
877 |
break;
|
878 |
case READ_6:
|
879 |
case READ_REVERSE:
|
880 |
case RECOVER_BUFFERED_DATA:
|
881 |
case WRITE_6:
|
882 |
cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); |
883 |
if (buf[1] & 0x01) { /* fixed */ |
884 |
cmd->xfer *= dev->blocksize; |
885 |
} |
886 |
break;
|
887 |
case READ_16:
|
888 |
case READ_REVERSE_16:
|
889 |
case VERIFY_16:
|
890 |
case WRITE_16:
|
891 |
cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16); |
892 |
if (buf[1] & 0x01) { /* fixed */ |
893 |
cmd->xfer *= dev->blocksize; |
894 |
} |
895 |
break;
|
896 |
case REWIND:
|
897 |
case LOAD_UNLOAD:
|
898 |
cmd->xfer = 0;
|
899 |
break;
|
900 |
case SPACE_16:
|
901 |
cmd->xfer = buf[13] | (buf[12] << 8); |
902 |
break;
|
903 |
case READ_POSITION:
|
904 |
cmd->xfer = buf[8] | (buf[7] << 8); |
905 |
break;
|
906 |
case FORMAT_UNIT:
|
907 |
cmd->xfer = buf[4] | (buf[3] << 8); |
908 |
break;
|
909 |
/* generic commands */
|
910 |
default:
|
911 |
return scsi_req_length(cmd, dev, buf);
|
912 |
} |
913 |
return 0; |
914 |
} |
915 |
|
916 |
static int scsi_req_medium_changer_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
917 |
{ |
918 |
switch (buf[0]) { |
919 |
/* medium changer commands */
|
920 |
case EXCHANGE_MEDIUM:
|
921 |
case INITIALIZE_ELEMENT_STATUS:
|
922 |
case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
|
923 |
case MOVE_MEDIUM:
|
924 |
case POSITION_TO_ELEMENT:
|
925 |
cmd->xfer = 0;
|
926 |
break;
|
927 |
case READ_ELEMENT_STATUS:
|
928 |
cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16); |
929 |
break;
|
930 |
|
931 |
/* generic commands */
|
932 |
default:
|
933 |
return scsi_req_length(cmd, dev, buf);
|
934 |
} |
935 |
return 0; |
936 |
} |
937 |
|
938 |
|
939 |
static void scsi_cmd_xfer_mode(SCSICommand *cmd) |
940 |
{ |
941 |
if (!cmd->xfer) {
|
942 |
cmd->mode = SCSI_XFER_NONE; |
943 |
return;
|
944 |
} |
945 |
switch (cmd->buf[0]) { |
946 |
case WRITE_6:
|
947 |
case WRITE_10:
|
948 |
case WRITE_VERIFY_10:
|
949 |
case WRITE_12:
|
950 |
case WRITE_VERIFY_12:
|
951 |
case WRITE_16:
|
952 |
case WRITE_VERIFY_16:
|
953 |
case COPY:
|
954 |
case COPY_VERIFY:
|
955 |
case COMPARE:
|
956 |
case CHANGE_DEFINITION:
|
957 |
case LOG_SELECT:
|
958 |
case MODE_SELECT:
|
959 |
case MODE_SELECT_10:
|
960 |
case SEND_DIAGNOSTIC:
|
961 |
case WRITE_BUFFER:
|
962 |
case FORMAT_UNIT:
|
963 |
case REASSIGN_BLOCKS:
|
964 |
case SEARCH_EQUAL:
|
965 |
case SEARCH_HIGH:
|
966 |
case SEARCH_LOW:
|
967 |
case UPDATE_BLOCK:
|
968 |
case WRITE_LONG_10:
|
969 |
case WRITE_SAME_10:
|
970 |
case WRITE_SAME_16:
|
971 |
case UNMAP:
|
972 |
case SEARCH_HIGH_12:
|
973 |
case SEARCH_EQUAL_12:
|
974 |
case SEARCH_LOW_12:
|
975 |
case MEDIUM_SCAN:
|
976 |
case SEND_VOLUME_TAG:
|
977 |
case SEND_CUE_SHEET:
|
978 |
case SEND_DVD_STRUCTURE:
|
979 |
case PERSISTENT_RESERVE_OUT:
|
980 |
case MAINTENANCE_OUT:
|
981 |
case ATA_PASSTHROUGH:
|
982 |
cmd->mode = SCSI_XFER_TO_DEV; |
983 |
break;
|
984 |
default:
|
985 |
cmd->mode = SCSI_XFER_FROM_DEV; |
986 |
break;
|
987 |
} |
988 |
} |
989 |
|
990 |
static uint64_t scsi_cmd_lba(SCSICommand *cmd)
|
991 |
{ |
992 |
uint8_t *buf = cmd->buf; |
993 |
uint64_t lba; |
994 |
|
995 |
switch (buf[0] >> 5) { |
996 |
case 0: |
997 |
lba = ldl_be_p(&buf[0]) & 0x1fffff; |
998 |
break;
|
999 |
case 1: |
1000 |
case 2: |
1001 |
case 5: |
1002 |
lba = ldl_be_p(&buf[2]) & 0xffffffffULL; |
1003 |
break;
|
1004 |
case 4: |
1005 |
lba = ldq_be_p(&buf[2]);
|
1006 |
break;
|
1007 |
default:
|
1008 |
lba = -1;
|
1009 |
|
1010 |
} |
1011 |
return lba;
|
1012 |
} |
1013 |
|
1014 |
int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
|
1015 |
{ |
1016 |
int rc;
|
1017 |
|
1018 |
switch (buf[0] >> 5) { |
1019 |
case 0: |
1020 |
cmd->len = 6;
|
1021 |
break;
|
1022 |
case 1: |
1023 |
case 2: |
1024 |
cmd->len = 10;
|
1025 |
break;
|
1026 |
case 4: |
1027 |
cmd->len = 16;
|
1028 |
break;
|
1029 |
case 5: |
1030 |
cmd->len = 12;
|
1031 |
break;
|
1032 |
default:
|
1033 |
return -1; |
1034 |
} |
1035 |
|
1036 |
switch (dev->type) {
|
1037 |
case TYPE_TAPE:
|
1038 |
rc = scsi_req_stream_length(cmd, dev, buf); |
1039 |
break;
|
1040 |
case TYPE_MEDIUM_CHANGER:
|
1041 |
rc = scsi_req_medium_changer_length(cmd, dev, buf); |
1042 |
break;
|
1043 |
default:
|
1044 |
rc = scsi_req_length(cmd, dev, buf); |
1045 |
break;
|
1046 |
} |
1047 |
|
1048 |
if (rc != 0) |
1049 |
return rc;
|
1050 |
|
1051 |
memcpy(cmd->buf, buf, cmd->len); |
1052 |
scsi_cmd_xfer_mode(cmd); |
1053 |
cmd->lba = scsi_cmd_lba(cmd); |
1054 |
return 0; |
1055 |
} |
1056 |
|
1057 |
/*
|
1058 |
* Predefined sense codes
|
1059 |
*/
|
1060 |
|
1061 |
/* No sense data available */
|
1062 |
const struct SCSISense sense_code_NO_SENSE = { |
1063 |
.key = NO_SENSE , .asc = 0x00 , .ascq = 0x00 |
1064 |
}; |
1065 |
|
1066 |
/* LUN not ready, Manual intervention required */
|
1067 |
const struct SCSISense sense_code_LUN_NOT_READY = { |
1068 |
.key = NOT_READY, .asc = 0x04, .ascq = 0x03 |
1069 |
}; |
1070 |
|
1071 |
/* LUN not ready, Medium not present */
|
1072 |
const struct SCSISense sense_code_NO_MEDIUM = { |
1073 |
.key = NOT_READY, .asc = 0x3a, .ascq = 0x00 |
1074 |
}; |
1075 |
|
1076 |
/* LUN not ready, medium removal prevented */
|
1077 |
const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = { |
1078 |
.key = NOT_READY, .asc = 0x53, .ascq = 0x00 |
1079 |
}; |
1080 |
|
1081 |
/* Hardware error, internal target failure */
|
1082 |
const struct SCSISense sense_code_TARGET_FAILURE = { |
1083 |
.key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00 |
1084 |
}; |
1085 |
|
1086 |
/* Illegal request, invalid command operation code */
|
1087 |
const struct SCSISense sense_code_INVALID_OPCODE = { |
1088 |
.key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00 |
1089 |
}; |
1090 |
|
1091 |
/* Illegal request, LBA out of range */
|
1092 |
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = { |
1093 |
.key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00 |
1094 |
}; |
1095 |
|
1096 |
/* Illegal request, Invalid field in CDB */
|
1097 |
const struct SCSISense sense_code_INVALID_FIELD = { |
1098 |
.key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00 |
1099 |
}; |
1100 |
|
1101 |
/* Illegal request, LUN not supported */
|
1102 |
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = { |
1103 |
.key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00 |
1104 |
}; |
1105 |
|
1106 |
/* Illegal request, Saving parameters not supported */
|
1107 |
const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = { |
1108 |
.key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00 |
1109 |
}; |
1110 |
|
1111 |
/* Illegal request, Incompatible medium installed */
|
1112 |
const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = { |
1113 |
.key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00 |
1114 |
}; |
1115 |
|
1116 |
/* Illegal request, medium removal prevented */
|
1117 |
const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = { |
1118 |
.key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00 |
1119 |
}; |
1120 |
|
1121 |
/* Command aborted, I/O process terminated */
|
1122 |
const struct SCSISense sense_code_IO_ERROR = { |
1123 |
.key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06 |
1124 |
}; |
1125 |
|
1126 |
/* Command aborted, I_T Nexus loss occurred */
|
1127 |
const struct SCSISense sense_code_I_T_NEXUS_LOSS = { |
1128 |
.key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07 |
1129 |
}; |
1130 |
|
1131 |
/* Command aborted, Logical Unit failure */
|
1132 |
const struct SCSISense sense_code_LUN_FAILURE = { |
1133 |
.key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01 |
1134 |
}; |
1135 |
|
1136 |
/* Unit attention, Power on, reset or bus device reset occurred */
|
1137 |
const struct SCSISense sense_code_RESET = { |
1138 |
.key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00 |
1139 |
}; |
1140 |
|
1141 |
/* Unit attention, No medium */
|
1142 |
const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = { |
1143 |
.key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00 |
1144 |
}; |
1145 |
|
1146 |
/* Unit attention, Medium may have changed */
|
1147 |
const struct SCSISense sense_code_MEDIUM_CHANGED = { |
1148 |
.key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00 |
1149 |
}; |
1150 |
|
1151 |
/* Unit attention, Reported LUNs data has changed */
|
1152 |
const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = { |
1153 |
.key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e |
1154 |
}; |
1155 |
|
1156 |
/* Unit attention, Device internal reset */
|
1157 |
const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = { |
1158 |
.key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04 |
1159 |
}; |
1160 |
|
1161 |
/*
|
1162 |
* scsi_build_sense
|
1163 |
*
|
1164 |
* Convert between fixed and descriptor sense buffers
|
1165 |
*/
|
1166 |
int scsi_build_sense(uint8_t *in_buf, int in_len, |
1167 |
uint8_t *buf, int len, bool fixed) |
1168 |
{ |
1169 |
bool fixed_in;
|
1170 |
SCSISense sense; |
1171 |
if (!fixed && len < 8) { |
1172 |
return 0; |
1173 |
} |
1174 |
|
1175 |
if (in_len == 0) { |
1176 |
sense.key = NO_SENSE; |
1177 |
sense.asc = 0;
|
1178 |
sense.ascq = 0;
|
1179 |
} else {
|
1180 |
fixed_in = (in_buf[0] & 2) == 0; |
1181 |
|
1182 |
if (fixed == fixed_in) {
|
1183 |
memcpy(buf, in_buf, MIN(len, in_len)); |
1184 |
return MIN(len, in_len);
|
1185 |
} |
1186 |
|
1187 |
if (fixed_in) {
|
1188 |
sense.key = in_buf[2];
|
1189 |
sense.asc = in_buf[12];
|
1190 |
sense.ascq = in_buf[13];
|
1191 |
} else {
|
1192 |
sense.key = in_buf[1];
|
1193 |
sense.asc = in_buf[2];
|
1194 |
sense.ascq = in_buf[3];
|
1195 |
} |
1196 |
} |
1197 |
|
1198 |
memset(buf, 0, len);
|
1199 |
if (fixed) {
|
1200 |
/* Return fixed format sense buffer */
|
1201 |
buf[0] = 0x70; |
1202 |
buf[2] = sense.key;
|
1203 |
buf[7] = 10; |
1204 |
buf[12] = sense.asc;
|
1205 |
buf[13] = sense.ascq;
|
1206 |
return MIN(len, 18); |
1207 |
} else {
|
1208 |
/* Return descriptor format sense buffer */
|
1209 |
buf[0] = 0x72; |
1210 |
buf[1] = sense.key;
|
1211 |
buf[2] = sense.asc;
|
1212 |
buf[3] = sense.ascq;
|
1213 |
return 8; |
1214 |
} |
1215 |
} |
1216 |
|
1217 |
static const char *scsi_command_name(uint8_t cmd) |
1218 |
{ |
1219 |
static const char *names[] = { |
1220 |
[ TEST_UNIT_READY ] = "TEST_UNIT_READY",
|
1221 |
[ REWIND ] = "REWIND",
|
1222 |
[ REQUEST_SENSE ] = "REQUEST_SENSE",
|
1223 |
[ FORMAT_UNIT ] = "FORMAT_UNIT",
|
1224 |
[ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
|
1225 |
[ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
|
1226 |
/* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
|
1227 |
[ READ_6 ] = "READ_6",
|
1228 |
[ WRITE_6 ] = "WRITE_6",
|
1229 |
[ SET_CAPACITY ] = "SET_CAPACITY",
|
1230 |
[ READ_REVERSE ] = "READ_REVERSE",
|
1231 |
[ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
|
1232 |
[ SPACE ] = "SPACE",
|
1233 |
[ INQUIRY ] = "INQUIRY",
|
1234 |
[ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
|
1235 |
[ MAINTENANCE_IN ] = "MAINTENANCE_IN",
|
1236 |
[ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
|
1237 |
[ MODE_SELECT ] = "MODE_SELECT",
|
1238 |
[ RESERVE ] = "RESERVE",
|
1239 |
[ RELEASE ] = "RELEASE",
|
1240 |
[ COPY ] = "COPY",
|
1241 |
[ ERASE ] = "ERASE",
|
1242 |
[ MODE_SENSE ] = "MODE_SENSE",
|
1243 |
[ START_STOP ] = "START_STOP/LOAD_UNLOAD",
|
1244 |
/* LOAD_UNLOAD and START_STOP use the same operation code */
|
1245 |
[ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
|
1246 |
[ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
|
1247 |
[ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
|
1248 |
[ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
|
1249 |
[ READ_10 ] = "READ_10",
|
1250 |
[ WRITE_10 ] = "WRITE_10",
|
1251 |
[ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT",
|
1252 |
/* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
|
1253 |
[ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
|
1254 |
[ VERIFY_10 ] = "VERIFY_10",
|
1255 |
[ SEARCH_HIGH ] = "SEARCH_HIGH",
|
1256 |
[ SEARCH_EQUAL ] = "SEARCH_EQUAL",
|
1257 |
[ SEARCH_LOW ] = "SEARCH_LOW",
|
1258 |
[ SET_LIMITS ] = "SET_LIMITS",
|
1259 |
[ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
|
1260 |
/* READ_POSITION and PRE_FETCH use the same operation code */
|
1261 |
[ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
|
1262 |
[ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
|
1263 |
[ READ_DEFECT_DATA ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
|
1264 |
/* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
|
1265 |
[ MEDIUM_SCAN ] = "MEDIUM_SCAN",
|
1266 |
[ COMPARE ] = "COMPARE",
|
1267 |
[ COPY_VERIFY ] = "COPY_VERIFY",
|
1268 |
[ WRITE_BUFFER ] = "WRITE_BUFFER",
|
1269 |
[ READ_BUFFER ] = "READ_BUFFER",
|
1270 |
[ UPDATE_BLOCK ] = "UPDATE_BLOCK",
|
1271 |
[ READ_LONG_10 ] = "READ_LONG_10",
|
1272 |
[ WRITE_LONG_10 ] = "WRITE_LONG_10",
|
1273 |
[ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
|
1274 |
[ WRITE_SAME_10 ] = "WRITE_SAME_10",
|
1275 |
[ UNMAP ] = "UNMAP",
|
1276 |
[ READ_TOC ] = "READ_TOC",
|
1277 |
[ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
|
1278 |
[ SANITIZE ] = "SANITIZE",
|
1279 |
[ GET_CONFIGURATION ] = "GET_CONFIGURATION",
|
1280 |
[ LOG_SELECT ] = "LOG_SELECT",
|
1281 |
[ LOG_SENSE ] = "LOG_SENSE",
|
1282 |
[ MODE_SELECT_10 ] = "MODE_SELECT_10",
|
1283 |
[ RESERVE_10 ] = "RESERVE_10",
|
1284 |
[ RELEASE_10 ] = "RELEASE_10",
|
1285 |
[ MODE_SENSE_10 ] = "MODE_SENSE_10",
|
1286 |
[ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
|
1287 |
[ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
|
1288 |
[ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
|
1289 |
[ EXTENDED_COPY ] = "EXTENDED_COPY",
|
1290 |
[ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
|
1291 |
[ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
|
1292 |
[ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
|
1293 |
[ READ_16 ] = "READ_16",
|
1294 |
[ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
|
1295 |
[ WRITE_16 ] = "WRITE_16",
|
1296 |
[ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
|
1297 |
[ VERIFY_16 ] = "VERIFY_16",
|
1298 |
[ PRE_FETCH_16 ] = "PRE_FETCH_16",
|
1299 |
[ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
|
1300 |
/* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
|
1301 |
[ LOCATE_16 ] = "LOCATE_16",
|
1302 |
[ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
|
1303 |
/* ERASE_16 and WRITE_SAME_16 use the same operation code */
|
1304 |
[ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
|
1305 |
[ WRITE_LONG_16 ] = "WRITE_LONG_16",
|
1306 |
[ REPORT_LUNS ] = "REPORT_LUNS",
|
1307 |
[ BLANK ] = "BLANK",
|
1308 |
[ MOVE_MEDIUM ] = "MOVE_MEDIUM",
|
1309 |
[ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM",
|
1310 |
[ LOAD_UNLOAD ] = "LOAD_UNLOAD",
|
1311 |
[ READ_12 ] = "READ_12",
|
1312 |
[ WRITE_12 ] = "WRITE_12",
|
1313 |
[ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
|
1314 |
/* ERASE_12 and GET_PERFORMANCE use the same operation code */
|
1315 |
[ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
|
1316 |
[ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
|
1317 |
[ VERIFY_12 ] = "VERIFY_12",
|
1318 |
[ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
|
1319 |
[ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
|
1320 |
[ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
|
1321 |
[ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
|
1322 |
[ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
|
1323 |
/* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
|
1324 |
[ READ_CD ] = "READ_CD",
|
1325 |
[ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
|
1326 |
[ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
|
1327 |
[ RESERVE_TRACK ] = "RESERVE_TRACK",
|
1328 |
[ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
|
1329 |
[ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
|
1330 |
[ SET_CD_SPEED ] = "SET_CD_SPEED",
|
1331 |
[ SET_READ_AHEAD ] = "SET_READ_AHEAD",
|
1332 |
[ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
|
1333 |
[ MECHANISM_STATUS ] = "MECHANISM_STATUS",
|
1334 |
}; |
1335 |
|
1336 |
if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) |
1337 |
return "*UNKNOWN*"; |
1338 |
return names[cmd];
|
1339 |
} |
1340 |
|
1341 |
SCSIRequest *scsi_req_ref(SCSIRequest *req) |
1342 |
{ |
1343 |
req->refcount++; |
1344 |
return req;
|
1345 |
} |
1346 |
|
1347 |
void scsi_req_unref(SCSIRequest *req)
|
1348 |
{ |
1349 |
assert(req->refcount > 0);
|
1350 |
if (--req->refcount == 0) { |
1351 |
if (req->ops->free_req) {
|
1352 |
req->ops->free_req(req); |
1353 |
} |
1354 |
g_free(req); |
1355 |
} |
1356 |
} |
1357 |
|
1358 |
/* Tell the device that we finished processing this chunk of I/O. It
|
1359 |
will start the next chunk or complete the command. */
|
1360 |
void scsi_req_continue(SCSIRequest *req)
|
1361 |
{ |
1362 |
trace_scsi_req_continue(req->dev->id, req->lun, req->tag); |
1363 |
if (req->cmd.mode == SCSI_XFER_TO_DEV) {
|
1364 |
req->ops->write_data(req); |
1365 |
} else {
|
1366 |
req->ops->read_data(req); |
1367 |
} |
1368 |
} |
1369 |
|
1370 |
/* Called by the devices when data is ready for the HBA. The HBA should
|
1371 |
start a DMA operation to read or fill the device's data buffer.
|
1372 |
Once it completes, calling scsi_req_continue will restart I/O. */
|
1373 |
void scsi_req_data(SCSIRequest *req, int len) |
1374 |
{ |
1375 |
uint8_t *buf; |
1376 |
if (req->io_canceled) {
|
1377 |
trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); |
1378 |
return;
|
1379 |
} |
1380 |
trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); |
1381 |
assert(req->cmd.mode != SCSI_XFER_NONE); |
1382 |
if (!req->sg) {
|
1383 |
req->resid -= len; |
1384 |
req->bus->info->transfer_data(req, len); |
1385 |
return;
|
1386 |
} |
1387 |
|
1388 |
/* If the device calls scsi_req_data and the HBA specified a
|
1389 |
* scatter/gather list, the transfer has to happen in a single
|
1390 |
* step. */
|
1391 |
assert(!req->dma_started); |
1392 |
req->dma_started = true;
|
1393 |
|
1394 |
buf = scsi_req_get_buf(req); |
1395 |
if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
|
1396 |
req->resid = dma_buf_read(buf, len, req->sg); |
1397 |
} else {
|
1398 |
req->resid = dma_buf_write(buf, len, req->sg); |
1399 |
} |
1400 |
scsi_req_continue(req); |
1401 |
} |
1402 |
|
1403 |
void scsi_req_print(SCSIRequest *req)
|
1404 |
{ |
1405 |
FILE *fp = stderr; |
1406 |
int i;
|
1407 |
|
1408 |
fprintf(fp, "[%s id=%d] %s",
|
1409 |
req->dev->qdev.parent_bus->name, |
1410 |
req->dev->id, |
1411 |
scsi_command_name(req->cmd.buf[0]));
|
1412 |
for (i = 1; i < req->cmd.len; i++) { |
1413 |
fprintf(fp, " 0x%02x", req->cmd.buf[i]);
|
1414 |
} |
1415 |
switch (req->cmd.mode) {
|
1416 |
case SCSI_XFER_NONE:
|
1417 |
fprintf(fp, " - none\n");
|
1418 |
break;
|
1419 |
case SCSI_XFER_FROM_DEV:
|
1420 |
fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
|
1421 |
break;
|
1422 |
case SCSI_XFER_TO_DEV:
|
1423 |
fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
|
1424 |
break;
|
1425 |
default:
|
1426 |
fprintf(fp, " - Oops\n");
|
1427 |
break;
|
1428 |
} |
1429 |
} |
1430 |
|
1431 |
void scsi_req_complete(SCSIRequest *req, int status) |
1432 |
{ |
1433 |
assert(req->status == -1);
|
1434 |
req->status = status; |
1435 |
|
1436 |
assert(req->sense_len <= sizeof(req->sense));
|
1437 |
if (status == GOOD) {
|
1438 |
req->sense_len = 0;
|
1439 |
} |
1440 |
|
1441 |
if (req->sense_len) {
|
1442 |
memcpy(req->dev->sense, req->sense, req->sense_len); |
1443 |
req->dev->sense_len = req->sense_len; |
1444 |
req->dev->sense_is_ua = (req->ops == &reqops_unit_attention); |
1445 |
} else {
|
1446 |
req->dev->sense_len = 0;
|
1447 |
req->dev->sense_is_ua = false;
|
1448 |
} |
1449 |
|
1450 |
/*
|
1451 |
* Unit attention state is now stored in the device's sense buffer
|
1452 |
* if the HBA didn't do autosense. Clear the pending unit attention
|
1453 |
* flags.
|
1454 |
*/
|
1455 |
scsi_clear_unit_attention(req); |
1456 |
|
1457 |
scsi_req_ref(req); |
1458 |
scsi_req_dequeue(req); |
1459 |
req->bus->info->complete(req, req->status, req->resid); |
1460 |
scsi_req_unref(req); |
1461 |
} |
1462 |
|
1463 |
void scsi_req_cancel(SCSIRequest *req)
|
1464 |
{ |
1465 |
if (!req->enqueued) {
|
1466 |
return;
|
1467 |
} |
1468 |
scsi_req_ref(req); |
1469 |
scsi_req_dequeue(req); |
1470 |
req->io_canceled = true;
|
1471 |
if (req->ops->cancel_io) {
|
1472 |
req->ops->cancel_io(req); |
1473 |
} |
1474 |
if (req->bus->info->cancel) {
|
1475 |
req->bus->info->cancel(req); |
1476 |
} |
1477 |
scsi_req_unref(req); |
1478 |
} |
1479 |
|
1480 |
void scsi_req_abort(SCSIRequest *req, int status) |
1481 |
{ |
1482 |
if (!req->enqueued) {
|
1483 |
return;
|
1484 |
} |
1485 |
scsi_req_ref(req); |
1486 |
scsi_req_dequeue(req); |
1487 |
req->io_canceled = true;
|
1488 |
if (req->ops->cancel_io) {
|
1489 |
req->ops->cancel_io(req); |
1490 |
} |
1491 |
scsi_req_complete(req, status); |
1492 |
scsi_req_unref(req); |
1493 |
} |
1494 |
|
1495 |
void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
|
1496 |
{ |
1497 |
SCSIRequest *req; |
1498 |
|
1499 |
while (!QTAILQ_EMPTY(&sdev->requests)) {
|
1500 |
req = QTAILQ_FIRST(&sdev->requests); |
1501 |
scsi_req_cancel(req); |
1502 |
} |
1503 |
sdev->unit_attention = sense; |
1504 |
} |
1505 |
|
1506 |
static char *scsibus_get_dev_path(DeviceState *dev) |
1507 |
{ |
1508 |
SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev); |
1509 |
DeviceState *hba = dev->parent_bus->parent; |
1510 |
char *id;
|
1511 |
char *path;
|
1512 |
|
1513 |
id = qdev_get_dev_path(hba); |
1514 |
if (id) {
|
1515 |
path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
|
1516 |
} else {
|
1517 |
path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
|
1518 |
} |
1519 |
g_free(id); |
1520 |
return path;
|
1521 |
} |
1522 |
|
1523 |
static char *scsibus_get_fw_dev_path(DeviceState *dev) |
1524 |
{ |
1525 |
SCSIDevice *d = SCSI_DEVICE(dev); |
1526 |
char path[100]; |
1527 |
|
1528 |
snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel, |
1529 |
qdev_fw_name(dev), d->id, d->lun); |
1530 |
|
1531 |
return strdup(path);
|
1532 |
} |
1533 |
|
1534 |
SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) |
1535 |
{ |
1536 |
BusChild *kid; |
1537 |
SCSIDevice *target_dev = NULL;
|
1538 |
|
1539 |
QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) { |
1540 |
DeviceState *qdev = kid->child; |
1541 |
SCSIDevice *dev = SCSI_DEVICE(qdev); |
1542 |
|
1543 |
if (dev->channel == channel && dev->id == id) {
|
1544 |
if (dev->lun == lun) {
|
1545 |
return dev;
|
1546 |
} |
1547 |
target_dev = dev; |
1548 |
} |
1549 |
} |
1550 |
return target_dev;
|
1551 |
} |
1552 |
|
1553 |
/* SCSI request list. For simplicity, pv points to the whole device */
|
1554 |
|
1555 |
static void put_scsi_requests(QEMUFile *f, void *pv, size_t size) |
1556 |
{ |
1557 |
SCSIDevice *s = pv; |
1558 |
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); |
1559 |
SCSIRequest *req; |
1560 |
|
1561 |
QTAILQ_FOREACH(req, &s->requests, next) { |
1562 |
assert(!req->io_canceled); |
1563 |
assert(req->status == -1);
|
1564 |
assert(req->enqueued); |
1565 |
|
1566 |
qemu_put_sbyte(f, req->retry ? 1 : 2); |
1567 |
qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
|
1568 |
qemu_put_be32s(f, &req->tag); |
1569 |
qemu_put_be32s(f, &req->lun); |
1570 |
if (bus->info->save_request) {
|
1571 |
bus->info->save_request(f, req); |
1572 |
} |
1573 |
if (req->ops->save_request) {
|
1574 |
req->ops->save_request(f, req); |
1575 |
} |
1576 |
} |
1577 |
qemu_put_sbyte(f, 0);
|
1578 |
} |
1579 |
|
1580 |
static int get_scsi_requests(QEMUFile *f, void *pv, size_t size) |
1581 |
{ |
1582 |
SCSIDevice *s = pv; |
1583 |
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); |
1584 |
int8_t sbyte; |
1585 |
|
1586 |
while ((sbyte = qemu_get_sbyte(f)) > 0) { |
1587 |
uint8_t buf[SCSI_CMD_BUF_SIZE]; |
1588 |
uint32_t tag; |
1589 |
uint32_t lun; |
1590 |
SCSIRequest *req; |
1591 |
|
1592 |
qemu_get_buffer(f, buf, sizeof(buf));
|
1593 |
qemu_get_be32s(f, &tag); |
1594 |
qemu_get_be32s(f, &lun); |
1595 |
req = scsi_req_new(s, tag, lun, buf, NULL);
|
1596 |
req->retry = (sbyte == 1);
|
1597 |
if (bus->info->load_request) {
|
1598 |
req->hba_private = bus->info->load_request(f, req); |
1599 |
} |
1600 |
if (req->ops->load_request) {
|
1601 |
req->ops->load_request(f, req); |
1602 |
} |
1603 |
|
1604 |
/* Just restart it later. */
|
1605 |
scsi_req_enqueue_internal(req); |
1606 |
|
1607 |
/* At this point, the request will be kept alive by the reference
|
1608 |
* added by scsi_req_enqueue_internal, so we can release our reference.
|
1609 |
* The HBA of course will add its own reference in the load_request
|
1610 |
* callback if it needs to hold on the SCSIRequest.
|
1611 |
*/
|
1612 |
scsi_req_unref(req); |
1613 |
} |
1614 |
|
1615 |
return 0; |
1616 |
} |
1617 |
|
1618 |
static const VMStateInfo vmstate_info_scsi_requests = { |
1619 |
.name = "scsi-requests",
|
1620 |
.get = get_scsi_requests, |
1621 |
.put = put_scsi_requests, |
1622 |
}; |
1623 |
|
1624 |
const VMStateDescription vmstate_scsi_device = {
|
1625 |
.name = "SCSIDevice",
|
1626 |
.version_id = 1,
|
1627 |
.minimum_version_id = 1,
|
1628 |
.minimum_version_id_old = 1,
|
1629 |
.fields = (VMStateField[]) { |
1630 |
VMSTATE_UINT8(unit_attention.key, SCSIDevice), |
1631 |
VMSTATE_UINT8(unit_attention.asc, SCSIDevice), |
1632 |
VMSTATE_UINT8(unit_attention.ascq, SCSIDevice), |
1633 |
VMSTATE_BOOL(sense_is_ua, SCSIDevice), |
1634 |
VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE), |
1635 |
VMSTATE_UINT32(sense_len, SCSIDevice), |
1636 |
{ |
1637 |
.name = "requests",
|
1638 |
.version_id = 0,
|
1639 |
.field_exists = NULL,
|
1640 |
.size = 0, /* ouch */ |
1641 |
.info = &vmstate_info_scsi_requests, |
1642 |
.flags = VMS_SINGLE, |
1643 |
.offset = 0,
|
1644 |
}, |
1645 |
VMSTATE_END_OF_LIST() |
1646 |
} |
1647 |
}; |
1648 |
|
1649 |
static void scsi_device_class_init(ObjectClass *klass, void *data) |
1650 |
{ |
1651 |
DeviceClass *k = DEVICE_CLASS(klass); |
1652 |
k->bus_type = TYPE_SCSI_BUS; |
1653 |
k->init = scsi_qdev_init; |
1654 |
k->unplug = qdev_simple_unplug_cb; |
1655 |
k->exit = scsi_qdev_exit; |
1656 |
k->props = scsi_props; |
1657 |
} |
1658 |
|
1659 |
static TypeInfo scsi_device_type_info = {
|
1660 |
.name = TYPE_SCSI_DEVICE, |
1661 |
.parent = TYPE_DEVICE, |
1662 |
.instance_size = sizeof(SCSIDevice),
|
1663 |
.abstract = true,
|
1664 |
.class_size = sizeof(SCSIDeviceClass),
|
1665 |
.class_init = scsi_device_class_init, |
1666 |
}; |
1667 |
|
1668 |
static void scsi_register_types(void) |
1669 |
{ |
1670 |
type_register_static(&scsi_bus_info); |
1671 |
type_register_static(&scsi_device_type_info); |
1672 |
} |
1673 |
|
1674 |
type_init(scsi_register_types) |