Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 6 | * This file contains dummy implementations of SCSI functions requried so |
| 7 | * that CONFIG_SCSI can be enabled for sandbox. |
| 8 | */ |
| 9 | |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 10 | #define LOG_CATEGORY UCLASS_SCSI |
| 11 | |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <os.h> |
| 14 | #include <malloc.h> |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 15 | #include <scsi.h> |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 16 | #include <scsi_emul.h> |
| 17 | |
| 18 | enum { |
| 19 | SANDBOX_SCSI_BLOCK_LEN = 512, |
| 20 | SANDBOX_SCSI_BUF_SIZE = 512, |
| 21 | }; |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 22 | |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 23 | /** |
| 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 | */ |
| 30 | struct sandbox_scsi_priv { |
| 31 | struct scsi_emul_info eminfo; |
| 32 | const char *pathname; |
| 33 | int fd; |
| 34 | }; |
| 35 | |
| 36 | static int sandbox_scsi_exec(struct udevice *dev, struct scsi_cmd *req) |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 37 | { |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 38 | 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 Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 69 | return 0; |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 72 | static int sandbox_scsi_bus_reset(struct udevice *dev) |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 73 | { |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 74 | /* Not implemented */ |
| 75 | |
| 76 | return 0; |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 79 | static int sandbox_scsi_of_to_plat(struct udevice *dev) |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 80 | { |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 81 | struct sandbox_scsi_priv *priv = dev_get_priv(dev); |
| 82 | |
| 83 | priv->pathname = dev_read_string(dev, "sandbox,filepath"); |
| 84 | |
Simon Glass | 471d867 | 2016-05-01 11:35:55 -0600 | [diff] [blame] | 85 | return 0; |
| 86 | } |
Simon Glass | fe0e249 | 2022-09-21 16:21:46 +0200 | [diff] [blame] | 87 | |
| 88 | static 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 | |
| 121 | static 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 | |
| 131 | struct scsi_ops sandbox_scsi_ops = { |
| 132 | .exec = sandbox_scsi_exec, |
| 133 | .bus_reset = sandbox_scsi_bus_reset, |
| 134 | }; |
| 135 | |
| 136 | static const struct udevice_id sanbox_scsi_ids[] = { |
| 137 | { .compatible = "sandbox,scsi" }, |
| 138 | { } |
| 139 | }; |
| 140 | |
| 141 | U_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 | }; |