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