1 /* 2 * UAS (USB Attached SCSI) emulation 3 * 4 * Copyright Red Hat, Inc. 2012 5 * 6 * Author: Gerd Hoffmann <kraxel@redhat.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 */ 11 12 #include "qemu/osdep.h" 13 #include "qemu/option.h" 14 #include "qemu/config-file.h" 15 #include "trace.h" 16 #include "qemu/error-report.h" 17 #include "qemu/main-loop.h" 18 #include "qemu/module.h" 19 20 #include "hw/usb.h" 21 #include "migration/vmstate.h" 22 #include "desc.h" 23 #include "hw/qdev-properties.h" 24 #include "hw/scsi/scsi.h" 25 #include "scsi/constants.h" 26 #include "qom/object.h" 27 28 /* --------------------------------------------------------------------- */ 29 30 #define UAS_UI_COMMAND 0x01 31 #define UAS_UI_SENSE 0x03 32 #define UAS_UI_RESPONSE 0x04 33 #define UAS_UI_TASK_MGMT 0x05 34 #define UAS_UI_READ_READY 0x06 35 #define UAS_UI_WRITE_READY 0x07 36 37 #define UAS_RC_TMF_COMPLETE 0x00 38 #define UAS_RC_INVALID_INFO_UNIT 0x02 39 #define UAS_RC_TMF_NOT_SUPPORTED 0x04 40 #define UAS_RC_TMF_FAILED 0x05 41 #define UAS_RC_TMF_SUCCEEDED 0x08 42 #define UAS_RC_INCORRECT_LUN 0x09 43 #define UAS_RC_OVERLAPPED_TAG 0x0a 44 45 #define UAS_TMF_ABORT_TASK 0x01 46 #define UAS_TMF_ABORT_TASK_SET 0x02 47 #define UAS_TMF_CLEAR_TASK_SET 0x04 48 #define UAS_TMF_LOGICAL_UNIT_RESET 0x08 49 #define UAS_TMF_I_T_NEXUS_RESET 0x10 50 #define UAS_TMF_CLEAR_ACA 0x40 51 #define UAS_TMF_QUERY_TASK 0x80 52 #define UAS_TMF_QUERY_TASK_SET 0x81 53 #define UAS_TMF_QUERY_ASYNC_EVENT 0x82 54 55 #define UAS_PIPE_ID_COMMAND 0x01 56 #define UAS_PIPE_ID_STATUS 0x02 57 #define UAS_PIPE_ID_DATA_IN 0x03 58 #define UAS_PIPE_ID_DATA_OUT 0x04 59 60 typedef struct { 61 uint8_t id; 62 uint8_t reserved; 63 uint16_t tag; 64 } QEMU_PACKED uas_iu_header; 65 66 typedef struct { 67 uint8_t prio_taskattr; /* 6:3 priority, 2:0 task attribute */ 68 uint8_t reserved_1; 69 uint8_t add_cdb_length; /* 7:2 additional adb length (dwords) */ 70 uint8_t reserved_2; 71 uint64_t lun; 72 uint8_t cdb[16]; 73 uint8_t add_cdb[]; 74 } QEMU_PACKED uas_iu_command; 75 76 typedef struct { 77 uint16_t status_qualifier; 78 uint8_t status; 79 uint8_t reserved[7]; 80 uint16_t sense_length; 81 uint8_t sense_data[18]; 82 } QEMU_PACKED uas_iu_sense; 83 84 typedef struct { 85 uint8_t add_response_info[3]; 86 uint8_t response_code; 87 } QEMU_PACKED uas_iu_response; 88 89 typedef struct { 90 uint8_t function; 91 uint8_t reserved; 92 uint16_t task_tag; 93 uint64_t lun; 94 } QEMU_PACKED uas_iu_task_mgmt; 95 96 typedef struct { 97 uas_iu_header hdr; 98 union { 99 uas_iu_command command; 100 uas_iu_sense sense; 101 uas_iu_task_mgmt task; 102 uas_iu_response response; 103 }; 104 } QEMU_PACKED uas_iu; 105 106 /* --------------------------------------------------------------------- */ 107 108 #define UAS_STREAM_BM_ATTR 4 109 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR) 110 111 typedef struct UASDevice UASDevice; 112 typedef struct UASRequest UASRequest; 113 typedef struct UASStatus UASStatus; 114 115 struct UASDevice { 116 USBDevice dev; 117 SCSIBus bus; 118 QEMUBH *status_bh; 119 QTAILQ_HEAD(, UASStatus) results; 120 QTAILQ_HEAD(, UASRequest) requests; 121 122 /* properties */ 123 uint32_t requestlog; 124 125 /* usb 2.0 only */ 126 USBPacket *status2; 127 UASRequest *datain2; 128 UASRequest *dataout2; 129 130 /* usb 3.0 only */ 131 USBPacket *data3[UAS_MAX_STREAMS + 1]; 132 USBPacket *status3[UAS_MAX_STREAMS + 1]; 133 }; 134 135 #define TYPE_USB_UAS "usb-uas" 136 DECLARE_INSTANCE_CHECKER(UASDevice, USB_UAS, 137 TYPE_USB_UAS) 138 139 struct UASRequest { 140 uint16_t tag; 141 uint64_t lun; 142 UASDevice *uas; 143 SCSIDevice *dev; 144 SCSIRequest *req; 145 USBPacket *data; 146 bool data_async; 147 bool active; 148 bool complete; 149 uint32_t buf_off; 150 uint32_t buf_size; 151 uint32_t data_off; 152 uint32_t data_size; 153 QTAILQ_ENTRY(UASRequest) next; 154 }; 155 156 struct UASStatus { 157 uint32_t stream; 158 uas_iu status; 159 uint32_t length; 160 QTAILQ_ENTRY(UASStatus) next; 161 }; 162 163 /* --------------------------------------------------------------------- */ 164 165 enum { 166 STR_MANUFACTURER = 1, 167 STR_PRODUCT, 168 STR_SERIALNUMBER, 169 STR_CONFIG_HIGH, 170 STR_CONFIG_SUPER, 171 }; 172 173 static const USBDescStrings desc_strings = { 174 [STR_MANUFACTURER] = "QEMU", 175 [STR_PRODUCT] = "USB Attached SCSI HBA", 176 [STR_SERIALNUMBER] = "27842", 177 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)", 178 [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)", 179 }; 180 181 static const USBDescIface desc_iface_high = { 182 .bInterfaceNumber = 0, 183 .bNumEndpoints = 4, 184 .bInterfaceClass = USB_CLASS_MASS_STORAGE, 185 .bInterfaceSubClass = 0x06, /* SCSI */ 186 .bInterfaceProtocol = 0x62, /* UAS */ 187 .eps = (USBDescEndpoint[]) { 188 { 189 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_COMMAND, 190 .bmAttributes = USB_ENDPOINT_XFER_BULK, 191 .wMaxPacketSize = 512, 192 .extra = (uint8_t[]) { 193 0x04, /* u8 bLength */ 194 0x24, /* u8 bDescriptorType */ 195 UAS_PIPE_ID_COMMAND, 196 0x00, /* u8 bReserved */ 197 }, 198 },{ 199 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_STATUS, 200 .bmAttributes = USB_ENDPOINT_XFER_BULK, 201 .wMaxPacketSize = 512, 202 .extra = (uint8_t[]) { 203 0x04, /* u8 bLength */ 204 0x24, /* u8 bDescriptorType */ 205 UAS_PIPE_ID_STATUS, 206 0x00, /* u8 bReserved */ 207 }, 208 },{ 209 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_DATA_IN, 210 .bmAttributes = USB_ENDPOINT_XFER_BULK, 211 .wMaxPacketSize = 512, 212 .extra = (uint8_t[]) { 213 0x04, /* u8 bLength */ 214 0x24, /* u8 bDescriptorType */ 215 UAS_PIPE_ID_DATA_IN, 216 0x00, /* u8 bReserved */ 217 }, 218 },{ 219 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_DATA_OUT, 220 .bmAttributes = USB_ENDPOINT_XFER_BULK, 221 .wMaxPacketSize = 512, 222 .extra = (uint8_t[]) { 223 0x04, /* u8 bLength */ 224 0x24, /* u8 bDescriptorType */ 225 UAS_PIPE_ID_DATA_OUT, 226 0x00, /* u8 bReserved */ 227 }, 228 }, 229 } 230 }; 231 232 static const USBDescIface desc_iface_super = { 233 .bInterfaceNumber = 0, 234 .bNumEndpoints = 4, 235 .bInterfaceClass = USB_CLASS_MASS_STORAGE, 236 .bInterfaceSubClass = 0x06, /* SCSI */ 237 .bInterfaceProtocol = 0x62, /* UAS */ 238 .eps = (USBDescEndpoint[]) { 239 { 240 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_COMMAND, 241 .bmAttributes = USB_ENDPOINT_XFER_BULK, 242 .wMaxPacketSize = 1024, 243 .bMaxBurst = 15, 244 .extra = (uint8_t[]) { 245 0x04, /* u8 bLength */ 246 0x24, /* u8 bDescriptorType */ 247 UAS_PIPE_ID_COMMAND, 248 0x00, /* u8 bReserved */ 249 }, 250 },{ 251 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_STATUS, 252 .bmAttributes = USB_ENDPOINT_XFER_BULK, 253 .wMaxPacketSize = 1024, 254 .bMaxBurst = 15, 255 .bmAttributes_super = UAS_STREAM_BM_ATTR, 256 .extra = (uint8_t[]) { 257 0x04, /* u8 bLength */ 258 0x24, /* u8 bDescriptorType */ 259 UAS_PIPE_ID_STATUS, 260 0x00, /* u8 bReserved */ 261 }, 262 },{ 263 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_DATA_IN, 264 .bmAttributes = USB_ENDPOINT_XFER_BULK, 265 .wMaxPacketSize = 1024, 266 .bMaxBurst = 15, 267 .bmAttributes_super = UAS_STREAM_BM_ATTR, 268 .extra = (uint8_t[]) { 269 0x04, /* u8 bLength */ 270 0x24, /* u8 bDescriptorType */ 271 UAS_PIPE_ID_DATA_IN, 272 0x00, /* u8 bReserved */ 273 }, 274 },{ 275 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_DATA_OUT, 276 .bmAttributes = USB_ENDPOINT_XFER_BULK, 277 .wMaxPacketSize = 1024, 278 .bMaxBurst = 15, 279 .bmAttributes_super = UAS_STREAM_BM_ATTR, 280 .extra = (uint8_t[]) { 281 0x04, /* u8 bLength */ 282 0x24, /* u8 bDescriptorType */ 283 UAS_PIPE_ID_DATA_OUT, 284 0x00, /* u8 bReserved */ 285 }, 286 }, 287 } 288 }; 289 290 static const USBDescDevice desc_device_high = { 291 .bcdUSB = 0x0200, 292 .bMaxPacketSize0 = 64, 293 .bNumConfigurations = 1, 294 .confs = (USBDescConfig[]) { 295 { 296 .bNumInterfaces = 1, 297 .bConfigurationValue = 1, 298 .iConfiguration = STR_CONFIG_HIGH, 299 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, 300 .nif = 1, 301 .ifs = &desc_iface_high, 302 }, 303 }, 304 }; 305 306 static const USBDescDevice desc_device_super = { 307 .bcdUSB = 0x0300, 308 .bMaxPacketSize0 = 9, 309 .bNumConfigurations = 1, 310 .confs = (USBDescConfig[]) { 311 { 312 .bNumInterfaces = 1, 313 .bConfigurationValue = 1, 314 .iConfiguration = STR_CONFIG_SUPER, 315 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, 316 .nif = 1, 317 .ifs = &desc_iface_super, 318 }, 319 }, 320 }; 321 322 static const USBDesc desc = { 323 .id = { 324 .idVendor = 0x46f4, /* CRC16() of "QEMU" */ 325 .idProduct = 0x0003, 326 .bcdDevice = 0, 327 .iManufacturer = STR_MANUFACTURER, 328 .iProduct = STR_PRODUCT, 329 .iSerialNumber = STR_SERIALNUMBER, 330 }, 331 .high = &desc_device_high, 332 .super = &desc_device_super, 333 .str = desc_strings, 334 }; 335 336 /* --------------------------------------------------------------------- */ 337 338 static bool uas_using_streams(UASDevice *uas) 339 { 340 return uas->dev.speed == USB_SPEED_SUPER; 341 } 342 343 /* --------------------------------------------------------------------- */ 344 345 static UASStatus *usb_uas_alloc_status(UASDevice *uas, uint8_t id, uint16_t tag) 346 { 347 UASStatus *st = g_new0(UASStatus, 1); 348 349 st->status.hdr.id = id; 350 st->status.hdr.tag = cpu_to_be16(tag); 351 st->length = sizeof(uas_iu_header); 352 if (uas_using_streams(uas)) { 353 st->stream = tag; 354 } 355 return st; 356 } 357 358 static void usb_uas_send_status_bh(void *opaque) 359 { 360 UASDevice *uas = opaque; 361 UASStatus *st; 362 USBPacket *p; 363 364 while ((st = QTAILQ_FIRST(&uas->results)) != NULL) { 365 if (uas_using_streams(uas)) { 366 p = uas->status3[st->stream]; 367 uas->status3[st->stream] = NULL; 368 } else { 369 p = uas->status2; 370 uas->status2 = NULL; 371 } 372 if (p == NULL) { 373 break; 374 } 375 376 usb_packet_copy(p, &st->status, st->length); 377 QTAILQ_REMOVE(&uas->results, st, next); 378 g_free(st); 379 380 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ 381 usb_packet_complete(&uas->dev, p); 382 } 383 } 384 385 static void usb_uas_queue_status(UASDevice *uas, UASStatus *st, int length) 386 { 387 USBPacket *p = uas_using_streams(uas) ? 388 uas->status3[st->stream] : uas->status2; 389 390 st->length += length; 391 QTAILQ_INSERT_TAIL(&uas->results, st, next); 392 if (p) { 393 /* 394 * Just schedule bh make sure any in-flight data transaction 395 * is finished before completing (sending) the status packet. 396 */ 397 qemu_bh_schedule(uas->status_bh); 398 } else { 399 USBEndpoint *ep = usb_ep_get(&uas->dev, USB_TOKEN_IN, 400 UAS_PIPE_ID_STATUS); 401 usb_wakeup(ep, st->stream); 402 } 403 } 404 405 static void usb_uas_queue_response(UASDevice *uas, uint16_t tag, uint8_t code) 406 { 407 UASStatus *st = usb_uas_alloc_status(uas, UAS_UI_RESPONSE, tag); 408 409 trace_usb_uas_response(uas->dev.addr, tag, code); 410 st->status.response.response_code = code; 411 usb_uas_queue_status(uas, st, sizeof(uas_iu_response)); 412 } 413 414 static void usb_uas_queue_sense(UASRequest *req, uint8_t status) 415 { 416 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_SENSE, req->tag); 417 int len, slen = 0; 418 419 trace_usb_uas_sense(req->uas->dev.addr, req->tag, status); 420 st->status.sense.status = status; 421 st->status.sense.status_qualifier = cpu_to_be16(0); 422 if (status != GOOD) { 423 slen = scsi_req_get_sense(req->req, st->status.sense.sense_data, 424 sizeof(st->status.sense.sense_data)); 425 st->status.sense.sense_length = cpu_to_be16(slen); 426 } 427 len = sizeof(uas_iu_sense) - sizeof(st->status.sense.sense_data) + slen; 428 usb_uas_queue_status(req->uas, st, len); 429 } 430 431 static void usb_uas_queue_fake_sense(UASDevice *uas, uint16_t tag, 432 struct SCSISense sense) 433 { 434 UASStatus *st = usb_uas_alloc_status(uas, UAS_UI_SENSE, tag); 435 int len, slen = 0; 436 437 st->status.sense.status = CHECK_CONDITION; 438 st->status.sense.status_qualifier = cpu_to_be16(0); 439 st->status.sense.sense_data[0] = 0x70; 440 st->status.sense.sense_data[2] = sense.key; 441 st->status.sense.sense_data[7] = 10; 442 st->status.sense.sense_data[12] = sense.asc; 443 st->status.sense.sense_data[13] = sense.ascq; 444 slen = 18; 445 len = sizeof(uas_iu_sense) - sizeof(st->status.sense.sense_data) + slen; 446 usb_uas_queue_status(uas, st, len); 447 } 448 449 static void usb_uas_queue_read_ready(UASRequest *req) 450 { 451 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_READ_READY, 452 req->tag); 453 454 trace_usb_uas_read_ready(req->uas->dev.addr, req->tag); 455 usb_uas_queue_status(req->uas, st, 0); 456 } 457 458 static void usb_uas_queue_write_ready(UASRequest *req) 459 { 460 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_WRITE_READY, 461 req->tag); 462 463 trace_usb_uas_write_ready(req->uas->dev.addr, req->tag); 464 usb_uas_queue_status(req->uas, st, 0); 465 } 466 467 /* --------------------------------------------------------------------- */ 468 469 static int usb_uas_get_lun(uint64_t lun64) 470 { 471 return (lun64 >> 48) & 0xff; 472 } 473 474 static SCSIDevice *usb_uas_get_dev(UASDevice *uas, uint64_t lun64) 475 { 476 if ((lun64 >> 56) != 0x00) { 477 return NULL; 478 } 479 return scsi_device_find(&uas->bus, 0, 0, usb_uas_get_lun(lun64)); 480 } 481 482 static void usb_uas_complete_data_packet(UASRequest *req) 483 { 484 USBPacket *p; 485 486 if (!req->data_async) { 487 return; 488 } 489 p = req->data; 490 req->data = NULL; 491 req->data_async = false; 492 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ 493 usb_packet_complete(&req->uas->dev, p); 494 } 495 496 static void usb_uas_copy_data(UASRequest *req) 497 { 498 uint32_t length; 499 500 length = MIN(req->buf_size - req->buf_off, 501 req->data->iov.size - req->data->actual_length); 502 trace_usb_uas_xfer_data(req->uas->dev.addr, req->tag, length, 503 req->data->actual_length, req->data->iov.size, 504 req->buf_off, req->buf_size); 505 usb_packet_copy(req->data, scsi_req_get_buf(req->req) + req->buf_off, 506 length); 507 req->buf_off += length; 508 req->data_off += length; 509 510 if (req->data->actual_length == req->data->iov.size) { 511 usb_uas_complete_data_packet(req); 512 } 513 if (req->buf_size && req->buf_off == req->buf_size) { 514 req->buf_off = 0; 515 req->buf_size = 0; 516 scsi_req_continue(req->req); 517 } 518 } 519 520 static void usb_uas_start_next_transfer(UASDevice *uas) 521 { 522 UASRequest *req; 523 524 if (uas_using_streams(uas)) { 525 return; 526 } 527 528 QTAILQ_FOREACH(req, &uas->requests, next) { 529 if (req->active || req->complete) { 530 continue; 531 } 532 if (req->req->cmd.mode == SCSI_XFER_FROM_DEV && uas->datain2 == NULL) { 533 uas->datain2 = req; 534 usb_uas_queue_read_ready(req); 535 req->active = true; 536 return; 537 } 538 if (req->req->cmd.mode == SCSI_XFER_TO_DEV && uas->dataout2 == NULL) { 539 uas->dataout2 = req; 540 usb_uas_queue_write_ready(req); 541 req->active = true; 542 return; 543 } 544 } 545 } 546 547 static UASRequest *usb_uas_alloc_request(UASDevice *uas, uas_iu *iu) 548 { 549 UASRequest *req; 550 551 req = g_new0(UASRequest, 1); 552 req->uas = uas; 553 req->tag = be16_to_cpu(iu->hdr.tag); 554 req->lun = be64_to_cpu(iu->command.lun); 555 req->dev = usb_uas_get_dev(req->uas, req->lun); 556 return req; 557 } 558 559 static void usb_uas_scsi_free_request(SCSIBus *bus, void *priv) 560 { 561 UASRequest *req = priv; 562 UASDevice *uas = req->uas; 563 564 if (req == uas->datain2) { 565 uas->datain2 = NULL; 566 } 567 if (req == uas->dataout2) { 568 uas->dataout2 = NULL; 569 } 570 QTAILQ_REMOVE(&uas->requests, req, next); 571 g_free(req); 572 usb_uas_start_next_transfer(uas); 573 } 574 575 static UASRequest *usb_uas_find_request(UASDevice *uas, uint16_t tag) 576 { 577 UASRequest *req; 578 579 QTAILQ_FOREACH(req, &uas->requests, next) { 580 if (req->tag == tag) { 581 return req; 582 } 583 } 584 return NULL; 585 } 586 587 static void usb_uas_scsi_transfer_data(SCSIRequest *r, uint32_t len) 588 { 589 UASRequest *req = r->hba_private; 590 591 trace_usb_uas_scsi_data(req->uas->dev.addr, req->tag, len); 592 req->buf_off = 0; 593 req->buf_size = len; 594 if (req->data) { 595 usb_uas_copy_data(req); 596 } else { 597 usb_uas_start_next_transfer(req->uas); 598 } 599 } 600 601 static void usb_uas_scsi_command_complete(SCSIRequest *r, 602 uint32_t status, size_t resid) 603 { 604 UASRequest *req = r->hba_private; 605 606 trace_usb_uas_scsi_complete(req->uas->dev.addr, req->tag, status, resid); 607 req->complete = true; 608 if (req->data) { 609 usb_uas_complete_data_packet(req); 610 } 611 usb_uas_queue_sense(req, status); 612 scsi_req_unref(req->req); 613 } 614 615 static void usb_uas_scsi_request_cancelled(SCSIRequest *r) 616 { 617 UASRequest *req = r->hba_private; 618 619 /* FIXME: queue notification to status pipe? */ 620 scsi_req_unref(req->req); 621 } 622 623 static const struct SCSIBusInfo usb_uas_scsi_info = { 624 .tcq = true, 625 .max_target = 0, 626 .max_lun = 255, 627 628 .transfer_data = usb_uas_scsi_transfer_data, 629 .complete = usb_uas_scsi_command_complete, 630 .cancel = usb_uas_scsi_request_cancelled, 631 .free_request = usb_uas_scsi_free_request, 632 }; 633 634 /* --------------------------------------------------------------------- */ 635 636 static void usb_uas_handle_reset(USBDevice *dev) 637 { 638 UASDevice *uas = USB_UAS(dev); 639 UASRequest *req, *nreq; 640 UASStatus *st, *nst; 641 642 trace_usb_uas_reset(dev->addr); 643 QTAILQ_FOREACH_SAFE(req, &uas->requests, next, nreq) { 644 scsi_req_cancel(req->req); 645 } 646 QTAILQ_FOREACH_SAFE(st, &uas->results, next, nst) { 647 QTAILQ_REMOVE(&uas->results, st, next); 648 g_free(st); 649 } 650 } 651 652 static void usb_uas_handle_control(USBDevice *dev, USBPacket *p, 653 int request, int value, int index, int length, uint8_t *data) 654 { 655 int ret; 656 657 ret = usb_desc_handle_control(dev, p, request, value, index, length, data); 658 if (ret >= 0) { 659 return; 660 } 661 error_report("%s: unhandled control request (req 0x%x, val 0x%x, idx 0x%x", 662 __func__, request, value, index); 663 p->status = USB_RET_STALL; 664 } 665 666 static void usb_uas_cancel_io(USBDevice *dev, USBPacket *p) 667 { 668 UASDevice *uas = USB_UAS(dev); 669 UASRequest *req, *nreq; 670 int i; 671 672 if (uas->status2 == p) { 673 uas->status2 = NULL; 674 qemu_bh_cancel(uas->status_bh); 675 return; 676 } 677 if (uas_using_streams(uas)) { 678 for (i = 0; i <= UAS_MAX_STREAMS; i++) { 679 if (uas->status3[i] == p) { 680 uas->status3[i] = NULL; 681 return; 682 } 683 if (uas->data3[i] == p) { 684 uas->data3[i] = NULL; 685 return; 686 } 687 } 688 } 689 QTAILQ_FOREACH_SAFE(req, &uas->requests, next, nreq) { 690 if (req->data == p) { 691 req->data = NULL; 692 return; 693 } 694 } 695 assert(!"canceled usb packet not found"); 696 } 697 698 static void usb_uas_command(UASDevice *uas, uas_iu *iu) 699 { 700 UASRequest *req; 701 uint32_t len; 702 uint16_t tag = be16_to_cpu(iu->hdr.tag); 703 704 if (uas_using_streams(uas) && tag > UAS_MAX_STREAMS) { 705 goto invalid_tag; 706 } 707 req = usb_uas_find_request(uas, tag); 708 if (req) { 709 goto overlapped_tag; 710 } 711 req = usb_uas_alloc_request(uas, iu); 712 if (req->dev == NULL) { 713 goto bad_target; 714 } 715 716 trace_usb_uas_command(uas->dev.addr, req->tag, 717 usb_uas_get_lun(req->lun), 718 req->lun >> 32, req->lun & 0xffffffff); 719 QTAILQ_INSERT_TAIL(&uas->requests, req, next); 720 if (uas_using_streams(uas) && uas->data3[req->tag] != NULL) { 721 req->data = uas->data3[req->tag]; 722 req->data_async = true; 723 uas->data3[req->tag] = NULL; 724 } 725 726 req->req = scsi_req_new(req->dev, req->tag, 727 usb_uas_get_lun(req->lun), 728 iu->command.cdb, req); 729 if (uas->requestlog) { 730 scsi_req_print(req->req); 731 } 732 len = scsi_req_enqueue(req->req); 733 if (len) { 734 req->data_size = len; 735 scsi_req_continue(req->req); 736 } 737 return; 738 739 invalid_tag: 740 usb_uas_queue_fake_sense(uas, tag, sense_code_INVALID_TAG); 741 return; 742 743 overlapped_tag: 744 usb_uas_queue_fake_sense(uas, tag, sense_code_OVERLAPPED_COMMANDS); 745 return; 746 747 bad_target: 748 usb_uas_queue_fake_sense(uas, tag, sense_code_LUN_NOT_SUPPORTED); 749 g_free(req); 750 } 751 752 static void usb_uas_task(UASDevice *uas, uas_iu *iu) 753 { 754 uint16_t tag = be16_to_cpu(iu->hdr.tag); 755 uint64_t lun64 = be64_to_cpu(iu->task.lun); 756 SCSIDevice *dev = usb_uas_get_dev(uas, lun64); 757 int lun = usb_uas_get_lun(lun64); 758 UASRequest *req; 759 uint16_t task_tag; 760 761 if (uas_using_streams(uas) && tag > UAS_MAX_STREAMS) { 762 goto invalid_tag; 763 } 764 req = usb_uas_find_request(uas, be16_to_cpu(iu->hdr.tag)); 765 if (req) { 766 goto overlapped_tag; 767 } 768 if (dev == NULL) { 769 goto incorrect_lun; 770 } 771 772 switch (iu->task.function) { 773 case UAS_TMF_ABORT_TASK: 774 task_tag = be16_to_cpu(iu->task.task_tag); 775 trace_usb_uas_tmf_abort_task(uas->dev.addr, tag, task_tag); 776 req = usb_uas_find_request(uas, task_tag); 777 if (req && req->dev == dev) { 778 scsi_req_cancel(req->req); 779 } 780 usb_uas_queue_response(uas, tag, UAS_RC_TMF_COMPLETE); 781 break; 782 783 case UAS_TMF_LOGICAL_UNIT_RESET: 784 trace_usb_uas_tmf_logical_unit_reset(uas->dev.addr, tag, lun); 785 qdev_reset_all(&dev->qdev); 786 usb_uas_queue_response(uas, tag, UAS_RC_TMF_COMPLETE); 787 break; 788 789 default: 790 trace_usb_uas_tmf_unsupported(uas->dev.addr, tag, iu->task.function); 791 usb_uas_queue_response(uas, tag, UAS_RC_TMF_NOT_SUPPORTED); 792 break; 793 } 794 return; 795 796 invalid_tag: 797 usb_uas_queue_response(uas, tag, UAS_RC_INVALID_INFO_UNIT); 798 return; 799 800 overlapped_tag: 801 usb_uas_queue_response(uas, req->tag, UAS_RC_OVERLAPPED_TAG); 802 return; 803 804 incorrect_lun: 805 usb_uas_queue_response(uas, tag, UAS_RC_INCORRECT_LUN); 806 } 807 808 static void usb_uas_handle_data(USBDevice *dev, USBPacket *p) 809 { 810 UASDevice *uas = USB_UAS(dev); 811 uas_iu iu; 812 UASStatus *st; 813 UASRequest *req; 814 int length; 815 816 switch (p->ep->nr) { 817 case UAS_PIPE_ID_COMMAND: 818 length = MIN(sizeof(iu), p->iov.size); 819 usb_packet_copy(p, &iu, length); 820 switch (iu.hdr.id) { 821 case UAS_UI_COMMAND: 822 usb_uas_command(uas, &iu); 823 break; 824 case UAS_UI_TASK_MGMT: 825 usb_uas_task(uas, &iu); 826 break; 827 default: 828 error_report("%s: unknown command iu: id 0x%x", 829 __func__, iu.hdr.id); 830 p->status = USB_RET_STALL; 831 break; 832 } 833 break; 834 case UAS_PIPE_ID_STATUS: 835 if (p->stream) { 836 QTAILQ_FOREACH(st, &uas->results, next) { 837 if (st->stream == p->stream) { 838 break; 839 } 840 } 841 if (st == NULL) { 842 assert(uas->status3[p->stream] == NULL); 843 uas->status3[p->stream] = p; 844 p->status = USB_RET_ASYNC; 845 break; 846 } 847 } else { 848 st = QTAILQ_FIRST(&uas->results); 849 if (st == NULL) { 850 assert(uas->status2 == NULL); 851 uas->status2 = p; 852 p->status = USB_RET_ASYNC; 853 break; 854 } 855 } 856 usb_packet_copy(p, &st->status, st->length); 857 QTAILQ_REMOVE(&uas->results, st, next); 858 g_free(st); 859 break; 860 case UAS_PIPE_ID_DATA_IN: 861 case UAS_PIPE_ID_DATA_OUT: 862 if (p->stream) { 863 req = usb_uas_find_request(uas, p->stream); 864 } else { 865 req = (p->ep->nr == UAS_PIPE_ID_DATA_IN) 866 ? uas->datain2 : uas->dataout2; 867 } 868 if (req == NULL) { 869 if (p->stream) { 870 assert(uas->data3[p->stream] == NULL); 871 uas->data3[p->stream] = p; 872 p->status = USB_RET_ASYNC; 873 break; 874 } else { 875 error_report("%s: no inflight request", __func__); 876 p->status = USB_RET_STALL; 877 break; 878 } 879 } 880 scsi_req_ref(req->req); 881 req->data = p; 882 usb_uas_copy_data(req); 883 if (p->actual_length == p->iov.size || req->complete) { 884 req->data = NULL; 885 } else { 886 req->data_async = true; 887 p->status = USB_RET_ASYNC; 888 } 889 scsi_req_unref(req->req); 890 usb_uas_start_next_transfer(uas); 891 break; 892 default: 893 error_report("%s: invalid endpoint %d", __func__, p->ep->nr); 894 p->status = USB_RET_STALL; 895 break; 896 } 897 } 898 899 static void usb_uas_unrealize(USBDevice *dev) 900 { 901 UASDevice *uas = USB_UAS(dev); 902 903 qemu_bh_delete(uas->status_bh); 904 } 905 906 static void usb_uas_realize(USBDevice *dev, Error **errp) 907 { 908 UASDevice *uas = USB_UAS(dev); 909 DeviceState *d = DEVICE(dev); 910 911 usb_desc_create_serial(dev); 912 usb_desc_init(dev); 913 if (d->hotplugged) { 914 uas->dev.auto_attach = 0; 915 } 916 917 QTAILQ_INIT(&uas->results); 918 QTAILQ_INIT(&uas->requests); 919 uas->status_bh = qemu_bh_new(usb_uas_send_status_bh, uas); 920 921 scsi_bus_new(&uas->bus, sizeof(uas->bus), DEVICE(dev), 922 &usb_uas_scsi_info, NULL); 923 } 924 925 static const VMStateDescription vmstate_usb_uas = { 926 .name = "usb-uas", 927 .unmigratable = 1, 928 .fields = (VMStateField[]) { 929 VMSTATE_USB_DEVICE(dev, UASDevice), 930 VMSTATE_END_OF_LIST() 931 } 932 }; 933 934 static Property uas_properties[] = { 935 DEFINE_PROP_UINT32("log-scsi-req", UASDevice, requestlog, 0), 936 DEFINE_PROP_END_OF_LIST(), 937 }; 938 939 static void usb_uas_class_initfn(ObjectClass *klass, void *data) 940 { 941 DeviceClass *dc = DEVICE_CLASS(klass); 942 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 943 944 uc->realize = usb_uas_realize; 945 uc->product_desc = desc_strings[STR_PRODUCT]; 946 uc->usb_desc = &desc; 947 uc->cancel_packet = usb_uas_cancel_io; 948 uc->handle_attach = usb_desc_attach; 949 uc->handle_reset = usb_uas_handle_reset; 950 uc->handle_control = usb_uas_handle_control; 951 uc->handle_data = usb_uas_handle_data; 952 uc->unrealize = usb_uas_unrealize; 953 uc->attached_settable = true; 954 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 955 dc->fw_name = "storage"; 956 dc->vmsd = &vmstate_usb_uas; 957 device_class_set_props(dc, uas_properties); 958 } 959 960 static const TypeInfo uas_info = { 961 .name = TYPE_USB_UAS, 962 .parent = TYPE_USB_DEVICE, 963 .instance_size = sizeof(UASDevice), 964 .class_init = usb_uas_class_initfn, 965 }; 966 967 static void usb_uas_register_types(void) 968 { 969 type_register_static(&uas_info); 970 } 971 972 type_init(usb_uas_register_types) 973