]> git.proxmox.com Git - mirror_qemu.git/blame - hw/scsi/scsi-bus.c
Merge tag 'for-upstream' of https://repo.or.cz/qemu/kevin into staging
[mirror_qemu.git] / hw / scsi / scsi-bus.c
CommitLineData
a4ab4792 1#include "qemu/osdep.h"
da34e65c 2#include "qapi/error.h"
1de7afc9 3#include "qemu/error-report.h"
0b8fa32f 4#include "qemu/module.h"
922a01a0 5#include "qemu/option.h"
15e09912 6#include "qemu/hw-version.h"
a27bd6c7 7#include "hw/qdev-properties.h"
0d09e41a 8#include "hw/scsi/scsi.h"
ca77ee28 9#include "migration/qemu-file-types.h"
d6454270 10#include "migration/vmstate.h"
08e2c9f1 11#include "scsi/constants.h"
fa1d36df 12#include "sysemu/block-backend.h"
9c17d615 13#include "sysemu/blockdev.h"
2f780b6a 14#include "sysemu/sysemu.h"
54d31236 15#include "sysemu/runstate.h"
5138efec 16#include "trace.h"
9c17d615 17#include "sysemu/dma.h"
f348b6d1 18#include "qemu/cutils.h"
d52affa7 19
baa1bd89 20static char *scsibus_get_dev_path(DeviceState *dev);
db07c0f8 21static char *scsibus_get_fw_dev_path(DeviceState *dev);
71544d30 22static void scsi_req_dequeue(SCSIRequest *req);
84642435
AH
23static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
24static void scsi_target_free_buf(SCSIRequest *req);
db07c0f8 25
d52affa7
GH
26static int next_scsi_bus;
27
8ddf958e
PB
28static SCSIDevice *do_scsi_device_find(SCSIBus *bus,
29 int channel, int id, int lun,
30 bool include_unrealized)
31{
32 BusChild *kid;
33 SCSIDevice *retval = NULL;
34
35 QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) {
36 DeviceState *qdev = kid->child;
37 SCSIDevice *dev = SCSI_DEVICE(qdev);
38
39 if (dev->channel == channel && dev->id == id) {
40 if (dev->lun == lun) {
41 retval = dev;
42 break;
43 }
44
45 /*
46 * If we don't find exact match (channel/bus/lun),
47 * we will return the first device which matches channel/bus
48 */
49
50 if (!retval) {
51 retval = dev;
52 }
53 }
54 }
55
56 /*
57 * This function might run on the IO thread and we might race against
58 * main thread hot-plugging the device.
59 * We assume that as soon as .realized is set to true we can let
60 * the user access the device.
61 */
62
26462a70 63 if (retval && !include_unrealized && !qdev_is_realized(&retval->qdev)) {
8ddf958e
PB
64 retval = NULL;
65 }
66
67 return retval;
68}
69
70SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
71{
72 RCU_READ_LOCK_GUARD();
73 return do_scsi_device_find(bus, channel, id, lun, false);
74}
75
8ff34495
ML
76SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
77{
78 SCSIDevice *d;
79 RCU_READ_LOCK_GUARD();
80 d = do_scsi_device_find(bus, channel, id, lun, false);
81 if (d) {
82 object_ref(d);
83 }
84 return d;
85}
86
a818a4b6 87static void scsi_device_realize(SCSIDevice *s, Error **errp)
b9eea3e6
AL
88{
89 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
a818a4b6
FZ
90 if (sc->realize) {
91 sc->realize(s, errp);
b9eea3e6 92 }
b9eea3e6
AL
93}
94
b69c3c21 95static void scsi_device_unrealize(SCSIDevice *s)
6b98c5aa
SE
96{
97 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
98 if (sc->unrealize) {
b69c3c21 99 sc->unrealize(s);
6b98c5aa
SE
100 }
101}
102
ff34c32c 103int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
fe9d8927 104 size_t buf_len, void *hba_private)
ff34c32c
PB
105{
106 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
107 int rc;
108
109 assert(cmd->len == 0);
fe9d8927 110 rc = scsi_req_parse_cdb(dev, cmd, buf, buf_len);
ff34c32c 111 if (bus->info->parse_cdb) {
fe9d8927 112 rc = bus->info->parse_cdb(dev, cmd, buf, buf_len, hba_private);
ff34c32c
PB
113 }
114 return rc;
115}
116
b9eea3e6
AL
117static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
118 uint8_t *buf, void *hba_private)
119{
120 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
121 if (sc->alloc_req) {
122 return sc->alloc_req(s, tag, lun, buf, hba_private);
123 }
124
125 return NULL;
126}
127
8d72db68 128void scsi_device_unit_attention_reported(SCSIDevice *s)
b9eea3e6
AL
129{
130 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
131 if (sc->unit_attention_reported) {
132 sc->unit_attention_reported(s);
133 }
134}
135
d52affa7 136/* Create a scsi bus, and attach devices to it. */
739e95f5
PM
137void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
138 const SCSIBusInfo *info, const char *bus_name)
d52affa7 139{
d637e1dc 140 qbus_init(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
d52affa7 141 bus->busnr = next_scsi_bus++;
afd4030c 142 bus->info = info;
cd7c8660 143 qbus_set_bus_hotplug_handler(BUS(bus));
d52affa7
GH
144}
145
71544d30
PB
146static void scsi_dma_restart_bh(void *opaque)
147{
148 SCSIDevice *s = opaque;
149 SCSIRequest *req, *next;
150
151 qemu_bh_delete(s->bh);
152 s->bh = NULL;
153
1919631e 154 aio_context_acquire(blk_get_aio_context(s->conf.blk));
71544d30
PB
155 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
156 scsi_req_ref(req);
157 if (req->retry) {
158 req->retry = false;
159 switch (req->cmd.mode) {
160 case SCSI_XFER_FROM_DEV:
161 case SCSI_XFER_TO_DEV:
162 scsi_req_continue(req);
163 break;
164 case SCSI_XFER_NONE:
165 scsi_req_dequeue(req);
166 scsi_req_enqueue(req);
167 break;
168 }
169 }
170 scsi_req_unref(req);
171 }
1919631e 172 aio_context_release(blk_get_aio_context(s->conf.blk));
cfd4e363
ML
173 /* Drop the reference that was acquired in scsi_dma_restart_cb */
174 object_unref(OBJECT(s));
71544d30
PB
175}
176
177void scsi_req_retry(SCSIRequest *req)
178{
179 /* No need to save a reference, because scsi_dma_restart_bh just
180 * looks at the request list. */
181 req->retry = true;
182}
183
538f0497 184static void scsi_dma_restart_cb(void *opaque, bool running, RunState state)
71544d30
PB
185{
186 SCSIDevice *s = opaque;
187
188 if (!running) {
189 return;
190 }
191 if (!s->bh) {
d223c104 192 AioContext *ctx = blk_get_aio_context(s->conf.blk);
cfd4e363
ML
193 /* The reference is dropped in scsi_dma_restart_bh.*/
194 object_ref(OBJECT(s));
f63192b0
AB
195 s->bh = aio_bh_new_guarded(ctx, scsi_dma_restart_bh, s,
196 &DEVICE(s)->mem_reentrancy_guard);
71544d30
PB
197 qemu_bh_schedule(s->bh);
198 }
199}
200
42a90a89 201static bool scsi_bus_is_address_free(SCSIBus *bus,
48805df9
YF
202 int channel, int target, int lun,
203 SCSIDevice **p_dev)
42a90a89 204{
8ddf958e
PB
205 SCSIDevice *d;
206
207 RCU_READ_LOCK_GUARD();
208 d = do_scsi_device_find(bus, channel, target, lun, true);
42a90a89
PB
209 if (d && d->lun == lun) {
210 if (p_dev) {
211 *p_dev = d;
212 }
213 return false;
214 }
215 if (p_dev) {
216 *p_dev = NULL;
217 }
218 return true;
219}
220
221static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Error **errp)
d52affa7 222{
b9eea3e6 223 SCSIDevice *dev = SCSI_DEVICE(qdev);
42a90a89 224 SCSIBus *bus = SCSI_BUS(qbus);
d52affa7 225
0d3545e7 226 if (dev->channel > bus->info->max_channel) {
a818a4b6 227 error_setg(errp, "bad scsi channel id: %d", dev->channel);
42a90a89 228 return false;
0d3545e7 229 }
7e0380b9 230 if (dev->id != -1 && dev->id > bus->info->max_target) {
a818a4b6 231 error_setg(errp, "bad scsi device id: %d", dev->id);
42a90a89 232 return false;
d52affa7 233 }
d3d250bd 234 if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
a818a4b6 235 error_setg(errp, "bad scsi device lun: %d", dev->lun);
42a90a89
PB
236 return false;
237 }
238
239 if (dev->id != -1 && dev->lun != -1) {
240 SCSIDevice *d;
241 if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun, &d)) {
242 error_setg(errp, "lun already used by '%s'", d->qdev.id);
243 return false;
244 }
d3d250bd 245 }
d52affa7 246
42a90a89
PB
247 return true;
248}
249
250static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
251{
252 SCSIDevice *dev = SCSI_DEVICE(qdev);
253 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
254 bool is_free;
255 Error *local_err = NULL;
256
7e0380b9
PB
257 if (dev->id == -1) {
258 int id = -1;
259 if (dev->lun == -1) {
260 dev->lun = 0;
261 }
262 do {
42a90a89
PB
263 is_free = scsi_bus_is_address_free(bus, dev->channel, ++id, dev->lun, NULL);
264 } while (!is_free && id < bus->info->max_target);
265 if (!is_free) {
a818a4b6
FZ
266 error_setg(errp, "no free target");
267 return;
7e0380b9
PB
268 }
269 dev->id = id;
270 } else if (dev->lun == -1) {
271 int lun = -1;
272 do {
42a90a89
PB
273 is_free = scsi_bus_is_address_free(bus, dev->channel, dev->id, ++lun, NULL);
274 } while (!is_free && lun < bus->info->max_lun);
275 if (!is_free) {
a818a4b6
FZ
276 error_setg(errp, "no free lun");
277 return;
7e0380b9
PB
278 }
279 dev->lun = lun;
d52affa7 280 }
d52affa7 281
9af99d98 282 QTAILQ_INIT(&dev->requests);
a818a4b6
FZ
283 scsi_device_realize(dev, &local_err);
284 if (local_err) {
285 error_propagate(errp, local_err);
286 return;
71544d30 287 }
1a8c091c
SH
288 dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
289 scsi_dma_restart_cb, dev);
01985dcf
GH
290}
291
b69c3c21 292static void scsi_qdev_unrealize(DeviceState *qdev)
01985dcf 293{
b9eea3e6 294 SCSIDevice *dev = SCSI_DEVICE(qdev);
01985dcf 295
71544d30
PB
296 if (dev->vmsentry) {
297 qemu_del_vm_change_state_handler(dev->vmsentry);
298 }
fb7b5c0d
PB
299
300 scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
6b98c5aa 301
b69c3c21 302 scsi_device_unrealize(dev);
6b98c5aa 303
fb7b5c0d 304 blockdev_mark_auto_del(dev->conf.blk);
d52affa7
GH
305}
306
d52affa7 307/* handle legacy '-drive if=scsi,...' cmd line args */
4be74634 308SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
76534da7 309 int unit, bool removable, int bootindex,
395b9539 310 bool share_rw,
b8efb36b
KW
311 BlockdevOnError rerror,
312 BlockdevOnError werror,
caad4eb3 313 const char *serial, Error **errp)
d52affa7
GH
314{
315 const char *driver;
22647504 316 char *name;
d52affa7 317 DeviceState *dev;
0d074bf8 318 DriveInfo *dinfo;
d52affa7 319
0d074bf8
PB
320 if (blk_is_sg(blk)) {
321 driver = "scsi-generic";
322 } else {
323 dinfo = blk_legacy_dinfo(blk);
324 if (dinfo && dinfo->media_cd) {
325 driver = "scsi-cd";
326 } else {
327 driver = "scsi-hd";
328 }
329 }
3e80f690 330 dev = qdev_new(driver);
22647504 331 name = g_strdup_printf("legacy[%d]", unit);
d2623129 332 object_property_add_child(OBJECT(bus), name, OBJECT(dev));
22647504
PB
333 g_free(name);
334
d52affa7 335 qdev_prop_set_uint32(dev, "scsi-id", unit);
ce4e7e46 336 if (bootindex >= 0) {
5325cc34 337 object_property_set_int(OBJECT(dev), "bootindex", bootindex,
8dece34f 338 &error_abort);
ce4e7e46 339 }
efba1595 340 if (object_property_find(OBJECT(dev), "removable")) {
2d1fd261
SH
341 qdev_prop_set_bit(dev, "removable", removable);
342 }
efba1595 343 if (serial && object_property_find(OBJECT(dev), "serial")) {
76534da7
KW
344 qdev_prop_set_string(dev, "serial", serial);
345 }
668f62ec 346 if (!qdev_prop_set_drive_err(dev, "drive", blk, errp)) {
02a5c4c9 347 object_unparent(OBJECT(dev));
18846dee
MA
348 return NULL;
349 }
668f62ec 350 if (!object_property_set_bool(OBJECT(dev), "share-rw", share_rw, errp)) {
395b9539
FZ
351 object_unparent(OBJECT(dev));
352 return NULL;
353 }
b8efb36b
KW
354
355 qdev_prop_set_enum(dev, "rerror", rerror);
356 qdev_prop_set_enum(dev, "werror", werror);
357
668f62ec 358 if (!qdev_realize_and_unref(dev, &bus->qbus, errp)) {
02a5c4c9 359 object_unparent(OBJECT(dev));
33e66b86 360 return NULL;
caad4eb3 361 }
b9eea3e6 362 return SCSI_DEVICE(dev);
d52affa7
GH
363}
364
14545097 365void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
d52affa7 366{
42e766a2 367 Location loc;
d52affa7 368 DriveInfo *dinfo;
caad4eb3 369 int unit;
d52affa7 370
42e766a2 371 loc_push_none(&loc);
d3d250bd 372 for (unit = 0; unit <= bus->info->max_target; unit++) {
d52affa7
GH
373 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
374 if (dinfo == NULL) {
375 continue;
376 }
42e766a2 377 qemu_opts_loc_restore(dinfo->opts);
4be74634 378 scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
b8efb36b
KW
379 unit, false, -1, false,
380 BLOCKDEV_ON_ERROR_AUTO,
381 BLOCKDEV_ON_ERROR_AUTO,
382 NULL, &error_fatal);
d52affa7 383 }
42e766a2 384 loc_pop(&loc);
d52affa7 385}
89b08ae1 386
12a08998
PB
387static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
388{
389 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
390 scsi_req_complete(req, CHECK_CONDITION);
391 return 0;
392}
393
394static const struct SCSIReqOps reqops_invalid_field = {
395 .size = sizeof(SCSIRequest),
396 .send_command = scsi_invalid_field
397};
398
afa46c46
PB
399/* SCSIReqOps implementation for invalid commands. */
400
401static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
402{
403 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
404 scsi_req_complete(req, CHECK_CONDITION);
405 return 0;
406}
407
adcf2754 408static const struct SCSIReqOps reqops_invalid_opcode = {
afa46c46
PB
409 .size = sizeof(SCSIRequest),
410 .send_command = scsi_invalid_command
411};
412
6dc06f08
PB
413/* SCSIReqOps implementation for unit attention conditions. */
414
415static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
416{
0bf8264e 417 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
6dc06f08
PB
418 scsi_req_build_sense(req, req->dev->unit_attention);
419 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
420 scsi_req_build_sense(req, req->bus->unit_attention);
421 }
422 scsi_req_complete(req, CHECK_CONDITION);
423 return 0;
424}
425
adcf2754 426static const struct SCSIReqOps reqops_unit_attention = {
6dc06f08
PB
427 .size = sizeof(SCSIRequest),
428 .send_command = scsi_unit_attention
429};
430
fdaef069
PB
431/* SCSIReqOps implementation for REPORT LUNS and for commands sent to
432 an invalid LUN. */
433
434typedef struct SCSITargetReq SCSITargetReq;
435
436struct SCSITargetReq {
437 SCSIRequest req;
438 int len;
84642435
AH
439 uint8_t *buf;
440 int buf_len;
fdaef069
PB
441};
442
443static void store_lun(uint8_t *outbuf, int lun)
444{
445 if (lun < 256) {
8cfe8013
ML
446 /* Simple logical unit addressing method*/
447 outbuf[0] = 0;
fdaef069 448 outbuf[1] = lun;
8cfe8013
ML
449 } else {
450 /* Flat space addressing method */
451 outbuf[0] = 0x40 | (lun >> 8);
452 outbuf[1] = (lun & 255);
fdaef069 453 }
fdaef069
PB
454}
455
456static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
457{
0866aca1 458 BusChild *kid;
0d3545e7 459 int channel, id;
8cfe8013
ML
460 uint8_t tmp[8] = {0};
461 int len = 0;
462 GByteArray *buf;
ba74307c 463
fdaef069
PB
464 if (r->req.cmd.xfer < 16) {
465 return false;
466 }
467 if (r->req.cmd.buf[2] > 2) {
468 return false;
469 }
8cfe8013
ML
470
471 /* reserve space for 63 LUNs*/
472 buf = g_byte_array_sized_new(512);
473
0d3545e7 474 channel = r->req.dev->channel;
ba74307c 475 id = r->req.dev->id;
2d24a646 476
8cfe8013
ML
477 /* add size (will be updated later to correct value */
478 g_byte_array_append(buf, tmp, 8);
479 len += 8;
2d24a646 480
8cfe8013
ML
481 /* add LUN0 */
482 g_byte_array_append(buf, tmp, 8);
483 len += 8;
ba74307c 484
8cfe8013
ML
485 WITH_RCU_READ_LOCK_GUARD() {
486 QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
487 DeviceState *qdev = kid->child;
488 SCSIDevice *dev = SCSI_DEVICE(qdev);
489
4382f167
SH
490 if (dev->channel == channel && dev->id == id && dev->lun != 0 &&
491 qdev_is_realized(&dev->qdev)) {
8cfe8013
ML
492 store_lun(tmp, dev->lun);
493 g_byte_array_append(buf, tmp, 8);
494 len += 8;
ba74307c 495 }
ba74307c
PB
496 }
497 }
ba74307c 498
8cfe8013
ML
499 r->buf_len = len;
500 r->buf = g_byte_array_free(buf, FALSE);
501 r->len = MIN(len, r->req.cmd.xfer & ~7);
2d24a646 502
8cfe8013
ML
503 /* store the LUN list length */
504 stl_be_p(&r->buf[0], len - 8);
fdaef069
PB
505 return true;
506}
507
508static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
509{
510 assert(r->req.dev->lun != r->req.lun);
84642435
AH
511
512 scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
513
fdaef069
PB
514 if (r->req.cmd.buf[1] & 0x2) {
515 /* Command support data - optional, not implemented */
516 return false;
517 }
518
519 if (r->req.cmd.buf[1] & 0x1) {
520 /* Vital product data */
521 uint8_t page_code = r->req.cmd.buf[2];
fdaef069
PB
522 r->buf[r->len++] = page_code ; /* this page */
523 r->buf[r->len++] = 0x00;
524
525 switch (page_code) {
526 case 0x00: /* Supported page codes, mandatory */
527 {
528 int pages;
529 pages = r->len++;
530 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
531 r->buf[pages] = r->len - pages - 1; /* number of pages */
532 break;
533 }
534 default:
535 return false;
536 }
537 /* done with EVPD */
84642435 538 assert(r->len < r->buf_len);
fdaef069
PB
539 r->len = MIN(r->req.cmd.xfer, r->len);
540 return true;
541 }
542
543 /* Standard INQUIRY data */
544 if (r->req.cmd.buf[2] != 0) {
545 return false;
546 }
547
548 /* PAGE CODE == 0 */
84642435 549 r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
fdaef069
PB
550 memset(r->buf, 0, r->len);
551 if (r->req.lun != 0) {
552 r->buf[0] = TYPE_NO_LUN;
553 } else {
554 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
555 r->buf[2] = 5; /* Version */
556 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
557 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
afd4030c 558 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
fdaef069
PB
559 memcpy(&r->buf[8], "QEMU ", 8);
560 memcpy(&r->buf[16], "QEMU TARGET ", 16);
35c2c8dc 561 pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
fdaef069
PB
562 }
563 return true;
564}
565
6959e508
JL
566static size_t scsi_sense_len(SCSIRequest *req)
567{
568 if (req->dev->type == TYPE_SCANNER)
569 return SCSI_SENSE_LEN_SCANNER;
570 else
571 return SCSI_SENSE_LEN;
572}
573
fdaef069
PB
574static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
575{
576 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
b07fbce6 577 int fixed_sense = (req->cmd.buf[1] & 1) == 0;
fdaef069 578
b07fbce6
HR
579 if (req->lun != 0 &&
580 buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
ded6ddc5
HR
581 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
582 scsi_req_complete(req, CHECK_CONDITION);
583 return 0;
584 }
fdaef069
PB
585 switch (buf[0]) {
586 case REPORT_LUNS:
587 if (!scsi_target_emulate_report_luns(r)) {
588 goto illegal_request;
589 }
590 break;
591 case INQUIRY:
592 if (!scsi_target_emulate_inquiry(r)) {
593 goto illegal_request;
594 }
595 break;
739df215 596 case REQUEST_SENSE:
6959e508 597 scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
b07fbce6
HR
598 if (req->lun != 0) {
599 const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
600
f68d98b2
PB
601 r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
602 sense, fixed_sense);
b07fbce6
HR
603 } else {
604 r->len = scsi_device_get_sense(r->req.dev, r->buf,
605 MIN(req->cmd.xfer, r->buf_len),
606 fixed_sense);
607 }
3653d8c4 608 if (r->req.dev->sense_is_ua) {
b9eea3e6 609 scsi_device_unit_attention_reported(req->dev);
3653d8c4
PB
610 r->req.dev->sense_len = 0;
611 r->req.dev->sense_is_ua = false;
612 }
739df215 613 break;
1cb27d92
PB
614 case TEST_UNIT_READY:
615 break;
fdaef069 616 default:
ded6ddc5 617 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
fdaef069
PB
618 scsi_req_complete(req, CHECK_CONDITION);
619 return 0;
620 illegal_request:
621 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
622 scsi_req_complete(req, CHECK_CONDITION);
623 return 0;
624 }
625
626 if (!r->len) {
627 scsi_req_complete(req, GOOD);
628 }
629 return r->len;
630}
631
632static void scsi_target_read_data(SCSIRequest *req)
633{
634 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
635 uint32_t n;
636
637 n = r->len;
638 if (n > 0) {
639 r->len = 0;
640 scsi_req_data(&r->req, n);
641 } else {
642 scsi_req_complete(&r->req, GOOD);
643 }
644}
645
646static uint8_t *scsi_target_get_buf(SCSIRequest *req)
647{
648 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
649
650 return r->buf;
651}
652
84642435
AH
653static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
654{
655 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
656
657 r->buf = g_malloc(len);
658 r->buf_len = len;
659
660 return r->buf;
661}
662
663static void scsi_target_free_buf(SCSIRequest *req)
664{
665 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
666
667 g_free(r->buf);
668}
669
adcf2754 670static const struct SCSIReqOps reqops_target_command = {
fdaef069
PB
671 .size = sizeof(SCSITargetReq),
672 .send_command = scsi_target_send_command,
673 .read_data = scsi_target_read_data,
674 .get_buf = scsi_target_get_buf,
84642435 675 .free_req = scsi_target_free_buf,
fdaef069
PB
676};
677
678
adcf2754
PB
679SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
680 uint32_t tag, uint32_t lun, void *hba_private)
89b08ae1
GH
681{
682 SCSIRequest *req;
cac3c384
PB
683 SCSIBus *bus = scsi_bus_from_device(d);
684 BusState *qbus = BUS(bus);
61e68b3f
FZ
685 const int memset_off = offsetof(SCSIRequest, sense)
686 + sizeof(req->sense);
89b08ae1 687
633dccb4 688 req = g_malloc(reqops->size);
61e68b3f 689 memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
5c6c0e51 690 req->refcount = 1;
cac3c384 691 req->bus = bus;
89b08ae1
GH
692 req->dev = d;
693 req->tag = tag;
694 req->lun = lun;
c5bf71a9 695 req->hba_private = hba_private;
ed3a34a3 696 req->status = -1;
f3126d65 697 req->host_status = -1;
8dbd4574 698 req->ops = reqops;
cac3c384
PB
699 object_ref(OBJECT(d));
700 object_ref(OBJECT(qbus->parent));
8e0a9320 701 notifier_list_init(&req->cancel_notifiers);
5138efec 702 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
89b08ae1
GH
703 return req;
704}
705
c5bf71a9 706SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
fe9d8927 707 uint8_t *buf, size_t buf_len, void *hba_private)
43a2b339 708{
6dc06f08 709 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
769998a1 710 const SCSIReqOps *ops;
ff34c32c 711 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
c39ce112 712 SCSIRequest *req;
769998a1
PB
713 SCSICommand cmd = { .len = 0 };
714 int ret;
715
6d1511ce
JM
716 if (buf_len == 0) {
717 trace_scsi_req_parse_bad(d->id, lun, tag, 0);
718 goto invalid_opcode;
719 }
720
769998a1
PB
721 if ((d->unit_attention.key == UNIT_ATTENTION ||
722 bus->unit_attention.key == UNIT_ATTENTION) &&
723 (buf[0] != INQUIRY &&
724 buf[0] != REPORT_LUNS &&
725 buf[0] != GET_CONFIGURATION &&
726 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
727
728 /*
729 * If we already have a pending unit attention condition,
730 * report this one before triggering another one.
731 */
732 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
733 ops = &reqops_unit_attention;
734 } else if (lun != d->lun ||
735 buf[0] == REPORT_LUNS ||
736 (buf[0] == REQUEST_SENSE && d->sense_len)) {
737 ops = &reqops_target_command;
738 } else {
739 ops = NULL;
740 }
afa46c46 741
ff34c32c 742 if (ops != NULL || !sc->parse_cdb) {
fe9d8927 743 ret = scsi_req_parse_cdb(d, &cmd, buf, buf_len);
ff34c32c 744 } else {
fe9d8927 745 ret = sc->parse_cdb(d, &cmd, buf, buf_len, hba_private);
ff34c32c
PB
746 }
747
769998a1 748 if (ret != 0) {
afa46c46 749 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
6d1511ce 750invalid_opcode:
afa46c46
PB
751 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
752 } else {
769998a1 753 assert(cmd.len != 0);
afa46c46
PB
754 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
755 cmd.mode, cmd.xfer);
3b6ffe50 756 if (cmd.lba != -1) {
afa46c46
PB
757 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
758 cmd.lba);
759 }
fdaef069 760
12a08998
PB
761 if (cmd.xfer > INT32_MAX) {
762 req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
769998a1
PB
763 } else if (ops) {
764 req = scsi_req_alloc(ops, d, tag, lun, hba_private);
fdaef069 765 } else {
b9eea3e6 766 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
fdaef069 767 }
afa46c46
PB
768 }
769
770 req->cmd = cmd;
5f412602 771 req->residual = req->cmd.xfer;
01e95455 772
98254542
PB
773 switch (buf[0]) {
774 case INQUIRY:
775 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
776 break;
777 case TEST_UNIT_READY:
778 trace_scsi_test_unit_ready(d->id, lun, tag);
779 break;
780 case REPORT_LUNS:
781 trace_scsi_report_luns(d->id, lun, tag);
782 break;
783 case REQUEST_SENSE:
784 trace_scsi_request_sense(d->id, lun, tag);
785 break;
786 default:
787 break;
788 }
789
c39ce112 790 return req;
43a2b339
PB
791}
792
0c34459b
PB
793uint8_t *scsi_req_get_buf(SCSIRequest *req)
794{
12010e7b 795 return req->ops->get_buf(req);
0c34459b
PB
796}
797
6dc06f08
PB
798static void scsi_clear_unit_attention(SCSIRequest *req)
799{
800 SCSISense *ua;
801 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
802 req->bus->unit_attention.key != UNIT_ATTENTION) {
803 return;
804 }
805
806 /*
807 * If an INQUIRY command enters the enabled command state,
808 * the device server shall [not] clear any unit attention condition;
809 * See also MMC-6, paragraphs 6.5 and 6.6.2.
810 */
811 if (req->cmd.buf[0] == INQUIRY ||
812 req->cmd.buf[0] == GET_CONFIGURATION ||
813 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
814 return;
815 }
816
817 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
818 ua = &req->dev->unit_attention;
819 } else {
820 ua = &req->bus->unit_attention;
821 }
822
823 /*
824 * If a REPORT LUNS command enters the enabled command state, [...]
825 * the device server shall clear any pending unit attention condition
826 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
827 */
828 if (req->cmd.buf[0] == REPORT_LUNS &&
829 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
830 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
831 return;
832 }
833
834 *ua = SENSE_CODE(NO_SENSE);
835}
836
74382217
HR
837int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
838{
6dc06f08
PB
839 int ret;
840
b45ef674
PB
841 assert(len >= 14);
842 if (!req->sense_len) {
74382217
HR
843 return 0;
844 }
6dc06f08 845
37b6045c 846 ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
6dc06f08
PB
847
848 /*
849 * FIXME: clearing unit attention conditions upon autosense should be done
850 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
851 * (SAM-5, 5.14).
852 *
853 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
854 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
3653d8c4 855 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
6dc06f08 856 */
3653d8c4 857 if (req->dev->sense_is_ua) {
b9eea3e6 858 scsi_device_unit_attention_reported(req->dev);
3653d8c4
PB
859 req->dev->sense_len = 0;
860 req->dev->sense_is_ua = false;
861 }
6dc06f08 862 return ret;
b45ef674
PB
863}
864
865int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
866{
37b6045c 867 return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
b45ef674
PB
868}
869
870void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
871{
872 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
873 sense.key, sense.asc, sense.ascq);
a3760467 874 req->sense_len = scsi_build_sense(req->sense, sense);
74382217
HR
875}
876
63f740dd 877static void scsi_req_enqueue_internal(SCSIRequest *req)
89b08ae1 878{
5c6c0e51
HR
879 assert(!req->enqueued);
880 scsi_req_ref(req);
3d5aba97
PB
881 if (req->bus->info->get_sg_list) {
882 req->sg = req->bus->info->get_sg_list(req);
883 } else {
884 req->sg = NULL;
885 }
5c6c0e51
HR
886 req->enqueued = true;
887 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
63f740dd 888}
fc4f0754 889
63f740dd
PB
890int32_t scsi_req_enqueue(SCSIRequest *req)
891{
892 int32_t rc;
893
894 assert(!req->retry);
895 scsi_req_enqueue_internal(req);
fc4f0754 896 scsi_req_ref(req);
c39ce112 897 rc = req->ops->send_command(req, req->cmd.buf);
fc4f0754
PB
898 scsi_req_unref(req);
899 return rc;
89b08ae1
GH
900}
901
a1f0cce2 902static void scsi_req_dequeue(SCSIRequest *req)
e8637c90 903{
5138efec 904 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
71544d30 905 req->retry = false;
e8637c90
JK
906 if (req->enqueued) {
907 QTAILQ_REMOVE(&req->dev->requests, req, next);
908 req->enqueued = false;
ad2d30f7 909 scsi_req_unref(req);
e8637c90
JK
910 }
911}
912
06b86357
PB
913static int scsi_get_performance_length(int num_desc, int type, int data_type)
914{
915 /* MMC-6, paragraph 6.7. */
916 switch (type) {
917 case 0:
918 if ((data_type & 3) == 0) {
919 /* Each descriptor is as in Table 295 - Nominal performance. */
920 return 16 * num_desc + 8;
921 } else {
922 /* Each descriptor is as in Table 296 - Exceptions. */
923 return 6 * num_desc + 8;
924 }
925 case 1:
926 case 4:
927 case 5:
928 return 8 * num_desc + 8;
929 case 2:
930 return 2048 * num_desc + 8;
931 case 3:
932 return 16 * num_desc + 8;
933 default:
934 return 8;
935 }
936}
937
e4b65262
CM
938static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
939{
940 int byte_block = (buf[2] >> 2) & 0x1;
941 int type = (buf[2] >> 4) & 0x1;
942 int xfer_unit;
943
944 if (byte_block) {
945 if (type) {
946 xfer_unit = dev->blocksize;
947 } else {
948 xfer_unit = 512;
949 }
950 } else {
951 xfer_unit = 1;
952 }
953
954 return xfer_unit;
955}
956
1894df02 957static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
e4b65262
CM
958{
959 int length = buf[2] & 0x3;
960 int xfer;
961 int unit = ata_passthrough_xfer_unit(dev, buf);
962
963 switch (length) {
964 case 0:
965 case 3: /* USB-specific. */
d83c951c 966 default:
e4b65262
CM
967 xfer = 0;
968 break;
969 case 1:
970 xfer = buf[3];
971 break;
972 case 2:
973 xfer = buf[4];
974 break;
975 }
976
977 return xfer * unit;
978}
979
1894df02 980static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
e4b65262
CM
981{
982 int extend = buf[1] & 0x1;
983 int length = buf[2] & 0x3;
984 int xfer;
985 int unit = ata_passthrough_xfer_unit(dev, buf);
986
987 switch (length) {
988 case 0:
989 case 3: /* USB-specific. */
d83c951c 990 default:
e4b65262
CM
991 xfer = 0;
992 break;
993 case 1:
994 xfer = buf[4];
995 xfer |= (extend ? buf[3] << 8 : 0);
996 break;
997 case 2:
998 xfer = buf[6];
999 xfer |= (extend ? buf[5] << 8 : 0);
1000 break;
1001 }
1002
1003 return xfer * unit;
1004}
1005
1894df02 1006static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
bb729f75 1007{
1894df02 1008 cmd->xfer = scsi_cdb_xfer(buf);
2599aece 1009 switch (buf[0]) {
2ec749cb 1010 case TEST_UNIT_READY:
5e30a07d 1011 case REWIND:
2ec749cb 1012 case START_STOP:
00a01ad4 1013 case SET_CAPACITY:
2ec749cb 1014 case WRITE_FILEMARKS:
06b86357 1015 case WRITE_FILEMARKS_16:
2ec749cb 1016 case SPACE:
a5e3d9ef
BK
1017 case RESERVE:
1018 case RELEASE:
2ec749cb
GH
1019 case ERASE:
1020 case ALLOW_MEDIUM_REMOVAL:
2ec749cb
GH
1021 case SEEK_10:
1022 case SYNCHRONIZE_CACHE:
06b86357
PB
1023 case SYNCHRONIZE_CACHE_16:
1024 case LOCATE_16:
2ec749cb 1025 case LOCK_UNLOCK_CACHE:
2ec749cb
GH
1026 case SET_CD_SPEED:
1027 case SET_LIMITS:
5e30a07d 1028 case WRITE_LONG_10:
2ec749cb 1029 case UPDATE_BLOCK:
06b86357
PB
1030 case RESERVE_TRACK:
1031 case SET_READ_AHEAD:
1032 case PRE_FETCH:
1033 case PRE_FETCH_16:
1034 case ALLOW_OVERWRITE:
2599aece 1035 cmd->xfer = 0;
2ec749cb 1036 break;
d12ad44c
PB
1037 case VERIFY_10:
1038 case VERIFY_12:
1039 case VERIFY_16:
1040 if ((buf[1] & 2) == 0) {
1041 cmd->xfer = 0;
7ef8cf9a 1042 } else if ((buf[1] & 4) != 0) {
d12ad44c
PB
1043 cmd->xfer = 1;
1044 }
1045 cmd->xfer *= dev->blocksize;
1046 break;
2ec749cb
GH
1047 case MODE_SENSE:
1048 break;
5e30a07d 1049 case WRITE_SAME_10:
a5ee9085 1050 case WRITE_SAME_16:
4397a018 1051 cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
2ec749cb 1052 break;
5e30a07d 1053 case READ_CAPACITY_10:
2599aece 1054 cmd->xfer = 8;
2ec749cb
GH
1055 break;
1056 case READ_BLOCK_LIMITS:
2599aece 1057 cmd->xfer = 6;
2ec749cb 1058 break;
2ec749cb 1059 case SEND_VOLUME_TAG:
06b86357
PB
1060 /* GPCMD_SET_STREAMING from multimedia commands. */
1061 if (dev->type == TYPE_ROM) {
1062 cmd->xfer = buf[10] | (buf[9] << 8);
1063 } else {
1064 cmd->xfer = buf[9] | (buf[8] << 8);
1065 }
2ec749cb 1066 break;
f62d0594
PB
1067 case WRITE_6:
1068 /* length 0 means 256 blocks */
1069 if (cmd->xfer == 0) {
1070 cmd->xfer = 256;
1071 }
c4ce4c4b 1072 /* fall through */
2ec749cb 1073 case WRITE_10:
5e30a07d 1074 case WRITE_VERIFY_10:
2ec749cb
GH
1075 case WRITE_12:
1076 case WRITE_VERIFY_12:
bd536cf3
GH
1077 case WRITE_16:
1078 case WRITE_VERIFY_16:
2599aece 1079 cmd->xfer *= dev->blocksize;
2ec749cb 1080 break;
2ec749cb
GH
1081 case READ_6:
1082 case READ_REVERSE:
f62d0594
PB
1083 /* length 0 means 256 blocks */
1084 if (cmd->xfer == 0) {
1085 cmd->xfer = 256;
1086 }
c4ce4c4b 1087 /* fall through */
f62d0594 1088 case READ_10:
2ec749cb 1089 case READ_12:
bd536cf3 1090 case READ_16:
2599aece 1091 cmd->xfer *= dev->blocksize;
2ec749cb 1092 break;
06b86357
PB
1093 case FORMAT_UNIT:
1094 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1095 * for block devices are restricted to the header right now. */
1096 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1097 cmd->xfer = 12;
1098 } else {
1099 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1100 }
1101 break;
2ec749cb 1102 case INQUIRY:
06b86357
PB
1103 case RECEIVE_DIAGNOSTIC:
1104 case SEND_DIAGNOSTIC:
2599aece 1105 cmd->xfer = buf[4] | (buf[3] << 8);
2ec749cb 1106 break;
06b86357
PB
1107 case READ_CD:
1108 case READ_BUFFER:
1109 case WRITE_BUFFER:
1110 case SEND_CUE_SHEET:
1111 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1112 break;
1113 case PERSISTENT_RESERVE_OUT:
1114 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1115 break;
1116 case ERASE_12:
1117 if (dev->type == TYPE_ROM) {
1118 /* MMC command GET PERFORMANCE. */
1119 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1120 buf[10], buf[1] & 0x1f);
1121 }
1122 break;
1123 case MECHANISM_STATUS:
1124 case READ_DVD_STRUCTURE:
1125 case SEND_DVD_STRUCTURE:
c7126d5b
NB
1126 case MAINTENANCE_OUT:
1127 case MAINTENANCE_IN:
2599aece 1128 if (dev->type == TYPE_ROM) {
c7126d5b 1129 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
2599aece 1130 cmd->xfer = buf[9] | (buf[8] << 8);
c7126d5b
NB
1131 }
1132 break;
e4b65262
CM
1133 case ATA_PASSTHROUGH_12:
1134 if (dev->type == TYPE_ROM) {
1135 /* BLANK command of MMC */
1136 cmd->xfer = 0;
1137 } else {
1894df02 1138 cmd->xfer = ata_passthrough_12_xfer(dev, buf);
e4b65262
CM
1139 }
1140 break;
1141 case ATA_PASSTHROUGH_16:
1894df02 1142 cmd->xfer = ata_passthrough_16_xfer(dev, buf);
e4b65262 1143 break;
2ec749cb
GH
1144 }
1145 return 0;
1146}
1147
1894df02 1148static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
2ec749cb 1149{
2599aece 1150 switch (buf[0]) {
2ec749cb 1151 /* stream commands */
06b86357
PB
1152 case ERASE_12:
1153 case ERASE_16:
1154 cmd->xfer = 0;
1155 break;
2ec749cb
GH
1156 case READ_6:
1157 case READ_REVERSE:
1158 case RECOVER_BUFFERED_DATA:
1159 case WRITE_6:
2599aece
PB
1160 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1161 if (buf[1] & 0x01) { /* fixed */
1162 cmd->xfer *= dev->blocksize;
1163 }
2ec749cb 1164 break;
065c2599
PB
1165 case READ_16:
1166 case READ_REVERSE_16:
1167 case VERIFY_16:
1168 case WRITE_16:
065c2599
PB
1169 cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1170 if (buf[1] & 0x01) { /* fixed */
1171 cmd->xfer *= dev->blocksize;
1172 }
1173 break;
2ec749cb 1174 case REWIND:
15e58a21 1175 case LOAD_UNLOAD:
2599aece 1176 cmd->xfer = 0;
2ec749cb 1177 break;
06b86357
PB
1178 case SPACE_16:
1179 cmd->xfer = buf[13] | (buf[12] << 8);
1180 break;
1181 case READ_POSITION:
9ce1bb2d
CH
1182 switch (buf[1] & 0x1f) /* operation code */ {
1183 case SHORT_FORM_BLOCK_ID:
1184 case SHORT_FORM_VENDOR_SPECIFIC:
1185 cmd->xfer = 20;
1186 break;
1187 case LONG_FORM:
1188 cmd->xfer = 32;
1189 break;
1190 case EXTENDED_FORM:
1191 cmd->xfer = buf[8] | (buf[7] << 8);
1192 break;
1193 default:
1194 return -1;
1195 }
1196
06b86357
PB
1197 break;
1198 case FORMAT_UNIT:
1199 cmd->xfer = buf[4] | (buf[3] << 8);
1200 break;
2ec749cb
GH
1201 /* generic commands */
1202 default:
1894df02 1203 return scsi_req_xfer(cmd, dev, buf);
2ec749cb
GH
1204 }
1205 return 0;
1206}
1207
1894df02 1208static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
40723a99
CH
1209{
1210 switch (buf[0]) {
1211 /* medium changer commands */
1212 case EXCHANGE_MEDIUM:
1213 case INITIALIZE_ELEMENT_STATUS:
1214 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1215 case MOVE_MEDIUM:
1216 case POSITION_TO_ELEMENT:
1217 cmd->xfer = 0;
1218 break;
1219 case READ_ELEMENT_STATUS:
1220 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1221 break;
1222
1223 /* generic commands */
1224 default:
1894df02 1225 return scsi_req_xfer(cmd, dev, buf);
40723a99
CH
1226 }
1227 return 0;
1228}
1229
297b044a
JL
1230static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1231{
1232 switch (buf[0]) {
1233 /* Scanner commands */
1234 case OBJECT_POSITION:
1235 cmd->xfer = 0;
1236 break;
1237 case SCAN:
1238 cmd->xfer = buf[4];
1239 break;
1240 case READ_10:
1241 case SEND:
1242 case GET_WINDOW:
1243 case SET_WINDOW:
1244 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1245 break;
1246 default:
1247 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1248 return scsi_req_xfer(cmd, dev, buf);
1249 }
1250
1251 return 0;
1252}
40723a99 1253
2599aece 1254static void scsi_cmd_xfer_mode(SCSICommand *cmd)
97a06435 1255{
a5ee9085
PB
1256 if (!cmd->xfer) {
1257 cmd->mode = SCSI_XFER_NONE;
1258 return;
1259 }
2599aece 1260 switch (cmd->buf[0]) {
97a06435
GH
1261 case WRITE_6:
1262 case WRITE_10:
5e30a07d 1263 case WRITE_VERIFY_10:
97a06435
GH
1264 case WRITE_12:
1265 case WRITE_VERIFY_12:
bd536cf3
GH
1266 case WRITE_16:
1267 case WRITE_VERIFY_16:
d12ad44c
PB
1268 case VERIFY_10:
1269 case VERIFY_12:
1270 case VERIFY_16:
97a06435
GH
1271 case COPY:
1272 case COPY_VERIFY:
1273 case COMPARE:
1274 case CHANGE_DEFINITION:
1275 case LOG_SELECT:
1276 case MODE_SELECT:
1277 case MODE_SELECT_10:
1278 case SEND_DIAGNOSTIC:
1279 case WRITE_BUFFER:
1280 case FORMAT_UNIT:
1281 case REASSIGN_BLOCKS:
97a06435
GH
1282 case SEARCH_EQUAL:
1283 case SEARCH_HIGH:
1284 case SEARCH_LOW:
1285 case UPDATE_BLOCK:
5e30a07d
HR
1286 case WRITE_LONG_10:
1287 case WRITE_SAME_10:
a5ee9085 1288 case WRITE_SAME_16:
381b634c 1289 case UNMAP:
97a06435
GH
1290 case SEARCH_HIGH_12:
1291 case SEARCH_EQUAL_12:
1292 case SEARCH_LOW_12:
97a06435
GH
1293 case MEDIUM_SCAN:
1294 case SEND_VOLUME_TAG:
06b86357
PB
1295 case SEND_CUE_SHEET:
1296 case SEND_DVD_STRUCTURE:
01bedeba 1297 case PERSISTENT_RESERVE_OUT:
c7126d5b 1298 case MAINTENANCE_OUT:
297b044a
JL
1299 case SET_WINDOW:
1300 case SCAN:
1301 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1302 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1303 */
2599aece 1304 cmd->mode = SCSI_XFER_TO_DEV;
97a06435 1305 break;
e4b65262
CM
1306 case ATA_PASSTHROUGH_12:
1307 case ATA_PASSTHROUGH_16:
1308 /* T_DIR */
1309 cmd->mode = (cmd->buf[2] & 0x8) ?
1310 SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1311 break;
97a06435 1312 default:
a5ee9085 1313 cmd->mode = SCSI_XFER_FROM_DEV;
97a06435
GH
1314 break;
1315 }
1316}
1317
fe9d8927
JM
1318int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
1319 size_t buf_len)
1894df02
HR
1320{
1321 int rc;
c170aad8 1322 int len;
1894df02
HR
1323
1324 cmd->lba = -1;
c170aad8 1325 len = scsi_cdb_length(buf);
6d1511ce 1326 if (len < 0 || len > buf_len) {
c170aad8
PB
1327 return -1;
1328 }
28b70c9d 1329
c170aad8 1330 cmd->len = len;
40723a99
CH
1331 switch (dev->type) {
1332 case TYPE_TAPE:
1894df02 1333 rc = scsi_req_stream_xfer(cmd, dev, buf);
40723a99
CH
1334 break;
1335 case TYPE_MEDIUM_CHANGER:
1894df02 1336 rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
40723a99 1337 break;
297b044a
JL
1338 case TYPE_SCANNER:
1339 rc = scsi_req_scanner_length(cmd, dev, buf);
1340 break;
40723a99 1341 default:
1894df02 1342 rc = scsi_req_xfer(cmd, dev, buf);
40723a99 1343 break;
2ec749cb 1344 }
40723a99 1345
2ec749cb
GH
1346 if (rc != 0)
1347 return rc;
1348
afa46c46
PB
1349 memcpy(cmd->buf, buf, cmd->len);
1350 scsi_cmd_xfer_mode(cmd);
1351 cmd->lba = scsi_cmd_lba(cmd);
2ec749cb
GH
1352 return 0;
1353}
ed3a34a3 1354
53200fad
PB
1355void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1356{
1357 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1358
1359 scsi_device_set_ua(dev, sense);
1360 if (bus->info->change) {
1361 bus->info->change(bus, dev, sense);
1362 }
1363}
1364
ad2d30f7
PB
1365SCSIRequest *scsi_req_ref(SCSIRequest *req)
1366{
8e86b93c 1367 assert(req->refcount > 0);
ad2d30f7
PB
1368 req->refcount++;
1369 return req;
1370}
1371
1372void scsi_req_unref(SCSIRequest *req)
1373{
68bd348a 1374 assert(req->refcount > 0);
ad2d30f7 1375 if (--req->refcount == 0) {
cac3c384
PB
1376 BusState *qbus = req->dev->qdev.parent_bus;
1377 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1378
8e86b93c
PB
1379 if (bus->info->free_request && req->hba_private) {
1380 bus->info->free_request(bus, req->hba_private);
1381 }
12010e7b
PB
1382 if (req->ops->free_req) {
1383 req->ops->free_req(req);
ad2d30f7 1384 }
cac3c384
PB
1385 object_unref(OBJECT(req->dev));
1386 object_unref(OBJECT(qbus->parent));
633dccb4 1387 g_free(req);
ad2d30f7
PB
1388 }
1389}
1390
ad3376cc
PB
1391/* Tell the device that we finished processing this chunk of I/O. It
1392 will start the next chunk or complete the command. */
1393void scsi_req_continue(SCSIRequest *req)
1394{
6f6710aa
PB
1395 if (req->io_canceled) {
1396 trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1397 return;
1398 }
ad3376cc
PB
1399 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1400 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
12010e7b 1401 req->ops->write_data(req);
ad3376cc 1402 } else {
12010e7b 1403 req->ops->read_data(req);
ad3376cc
PB
1404 }
1405}
1406
ab9adc88
PB
1407/* Called by the devices when data is ready for the HBA. The HBA should
1408 start a DMA operation to read or fill the device's data buffer.
ad3376cc 1409 Once it completes, calling scsi_req_continue will restart I/O. */
ab9adc88
PB
1410void scsi_req_data(SCSIRequest *req, int len)
1411{
3d5aba97 1412 uint8_t *buf;
e88c591d
PB
1413 if (req->io_canceled) {
1414 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
01e95455 1415 return;
e88c591d 1416 }
01e95455
PB
1417 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1418 assert(req->cmd.mode != SCSI_XFER_NONE);
3d5aba97 1419 if (!req->sg) {
5f412602 1420 req->residual -= len;
3d5aba97
PB
1421 req->bus->info->transfer_data(req, len);
1422 return;
1423 }
1424
1425 /* If the device calls scsi_req_data and the HBA specified a
1426 * scatter/gather list, the transfer has to happen in a single
1427 * step. */
1428 assert(!req->dma_started);
1429 req->dma_started = true;
1430
1431 buf = scsi_req_get_buf(req);
1432 if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
f02b664a
PMD
1433 dma_buf_read(buf, len, &req->residual, req->sg,
1434 MEMTXATTRS_UNSPECIFIED);
3d5aba97 1435 } else {
f02b664a
PMD
1436 dma_buf_write(buf, len, &req->residual, req->sg,
1437 MEMTXATTRS_UNSPECIFIED);
3d5aba97
PB
1438 }
1439 scsi_req_continue(req);
ab9adc88
PB
1440}
1441
ec766865
GH
1442void scsi_req_print(SCSIRequest *req)
1443{
1444 FILE *fp = stderr;
1445 int i;
1446
1447 fprintf(fp, "[%s id=%d] %s",
1448 req->dev->qdev.parent_bus->name,
1449 req->dev->id,
1450 scsi_command_name(req->cmd.buf[0]));
1451 for (i = 1; i < req->cmd.len; i++) {
1452 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1453 }
1454 switch (req->cmd.mode) {
1455 case SCSI_XFER_NONE:
1456 fprintf(fp, " - none\n");
1457 break;
1458 case SCSI_XFER_FROM_DEV:
1459 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1460 break;
1461 case SCSI_XFER_TO_DEV:
1462 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1463 break;
1464 default:
1465 fprintf(fp, " - Oops\n");
1466 break;
1467 }
1468}
1469
f3126d65
HR
1470void scsi_req_complete_failed(SCSIRequest *req, int host_status)
1471{
1472 SCSISense sense;
1473 int status;
1474
1475 assert(req->status == -1 && req->host_status == -1);
1476 assert(req->ops != &reqops_unit_attention);
1477
1478 if (!req->bus->info->fail) {
1479 status = scsi_sense_from_host_status(req->host_status, &sense);
1480 if (status == CHECK_CONDITION) {
1481 scsi_req_build_sense(req, sense);
1482 }
1483 scsi_req_complete(req, status);
1484 return;
1485 }
1486
1487 req->host_status = host_status;
1488 scsi_req_ref(req);
1489 scsi_req_dequeue(req);
1490 req->bus->info->fail(req);
1491
1492 /* Cancelled requests might end up being completed instead of cancelled */
1493 notifier_list_notify(&req->cancel_notifiers, req);
1494 scsi_req_unref(req);
1495}
1496
682a9b21 1497void scsi_req_complete(SCSIRequest *req, int status)
ed3a34a3 1498{
f3126d65 1499 assert(req->status == -1 && req->host_status == -1);
682a9b21 1500 req->status = status;
f3126d65 1501 req->host_status = SCSI_HOST_OK;
b45ef674 1502
335f560f 1503 assert(req->sense_len <= sizeof(req->sense));
b45ef674
PB
1504 if (status == GOOD) {
1505 req->sense_len = 0;
1506 }
1507
1508 if (req->sense_len) {
1509 memcpy(req->dev->sense, req->sense, req->sense_len);
3653d8c4
PB
1510 req->dev->sense_len = req->sense_len;
1511 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1512 } else {
1513 req->dev->sense_len = 0;
1514 req->dev->sense_is_ua = false;
b45ef674 1515 }
b45ef674 1516
6dc06f08
PB
1517 /*
1518 * Unit attention state is now stored in the device's sense buffer
1519 * if the HBA didn't do autosense. Clear the pending unit attention
1520 * flags.
1521 */
1522 scsi_clear_unit_attention(req);
1523
ad2d30f7 1524 scsi_req_ref(req);
e8637c90 1525 scsi_req_dequeue(req);
5f412602 1526 req->bus->info->complete(req, req->residual);
8e0a9320
FZ
1527
1528 /* Cancelled requests might end up being completed instead of cancelled */
1529 notifier_list_notify(&req->cancel_notifiers, req);
ad2d30f7 1530 scsi_req_unref(req);
ed3a34a3 1531}
db07c0f8 1532
d5776465
FZ
1533/* Called by the devices when the request is canceled. */
1534void scsi_req_cancel_complete(SCSIRequest *req)
1535{
1536 assert(req->io_canceled);
1537 if (req->bus->info->cancel) {
1538 req->bus->info->cancel(req);
1539 }
8e0a9320 1540 notifier_list_notify(&req->cancel_notifiers, req);
d5776465
FZ
1541 scsi_req_unref(req);
1542}
1543
8e0a9320
FZ
1544/* Cancel @req asynchronously. @notifier is added to @req's cancellation
1545 * notifier list, the bus will be notified the requests cancellation is
1546 * completed.
1547 * */
1548void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1549{
1550 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1551 if (notifier) {
1552 notifier_list_add(&req->cancel_notifiers, notifier);
1553 }
3daa4107
PB
1554 if (req->io_canceled) {
1555 /* A blk_aio_cancel_async is pending; when it finishes,
1556 * scsi_req_cancel_complete will be called and will
1557 * call the notifier we just added. Just wait for that.
1558 */
1559 assert(req->aiocb);
1560 return;
1561 }
1562 /* Dropped in scsi_req_cancel_complete. */
8e0a9320
FZ
1563 scsi_req_ref(req);
1564 scsi_req_dequeue(req);
1565 req->io_canceled = true;
1566 if (req->aiocb) {
4be74634 1567 blk_aio_cancel_async(req->aiocb);
2aeba9d8
FZ
1568 } else {
1569 scsi_req_cancel_complete(req);
8e0a9320
FZ
1570 }
1571}
1572
94d3f98a
PB
1573void scsi_req_cancel(SCSIRequest *req)
1574{
814589c4 1575 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
e88c591d
PB
1576 if (!req->enqueued) {
1577 return;
94d3f98a 1578 }
3daa4107
PB
1579 assert(!req->io_canceled);
1580 /* Dropped in scsi_req_cancel_complete. */
94d3f98a
PB
1581 scsi_req_ref(req);
1582 scsi_req_dequeue(req);
e88c591d 1583 req->io_canceled = true;
a83cfd12 1584 if (req->aiocb) {
4be74634 1585 blk_aio_cancel(req->aiocb);
488eef2f
PB
1586 } else {
1587 scsi_req_cancel_complete(req);
e88c591d 1588 }
94d3f98a
PB
1589}
1590
e48e84ea
PB
1591static int scsi_ua_precedence(SCSISense sense)
1592{
1593 if (sense.key != UNIT_ATTENTION) {
1594 return INT_MAX;
1595 }
1596 if (sense.asc == 0x29 && sense.ascq == 0x04) {
1597 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1598 return 1;
1599 } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1600 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1601 return 2;
1602 } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1603 /* These two go with "all others". */
1604 ;
1605 } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1606 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1607 * POWER ON OCCURRED = 1
1608 * SCSI BUS RESET OCCURRED = 2
1609 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1610 * I_T NEXUS LOSS OCCURRED = 7
1611 */
1612 return sense.ascq;
1613 } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1614 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1615 return 8;
1616 }
1617 return (sense.asc << 8) | sense.ascq;
1618}
1619
8cc5583a
VB
1620void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense)
1621{
1622 int prec1, prec2;
1623 if (sense.key != UNIT_ATTENTION) {
1624 return;
1625 }
1626
1627 /*
1628 * Override a pre-existing unit attention condition, except for a more
1629 * important reset condition.
1630 */
1631 prec1 = scsi_ua_precedence(bus->unit_attention);
1632 prec2 = scsi_ua_precedence(sense);
1633 if (prec2 < prec1) {
1634 bus->unit_attention = sense;
1635 }
1636}
1637
e48e84ea
PB
1638void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1639{
1640 int prec1, prec2;
1641 if (sense.key != UNIT_ATTENTION) {
1642 return;
1643 }
1644 trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1645 sense.asc, sense.ascq);
1646
1647 /*
1648 * Override a pre-existing unit attention condition, except for a more
1649 * important reset condition.
1650 */
1651 prec1 = scsi_ua_precedence(sdev->unit_attention);
1652 prec2 = scsi_ua_precedence(sense);
1653 if (prec2 < prec1) {
1654 sdev->unit_attention = sense;
1655 }
1656}
1657
c7b48872 1658void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
c557e889
PB
1659{
1660 SCSIRequest *req;
1661
8aad35f6 1662 aio_context_acquire(blk_get_aio_context(sdev->conf.blk));
c557e889
PB
1663 while (!QTAILQ_EMPTY(&sdev->requests)) {
1664 req = QTAILQ_FIRST(&sdev->requests);
8aad35f6 1665 scsi_req_cancel_async(req, NULL);
c557e889 1666 }
8aad35f6
PB
1667 blk_drain(sdev->conf.blk);
1668 aio_context_release(blk_get_aio_context(sdev->conf.blk));
e48e84ea 1669 scsi_device_set_ua(sdev, sense);
c557e889
PB
1670}
1671
766aa2de
SH
1672void scsi_device_drained_begin(SCSIDevice *sdev)
1673{
1674 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1675 if (!bus) {
1676 return;
1677 }
1678
1679 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1680 assert(bus->drain_count < INT_MAX);
1681
1682 /*
1683 * Multiple BlockBackends can be on a SCSIBus and each may begin/end
1684 * draining at any time. Keep a counter so HBAs only see begin/end once.
1685 */
1686 if (bus->drain_count++ == 0) {
1687 trace_scsi_bus_drained_begin(bus, sdev);
1688 if (bus->info->drained_begin) {
1689 bus->info->drained_begin(bus);
1690 }
1691 }
1692}
1693
1694void scsi_device_drained_end(SCSIDevice *sdev)
1695{
1696 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, sdev->qdev.parent_bus);
1697 if (!bus) {
1698 return;
1699 }
1700
1701 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1702 assert(bus->drain_count > 0);
1703
1704 if (bus->drain_count-- == 1) {
1705 trace_scsi_bus_drained_end(bus, sdev);
1706 if (bus->info->drained_end) {
1707 bus->info->drained_end(bus);
1708 }
1709 }
1710}
1711
baa1bd89
PB
1712static char *scsibus_get_dev_path(DeviceState *dev)
1713{
e1dc6815 1714 SCSIDevice *d = SCSI_DEVICE(dev);
baa1bd89 1715 DeviceState *hba = dev->parent_bus->parent;
09e5ab63 1716 char *id;
b7c8c35f 1717 char *path;
baa1bd89 1718
09e5ab63 1719 id = qdev_get_dev_path(hba);
baa1bd89 1720 if (id) {
b7c8c35f 1721 path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
baa1bd89 1722 } else {
b7c8c35f 1723 path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
baa1bd89 1724 }
b7c8c35f
PB
1725 g_free(id);
1726 return path;
baa1bd89
PB
1727}
1728
db07c0f8
GN
1729static char *scsibus_get_fw_dev_path(DeviceState *dev)
1730{
b9eea3e6 1731 SCSIDevice *d = SCSI_DEVICE(dev);
a5cf8262
JM
1732 return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1733 qdev_fw_name(dev), d->id, d->lun);
f48a7a6e
PB
1734}
1735
63f740dd
PB
1736/* SCSI request list. For simplicity, pv points to the whole device */
1737
2c21ee76 1738static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
3ddba9a9 1739 const VMStateField *field, JSONWriter *vmdesc)
63f740dd
PB
1740{
1741 SCSIDevice *s = pv;
1742 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1743 SCSIRequest *req;
1744
1745 QTAILQ_FOREACH(req, &s->requests, next) {
1746 assert(!req->io_canceled);
f3126d65 1747 assert(req->status == -1 && req->host_status == -1);
63f740dd
PB
1748 assert(req->enqueued);
1749
18eef3bc 1750 qemu_put_sbyte(f, req->retry ? 1 : 2);
63f740dd
PB
1751 qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1752 qemu_put_be32s(f, &req->tag);
1753 qemu_put_be32s(f, &req->lun);
1754 if (bus->info->save_request) {
1755 bus->info->save_request(f, req);
1756 }
1757 if (req->ops->save_request) {
1758 req->ops->save_request(f, req);
1759 }
1760 }
1761 qemu_put_sbyte(f, 0);
2c21ee76
JD
1762
1763 return 0;
63f740dd
PB
1764}
1765
2c21ee76 1766static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
03fee66f 1767 const VMStateField *field)
63f740dd
PB
1768{
1769 SCSIDevice *s = pv;
1770 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
18eef3bc 1771 int8_t sbyte;
63f740dd 1772
18eef3bc 1773 while ((sbyte = qemu_get_sbyte(f)) > 0) {
63f740dd
PB
1774 uint8_t buf[SCSI_CMD_BUF_SIZE];
1775 uint32_t tag;
1776 uint32_t lun;
1777 SCSIRequest *req;
1778
1779 qemu_get_buffer(f, buf, sizeof(buf));
1780 qemu_get_be32s(f, &tag);
1781 qemu_get_be32s(f, &lun);
fe9d8927
JM
1782 /*
1783 * A too-short CDB would have been rejected by scsi_req_new, so just use
1784 * SCSI_CMD_BUF_SIZE as the CDB length.
1785 */
1786 req = scsi_req_new(s, tag, lun, buf, sizeof(buf), NULL);
18eef3bc 1787 req->retry = (sbyte == 1);
63f740dd
PB
1788 if (bus->info->load_request) {
1789 req->hba_private = bus->info->load_request(f, req);
1790 }
1791 if (req->ops->load_request) {
1792 req->ops->load_request(f, req);
1793 }
1794
1795 /* Just restart it later. */
63f740dd
PB
1796 scsi_req_enqueue_internal(req);
1797
1798 /* At this point, the request will be kept alive by the reference
1799 * added by scsi_req_enqueue_internal, so we can release our reference.
1800 * The HBA of course will add its own reference in the load_request
1801 * callback if it needs to hold on the SCSIRequest.
1802 */
1803 scsi_req_unref(req);
1804 }
1805
1806 return 0;
1807}
1808
12badfc2 1809static const VMStateInfo vmstate_info_scsi_requests = {
63f740dd
PB
1810 .name = "scsi-requests",
1811 .get = get_scsi_requests,
1812 .put = put_scsi_requests,
1813};
1814
2e323f03
FZ
1815static bool scsi_sense_state_needed(void *opaque)
1816{
1817 SCSIDevice *s = opaque;
1818
1819 return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1820}
1821
1822static const VMStateDescription vmstate_scsi_sense_state = {
1823 .name = "SCSIDevice/sense",
1824 .version_id = 1,
1825 .minimum_version_id = 1,
5cd8cada 1826 .needed = scsi_sense_state_needed,
d49805ae 1827 .fields = (VMStateField[]) {
2e323f03
FZ
1828 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1829 SCSI_SENSE_BUF_SIZE_OLD,
1830 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1831 VMSTATE_END_OF_LIST()
1832 }
1833};
1834
63f740dd
PB
1835const VMStateDescription vmstate_scsi_device = {
1836 .name = "SCSIDevice",
1837 .version_id = 1,
1838 .minimum_version_id = 1,
63f740dd
PB
1839 .fields = (VMStateField[]) {
1840 VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1841 VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1842 VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1843 VMSTATE_BOOL(sense_is_ua, SCSIDevice),
2e323f03 1844 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
63f740dd
PB
1845 VMSTATE_UINT32(sense_len, SCSIDevice),
1846 {
1847 .name = "requests",
1848 .version_id = 0,
1849 .field_exists = NULL,
1850 .size = 0, /* ouch */
1851 .info = &vmstate_info_scsi_requests,
1852 .flags = VMS_SINGLE,
1853 .offset = 0,
1854 },
1855 VMSTATE_END_OF_LIST()
2e323f03 1856 },
5cd8cada
JQ
1857 .subsections = (const VMStateDescription*[]) {
1858 &vmstate_scsi_sense_state,
1859 NULL
63f740dd
PB
1860 }
1861};
1862
42a90a89
PB
1863static Property scsi_props[] = {
1864 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
1865 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
1866 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
1867 DEFINE_PROP_END_OF_LIST(),
1868};
1869
39bffca2
AL
1870static void scsi_device_class_init(ObjectClass *klass, void *data)
1871{
1872 DeviceClass *k = DEVICE_CLASS(klass);
125ee0ed 1873 set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
a818a4b6
FZ
1874 k->bus_type = TYPE_SCSI_BUS;
1875 k->realize = scsi_qdev_realize;
a818a4b6 1876 k->unrealize = scsi_qdev_unrealize;
4f67d30b 1877 device_class_set_props(k, scsi_props);
39bffca2
AL
1878}
1879
44fb6337
GA
1880static void scsi_dev_instance_init(Object *obj)
1881{
1882 DeviceState *dev = DEVICE(obj);
e1dc6815 1883 SCSIDevice *s = SCSI_DEVICE(dev);
44fb6337
GA
1884
1885 device_add_bootindex_property(obj, &s->conf.bootindex,
1886 "bootindex", NULL,
40c2281c 1887 &s->qdev);
44fb6337
GA
1888}
1889
8c43a6f0 1890static const TypeInfo scsi_device_type_info = {
b9eea3e6
AL
1891 .name = TYPE_SCSI_DEVICE,
1892 .parent = TYPE_DEVICE,
1893 .instance_size = sizeof(SCSIDevice),
1894 .abstract = true,
1895 .class_size = sizeof(SCSIDeviceClass),
39bffca2 1896 .class_init = scsi_device_class_init,
44fb6337 1897 .instance_init = scsi_dev_instance_init,
b9eea3e6
AL
1898};
1899
42a90a89
PB
1900static void scsi_bus_class_init(ObjectClass *klass, void *data)
1901{
1902 BusClass *k = BUS_CLASS(klass);
1903 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1904
1905 k->get_dev_path = scsibus_get_dev_path;
1906 k->get_fw_dev_path = scsibus_get_fw_dev_path;
1907 k->check_address = scsi_bus_check_address;
1908 hc->unplug = qdev_simple_device_unplug_cb;
1909}
1910
1911static const TypeInfo scsi_bus_info = {
1912 .name = TYPE_SCSI_BUS,
1913 .parent = TYPE_BUS,
1914 .instance_size = sizeof(SCSIBus),
1915 .class_init = scsi_bus_class_init,
1916 .interfaces = (InterfaceInfo[]) {
1917 { TYPE_HOTPLUG_HANDLER },
1918 { }
1919 }
1920};
1921
83f7d43a 1922static void scsi_register_types(void)
b9eea3e6 1923{
0d936928 1924 type_register_static(&scsi_bus_info);
b9eea3e6
AL
1925 type_register_static(&scsi_device_type_info);
1926}
1927
83f7d43a 1928type_init(scsi_register_types)