Skip to content

Commit

Permalink
rpmsg_virtio: Use VIRTIO_ROLE_IS_{DRIVER,DEVICE}() macros
Browse files Browse the repository at this point in the history
Now that we match role to VIRTIO_DEV_{DRIVER,DEVICE} we can replace
all these double check if statements with the role checking macro
already used in all other files.

Signed-off-by: Andrew Davis <[email protected]>
  • Loading branch information
glneo committed Apr 23, 2024
1 parent 6f9020a commit dd06ed9
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions lib/rpmsg/rpmsg_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev,
void *buffer, uint32_t len,
uint16_t idx)
{
unsigned int role = rpmsg_virtio_get_role(rvdev);
int ret;

BUFFER_INVALIDATE(buffer, len);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
struct virtqueue_buf vqbuf;

(void)idx;
Expand All @@ -125,7 +124,7 @@ static void rpmsg_virtio_return_buffer(struct rpmsg_virtio_device *rvdev,
RPMSG_ASSERT(ret == VQUEUE_SUCCESS, "add buffer failed\r\n");
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
(void)buffer;
ret = virtqueue_add_consumed_buffer(rvdev->rvq, idx, len);
RPMSG_ASSERT(ret == VQUEUE_SUCCESS, "add consumed buffer failed\r\n");
Expand All @@ -148,11 +147,9 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev,
void *buffer, uint32_t len,
uint16_t idx)
{
unsigned int role = rpmsg_virtio_get_role(rvdev);

BUFFER_FLUSH(buffer, len);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
struct virtqueue_buf vqbuf;
(void)idx;

Expand All @@ -162,7 +159,7 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev,
return virtqueue_add_buffer(rvdev->svq, &vqbuf, 1, 0, buffer);
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
(void)buffer;
return virtqueue_add_consumed_buffer(rvdev->svq, idx, len);
}
Expand All @@ -184,7 +181,6 @@ static int rpmsg_virtio_enqueue_buffer(struct rpmsg_virtio_device *rvdev,
static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
uint32_t *len, uint16_t *idx)
{
unsigned int role = rpmsg_virtio_get_role(rvdev);
struct metal_list *node;
struct vbuff_reclaimer_t *r_desc;
void *data = NULL;
Expand All @@ -197,19 +193,19 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
data = r_desc;
*idx = r_desc->idx;

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER)
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev))
*len = rvdev->config.h2r_buf_size;
if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE)
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev))
*len = virtqueue_get_buffer_length(rvdev->svq, *idx);
} else if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
} else if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
data = virtqueue_get_buffer(rvdev->svq, len, idx);
if (!data && rvdev->svq->vq_free_cnt) {
data = rpmsg_virtio_shm_pool_get_buffer(rvdev->shpool,
rvdev->config.h2r_buf_size);
*len = rvdev->config.h2r_buf_size;
*idx = 0;
}
} else if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
} else if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
data = virtqueue_get_available_buffer(rvdev->svq, idx, len);
}

