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"
27 /* --------------------------------------------------------------------- */
29 #define UAS_UI_COMMAND 0x01
30 #define UAS_UI_SENSE 0x03
31 #define UAS_UI_RESPONSE 0x04
32 #define UAS_UI_TASK_MGMT 0x05
33 #define UAS_UI_READ_READY 0x06
34 #define UAS_UI_WRITE_READY 0x07
36 #define UAS_RC_TMF_COMPLETE 0x00
37 #define UAS_RC_INVALID_INFO_UNIT 0x02
38 #define UAS_RC_TMF_NOT_SUPPORTED 0x04
39 #define UAS_RC_TMF_FAILED 0x05
40 #define UAS_RC_TMF_SUCCEEDED 0x08
41 #define UAS_RC_INCORRECT_LUN 0x09
42 #define UAS_RC_OVERLAPPED_TAG 0x0a
44 #define UAS_TMF_ABORT_TASK 0x01
45 #define UAS_TMF_ABORT_TASK_SET 0x02
46 #define UAS_TMF_CLEAR_TASK_SET 0x04
47 #define UAS_TMF_LOGICAL_UNIT_RESET 0x08
48 #define UAS_TMF_I_T_NEXUS_RESET 0x10
49 #define UAS_TMF_CLEAR_ACA 0x40
50 #define UAS_TMF_QUERY_TASK 0x80
51 #define UAS_TMF_QUERY_TASK_SET 0x81
52 #define UAS_TMF_QUERY_ASYNC_EVENT 0x82
54 #define UAS_PIPE_ID_COMMAND 0x01
55 #define UAS_PIPE_ID_STATUS 0x02
56 #define UAS_PIPE_ID_DATA_IN 0x03
57 #define UAS_PIPE_ID_DATA_OUT 0x04
63 } QEMU_PACKED uas_iu_header
;
66 uint8_t prio_taskattr
; /* 6:3 priority, 2:0 task attribute */
68 uint8_t add_cdb_length
; /* 7:2 additional adb length (dwords) */
73 } QEMU_PACKED uas_iu_command
;
76 uint16_t status_qualifier
;
79 uint16_t sense_length
;
80 uint8_t sense_data
[18];
81 } QEMU_PACKED uas_iu_sense
;
84 uint8_t add_response_info
[3];
85 uint8_t response_code
;
86 } QEMU_PACKED uas_iu_response
;
93 } QEMU_PACKED uas_iu_task_mgmt
;
98 uas_iu_command command
;
100 uas_iu_task_mgmt task
;
101 uas_iu_response response
;
103 } QEMU_PACKED uas_iu
;
105 /* --------------------------------------------------------------------- */
107 #define UAS_STREAM_BM_ATTR 4
108 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR)
110 typedef struct UASDevice UASDevice
;
111 typedef struct UASRequest UASRequest
;
112 typedef struct UASStatus UASStatus
;
118 QTAILQ_HEAD(, UASStatus
) results
;
119 QTAILQ_HEAD(, UASRequest
) requests
;
127 UASRequest
*dataout2
;
130 USBPacket
*data3
[UAS_MAX_STREAMS
+ 1];
131 USBPacket
*status3
[UAS_MAX_STREAMS
+ 1];
134 #define TYPE_USB_UAS "usb-uas"
135 #define USB_UAS(obj) OBJECT_CHECK(UASDevice, (obj), TYPE_USB_UAS)
151 QTAILQ_ENTRY(UASRequest
) next
;
158 QTAILQ_ENTRY(UASStatus
) next
;
161 /* --------------------------------------------------------------------- */
164 STR_MANUFACTURER
= 1,
171 static const USBDescStrings desc_strings
= {
172 [STR_MANUFACTURER
] = "QEMU",
173 [STR_PRODUCT
] = "USB Attached SCSI HBA",
174 [STR_SERIALNUMBER
] = "27842",
175 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
176 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
179 static const USBDescIface desc_iface_high
= {
180 .bInterfaceNumber
= 0,
182 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
183 .bInterfaceSubClass
= 0x06, /* SCSI */
184 .bInterfaceProtocol
= 0x62, /* UAS */
185 .eps
= (USBDescEndpoint
[]) {
187 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
188 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
189 .wMaxPacketSize
= 512,
190 .extra
= (uint8_t[]) {
191 0x04, /* u8 bLength */
192 0x24, /* u8 bDescriptorType */
194 0x00, /* u8 bReserved */
197 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
198 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
199 .wMaxPacketSize
= 512,
200 .extra
= (uint8_t[]) {
201 0x04, /* u8 bLength */
202 0x24, /* u8 bDescriptorType */
204 0x00, /* u8 bReserved */
207 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
208 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
209 .wMaxPacketSize
= 512,
210 .extra
= (uint8_t[]) {
211 0x04, /* u8 bLength */
212 0x24, /* u8 bDescriptorType */
214 0x00, /* u8 bReserved */
217 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
218 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
219 .wMaxPacketSize
= 512,
220 .extra
= (uint8_t[]) {
221 0x04, /* u8 bLength */
222 0x24, /* u8 bDescriptorType */
223 UAS_PIPE_ID_DATA_OUT
,
224 0x00, /* u8 bReserved */
230 static const USBDescIface desc_iface_super
= {
231 .bInterfaceNumber
= 0,
233 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
234 .bInterfaceSubClass
= 0x06, /* SCSI */
235 .bInterfaceProtocol
= 0x62, /* UAS */
236 .eps
= (USBDescEndpoint
[]) {
238 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
239 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
240 .wMaxPacketSize
= 1024,
242 .extra
= (uint8_t[]) {
243 0x04, /* u8 bLength */
244 0x24, /* u8 bDescriptorType */
246 0x00, /* u8 bReserved */
249 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
250 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
251 .wMaxPacketSize
= 1024,
253 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
254 .extra
= (uint8_t[]) {
255 0x04, /* u8 bLength */
256 0x24, /* u8 bDescriptorType */
258 0x00, /* u8 bReserved */
261 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
262 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
263 .wMaxPacketSize
= 1024,
265 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
266 .extra
= (uint8_t[]) {
267 0x04, /* u8 bLength */
268 0x24, /* u8 bDescriptorType */
270 0x00, /* u8 bReserved */
273 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
274 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
275 .wMaxPacketSize
= 1024,
277 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
278 .extra
= (uint8_t[]) {
279 0x04, /* u8 bLength */
280 0x24, /* u8 bDescriptorType */
281 UAS_PIPE_ID_DATA_OUT
,
282 0x00, /* u8 bReserved */
288 static const USBDescDevice desc_device_high
= {
290 .bMaxPacketSize0
= 64,
291 .bNumConfigurations
= 1,
292 .confs
= (USBDescConfig
[]) {
295 .bConfigurationValue
= 1,
296 .iConfiguration
= STR_CONFIG_HIGH
,
297 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
299 .ifs
= &desc_iface_high
,
304 static const USBDescDevice desc_device_super
= {
306 .bMaxPacketSize0
= 9,
307 .bNumConfigurations
= 1,
308 .confs
= (USBDescConfig
[]) {
311 .bConfigurationValue
= 1,
312 .iConfiguration
= STR_CONFIG_SUPER
,
313 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
315 .ifs
= &desc_iface_super
,
320 static const USBDesc desc
= {
322 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
325 .iManufacturer
= STR_MANUFACTURER
,
326 .iProduct
= STR_PRODUCT
,
327 .iSerialNumber
= STR_SERIALNUMBER
,
329 .high
= &desc_device_high
,
330 .super
= &desc_device_super
,
334 /* --------------------------------------------------------------------- */
336 static bool uas_using_streams(UASDevice
*uas
)
338 return uas
->dev
.speed
== USB_SPEED_SUPER
;
341 /* --------------------------------------------------------------------- */
343 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
345 UASStatus
*st
= g_new0(UASStatus
, 1);
347 st
->status
.hdr
.id
= id
;
348 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
349 st
->length
= sizeof(uas_iu_header
);
350 if (uas_using_streams(uas
)) {
356 static void usb_uas_send_status_bh(void *opaque
)
358 UASDevice
*uas
= opaque
;
362 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
363 if (uas_using_streams(uas
)) {
364 p
= uas
->status3
[st
->stream
];
365 uas
->status3
[st
->stream
] = NULL
;
374 usb_packet_copy(p
, &st
->status
, st
->length
);
375 QTAILQ_REMOVE(&uas
->results
, st
, next
);
378 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
379 usb_packet_complete(&uas
->dev
, p
);
383 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
385 USBPacket
*p
= uas_using_streams(uas
) ?
386 uas
->status3
[st
->stream
] : uas
->status2
;
388 st
->length
+= length
;
389 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
392 * Just schedule bh make sure any in-flight data transaction
393 * is finished before completing (sending) the status packet.
395 qemu_bh_schedule(uas
->status_bh
);
397 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
399 usb_wakeup(ep
, st
->stream
);
403 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
, uint8_t code
)
405 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
407 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
408 st
->status
.response
.response_code
= code
;
409 usb_uas_queue_status(uas
, st
, sizeof(uas_iu_response
));
412 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
414 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
417 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
418 st
->status
.sense
.status
= status
;
419 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
420 if (status
!= GOOD
) {
421 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
422 sizeof(st
->status
.sense
.sense_data
));
423 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
425 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
426 usb_uas_queue_status(req
->uas
, st
, len
);
429 static void usb_uas_queue_fake_sense(UASDevice
*uas
, uint16_t tag
,
430 struct SCSISense sense
)
432 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_SENSE
, tag
);
435 st
->status
.sense
.status
= CHECK_CONDITION
;
436 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
437 st
->status
.sense
.sense_data
[0] = 0x70;
438 st
->status
.sense
.sense_data
[2] = sense
.key
;
439 st
->status
.sense
.sense_data
[7] = 10;
440 st
->status
.sense
.sense_data
[12] = sense
.asc
;
441 st
->status
.sense
.sense_data
[13] = sense
.ascq
;
443 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
444 usb_uas_queue_status(uas
, st
, len
);
447 static void usb_uas_queue_read_ready(UASRequest
*req
)
449 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
452 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
453 usb_uas_queue_status(req
->uas
, st
, 0);
456 static void usb_uas_queue_write_ready(UASRequest
*req
)
458 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
461 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
462 usb_uas_queue_status(req
->uas
, st
, 0);
465 /* --------------------------------------------------------------------- */
467 static int usb_uas_get_lun(uint64_t lun64
)
469 return (lun64
>> 48) & 0xff;
472 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
474 if ((lun64
>> 56) != 0x00) {
477 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
480 static void usb_uas_complete_data_packet(UASRequest
*req
)
484 if (!req
->data_async
) {
489 req
->data_async
= false;
490 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
491 usb_packet_complete(&req
->uas
->dev
, p
);
494 static void usb_uas_copy_data(UASRequest
*req
)
498 length
= MIN(req
->buf_size
- req
->buf_off
,
499 req
->data
->iov
.size
- req
->data
->actual_length
);
500 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
501 req
->data
->actual_length
, req
->data
->iov
.size
,
502 req
->buf_off
, req
->buf_size
);
503 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
505 req
->buf_off
+= length
;
506 req
->data_off
+= length
;
508 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
509 usb_uas_complete_data_packet(req
);
511 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
514 scsi_req_continue(req
->req
);
518 static void usb_uas_start_next_transfer(UASDevice
*uas
)
522 if (uas_using_streams(uas
)) {
526 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
527 if (req
->active
|| req
->complete
) {
530 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
532 usb_uas_queue_read_ready(req
);
536 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
538 usb_uas_queue_write_ready(req
);
545 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_iu
*iu
)
549 req
= g_new0(UASRequest
, 1);
551 req
->tag
= be16_to_cpu(iu
->hdr
.tag
);
552 req
->lun
= be64_to_cpu(iu
->command
.lun
);
553 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
557 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
559 UASRequest
*req
= priv
;
560 UASDevice
*uas
= req
->uas
;
562 if (req
== uas
->datain2
) {
565 if (req
== uas
->dataout2
) {
566 uas
->dataout2
= NULL
;
568 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
570 usb_uas_start_next_transfer(uas
);
573 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
577 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
578 if (req
->tag
== tag
) {
585 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
587 UASRequest
*req
= r
->hba_private
;
589 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
593 usb_uas_copy_data(req
);
595 usb_uas_start_next_transfer(req
->uas
);
599 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
600 uint32_t status
, size_t resid
)
602 UASRequest
*req
= r
->hba_private
;
604 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
605 req
->complete
= true;
607 usb_uas_complete_data_packet(req
);
609 usb_uas_queue_sense(req
, status
);
610 scsi_req_unref(req
->req
);
613 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
615 UASRequest
*req
= r
->hba_private
;
617 /* FIXME: queue notification to status pipe? */
618 scsi_req_unref(req
->req
);
621 static const struct SCSIBusInfo usb_uas_scsi_info
= {
626 .transfer_data
= usb_uas_scsi_transfer_data
,
627 .complete
= usb_uas_scsi_command_complete
,
628 .cancel
= usb_uas_scsi_request_cancelled
,
629 .free_request
= usb_uas_scsi_free_request
,
632 /* --------------------------------------------------------------------- */
634 static void usb_uas_handle_reset(USBDevice
*dev
)
636 UASDevice
*uas
= USB_UAS(dev
);
637 UASRequest
*req
, *nreq
;
640 trace_usb_uas_reset(dev
->addr
);
641 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
642 scsi_req_cancel(req
->req
);
644 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
645 QTAILQ_REMOVE(&uas
->results
, st
, next
);
650 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
651 int request
, int value
, int index
, int length
, uint8_t *data
)
655 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
659 error_report("%s: unhandled control request (req 0x%x, val 0x%x, idx 0x%x",
660 __func__
, request
, value
, index
);
661 p
->status
= USB_RET_STALL
;
664 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
666 UASDevice
*uas
= USB_UAS(dev
);
667 UASRequest
*req
, *nreq
;
670 if (uas
->status2
== p
) {
672 qemu_bh_cancel(uas
->status_bh
);
675 if (uas_using_streams(uas
)) {
676 for (i
= 0; i
<= UAS_MAX_STREAMS
; i
++) {
677 if (uas
->status3
[i
] == p
) {
678 uas
->status3
[i
] = NULL
;
681 if (uas
->data3
[i
] == p
) {
682 uas
->data3
[i
] = NULL
;
687 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
688 if (req
->data
== p
) {
693 assert(!"canceled usb packet not found");
696 static void usb_uas_command(UASDevice
*uas
, uas_iu
*iu
)
700 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
702 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
705 req
= usb_uas_find_request(uas
, tag
);
709 req
= usb_uas_alloc_request(uas
, iu
);
710 if (req
->dev
== NULL
) {
714 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
715 usb_uas_get_lun(req
->lun
),
716 req
->lun
>> 32, req
->lun
& 0xffffffff);
717 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
718 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
719 req
->data
= uas
->data3
[req
->tag
];
720 req
->data_async
= true;
721 uas
->data3
[req
->tag
] = NULL
;
724 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
725 usb_uas_get_lun(req
->lun
),
726 iu
->command
.cdb
, req
);
727 if (uas
->requestlog
) {
728 scsi_req_print(req
->req
);
730 len
= scsi_req_enqueue(req
->req
);
732 req
->data_size
= len
;
733 scsi_req_continue(req
->req
);
738 usb_uas_queue_fake_sense(uas
, tag
, sense_code_INVALID_TAG
);
742 usb_uas_queue_fake_sense(uas
, tag
, sense_code_OVERLAPPED_COMMANDS
);
746 usb_uas_queue_fake_sense(uas
, tag
, sense_code_LUN_NOT_SUPPORTED
);
750 static void usb_uas_task(UASDevice
*uas
, uas_iu
*iu
)
752 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
753 uint64_t lun64
= be64_to_cpu(iu
->task
.lun
);
754 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
755 int lun
= usb_uas_get_lun(lun64
);
759 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
762 req
= usb_uas_find_request(uas
, be16_to_cpu(iu
->hdr
.tag
));
770 switch (iu
->task
.function
) {
771 case UAS_TMF_ABORT_TASK
:
772 task_tag
= be16_to_cpu(iu
->task
.task_tag
);
773 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
774 req
= usb_uas_find_request(uas
, task_tag
);
775 if (req
&& req
->dev
== dev
) {
776 scsi_req_cancel(req
->req
);
778 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
781 case UAS_TMF_LOGICAL_UNIT_RESET
:
782 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
783 qdev_reset_all(&dev
->qdev
);
784 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
788 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, iu
->task
.function
);
789 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
);
795 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
);
799 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
);
803 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
);
806 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
808 UASDevice
*uas
= USB_UAS(dev
);
815 case UAS_PIPE_ID_COMMAND
:
816 length
= MIN(sizeof(iu
), p
->iov
.size
);
817 usb_packet_copy(p
, &iu
, length
);
820 usb_uas_command(uas
, &iu
);
822 case UAS_UI_TASK_MGMT
:
823 usb_uas_task(uas
, &iu
);
826 error_report("%s: unknown command iu: id 0x%x",
827 __func__
, iu
.hdr
.id
);
828 p
->status
= USB_RET_STALL
;
832 case UAS_PIPE_ID_STATUS
:
834 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
835 if (st
->stream
== p
->stream
) {
840 assert(uas
->status3
[p
->stream
] == NULL
);
841 uas
->status3
[p
->stream
] = p
;
842 p
->status
= USB_RET_ASYNC
;
846 st
= QTAILQ_FIRST(&uas
->results
);
848 assert(uas
->status2
== NULL
);
850 p
->status
= USB_RET_ASYNC
;
854 usb_packet_copy(p
, &st
->status
, st
->length
);
855 QTAILQ_REMOVE(&uas
->results
, st
, next
);
858 case UAS_PIPE_ID_DATA_IN
:
859 case UAS_PIPE_ID_DATA_OUT
:
861 req
= usb_uas_find_request(uas
, p
->stream
);
863 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
864 ? uas
->datain2
: uas
->dataout2
;
868 assert(uas
->data3
[p
->stream
] == NULL
);
869 uas
->data3
[p
->stream
] = p
;
870 p
->status
= USB_RET_ASYNC
;
873 error_report("%s: no inflight request", __func__
);
874 p
->status
= USB_RET_STALL
;
878 scsi_req_ref(req
->req
);
880 usb_uas_copy_data(req
);
881 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
884 req
->data_async
= true;
885 p
->status
= USB_RET_ASYNC
;
887 scsi_req_unref(req
->req
);
888 usb_uas_start_next_transfer(uas
);
891 error_report("%s: invalid endpoint %d", __func__
, p
->ep
->nr
);
892 p
->status
= USB_RET_STALL
;
897 static void usb_uas_unrealize(USBDevice
*dev
, Error
**errp
)
899 UASDevice
*uas
= USB_UAS(dev
);
901 qemu_bh_delete(uas
->status_bh
);
904 static void usb_uas_realize(USBDevice
*dev
, Error
**errp
)
906 UASDevice
*uas
= USB_UAS(dev
);
907 DeviceState
*d
= DEVICE(dev
);
909 usb_desc_create_serial(dev
);
912 uas
->dev
.auto_attach
= 0;
915 QTAILQ_INIT(&uas
->results
);
916 QTAILQ_INIT(&uas
->requests
);
917 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
919 scsi_bus_new(&uas
->bus
, sizeof(uas
->bus
), DEVICE(dev
),
920 &usb_uas_scsi_info
, NULL
);
923 static const VMStateDescription vmstate_usb_uas
= {
926 .fields
= (VMStateField
[]) {
927 VMSTATE_USB_DEVICE(dev
, UASDevice
),
928 VMSTATE_END_OF_LIST()
932 static Property uas_properties
[] = {
933 DEFINE_PROP_UINT32("log-scsi-req", UASDevice
, requestlog
, 0),
934 DEFINE_PROP_END_OF_LIST(),
937 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
939 DeviceClass
*dc
= DEVICE_CLASS(klass
);
940 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
942 uc
->realize
= usb_uas_realize
;
943 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
944 uc
->usb_desc
= &desc
;
945 uc
->cancel_packet
= usb_uas_cancel_io
;
946 uc
->handle_attach
= usb_desc_attach
;
947 uc
->handle_reset
= usb_uas_handle_reset
;
948 uc
->handle_control
= usb_uas_handle_control
;
949 uc
->handle_data
= usb_uas_handle_data
;
950 uc
->unrealize
= usb_uas_unrealize
;
951 uc
->attached_settable
= true;
952 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
953 dc
->fw_name
= "storage";
954 dc
->vmsd
= &vmstate_usb_uas
;
955 device_class_set_props(dc
, uas_properties
);
958 static const TypeInfo uas_info
= {
959 .name
= TYPE_USB_UAS
,
960 .parent
= TYPE_USB_DEVICE
,
961 .instance_size
= sizeof(UASDevice
),
962 .class_init
= usb_uas_class_initfn
,
965 static void usb_uas_register_types(void)
967 type_register_static(&uas_info
);
970 type_init(usb_uas_register_types
)