This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 5c93d1b2857525b029570034ee9f21ceaeedadb3
Author: Yongrong Wang <[email protected]>
AuthorDate: Fri Feb 6 14:19:32 2026 +0800

    drivers/virtio: add /dev/virtio and VIRTIOIOC_DUMP
    
    Register /dev/virtio with a VIRTIOIOC_DUMP ioctl that walks the bus and
    prints each device, its driver and its virtqueues to syslog. The ioctl
    argument is an optional device name as returned by virtio_dev_name()
    (e.g. "Block"); NULL or "all" dumps every device.
    
    Add a dump callback to struct virtio_driver so a driver can append its
    own state; it is called after the generic part.
    
    _VIRTIOBASE takes 0x5100, the first free base after _RPTUNBASE.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/virtio/virtio.c       | 125 +++++++++++++++++++++++++++++++++++++++++-
 include/nuttx/fs/ioctl.h      |   8 +++
 include/nuttx/virtio/virtio.h |  14 ++++-
 3 files changed, 143 insertions(+), 4 deletions(-)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 009e6d12794..d64a52757d7 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -27,8 +27,11 @@
 #include <nuttx/debug.h>
 
 #include <string.h>
+#include <strings.h>
+#include <syslog.h>
 
 #include <nuttx/arch.h>
+#include <nuttx/fs/fs.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/mutex.h>
 #include <nuttx/kmalloc.h>
@@ -60,7 +63,7 @@ struct virtio_bus_s
 {
   mutex_t lock;                  /* Lock for the list */
   struct dq_queue_s device;      /* Wait match virtio device list */
-  struct dq_queue_s driver;      /* Virtio driver list */
+  struct dq_queue_s driver;      /* VirtIO driver list */
 };
 
 struct virtio_device_item_s
@@ -72,15 +75,127 @@ struct virtio_device_item_s
                                   */
 };
 
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int virtio_dev_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
+static const struct file_operations g_virtio_dev_ops =
+{
+  NULL,              /* open */
+  NULL,              /* close */
+  NULL,              /* read */
+  NULL,              /* write */
+  NULL,              /* seek */
+  virtio_dev_ioctl,  /* ioctl */
+};
+
 static struct virtio_bus_s g_virtio_bus =
 {
   .lock = NXMUTEX_INITIALIZER,
 };
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: virtio_dump
+ ****************************************************************************/
+
+static void virtio_dump(FAR const char *name)
+{
+  FAR struct dq_entry_s *node;
+  int count = 0;
+  int ret;
+
+  syslog(LOG_EMERG, "VirtIO devices:\n");
+
+  name = name == NULL ? "all" : name;
+  ret = nxmutex_lock(&g_virtio_bus.lock);
+  if (ret < 0)
+    {
+      syslog(LOG_EMERG, "  Failed to acquire lock\n");
+      return;
+    }
+
+  dq_for_every(&g_virtio_bus.device, node)
+    {
+      FAR struct virtio_device_item_s *item =
+        container_of(node, struct virtio_device_item_s, node);
+      FAR struct virtio_device *vdev = item->device;
+      FAR const char *dev_name;
+      unsigned int i;
+
+      dev_name = virtio_dev_name(vdev->id.device);
+      dev_name = dev_name == NULL ? "unknown" : dev_name;
+
+      /* Apply type filter if specified, "all" means no filter */
+
+      if (strcasecmp(name, "all") != 0 && strcasecmp(dev_name, name) != 0)
+        {
+          continue;
+        }
+
+      count++;
+
+      syslog(LOG_EMERG, "  [%s] id=%" PRIu32 " vdev=%p driver=%p priv=%p\n",
+             dev_name, vdev->id.device, vdev, item->driver, vdev->priv);
+
+      /* Call driver-specific dump if available */
+
+      if (item->driver && item->driver->dump)
+        {
+          item->driver->dump(vdev);
+        }
+
+      /* Dump virtqueue information */
+
+      for (i = 0; i < vdev->vrings_num; i++)
+        {
+          FAR struct virtio_vring_info *vinfo = &vdev->vrings_info[i];
+
+          virtqueue_dump(vinfo->vq);
+        }
+    }
+
+  if (count == 0)
+    {
+      syslog(LOG_EMERG, "  No devices of type '%s' found\n", name);
+    }
+
+  nxmutex_unlock(&g_virtio_bus.lock);
+}
+
+/****************************************************************************
+ * Name: virtio_dev_ioctl
+ ****************************************************************************/
+
+static int virtio_dev_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg)
+{
+  int ret = -ENOTTY;
+
+  switch (cmd)
+    {
+      case VIRTIOIOC_DUMP:
+        virtio_dump((FAR const char *)(uintptr_t)arg);
+        ret = OK;
+        break;
+
+      default:
+        break;
+    }
+
+  return ret;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -100,6 +215,14 @@ void virtio_register_drivers(void)
       vrterr("metal_init failed, ret=%d\n", ret);
     }
 
