scsi.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef QEMU_HW_SCSI_H
  2. #define QEMU_HW_SCSI_H
  3. #include "qdev.h"
  4. #include "block.h"
  5. #include "block_int.h"
  6. #define MAX_SCSI_DEVS 255
  7. #define SCSI_CMD_BUF_SIZE 16
  8. typedef struct SCSIBus SCSIBus;
  9. typedef struct SCSIBusOps SCSIBusOps;
  10. typedef struct SCSIDevice SCSIDevice;
  11. typedef struct SCSIDeviceInfo SCSIDeviceInfo;
  12. typedef struct SCSIRequest SCSIRequest;
  13. enum SCSIXferMode {
  14. SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
  15. SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
  16. SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
  17. };
  18. typedef struct SCSISense {
  19. uint8_t key;
  20. uint8_t asc;
  21. uint8_t ascq;
  22. } SCSISense;
  23. struct SCSIRequest {
  24. SCSIBus *bus;
  25. SCSIDevice *dev;
  26. uint32_t refcount;
  27. uint32_t tag;
  28. uint32_t lun;
  29. uint32_t status;
  30. struct {
  31. uint8_t buf[SCSI_CMD_BUF_SIZE];
  32. int len;
  33. size_t xfer;
  34. uint64_t lba;
  35. enum SCSIXferMode mode;
  36. } cmd;
  37. BlockDriverAIOCB *aiocb;
  38. bool enqueued;
  39. void *hba_private;
  40. QTAILQ_ENTRY(SCSIRequest) next;
  41. };
  42. struct SCSIDevice
  43. {
  44. DeviceState qdev;
  45. uint32_t id;
  46. BlockConf conf;
  47. SCSIDeviceInfo *info;
  48. QTAILQ_HEAD(, SCSIRequest) requests;
  49. int blocksize;
  50. int type;
  51. };
  52. /* cdrom.c */
  53. int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
  54. int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
  55. /* scsi-bus.c */
  56. typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
  57. struct SCSIDeviceInfo {
  58. DeviceInfo qdev;
  59. scsi_qdev_initfn init;
  60. void (*destroy)(SCSIDevice *s);
  61. SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
  62. void *hba_private);
  63. void (*free_req)(SCSIRequest *req);
  64. int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
  65. void (*read_data)(SCSIRequest *req);
  66. void (*write_data)(SCSIRequest *req);
  67. void (*cancel_io)(SCSIRequest *req);
  68. uint8_t *(*get_buf)(SCSIRequest *req);
  69. int (*get_sense)(SCSIRequest *req, uint8_t *buf, int len);
  70. };
  71. struct SCSIBusOps {
  72. void (*transfer_data)(SCSIRequest *req, uint32_t arg);
  73. void (*complete)(SCSIRequest *req, uint32_t arg);
  74. void (*cancel)(SCSIRequest *req);
  75. };
  76. struct SCSIBus {
  77. BusState qbus;
  78. int busnr;
  79. int tcq, ndev;
  80. const SCSIBusOps *ops;
  81. SCSIDevice *devs[MAX_SCSI_DEVS];
  82. };
  83. void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
  84. const SCSIBusOps *ops);
  85. void scsi_qdev_register(SCSIDeviceInfo *info);
  86. static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
  87. {
  88. return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
  89. }
  90. SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
  91. int unit, bool removable);
  92. int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
  93. /*
  94. * Predefined sense codes
  95. */
  96. /* No sense data available */
  97. extern const struct SCSISense sense_code_NO_SENSE;
  98. /* LUN not ready, Manual intervention required */
  99. extern const struct SCSISense sense_code_LUN_NOT_READY;
  100. /* LUN not ready, Medium not present */
  101. extern const struct SCSISense sense_code_NO_MEDIUM;
  102. /* Hardware error, internal target failure */
  103. extern const struct SCSISense sense_code_TARGET_FAILURE;
  104. /* Illegal request, invalid command operation code */
  105. extern const struct SCSISense sense_code_INVALID_OPCODE;
  106. /* Illegal request, LBA out of range */
  107. extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
  108. /* Illegal request, Invalid field in CDB */
  109. extern const struct SCSISense sense_code_INVALID_FIELD;
  110. /* Illegal request, LUN not supported */
  111. extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
  112. /* Command aborted, I/O process terminated */
  113. extern const struct SCSISense sense_code_IO_ERROR;
  114. /* Command aborted, I_T Nexus loss occurred */
  115. extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
  116. /* Command aborted, Logical Unit failure */
  117. extern const struct SCSISense sense_code_LUN_FAILURE;
  118. #define SENSE_CODE(x) sense_code_ ## x
  119. int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
  120. int scsi_sense_valid(SCSISense sense);
  121. SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag,
  122. uint32_t lun, void *hba_private);
  123. SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
  124. void *hba_private);
  125. int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
  126. void scsi_req_free(SCSIRequest *req);
  127. SCSIRequest *scsi_req_ref(SCSIRequest *req);
  128. void scsi_req_unref(SCSIRequest *req);
  129. int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
  130. void scsi_req_print(SCSIRequest *req);
  131. void scsi_req_continue(SCSIRequest *req);
  132. void scsi_req_data(SCSIRequest *req, int len);
  133. void scsi_req_complete(SCSIRequest *req);
  134. uint8_t *scsi_req_get_buf(SCSIRequest *req);
  135. int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
  136. void scsi_req_abort(SCSIRequest *req, int status);
  137. void scsi_req_cancel(SCSIRequest *req);
  138. void scsi_device_purge_requests(SCSIDevice *sdev);
  139. #endif