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