+  /* Register virtio character device */
+
+  ret = register_driver("/dev/virtio", &g_virtio_dev_ops, 0200, NULL);
+  if (ret < 0)
+    {
+      vrterr("register_driver /dev/virtio failed, ret=%d\n", ret);
+    }
+
 #ifdef CONFIG_DRIVERS_VIRTIO_BLK
   ret = virtio_register_blk_driver();
   if (ret < 0)
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index adeb5906c76..127b9e980a8 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -116,6 +116,7 @@
 #define _DSHOTIOCBASE   (0x4800) /* Dshot device ioctl commands */
 #define _PULSECOUNTBASE (0x4900) /* Pulse count driver ioctl commands */
 #define _RPTUNBASE      (0x5000) /* Remote processor tunneling ioctl commands 
*/
+#define _VIRTIOBASE     (0x5100) /* VirtIO ioctl commands */
 #define _WLIOCBASE      (0x8b00) /* Wireless modules ioctl network commands */
 #define _DMABUFBASE     (0x8d00) /* DMA-Buf modules ioctl commands */
 
@@ -865,6 +866,13 @@
 #define _RPTUNIOCVALID(c)     (_IOC_TYPE(c)==_RPTUNBASE)
 #define _RPTUNIOC(nr)         _IOC(_RPTUNBASE,nr)
 
+/* VirtIO driver ioctl definitions ******************************************/
+
+/* see nuttx/include/nuttx/virtio/virtio.h */
+
+#define _VIRTIOIOCVALID(c)    (_IOC_TYPE(c)==_VIRTIOBASE)
+#define _VIRTIOIOC(nr)        _IOC(_VIRTIOBASE,nr)
+
 /* DMA-Buf driver ioctl definitions *****************************************/
 
 /* see nuttx/include/nuttx/dma-buf.h */
diff --git a/include/nuttx/virtio/virtio.h b/include/nuttx/virtio/virtio.h
index 00374fa5455..a6be3d5bd2a 100644
--- a/include/nuttx/virtio/virtio.h
+++ b/include/nuttx/virtio/virtio.h
@@ -29,13 +29,20 @@
 
 #include <stdint.h>
 
+#include <nuttx/fs/ioctl.h>
 #include <nuttx/queue.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/virtio/virtio-config.h>
 
-#ifdef CONFIG_OPENAMP
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
-#include <openamp/open_amp.h>
+/* VirtIO ioctl command definitions */
+
+#define VIRTIOIOC_DUMP        _VIRTIOIOC(1)
+
+#ifdef CONFIG_OPENAMP
 
 /****************************************************************************
  * Public Type Definitions
@@ -47,6 +54,7 @@ struct virtio_driver
   uint32_t           device;   /* device id */
   CODE int         (*probe)(FAR struct virtio_device *vdev);
   CODE void        (*remove)(FAR struct virtio_device *vdev);
+  CODE void        (*dump)(FAR struct virtio_device *vdev);
   struct dq_entry_s  node;
 };
 #endif
@@ -225,7 +233,7 @@ int virtio_register_device(FAR struct virtio_device 
*device);
 int virtio_unregister_driver(FAR struct virtio_driver *driver);
 int virtio_unregister_device(FAR struct virtio_device *device);
 
-/* Virtio driver initailied function, called in NuttX driver_intialize() */
+/* VirtIO driver initailied function, called in NuttX driver_intialize() */
 
 void virtio_register_drivers(void);
 

Reply via email to