scsi: move sense handling to generic code
[qemu/agraf.git] / hw / scsi.h
blobc1cb987cf6ce981f1f7972670b53db7a1e008533
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;
18 enum SCSIXferMode {
19 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
20 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
21 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
24 typedef struct SCSISense {
25 uint8_t key;
26 uint8_t asc;
27 uint8_t ascq;
28 } SCSISense;
30 #define SCSI_SENSE_BUF_SIZE 96
32 struct SCSIRequest {
33 SCSIBus *bus;
34 SCSIDevice *dev;
35 uint32_t refcount;
36 uint32_t tag;
37 uint32_t lun;
38 uint32_t status;
39 struct {
40 uint8_t buf[SCSI_CMD_BUF_SIZE];
41 int len;
42 size_t xfer;
43 uint64_t lba;
44 enum SCSIXferMode mode;
45 } cmd;
46 BlockDriverAIOCB *aiocb;
47 uint8_t sense[SCSI_SENSE_BUF_SIZE];
48 uint32_t sense_len;
49 bool enqueued;
50 void *hba_private;
51 QTAILQ_ENTRY(SCSIRequest) next;
54 struct SCSIDevice
56 DeviceState qdev;
57 uint32_t id;
58 BlockConf conf;
59 SCSIDeviceInfo *info;
60 uint8_t sense[SCSI_SENSE_BUF_SIZE];
61 uint32_t sense_len;
62 QTAILQ_HEAD(, SCSIRequest) requests;
63 int blocksize;
64 int type;
67 /* cdrom.c */
68 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
69 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
71 /* scsi-bus.c */
72 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
73 struct SCSIDeviceInfo {
74 DeviceInfo qdev;
75 scsi_qdev_initfn init;
76 void (*destroy)(SCSIDevice *s);
77 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
78 void *hba_private);
79 void (*free_req)(SCSIRequest *req);
80 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
81 void (*read_data)(SCSIRequest *req);
82 void (*write_data)(SCSIRequest *req);
83 void (*cancel_io)(SCSIRequest *req);
84 uint8_t *(*get_buf)(SCSIRequest *req);
87 struct SCSIBusOps {
88 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
89 void (*complete)(SCSIRequest *req, uint32_t arg);
90 void (*cancel)(SCSIRequest *req);
93 struct SCSIBus {
94 BusState qbus;
95 int busnr;
97 int tcq, ndev;
98 const SCSIBusOps *ops;
100 SCSIDevice *devs[MAX_SCSI_DEVS];
103 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
104 const SCSIBusOps *ops);
105 void scsi_qdev_register(SCSIDeviceInfo *info);
107 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
109 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
112 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
113 int unit, bool removable);
114 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
117 * Predefined sense codes
120 /* No sense data available */
121 extern const struct SCSISense sense_code_NO_SENSE;
122 /* LUN not ready, Manual intervention required */
123 extern const struct SCSISense sense_code_LUN_NOT_READY;
124 /* LUN not ready, Medium not present */
125 extern const struct SCSISense sense_code_NO_MEDIUM;
126 /* Hardware error, internal target failure */
127 extern const struct SCSISense sense_code_TARGET_FAILURE;
128 /* Illegal request, invalid command operation code */
129 extern const struct SCSISense sense_code_INVALID_OPCODE;
130 /* Illegal request, LBA out of range */
131 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
132 /* Illegal request, Invalid field in CDB */
133 extern const struct SCSISense sense_code_INVALID_FIELD;
134 /* Illegal request, LUN not supported */
135 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
136 /* Command aborted, I/O process terminated */
137 extern const struct SCSISense sense_code_IO_ERROR;
138 /* Command aborted, I_T Nexus loss occurred */
139 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
140 /* Command aborted, Logical Unit failure */
141 extern const struct SCSISense sense_code_LUN_FAILURE;
143 #define SENSE_CODE(x) sense_code_ ## x
145 int scsi_sense_valid(SCSISense sense);
147 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag,
148 uint32_t lun, void *hba_private);
149 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
150 void *hba_private);
151 int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
152 void scsi_req_free(SCSIRequest *req);
153 SCSIRequest *scsi_req_ref(SCSIRequest *req);
154 void scsi_req_unref(SCSIRequest *req);
156 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
157 int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
158 void scsi_req_print(SCSIRequest *req);
159 void scsi_req_continue(SCSIRequest *req);
160 void scsi_req_data(SCSIRequest *req, int len);
161 void scsi_req_complete(SCSIRequest *req, int status);
162 uint8_t *scsi_req_get_buf(SCSIRequest *req);
163 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
164 void scsi_req_abort(SCSIRequest *req, int status);
165 void scsi_req_cancel(SCSIRequest *req);
166 void scsi_device_purge_requests(SCSIDevice *sdev);
167 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
169 #endif