| 1 | /* |
| 2 | * Generic SCSI Device support |
| 3 | * |
| 4 | * Copyright (c) 2007 Bull S.A.S. |
| 5 | * Based on code by Paul Brook |
| 6 | * Based on code by Fabrice Bellard |
| 7 | * |
| 8 | * Written by Laurent Vivier <Laurent.Vivier@bull.net> |
| 9 | * |
| 10 | * This code is licensed under the LGPL. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "qemu/osdep.h" |
| 15 | #include "qapi/error.h" |
| 16 | #include "qemu/bswap.h" |
| 17 | #include "qemu/ctype.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | #include "qemu/module.h" |
| 20 | #include "hw/scsi/scsi.h" |
| 21 | #include "migration/qemu-file-types.h" |
| 22 | #include "hw/core/qdev-properties.h" |
| 23 | #include "hw/core/qdev-properties-system.h" |
| 24 | #include "hw/scsi/emulation.h" |
| 25 | #include "system/block-backend.h" |
| 26 | #include "trace.h" |
| 27 | |
| 28 | #ifdef __linux__ |
| 29 | |
| 30 | #include <scsi/sg.h> |
| 31 | #include "scsi/constants.h" |
| 32 | |
| 33 | #ifndef MAX_UINT |
| 34 | #define MAX_UINT ((unsigned int)-1) |
| 35 | #endif |
| 36 | |
| 37 | typedef struct SCSIGenericReq { |
| 38 | SCSIRequest req; |
| 39 | uint8_t *buf; |
| 40 | int buflen; |
| 41 | int len; |
| 42 | sg_io_hdr_t io_header; |
| 43 | } SCSIGenericReq; |
| 44 | |
| 45 | static void scsi_generic_save_request(QEMUFile *f, SCSIRequest *req) |
| 46 | { |
| 47 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 48 | |
| 49 | qemu_put_sbe32s(f, &r->buflen); |
| 50 | if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 51 | assert(!r->req.sg); |
| 52 | qemu_put_buffer(f, r->buf, r->req.cmd.xfer); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void scsi_generic_load_request(QEMUFile *f, SCSIRequest *req) |
| 57 | { |
| 58 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 59 | |
| 60 | qemu_get_sbe32s(f, &r->buflen); |
| 61 | if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 62 | assert(!r->req.sg); |
| 63 | qemu_get_buffer(f, r->buf, r->req.cmd.xfer); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void scsi_free_request(SCSIRequest *req) |
| 68 | { |
| 69 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 70 | |
| 71 | g_free(r->buf); |
| 72 | } |
| 73 | |
| 74 | /* Helper function for command completion. */ |
| 75 | static void scsi_command_complete_noio(SCSIGenericReq *r, int ret) |
| 76 | { |
| 77 | int status; |
| 78 | SCSISense sense; |
| 79 | sg_io_hdr_t *io_hdr = &r->io_header; |
| 80 | |
| 81 | assert(r->req.aiocb == NULL); |
| 82 | |
| 83 | if (r->req.io_canceled) { |
| 84 | scsi_req_cancel_complete(&r->req); |
| 85 | goto done; |
| 86 | } |
| 87 | if (ret < 0) { |
| 88 | status = scsi_sense_from_errno(-ret, &sense); |
| 89 | if (status == CHECK_CONDITION) { |
| 90 | scsi_req_build_sense(&r->req, sense); |
| 91 | } |
| 92 | } else if (io_hdr->host_status != SCSI_HOST_OK) { |
| 93 | scsi_req_complete_failed(&r->req, io_hdr->host_status); |
| 94 | goto done; |
| 95 | } else if (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT) { |
| 96 | status = BUSY; |
| 97 | } else { |
| 98 | status = io_hdr->status; |
| 99 | if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) { |
| 100 | r->req.sense_len = io_hdr->sb_len_wr; |
| 101 | } |
| 102 | } |
| 103 | trace_scsi_generic_command_complete_noio(r, r->req.tag, status); |
| 104 | |
| 105 | scsi_req_complete(&r->req, status); |
| 106 | done: |
| 107 | scsi_req_unref(&r->req); |
| 108 | } |
| 109 | |
| 110 | static void scsi_command_complete(void *opaque, int ret) |
| 111 | { |
| 112 | SCSIGenericReq *r = (SCSIGenericReq *)opaque; |
| 113 | |
| 114 | assert(r->req.aiocb != NULL); |
| 115 | r->req.aiocb = NULL; |
| 116 | |
| 117 | scsi_command_complete_noio(r, ret); |
| 118 | } |
| 119 | |
| 120 | static int execute_command(BlockBackend *blk, |
| 121 | SCSIGenericReq *r, int direction, |
| 122 | BlockCompletionFunc *complete) |
| 123 | { |
| 124 | SCSIDevice *s = r->req.dev; |
| 125 | |
| 126 | r->io_header.interface_id = 'S'; |
| 127 | r->io_header.dxfer_direction = direction; |
| 128 | r->io_header.dxferp = r->buf; |
| 129 | r->io_header.dxfer_len = r->buflen; |
| 130 | r->io_header.cmdp = r->req.cmd.buf; |
| 131 | r->io_header.cmd_len = r->req.cmd.len; |
| 132 | r->io_header.mx_sb_len = sizeof(r->req.sense); |
| 133 | r->io_header.sbp = r->req.sense; |
| 134 | r->io_header.timeout = s->io_timeout * 1000; |
| 135 | r->io_header.usr_ptr = r; |
| 136 | r->io_header.flags |= SG_FLAG_DIRECT_IO; |
| 137 | |
| 138 | trace_scsi_generic_aio_sgio_command(r->req.tag, r->req.cmd.buf[0], |
| 139 | r->io_header.timeout); |
| 140 | r->req.aiocb = blk_aio_ioctl(blk, SG_IO, &r->io_header, complete, r); |
| 141 | if (r->req.aiocb == NULL) { |
| 142 | return -EIO; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static uint64_t calculate_max_transfer(SCSIDevice *s) |
| 149 | { |
| 150 | uint64_t max_transfer = blk_get_max_hw_transfer(s->conf.blk); |
| 151 | uint32_t max_iov = blk_get_max_hw_iov(s->conf.blk); |
| 152 | |
| 153 | assert(max_transfer); |
| 154 | max_transfer = MIN_NON_ZERO(max_transfer, |
| 155 | max_iov * qemu_real_host_page_size()); |
| 156 | |
| 157 | return max_transfer / s->blocksize; |
| 158 | } |
| 159 | |
| 160 | static int scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s, int len) |
| 161 | { |
| 162 | uint8_t page, page_idx; |
| 163 | |
| 164 | /* |
| 165 | * EVPD set to zero returns the standard INQUIRY data. |
| 166 | * |
| 167 | * Check if scsi_version is unset (-1) to avoid re-defining it |
| 168 | * each time an INQUIRY with standard data is received. |
| 169 | * scsi_version is initialized with -1 in scsi_generic_reset |
| 170 | * and scsi_disk_reset, making sure that we'll set the |
| 171 | * scsi_version after a reset. If the version field of the |
| 172 | * INQUIRY response somehow changes after a guest reboot, |
| 173 | * we'll be able to keep track of it. |
| 174 | * |
| 175 | * On SCSI-2 and older, first 3 bits of byte 2 is the |
| 176 | * ANSI-approved version, while on later versions the |
| 177 | * whole byte 2 contains the version. Check if we're dealing |
| 178 | * with a newer version and, in that case, assign the |
| 179 | * whole byte. |
| 180 | */ |
| 181 | if (s->scsi_version == -1 && !(r->req.cmd.buf[1] & 0x01)) { |
| 182 | s->scsi_version = r->buf[2] & 0x07; |
| 183 | if (s->scsi_version > 2) { |
| 184 | s->scsi_version = r->buf[2]; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if ((s->type == TYPE_DISK || s->type == TYPE_ZBC) && |
| 189 | (r->req.cmd.buf[1] & 0x01)) { |
| 190 | page = r->req.cmd.buf[2]; |
| 191 | if (page == 0xb0 && r->buflen >= 8) { |
| 192 | uint8_t buf[16] = {}; |
| 193 | uint8_t buf_used = MIN(r->buflen, 16); |
| 194 | uint64_t max_transfer = calculate_max_transfer(s); |
| 195 | |
| 196 | memcpy(buf, r->buf, buf_used); |
| 197 | stl_be_p(&buf[8], max_transfer); |
| 198 | stl_be_p(&buf[12], MIN_NON_ZERO(max_transfer, ldl_be_p(&buf[12]))); |
| 199 | memcpy(r->buf + 8, buf + 8, buf_used - 8); |
| 200 | |
| 201 | } else if (s->needs_vpd_bl_emulation && page == 0x00 && r->buflen >= 4) { |
| 202 | /* |
| 203 | * Now we're capable of supplying the VPD Block Limits |
| 204 | * response if the hardware can't. Add it in the INQUIRY |
| 205 | * Supported VPD pages response in case we are using the |
| 206 | * emulation for this device. |
| 207 | * |
| 208 | * This way, the guest kernel will be aware of the support |
| 209 | * and will use it to proper setup the SCSI device. |
| 210 | * |
| 211 | * VPD page numbers must be sorted, so insert 0xb0 at the |
| 212 | * right place with an in-place insert. When the while loop |
| 213 | * begins the device response is at r[0] to r[page_idx - 1]. |
| 214 | */ |
| 215 | page_idx = lduw_be_p(r->buf + 2) + 4; |
| 216 | page_idx = MIN(page_idx, r->buflen); |
| 217 | while (page_idx > 4 && r->buf[page_idx - 1] >= 0xb0) { |
| 218 | if (page_idx < r->buflen) { |
| 219 | r->buf[page_idx] = r->buf[page_idx - 1]; |
| 220 | } |
| 221 | page_idx--; |
| 222 | } |
| 223 | if (page_idx < r->buflen) { |
| 224 | r->buf[page_idx] = 0xb0; |
| 225 | } |
| 226 | stw_be_p(r->buf + 2, lduw_be_p(r->buf + 2) + 1); |
| 227 | |
| 228 | if (len < r->buflen) { |
| 229 | len++; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | return len; |
| 234 | } |
| 235 | |
| 236 | static int scsi_generic_emulate_block_limits(SCSIGenericReq *r, SCSIDevice *s) |
| 237 | { |
| 238 | int len; |
| 239 | uint8_t buf[64]; |
| 240 | |
| 241 | SCSIBlockLimits bl = { |
| 242 | .max_io_sectors = calculate_max_transfer(s), |
| 243 | }; |
| 244 | |
| 245 | memset(r->buf, 0, r->buflen); |
| 246 | stb_p(buf, s->type); |
| 247 | stb_p(buf + 1, 0xb0); |
| 248 | len = scsi_emulate_block_limits(buf + 4, &bl); |
| 249 | assert(len <= sizeof(buf) - 4); |
| 250 | stw_be_p(buf + 2, len); |
| 251 | |
| 252 | memcpy(r->buf, buf, MIN(r->buflen, len + 4)); |
| 253 | |
| 254 | r->io_header.sb_len_wr = 0; |
| 255 | |
| 256 | /* |
| 257 | * We have valid contents in the reply buffer but the |
| 258 | * io_header can report a sense error coming from |
| 259 | * the hardware in scsi_command_complete_noio. Clean |
| 260 | * up the io_header to avoid reporting it. |
| 261 | */ |
| 262 | r->io_header.driver_status = 0; |
| 263 | r->io_header.status = 0; |
| 264 | |
| 265 | return r->buflen; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Patch persistent reservation capabilities that are not emulated. |
| 270 | */ |
| 271 | static void scsi_handle_persistent_reserve_in_reply(SCSIGenericReq *r, |
| 272 | SCSIDevice *s) |
| 273 | { |
| 274 | uint8_t service_action = r->req.cmd.buf[1] & 0x1f; |
| 275 | |
| 276 | if (!s->migrate_pr) { |
| 277 | return; /* when migration is disabled there is no need for patching */ |
| 278 | } |
| 279 | |
| 280 | if (service_action == PRI_REPORT_CAPABILITIES) { |
| 281 | assert(r->buflen >= 3); |
| 282 | |
| 283 | /* |
| 284 | * Clear specify initiator ports capable (SIP_C) and all target ports |
| 285 | * capable (ATC_C). |
| 286 | * |
| 287 | * SPEC_I_PT is not supported because the guest sees an emulated SCSI |
| 288 | * bus and does not have the underlying transport IDs needed to use |
| 289 | * SPEC_I_PT. |
| 290 | * |
| 291 | * ALL_TG_PT is not supported because we only track the state of this |
| 292 | * emulated I_T nexus, not the underlying device's target ports. |
| 293 | */ |
| 294 | r->buf[2] &= ~0xc; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static int scsi_generic_read_reservation(SCSIDevice *s, uint64_t *key, |
| 299 | uint8_t *resv_type, Error **errp) |
| 300 | { |
| 301 | uint8_t cmd[10] = {}; |
| 302 | uint8_t buf[24] = {}; |
| 303 | uint32_t additional_length; |
| 304 | int ret; |
| 305 | |
| 306 | *key = 0; |
| 307 | *resv_type = 0; |
| 308 | |
| 309 | cmd[0] = PERSISTENT_RESERVE_IN; |
| 310 | cmd[1] = PRI_READ_RESERVATION; |
| 311 | cmd[8] = sizeof(buf); |
| 312 | |
| 313 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
| 314 | buf, sizeof(buf), s->io_timeout, errp); |
| 315 | if (ret < 0) { |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | memcpy(&additional_length, &buf[4], sizeof(additional_length)); |
| 320 | be32_to_cpus(&additional_length); |
| 321 | |
| 322 | if (additional_length >= 0x10) { |
| 323 | memcpy(key, &buf[8], sizeof(*key)); |
| 324 | be64_to_cpus(key); |
| 325 | |
| 326 | *resv_type = buf[21] & 0xf; |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Snoop changes to registered keys and reservations so that this information |
| 333 | * can be transferred during live migration. |
| 334 | */ |
| 335 | static void scsi_handle_persistent_reserve_out_reply( |
| 336 | SCSIGenericReq *r, |
| 337 | SCSIDevice *s) |
| 338 | { |
| 339 | SCSIPRState *pr_state = &s->pr_state; |
| 340 | uint8_t service_action = r->req.cmd.buf[1] & 0x1f; |
| 341 | uint8_t resv_type = r->req.cmd.buf[2] & 0xf; |
| 342 | uint64_t old_key; |
| 343 | uint64_t new_key; |
| 344 | |
| 345 | assert(r->buflen >= 16); |
| 346 | memcpy(&old_key, &r->buf[0], sizeof(old_key)); |
| 347 | memcpy(&new_key, &r->buf[8], sizeof(new_key)); |
| 348 | be64_to_cpus(&old_key); |
| 349 | be64_to_cpus(&new_key); |
| 350 | |
| 351 | trace_scsi_generic_persistent_reserve_out_reply(service_action, resv_type, |
| 352 | old_key, new_key); |
| 353 | |
| 354 | switch (service_action) { |
| 355 | case PRO_REGISTER: /* fallthrough */ |
| 356 | case PRO_REGISTER_AND_IGNORE_EXISTING_KEY: |
| 357 | if (service_action == PRO_REGISTER && old_key == 0 && new_key == 0) { |
| 358 | /* Do nothing */ |
| 359 | } else { |
| 360 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 361 | pr_state->key = new_key; |
| 362 | if (new_key == 0) { |
| 363 | pr_state->resv_type = 0; /* release reservation */ |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | break; |
| 368 | |
| 369 | case PRO_RESERVE: |
| 370 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 371 | pr_state->resv_type = resv_type; |
| 372 | } |
| 373 | break; |
| 374 | |
| 375 | case PRO_RELEASE: |
| 376 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 377 | pr_state->resv_type = 0; |
| 378 | } |
| 379 | break; |
| 380 | |
| 381 | case PRO_CLEAR: |
| 382 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 383 | pr_state->key = 0; |
| 384 | pr_state->resv_type = 0; |
| 385 | } |
| 386 | break; |
| 387 | |
| 388 | case PRO_REPLACE_LOST_RESERVATION: |
| 389 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 390 | pr_state->key = new_key; |
| 391 | pr_state->resv_type = resv_type; |
| 392 | } |
| 393 | break; |
| 394 | |
| 395 | case PRO_PREEMPT: /* fallthrough */ |
| 396 | case PRO_PREEMPT_AND_ABORT: { |
| 397 | uint64_t dev_key; |
| 398 | uint8_t dev_resv_type; |
| 399 | Error *local_err = NULL; |
| 400 | |
| 401 | /* Not enough information to know actual state, ask the device */ |
| 402 | if (!scsi_generic_read_reservation(s, &dev_key, &dev_resv_type, |
| 403 | &local_err)) { |
| 404 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 405 | if (pr_state->key == dev_key) { |
| 406 | pr_state->resv_type = dev_resv_type; |
| 407 | } else { |
| 408 | pr_state->resv_type = 0; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | if (local_err) { |
| 413 | warn_report_err(local_err); |
| 414 | } |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * PRO_REGISTER_AND_MOVE cannot be implemented since it involves the |
| 420 | * physical SCSI bus target ports. |
| 421 | */ |
| 422 | default: |
| 423 | break; /* do nothing */ |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static bool scsi_generic_pr_register(SCSIDevice *s, uint64_t key, Error **errp) |
| 428 | { |
| 429 | uint8_t cmd[10] = {}; |
| 430 | uint8_t buf[24] = {}; |
| 431 | uint64_t key_be = cpu_to_be64(key); |
| 432 | int ret; |
| 433 | |
| 434 | cmd[0] = PERSISTENT_RESERVE_OUT; |
| 435 | cmd[1] = PRO_REGISTER; |
| 436 | cmd[8] = sizeof(buf); |
| 437 | memcpy(&buf[8], &key_be, sizeof(key_be)); |
| 438 | |
| 439 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_TO_DEV, cmd, sizeof(cmd), |
| 440 | buf, sizeof(buf), s->io_timeout, errp); |
| 441 | if (ret < 0) { |
| 442 | error_prepend(errp, "PERSISTENT RESERVE OUT with REGISTER: "); |
| 443 | return false; |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | static bool scsi_generic_pr_preempt(SCSIDevice *s, uint64_t key, |
| 449 | uint8_t resv_type, Error **errp) |
| 450 | { |
| 451 | uint8_t cmd[10] = {}; |
| 452 | uint8_t buf[24] = {}; |
| 453 | uint64_t key_be = cpu_to_be64(key); |
| 454 | int ret; |
| 455 | |
| 456 | /* |
| 457 | * The LIO iSCSI target in Linux up to at least version 7.0 rejects PREEMPT |
| 458 | * commands with a zero TYPE field although the SPC-6 specification says |
| 459 | * the field should be ignored when there is no persistent reservation. |
| 460 | * Work around this by choosing an arbitrary valid PR type value. |
| 461 | */ |
| 462 | if (resv_type == 0) { |
| 463 | resv_type = PR_TYPE_WRITE_EXCLUSIVE; |
| 464 | } |
| 465 | |
| 466 | cmd[0] = PERSISTENT_RESERVE_OUT; |
| 467 | cmd[1] = PRO_PREEMPT; |
| 468 | cmd[2] = resv_type & 0xf; |
| 469 | cmd[8] = sizeof(buf); |
| 470 | memcpy(&buf[0], &key_be, sizeof(key_be)); |
| 471 | memcpy(&buf[8], &key_be, sizeof(key_be)); |
| 472 | |
| 473 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_TO_DEV, cmd, sizeof(cmd), |
| 474 | buf, sizeof(buf), s->io_timeout, errp); |
| 475 | if (ret < 0) { |
| 476 | error_prepend(errp, "PERSISTENT RESERVE OUT with PREEMPT: "); |
| 477 | return false; |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Returns true if the given key is registered or false otherwise (including |
| 484 | * errors). |
| 485 | */ |
| 486 | static bool scsi_generic_pr_key_registered(SCSIDevice *s, uint64_t key, |
| 487 | Error **errp) |
| 488 | { |
| 489 | const size_t key_list_offset = 8; /* in READ KEYS parameter data */ |
| 490 | uint64_t key_be = cpu_to_be64(key); |
| 491 | uint8_t cmd[10] = {}; |
| 492 | size_t buf_len; |
| 493 | g_autofree uint8_t *buf = NULL; |
| 494 | uint32_t additional_length = 16 * 8; /* initial key list size */ |
| 495 | |
| 496 | /* |
| 497 | * Loop to resize parameter data buffer when there are many keys. It would |
| 498 | * be simpler to hardcode the maximum buffer size (it's only 64 KB), but |
| 499 | * SG_IO can fail with EINVAL if the host kernel blkdev queue limits are |
| 500 | * too low. |
| 501 | */ |
| 502 | do { |
| 503 | uint16_t allocation_length_be; |
| 504 | int ret; |
| 505 | |
| 506 | buf_len = key_list_offset + additional_length; |
| 507 | buf = g_realloc(buf, buf_len); |
| 508 | memset(buf, 0, buf_len); |
| 509 | |
| 510 | cmd[0] = PERSISTENT_RESERVE_IN; |
| 511 | cmd[1] = PRI_READ_KEYS; |
| 512 | allocation_length_be = cpu_to_be16(buf_len); |
| 513 | memcpy(&cmd[7], &allocation_length_be, sizeof(allocation_length_be)); |
| 514 | |
| 515 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
| 516 | buf, buf_len, s->io_timeout, errp); |
| 517 | if (ret < 0) { |
| 518 | error_prepend(errp, "PERSISTENT RESERVE IN with READ KEYS: "); |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | memcpy(&additional_length, &buf[4], sizeof(additional_length)); |
| 523 | be32_to_cpus(&additional_length); |
| 524 | |
| 525 | /* |
| 526 | * The parameter data's ADDITIONAL LENGTH must not overflow the CDB's |
| 527 | * 16-bit ALLOCATION LENGTH field since the next loop iteration will |
| 528 | * compute ALLOCATION LENGTH based on ADDITIONAL LENGTH. |
| 529 | */ |
| 530 | if (additional_length > UINT16_MAX - key_list_offset) { |
| 531 | error_setg(errp, "got invalid ADDITIONAL LENGTH %" PRIu32 |
| 532 | " from READ KEYS", additional_length); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | for (size_t i = key_list_offset; i < buf_len; i += sizeof(key_be)) { |
| 537 | if (i - key_list_offset >= additional_length) { |
| 538 | break; /* end of parameter list */ |
| 539 | } |
| 540 | |
| 541 | if (memcmp(&key_be, &buf[i], sizeof(key_be)) == 0) { |
| 542 | return true; /* key found */ |
| 543 | } |
| 544 | } |
| 545 | } while (additional_length > buf_len - key_list_offset); |
| 546 | |
| 547 | return false; /* key not found */ |
| 548 | } |
| 549 | |
| 550 | /* Register keys and preempt reservations after live migration */ |
| 551 | bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp) |
| 552 | { |
| 553 | SCSIPRState *pr_state = &s->pr_state; |
| 554 | Error *local_err = NULL; |
| 555 | bool check_stale_key = true; |
| 556 | uint64_t key; |
| 557 | uint8_t resv_type; |
| 558 | |
| 559 | /* Get the migrated PR state */ |
| 560 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 561 | key = pr_state->key; |
| 562 | resv_type = pr_state->resv_type; |
| 563 | } |
| 564 | |
| 565 | trace_scsi_generic_pr_state_preempt(key, resv_type); |
| 566 | |
| 567 | /* Handle stale PR state (e.g. another node preempted) */ |
| 568 | if (resv_type) { |
| 569 | uint64_t dev_key; |
| 570 | uint8_t dev_resv_type; |
| 571 | |
| 572 | if (scsi_generic_read_reservation(s, &dev_key, &dev_resv_type, |
| 573 | errp) < 0) { |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | if (dev_resv_type != resv_type) { |
| 578 | /* vmstate had a stale reservation type */ |
| 579 | g_autofree char *name = qdev_get_human_name(&s->qdev); |
| 580 | warn_report("Expected SCSI reservation type 0x%x on device '%s', " |
| 581 | "got 0x%x, using new type", |
| 582 | resv_type, name, dev_resv_type); |
| 583 | resv_type = dev_resv_type; |
| 584 | } |
| 585 | |
| 586 | if (dev_key == key) { |
| 587 | /* The reservation exists, no need to check for a stale key */ |
| 588 | check_stale_key = false; |
| 589 | } else { |
| 590 | g_autofree char *name = qdev_get_human_name(&s->qdev); |
| 591 | warn_report("Expected SCSI reservation with key 0x%" PRIx64 |
| 592 | " on device '%s', got 0x%" PRIx64 ", ignoring " |
| 593 | "reservation", |
| 594 | key, name, dev_key); |
| 595 | resv_type = 0; /* vmstate had a stale reservation */ |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (key != 0 && check_stale_key && |
| 600 | !scsi_generic_pr_key_registered(s, key, &local_err)) { |
| 601 | if (local_err) { |
| 602 | error_propagate(errp, local_err); |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | g_autofree char *name = qdev_get_human_name(&s->qdev); |
| 607 | warn_report("SCSI reservation key 0x%" PRIx64 " on device '%s' not " |
| 608 | "registered after migration, ignoring", |
| 609 | key, name); |
| 610 | key = 0; /* vmstate had a stale key */ |
| 611 | } |
| 612 | |
| 613 | /* Stale PR state may have been updated */ |
| 614 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
| 615 | pr_state->key = key; |
| 616 | pr_state->resv_type = resv_type; |
| 617 | } |
| 618 | |
| 619 | if (key == 0) { |
| 620 | return true; /* no PR state, do nothing */ |
| 621 | } |
| 622 | |
| 623 | if (!scsi_generic_pr_register(s, key, errp)) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Two cases: |
| 629 | * |
| 630 | * 1. There is no reservation (resv_type is 0) and the other I_T nexus |
| 631 | * will be unregistered. This is important so the source host does |
| 632 | * not leak registered keys across live migration. |
| 633 | * |
| 634 | * 2. There is a reservation (resv_type is not 0) and the other I_T |
| 635 | * nexus will be unregistered and its reservation is atomically |
| 636 | * taken over by us. This is the scenario where a reservation is |
| 637 | * migrated along with the guest. |
| 638 | */ |
| 639 | if (!scsi_generic_pr_preempt(s, key, resv_type, errp)) { |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Some SCSI targets, like the Linux LIO target, remove our |
| 645 | * registration when preempting without a reservation (resv_type is 0). |
| 646 | * Try to register again but ignore the error since a RESERVATION |
| 647 | * CONFLICT is expected if our registration remained in place. |
| 648 | */ |
| 649 | if (resv_type == 0) { |
| 650 | scsi_generic_pr_register(s, key, NULL); |
| 651 | } |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | static void scsi_read_complete(void * opaque, int ret) |
| 656 | { |
| 657 | SCSIGenericReq *r = (SCSIGenericReq *)opaque; |
| 658 | SCSIDevice *s = r->req.dev; |
| 659 | int len; |
| 660 | |
| 661 | assert(r->req.aiocb != NULL); |
| 662 | r->req.aiocb = NULL; |
| 663 | |
| 664 | if (ret || r->req.io_canceled) { |
| 665 | scsi_command_complete_noio(r, ret); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | len = r->io_header.dxfer_len - r->io_header.resid; |
| 670 | trace_scsi_generic_read_complete(r->req.tag, len); |
| 671 | |
| 672 | r->len = -1; |
| 673 | |
| 674 | if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) { |
| 675 | SCSISense sense = |
| 676 | scsi_parse_sense_buf(r->req.sense, r->io_header.sb_len_wr); |
| 677 | |
| 678 | /* |
| 679 | * Check if this is a VPD Block Limits request that |
| 680 | * resulted in sense error but would need emulation. |
| 681 | * In this case, emulate a valid VPD response. |
| 682 | */ |
| 683 | if (sense.key == ILLEGAL_REQUEST && |
| 684 | s->needs_vpd_bl_emulation && |
| 685 | r->req.cmd.buf[0] == INQUIRY && |
| 686 | (r->req.cmd.buf[1] & 0x01) && |
| 687 | r->req.cmd.buf[2] == 0xb0) { |
| 688 | len = scsi_generic_emulate_block_limits(r, s); |
| 689 | /* |
| 690 | * It's okay to jup to req_complete: no need to |
| 691 | * let scsi_handle_inquiry_reply handle an |
| 692 | * INQUIRY VPD BL request we created manually. |
| 693 | */ |
| 694 | } |
| 695 | if (sense.key) { |
| 696 | goto req_complete; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (r->io_header.host_status != SCSI_HOST_OK || |
| 701 | (r->io_header.driver_status & SG_ERR_DRIVER_TIMEOUT) || |
| 702 | r->io_header.status != GOOD || |
| 703 | len == 0) { |
| 704 | scsi_command_complete_noio(r, 0); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | /* Snoop READ CAPACITY output to set the blocksize. */ |
| 709 | if (r->req.cmd.buf[0] == READ_CAPACITY_10 && |
| 710 | (ldl_be_p(&r->buf[0]) != 0xffffffffU || s->max_lba == 0)) { |
| 711 | s->blocksize = ldl_be_p(&r->buf[4]); |
| 712 | s->max_lba = ldl_be_p(&r->buf[0]) & 0xffffffffULL; |
| 713 | } else if (r->req.cmd.buf[0] == SERVICE_ACTION_IN_16 && |
| 714 | (r->req.cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) { |
| 715 | s->blocksize = ldl_be_p(&r->buf[8]); |
| 716 | s->max_lba = ldq_be_p(&r->buf[0]); |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Patch MODE SENSE device specific parameters if the BDS is opened |
| 721 | * readonly. |
| 722 | */ |
| 723 | if ((s->type == TYPE_DISK || s->type == TYPE_TAPE || s->type == TYPE_ZBC) && |
| 724 | !blk_is_writable(s->conf.blk) && |
| 725 | (r->req.cmd.buf[0] == MODE_SENSE || |
| 726 | r->req.cmd.buf[0] == MODE_SENSE_10) && |
| 727 | (r->req.cmd.buf[1] & 0x8) == 0) { |
| 728 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
| 729 | r->buf[2] |= 0x80; |
| 730 | } else { |
| 731 | r->buf[3] |= 0x80; |
| 732 | } |
| 733 | } |
| 734 | if (r->req.cmd.buf[0] == INQUIRY) { |
| 735 | len = scsi_handle_inquiry_reply(r, s, len); |
| 736 | } |
| 737 | if (r->req.cmd.buf[0] == PERSISTENT_RESERVE_IN) { |
| 738 | scsi_handle_persistent_reserve_in_reply(r, s); |
| 739 | } |
| 740 | |
| 741 | req_complete: |
| 742 | scsi_req_data(&r->req, len); |
| 743 | scsi_req_unref(&r->req); |
| 744 | } |
| 745 | |
| 746 | /* Read more data from scsi device into buffer. */ |
| 747 | static void scsi_read_data(SCSIRequest *req) |
| 748 | { |
| 749 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 750 | SCSIDevice *s = r->req.dev; |
| 751 | int ret; |
| 752 | |
| 753 | trace_scsi_generic_read_data(req->tag); |
| 754 | |
| 755 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 756 | scsi_req_ref(&r->req); |
| 757 | if (r->len == -1) { |
| 758 | scsi_command_complete_noio(r, 0); |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | ret = execute_command(s->conf.blk, r, SG_DXFER_FROM_DEV, |
| 763 | scsi_read_complete); |
| 764 | if (ret < 0) { |
| 765 | scsi_command_complete_noio(r, ret); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | static void scsi_write_complete(void * opaque, int ret) |
| 770 | { |
| 771 | SCSIGenericReq *r = (SCSIGenericReq *)opaque; |
| 772 | SCSIDevice *s = r->req.dev; |
| 773 | |
| 774 | trace_scsi_generic_write_complete(ret); |
| 775 | |
| 776 | assert(r->req.aiocb != NULL); |
| 777 | r->req.aiocb = NULL; |
| 778 | |
| 779 | if (ret || r->req.io_canceled) { |
| 780 | scsi_command_complete_noio(r, ret); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | if (r->req.cmd.buf[0] == MODE_SELECT && r->req.cmd.buf[4] == 12 && |
| 785 | s->type == TYPE_TAPE) { |
| 786 | s->blocksize = (r->buf[9] << 16) | (r->buf[10] << 8) | r->buf[11]; |
| 787 | trace_scsi_generic_write_complete_blocksize(s->blocksize); |
| 788 | } |
| 789 | if (r->req.cmd.buf[0] == PERSISTENT_RESERVE_OUT) { |
| 790 | scsi_handle_persistent_reserve_out_reply(r, s); |
| 791 | } |
| 792 | |
| 793 | scsi_command_complete_noio(r, ret); |
| 794 | } |
| 795 | |
| 796 | /* Write data to a scsi device. Returns nonzero on failure. |
| 797 | The transfer may complete asynchronously. */ |
| 798 | static void scsi_write_data(SCSIRequest *req) |
| 799 | { |
| 800 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 801 | SCSIDevice *s = r->req.dev; |
| 802 | int ret; |
| 803 | |
| 804 | trace_scsi_generic_write_data(req->tag); |
| 805 | if (r->len == 0) { |
| 806 | r->len = r->buflen; |
| 807 | scsi_req_data(&r->req, r->len); |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 812 | scsi_req_ref(&r->req); |
| 813 | ret = execute_command(s->conf.blk, r, SG_DXFER_TO_DEV, scsi_write_complete); |
| 814 | if (ret < 0) { |
| 815 | scsi_command_complete_noio(r, ret); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /* Return a pointer to the data buffer. */ |
| 820 | static uint8_t *scsi_get_buf(SCSIRequest *req) |
| 821 | { |
| 822 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 823 | |
| 824 | return r->buf; |
| 825 | } |
| 826 | |
| 827 | static void scsi_generic_command_dump(uint8_t *cmd, int len) |
| 828 | { |
| 829 | int i; |
| 830 | char *line_buffer, *p; |
| 831 | |
| 832 | line_buffer = g_malloc(len * 5 + 1); |
| 833 | |
| 834 | for (i = 0, p = line_buffer; i < len; i++) { |
| 835 | p += sprintf(p, " 0x%02x", cmd[i]); |
| 836 | } |
| 837 | trace_scsi_generic_send_command(line_buffer); |
| 838 | |
| 839 | g_free(line_buffer); |
| 840 | } |
| 841 | |
| 842 | /* Execute a scsi command. Returns the length of the data expected by the |
| 843 | command. This will be Positive for data transfers from the device |
| 844 | (eg. disk reads), negative for transfers to the device (eg. disk writes), |
| 845 | and zero if the command does not transfer any data. */ |
| 846 | |
| 847 | static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd) |
| 848 | { |
| 849 | SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); |
| 850 | SCSIDevice *s = r->req.dev; |
| 851 | int ret; |
| 852 | |
| 853 | if (trace_event_get_state_backends(TRACE_SCSI_GENERIC_SEND_COMMAND)) { |
| 854 | scsi_generic_command_dump(cmd, r->req.cmd.len); |
| 855 | } |
| 856 | |
| 857 | if (r->req.cmd.xfer == 0) { |
| 858 | g_free(r->buf); |
| 859 | r->buflen = 0; |
| 860 | r->buf = NULL; |
| 861 | /* The request is used as the AIO opaque value, so add a ref. */ |
| 862 | scsi_req_ref(&r->req); |
| 863 | ret = execute_command(s->conf.blk, r, SG_DXFER_NONE, |
| 864 | scsi_command_complete); |
| 865 | if (ret < 0) { |
| 866 | scsi_command_complete_noio(r, ret); |
| 867 | return 0; |
| 868 | } |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | if (r->buflen != r->req.cmd.xfer) { |
| 873 | g_free(r->buf); |
| 874 | r->buf = g_malloc(r->req.cmd.xfer); |
| 875 | r->buflen = r->req.cmd.xfer; |
| 876 | } |
| 877 | |
| 878 | memset(r->buf, 0, r->buflen); |
| 879 | r->len = r->req.cmd.xfer; |
| 880 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
| 881 | r->len = 0; |
| 882 | return -r->req.cmd.xfer; |
| 883 | } else { |
| 884 | return r->req.cmd.xfer; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | static int read_naa_id(const uint8_t *p, uint64_t *p_wwn) |
| 889 | { |
| 890 | int i; |
| 891 | |
| 892 | if ((p[1] & 0xF) == 3) { |
| 893 | /* NAA designator type */ |
| 894 | if (p[3] != 8) { |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | *p_wwn = ldq_be_p(p + 4); |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | if ((p[1] & 0xF) == 8) { |
| 902 | /* SCSI name string designator type */ |
| 903 | if (p[3] < 20 || memcmp(&p[4], "naa.", 4)) { |
| 904 | return -EINVAL; |
| 905 | } |
| 906 | if (p[3] > 20 && p[24] != ',') { |
| 907 | return -EINVAL; |
| 908 | } |
| 909 | *p_wwn = 0; |
| 910 | for (i = 8; i < 24; i++) { |
| 911 | char c = qemu_toupper(p[i]); |
| 912 | c -= (c >= '0' && c <= '9' ? '0' : 'A' - 10); |
| 913 | *p_wwn = (*p_wwn << 4) | c; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | |
| 921 | int scsi_SG_IO(BlockBackend *blk, int direction, uint8_t *cmd, |
| 922 | uint8_t cmd_size, uint8_t *buf, unsigned int buf_size, |
| 923 | uint32_t timeout, Error **errp) |
| 924 | { |
| 925 | sg_io_hdr_t io_header; |
| 926 | uint8_t sensebuf[8] = {}; |
| 927 | int ret; |
| 928 | |
| 929 | memset(&io_header, 0, sizeof(io_header)); |
| 930 | io_header.interface_id = 'S'; |
| 931 | io_header.dxfer_direction = direction; |
| 932 | io_header.dxfer_len = buf_size; |
| 933 | io_header.dxferp = buf; |
| 934 | io_header.cmdp = cmd; |
| 935 | io_header.cmd_len = cmd_size; |
| 936 | io_header.mx_sb_len = sizeof(sensebuf); |
| 937 | io_header.sbp = sensebuf; |
| 938 | io_header.timeout = timeout * 1000; |
| 939 | |
| 940 | trace_scsi_generic_ioctl_sgio_command(cmd[0], io_header.timeout); |
| 941 | ret = blk_ioctl(blk, SG_IO, &io_header); |
| 942 | if (ret < 0 || io_header.status || |
| 943 | io_header.driver_status || io_header.host_status) { |
| 944 | trace_scsi_generic_ioctl_sgio_done(cmd[0], ret, io_header.status, |
| 945 | io_header.host_status); |
| 946 | if (ret < 0) { |
| 947 | error_setg_errno(errp, -ret, "SG_IO ioctl failed"); |
| 948 | } else { |
| 949 | g_autofree char *sensebuf_hex = |
| 950 | g_strdup_printf("%02x%02x%02x%02x%02x%02x%02x%02x", |
| 951 | sensebuf[0], |
| 952 | sensebuf[1], |
| 953 | sensebuf[2], |
| 954 | sensebuf[3], |
| 955 | sensebuf[4], |
| 956 | sensebuf[5], |
| 957 | sensebuf[6], |
| 958 | sensebuf[7]); |
| 959 | |
| 960 | error_setg(errp, "SG_IO SCSI command failed with status=0x%x " |
| 961 | "driver_status=0x%x host_status=0x%x sensebuf=%s " |
| 962 | "sb_len_wr=%u", |
| 963 | io_header.status, |
| 964 | io_header.driver_status, |
| 965 | io_header.host_status, |
| 966 | sensebuf_hex, |
| 967 | io_header.sb_len_wr); |
| 968 | } |
| 969 | return -1; |
| 970 | } |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Executes an INQUIRY request with EVPD set to retrieve the |
| 976 | * available VPD pages of the device. If the device does |
| 977 | * not support the Block Limits page (page 0xb0), set |
| 978 | * the needs_vpd_bl_emulation flag for future use. |
| 979 | */ |
| 980 | static void scsi_generic_set_vpd_bl_emulation(SCSIDevice *s) |
| 981 | { |
| 982 | uint8_t cmd[6]; |
| 983 | uint8_t buf[250]; |
| 984 | uint8_t page_len; |
| 985 | int ret, i; |
| 986 | |
| 987 | memset(cmd, 0, sizeof(cmd)); |
| 988 | memset(buf, 0, sizeof(buf)); |
| 989 | cmd[0] = INQUIRY; |
| 990 | cmd[1] = 1; |
| 991 | cmd[2] = 0x00; |
| 992 | cmd[4] = sizeof(buf); |
| 993 | |
| 994 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
| 995 | buf, sizeof(buf), s->io_timeout, NULL); |
| 996 | if (ret < 0) { |
| 997 | /* |
| 998 | * Do not assume anything if we can't retrieve the |
| 999 | * INQUIRY response to assert the VPD Block Limits |
| 1000 | * support. |
| 1001 | */ |
| 1002 | s->needs_vpd_bl_emulation = false; |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | page_len = buf[3]; |
| 1007 | for (i = 4; i < MIN(sizeof(buf), page_len + 4); i++) { |
| 1008 | if (buf[i] == 0xb0) { |
| 1009 | s->needs_vpd_bl_emulation = false; |
| 1010 | return; |
| 1011 | } |
| 1012 | } |
| 1013 | s->needs_vpd_bl_emulation = true; |
| 1014 | } |
| 1015 | |
| 1016 | static void scsi_generic_read_device_identification(SCSIDevice *s) |
| 1017 | { |
| 1018 | uint8_t cmd[6]; |
| 1019 | uint8_t buf[250]; |
| 1020 | int ret; |
| 1021 | int i, len; |
| 1022 | |
| 1023 | memset(cmd, 0, sizeof(cmd)); |
| 1024 | memset(buf, 0, sizeof(buf)); |
| 1025 | cmd[0] = INQUIRY; |
| 1026 | cmd[1] = 1; |
| 1027 | cmd[2] = 0x83; |
| 1028 | cmd[4] = sizeof(buf); |
| 1029 | |
| 1030 | ret = scsi_SG_IO(s->conf.blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
| 1031 | buf, sizeof(buf), s->io_timeout, NULL); |
| 1032 | if (ret < 0) { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | len = MIN((buf[2] << 8) | buf[3], sizeof(buf) - 4); |
| 1037 | for (i = 0; i + 3 <= len; ) { |
| 1038 | const uint8_t *p = &buf[i + 4]; |
| 1039 | uint64_t wwn; |
| 1040 | |
| 1041 | if (i + (p[3] + 4) > len) { |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | if ((p[1] & 0x10) == 0) { |
| 1046 | /* Associated with the logical unit */ |
| 1047 | if (read_naa_id(p, &wwn) == 0) { |
| 1048 | s->wwn = wwn; |
| 1049 | } |
| 1050 | } else if ((p[1] & 0x10) == 0x10) { |
| 1051 | /* Associated with the target port */ |
| 1052 | if (read_naa_id(p, &wwn) == 0) { |
| 1053 | s->port_wwn = wwn; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | i += p[3] + 4; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | void scsi_generic_read_device_inquiry(SCSIDevice *s) |
| 1062 | { |
| 1063 | scsi_generic_read_device_identification(s); |
| 1064 | if (s->type == TYPE_DISK || s->type == TYPE_ZBC) { |
| 1065 | scsi_generic_set_vpd_bl_emulation(s); |
| 1066 | } else { |
| 1067 | s->needs_vpd_bl_emulation = false; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | static int get_stream_blocksize(BlockBackend *blk) |
| 1072 | { |
| 1073 | uint8_t cmd[6]; |
| 1074 | uint8_t buf[12]; |
| 1075 | int ret; |
| 1076 | |
| 1077 | memset(cmd, 0, sizeof(cmd)); |
| 1078 | memset(buf, 0, sizeof(buf)); |
| 1079 | cmd[0] = MODE_SENSE; |
| 1080 | cmd[4] = sizeof(buf); |
| 1081 | |
| 1082 | ret = scsi_SG_IO(blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
| 1083 | buf, sizeof(buf), 6, NULL); |
| 1084 | if (ret < 0) { |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | return (buf[9] << 16) | (buf[10] << 8) | buf[11]; |
| 1089 | } |
| 1090 | |
| 1091 | static void scsi_generic_reset(DeviceState *dev) |
| 1092 | { |
| 1093 | SCSIDevice *s = SCSI_DEVICE(dev); |
| 1094 | |
| 1095 | s->scsi_version = s->default_scsi_version; |
| 1096 | scsi_device_purge_requests(s, SENSE_CODE(RESET)); |
| 1097 | } |
| 1098 | |
| 1099 | static void scsi_generic_realize(SCSIDevice *s, Error **errp) |
| 1100 | { |
| 1101 | int rc; |
| 1102 | int sg_version; |
| 1103 | struct sg_scsi_id scsiid; |
| 1104 | |
| 1105 | if (!s->conf.blk) { |
| 1106 | error_setg(errp, "drive property not set"); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | if (blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC && |
| 1111 | blk_get_on_error(s->conf.blk, 0) != BLOCKDEV_ON_ERROR_REPORT) { |
| 1112 | error_setg(errp, "Device doesn't support drive option werror"); |
| 1113 | return; |
| 1114 | } |
| 1115 | if (blk_get_on_error(s->conf.blk, 1) != BLOCKDEV_ON_ERROR_REPORT) { |
| 1116 | error_setg(errp, "Device doesn't support drive option rerror"); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | /* check we are using a driver managing SG_IO (version 3 and after */ |
| 1121 | rc = blk_ioctl(s->conf.blk, SG_GET_VERSION_NUM, &sg_version); |
| 1122 | if (rc < 0) { |
| 1123 | error_setg_errno(errp, -rc, "cannot get SG_IO version number"); |
| 1124 | if (rc != -EPERM) { |
| 1125 | error_append_hint(errp, "Is this a SCSI device?\n"); |
| 1126 | } |
| 1127 | return; |
| 1128 | } |
| 1129 | if (sg_version < 30000) { |
| 1130 | error_setg(errp, "scsi generic interface too old"); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* get LUN of the /dev/sg? */ |
| 1135 | if (blk_ioctl(s->conf.blk, SG_GET_SCSI_ID, &scsiid)) { |
| 1136 | error_setg(errp, "SG_GET_SCSI_ID ioctl failed"); |
| 1137 | return; |
| 1138 | } |
| 1139 | if (!blkconf_apply_backend_options(&s->conf, |
| 1140 | !blk_supports_write_perm(s->conf.blk), |
| 1141 | true, errp)) { |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | /* define device state */ |
| 1146 | s->type = scsiid.scsi_type; |
| 1147 | trace_scsi_generic_realize_type(s->type); |
| 1148 | |
| 1149 | switch (s->type) { |
| 1150 | case TYPE_TAPE: |
| 1151 | s->blocksize = get_stream_blocksize(s->conf.blk); |
| 1152 | if (s->blocksize == -1) { |
| 1153 | s->blocksize = 0; |
| 1154 | } |
| 1155 | break; |
| 1156 | |
| 1157 | /* Make a guess for block devices, we'll fix it when the guest sends. |
| 1158 | * READ CAPACITY. If they don't, they likely would assume these sizes |
| 1159 | * anyway. (TODO: they could also send MODE SENSE). |
| 1160 | */ |
| 1161 | case TYPE_ROM: |
| 1162 | case TYPE_WORM: |
| 1163 | s->blocksize = 2048; |
| 1164 | break; |
| 1165 | default: |
| 1166 | s->blocksize = 512; |
| 1167 | break; |
| 1168 | } |
| 1169 | |
| 1170 | trace_scsi_generic_realize_blocksize(s->blocksize); |
| 1171 | |
| 1172 | /* Only used by scsi-block, but initialize it nevertheless to be clean. */ |
| 1173 | s->default_scsi_version = -1; |
| 1174 | scsi_generic_read_device_inquiry(s); |
| 1175 | } |
| 1176 | |
| 1177 | const SCSIReqOps scsi_generic_req_ops = { |
| 1178 | .size = sizeof(SCSIGenericReq), |
| 1179 | .free_req = scsi_free_request, |
| 1180 | .send_command = scsi_send_command, |
| 1181 | .read_data = scsi_read_data, |
| 1182 | .write_data = scsi_write_data, |
| 1183 | .get_buf = scsi_get_buf, |
| 1184 | .load_request = scsi_generic_load_request, |
| 1185 | .save_request = scsi_generic_save_request, |
| 1186 | }; |
| 1187 | |
| 1188 | static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, |
| 1189 | uint8_t *buf, void *hba_private) |
| 1190 | { |
| 1191 | return scsi_req_alloc(&scsi_generic_req_ops, d, tag, lun, hba_private); |
| 1192 | } |
| 1193 | |
| 1194 | static const Property scsi_generic_properties[] = { |
| 1195 | DEFINE_PROP_DRIVE("drive", SCSIDevice, conf.blk), |
| 1196 | DEFINE_PROP_BOOL("share-rw", SCSIDevice, conf.share_rw, false), |
| 1197 | DEFINE_PROP_UINT32("io_timeout", SCSIDevice, io_timeout, |
| 1198 | DEFAULT_IO_TIMEOUT), |
| 1199 | }; |
| 1200 | |
| 1201 | static int scsi_generic_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, |
| 1202 | uint8_t *buf, size_t buf_len, |
| 1203 | void *hba_private) |
| 1204 | { |
| 1205 | return scsi_bus_parse_cdb(dev, cmd, buf, buf_len, hba_private); |
| 1206 | } |
| 1207 | |
| 1208 | static void scsi_generic_class_initfn(ObjectClass *klass, const void *data) |
| 1209 | { |
| 1210 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 1211 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
| 1212 | |
| 1213 | sc->realize = scsi_generic_realize; |
| 1214 | sc->alloc_req = scsi_new_request; |
| 1215 | sc->parse_cdb = scsi_generic_parse_cdb; |
| 1216 | dc->fw_name = "disk"; |
| 1217 | dc->desc = "pass through generic scsi device (/dev/sg*)"; |
| 1218 | device_class_set_legacy_reset(dc, scsi_generic_reset); |
| 1219 | device_class_set_props(dc, scsi_generic_properties); |
| 1220 | dc->vmsd = &vmstate_scsi_device; |
| 1221 | } |
| 1222 | |
| 1223 | static const TypeInfo scsi_generic_info = { |
| 1224 | .name = "scsi-generic", |
| 1225 | .parent = TYPE_SCSI_DEVICE, |
| 1226 | .instance_size = sizeof(SCSIDevice), |
| 1227 | .class_init = scsi_generic_class_initfn, |
| 1228 | }; |
| 1229 | |
| 1230 | static void scsi_generic_register_types(void) |
| 1231 | { |
| 1232 | type_register_static(&scsi_generic_info); |
| 1233 | } |
| 1234 | |
| 1235 | type_init(scsi_generic_register_types) |
| 1236 | |
| 1237 | #endif /* __linux__ */ |