Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 69927fab3891b72d92c47cac5ca6786ac7d6cc5e)
+++ uspace/Makefile	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -251,5 +251,6 @@
 	lib/bithenge \
 	lib/posix \
-	lib/ieee80211
+	lib/ieee80211 \
+	lib/virtio
 
 BASE_BUILDS := $(addsuffix .build,$(BASE_LIBS))
Index: uspace/drv/nic/virtio-net/Makefile
===================================================================
--- uspace/drv/nic/virtio-net/Makefile	(revision 69927fab3891b72d92c47cac5ca6786ac7d6cc5e)
+++ uspace/drv/nic/virtio-net/Makefile	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -28,5 +28,5 @@
 
 USPACE_PREFIX = ../../..
-LIBS = drv nic
+LIBS = drv nic virtio
 BINARY = virtio-net
 
Index: uspace/drv/nic/virtio-net/virtio-net.c
===================================================================
--- uspace/drv/nic/virtio-net/virtio-net.c	(revision 69927fab3891b72d92c47cac5ca6786ac7d6cc5e)
+++ uspace/drv/nic/virtio-net/virtio-net.c	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -37,16 +37,7 @@
 #include <nic.h>
 
+#include <virtio-pci.h>
+
 #define NAME	"virtio-net"
-
-#define VIRTIO_PCI_CAP_TYPE(c)		((c) + 3)
-#define VIRTIO_PCI_CAP_BAR(c)		((c) + 4)
-#define VIRTIO_PCI_CAP_OFFSET(c)	((c) + 8)
-#define VIRTIO_PCI_CAP_LENGTH(c)	((c) + 12)
-
-#define VIRTIO_PCI_CAP_COMMON_CFG	1
-#define VIRTIO_PCI_CAP_NOTIFY_CFG	2
-#define VIRTIO_PCI_CAP_ISR_CFG		3
-#define VIRTIO_PCI_CAP_DEVICE_CFG	4
-#define VIRTIO_PCI_CAP_PCI_CFG		5
 
 static errno_t virtio_net_dev_add(ddf_dev_t *dev)
@@ -55,41 +46,7 @@
 	    ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
 
-	async_sess_t *pci_sess = ddf_dev_parent_sess_get(dev);
-	if (!pci_sess)
-		return ENOENT;
-
-	/*
-	 * Find the VIRTIO PCI Capabilities
-	 */
-	errno_t rc;
-	uint8_t c;
-	uint8_t id;
-	for (rc = pci_config_space_cap_first(pci_sess, &c, &id);
-	    (rc == EOK) && c;
-	    rc = pci_config_space_cap_next(pci_sess, &c, &id)) {
-		if (id == PCI_CAP_VENDORSPECID) {
-			uint8_t type;
-
-			rc = pci_config_space_read_8(pci_sess,
-			    VIRTIO_PCI_CAP_TYPE(c), &type);
-			if (rc != EOK)
-				return rc;
-
-			switch (type) {
-			case VIRTIO_PCI_CAP_COMMON_CFG:
-				break;
-			case VIRTIO_PCI_CAP_NOTIFY_CFG:
-				break;
-			case VIRTIO_PCI_CAP_ISR_CFG:
-				break;
-			case VIRTIO_PCI_CAP_DEVICE_CFG:
-				break;
-			case VIRTIO_PCI_CAP_PCI_CFG:
-				break;
-			default:
-				break;
-			}
-		}
-	}
+	// XXX: this will be part of the nic data
+	virtio_dev_t virtio_dev;
+	errno_t rc = virtio_pci_dev_init(dev, &virtio_dev);
 	if (rc != EOK)
 		return rc;
