]> git.proxmox.com Git - qemu.git/blame - hw/scsi.h
esp: cancel current request only if some request is in flight
[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"
428c149b 6#include "block_int.h"
43b443b6 7
27d6bf40
MA
8#define MAX_SCSI_DEVS 255
9
29362ebe
GH
10#define SCSI_CMD_BUF_SIZE 16
11
43b443b6 12typedef struct SCSIBus SCSIBus;
cfdc1bb0 13typedef struct SCSIBusOps SCSIBusOps;
43b443b6
GH
14typedef struct SCSIDevice SCSIDevice;
15typedef struct SCSIDeviceInfo SCSIDeviceInfo;
5c6c0e51 16typedef struct SCSIRequest SCSIRequest;
43b443b6 17
97a06435
GH
18enum SCSIXferMode {
19 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
20 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
21 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
22};
23
a1f0cce2
HR
24typedef struct SCSISense {
25 uint8_t key;
26 uint8_t asc;
27 uint8_t ascq;
28} SCSISense;
29
5c6c0e51 30struct SCSIRequest {
4c41d2ef
GH
31 SCSIBus *bus;
32 SCSIDevice *dev;
ad2d30f7 33 uint32_t refcount;
4c41d2ef 34 uint32_t tag;
89b08ae1 35 uint32_t lun;
ed3a34a3 36 uint32_t status;
29362ebe
GH
37 struct {
38 uint8_t buf[SCSI_CMD_BUF_SIZE];
39 int len;
2ec749cb
GH
40 size_t xfer;
41 uint64_t lba;
97a06435 42 enum SCSIXferMode mode;
29362ebe 43 } cmd;
4c41d2ef 44 BlockDriverAIOCB *aiocb;
e8637c90 45 bool enqueued;
9af99d98 46 QTAILQ_ENTRY(SCSIRequest) next;
5c6c0e51 47};
4c41d2ef 48
43b443b6
GH
49struct SCSIDevice
50{
51 DeviceState qdev;
52 uint32_t id;
428c149b 53 BlockConf conf;
43b443b6 54 SCSIDeviceInfo *info;
9af99d98 55 QTAILQ_HEAD(, SCSIRequest) requests;
b07995e3 56 int blocksize;
91376656 57 int type;
43b443b6
GH
58};
59
60/* cdrom.c */
61int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
62int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
63
64/* scsi-bus.c */
65typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
66struct SCSIDeviceInfo {
67 DeviceInfo qdev;
68 scsi_qdev_initfn init;
69 void (*destroy)(SCSIDevice *s);
5c6c0e51 70 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
ad2d30f7 71 void (*free_req)(SCSIRequest *req);
5c6c0e51
HR
72 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
73 void (*read_data)(SCSIRequest *req);
42741212 74 void (*write_data)(SCSIRequest *req);
5c6c0e51
HR
75 void (*cancel_io)(SCSIRequest *req);
76 uint8_t *(*get_buf)(SCSIRequest *req);
74382217 77 int (*get_sense)(SCSIRequest *req, uint8_t *buf, int len);
43b443b6
GH
78};
79
cfdc1bb0 80struct SCSIBusOps {
c6df7102
PB
81 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
82 void (*complete)(SCSIRequest *req, uint32_t arg);
94d3f98a 83 void (*cancel)(SCSIRequest *req);
cfdc1bb0
PB
84};
85
43b443b6
GH
86struct SCSIBus {
87 BusState qbus;
88 int busnr;
89
90 int tcq, ndev;
cfdc1bb0 91 const SCSIBusOps *ops;
43b443b6 92
622b520f 93 SCSIDevice *devs[MAX_SCSI_DEVS];
43b443b6
GH
94};
95
96void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
cfdc1bb0 97 const SCSIBusOps *ops);
43b443b6
GH
98void scsi_qdev_register(SCSIDeviceInfo *info);
99
100static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
101{
102 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
103}
104
2d1fd261
SH
105SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
106 int unit, bool removable);
fa66b909 107int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
43b443b6 108
a1f0cce2
HR
109/*
110 * Predefined sense codes
111 */
112
113/* No sense data available */
114extern const struct SCSISense sense_code_NO_SENSE;
115/* LUN not ready, Manual intervention required */
116extern const struct SCSISense sense_code_LUN_NOT_READY;
117/* LUN not ready, Medium not present */
118extern const struct SCSISense sense_code_NO_MEDIUM;
119/* Hardware error, internal target failure */
120extern const struct SCSISense sense_code_TARGET_FAILURE;
121/* Illegal request, invalid command operation code */
122extern const struct SCSISense sense_code_INVALID_OPCODE;
123/* Illegal request, LBA out of range */
124extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
125/* Illegal request, Invalid field in CDB */
126extern const struct SCSISense sense_code_INVALID_FIELD;
127/* Illegal request, LUN not supported */
128extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
129/* Command aborted, I/O process terminated */
130extern const struct SCSISense sense_code_IO_ERROR;
131/* Command aborted, I_T Nexus loss occurred */
132extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
133/* Command aborted, Logical Unit failure */
134extern const struct SCSISense sense_code_LUN_FAILURE;
135
136#define SENSE_CODE(x) sense_code_ ## x
137
138int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
139int scsi_sense_valid(SCSISense sense);
140
89b08ae1 141SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
43a2b339 142SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun);
fc4f0754 143int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
89b08ae1 144void scsi_req_free(SCSIRequest *req);
ad2d30f7
PB
145SCSIRequest *scsi_req_ref(SCSIRequest *req);
146void scsi_req_unref(SCSIRequest *req);
37659e51 147
2ec749cb 148int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
ec766865 149void scsi_req_print(SCSIRequest *req);
ad3376cc 150void scsi_req_continue(SCSIRequest *req);
ab9adc88 151void scsi_req_data(SCSIRequest *req, int len);
ed3a34a3 152void scsi_req_complete(SCSIRequest *req);
0c34459b 153uint8_t *scsi_req_get_buf(SCSIRequest *req);
74382217 154int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
19d110ab 155void scsi_req_abort(SCSIRequest *req, int status);
94d3f98a 156void scsi_req_cancel(SCSIRequest *req);
c557e889 157void scsi_device_purge_requests(SCSIDevice *sdev);
89b08ae1 158
43b443b6 159#endif