1#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
3
4#include "hw/qdev.h"
5#include "qemu/typedefs.h"
6#include "hw/block/block.h"
7#include "sysemu/sysemu.h"
8#include "qemu/notify.h"
9
10#define MAX_SCSI_DEVS 255
11
12#define SCSI_CMD_BUF_SIZE 16
13#define SCSI_SENSE_LEN 18
14#define SCSI_INQUIRY_LEN 36
15
16typedef struct SCSIBus SCSIBus;
17typedef struct SCSIBusInfo SCSIBusInfo;
18typedef struct SCSICommand SCSICommand;
19typedef struct SCSIDevice SCSIDevice;
20typedef struct SCSIRequest SCSIRequest;
21typedef struct SCSIReqOps SCSIReqOps;
22
23enum SCSIXferMode {
24 SCSI_XFER_NONE,
25 SCSI_XFER_FROM_DEV,
26 SCSI_XFER_TO_DEV,
27};
28
29typedef struct SCSISense {
30 uint8_t key;
31 uint8_t asc;
32 uint8_t ascq;
33} SCSISense;
34
35#define SCSI_SENSE_BUF_SIZE_OLD 96
36#define SCSI_SENSE_BUF_SIZE 252
37
38struct SCSICommand {
39 uint8_t buf[SCSI_CMD_BUF_SIZE];
40 int len;
41 size_t xfer;
42 uint64_t lba;
43 enum SCSIXferMode mode;
44};
45
46struct SCSIRequest {
47 SCSIBus *bus;
48 SCSIDevice *dev;
49 const SCSIReqOps *ops;
50 uint32_t refcount;
51 uint32_t tag;
52 uint32_t lun;
53 uint32_t status;
54 void *hba_private;
55 size_t resid;
56 SCSICommand cmd;
57 NotifierList cancel_notifiers;
58
59
60
61
62
63
64
65 uint8_t sense[SCSI_SENSE_BUF_SIZE];
66 uint32_t sense_len;
67 bool enqueued;
68 bool io_canceled;
69 bool retry;
70 bool dma_started;
71 BlockAIOCB *aiocb;
72 QEMUSGList *sg;
73 QTAILQ_ENTRY(SCSIRequest) next;
74};
75
76#define TYPE_SCSI_DEVICE "scsi-device"
77#define SCSI_DEVICE(obj) \
78 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
79#define SCSI_DEVICE_CLASS(klass) \
80 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
81#define SCSI_DEVICE_GET_CLASS(obj) \
82 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
83
84typedef struct SCSIDeviceClass {
85 DeviceClass parent_class;
86 void (*realize)(SCSIDevice *dev, Error **errp);
87 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
88 void *hba_private);
89 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
90 uint8_t *buf, void *hba_private);
91 void (*unit_attention_reported)(SCSIDevice *s);
92} SCSIDeviceClass;
93
94struct SCSIDevice
95{
96 DeviceState qdev;
97 VMChangeStateEntry *vmsentry;
98 QEMUBH *bh;
99 uint32_t id;
100 BlockConf conf;
101 SCSISense unit_attention;
102 bool sense_is_ua;
103 uint8_t sense[SCSI_SENSE_BUF_SIZE];
104 uint32_t sense_len;
105 QTAILQ_HEAD(, SCSIRequest) requests;
106 uint32_t channel;
107 uint32_t lun;
108 int blocksize;
109 int type;
110 uint64_t max_lba;
111};
112
113extern const VMStateDescription vmstate_scsi_device;
114
115#define VMSTATE_SCSI_DEVICE(_field, _state) { \
116 .name = (stringify(_field)), \
117 .size = sizeof(SCSIDevice), \
118 .vmsd = &vmstate_scsi_device, \
119 .flags = VMS_STRUCT, \
120 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
121}
122
123
124int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
125int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
126
127
128struct SCSIReqOps {
129 size_t size;
130 void (*free_req)(SCSIRequest *req);
131 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
132 void (*read_data)(SCSIRequest *req);
133 void (*write_data)(SCSIRequest *req);
134 uint8_t *(*get_buf)(SCSIRequest *req);
135
136 void (*save_request)(QEMUFile *f, SCSIRequest *req);
137 void (*load_request)(QEMUFile *f, SCSIRequest *req);
138};
139
140struct SCSIBusInfo {
141 int tcq;
142 int max_channel, max_target, max_lun;
143 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
144 void *hba_private);
145 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
146 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
147 void (*cancel)(SCSIRequest *req);
148 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
149 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
150
151 void (*save_request)(QEMUFile *f, SCSIRequest *req);
152 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
153 void (*free_request)(SCSIBus *bus, void *priv);
154};
155
156#define TYPE_SCSI_BUS "SCSI"
157#define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
158
159struct SCSIBus {
160 BusState qbus;
161 int busnr;
162
163 SCSISense unit_attention;
164 const SCSIBusInfo *info;
165};
166
167void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
168 const SCSIBusInfo *info, const char *bus_name);
169
170static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
171{
172 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
173}
174
175SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
176 int unit, bool removable, int bootindex,
177 const char *serial, Error **errp);
178void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp);
179
180
181
182
183
184
185extern const struct SCSISense sense_code_NO_SENSE;
186
187extern const struct SCSISense sense_code_LUN_NOT_READY;
188
189extern const struct SCSISense sense_code_NO_MEDIUM;
190
191extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
192
193extern const struct SCSISense sense_code_TARGET_FAILURE;
194
195extern const struct SCSISense sense_code_INVALID_OPCODE;
196
197extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
198
199extern const struct SCSISense sense_code_INVALID_FIELD;
200
201extern const struct SCSISense sense_code_INVALID_PARAM;
202
203extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
204
205extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
206
207extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
208
209extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
210
211extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
212
213extern const struct SCSISense sense_code_INVALID_TAG;
214
215extern const struct SCSISense sense_code_IO_ERROR;
216
217extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
218
219extern const struct SCSISense sense_code_LUN_FAILURE;
220
221extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
222
223extern const struct SCSISense sense_code_CAPACITY_CHANGED;
224
225extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
226
227extern const struct SCSISense sense_code_RESET;
228
229extern const struct SCSISense sense_code_MEDIUM_CHANGED;
230
231extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
232
233extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
234
235extern const struct SCSISense sense_code_WRITE_PROTECTED;
236
237extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
238
239#define SENSE_CODE(x) sense_code_ ## x
240
241uint32_t scsi_data_cdb_xfer(uint8_t *buf);
242uint32_t scsi_cdb_xfer(uint8_t *buf);
243int scsi_cdb_length(uint8_t *buf);
244int scsi_sense_valid(SCSISense sense);
245int scsi_build_sense(uint8_t *in_buf, int in_len,
246 uint8_t *buf, int len, bool fixed);
247
248SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
249 uint32_t tag, uint32_t lun, void *hba_private);
250SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
251 uint8_t *buf, void *hba_private);
252int32_t scsi_req_enqueue(SCSIRequest *req);
253void scsi_req_free(SCSIRequest *req);
254SCSIRequest *scsi_req_ref(SCSIRequest *req);
255void scsi_req_unref(SCSIRequest *req);
256
257int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
258 void *hba_private);
259int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
260void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
261void scsi_req_print(SCSIRequest *req);
262void scsi_req_continue(SCSIRequest *req);
263void scsi_req_data(SCSIRequest *req, int len);
264void scsi_req_complete(SCSIRequest *req, int status);
265uint8_t *scsi_req_get_buf(SCSIRequest *req);
266int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
267void scsi_req_cancel_complete(SCSIRequest *req);
268void scsi_req_cancel(SCSIRequest *req);
269void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
270void scsi_req_retry(SCSIRequest *req);
271void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
272void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
273void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
274void scsi_device_unit_attention_reported(SCSIDevice *dev);
275int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
276SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
277
278
279extern const SCSIReqOps scsi_generic_req_ops;
280
281#endif
282