]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/scsi/scsi.h
Merge remote-tracking branch 'remotes/hdeller/tags/hppa-updates-pull-request' into...
[mirror_qemu.git] / include / hw / scsi / scsi.h
CommitLineData
1e37607b
GH
1#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
43b443b6 3
db725815 4#include "block/aio.h"
0d09e41a 5#include "hw/block/block.h"
a27bd6c7 6#include "hw/qdev-core.h"
e5b5728c 7#include "scsi/utils.h"
8e0a9320 8#include "qemu/notify.h"
db1015e9 9#include "qom/object.h"
43b443b6 10
27d6bf40
MA
11#define MAX_SCSI_DEVS 255
12
43b443b6 13typedef struct SCSIBus SCSIBus;
afd4030c 14typedef struct SCSIBusInfo SCSIBusInfo;
43b443b6 15typedef struct SCSIDevice SCSIDevice;
5c6c0e51 16typedef struct SCSIRequest SCSIRequest;
8dbd4574 17typedef struct SCSIReqOps SCSIReqOps;
43b443b6 18
2e323f03 19#define SCSI_SENSE_BUF_SIZE_OLD 96
c5f52875 20#define SCSI_SENSE_BUF_SIZE 252
c9b6609b 21#define DEFAULT_IO_TIMEOUT 30
b45ef674 22
5c6c0e51 23struct SCSIRequest {
4c41d2ef
GH
24 SCSIBus *bus;
25 SCSIDevice *dev;
adcf2754 26 const SCSIReqOps *ops;
ad2d30f7 27 uint32_t refcount;
4c41d2ef 28 uint32_t tag;
89b08ae1 29 uint32_t lun;
f3126d65
HR
30 int16_t status;
31 int16_t host_status;
61e68b3f 32 void *hba_private;
f02b664a 33 uint64_t residual;
2599aece 34 SCSICommand cmd;
8e0a9320 35 NotifierList cancel_notifiers;
61e68b3f
FZ
36
37 /* Note:
38 * - fields before sense are initialized by scsi_req_alloc;
39 * - sense[] is uninitialized;
40 * - fields after sense are memset to 0 by scsi_req_alloc.
41 * */
42
43 uint8_t sense[SCSI_SENSE_BUF_SIZE];
44 uint32_t sense_len;
45 bool enqueued;
46 bool io_canceled;
47 bool retry;
48 bool dma_started;
7c84b1b8 49 BlockAIOCB *aiocb;
3d5aba97 50 QEMUSGList *sg;
9af99d98 51 QTAILQ_ENTRY(SCSIRequest) next;
5c6c0e51 52};
4c41d2ef 53
b9eea3e6 54#define TYPE_SCSI_DEVICE "scsi-device"
a489d195 55OBJECT_DECLARE_TYPE(SCSIDevice, SCSIDeviceClass, SCSI_DEVICE)
b9eea3e6 56
db1015e9 57struct SCSIDeviceClass {
b9eea3e6 58 DeviceClass parent_class;
a818a4b6 59 void (*realize)(SCSIDevice *dev, Error **errp);
b69c3c21 60 void (*unrealize)(SCSIDevice *dev);
ff34c32c
PB
61 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
62 void *hba_private);
b9eea3e6
AL
63 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
64 uint8_t *buf, void *hba_private);
65 void (*unit_attention_reported)(SCSIDevice *s);
db1015e9 66};
b9eea3e6 67
43b443b6
GH
68struct SCSIDevice
69{
70 DeviceState qdev;
71544d30
PB
71 VMChangeStateEntry *vmsentry;
72 QEMUBH *bh;
43b443b6 73 uint32_t id;
428c149b 74 BlockConf conf;
6dc06f08 75 SCSISense unit_attention;
3653d8c4 76 bool sense_is_ua;
b45ef674
PB
77 uint8_t sense[SCSI_SENSE_BUF_SIZE];
78 uint32_t sense_len;
9af99d98 79 QTAILQ_HEAD(, SCSIRequest) requests;
0d3545e7 80 uint32_t channel;
87dcd1b2 81 uint32_t lun;
b07995e3 82 int blocksize;
91376656 83 int type;
7877903a 84 uint64_t max_lba;
2ecab408
PB
85 uint64_t wwn;
86 uint64_t port_wwn;
2343be0d
PB
87 int scsi_version;
88 int default_scsi_version;
c9b6609b 89 uint32_t io_timeout;
a71c775b 90 bool needs_vpd_bl_emulation;
4f71fb43 91 bool hba_supports_iothread;
43b443b6
GH
92};
93
63f740dd
PB
94extern const VMStateDescription vmstate_scsi_device;
95
96#define VMSTATE_SCSI_DEVICE(_field, _state) { \
97 .name = (stringify(_field)), \
98 .size = sizeof(SCSIDevice), \
99 .vmsd = &vmstate_scsi_device, \
100 .flags = VMS_STRUCT, \
101 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
102}
103
43b443b6
GH
104/* cdrom.c */
105int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
106int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
107
108/* scsi-bus.c */
8dbd4574
PB
109struct SCSIReqOps {
110 size_t size;
12010e7b
PB
111 void (*free_req)(SCSIRequest *req);
112 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
113 void (*read_data)(SCSIRequest *req);
114 void (*write_data)(SCSIRequest *req);
12010e7b 115 uint8_t *(*get_buf)(SCSIRequest *req);
63f740dd
PB
116
117 void (*save_request)(QEMUFile *f, SCSIRequest *req);
118 void (*load_request)(QEMUFile *f, SCSIRequest *req);
8dbd4574
PB
119};
120
afd4030c 121struct SCSIBusInfo {
7e0380b9 122 int tcq;
0d3545e7 123 int max_channel, max_target, max_lun;
ff34c32c
PB
124 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
125 void *hba_private);
c6df7102 126 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
f3126d65 127 void (*fail)(SCSIRequest *req);
5f412602 128 void (*complete)(SCSIRequest *req, size_t residual);
94d3f98a 129 void (*cancel)(SCSIRequest *req);
53200fad 130 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
3d5aba97 131 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
63f740dd
PB
132
133 void (*save_request)(QEMUFile *f, SCSIRequest *req);
134 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
8e86b93c 135 void (*free_request)(SCSIBus *bus, void *priv);
cfdc1bb0
PB
136};
137
0d936928 138#define TYPE_SCSI_BUS "SCSI"
8063396b 139OBJECT_DECLARE_SIMPLE_TYPE(SCSIBus, SCSI_BUS)
0d936928 140
43b443b6
GH
141struct SCSIBus {
142 BusState qbus;
143 int busnr;
144
6dc06f08 145 SCSISense unit_attention;
afd4030c 146 const SCSIBusInfo *info;
43b443b6
GH
147};
148
739e95f5
PM
149/**
150 * scsi_bus_init_named: Initialize a SCSI bus with the specified name
151 * @bus: SCSIBus object to initialize
152 * @bus_size: size of @bus object
153 * @host: Device which owns the bus (generally the SCSI controller)
154 * @info: structure defining callbacks etc for the controller
155 * @bus_name: Name to use for this bus
156 *
157 * This in-place initializes @bus as a new SCSI bus with a name
158 * provided by the caller. It is the caller's responsibility to make
159 * sure that name does not clash with the name of any other bus in the
160 * system. Unless you need the new bus to have a specific name, you
7a3ce79c 161 * should use scsi_bus_init() instead.
739e95f5
PM
162 */
163void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host,
164 const SCSIBusInfo *info, const char *bus_name);
165
166/**
167 * scsi_bus_init: Initialize a SCSI bus
168 *
169 * This in-place-initializes @bus as a new SCSI bus and gives it
170 * an automatically generated unique name.
171 */
172static inline void scsi_bus_init(SCSIBus *bus, size_t bus_size,
173 DeviceState *host, const SCSIBusInfo *info)
174{
175 scsi_bus_init_named(bus, bus_size, host, info, NULL);
176}
43b443b6
GH
177
178static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
179{
180 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
181}
182
4be74634 183SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
76534da7 184 int unit, bool removable, int bootindex,
395b9539 185 bool share_rw,
b8efb36b
KW
186 BlockdevOnError rerror,
187 BlockdevOnError werror,
caad4eb3 188 const char *serial, Error **errp);
14545097 189void scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
fb8b660e 190void scsi_legacy_handle_cmdline(void);
43b443b6 191
adcf2754
PB
192SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
193 uint32_t tag, uint32_t lun, void *hba_private);
c5bf71a9 194SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
c39ce112
PB
195 uint8_t *buf, void *hba_private);
196int32_t scsi_req_enqueue(SCSIRequest *req);
ad2d30f7
PB
197SCSIRequest *scsi_req_ref(SCSIRequest *req);
198void scsi_req_unref(SCSIRequest *req);
37659e51 199
ff34c32c
PB
200int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
201 void *hba_private);
3e7e180a 202int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
b45ef674 203void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
ec766865 204void scsi_req_print(SCSIRequest *req);
ad3376cc 205void scsi_req_continue(SCSIRequest *req);
ab9adc88 206void scsi_req_data(SCSIRequest *req, int len);
682a9b21 207void scsi_req_complete(SCSIRequest *req, int status);
f3126d65 208void scsi_req_complete_failed(SCSIRequest *req, int host_status);
0c34459b 209uint8_t *scsi_req_get_buf(SCSIRequest *req);
74382217 210int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
d5776465 211void scsi_req_cancel_complete(SCSIRequest *req);
94d3f98a 212void scsi_req_cancel(SCSIRequest *req);
8e0a9320 213void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
71544d30 214void scsi_req_retry(SCSIRequest *req);
c7b48872 215void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
e48e84ea 216void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
53200fad 217void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
8d72db68 218void scsi_device_unit_attention_reported(SCSIDevice *dev);
a71c775b 219void scsi_generic_read_device_inquiry(SCSIDevice *dev);
b45ef674 220int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
a0c7e35b 221int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
c9b6609b 222 uint8_t *buf, uint8_t buf_size, uint32_t timeout);
0d3545e7 223SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
8ff34495 224SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);
89b08ae1 225
765d1525
PB
226/* scsi-generic.c. */
227extern const SCSIReqOps scsi_generic_req_ops;
228
43b443b6 229#endif