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