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-common.h" 15 #include "qemu/error-report.h" 16 #include "hw/scsi/scsi.h" 17 #include "sysemu/blockdev.h" 18 19 #ifdef __linux__ 20 21 //#define DEBUG_SCSI 22 23 #ifdef DEBUG_SCSI 24 #define DPRINTF(fmt, ...) \ 25 do { printf("scsi-generic: " fmt , ## __VA_ARGS__); } while (0) 26 #else 27 #define DPRINTF(fmt, ...) do {} while(0) 28 #endif 29 30 #define BADF(fmt, ...) \ 31 do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0) 32 33 #include <stdio.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <unistd.h> 37 #include <scsi/sg.h> 38 #include "block/scsi.h" 39 40 #define SG_ERR_DRIVER_TIMEOUT 0x06 41 #define SG_ERR_DRIVER_SENSE 0x08 42 43 #define SG_ERR_DID_OK 0x00 44 #define SG_ERR_DID_NO_CONNECT 0x01 45 #define SG_ERR_DID_BUS_BUSY 0x02 46 #define SG_ERR_DID_TIME_OUT 0x03 47 48 #ifndef MAX_UINT 49 #define MAX_UINT ((unsigned int)-1) 50 #endif 51 52 typedef struct SCSIGenericReq { 53 SCSIRequest req; 54 uint8_t *buf; 55 int buflen; 56 int len; 57 sg_io_hdr_t io_header; 58 } SCSIGenericReq; 59 60 static void scsi_generic_save_request(QEMUFile *f, SCSIRequest *req) 61 { 62 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 63 64 qemu_put_sbe32s(f, &r->buflen); 65 if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) { 66 assert(!r->req.sg); 67 qemu_put_buffer(f, r->buf, r->req.cmd.xfer); 68 } 69 } 70 71 static void scsi_generic_load_request(QEMUFile *f, SCSIRequest *req) 72 { 73 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 74 75 qemu_get_sbe32s(f, &r->buflen); 76 if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) { 77 assert(!r->req.sg); 78 qemu_get_buffer(f, r->buf, r->req.cmd.xfer); 79 } 80 } 81 82 static void scsi_free_request(SCSIRequest *req) 83 { 84 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 85 86 g_free(r->buf); 87 } 88 89 /* Helper function for command completion. */ 90 static void scsi_command_complete(void *opaque, int ret) 91 { 92 int status; 93 SCSIGenericReq *r = (SCSIGenericReq *)opaque; 94 95 r->req.aiocb = NULL; 96 if (r->req.io_canceled) { 97 scsi_req_cancel_complete(&r->req); 98 goto done; 99 } 100 if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) { 101 r->req.sense_len = r->io_header.sb_len_wr; 102 } 103 104 if (ret != 0) { 105 switch (ret) { 106 case -EDOM: 107 status = TASK_SET_FULL; 108 break; 109 case -ENOMEM: 110 status = CHECK_CONDITION; 111 scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE)); 112 break; 113 default: 114 status = CHECK_CONDITION; 115 scsi_req_build_sense(&r->req, SENSE_CODE(IO_ERROR)); 116 break; 117 } 118 } else { 119 if (r->io_header.host_status == SG_ERR_DID_NO_CONNECT || 120 r->io_header.host_status == SG_ERR_DID_BUS_BUSY || 121 r->io_header.host_status == SG_ERR_DID_TIME_OUT || 122 (r->io_header.driver_status & SG_ERR_DRIVER_TIMEOUT)) { 123 status = BUSY; 124 BADF("Driver Timeout\n"); 125 } else if (r->io_header.host_status) { 126 status = CHECK_CONDITION; 127 scsi_req_build_sense(&r->req, SENSE_CODE(I_T_NEXUS_LOSS)); 128 } else if (r->io_header.status) { 129 status = r->io_header.status; 130 } else if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) { 131 status = CHECK_CONDITION; 132 } else { 133 status = GOOD; 134 } 135 } 136 DPRINTF("Command complete 0x%p tag=0x%x status=%d\n", 137 r, r->req.tag, status); 138 139 scsi_req_complete(&r->req, status); 140 done: 141 scsi_req_unref(&r->req); 142 } 143 144 static int execute_command(BlockDriverState *bdrv, 145 SCSIGenericReq *r, int direction, 146 BlockDriverCompletionFunc *complete) 147 { 148 r->io_header.interface_id = 'S'; 149 r->io_header.dxfer_direction = direction; 150 r->io_header.dxferp = r->buf; 151 r->io_header.dxfer_len = r->buflen; 152 r->io_header.cmdp = r->req.cmd.buf; 153 r->io_header.cmd_len = r->req.cmd.len; 154 r->io_header.mx_sb_len = sizeof(r->req.sense); 155 r->io_header.sbp = r->req.sense; 156 r->io_header.timeout = MAX_UINT; 157 r->io_header.usr_ptr = r; 158 r->io_header.flags |= SG_FLAG_DIRECT_IO; 159 160 r->req.aiocb = bdrv_aio_ioctl(bdrv, SG_IO, &r->io_header, complete, r); 161 if (r->req.aiocb == NULL) { 162 return -EIO; 163 } 164 165 return 0; 166 } 167 168 static void scsi_read_complete(void * opaque, int ret) 169 { 170 SCSIGenericReq *r = (SCSIGenericReq *)opaque; 171 SCSIDevice *s = r->req.dev; 172 int len; 173 174 r->req.aiocb = NULL; 175 if (ret || r->req.io_canceled) { 176 scsi_command_complete(r, ret); 177 return; 178 } 179 len = r->io_header.dxfer_len - r->io_header.resid; 180 DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, len); 181 182 r->len = -1; 183 if (len == 0) { 184 scsi_command_complete(r, 0); 185 } else { 186 /* Snoop READ CAPACITY output to set the blocksize. */ 187 if (r->req.cmd.buf[0] == READ_CAPACITY_10 && 188 (ldl_be_p(&r->buf[0]) != 0xffffffffU || s->max_lba == 0)) { 189 s->blocksize = ldl_be_p(&r->buf[4]); 190 s->max_lba = ldl_be_p(&r->buf[0]) & 0xffffffffULL; 191 } else if (r->req.cmd.buf[0] == SERVICE_ACTION_IN_16 && 192 (r->req.cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) { 193 s->blocksize = ldl_be_p(&r->buf[8]); 194 s->max_lba = ldq_be_p(&r->buf[0]); 195 } 196 bdrv_set_guest_block_size(s->conf.bs, s->blocksize); 197 198 scsi_req_data(&r->req, len); 199 scsi_req_unref(&r->req); 200 } 201 } 202 203 /* Read more data from scsi device into buffer. */ 204 static void scsi_read_data(SCSIRequest *req) 205 { 206 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 207 SCSIDevice *s = r->req.dev; 208 int ret; 209 210 DPRINTF("scsi_read_data 0x%x\n", req->tag); 211 212 /* The request is used as the AIO opaque value, so add a ref. */ 213 scsi_req_ref(&r->req); 214 if (r->len == -1) { 215 scsi_command_complete(r, 0); 216 return; 217 } 218 219 ret = execute_command(s->conf.bs, r, SG_DXFER_FROM_DEV, scsi_read_complete); 220 if (ret < 0) { 221 scsi_command_complete(r, ret); 222 } 223 } 224 225 static void scsi_write_complete(void * opaque, int ret) 226 { 227 SCSIGenericReq *r = (SCSIGenericReq *)opaque; 228 SCSIDevice *s = r->req.dev; 229 230 DPRINTF("scsi_write_complete() ret = %d\n", ret); 231 r->req.aiocb = NULL; 232 if (ret || r->req.io_canceled) { 233 scsi_command_complete(r, ret); 234 return; 235 } 236 237 if (r->req.cmd.buf[0] == MODE_SELECT && r->req.cmd.buf[4] == 12 && 238 s->type == TYPE_TAPE) { 239 s->blocksize = (r->buf[9] << 16) | (r->buf[10] << 8) | r->buf[11]; 240 DPRINTF("block size %d\n", s->blocksize); 241 } 242 243 scsi_command_complete(r, ret); 244 } 245 246 /* Write data to a scsi device. Returns nonzero on failure. 247 The transfer may complete asynchronously. */ 248 static void scsi_write_data(SCSIRequest *req) 249 { 250 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 251 SCSIDevice *s = r->req.dev; 252 int ret; 253 254 DPRINTF("scsi_write_data 0x%x\n", req->tag); 255 if (r->len == 0) { 256 r->len = r->buflen; 257 scsi_req_data(&r->req, r->len); 258 return; 259 } 260 261 /* The request is used as the AIO opaque value, so add a ref. */ 262 scsi_req_ref(&r->req); 263 ret = execute_command(s->conf.bs, r, SG_DXFER_TO_DEV, scsi_write_complete); 264 if (ret < 0) { 265 scsi_command_complete(r, ret); 266 } 267 } 268 269 /* Return a pointer to the data buffer. */ 270 static uint8_t *scsi_get_buf(SCSIRequest *req) 271 { 272 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 273 274 return r->buf; 275 } 276 277 /* Execute a scsi command. Returns the length of the data expected by the 278 command. This will be Positive for data transfers from the device 279 (eg. disk reads), negative for transfers to the device (eg. disk writes), 280 and zero if the command does not transfer any data. */ 281 282 static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd) 283 { 284 SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req); 285 SCSIDevice *s = r->req.dev; 286 int ret; 287 288 #ifdef DEBUG_SCSI 289 { 290 int i; 291 for (i = 1; i < r->req.cmd.len; i++) { 292 printf(" 0x%02x", cmd[i]); 293 } 294 printf("\n"); 295 } 296 #endif 297 298 if (r->req.cmd.xfer == 0) { 299 if (r->buf != NULL) 300 g_free(r->buf); 301 r->buflen = 0; 302 r->buf = NULL; 303 /* The request is used as the AIO opaque value, so add a ref. */ 304 scsi_req_ref(&r->req); 305 ret = execute_command(s->conf.bs, r, SG_DXFER_NONE, scsi_command_complete); 306 if (ret < 0) { 307 scsi_command_complete(r, ret); 308 return 0; 309 } 310 return 0; 311 } 312 313 if (r->buflen != r->req.cmd.xfer) { 314 if (r->buf != NULL) 315 g_free(r->buf); 316 r->buf = g_malloc(r->req.cmd.xfer); 317 r->buflen = r->req.cmd.xfer; 318 } 319 320 memset(r->buf, 0, r->buflen); 321 r->len = r->req.cmd.xfer; 322 if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { 323 r->len = 0; 324 return -r->req.cmd.xfer; 325 } else { 326 return r->req.cmd.xfer; 327 } 328 } 329 330 static int get_stream_blocksize(BlockDriverState *bdrv) 331 { 332 uint8_t cmd[6]; 333 uint8_t buf[12]; 334 uint8_t sensebuf[8]; 335 sg_io_hdr_t io_header; 336 int ret; 337 338 memset(cmd, 0, sizeof(cmd)); 339 memset(buf, 0, sizeof(buf)); 340 cmd[0] = MODE_SENSE; 341 cmd[4] = sizeof(buf); 342 343 memset(&io_header, 0, sizeof(io_header)); 344 io_header.interface_id = 'S'; 345 io_header.dxfer_direction = SG_DXFER_FROM_DEV; 346 io_header.dxfer_len = sizeof(buf); 347 io_header.dxferp = buf; 348 io_header.cmdp = cmd; 349 io_header.cmd_len = sizeof(cmd); 350 io_header.mx_sb_len = sizeof(sensebuf); 351 io_header.sbp = sensebuf; 352 io_header.timeout = 6000; /* XXX */ 353 354 ret = bdrv_ioctl(bdrv, SG_IO, &io_header); 355 if (ret < 0 || io_header.driver_status || io_header.host_status) { 356 return -1; 357 } 358 return (buf[9] << 16) | (buf[10] << 8) | buf[11]; 359 } 360 361 static void scsi_generic_reset(DeviceState *dev) 362 { 363 SCSIDevice *s = SCSI_DEVICE(dev); 364 365 scsi_device_purge_requests(s, SENSE_CODE(RESET)); 366 } 367 368 static void scsi_unrealize(SCSIDevice *s, Error **errp) 369 { 370 scsi_device_purge_requests(s, SENSE_CODE(NO_SENSE)); 371 blockdev_mark_auto_del(s->conf.bs); 372 } 373 374 static void scsi_generic_realize(SCSIDevice *s, Error **errp) 375 { 376 int rc; 377 int sg_version; 378 struct sg_scsi_id scsiid; 379 380 if (!s->conf.bs) { 381 error_setg(errp, "drive property not set"); 382 return; 383 } 384 385 if (bdrv_get_on_error(s->conf.bs, 0) != BLOCKDEV_ON_ERROR_ENOSPC) { 386 error_setg(errp, "Device doesn't support drive option werror"); 387 return; 388 } 389 if (bdrv_get_on_error(s->conf.bs, 1) != BLOCKDEV_ON_ERROR_REPORT) { 390 error_setg(errp, "Device doesn't support drive option rerror"); 391 return; 392 } 393 394 /* check we are using a driver managing SG_IO (version 3 and after */ 395 rc = bdrv_ioctl(s->conf.bs, SG_GET_VERSION_NUM, &sg_version); 396 if (rc < 0) { 397 error_setg(errp, "cannot get SG_IO version number: %s. " 398 "Is this a SCSI device?", 399 strerror(-rc)); 400 return; 401 } 402 if (sg_version < 30000) { 403 error_setg(errp, "scsi generic interface too old"); 404 return; 405 } 406 407 /* get LUN of the /dev/sg? */ 408 if (bdrv_ioctl(s->conf.bs, SG_GET_SCSI_ID, &scsiid)) { 409 error_setg(errp, "SG_GET_SCSI_ID ioctl failed"); 410 return; 411 } 412 413 /* define device state */ 414 s->type = scsiid.scsi_type; 415 DPRINTF("device type %d\n", s->type); 416 if (s->type == TYPE_DISK || s->type == TYPE_ROM) { 417 add_boot_device_path(s->conf.bootindex, &s->qdev, NULL); 418 } 419 420 switch (s->type) { 421 case TYPE_TAPE: 422 s->blocksize = get_stream_blocksize(s->conf.bs); 423 if (s->blocksize == -1) { 424 s->blocksize = 0; 425 } 426 break; 427 428 /* Make a guess for block devices, we'll fix it when the guest sends. 429 * READ CAPACITY. If they don't, they likely would assume these sizes 430 * anyway. (TODO: they could also send MODE SENSE). 431 */ 432 case TYPE_ROM: 433 case TYPE_WORM: 434 s->blocksize = 2048; 435 break; 436 default: 437 s->blocksize = 512; 438 break; 439 } 440 441 DPRINTF("block size %d\n", s->blocksize); 442 } 443 444 const SCSIReqOps scsi_generic_req_ops = { 445 .size = sizeof(SCSIGenericReq), 446 .free_req = scsi_free_request, 447 .send_command = scsi_send_command, 448 .read_data = scsi_read_data, 449 .write_data = scsi_write_data, 450 .get_buf = scsi_get_buf, 451 .load_request = scsi_generic_load_request, 452 .save_request = scsi_generic_save_request, 453 }; 454 455 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, 456 uint8_t *buf, void *hba_private) 457 { 458 SCSIRequest *req; 459 460 req = scsi_req_alloc(&scsi_generic_req_ops, d, tag, lun, hba_private); 461 return req; 462 } 463 464 static Property scsi_generic_properties[] = { 465 DEFINE_PROP_DRIVE("drive", SCSIDevice, conf.bs), 466 DEFINE_PROP_INT32("bootindex", SCSIDevice, conf.bootindex, -1), 467 DEFINE_PROP_END_OF_LIST(), 468 }; 469 470 static int scsi_generic_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, 471 uint8_t *buf, void *hba_private) 472 { 473 return scsi_bus_parse_cdb(dev, cmd, buf, hba_private); 474 } 475 476 static void scsi_generic_class_initfn(ObjectClass *klass, void *data) 477 { 478 DeviceClass *dc = DEVICE_CLASS(klass); 479 SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); 480 481 sc->realize = scsi_generic_realize; 482 sc->unrealize = scsi_unrealize; 483 sc->alloc_req = scsi_new_request; 484 sc->parse_cdb = scsi_generic_parse_cdb; 485 dc->fw_name = "disk"; 486 dc->desc = "pass through generic scsi device (/dev/sg*)"; 487 dc->reset = scsi_generic_reset; 488 dc->props = scsi_generic_properties; 489 dc->vmsd = &vmstate_scsi_device; 490 } 491 492 static const TypeInfo scsi_generic_info = { 493 .name = "scsi-generic", 494 .parent = TYPE_SCSI_DEVICE, 495 .instance_size = sizeof(SCSIDevice), 496 .class_init = scsi_generic_class_initfn, 497 }; 498 499 static void scsi_generic_register_types(void) 500 { 501 type_register_static(&scsi_generic_info); 502 } 503 504 type_init(scsi_generic_register_types) 505 506 #endif /* __linux__ */ 507