Skip to contentSkip to conversation

usb/msd: Permit relaxed ordering of IN packets

RFC PATCH In review Nicholas Piggin 2 participants 4 messages Nov 10, 2024

lore
Share & tools
lore
#0 lore raw

cover usb/msd: Permit relaxed ordering of IN packets

Hi,

This allows MSD Data-In and CSW packets before the CBW packet.

This is just for RFC for now, but it does work. Patch 2 has the main description of the problem and solution.

I've not found a good way to split this into smaller pieces yet, but I will try, also migration is currently broken, I'm not sure the best way to test that. I would like to add a MSD qtest and hopefully the XHCI transfer ring qtest could help with that, but it's still quite a big job I think.

Thanks,
Nick

Nicholas Piggin (2):
usb/msd: Add status to usb_msd_packet_complete() function usb/msd: Permit relaxed ordering of IN packets

 include/hw/usb/msd.h |  20 +-
 hw/usb/dev-storage.c | 512 ++++++++++++++++++++++++++++---------------
 2 files changed, 352 insertions(+), 180 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

RFC PATCH 1/2 usb/msd: Add status to usb_msd_packet_complete() function

This is a convenience change that accepts a status when completing a packet.

---
 hw/usb/dev-storage.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
hw/usb/dev-storage.c +8 -9
index 341e505bd0..4f1e8b7f6c 100644
@@ -177,7 +177,7 @@ static const USBDesc desc = {
     .str   = desc_strings,
 };
 
-static void usb_msd_packet_complete(MSDState *s)
+static void usb_msd_packet_complete(MSDState *s, int status)
 {
     USBPacket *p = s->packet;
 
@@ -187,6 +187,7 @@ static void usb_msd_packet_complete(MSDState *s)
      * usb_packet_complete returns.
      */
     trace_usb_msd_packet_complete();
+    p->status = status;
     s->packet = NULL;
     usb_packet_complete(&s->dev, p);
 }
@@ -196,8 +197,7 @@ static void usb_msd_fatal_error(MSDState *s)
     trace_usb_msd_fatal_error();
 
     if (s->packet) {
-        s->packet->status = USB_RET_STALL;
-        usb_msd_packet_complete(s);
+        usb_msd_packet_complete(s, USB_RET_STALL);
     }
 
     /*
@@ -255,8 +255,8 @@ void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
         usb_msd_copy_data(s, p);
         p = s->packet;
         if (p && p->actual_length == p->iov.size) {
-            p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
-            usb_msd_packet_complete(s);
+            /* USB_RET_SUCCESS status clears previous ASYNC status */
+            usb_msd_packet_complete(s, USB_RET_SUCCESS);
         }
     }
 }
@@ -295,8 +295,8 @@ void usb_msd_command_complete(SCSIRequest *req, size_t resid)
                 s->mode = USB_MSDM_CSW;
             }
         }
-        p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
-        usb_msd_packet_complete(s);
+        /* USB_RET_SUCCESS status clears previous ASYNC status */
+        usb_msd_packet_complete(s, USB_RET_SUCCESS);
     } else if (s->data_len == 0) {
         s->mode = USB_MSDM_CSW;
     }
@@ -332,8 +332,7 @@ void usb_msd_handle_reset(USBDevice *dev)
     assert(s->req == NULL);
 
     if (s->packet) {
-        s->packet->status = USB_RET_STALL;
-        usb_msd_packet_complete(s);
+        usb_msd_packet_complete(s, USB_RET_STALL);
     }
 
     memset(&s->csw, 0, sizeof(s->csw));
Signature · 1 line
2.45.2
#3 lore raw

RFC PATCH 2/2 usb/msd: Permit relaxed ordering of IN packets

In reply to Nicholas Piggin

The USB MSD protocol has 3 packets that make up a command, and only one command may be active at any time.

- CBW to start a command (that contains a SCSI request).
- DATA (IN or OUT) to send request data between host and SCSI layer.
- CSW to return status and complete the command.

DATA is optional if the request has no data.

The QEMU MSD model requires these packets to arrive in this order, CBW, DATA(*), CSW. This is the way the state machine is described in the MSD spec, and this must be how most USB stacks operate. Except AIX.

Universal Serial Bus Mass Storage Class Bulk-Only Transport 1.0 contains:

3.3 Host/Device Packet Transfer Order
The host shall send the CBW before the associated Data-Out, and the device shall send Data-In after the associated CBW and before the associated CSW. The host may request Data-In or CSW before sending the associated CBW.

Complicating matters, DATA-IN and CSW are both input packets that arrive in the same manner, so before a CBW it is impossible to determine if an IN packet is for data or CSW.

This reworks the msd state machine to cope with out of order packets by tracking them as an "unknown_in" packet until the CBW arrives, when they are categorized into data or CSW.

---
 include/hw/usb/msd.h |  20 +-
 hw/usb/dev-storage.c | 505 ++++++++++++++++++++++++++++---------------
 2 files changed, 349 insertions(+), 176 deletions(-)
include/hw/usb/msd.h +13 -7
index f9fd862b52..847a708890 100644
@@ -10,11 +10,11 @@
 #include "hw/usb.h"
 #include "hw/scsi/scsi.h"
 
-enum USBMSDMode {
-    USB_MSDM_CBW, /* Command Block.  */
-    USB_MSDM_DATAOUT, /* Transfer data to device.  */
-    USB_MSDM_DATAIN, /* Transfer data from device.  */
-    USB_MSDM_CSW /* Command Status.  */
+enum USBMSDCBWType {
+    USB_MSD_CBW_NONE       = 0,
+    USB_MSD_CBW_ZERO,           /* zero-length */
+    USB_MSD_CBW_DATAIN,         /* data-in command */
+    USB_MSD_CBW_DATAOUT,        /* data-out command */
 };
 
 struct QEMU_PACKED usb_msd_csw {
@@ -26,15 +26,21 @@ struct QEMU_PACKED usb_msd_csw {
 
 struct MSDState {
     USBDevice dev;
-    enum USBMSDMode mode;
+    bool ready;
+    bool cmd_done;
+    enum USBMSDCBWType cbw_type;
     uint32_t scsi_off;
     uint32_t scsi_len;
     uint32_t data_len;
     struct usb_msd_csw csw;
     SCSIRequest *req;
     SCSIBus bus;
+
     /* For async completion.  */
-    USBPacket *packet;
+    USBPacket *data_packet;
+    USBPacket *csw_in_packet;
+    USBPacket *unknown_in_packet;
+
     /* usb-storage only */
     BlockConf conf;
     bool removable;
hw/usb/dev-storage.c +336 -169

645 lines not on this page · raw message

Signature · 1 line
2.45.2