Received: by 10.223.185.116 with SMTP id b49csp2213742wrg; Thu, 22 Feb 2018 09:54:27 -0800 (PST) X-Google-Smtp-Source: AH8x2255VE4BlVP1ucsaAuMWg8ZW/fKFaaE72Rh3ojsqkiv5D5n49wAFwPEhkeYeUIB0NIPJgeSW X-Received: by 10.98.58.129 with SMTP id v1mr7667464pfj.203.1519322067737; Thu, 22 Feb 2018 09:54:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519322067; cv=none; d=google.com; s=arc-20160816; b=R1w/5Zdq8Atf5B2RsQYs4D00YHG6B840XrxC+ExaJwk/ZPl86SIDZUWc5kvUuMvB2j G2i4jmUR5eb25aly9yOULZC0+fbkR4+R7uGTWaHlw8q2WZJyiuDT1QdutxFzmIOCf5pz 3EPLVFV7BhbIfTZNvH/pVIlmXTx9rDmryYNB4BC6Qcr2qFaYjFHxWVrh2K6vtNLE89+H tAtm4tmsLrHNgsUes011W9gS1m5swjTW3GiO75CazRB0lNt4h4nm3GjDh4lXuNO6it8H 9huAIEwt9p+SBArxhHJWzGk1wKOubnUANioz3JzcI8cg5QDXYX1iNT8tAiIXK26+0rhB EuQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:from:to:subject :arc-authentication-results; bh=j9qIR9dQvaJl46Hpf0GpzbaDRw463QysY/d3RYrkQck=; b=Uphd98zHldXOZsxH+eKziIgAaI4ToH2eEGkus2SvHs3/MCN0VAjcRzsBRcbw/eb10S kxex1NSx4PTJy7RzILAu4ytL92hd/W6S6ywv259jqufUf7pThkXWSUGatUYxvQLdTmd/ O5C3UneVF3Viywg7aHJsTjhByvHWkPSKLl+NAzSfUC0vyNqww1BjgAEUi/TRCZHGrNlD 7C+MKdTpMyB+paU40LLK68GFlfrLJ6c/75IGe8BHz1ZvOAVIEAQOvIGJMHTmPsiu6oXK AiIlrF/n8YudSeJHuiBZUHxZQiXUX1RrvW//kbkZWUZw6wDm1RRGQZiM6qTPHhzF6nS8 DYIQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k195si322446pga.68.2018.02.22.09.54.12; Thu, 22 Feb 2018 09:54:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933553AbeBVRwz (ORCPT + 99 others); Thu, 22 Feb 2018 12:52:55 -0500 Received: from mga02.intel.com ([134.134.136.20]:60520 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933581AbeBVRwu (ORCPT ); Thu, 22 Feb 2018 12:52:50 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Feb 2018 09:52:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,378,1515484800"; d="scan'208";a="203012116" Received: from mdrustad-mac04.sj.intel.com (HELO mdrustad-mac04.local) ([10.233.81.27]) by orsmga005.jf.intel.com with ESMTP; 22 Feb 2018 09:52:48 -0800 Subject: [RFC PATCH V2] virtio_pci: Add SR-IOV support To: virtio-dev@lists.oasis-open.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org From: Mark Rustad Cc: dan.daly@intel.com, alex.williamson@redhat.com, MRustad@gmail.com, alexander.duyck@gmail.com, mst@redhat.com Date: Thu, 22 Feb 2018 09:52:50 -0800 Message-ID: <20180222175250.97135.98386.stgit@mdrustad-mac04.local> User-Agent: StGit/0.15 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hardware-realized virtio-pci devices can implement SR-IOV, so this patch enables its use. The device in question is an upcoming Intel NIC that implements both a virtio-net PF and virtio-net VFs. These are hardware realizations of what has been up to now been a software interface. The device in question has the following 4-part PCI IDs: PF: device: 1af4 vendor: 1041 subvendor: 8086 subdevice: 15fe VF: device: 1af4 vendor: 1041 subvendor: 8086 subdevice: 05fe The patch needs no check for device ID, because the callback will never be made for devices that do not assert the capability or when run on a platform incapable of SR-IOV. One reason for this patch is because the hardware requires the vendor ID of a VF to be the same as the vendor ID of the PF that created it. So it seemed logical to simply have a fully-functioning virtio-net PF create the VFs. This patch makes that possible. Signed-off-by: Mark Rustad Reviewed-by: Alexander Duyck --- Changes in V2: - Simplified logic from previous version, removed added driver variable - Disable SR-IOV on driver removal excapt when VFs are assigned - Sent as RFC to virtio-dev, linux-pci, netdev, lkml and others --- drivers/virtio/virtio_pci_common.c | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c index 48d4d1cf1cb6..78b53ffc4cee 100644 --- a/drivers/virtio/virtio_pci_common.c +++ b/drivers/virtio/virtio_pci_common.c @@ -572,6 +572,47 @@ static int virtio_pci_probe(struct pci_dev *pci_dev, return rc; } +#ifdef CONFIG_PCI_IOV +static int virtio_pci_sriov_disable(struct pci_dev *pci_dev) +{ + /* If vfs are assigned we cannot shut down SR-IOV without causing + * issues, so just leave the hardware available. + */ + if (pci_vfs_assigned(pci_dev)) { + dev_warn(&pci_dev->dev, + "Unloading driver while VFs are assigned - VFs will not be deallocated\n"); + return -EPERM; + } + pci_disable_sriov(pci_dev); + return 0; +} + +static int virtio_pci_sriov_enable(struct pci_dev *pci_dev, int num_vfs) +{ + int rc = 0; + + if (pci_num_vf(pci_dev)) + return -EINVAL; + + rc = pci_enable_sriov(pci_dev, num_vfs); + if (rc) { + dev_warn(&pci_dev->dev, "Failed to enable PCI sriov: %d\n", rc); + return rc; + } + dev_info(&pci_dev->dev, "SR-IOV enabled with %d VFs\n", num_vfs); + return num_vfs; +} + +static int virtio_pci_sriov_configure(struct pci_dev *dev, int num_vfs) +{ + if (num_vfs) + return virtio_pci_sriov_enable(dev, num_vfs); + if (!pci_num_vf(dev)) + return -EINVAL; + return virtio_pci_sriov_disable(dev); +} +#endif /* CONFIG_PCI_IOV */ + static void virtio_pci_remove(struct pci_dev *pci_dev) { struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); @@ -584,6 +625,9 @@ static void virtio_pci_remove(struct pci_dev *pci_dev) else virtio_pci_modern_remove(vp_dev); +#ifdef CONFIG_PCI_IOV + virtio_pci_sriov_disable(pci_dev); +#endif pci_disable_device(pci_dev); put_device(dev); } @@ -596,6 +640,9 @@ static void virtio_pci_remove(struct pci_dev *pci_dev) #ifdef CONFIG_PM_SLEEP .driver.pm = &virtio_pci_pm_ops, #endif +#ifdef CONFIG_PCI_IOV + .sriov_configure = virtio_pci_sriov_configure, +#endif }; module_pci_driver(virtio_pci_driver);