Statistics
| Branch: | Revision:

root / hw / scsi.h @ a872a304

History | View | Annotate | Download (6 kB)

1
#ifndef QEMU_HW_SCSI_H
2
#define QEMU_HW_SCSI_H
3

    
4
#include "qdev.h"
5
#include "block.h"
6
#include "block_int.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 SCSIBusOps SCSIBusOps;
14
typedef struct SCSICommand SCSICommand;
15
typedef struct SCSIDevice SCSIDevice;
16
typedef struct SCSIDeviceInfo SCSIDeviceInfo;
17
typedef struct SCSIRequest SCSIRequest;
18
typedef struct SCSIReqOps SCSIReqOps;
19

    
20
enum SCSIXferMode {
21
    SCSI_XFER_NONE,      /*  TEST_UNIT_READY, ...            */
22
    SCSI_XFER_FROM_DEV,  /*  READ, INQUIRY, MODE_SENSE, ...  */
23
    SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
24
};
25

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

    
32
#define SCSI_SENSE_BUF_SIZE 96
33

    
34
struct SCSICommand {
35
    uint8_t buf[SCSI_CMD_BUF_SIZE];
36
    int len;
37
    size_t xfer;
38
    uint64_t lba;
39
    enum SCSIXferMode mode;
40
};
41

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

    
59
struct SCSIDevice
60
{
61
    DeviceState qdev;
62
    uint32_t id;
63
    BlockConf conf;
64
    SCSIDeviceInfo *info;
65
    uint8_t sense[SCSI_SENSE_BUF_SIZE];
66
    uint32_t sense_len;
67
    QTAILQ_HEAD(, SCSIRequest) requests;
68
    uint32_t lun;
69
    int blocksize;
70
    int type;
71
};
72

    
73
/* cdrom.c */
74
int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
75
int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
76

    
77
/* scsi-bus.c */
78
struct SCSIReqOps {
79
    size_t size;
80
    void (*free_req)(SCSIRequest *req);
81
    int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
82
    void (*read_data)(SCSIRequest *req);
83
    void (*write_data)(SCSIRequest *req);
84
    void (*cancel_io)(SCSIRequest *req);
85
    uint8_t *(*get_buf)(SCSIRequest *req);
86
};
87

    
88
typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
89
struct SCSIDeviceInfo {
90
    DeviceInfo qdev;
91
    scsi_qdev_initfn init;
92
    void (*destroy)(SCSIDevice *s);
93
    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
94
                              void *hba_private);
95
    SCSIReqOps reqops;
96
};
97

    
98
struct SCSIBusOps {
99
    void (*transfer_data)(SCSIRequest *req, uint32_t arg);
100
    void (*complete)(SCSIRequest *req, uint32_t arg);
101
    void (*cancel)(SCSIRequest *req);
102
};
103

    
104
struct SCSIBus {
105
    BusState qbus;
106
    int busnr;
107

    
108
    int tcq, ndev;
109
    const SCSIBusOps *ops;
110

    
111
    SCSIDevice *devs[MAX_SCSI_DEVS];
112
};
113

    
114
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
115
                  const SCSIBusOps *ops);
116
void scsi_qdev_register(SCSIDeviceInfo *info);
117

    
118
static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
119
{
120
    return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
121
}
122

    
123
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
124
                                      int unit, bool removable);
125
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
126

    
127
/*
128
 * Predefined sense codes
129
 */
130

    
131
/* No sense data available */
132
extern const struct SCSISense sense_code_NO_SENSE;
133
/* LUN not ready, Manual intervention required */
134
extern const struct SCSISense sense_code_LUN_NOT_READY;
135
/* LUN not ready, Medium not present */
136
extern const struct SCSISense sense_code_NO_MEDIUM;
137
/* Hardware error, internal target failure */
138
extern const struct SCSISense sense_code_TARGET_FAILURE;
139
/* Illegal request, invalid command operation code */
140
extern const struct SCSISense sense_code_INVALID_OPCODE;
141
/* Illegal request, LBA out of range */
142
extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
143
/* Illegal request, Invalid field in CDB */
144
extern const struct SCSISense sense_code_INVALID_FIELD;
145
/* Illegal request, LUN not supported */
146
extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
147
/* Illegal request, Saving parameters not supported */
148
extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
149
/* Illegal request, Incompatible format */
150
extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
151
/* Command aborted, I/O process terminated */
152
extern const struct SCSISense sense_code_IO_ERROR;
153
/* Command aborted, I_T Nexus loss occurred */
154
extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
155
/* Command aborted, Logical Unit failure */
156
extern const struct SCSISense sense_code_LUN_FAILURE;
157
/* Unit attention, Power on, reset or bus device reset occurred */
158
extern const struct SCSISense sense_code_RESET;
159
/* Unit attention, Medium may have changed*/
160
extern const struct SCSISense sense_code_MEDIUM_CHANGED;
161
/* Unit attention, Reported LUNs data has changed */
162
extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
163
/* Unit attention, Device internal reset */
164
extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
165

    
166
#define SENSE_CODE(x) sense_code_ ## x
167

    
168
int scsi_sense_valid(SCSISense sense);
169

    
170
SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
171
                            uint32_t lun, void *hba_private);
172
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
173
                          uint8_t *buf, void *hba_private);
174
int32_t scsi_req_enqueue(SCSIRequest *req);
175
void scsi_req_free(SCSIRequest *req);
176
SCSIRequest *scsi_req_ref(SCSIRequest *req);
177
void scsi_req_unref(SCSIRequest *req);
178

    
179
void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
180
void scsi_req_print(SCSIRequest *req);
181
void scsi_req_continue(SCSIRequest *req);
182
void scsi_req_data(SCSIRequest *req, int len);
183
void scsi_req_complete(SCSIRequest *req, int status);
184
uint8_t *scsi_req_get_buf(SCSIRequest *req);
185
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
186
void scsi_req_abort(SCSIRequest *req, int status);
187
void scsi_req_cancel(SCSIRequest *req);
188
void scsi_device_purge_requests(SCSIDevice *sdev);
189
int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
190

    
191
#endif