virtio: drop name parameter for virtio_init()

This patch drops the name parameter for the virtio_init function.

The pair between the numeric device ID and the string device ID
(name) of a virtio device already exists, but not in a way that
lets us map between them.

This patch lets us do this and removes the need for the name
parameter in the virtio_init function.

Signed-off-by: Jonah Palmer <jonah.palmer@oracle.com>
Message-Id: <1648819405-25696-2-git-send-email-jonah.palmer@oracle.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Jonah Palmer 2022-04-01 09:23:18 -04:00 committed by Michael S. Tsirkin
parent 503e355465
commit 3857cd5c7f
24 changed files with 77 additions and 43 deletions

View File

@ -216,7 +216,7 @@ static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
} }
v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size); virtio_init(vdev, VIRTIO_ID_9P, v->config_size);
v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
} }

View File

@ -491,7 +491,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, virtio_init(vdev, VIRTIO_ID_BLOCK,
sizeof(struct virtio_blk_config)); sizeof(struct virtio_blk_config));
s->virtqs = g_new(VirtQueue *, s->num_queues); s->virtqs = g_new(VirtQueue *, s->num_queues);

View File

@ -1206,7 +1206,7 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
virtio_blk_set_config_size(s, s->host_features); virtio_blk_set_config_size(s, s->host_features);
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, s->config_size); virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
s->blk = conf->conf.blk; s->blk = conf->conf.blk;
s->rq = NULL; s->rq = NULL;

View File

@ -1044,8 +1044,7 @@ static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
VIRTIO_CONSOLE_F_EMERG_WRITE)) { VIRTIO_CONSOLE_F_EMERG_WRITE)) {
config_size = offsetof(struct virtio_console_config, emerg_wr); config_size = offsetof(struct virtio_console_config, emerg_wr);
} }
virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE, virtio_init(vdev, VIRTIO_ID_CONSOLE, config_size);
config_size);
/* Spawn a new virtio-serial bus on which the ports will ride as devices */ /* Spawn a new virtio-serial bus on which the ports will ride as devices */
qbus_init(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS, qbus_init(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,

View File

@ -173,7 +173,7 @@ virtio_gpu_base_device_realize(DeviceState *qdev,
} }
g->virtio_config.num_scanouts = cpu_to_le32(g->conf.max_outputs); g->virtio_config.num_scanouts = cpu_to_le32(g->conf.max_outputs);
virtio_init(VIRTIO_DEVICE(g), "virtio-gpu", VIRTIO_ID_GPU, virtio_init(VIRTIO_DEVICE(g), VIRTIO_ID_GPU,
sizeof(struct virtio_gpu_config)); sizeof(struct virtio_gpu_config));
if (virtio_gpu_virgl_enabled(g->conf)) { if (virtio_gpu_virgl_enabled(g->conf)) {

View File

@ -257,8 +257,7 @@ static void virtio_input_device_realize(DeviceState *dev, Error **errp)
vinput->cfg_size += 8; vinput->cfg_size += 8;
assert(vinput->cfg_size <= sizeof(virtio_input_config)); assert(vinput->cfg_size <= sizeof(virtio_input_config));
virtio_init(vdev, "virtio-input", VIRTIO_ID_INPUT, virtio_init(vdev, VIRTIO_ID_INPUT, vinput->cfg_size);
vinput->cfg_size);
vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt); vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts); vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
} }

View File

@ -3393,7 +3393,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
} }
virtio_net_set_config_size(n, n->host_features); virtio_net_set_config_size(n, n->host_features);
virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); virtio_init(vdev, VIRTIO_ID_NET, n->config_size);
/* /*
* We set a lower limit on RX queue size to what it always was. * We set a lower limit on RX queue size to what it always was.

View File

@ -1013,8 +1013,7 @@ void virtio_scsi_common_realize(DeviceState *dev,
VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
int i; int i;
virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, virtio_init(vdev, VIRTIO_ID_SCSI, sizeof(VirtIOSCSIConfig));
sizeof(VirtIOSCSIConfig));
if (s->conf.num_queues == VIRTIO_SCSI_AUTO_NUM_QUEUES) { if (s->conf.num_queues == VIRTIO_SCSI_AUTO_NUM_QUEUES) {
s->conf.num_queues = 1; s->conf.num_queues = 1;

View File

@ -219,8 +219,7 @@ static void vuf_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "vhost-user-fs", VIRTIO_ID_FS, virtio_init(vdev, VIRTIO_ID_FS, sizeof(struct virtio_fs_config));
sizeof(struct virtio_fs_config));
/* Hiprio queue */ /* Hiprio queue */
fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);

