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