Expand All @@ -230,14 +226,13 @@ static void *rpmsg_virtio_get_tx_buffer(struct rpmsg_virtio_device *rvdev,
static void *rpmsg_virtio_get_rx_buffer(struct rpmsg_virtio_device *rvdev,
uint32_t *len, uint16_t *idx)
{
unsigned int role = rpmsg_virtio_get_role(rvdev);
void *data = NULL;

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
data = virtqueue_get_buffer(rvdev->rvq, len, idx);
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
data =
virtqueue_get_available_buffer(rvdev->rvq, idx, len);
}
Expand Down Expand Up @@ -452,7 +447,7 @@ static int rpmsg_virtio_send_offchannel_nocopy(struct rpmsg_device *rdev,

metal_mutex_acquire(&rdev->lock);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && rpmsg_virtio_get_role(rvdev) == VIRTIO_DEV_DRIVER)
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev))
buff_len = rvdev->config.h2r_buf_size;
else
buff_len = virtqueue_get_buffer_length(rvdev->svq, idx);
Expand Down Expand Up @@ -704,25 +699,23 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data,
int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev)
{
struct rpmsg_virtio_device *rvdev;
unsigned int role;
int size = 0;

if (!rdev)
return RPMSG_ERR_PARAM;

metal_mutex_acquire(&rdev->lock);
rvdev = (struct rpmsg_virtio_device *)rdev;
role = rpmsg_virtio_get_role(rvdev);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
/*
* If device role is host then buffers are provided by us,
* so just provide the macro.
*/
size = rvdev->config.h2r_buf_size - sizeof(struct rpmsg_hdr);
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
/*
* If other core is host then buffers are provided by it,
* so get the buffer size from the virtqueue.
Expand All @@ -742,25 +735,23 @@ int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev)
int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev)
{
struct rpmsg_virtio_device *rvdev;
unsigned int role;
int size = 0;

if (!rdev)
return RPMSG_ERR_PARAM;

metal_mutex_acquire(&rdev->lock);
rvdev = (struct rpmsg_virtio_device *)rdev;
role = rpmsg_virtio_get_role(rvdev);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
/*
* If device role is host then buffers are provided by us,
* so just provide the macro.
*/
size = rvdev->config.r2h_buf_size - sizeof(struct rpmsg_hdr);
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
/*
* If other core is host then buffers are provided by it,
* so get the buffer size from the virtqueue.
Expand Down Expand Up @@ -799,7 +790,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
vq_callback callback[RPMSG_NUM_VRINGS];
uint32_t features;
int status;
unsigned int i, role;
unsigned int i;

if (!rvdev || !vdev || !shm_io)
return RPMSG_ERR_PARAM;
Expand All @@ -819,9 +810,8 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
rdev->ops.release_tx_buffer = rpmsg_virtio_release_tx_buffer;
rdev->ops.get_rx_buffer_size = rpmsg_virtio_get_rx_buffer_size;
rdev->ops.get_tx_buffer_size = rpmsg_virtio_get_tx_buffer_size;
role = rpmsg_virtio_get_role(rvdev);

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
/*
* The virtio configuration contains only options applicable to
* a virtio driver, implying rpmsg host role.
Expand All @@ -832,7 +822,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
rvdev->config = *config;
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
/* wait synchro with the host */
status = rpmsg_virtio_wait_remote_ready(rvdev);
if (status)
Expand All @@ -844,7 +834,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
return status;
rdev->support_ns = !!(features & (1 << VIRTIO_RPMSG_F_NS));

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
/*
* Since device is RPMSG Remote so we need to manage the
* shared buffers. Create shared memory pool to handle buffers.
Expand All @@ -861,7 +851,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
callback[1] = rpmsg_virtio_tx_callback;
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
vq_names[0] = "tx_vq";
vq_names[1] = "rx_vq";
callback[0] = rpmsg_virtio_tx_callback;
Expand All @@ -878,12 +868,12 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
return status;

/* Create virtqueue success, assign back the virtqueue */
if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
rvdev->rvq = vdev->vrings_info[0].vq;
rvdev->svq = vdev->vrings_info[1].vq;
}

if (VIRTIO_ENABLED(VIRTIO_DEVICE_SUPPORT) && role == VIRTIO_DEV_DEVICE) {
if (VIRTIO_ROLE_IS_DEVICE(rvdev->vdev)) {
rvdev->rvq = vdev->vrings_info[1].vq;
rvdev->svq = vdev->vrings_info[0].vq;
}
Expand All @@ -902,7 +892,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
vq->shm_io = shm_io;
}

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
struct virtqueue_buf vqbuf;
unsigned int idx;
void *buffer;
Expand Down Expand Up @@ -947,7 +937,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev,
rpmsg_virtio_ns_callback, NULL, rvdev);
}

if (VIRTIO_ENABLED(VIRTIO_DRIVER_SUPPORT) && role == VIRTIO_DEV_DRIVER) {
if (VIRTIO_ROLE_IS_DRIVER(rvdev->vdev)) {
status = virtio_set_status(rvdev->vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
if (status)
goto err;
Expand Down

0 comments on commit dd06ed9

Please sign in to comment.