Received: by 10.223.185.116 with SMTP id b49csp3298822wrg; Sun, 25 Feb 2018 19:20:12 -0800 (PST) X-Google-Smtp-Source: AH8x225vRs6OMEXrE3FD819PGVT+sotoeK80kcg8AZFsOMSWRz440ZF65TEv5nAWIWLEKmOVAZQo X-Received: by 10.99.179.77 with SMTP id x13mr7370320pgt.148.1519615212143; Sun, 25 Feb 2018 19:20:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519615212; cv=none; d=google.com; s=arc-20160816; b=bpyl3Immkg6E3fGXKsPBmne6psCQ4uADJIx8U4/4n99MtqJP5hz7nSmTOdMLGdFLFc NyVPIAT4oaZzsgvezrlwK1eySoN3WBvpzx0r1AbNCEkV8fgISPzZxL61JOWhYKPg732V 7eVnRNREFL9e+PcnAVGOFHSDH1UkqKG72IYVqaQvn4qUrel2UbkO/IfIGSqAjAGsmGDx 9r07STSVgTE3yIsPExvLbZhI5n5lu/8WBZWx+jJkoun09hpR5A3dfADhI5tHOgmaha0i 2kSudRMtcTrFE34Ea/0i6VmeyAC88UCBgziOIOFQfKG6fPpVb9WwZ0DKhQM9+8a5wLV9 IO7g== 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=vL5JCiLQh3NH2CnElnbTCi14ya8L0+Z3GapKCBMp75I=; b=gEm+FB6l7Y4nN3lRxoiEgBvAsnOFQGvipl6JUuyH+wpn/nPkkOxvyE6S67AC0SBHxr nL6R4tF/X2TOGy02Fb6E95Y5/Go4zm0lEtRrSKv94QwF8eBfvphv5ODV98Wtj/fVjlq/ YysBDC3dU8h8C3rwsfbKmxGMxEKRUyR+E2EwMHNZAHGlBBu7F8a/7jEIUz+3LYXsmmLl ZxqQ4GQTnFbQd+oZA1KdWWrPDb9HPFPYv/gU6wb6IgXmhPtpoQskJ9amnqGltCJKNH9x 6wol6Ye1FcN1PhwY3jM/Y6ZVPjnV/8LVUPPdgy1hSUb2HQnm63PT4f8wb+6nmVF2EgFa +RBw== 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 y18si5304819pfa.9.2018.02.25.19.19.57; Sun, 25 Feb 2018 19:20:12 -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 S1752118AbeBZDTP (ORCPT + 99 others); Sun, 25 Feb 2018 22:19:15 -0500 Received: from mga11.intel.com ([192.55.52.93]:33155 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751958AbeBZDTN (ORCPT ); Sun, 25 Feb 2018 22:19:13 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Feb 2018 19:19:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,395,1515484800"; d="scan'208";a="20265594" Received: from mdrustad-mac04.sj.intel.com (HELO mdrustad-mac04.local) ([10.233.80.11]) by orsmga007.jf.intel.com with ESMTP; 25 Feb 2018 19:19:11 -0800 Subject: [RFC PATCH V3] 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: Sun, 25 Feb 2018 19:19:11 -0800 Message-ID: <20180226031911.18980.80488.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 V3: - Move most code to a new helper in pci/iov.c, pci_sriov_configure 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/pci/iov.c | 48 ++++++++++++++++++++++++++++++++++++ drivers/virtio/virtio_pci_common.c | 2 ++ include/linux/pci.h | 10 ++++++++ 3 files changed, 60 insertions(+) diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c index 677924ae0350..ddd44a9d93ec 100644 --- a/drivers/pci/iov.c +++ b/drivers/pci/iov.c @@ -367,6 +367,54 @@ static void sriov_disable(struct pci_dev *dev) pci_iov_set_numvfs(dev, 0); } +/** + * pci_sriov_disable - standard helper to disable SR-IOV + * @dev:the PCI PF device whose VFs are to be disabled + */ +int pci_sriov_disable(struct pci_dev *dev) +{ + /* + * If vfs are assigned we cannot shut down SR-IOV without causing + * issues, so just leave the hardware available. + */ + if (pci_vfs_assigned(dev)) { + pci_warn(&dev->dev, + "Cannot disable SR-IOV while VFs are assigned - VFs will not be deallocated\n"); + return -EPERM; + } + pci_disable_sriov(dev); + return 0; +} + +static int pci_sriov_enable(struct pci_dev *dev, int num_vfs) +{ + int rc; + + if (pci_num_vf(dev)) + return -EINVAL; + + rc = pci_enable_sriov(dev, num_vfs); + if (rc) { + pci_warn(dev, "Failed to enable PCI sriov: %d\n", rc); + return rc; + } + dev_info(dev, "SR-IOV enabled with %d VFs\n", num_vfs); + return num_vfs; +} + +/** + * pci_sriov_configure - standard helper to configure SR-IOV + * @dev: the PCI PF device that is configuring SR-IOV + */ +int pci_sriov_configure(struct pci_dev *dev, int num_vfs) +{ + if (num_vfs) + return pci_sriov_enable(dev, num_vfs); + if (!pci_num_vf(dev)) + return -EINVAL; + return pci_sriov_disable(dev); +} + static int sriov_init(struct pci_dev *dev, int pos) { int i, bar64; diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c index 48d4d1cf1cb6..37e353c4f8b4 100644 --- a/drivers/virtio/virtio_pci_common.c +++ b/drivers/virtio/virtio_pci_common.c @@ -584,6 +584,7 @@ static void virtio_pci_remove(struct pci_dev *pci_dev) else virtio_pci_modern_remove(vp_dev); + pci_sriov_disable(pci_dev); pci_disable_device(pci_dev); put_device(dev); } @@ -596,6 +597,7 @@ static struct pci_driver virtio_pci_driver = { #ifdef CONFIG_PM_SLEEP .driver.pm = &virtio_pci_pm_ops, #endif + .sriov_configure = pci_sriov_configure, }; module_pci_driver(virtio_pci_driver); diff --git a/include/linux/pci.h b/include/linux/pci.h index 024a1beda008..ef6b359afefd 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1947,6 +1947,8 @@ int pci_iov_virtfn_devfn(struct pci_dev *dev, int id); int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn); void pci_disable_sriov(struct pci_dev *dev); +int pci_sriov_disable(struct pci_dev *dev); +int pci_sriov_configure(struct pci_dev *dev, int num_vfs); int pci_iov_add_virtfn(struct pci_dev *dev, int id); void pci_iov_remove_virtfn(struct pci_dev *dev, int id); int pci_num_vf(struct pci_dev *dev); @@ -1973,6 +1975,14 @@ static inline int pci_iov_add_virtfn(struct pci_dev *dev, int id) static inline void pci_iov_remove_virtfn(struct pci_dev *dev, int id) { } static inline void pci_disable_sriov(struct pci_dev *dev) { } +static inline int pci_sriov_disable(struct pci_dev *dev) +{ + return -ENODEV; +} +static inline int pci_sriov_configure(struct pci_dev *dev, int num_vfs) +{ + return -ENODEV; +} static inline int pci_num_vf(struct pci_dev *dev) { return 0; } static inline int pci_vfs_assigned(struct pci_dev *dev) { return 0; }