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.h"
18 #include "hw/usb/desc.h"
20 #include "hw/scsi-defs.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 typedef struct UASDevice UASDevice
;
103 typedef struct UASRequest UASRequest
;
104 typedef struct UASStatus UASStatus
;
113 QTAILQ_HEAD(, UASStatus
) results
;
114 QTAILQ_HEAD(, UASRequest
) requests
;
131 QTAILQ_ENTRY(UASRequest
) next
;
137 QTAILQ_ENTRY(UASStatus
) next
;
140 /* --------------------------------------------------------------------- */
143 STR_MANUFACTURER
= 1,
149 static const USBDescStrings desc_strings
= {
150 [STR_MANUFACTURER
] = "QEMU",
151 [STR_PRODUCT
] = "USB Attached SCSI HBA",
152 [STR_SERIALNUMBER
] = "27842",
153 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
156 static const USBDescIface desc_iface_high
= {
157 .bInterfaceNumber
= 0,
159 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
160 .bInterfaceSubClass
= 0x06, /* SCSI */
161 .bInterfaceProtocol
= 0x62, /* UAS */
162 .eps
= (USBDescEndpoint
[]) {
164 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_COMMAND
,
165 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
166 .wMaxPacketSize
= 512,
167 .extra
= (uint8_t[]) {
168 0x04, /* u8 bLength */
169 0x24, /* u8 bDescriptorType */
171 0x00, /* u8 bReserved */
174 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_STATUS
,
175 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
176 .wMaxPacketSize
= 512,
177 .extra
= (uint8_t[]) {
178 0x04, /* u8 bLength */
179 0x24, /* u8 bDescriptorType */
181 0x00, /* u8 bReserved */
184 .bEndpointAddress
= USB_DIR_IN
| UAS_PIPE_ID_DATA_IN
,
185 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
186 .wMaxPacketSize
= 512,
187 .extra
= (uint8_t[]) {
188 0x04, /* u8 bLength */
189 0x24, /* u8 bDescriptorType */
191 0x00, /* u8 bReserved */
194 .bEndpointAddress
= USB_DIR_OUT
| UAS_PIPE_ID_DATA_OUT
,
195 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
196 .wMaxPacketSize
= 512,
197 .extra
= (uint8_t[]) {
198 0x04, /* u8 bLength */
199 0x24, /* u8 bDescriptorType */
200 UAS_PIPE_ID_DATA_OUT
,
201 0x00, /* u8 bReserved */
207 static const USBDescDevice desc_device_high
= {
209 .bMaxPacketSize0
= 64,
210 .bNumConfigurations
= 1,
211 .confs
= (USBDescConfig
[]) {
214 .bConfigurationValue
= 1,
215 .iConfiguration
= STR_CONFIG_HIGH
,
216 .bmAttributes
= 0xc0,
218 .ifs
= &desc_iface_high
,
223 static const USBDesc desc
= {
225 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
228 .iManufacturer
= STR_MANUFACTURER
,
229 .iProduct
= STR_PRODUCT
,
230 .iSerialNumber
= STR_SERIALNUMBER
,
232 .high
= &desc_device_high
,
236 /* --------------------------------------------------------------------- */
238 static UASStatus
*usb_uas_alloc_status(uint8_t id
, uint16_t tag
)
240 UASStatus
*st
= g_new0(UASStatus
, 1);
242 st
->status
.hdr
.id
= id
;
243 st
->status
.hdr
.tag
= cpu_to_be16(tag
);
244 st
->length
= sizeof(uas_ui_header
);
248 static void usb_uas_send_status_bh(void *opaque
)
250 UASDevice
*uas
= opaque
;
251 UASStatus
*st
= QTAILQ_FIRST(&uas
->results
);
252 USBPacket
*p
= uas
->status
;
258 usb_packet_copy(p
, &st
->status
, st
->length
);
259 QTAILQ_REMOVE(&uas
->results
, st
, next
);
262 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
263 usb_packet_complete(&uas
->dev
, p
);
266 static void usb_uas_queue_status(UASDevice
*uas
, UASStatus
*st
, int length
)
268 st
->length
+= length
;
269 QTAILQ_INSERT_TAIL(&uas
->results
, st
, next
);
272 * Just schedule bh make sure any in-flight data transaction
273 * is finished before completing (sending) the status packet.
275 qemu_bh_schedule(uas
->status_bh
);
277 USBEndpoint
*ep
= usb_ep_get(&uas
->dev
, USB_TOKEN_IN
,
283 static void usb_uas_queue_response(UASDevice
*uas
, uint16_t tag
,
284 uint8_t code
, uint16_t add_info
)
286 UASStatus
*st
= usb_uas_alloc_status(UAS_UI_RESPONSE
, tag
);
288 trace_usb_uas_response(uas
->dev
.addr
, tag
, code
);
289 st
->status
.response
.response_code
= code
;
290 st
->status
.response
.add_response_info
= cpu_to_be16(add_info
);
291 usb_uas_queue_status(uas
, st
, sizeof(uas_ui_response
));
294 static void usb_uas_queue_sense(UASRequest
*req
, uint8_t status
)
296 UASStatus
*st
= usb_uas_alloc_status(UAS_UI_SENSE
, req
->tag
);
299 trace_usb_uas_sense(req
->uas
->dev
.addr
, req
->tag
, status
);
300 st
->status
.sense
.status
= status
;
301 st
->status
.sense
.status_qualifier
= cpu_to_be16(0);
302 if (status
!= GOOD
) {
303 slen
= scsi_req_get_sense(req
->req
, st
->status
.sense
.sense_data
,
304 sizeof(st
->status
.sense
.sense_data
));
305 st
->status
.sense
.sense_length
= cpu_to_be16(slen
);
307 len
= sizeof(uas_ui_sense
) - sizeof(st
->status
.sense
.sense_data
) + slen
;
308 usb_uas_queue_status(req
->uas
, st
, len
);
311 static void usb_uas_queue_read_ready(UASRequest
*req
)
313 UASStatus
*st
= usb_uas_alloc_status(UAS_UI_READ_READY
, req
->tag
);
315 trace_usb_uas_read_ready(req
->uas
->dev
.addr
, req
->tag
);
316 usb_uas_queue_status(req
->uas
, st
, 0);
319 static void usb_uas_queue_write_ready(UASRequest
*req
)
321 UASStatus
*st
= usb_uas_alloc_status(UAS_UI_WRITE_READY
, req
->tag
);
323 trace_usb_uas_write_ready(req
->uas
->dev
.addr
, req
->tag
);
324 usb_uas_queue_status(req
->uas
, st
, 0);
327 /* --------------------------------------------------------------------- */
329 static int usb_uas_get_lun(uint64_t lun64
)
331 return (lun64
>> 48) & 0xff;
334 static SCSIDevice
*usb_uas_get_dev(UASDevice
*uas
, uint64_t lun64
)
336 if ((lun64
>> 56) != 0x00) {
339 return scsi_device_find(&uas
->bus
, 0, 0, usb_uas_get_lun(lun64
));
342 static void usb_uas_complete_data_packet(UASRequest
*req
)
346 if (!req
->data_async
) {
351 req
->data_async
= false;
352 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
353 usb_packet_complete(&req
->uas
->dev
, p
);
356 static void usb_uas_copy_data(UASRequest
*req
)
360 length
= MIN(req
->buf_size
- req
->buf_off
,
361 req
->data
->iov
.size
- req
->data
->actual_length
);
362 trace_usb_uas_xfer_data(req
->uas
->dev
.addr
, req
->tag
, length
,
363 req
->data
->actual_length
, req
->data
->iov
.size
,
364 req
->buf_off
, req
->buf_size
);
365 usb_packet_copy(req
->data
, scsi_req_get_buf(req
->req
) + req
->buf_off
,
367 req
->buf_off
+= length
;
368 req
->data_off
+= length
;
370 if (req
->data
->actual_length
== req
->data
->iov
.size
) {
371 usb_uas_complete_data_packet(req
);
373 if (req
->buf_size
&& req
->buf_off
== req
->buf_size
) {
376 scsi_req_continue(req
->req
);
380 static void usb_uas_start_next_transfer(UASDevice
*uas
)
384 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
385 if (req
->active
|| req
->complete
) {
388 if (req
->req
->cmd
.mode
== SCSI_XFER_FROM_DEV
&& uas
->datain
== NULL
) {
390 usb_uas_queue_read_ready(req
);
394 if (req
->req
->cmd
.mode
== SCSI_XFER_TO_DEV
&& uas
->dataout
== NULL
) {
396 usb_uas_queue_write_ready(req
);
403 static UASRequest
*usb_uas_alloc_request(UASDevice
*uas
, uas_ui
*ui
)
407 req
= g_new0(UASRequest
, 1);
409 req
->tag
= be16_to_cpu(ui
->hdr
.tag
);
410 req
->lun
= be64_to_cpu(ui
->command
.lun
);
411 req
->dev
= usb_uas_get_dev(req
->uas
, req
->lun
);
415 static void usb_uas_scsi_free_request(SCSIBus
*bus
, void *priv
)
417 UASRequest
*req
= priv
;
418 UASDevice
*uas
= req
->uas
;
420 if (req
== uas
->datain
) {
423 if (req
== uas
->dataout
) {
426 QTAILQ_REMOVE(&uas
->requests
, req
, next
);
428 usb_uas_start_next_transfer(uas
);
431 static UASRequest
*usb_uas_find_request(UASDevice
*uas
, uint16_t tag
)
435 QTAILQ_FOREACH(req
, &uas
->requests
, next
) {
436 if (req
->tag
== tag
) {
443 static void usb_uas_scsi_transfer_data(SCSIRequest
*r
, uint32_t len
)
445 UASRequest
*req
= r
->hba_private
;
447 trace_usb_uas_scsi_data(req
->uas
->dev
.addr
, req
->tag
, len
);
451 usb_uas_copy_data(req
);
453 usb_uas_start_next_transfer(req
->uas
);
457 static void usb_uas_scsi_command_complete(SCSIRequest
*r
,
458 uint32_t status
, size_t resid
)
460 UASRequest
*req
= r
->hba_private
;
462 trace_usb_uas_scsi_complete(req
->uas
->dev
.addr
, req
->tag
, status
, resid
);
463 req
->complete
= true;
465 usb_uas_complete_data_packet(req
);
467 usb_uas_queue_sense(req
, status
);
468 scsi_req_unref(req
->req
);
471 static void usb_uas_scsi_request_cancelled(SCSIRequest
*r
)
473 UASRequest
*req
= r
->hba_private
;
475 /* FIXME: queue notification to status pipe? */
476 scsi_req_unref(req
->req
);
479 static const struct SCSIBusInfo usb_uas_scsi_info
= {
484 .transfer_data
= usb_uas_scsi_transfer_data
,
485 .complete
= usb_uas_scsi_command_complete
,
486 .cancel
= usb_uas_scsi_request_cancelled
,
487 .free_request
= usb_uas_scsi_free_request
,
490 /* --------------------------------------------------------------------- */
492 static void usb_uas_handle_reset(USBDevice
*dev
)
494 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
495 UASRequest
*req
, *nreq
;
498 trace_usb_uas_reset(dev
->addr
);
499 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
500 scsi_req_cancel(req
->req
);
502 QTAILQ_FOREACH_SAFE(st
, &uas
->results
, next
, nst
) {
503 QTAILQ_REMOVE(&uas
->results
, st
, next
);
508 static void usb_uas_handle_control(USBDevice
*dev
, USBPacket
*p
,
509 int request
, int value
, int index
, int length
, uint8_t *data
)
513 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
517 fprintf(stderr
, "%s: unhandled control request\n", __func__
);
518 p
->status
= USB_RET_STALL
;
521 static void usb_uas_cancel_io(USBDevice
*dev
, USBPacket
*p
)
523 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
524 UASRequest
*req
, *nreq
;
526 if (uas
->status
== p
) {
528 qemu_bh_cancel(uas
->status_bh
);
531 QTAILQ_FOREACH_SAFE(req
, &uas
->requests
, next
, nreq
) {
532 if (req
->data
== p
) {
537 assert(!"canceled usb packet not found");
540 static void usb_uas_command(UASDevice
*uas
, uas_ui
*ui
)
545 req
= usb_uas_find_request(uas
, be16_to_cpu(ui
->hdr
.tag
));
549 req
= usb_uas_alloc_request(uas
, ui
);
550 if (req
->dev
== NULL
) {
554 trace_usb_uas_command(uas
->dev
.addr
, req
->tag
,
555 usb_uas_get_lun(req
->lun
),
556 req
->lun
>> 32, req
->lun
& 0xffffffff);
557 QTAILQ_INSERT_TAIL(&uas
->requests
, req
, next
);
558 req
->req
= scsi_req_new(req
->dev
, req
->tag
,
559 usb_uas_get_lun(req
->lun
),
560 ui
->command
.cdb
, req
);
561 len
= scsi_req_enqueue(req
->req
);
563 req
->data_size
= len
;
564 scsi_req_continue(req
->req
);
569 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
, 0);
574 * FIXME: Seems to upset linux, is this wrong?
575 * NOTE: Happens only with no scsi devices at the bus, not sure
576 * this is a valid UAS setup in the first place.
578 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_INVALID_INFO_UNIT
, 0);
582 static void usb_uas_task(UASDevice
*uas
, uas_ui
*ui
)
584 uint16_t tag
= be16_to_cpu(ui
->hdr
.tag
);
585 uint64_t lun64
= be64_to_cpu(ui
->task
.lun
);
586 SCSIDevice
*dev
= usb_uas_get_dev(uas
, lun64
);
587 int lun
= usb_uas_get_lun(lun64
);
591 req
= usb_uas_find_request(uas
, be16_to_cpu(ui
->hdr
.tag
));
596 switch (ui
->task
.function
) {
597 case UAS_TMF_ABORT_TASK
:
598 task_tag
= be16_to_cpu(ui
->task
.task_tag
);
599 trace_usb_uas_tmf_abort_task(uas
->dev
.addr
, tag
, task_tag
);
603 if (dev
->lun
!= lun
) {
606 req
= usb_uas_find_request(uas
, task_tag
);
607 if (req
&& req
->dev
== dev
) {
608 scsi_req_cancel(req
->req
);
610 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
, 0);
613 case UAS_TMF_LOGICAL_UNIT_RESET
:
614 trace_usb_uas_tmf_logical_unit_reset(uas
->dev
.addr
, tag
, lun
);
618 if (dev
->lun
!= lun
) {
621 qdev_reset_all(&dev
->qdev
);
622 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_COMPLETE
, 0);
626 trace_usb_uas_tmf_unsupported(uas
->dev
.addr
, tag
, ui
->task
.function
);
627 usb_uas_queue_response(uas
, tag
, UAS_RC_TMF_NOT_SUPPORTED
, 0);
633 usb_uas_queue_response(uas
, req
->tag
, UAS_RC_OVERLAPPED_TAG
, 0);
637 /* FIXME: correct? [see long comment in usb_uas_command()] */
638 usb_uas_queue_response(uas
, tag
, UAS_RC_INVALID_INFO_UNIT
, 0);
642 usb_uas_queue_response(uas
, tag
, UAS_RC_INCORRECT_LUN
, 0);
645 static void usb_uas_handle_data(USBDevice
*dev
, USBPacket
*p
)
647 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
654 case UAS_PIPE_ID_COMMAND
:
655 length
= MIN(sizeof(ui
), p
->iov
.size
);
656 usb_packet_copy(p
, &ui
, length
);
659 usb_uas_command(uas
, &ui
);
661 case UAS_UI_TASK_MGMT
:
662 usb_uas_task(uas
, &ui
);
665 fprintf(stderr
, "%s: unknown command ui: id 0x%x\n",
666 __func__
, ui
.hdr
.id
);
667 p
->status
= USB_RET_STALL
;
671 case UAS_PIPE_ID_STATUS
:
672 st
= QTAILQ_FIRST(&uas
->results
);
674 assert(uas
->status
== NULL
);
676 p
->status
= USB_RET_ASYNC
;
679 usb_packet_copy(p
, &st
->status
, st
->length
);
680 QTAILQ_REMOVE(&uas
->results
, st
, next
);
683 case UAS_PIPE_ID_DATA_IN
:
684 case UAS_PIPE_ID_DATA_OUT
:
685 req
= (p
->ep
->nr
== UAS_PIPE_ID_DATA_IN
) ? uas
->datain
: uas
->dataout
;
687 fprintf(stderr
, "%s: no inflight request\n", __func__
);
688 p
->status
= USB_RET_STALL
;
691 scsi_req_ref(req
->req
);
693 usb_uas_copy_data(req
);
694 if (p
->actual_length
== p
->iov
.size
|| req
->complete
) {
697 req
->data_async
= true;
698 p
->status
= USB_RET_ASYNC
;
700 scsi_req_unref(req
->req
);
701 usb_uas_start_next_transfer(uas
);
704 fprintf(stderr
, "%s: invalid endpoint %d\n", __func__
, p
->ep
->nr
);
705 p
->status
= USB_RET_STALL
;
710 static void usb_uas_handle_destroy(USBDevice
*dev
)
712 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
714 qemu_bh_delete(uas
->status_bh
);
717 static int usb_uas_init(USBDevice
*dev
)
719 UASDevice
*uas
= DO_UPCAST(UASDevice
, dev
, dev
);
721 usb_desc_create_serial(dev
);
724 QTAILQ_INIT(&uas
->results
);
725 QTAILQ_INIT(&uas
->requests
);
726 uas
->status_bh
= qemu_bh_new(usb_uas_send_status_bh
, uas
);
728 scsi_bus_new(&uas
->bus
, &uas
->dev
.qdev
, &usb_uas_scsi_info
);
733 static const VMStateDescription vmstate_usb_uas
= {
736 .fields
= (VMStateField
[]) {
737 VMSTATE_USB_DEVICE(dev
, UASDevice
),
738 VMSTATE_END_OF_LIST()
742 static void usb_uas_class_initfn(ObjectClass
*klass
, void *data
)
744 DeviceClass
*dc
= DEVICE_CLASS(klass
);
745 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
747 uc
->init
= usb_uas_init
;
748 uc
->product_desc
= desc_strings
[STR_PRODUCT
];
749 uc
->usb_desc
= &desc
;
750 uc
->cancel_packet
= usb_uas_cancel_io
;
751 uc
->handle_attach
= usb_desc_attach
;
752 uc
->handle_reset
= usb_uas_handle_reset
;
753 uc
->handle_control
= usb_uas_handle_control
;
754 uc
->handle_data
= usb_uas_handle_data
;
755 uc
->handle_destroy
= usb_uas_handle_destroy
;
756 dc
->fw_name
= "storage";
757 dc
->vmsd
= &vmstate_usb_uas
;
760 static TypeInfo uas_info
= {
762 .parent
= TYPE_USB_DEVICE
,
763 .instance_size
= sizeof(UASDevice
),
764 .class_init
= usb_uas_class_initfn
,
767 static void usb_uas_register_types(void)
769 type_register_static(&uas_info
);
772 type_init(usb_uas_register_types
)