sandbox: Convert to use driver model for SCSI

At present sandbox is producing a warning about SCSI migration. Drop the
legacy code and replace it with a new implementation.

Also drop the SATA command, which does not work with driver model.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/arch/Kconfig b/arch/Kconfig
index 994fd3b..1ffd77c 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -166,7 +166,6 @@
 	imply CMD_IO
 	imply CMD_IOTRACE
 	imply CMD_LZMADEC
-	imply CMD_SATA
 	imply CMD_SF
 	imply CMD_SF_TEST
 	imply CRC32_VERIFY
diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index 9dab89a..b20b181 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -52,7 +52,6 @@
 CONFIG_CMD_PCI=y
 CONFIG_CMD_READ=y
 CONFIG_CMD_REMOTEPROC=y
-# CONFIG_CMD_SATA is not set
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
 CONFIG_BOOTP_DNS2=y
@@ -204,9 +203,7 @@
 CONFIG_DM_RTC=y
 CONFIG_RTC_RV8803=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SMEM=y
 CONFIG_SANDBOX_SMEM=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index caecb50..df6a28e 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -137,7 +137,6 @@
 CONFIG_SIMPLE_PM_BUS=y
 CONFIG_ADC=y
 CONFIG_ADC_SANDBOX=y
-CONFIG_SYS_SATA_MAX_DEVICE=2
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_SYS_IDE_MAXBUS=1
@@ -264,9 +263,7 @@
 CONFIG_DM_RTC=y
 CONFIG_RTC_RV8803=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SMEM=y
 CONFIG_SANDBOX_SMEM=y
diff --git a/configs/sandbox_flattree_defconfig b/configs/sandbox_flattree_defconfig
index a834402..a2eb7af 100644
--- a/configs/sandbox_flattree_defconfig
+++ b/configs/sandbox_flattree_defconfig
@@ -85,7 +85,6 @@
 CONFIG_DEBUG_DEVRES=y
 CONFIG_ADC=y
 CONFIG_ADC_SANDBOX=y
-CONFIG_SYS_SATA_MAX_DEVICE=2
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_CLK=y
@@ -175,9 +174,7 @@
 CONFIG_SANDBOX_RESET=y
 CONFIG_DM_RTC=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SOUND=y
 CONFIG_SOUND_SANDBOX=y
diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
index da83165..8bf10bb 100644
--- a/configs/sandbox_noinst_defconfig
+++ b/configs/sandbox_noinst_defconfig
@@ -108,7 +108,6 @@
 # CONFIG_SPL_SIMPLE_BUS is not set
 CONFIG_ADC=y
 CONFIG_ADC_SANDBOX=y
-CONFIG_SYS_SATA_MAX_DEVICE=2
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_SYS_IDE_MAXBUS=1
@@ -201,9 +200,7 @@
 CONFIG_DM_RTC=y
 CONFIG_SPL_DM_RTC=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SOUND=y
 CONFIG_SOUND_SANDBOX=y
diff --git a/configs/sandbox_spl_defconfig b/configs/sandbox_spl_defconfig
index 0d7c597..c33f171 100644
--- a/configs/sandbox_spl_defconfig
+++ b/configs/sandbox_spl_defconfig
@@ -61,7 +61,6 @@
 CONFIG_CMD_OSD=y
 CONFIG_CMD_PCI=y
 CONFIG_CMD_REMOTEPROC=y
-# CONFIG_CMD_SATA is not set
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
 CONFIG_BOOTP_DNS2=y
@@ -204,9 +203,7 @@
 CONFIG_DM_RTC=y
 CONFIG_SPL_DM_RTC=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SOUND=y
 CONFIG_SOUND_SANDBOX=y
diff --git a/configs/sandbox_vpl_defconfig b/configs/sandbox_vpl_defconfig
index 7bddac6..be8186e 100644
--- a/configs/sandbox_vpl_defconfig
+++ b/configs/sandbox_vpl_defconfig
@@ -124,7 +124,6 @@
 # CONFIG_SPL_SIMPLE_BUS is not set
 CONFIG_ADC=y
 CONFIG_ADC_SANDBOX=y
-CONFIG_SYS_SATA_MAX_DEVICE=2
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_CLK=y
@@ -210,9 +209,7 @@
 CONFIG_SPL_DM_RTC=y
 CONFIG_TPL_DM_RTC=y
 CONFIG_SCSI=y
-CONFIG_SCSI_AHCI_PLAT=y
-CONFIG_SYS_SCSI_MAX_SCSI_ID=8
-CONFIG_SYS_SCSI_MAX_LUN=4
+CONFIG_DM_SCSI=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SOUND=y
 CONFIG_SOUND_SANDBOX=y
diff --git a/configs/tools-only_defconfig b/configs/tools-only_defconfig
index 211acc7..2c418f5 100644
--- a/configs/tools-only_defconfig
+++ b/configs/tools-only_defconfig
@@ -18,7 +18,6 @@
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 # CONFIG_NET is not set
 # CONFIG_ACPIGEN is not set
-CONFIG_SYS_SATA_MAX_DEVICE=2
 CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_SANDBOX_GPIO=y
