Statistics
| Branch: | Revision:

root / hw / scsi.h @ c6df7102

History | View | Annotate | Download (4.8 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 SCSIDevice SCSIDevice;
15
typedef struct SCSIDeviceInfo SCSIDeviceInfo;
16
typedef struct SCSIRequest SCSIRequest;
17

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

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

    
30
struct SCSIRequest {
31
    SCSIBus           *bus;
32
    SCSIDevice        *dev;
33
    uint32_t          refcount;
34
    uint32_t          tag;
35
    uint32_t          lun;
36
    uint32_t          status;
37
    struct {
38
        uint8_t buf[SCSI_CMD_BUF_SIZE];
39
        int len;
40
        size_t xfer;
41
        uint64_t lba;
42
        enum SCSIXferMode mode;
43
    } cmd;
44
    BlockDriverAIOCB  *aiocb;
45
    bool enqueued;
46
    QTAILQ_ENTRY(SCSIRequest) next;
47
};
48

    
49
struct SCSIDevice
50
{
51
    DeviceState qdev;
52
    uint32_t id;
53
    BlockConf conf;
54
    SCSIDeviceInfo *info;
55
    QTAILQ_HEAD(, SCSIRequest) requests;
56
    int blocksize;
57
    int type;
58
};
59

    
60
/* cdrom.c */
61
int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
62
int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
63

    
64
/* scsi-bus.c */
65
typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
66
struct SCSIDeviceInfo {
67
    DeviceInfo qdev;
68
    scsi_qdev_initfn init;
69
    void (*destroy)(SCSIDevice *s);
70
    SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun);
71
    void (*free_req)(SCSIRequest *req);
72
    int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
73
    void (*read_data)(SCSIRequest *req);
74
    void (*write_data)(SCSIRequest *req);
75
    void (*cancel_io)(SCSIRequest *req);
76
    uint8_t *(*get_buf)(SCSIRequest *req);
77
    int (*get_sense)(SCSIRequest *req, uint8_t *buf, int len);
78
};
79

    
80
struct SCSIBusOps {
81
    void (*transfer_data)(SCSIRequest *req, uint32_t arg);
82
    void (*complete)(SCSIRequest *req, uint32_t arg);
83
    void (*cancel)(SCSIRequest *req);
84
};
85

    
86
struct SCSIBus {
87
    BusState qbus;
88
    int busnr;
89

    
90
    int tcq, ndev;
91
    const SCSIBusOps *ops;
92

    
93
    SCSIDevice *devs[MAX_SCSI_DEVS];
94
};
95

    
96
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
97
                  const SCSIBusOps *ops);
98
void scsi_qdev_register(SCSIDeviceInfo *info);
99

    
100
static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
101
{
102
    return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
103
}
104

    
105
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
106
                                      int unit, bool removable);
107
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
108

    
109
/*
110
 * Predefined sense codes
111
 */
112

    
113
/* No sense data available */
114
extern const struct SCSISense sense_code_NO_SENSE;
115
/* LUN not ready, Manual intervention required */
116
extern const struct SCSISense sense_code_LUN_NOT_READY;
117
/* LUN not ready, Medium not present */
118
extern const struct SCSISense sense_code_NO_MEDIUM;
119
/* Hardware error, internal target failure */
120
extern const struct SCSISense sense_code_TARGET_FAILURE;
121
/* Illegal request, invalid command operation code */
122
extern const struct SCSISense sense_code_INVALID_OPCODE;
123
/* Illegal request, LBA out of range */
124
extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
125
/* Illegal request, Invalid field in CDB */
126
extern const struct SCSISense sense_code_INVALID_FIELD;
127
/* Illegal request, LUN not supported */
128
extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
129
/* Command aborted, I/O process terminated */
130
extern const struct SCSISense sense_code_IO_ERROR;
131
/* Command aborted, I_T Nexus loss occurred */
132
extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
133
/* Command aborted, Logical Unit failure */
134
extern const struct SCSISense sense_code_LUN_FAILURE;
135

    
136
#define SENSE_CODE(x) sense_code_ ## x
137

    
138
int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed);
139
int scsi_sense_valid(SCSISense sense);
140

    
141
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
142
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun);
143
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf);
144
void scsi_req_free(SCSIRequest *req);
145
SCSIRequest *scsi_req_ref(SCSIRequest *req);
146
void scsi_req_unref(SCSIRequest *req);
147

    
148
int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
149
void scsi_req_print(SCSIRequest *req);
150
void scsi_req_continue(SCSIRequest *req);
151
void scsi_req_data(SCSIRequest *req, int len);
152
void scsi_req_complete(SCSIRequest *req);
153
uint8_t *scsi_req_get_buf(SCSIRequest *req);
154
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
155
void scsi_req_abort(SCSIRequest *req, int status);
156
void scsi_req_cancel(SCSIRequest *req);
157
void scsi_device_purge_requests(SCSIDevice *sdev);
158

    
159
#endif