xref: /openbmc/qemu/include/hw/scsi/scsi.h (revision bc831f37398b51dfe65d99a67bcff9352f84a9d2)
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
3 
4 #include "block/aio.h"
5 #include "hw/block/block.h"
6 #include "hw/qdev-core.h"
7 #include "scsi/utils.h"
8 #include "qemu/notify.h"
9 #include "qom/object.h"
10 
11 #define MAX_SCSI_DEVS 255
12 
13 #define TYPE_SCSI_BUS "SCSI"
14 OBJECT_DECLARE_SIMPLE_TYPE(SCSIBus, SCSI_BUS)
15 
16 typedef struct SCSIBusInfo SCSIBusInfo;
17 typedef struct SCSIDevice SCSIDevice;
18 typedef struct SCSIRequest SCSIRequest;
19 typedef struct SCSIReqOps SCSIReqOps;
20 
21 #define SCSI_SENSE_BUF_SIZE_OLD 96
22 #define SCSI_SENSE_BUF_SIZE 252
23 #define DEFAULT_IO_TIMEOUT 30
24 
25 struct SCSIRequest {
26     SCSIBus           *bus;
27     SCSIDevice        *dev;
28     const SCSIReqOps  *ops;
29     AioContext        *ctx;
30     uint32_t          refcount;
31     uint32_t          tag;
32     uint32_t          lun;
33     int16_t           status;
34     int16_t           host_status;
35     void              *hba_private;
36     uint64_t          residual;
37     SCSICommand       cmd;
38     NotifierList      cancel_notifiers;
39 
40     /* Note:
41      * - fields before sense are initialized by scsi_req_alloc;
42      * - sense[] is uninitialized;
43      * - fields after sense are memset to 0 by scsi_req_alloc.
44      * */
45 
46     uint8_t           sense[SCSI_SENSE_BUF_SIZE];
47     uint32_t          sense_len;
48     bool              enqueued;
49     bool              io_canceled;
50     bool              retry;
51     bool              dma_started;
52     BlockAIOCB        *aiocb;
53     QEMUSGList        *sg;
54 
55     /* Protected by SCSIDevice->requests_lock */
56     QTAILQ_ENTRY(SCSIRequest) next;
57 };
58 
59 #define TYPE_SCSI_DEVICE "scsi-device"
60 OBJECT_DECLARE_TYPE(SCSIDevice, SCSIDeviceClass, SCSI_DEVICE)
61 
62 struct SCSIDeviceClass {
63     DeviceClass parent_class;
64     void (*realize)(SCSIDevice *dev, Error **errp);
65     void (*unrealize)(SCSIDevice *dev);
66     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
67                      size_t buf_len, void *hba_private);
68     SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
69                               uint8_t *buf, void *hba_private);
70     void (*unit_attention_reported)(SCSIDevice *s);
71 };
72 
73 struct SCSIDevice
74 {
75     DeviceState qdev;
76     VMChangeStateEntry *vmsentry;
77     uint32_t id;
78     BlockConf conf;
79     SCSISense unit_attention;
80     bool sense_is_ua;
81     uint8_t sense[SCSI_SENSE_BUF_SIZE];
82     uint32_t sense_len;
83 
84     QemuMutex requests_lock; /* protects the requests list */
85     QTAILQ_HEAD(, SCSIRequest) requests;
86 
87     uint32_t channel;
88     uint32_t lun;
89     int blocksize;
90     int type;
91     uint64_t max_lba;
92     uint64_t wwn;
93     uint64_t port_wwn;
94     int scsi_version;
95     int default_scsi_version;
96     uint32_t io_timeout;
97     bool needs_vpd_bl_emulation;
98     bool hba_supports_iothread;
99 };
100 
101 extern const VMStateDescription vmstate_scsi_device;
102 
103 #define VMSTATE_SCSI_DEVICE(_field, _state) {                        \
104     .name       = (stringify(_field)),                               \
105     .size       = sizeof(SCSIDevice),                                \
106     .vmsd       = &vmstate_scsi_device,                              \
107     .flags      = VMS_STRUCT,                                        \
108     .offset     = vmstate_offset_value(_state, _field, SCSIDevice),  \
109 }
110 
111 /* cdrom.c */
112 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
113 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
114 
115 /* scsi-bus.c */
116 struct SCSIReqOps {
117     size_t size;
118     void (*init_req)(SCSIRequest *req);
119     void (*free_req)(SCSIRequest *req);
120     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
121     void (*read_data)(SCSIRequest *req);
122     void (*write_data)(SCSIRequest *req);
123     uint8_t *(*get_buf)(SCSIRequest *req);
124 
125     void (*save_request)(QEMUFile *f, SCSIRequest *req);
126     void (*load_request)(QEMUFile *f, SCSIRequest *req);
127 };
128 
129 struct SCSIBusInfo {
130     int tcq;
131     int max_channel, max_target, max_lun;
132     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
133                      size_t buf_len, void *hba_private);
134     void (*transfer_data)(SCSIRequest *req, uint32_t arg);
135     void (*fail)(SCSIRequest *req);
136     void (*complete)(SCSIRequest *req, size_t residual);
137     void (*cancel)(SCSIRequest *req);
138     void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
139     QEMUSGList *(*get_sg_list)(SCSIRequest *req);
140 
141     void (*save_request)(QEMUFile *f, SCSIRequest *req);
142     void *(*load_request)(QEMUFile *f, SCSIRequest *req);
143     void (*free_request)(SCSIBus *bus, void *priv);
144 
145     /*
146      * Temporarily stop submitting new requests between drained_begin() and
147      * drained_end(). Called from the main loop thread with the BQL held.
148      *
149      * Implement these callbacks if request processing is triggered by a file
150      * descriptor like an EventNotifier. Otherwise set them to NULL.
151      */
152     void (*drained_begin)(SCSIBus *bus);
153     void (*drained_end)(SCSIBus *bus);
154 };
155 
156 struct SCSIBus {
157     BusState qbus;
158     int busnr;
159 
160     SCSISense unit_attention;
161     const SCSIBusInfo *info;
162 
163     int drain_count; /* protected by BQL */
164 };
165 
166 /**
167  * scsi_bus_init_named: Initialize a SCSI bus with the specified name
168  * @bus: SCSIBus object to initialize
169  * @bus_size: size of @bus object
170  * @host: Device which owns the bus (generally the SCSI controller)
171  * @info: structure defining callbacks etc for the controller
172  * @bus_name: Name to use for this bus
173  *
174  * This in-place initializes @bus as a new SCSI bus with a name
175  * provided by the caller. It is the caller's responsibility to make
176  * sure that name does not clash with the name of any other bus in the
177  * system. Unless you need the new bus to have a specific name, you
178  * should use scsi_bus_init() instead.
179  */
180 void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
181                          const SCSIBusInfo *info, const char *bus_name);
182 
183 /**
184  * scsi_bus_init: Initialize a SCSI bus
185  *
186  * This in-place-initializes @bus as a new SCSI bus and gives it
187  * an automatically generated unique name.
188  */
189 static inline void scsi_bus_init(SCSIBus *bus, size_t bus_size,
190                                  DeviceState *host, const SCSIBusInfo *info)
191 {
192     scsi_bus_init_named(bus, bus_size, host, info, NULL);
193 }
194 
195 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
196 {
197     return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
198 }
199 
200 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
201                                       int unit, bool removable, BlockConf *conf,
202                                       const char *serial, Error **errp);
203 void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense);
204 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
205 
206 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
207                             uint32_t tag, uint32_t lun, void *hba_private);
208 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
209                           uint8_t *buf, size_t buf_len, void *hba_private);
210 int32_t scsi_req_enqueue(SCSIRequest *req);
211 SCSIRequest *scsi_req_ref(SCSIRequest *req);
212 void scsi_req_unref(SCSIRequest *req);
213 
214 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
215                        size_t buf_len, void *hba_private);
216 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
217                        size_t buf_len);
218 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
219 void scsi_req_print(SCSIRequest *req);
220 void scsi_req_continue(SCSIRequest *req);
221 void scsi_req_data(SCSIRequest *req, int len);
222 void scsi_req_complete(SCSIRequest *req, int status);
223 void scsi_req_complete_failed(SCSIRequest *req, int host_status);
224 uint8_t *scsi_req_get_buf(SCSIRequest *req);
225 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
226 void scsi_req_cancel_complete(SCSIRequest *req);
227 void scsi_req_cancel(SCSIRequest *req);
228 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
229 void scsi_req_retry(SCSIRequest *req);
230 void scsi_device_drained_begin(SCSIDevice *sdev);
231 void scsi_device_drained_end(SCSIDevice *sdev);
232 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
233 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
234 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
235 void scsi_device_unit_attention_reported(SCSIDevice *dev);
236 void scsi_generic_read_device_inquiry(SCSIDevice *dev);
237 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
238 int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
239                         uint8_t *buf, uint8_t buf_size, uint32_t timeout);
240 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
241 SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);
242 
243 /* scsi-generic.c. */
244 extern const SCSIReqOps scsi_generic_req_ops;
245 
246 /* scsi-disk.c */
247 #define SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR             0
248 #define SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD             1
249 #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE    2
250 #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED                3
251 
252 #endif
253