scsi.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef QEMU_HW_SCSI_H
  2. #define QEMU_HW_SCSI_H
  3. #include "block/aio.h"
  4. #include "hw/block/block.h"
  5. #include "hw/qdev-core.h"
  6. #include "scsi/utils.h"
  7. #include "qemu/notify.h"
  8. #include "qom/object.h"
  9. #define MAX_SCSI_DEVS 255
  10. typedef struct SCSIBus SCSIBus;
  11. typedef struct SCSIBusInfo SCSIBusInfo;
  12. typedef struct SCSIDevice SCSIDevice;
  13. typedef struct SCSIRequest SCSIRequest;
  14. typedef struct SCSIReqOps SCSIReqOps;
  15. #define SCSI_SENSE_BUF_SIZE_OLD 96
  16. #define SCSI_SENSE_BUF_SIZE 252
  17. #define DEFAULT_IO_TIMEOUT 30
  18. struct SCSIRequest {
  19. SCSIBus *bus;
  20. SCSIDevice *dev;
  21. const SCSIReqOps *ops;
  22. uint32_t refcount;
  23. uint32_t tag;
  24. uint32_t lun;
  25. int16_t status;
  26. int16_t host_status;
  27. void *hba_private;
  28. uint64_t residual;
  29. SCSICommand cmd;
  30. NotifierList cancel_notifiers;
  31. /* Note:
  32. * - fields before sense are initialized by scsi_req_alloc;
  33. * - sense[] is uninitialized;
  34. * - fields after sense are memset to 0 by scsi_req_alloc.
  35. * */
  36. uint8_t sense[SCSI_SENSE_BUF_SIZE];
  37. uint32_t sense_len;
  38. bool enqueued;
  39. bool io_canceled;
  40. bool retry;
  41. bool dma_started;
  42. BlockAIOCB *aiocb;
  43. QEMUSGList *sg;
  44. QTAILQ_ENTRY(SCSIRequest) next;
  45. };
  46. #define TYPE_SCSI_DEVICE "scsi-device"
  47. OBJECT_DECLARE_TYPE(SCSIDevice, SCSIDeviceClass, SCSI_DEVICE)
  48. struct SCSIDeviceClass {
  49. DeviceClass parent_class;
  50. void (*realize)(SCSIDevice *dev, Error **errp);
  51. void (*unrealize)(SCSIDevice *dev);
  52. int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
  53. size_t buf_len, void *hba_private);
  54. SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
  55. uint8_t *buf, void *hba_private);
  56. void (*unit_attention_reported)(SCSIDevice *s);
  57. };
  58. struct SCSIDevice
  59. {
  60. DeviceState qdev;
  61. VMChangeStateEntry *vmsentry;
  62. QEMUBH *bh;
  63. uint32_t id;
  64. BlockConf conf;
  65. SCSISense unit_attention;
  66. bool sense_is_ua;
  67. uint8_t sense[SCSI_SENSE_BUF_SIZE];
  68. uint32_t sense_len;
  69. QTAILQ_HEAD(, SCSIRequest) requests;
  70. uint32_t channel;
  71. uint32_t lun;
  72. int blocksize;
  73. int type;
  74. uint64_t max_lba;
  75. uint64_t wwn;
  76. uint64_t port_wwn;
  77. int scsi_version;
  78. int default_scsi_version;
  79. uint32_t io_timeout;
  80. bool needs_vpd_bl_emulation;
  81. bool hba_supports_iothread;
  82. };
  83. extern const VMStateDescription vmstate_scsi_device;
  84. #define VMSTATE_SCSI_DEVICE(_field, _state) { \
  85. .name = (stringify(_field)), \
  86. .size = sizeof(SCSIDevice), \
  87. .vmsd = &vmstate_scsi_device, \
  88. .flags = VMS_STRUCT, \
  89. .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
  90. }
  91. /* cdrom.c */
  92. int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
  93. int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
  94. /* scsi-bus.c */
  95. struct SCSIReqOps {
  96. size_t size;
  97. void (*free_req)(SCSIRequest *req);
  98. int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
  99. void (*read_data)(SCSIRequest *req);
  100. void (*write_data)(SCSIRequest *req);
  101. uint8_t *(*get_buf)(SCSIRequest *req);
  102. void (*save_request)(QEMUFile *f, SCSIRequest *req);
  103. void (*load_request)(QEMUFile *f, SCSIRequest *req);
  104. };
  105. struct SCSIBusInfo {
  106. int tcq;
  107. int max_channel, max_target, max_lun;
  108. int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
  109. size_t buf_len, void *hba_private);
  110. void (*transfer_data)(SCSIRequest *req, uint32_t arg);
  111. void (*fail)(SCSIRequest *req);
  112. void (*complete)(SCSIRequest *req, size_t residual);
  113. void (*cancel)(SCSIRequest *req);
  114. void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
  115. QEMUSGList *(*get_sg_list)(SCSIRequest *req);
  116. void (*save_request)(QEMUFile *f, SCSIRequest *req);
  117. void *(*load_request)(QEMUFile *f, SCSIRequest *req);
  118. void (*free_request)(SCSIBus *bus, void *priv);
  119. };
  120. #define TYPE_SCSI_BUS "SCSI"
  121. OBJECT_DECLARE_SIMPLE_TYPE(SCSIBus, SCSI_BUS)
  122. struct SCSIBus {
  123. BusState qbus;
  124. int busnr;
  125. SCSISense unit_attention;
  126. const SCSIBusInfo *info;
  127. };
  128. /**
  129. * scsi_bus_init_named: Initialize a SCSI bus with the specified name
  130. * @bus: SCSIBus object to initialize
  131. * @bus_size: size of @bus object
  132. * @host: Device which owns the bus (generally the SCSI controller)
  133. * @info: structure defining callbacks etc for the controller
  134. * @bus_name: Name to use for this bus
  135. *
  136. * This in-place initializes @bus as a new SCSI bus with a name
  137. * provided by the caller. It is the caller's responsibility to make
  138. * sure that name does not clash with the name of any other bus in the
  139. * system. Unless you need the new bus to have a specific name, you
  140. * should use scsi_bus_init() instead.
  141. */
  142. void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
  143. const SCSIBusInfo *info, const char *bus_name);
  144. /**
  145. * scsi_bus_init: Initialize a SCSI bus
  146. *
  147. * This in-place-initializes @bus as a new SCSI bus and gives it
  148. * an automatically generated unique name.
  149. */
  150. static inline void scsi_bus_init(SCSIBus *bus, size_t bus_size,
  151. DeviceState *host, const SCSIBusInfo *info)
  152. {
  153. scsi_bus_init_named(bus, bus_size, host, info, NULL);
  154. }
  155. static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
  156. {
  157. return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
  158. }
  159. SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
  160. int unit, bool removable, int bootindex,
  161. bool share_rw,
  162. BlockdevOnError rerror,
  163. BlockdevOnError werror,
  164. const char *serial, Error **errp);
  165. void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense);
  166. void scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
  167. SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
  168. uint32_t tag, uint32_t lun, void *hba_private);
  169. SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
  170. uint8_t *buf, size_t buf_len, void *hba_private);
  171. int32_t scsi_req_enqueue(SCSIRequest *req);
  172. SCSIRequest *scsi_req_ref(SCSIRequest *req);
  173. void scsi_req_unref(SCSIRequest *req);
  174. int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
  175. size_t buf_len, void *hba_private);
  176. int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
  177. size_t buf_len);
  178. void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
  179. void scsi_req_print(SCSIRequest *req);
  180. void scsi_req_continue(SCSIRequest *req);
  181. void scsi_req_data(SCSIRequest *req, int len);
  182. void scsi_req_complete(SCSIRequest *req, int status);
  183. void scsi_req_complete_failed(SCSIRequest *req, int host_status);
  184. uint8_t *scsi_req_get_buf(SCSIRequest *req);
  185. int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
  186. void scsi_req_cancel_complete(SCSIRequest *req);
  187. void scsi_req_cancel(SCSIRequest *req);
  188. void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
  189. void scsi_req_retry(SCSIRequest *req);
  190. void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
  191. void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
  192. void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
  193. void scsi_device_unit_attention_reported(SCSIDevice *dev);
  194. void scsi_generic_read_device_inquiry(SCSIDevice *dev);
  195. int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
  196. int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
  197. uint8_t *buf, uint8_t buf_size, uint32_t timeout);
  198. SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
  199. SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);
  200. /* scsi-generic.c. */
  201. extern const SCSIReqOps scsi_generic_req_ops;
  202. /* scsi-disk.c */
  203. #define SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR 0
  204. #define SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD 1
  205. #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE 2
  206. #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED 3
  207. #endif