Skip to content

Commit

Permalink
Use bool for callbacks in virtIO devices
Browse files Browse the repository at this point in the history
We already actually cast the return values to 'bool' in `src/mmio.c`
anyways, this just makes things a bit more consistent.

Signed-off-by: Ivan Velickovic <[email protected]>
  • Loading branch information
Ivan-Velickovic committed Jul 30, 2024
1 parent c2a83dc commit f3c6a33
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
2 changes: 1 addition & 1 deletion include/libvmm/virtio/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ bool virtio_mmio_console_init(struct virtio_console_device *console,
serial_queue_handle_t *txq,
int tx_ch);

int virtio_console_handle_rx(struct virtio_console_device *console);
bool virtio_console_handle_rx(struct virtio_console_device *console);
10 changes: 5 additions & 5 deletions include/libvmm/virtio/mmio.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ typedef struct virtio_emul_funs {
void (*device_reset)(struct virtio_device *dev);

// REG_VIRTIO_MMIO_DEVICE_FEATURES related operations
int (*get_device_features)(struct virtio_device *dev, uint32_t *features);
int (*set_driver_features)(struct virtio_device *dev, uint32_t features);
bool (*get_device_features)(struct virtio_device *dev, uint32_t *features);
bool (*set_driver_features)(struct virtio_device *dev, uint32_t features);

// REG_VIRTIO_MMIO_CONFIG related operations
int (*get_device_config)(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val);
int (*set_device_config)(struct virtio_device *dev, uint32_t offset, uint32_t val);
int (*queue_notify)(struct virtio_device *dev);
bool (*get_device_config)(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val);
bool (*set_device_config)(struct virtio_device *dev, uint32_t offset, uint32_t val);
bool (*queue_notify)(struct virtio_device *dev);
} virtio_device_funs_t;

// infomation you need for manipulating MMIO Device Register Layout
Expand Down
27 changes: 15 additions & 12 deletions src/virtio/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void virtio_blk_mmio_reset(struct virtio_device *dev)
dev->vqs[VIRTIO_BLK_DEFAULT_VIRTQ].last_idx = 0;
}

static int virtio_blk_mmio_get_device_features(struct virtio_device *dev, uint32_t *features)
static bool virtio_blk_mmio_get_device_features(struct virtio_device *dev, uint32_t *features)
{
if (dev->data.Status & VIRTIO_CONFIG_S_FEATURES_OK) {
LOG_BLOCK_ERR("driver somehow wants to read device features after FEATURES_OK\n");
Expand All @@ -52,17 +52,18 @@ static int virtio_blk_mmio_get_device_features(struct virtio_device *dev, uint32
default:
LOG_BLOCK_ERR("driver sets DeviceFeaturesSel to 0x%x, which doesn't make sense\n",
dev->data.DeviceFeaturesSel);
return 0;
return false;
}
return 1;

return true;
}

static int virtio_blk_mmio_set_driver_features(struct virtio_device *dev, uint32_t features)
static bool virtio_blk_mmio_set_driver_features(struct virtio_device *dev, uint32_t features)
{
/* According to virtio initialisation protocol,
this should check what device features were set, and return the subset of features understood
by the driver. */
int success = 1;
bool success = false;

uint32_t device_features = 0;
device_features = device_features | BIT_LOW(VIRTIO_BLK_F_FLUSH);
Expand All @@ -80,7 +81,7 @@ static int virtio_blk_mmio_set_driver_features(struct virtio_device *dev, uint32
default:
LOG_BLOCK_ERR("driver sets DriverFeaturesSel to 0x%x, which doesn't make sense\n",
dev->data.DriverFeaturesSel);
success = 0;
return false;
}

if (success) {
Expand All @@ -90,7 +91,7 @@ static int virtio_blk_mmio_set_driver_features(struct virtio_device *dev, uint32
return success;
}

static int virtio_blk_mmio_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val)
static bool virtio_blk_mmio_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val)
{
struct virtio_blk_device *state = device_state(dev);

Expand All @@ -100,10 +101,11 @@ static int virtio_blk_mmio_get_device_config(struct virtio_device *dev, uint32_t
*ret_val = *config_field_addr;
LOG_BLOCK("get device config with base_addr 0x%x and field_address 0x%x has value %d\n",
config_base_addr, config_field_addr, *ret_val);
return 1;

return true;
}

static int virtio_blk_mmio_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t val)
static bool virtio_blk_mmio_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t val)
{
struct virtio_blk_device *state = device_state(dev);

Expand All @@ -113,7 +115,8 @@ static int virtio_blk_mmio_set_device_config(struct virtio_device *dev, uint32_t
*config_field_addr = val;
LOG_BLOCK("set device config with base_addr 0x%x and field_address 0x%x with value %d\n",
config_base_addr, config_field_addr, val);
return 1;

return true;
}

static void virtio_blk_used_buffer(struct virtio_device *dev, uint16_t desc)
Expand Down Expand Up @@ -183,7 +186,7 @@ static bool sddf_make_req_check(struct virtio_blk_device *state, uint16_t sddf_c
return true;
}

static int virtio_blk_mmio_queue_notify(struct virtio_device *dev)
static bool virtio_blk_mmio_queue_notify(struct virtio_device *dev)
{
/* If multiqueue feature bit negotiated, should read which queue from dev->QueueNotify,
but for now we just assume it's the one and only default queue */
Expand Down Expand Up @@ -362,7 +365,7 @@ static int virtio_blk_mmio_queue_notify(struct virtio_device *dev)
/* Update virtq index to the next available request to be handled */
vq->last_idx = idx;

int success = 1;
bool success = true;

/* If any request has to be dropped due to any number of reasons, we inject an interrupt */
if (has_dropped) {
Expand Down
25 changes: 13 additions & 12 deletions src/virtio/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void virtio_console_reset(struct virtio_device *dev)
}
}

static int virtio_console_get_device_features(struct virtio_device *dev, uint32_t *features)
static bool virtio_console_get_device_features(struct virtio_device *dev, uint32_t *features)
{
LOG_CONSOLE("operation: get device features\n");

Expand All @@ -56,23 +56,24 @@ static int virtio_console_get_device_features(struct virtio_device *dev, uint32_
break;
default:
LOG_CONSOLE_ERR("driver sets DeviceFeaturesSel to 0x%x, which doesn't make sense\n", dev->data.DeviceFeaturesSel);
return 0;
return false;
}

return 1;
return true;
}

static int virtio_console_set_driver_features(struct virtio_device *dev, uint32_t features)
static bool virtio_console_set_driver_features(struct virtio_device *dev, uint32_t features)
{
LOG_CONSOLE("operation: set driver features\n");
virtio_console_features_print(features);

int success = 1;
bool success = false;

switch (dev->data.DriverFeaturesSel) {
// feature bits 0 to 31
case 0:
/* No low feature bits to check */
/* We do not offer any features in the first 32-bit bits */
success = (features == 0);
break;
// features bits 32 to 63
case 1:
Expand All @@ -91,19 +92,19 @@ static int virtio_console_set_driver_features(struct virtio_device *dev, uint32_
return success;
}

static int virtio_console_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *config)
static bool virtio_console_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *config)
{
LOG_CONSOLE("operation: get device config\n");
return -1;
return false;
}

static int virtio_console_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t config)
static bool virtio_console_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t config)
{
LOG_CONSOLE("operation: set device config\n");
return -1;
return false;
}

