KVM Archive on lore.kernel.org
 help / color / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: alex.williamson@redhat.com
Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
Subject: [PATCH] vfio-pci: Attempt bus/slot reset on open
Date: Thu, 09 Apr 2015 09:55:22 -0600
Message-ID: <20150409155229.6935.47756.stgit@gimli.home> (raw)

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 <alex.williamson@redhat.com>

Depends on export of pci_probe_reset_function()

 drivers/vfio/pci/vfio_pci.c |   24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

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);
+	/* 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.
+	 */
 	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;
+	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) {

                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150409155229.6935.47756.stgit@gimli.home \
    --to=alex.williamson@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

KVM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kvm/0 kvm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kvm kvm/ https://lore.kernel.org/kvm \
	public-inbox-index kvm

Example config snippet for mirrors

Newsgroup available over NNTP:

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git