Skip to content

Commit

Permalink
VirtIO apply clang-format
Browse files Browse the repository at this point in the history
Signed-off-by: Vitalii Chulak <[email protected]>
  • Loading branch information
Jedoku committed Nov 20, 2024
1 parent a90cc39 commit 3bac9d2
Show file tree
Hide file tree
Showing 21 changed files with 548 additions and 751 deletions.
48 changes: 19 additions & 29 deletions VirtIO/VirtIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,38 @@ struct VirtIOBufferDescriptor {
ULONG length;
};

typedef int (*proc_virtqueue_add_buf)(
struct virtqueue *vq,
struct scatterlist sg[],
unsigned int out_num,
unsigned int in_num,
void *opaque,
void *va_indirect,
ULONGLONG phys_indirect);
typedef int (*proc_virtqueue_add_buf)(struct virtqueue *vq, struct scatterlist sg[],
unsigned int out_num, unsigned int in_num, void *opaque,
void *va_indirect, ULONGLONG phys_indirect);

typedef bool(*proc_virtqueue_kick_prepare)(struct virtqueue *vq);
typedef bool (*proc_virtqueue_kick_prepare)(struct virtqueue *vq);

typedef void(*proc_virtqueue_kick_always)(struct virtqueue *vq);
typedef void (*proc_virtqueue_kick_always)(struct virtqueue *vq);

typedef void * (*proc_virtqueue_get_buf)(struct virtqueue *vq, unsigned int *len);
typedef void *(*proc_virtqueue_get_buf)(struct virtqueue *vq, unsigned int *len);

typedef void(*proc_virtqueue_disable_cb)(struct virtqueue *vq);
typedef void (*proc_virtqueue_disable_cb)(struct virtqueue *vq);

typedef bool(*proc_virtqueue_enable_cb)(struct virtqueue *vq);
typedef bool (*proc_virtqueue_enable_cb)(struct virtqueue *vq);

typedef bool(*proc_virtqueue_enable_cb_delayed)(struct virtqueue *vq);
typedef bool (*proc_virtqueue_enable_cb_delayed)(struct virtqueue *vq);

typedef void * (*proc_virtqueue_detach_unused_buf)(struct virtqueue *vq);
typedef void *(*proc_virtqueue_detach_unused_buf)(struct virtqueue *vq);

typedef BOOLEAN(*proc_virtqueue_is_interrupt_enabled)(struct virtqueue *vq);
typedef BOOLEAN (*proc_virtqueue_is_interrupt_enabled)(struct virtqueue *vq);

typedef BOOLEAN(*proc_virtqueue_has_buf)(struct virtqueue *vq);
typedef BOOLEAN (*proc_virtqueue_has_buf)(struct virtqueue *vq);

typedef void(*proc_virtqueue_shutdown)(struct virtqueue *vq);
typedef void (*proc_virtqueue_shutdown)(struct virtqueue *vq);