View File

@ -14,11 +14,6 @@
#include "qemu/error-report.h" #include "qemu/error-report.h"
#include "standard-headers/linux/virtio_ids.h" #include "standard-headers/linux/virtio_ids.h"
/* Remove this once the header is updated in Linux kernel */
#ifndef VIRTIO_ID_I2C_ADAPTER
#define VIRTIO_ID_I2C_ADAPTER 34
#endif
static const int feature_bits[] = { static const int feature_bits[] = {
VIRTIO_I2C_F_ZERO_LENGTH_REQUEST, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
VHOST_INVALID_FEATURE_BIT VHOST_INVALID_FEATURE_BIT
@ -227,7 +222,7 @@ static void vu_i2c_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "vhost-user-i2c", VIRTIO_ID_I2C_ADAPTER, 0); virtio_init(vdev, VIRTIO_ID_I2C_ADAPTER, 0);
i2c->vhost_dev.nvqs = 1; i2c->vhost_dev.nvqs = 1;
i2c->vq = virtio_add_queue(vdev, 4, vu_i2c_handle_output); i2c->vq = virtio_add_queue(vdev, 4, vu_i2c_handle_output);

View File

@ -203,7 +203,7 @@ static void vu_rng_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "vhost-user-rng", VIRTIO_ID_RNG, 0); virtio_init(vdev, VIRTIO_ID_RNG, 0);
rng->req_vq = virtio_add_queue(vdev, 4, vu_rng_handle_output); rng->req_vq = virtio_add_queue(vdev, 4, vu_rng_handle_output);
if (!rng->req_vq) { if (!rng->req_vq) {

View File

@ -107,7 +107,7 @@ static void vuv_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
vhost_vsock_common_realize(vdev, "vhost-user-vsock"); vhost_vsock_common_realize(vdev);
vhost_dev_set_config_notifier(&vvc->vhost_dev, &vsock_ops); vhost_dev_set_config_notifier(&vvc->vhost_dev, &vsock_ops);

View File

@ -224,12 +224,11 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
return 0; return 0;
} }
void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) void vhost_vsock_common_realize(VirtIODevice *vdev)
{ {
VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
virtio_init(vdev, name, VIRTIO_ID_VSOCK, virtio_init(vdev, VIRTIO_ID_VSOCK, sizeof(struct virtio_vsock_config));
sizeof(struct virtio_vsock_config));
/* Receive and transmit queues belong to vhost */ /* Receive and transmit queues belong to vhost */
vvc->recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, vvc->recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,

View File

@ -169,7 +169,7 @@ static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
} }
} }
vhost_vsock_common_realize(vdev, "vhost-vsock"); vhost_vsock_common_realize(vdev);
ret = vhost_dev_init(&vvc->vhost_dev, (void *)(uintptr_t)vhostfd, ret = vhost_dev_init(&vvc->vhost_dev, (void *)(uintptr_t)vhostfd,
VHOST_BACKEND_TYPE_KERNEL, 0, errp); VHOST_BACKEND_TYPE_KERNEL, 0, errp);

View File

@ -882,8 +882,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
VirtIOBalloon *s = VIRTIO_BALLOON(dev); VirtIOBalloon *s = VIRTIO_BALLOON(dev);
int ret; int ret;
virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, virtio_init(vdev, VIRTIO_ID_BALLOON, virtio_balloon_config_size(s));
virtio_balloon_config_size(s));
ret = qemu_add_balloon_handler(virtio_balloon_to_target, ret = qemu_add_balloon_handler(virtio_balloon_to_target,
virtio_balloon_stat, s); virtio_balloon_stat, s);

View File

@ -810,7 +810,7 @@ static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "virtio-crypto", VIRTIO_ID_CRYPTO, vcrypto->config_size); virtio_init(vdev, VIRTIO_ID_CRYPTO, vcrypto->config_size);
vcrypto->curr_queues = 1; vcrypto->curr_queues = 1;
vcrypto->vqs = g_new0(VirtIOCryptoQueue, vcrypto->max_queues); vcrypto->vqs = g_new0(VirtIOCryptoQueue, vcrypto->max_queues);
for (i = 0; i < vcrypto->max_queues; i++) { for (i = 0; i < vcrypto->max_queues; i++) {

View File

@ -1033,8 +1033,7 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VirtIOIOMMU *s = VIRTIO_IOMMU(dev); VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU, virtio_init(vdev, VIRTIO_ID_IOMMU, sizeof(struct virtio_iommu_config));
sizeof(struct virtio_iommu_config));
memset(s->iommu_pcibus_by_bus_num, 0, sizeof(s->iommu_pcibus_by_bus_num)); memset(s->iommu_pcibus_by_bus_num, 0, sizeof(s->iommu_pcibus_by_bus_num));

View File

@ -867,8 +867,7 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp)
vmem->block_size; vmem->block_size;
vmem->bitmap = bitmap_new(vmem->bitmap_size); vmem->bitmap = bitmap_new(vmem->bitmap_size);
virtio_init(vdev, TYPE_VIRTIO_MEM, VIRTIO_ID_MEM, virtio_init(vdev, VIRTIO_ID_MEM, sizeof(struct virtio_mem_config));
sizeof(struct virtio_mem_config));
vmem->vq = virtio_add_queue(vdev, 128, virtio_mem_handle_request); vmem->vq = virtio_add_queue(vdev, 128, virtio_mem_handle_request);
host_memory_backend_set_mapped(vmem->memdev, true); host_memory_backend_set_mapped(vmem->memdev, true);

