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