blob: 544a0247083d9da954a0679ae97ac65f536dfe9e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass471d8672016-05-01 11:35:55 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
Simon Glass471d8672016-05-01 11:35:55 -06006 * This file contains dummy implementations of SCSI functions requried so
7 * that CONFIG_SCSI can be enabled for sandbox.
8 */
9
Simon Glassfe0e2492022-09-21 16:21:46 +020010#define LOG_CATEGORY UCLASS_SCSI
11
Simon Glassfe0e2492022-09-21 16:21:46 +020012#include <dm.h>
13#include <os.h>
14#include <malloc.h>
Simon Glass471d8672016-05-01 11:35:55 -060015#include <scsi.h>
Simon Glassfe0e2492022-09-21 16:21:46 +020016#include <scsi_emul.h>
17
18enum {
19 SANDBOX_SCSI_BLOCK_LEN = 512,
20 SANDBOX_SCSI_BUF_SIZE = 512,
21};
Simon Glass471d8672016-05-01 11:35:55 -060022
Simon Glassfe0e2492022-09-21 16:21:46 +020023/**
24 * struct sandbox_scsi_priv
25 *
26 * @eminfo: emulator state
27 * @pathanme: Path to the backing file, e.g. 'scsi.img'
28 * @fd: File descriptor of backing file
29 */
30struct sandbox_scsi_priv {
31 struct scsi_emul_info eminfo;
32 const char *pathname;
33 int fd;
34};
35
36static int sandbox_scsi_exec(struct udevice *dev, struct scsi_cmd *req)
Simon Glass471d8672016-05-01 11:35:55 -060037{
Simon Glassfe0e2492022-09-21 16:21:46 +020038 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
39 struct scsi_emul_info *info = &priv->eminfo;
40 int ret;
41
42 if (req->lun || req->target)
43 return -EIO;
44 ret = sb_scsi_emul_command(info, req, req->cmdlen);
45 if (ret < 0) {
46 log_debug("SCSI command 0x%02x ret errno %d\n", req->cmd[0],
47 ret);
48 return ret;
49 } else if (ret == SCSI_EMUL_DO_READ && priv->fd != -1) {
50 long bytes_read;
51
52 log_debug("read %x %x\n", info->seek_block, info->read_len);
53 os_lseek(priv->fd, info->seek_block * info->block_size,
54 OS_SEEK_SET);
55 bytes_read = os_read(priv->fd, req->pdata, info->buff_used);
56 if (bytes_read < 0)
57 return bytes_read;
58 if (bytes_read != info->buff_used)
59 return -EIO;
60 } else if (!ret) {
61 req->pdata = info->buff;
62 info->phase = SCSIPH_STATUS;
63 log_debug("sending buf\n");
64 } else {
65 log_debug("error\n");
66 return -EIO;
67 }
68
Simon Glass11b2b622017-06-14 21:28:40 -060069 return 0;
Simon Glass471d8672016-05-01 11:35:55 -060070}
71
Simon Glassfe0e2492022-09-21 16:21:46 +020072static int sandbox_scsi_bus_reset(struct udevice *dev)
Simon Glass471d8672016-05-01 11:35:55 -060073{
Simon Glassfe0e2492022-09-21 16:21:46 +020074 /* Not implemented */
75
76 return 0;
Simon Glass471d8672016-05-01 11:35:55 -060077}
78
Simon Glassfe0e2492022-09-21 16:21:46 +020079static int sandbox_scsi_of_to_plat(struct udevice *dev)
Simon Glass471d8672016-05-01 11:35:55 -060080{
Simon Glassfe0e2492022-09-21 16:21:46 +020081 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
82
83 priv->pathname = dev_read_string(dev, "sandbox,filepath");
84
Simon Glass471d8672016-05-01 11:35:55 -060085 return 0;
86}
Simon Glassfe0e2492022-09-21 16:21:46 +020087
88static int sandbox_scsi_probe(struct udevice *dev)
89{
90 struct scsi_plat *scsi_plat = dev_get_uclass_plat(dev);
91 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
92 struct scsi_emul_info *info = &priv->eminfo;
93 int ret;
94
95 scsi_plat->max_id = 2;
96 scsi_plat->max_lun = 3;
97 scsi_plat->max_bytes_per_req = 1 << 20;
98
99 info->vendor = "SANDBOX";
100 info->product = "FAKE DISK";
101 info->buff = malloc(SANDBOX_SCSI_BUF_SIZE);
102 if (!info->buff)
103 return log_ret(-ENOMEM);
104 info->block_size = SANDBOX_SCSI_BLOCK_LEN;
105
106 if (priv->pathname) {
107 priv->fd = os_open(priv->pathname, OS_O_RDONLY);
108 if (priv->fd != -1) {
109 ret = os_get_filesize(priv->pathname, &info->file_size);
110 if (ret)
111 return log_msg_ret("sz", ret);
112 }
113 } else {
114 priv->fd = -1;
115 }
116 log_debug("filename: %s, fd %d\n", priv->pathname, priv->fd);
117
118 return 0;
119}
120
121static int sandbox_scsi_remove(struct udevice *dev)
122{
123 struct sandbox_scsi_priv *priv = dev_get_priv(dev);
124 struct scsi_emul_info *info = &priv->eminfo;
125
126 free(info->buff);
127
128 return 0;
129}
130
131struct scsi_ops sandbox_scsi_ops = {
132 .exec = sandbox_scsi_exec,
133 .bus_reset = sandbox_scsi_bus_reset,
134};
135
136static const struct udevice_id sanbox_scsi_ids[] = {
137 { .compatible = "sandbox,scsi" },
138 { }
139};
140
141U_BOOT_DRIVER(sandbox_scsi) = {
142 .name = "sandbox_scsi",
143 .id = UCLASS_SCSI,
144 .ops = &sandbox_scsi_ops,
145 .of_match = sanbox_scsi_ids,
146 .of_to_plat = sandbox_scsi_of_to_plat,
147 .probe = sandbox_scsi_probe,
148 .remove = sandbox_scsi_remove,
149 .priv_auto = sizeof(struct sandbox_scsi_priv),
150};