scsi: introduce SCSIReqOps
[qemu/kevin.git] / hw / scsi.h
blobee76c64bc5518922d8d88a3c2f544076eccecceb
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
4 #include "qdev.h"
5 #include "block.h"
6 #include "block_int.h"
8 #define MAX_SCSI_DEVS 255
10 #define SCSI_CMD_BUF_SIZE 16
12 typedef struct SCSIBus SCSIBus;
13 typedef struct SCSIBusOps SCSIBusOps;
14 typedef struct SCSIDevice SCSIDevice;
15 typedef struct SCSIDeviceInfo SCSIDeviceInfo;
16 typedef struct SCSIRequest SCSIRequest;
17 typedef struct SCSIReqOps SCSIReqOps;
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, ... */
25 typedef struct SCSISense {
26 uint8_t key;
27 uint8_t asc;
28 uint8_t ascq;
29 } SCSISense;
31 #define SCSI_SENSE_BUF_SIZE 96
33 struct SCSIRequest {
34 SCSIBus *bus;
35 SCSIDevice *dev;
36 SCSIReqOps *ops;
37 uint32_t refcount;
38 uint32_t tag;
39 uint32_t lun;
40 uint32_t status;
41 struct {
42 uint8_t buf[SCSI_CMD_BUF_SIZE];
43 int len;
44 size_t xfer;
45 uint64_t lba;
46 enum SCSIXferMode mode;
47 } cmd;
48 BlockDriverAIOCB *aiocb;
49 uint8_t sense[SCSI_SENSE_BUF_SIZE];
50 uint32_t sense_len;
51 bool enqueued;
52 void *hba_private;
53 QTAILQ_ENTRY(SCSIRequest) next;
56 struct SCSIDevice
58 DeviceState qdev;
59 uint32_t id;
60 BlockConf conf;
61 SCSIDeviceInfo *info;
62 uint8_t sense[SCSI_SENSE_BUF_SIZE];
63 uint32_t sense_len;
64 QTAILQ_HEAD(, SCSIRequest) requests;
65 int blocksize;
66 int type;
69 /* cdrom.c */
70 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
71 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
73 /* scsi-bus.c */
74 struct SCSIReqOps {
75 size_t size;
78 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
79 struct SCSIDeviceInfo {
80 DeviceInfo qdev;
81 scsi_qdev_initfn init;
82 void (*destroy)(SCSIDevice *s);
83 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
84 void *hba_private);
85 void (*free_req)(SCSIRequest *req);
86 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
87 void (*read_data)(SCSIRequest *req);
88 void (*write_data)(SCSIRequest *req);
89 void (*cancel_io)(SCSIRequest *req);
90 uint8_t *(*get_buf)(SCSIRequest *req);
93 struct SCSIBusOps {
94 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
95 void (*complete)(SCSIRequest *req, uint32_t arg);
96 void (*cancel)(SCSIRequest *req);
99 struct SCSIBus {
100 BusState qbus;
101 int busnr;
103 int tcq, ndev;
104 const SCSIBusOps *ops;
106 SCSIDevice *devs[MAX_SCSI_DEVS];
109 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
110 const SCSIBusOps *ops);
111 void scsi_qdev_register(SCSIDeviceInfo *info);
113 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
115 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
118 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
119 int unit, bool removable);
120 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
123 * Predefined sense codes
126 /* No sense data available */
127 extern const struct SCSISense sense_code_NO_SENSE;
128 /* LUN not ready, Manual intervention required */
129 extern const struct SCSISense sense_code_LUN_NOT_READY;
130 /* LUN not ready, Medium not present */
131 extern const struct SCSISense sense_code_NO_MEDIUM;
132 /* Hardware error, internal target failure */
133 extern const struct SCSISense sense_code_TARGET_FAILURE;
134 /* Illegal request, invalid command operation code */
135 extern const struct SCSISense sense_code_INVALID_OPCODE;
136 /* Illegal request, LBA out of range */
137 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
138 /* Illegal request, Invalid field in CDB */
139 extern const struct SCSISense sense_code_INVALID_FIELD;
140 /* Illegal request, LUN not supported */
141 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
142 /* Command aborted, I/O process terminated */
143 extern const struct SCSISense sense_code_IO_ERROR;
144 /* Command aborted, I_T Nexus loss occurred */
145 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
146 /* Command aborted, Logical Unit failure */
147 extern const struct SCSISense sense_code_LUN_FAILURE;
149 #define SENSE_CODE(x) sense_code_ ## x
151 int scsi_sense_valid(SCSISense sense);
153 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
154 uint32_t lun, void *hba_private);
155 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
156 void *hba_private);
157 int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
158 void scsi_req_free(SCSIRequest *req);
159 SCSIRequest *scsi_req_ref(SCSIRequest *req);
160 void scsi_req_unref(SCSIRequest *req);
162 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
163 int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
164 void scsi_req_print(SCSIRequest *req);
165 void scsi_req_continue(SCSIRequest *req);
166 void scsi_req_data(SCSIRequest *req, int len);
167 void scsi_req_complete(SCSIRequest *req, int status);
168 uint8_t *scsi_req_get_buf(SCSIRequest *req);
169 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
170 void scsi_req_abort(SCSIRequest *req, int status);
171 void scsi_req_cancel(SCSIRequest *req);
172 void scsi_device_purge_requests(SCSIDevice *sdev);
173 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
175 #endif