From patchwork Wed Sep 21 14:21:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1680725 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=k3nBhYcG; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MXgds2H5Sz1ypX for ; Thu, 22 Sep 2022 00:24:17 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1E97C84CD1; Wed, 21 Sep 2022 16:23:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="k3nBhYcG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E517284CB6; Wed, 21 Sep 2022 16:22:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 84DBE84CAB for ; Wed, 21 Sep 2022 16:22:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ej1-x62e.google.com with SMTP id a26so14050357ejc.4 for ; Wed, 21 Sep 2022 07:22:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=PZqCYJXCZy3vXX6ogyYvIJe3JQRxJCEqbgahKsgxE+w=; b=k3nBhYcGscTWfrDHRXm0426KtFknuVTKp02QaCFlvU1EdOf8RPUNgSHWdfQWK/47t1 k9XpX/2gjExdEhteJypiGLdslRiqDr7ZMo8fzo+1Dgwoj2SE3MdJuo03crOAXxyWNslr aRnhNkcCwtEI5WfNoTSWJ5S7TNZ43GcB/S0a8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=PZqCYJXCZy3vXX6ogyYvIJe3JQRxJCEqbgahKsgxE+w=; b=sPvjd84PtHtXSGK370OY1Avx50UgaKtBlnMfuCUn15+YiyF+Vq7wEGQToaRnRinBnA IXLjPTGKQ9qBGYAl/mgYyAG0ENyM35ccnYN5LQRUNsxjk1UOAzmA6MZlnCmG+oxPXZFY 0dDrsx3XDMRP0xNxqvNZilWkcpf/WX171XOKLNmVLGWU5V+GREe9eJwmtKrodxYb3DVM wvOPM/DitrGPYHCGny+KKhuZ1YADRRGmC4s1TfuBzo/6jigypl8oo0l24bhPi6uB0Yu/ GR2hVglxIbacNnqJyZafA9OV0TW1Ef5tvQv9GPiEwoeWan4CSJjYfJDKY1vl3szlFs+a dfeA== X-Gm-Message-State: ACrzQf1k0bmJeXMIMySA43GIjXu6RJDgc1/WCCno+ULj0aC+vxL/glf3 BnbsV1hPoV/BFQWCS5z+TZ3GZt5sBJEmZPEC X-Google-Smtp-Source: AMsMyM4LPlZxS3NzNPVeIWqQ2UwOaJz8n6kn5kWwyZIXF3lLuaGjxu4bkYq9rF4XFIAPR2wMKreNng== X-Received: by 2002:a17:907:3e86:b0:6f5:917:10cc with SMTP id hs6-20020a1709073e8600b006f5091710ccmr21888378ejc.53.1663770155955; Wed, 21 Sep 2022 07:22:35 -0700 (PDT) Received: from sjg1.roam.corp.google.com (66.218.241.83.in-addr.dgcsystems.net. [83.241.218.66]) by smtp.gmail.com with ESMTPSA id 17-20020a170906219100b007807e4f6b30sm1387089eju.103.2022.09.21.07.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 07:22:35 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass , Andrew Scull , Marek Vasut , Rob Herring , Sean Anderson , Stefan Roese Subject: [PATCH v2 13/15] sandbox: scsi: Move request-handling code to scsi_emul Date: Wed, 21 Sep 2022 16:21:45 +0200 Message-Id: <20220921142147.1083886-14-sjg@chromium.org> X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog In-Reply-To: <20220921142147.1083886-1-sjg@chromium.org> References: <20220921142147.1083886-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Move this code into the emulator file so it can be used by multiple drivers. Signed-off-by: Simon Glass --- Changes in v2: - Fix 'start' type drivers/scsi/Makefile | 1 + drivers/scsi/scsi_emul.c | 74 +++++++++++++++++++++++++++ drivers/usb/emul/sandbox_flash.c | 87 ++++---------------------------- include/scsi_emul.h | 24 +++++++++ 4 files changed, 109 insertions(+), 77 deletions(-) create mode 100644 drivers/scsi/scsi_emul.c diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile index 25194eeec11..d1b40c61401 100644 --- a/drivers/scsi/Makefile +++ b/drivers/scsi/Makefile @@ -17,4 +17,5 @@ endif ifdef CONFIG_SCSI obj-$(CONFIG_SANDBOX) += sandbox_scsi.o +obj-$(CONFIG_SANDBOX) += scsi_emul.o endif diff --git a/drivers/scsi/scsi_emul.c b/drivers/scsi/scsi_emul.c new file mode 100644 index 00000000000..5ba364bdac7 --- /dev/null +++ b/drivers/scsi/scsi_emul.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Emulation of enough SCSI commands to find and read from a unit + * + * Copyright 2022 Google LLC + * Written by Simon Glass + * + * implementation of SCSI functions required so that CONFIG_SCSI can be enabled + * for sandbox. + */ + +#define LOG_CATEGORY UCLASS_SCSI + +#include +#include +#include +#include +#include + +int sb_scsi_emul_command(struct scsi_emul_info *info, + const struct scsi_cmd *req, int len) +{ + int ret = 0; + + info->buff_used = 0; + log_debug("emul %x\n", *req->cmd); + switch (*req->cmd) { + case SCSI_INQUIRY: { + struct scsi_inquiry_resp *resp = (void *)info->buff; + + info->alloc_len = req->cmd[4]; + memset(resp, '\0', sizeof(*resp)); + resp->data_format = 1; + resp->additional_len = 0x1f; + strncpy(resp->vendor, info->vendor, sizeof(resp->vendor)); + strncpy(resp->product, info->product, sizeof(resp->product)); + strncpy(resp->revision, "1.0", sizeof(resp->revision)); + info->buff_used = sizeof(*resp); + break; + } + case SCSI_TST_U_RDY: + break; + case SCSI_RD_CAPAC: { + struct scsi_read_capacity_resp *resp = (void *)info->buff; + uint blocks; + + if (info->file_size) + blocks = info->file_size / info->block_size - 1; + else + blocks = 0; + resp->last_block_addr = cpu_to_be32(blocks); + resp->block_len = cpu_to_be32(info->block_size); + info->buff_used = sizeof(*resp); + break; + } + case SCSI_READ10: { + const struct scsi_read10_req *read_req = (void *)req; + + info->seek_block = be32_to_cpu(read_req->lba); + info->read_len = be16_to_cpu(read_req->xfer_len); + info->buff_used = info->read_len * info->block_size; + ret = SCSI_EMUL_DO_READ; + break; + } + default: + debug("Command not supported: %x\n", req->cmd[0]); + ret = -EPROTONOSUPPORT; + } + if (ret >= 0) + info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS; + log_debug(" - done %x: ret=%d\n", *req->cmd, ret); + + return ret; +} diff --git a/drivers/usb/emul/sandbox_flash.c b/drivers/usb/emul/sandbox_flash.c index 2059fc7fe42..2589c708d88 100644 --- a/drivers/usb/emul/sandbox_flash.c +++ b/drivers/usb/emul/sandbox_flash.c @@ -180,97 +180,30 @@ static void setup_response(struct sandbox_flash_priv *priv) csw->bCSWStatus = CSWSTATUS_GOOD; } -/** - * handle_read() - prepare for reading data from the backing file - * - * This seeks to the correct file position and sets info->buff_used to the - * correct size. - * - * @priv: Private information - * @lba: Start block to read from - * @transfer_length: Number of blocks to read - * @return 0 if OK, -EIO on failure - */ -static int handle_read(struct sandbox_flash_priv *priv, ulong lba, - ulong transfer_len) -{ - struct scsi_emul_info *info = &priv->eminfo; - - debug("%s: lba=%lx, transfer_len=%lx\n", __func__, lba, transfer_len); - info->read_len = transfer_len; - if (priv->fd != -1) { - os_lseek(priv->fd, lba * info->block_size, OS_SEEK_SET); - info->buff_used = transfer_len * info->block_size; - return 0; - } - - return -EIO; -} - -static int handle_ufi_command(struct sandbox_flash_plat *plat, - struct sandbox_flash_priv *priv, const void *buff, +static int handle_ufi_command(struct sandbox_flash_priv *priv, const void *buff, int len) { struct scsi_emul_info *info = &priv->eminfo; const struct scsi_cmd *req = buff; + int ret; - info->buff_used = 0; - switch (*req->cmd) { - case SCSI_INQUIRY: { - struct scsi_inquiry_resp *resp = (void *)info->buff; - - info->alloc_len = req->cmd[4]; - memset(resp, '\0', sizeof(*resp)); - resp->data_format = 1; - resp->additional_len = 0x1f; - strncpy(resp->vendor, info->vendor, sizeof(resp->vendor)); - strncpy(resp->product, info->product, sizeof(resp->product)); - strncpy(resp->revision, "1.0", sizeof(resp->revision)); - info->buff_used = sizeof(*resp); - setup_response(priv); - break; - } - case SCSI_TST_U_RDY: + ret = sb_scsi_emul_command(info, req, len); + if (!ret) { setup_response(priv); - break; - case SCSI_RD_CAPAC: { - struct scsi_read_capacity_resp *resp = (void *)info->buff; - uint blocks; - - if (info->file_size) - blocks = info->file_size / info->block_size - 1; - else - blocks = 0; - resp->last_block_addr = cpu_to_be32(blocks); - resp->block_len = cpu_to_be32(info->block_size); - info->buff_used = sizeof(*resp); + } else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) { + os_lseek(priv->fd, info->seek_block * info->block_size, + OS_SEEK_SET); setup_response(priv); - break; + } else { + setup_fail_response(priv); } - case SCSI_READ10: { - struct scsi_read10_req *req = (void *)buff; - if (!handle_read(priv, be32_to_cpu(req->lba), - be16_to_cpu(req->xfer_len))) - setup_response(priv); - else - setup_fail_response(priv); - - break; - } - default: - debug("Command not supported: %x\n", req->cmd[0]); - return -EPROTONOSUPPORT; - } - - info->phase = info->transfer_len ? SCSIPH_DATA : SCSIPH_STATUS; return 0; } static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev, unsigned long pipe, void *buff, int len) { - struct sandbox_flash_plat *plat = dev_get_plat(dev); struct sandbox_flash_priv *priv = dev_get_priv(dev); struct scsi_emul_info *info = &priv->eminfo; int ep = usb_pipeendpoint(pipe); @@ -294,7 +227,7 @@ static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev, goto err; info->transfer_len = cbw->dCBWDataTransferLength; priv->tag = cbw->dCBWTag; - return handle_ufi_command(plat, priv, cbw->CBWCDB, + return handle_ufi_command(priv, cbw->CBWCDB, cbw->bCDBLength); case SCSIPH_DATA: debug("data out\n"); diff --git a/include/scsi_emul.h b/include/scsi_emul.h index 3c52398e3ff..13c3f860b40 100644 --- a/include/scsi_emul.h +++ b/include/scsi_emul.h @@ -19,6 +19,7 @@ * @product: Product name * @block_size: Block size of device in bytes (normally 512) * @file_size: Size of the backing file for this emulator, in bytes + * @seek_block: Seek position for file (block number) * * @phase: Current SCSI phase * @buff_used: Number of bytes ready to transfer back to host @@ -34,13 +35,36 @@ struct scsi_emul_info { const char *product; int block_size; loff_t file_size; + int seek_block; /* state maintained by the emulator: */ enum scsi_cmd_phase phase; int buff_used; int read_len; + uint seek_pos; int alloc_len; uint transfer_len; }; +/* Indicates that a read is being started */ +#define SCSI_EMUL_DO_READ 1 + +/** + * sb_scsi_emul_command() - Process a SCSI command + * + * This sets up the response in info->buff and updates various other values + * in info. + * + * If SCSI_EMUL_DO_READ is returned then the caller should set up so that the + * backing file can be read, or return an error status if there is no file. + * + * @info: Emulation information + * @req: Request to process + * @len: Length of request in bytes + * @return SCSI_EMUL_DO_READ if a read has started, 0 if some other operation + * has started, -ve if there was an error + */ +int sb_scsi_emul_command(struct scsi_emul_info *info, + const struct scsi_cmd *req, int len); + #endif