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];
144 QTAILQ_ENTRY(UASRequest
) next
;
151 QTAILQ_ENTRY(UASStatus
) next
;
154 /* --------------------------------------------------------------------- */
157 STR_MANUFACTURER
= 1,
164 static const USBDescStrings desc_strings
= {
165 [STR_MANUFACTURER
] = "QEMU",
166 [STR_PRODUCT
] = "USB Attached SCSI HBA",
167 [STR_SERIALNUMBER
] = "27842",
168 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
169 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
172 static const USBDescIface desc_iface_high
= {
173 .bInterfaceNumber
= 0,
175 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
176 .bInterfaceSubClass
= 0x06, /* SCSI */
177 .bInterfaceProtocol
= 0x62, /* UAS */
178 .eps
= (USBDescEndpoint
[]) {
180 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
181 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
182 .wMaxPacketSize
= 512,
183 .extra
= (uint8_t[]) {
184 0x04, /* u8 bLength */
185 0x24, /* u8 bDescriptorType */
187 0x00, /* u8 bReserved */
190 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
191 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
192 .wMaxPacketSize
= 512,
193 .extra
= (uint8_t[]) {
194 0x04, /* u8 bLength */
195 0x24, /* u8 bDescriptorType */
197 0x00, /* u8 bReserved */
200 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
201 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
202 .wMaxPacketSize
= 512,
203 .extra
= (uint8_t[]) {
204 0x04, /* u8 bLength */
205 0x24, /* u8 bDescriptorType */
207 0x00, /* u8 bReserved */
210 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
211 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
212 .wMaxPacketSize
= 512,
213 .extra
= (uint8_t[]) {
214 0x04, /* u8 bLength */
215 0x24, /* u8 bDescriptorType */
216 UAS_PIPE_ID_DATA_OUT
,
217 0x00, /* u8 bReserved */
223 static const USBDescIface desc_iface_super
= {
224 .bInterfaceNumber
= 0,
226 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
227 .bInterfaceSubClass
= 0x06, /* SCSI */
228 .bInterfaceProtocol
= 0x62, /* UAS */
229 .eps
= (USBDescEndpoint
[]) {
231 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
232 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
233 .wMaxPacketSize
= 1024,
235 .extra
= (uint8_t[]) {
236 0x04, /* u8 bLength */
237 0x24, /* u8 bDescriptorType */
239 0x00, /* u8 bReserved */
242 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
243 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
244 .wMaxPacketSize
= 1024,
246 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
247 .extra
= (uint8_t[]) {
248 0x04, /* u8 bLength */
249 0x24, /* u8 bDescriptorType */
251 0x00, /* u8 bReserved */
254 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
255 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
256 .wMaxPacketSize
= 1024,
258 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
259 .extra
= (uint8_t[]) {
260 0x04, /* u8 bLength */
261 0x24, /* u8 bDescriptorType */
263 0x00, /* u8 bReserved */
266 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
267 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
268 .wMaxPacketSize
= 1024,
270 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
271 .extra
= (uint8_t[]) {
272 0x04, /* u8 bLength */
273 0x24, /* u8 bDescriptorType */
274 UAS_PIPE_ID_DATA_OUT
,
275 0x00, /* u8 bReserved */
281 static const USBDescDevice desc_device_high
= {
283 .bMaxPacketSize0
= 64,
284 .bNumConfigurations
= 1,
285 .confs
= (USBDescConfig
[]) {
288 .bConfigurationValue
= 1,
289 .iConfiguration
= STR_CONFIG_HIGH
,
290 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
292 .ifs
= &desc_iface_high
,
297 static const USBDescDevice desc_device_super
= {
299 .bMaxPacketSize0
= 64,
300 .bNumConfigurations
= 1,
301 .confs
= (USBDescConfig
[]) {
304 .bConfigurationValue
= 1,
305 .iConfiguration
= STR_CONFIG_SUPER
,
306 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
308 .ifs
= &desc_iface_super
,
313 static const USBDesc desc
= {
315 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
318 .iManufacturer
= STR_MANUFACTURER
,
319 .iProduct
= STR_PRODUCT
,
320 .iSerialNumber
= STR_SERIALNUMBER
,
322 .high
= &desc_device_high
,
323 .super
= &desc_device_super
,
327 /* --------------------------------------------------------------------- */
329 static bool uas_using_streams(UASDevice
*uas
)
331 return uas
->dev
.speed
== USB_SPEED_SUPER
;
334 /* --------------------------------------------------------------------- */
336 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
338 UASStatus
*st
= g_new0(UASStatus
, 1);
340 st
->status
.hdr
.id
= id
;
341 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
342 st
->length
= sizeof(uas_iu_header
);
343 if (uas_using_streams(uas
)) {
349 static void usb_uas_send_status_bh(void *opaque
)
351 UASDevice
*uas
= opaque
;
355 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
356 if (uas_using_streams(uas
)) {
357 p
= uas
->status3
[st
->stream
];
358 uas
->status3
[st
->stream
] = NULL
;
367 usb_packet_copy(p
, &st
->status
, st
->length
);
368 QTAILQ_REMOVE(&uas
->results
, st
, next
);
371 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
372 usb_packet_complete(&uas
->dev
, p
);
376 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
378 USBPacket
*p
= uas_using_streams(uas
) ?
379 uas
->status3
[st
->stream
] : uas
->status2
;
381 st
->length
+= length
;
382 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
385 * Just schedule bh make sure any in-flight data transaction
386 * is finished before completing (sending) the status packet.
388 qemu_bh_schedule(uas
->status_bh
);
390 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
392 usb_wakeup(ep
, st
->stream
);
396 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
, uint8_t code
)
398 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
400 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
401 st
->status
.response
.response_code
= code
;
402 usb_uas_queue_status(uas
, st
, sizeof(uas_iu_response
));
405 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
407 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
410 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
411 st
->status
.sense
.status
= status
;
412 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
413 if (status
!= GOOD
) {
414 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
415 sizeof(st
->status
.sense
.sense_data
));
416 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
418 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
419 usb_uas_queue_status(req
->uas
, st
, len
);
422 static void usb_uas_queue_fake_sense(UASDevice
*uas
, uint16_t tag
,
423 struct SCSISense sense
)
425 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_SENSE
, tag
);
428 st
->status
.sense
.status
= CHECK_CONDITION
;
429 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
430 st
->status
.sense
.sense_data
[0] = 0x70;
431 st
->status
.sense
.sense_data
[2] = sense
.key
;
432 st
->status
.sense
.sense_data
[7] = 10;
433 st
->status
.sense
.sense_data
[12] = sense
.asc
;
434 st
->status
.sense
.sense_data
[13] = sense
.ascq
;
436 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
437 usb_uas_queue_status(uas
, st
, len
);
440 static void usb_uas_queue_read_ready(UASRequest
*req
)
442 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
445 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
446 usb_uas_queue_status(req
->uas
, st
, 0);
449 static void usb_uas_queue_write_ready(UASRequest
*req
)
451 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
454 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
455 usb_uas_queue_status(req
->uas
, st
, 0);
458 /* --------------------------------------------------------------------- */
460 static int usb_uas_get_lun(uint64_t lun64
)
462 return (lun64
>> 48) & 0xff;
465 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
467 if ((lun64
>> 56) != 0x00) {
470 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
473 static void usb_uas_complete_data_packet(UASRequest
*req
)
477 if (!req
->data_async
) {
482 req
->data_async
= false;
483 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
484 usb_packet_complete(&req
->uas
->dev
, p
);
487 static void usb_uas_copy_data(UASRequest
*req
)
491 length
= MIN(req
->buf_size
- req
->buf_off
,
492 req
->data
->iov
.size
- req
->data
->actual_length
);
493 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
494 req
->data
->actual_length
, req
->data
->iov
.size
,
495 req
->buf_off
, req
->buf_size
);
496 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
498 req
->buf_off
+= length
;
499 req
->data_off
+= length
;
501 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
502 usb_uas_complete_data_packet(req
);
504 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
507 scsi_req_continue(req
->req
);
511 static void usb_uas_start_next_transfer(UASDevice
*uas
)
515 if (uas_using_streams(uas
)) {
519 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
520 if (req
->active
|| req
->complete
) {
523 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
525 usb_uas_queue_read_ready(req
);
529 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
531 usb_uas_queue_write_ready(req
);
538 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_iu
*iu
)
542 req
= g_new0(UASRequest
, 1);
544 req
->tag
= be16_to_cpu(iu
->hdr
.tag
);
545 req
->lun
= be64_to_cpu(iu
->command
.lun
);
546 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
550 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
552 UASRequest
*req
= priv
;
553 UASDevice
*uas
= req
->uas
;
555 if (req
== uas
->datain2
) {
558 if (req
== uas
->dataout2
) {
559 uas
->dataout2
= NULL
;
561 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
563 usb_uas_start_next_transfer(uas
);
566 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
570 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
571 if (req
->tag
== tag
) {
578 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
580 UASRequest
*req
= r
->hba_private
;
582 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
586 usb_uas_copy_data(req
);
588 usb_uas_start_next_transfer(req
->uas
);
592 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
593 uint32_t status
, size_t resid
)
595 UASRequest
*req
= r
->hba_private
;
597 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
598 req
->complete
= true;
600 usb_uas_complete_data_packet(req
);
602 usb_uas_queue_sense(req
, status
);
603 scsi_req_unref(req
->req
);
606 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
608 UASRequest
*req
= r
->hba_private
;
610 /* FIXME: queue notification to status pipe? */
611 scsi_req_unref(req
->req
);
614 static const struct SCSIBusInfo usb_uas_scsi_info
= {
619 .transfer_data
= usb_uas_scsi_transfer_data
,
620 .complete
= usb_uas_scsi_command_complete
,
621 .cancel
= usb_uas_scsi_request_cancelled
,
622 .free_request
= usb_uas_scsi_free_request
,
625 /* --------------------------------------------------------------------- */
627 static void usb_uas_handle_reset(USBDevice
*dev
)
629 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
630 UASRequest
*req
, *nreq
;
633 trace_usb_uas_reset(dev
->addr
);
634 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
635 scsi_req_cancel(req
->req
);
637 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
638 QTAILQ_REMOVE(&uas
->results
, st
, next
);
643 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
644 int request
, int value
, int index
, int length
, uint8_t *data
)
648 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
652 error_report("%s: unhandled control request", __func__
);
653 p
->status
= USB_RET_STALL
;
656 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
658 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
659 UASRequest
*req
, *nreq
;
662 if (uas
->status2
== p
) {
664 qemu_bh_cancel(uas
->status_bh
);
667 if (uas_using_streams(uas
)) {
668 for (i
= 0; i
<= UAS_MAX_STREAMS
; i
++) {
669 if (uas
->status3
[i
] == p
) {
670 uas
->status3
[i
] = NULL
;
673 if (uas
->data3
[i
] == p
) {
674 uas
->data3
[i
] = NULL
;
679 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
680 if (req
->data
== p
) {
685 assert(!"canceled usb packet not found");
688 static void usb_uas_command(UASDevice
*uas
, uas_iu
*iu
)
692 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
694 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
697 req
= usb_uas_find_request(uas
, tag
);
701 req
= usb_uas_alloc_request(uas
, iu
);
702 if (req
->dev
== NULL
) {
706 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
707 usb_uas_get_lun(req
->lun
),
708 req
->lun
>> 32, req
->lun
& 0xffffffff);
709 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
710 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
711 req
->data
= uas
->data3
[req
->tag
];
712 req
->data_async
= true;
713 uas
->data3
[req
->tag
] = NULL
;
716 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
717 usb_uas_get_lun(req
->lun
),
718 iu
->command
.cdb
, req
);
719 if (uas
->requestlog
) {
720 scsi_req_print(req
->req
);
722 len
= scsi_req_enqueue(req
->req
);
724 req
->data_size
= len
;
725 scsi_req_continue(req
->req
);
730 usb_uas_queue_fake_sense(uas
, tag
, sense_code_INVALID_TAG
);
734 usb_uas_queue_fake_sense(uas
, tag
, sense_code_OVERLAPPED_COMMANDS
);
738 usb_uas_queue_fake_sense(uas
, tag
, sense_code_LUN_NOT_SUPPORTED
);
742 static void usb_uas_task(UASDevice
*uas
, uas_iu
*iu
)
744 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
745 uint64_t lun64
= be64_to_cpu(iu
->task
.lun
);
746 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
747 int lun
= usb_uas_get_lun(lun64
);
751 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
754 req
= usb_uas_find_request(uas
, be16_to_cpu(iu
->hdr
.tag
));
762 switch (iu
->task
.function
) {
763 case UAS_TMF_ABORT_TASK
:
764 task_tag
= be16_to_cpu(iu
->task
.task_tag
);
765 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
766 req
= usb_uas_find_request(uas
, task_tag
);
767 if (req
&& req
->dev
== dev
) {
768 scsi_req_cancel(req
->req
);
770 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
773 case UAS_TMF_LOGICAL_UNIT_RESET
:
774 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
775 qdev_reset_all(&dev
->qdev
);
776 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
780 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, iu
->task
.function
);
781 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
);
787 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
);
791 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
);
795 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
);
798 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
800 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
807 case UAS_PIPE_ID_COMMAND
:
808 length
= MIN(sizeof(iu
), p
->iov
.size
);
809 usb_packet_copy(p
, &iu
, length
);
812 usb_uas_command(uas
, &iu
);
814 case UAS_UI_TASK_MGMT
:
815 usb_uas_task(uas
, &iu
);
818 error_report("%s: unknown command iu: id 0x%x",
819 __func__
, iu
.hdr
.id
);
820 p
->status
= USB_RET_STALL
;
824 case UAS_PIPE_ID_STATUS
:
826 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
827 if (st
->stream
== p
->stream
) {
832 assert(uas
->status3
[p
->stream
] == NULL
);
833 uas
->status3
[p
->stream
] = p
;
834 p
->status
= USB_RET_ASYNC
;
838 st
= QTAILQ_FIRST(&uas
->results
);
840 assert(uas
->status2
== NULL
);
842 p
->status
= USB_RET_ASYNC
;
846 usb_packet_copy(p
, &st
->status
, st
->length
);
847 QTAILQ_REMOVE(&uas
->results
, st
, next
);
850 case UAS_PIPE_ID_DATA_IN
:
851 case UAS_PIPE_ID_DATA_OUT
:
853 req
= usb_uas_find_request(uas
, p
->stream
);
855 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
856 ? uas
->datain2
: uas
->dataout2
;
860 assert(uas
->data3
[p
->stream
] == NULL
);
861 uas
->data3
[p
->stream
] = p
;
862 p
->status
= USB_RET_ASYNC
;
865 error_report("%s: no inflight request", __func__
);
866 p
->status
= USB_RET_STALL
;
870 scsi_req_ref(req
->req
);
872 usb_uas_copy_data(req
);
873 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
876 req
->data_async
= true;
877 p
->status
= USB_RET_ASYNC
;
879 scsi_req_unref(req
->req
);
880 usb_uas_start_next_transfer(uas
);
883 error_report("%s: invalid endpoint %d", __func__
, p
->ep
->nr
);
884 p
->status
= USB_RET_STALL
;
889 static void usb_uas_handle_destroy(USBDevice
*dev
)
891 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
893 qemu_bh_delete(uas
->status_bh
);
896 static void usb_uas_realize(USBDevice
*dev
, Error
**errp
)
898 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
900 usb_desc_create_serial(dev
);
903 QTAILQ_INIT(&uas
->results
);
904 QTAILQ_INIT(&uas
->requests
);
905 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
907 scsi_bus_new(&uas
->bus
, sizeof(uas
->bus
), DEVICE(dev
),
908 &usb_uas_scsi_info
, NULL
);
911 static const VMStateDescription vmstate_usb_uas
= {
914 .fields
= (VMStateField
[]) {
915 VMSTATE_USB_DEVICE(dev
, UASDevice
),
916 VMSTATE_END_OF_LIST()
920 static Property uas_properties
[] = {
921 DEFINE_PROP_UINT32("log-scsi-req", UASDevice
, requestlog
, 0),
922 DEFINE_PROP_END_OF_LIST(),
925 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
927 DeviceClass
*dc
= DEVICE_CLASS(klass
);
928 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
930 uc
->realize
= usb_uas_realize
;
931 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
932 uc
->usb_desc
= &desc
;
933 uc
->cancel_packet
= usb_uas_cancel_io
;
934 uc
->handle_attach
= usb_desc_attach
;
935 uc
->handle_reset
= usb_uas_handle_reset
;
936 uc
->handle_control
= usb_uas_handle_control
;
937 uc
->handle_data
= usb_uas_handle_data
;
938 uc
->handle_destroy
= usb_uas_handle_destroy
;
939 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
940 dc
->fw_name
= "storage";
941 dc
->vmsd
= &vmstate_usb_uas
;
942 dc
->props
= uas_properties
;
945 static const TypeInfo uas_info
= {
947 .parent
= TYPE_USB_DEVICE
,
948 .instance_size
= sizeof(UASDevice
),
949 .class_init
= usb_uas_class_initfn
,
952 static void usb_uas_register_types(void)
954 type_register_static(&uas_info
);
957 type_init(usb_uas_register_types
)