From patchwork Wed Feb 17 10:43:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cao jin X-Patchwork-Id: 8336561 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7539CC02AA for ; Wed, 17 Feb 2016 10:46:13 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B3C51202E9 for ; Wed, 17 Feb 2016 10:46:12 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DD15220272 for ; Wed, 17 Feb 2016 10:46:11 +0000 (UTC) Received: from localhost ([::1]:56213 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVzcZ-0007iO-Ak for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Feb 2016 05:46:11 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33565) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVzaL-0003xc-Hc for qemu-devel@nongnu.org; Wed, 17 Feb 2016 05:43:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aVzaK-0003oV-Db for qemu-devel@nongnu.org; Wed, 17 Feb 2016 05:43:53 -0500 Received: from [59.151.112.132] (port=35019 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aVzaH-0003hj-JI for qemu-devel@nongnu.org; Wed, 17 Feb 2016 05:43:52 -0500 X-IronPort-AV: E=Sophos;i="5.20,346,1444665600"; d="scan'208";a="3636115" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 17 Feb 2016 18:43:41 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 8BE3942AC840; Wed, 17 Feb 2016 18:41:59 +0800 (CST) Received: from G08FNSTD140223.g08.fujitsu.local (10.167.226.69) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Wed, 17 Feb 2016 18:43:35 +0800 From: Cao jin To: Date: Wed, 17 Feb 2016 18:43:50 +0800 Message-ID: <1455705833-17682-7-git-send-email-caoj.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1455705833-17682-1-git-send-email-caoj.fnst@cn.fujitsu.com> References: <1455705833-17682-1-git-send-email-caoj.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.69] X-yoursite-MailScanner-ID: 8BE3942AC840.AC3F6 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: caoj.fnst@cn.fujitsu.com X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: chen.fan.fnst@cn.fujitsu.com, izumi.taku@jp.fujitsu.com, alex.williamson@redhat.com, mst@redhat.com Subject: [Qemu-devel] [PATCH 06/10] add check reset mechanism when hotplug vfio device 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Chen Fan Since we support multi-function hotplug, the function 0 indicates the closure of the slot, so we have the chance to do the check. Signed-off-by: Chen Fan --- hw/pci/pci.c | 29 +++++++++++++++++++++++++++++ hw/vfio/pci.c | 19 +++++++++++++++++++ hw/vfio/pci.h | 2 ++ include/hw/pci/pci_bus.h | 5 +++++ 4 files changed, 55 insertions(+) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index d940f79..e41925e 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -82,6 +82,7 @@ static void pci_bus_realize(BusState *qbus, Error **errp) PCIBus *bus = PCI_BUS(qbus); vmstate_register(NULL, -1, &vmstate_pcibus, bus); + notifier_with_return_list_init(&bus->hotplug_notifiers); } static void pci_bus_unrealize(BusState *qbus, Error **errp) @@ -1836,6 +1837,22 @@ PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) return bus->devices[devfn]; } +void pci_bus_add_hotplug_notifier(PCIBus *bus, NotifierWithReturn *notify) +{ + notifier_with_return_list_add(&bus->hotplug_notifiers, notify); +} + +void pci_bus_remove_hotplug_notifier(NotifierWithReturn *notifier) +{ + notifier_with_return_remove(notifier); +} + +static int pci_bus_hotplug_notifier(PCIBus *bus, void *opaque) +{ + return notifier_with_return_list_notify(&bus->hotplug_notifiers, + opaque); +} + static void pci_qdev_realize(DeviceState *qdev, Error **errp) { PCIDevice *pci_dev = (PCIDevice *)qdev; @@ -1878,6 +1895,18 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) pci_qdev_unrealize(DEVICE(pci_dev), NULL); return; } + + /* + * If the function is func 0, indicate the closure of the slot. + * signal the callback. + */ + if (DEVICE(pci_dev)->hotplugged && + pci_get_function_0(pci_dev) == pci_dev && + pci_bus_hotplug_notifier(bus, pci_dev)) { + error_setg(errp, "failed to hotplug function 0"); + pci_qdev_unrealize(DEVICE(pci_dev), NULL); + return; + } } static void pci_default_realize(PCIDevice *dev, Error **errp) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 54032d0..b900f70 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2076,6 +2076,19 @@ static int vfio_check_devices_host_bus_reset(void) return 0; } +static int vfio_check_bus_reset(NotifierWithReturn *n, void *opaque) +{ + VFIOPCIDevice *vdev = container_of(n, VFIOPCIDevice, hotplug_notifier); + PCIDevice *pci_dev = PCI_DEVICE(vdev); + PCIDevice *pci_func0 = opaque; + + if (pci_get_function_0(pci_dev) != pci_func0) { + return 0; + } + + return vfio_check_host_bus_reset(vdev); +} + static int vfio_setup_aer(VFIOPCIDevice *vdev, uint8_t cap_ver, int pos, uint16_t size) { @@ -2123,6 +2136,9 @@ static int vfio_setup_aer(VFIOPCIDevice *vdev, uint8_t cap_ver, pdev->exp.aer_log.log_max = 0; } + vdev->hotplug_notifier.notify = vfio_check_bus_reset; + pci_bus_add_hotplug_notifier(pdev->bus, &vdev->hotplug_notifier); + pcie_cap_deverr_init(pdev); return pcie_aer_init(pdev, pos, size); @@ -3006,6 +3022,9 @@ static void vfio_exitfn(PCIDevice *pdev) vfio_unregister_req_notifier(vdev); vfio_unregister_err_notifier(vdev); pci_device_set_intx_routing_notifier(&vdev->pdev, NULL); + if (vdev->features & VFIO_FEATURE_ENABLE_AER) { + pci_bus_remove_hotplug_notifier(&vdev->hotplug_notifier); + } vfio_disable_interrupts(vdev); if (vdev->intx.mmap_timer) { timer_free(vdev->intx.mmap_timer); diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index aff46c2..bf9e93c 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -143,6 +143,8 @@ typedef struct VFIOPCIDevice { bool no_kvm_intx; bool no_kvm_msi; bool no_kvm_msix; + + NotifierWithReturn hotplug_notifier; } VFIOPCIDevice; uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len); diff --git a/include/hw/pci/pci_bus.h b/include/hw/pci/pci_bus.h index 403fec6..7812fa9 100644 --- a/include/hw/pci/pci_bus.h +++ b/include/hw/pci/pci_bus.h @@ -39,8 +39,13 @@ struct PCIBus { Keep a count of the number of devices with raised IRQs. */ int nirq; int *irq_count; + + NotifierWithReturnList hotplug_notifiers; }; +void pci_bus_add_hotplug_notifier(PCIBus *bus, NotifierWithReturn *notify); +void pci_bus_remove_hotplug_notifier(NotifierWithReturn *notify); + typedef struct PCIBridgeWindows PCIBridgeWindows; /*