diff options
author | Simon Glass <sjg@chromium.org> | 2022-09-21 16:21:45 +0200 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2022-09-25 13:59:38 -0600 |
commit | 02cea1145a5ad49e400bd100b907ad763db16863 (patch) | |
tree | c4c0bec672b962e0c0bd787b0dc38011066aa77b /drivers | |
parent | 5eff9d9c36e9e2b2437743d989f25376b981700a (diff) | |
download | u-boot-02cea1145a5ad49e400bd100b907ad763db16863.tar.gz |
sandbox: scsi: Move request-handling code to scsi_emul
Move this code into the emulator file so it can be used by multiple
drivers.
Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/scsi/Makefile | 1 | ||||
-rw-r--r-- | drivers/scsi/scsi_emul.c | 74 | ||||
-rw-r--r-- | drivers/usb/emul/sandbox_flash.c | 87 |
3 files changed, 85 insertions, 77 deletions
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 <sjg@chromium.org> + * + * implementation of SCSI functions required so that CONFIG_SCSI can be enabled + * for sandbox. + */ + +#define LOG_CATEGORY UCLASS_SCSI + +#include <common.h> +#include <dm.h> +#include <log.h> +#include <scsi.h> +#include <scsi_emul.h> + +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"); |