Skip to contentSkip to conversation

usb/scsi: usb attached scsi emulation

PATCH In review Gerd Hoffmann 2 participants 6 messages Jul 9, 2012

lore
Share & tools
lore
#0 lore raw

cover usb/scsi: usb attached scsi emulation

Hi,

v2 of the usb attached scsi emulation patches. Patch #1 is almost unmodified compared to v1. Patch #2 is new and makes UAS emulation use the new free_request callback (patch just posted by paolo) and obviously depends on that patch to compile.

cheers,
Gerd

Gerd Hoffmann (2):
usb: add usb attached scsi emulation
uas: use scsi req refcounting + free_request callback

 docs/usb-storage.txt |   38 +++
 hw/usb/Makefile.objs |    1 +
 hw/usb/dev-uas.c     |  779 ++++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |   14 +
 4 files changed, 832 insertions(+), 0 deletions(-)
 create mode 100644 docs/usb-storage.txt
 create mode 100644 hw/usb/dev-uas.c
#1 lore raw

PATCH 1/2 usb: add usb attached scsi emulation

$subject says all. First cut.

It's a pure UAS (usb attached scsi) emulation, without BOT (bulk-only transport) compatibility. If your guest can't handle it use usb-storage instead.

The emulation works like any other scsi hba emulation (eps, lsi, virtio, megasas, ...). It provides just the HBA where you can attach scsi devices as you like using '-device'. A single scsi target with up to 256 luns is supported.

For now only usb 2.0 transport is supported. This will change in the future though as I plan to use this as playground when codeing up & testing usb 3.0 transport and streams support in the qemu usb core and the xhci emulation.

No migration support yet. I'm planning to add usb 3.0 support first as this probably requires saving additional state.

Special thanks go to Paolo for bringing the qemu scsi emulation into shape, so this can be added nicely without having to touch a single line of scsi code.

---
 docs/usb-storage.txt |   38 +++
 hw/usb/Makefile.objs |    1 +
 hw/usb/dev-uas.c     |  792 ++++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |   14 +
 4 files changed, 845 insertions(+), 0 deletions(-)
 create mode 100644 docs/usb-storage.txt
 create mode 100644 hw/usb/dev-uas.c
docs/usb-storage.txt new file +38 -0
new file mode 100644
index 0000000..ff97559
@@ -0,0 +1,38 @@
+
+qemu usb storage emulation
+--------------------------
+
+Qemu has two emulations for usb storage devices.
+
+Number one emulates the classic bulk-only transport protocol which is
+used by 99% of the usb sticks on the marked today and is called
+"usb-storage".  Usage (hooking up to xhci, other host controllers work
+too):
+
+  qemu ${other_vm_args}                                \
+       -drive if=none,id=stick,file=/path/to/file.img  \
+       -device nec-usb-xhci,id=xhci                    \
+       -device usb-storage,bus=xhci.0,drive=stick
+
+
+Number two is the newer usb attached scsi transport.  This one doesn't
+automagically create a scsi disk, so you have to explicitly attach one
+manually.  Multiple logical units are supported.  Here is an example
+with tree logical units:
+
+  qemu ${other_vm_args}                                                \
+       -drive if=none,id=uas-disk1,file=/path/to/file1.img             \
+       -drive if=none,id=uas-disk2,file=/path/to/file2.img             \
+       -drive if=none,id=uas-cdrom,media=cdrom,file=/path/to/image.iso \
+       -device nec-usb-xhci,id=xhci                                    \
+       -device usb-uas,id=uas,bus=xhci.0                               \
+       -device scsi-hd,bus=uas.0,scsi-id=0,lun=0,drive=uas-disk1       \
+       -device scsi-hd,bus=uas.0,scsi-id=0,lun=1,drive=uas-disk2       \
+       -device scsi-cd,bus=uas.0,scsi-id=0,lun=5,drive=uas-cdrom
+
+
+enjoy,
+  Gerd
+
+--
+Gerd Hoffmann <[email protected]>
hw/usb/Makefile.objs +1 -0
index 9c7ddf5..4225136 100644
@@ -11,3 +11,4 @@ common-obj-y += core.o bus.o desc.o dev-hub.o
 common-obj-y += host-$(HOST_USB).o dev-bluetooth.o
 common-obj-y += dev-hid.o dev-storage.o dev-wacom.o
 common-obj-y += dev-serial.o dev-network.o dev-audio.o
