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