Statistics
| Branch: | Revision:

root / hw / scsi.h @ 57c83dac

History | View | Annotate | Download (7 kB)

1
#ifndef QEMU_HW_SCSI_H
2
#define QEMU_HW_SCSI_H
3

    
4
#include "qdev.h"
5
#include "block.h"
6
#include "sysemu.h"
7

    
8
#define MAX_SCSI_DEVS        255
9

    
10
#define SCSI_CMD_BUF_SIZE     16
11

    
12
typedef struct SCSIBus SCSIBus;
13
typedef struct SCSIBusInfo SCSIBusInfo;
14
typedef struct SCSICommand SCSICommand;
15
typedef struct SCSIDevice SCSIDevice;
16
typedef struct SCSIRequest SCSIRequest;
17
typedef struct SCSIReqOps SCSIReqOps;
18

    
19
enum 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

    
25
typedef struct SCSISense {
26
    uint8_t key;
27
    uint8_t asc;
28
    uint8_t ascq;
29
} SCSISense;
30

    
31
#define SCSI_SENSE_BUF_SIZE 96
32

    
33
struct 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

    
41
struct SCSIRequest {
42
    SCSIBus           *bus;
43
    SCSIDevice        *dev;
44
    const SCSIReqOps  *ops;
45
    uint32_t          refcount;
46
    uint32_t          tag;
47
    uint32_t          lun;
48
    uint32_t          status;
49
    SCSICommand       cmd;
50
    BlockDriverAIOCB  *aiocb;
51
    uint8_t sense[SCSI_SENSE_BUF_SIZE];
52
    uint32_t sense_len;
53
    bool enqueued;
54
    bool io_canceled;
55
    bool retry;
56
    void *hba_private;
57
    QTAILQ_ENTRY(SCSIRequest) next;
58
};
59

    
60
#define TYPE_SCSI_DEVICE "scsi-device"
61
#define SCSI_DEVICE(obj) \
62
     OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
63
#define SCSI_DEVICE_CLASS(klass) \
64
     OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
65
#define SCSI_DEVICE_GET_CLASS(obj) \
66
     OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
67

    
68
typedef struct SCSIDeviceClass {
69
    DeviceClass parent_class;
70
    int (*init)(SCSIDevice *dev);
71
    void (*destroy)(SCSIDevice *s);
72
    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
73
                              uint8_t *buf, void *hba_private);
74
    void (*unit_attention_reported)(SCSIDevice *s);
75
} SCSIDeviceClass;
76

    
77
struct SCSIDevice
78
{
79
    DeviceState qdev;
80
    VMChangeStateEntry *vmsentry;
81
    QEMUBH *bh;
82
    uint32_t id;
83
    BlockConf conf;
84
    SCSISense unit_attention;
85
    bool sense_is_ua;
86
    uint8_t sense[SCSI_SENSE_BUF_SIZE];
87
    uint32_t sense_len;
88
    QTAILQ_HEAD(, SCSIRequest) requests;
89
    uint32_t channel;
90
    uint32_t lun;
91
    int blocksize;
92
    int type;
93
    uint64_t max_lba;
94
};
95

    
96
/* cdrom.c */
97
int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
98
int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
99

    
100
/* scsi-bus.c */
101
struct SCSIReqOps {
102
    size_t size;
103
    void (*free_req)(SCSIRequest *req);
104
    int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
105
    void (*read_data)(SCSIRequest *req);
106
    void (*write_data)(SCSIRequest *req);
107
    void (*cancel_io)(SCSIRequest *req);
108
    uint8_t *(*get_buf)(SCSIRequest *req);
109
};
110

    
111
struct SCSIBusInfo {
112
    int tcq;
113
    int max_channel, max_target, max_lun;
114
    void (*transfer_data)(SCSIRequest *req, uint32_t arg);
115
    void (*complete)(SCSIRequest *req, uint32_t arg);
116
    void (*cancel)(SCSIRequest *req);
117
};
118

    
119
struct SCSIBus {
120
    BusState qbus;
121
    int busnr;
122

    
123
    SCSISense unit_attention;
124
    const SCSIBusInfo *info;
125
};
126

    
127
void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info);
128

    
129
static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
130
{
131
    return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
132
}
133

    
134
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
135
                                      int unit, bool removable, int bootindex);
136
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
137

    
138
/*
139
 * Predefined sense codes
140
 */
141

    
142
/* No sense data available */
143
extern const struct SCSISense sense_code_NO_SENSE;
144
/* LUN not ready, Manual intervention required */
145
extern const struct SCSISense sense_code_LUN_NOT_READY;
146
/* LUN not ready, Medium not present */
147
extern const struct SCSISense sense_code_NO_MEDIUM;
148
/* LUN not ready, medium removal prevented */
149
extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
150
/* Hardware error, internal target failure */
151
extern const struct SCSISense sense_code_TARGET_FAILURE;
152
/* Illegal request, invalid command operation code */
153
extern const struct SCSISense sense_code_INVALID_OPCODE;
154
/* Illegal request, LBA out of range */
155
extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
156
/* Illegal request, Invalid field in CDB */
157
extern const struct SCSISense sense_code_INVALID_FIELD;
158
/* Illegal request, LUN not supported */
159
extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
160
/* Illegal request, Saving parameters not supported */
161
extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
162
/* Illegal request, Incompatible format */
163
extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
164
/* Illegal request, medium removal prevented */
165
extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
166
/* Command aborted, I/O process terminated */
167
extern const struct SCSISense sense_code_IO_ERROR;
168
/* Command aborted, I_T Nexus loss occurred */
169
extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
170
/* Command aborted, Logical Unit failure */
171
extern const struct SCSISense sense_code_LUN_FAILURE;
172
/* LUN not ready, Medium not present */
173
extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
174
/* Unit attention, Power on, reset or bus device reset occurred */
175
extern const struct SCSISense sense_code_RESET;
176
/* Unit attention, Medium may have changed*/
177
extern const struct SCSISense sense_code_MEDIUM_CHANGED;
178
/* Unit attention, Reported LUNs data has changed */
179
extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
180
/* Unit attention, Device internal reset */
181
extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
182

    
183
#define SENSE_CODE(x) sense_code_ ## x
184

    
185
int scsi_sense_valid(SCSISense sense);
186
int scsi_build_sense(uint8_t *in_buf, int in_len,
187
                     uint8_t *buf, int len, bool fixed);
188

    
189
SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
190
                            uint32_t tag, uint32_t lun, void *hba_private);
191
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
192
                          uint8_t *buf, void *hba_private);
193
int32_t scsi_req_enqueue(SCSIRequest *req);
194
void scsi_req_free(SCSIRequest *req);
195
SCSIRequest *scsi_req_ref(SCSIRequest *req);
196
void scsi_req_unref(SCSIRequest *req);
197

    
198
void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
199
void scsi_req_print(SCSIRequest *req);
200
void scsi_req_continue(SCSIRequest *req);
201
void scsi_req_data(SCSIRequest *req, int len);
202
void scsi_req_complete(SCSIRequest *req, int status);
203
uint8_t *scsi_req_get_buf(SCSIRequest *req);
204
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
205
void scsi_req_abort(SCSIRequest *req, int status);
206
void scsi_req_cancel(SCSIRequest *req);
207
void scsi_req_retry(SCSIRequest *req);
208
void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
209
int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
210
SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
211

    
212
/* scsi-generic.c. */
213
extern const SCSIReqOps scsi_generic_req_ops;
214

    
215
#endif