From patchwork Mon Sep 17 10:00:33 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeny Voevodin X-Patchwork-Id: 184364 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A61212C0091 for ; Mon, 17 Sep 2012 20:01:31 +1000 (EST) Received: from localhost ([::1]:33432 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDY8v-0006as-Jv for incoming@patchwork.ozlabs.org; Mon, 17 Sep 2012 06:01:29 -0400 Received: from eggs.gnu.org ([208.118.235.92]:53124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDY8R-00066O-9R for qemu-devel@nongnu.org; Mon, 17 Sep 2012 06:01:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TDY8M-00051a-AP for qemu-devel@nongnu.org; Mon, 17 Sep 2012 06:00:59 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:23337) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDY8M-00050O-2T for qemu-devel@nongnu.org; Mon, 17 Sep 2012 06:00:54 -0400 Received: from eusync3.samsung.com (mailout1.w1.samsung.com [210.118.77.11]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MAH005RPNU4KF80@mailout1.w1.samsung.com> for qemu-devel@nongnu.org; Mon, 17 Sep 2012 11:01:16 +0100 (BST) Received: from evvoevodinPC.rnd.samsung.ru ([106.109.8.14]) by eusync3.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0MAH00DV7NT8FQ90@eusync3.samsung.com> for qemu-devel@nongnu.org; Mon, 17 Sep 2012 11:00:51 +0100 (BST) From: Evgeny Voevodin To: qemu-devel@nongnu.org Date: Mon, 17 Sep 2012 14:00:33 +0400 Message-id: <1347876042-22609-4-git-send-email-e.voevodin@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1347876042-22609-1-git-send-email-e.voevodin@samsung.com> References: <1347876042-22609-1-git-send-email-e.voevodin@samsung.com> X-TM-AS-MML: No X-detected-operating-system: by eggs.gnu.org: Solaris 10 (1203?) X-Received-From: 210.118.77.11 Cc: peter.maydell@linaro.org, kyungmin.park@samsung.com, aliguori@us.ibm.com, Evgeny Voevodin Subject: [Qemu-devel] [RFC v2 03/12] Virtio: Add transport bindings. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Evgeny Voevodin --- hw/Makefile.objs | 1 + hw/virtio-transport.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-transport.h | 74 +++++++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 hw/virtio-transport.c create mode 100644 hw/virtio-transport.h diff --git a/hw/Makefile.objs b/hw/Makefile.objs index 6dfebd2..db4c16d 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -2,6 +2,7 @@ hw-obj-y = usb/ ide/ hw-obj-y += loader.o hw-obj-$(CONFIG_VIRTIO) += virtio-console.o hw-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o +hw-obj-$(CONFIG_VIRTIO) += virtio-transport.o hw-obj-y += fw_cfg.o hw-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o hw-obj-$(CONFIG_PCI) += msix.o msi.o diff --git a/hw/virtio-transport.c b/hw/virtio-transport.c new file mode 100644 index 0000000..76360ba --- /dev/null +++ b/hw/virtio-transport.c @@ -0,0 +1,147 @@ +/* + * Virtio transport bindings + * + * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd. + * + * Author: + * Evgeny Voevodin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "virtio-transport.h" + +#define VIRTIO_TRANSPORT_BUS "virtio-transport" + +static QTAILQ_HEAD(, VirtIOTransportLink) transport_links = + QTAILQ_HEAD_INITIALIZER(transport_links); + +/* + * Find transport device by its ID. + */ +VirtIOTransportLink* virtio_find_transport(const char *name) +{ + VirtIOTransportLink *trl; + + assert(name != NULL); + + QTAILQ_FOREACH(trl, &transport_links, sibling) { + if (trl->tr->id != NULL) { + if (!strcmp(name, trl->tr->id)) { + return trl; + } + } + } + + return NULL; +} + +/* + * Count transport devices by ID. + */ +uint32_t virtio_count_transports(const char *name) +{ + VirtIOTransportLink *trl; + uint32_t i = 0; + + QTAILQ_FOREACH(trl, &transport_links, sibling) { + if (name == NULL) { + i++; + continue; + } + + if (trl->tr->id != NULL) { + if (!strncmp(name, trl->tr->id,strlen(name))) { + i++; + } + } + } + return i; +} + +/* + * Initialize new transport device + */ +char* virtio_init_transport(DeviceState *dev, VirtIOTransportLink **trl, + const char* name, virtio_backend_init_cb cb) +{ + VirtIOTransportLink *link = g_malloc0(sizeof(VirtIOTransportLink)); + char *buf; + size_t len; + uint32_t i; + + assert(dev != NULL); + assert(name != NULL); + assert(trl != NULL); + + i = virtio_count_transports(name); + len = strlen(name) + 16; + buf = g_malloc(len); + snprintf(buf, len, "%s.%d", name, i); + qbus_create(TYPE_VIRTIO_BUS, dev, buf); + + /* Add new transport */ + QTAILQ_INSERT_TAIL(&transport_links, link, sibling); + link->tr = dev; + link->cb = cb; + // TODO: Add a link property + *trl = link; + return buf; +} + +/* + * Unplug back-end from system bus and plug it into transport bus. + */ +void virtio_plug_into_transport(DeviceState *dev, VirtIOTransportLink *trl) +{ + BusChild *kid; + + /* Unplug back-end from system bus */ + QTAILQ_FOREACH(kid, &qdev_get_parent_bus(dev)->children, sibling) { + if (kid->child == dev) { + QTAILQ_REMOVE(&qdev_get_parent_bus(dev)->children, kid, sibling); + break; + } + } + + /* Plug back-end into transport's bus */ + qdev_set_parent_bus(dev, QLIST_FIRST(&trl->tr->child_bus)); + +} + +/* + * Execute call-back on back-end initialization. + * Performs initialization of MMIO or PCI transport. + */ +int virtio_call_backend_init_cb(DeviceState *dev, VirtIOTransportLink *trl, + VirtIODevice *vdev) +{ + if (trl->cb) { + return trl->cb(dev, vdev, trl); + } + + return 0; +} + +static const TypeInfo virtio_bus_info = { + .name = TYPE_VIRTIO_BUS, + .parent = TYPE_BUS, + .instance_size = sizeof(BusState), +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_bus_info); +} + +type_init(virtio_register_types) diff --git a/hw/virtio-transport.h b/hw/virtio-transport.h new file mode 100644 index 0000000..43200dc --- /dev/null +++ b/hw/virtio-transport.h @@ -0,0 +1,74 @@ +/* + * Virtio transport header + * + * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd. + * + * Author: + * Evgeny Voevodin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef VIRTIO_TRANSPORT_H_ +#define VIRTIO_TRANSPORT_H_ + +#include "qdev.h" +#include "qemu-common.h" + +#define VIRTIO_MMIO "virtio-mmio" +#define VIRTIO_PCI "virtio-pci" + +#define TYPE_VIRTIO_BUS "virtio-bus" +#define VIRTIO_BUS(obj) OBJECT_CHECK(virtio_bus, (obj), TYPE_VIRTIO_BUS) + +struct VirtIOTransportLink; + +typedef int (*virtio_backend_init_cb)(DeviceState *dev, VirtIODevice *vdev, + struct VirtIOTransportLink *trl); + +typedef struct VirtIOTransportLink { + DeviceState *tr; + virtio_backend_init_cb cb; + uint32_t host_features; + QTAILQ_ENTRY(VirtIOTransportLink) sibling; +} VirtIOTransportLink; + +/* + * Find transport device by its ID. + */ +VirtIOTransportLink* virtio_find_transport(const char *name); + +/* + * Count transport devices by ID. + */ +uint32_t virtio_count_transports(const char *name); + +/* + * Initialize new transport device + */ +char* virtio_init_transport(DeviceState *dev, VirtIOTransportLink **trl, + const char* name, virtio_backend_init_cb cb); + +/* + * Unplug back-end from system bus and plug it into transport bus. + */ +void virtio_plug_into_transport(DeviceState *dev, VirtIOTransportLink *trl); + +/* + * Execute call-back on back-end initialization. + * Performs initialization of MMIO or PCI transport. + */ +int virtio_call_backend_init_cb(DeviceState *dev, VirtIOTransportLink *trl, + VirtIODevice *vdev); + +#endif /* VIRTIO_TRANSPORT_H_ */