From patchwork Thu Jun 23 13:19:18 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 101633 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A03AAB6F8C for ; Fri, 24 Jun 2011 00:37:47 +1000 (EST) Received: from localhost ([::1]:43777 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QZl2N-0007ap-Lw for incoming@patchwork.ozlabs.org; Thu, 23 Jun 2011 10:37:43 -0400 Received: from eggs.gnu.org ([140.186.70.92]:48401) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QZjkf-0001RV-5w for qemu-devel@nongnu.org; Thu, 23 Jun 2011 09:15:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QZjkd-0007As-Gs for qemu-devel@nongnu.org; Thu, 23 Jun 2011 09:15:20 -0400 Received: from smtp.eu.citrix.com ([62.200.22.115]:14057) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QZjkc-0007Ae-UE for qemu-devel@nongnu.org; Thu, 23 Jun 2011 09:15:19 -0400 X-IronPort-AV: E=Sophos;i="4.65,413,1304294400"; d="scan'208";a="6447134" Received: from lonpmailmx01.citrite.net ([10.30.224.162]) by LONPIPO01.EU.CITRIX.COM with ESMTP/TLS/RC4-MD5; 23 Jun 2011 13:15:17 +0000 Received: from kaball.uk.xensource.com (10.80.2.59) by LONPMAILMX01.citrite.net (10.30.224.162) with Microsoft SMTP Server id 8.3.137.0; Thu, 23 Jun 2011 14:15:17 +0100 Date: Thu, 23 Jun 2011 14:19:18 +0100 From: Stefano Stabellini X-X-Sender: sstabellini@kaball-desktop To: Message-ID: User-Agent: Alpine 2.00 (DEB 1167 2008-08-23) MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 62.200.22.115 Cc: Kevin Wolf , xen-devel@lists.xensource.com, "Michael S. Tsirkin" , Stefano Stabellini , Alexander Graf , Anthony.Perard@citrix.com Subject: [Qemu-devel] [PATCH v2] xen: implement unplug protocol in xen_platform 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 xen: implement unplug protocol in xen_platform The unplug protocol is necessary to support PV drivers in the guest: the drivers expect to be able to "unplug" emulated disks and nics before initializing the Xen PV interfaces. It is responsibility of the guest to make sure that the unplug is done before the emulated devices or the PV interface start to be used. We use pci_for_each_device to walk the PCI bus, identify the devices and disks that we want to disable and dynamically unplug them. Signed-off-by: Stefano Stabellini diff --git a/hw/xen_platform.c b/hw/xen_platform.c index b167eee..ae8ae77 100644 --- a/hw/xen_platform.c +++ b/hw/xen_platform.c @@ -34,6 +34,8 @@ #include "xen_backend.h" #include "rwhandler.h" #include "trace.h" +#include "hw/ide/pci.h" +#include "hw/pci_ids.h" #include @@ -76,6 +78,55 @@ static void log_writeb(PCIXenPlatformState *s, char val) } /* Xen Platform, Fixed IOPort */ +#define UNPLUG_ALL_IDE_DISKS 1 +#define UNPLUG_ALL_NICS 2 +#define UNPLUG_AUX_IDE_DISKS 4 + +static int unplug_param; + +static void unplug_nic(PCIBus *b, PCIDevice *d) +{ + if (pci_get_word(d->config + PCI_CLASS_DEVICE) == + PCI_CLASS_NETWORK_ETHERNET) { + qdev_unplug(&(d->qdev)); + } +} + +static void pci_unplug_nics(PCIBus *bus) +{ + pci_for_each_device(bus, 0, unplug_nic); +} + +static void unplug_disks(PCIBus *b, PCIDevice *d) +{ + if (pci_get_word(d->config + PCI_CLASS_DEVICE) == + PCI_CLASS_STORAGE_IDE) { + PCIIDEState *pci_ide = DO_UPCAST(PCIIDEState, dev, d); + DriveInfo *di; + int i = 0; + + if (unplug_param & UNPLUG_AUX_IDE_DISKS) + i++; + + for (; i < 3; i++) { + di = drive_get_by_index(IF_IDE, i); + if (di != NULL && di->bdrv != NULL && di->bdrv->type != BDRV_TYPE_CDROM) { + DeviceState *ds = bdrv_get_attached(di->bdrv); + if (ds) + bdrv_detach(di->bdrv, ds); + bdrv_close(di->bdrv); + pci_ide->bus[di->bus].ifs[di->unit].bs = NULL; + drive_put_ref(di); + } + } + qdev_reset_all(&(pci_ide->dev.qdev)); + } +} + +static void pci_unplug_disks(PCIBus *bus) +{ + pci_for_each_device(bus, 0, unplug_disks); +} static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val) { @@ -83,10 +134,20 @@ static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t v switch (addr - XEN_PLATFORM_IOPORT) { case 0: - /* TODO: */ + unplug_param = val; /* Unplug devices. Value is a bitmask of which devices to unplug, with bit 0 the IDE devices, bit 1 the network devices, and bit 2 the non-primary-master IDE devices. */ + if (val & UNPLUG_ALL_IDE_DISKS || val & UNPLUG_AUX_IDE_DISKS) { + DPRINTF("unplug disks\n"); + qemu_aio_flush(); + bdrv_flush_all(); + pci_unplug_disks(s->pci_dev.bus); + } + if (val & UNPLUG_ALL_NICS) { + DPRINTF("unplug nics\n"); + pci_unplug_nics(s->pci_dev.bus); + } break; case 2: switch (val) {