]> git.proxmox.com Git - qemu.git/blob - hw/scsi.h
Merge remote-tracking branch 'kiszka/queues/slirp' into staging
[qemu.git] / hw / scsi.h
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
3
4 #include "qdev.h"
5 #include "block.h"
6
7 #define MAX_SCSI_DEVS 255
8
9 #define SCSI_CMD_BUF_SIZE 16
10
11 typedef struct SCSIBus SCSIBus;
12 typedef struct SCSIBusOps SCSIBusOps;
13 typedef struct SCSICommand SCSICommand;
14 typedef struct SCSIDevice SCSIDevice;
15 typedef struct SCSIDeviceInfo SCSIDeviceInfo;
16 typedef struct SCSIRequest SCSIRequest;
17 typedef struct SCSIReqOps SCSIReqOps;
18
19 enum SCSIXferMode {
20 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
21 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
22 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
23 };
24
25 typedef struct SCSISense {
26 uint8_t key;
27 uint8_t asc;
28 uint8_t ascq;
29 } SCSISense;
30
31 #define SCSI_SENSE_BUF_SIZE 96
32
33 struct SCSICommand {
34 uint8_t buf[SCSI_CMD_BUF_SIZE];
35 int len;
36 size_t xfer;
37 uint64_t lba;
38 enum SCSIXferMode mode;
39 };
40
41 struct SCSIRequest {
42 SCSIBus *bus;
43 SCSIDevice *dev;
44 SCSIReqOps *ops;
45 uint32_t refcount;
46 uint32_t tag;
47 uint32_t lun;
48 uint32_t status;
49 SCSICommand cmd;
50 BlockDriverAIOCB *aiocb;
51 uint8_t sense[SCSI_SENSE_BUF_SIZE];
52 uint32_t sense_len;
53 bool enqueued;
54 void *hba_private;
55 QTAILQ_ENTRY(SCSIRequest) next;
56 };
57
58 struct SCSIDevice
59 {
60 DeviceState qdev;
61 uint32_t id;
62 BlockConf conf;
63 SCSIDeviceInfo *info;
64 SCSISense unit_attention;
65 uint8_t sense[SCSI_SENSE_BUF_SIZE];
66 uint32_t sense_len;
67 QTAILQ_HEAD(, SCSIRequest) requests;
68 uint32_t lun;
69 int blocksize;
70 int type;
71 };
72
73 /* cdrom.c */
74 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
75 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
76
77 /* scsi-bus.c */
78 struct SCSIReqOps {
79 size_t size;
80 void (*free_req)(SCSIRequest *req);
81 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
82 void (*read_data)(SCSIRequest *req);
83 void (*write_data)(SCSIRequest *req);
84 void (*cancel_io)(SCSIRequest *req);
85 uint8_t *(*get_buf)(SCSIRequest *req);
86 };
87
88 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
89 struct SCSIDeviceInfo {
90 DeviceInfo qdev;
91 scsi_qdev_initfn init;
92 void (*destroy)(SCSIDevice *s);
93 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
94 void *hba_private);
95 SCSIReqOps reqops;
96 };
97
98 struct SCSIBusOps {
99 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
100 void (*complete)(SCSIRequest *req, uint32_t arg);
101 void (*cancel)(SCSIRequest *req);
102 };
103
104 struct SCSIBus {
105 BusState qbus;
106 int busnr;
107
108 SCSISense unit_attention;
109 int tcq, ndev;
110 const SCSIBusOps *ops;
111
112 SCSIDevice *devs[MAX_SCSI_DEVS];
113 };
114
115 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
116 const SCSIBusOps *ops);
117 void scsi_qdev_register(SCSIDeviceInfo *info);
118
119 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
120 {
121 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
122 }
123
124 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
125 int unit, bool removable);
126 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
127
128 /*
129 * Predefined sense codes
130 */
131
132 /* No sense data available */
133 extern const struct SCSISense sense_code_NO_SENSE;
134 /* LUN not ready, Manual intervention required */
135 extern const struct SCSISense sense_code_LUN_NOT_READY;
136 /* LUN not ready, Medium not present */
137 extern const struct SCSISense sense_code_NO_MEDIUM;
138 /* LUN not ready, medium removal prevented */
139 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
140 /* Hardware error, internal target failure */
141 extern const struct SCSISense sense_code_TARGET_FAILURE;
142 /* Illegal request, invalid command operation code */
143 extern const struct SCSISense sense_code_INVALID_OPCODE;
144 /* Illegal request, LBA out of range */
145 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
146 /* Illegal request, Invalid field in CDB */
147 extern const struct SCSISense sense_code_INVALID_FIELD;
148 /* Illegal request, LUN not supported */
149 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
150 /* Illegal request, Saving parameters not supported */
151 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
152 /* Illegal request, Incompatible format */
153 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
154 /* Illegal request, medium removal prevented */
155 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
156 /* Command aborted, I/O process terminated */
157 extern const struct SCSISense sense_code_IO_ERROR;
158 /* Command aborted, I_T Nexus loss occurred */
159 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
160 /* Command aborted, Logical Unit failure */
161 extern const struct SCSISense sense_code_LUN_FAILURE;
162 /* Unit attention, Power on, reset or bus device reset occurred */
163 extern const struct SCSISense sense_code_RESET;
164 /* Unit attention, Medium may have changed*/
165 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
166 /* Unit attention, Reported LUNs data has changed */
167 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
168 /* Unit attention, Device internal reset */
169 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
170
171 #define SENSE_CODE(x) sense_code_ ## x
172
173 int scsi_sense_valid(SCSISense sense);
174
175 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
176 uint32_t lun, void *hba_private);
177 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
178 uint8_t *buf, void *hba_private);
179 int32_t scsi_req_enqueue(SCSIRequest *req);
180 void scsi_req_free(SCSIRequest *req);
181 SCSIRequest *scsi_req_ref(SCSIRequest *req);
182 void scsi_req_unref(SCSIRequest *req);
183
184 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
185 void scsi_req_print(SCSIRequest *req);
186 void scsi_req_continue(SCSIRequest *req);
187 void scsi_req_data(SCSIRequest *req, int len);
188 void scsi_req_complete(SCSIRequest *req, int status);
189 uint8_t *scsi_req_get_buf(SCSIRequest *req);
190 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
191 void scsi_req_abort(SCSIRequest *req, int status);
192 void scsi_req_cancel(SCSIRequest *req);
193 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
194 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
195
196 #endif