+common-obj-y += dev-uas.o
hw/usb/dev-uas.c new file +792 -0

792 lines not on this page · raw message

trace-events +14 -0
index c33d58c..6cc910e 100644
@@ -347,6 +347,20 @@ usb_hub_clear_port_feature(int addr, int nr, const char *f) "dev %d, port %d, fe
 usb_hub_attach(int addr, int nr) "dev %d, port %d"
 usb_hub_detach(int addr, int nr) "dev %d, port %d"
 
+# hw/usb/dev-uas.c
+usb_uas_reset(int addr) "dev %d"
+usb_uas_command(int addr, uint16_t tag, int lun, uint32_t lun64_1, uint32_t lun64_2) "dev %d, tag 0x%x, lun %d, lun64 %08x-%08x"
+usb_uas_response(int addr, uint16_t tag, uint8_t code) "dev %d, tag 0x%x, code 0x%x"
+usb_uas_sense(int addr, uint16_t tag, uint8_t status) "dev %d, tag 0x%x, status 0x%x"
+usb_uas_read_ready(int addr, uint16_t tag) "dev %d, tag 0x%x"
+usb_uas_write_ready(int addr, uint16_t tag) "dev %d, tag 0x%x"
+usb_uas_xfer_data(int addr, uint16_t tag, uint32_t copy, uint32_t uoff, uint32_t usize, uint32_t soff, uint32_t ssize) "dev %d, tag 0x%x, copy %d, usb-pkt %d/%d, scsi-buf %d/%d"
+usb_uas_scsi_data(int addr, uint16_t tag, uint32_t bytes) "dev %d, tag 0x%x, bytes %d"
+usb_uas_scsi_complete(int addr, uint16_t tag, uint32_t status, uint32_t resid) "dev %d, tag 0x%x, status 0x%x, residue %d"
+usb_uas_tmf_abort_task(int addr, uint16_t tag, uint16_t task_tag) "dev %d, tag 0x%x, task-tag 0x%x"
+usb_uas_tmf_logical_unit_reset(int addr, uint16_t tag, int lun) "dev %d, tag 0x%x, lun %d"
+usb_uas_tmf_unsupported(int addr, uint16_t tag, uint32_t function) "dev %d, tag 0x%x, function 0x%x"
+
 # hw/usb/host-linux.c
 usb_host_open_started(int bus, int addr) "dev %d:%d"
 usb_host_open_success(int bus, int addr) "dev %d:%d"
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/2 uas: use scsi req refcounting + free_request callback

In reply to Gerd Hoffmann

With the new free_request callback for SCSIBusInfo we can use scsi request refcounting instead of our own for the uas request lifecycle.

---
 hw/usb/dev-uas.c |   33 ++++++++++-----------------------
 1 files changed, 10 insertions(+), 23 deletions(-)
hw/usb/dev-uas.c +10 -23
index 21fc80e..9b02ff4 100644
@@ -123,11 +123,11 @@ struct UASRequest {
     USBPacket    *data;
     bool         data_async;
     bool         active;
+    bool         complete;
     uint32_t     buf_off;
     uint32_t     buf_size;
     uint32_t     data_off;
     uint32_t     data_size;
-    uint32_t     refcount;
     QTAILQ_ENTRY(UASRequest)  next;
 };
 
