scsi_emul.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* 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. #ifndef __scsi_emul_h
  12. #define __scsi_emul_h
  13. /**
  14. * struct scsi_emul_info - information for emulating a SCSI device
  15. *
  16. * @vendor: Vendor name
  17. * @product: Product name
  18. * @block_size: Block size of device in bytes (normally 512)
  19. * @file_size: Size of the backing file for this emulator, in bytes
  20. * @seek_block: Seek position for file (block number)
  21. *
  22. * @phase: Current SCSI phase
  23. * @buff_used: Number of bytes ready to transfer back to host
  24. * @read_len: Number of bytes of data left in the current read command
  25. * @alloc_len: Allocation length from the last incoming command
  26. * @transfer_len: Transfer length from CBW header
  27. * @buff: Data buffer for outgoing data
  28. */
  29. struct scsi_emul_info {
  30. /* provided by the caller: */
  31. void *buff;
  32. const char *vendor;
  33. const char *product;
  34. int block_size;
  35. loff_t file_size;
  36. int seek_block;
  37. /* state maintained by the emulator: */
  38. enum scsi_cmd_phase phase;
  39. int buff_used;
  40. int read_len;
  41. int write_len;
  42. uint seek_pos;
  43. int alloc_len;
  44. uint transfer_len;
  45. };
  46. /**
  47. * Return value from sb_scsi_emul_command() indicates that a read or write is
  48. * being started
  49. */
  50. enum {
  51. SCSI_EMUL_DO_READ = 1,
  52. SCSI_EMUL_DO_WRITE = 2,
  53. };
  54. /**
  55. * sb_scsi_emul_command() - Process a SCSI command
  56. *
  57. * This sets up the response in info->buff and updates various other values
  58. * in info.
  59. *
  60. * If SCSI_EMUL_DO_READ is returned then the caller should set up so that the
  61. * backing file can be read, or return an error status if there is no file.
  62. *
  63. * @info: Emulation information
  64. * @req: Request to process
  65. * @len: Length of request in bytes
  66. * @return SCSI_EMUL_DO_READ if a read has started, SCSI_EMUL_DO_WRITE if a
  67. * write has started, 0 if some other operation has started, -ve if there
  68. * was an error
  69. */
  70. int sb_scsi_emul_command(struct scsi_emul_info *info,
  71. const struct scsi_cmd *req, int len);
  72. #endif