static int virtio_console_handle_tx(struct virtio_device *dev)
static bool virtio_console_handle_tx(struct virtio_device *dev)
{
LOG_CONSOLE("operation: handle transmit\n");
// @ivanv: we need to check the pre-conditions before doing anything. e.g check
Expand Down Expand Up @@ -169,7 +170,7 @@ static int virtio_console_handle_tx(struct virtio_device *dev)
return true;
}

int virtio_console_handle_rx(struct virtio_console_device *console)
bool virtio_console_handle_rx(struct virtio_console_device *console)
{
LOG_CONSOLE("operation: handle rx\n");
assert(console->virtio_device.num_vqs > RX_QUEUE);
Expand Down
27 changes: 14 additions & 13 deletions src/virtio/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void virtio_snd_mmio_reset(struct virtio_device *dev)
}
}

static int virtio_snd_mmio_get_device_features(struct virtio_device *dev, uint32_t *features)
static bool virtio_snd_mmio_get_device_features(struct virtio_device *dev, uint32_t *features)
{
switch (dev->data.DeviceFeaturesSel) {
case 0:
Expand All @@ -50,14 +50,15 @@ static int virtio_snd_mmio_get_device_features(struct virtio_device *dev, uint32
break;
default:
LOG_SOUND_ERR("driver sets DeviceFeaturesSel to 0x%x, which doesn't make sense\n", dev->data.DeviceFeaturesSel);
return 0;
return false;
}
return 1;

return true;
}

static int virtio_snd_mmio_set_driver_features(struct virtio_device *dev, uint32_t features)
static bool virtio_snd_mmio_set_driver_features(struct virtio_device *dev, uint32_t features)
{
int success;
bool success = false;
switch (dev->data.DriverFeaturesSel) {
// feature bits 0 to 31
case 0:
Expand All @@ -69,7 +70,7 @@ static int virtio_snd_mmio_set_driver_features(struct virtio_device *dev, uint32
break;
default:
LOG_SOUND_ERR("driver sets DriverFeaturesSel to 0x%x, which doesn't make sense\n", dev->data.DriverFeaturesSel);
success = 0;
return false;
}

if (success) {
Expand All @@ -79,21 +80,21 @@ static int virtio_snd_mmio_set_driver_features(struct virtio_device *dev, uint32
return success;
}

static int virtio_snd_mmio_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val)
static bool virtio_snd_mmio_get_device_config(struct virtio_device *dev, uint32_t offset, uint32_t *ret_val)
{
struct virtio_snd_device *state = device_state(dev);
uintptr_t config_base_addr = (uintptr_t)&state->config;
uintptr_t config_field_offset = (uintptr_t)(offset - REG_VIRTIO_MMIO_CONFIG);
uint32_t *config_field_addr = (uint32_t *)(config_base_addr + config_field_offset);
*ret_val = *config_field_addr;

return 1;
return true;
}

static int virtio_snd_mmio_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t val)
static bool virtio_snd_mmio_set_device_config(struct virtio_device *dev, uint32_t offset, uint32_t val)
{
LOG_SOUND_ERR("Not allowed to change sound config.");
return 0;
return false;
}

static const char *code_to_str(uint32_t code)
Expand Down Expand Up @@ -609,13 +610,13 @@ static void handle_virtq(struct virtio_device *dev,
vq->last_idx = idx;
}

static int virtio_snd_mmio_queue_notify(struct virtio_device *dev)
static bool virtio_snd_mmio_queue_notify(struct virtio_device *dev)
{
struct virtio_snd_device *state = device_state(dev);

if (dev->data.QueueSel > VIRTIO_SND_NUM_VIRTQ) {
LOG_SOUND_ERR("Invalid queue\n");
return 0;
return false;
}

bool notify_driver = false;
Expand All @@ -630,7 +631,7 @@ static int virtio_snd_mmio_queue_notify(struct virtio_device *dev)
virtio_snd_respond(dev);
}

return 1;
return true;
}

static virtio_device_funs_t functions = {
Expand Down

0 comments on commit f3c6a33

Please sign in to comment.