@@ -381,7 +381,7 @@ static void usb_uas_start_next_transfer(UASDevice *uas)
     UASRequest *req;
 
     QTAILQ_FOREACH(req, &uas->requests, next) {
-        if (req->active) {
+        if (req->active || req->complete) {
             continue;
         }
         if (req->req->cmd.mode == SCSI_XFER_FROM_DEV && uas->datain == NULL) {
@@ -408,12 +408,12 @@ static UASRequest *usb_uas_alloc_request(UASDevice *uas, uas_ui *ui)
     req->tag = be16_to_cpu(ui->hdr.tag);
     req->lun = be64_to_cpu(ui->command.lun);
     req->dev = usb_uas_get_dev(req->uas, req->lun);
-    req->refcount = 1;
     return req;
 }
 
-static void usb_uas_release_request(UASRequest *req)
+static void usb_uas_scsi_free_request(SCSIBus *bus, void *priv)
 {
+    UASRequest *req = priv;
     UASDevice *uas = req->uas;
 
     if (req == uas->datain) {
@@ -438,19 +438,6 @@ static UASRequest *usb_uas_find_request(UASDevice *uas, uint16_t tag)
     return NULL;
 }
 
-static void usb_uas_ref_request(UASRequest *req)
-{
-    req->refcount++;
-}
-
-static void usb_uas_unref_request(UASRequest *req)
-{
-    req->refcount--;
-    if (req->refcount == 0) {
-        usb_uas_release_request(req);
-    }
-}
-
 static void usb_uas_scsi_transfer_data(SCSIRequest *r, uint32_t len)
 {
     UASRequest *req = r->hba_private;
@@ -472,13 +459,12 @@ static void usb_uas_scsi_command_complete(SCSIRequest *r,
     UASDevice *uas = req->uas;
 
     trace_usb_uas_scsi_complete(req->uas->dev.addr, req->tag, status, resid);
+    req->complete = true;
     if (req->data) {
         usb_uas_complete_data_packet(req);
     }
     usb_uas_queue_sense(req, status);
     scsi_req_unref(req->req);
-    req->req = NULL;
-    usb_uas_unref_request(req);
     usb_uas_start_next_transfer(uas);
 }
 
@@ -487,7 +473,7 @@ static void usb_uas_scsi_request_cancelled(SCSIRequest *r)
     UASRequest *req = r->hba_private;
 
     /* FIXME: queue notification to status pipe? */
-    usb_uas_unref_request(req);
+    scsi_req_unref(req->req);
 }
 
 static const struct SCSIBusInfo usb_uas_scsi_info = {
@@ -498,6 +484,7 @@ static const struct SCSIBusInfo usb_uas_scsi_info = {
     .transfer_data = usb_uas_scsi_transfer_data,
     .complete = usb_uas_scsi_command_complete,
     .cancel = usb_uas_scsi_request_cancelled,
+    .free_request = usb_uas_scsi_free_request,
 };
 
 /* --------------------------------------------------------------------- */
@@ -706,17 +693,17 @@ static int usb_uas_handle_data(USBDevice *dev, USBPacket *p)
             ret = USB_RET_STALL;
             break;
         }
-        usb_uas_ref_request(req);
+        scsi_req_ref(req->req);
         req->data = p;
         usb_uas_copy_data(req);
-        if (p->result == p->iov.size || req->req == NULL) {
+        if (p->result == p->iov.size || req->complete) {
             req->data = NULL;
             ret = p->result;
         } else {
             req->data_async = true;
             ret = USB_RET_ASYNC;
         }
-        usb_uas_unref_request(req);
+        scsi_req_unref(req->req);
         usb_uas_start_next_transfer(uas);
         break;
     default:
Signature · 1 line
1.7.1
#3 lore raw
In reply to Gerd Hoffmann
Quoted 6 lines · Gerd Hoffmann

Hi,

v2 of the usb attached scsi emulation patches. Patch #1 is almost unmodified compared to v1. Patch #2 is new and makes UAS emulation use the new free_request callback (patch just posted by paolo) and obviously depends on that patch to compile.

Pushed with paolos patch and all pending usb bugfixes to "git://git.kraxel.org/qemu usb.56" for those who want to play with it.

cheers,
Gerd

#4 lore raw

Il 09/07/2012 12:39, Gerd Hoffmann ha scritto:

Quoted 10 lines · Gerd Hoffmann
Quoted 6 lines

Hi,

v2 of the usb attached scsi emulation patches. Patch #1 is almost unmodified compared to v1. Patch #2 is new and makes UAS emulation use the new free_request callback (patch just posted by paolo) and obviously depends on that patch to compile.

Pushed with paolos patch and all pending usb bugfixes to "git://git.kraxel.org/qemu usb.56" for those who want to play with it.

Feel free to include my patch in a pull request, of course.

Paolo

#5 lore raw
Quoted 9 lines · Paolo Bonzini

Il 09/07/2012 12:39, Gerd Hoffmann ha scritto:

Quoted 8 lines · Gerd Hoffmann
Quoted 6 lines

Hi,

v2 of the usb attached scsi emulation patches. Patch #1 is almost unmodified compared to v1. Patch #2 is new and makes UAS emulation use the new free_request callback (patch just posted by paolo) and obviously depends on that patch to compile.

Quoted 4 lines
Quoted 2 lines

Pushed with paolos patch and all pending usb bugfixes to "git://git.kraxel.org/qemu usb.56" for those who want to play with it.

Feel free to include my patch in a pull request, of course.

Ok, will do when I send out the uas pull request, just waiting a bit so people have some time to review, also anthony is on vacation anyway atm.

cheers,
Gerd