]> git.proxmox.com Git - qemu.git/blame - hw/scsi.h
scsi-disk: improve the lba-out-of-range tests for read/write/verify
[qemu.git] / hw / scsi.h
CommitLineData
1e37607b
GH
1#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
43b443b6
GH
3
4#include "qdev.h"
4c41d2ef 5#include "block.h"
31e404f4 6#include "hw/block-common.h"
71544d30 7#include "sysemu.h"
43b443b6 8
27d6bf40
MA
9#define MAX_SCSI_DEVS 255
10
29362ebe
GH
11#define SCSI_CMD_BUF_SIZE 16
12
43b443b6 13typedef struct SCSIBus SCSIBus;
afd4030c 14typedef struct SCSIBusInfo SCSIBusInfo;
2599aece 15typedef struct SCSICommand SCSICommand;
43b443b6 16typedef struct SCSIDevice SCSIDevice;
5c6c0e51 17typedef struct SCSIRequest SCSIRequest;
8dbd4574 18typedef struct SCSIReqOps SCSIReqOps;
43b443b6 19
97a06435
GH
20enum SCSIXferMode {
21 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
22 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
23 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
24};
25
a1f0cce2
HR
26typedef struct SCSISense {
27 uint8_t key;
28 uint8_t asc;
29 uint8_t ascq;
30} SCSISense;
31
b45ef674
PB
32#define SCSI_SENSE_BUF_SIZE 96
33
2599aece
PB
34struct SCSICommand {
35 uint8_t buf[SCSI_CMD_BUF_SIZE];
36 int len;
37 size_t xfer;
38 uint64_t lba;
39 enum SCSIXferMode mode;
40};
41
5c6c0e51 42struct SCSIRequest {
4c41d2ef
GH
43 SCSIBus *bus;
44 SCSIDevice *dev;
adcf2754 45 const SCSIReqOps *ops;
ad2d30f7 46 uint32_t refcount;
4c41d2ef 47 uint32_t tag;
89b08ae1 48 uint32_t lun;
ed3a34a3 49 uint32_t status;
01e95455 50 size_t resid;
2599aece 51 SCSICommand cmd;
4c41d2ef 52 BlockDriverAIOCB *aiocb;
3d5aba97
PB
53 QEMUSGList *sg;
54 bool dma_started;
b45ef674
PB
55 uint8_t sense[SCSI_SENSE_BUF_SIZE];
56 uint32_t sense_len;
e8637c90 57 bool enqueued;
e88c591d 58 bool io_canceled;
71544d30 59 bool retry;
c5bf71a9 60 void *hba_private;
9af99d98 61 QTAILQ_ENTRY(SCSIRequest) next;
5c6c0e51 62};
4c41d2ef 63
b9eea3e6
AL
64#define TYPE_SCSI_DEVICE "scsi-device"
65#define SCSI_DEVICE(obj) \
66 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
67#define SCSI_DEVICE_CLASS(klass) \
68 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
69#define SCSI_DEVICE_GET_CLASS(obj) \
70 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
71
72typedef struct SCSIDeviceClass {
73 DeviceClass parent_class;
74 int (*init)(SCSIDevice *dev);
75 void (*destroy)(SCSIDevice *s);
76 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
77 uint8_t *buf, void *hba_private);
78 void (*unit_attention_reported)(SCSIDevice *s);
79} SCSIDeviceClass;
80
43b443b6
GH
81struct SCSIDevice
82{
83 DeviceState qdev;
71544d30
PB
84 VMChangeStateEntry *vmsentry;
85 QEMUBH *bh;
43b443b6 86 uint32_t id;
428c149b 87 BlockConf conf;
6dc06f08 88 SCSISense unit_attention;
3653d8c4 89 bool sense_is_ua;
b45ef674
PB
90 uint8_t sense[SCSI_SENSE_BUF_SIZE];
91 uint32_t sense_len;
9af99d98 92 QTAILQ_HEAD(, SCSIRequest) requests;
0d3545e7 93 uint32_t channel;
87dcd1b2 94 uint32_t lun;
b07995e3 95 int blocksize;
91376656 96 int type;
7877903a 97 uint64_t max_lba;
43b443b6
GH
98};
99
63f740dd
PB
100extern const VMStateDescription vmstate_scsi_device;
101
102#define VMSTATE_SCSI_DEVICE(_field, _state) { \
103 .name = (stringify(_field)), \
104 .size = sizeof(SCSIDevice), \
105 .vmsd = &vmstate_scsi_device, \
106 .flags = VMS_STRUCT, \
107 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
108}
109
43b443b6
GH
110/* cdrom.c */
111int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
112int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
113
114/* scsi-bus.c */
8dbd4574
PB
115struct SCSIReqOps {
116 size_t size;
12010e7b
PB
117 void (*free_req)(SCSIRequest *req);
118 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
119 void (*read_data)(SCSIRequest *req);
120 void (*write_data)(SCSIRequest *req);
121 void (*cancel_io)(SCSIRequest *req);
122 uint8_t *(*get_buf)(SCSIRequest *req);
63f740dd
PB
123
124 void (*save_request)(QEMUFile *f, SCSIRequest *req);
125 void (*load_request)(QEMUFile *f, SCSIRequest *req);
8dbd4574
PB
126};
127
afd4030c 128struct SCSIBusInfo {
7e0380b9 129 int tcq;
0d3545e7 130 int max_channel, max_target, max_lun;
c6df7102 131 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
01e95455 132 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
94d3f98a 133 void (*cancel)(SCSIRequest *req);
3d5aba97 134 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
63f740dd
PB
135
136 void (*save_request)(QEMUFile *f, SCSIRequest *req);
137 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
8e86b93c 138 void (*free_request)(SCSIBus *bus, void *priv);
cfdc1bb0
PB
139};
140
0d936928
AL
141#define TYPE_SCSI_BUS "SCSI"
142#define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
143
43b443b6
GH
144struct SCSIBus {
145 BusState qbus;
146 int busnr;
147
6dc06f08 148 SCSISense unit_attention;
afd4030c 149 const SCSIBusInfo *info;
43b443b6
GH
150};
151
afd4030c 152void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
43b443b6
GH
153
154static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
155{
156 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
157}
158
2d1fd261 159SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
ce4e7e46 160 int unit, bool removable, int bootindex);
fa66b909 161int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
43b443b6 162
a1f0cce2
HR
163/*
164 * Predefined sense codes
165 */
166
167/* No sense data available */
168extern const struct SCSISense sense_code_NO_SENSE;
169/* LUN not ready, Manual intervention required */
170extern const struct SCSISense sense_code_LUN_NOT_READY;
171/* LUN not ready, Medium not present */
172extern const struct SCSISense sense_code_NO_MEDIUM;
68bb01f3
MA
173/* LUN not ready, medium removal prevented */
174extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
a1f0cce2
HR
175/* Hardware error, internal target failure */
176extern const struct SCSISense sense_code_TARGET_FAILURE;
177/* Illegal request, invalid command operation code */
178extern const struct SCSISense sense_code_INVALID_OPCODE;
179/* Illegal request, LBA out of range */
180extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
181/* Illegal request, Invalid field in CDB */
182extern const struct SCSISense sense_code_INVALID_FIELD;
380feaff
PB
183/* Illegal request, Invalid field in parameter list */
184extern const struct SCSISense sense_code_INVALID_PARAM;
185/* Illegal request, Parameter list length error */
186extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
a1f0cce2
HR
187/* Illegal request, LUN not supported */
188extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
a872a304
PB
189/* Illegal request, Saving parameters not supported */
190extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
191/* Illegal request, Incompatible format */
192extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
68bb01f3
MA
193/* Illegal request, medium removal prevented */
194extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
a1f0cce2
HR
195/* Command aborted, I/O process terminated */
196extern const struct SCSISense sense_code_IO_ERROR;
197/* Command aborted, I_T Nexus loss occurred */
198extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
199/* Command aborted, Logical Unit failure */
200extern const struct SCSISense sense_code_LUN_FAILURE;
8a9c16f6
PB
201/* LUN not ready, Medium not present */
202extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
a872a304
PB
203/* Unit attention, Power on, reset or bus device reset occurred */
204extern const struct SCSISense sense_code_RESET;
205/* Unit attention, Medium may have changed*/
206extern const struct SCSISense sense_code_MEDIUM_CHANGED;
207/* Unit attention, Reported LUNs data has changed */
208extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
209/* Unit attention, Device internal reset */
210extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
a1f0cce2
HR
211
212#define SENSE_CODE(x) sense_code_ ## x
213
a1f0cce2 214int scsi_sense_valid(SCSISense sense);
f3b338ef
PB
215int scsi_build_sense(uint8_t *in_buf, int in_len,
216 uint8_t *buf, int len, bool fixed);
a1f0cce2 217
adcf2754
PB
218SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
219 uint32_t tag, uint32_t lun, void *hba_private);
c5bf71a9 220SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
c39ce112
PB
221 uint8_t *buf, void *hba_private);
222int32_t scsi_req_enqueue(SCSIRequest *req);
89b08ae1 223void scsi_req_free(SCSIRequest *req);
ad2d30f7
PB
224SCSIRequest *scsi_req_ref(SCSIRequest *req);
225void scsi_req_unref(SCSIRequest *req);
37659e51 226
b45ef674 227void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
ec766865 228void scsi_req_print(SCSIRequest *req);
ad3376cc 229void scsi_req_continue(SCSIRequest *req);
ab9adc88 230void scsi_req_data(SCSIRequest *req, int len);
682a9b21 231void scsi_req_complete(SCSIRequest *req, int status);
0c34459b 232uint8_t *scsi_req_get_buf(SCSIRequest *req);
74382217 233int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
19d110ab 234void scsi_req_abort(SCSIRequest *req, int status);
94d3f98a 235void scsi_req_cancel(SCSIRequest *req);
71544d30 236void scsi_req_retry(SCSIRequest *req);
c7b48872 237void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
b45ef674 238int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
0d3545e7 239SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
89b08ae1 240
765d1525
PB
241/* scsi-generic.c. */
242extern const SCSIReqOps scsi_generic_req_ops;
243
43b443b6 244#endif