Merge commit 'd34e8f6e9d3a396c3327aa9807c83f9e1f4a7bd7' into upstream-merge
[qemu-kvm.git] / hw / scsi.h
blob4290b20df52a609e42462c55e58cb698393cd635
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
4 #include "qdev.h"
5 #include "block.h"
6 #include "sysemu.h"
8 #define MAX_SCSI_DEVS 255
10 #define SCSI_CMD_BUF_SIZE 16
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;
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, ... */
25 typedef struct SCSISense {
26 uint8_t key;
27 uint8_t asc;
28 uint8_t ascq;
29 } SCSISense;
31 #define SCSI_SENSE_BUF_SIZE 96
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;
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 SCSICommand cmd;
50 BlockDriverAIOCB *aiocb;
51 uint8_t sense[SCSI_SENSE_BUF_SIZE];
52 uint32_t sense_len;
53 bool enqueued;
54 bool io_canceled;
55 bool retry;
56 void *hba_private;
57 QTAILQ_ENTRY(SCSIRequest) next;
60 #define TYPE_SCSI_DEVICE "scsi-device"
61 #define SCSI_DEVICE(obj) \
62 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
63 #define SCSI_DEVICE_CLASS(klass) \
64 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
65 #define SCSI_DEVICE_GET_CLASS(obj) \
66 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
68 typedef struct SCSIDeviceClass {
69 DeviceClass parent_class;
70 int (*init)(SCSIDevice *dev);
71 void (*destroy)(SCSIDevice *s);
72 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
73 uint8_t *buf, void *hba_private);
74 void (*unit_attention_reported)(SCSIDevice *s);
75 } SCSIDeviceClass;
77 struct SCSIDevice
79 DeviceState qdev;
80 VMChangeStateEntry *vmsentry;
81 QEMUBH *bh;
82 uint32_t id;
83 BlockConf conf;
84 SCSISense unit_attention;
85 bool sense_is_ua;
86 uint8_t sense[SCSI_SENSE_BUF_SIZE];
87 uint32_t sense_len;
88 QTAILQ_HEAD(, SCSIRequest) requests;
89 uint32_t channel;
90 uint32_t lun;
91 int blocksize;
92 int type;
93 uint64_t max_lba;
96 /* cdrom.c */
97 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
98 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
100 /* scsi-bus.c */
101 struct SCSIReqOps {
102 size_t size;
103 void (*free_req)(SCSIRequest *req);
104 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
105 void (*read_data)(SCSIRequest *req);
106 void (*write_data)(SCSIRequest *req);
107 void (*cancel_io)(SCSIRequest *req);
108 uint8_t *(*get_buf)(SCSIRequest *req);
111 struct SCSIBusInfo {
112 int tcq;
113 int max_channel, max_target, max_lun;
114 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
115 void (*complete)(SCSIRequest *req, uint32_t arg);
116 void (*cancel)(SCSIRequest *req);
119 struct SCSIBus {
120 BusState qbus;
121 int busnr;
123 SCSISense unit_attention;
124 const SCSIBusInfo *info;
127 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
128 void scsi_qdev_register(DeviceInfo *info);
130 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
132 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
135 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
136 int unit, bool removable, int bootindex);
137 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
140 * Predefined sense codes
143 /* No sense data available */
144 extern const struct SCSISense sense_code_NO_SENSE;
145 /* LUN not ready, Manual intervention required */
146 extern const struct SCSISense sense_code_LUN_NOT_READY;
147 /* LUN not ready, Medium not present */
148 extern const struct SCSISense sense_code_NO_MEDIUM;
149 /* LUN not ready, medium removal prevented */
150 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
151 /* Hardware error, internal target failure */
152 extern const struct SCSISense sense_code_TARGET_FAILURE;
153 /* Illegal request, invalid command operation code */
154 extern const struct SCSISense sense_code_INVALID_OPCODE;
155 /* Illegal request, LBA out of range */
156 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
157 /* Illegal request, Invalid field in CDB */
158 extern const struct SCSISense sense_code_INVALID_FIELD;
159 /* Illegal request, LUN not supported */
160 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
161 /* Illegal request, Saving parameters not supported */
162 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
163 /* Illegal request, Incompatible format */
164 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
165 /* Illegal request, medium removal prevented */
166 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
167 /* Command aborted, I/O process terminated */
168 extern const struct SCSISense sense_code_IO_ERROR;
169 /* Command aborted, I_T Nexus loss occurred */
170 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
171 /* Command aborted, Logical Unit failure */
172 extern const struct SCSISense sense_code_LUN_FAILURE;
173 /* LUN not ready, Medium not present */
174 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
175 /* Unit attention, Power on, reset or bus device reset occurred */
176 extern const struct SCSISense sense_code_RESET;
177 /* Unit attention, Medium may have changed*/
178 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
179 /* Unit attention, Reported LUNs data has changed */
180 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
181 /* Unit attention, Device internal reset */
182 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
184 #define SENSE_CODE(x) sense_code_ ## x
186 int scsi_sense_valid(SCSISense sense);
187 int scsi_build_sense(uint8_t *in_buf, int in_len,
188 uint8_t *buf, int len, bool fixed);
190 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
191 uint32_t tag, uint32_t lun, void *hba_private);
192 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
193 uint8_t *buf, void *hba_private);
194 int32_t scsi_req_enqueue(SCSIRequest *req);
195 void scsi_req_free(SCSIRequest *req);
196 SCSIRequest *scsi_req_ref(SCSIRequest *req);
197 void scsi_req_unref(SCSIRequest *req);
199 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
200 void scsi_req_print(SCSIRequest *req);
201 void scsi_req_continue(SCSIRequest *req);
202 void scsi_req_data(SCSIRequest *req, int len);
203 void scsi_req_complete(SCSIRequest *req, int status);
204 uint8_t *scsi_req_get_buf(SCSIRequest *req);
205 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
206 void scsi_req_abort(SCSIRequest *req, int status);
207 void scsi_req_cancel(SCSIRequest *req);
208 void scsi_req_retry(SCSIRequest *req);
209 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
210 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
211 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
213 /* scsi-generic.c. */
214 extern const SCSIReqOps scsi_generic_req_ops;
216 #endif