xref: /openbmc/qemu/include/hw/scsi/scsi.h (revision bfb27e60)
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
3 
4 #include "hw/qdev.h"
5 #include "block/block.h"
6 #include "hw/block/block.h"
7 #include "sysemu/sysemu.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 
15 typedef struct SCSIBus SCSIBus;
16 typedef struct SCSIBusInfo SCSIBusInfo;
17 typedef struct SCSICommand SCSICommand;
18 typedef struct SCSIDevice SCSIDevice;
19 typedef struct SCSIRequest SCSIRequest;
20 typedef struct SCSIReqOps SCSIReqOps;
21 
22 enum SCSIXferMode {
23     SCSI_XFER_NONE,      /*  TEST_UNIT_READY, ...            */
24     SCSI_XFER_FROM_DEV,  /*  READ, INQUIRY, MODE_SENSE, ...  */
25     SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
26 };
27 
28 typedef 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 
37 struct 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 
45 struct 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     size_t            resid;
54     SCSICommand       cmd;
55     BlockDriverAIOCB  *aiocb;
56     QEMUSGList        *sg;
57     bool              dma_started;
58     uint8_t sense[SCSI_SENSE_BUF_SIZE];
59     uint32_t sense_len;
60     bool enqueued;
61     bool io_canceled;
62     bool retry;
63     void *hba_private;
64     QTAILQ_ENTRY(SCSIRequest) next;
65 };
66 
67 #define TYPE_SCSI_DEVICE "scsi-device"
68 #define SCSI_DEVICE(obj) \
69      OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
70 #define SCSI_DEVICE_CLASS(klass) \
71      OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
72 #define SCSI_DEVICE_GET_CLASS(obj) \
73      OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
74 
75 typedef struct SCSIDeviceClass {
76     DeviceClass parent_class;
77     void (*realize)(SCSIDevice *dev, Error **errp);
78     void (*unrealize)(SCSIDevice *dev, Error **errp);
79     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
80                      void *hba_private);
81     SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
82                               uint8_t *buf, void *hba_private);
83     void (*unit_attention_reported)(SCSIDevice *s);
84 } SCSIDeviceClass;
85 
86 struct SCSIDevice
87 {
88     DeviceState qdev;
89     VMChangeStateEntry *vmsentry;
90     QEMUBH *bh;
91     uint32_t id;
92     BlockConf conf;
93     SCSISense unit_attention;
94     bool sense_is_ua;
95     uint8_t sense[SCSI_SENSE_BUF_SIZE];
96     uint32_t sense_len;
97     QTAILQ_HEAD(, SCSIRequest) requests;
98     uint32_t channel;
99     uint32_t lun;
100     int blocksize;
101     int type;
102     uint64_t max_lba;
103 };
104 
105 extern const VMStateDescription vmstate_scsi_device;
106 
107 #define VMSTATE_SCSI_DEVICE(_field, _state) {                        \
108     .name       = (stringify(_field)),                               \
109     .size       = sizeof(SCSIDevice),                                \
110     .vmsd       = &vmstate_scsi_device,                              \
111     .flags      = VMS_STRUCT,                                        \
112     .offset     = vmstate_offset_value(_state, _field, SCSIDevice),  \
113 }
114 
115 /* cdrom.c */
116 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
117 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
118 
119 /* scsi-bus.c */
120 struct SCSIReqOps {
121     size_t size;
122     void (*free_req)(SCSIRequest *req);
123     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
124     void (*read_data)(SCSIRequest *req);
125     void (*write_data)(SCSIRequest *req);
126     void (*cancel_io)(SCSIRequest *req);
127     uint8_t *(*get_buf)(SCSIRequest *req);
128 
129     void (*save_request)(QEMUFile *f, SCSIRequest *req);
130     void (*load_request)(QEMUFile *f, SCSIRequest *req);
131 };
132 
133 struct SCSIBusInfo {
134     int tcq;
135     int max_channel, max_target, max_lun;
136     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
137                      void *hba_private);
138     void (*transfer_data)(SCSIRequest *req, uint32_t arg);
139     void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
140     void (*cancel)(SCSIRequest *req);
141     void (*hotplug)(SCSIBus *bus, SCSIDevice *dev);
142     void (*hot_unplug)(SCSIBus *bus, SCSIDevice *dev);
143     void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
144     QEMUSGList *(*get_sg_list)(SCSIRequest *req);
145 
146     void (*save_request)(QEMUFile *f, SCSIRequest *req);
147     void *(*load_request)(QEMUFile *f, SCSIRequest *req);
148     void (*free_request)(SCSIBus *bus, void *priv);
149 };
150 
151 #define TYPE_SCSI_BUS "SCSI"
152 #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
153 
154 struct SCSIBus {
155     BusState qbus;
156     int busnr;
157 
158     SCSISense unit_attention;
159     const SCSIBusInfo *info;
160 };
161 
162 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
163                   const SCSIBusInfo *info, const char *bus_name);
164 
165 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
166 {
167     return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
168 }
169 
170 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
171                                       int unit, bool removable, int bootindex,
172                                       const char *serial, Error **errp);
173 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp);
174 
175 /*
176  * Predefined sense codes
177  */
178 
179 /* No sense data available */
180 extern const struct SCSISense sense_code_NO_SENSE;
181 /* LUN not ready, Manual intervention required */
182 extern const struct SCSISense sense_code_LUN_NOT_READY;
183 /* LUN not ready, Medium not present */
184 extern const struct SCSISense sense_code_NO_MEDIUM;
185 /* LUN not ready, medium removal prevented */
186 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
187 /* Hardware error, internal target failure */
188 extern const struct SCSISense sense_code_TARGET_FAILURE;
189 /* Illegal request, invalid command operation code */
190 extern const struct SCSISense sense_code_INVALID_OPCODE;
191 /* Illegal request, LBA out of range */
192 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
193 /* Illegal request, Invalid field in CDB */
194 extern const struct SCSISense sense_code_INVALID_FIELD;
195 /* Illegal request, Invalid field in parameter list */
196 extern const struct SCSISense sense_code_INVALID_PARAM;
197 /* Illegal request, Parameter list length error */
198 extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
199 /* Illegal request, LUN not supported */
200 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
201 /* Illegal request, Saving parameters not supported */
202 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
203 /* Illegal request, Incompatible format */
204 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
205 /* Illegal request, medium removal prevented */
206 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
207 /* Illegal request, Invalid Transfer Tag */
208 extern const struct SCSISense sense_code_INVALID_TAG;
209 /* Command aborted, I/O process terminated */
210 extern const struct SCSISense sense_code_IO_ERROR;
211 /* Command aborted, I_T Nexus loss occurred */
212 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
213 /* Command aborted, Logical Unit failure */
214 extern const struct SCSISense sense_code_LUN_FAILURE;
215 /* Command aborted, Overlapped Commands Attempted */
216 extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
217 /* LUN not ready, Capacity data has changed */
218 extern const struct SCSISense sense_code_CAPACITY_CHANGED;
219 /* LUN not ready, Medium not present */
220 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
221 /* Unit attention, Power on, reset or bus device reset occurred */
222 extern const struct SCSISense sense_code_RESET;
223 /* Unit attention, Medium may have changed*/
224 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
225 /* Unit attention, Reported LUNs data has changed */
226 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
227 /* Unit attention, Device internal reset */
228 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
229 /* Data Protection, Write Protected */
230 extern const struct SCSISense sense_code_WRITE_PROTECTED;
231 /* Data Protection, Space Allocation Failed Write Protect */
232 extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
233 
234 #define SENSE_CODE(x) sense_code_ ## x
235 
236 uint32_t scsi_data_cdb_length(uint8_t *buf);
237 uint32_t scsi_cdb_length(uint8_t *buf);
238 int scsi_sense_valid(SCSISense sense);
239 int scsi_build_sense(uint8_t *in_buf, int in_len,
240                      uint8_t *buf, int len, bool fixed);
241 
242 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
243                             uint32_t tag, uint32_t lun, void *hba_private);
244 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
245                           uint8_t *buf, void *hba_private);
246 int32_t scsi_req_enqueue(SCSIRequest *req);
247 void scsi_req_free(SCSIRequest *req);
248 SCSIRequest *scsi_req_ref(SCSIRequest *req);
249 void scsi_req_unref(SCSIRequest *req);
250 
251 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
252                        void *hba_private);
253 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
254 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
255 void scsi_req_print(SCSIRequest *req);
256 void scsi_req_continue(SCSIRequest *req);
257 void scsi_req_data(SCSIRequest *req, int len);
258 void scsi_req_complete(SCSIRequest *req, int status);
259 uint8_t *scsi_req_get_buf(SCSIRequest *req);
260 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
261 void scsi_req_abort(SCSIRequest *req, int status);
262 void scsi_req_cancel(SCSIRequest *req);
263 void scsi_req_retry(SCSIRequest *req);
264 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
265 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
266 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
267 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
268 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
269 
270 /* scsi-generic.c. */
271 extern const SCSIReqOps scsi_generic_req_ops;
272 
273 #endif
274