mirror of https://github.com/xemu-project/xemu.git
scsi: move request lists to QTAILQ.
Changes: * Move from open-coded lists to QTAILQ macros. * Move the struct elements to the common data structures (SCSIDevice + SCSIRequest). * Drop free request pools. * Fix request cleanup in the destroy callback. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
4c41d2ef5f
commit
9af99d980e
|
@ -50,6 +50,7 @@ static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
|
||||||
bus->devs[dev->id] = dev;
|
bus->devs[dev->id] = dev;
|
||||||
|
|
||||||
dev->info = info;
|
dev->info = info;
|
||||||
|
QTAILQ_INIT(&dev->requests);
|
||||||
rc = dev->info->init(dev);
|
rc = dev->info->init(dev);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
bus->devs[dev->id] = NULL;
|
bus->devs[dev->id] = NULL;
|
||||||
|
|
|
@ -55,7 +55,6 @@ typedef struct SCSIDiskReq {
|
||||||
uint32_t sector_count;
|
uint32_t sector_count;
|
||||||
struct iovec iov;
|
struct iovec iov;
|
||||||
QEMUIOVector qiov;
|
QEMUIOVector qiov;
|
||||||
struct SCSIDiskReq *next;
|
|
||||||
uint32_t status;
|
uint32_t status;
|
||||||
} SCSIDiskReq;
|
} SCSIDiskReq;
|
||||||
|
|
||||||
|
@ -63,7 +62,6 @@ struct SCSIDiskState
|
||||||
{
|
{
|
||||||
SCSIDevice qdev;
|
SCSIDevice qdev;
|
||||||
DriveInfo *dinfo;
|
DriveInfo *dinfo;
|
||||||
SCSIDiskReq *requests;
|
|
||||||
/* The qemu block layer uses a fixed 512 byte sector size.
|
/* The qemu block layer uses a fixed 512 byte sector size.
|
||||||
This is the number of 512 byte blocks in a single scsi sector. */
|
This is the number of 512 byte blocks in a single scsi sector. */
|
||||||
int cluster_size;
|
int cluster_size;
|
||||||
|
@ -73,64 +71,37 @@ struct SCSIDiskState
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Global pool of SCSIRequest structures. */
|
|
||||||
static SCSIDiskReq *free_requests = NULL;
|
|
||||||
|
|
||||||
static SCSIDiskReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
|
static SCSIDiskReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
|
||||||
{
|
{
|
||||||
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d);
|
|
||||||
SCSIDiskReq *r;
|
SCSIDiskReq *r;
|
||||||
|
|
||||||
if (free_requests) {
|
r = qemu_mallocz(sizeof(SCSIDiskReq));
|
||||||
r = free_requests;
|
|
||||||
free_requests = r->next;
|
|
||||||
} else {
|
|
||||||
r = qemu_malloc(sizeof(SCSIDiskReq));
|
|
||||||
r->iov.iov_base = qemu_memalign(512, SCSI_DMA_BUF_SIZE);
|
r->iov.iov_base = qemu_memalign(512, SCSI_DMA_BUF_SIZE);
|
||||||
}
|
|
||||||
r->req.bus = scsi_bus_from_device(d);
|
r->req.bus = scsi_bus_from_device(d);
|
||||||
r->req.dev = d;
|
r->req.dev = d;
|
||||||
r->req.tag = tag;
|
r->req.tag = tag;
|
||||||
r->sector_count = 0;
|
|
||||||
r->iov.iov_len = 0;
|
|
||||||
r->req.aiocb = NULL;
|
|
||||||
r->status = 0;
|
|
||||||
|
|
||||||
r->next = s->requests;
|
QTAILQ_INSERT_TAIL(&d->requests, &r->req, next);
|
||||||
s->requests = r;
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scsi_remove_request(SCSIDiskReq *r)
|
static void scsi_remove_request(SCSIDiskReq *r)
|
||||||
{
|
{
|
||||||
SCSIDiskReq *last;
|
qemu_free(r->iov.iov_base);
|
||||||
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
|
QTAILQ_REMOVE(&r->req.dev->requests, &r->req, next);
|
||||||
|
qemu_free(r);
|
||||||
if (s->requests == r) {
|
|
||||||
s->requests = r->next;
|
|
||||||
} else {
|
|
||||||
last = s->requests;
|
|
||||||
while (last && last->next != r)
|
|
||||||
last = last->next;
|
|
||||||
if (last) {
|
|
||||||
last->next = r->next;
|
|
||||||
} else {
|
|
||||||
BADF("Orphaned request\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r->next = free_requests;
|
|
||||||
free_requests = r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SCSIDiskReq *scsi_find_request(SCSIDiskState *s, uint32_t tag)
|
static SCSIDiskReq *scsi_find_request(SCSIDiskState *s, uint32_t tag)
|
||||||
{
|
{
|
||||||
SCSIDiskReq *r;
|
SCSIRequest *req;
|
||||||
|
|
||||||
r = s->requests;
|
QTAILQ_FOREACH(req, &s->qdev.requests, next) {
|
||||||
while (r && r->req.tag != tag)
|
if (req->tag == tag) {
|
||||||
r = r->next;
|
return DO_UPCAST(SCSIDiskReq, req, req);
|
||||||
|
}
|
||||||
return r;
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper function for command completion. */
|
/* Helper function for command completion. */
|
||||||
|
@ -310,17 +281,18 @@ static int scsi_write_data(SCSIDevice *d, uint32_t tag)
|
||||||
static void scsi_dma_restart_bh(void *opaque)
|
static void scsi_dma_restart_bh(void *opaque)
|
||||||
{
|
{
|
||||||
SCSIDiskState *s = opaque;
|
SCSIDiskState *s = opaque;
|
||||||
SCSIDiskReq *r = s->requests;
|
SCSIRequest *req;
|
||||||
|
SCSIDiskReq *r;
|
||||||
|
|
||||||
qemu_bh_delete(s->bh);
|
qemu_bh_delete(s->bh);
|
||||||
s->bh = NULL;
|
s->bh = NULL;
|
||||||
|
|
||||||
while (r) {
|
QTAILQ_FOREACH(req, &s->qdev.requests, next) {
|
||||||
|
r = DO_UPCAST(SCSIDiskReq, req, req);
|
||||||
if (r->status & SCSI_REQ_STATUS_RETRY) {
|
if (r->status & SCSI_REQ_STATUS_RETRY) {
|
||||||
r->status &= ~SCSI_REQ_STATUS_RETRY;
|
r->status &= ~SCSI_REQ_STATUS_RETRY;
|
||||||
scsi_write_request(r);
|
scsi_write_request(r);
|
||||||
}
|
}
|
||||||
r = r->next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -959,7 +931,12 @@ static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
|
||||||
static void scsi_destroy(SCSIDevice *dev)
|
static void scsi_destroy(SCSIDevice *dev)
|
||||||
{
|
{
|
||||||
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
|
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
|
||||||
|
SCSIDiskReq *r;
|
||||||
|
|
||||||
|
while (!QTAILQ_EMPTY(&s->qdev.requests)) {
|
||||||
|
r = DO_UPCAST(SCSIDiskReq, req, QTAILQ_FIRST(&s->qdev.requests));
|
||||||
|
scsi_remove_request(r);
|
||||||
|
}
|
||||||
drive_uninit(s->dinfo);
|
drive_uninit(s->dinfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,6 @@ typedef struct SCSIGenericState SCSIGenericState;
|
||||||
|
|
||||||
typedef struct SCSIGenericReq {
|
typedef struct SCSIGenericReq {
|
||||||
SCSIRequest req;
|
SCSIRequest req;
|
||||||
struct SCSIGenericReq *next;
|
|
||||||
uint8_t cmd[SCSI_CMD_BUF_SIZE];
|
uint8_t cmd[SCSI_CMD_BUF_SIZE];
|
||||||
int cmdlen;
|
int cmdlen;
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
|
@ -68,7 +67,6 @@ typedef struct SCSIGenericReq {
|
||||||
struct SCSIGenericState
|
struct SCSIGenericState
|
||||||
{
|
{
|
||||||
SCSIDevice qdev;
|
SCSIDevice qdev;
|
||||||
SCSIGenericReq *requests;
|
|
||||||
DriveInfo *dinfo;
|
DriveInfo *dinfo;
|
||||||
int type;
|
int type;
|
||||||
int blocksize;
|
int blocksize;
|
||||||
|
@ -78,68 +76,36 @@ struct SCSIGenericState
|
||||||
uint8_t senselen;
|
uint8_t senselen;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Global pool of SCSIGenericReq structures. */
|
|
||||||
static SCSIGenericReq *free_requests = NULL;
|
|
||||||
|
|
||||||
static SCSIGenericReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
|
static SCSIGenericReq *scsi_new_request(SCSIDevice *d, uint32_t tag)
|
||||||
{
|
{
|
||||||
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
|
|
||||||
SCSIGenericReq *r;
|
SCSIGenericReq *r;
|
||||||
|
|
||||||
if (free_requests) {
|
r = qemu_mallocz(sizeof(SCSIGenericReq));
|
||||||
r = free_requests;
|
|
||||||
free_requests = r->next;
|
|
||||||
} else {
|
|
||||||
r = qemu_malloc(sizeof(SCSIGenericReq));
|
|
||||||
r->buf = NULL;
|
|
||||||
r->buflen = 0;
|
|
||||||
}
|
|
||||||
r->req.bus = scsi_bus_from_device(d);
|
r->req.bus = scsi_bus_from_device(d);
|
||||||
r->req.dev = d;
|
r->req.dev = d;
|
||||||
r->req.tag = tag;
|
r->req.tag = tag;
|
||||||
memset(r->cmd, 0, sizeof(r->cmd));
|
|
||||||
memset(&r->io_header, 0, sizeof(r->io_header));
|
|
||||||
r->cmdlen = 0;
|
|
||||||
r->len = 0;
|
|
||||||
r->req.aiocb = NULL;
|
|
||||||
|
|
||||||
/* link */
|
QTAILQ_INSERT_TAIL(&d->requests, &r->req, next);
|
||||||
|
|
||||||
r->next = s->requests;
|
|
||||||
s->requests = r;
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scsi_remove_request(SCSIGenericReq *r)
|
static void scsi_remove_request(SCSIGenericReq *r)
|
||||||
{
|
{
|
||||||
SCSIGenericReq *last;
|
qemu_free(r->buf);
|
||||||
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, r->req.dev);
|
QTAILQ_REMOVE(&r->req.dev->requests, &r->req, next);
|
||||||
|
qemu_free(r);
|
||||||
if (s->requests == r) {
|
|
||||||
s->requests = r->next;
|
|
||||||
} else {
|
|
||||||
last = s->requests;
|
|
||||||
while (last && last->next != r)
|
|
||||||
last = last->next;
|
|
||||||
if (last) {
|
|
||||||
last->next = r->next;
|
|
||||||
} else {
|
|
||||||
BADF("Orphaned request\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r->next = free_requests;
|
|
||||||
free_requests = r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SCSIGenericReq *scsi_find_request(SCSIGenericState *s, uint32_t tag)
|
static SCSIGenericReq *scsi_find_request(SCSIGenericState *s, uint32_t tag)
|
||||||
{
|
{
|
||||||
SCSIGenericReq *r;
|
SCSIRequest *req;
|
||||||
|
|
||||||
r = s->requests;
|
QTAILQ_FOREACH(req, &s->qdev.requests, next) {
|
||||||
while (r && r->req.tag != tag)
|
if (req->tag == tag) {
|
||||||
r = r->next;
|
return DO_UPCAST(SCSIGenericReq, req, req);
|
||||||
|
}
|
||||||
return r;
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper function for command completion. */
|
/* Helper function for command completion. */
|
||||||
|
@ -653,22 +619,12 @@ static int get_stream_blocksize(BlockDriverState *bdrv)
|
||||||
static void scsi_destroy(SCSIDevice *d)
|
static void scsi_destroy(SCSIDevice *d)
|
||||||
{
|
{
|
||||||
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
|
SCSIGenericState *s = DO_UPCAST(SCSIGenericState, qdev, d);
|
||||||
SCSIGenericReq *r, *n;
|
SCSIGenericReq *r;
|
||||||
|
|
||||||
r = s->requests;
|
while (!QTAILQ_EMPTY(&s->qdev.requests)) {
|
||||||
while (r) {
|
r = DO_UPCAST(SCSIGenericReq, req, QTAILQ_FIRST(&s->qdev.requests));
|
||||||
n = r->next;
|
scsi_remove_request(r);
|
||||||
qemu_free(r);
|
|
||||||
r = n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r = free_requests;
|
|
||||||
while (r) {
|
|
||||||
n = r->next;
|
|
||||||
qemu_free(r);
|
|
||||||
r = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
drive_uninit(s->dinfo);
|
drive_uninit(s->dinfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ typedef struct SCSIRequest {
|
||||||
SCSIDevice *dev;
|
SCSIDevice *dev;
|
||||||
uint32_t tag;
|
uint32_t tag;
|
||||||
BlockDriverAIOCB *aiocb;
|
BlockDriverAIOCB *aiocb;
|
||||||
|
QTAILQ_ENTRY(SCSIRequest) next;
|
||||||
} SCSIRequest;
|
} SCSIRequest;
|
||||||
|
|
||||||
struct SCSIDevice
|
struct SCSIDevice
|
||||||
|
@ -28,6 +29,7 @@ struct SCSIDevice
|
||||||
DeviceState qdev;
|
DeviceState qdev;
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
SCSIDeviceInfo *info;
|
SCSIDeviceInfo *info;
|
||||||
|
QTAILQ_HEAD(, SCSIRequest) requests;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* cdrom.c */
|
/* cdrom.c */
|
||||||
|
|
Loading…
Reference in New Issue