xref: /qemu/include/hw/scsi/scsi.h (revision 41af878b)
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 typedef struct SCSIBus SCSIBus;
14 typedef struct SCSIBusInfo SCSIBusInfo;
15 typedef struct SCSIDevice SCSIDevice;
16 typedef struct SCSIRequest SCSIRequest;
17 typedef struct SCSIReqOps SCSIReqOps;
18 
19 #define SCSI_SENSE_BUF_SIZE_OLD 96
20 #define SCSI_SENSE_BUF_SIZE 252
21 #define DEFAULT_IO_TIMEOUT 30
22 
23 struct SCSIRequest {
24     SCSIBus           *bus;
25     SCSIDevice        *dev;
26     const SCSIReqOps  *ops;
27     uint32_t          refcount;
28     uint32_t          tag;
29     uint32_t          lun;
30     uint32_t          status;
31     void              *hba_private;
32     size_t            resid;
33     SCSICommand       cmd;
34     NotifierList      cancel_notifiers;
35 
36     /* Note:
37      * - fields before sense are initialized by scsi_req_alloc;
38      * - sense[] is uninitialized;
39      * - fields after sense are memset to 0 by scsi_req_alloc.
40      * */
41 
42     uint8_t           sense[SCSI_SENSE_BUF_SIZE];
43     uint32_t          sense_len;
44     bool              enqueued;
45     bool              io_canceled;
46     bool              retry;
47     bool              dma_started;
48     BlockAIOCB        *aiocb;
49     QEMUSGList        *sg;
50     QTAILQ_ENTRY(SCSIRequest) next;
51 };
52 
53 #define TYPE_SCSI_DEVICE "scsi-device"
54 OBJECT_DECLARE_TYPE(SCSIDevice, SCSIDeviceClass, SCSI_DEVICE)
55 
56 struct SCSIDeviceClass {
57     DeviceClass parent_class;
58     void (*realize)(SCSIDevice *dev, Error **errp);
59     void (*unrealize)(SCSIDevice *dev);
60     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
61                      void *hba_private);
62     SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
63                               uint8_t *buf, void *hba_private);
64     void (*unit_attention_reported)(SCSIDevice *s);
65 };
66 
67 struct SCSIDevice
68 {
69     DeviceState qdev;
70     VMChangeStateEntry *vmsentry;
71     QEMUBH *bh;
72     uint32_t id;
73     BlockConf conf;
74     SCSISense unit_attention;
75     bool sense_is_ua;
76     uint8_t sense[SCSI_SENSE_BUF_SIZE];
77     uint32_t sense_len;
78     QTAILQ_HEAD(, SCSIRequest) requests;
79     uint32_t channel;
80     uint32_t lun;
81     int blocksize;
82     int type;
83     uint64_t max_lba;
84     uint64_t wwn;
85     uint64_t port_wwn;
86     int scsi_version;
87     int default_scsi_version;
88     uint32_t io_timeout;
89     bool needs_vpd_bl_emulation;
90     bool hba_supports_iothread;
91 };
92 
93 extern const VMStateDescription vmstate_scsi_device;
94 
95 #define VMSTATE_SCSI_DEVICE(_field, _state) {                        \
96     .name       = (stringify(_field)),                               \
97     .size       = sizeof(SCSIDevice),                                \
98     .vmsd       = &vmstate_scsi_device,                              \
99     .flags      = VMS_STRUCT,                                        \
100     .offset     = vmstate_offset_value(_state, _field, SCSIDevice),  \
101 }
102 
103 /* cdrom.c */
104 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
105 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
106 
107 /* scsi-bus.c */
108 struct SCSIReqOps {
109     size_t size;
110     void (*free_req)(SCSIRequest *req);
111     int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
112     void (*read_data)(SCSIRequest *req);
113     void (*write_data)(SCSIRequest *req);
114     uint8_t *(*get_buf)(SCSIRequest *req);
115 
116     void (*save_request)(QEMUFile *f, SCSIRequest *req);
117     void (*load_request)(QEMUFile *f, SCSIRequest *req);
118 };
119 
120 struct SCSIBusInfo {
121     int tcq;
122     int max_channel, max_target, max_lun;
123     int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
124                      void *hba_private);
125     void (*transfer_data)(SCSIRequest *req, uint32_t arg);
126     void (*complete)(SCSIRequest *req, size_t resid);
127     void (*cancel)(SCSIRequest *req);
128     void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
129     QEMUSGList *(*get_sg_list)(SCSIRequest *req);
130 
131     void (*save_request)(QEMUFile *f, SCSIRequest *req);
132     void *(*load_request)(QEMUFile *f, SCSIRequest *req);
133     void (*free_request)(SCSIBus *bus, void *priv);
134 };
135 
136 #define TYPE_SCSI_BUS "SCSI"
137 OBJECT_DECLARE_SIMPLE_TYPE(SCSIBus, SCSI_BUS)
138 
139 struct SCSIBus {
140     BusState qbus;
141     int busnr;
142 
143     SCSISense unit_attention;
144     const SCSIBusInfo *info;
145 };
146 
147 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
148                   const SCSIBusInfo *info, const char *bus_name);
149 
150 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
151 {
152     return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
153 }
154 
155 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
156                                       int unit, bool removable, int bootindex,
157                                       bool share_rw,
158                                       BlockdevOnError rerror,
159                                       BlockdevOnError werror,
160                                       const char *serial, Error **errp);
161 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
162 void scsi_legacy_handle_cmdline(void);
163 
164 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
165                             uint32_t tag, uint32_t lun, void *hba_private);
166 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
167                           uint8_t *buf, void *hba_private);
168 int32_t scsi_req_enqueue(SCSIRequest *req);
169 SCSIRequest *scsi_req_ref(SCSIRequest *req);
170 void scsi_req_unref(SCSIRequest *req);
171 
172 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
173                        void *hba_private);
174 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
175 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
176 void scsi_req_print(SCSIRequest *req);
177 void scsi_req_continue(SCSIRequest *req);
178 void scsi_req_data(SCSIRequest *req, int len);
179 void scsi_req_complete(SCSIRequest *req, int status);
180 uint8_t *scsi_req_get_buf(SCSIRequest *req);
181 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
182 void scsi_req_cancel_complete(SCSIRequest *req);
183 void scsi_req_cancel(SCSIRequest *req);
184 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
185 void scsi_req_retry(SCSIRequest *req);
186 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
187 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
188 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
189 void scsi_device_unit_attention_reported(SCSIDevice *dev);
190 void scsi_generic_read_device_inquiry(SCSIDevice *dev);
191 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
192 int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
193                         uint8_t *buf, uint8_t buf_size, uint32_t timeout);
194 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
195 SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);
196 
197 /* scsi-generic.c. */
198 extern const SCSIReqOps scsi_generic_req_ops;
199 
200 #endif
201