scsi: reference-count requests
[qemu/aliguori.git] / hw / scsi.h
bloba1d0e7451f14e878d9c21817cd78d9b0291df0ff
1 #ifndef QEMU_HW_SCSI_H
2 #define QEMU_HW_SCSI_H
4 #include "qdev.h"
5 #include "block.h"
6 #include "block_int.h"
8 #define MAX_SCSI_DEVS 255
10 #define SCSI_CMD_BUF_SIZE 16
12 /* scsi-disk.c */
13 enum scsi_reason {
14 SCSI_REASON_DONE, /* Command complete. */
15 SCSI_REASON_DATA /* Transfer complete, more data required. */
18 typedef struct SCSIBus SCSIBus;
19 typedef struct SCSIBusOps SCSIBusOps;
20 typedef struct SCSIDevice SCSIDevice;
21 typedef struct SCSIDeviceInfo SCSIDeviceInfo;
23 enum SCSIXferMode {
24 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
25 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
26 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
29 typedef struct SCSIRequest {
30 SCSIBus *bus;
31 SCSIDevice *dev;
32 uint32_t refcount;
33 uint32_t tag;
34 uint32_t lun;
35 uint32_t status;
36 struct {
37 uint8_t buf[SCSI_CMD_BUF_SIZE];
38 int len;
39 size_t xfer;
40 uint64_t lba;
41 enum SCSIXferMode mode;
42 } cmd;
43 BlockDriverAIOCB *aiocb;
44 bool enqueued;
45 QTAILQ_ENTRY(SCSIRequest) next;
46 } SCSIRequest;
48 struct SCSIDevice
50 DeviceState qdev;
51 uint32_t id;
52 BlockConf conf;
53 SCSIDeviceInfo *info;
54 QTAILQ_HEAD(, SCSIRequest) requests;
55 int blocksize;
56 int type;
59 /* cdrom.c */
60 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
61 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
63 /* scsi-bus.c */
64 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
65 struct SCSIDeviceInfo {
66 DeviceInfo qdev;
67 scsi_qdev_initfn init;
68 void (*destroy)(SCSIDevice *s);
69 void (*free_req)(SCSIRequest *req);
70 int32_t (*send_command)(SCSIDevice *s, uint32_t tag, uint8_t *buf,
71 int lun);
72 void (*read_data)(SCSIDevice *s, uint32_t tag);
73 int (*write_data)(SCSIDevice *s, uint32_t tag);
74 void (*cancel_io)(SCSIDevice *s, uint32_t tag);
75 uint8_t *(*get_buf)(SCSIDevice *s, uint32_t tag);
78 struct SCSIBusOps {
79 void (*complete)(SCSIBus *bus, int reason, uint32_t tag, uint32_t arg);
82 struct SCSIBus {
83 BusState qbus;
84 int busnr;
86 int tcq, ndev;
87 const SCSIBusOps *ops;
89 SCSIDevice *devs[MAX_SCSI_DEVS];
92 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
93 const SCSIBusOps *ops);
94 void scsi_qdev_register(SCSIDeviceInfo *info);
96 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
98 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
101 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
102 int unit, bool removable);
103 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
105 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
106 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag);
107 void scsi_req_free(SCSIRequest *req);
108 void scsi_req_dequeue(SCSIRequest *req);
109 SCSIRequest *scsi_req_ref(SCSIRequest *req);
110 void scsi_req_unref(SCSIRequest *req);
112 int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
113 void scsi_req_print(SCSIRequest *req);
114 void scsi_req_data(SCSIRequest *req, int len);
115 void scsi_req_complete(SCSIRequest *req);
117 #endif