From patchwork Thu Apr 9 15:55:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 6188861 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9ECD59F2E9 for ; Thu, 9 Apr 2015 15:55:28 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C1A112037A for ; Thu, 9 Apr 2015 15:55:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EA8D920357 for ; Thu, 9 Apr 2015 15:55:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755581AbbDIPzY (ORCPT ); Thu, 9 Apr 2015 11:55:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53479 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755461AbbDIPzX (ORCPT ); Thu, 9 Apr 2015 11:55:23 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t39FtNtd032579 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 9 Apr 2015 11:55:23 -0400 Received: from gimli.home (ovpn-113-40.phx2.redhat.com [10.3.113.40]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t39FtMAb027010; Thu, 9 Apr 2015 11:55:23 -0400 Subject: [PATCH] vfio-pci: Attempt bus/slot reset on open From: Alex Williamson To: alex.williamson@redhat.com Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Date: Thu, 09 Apr 2015 09:55:22 -0600 Message-ID: <20150409155229.6935.47756.stgit@gimli.home> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, 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 Apply the same logic we use in vfio_pci_try_bus_reset() to perform a bus or slot reset as devices are released to the device open path. This not only improves our chances of handing the device to the user in a clean state, but we can also avoid duplicate resets if the device is released and re-opened while bound to vfio-pci. We can also potentially reset multiple device with a single bus/slot reset and avoid multiple function-level resets later. We switch to using pci_probe_reset_function() since we still want to expose function level reset to the user, but we may not use it as the reset mechanism of choice when enabling the device. Signed-off-by: Alex Williamson --- Depends on export of pci_probe_reset_function() http://article.gmane.org/gmane.linux.kernel.pci/40662 drivers/vfio/pci/vfio_pci.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 69fab0f..d9f7ec5 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -119,16 +119,32 @@ static int vfio_pci_enable(struct vfio_pci_device *vdev) u16 cmd; u8 msix_pos; - pci_set_power_state(pdev, PCI_D0); + /* + * Start by resetting the device(s). A bus reset is our best method + * and may not be available once other devices are in use. It may + * also mark multiple devices as clean, avoiding duplicate function + * level resets later. + */ + vfio_pci_try_bus_reset(vdev); + if (vdev->needs_reset) + vdev->needs_reset = !(pci_try_reset_function(pdev) == 0); - /* Don't allow our initial saved state to include busmaster */ + /* + * These are reset defaults, but let's be sure the device is awake + * and not doing DMA. We do not want BusMaster enabled in save state. + */ + pci_set_power_state(pdev, PCI_D0); pci_clear_master(pdev); + /* Initialize device */ ret = pci_enable_device(pdev); if (ret) return ret; - vdev->reset_works = (pci_reset_function(pdev) == 0); + /* + * Checkpoint, this is the base level state we want to + * restore when the device is released. + */ pci_save_state(pdev); vdev->pci_saved_state = pci_store_saved_state(pdev); if (!vdev->pci_saved_state) @@ -948,6 +964,8 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) vdev->irq_type = VFIO_PCI_NUM_IRQS; mutex_init(&vdev->igate); spin_lock_init(&vdev->irqlock); + vdev->reset_works = (pci_probe_reset_function(pdev) == 0); + vdev->needs_reset = true; ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); if (ret) {