From nobody Wed Nov 27 14:35:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1731210075; cv=none; d=zohomail.com; s=zohoarc; b=cv0mW2wrCpaeOAmC4MnXIQK+/FMiVw7Qmbfsi6j7hXaEE/VoJUhYamxXQYgamnk+5NMp+RS8ffSG9Tzh0mdE9gA/60DqTA6nQJA2aV7JTCrjwKuAU8QQJUMrvASqD82FXUdvJXfpwjdyrb7v7wP5A3oIJkYb7KDh3W8Q29OAHHM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731210075; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=o4v7dNh0tFihXRU3YW4bfnWuiADJBD6mg/js2X/rZcE=; b=Z3gdJqvfgVrVttuQFvLHFZVDFvcziElZr1+W635yLS/7u2PiIecnNmC2EQQMspNqSUWyRWfwMVTKQsXcJ68d44L8Q29AwscMY1bddwmJ99QNhC8bdfzWG+n7Cuf8KC9vta+OV1d/7k0KCp7b4TTNptKGZP4TL/Gw24LP0k6Oyx8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731210075339227.33370493283144; Sat, 9 Nov 2024 19:41:15 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t9yoJ-0007gk-Tv; Sat, 09 Nov 2024 22:40:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t9yoD-0007fU-J4 for qemu-devel@nongnu.org; Sat, 09 Nov 2024 22:40:18 -0500 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1t9yoA-0000QN-Qs for qemu-devel@nongnu.org; Sat, 09 Nov 2024 22:40:17 -0500 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-71e4244fdc6so2959460b3a.0 for ; Sat, 09 Nov 2024 19:40:14 -0800 (PST) Received: from wheely.local0.net (124-171-217-17.tpgi.com.au. [124.171.217.17]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7240799bb78sm6345273b3a.95.2024.11.09.19.40.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 19:40:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731210013; x=1731814813; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o4v7dNh0tFihXRU3YW4bfnWuiADJBD6mg/js2X/rZcE=; b=XR5H6L3NngbayT9lniqF+iR0vR6eW42tw4i/5YyoZ+ZiwSA3knXBiH/VQCRkOQRXYK wzvJvvvG+sT6+G7Neky70TRTX4o8ObwBO9wvN6+BahWDxF0ZYTo0jWXnE6tO/cJkmBW8 W83g45t5wk1zhmbOQ/CNmJaBOdASacqJ3H+uxu0nz9aHkY5XkpGEhlnGyW/esF1Jam0Z /CauRx58LrHG1sBOyuZNDEwoE7NCEdnOjVAEqdelJV6izIglpyShgVdy2qckMwwzjHqG +MS++QB6RJtsw+B5oxy3XrQwMVo55KjR59mDhwCvq02UHVTR5OFjCMMdCRYpw4u42d0c YG0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731210013; x=1731814813; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o4v7dNh0tFihXRU3YW4bfnWuiADJBD6mg/js2X/rZcE=; b=jVgnhbVHlbiTmiwmjYQoMmcic2iRDdxnZWjSzVXpkRl0DAMlknVX8KEKvXU4X2aAKU yyAAEub7cX00+JW2emSc8jJviqO0sOmq7n7Ga/AAFW1u8arzvQqKWfxySk+fAhOW2mAA T+1h3Oyih/6h0PXaN6HE9KrWk3SUAP/dx9xMcuf0XIxTSlUDj07AqE7ieTH4h2M51n15 mG+SQfwjuYcqBTMmKniyrA2ufyav4O/yPQ570c/G5ZAbNr7ywdJf/VtE4FKdSMvFoRLI /BIssSzdvl5xW2sVpxY++Rb9dYZgFm4lnFb97DFBTV+QIjo9iE07d8UldysSe+S1Tlv6 uOAg== X-Gm-Message-State: AOJu0YzOyylzkeg7EBjzoV2L62WNleS9KMHfovsqg7JRNxg3lhZJNTXk Kw90GTJ1yHjBR+HEDVnDI50tTds/z3kvg5bIlw2+qEousOUcgy7qAwFmxg== X-Google-Smtp-Source: AGHT+IEg4mtlOs/CoX2ME4AjdWJD2+onbfwjashpKfPxCCmNmTwNu94PG7YVN4B5iMfbVnpy2r5dlQ== X-Received: by 2002:a05:6a00:c89:b0:71e:6eb:786e with SMTP id d2e1a72fcca58-724132cd14amr10871276b3a.13.1731210012714; Sat, 09 Nov 2024 19:40:12 -0800 (PST) From: Nicholas Piggin To: qemu-devel@nongnu.org Cc: Nicholas Piggin , Gerd Hoffmann Subject: [RFC PATCH 2/2] usb/msd: Permit relaxed ordering of IN packets Date: Sun, 10 Nov 2024 13:40:00 +1000 Message-ID: <20241110034000.379463-3-npiggin@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241110034000.379463-1-npiggin@gmail.com> References: <20241110034000.379463-1-npiggin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::435; envelope-from=npiggin@gmail.com; helo=mail-pf1-x435.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1731210077446116600 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Nicholas Piggin --- include/hw/usb/msd.h | 20 +- hw/usb/dev-storage.c | 505 ++++++++++++++++++++++++++++--------------- 2 files changed, 349 insertions(+), 176 deletions(-) diff --git a/include/hw/usb/msd.h b/include/hw/usb/msd.h index f9fd862b52..847a708890 100644 --- a/include/hw/usb/msd.h +++ b/include/hw/usb/msd.h @@ -10,11 +10,11 @@ #include "hw/usb.h" #include "hw/scsi/scsi.h" =20 -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 =3D 0, + USB_MSD_CBW_ZERO, /* zero-length */ + USB_MSD_CBW_DATAIN, /* data-in command */ + USB_MSD_CBW_DATAOUT, /* data-out command */ }; =20 struct QEMU_PACKED usb_msd_csw { @@ -26,15 +26,21 @@ struct QEMU_PACKED usb_msd_csw { =20 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; diff --git a/hw/usb/dev-storage.c b/hw/usb/dev-storage.c index 4f1e8b7f6c..e64fcd5c6a 100644 --- a/hw/usb/dev-storage.c +++ b/hw/usb/dev-storage.c @@ -35,7 +35,7 @@ struct usb_msd_cbw { uint8_t lun; uint8_t cmd_len; uint8_t cmd[16]; -}; +} __attribute__((packed)); =20 enum { STR_MANUFACTURER =3D 1, @@ -177,18 +177,33 @@ static const USBDesc desc =3D { .str =3D desc_strings, }; =20 -static void usb_msd_packet_complete(MSDState *s, int status) +static void usb_msd_data_packet_complete(MSDState *s, int status) +{ + USBPacket *p =3D s->data_packet; + + /* + * Set s->data_packet to NULL before calling usb_packet_complete + * because another request may be issued before usb_packet_complete + * returns. + */ + trace_usb_msd_packet_complete(); + s->data_packet =3D NULL; + p->status =3D status; + usb_packet_complete(&s->dev, p); +} + +static void usb_msd_csw_packet_complete(MSDState *s, int status) { - USBPacket *p =3D s->packet; + USBPacket *p =3D s->csw_in_packet; =20 /* - * Set s->packet to NULL before calling usb_packet_complete - * because another request may be issued before - * usb_packet_complete returns. + * Set s->csw_in_packet to NULL before calling usb_packet_complete + * because another request may be issued before usb_packet_complete + * returns. */ trace_usb_msd_packet_complete(); + s->csw_in_packet =3D NULL; p->status =3D status; - s->packet =3D NULL; usb_packet_complete(&s->dev, p); } =20 @@ -196,8 +211,12 @@ static void usb_msd_fatal_error(MSDState *s) { trace_usb_msd_fatal_error(); =20 - if (s->packet) { - usb_msd_packet_complete(s, USB_RET_STALL); + if (s->data_packet) { + usb_msd_data_packet_complete(s, USB_RET_STALL); + } + + if (s->csw_in_packet) { + usb_msd_csw_packet_complete(s, USB_RET_STALL); } =20 /* @@ -242,21 +261,32 @@ static void usb_msd_send_status(MSDState *s, USBPacke= t *p) void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) { MSDState *s =3D DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); - USBPacket *p =3D s->packet; + USBPacket *p =3D s->data_packet; =20 - if ((s->mode =3D=3D USB_MSDM_DATAOUT) !=3D (req->cmd.mode =3D=3D SCSI_= XFER_TO_DEV)) { - usb_msd_fatal_error(s); - return; + if (s->cbw_type =3D=3D USB_MSD_CBW_DATAIN) { + if (req->cmd.mode =3D=3D SCSI_XFER_TO_DEV) { + usb_msd_fatal_error(s); + return; + } + } else if (s->cbw_type =3D=3D USB_MSD_CBW_DATAOUT) { + if (req->cmd.mode !=3D SCSI_XFER_TO_DEV) { + usb_msd_fatal_error(s); + return; + } + } else { + g_assert_not_reached(); } =20 + assert(s->scsi_len =3D=3D 0); s->scsi_len =3D len; s->scsi_off =3D 0; + if (p) { usb_msd_copy_data(s, p); - p =3D s->packet; + p =3D s->data_packet; if (p && p->actual_length =3D=3D p->iov.size) { /* USB_RET_SUCCESS status clears previous ASYNC status */ - usb_msd_packet_complete(s, USB_RET_SUCCESS); + usb_msd_data_packet_complete(s, USB_RET_SUCCESS); } } } @@ -264,44 +294,51 @@ void usb_msd_transfer_data(SCSIRequest *req, uint32_t= len) void usb_msd_command_complete(SCSIRequest *req, size_t resid) { MSDState *s =3D DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); - USBPacket *p =3D s->packet; + USBPacket *p =3D s->data_packet; =20 trace_usb_msd_cmd_complete(req->status, req->tag); =20 + g_assert(s->req); + /* The CBW is what starts the SCSI request */ + g_assert(s->cbw_type !=3D 0); + g_assert(!s->cmd_done); + + if (s->cbw_type =3D=3D USB_MSD_CBW_ZERO) { + g_assert(!p); + } + s->csw.sig =3D cpu_to_le32(0x53425355); s->csw.tag =3D cpu_to_le32(req->tag); s->csw.residue =3D cpu_to_le32(s->data_len); s->csw.status =3D req->status !=3D 0; =20 - if (s->packet) { - if (s->data_len =3D=3D 0 && s->mode =3D=3D USB_MSDM_DATAOUT) { - /* A deferred packet with no write data remaining must be - the status read packet. */ - usb_msd_send_status(s, p); - s->mode =3D USB_MSDM_CBW; - } else if (s->mode =3D=3D USB_MSDM_CSW) { - usb_msd_send_status(s, p); - s->mode =3D USB_MSDM_CBW; - } else { - if (s->data_len) { - int len =3D (p->iov.size - p->actual_length); - usb_packet_skip(p, len); - if (len > s->data_len) { - len =3D s->data_len; - } - s->data_len -=3D len; - } - if (s->data_len =3D=3D 0) { - s->mode =3D USB_MSDM_CSW; + /* Set cmd_done req to NULL before completing packet because another + * packet may come in after that */ + s->cmd_done =3D true; + scsi_req_unref(req); + s->req =3D NULL; + + if (p) { + if (s->data_len) { + int len =3D (p->iov.size - p->actual_length); + usb_packet_skip(p, len); + if (len > s->data_len) { + len =3D s->data_len; } + s->data_len -=3D len; } /* USB_RET_SUCCESS status clears previous ASYNC status */ - usb_msd_packet_complete(s, USB_RET_SUCCESS); - } else if (s->data_len =3D=3D 0) { - s->mode =3D USB_MSDM_CSW; + usb_msd_data_packet_complete(s, USB_RET_SUCCESS); + } + + if (s->csw_in_packet) { + g_assert(s->csw_in_packet); + usb_msd_send_status(s, s->csw_in_packet); + usb_msd_csw_packet_complete(s, USB_RET_SUCCESS); + s->cbw_type =3D USB_MSD_CBW_NONE; + s->cmd_done =3D false; + s->ready =3D true; } - scsi_req_unref(req); - s->req =3D NULL; } =20 void usb_msd_request_cancelled(SCSIRequest *req) @@ -315,9 +352,10 @@ void usb_msd_request_cancelled(SCSIRequest *req) s->csw.tag =3D cpu_to_le32(req->tag); s->csw.status =3D 1; /* error */ =20 + s->scsi_len =3D 0; + s->cmd_done =3D true; scsi_req_unref(s->req); s->req =3D NULL; - s->scsi_len =3D 0; } } =20 @@ -331,12 +369,19 @@ void usb_msd_handle_reset(USBDevice *dev) } assert(s->req =3D=3D NULL); =20 - if (s->packet) { - usb_msd_packet_complete(s, USB_RET_STALL); + if (s->data_packet) { + usb_msd_data_packet_complete(s, USB_RET_STALL); } =20 + if (s->csw_in_packet) { + usb_msd_csw_packet_complete(s, USB_RET_STALL); + } + + s->unknown_in_packet =3D NULL; memset(&s->csw, 0, sizeof(s->csw)); - s->mode =3D USB_MSDM_CBW; + s->cbw_type =3D USB_MSD_CBW_NONE; + s->cmd_done =3D false; + s->ready =3D true; =20 s->needs_reset =3D false; } @@ -355,11 +400,12 @@ static void usb_msd_handle_control(USBDevice *dev, US= BPacket *p, =20 switch (request) { case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: + /* XXX clear halt not implemented */ break; /* Class specific requests. */ case ClassInterfaceOutRequest | MassStorageReset: /* Reset state ready for the next CBW. */ - s->mode =3D USB_MSDM_CBW; + usb_msd_handle_reset(dev); break; case ClassInterfaceRequest | GetMaxLun: maxlun =3D 0; @@ -387,166 +433,284 @@ static void usb_msd_cancel_io(USBDevice *dev, USBPa= cket *p) { MSDState *s =3D USB_STORAGE_DEV(dev); =20 - assert(s->packet =3D=3D p); - s->packet =3D NULL; - - if (s->req) { - scsi_req_cancel(s->req); + if (p =3D=3D s->data_packet) { + s->data_packet =3D NULL; + if (s->req) { + scsi_req_cancel(s->req); + } + } else if (p =3D=3D s->csw_in_packet) { + s->csw_in_packet =3D NULL; + } else if (p =3D=3D s->unknown_in_packet) { + s->unknown_in_packet =3D NULL; + } else { + g_assert_not_reached(); } } =20 -static void usb_msd_handle_data(USBDevice *dev, USBPacket *p) +static void usb_msd_handle_data_out(USBDevice *dev, USBPacket *p) { MSDState *s =3D (MSDState *)dev; - uint32_t tag; - struct usb_msd_cbw cbw; - uint8_t devep =3D p->ep->nr; - SCSIDevice *scsi_dev; - int len; =20 - if (s->needs_reset) { - p->status =3D USB_RET_STALL; - return; - } + if (!s->cbw_type) { + uint32_t tag; + struct usb_msd_cbw cbw; + SCSIDevice *scsi_dev; + int len; =20 - switch (p->pid) { - case USB_TOKEN_OUT: - if (devep !=3D 2) + if (p->iov.size !=3D 31) { + error_report("usb-msd: Bad CBW size %ld", p->iov.size); + goto fail; + } + usb_packet_copy(p, &cbw, 31); + if (le32_to_cpu(cbw.sig) !=3D 0x43425355) { + error_report("usb-msd: Bad signature %08x", + le32_to_cpu(cbw.sig)); + goto fail; + } + scsi_dev =3D scsi_device_find(&s->bus, 0, 0, cbw.lun); + if (scsi_dev =3D=3D NULL) { + error_report("usb-msd: Bad LUN %d", cbw.lun); goto fail; + } =20 - switch (s->mode) { - case USB_MSDM_CBW: - if (p->iov.size !=3D 31) { - error_report("usb-msd: Bad CBW size"); - goto fail; - } - usb_packet_copy(p, &cbw, 31); - if (le32_to_cpu(cbw.sig) !=3D 0x43425355) { - error_report("usb-msd: Bad signature %08x", - le32_to_cpu(cbw.sig)); - goto fail; - } - scsi_dev =3D scsi_device_find(&s->bus, 0, 0, cbw.lun); - if (scsi_dev =3D=3D NULL) { - error_report("usb-msd: Bad LUN %d", cbw.lun); - goto fail; + tag =3D le32_to_cpu(cbw.tag); + s->data_len =3D le32_to_cpu(cbw.data_len); + + s->ready =3D false; + if ((cbw.flags & 0x80) && s->data_len > 0) { + s->cbw_type =3D USB_MSD_CBW_DATAIN; + if (s->unknown_in_packet) { + /* In packet must be be DATAIN */ + g_assert(!s->data_packet); + s->data_packet =3D s->unknown_in_packet; + s->unknown_in_packet =3D NULL; } - tag =3D le32_to_cpu(cbw.tag); - s->data_len =3D le32_to_cpu(cbw.data_len); + + } else { if (s->data_len =3D=3D 0) { - s->mode =3D USB_MSDM_CSW; - } else if (cbw.flags & 0x80) { - s->mode =3D USB_MSDM_DATAIN; + s->cbw_type =3D USB_MSD_CBW_ZERO; } else { - s->mode =3D USB_MSDM_DATAOUT; - } - trace_usb_msd_cmd_submit(cbw.lun, tag, cbw.flags, - cbw.cmd_len, s->data_len); - assert(le32_to_cpu(s->csw.residue) =3D=3D 0); - s->scsi_len =3D 0; - s->req =3D scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, cbw.c= md_len, NULL); - if (s->commandlog) { - scsi_req_print(s->req); + s->cbw_type =3D USB_MSD_CBW_DATAOUT; } - len =3D scsi_req_enqueue(s->req); - if (len) { - scsi_req_continue(s->req); - } - break; + if (s->unknown_in_packet) { + /* In packet must be the CSW */ + if (s->unknown_in_packet->iov.size !=3D 13) { + /* + * Not strictly within spec to reject size > 13? But + * could catch some guest errors. + */ + error_report("usb-msd: CSW size %zd !=3D 13\n", + s->unknown_in_packet->iov.size); + goto fail; + } =20 - case USB_MSDM_DATAOUT: - trace_usb_msd_data_out(p->iov.size, s->data_len); - if (p->iov.size > s->data_len) { - goto fail; + /* In packet must be be CSW */ + s->csw_in_packet =3D s->unknown_in_packet; + s->unknown_in_packet =3D NULL; } + } + trace_usb_msd_cmd_submit(cbw.lun, tag, cbw.flags, + cbw.cmd_len, s->data_len); + assert(le32_to_cpu(s->csw.residue) =3D=3D 0); + assert(s->scsi_len =3D=3D 0); + s->req =3D scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, + cbw.cmd_len, NULL); + if (s->commandlog) { + scsi_req_print(s->req); + } + len =3D scsi_req_enqueue(s->req); + if (len) { + scsi_req_continue(s->req); + } =20 - if (s->scsi_len) { - usb_msd_copy_data(s, p); - } - if (le32_to_cpu(s->csw.residue)) { - len =3D p->iov.size - p->actual_length; - if (len) { - usb_packet_skip(p, len); - if (len > s->data_len) { - len =3D s->data_len; - } - s->data_len -=3D len; - if (s->data_len =3D=3D 0) { - s->mode =3D USB_MSDM_CSW; - } + } else if (s->cbw_type =3D=3D USB_MSD_CBW_DATAOUT) { + trace_usb_msd_data_out(p->iov.size, s->data_len); + if (s->data_packet) { + error_report("usb-msd: dataout packet while dataout in progres= s"); + goto fail; + } + if (s->unknown_in_packet) { + error_report("usb-msd: dataout packet with unknown in packet"); + goto fail; + } + if (p->iov.size > s->data_len) { + error_report("usb-msd: dataout packet too large for CBW"); + goto fail; + } + + if (s->scsi_len) { + usb_msd_copy_data(s, p); + } + + if (s->cmd_done) { + int len; + + g_assert(!s->req); + len =3D p->iov.size - p->actual_length; + if (len) { + usb_packet_skip(p, len); + if (len > s->data_len) { + len =3D s->data_len; } + s->data_len -=3D len; + } + if (s->csw_in_packet) { + usb_msd_send_status(s, s->csw_in_packet); + usb_msd_csw_packet_complete(s, USB_RET_SUCCESS); + s->cbw_type =3D USB_MSD_CBW_NONE; + s->cmd_done =3D false; + s->ready =3D true; } + } else { if (p->actual_length < p->iov.size) { trace_usb_msd_packet_async(); - s->packet =3D p; + assert(!s->csw_in_packet); + s->data_packet =3D p; p->status =3D USB_RET_ASYNC; } - break; - - default: - goto fail; } - break; + } else { /* USB_MSD_CBW_ZERO or USB_MSD_CBW_DATAIN */ + error_report("usb-msd: unexpected out packet in state %x", + s->cbw_type); +fail: + p->status =3D USB_RET_STALL; + } +} =20 - case USB_TOKEN_IN: - if (devep !=3D 1) - goto fail; +static void usb_msd_handle_data_in(USBDevice *dev, USBPacket *p) +{ + MSDState *s =3D (MSDState *)dev; =20 - switch (s->mode) { - case USB_MSDM_DATAOUT: - if (s->data_len !=3D 0 || p->iov.size < 13) { - goto fail; + if (!s->cbw_type) { + s->ready =3D false; + if (s->unknown_in_packet) { + /* + * It's unclear from the USB MSD spec if this is permitted, but + * for now we don't try to handle it. + */ + error_report("usb-msd: 2nd datain packet before CBW"); + goto fail; + } + g_assert(!s->req); + g_assert(!s->data_packet); + g_assert(!s->csw_in_packet); + s->unknown_in_packet =3D p; + trace_usb_msd_packet_async(); + p->status =3D USB_RET_ASYNC; + + } else if (s->cmd_done) { + g_assert(!s->req); + if (s->data_len) { + int len =3D (p->iov.size - p->actual_length); + usb_packet_skip(p, len); + if (len > s->data_len) { + len =3D s->data_len; } - /* Waiting for SCSI write to complete. */ - trace_usb_msd_packet_async(); - s->packet =3D p; - p->status =3D USB_RET_ASYNC; - break; - - case USB_MSDM_CSW: - if (p->iov.size < 13) { + s->data_len -=3D len; + } else { + if (p->iov.size !=3D 13) { + qemu_log("usb-msd: CSD size is %ld (expecting 13)\n", p->i= ov.size); goto fail; } =20 - if (s->req) { - /* still in flight */ - trace_usb_msd_packet_async(); - s->packet =3D p; - p->status =3D USB_RET_ASYNC; - } else { - usb_msd_send_status(s, p); - s->mode =3D USB_MSDM_CBW; - } - break; + usb_msd_send_status(s, p); + s->cbw_type =3D USB_MSD_CBW_NONE; + s->cmd_done =3D false; + s->ready =3D true; + } =20 - case USB_MSDM_DATAIN: - trace_usb_msd_data_in(p->iov.size, s->data_len, s->scsi_len); - if (s->scsi_len) { - usb_msd_copy_data(s, p); - } - if (le32_to_cpu(s->csw.residue)) { - len =3D p->iov.size - p->actual_length; - if (len) { - usb_packet_skip(p, len); - if (len > s->data_len) { - len =3D s->data_len; - } - s->data_len -=3D len; - if (s->data_len =3D=3D 0) { - s->mode =3D USB_MSDM_CSW; - } + } else if (s->cbw_type =3D=3D USB_MSD_CBW_DATAIN) { + trace_usb_msd_data_in(p->iov.size, s->data_len, s->scsi_len); + if (s->data_packet) { + error_report("usb-msd: data packet while datain in progress"); + goto fail; + } + g_assert(!s->data_packet); + g_assert(!s->unknown_in_packet); + g_assert(!s->csw_in_packet); + + if (s->scsi_len) { + usb_msd_copy_data(s, p); + } + + if (s->cmd_done) { + int len; + + g_assert(!s->req); + len =3D p->iov.size - p->actual_length; + if (len) { + usb_packet_skip(p, len); + if (len > s->data_len) { + len =3D s->data_len; } + s->data_len -=3D len; + g_assert(s->data_len =3D=3D 0); } - if (p->actual_length < p->iov.size && s->mode =3D=3D USB_MSDM_= DATAIN) { + if (p->status =3D=3D USB_RET_ASYNC) { + usb_msd_data_packet_complete(s, USB_RET_SUCCESS); + } + } else { + if (p->actual_length < p->iov.size) { trace_usb_msd_packet_async(); - s->packet =3D p; + s->data_packet =3D p; p->status =3D USB_RET_ASYNC; } - break; + } + } else { /* USB_MSD_CBW_ZERO or USB_MSD_CBW_DATAOUT */ + if (s->csw_in_packet) { + /* + * It's unclear from the USB MSD spec if this is permitted, but + * for now we don't try to handle it. + */ + error_report("usb-msd: 2nd CSW packet"); + goto fail; + } + g_assert(!s->unknown_in_packet); + if (s->cbw_type =3D=3D USB_MSD_CBW_ZERO) { + g_assert(s->data_len =3D=3D 0); + g_assert(!s->data_packet); + } + g_assert(s->req); + g_assert(!s->unknown_in_packet); =20 - default: + if (p->iov.size !=3D 13) { + qemu_log("usb-msd: CSD size is %ld (expecting 13)\n", p->iov.s= ize); goto fail; } + + /* Waiting for SCSI zero/write to complete. */ + trace_usb_msd_packet_async(); + s->csw_in_packet =3D p; + p->status =3D USB_RET_ASYNC; + } + return; +fail: + p->status =3D USB_RET_STALL; +} + +static void usb_msd_handle_data(USBDevice *dev, USBPacket *p) +{ + MSDState *s =3D (MSDState *)dev; + uint8_t devep =3D p->ep->nr; + + if (s->needs_reset) { + p->status =3D USB_RET_STALL; + return; + } + + switch (p->pid) { + case USB_TOKEN_OUT: + if (devep !=3D 2) { + goto fail; + } + usb_msd_handle_data_out(dev, p); + break; + + case USB_TOKEN_IN: + if (devep !=3D 1) { + goto fail; + } + usb_msd_handle_data_in(dev, p); break; =20 default: @@ -573,7 +737,7 @@ static const VMStateDescription vmstate_usb_msd =3D { .minimum_version_id =3D 1, .fields =3D (const VMStateField[]) { VMSTATE_USB_DEVICE(dev, MSDState), - VMSTATE_UINT32(mode, MSDState), +// VMSTATE_UINT32(state, MSDState), VMSTATE_UINT32(scsi_len, MSDState), VMSTATE_UINT32(scsi_off, MSDState), VMSTATE_UINT32(data_len, MSDState), @@ -612,6 +776,9 @@ static const TypeInfo usb_storage_dev_type_info =3D { =20 static void usb_msd_register_types(void) { + qemu_build_assert(sizeof(struct usb_msd_cbw) =3D=3D 31); + qemu_build_assert(sizeof(struct usb_msd_csw) =3D=3D 13); + type_register_static(&usb_storage_dev_type_info); } =20 --=20 2.45.2