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-common.h"
13 #include "qemu/option.h"
14 #include "qemu/config-file.h"
18 #include "hw/usb/desc.h"
19 #include "hw/scsi/scsi.h"
20 #include "block/scsi.h"
22 /* --------------------------------------------------------------------- */
24 #define UAS_UI_COMMAND 0x01
25 #define UAS_UI_SENSE 0x03
26 #define UAS_UI_RESPONSE 0x04
27 #define UAS_UI_TASK_MGMT 0x05
28 #define UAS_UI_READ_READY 0x06
29 #define UAS_UI_WRITE_READY 0x07
31 #define UAS_RC_TMF_COMPLETE 0x00
32 #define UAS_RC_INVALID_INFO_UNIT 0x02
33 #define UAS_RC_TMF_NOT_SUPPORTED 0x04
34 #define UAS_RC_TMF_FAILED 0x05
35 #define UAS_RC_TMF_SUCCEEDED 0x08
36 #define UAS_RC_INCORRECT_LUN 0x09
37 #define UAS_RC_OVERLAPPED_TAG 0x0a
39 #define UAS_TMF_ABORT_TASK 0x01
40 #define UAS_TMF_ABORT_TASK_SET 0x02
41 #define UAS_TMF_CLEAR_TASK_SET 0x04
42 #define UAS_TMF_LOGICAL_UNIT_RESET 0x08
43 #define UAS_TMF_I_T_NEXUS_RESET 0x10
44 #define UAS_TMF_CLEAR_ACA 0x40
45 #define UAS_TMF_QUERY_TASK 0x80
46 #define UAS_TMF_QUERY_TASK_SET 0x81
47 #define UAS_TMF_QUERY_ASYNC_EVENT 0x82
49 #define UAS_PIPE_ID_COMMAND 0x01
50 #define UAS_PIPE_ID_STATUS 0x02
51 #define UAS_PIPE_ID_DATA_IN 0x03
52 #define UAS_PIPE_ID_DATA_OUT 0x04
58 } QEMU_PACKED uas_ui_header
;
61 uint8_t prio_taskattr
; /* 6:3 priority, 2:0 task attribute */
63 uint8_t add_cdb_length
; /* 7:2 additional adb length (dwords) */
68 } QEMU_PACKED uas_ui_command
;
71 uint16_t status_qualifier
;
74 uint16_t sense_length
;
75 uint8_t sense_data
[18];
76 } QEMU_PACKED uas_ui_sense
;
79 uint16_t add_response_info
;
80 uint8_t response_code
;
81 } QEMU_PACKED uas_ui_response
;
88 } QEMU_PACKED uas_ui_task_mgmt
;
93 uas_ui_command command
;
95 uas_ui_task_mgmt task
;
96 uas_ui_response response
;
100 /* --------------------------------------------------------------------- */
102 #define UAS_STREAM_BM_ATTR 4
103 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR)
105 typedef struct UASDevice UASDevice
;
106 typedef struct UASRequest UASRequest
;
107 typedef struct UASStatus UASStatus
;
113 QTAILQ_HEAD(, UASStatus
) results
;
114 QTAILQ_HEAD(, UASRequest
) requests
;
119 UASRequest
*dataout2
;
122 USBPacket
*data3
[UAS_MAX_STREAMS
];
123 USBPacket
*status3
[UAS_MAX_STREAMS
];
140 QTAILQ_ENTRY(UASRequest
) next
;
147 QTAILQ_ENTRY(UASStatus
) next
;
150 /* --------------------------------------------------------------------- */
153 STR_MANUFACTURER
= 1,
160 static const USBDescStrings desc_strings
= {
161 [STR_MANUFACTURER
] = "QEMU",
162 [STR_PRODUCT
] = "USB Attached SCSI HBA",
163 [STR_SERIALNUMBER
] = "27842",
164 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
165 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
168 static const USBDescIface desc_iface_high
= {
169 .bInterfaceNumber
= 0,
171 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
172 .bInterfaceSubClass
= 0x06, /* SCSI */
173 .bInterfaceProtocol
= 0x62, /* UAS */
174 .eps
= (USBDescEndpoint
[]) {
176 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
177 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
178 .wMaxPacketSize
= 512,
179 .extra
= (uint8_t[]) {
180 0x04, /* u8 bLength */
181 0x24, /* u8 bDescriptorType */
183 0x00, /* u8 bReserved */
186 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
187 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
188 .wMaxPacketSize
= 512,
189 .extra
= (uint8_t[]) {
190 0x04, /* u8 bLength */
191 0x24, /* u8 bDescriptorType */
193 0x00, /* u8 bReserved */
196 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
197 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
198 .wMaxPacketSize
= 512,
199 .extra
= (uint8_t[]) {
200 0x04, /* u8 bLength */
201 0x24, /* u8 bDescriptorType */
203 0x00, /* u8 bReserved */
206 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
207 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
208 .wMaxPacketSize
= 512,
209 .extra
= (uint8_t[]) {
210 0x04, /* u8 bLength */
211 0x24, /* u8 bDescriptorType */
212 UAS_PIPE_ID_DATA_OUT
,
213 0x00, /* u8 bReserved */
219 static const USBDescIface desc_iface_super
= {
220 .bInterfaceNumber
= 0,
222 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
223 .bInterfaceSubClass
= 0x06, /* SCSI */
224 .bInterfaceProtocol
= 0x62, /* UAS */
225 .eps
= (USBDescEndpoint
[]) {
227 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
228 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
229 .wMaxPacketSize
= 1024,
231 .extra
= (uint8_t[]) {
232 0x04, /* u8 bLength */
233 0x24, /* u8 bDescriptorType */
235 0x00, /* u8 bReserved */
238 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
239 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
240 .wMaxPacketSize
= 1024,
242 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
243 .extra
= (uint8_t[]) {
244 0x04, /* u8 bLength */
245 0x24, /* u8 bDescriptorType */
247 0x00, /* u8 bReserved */
250 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
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_OUT
| UAS_PIPE_ID_DATA_OUT
,
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 */
270 UAS_PIPE_ID_DATA_OUT
,
271 0x00, /* u8 bReserved */
277 static const USBDescDevice desc_device_high
= {
279 .bMaxPacketSize0
= 64,
280 .bNumConfigurations
= 1,
281 .confs
= (USBDescConfig
[]) {
284 .bConfigurationValue
= 1,
285 .iConfiguration
= STR_CONFIG_HIGH
,
286 .bmAttributes
= 0xc0,
288 .ifs
= &desc_iface_high
,
293 static const USBDescDevice desc_device_super
= {
295 .bMaxPacketSize0
= 64,
296 .bNumConfigurations
= 1,
297 .confs
= (USBDescConfig
[]) {
300 .bConfigurationValue
= 1,
301 .iConfiguration
= STR_CONFIG_SUPER
,
302 .bmAttributes
= 0xc0,
304 .ifs
= &desc_iface_super
,
309 static const USBDesc desc
= {
311 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
314 .iManufacturer
= STR_MANUFACTURER
,
315 .iProduct
= STR_PRODUCT
,
316 .iSerialNumber
= STR_SERIALNUMBER
,
318 .high
= &desc_device_high
,
319 .super
= &desc_device_super
,
323 /* --------------------------------------------------------------------- */
325 static bool uas_using_streams(UASDevice
*uas
)
327 return uas
->dev
.speed
== USB_SPEED_SUPER
;
330 /* --------------------------------------------------------------------- */
332 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
334 UASStatus
*st
= g_new0(UASStatus
, 1);
336 st
->status
.hdr
.id
= id
;
337 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
338 st
->length
= sizeof(uas_ui_header
);
339 if (uas_using_streams(uas
)) {
345 static void usb_uas_send_status_bh(void *opaque
)
347 UASDevice
*uas
= opaque
;
351 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
352 if (uas_using_streams(uas
)) {
353 p
= uas
->status3
[st
->stream
];
354 uas
->status3
[st
->stream
] = NULL
;
363 usb_packet_copy(p
, &st
->status
, st
->length
);
364 QTAILQ_REMOVE(&uas
->results
, st
, next
);
367 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
368 usb_packet_complete(&uas
->dev
, p
);
372 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
374 USBPacket
*p
= uas_using_streams(uas
) ?
375 uas
->status3
[st
->stream
] : uas
->status2
;
377 st
->length
+= length
;
378 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
381 * Just schedule bh make sure any in-flight data transaction
382 * is finished before completing (sending) the status packet.
384 qemu_bh_schedule(uas
->status_bh
);
386 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
388 usb_wakeup(ep
, st
->stream
);
392 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
,
393 uint8_t code
, uint16_t add_info
)
395 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
397 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
398 st
->status
.response
.response_code
= code
;
399 st
->status
.response
.add_response_info
= cpu_to_be16(add_info
);
400 usb_uas_queue_status(uas
, st
, sizeof(uas_ui_response
));
403 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
405 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
408 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
409 st
->status
.sense
.status
= status
;
410 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
411 if (status
!= GOOD
) {
412 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
413 sizeof(st
->status
.sense
.sense_data
));
414 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
416 len
= sizeof(uas_ui_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
417 usb_uas_queue_status(req
->uas
, st
, len
);
420 static void usb_uas_queue_read_ready(UASRequest
*req
)
422 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
425 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
426 usb_uas_queue_status(req
->uas
, st
, 0);
429 static void usb_uas_queue_write_ready(UASRequest
*req
)
431 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
434 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
435 usb_uas_queue_status(req
->uas
, st
, 0);
438 /* --------------------------------------------------------------------- */
440 static int usb_uas_get_lun(uint64_t lun64
)
442 return (lun64
>> 48) & 0xff;
445 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
447 if ((lun64
>> 56) != 0x00) {
450 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
453 static void usb_uas_complete_data_packet(UASRequest
*req
)
457 if (!req
->data_async
) {
462 req
->data_async
= false;
463 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
464 usb_packet_complete(&req
->uas
->dev
, p
);
467 static void usb_uas_copy_data(UASRequest
*req
)
471 length
= MIN(req
->buf_size
- req
->buf_off
,
472 req
->data
->iov
.size
- req
->data
->actual_length
);
473 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
474 req
->data
->actual_length
, req
->data
->iov
.size
,
475 req
->buf_off
, req
->buf_size
);
476 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
478 req
->buf_off
+= length
;
479 req
->data_off
+= length
;
481 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
482 usb_uas_complete_data_packet(req
);
484 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
487 scsi_req_continue(req
->req
);
491 static void usb_uas_start_next_transfer(UASDevice
*uas
)
495 if (uas_using_streams(uas
)) {
499 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
500 if (req
->active
|| req
->complete
) {
503 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
505 usb_uas_queue_read_ready(req
);
509 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
511 usb_uas_queue_write_ready(req
);
518 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_ui
*ui
)
522 req
= g_new0(UASRequest
, 1);
524 req
->tag
= be16_to_cpu(ui
->hdr
.tag
);
525 req
->lun
= be64_to_cpu(ui
->command
.lun
);
526 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
530 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
532 UASRequest
*req
= priv
;
533 UASDevice
*uas
= req
->uas
;
535 if (req
== uas
->datain2
) {
538 if (req
== uas
->dataout2
) {
539 uas
->dataout2
= NULL
;
541 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
543 usb_uas_start_next_transfer(uas
);
546 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
550 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
551 if (req
->tag
== tag
) {
558 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
560 UASRequest
*req
= r
->hba_private
;
562 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
566 usb_uas_copy_data(req
);
568 usb_uas_start_next_transfer(req
->uas
);
572 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
573 uint32_t status
, size_t resid
)
575 UASRequest
*req
= r
->hba_private
;
577 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
578 req
->complete
= true;
580 usb_uas_complete_data_packet(req
);
582 usb_uas_queue_sense(req
, status
);
583 scsi_req_unref(req
->req
);
586 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
588 UASRequest
*req
= r
->hba_private
;
590 /* FIXME: queue notification to status pipe? */
591 scsi_req_unref(req
->req
);
594 static const struct SCSIBusInfo usb_uas_scsi_info
= {
599 .transfer_data
= usb_uas_scsi_transfer_data
,
600 .complete
= usb_uas_scsi_command_complete
,
601 .cancel
= usb_uas_scsi_request_cancelled
,
602 .free_request
= usb_uas_scsi_free_request
,
605 /* --------------------------------------------------------------------- */
607 static void usb_uas_handle_reset(USBDevice
*dev
)
609 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
610 UASRequest
*req
, *nreq
;
613 trace_usb_uas_reset(dev
->addr
);
614 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
615 scsi_req_cancel(req
->req
);
617 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
618 QTAILQ_REMOVE(&uas
->results
, st
, next
);
623 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
624 int request
, int value
, int index
, int length
, uint8_t *data
)
628 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
632 fprintf(stderr
, "%s: unhandled control request\n", __func__
);
633 p
->status
= USB_RET_STALL
;
636 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
638 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
639 UASRequest
*req
, *nreq
;
642 if (uas
->status2
== p
) {
644 qemu_bh_cancel(uas
->status_bh
);
647 if (uas_using_streams(uas
)) {
648 for (i
= 0; i
< UAS_MAX_STREAMS
; i
++) {
649 if (uas
->status3
[i
] == p
) {
650 uas
->status3
[i
] = NULL
;
653 if (uas
->data3
[i
] == p
) {
654 uas
->data3
[i
] = NULL
;
659 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
660 if (req
->data
== p
) {
665 assert(!"canceled usb packet not found");
668 static void usb_uas_command(UASDevice
*uas
, uas_ui
*ui
)
673 req
= usb_uas_find_request(uas
, be16_to_cpu(ui
->hdr
.tag
));
677 req
= usb_uas_alloc_request(uas
, ui
);
678 if (req
->dev
== NULL
) {
682 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
683 usb_uas_get_lun(req
->lun
),
684 req
->lun
>> 32, req
->lun
& 0xffffffff);
685 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
686 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
687 req
->data
= uas
->data3
[req
->tag
];
688 req
->data_async
= true;
689 uas
->data3
[req
->tag
] = NULL
;
692 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
693 usb_uas_get_lun(req
->lun
),
694 ui
->command
.cdb
, req
);
696 scsi_req_print(req
->req
);
698 len
= scsi_req_enqueue(req
->req
);
700 req
->data_size
= len
;
701 scsi_req_continue(req
->req
);
706 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
, 0);
711 * FIXME: Seems to upset linux, is this wrong?
712 * NOTE: Happens only with no scsi devices at the bus, not sure
713 * this is a valid UAS setup in the first place.
715 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_INVALID_INFO_UNIT
, 0);
719 static void usb_uas_task(UASDevice
*uas
, uas_ui
*ui
)
721 uint16_t tag
= be16_to_cpu(ui
->hdr
.tag
);
722 uint64_t lun64
= be64_to_cpu(ui
->task
.lun
);
723 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
724 int lun
= usb_uas_get_lun(lun64
);
728 req
= usb_uas_find_request(uas
, be16_to_cpu(ui
->hdr
.tag
));
733 switch (ui
->task
.function
) {
734 case UAS_TMF_ABORT_TASK
:
735 task_tag
= be16_to_cpu(ui
->task
.task_tag
);
736 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
740 if (dev
->lun
!= lun
) {
743 req
= usb_uas_find_request(uas
, task_tag
);
744 if (req
&& req
->dev
== dev
) {
745 scsi_req_cancel(req
->req
);
747 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
, 0);
750 case UAS_TMF_LOGICAL_UNIT_RESET
:
751 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
755 if (dev
->lun
!= lun
) {
758 qdev_reset_all(&dev
->qdev
);
759 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
, 0);
763 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, ui
->task
.function
);
764 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
, 0);
770 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
, 0);
774 /* FIXME: correct? [see long comment in usb_uas_command()] */
775 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
, 0);
779 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
, 0);
782 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
784 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
791 case UAS_PIPE_ID_COMMAND
:
792 length
= MIN(sizeof(ui
), p
->iov
.size
);
793 usb_packet_copy(p
, &ui
, length
);
796 usb_uas_command(uas
, &ui
);
798 case UAS_UI_TASK_MGMT
:
799 usb_uas_task(uas
, &ui
);
802 fprintf(stderr
, "%s: unknown command ui: id 0x%x\n",
803 __func__
, ui
.hdr
.id
);
804 p
->status
= USB_RET_STALL
;
808 case UAS_PIPE_ID_STATUS
:
810 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
811 if (st
->stream
== p
->stream
) {
816 assert(uas
->status3
[p
->stream
] == NULL
);
817 uas
->status3
[p
->stream
] = p
;
818 p
->status
= USB_RET_ASYNC
;
822 st
= QTAILQ_FIRST(&uas
->results
);
824 assert(uas
->status2
== NULL
);
826 p
->status
= USB_RET_ASYNC
;
830 usb_packet_copy(p
, &st
->status
, st
->length
);
831 QTAILQ_REMOVE(&uas
->results
, st
, next
);
834 case UAS_PIPE_ID_DATA_IN
:
835 case UAS_PIPE_ID_DATA_OUT
:
837 req
= usb_uas_find_request(uas
, p
->stream
);
839 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
840 ? uas
->datain2
: uas
->dataout2
;
844 assert(uas
->data3
[p
->stream
] == NULL
);
845 uas
->data3
[p
->stream
] = p
;
846 p
->status
= USB_RET_ASYNC
;
849 fprintf(stderr
, "%s: no inflight request\n", __func__
);
850 p
->status
= USB_RET_STALL
;
854 scsi_req_ref(req
->req
);
856 usb_uas_copy_data(req
);
857 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
860 req
->data_async
= true;
861 p
->status
= USB_RET_ASYNC
;
863 scsi_req_unref(req
->req
);
864 usb_uas_start_next_transfer(uas
);
867 fprintf(stderr
, "%s: invalid endpoint %d\n", __func__
, p
->ep
->nr
);
868 p
->status
= USB_RET_STALL
;
873 static void usb_uas_handle_destroy(USBDevice
*dev
)
875 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
877 qemu_bh_delete(uas
->status_bh
);
880 static int usb_uas_init(USBDevice
*dev
)
882 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
884 usb_desc_create_serial(dev
);
887 QTAILQ_INIT(&uas
->results
);
888 QTAILQ_INIT(&uas
->requests
);
889 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
891 scsi_bus_new(&uas
->bus
, &uas
->dev
.qdev
, &usb_uas_scsi_info
);
896 static const VMStateDescription vmstate_usb_uas
= {
899 .fields
= (VMStateField
[]) {
900 VMSTATE_USB_DEVICE(dev
, UASDevice
),
901 VMSTATE_END_OF_LIST()
905 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
907 DeviceClass
*dc
= DEVICE_CLASS(klass
);
908 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
910 uc
->init
= usb_uas_init
;
911 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
912 uc
->usb_desc
= &desc
;
913 uc
->cancel_packet
= usb_uas_cancel_io
;
914 uc
->handle_attach
= usb_desc_attach
;
915 uc
->handle_reset
= usb_uas_handle_reset
;
916 uc
->handle_control
= usb_uas_handle_control
;
917 uc
->handle_data
= usb_uas_handle_data
;
918 uc
->handle_destroy
= usb_uas_handle_destroy
;
919 dc
->fw_name
= "storage";
920 dc
->vmsd
= &vmstate_usb_uas
;
923 static const TypeInfo uas_info
= {
925 .parent
= TYPE_USB_DEVICE
,
926 .instance_size
= sizeof(UASDevice
),
927 .class_init
= usb_uas_class_initfn
,
930 static void usb_uas_register_types(void)
932 type_register_static(&uas_info
);
935 type_init(usb_uas_register_types
)