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"
16 #include "qemu/error-report.h"
19 #include "hw/usb/desc.h"
20 #include "hw/scsi/scsi.h"
21 #include "block/scsi.h"
23 /* --------------------------------------------------------------------- */
25 #define UAS_UI_COMMAND 0x01
26 #define UAS_UI_SENSE 0x03
27 #define UAS_UI_RESPONSE 0x04
28 #define UAS_UI_TASK_MGMT 0x05
29 #define UAS_UI_READ_READY 0x06
30 #define UAS_UI_WRITE_READY 0x07
32 #define UAS_RC_TMF_COMPLETE 0x00
33 #define UAS_RC_INVALID_INFO_UNIT 0x02
34 #define UAS_RC_TMF_NOT_SUPPORTED 0x04
35 #define UAS_RC_TMF_FAILED 0x05
36 #define UAS_RC_TMF_SUCCEEDED 0x08
37 #define UAS_RC_INCORRECT_LUN 0x09
38 #define UAS_RC_OVERLAPPED_TAG 0x0a
40 #define UAS_TMF_ABORT_TASK 0x01
41 #define UAS_TMF_ABORT_TASK_SET 0x02
42 #define UAS_TMF_CLEAR_TASK_SET 0x04
43 #define UAS_TMF_LOGICAL_UNIT_RESET 0x08
44 #define UAS_TMF_I_T_NEXUS_RESET 0x10
45 #define UAS_TMF_CLEAR_ACA 0x40
46 #define UAS_TMF_QUERY_TASK 0x80
47 #define UAS_TMF_QUERY_TASK_SET 0x81
48 #define UAS_TMF_QUERY_ASYNC_EVENT 0x82
50 #define UAS_PIPE_ID_COMMAND 0x01
51 #define UAS_PIPE_ID_STATUS 0x02
52 #define UAS_PIPE_ID_DATA_IN 0x03
53 #define UAS_PIPE_ID_DATA_OUT 0x04
59 } QEMU_PACKED uas_iu_header
;
62 uint8_t prio_taskattr
; /* 6:3 priority, 2:0 task attribute */
64 uint8_t add_cdb_length
; /* 7:2 additional adb length (dwords) */
69 } QEMU_PACKED uas_iu_command
;
72 uint16_t status_qualifier
;
75 uint16_t sense_length
;
76 uint8_t sense_data
[18];
77 } QEMU_PACKED uas_iu_sense
;
80 uint8_t add_response_info
[3];
81 uint8_t response_code
;
82 } QEMU_PACKED uas_iu_response
;
89 } QEMU_PACKED uas_iu_task_mgmt
;
94 uas_iu_command command
;
96 uas_iu_task_mgmt task
;
97 uas_iu_response response
;
101 /* --------------------------------------------------------------------- */
103 #define UAS_STREAM_BM_ATTR 4
104 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR)
106 typedef struct UASDevice UASDevice
;
107 typedef struct UASRequest UASRequest
;
108 typedef struct UASStatus UASStatus
;
114 QTAILQ_HEAD(, UASStatus
) results
;
115 QTAILQ_HEAD(, UASRequest
) requests
;
123 UASRequest
*dataout2
;
126 USBPacket
*data3
[UAS_MAX_STREAMS
+ 1];
127 USBPacket
*status3
[UAS_MAX_STREAMS
+ 1];
130 #define TYPE_USB_UAS "usb-uas"
131 #define USB_UAS(obj) OBJECT_CHECK(UASDevice, (obj), TYPE_USB_UAS)
147 QTAILQ_ENTRY(UASRequest
) next
;
154 QTAILQ_ENTRY(UASStatus
) next
;
157 /* --------------------------------------------------------------------- */
160 STR_MANUFACTURER
= 1,
167 static const USBDescStrings desc_strings
= {
168 [STR_MANUFACTURER
] = "QEMU",
169 [STR_PRODUCT
] = "USB Attached SCSI HBA",
170 [STR_SERIALNUMBER
] = "27842",
171 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
172 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
175 static const USBDescIface desc_iface_high
= {
176 .bInterfaceNumber
= 0,
178 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
179 .bInterfaceSubClass
= 0x06, /* SCSI */
180 .bInterfaceProtocol
= 0x62, /* UAS */
181 .eps
= (USBDescEndpoint
[]) {
183 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
184 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
185 .wMaxPacketSize
= 512,
186 .extra
= (uint8_t[]) {
187 0x04, /* u8 bLength */
188 0x24, /* u8 bDescriptorType */
190 0x00, /* u8 bReserved */
193 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
194 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
195 .wMaxPacketSize
= 512,
196 .extra
= (uint8_t[]) {
197 0x04, /* u8 bLength */
198 0x24, /* u8 bDescriptorType */
200 0x00, /* u8 bReserved */
203 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
204 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
205 .wMaxPacketSize
= 512,
206 .extra
= (uint8_t[]) {
207 0x04, /* u8 bLength */
208 0x24, /* u8 bDescriptorType */
210 0x00, /* u8 bReserved */
213 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
214 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
215 .wMaxPacketSize
= 512,
216 .extra
= (uint8_t[]) {
217 0x04, /* u8 bLength */
218 0x24, /* u8 bDescriptorType */
219 UAS_PIPE_ID_DATA_OUT
,
220 0x00, /* u8 bReserved */
226 static const USBDescIface desc_iface_super
= {
227 .bInterfaceNumber
= 0,
229 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
230 .bInterfaceSubClass
= 0x06, /* SCSI */
231 .bInterfaceProtocol
= 0x62, /* UAS */
232 .eps
= (USBDescEndpoint
[]) {
234 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
235 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
236 .wMaxPacketSize
= 1024,
238 .extra
= (uint8_t[]) {
239 0x04, /* u8 bLength */
240 0x24, /* u8 bDescriptorType */
242 0x00, /* u8 bReserved */
245 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
246 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
247 .wMaxPacketSize
= 1024,
249 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
250 .extra
= (uint8_t[]) {
251 0x04, /* u8 bLength */
252 0x24, /* u8 bDescriptorType */
254 0x00, /* u8 bReserved */
257 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
258 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
259 .wMaxPacketSize
= 1024,
261 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
262 .extra
= (uint8_t[]) {
263 0x04, /* u8 bLength */
264 0x24, /* u8 bDescriptorType */
266 0x00, /* u8 bReserved */
269 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
270 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
271 .wMaxPacketSize
= 1024,
273 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
274 .extra
= (uint8_t[]) {
275 0x04, /* u8 bLength */
276 0x24, /* u8 bDescriptorType */
277 UAS_PIPE_ID_DATA_OUT
,
278 0x00, /* u8 bReserved */
284 static const USBDescDevice desc_device_high
= {
286 .bMaxPacketSize0
= 64,
287 .bNumConfigurations
= 1,
288 .confs
= (USBDescConfig
[]) {
291 .bConfigurationValue
= 1,
292 .iConfiguration
= STR_CONFIG_HIGH
,
293 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
295 .ifs
= &desc_iface_high
,
300 static const USBDescDevice desc_device_super
= {
302 .bMaxPacketSize0
= 64,
303 .bNumConfigurations
= 1,
304 .confs
= (USBDescConfig
[]) {
307 .bConfigurationValue
= 1,
308 .iConfiguration
= STR_CONFIG_SUPER
,
309 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
311 .ifs
= &desc_iface_super
,
316 static const USBDesc desc
= {
318 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
321 .iManufacturer
= STR_MANUFACTURER
,
322 .iProduct
= STR_PRODUCT
,
323 .iSerialNumber
= STR_SERIALNUMBER
,
325 .high
= &desc_device_high
,
326 .super
= &desc_device_super
,
330 /* --------------------------------------------------------------------- */
332 static bool uas_using_streams(UASDevice
*uas
)
334 return uas
->dev
.speed
== USB_SPEED_SUPER
;
337 /* --------------------------------------------------------------------- */
339 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
341 UASStatus
*st
= g_new0(UASStatus
, 1);
343 st
->status
.hdr
.id
= id
;
344 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
345 st
->length
= sizeof(uas_iu_header
);
346 if (uas_using_streams(uas
)) {
352 static void usb_uas_send_status_bh(void *opaque
)
354 UASDevice
*uas
= opaque
;
358 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
359 if (uas_using_streams(uas
)) {
360 p
= uas
->status3
[st
->stream
];
361 uas
->status3
[st
->stream
] = NULL
;
370 usb_packet_copy(p
, &st
->status
, st
->length
);
371 QTAILQ_REMOVE(&uas
->results
, st
, next
);
374 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
375 usb_packet_complete(&uas
->dev
, p
);
379 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
381 USBPacket
*p
= uas_using_streams(uas
) ?
382 uas
->status3
[st
->stream
] : uas
->status2
;
384 st
->length
+= length
;
385 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
388 * Just schedule bh make sure any in-flight data transaction
389 * is finished before completing (sending) the status packet.
391 qemu_bh_schedule(uas
->status_bh
);
393 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
395 usb_wakeup(ep
, st
->stream
);
399 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
, uint8_t code
)
401 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
403 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
404 st
->status
.response
.response_code
= code
;
405 usb_uas_queue_status(uas
, st
, sizeof(uas_iu_response
));
408 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
410 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
413 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
414 st
->status
.sense
.status
= status
;
415 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
416 if (status
!= GOOD
) {
417 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
418 sizeof(st
->status
.sense
.sense_data
));
419 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
421 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
422 usb_uas_queue_status(req
->uas
, st
, len
);
425 static void usb_uas_queue_fake_sense(UASDevice
*uas
, uint16_t tag
,
426 struct SCSISense sense
)
428 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_SENSE
, tag
);
431 st
->status
.sense
.status
= CHECK_CONDITION
;
432 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
433 st
->status
.sense
.sense_data
[0] = 0x70;
434 st
->status
.sense
.sense_data
[2] = sense
.key
;
435 st
->status
.sense
.sense_data
[7] = 10;
436 st
->status
.sense
.sense_data
[12] = sense
.asc
;
437 st
->status
.sense
.sense_data
[13] = sense
.ascq
;
439 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
440 usb_uas_queue_status(uas
, st
, len
);
443 static void usb_uas_queue_read_ready(UASRequest
*req
)
445 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
448 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
449 usb_uas_queue_status(req
->uas
, st
, 0);
452 static void usb_uas_queue_write_ready(UASRequest
*req
)
454 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
457 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
458 usb_uas_queue_status(req
->uas
, st
, 0);
461 /* --------------------------------------------------------------------- */
463 static int usb_uas_get_lun(uint64_t lun64
)
465 return (lun64
>> 48) & 0xff;
468 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
470 if ((lun64
>> 56) != 0x00) {
473 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
476 static void usb_uas_complete_data_packet(UASRequest
*req
)
480 if (!req
->data_async
) {
485 req
->data_async
= false;
486 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
487 usb_packet_complete(&req
->uas
->dev
, p
);
490 static void usb_uas_copy_data(UASRequest
*req
)
494 length
= MIN(req
->buf_size
- req
->buf_off
,
495 req
->data
->iov
.size
- req
->data
->actual_length
);
496 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
497 req
->data
->actual_length
, req
->data
->iov
.size
,
498 req
->buf_off
, req
->buf_size
);
499 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
501 req
->buf_off
+= length
;
502 req
->data_off
+= length
;
504 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
505 usb_uas_complete_data_packet(req
);
507 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
510 scsi_req_continue(req
->req
);
514 static void usb_uas_start_next_transfer(UASDevice
*uas
)
518 if (uas_using_streams(uas
)) {
522 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
523 if (req
->active
|| req
->complete
) {
526 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
528 usb_uas_queue_read_ready(req
);
532 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
534 usb_uas_queue_write_ready(req
);
541 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_iu
*iu
)
545 req
= g_new0(UASRequest
, 1);
547 req
->tag
= be16_to_cpu(iu
->hdr
.tag
);
548 req
->lun
= be64_to_cpu(iu
->command
.lun
);
549 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
553 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
555 UASRequest
*req
= priv
;
556 UASDevice
*uas
= req
->uas
;
558 if (req
== uas
->datain2
) {
561 if (req
== uas
->dataout2
) {
562 uas
->dataout2
= NULL
;
564 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
566 usb_uas_start_next_transfer(uas
);
569 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
573 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
574 if (req
->tag
== tag
) {
581 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
583 UASRequest
*req
= r
->hba_private
;
585 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
589 usb_uas_copy_data(req
);
591 usb_uas_start_next_transfer(req
->uas
);
595 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
596 uint32_t status
, size_t resid
)
598 UASRequest
*req
= r
->hba_private
;
600 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
601 req
->complete
= true;
603 usb_uas_complete_data_packet(req
);
605 usb_uas_queue_sense(req
, status
);
606 scsi_req_unref(req
->req
);
609 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
611 UASRequest
*req
= r
->hba_private
;
613 /* FIXME: queue notification to status pipe? */
614 scsi_req_unref(req
->req
);
617 static const struct SCSIBusInfo usb_uas_scsi_info
= {
622 .transfer_data
= usb_uas_scsi_transfer_data
,
623 .complete
= usb_uas_scsi_command_complete
,
624 .cancel
= usb_uas_scsi_request_cancelled
,
625 .free_request
= usb_uas_scsi_free_request
,
628 /* --------------------------------------------------------------------- */
630 static void usb_uas_handle_reset(USBDevice
*dev
)
632 UASDevice
*uas
= USB_UAS(dev
);
633 UASRequest
*req
, *nreq
;
636 trace_usb_uas_reset(dev
->addr
);
637 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
638 scsi_req_cancel(req
->req
);
640 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
641 QTAILQ_REMOVE(&uas
->results
, st
, next
);
646 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
647 int request
, int value
, int index
, int length
, uint8_t *data
)
651 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
655 error_report("%s: unhandled control request", __func__
);
656 p
->status
= USB_RET_STALL
;
659 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
661 UASDevice
*uas
= USB_UAS(dev
);
662 UASRequest
*req
, *nreq
;
665 if (uas
->status2
== p
) {
667 qemu_bh_cancel(uas
->status_bh
);
670 if (uas_using_streams(uas
)) {
671 for (i
= 0; i
<= UAS_MAX_STREAMS
; i
++) {
672 if (uas
->status3
[i
] == p
) {
673 uas
->status3
[i
] = NULL
;
676 if (uas
->data3
[i
] == p
) {
677 uas
->data3
[i
] = NULL
;
682 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
683 if (req
->data
== p
) {
688 assert(!"canceled usb packet not found");
691 static void usb_uas_command(UASDevice
*uas
, uas_iu
*iu
)
695 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
697 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
700 req
= usb_uas_find_request(uas
, tag
);
704 req
= usb_uas_alloc_request(uas
, iu
);
705 if (req
->dev
== NULL
) {
709 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
710 usb_uas_get_lun(req
->lun
),
711 req
->lun
>> 32, req
->lun
& 0xffffffff);
712 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
713 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
714 req
->data
= uas
->data3
[req
->tag
];
715 req
->data_async
= true;
716 uas
->data3
[req
->tag
] = NULL
;
719 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
720 usb_uas_get_lun(req
->lun
),
721 iu
->command
.cdb
, req
);
722 if (uas
->requestlog
) {
723 scsi_req_print(req
->req
);
725 len
= scsi_req_enqueue(req
->req
);
727 req
->data_size
= len
;
728 scsi_req_continue(req
->req
);
733 usb_uas_queue_fake_sense(uas
, tag
, sense_code_INVALID_TAG
);
737 usb_uas_queue_fake_sense(uas
, tag
, sense_code_OVERLAPPED_COMMANDS
);
741 usb_uas_queue_fake_sense(uas
, tag
, sense_code_LUN_NOT_SUPPORTED
);
745 static void usb_uas_task(UASDevice
*uas
, uas_iu
*iu
)
747 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
748 uint64_t lun64
= be64_to_cpu(iu
->task
.lun
);
749 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
750 int lun
= usb_uas_get_lun(lun64
);
754 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
757 req
= usb_uas_find_request(uas
, be16_to_cpu(iu
->hdr
.tag
));
765 switch (iu
->task
.function
) {
766 case UAS_TMF_ABORT_TASK
:
767 task_tag
= be16_to_cpu(iu
->task
.task_tag
);
768 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
769 req
= usb_uas_find_request(uas
, task_tag
);
770 if (req
&& req
->dev
== dev
) {
771 scsi_req_cancel(req
->req
);
773 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
776 case UAS_TMF_LOGICAL_UNIT_RESET
:
777 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
778 qdev_reset_all(&dev
->qdev
);
779 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
783 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, iu
->task
.function
);
784 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
);
790 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
);
794 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
);
798 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
);
801 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
803 UASDevice
*uas
= USB_UAS(dev
);
810 case UAS_PIPE_ID_COMMAND
:
811 length
= MIN(sizeof(iu
), p
->iov
.size
);
812 usb_packet_copy(p
, &iu
, length
);
815 usb_uas_command(uas
, &iu
);
817 case UAS_UI_TASK_MGMT
:
818 usb_uas_task(uas
, &iu
);
821 error_report("%s: unknown command iu: id 0x%x",
822 __func__
, iu
.hdr
.id
);
823 p
->status
= USB_RET_STALL
;
827 case UAS_PIPE_ID_STATUS
:
829 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
830 if (st
->stream
== p
->stream
) {
835 assert(uas
->status3
[p
->stream
] == NULL
);
836 uas
->status3
[p
->stream
] = p
;
837 p
->status
= USB_RET_ASYNC
;
841 st
= QTAILQ_FIRST(&uas
->results
);
843 assert(uas
->status2
== NULL
);
845 p
->status
= USB_RET_ASYNC
;
849 usb_packet_copy(p
, &st
->status
, st
->length
);
850 QTAILQ_REMOVE(&uas
->results
, st
, next
);
853 case UAS_PIPE_ID_DATA_IN
:
854 case UAS_PIPE_ID_DATA_OUT
:
856 req
= usb_uas_find_request(uas
, p
->stream
);
858 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
859 ? uas
->datain2
: uas
->dataout2
;
863 assert(uas
->data3
[p
->stream
] == NULL
);
864 uas
->data3
[p
->stream
] = p
;
865 p
->status
= USB_RET_ASYNC
;
868 error_report("%s: no inflight request", __func__
);
869 p
->status
= USB_RET_STALL
;
873 scsi_req_ref(req
->req
);
875 usb_uas_copy_data(req
);
876 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
879 req
->data_async
= true;
880 p
->status
= USB_RET_ASYNC
;
882 scsi_req_unref(req
->req
);
883 usb_uas_start_next_transfer(uas
);
886 error_report("%s: invalid endpoint %d", __func__
, p
->ep
->nr
);
887 p
->status
= USB_RET_STALL
;
892 static void usb_uas_handle_destroy(USBDevice
*dev
)
894 UASDevice
*uas
= USB_UAS(dev
);
896 qemu_bh_delete(uas
->status_bh
);
899 static void usb_uas_realize(USBDevice
*dev
, Error
**errp
)
901 UASDevice
*uas
= USB_UAS(dev
);
903 usb_desc_create_serial(dev
);
906 QTAILQ_INIT(&uas
->results
);
907 QTAILQ_INIT(&uas
->requests
);
908 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
910 scsi_bus_new(&uas
->bus
, sizeof(uas
->bus
), DEVICE(dev
),
911 &usb_uas_scsi_info
, NULL
);
914 static const VMStateDescription vmstate_usb_uas
= {
917 .fields
= (VMStateField
[]) {
918 VMSTATE_USB_DEVICE(dev
, UASDevice
),
919 VMSTATE_END_OF_LIST()
923 static Property uas_properties
[] = {
924 DEFINE_PROP_UINT32("log-scsi-req", UASDevice
, requestlog
, 0),
925 DEFINE_PROP_END_OF_LIST(),
928 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
930 DeviceClass
*dc
= DEVICE_CLASS(klass
);
931 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
933 uc
->realize
= usb_uas_realize
;
934 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
935 uc
->usb_desc
= &desc
;
936 uc
->cancel_packet
= usb_uas_cancel_io
;
937 uc
->handle_attach
= usb_desc_attach
;
938 uc
->handle_reset
= usb_uas_handle_reset
;
939 uc
->handle_control
= usb_uas_handle_control
;
940 uc
->handle_data
= usb_uas_handle_data
;
941 uc
->handle_destroy
= usb_uas_handle_destroy
;
942 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
943 dc
->fw_name
= "storage";
944 dc
->vmsd
= &vmstate_usb_uas
;
945 dc
->props
= uas_properties
;
948 static const TypeInfo uas_info
= {
949 .name
= TYPE_USB_UAS
,
950 .parent
= TYPE_USB_DEVICE
,
951 .instance_size
= sizeof(UASDevice
),
952 .class_init
= usb_uas_class_initfn
,
955 static void usb_uas_register_types(void)
957 type_register_static(&uas_info
);
960 type_init(usb_uas_register_types
)