Index: uspace/lib/virtio/Makefile
===================================================================
--- uspace/lib/virtio/Makefile	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
+++ uspace/lib/virtio/Makefile	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -0,0 +1,36 @@
+#
+# Copyright (c) 2018 Jakub Jermar
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../..
+LIBRARY = libvirtio
+LIBS = drv
+
+SOURCES = \
+	virtio-pci.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/virtio/virtio-pci.c
===================================================================
--- uspace/lib/virtio/virtio-pci.c	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
+++ uspace/lib/virtio/virtio-pci.c	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2018 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @file VIRTIO support
+ */
+
+#include "virtio-pci.h"
+
+#include <ddf/driver.h>
+#include <pci_dev_iface.h>
+
+static void virtio_pci_common_cfg(virtio_dev_t *vdev, hw_resource_list_t *res,
+    uint8_t bar, uint32_t offset, uint32_t length)
+{
+	/* Proceed only if we don't have common config structure yet */
+	if (vdev->common_cfg)
+		return;
+
+	/* We must ignore the capability if bar is greater than 5 */
+	if (bar > 5)
+		return;
+}
+
+errno_t virtio_pci_dev_init(ddf_dev_t *dev, virtio_dev_t *vdev)
+{
+	async_sess_t *pci_sess = ddf_dev_parent_sess_get(dev);
+	if (!pci_sess)
+		return ENOENT;
+
+	errno_t rc;
+	hw_resource_list_t hw_res;
+	rc = hw_res_get_resource_list(pci_sess, &hw_res);
+	if (rc != EOK)
+		return rc;
+
+	/*
+	 * Find the VIRTIO PCI Capabilities
+	 */
+	uint8_t c;
+	uint8_t id;
+	for (rc = pci_config_space_cap_first(pci_sess, &c, &id);
+	    (rc == EOK) && c;
+	    rc = pci_config_space_cap_next(pci_sess, &c, &id)) {
+		if (id == PCI_CAP_VENDORSPECID) {
+			uint8_t type;
+			rc = pci_config_space_read_8(pci_sess,
+			    VIRTIO_PCI_CAP_TYPE(c), &type);
+			if (rc != EOK)
+				return rc;
+
+			uint8_t bar;
+			rc = pci_config_space_read_8(pci_sess,
+			    VIRTIO_PCI_CAP_BAR(c), &bar);
+			if (rc != EOK)
+				return rc;
+
+			uint32_t offset;
+			rc = pci_config_space_read_32(pci_sess,
+			    VIRTIO_PCI_CAP_OFFSET(c), &offset);
+			if (rc != EOK)
+				return rc;
+
+			uint32_t length;
+			rc = pci_config_space_read_32(pci_sess,
+			    VIRTIO_PCI_CAP_LENGTH(c), &length);
+			if (rc != EOK)
+				return rc;
+
+			switch (type) {
+			case VIRTIO_PCI_CAP_COMMON_CFG:
+				virtio_pci_common_cfg(vdev, &hw_res, bar,
+				    offset, length);
+				break;
+			case VIRTIO_PCI_CAP_NOTIFY_CFG:
+				break;
+			case VIRTIO_PCI_CAP_ISR_CFG:
+				break;
+			case VIRTIO_PCI_CAP_DEVICE_CFG:
+				break;
+			case VIRTIO_PCI_CAP_PCI_CFG:
+				break;
+			default:
+				break;
+			}
+		}
+	}
+
+	return rc;
+}
+
+/** @}
+ */
Index: uspace/lib/virtio/virtio-pci.h
===================================================================
--- uspace/lib/virtio/virtio-pci.h	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
+++ uspace/lib/virtio/virtio-pci.h	(revision 00192cde306d224210f23dbe03b16877c0d01e4d)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2018 Jakub Jermar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @file VIRTIO PCI definitions
+ */
+
+#ifndef __VIRTIO_PCI_H__
+#define __VIRTIO_PCI_H__
+
+#include <ddf/driver.h>
+#include <ddi.h>
+
+#define VIRTIO_PCI_CAP_TYPE(c)		((c) + 3)
+#define VIRTIO_PCI_CAP_BAR(c)		((c) + 4)
+#define VIRTIO_PCI_CAP_OFFSET(c)	((c) + 8)
+#define VIRTIO_PCI_CAP_LENGTH(c)	((c) + 12)
+
+#define VIRTIO_PCI_CAP_COMMON_CFG	1
+#define VIRTIO_PCI_CAP_NOTIFY_CFG	2
+#define VIRTIO_PCI_CAP_ISR_CFG		3
+#define VIRTIO_PCI_CAP_DEVICE_CFG	4
+#define VIRTIO_PCI_CAP_PCI_CFG		5
+
+/** Common configuration structure layout according to VIRTIO v1. */
+typedef struct virtio_pci_common_cfg {
+	ioport32_t device_feature_select;
+	const ioport32_t device_feature;
+	ioport32_t driver_feature_select;
+	ioport32_t driver_feature;
+	ioport16_t msix_config;
+	const ioport16_t num_queues;
+	ioport8_t device_status;
+	const ioport8_t config_generation;
+	ioport16_t queue_select;
+	ioport16_t queue_size;
+	ioport16_t queue_msix_vector;
+	ioport16_t queue_enable;
+	const ioport16_t queue_notif_off;
+	ioport64_t queue_desc;
+	ioport64_t queue_avail;
+	ioport64_t queue_used;
+} virtio_pci_common_cfg_t;
+
+typedef struct {
+	virtio_pci_common_cfg_t *common_cfg;
+} virtio_dev_t;
+
+errno_t virtio_pci_dev_init(ddf_dev_t *, virtio_dev_t *);
+
+#endif
+
+/** @}
+ */