View File

@ -122,8 +122,7 @@ static void virtio_pmem_realize(DeviceState *dev, Error **errp)
} }
host_memory_backend_set_mapped(pmem->memdev, true); host_memory_backend_set_mapped(pmem->memdev, true);
virtio_init(vdev, TYPE_VIRTIO_PMEM, VIRTIO_ID_PMEM, virtio_init(vdev, VIRTIO_ID_PMEM, sizeof(struct virtio_pmem_config));
sizeof(struct virtio_pmem_config));
pmem->rq_vq = virtio_add_queue(vdev, 128, virtio_pmem_flush); pmem->rq_vq = virtio_add_queue(vdev, 128, virtio_pmem_flush);
} }

View File

@ -215,7 +215,7 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
return; return;
} }
virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0); virtio_init(vdev, VIRTIO_ID_RNG, 0);
vrng->vq = virtio_add_queue(vdev, 8, handle_input); vrng->vq = virtio_add_queue(vdev, 8, handle_input);
vrng->quota_remaining = vrng->conf.max_bytes; vrng->quota_remaining = vrng->conf.max_bytes;

View File

@ -132,6 +132,56 @@ struct VirtQueue
QLIST_ENTRY(VirtQueue) node; QLIST_ENTRY(VirtQueue) node;
}; };
const char *virtio_device_names[] = {
[VIRTIO_ID_NET] = "virtio-net",
[VIRTIO_ID_BLOCK] = "virtio-blk",
[VIRTIO_ID_CONSOLE] = "virtio-serial",
[VIRTIO_ID_RNG] = "virtio-rng",
[VIRTIO_ID_BALLOON] = "virtio-balloon",
[VIRTIO_ID_IOMEM] = "virtio-iomem",
[VIRTIO_ID_RPMSG] = "virtio-rpmsg",
[VIRTIO_ID_SCSI] = "virtio-scsi",
[VIRTIO_ID_9P] = "virtio-9p",
[VIRTIO_ID_MAC80211_WLAN] = "virtio-mac-wlan",
[VIRTIO_ID_RPROC_SERIAL] = "virtio-rproc-serial",
[VIRTIO_ID_CAIF] = "virtio-caif",
[VIRTIO_ID_MEMORY_BALLOON] = "virtio-mem-balloon",
[VIRTIO_ID_GPU] = "virtio-gpu",
[VIRTIO_ID_CLOCK] = "virtio-clk",
[VIRTIO_ID_INPUT] = "virtio-input",
[VIRTIO_ID_VSOCK] = "vhost-vsock",
[VIRTIO_ID_CRYPTO] = "virtio-crypto",
[VIRTIO_ID_SIGNAL_DIST] = "virtio-signal",
[VIRTIO_ID_PSTORE] = "virtio-pstore",
[VIRTIO_ID_IOMMU] = "virtio-iommu",
[VIRTIO_ID_MEM] = "virtio-mem",
[VIRTIO_ID_SOUND] = "virtio-sound",
[VIRTIO_ID_FS] = "virtio-user-fs",
[VIRTIO_ID_PMEM] = "virtio-pmem",
[VIRTIO_ID_RPMB] = "virtio-rpmb",
[VIRTIO_ID_MAC80211_HWSIM] = "virtio-mac-hwsim",
[VIRTIO_ID_VIDEO_ENCODER] = "virtio-vid-encoder",
[VIRTIO_ID_VIDEO_DECODER] = "virtio-vid-decoder",
[VIRTIO_ID_SCMI] = "virtio-scmi",
[VIRTIO_ID_NITRO_SEC_MOD] = "virtio-nitro-sec-mod",
[VIRTIO_ID_I2C_ADAPTER] = "vhost-user-i2c",
[VIRTIO_ID_WATCHDOG] = "virtio-watchdog",
[VIRTIO_ID_CAN] = "virtio-can",
[VIRTIO_ID_DMABUF] = "virtio-dmabuf",
[VIRTIO_ID_PARAM_SERV] = "virtio-param-serv",
[VIRTIO_ID_AUDIO_POLICY] = "virtio-audio-pol",
[VIRTIO_ID_BT] = "virtio-bluetooth",
[VIRTIO_ID_GPIO] = "virtio-gpio"
};
static const char *virtio_id_to_name(uint16_t device_id)
{
assert(device_id < G_N_ELEMENTS(virtio_device_names));
const char *name = virtio_device_names[device_id];
assert(name != NULL);
return name;
}
/* Called within call_rcu(). */ /* Called within call_rcu(). */
static void virtio_free_region_cache(VRingMemoryRegionCaches *caches) static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
{ {
@ -3207,8 +3257,7 @@ void virtio_instance_init_common(Object *proxy_obj, void *data,
qdev_alias_all_properties(vdev, proxy_obj); qdev_alias_all_properties(vdev, proxy_obj);
} }
void virtio_init(VirtIODevice *vdev, const char *name, void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
uint16_t device_id, size_t config_size)
{ {
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
@ -3237,7 +3286,7 @@ void virtio_init(VirtIODevice *vdev, const char *name,
vdev->vq[i].host_notifier_enabled = false; vdev->vq[i].host_notifier_enabled = false;
} }
vdev->name = name; vdev->name = virtio_id_to_name(device_id);
vdev->config_len = config_size; vdev->config_len = config_size;
if (vdev->config_len) { if (vdev->config_len) {
vdev->config = g_malloc0(config_size); vdev->config = g_malloc0(config_size);

View File

@ -44,7 +44,7 @@ int vhost_vsock_common_start(VirtIODevice *vdev);
void vhost_vsock_common_stop(VirtIODevice *vdev); void vhost_vsock_common_stop(VirtIODevice *vdev);
int vhost_vsock_common_pre_save(void *opaque); int vhost_vsock_common_pre_save(void *opaque);
int vhost_vsock_common_post_load(void *opaque, int version_id); int vhost_vsock_common_post_load(void *opaque, int version_id);
void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name); void vhost_vsock_common_realize(VirtIODevice *vdev);
void vhost_vsock_common_unrealize(VirtIODevice *vdev); void vhost_vsock_common_unrealize(VirtIODevice *vdev);
uint64_t vhost_vsock_common_get_features(VirtIODevice *vdev, uint64_t features, uint64_t vhost_vsock_common_get_features(VirtIODevice *vdev, uint64_t features,
Error **errp); Error **errp);

View File

@ -22,6 +22,7 @@
#include "sysemu/vhost-user-backend.h" #include "sysemu/vhost-user-backend.h"
#include "standard-headers/linux/virtio_gpu.h" #include "standard-headers/linux/virtio_gpu.h"
#include "standard-headers/linux/virtio_ids.h"
#include "qom/object.h" #include "qom/object.h"
#define TYPE_VIRTIO_GPU_BASE "virtio-gpu-base" #define TYPE_VIRTIO_GPU_BASE "virtio-gpu-base"
@ -37,8 +38,6 @@ OBJECT_DECLARE_SIMPLE_TYPE(VirtIOGPUGL, VIRTIO_GPU_GL)
#define TYPE_VHOST_USER_GPU "vhost-user-gpu" #define TYPE_VHOST_USER_GPU "vhost-user-gpu"
OBJECT_DECLARE_SIMPLE_TYPE(VhostUserGPU, VHOST_USER_GPU) OBJECT_DECLARE_SIMPLE_TYPE(VhostUserGPU, VHOST_USER_GPU)
#define VIRTIO_ID_GPU 16
struct virtio_gpu_simple_resource { struct virtio_gpu_simple_resource {
uint32_t resource_id; uint32_t resource_id;
uint32_t width; uint32_t width;

View File

@ -165,8 +165,8 @@ struct VirtioDeviceClass {
void virtio_instance_init_common(Object *proxy_obj, void *data, void virtio_instance_init_common(Object *proxy_obj, void *data,
size_t vdev_size, const char *vdev_name); size_t vdev_size, const char *vdev_name);
void virtio_init(VirtIODevice *vdev, const char *name, void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size);
uint16_t device_id, size_t config_size);
void virtio_cleanup(VirtIODevice *vdev); void virtio_cleanup(VirtIODevice *vdev);
void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3); void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3);