blob: 2fe8c120601b6005ae3ece50d9efa80f86db4f88 [file] [log] [blame]
Gerd Hoffmann1e376072009-11-26 15:33:47 +01001#ifndef QEMU_HW_SCSI_H
2#define QEMU_HW_SCSI_H
Gerd Hoffmann43b443b2009-10-30 09:54:00 +01003
4#include "qdev.h"
Gerd Hoffmann4c41d2e2009-11-26 15:33:48 +01005#include "block.h"
Gerd Hoffmann43b443b2009-10-30 09:54:00 +01006
Gerd Hoffmann29362eb2009-11-26 15:33:51 +01007#define SCSI_CMD_BUF_SIZE 16
8
Gerd Hoffmann43b443b2009-10-30 09:54:00 +01009/* scsi-disk.c */
10enum scsi_reason {
11 SCSI_REASON_DONE, /* Command complete. */
12 SCSI_REASON_DATA /* Transfer complete, more data required. */
13};
14
15typedef struct SCSIBus SCSIBus;
16typedef struct SCSIDevice SCSIDevice;
17typedef struct SCSIDeviceInfo SCSIDeviceInfo;
18typedef void (*scsi_completionfn)(SCSIBus *bus, int reason, uint32_t tag,
19 uint32_t arg);
20
Gerd Hoffmann97a06432009-11-26 15:33:57 +010021enum SCSIXferMode {
22 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
23 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
24 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
25};
26
Gerd Hoffmann37659e52009-11-26 15:33:58 +010027typedef struct SCSISense {
28 uint8_t key;
29} SCSISense;
30
Gerd Hoffmann4c41d2e2009-11-26 15:33:48 +010031typedef struct SCSIRequest {
32 SCSIBus *bus;
33 SCSIDevice *dev;
34 uint32_t tag;
Gerd Hoffmann89b08ae2009-11-26 15:33:50 +010035 uint32_t lun;
Gerd Hoffmanned3a34a2009-11-26 15:34:00 +010036 uint32_t status;
Gerd Hoffmann29362eb2009-11-26 15:33:51 +010037 struct {
38 uint8_t buf[SCSI_CMD_BUF_SIZE];
39 int len;
Gerd Hoffmann2ec749c2009-11-26 15:33:55 +010040 size_t xfer;
41 uint64_t lba;
Gerd Hoffmann97a06432009-11-26 15:33:57 +010042 enum SCSIXferMode mode;
Gerd Hoffmann29362eb2009-11-26 15:33:51 +010043 } cmd;
Gerd Hoffmann4c41d2e2009-11-26 15:33:48 +010044 BlockDriverAIOCB *aiocb;
Gerd Hoffmann9af99d92009-11-26 15:33:49 +010045 QTAILQ_ENTRY(SCSIRequest) next;
Gerd Hoffmann4c41d2e2009-11-26 15:33:48 +010046} SCSIRequest;
47
Gerd Hoffmann43b443b2009-10-30 09:54:00 +010048struct SCSIDevice
49{
50 DeviceState qdev;
51 uint32_t id;
Gerd Hoffmann251882b2009-11-26 15:33:59 +010052 DriveInfo *dinfo;
Gerd Hoffmann43b443b2009-10-30 09:54:00 +010053 SCSIDeviceInfo *info;
Gerd Hoffmann9af99d92009-11-26 15:33:49 +010054 QTAILQ_HEAD(, SCSIRequest) requests;
Gerd Hoffmannb07995e2009-11-26 15:33:52 +010055 int blocksize;
Gerd Hoffmann91376652009-11-26 15:33:54 +010056 int type;
Gerd Hoffmann37659e52009-11-26 15:33:58 +010057 struct SCSISense sense;
Gerd Hoffmann43b443b2009-10-30 09:54:00 +010058};
59
60/* cdrom.c */
61int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
62int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
63
64/* scsi-bus.c */
65typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
66struct SCSIDeviceInfo {
67 DeviceInfo qdev;
68 scsi_qdev_initfn init;
69 void (*destroy)(SCSIDevice *s);
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);
76};
77
78typedef void (*SCSIAttachFn)(DeviceState *host, BlockDriverState *bdrv,
79 int unit);
80struct SCSIBus {
81 BusState qbus;
82 int busnr;
83
84 int tcq, ndev;
85 scsi_completionfn complete;
86
87 SCSIDevice *devs[8];
88};
89
90void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
91 scsi_completionfn complete);
92void scsi_qdev_register(SCSIDeviceInfo *info);
93
94static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
95{
96 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
97}
98
99SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, DriveInfo *dinfo, int unit);
100void scsi_bus_legacy_handle_cmdline(SCSIBus *bus);
101
Gerd Hoffmann37659e52009-11-26 15:33:58 +0100102void scsi_dev_clear_sense(SCSIDevice *dev);
103void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key);
104
Gerd Hoffmann89b08ae2009-11-26 15:33:50 +0100105SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun);
106SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag);
107void scsi_req_free(SCSIRequest *req);
Gerd Hoffmann37659e52009-11-26 15:33:58 +0100108
Gerd Hoffmann2ec749c2009-11-26 15:33:55 +0100109int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
Gerd Hoffmanned3a34a2009-11-26 15:34:00 +0100110void scsi_req_complete(SCSIRequest *req);
Gerd Hoffmann89b08ae2009-11-26 15:33:50 +0100111
Gerd Hoffmann43b443b2009-10-30 09:54:00 +0100112#endif