Statistics
| Branch: | Revision:

root / hw / scsi.h @ 6dc06f08

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
    SCSISense unit_attention;
66
    uint8_t sense[SCSI_SENSE_BUF_SIZE];
67
    uint32_t sense_len;
68
    QTAILQ_HEAD(, SCSIRequest) requests;
69
    uint32_t lun;
70
    int blocksize;
71
    int type;
72
};
73

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

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

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

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

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

    
109
    SCSISense unit_attention;
110
    int tcq, ndev;
111
    const SCSIBusOps *ops;
112

    
113
    SCSIDevice *devs[MAX_SCSI_DEVS];
114
};
115

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

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

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

    
129
/*
130
 * Predefined sense codes
131
 */
132

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

    
168
#define SENSE_CODE(x) sense_code_ ## x
169

    
170
int scsi_sense_valid(SCSISense sense);
171

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

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

    
193
#endif