scsi.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef QEMU_HW_SCSI_H
  2. #define QEMU_HW_SCSI_H
  3. #include "qdev.h"
  4. #include "block.h"
  5. #include "sysemu.h"
  6. #define MAX_SCSI_DEVS 255
  7. #define SCSI_CMD_BUF_SIZE 16
  8. typedef struct SCSIBus SCSIBus;
  9. typedef struct SCSIBusInfo SCSIBusInfo;
  10. typedef struct SCSICommand SCSICommand;
  11. typedef struct SCSIDevice SCSIDevice;
  12. typedef struct SCSIDeviceInfo SCSIDeviceInfo;
  13. typedef struct SCSIRequest SCSIRequest;
  14. typedef struct SCSIReqOps SCSIReqOps;
  15. enum SCSIXferMode {
  16. SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
  17. SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
  18. SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
  19. };
  20. typedef struct SCSISense {
  21. uint8_t key;
  22. uint8_t asc;
  23. uint8_t ascq;
  24. } SCSISense;
  25. #define SCSI_SENSE_BUF_SIZE 96
  26. struct SCSICommand {
  27. uint8_t buf[SCSI_CMD_BUF_SIZE];
  28. int len;
  29. size_t xfer;
  30. uint64_t lba;
  31. enum SCSIXferMode mode;
  32. };
  33. struct SCSIRequest {
  34. SCSIBus *bus;
  35. SCSIDevice *dev;
  36. const SCSIReqOps *ops;
  37. uint32_t refcount;
  38. uint32_t tag;
  39. uint32_t lun;
  40. uint32_t status;
  41. SCSICommand cmd;
  42. BlockDriverAIOCB *aiocb;
  43. uint8_t sense[SCSI_SENSE_BUF_SIZE];
  44. uint32_t sense_len;
  45. bool enqueued;
  46. bool io_canceled;
  47. bool retry;
  48. void *hba_private;
  49. QTAILQ_ENTRY(SCSIRequest) next;
  50. };
  51. struct SCSIDevice
  52. {
  53. DeviceState qdev;
  54. VMChangeStateEntry *vmsentry;
  55. QEMUBH *bh;
  56. uint32_t id;
  57. BlockConf conf;
  58. SCSIDeviceInfo *info;
  59. SCSISense unit_attention;
  60. bool sense_is_ua;
  61. uint8_t sense[SCSI_SENSE_BUF_SIZE];
  62. uint32_t sense_len;
  63. QTAILQ_HEAD(, SCSIRequest) requests;
  64. uint32_t channel;
  65. uint32_t lun;
  66. int blocksize;
  67. int type;
  68. uint64_t max_lba;
  69. };
  70. /* cdrom.c */
  71. int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
  72. int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
  73. /* scsi-bus.c */
  74. struct SCSIReqOps {
  75. size_t size;
  76. void (*free_req)(SCSIRequest *req);
  77. int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
  78. void (*read_data)(SCSIRequest *req);
  79. void (*write_data)(SCSIRequest *req);
  80. void (*cancel_io)(SCSIRequest *req);
  81. uint8_t *(*get_buf)(SCSIRequest *req);
  82. };
  83. typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
  84. struct SCSIDeviceInfo {
  85. DeviceInfo qdev;
  86. scsi_qdev_initfn init;
  87. void (*destroy)(SCSIDevice *s);
  88. SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
  89. uint8_t *buf, void *hba_private);
  90. void (*unit_attention_reported)(SCSIDevice *s);
  91. };
  92. struct SCSIBusInfo {
  93. int tcq;
  94. int max_channel, max_target, max_lun;
  95. void (*transfer_data)(SCSIRequest *req, uint32_t arg);
  96. void (*complete)(SCSIRequest *req, uint32_t arg);
  97. void (*cancel)(SCSIRequest *req);
  98. };
  99. struct SCSIBus {
  100. BusState qbus;
  101. int busnr;
  102. SCSISense unit_attention;
  103. const SCSIBusInfo *info;
  104. };
  105. void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
  106. void scsi_qdev_register(SCSIDeviceInfo *info);
  107. static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
  108. {
  109. return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
  110. }
  111. SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
  112. int unit, bool removable, int bootindex);
  113. int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
  114. /*
  115. * Predefined sense codes
  116. */
  117. /* No sense data available */
  118. extern const struct SCSISense sense_code_NO_SENSE;
  119. /* LUN not ready, Manual intervention required */
  120. extern const struct SCSISense sense_code_LUN_NOT_READY;
  121. /* LUN not ready, Medium not present */
  122. extern const struct SCSISense sense_code_NO_MEDIUM;
  123. /* LUN not ready, medium removal prevented */
  124. extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
  125. /* Hardware error, internal target failure */
  126. extern const struct SCSISense sense_code_TARGET_FAILURE;
  127. /* Illegal request, invalid command operation code */
  128. extern const struct SCSISense sense_code_INVALID_OPCODE;
  129. /* Illegal request, LBA out of range */
  130. extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
  131. /* Illegal request, Invalid field in CDB */
  132. extern const struct SCSISense sense_code_INVALID_FIELD;
  133. /* Illegal request, LUN not supported */
  134. extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
  135. /* Illegal request, Saving parameters not supported */
  136. extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
  137. /* Illegal request, Incompatible format */
  138. extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
  139. /* Illegal request, medium removal prevented */
  140. extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
  141. /* Command aborted, I/O process terminated */
  142. extern const struct SCSISense sense_code_IO_ERROR;
  143. /* Command aborted, I_T Nexus loss occurred */
  144. extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
  145. /* Command aborted, Logical Unit failure */
  146. extern const struct SCSISense sense_code_LUN_FAILURE;
  147. /* LUN not ready, Medium not present */
  148. extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
  149. /* Unit attention, Power on, reset or bus device reset occurred */
  150. extern const struct SCSISense sense_code_RESET;
  151. /* Unit attention, Medium may have changed*/
  152. extern const struct SCSISense sense_code_MEDIUM_CHANGED;
  153. /* Unit attention, Reported LUNs data has changed */
  154. extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
  155. /* Unit attention, Device internal reset */
  156. extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
  157. #define SENSE_CODE(x) sense_code_ ## x
  158. int scsi_sense_valid(SCSISense sense);
  159. int scsi_build_sense(uint8_t *in_buf, int in_len,
  160. uint8_t *buf, int len, bool fixed);
  161. SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
  162. uint32_t tag, uint32_t lun, void *hba_private);
  163. SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
  164. uint8_t *buf, void *hba_private);
  165. int32_t scsi_req_enqueue(SCSIRequest *req);
  166. void scsi_req_free(SCSIRequest *req);
  167. SCSIRequest *scsi_req_ref(SCSIRequest *req);
  168. void scsi_req_unref(SCSIRequest *req);
  169. void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
  170. void scsi_req_print(SCSIRequest *req);
  171. void scsi_req_continue(SCSIRequest *req);
  172. void scsi_req_data(SCSIRequest *req, int len);
  173. void scsi_req_complete(SCSIRequest *req, int status);
  174. uint8_t *scsi_req_get_buf(SCSIRequest *req);
  175. int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
  176. void scsi_req_abort(SCSIRequest *req, int status);
  177. void scsi_req_cancel(SCSIRequest *req);
  178. void scsi_req_retry(SCSIRequest *req);
  179. void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
  180. int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
  181. SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
  182. /* scsi-generic.c. */
  183. extern const SCSIReqOps scsi_generic_req_ops;
  184. #endif