]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi.h
scsi: add scatter/gather functionality
[mirror_qemu.git] / hw / scsi.h
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
3
4 #include "qdev.h"
5 #include "block.h"
6 #include "sysemu.h"
7
8 #define MAX_SCSI_DEVS 255
9
10 #define SCSI_CMD_BUF_SIZE 16
11
12 typedef struct SCSIBus SCSIBus;
13 typedef struct SCSIBusInfo SCSIBusInfo;
14 typedef struct SCSICommand SCSICommand;
15 typedef struct SCSIDevice SCSIDevice;
16 typedef struct SCSIRequest SCSIRequest;
17 typedef struct SCSIReqOps SCSIReqOps;
18
19 enum SCSIXferMode {
20 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
21 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
22 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
23 };
24
25 typedef struct SCSISense {
26 uint8_t key;
27 uint8_t asc;
28 uint8_t ascq;
29 } SCSISense;
30
31 #define SCSI_SENSE_BUF_SIZE 96
32
33 struct SCSICommand {
34 uint8_t buf[SCSI_CMD_BUF_SIZE];
35 int len;
36 size_t xfer;
37 uint64_t lba;
38 enum SCSIXferMode mode;
39 };
40
41 struct SCSIRequest {
42 SCSIBus *bus;
43 SCSIDevice *dev;
44 const SCSIReqOps *ops;
45 uint32_t refcount;
46 uint32_t tag;
47 uint32_t lun;
48 uint32_t status;
49 size_t resid;
50 SCSICommand cmd;
51 BlockDriverAIOCB *aiocb;
52 QEMUSGList *sg;
53 bool dma_started;
54 uint8_t sense[SCSI_SENSE_BUF_SIZE];
55 uint32_t sense_len;
56 bool enqueued;
57 bool io_canceled;
58 bool retry;
59 void *hba_private;
60 QTAILQ_ENTRY(SCSIRequest) next;
61 };
62
63 #define TYPE_SCSI_DEVICE "scsi-device"
64 #define SCSI_DEVICE(obj) \
65 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
66 #define SCSI_DEVICE_CLASS(klass) \
67 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
68 #define SCSI_DEVICE_GET_CLASS(obj) \
69 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
70
71 typedef struct SCSIDeviceClass {
72 DeviceClass parent_class;
73 int (*init)(SCSIDevice *dev);
74 void (*destroy)(SCSIDevice *s);
75 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
76 uint8_t *buf, void *hba_private);
77 void (*unit_attention_reported)(SCSIDevice *s);
78 } SCSIDeviceClass;
79
80 struct SCSIDevice
81 {
82 DeviceState qdev;
83 VMChangeStateEntry *vmsentry;
84 QEMUBH *bh;
85 uint32_t id;
86 BlockConf conf;
87 SCSISense unit_attention;
88 bool sense_is_ua;
89 uint8_t sense[SCSI_SENSE_BUF_SIZE];
90 uint32_t sense_len;
91 QTAILQ_HEAD(, SCSIRequest) requests;
92 uint32_t channel;
93 uint32_t lun;
94 int blocksize;
95 int type;
96 uint64_t max_lba;
97 };
98
99 /* cdrom.c */
100 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
101 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
102
103 /* scsi-bus.c */
104 struct SCSIReqOps {
105 size_t size;
106 void (*free_req)(SCSIRequest *req);
107 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
108 void (*read_data)(SCSIRequest *req);
109 void (*write_data)(SCSIRequest *req);
110 void (*cancel_io)(SCSIRequest *req);
111 uint8_t *(*get_buf)(SCSIRequest *req);
112 };
113
114 struct SCSIBusInfo {
115 int tcq;
116 int max_channel, max_target, max_lun;
117 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
118 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
119 void (*cancel)(SCSIRequest *req);
120 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
121 };
122
123 struct SCSIBus {
124 BusState qbus;
125 int busnr;
126
127 SCSISense unit_attention;
128 const SCSIBusInfo *info;
129 };
130
131 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
132
133 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
134 {
135 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
136 }
137
138 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
139 int unit, bool removable, int bootindex);
140 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
141
142 /*
143 * Predefined sense codes
144 */
145
146 /* No sense data available */
147 extern const struct SCSISense sense_code_NO_SENSE;
148 /* LUN not ready, Manual intervention required */
149 extern const struct SCSISense sense_code_LUN_NOT_READY;
150 /* LUN not ready, Medium not present */
151 extern const struct SCSISense sense_code_NO_MEDIUM;
152 /* LUN not ready, medium removal prevented */
153 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
154 /* Hardware error, internal target failure */
155 extern const struct SCSISense sense_code_TARGET_FAILURE;
156 /* Illegal request, invalid command operation code */
157 extern const struct SCSISense sense_code_INVALID_OPCODE;
158 /* Illegal request, LBA out of range */
159 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
160 /* Illegal request, Invalid field in CDB */
161 extern const struct SCSISense sense_code_INVALID_FIELD;
162 /* Illegal request, LUN not supported */
163 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
164 /* Illegal request, Saving parameters not supported */
165 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
166 /* Illegal request, Incompatible format */
167 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
168 /* Illegal request, medium removal prevented */
169 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
170 /* Command aborted, I/O process terminated */
171 extern const struct SCSISense sense_code_IO_ERROR;
172 /* Command aborted, I_T Nexus loss occurred */
173 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
174 /* Command aborted, Logical Unit failure */
175 extern const struct SCSISense sense_code_LUN_FAILURE;
176 /* LUN not ready, Medium not present */
177 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
178 /* Unit attention, Power on, reset or bus device reset occurred */
179 extern const struct SCSISense sense_code_RESET;
180 /* Unit attention, Medium may have changed*/
181 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
182 /* Unit attention, Reported LUNs data has changed */
183 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
184 /* Unit attention, Device internal reset */
185 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
186
187 #define SENSE_CODE(x) sense_code_ ## x
188
189 int scsi_sense_valid(SCSISense sense);
190 int scsi_build_sense(uint8_t *in_buf, int in_len,
191 uint8_t *buf, int len, bool fixed);
192
193 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
194 uint32_t tag, uint32_t lun, void *hba_private);
195 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
196 uint8_t *buf, void *hba_private);
197 int32_t scsi_req_enqueue(SCSIRequest *req);
198 void scsi_req_free(SCSIRequest *req);
199 SCSIRequest *scsi_req_ref(SCSIRequest *req);
200 void scsi_req_unref(SCSIRequest *req);
201
202 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
203 void scsi_req_print(SCSIRequest *req);
204 void scsi_req_continue(SCSIRequest *req);
205 void scsi_req_data(SCSIRequest *req, int len);
206 void scsi_req_complete(SCSIRequest *req, int status);
207 uint8_t *scsi_req_get_buf(SCSIRequest *req);
208 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
209 void scsi_req_abort(SCSIRequest *req, int status);
210 void scsi_req_cancel(SCSIRequest *req);
211 void scsi_req_retry(SCSIRequest *req);
212 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
213 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
214 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
215
216 /* scsi-generic.c. */
217 extern const SCSIReqOps scsi_generic_req_ops;
218
219 #endif