]> git.proxmox.com Git - qemu.git/blame - hw/scsi.h
scsi-generic: bump SCSIRequest reference count until aio completion runs
[qemu.git] / hw / scsi.h
CommitLineData
1e37607b
GH
1#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
43b443b6
GH
3
4#include "qdev.h"
4c41d2ef 5#include "block.h"
43b443b6 6
27d6bf40
MA
7#define MAX_SCSI_DEVS 255
8
29362ebe
GH
9#define SCSI_CMD_BUF_SIZE 16
10
43b443b6 11typedef struct SCSIBus SCSIBus;
afd4030c 12typedef struct SCSIBusInfo SCSIBusInfo;
2599aece 13typedef struct SCSICommand SCSICommand;
43b443b6
GH
14typedef struct SCSIDevice SCSIDevice;
15typedef struct SCSIDeviceInfo SCSIDeviceInfo;
5c6c0e51 16typedef struct SCSIRequest SCSIRequest;
8dbd4574 17typedef struct SCSIReqOps SCSIReqOps;
43b443b6 18
97a06435
GH
19enum 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
a1f0cce2
HR
25typedef struct SCSISense {
26 uint8_t key;
27 uint8_t asc;
28 uint8_t ascq;
29} SCSISense;
30
b45ef674
PB
31#define SCSI_SENSE_BUF_SIZE 96
32
2599aece
PB
33struct 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
5c6c0e51 41struct SCSIRequest {
4c41d2ef
GH
42 SCSIBus *bus;
43 SCSIDevice *dev;
adcf2754 44 const SCSIReqOps *ops;
ad2d30f7 45 uint32_t refcount;
4c41d2ef 46 uint32_t tag;
89b08ae1 47 uint32_t lun;
ed3a34a3 48 uint32_t status;
2599aece 49 SCSICommand cmd;
4c41d2ef 50 BlockDriverAIOCB *aiocb;
b45ef674
PB
51 uint8_t sense[SCSI_SENSE_BUF_SIZE];
52 uint32_t sense_len;
e8637c90 53 bool enqueued;
e88c591d 54 bool io_canceled;
c5bf71a9 55 void *hba_private;
9af99d98 56 QTAILQ_ENTRY(SCSIRequest) next;
5c6c0e51 57};
4c41d2ef 58
43b443b6
GH
59struct SCSIDevice
60{
61 DeviceState qdev;
62 uint32_t id;
428c149b 63 BlockConf conf;
43b443b6 64 SCSIDeviceInfo *info;
6dc06f08 65 SCSISense unit_attention;
3653d8c4 66 bool sense_is_ua;
b45ef674
PB
67 uint8_t sense[SCSI_SENSE_BUF_SIZE];
68 uint32_t sense_len;
9af99d98 69 QTAILQ_HEAD(, SCSIRequest) requests;
0d3545e7 70 uint32_t channel;
87dcd1b2 71 uint32_t lun;
b07995e3 72 int blocksize;
91376656 73 int type;
7877903a 74 uint64_t max_lba;
43b443b6
GH
75};
76
77/* cdrom.c */
78int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
79int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
80
81/* scsi-bus.c */
8dbd4574
PB
82struct SCSIReqOps {
83 size_t size;
12010e7b
PB
84 void (*free_req)(SCSIRequest *req);
85 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
86 void (*read_data)(SCSIRequest *req);
87 void (*write_data)(SCSIRequest *req);
88 void (*cancel_io)(SCSIRequest *req);
89 uint8_t *(*get_buf)(SCSIRequest *req);
8dbd4574
PB
90};
91
43b443b6
GH
92typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
93struct SCSIDeviceInfo {
94 DeviceInfo qdev;
95 scsi_qdev_initfn init;
96 void (*destroy)(SCSIDevice *s);
c5bf71a9 97 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
63db0f0e 98 uint8_t *buf, void *hba_private);
3653d8c4 99 void (*unit_attention_reported)(SCSIDevice *s);
43b443b6
GH
100};
101
afd4030c 102struct SCSIBusInfo {
7e0380b9 103 int tcq;
0d3545e7 104 int max_channel, max_target, max_lun;
c6df7102
PB
105 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
106 void (*complete)(SCSIRequest *req, uint32_t arg);
94d3f98a 107 void (*cancel)(SCSIRequest *req);
cfdc1bb0
PB
108};
109
43b443b6
GH
110struct SCSIBus {
111 BusState qbus;
112 int busnr;
113
6dc06f08 114 SCSISense unit_attention;
afd4030c 115 const SCSIBusInfo *info;
43b443b6
GH
116};
117
afd4030c 118void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
43b443b6
GH
119void scsi_qdev_register(SCSIDeviceInfo *info);
120
121static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
122{
123 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
124}
125
2d1fd261
SH
126SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
127 int unit, bool removable);
fa66b909 128int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
43b443b6 129
a1f0cce2
HR
130/*
131 * Predefined sense codes
132 */
133
134/* No sense data available */
135extern const struct SCSISense sense_code_NO_SENSE;
136/* LUN not ready, Manual intervention required */
137extern const struct SCSISense sense_code_LUN_NOT_READY;
138/* LUN not ready, Medium not present */
139extern const struct SCSISense sense_code_NO_MEDIUM;
68bb01f3
MA
140/* LUN not ready, medium removal prevented */
141extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
a1f0cce2
HR
142/* Hardware error, internal target failure */
143extern const struct SCSISense sense_code_TARGET_FAILURE;
144/* Illegal request, invalid command operation code */
145extern const struct SCSISense sense_code_INVALID_OPCODE;
146/* Illegal request, LBA out of range */
147extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
148/* Illegal request, Invalid field in CDB */
149extern const struct SCSISense sense_code_INVALID_FIELD;
150/* Illegal request, LUN not supported */
151extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
a872a304
PB
152/* Illegal request, Saving parameters not supported */
153extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
154/* Illegal request, Incompatible format */
155extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
68bb01f3
MA
156/* Illegal request, medium removal prevented */
157extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
a1f0cce2
HR
158/* Command aborted, I/O process terminated */
159extern const struct SCSISense sense_code_IO_ERROR;
160/* Command aborted, I_T Nexus loss occurred */
161extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
162/* Command aborted, Logical Unit failure */
163extern const struct SCSISense sense_code_LUN_FAILURE;
8a9c16f6
PB
164/* LUN not ready, Medium not present */
165extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
a872a304
PB
166/* Unit attention, Power on, reset or bus device reset occurred */
167extern const struct SCSISense sense_code_RESET;
168/* Unit attention, Medium may have changed*/
169extern const struct SCSISense sense_code_MEDIUM_CHANGED;
170/* Unit attention, Reported LUNs data has changed */
171extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
172/* Unit attention, Device internal reset */
173extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
a1f0cce2
HR
174
175#define SENSE_CODE(x) sense_code_ ## x
176
a1f0cce2
HR
177int scsi_sense_valid(SCSISense sense);
178
adcf2754
PB
179SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
180 uint32_t tag, uint32_t lun, void *hba_private);
c5bf71a9 181SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
c39ce112
PB
182 uint8_t *buf, void *hba_private);
183int32_t scsi_req_enqueue(SCSIRequest *req);
89b08ae1 184void scsi_req_free(SCSIRequest *req);
ad2d30f7
PB
185SCSIRequest *scsi_req_ref(SCSIRequest *req);
186void scsi_req_unref(SCSIRequest *req);
37659e51 187
b45ef674 188void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
ec766865 189void scsi_req_print(SCSIRequest *req);
ad3376cc 190void scsi_req_continue(SCSIRequest *req);
ab9adc88 191void scsi_req_data(SCSIRequest *req, int len);
682a9b21 192void scsi_req_complete(SCSIRequest *req, int status);
0c34459b 193uint8_t *scsi_req_get_buf(SCSIRequest *req);
74382217 194int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
19d110ab 195void scsi_req_abort(SCSIRequest *req, int status);
94d3f98a 196void scsi_req_cancel(SCSIRequest *req);
c7b48872 197void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
b45ef674 198int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
0d3545e7 199SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
89b08ae1 200
765d1525
PB
201/* scsi-generic.c. */
202extern const SCSIReqOps scsi_generic_req_ops;
203
43b443b6 204#endif