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_iu_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_iu_command
;
71 uint16_t status_qualifier
;
74 uint16_t sense_length
;
75 uint8_t sense_data
[18];
76 } QEMU_PACKED uas_iu_sense
;
79 uint8_t add_response_info
[3];
80 uint8_t response_code
;
81 } QEMU_PACKED uas_iu_response
;
88 } QEMU_PACKED uas_iu_task_mgmt
;
93 uas_iu_command command
;
95 uas_iu_task_mgmt task
;
96 uas_iu_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
;
122 UASRequest
*dataout2
;
125 USBPacket
*data3
[UAS_MAX_STREAMS
+ 1];
126 USBPacket
*status3
[UAS_MAX_STREAMS
+ 1];
143 QTAILQ_ENTRY(UASRequest
) next
;
150 QTAILQ_ENTRY(UASStatus
) next
;
153 /* --------------------------------------------------------------------- */
156 STR_MANUFACTURER
= 1,
163 static const USBDescStrings desc_strings
= {
164 [STR_MANUFACTURER
] = "QEMU",
165 [STR_PRODUCT
] = "USB Attached SCSI HBA",
166 [STR_SERIALNUMBER
] = "27842",
167 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
168 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
171 static const USBDescIface desc_iface_high
= {
172 .bInterfaceNumber
= 0,
174 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
175 .bInterfaceSubClass
= 0x06, /* SCSI */
176 .bInterfaceProtocol
= 0x62, /* UAS */
177 .eps
= (USBDescEndpoint
[]) {
179 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
180 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
181 .wMaxPacketSize
= 512,
182 .extra
= (uint8_t[]) {
183 0x04, /* u8 bLength */
184 0x24, /* u8 bDescriptorType */
186 0x00, /* u8 bReserved */
189 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
190 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
191 .wMaxPacketSize
= 512,
192 .extra
= (uint8_t[]) {
193 0x04, /* u8 bLength */
194 0x24, /* u8 bDescriptorType */
196 0x00, /* u8 bReserved */
199 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
200 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
201 .wMaxPacketSize
= 512,
202 .extra
= (uint8_t[]) {
203 0x04, /* u8 bLength */
204 0x24, /* u8 bDescriptorType */
206 0x00, /* u8 bReserved */
209 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
210 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
211 .wMaxPacketSize
= 512,
212 .extra
= (uint8_t[]) {
213 0x04, /* u8 bLength */
214 0x24, /* u8 bDescriptorType */
215 UAS_PIPE_ID_DATA_OUT
,
216 0x00, /* u8 bReserved */
222 static const USBDescIface desc_iface_super
= {
223 .bInterfaceNumber
= 0,
225 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
226 .bInterfaceSubClass
= 0x06, /* SCSI */
227 .bInterfaceProtocol
= 0x62, /* UAS */
228 .eps
= (USBDescEndpoint
[]) {
230 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
231 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
232 .wMaxPacketSize
= 1024,
234 .extra
= (uint8_t[]) {
235 0x04, /* u8 bLength */
236 0x24, /* u8 bDescriptorType */
238 0x00, /* u8 bReserved */
241 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
242 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
243 .wMaxPacketSize
= 1024,
245 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
246 .extra
= (uint8_t[]) {
247 0x04, /* u8 bLength */
248 0x24, /* u8 bDescriptorType */
250 0x00, /* u8 bReserved */
253 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
254 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
255 .wMaxPacketSize
= 1024,
257 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
258 .extra
= (uint8_t[]) {
259 0x04, /* u8 bLength */
260 0x24, /* u8 bDescriptorType */
262 0x00, /* u8 bReserved */
265 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
266 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
267 .wMaxPacketSize
= 1024,
269 .bmAttributes_super
= UAS_STREAM_BM_ATTR
,
270 .extra
= (uint8_t[]) {
271 0x04, /* u8 bLength */
272 0x24, /* u8 bDescriptorType */
273 UAS_PIPE_ID_DATA_OUT
,
274 0x00, /* u8 bReserved */
280 static const USBDescDevice desc_device_high
= {
282 .bMaxPacketSize0
= 64,
283 .bNumConfigurations
= 1,
284 .confs
= (USBDescConfig
[]) {
287 .bConfigurationValue
= 1,
288 .iConfiguration
= STR_CONFIG_HIGH
,
289 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
291 .ifs
= &desc_iface_high
,
296 static const USBDescDevice desc_device_super
= {
298 .bMaxPacketSize0
= 64,
299 .bNumConfigurations
= 1,
300 .confs
= (USBDescConfig
[]) {
303 .bConfigurationValue
= 1,
304 .iConfiguration
= STR_CONFIG_SUPER
,
305 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
307 .ifs
= &desc_iface_super
,
312 static const USBDesc desc
= {
314 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
317 .iManufacturer
= STR_MANUFACTURER
,
318 .iProduct
= STR_PRODUCT
,
319 .iSerialNumber
= STR_SERIALNUMBER
,
321 .high
= &desc_device_high
,
322 .super
= &desc_device_super
,
326 /* --------------------------------------------------------------------- */
328 static bool uas_using_streams(UASDevice
*uas
)
330 return uas
->dev
.speed
== USB_SPEED_SUPER
;
333 /* --------------------------------------------------------------------- */
335 static UASStatus
*usb_uas_alloc_status(UASDevice
*uas
, uint8_t id
, uint16_t tag
)
337 UASStatus
*st
= g_new0(UASStatus
, 1);
339 st
->status
.hdr
.id
= id
;
340 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
341 st
->length
= sizeof(uas_iu_header
);
342 if (uas_using_streams(uas
)) {
348 static void usb_uas_send_status_bh(void *opaque
)
350 UASDevice
*uas
= opaque
;
354 while ((st
= QTAILQ_FIRST(&uas
->results
)) != NULL
) {
355 if (uas_using_streams(uas
)) {
356 p
= uas
->status3
[st
->stream
];
357 uas
->status3
[st
->stream
] = NULL
;
366 usb_packet_copy(p
, &st
->status
, st
->length
);
367 QTAILQ_REMOVE(&uas
->results
, st
, next
);
370 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
371 usb_packet_complete(&uas
->dev
, p
);
375 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
377 USBPacket
*p
= uas_using_streams(uas
) ?
378 uas
->status3
[st
->stream
] : uas
->status2
;
380 st
->length
+= length
;
381 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
384 * Just schedule bh make sure any in-flight data transaction
385 * is finished before completing (sending) the status packet.
387 qemu_bh_schedule(uas
->status_bh
);
389 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
391 usb_wakeup(ep
, st
->stream
);
395 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
, uint8_t code
)
397 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_RESPONSE
, tag
);
399 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
400 st
->status
.response
.response_code
= code
;
401 usb_uas_queue_status(uas
, st
, sizeof(uas_iu_response
));
404 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
406 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_SENSE
, req
->tag
);
409 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
410 st
->status
.sense
.status
= status
;
411 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
412 if (status
!= GOOD
) {
413 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
414 sizeof(st
->status
.sense
.sense_data
));
415 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
417 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
418 usb_uas_queue_status(req
->uas
, st
, len
);
421 static void usb_uas_queue_fake_sense(UASDevice
*uas
, uint16_t tag
,
422 struct SCSISense sense
)
424 UASStatus
*st
= usb_uas_alloc_status(uas
, UAS_UI_SENSE
, tag
);
427 st
->status
.sense
.status
= CHECK_CONDITION
;
428 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
429 st
->status
.sense
.sense_data
[0] = 0x70;
430 st
->status
.sense
.sense_data
[2] = sense
.key
;
431 st
->status
.sense
.sense_data
[7] = 10;
432 st
->status
.sense
.sense_data
[12] = sense
.asc
;
433 st
->status
.sense
.sense_data
[13] = sense
.ascq
;
435 len
= sizeof(uas_iu_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
436 usb_uas_queue_status(uas
, st
, len
);
439 static void usb_uas_queue_read_ready(UASRequest
*req
)
441 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_READ_READY
,
444 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
445 usb_uas_queue_status(req
->uas
, st
, 0);
448 static void usb_uas_queue_write_ready(UASRequest
*req
)
450 UASStatus
*st
= usb_uas_alloc_status(req
->uas
, UAS_UI_WRITE_READY
,
453 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
454 usb_uas_queue_status(req
->uas
, st
, 0);
457 /* --------------------------------------------------------------------- */
459 static int usb_uas_get_lun(uint64_t lun64
)
461 return (lun64
>> 48) & 0xff;
464 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
466 if ((lun64
>> 56) != 0x00) {
469 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
472 static void usb_uas_complete_data_packet(UASRequest
*req
)
476 if (!req
->data_async
) {
481 req
->data_async
= false;
482 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
483 usb_packet_complete(&req
->uas
->dev
, p
);
486 static void usb_uas_copy_data(UASRequest
*req
)
490 length
= MIN(req
->buf_size
- req
->buf_off
,
491 req
->data
->iov
.size
- req
->data
->actual_length
);
492 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
493 req
->data
->actual_length
, req
->data
->iov
.size
,
494 req
->buf_off
, req
->buf_size
);
495 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
497 req
->buf_off
+= length
;
498 req
->data_off
+= length
;
500 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
501 usb_uas_complete_data_packet(req
);
503 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
506 scsi_req_continue(req
->req
);
510 static void usb_uas_start_next_transfer(UASDevice
*uas
)
514 if (uas_using_streams(uas
)) {
518 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
519 if (req
->active
|| req
->complete
) {
522 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain2
== NULL
) {
524 usb_uas_queue_read_ready(req
);
528 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout2
== NULL
) {
530 usb_uas_queue_write_ready(req
);
537 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_iu
*iu
)
541 req
= g_new0(UASRequest
, 1);
543 req
->tag
= be16_to_cpu(iu
->hdr
.tag
);
544 req
->lun
= be64_to_cpu(iu
->command
.lun
);
545 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
549 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
551 UASRequest
*req
= priv
;
552 UASDevice
*uas
= req
->uas
;
554 if (req
== uas
->datain2
) {
557 if (req
== uas
->dataout2
) {
558 uas
->dataout2
= NULL
;
560 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
562 usb_uas_start_next_transfer(uas
);
565 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
569 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
570 if (req
->tag
== tag
) {
577 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
579 UASRequest
*req
= r
->hba_private
;
581 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
585 usb_uas_copy_data(req
);
587 usb_uas_start_next_transfer(req
->uas
);
591 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
592 uint32_t status
, size_t resid
)
594 UASRequest
*req
= r
->hba_private
;
596 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
597 req
->complete
= true;
599 usb_uas_complete_data_packet(req
);
601 usb_uas_queue_sense(req
, status
);
602 scsi_req_unref(req
->req
);
605 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
607 UASRequest
*req
= r
->hba_private
;
609 /* FIXME: queue notification to status pipe? */
610 scsi_req_unref(req
->req
);
613 static const struct SCSIBusInfo usb_uas_scsi_info
= {
618 .transfer_data
= usb_uas_scsi_transfer_data
,
619 .complete
= usb_uas_scsi_command_complete
,
620 .cancel
= usb_uas_scsi_request_cancelled
,
621 .free_request
= usb_uas_scsi_free_request
,
624 /* --------------------------------------------------------------------- */
626 static void usb_uas_handle_reset(USBDevice
*dev
)
628 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
629 UASRequest
*req
, *nreq
;
632 trace_usb_uas_reset(dev
->addr
);
633 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
634 scsi_req_cancel(req
->req
);
636 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
637 QTAILQ_REMOVE(&uas
->results
, st
, next
);
642 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
643 int request
, int value
, int index
, int length
, uint8_t *data
)
647 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
651 fprintf(stderr
, "%s: unhandled control request\n", __func__
);
652 p
->status
= USB_RET_STALL
;
655 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
657 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
658 UASRequest
*req
, *nreq
;
661 if (uas
->status2
== p
) {
663 qemu_bh_cancel(uas
->status_bh
);
666 if (uas_using_streams(uas
)) {
667 for (i
= 0; i
<= UAS_MAX_STREAMS
; i
++) {
668 if (uas
->status3
[i
] == p
) {
669 uas
->status3
[i
] = NULL
;
672 if (uas
->data3
[i
] == p
) {
673 uas
->data3
[i
] = NULL
;
678 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
679 if (req
->data
== p
) {
684 assert(!"canceled usb packet not found");
687 static void usb_uas_command(UASDevice
*uas
, uas_iu
*iu
)
691 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
693 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
696 req
= usb_uas_find_request(uas
, tag
);
700 req
= usb_uas_alloc_request(uas
, iu
);
701 if (req
->dev
== NULL
) {
705 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
706 usb_uas_get_lun(req
->lun
),
707 req
->lun
>> 32, req
->lun
& 0xffffffff);
708 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
709 if (uas_using_streams(uas
) && uas
->data3
[req
->tag
] != NULL
) {
710 req
->data
= uas
->data3
[req
->tag
];
711 req
->data_async
= true;
712 uas
->data3
[req
->tag
] = NULL
;
715 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
716 usb_uas_get_lun(req
->lun
),
717 iu
->command
.cdb
, req
);
718 if (uas
->requestlog
) {
719 scsi_req_print(req
->req
);
721 len
= scsi_req_enqueue(req
->req
);
723 req
->data_size
= len
;
724 scsi_req_continue(req
->req
);
729 usb_uas_queue_fake_sense(uas
, tag
, sense_code_INVALID_TAG
);
733 usb_uas_queue_fake_sense(uas
, tag
, sense_code_OVERLAPPED_COMMANDS
);
737 usb_uas_queue_fake_sense(uas
, tag
, sense_code_LUN_NOT_SUPPORTED
);
741 static void usb_uas_task(UASDevice
*uas
, uas_iu
*iu
)
743 uint16_t tag
= be16_to_cpu(iu
->hdr
.tag
);
744 uint64_t lun64
= be64_to_cpu(iu
->task
.lun
);
745 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
746 int lun
= usb_uas_get_lun(lun64
);
750 if (uas_using_streams(uas
) && tag
> UAS_MAX_STREAMS
) {
753 req
= usb_uas_find_request(uas
, be16_to_cpu(iu
->hdr
.tag
));
761 switch (iu
->task
.function
) {
762 case UAS_TMF_ABORT_TASK
:
763 task_tag
= be16_to_cpu(iu
->task
.task_tag
);
764 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
765 req
= usb_uas_find_request(uas
, task_tag
);
766 if (req
&& req
->dev
== dev
) {
767 scsi_req_cancel(req
->req
);
769 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
772 case UAS_TMF_LOGICAL_UNIT_RESET
:
773 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
774 qdev_reset_all(&dev
->qdev
);
775 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
);
779 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, iu
->task
.function
);
780 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
);
786 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
);
790 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
);
794 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
);
797 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
799 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
806 case UAS_PIPE_ID_COMMAND
:
807 length
= MIN(sizeof(iu
), p
->iov
.size
);
808 usb_packet_copy(p
, &iu
, length
);
811 usb_uas_command(uas
, &iu
);
813 case UAS_UI_TASK_MGMT
:
814 usb_uas_task(uas
, &iu
);
817 fprintf(stderr
, "%s: unknown command iu: id 0x%x\n",
818 __func__
, iu
.hdr
.id
);
819 p
->status
= USB_RET_STALL
;
823 case UAS_PIPE_ID_STATUS
:
825 QTAILQ_FOREACH(st
, &uas
->results
, next
) {
826 if (st
->stream
== p
->stream
) {
831 assert(uas
->status3
[p
->stream
] == NULL
);
832 uas
->status3
[p
->stream
] = p
;
833 p
->status
= USB_RET_ASYNC
;
837 st
= QTAILQ_FIRST(&uas
->results
);
839 assert(uas
->status2
== NULL
);
841 p
->status
= USB_RET_ASYNC
;
845 usb_packet_copy(p
, &st
->status
, st
->length
);
846 QTAILQ_REMOVE(&uas
->results
, st
, next
);
849 case UAS_PIPE_ID_DATA_IN
:
850 case UAS_PIPE_ID_DATA_OUT
:
852 req
= usb_uas_find_request(uas
, p
->stream
);
854 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
)
855 ? uas
->datain2
: uas
->dataout2
;
859 assert(uas
->data3
[p
->stream
] == NULL
);
860 uas
->data3
[p
->stream
] = p
;
861 p
->status
= USB_RET_ASYNC
;
864 fprintf(stderr
, "%s: no inflight request\n", __func__
);
865 p
->status
= USB_RET_STALL
;
869 scsi_req_ref(req
->req
);
871 usb_uas_copy_data(req
);
872 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
875 req
->data_async
= true;
876 p
->status
= USB_RET_ASYNC
;
878 scsi_req_unref(req
->req
);
879 usb_uas_start_next_transfer(uas
);
882 fprintf(stderr
, "%s: invalid endpoint %d\n", __func__
, p
->ep
->nr
);
883 p
->status
= USB_RET_STALL
;
888 static void usb_uas_handle_destroy(USBDevice
*dev
)
890 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
892 qemu_bh_delete(uas
->status_bh
);
895 static int usb_uas_init(USBDevice
*dev
)
897 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
899 usb_desc_create_serial(dev
);
902 QTAILQ_INIT(&uas
->results
);
903 QTAILQ_INIT(&uas
->requests
);
904 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
906 scsi_bus_new(&uas
->bus
, sizeof(uas
->bus
), DEVICE(dev
),
907 &usb_uas_scsi_info
, NULL
);
912 static const VMStateDescription vmstate_usb_uas
= {
915 .fields
= (VMStateField
[]) {
916 VMSTATE_USB_DEVICE(dev
, UASDevice
),
917 VMSTATE_END_OF_LIST()
921 static Property uas_properties
[] = {
922 DEFINE_PROP_UINT32("log-scsi-req", UASDevice
, requestlog
, 0),
923 DEFINE_PROP_END_OF_LIST(),
926 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
928 DeviceClass
*dc
= DEVICE_CLASS(klass
);
929 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
931 uc
->init
= usb_uas_init
;
932 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
933 uc
->usb_desc
= &desc
;
934 uc
->cancel_packet
= usb_uas_cancel_io
;
935 uc
->handle_attach
= usb_desc_attach
;
936 uc
->handle_reset
= usb_uas_handle_reset
;
937 uc
->handle_control
= usb_uas_handle_control
;
938 uc
->handle_data
= usb_uas_handle_data
;
939 uc
->handle_destroy
= usb_uas_handle_destroy
;
940 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
941 dc
->fw_name
= "storage";
942 dc
->vmsd
= &vmstate_usb_uas
;
943 dc
->props
= uas_properties
;
946 static const TypeInfo uas_info
= {
948 .parent
= TYPE_USB_DEVICE
,
949 .instance_size
= sizeof(UASDevice
),
950 .class_init
= usb_uas_class_initfn
,
953 static void usb_uas_register_types(void)
955 type_register_static(&uas_info
);
958 type_init(usb_uas_register_types
)