diff --git a/drivers/scsi/sandbox_scsi.c b/drivers/scsi/sandbox_scsi.c
index 39b969a..a7ac33c 100644
--- a/drivers/scsi/sandbox_scsi.c
+++ b/drivers/scsi/sandbox_scsi.c
@@ -7,19 +7,145 @@
  * that CONFIG_SCSI can be enabled for sandbox.
  */
 
+#define LOG_CATEGORY UCLASS_SCSI
+
 #include <common.h>
+#include <dm.h>
+#include <os.h>
+#include <malloc.h>
 #include <scsi.h>
+#include <scsi_emul.h>
 
-int scsi_bus_reset(struct udevice *dev)
+enum {
+	SANDBOX_SCSI_BLOCK_LEN		= 512,
+	SANDBOX_SCSI_BUF_SIZE		= 512,
+};
+
+/**
+ * struct sandbox_scsi_priv
+ *
+ * @eminfo: emulator state
+ * @pathanme: Path to the backing file, e.g. 'scsi.img'
+ * @fd: File descriptor of backing file
+ */
+struct sandbox_scsi_priv {
+	struct scsi_emul_info eminfo;
+	const char *pathname;
+	int fd;
+};
+
+static int sandbox_scsi_exec(struct udevice *dev, struct scsi_cmd *req)
 {
+	struct sandbox_scsi_priv *priv = dev_get_priv(dev);
+	struct scsi_emul_info *info = &priv->eminfo;
+	int ret;
+
+	if (req->lun || req->target)
+		return -EIO;
+	ret = sb_scsi_emul_command(info, req, req->cmdlen);
+	if (ret < 0) {
+		log_debug("SCSI command 0x%02x ret errno %d\n", req->cmd[0],
+			  ret);
+		return ret;
+	} else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) {
+		long bytes_read;
+
+		log_debug("read %x %x\n", info->seek_block, info->read_len);
+		os_lseek(priv->fd, info->seek_block * info->block_size,
+			 OS_SEEK_SET);
+		bytes_read = os_read(priv->fd, req->pdata, info->buff_used);
+		if (bytes_read < 0)
+			return bytes_read;
+		if (bytes_read != info->buff_used)
+			return -EIO;
+	} else if (!ret) {
+		req->pdata = info->buff;
+		info->phase = SCSIPH_STATUS;
+		log_debug("sending buf\n");
+	} else {
+		log_debug("error\n");
+		return -EIO;
+	}
+
 	return 0;
 }
 
-void scsi_init(void)
+static int sandbox_scsi_bus_reset(struct udevice *dev)
 {
-}
+	/* Not implemented */
 
-int scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
-{
 	return 0;
 }
+
+static int sandbox_scsi_of_to_plat(struct udevice *dev)
+{
+	struct sandbox_scsi_priv *priv = dev_get_priv(dev);
+
+	priv->pathname = dev_read_string(dev, "sandbox,filepath");
+
+	return 0;
+}
+
+static int sandbox_scsi_probe(struct udevice *dev)
+{
+	struct scsi_plat *scsi_plat = dev_get_uclass_plat(dev);
+	struct sandbox_scsi_priv *priv = dev_get_priv(dev);
+	struct scsi_emul_info *info = &priv->eminfo;
+	int ret;
+
+	scsi_plat->max_id = 2;
+	scsi_plat->max_lun = 3;
+	scsi_plat->max_bytes_per_req = 1 << 20;
+
+	info->vendor = "SANDBOX";
+	info->product = "FAKE DISK";
+	info->buff = malloc(SANDBOX_SCSI_BUF_SIZE);
+	if (!info->buff)
+		return log_ret(-ENOMEM);
+	info->block_size = SANDBOX_SCSI_BLOCK_LEN;
+
+	if (priv->pathname) {
+		priv->fd = os_open(priv->pathname, OS_O_RDONLY);
+		if (priv->fd != -1) {
+			ret = os_get_filesize(priv->pathname, &info->file_size);
+			if (ret)
+				return log_msg_ret("sz", ret);
+		}
+	} else {
+		priv->fd = -1;
+	}
+	log_debug("filename: %s, fd %d\n", priv->pathname, priv->fd);
+
+	return 0;
+}
+
+static int sandbox_scsi_remove(struct udevice *dev)
+{
+	struct sandbox_scsi_priv *priv = dev_get_priv(dev);
+	struct scsi_emul_info *info = &priv->eminfo;
+
+	free(info->buff);
+
+	return 0;
+}
+
+struct scsi_ops sandbox_scsi_ops = {
+	.exec		= sandbox_scsi_exec,
+	.bus_reset	= sandbox_scsi_bus_reset,
+};
+
+static const struct udevice_id sanbox_scsi_ids[] = {
+	{ .compatible = "sandbox,scsi" },
+	{ }
+};
+
+U_BOOT_DRIVER(sandbox_scsi) = {
+	.name		= "sandbox_scsi",
+	.id		= UCLASS_SCSI,
+	.ops		= &sandbox_scsi_ops,
+	.of_match	= sanbox_scsi_ids,
+	.of_to_plat	= sandbox_scsi_of_to_plat,
+	.probe		= sandbox_scsi_probe,
+	.remove		= sandbox_scsi_remove,
+	.priv_auto	= sizeof(struct sandbox_scsi_priv),
+};