Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2708426ybi; Thu, 4 Jul 2019 17:43:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqwpU2f9yLuqvGlpQz6ICFs0g/LcWDRDUtQqFUU+0bTKpUkWcISAlYpyYp4ix/BWkHtH+4BR X-Received: by 2002:a17:90a:ae12:: with SMTP id t18mr886438pjq.32.1562287385301; Thu, 04 Jul 2019 17:43:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562287385; cv=none; d=google.com; s=arc-20160816; b=WYH2eJE3gvLUrHn4x1rwRWgLMsxyOqd1D2gapLgqgBY7pXiapu9PT6LS5gfHdOkQZi B8x7hwjvnDtMyzqB/4JBsq99HP8CC1rMy9q6ke5bjp2EmcyWDehgjSKhX1LC9IQzmxn3 KYqpAUDpZ4y2LCGKhbUY6tSNC/ul7eMfyNjbj70S5sGgtTiXI5QhenrBx9IsPHpbJJn3 ymzr9xnudDaMQyqcBg4lqnmIV3lkCEWmxIw1QrK/RFwJq6PbMZnRdKgcwATTwy4bU4I3 lsoR/KU3ALDzTX1pQnnYOpLORJAEZ44Ng55jHvBZ060s0NnCpMChx6uVcLS04PbG6w2D mrcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=ZhgddCO9knhT2t5F7GCvMMa0x2kLuQoYQ+0XAvwUw8g=; b=pB2GiGiWGho+/YDPrKiQJrT4LnbdOiF9zzEtkwuZ8w35yoMHOhm+nR5Do68w6Y0ZNt wQqbhP9wlQWoubgoqG8mA03Vp57IjoHLOugJJeRkqQhFMLCm3u2zX4HDxFXW/NhWckR9 3w2TP0NVxH5aUyD9CxFfHLw/X27gXn+MyazbLf8QIdf80VWuIDdNhU5kyh8Q4xb6wr5Q IUS4er9nc2K9Y5YKGneMa37rHUaztL1GUjtSb6wRj3Uf/XR/icaxMTA/VOD2RmTaKj2U vGL9Thyt+fVnwz0AUdU8qZAMmdwJG9nJXYJWcJrGYlTD2KWahtuXMuU/Fwq2Q8K/SREV 7wVw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c64si6106515pje.9.2019.07.04.17.42.50; Thu, 04 Jul 2019 17:43:05 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727861AbfGEAmH (ORCPT + 99 others); Thu, 4 Jul 2019 20:42:07 -0400 Received: from mga03.intel.com ([134.134.136.65]:40861 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727348AbfGEAmF (ORCPT ); Thu, 4 Jul 2019 20:42:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jul 2019 17:42:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,453,1557212400"; d="scan'208";a="185074759" Received: from hao-dev.bj.intel.com ([10.238.157.65]) by fmsmga001.fm.intel.com with ESMTP; 04 Jul 2019 17:42:03 -0700 From: Wu Hao To: gregkh@linuxfoundation.org, mdf@kernel.org, linux-fpga@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, atull@kernel.org, Wu Hao , Zhang Yi Z , Xu Yilun Subject: [PATCH v2 03/11] fpga: dfl: pci: enable SRIOV support. Date: Fri, 5 Jul 2019 08:23:50 +0800 Message-Id: <1562286238-11413-4-git-send-email-hao.wu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1562286238-11413-1-git-send-email-hao.wu@intel.com> References: <1562286238-11413-1-git-send-email-hao.wu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch enables the standard sriov support. It allows user to enable SRIOV (and VFs), then user could pass through accelerators (VFs) into virtual machine or use VFs directly in host. Signed-off-by: Zhang Yi Z Signed-off-by: Xu Yilun Signed-off-by: Wu Hao Acked-by: Alan Tull Acked-by: Moritz Fischer Signed-off-by: Moritz Fischer --- v2: remove DRV/MODULE_VERSION modifications. --- drivers/fpga/dfl-pci.c | 39 +++++++++++++++++++++++++++++++++++++++ drivers/fpga/dfl.c | 41 +++++++++++++++++++++++++++++++++++++++++ drivers/fpga/dfl.h | 1 + 3 files changed, 81 insertions(+) diff --git a/drivers/fpga/dfl-pci.c b/drivers/fpga/dfl-pci.c index 66b5720..0e65d81 100644 --- a/drivers/fpga/dfl-pci.c +++ b/drivers/fpga/dfl-pci.c @@ -223,8 +223,46 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid) return ret; } +static int cci_pci_sriov_configure(struct pci_dev *pcidev, int num_vfs) +{ + struct cci_drvdata *drvdata = pci_get_drvdata(pcidev); + struct dfl_fpga_cdev *cdev = drvdata->cdev; + int ret = 0; + + mutex_lock(&cdev->lock); + + if (!num_vfs) { + /* + * disable SRIOV and then put released ports back to default + * PF access mode. + */ + pci_disable_sriov(pcidev); + + __dfl_fpga_cdev_config_port_vf(cdev, false); + + } else if (cdev->released_port_num == num_vfs) { + /* + * only enable SRIOV if cdev has matched released ports, put + * released ports into VF access mode firstly. + */ + __dfl_fpga_cdev_config_port_vf(cdev, true); + + ret = pci_enable_sriov(pcidev, num_vfs); + if (ret) + __dfl_fpga_cdev_config_port_vf(cdev, false); + } else { + ret = -EINVAL; + } + + mutex_unlock(&cdev->lock); + return ret; +} + static void cci_pci_remove(struct pci_dev *pcidev) { + if (dev_is_pf(&pcidev->dev)) + cci_pci_sriov_configure(pcidev, 0); + cci_remove_feature_devs(pcidev); pci_disable_pcie_error_reporting(pcidev); } @@ -234,6 +272,7 @@ static void cci_pci_remove(struct pci_dev *pcidev) .id_table = cci_pcie_id_tbl, .probe = cci_pci_probe, .remove = cci_pci_remove, + .sriov_configure = cci_pci_sriov_configure, }; module_pci_driver(cci_pci_driver); diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c index e04ed45..c3a8e1d 100644 --- a/drivers/fpga/dfl.c +++ b/drivers/fpga/dfl.c @@ -1112,6 +1112,47 @@ int dfl_fpga_cdev_config_port(struct dfl_fpga_cdev *cdev, int port_id, } EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_port); +static void config_port_vf(struct device *fme_dev, int port_id, bool is_vf) +{ + void __iomem *base; + u64 v; + + base = dfl_get_feature_ioaddr_by_id(fme_dev, FME_FEATURE_ID_HEADER); + + v = readq(base + FME_HDR_PORT_OFST(port_id)); + + v &= ~FME_PORT_OFST_ACC_CTRL; + v |= FIELD_PREP(FME_PORT_OFST_ACC_CTRL, + is_vf ? FME_PORT_OFST_ACC_VF : FME_PORT_OFST_ACC_PF); + + writeq(v, base + FME_HDR_PORT_OFST(port_id)); +} + +/** + * __dfl_fpga_cdev_config_port_vf - configure port to VF access mode + * + * @cdev: parent container device. + * @if_vf: true for VF access mode, and false for PF access mode + * + * Return: 0 on success, negative error code otherwise. + * + * This function is needed in sriov configuration routine. It could be used to + * configures the released ports access mode to VF or PF. + * The caller needs to hold lock for protection. + */ +void __dfl_fpga_cdev_config_port_vf(struct dfl_fpga_cdev *cdev, bool is_vf) +{ + struct dfl_feature_platform_data *pdata; + + list_for_each_entry(pdata, &cdev->port_dev_list, node) { + if (device_is_registered(&pdata->dev->dev)) + continue; + + config_port_vf(cdev->fme_dev, pdata->id, is_vf); + } +} +EXPORT_SYMBOL_GPL(__dfl_fpga_cdev_config_port_vf); + static int __init dfl_fpga_init(void) { int ret; diff --git a/drivers/fpga/dfl.h b/drivers/fpga/dfl.h index d700ee9..061ccd4 100644 --- a/drivers/fpga/dfl.h +++ b/drivers/fpga/dfl.h @@ -421,5 +421,6 @@ struct platform_device * int dfl_fpga_cdev_config_port(struct dfl_fpga_cdev *cdev, int port_id, bool release); +void __dfl_fpga_cdev_config_port_vf(struct dfl_fpga_cdev *cdev, bool is_vf); #endif /* __FPGA_DFL_H */ -- 1.8.3.1