2 * UAS (USB Attached SCSI) emulation
4 * Copyright Red Hat, Inc. 2012
6 * Author: Gerd Hoffmann <kraxel@redhat.com>
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.
12 #include "qemu/osdep.h"
13 #include "qemu/option.h"
14 #include "qemu/config-file.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
21 #include "migration/vmstate.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/scsi/scsi.h"
25 #include "scsi/constants.h"
26 #include "qom/object.h"
28 /* --------------------------------------------------------------------- */
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
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
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
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
64 } QEMU_PACKED uas_iu_header
;
67 uint8_t prio_taskattr
; /* 6:3 priority, 2:0 task attribute */
69 uint8_t add_cdb_length
; /* 7:2 additional adb length (dwords) */
74 } QEMU_PACKED uas_iu_command
;
77 uint16_t status_qualifier
;
80 uint16_t sense_length
;
81 uint8_t sense_data
[18];
82 } QEMU_PACKED uas_iu_sense
;
85 uint8_t add_response_info
[3];
86 uint8_t response_code
;
87 } QEMU_PACKED uas_iu_response
;
94 } QEMU_PACKED uas_iu_task_mgmt
;
99 uas_iu_command command
;
101 uas_iu_task_mgmt task
;
102 uas_iu_response response
;
104 } QEMU_PACKED uas_iu
;
106 /* --------------------------------------------------------------------- */
108 #define UAS_STREAM_BM_ATTR 4
109 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR)
111 typedef struct UASDevice UASDevice
;
112 typedef struct UASRequest UASRequest
;
113 typedef struct UASStatus UASStatus
;
119 QTAILQ_HEAD(, UASStatus
) results
;
120 QTAILQ_HEAD(, UASRequest
) requests
;
128 UASRequest
*dataout2
;
131 USBPacket
*data3
[UAS_MAX_STREAMS
+ 1];
132 USBPacket
*status3
[UAS_MAX_STREAMS
+ 1];
135 #define TYPE_USB_UAS "usb-uas"
136 OBJECT_DECLARE_SIMPLE_TYPE(UASDevice
, USB_UAS
)
152 QTAILQ_ENTRY(UASRequest
) next
;
159 QTAILQ_ENTRY(UASStatus
) next
;
162 /* --------------------------------------------------------------------- */
165 STR_MANUFACTURER
= 1,
172 static const USBDescStrings desc_strings
= {
173 [STR_MANUFACTURER
] = "QEMU",
174 [STR_PRODUCT
] = "USB Attached SCSI HBA",
175 [STR_SERIALNUMBER
] = "27842",
176 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
177 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
180 static const USBDescIface desc_iface_high
= {
181 .bInterfaceNumber
= 0,
183 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
184 .bInterfaceSubClass
= 0x06, /* SCSI */
185 .bInterfaceProtocol
= 0x62, /* UAS */
186 .eps
= (USBDescEndpoint
[]) {
188 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
189 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
190 .wMaxPacketSize
= 512,
191 .extra
= (uint8_t[]) {
192 0x04, /* u8 bLength */
193 0x24, /* u8 bDescriptorType */
195 0x00, /* u8 bReserved */
198 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
199 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
200 .wMaxPacketSize
= 512,
201 .extra
= (uint8_t[]) {
202 0x04, /* u8 bLength */
203 0x24, /* u8 bDescriptorType */
205 0x00, /* u8 bReserved */
208 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
209 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
210 .wMaxPacketSize
= 512,
211 .extra
= (uint8_t[]) {
212 0x04, /* u8 bLength */
213 0x24, /* u8 bDescriptorType */
215 0x00, /* u8 bReserved */
218 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
219 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
220 .wMaxPacketSize
= 512,
221 .extra
= (uint8_t[]) {
222 0x04, /* u8 bLength */
223 0x24, /* u8 bDescriptorType */
224 UAS_PIPE_ID_DATA_OUT
,
225 0x00, /* u8 bReserved */
231 static const USBDescIface desc_iface_super
= {
232 .bInterfaceNumber
= 0,
234 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
235 .bInterfaceSubClass
= 0x06, /* SCSI */
236 .bInterfaceProtocol
= 0x62, /* UAS */
237 .eps
= (USBDescEndpoint
[]) {
239 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
240 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
241 .wMaxPacketSize
= 1024,
243 .extra
= (uint8_t[]) {
244 0x04, /* u8 bLength */
245 0x24, /* u8 bDescriptorType */
247 0x00, /* u8 bReserved */
250 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
251 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
252 .wMaxPacketSize
= 1024,
254 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
255 .extra
= (uint8_t[]) {
256 0x04, /* u8 bLength */
257 0x24, /* u8 bDescriptorType */
259 0x00, /* u8 bReserved */
262 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
263 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
264 .wMaxPacketSize
= 1024,
266 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
267 .extra
= (uint8_t[]) {
268 0x04, /* u8 bLength */
269 0x24, /* u8 bDescriptorType */
271 0x00, /* u8 bReserved */
274 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
275 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
276 .wMaxPacketSize
= 1024,
278 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
279 .extra
= (uint8_t[]) {
280 0x04, /* u8 bLength */
281 0x24, /* u8 bDescriptorType */
282 UAS_PIPE_ID_DATA_OUT
,
283 0x00, /* u8 bReserved */
289 static const USBDescDevice desc_device_high
= {
291 .bMaxPacketSize0
= 64,
292 .bNumConfigurations
= 1,
293 .confs
= (USBDescConfig
[]) {
296 .bConfigurationValue
= 1,
297 .iConfiguration
= STR_CONFIG_HIGH
,
298 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
300 .ifs
= &desc_iface_high
,
305 static const USBDescDevice desc_device_super
= {
307 .bMaxPacketSize0
= 9,
308 .bNumConfigurations
= 1,
309 .confs
= (USBDescConfig
[]) {
312 .bConfigurationValue
= 1,
313 .iConfiguration
= STR_CONFIG_SUPER
,
314 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
316 .ifs
= &desc_iface_super
,
321 static const USBDesc desc
= {
323 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
326 .iManufacturer
= STR_MANUFACTURER
,
327 .iProduct
= STR_PRODUCT
,
328 .iSerialNumber
= STR_SERIALNUMBER
,
330 .high
= &desc_device_high
,
331 .super
= &desc_device_super
,
335 /* --------------------------------------------------------------------- */
337 static bool uas_using_streams(UASDevice
*uas
)
339 return uas
->dev
.speed
== USB_SPEED_SUPER
;
342 /* --------------------------------------------------------------------- */
344 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
346 UASStatus
*st
= g_new0(UASStatus
, 1);
348 st
->status
.hdr
.id
= id
;
349 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
350 st
->length
= sizeof(uas_iu_header
);
351 if (uas_using_streams(uas
)) {
357 static void usb_uas_send_status_bh(void *opaque
)
359 UASDevice
*uas
= opaque
;
363 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
364 if (uas_using_streams(uas
)) {
365 p
= uas
->status3
[st
->stream
];
366 uas
->status3
[st
->stream
] = NULL
;
375 usb_packet_copy(p
, &st
->status
, st
->length
);
376 QTAILQ_REMOVE(&uas
->results
, st
, next
);
379 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
380 usb_packet_complete(&uas
->dev
, p
);
384 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
386 USBPacket
*p
= uas_using_streams(uas
) ?
387 uas
->status3
[st
->stream
] : uas
->status2
;
389 st
->length
+= length
;
390 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
393 * Just schedule bh make sure any in-flight data transaction
394 * is finished before completing (sending) the status packet.
396 qemu_bh_schedule(uas
->status_bh
);
398 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
400 usb_wakeup(ep
, st
->stream
);
404 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
, uint8_t code
)
406 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
408 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
409 st
->status
.response
.response_code
= code
;
410 usb_uas_queue_status(uas
, st
, sizeof(uas_iu_response
));
413 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
415 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
418 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
419 st
->status
.sense
.status
= status
;
420 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
421 if (status
!= GOOD
) {
422 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
423 sizeof(st
->status
.sense
.sense_data
));
424 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
426 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
427 usb_uas_queue_status(req
->uas
, st
, len
);
430 static void usb_uas_queue_fake_sense(UASDevice
*uas
, uint16_t tag
,
431 struct SCSISense sense
)
433 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_SENSE
, tag
);
436 st
->status
.sense
.status
= CHECK_CONDITION
;
437 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
438 st
->status
.sense
.sense_data
[0] = 0x70;
439 st
->status
.sense
.sense_data
[2] = sense
.key
;
440 st
->status
.sense
.sense_data
[7] = 10;
441 st
->status
.sense
.sense_data
[12] = sense
.asc
;
442 st
->status
.sense
.sense_data
[13] = sense
.ascq
;
444 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
445 usb_uas_queue_status(uas
, st
, len
);
448 static void usb_uas_queue_read_ready(UASRequest
*req
)
450 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
453 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
454 usb_uas_queue_status(req
->uas
, st
, 0);
457 static void usb_uas_queue_write_ready(UASRequest
*req
)
459 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
462 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
463 usb_uas_queue_status(req
->uas
, st
, 0);
466 /* --------------------------------------------------------------------- */
468 static int usb_uas_get_lun(uint64_t lun64
)
470 return (lun64
>> 48) & 0xff;
473 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
475 if ((lun64
>> 56) != 0x00) {
478 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
481 static void usb_uas_complete_data_packet(UASRequest
*req
)
485 if (!req
->data_async
) {
490 req
->data_async
= false;
491 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
492 usb_packet_complete(&req
->uas
->dev
, p
);
495 static void usb_uas_copy_data(UASRequest
*req
)
499 length
= MIN(req
->buf_size
- req
->buf_off
,
500 req
->data
->iov
.size
- req
->data
->actual_length
);
501 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
502 req
->data
->actual_length
, req
->data
->iov
.size
,
503 req
->buf_off
, req
->buf_size
);
504 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
506 req
->buf_off
+= length
;
507 req
->data_off
+= length
;
509 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
510 usb_uas_complete_data_packet(req
);
512 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
515 scsi_req_continue(req
->req
);
519 static void usb_uas_start_next_transfer(UASDevice
*uas
)
523 if (uas_using_streams(uas
)) {
527 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
528 if (req
->active
|| req
->complete
) {
531 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
533 usb_uas_queue_read_ready(req
);
537 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
539 usb_uas_queue_write_ready(req
);
546 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_iu
*iu
)
550 req
= g_new0(UASRequest
, 1);
552 req
->tag
= be16_to_cpu(iu
->hdr
.tag
);
553 req
->lun
= be64_to_cpu(iu
->command
.lun
);
554 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
558 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
560 UASRequest
*req
= priv
;
561 UASDevice
*uas
= req
->uas
;
563 if (req
== uas
->datain2
) {
566 if (req
== uas
->dataout2
) {
567 uas
->dataout2
= NULL
;
569 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
571 usb_uas_start_next_transfer(uas
);
574 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
578 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
579 if (req
->tag
== tag
) {
586 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
588 UASRequest
*req
= r
->hba_private
;
590 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
594 usb_uas_copy_data(req
);
596 usb_uas_start_next_transfer(req
->uas
);
600 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
601 uint32_t status
, size_t resid
)
603 UASRequest
*req
= r
->hba_private
;
605 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
606 req
->complete
= true;
608 usb_uas_complete_data_packet(req
);
610 usb_uas_queue_sense(req
, status
);
611 scsi_req_unref(req
->req
);
614 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
616 UASRequest
*req
= r
->hba_private
;
618 /* FIXME: queue notification to status pipe? */
619 scsi_req_unref(req
->req
);
622 static const struct SCSIBusInfo usb_uas_scsi_info
= {
627 .transfer_data
= usb_uas_scsi_transfer_data
,
628 .complete
= usb_uas_scsi_command_complete
,
629 .cancel
= usb_uas_scsi_request_cancelled
,
630 .free_request
= usb_uas_scsi_free_request
,
633 /* --------------------------------------------------------------------- */
635 static void usb_uas_handle_reset(USBDevice
*dev
)
637 UASDevice
*uas
= USB_UAS(dev
);
638 UASRequest
*req
, *nreq
;
641 trace_usb_uas_reset(dev
->addr
);
642 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
643 scsi_req_cancel(req
->req
);
645 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
646 QTAILQ_REMOVE(&uas
->results
, st
, next
);
651 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
652 int request
, int value
, int index
, int length
, uint8_t *data
)
656 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
660 error_report("%s: unhandled control request (req 0x%x, val 0x%x, idx 0x%x",
661 __func__
, request
, value
, index
);
662 p
->status
= USB_RET_STALL
;
665 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
667 UASDevice
*uas
= USB_UAS(dev
);
668 UASRequest
*req
, *nreq
;
671 if (uas
->status2
== p
) {
673 qemu_bh_cancel(uas
->status_bh
);
676 if (uas_using_streams(uas
)) {
677 for (i
= 0; i
<= UAS_MAX_STREAMS
; i
++) {
678 if (uas
->status3
[i
] == p
) {
679 uas
->status3
[i
] = NULL
;
682 if (uas
->data3
[i
] == p
) {
683 uas
->data3
[i
] = NULL
;
688 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
689 if (req
->data
== p
) {
694 assert(!"canceled usb packet not found");
697 static void usb_uas_command(UASDevice
*uas
, uas_iu
*iu
)
701 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
703 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
706 req
= usb_uas_find_request(uas
, tag
);
710 req
= usb_uas_alloc_request(uas
, iu
);
711 if (req
->dev
== NULL
) {
715 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
716 usb_uas_get_lun(req
->lun
),
717 req
->lun
>> 32, req
->lun
& 0xffffffff);
718 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
719 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
720 req
->data
= uas
->data3
[req
->tag
];
721 req
->data_async
= true;
722 uas
->data3
[req
->tag
] = NULL
;
725 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
726 usb_uas_get_lun(req
->lun
),
727 iu
->command
.cdb
, req
);
728 if (uas
->requestlog
) {
729 scsi_req_print(req
->req
);
731 len
= scsi_req_enqueue(req
->req
);
733 req
->data_size
= len
;
734 scsi_req_continue(req
->req
);
739 usb_uas_queue_fake_sense(uas
, tag
, sense_code_INVALID_TAG
);
743 usb_uas_queue_fake_sense(uas
, tag
, sense_code_OVERLAPPED_COMMANDS
);
747 usb_uas_queue_fake_sense(uas
, tag
, sense_code_LUN_NOT_SUPPORTED
);
751 static void usb_uas_task(UASDevice
*uas
, uas_iu
*iu
)
753 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
754 uint64_t lun64
= be64_to_cpu(iu
->task
.lun
);
755 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
756 int lun
= usb_uas_get_lun(lun64
);
760 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
763 req
= usb_uas_find_request(uas
, be16_to_cpu(iu
->hdr
.tag
));
771 switch (iu
->task
.function
) {
772 case UAS_TMF_ABORT_TASK
:
773 task_tag
= be16_to_cpu(iu
->task
.task_tag
);
774 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
775 req
= usb_uas_find_request(uas
, task_tag
);
776 if (req
&& req
->dev
== dev
) {
777 scsi_req_cancel(req
->req
);
779 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
782 case UAS_TMF_LOGICAL_UNIT_RESET
:
783 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
784 qdev_reset_all(&dev
->qdev
);
785 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
789 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, iu
->task
.function
);
790 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
);
796 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
);
800 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
);
804 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
);
807 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
809 UASDevice
*uas
= USB_UAS(dev
);
816 case UAS_PIPE_ID_COMMAND
:
817 length
= MIN(sizeof(iu
), p
->iov
.size
);
818 usb_packet_copy(p
, &iu
, length
);
821 usb_uas_command(uas
, &iu
);
823 case UAS_UI_TASK_MGMT
:
824 usb_uas_task(uas
, &iu
);
827 error_report("%s: unknown command iu: id 0x%x",
828 __func__
, iu
.hdr
.id
);
829 p
->status
= USB_RET_STALL
;
833 case UAS_PIPE_ID_STATUS
:
835 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
836 if (st
->stream
== p
->stream
) {
841 assert(uas
->status3
[p
->stream
] == NULL
);
842 uas
->status3
[p
->stream
] = p
;
843 p
->status
= USB_RET_ASYNC
;
847 st
= QTAILQ_FIRST(&uas
->results
);
849 assert(uas
->status2
== NULL
);
851 p
->status
= USB_RET_ASYNC
;
855 usb_packet_copy(p
, &st
->status
, st
->length
);
856 QTAILQ_REMOVE(&uas
->results
, st
, next
);
859 case UAS_PIPE_ID_DATA_IN
:
860 case UAS_PIPE_ID_DATA_OUT
:
862 req
= usb_uas_find_request(uas
, p
->stream
);
864 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
865 ? uas
->datain2
: uas
->dataout2
;
869 assert(uas
->data3
[p
->stream
] == NULL
);
870 uas
->data3
[p
->stream
] = p
;
871 p
->status
= USB_RET_ASYNC
;
874 error_report("%s: no inflight request", __func__
);
875 p
->status
= USB_RET_STALL
;
879 scsi_req_ref(req
->req
);
881 usb_uas_copy_data(req
);
882 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
885 req
->data_async
= true;
886 p
->status
= USB_RET_ASYNC
;
888 scsi_req_unref(req
->req
);
889 usb_uas_start_next_transfer(uas
);
892 error_report("%s: invalid endpoint %d", __func__
, p
->ep
->nr
);
893 p
->status
= USB_RET_STALL
;
898 static void usb_uas_unrealize(USBDevice
*dev
)
900 UASDevice
*uas
= USB_UAS(dev
);
902 qemu_bh_delete(uas
->status_bh
);
905 static void usb_uas_realize(USBDevice
*dev
, Error
**errp
)
907 UASDevice
*uas
= USB_UAS(dev
);
908 DeviceState
*d
= DEVICE(dev
);
910 usb_desc_create_serial(dev
);
913 uas
->dev
.auto_attach
= 0;
916 QTAILQ_INIT(&uas
->results
);
917 QTAILQ_INIT(&uas
->requests
);
918 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
920 scsi_bus_new(&uas
->bus
, sizeof(uas
->bus
), DEVICE(dev
),
921 &usb_uas_scsi_info
, NULL
);
924 static const VMStateDescription vmstate_usb_uas
= {
927 .fields
= (VMStateField
[]) {
928 VMSTATE_USB_DEVICE(dev
, UASDevice
),
929 VMSTATE_END_OF_LIST()
933 static Property uas_properties
[] = {
934 DEFINE_PROP_UINT32("log-scsi-req", UASDevice
, requestlog
, 0),
935 DEFINE_PROP_END_OF_LIST(),
938 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
940 DeviceClass
*dc
= DEVICE_CLASS(klass
);
941 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
943 uc
->realize
= usb_uas_realize
;
944 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
945 uc
->usb_desc
= &desc
;
946 uc
->cancel_packet
= usb_uas_cancel_io
;
947 uc
->handle_attach
= usb_desc_attach
;
948 uc
->handle_reset
= usb_uas_handle_reset
;
949 uc
->handle_control
= usb_uas_handle_control
;
950 uc
->handle_data
= usb_uas_handle_data
;
951 uc
->unrealize
= usb_uas_unrealize
;
952 uc
->attached_settable
= true;
953 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
954 dc
->fw_name
= "storage";
955 dc
->vmsd
= &vmstate_usb_uas
;
956 device_class_set_props(dc
, uas_properties
);
959 static const TypeInfo uas_info
= {
960 .name
= TYPE_USB_UAS
,
961 .parent
= TYPE_USB_DEVICE
,
962 .instance_size
= sizeof(UASDevice
),
963 .class_init
= usb_uas_class_initfn
,
966 static void usb_uas_register_types(void)
968 type_register_static(&uas_info
);
971 type_init(usb_uas_register_types
)