blob: 13c3f860b403ea0fe3c884235d905848756ab655 [file] [log] [blame]
Simon Glass1377d442022-09-21 16:21:36 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Emulation of enough SCSI commands to find and read from a unit
4 *
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 *
8 * implementations of SCSI functions required so that CONFIG_SCSI can be enabled
9 * for sandbox
10 */
11
12#ifndef __scsi_emul_h
13#define __scsi_emul_h
14
15/**
16 * struct scsi_emul_info - information for emulating a SCSI device
17 *
Simon Glass0c12d9d2022-09-21 16:21:38 +020018 * @vendor: Vendor name
19 * @product: Product name
Simon Glassa3718f12022-09-21 16:21:39 +020020 * @block_size: Block size of device in bytes (normally 512)
Simon Glassf148ad12022-09-21 16:21:40 +020021 * @file_size: Size of the backing file for this emulator, in bytes
Simon Glass02cea112022-09-21 16:21:45 +020022 * @seek_block: Seek position for file (block number)
Simon Glass0c12d9d2022-09-21 16:21:38 +020023 *
Simon Glass1377d442022-09-21 16:21:36 +020024 * @phase: Current SCSI phase
25 * @buff_used: Number of bytes ready to transfer back to host
26 * @read_len: Number of bytes of data left in the current read command
27 * @alloc_len: Allocation length from the last incoming command
28 * @transfer_len: Transfer length from CBW header
Simon Glassfc7a7ed2022-09-21 16:21:37 +020029 * @buff: Data buffer for outgoing data
Simon Glass1377d442022-09-21 16:21:36 +020030 */
31struct scsi_emul_info {
Simon Glassfc7a7ed2022-09-21 16:21:37 +020032 /* provided by the caller: */
33 void *buff;
Simon Glass0c12d9d2022-09-21 16:21:38 +020034 const char *vendor;
35 const char *product;
Simon Glassa3718f12022-09-21 16:21:39 +020036 int block_size;
Simon Glassf148ad12022-09-21 16:21:40 +020037 loff_t file_size;
Simon Glass02cea112022-09-21 16:21:45 +020038 int seek_block;
Simon Glassfc7a7ed2022-09-21 16:21:37 +020039
40 /* state maintained by the emulator: */
Simon Glass1377d442022-09-21 16:21:36 +020041 enum scsi_cmd_phase phase;
42 int buff_used;
43 int read_len;
Simon Glass02cea112022-09-21 16:21:45 +020044 uint seek_pos;
Simon Glass1377d442022-09-21 16:21:36 +020045 int alloc_len;
46 uint transfer_len;
47};
48
Simon Glass02cea112022-09-21 16:21:45 +020049/* Indicates that a read is being started */
50#define SCSI_EMUL_DO_READ 1
51
52/**
53 * sb_scsi_emul_command() - Process a SCSI command
54 *
55 * This sets up the response in info->buff and updates various other values
56 * in info.
57 *
58 * If SCSI_EMUL_DO_READ is returned then the caller should set up so that the
59 * backing file can be read, or return an error status if there is no file.
60 *
61 * @info: Emulation information
62 * @req: Request to process
63 * @len: Length of request in bytes
64 * @return SCSI_EMUL_DO_READ if a read has started, 0 if some other operation
65 * has started, -ve if there was an error
66 */
67int sb_scsi_emul_command(struct scsi_emul_info *info,
68 const struct scsi_cmd *req, int len);
69
Simon Glass1377d442022-09-21 16:21:36 +020070#endif