8 #define MAX_SCSI_DEVS 255
10 #define SCSI_CMD_BUF_SIZE 16
14 SCSI_REASON_DONE
, /* Command complete. */
15 SCSI_REASON_DATA
/* Transfer complete, more data required. */
18 typedef struct SCSIBus SCSIBus
;
19 typedef struct SCSIBusOps SCSIBusOps
;
20 typedef struct SCSIDevice SCSIDevice
;
21 typedef struct SCSIDeviceInfo SCSIDeviceInfo
;
22 typedef struct SCSIRequest SCSIRequest
;
25 SCSI_XFER_NONE
, /* TEST_UNIT_READY, ... */
26 SCSI_XFER_FROM_DEV
, /* READ, INQUIRY, MODE_SENSE, ... */
27 SCSI_XFER_TO_DEV
, /* WRITE, MODE_SELECT, ... */
30 typedef struct SCSISense
{
44 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
48 enum SCSIXferMode mode
;
50 BlockDriverAIOCB
*aiocb
;
52 QTAILQ_ENTRY(SCSIRequest
) next
;
61 QTAILQ_HEAD(, SCSIRequest
) requests
;
67 int cdrom_read_toc(int nb_sectors
, uint8_t *buf
, int msf
, int start_track
);
68 int cdrom_read_toc_raw(int nb_sectors
, uint8_t *buf
, int msf
, int session_num
);
71 typedef int (*scsi_qdev_initfn
)(SCSIDevice
*dev
);
72 struct SCSIDeviceInfo
{
74 scsi_qdev_initfn init
;
75 void (*destroy
)(SCSIDevice
*s
);
76 SCSIRequest
*(*alloc_req
)(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
);
77 void (*free_req
)(SCSIRequest
*req
);
78 int32_t (*send_command
)(SCSIRequest
*req
, uint8_t *buf
);
79 void (*read_data
)(SCSIRequest
*req
);
80 int (*write_data
)(SCSIRequest
*req
);
81 void (*cancel_io
)(SCSIRequest
*req
);
82 uint8_t *(*get_buf
)(SCSIRequest
*req
);
86 void (*complete
)(SCSIRequest
*req
, int reason
, uint32_t arg
);
87 void (*cancel
)(SCSIRequest
*req
);
95 const SCSIBusOps
*ops
;
97 SCSIDevice
*devs
[MAX_SCSI_DEVS
];
100 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
101 const SCSIBusOps
*ops
);
102 void scsi_qdev_register(SCSIDeviceInfo
*info
);
104 static inline SCSIBus
*scsi_bus_from_device(SCSIDevice
*d
)
106 return DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
109 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
110 int unit
, bool removable
);
111 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
);
114 * Predefined sense codes
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 /* Hardware error, internal target failure */
124 extern const struct SCSISense sense_code_TARGET_FAILURE
;
125 /* Illegal request, invalid command operation code */
126 extern const struct SCSISense sense_code_INVALID_OPCODE
;
127 /* Illegal request, LBA out of range */
128 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE
;
129 /* Illegal request, Invalid field in CDB */
130 extern const struct SCSISense sense_code_INVALID_FIELD
;
131 /* Illegal request, LUN not supported */
132 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED
;
133 /* Command aborted, I/O process terminated */
134 extern const struct SCSISense sense_code_IO_ERROR
;
135 /* Command aborted, I_T Nexus loss occurred */
136 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS
;
137 /* Command aborted, Logical Unit failure */
138 extern const struct SCSISense sense_code_LUN_FAILURE
;
140 #define SENSE_CODE(x) sense_code_ ## x
142 int scsi_build_sense(SCSISense sense
, uint8_t *buf
, int len
, int fixed
);
143 int scsi_sense_valid(SCSISense sense
);
145 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
);
146 void scsi_req_enqueue(SCSIRequest
*req
);
147 void scsi_req_free(SCSIRequest
*req
);
148 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
);
149 void scsi_req_unref(SCSIRequest
*req
);
151 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
);
152 void scsi_req_print(SCSIRequest
*req
);
153 void scsi_req_data(SCSIRequest
*req
, int len
);
154 void scsi_req_complete(SCSIRequest
*req
);
155 void scsi_req_abort(SCSIRequest
*req
, int status
);
156 void scsi_req_cancel(SCSIRequest
*req
);
157 void scsi_device_purge_requests(SCSIDevice
*sdev
);