]> Git Repo - qemu.git/blame - include/hw/scsi/scsi.h
block: Pass errp in blkconf_geometry
[qemu.git] / include / hw / scsi / scsi.h
CommitLineData
1e37607b
GH
1#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
43b443b6 3
83c9f4ca 4#include "hw/qdev.h"
737e150e 5#include "block/block.h"
0d09e41a 6#include "hw/block/block.h"
9c17d615 7#include "sysemu/sysemu.h"
43b443b6 8
27d6bf40
MA
9#define MAX_SCSI_DEVS 255
10
29362ebe 11#define SCSI_CMD_BUF_SIZE 16
84642435
AH
12#define SCSI_SENSE_LEN 18
13#define SCSI_INQUIRY_LEN 36
29362ebe 14
43b443b6 15typedef struct SCSIBus SCSIBus;
afd4030c 16typedef struct SCSIBusInfo SCSIBusInfo;
2599aece 17typedef struct SCSICommand SCSICommand;
43b443b6 18typedef struct SCSIDevice SCSIDevice;
5c6c0e51 19typedef struct SCSIRequest SCSIRequest;
8dbd4574 20typedef struct SCSIReqOps SCSIReqOps;
43b443b6 21
97a06435
GH
22enum 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
a1f0cce2
HR
28typedef struct SCSISense {
29 uint8_t key;
30 uint8_t asc;
31 uint8_t ascq;
32} SCSISense;
33
2e323f03 34#define SCSI_SENSE_BUF_SIZE_OLD 96
c5f52875 35#define SCSI_SENSE_BUF_SIZE 252
b45ef674 36
2599aece
PB
37struct 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
5c6c0e51 45struct SCSIRequest {
4c41d2ef
GH
46 SCSIBus *bus;
47 SCSIDevice *dev;
adcf2754 48 const SCSIReqOps *ops;
ad2d30f7 49 uint32_t refcount;
4c41d2ef 50 uint32_t tag;
89b08ae1 51 uint32_t lun;
ed3a34a3 52 uint32_t status;
01e95455 53 size_t resid;
2599aece 54 SCSICommand cmd;
4c41d2ef 55 BlockDriverAIOCB *aiocb;
3d5aba97
PB
56 QEMUSGList *sg;
57 bool dma_started;
b45ef674
PB
58 uint8_t sense[SCSI_SENSE_BUF_SIZE];
59 uint32_t sense_len;
e8637c90 60 bool enqueued;
e88c591d 61 bool io_canceled;
71544d30 62 bool retry;
c5bf71a9 63 void *hba_private;
9af99d98 64 QTAILQ_ENTRY(SCSIRequest) next;
5c6c0e51 65};
4c41d2ef 66
b9eea3e6
AL
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
75typedef struct SCSIDeviceClass {
76 DeviceClass parent_class;
77 int (*init)(SCSIDevice *dev);
78 void (*destroy)(SCSIDevice *s);
ff34c32c
PB
79 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
80 void *hba_private);
b9eea3e6
AL
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
43b443b6
GH
86struct SCSIDevice
87{
88 DeviceState qdev;
71544d30
PB
89 VMChangeStateEntry *vmsentry;
90 QEMUBH *bh;
43b443b6 91 uint32_t id;
428c149b 92 BlockConf conf;
6dc06f08 93 SCSISense unit_attention;
3653d8c4 94 bool sense_is_ua;
b45ef674
PB
95 uint8_t sense[SCSI_SENSE_BUF_SIZE];
96 uint32_t sense_len;
9af99d98 97 QTAILQ_HEAD(, SCSIRequest) requests;
0d3545e7 98 uint32_t channel;
87dcd1b2 99 uint32_t lun;
b07995e3 100 int blocksize;
91376656 101 int type;
7877903a 102 uint64_t max_lba;
43b443b6
GH
103};
104
63f740dd
PB
105extern 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
43b443b6
GH
115/* cdrom.c */
116int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
117int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
118
119/* scsi-bus.c */
8dbd4574
PB
120struct SCSIReqOps {
121 size_t size;
12010e7b
PB
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);
63f740dd
PB
128
129 void (*save_request)(QEMUFile *f, SCSIRequest *req);
130 void (*load_request)(QEMUFile *f, SCSIRequest *req);
8dbd4574
PB
131};
132
afd4030c 133struct SCSIBusInfo {
7e0380b9 134 int tcq;
0d3545e7 135 int max_channel, max_target, max_lun;
ff34c32c
PB
136 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
137 void *hba_private);
c6df7102 138 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
01e95455 139 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
94d3f98a 140 void (*cancel)(SCSIRequest *req);
350e6e41
CM
141 void (*hotplug)(SCSIBus *bus, SCSIDevice *dev);
142 void (*hot_unplug)(SCSIBus *bus, SCSIDevice *dev);
53200fad 143 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
3d5aba97 144 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
63f740dd
PB
145
146 void (*save_request)(QEMUFile *f, SCSIRequest *req);
147 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
8e86b93c 148 void (*free_request)(SCSIBus *bus, void *priv);
cfdc1bb0
PB
149};
150
0d936928
AL
151#define TYPE_SCSI_BUS "SCSI"
152#define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
153
43b443b6
GH
154struct SCSIBus {
155 BusState qbus;
156 int busnr;
157
6dc06f08 158 SCSISense unit_attention;
afd4030c 159 const SCSIBusInfo *info;
43b443b6
GH
160};
161
b1187b51
AF
162void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
163 const SCSIBusInfo *info, const char *bus_name);
43b443b6
GH
164
165static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
166{
167 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
168}
169
2d1fd261 170SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
76534da7 171 int unit, bool removable, int bootindex,
caad4eb3
AF
172 const char *serial, Error **errp);
173void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp);
43b443b6 174
a1f0cce2
HR
175/*
176 * Predefined sense codes
177 */
178
179/* No sense data available */
180extern const struct SCSISense sense_code_NO_SENSE;
181/* LUN not ready, Manual intervention required */
182extern const struct SCSISense sense_code_LUN_NOT_READY;
183/* LUN not ready, Medium not present */
184extern const struct SCSISense sense_code_NO_MEDIUM;
68bb01f3
MA
185/* LUN not ready, medium removal prevented */
186extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
a1f0cce2
HR
187/* Hardware error, internal target failure */
188extern const struct SCSISense sense_code_TARGET_FAILURE;
189/* Illegal request, invalid command operation code */
190extern const struct SCSISense sense_code_INVALID_OPCODE;
191/* Illegal request, LBA out of range */
192extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
193/* Illegal request, Invalid field in CDB */
194extern const struct SCSISense sense_code_INVALID_FIELD;
380feaff
PB
195/* Illegal request, Invalid field in parameter list */
196extern const struct SCSISense sense_code_INVALID_PARAM;
197/* Illegal request, Parameter list length error */
198extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
a1f0cce2
HR
199/* Illegal request, LUN not supported */
200extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
a872a304
PB
201/* Illegal request, Saving parameters not supported */
202extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
203/* Illegal request, Incompatible format */
204extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
68bb01f3
MA
205/* Illegal request, medium removal prevented */
206extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
9ec557bd
HG
207/* Illegal request, Invalid Transfer Tag */
208extern const struct SCSISense sense_code_INVALID_TAG;
a1f0cce2
HR
209/* Command aborted, I/O process terminated */
210extern const struct SCSISense sense_code_IO_ERROR;
211/* Command aborted, I_T Nexus loss occurred */
212extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
213/* Command aborted, Logical Unit failure */
214extern const struct SCSISense sense_code_LUN_FAILURE;
9ec557bd
HG
215/* Command aborted, Overlapped Commands Attempted */
216extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
aaebacef
PB
217/* LUN not ready, Capacity data has changed */
218extern const struct SCSISense sense_code_CAPACITY_CHANGED;
8a9c16f6
PB
219/* LUN not ready, Medium not present */
220extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
a872a304
PB
221/* Unit attention, Power on, reset or bus device reset occurred */
222extern const struct SCSISense sense_code_RESET;
223/* Unit attention, Medium may have changed*/
224extern const struct SCSISense sense_code_MEDIUM_CHANGED;
225/* Unit attention, Reported LUNs data has changed */
226extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
227/* Unit attention, Device internal reset */
228extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
6a8a685c
RS
229/* Data Protection, Write Protected */
230extern const struct SCSISense sense_code_WRITE_PROTECTED;
703dd81a
PB
231/* Data Protection, Space Allocation Failed Write Protect */
232extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
a1f0cce2
HR
233
234#define SENSE_CODE(x) sense_code_ ## x
235
bb729f75
PB
236uint32_t scsi_data_cdb_length(uint8_t *buf);
237uint32_t scsi_cdb_length(uint8_t *buf);
a1f0cce2 238int scsi_sense_valid(SCSISense sense);
f3b338ef
PB
239int scsi_build_sense(uint8_t *in_buf, int in_len,
240 uint8_t *buf, int len, bool fixed);
a1f0cce2 241
adcf2754
PB
242SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
243 uint32_t tag, uint32_t lun, void *hba_private);
c5bf71a9 244SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
c39ce112
PB
245 uint8_t *buf, void *hba_private);
246int32_t scsi_req_enqueue(SCSIRequest *req);
89b08ae1 247void scsi_req_free(SCSIRequest *req);
ad2d30f7
PB
248SCSIRequest *scsi_req_ref(SCSIRequest *req);
249void scsi_req_unref(SCSIRequest *req);
37659e51 250
ff34c32c
PB
251int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
252 void *hba_private);
3e7e180a 253int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
b45ef674 254void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
ec766865 255void scsi_req_print(SCSIRequest *req);
ad3376cc 256void scsi_req_continue(SCSIRequest *req);
ab9adc88 257void scsi_req_data(SCSIRequest *req, int len);
682a9b21 258void scsi_req_complete(SCSIRequest *req, int status);
0c34459b 259uint8_t *scsi_req_get_buf(SCSIRequest *req);
74382217 260int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
19d110ab 261void scsi_req_abort(SCSIRequest *req, int status);
94d3f98a 262void scsi_req_cancel(SCSIRequest *req);
71544d30 263void scsi_req_retry(SCSIRequest *req);
c7b48872 264void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
e48e84ea 265void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
53200fad 266void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
b45ef674 267int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
0d3545e7 268SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
89b08ae1 269
765d1525
PB
270/* scsi-generic.c. */
271extern const SCSIReqOps scsi_generic_req_ops;
272
43b443b6 273#endif
This page took 0.594612 seconds and 4 git commands to generate.