提交 20764be0 编写于 作者: S Stefano Garzarella 提交者: Stefan Hajnoczi

virtio-blk: set config size depending on the features enabled

Starting from DISABLE and WRITE_ZEROES features, we use an array of
VirtIOFeature (as virtio-net) to properly set the config size
depending on the features enabled.
Reviewed-by: NStefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: NStefano Garzarella <sgarzare@redhat.com>
Message-id: 20190221103314.58500-6-sgarzare@redhat.com
Message-Id: <20190221103314.58500-6-sgarzare@redhat.com>
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
上级 ba550851
...@@ -28,9 +28,28 @@ ...@@ -28,9 +28,28 @@
#include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-bus.h"
#include "hw/virtio/virtio-access.h" #include "hw/virtio/virtio-access.h"
/* We don't support discard yet, hide associated config fields. */ /* Config size before the discard support (hide associated config fields) */
#define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \ #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \
max_discard_sectors) max_discard_sectors)
/*
* Starting from the discard feature, we can use this array to properly
* set the config size depending on the features enabled.
*/
static VirtIOFeature feature_sizes[] = {
{.flags = 1ULL << VIRTIO_BLK_F_DISCARD,
.end = virtio_endof(struct virtio_blk_config, discard_sector_alignment)},
{.flags = 1ULL << VIRTIO_BLK_F_WRITE_ZEROES,
.end = virtio_endof(struct virtio_blk_config, write_zeroes_may_unmap)},
{}
};
static void virtio_blk_set_config_size(VirtIOBlock *s, uint64_t host_features)
{
s->config_size = MAX(VIRTIO_BLK_CFG_SIZE,
virtio_feature_get_config_size(feature_sizes, host_features));
assert(s->config_size <= sizeof(struct virtio_blk_config));
}
static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq, static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
VirtIOBlockReq *req) VirtIOBlockReq *req)
...@@ -763,8 +782,7 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) ...@@ -763,8 +782,7 @@ static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
blkcfg.alignment_offset = 0; blkcfg.alignment_offset = 0;
blkcfg.wce = blk_enable_write_cache(s->blk); blkcfg.wce = blk_enable_write_cache(s->blk);
virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues); virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
memcpy(config, &blkcfg, VIRTIO_BLK_CFG_SIZE); memcpy(config, &blkcfg, s->config_size);
QEMU_BUILD_BUG_ON(VIRTIO_BLK_CFG_SIZE > sizeof(blkcfg));
} }
static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
...@@ -772,8 +790,7 @@ static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) ...@@ -772,8 +790,7 @@ static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
VirtIOBlock *s = VIRTIO_BLK(vdev); VirtIOBlock *s = VIRTIO_BLK(vdev);
struct virtio_blk_config blkcfg; struct virtio_blk_config blkcfg;
memcpy(&blkcfg, config, VIRTIO_BLK_CFG_SIZE); memcpy(&blkcfg, config, s->config_size);
QEMU_BUILD_BUG_ON(VIRTIO_BLK_CFG_SIZE > sizeof(blkcfg));
aio_context_acquire(blk_get_aio_context(s->blk)); aio_context_acquire(blk_get_aio_context(s->blk));
blk_set_enable_write_cache(s->blk, blkcfg.wce != 0); blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
...@@ -956,7 +973,9 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp) ...@@ -956,7 +973,9 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, VIRTIO_BLK_CFG_SIZE); virtio_blk_set_config_size(s, s->host_features);
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, s->config_size);
s->blk = conf->conf.blk; s->blk = conf->conf.blk;
s->rq = NULL; s->rq = NULL;
......
...@@ -56,6 +56,7 @@ typedef struct VirtIOBlock { ...@@ -56,6 +56,7 @@ typedef struct VirtIOBlock {
bool dataplane_started; bool dataplane_started;
struct VirtIOBlockDataPlane *dataplane; struct VirtIOBlockDataPlane *dataplane;
uint64_t host_features; uint64_t host_features;
size_t config_size;
} VirtIOBlock; } VirtIOBlock;
typedef struct VirtIOBlockReq { typedef struct VirtIOBlockReq {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册