/* Represents one virtqueue; only data pointed to by the vring structure is exposed to the host */
struct virtqueue {
VirtIODevice *vdev;
unsigned int index;
void (*notification_cb)(struct virtqueue *vq);
void *notification_addr;
void *avail_va;
void *used_va;
void *notification_addr;
void *avail_va;
void *used_va;
proc_virtqueue_add_buf add_buf;
proc_virtqueue_kick_prepare kick_prepare;
proc_virtqueue_kick_always kick_always;
Expand All @@ -60,14 +55,9 @@ struct virtqueue {
proc_virtqueue_shutdown shutdown;
};

static inline int virtqueue_add_buf(
struct virtqueue *vq,
struct scatterlist sg[],
unsigned int out_num,
unsigned int in_num,
void *opaque,
void *va_indirect,
ULONGLONG phys_indirect)
static inline int virtqueue_add_buf(struct virtqueue *vq, struct scatterlist sg[],
unsigned int out_num, unsigned int in_num, void *opaque,
void *va_indirect, ULONGLONG phys_indirect)
{
return vq->add_buf(vq, sg, out_num, in_num, opaque, va_indirect, phys_indirect);
}
Expand Down
71 changes: 25 additions & 46 deletions VirtIO/VirtIOPCICommon.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@

#include "virtio_pci_common.h"

NTSTATUS virtio_device_initialize(VirtIODevice *vdev,
const VirtIOSystemOps *pSystemOps,
PVOID DeviceContext,
bool msix_used)
NTSTATUS virtio_device_initialize(VirtIODevice *vdev, const VirtIOSystemOps *pSystemOps,
PVOID DeviceContext, bool msix_used)
{
NTSTATUS status;

Expand Down Expand Up @@ -76,8 +74,7 @@ NTSTATUS virtio_device_initialize(VirtIODevice *vdev,

void virtio_device_shutdown(VirtIODevice *vdev)
{
if (vdev->info &&
vdev->info != vdev->inline_info) {
if (vdev->info && vdev->info != vdev->inline_info) {
mem_free_nonpaged_block(vdev, vdev->info);
vdev->info = NULL;
}
Expand Down Expand Up @@ -143,29 +140,26 @@ NTSTATUS virtio_set_features(VirtIODevice *vdev, u64 features)
}

/* Read @count fields, @bytes each. */
static void virtio_cread_many(VirtIODevice *vdev,
unsigned int offset,
void *buf, size_t count, size_t bytes)
static void virtio_cread_many(VirtIODevice *vdev, unsigned int offset, void *buf, size_t count,
size_t bytes)
{
u32 old, gen = vdev->device->get_config_generation ?
vdev->device->get_config_generation(vdev) : 0;
u32 old,
gen = vdev->device->get_config_generation ? vdev->device->get_config_generation(vdev) : 0;
size_t i;

do {
old = gen;

for (i = 0; i < count; i++) {
vdev->device->get_config(vdev, (unsigned)(offset + bytes * i),
(char *)buf + i * bytes, (unsigned)bytes);
vdev->device->get_config(vdev, (unsigned)(offset + bytes * i), (char *)buf + i * bytes,
(unsigned)bytes);
}

gen = vdev->device->get_config_generation ?
vdev->device->get_config_generation(vdev) : 0;
gen = vdev->device->get_config_generation ? vdev->device->get_config_generation(vdev) : 0;
} while (gen != old);
}

void virtio_get_config(VirtIODevice *vdev, unsigned offset,
void *buf, unsigned len)
void virtio_get_config(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
{
switch (len) {
case 1:
Expand All @@ -183,19 +177,17 @@ void virtio_get_config(VirtIODevice *vdev, unsigned offset,
}

/* Write @count fields, @bytes each. */
static void virtio_cwrite_many(VirtIODevice *vdev,
unsigned int offset,
void *buf, size_t count, size_t bytes)
static void virtio_cwrite_many(VirtIODevice *vdev, unsigned int offset, void *buf, size_t count,
size_t bytes)
{
size_t i;
for (i = 0; i < count; i++) {
vdev->device->set_config(vdev, (unsigned)(offset + bytes * i),
(char *)buf + i * bytes, (unsigned)bytes);
vdev->device->set_config(vdev, (unsigned)(offset + bytes * i), (char *)buf + i * bytes,
(unsigned)bytes);
}
}

void virtio_set_config(VirtIODevice *vdev, unsigned offset,
void *buf, unsigned len)
void virtio_set_config(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
{
switch (len) {
case 1:
Expand All @@ -212,10 +204,8 @@ void virtio_set_config(VirtIODevice *vdev, unsigned offset,
}
}

NTSTATUS virtio_query_queue_allocation(VirtIODevice *vdev,
unsigned index,
unsigned short *pNumEntries,
unsigned long *pRingSize,
NTSTATUS virtio_query_queue_allocation(VirtIODevice *vdev, unsigned index,
unsigned short *pNumEntries, unsigned long *pRingSize,
unsigned long *pHeapSize)
{
return vdev->device->query_queue_alloc(vdev, index, pNumEntries, pRingSize, pHeapSize);
Expand All @@ -239,8 +229,7 @@ NTSTATUS virtio_reserve_queue_memory(VirtIODevice *vdev, unsigned nvqs)
return STATUS_SUCCESS;
}

static NTSTATUS vp_setup_vq(struct virtqueue **queue,
VirtIODevice *vdev, unsigned index,
static NTSTATUS vp_setup_vq(struct virtqueue **queue, VirtIODevice *vdev, unsigned index,
u16 msix_vec)
{
VirtIOQueueInfo *info = &vdev->info[index];
Expand All @@ -253,20 +242,13 @@ static NTSTATUS vp_setup_vq(struct virtqueue **queue,
return status;
}

NTSTATUS virtio_find_queue(VirtIODevice *vdev, unsigned index,
struct virtqueue **vq)
NTSTATUS virtio_find_queue(VirtIODevice *vdev, unsigned index, struct virtqueue **vq)
{
u16 msix_vec = vdev_get_msix_vector(vdev, index);
return vp_setup_vq(
vq,
vdev,
index,
msix_vec);
return vp_setup_vq(vq, vdev, index, msix_vec);
}

NTSTATUS virtio_find_queues(VirtIODevice *vdev,
unsigned nvqs,
struct virtqueue *vqs[])
NTSTATUS virtio_find_queues(VirtIODevice *vdev, unsigned nvqs, struct virtqueue *vqs[])
{
unsigned i;
NTSTATUS status;
Expand All @@ -292,11 +274,7 @@ NTSTATUS virtio_find_queues(VirtIODevice *vdev,
/* set up queue interrupts */
for (i = 0; i < nvqs; i++) {
msix_vec = vdev_get_msix_vector(vdev, i);
status = vp_setup_vq(
&vqs[i],
vdev,
i,
msix_vec);
status = vp_setup_vq(&vqs[i], vdev, i, msix_vec);
if (!NT_SUCCESS(status)) {
goto error_find;
}
Expand All @@ -322,8 +300,9 @@ void virtio_delete_queues(VirtIODevice *vdev)
struct virtqueue *vq;
unsigned i;

if (vdev->info == NULL)
if (vdev->info == NULL) {
return;
}

for (i = 0; i < vdev->maxQueues; i++) {
vq = vdev->info[i].vq;
Expand Down
44 changes: 18 additions & 26 deletions VirtIO/VirtIOPCILegacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ void vio_legacy_dump_registers(VirtIODevice *vdev)
{
DPrintf(5, ("%s\n", __FUNCTION__));

DPrintf(0, "[VIRTIO_PCI_HOST_FEATURES] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_HOST_FEATURES));
DPrintf(0, "[VIRTIO_PCI_GUEST_FEATURES] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_GUEST_FEATURES));
DPrintf(0, "[VIRTIO_PCI_HOST_FEATURES] = %x\n",
ioread32(vdev, vdev->addr + VIRTIO_PCI_HOST_FEATURES));
DPrintf(0, "[VIRTIO_PCI_GUEST_FEATURES] = %x\n",
ioread32(vdev, vdev->addr + VIRTIO_PCI_GUEST_FEATURES));
DPrintf(0, "[VIRTIO_PCI_QUEUE_PFN] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_QUEUE_PFN));
DPrintf(0, "[VIRTIO_PCI_QUEUE_NUM] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_QUEUE_NUM));
DPrintf(0, "[VIRTIO_PCI_QUEUE_SEL] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_QUEUE_SEL));
DPrintf(0, "[VIRTIO_PCI_QUEUE_NOTIFY] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_QUEUE_NOTIFY));
DPrintf(0, "[VIRTIO_PCI_QUEUE_NOTIFY] = %x\n",
ioread32(vdev, vdev->addr + VIRTIO_PCI_QUEUE_NOTIFY));
DPrintf(0, "[VIRTIO_PCI_STATUS] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_STATUS));
DPrintf(0, "[VIRTIO_PCI_ISR] = %x\n", ioread32(vdev, vdev->addr + VIRTIO_PCI_ISR));
}

static void vio_legacy_get_config(VirtIODevice * vdev,
unsigned offset,
void *buf,
unsigned len)
static void vio_legacy_get_config(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
{
ULONG_PTR ioaddr = vdev->addr + VIRTIO_PCI_CONFIG(vdev->msix_used) + offset;
u8 *ptr = buf;
Expand All @@ -77,9 +77,7 @@ static void vio_legacy_get_config(VirtIODevice * vdev,
}
}

static void vio_legacy_set_config(VirtIODevice *vdev,
unsigned offset,
const void *buf,
static void vio_legacy_set_config(VirtIODevice *vdev, unsigned offset, const void *buf,
unsigned len)
{
ULONG_PTR ioaddr = vdev->addr + VIRTIO_PCI_CONFIG(vdev->msix_used) + offset;
Expand Down Expand Up @@ -146,10 +144,8 @@ static u16 vio_legacy_set_queue_vector(struct virtqueue *vq, u16 vector)
return ioread16(vdev, vdev->addr + VIRTIO_MSI_QUEUE_VECTOR);
}

static NTSTATUS vio_legacy_query_vq_alloc(VirtIODevice *vdev,
unsigned index,
unsigned short *pNumEntries,
unsigned long *pRingSize,
static NTSTATUS vio_legacy_query_vq_alloc(VirtIODevice *vdev, unsigned index,
unsigned short *pNumEntries, unsigned long *pRingSize,
unsigned long *pHeapSize)
{
unsigned long ring_size, data_size;
Expand All @@ -174,11 +170,8 @@ static NTSTATUS vio_legacy_query_vq_alloc(VirtIODevice *vdev,
return STATUS_SUCCESS;
}

static NTSTATUS vio_legacy_setup_vq(struct virtqueue **queue,
VirtIODevice *vdev,
VirtIOQueueInfo *info,
unsigned index,
u16 msix_vec)
static NTSTATUS vio_legacy_setup_vq(struct virtqueue **queue, VirtIODevice *vdev,
VirtIOQueueInfo *info, unsigned index, u16 msix_vec)
{
struct virtqueue *vq;
unsigned long ring_size, heap_size;
Expand All @@ -196,13 +189,13 @@ static NTSTATUS vio_legacy_setup_vq(struct virtqueue **queue,
}

/* activate the queue */
iowrite32(vdev, (u32)(mem_get_physical_address(vdev, info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT),
vdev->addr + VIRTIO_PCI_QUEUE_PFN);
iowrite32(vdev,
(u32)(mem_get_physical_address(vdev, info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT),
vdev->addr + VIRTIO_PCI_QUEUE_PFN);

/* create the vring */
vq = vring_new_virtqueue_split(index, info->num,
VIRTIO_PCI_VRING_ALIGN, vdev,
info->queue, vp_notify,
vq = vring_new_virtqueue_split(
index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev, info->queue, vp_notify,
(u8 *)info->queue + ROUND_TO_PAGES(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN, false)));
if (!vq) {
status = STATUS_INSUFFICIENT_RESOURCES;
Expand Down Expand Up @@ -237,8 +230,7 @@ static void vio_legacy_del_vq(VirtIOQueueInfo *info)
iowrite16(vdev, (u16)vq->index, vdev->addr + VIRTIO_PCI_QUEUE_SEL);

if (vdev->msix_used) {
iowrite16(vdev, VIRTIO_MSI_NO_VECTOR,
vdev->addr + VIRTIO_MSI_QUEUE_VECTOR);
iowrite16(vdev, VIRTIO_MSI_NO_VECTOR, vdev->addr + VIRTIO_MSI_QUEUE_VECTOR);
/* Flush the write out to device */
ioread8(vdev, vdev->addr + VIRTIO_PCI_ISR);
}
Expand Down
Loading

0 comments on commit 3bac9d2

Please sign in to comment.