]> git.proxmox.com Git - qemu.git/blob - hw/scsi.h
scsi: move max_lba to SCSIDevice
[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 SCSIBusInfo SCSIBusInfo;
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 bool sense_is_ua;
66 uint8_t sense[SCSI_SENSE_BUF_SIZE];
67 uint32_t sense_len;
68 QTAILQ_HEAD(, SCSIRequest) requests;
69 uint32_t channel;
70 uint32_t lun;
71 int blocksize;
72 int type;
73 uint64_t max_lba;
74 };
75
76 /* cdrom.c */
77 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
78 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
79
80 /* scsi-bus.c */
81 struct SCSIReqOps {
82 size_t size;
83 void (*free_req)(SCSIRequest *req);
84 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
85 void (*read_data)(SCSIRequest *req);
86 void (*write_data)(SCSIRequest *req);
87 void (*cancel_io)(SCSIRequest *req);
88 uint8_t *(*get_buf)(SCSIRequest *req);
89 };
90
91 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
92 struct SCSIDeviceInfo {
93 DeviceInfo qdev;
94 scsi_qdev_initfn init;
95 void (*destroy)(SCSIDevice *s);
96 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
97 void *hba_private);
98 void (*unit_attention_reported)(SCSIDevice *s);
99 SCSIReqOps reqops;
100 };
101
102 struct SCSIBusInfo {
103 int tcq;
104 int max_channel, max_target, max_lun;
105 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
106 void (*complete)(SCSIRequest *req, uint32_t arg);
107 void (*cancel)(SCSIRequest *req);
108 };
109
110 struct SCSIBus {
111 BusState qbus;
112 int busnr;
113
114 SCSISense unit_attention;
115 const SCSIBusInfo *info;
116 };
117
118 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
119 void scsi_qdev_register(SCSIDeviceInfo *info);
120
121 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
122 {
123 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
124 }
125
126 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
127 int unit, bool removable);
128 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
129
130 /*
131 * Predefined sense codes
132 */
133
134 /* No sense data available */
135 extern const struct SCSISense sense_code_NO_SENSE;
136 /* LUN not ready, Manual intervention required */
137 extern const struct SCSISense sense_code_LUN_NOT_READY;
138 /* LUN not ready, Medium not present */
139 extern const struct SCSISense sense_code_NO_MEDIUM;
140 /* LUN not ready, medium removal prevented */
141 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
142 /* Hardware error, internal target failure */
143 extern const struct SCSISense sense_code_TARGET_FAILURE;
144 /* Illegal request, invalid command operation code */
145 extern const struct SCSISense sense_code_INVALID_OPCODE;
146 /* Illegal request, LBA out of range */
147 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
148 /* Illegal request, Invalid field in CDB */
149 extern const struct SCSISense sense_code_INVALID_FIELD;
150 /* Illegal request, LUN not supported */
151 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
152 /* Illegal request, Saving parameters not supported */
153 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
154 /* Illegal request, Incompatible format */
155 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
156 /* Illegal request, medium removal prevented */
157 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
158 /* Command aborted, I/O process terminated */
159 extern const struct SCSISense sense_code_IO_ERROR;
160 /* Command aborted, I_T Nexus loss occurred */
161 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
162 /* Command aborted, Logical Unit failure */
163 extern const struct SCSISense sense_code_LUN_FAILURE;
164 /* LUN not ready, Medium not present */
165 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
166 /* Unit attention, Power on, reset or bus device reset occurred */
167 extern const struct SCSISense sense_code_RESET;
168 /* Unit attention, Medium may have changed*/
169 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
170 /* Unit attention, Reported LUNs data has changed */
171 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
172 /* Unit attention, Device internal reset */
173 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
174
175 #define SENSE_CODE(x) sense_code_ ## x
176
177 int scsi_sense_valid(SCSISense sense);
178
179 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
180 uint32_t lun, void *hba_private);
181 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
182 uint8_t *buf, void *hba_private);
183 int32_t scsi_req_enqueue(SCSIRequest *req);
184 void scsi_req_free(SCSIRequest *req);
185 SCSIRequest *scsi_req_ref(SCSIRequest *req);
186 void scsi_req_unref(SCSIRequest *req);
187
188 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
189 void scsi_req_print(SCSIRequest *req);
190 void scsi_req_continue(SCSIRequest *req);
191 void scsi_req_data(SCSIRequest *req, int len);
192 void scsi_req_complete(SCSIRequest *req, int status);
193 uint8_t *scsi_req_get_buf(SCSIRequest *req);
194 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
195 void scsi_req_abort(SCSIRequest *req, int status);
196 void scsi_req_cancel(SCSIRequest *req);
197 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
198 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
199 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
200
201 #endif