From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 13BCE45501; Wed, 26 Jun 2024 21:56:44 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6BB4440696; Wed, 26 Jun 2024 21:56:11 +0200 (CEST) Received: from egress-ip42a.ess.de.barracuda.com (egress-ip42a.ess.de.barracuda.com [18.185.115.201]) by mails.dpdk.org (Postfix) with ESMTP id E82DD4026E for ; Wed, 26 Jun 2024 21:56:05 +0200 (CEST) Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02lp2104.outbound.protection.outlook.com [104.47.11.104]) by mx-outbound42-4.eu-central-1c.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 26 Jun 2024 19:56:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OeVvMokyuh0z62GcgnTr2L52b4kUbr6lARiVabsEDjmkaxO8xgyrGPsix16b2ea6UtBYO5CE5xxmP9LB1cbrXMmwCjoybzZ34o6WfW2M8zS0MPHVHEZazr6x8IeJ2T7eUVXYrdeeoCbHzzpV3xPnSb6sd6RmyQ+/0tuL4YYFpo+EtWvUx2oFn+t7yxDpfXPNX4ttQq4P8oAurGIoqGEb5GN3Jh8uLd2ElDEMI5Vp7wCIgfST1NNDSYSfWto3VbbFymGV1ohpk2TGiPK/rMhwQaMd7gdrzSf3OrLFkWFcCSogfMW+99F5BSbm1yevSe21cyc3boETyfoZa+BvRhORig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wVSImpkEG0L+LbsjKneTELaHwV9f+uASfpK0h4SHQNE=; b=KPtLcaCqU+NxrvZeVBM2PN763zQqAuXHOvXxgqw28H9jADBppZ4/+pYVIT+fZleXHqDwOg6bv+5PkvC8qJXQBHHATfJr0lsL7etB58nDDhgkdvcNSjT7T9t/cSVYcrarDnek5kOqEVVjAtXgv6l3wJpc3iQMnna2VIVyMfguYWjDa2AMF3Lrx32U6ir2mcsAVT5HJGbpJyZfHxlM4gX69MLGxOUZzi8P2QZUurf8mnW+wksQCqWnV6XpK7sMBr/AiDmUmqjtLqRBjMBSEX//W4OdcfwZ8dFWee1mzkOCXEMDjgSLtiHJPcC0GzhenynLmrO+n/uQqA09UcVk+OgbrQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=fail (sender ip is 178.72.21.4) smtp.rcpttodomain=dpdk.org smtp.mailfrom=napatech.com; dmarc=fail (p=reject sp=reject pct=100) action=oreject header.from=napatech.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=napatech.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wVSImpkEG0L+LbsjKneTELaHwV9f+uASfpK0h4SHQNE=; b=KJM2WQXUwIY2NPejLJWHKmhz+tkWTpBcRsB3MXvTI3cDZO5Oh10i8AWaReFrSbSCVAG9b473q/EOiMglQBvQa3TMSdB9a5lmqtzhXg08yy1FCWyKtz4/pJVTZYhGWrxDAUILEsiRuI+7NS2z0ToXX+4siOGgfTyJa/F3Z+0gUXk= Received: from AS4P250CA0025.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:5e3::15) by GVXP190MB2076.EURP190.PROD.OUTLOOK.COM (2603:10a6:150:149::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.30; Wed, 26 Jun 2024 19:56:00 +0000 Received: from AMS0EPF000001AC.eurprd05.prod.outlook.com (2603:10a6:20b:5e3:cafe::cf) by AS4P250CA0025.outlook.office365.com (2603:10a6:20b:5e3::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7719.22 via Frontend Transport; Wed, 26 Jun 2024 19:56:00 +0000 X-MS-Exchange-Authentication-Results: spf=fail (sender IP is 178.72.21.4) smtp.mailfrom=napatech.com; dkim=none (message not signed) header.d=none;dmarc=fail action=oreject header.from=napatech.com; Received-SPF: Fail (protection.outlook.com: domain of napatech.com does not designate 178.72.21.4 as permitted sender) receiver=protection.outlook.com; client-ip=178.72.21.4; helo=localhost.localdomain; Received: from localhost.localdomain (178.72.21.4) by AMS0EPF000001AC.mail.protection.outlook.com (10.167.16.152) with Microsoft SMTP Server id 15.20.7677.15 via Frontend Transport; Wed, 26 Jun 2024 19:56:00 +0000 From: Serhii Iliushyk To: dev@dpdk.org Cc: mko-plv@napatech.com, sil-plv@napatech.com, ckm@napatech.com, andrew.rybchenko@oktetlabs.ru, ferruh.yigit@amd.com Subject: [PATCH v4 05/23] net/ntnic: add VFIO module Date: Wed, 26 Jun 2024 21:55:15 +0200 Message-ID: <20240626195545.1793419-5-sil-plv@napatech.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240626195545.1793419-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> <20240626195545.1793419-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS0EPF000001AC:EE_|GVXP190MB2076:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 2441b2de-04cf-4de7-2cad-08dc961a00d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230038|1800799022|82310400024|36860700011|376012; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?7SjeSqND6kojjJLjPhqk4byI3iwyqb+J9a+D/J9wxZ1bdI1OpP8RXwCygdo/?= =?us-ascii?Q?0qCQQUhP3fXcnyEOUdp+ILDSg6NnJ7FW1GET5skF0kPbCxN4r52Ap5FV7vCe?= =?us-ascii?Q?mHUpZW8W+7Z57O9z78yc5MlsUKqkGB6nUO81lrU9qlsx9Q5TtTrKMqmceZEt?= =?us-ascii?Q?g5JG3bNo8wcYd3MsbFvbrC8ecN0PytNKpG4Gs4QaTzN/+GJiWlIn6dVi537L?= =?us-ascii?Q?l8vOmF7eYXFBIHQX3TKaWoaHLH3CozV/tDXoRAwdhbKB1EydiRpJl6tSC0xr?= =?us-ascii?Q?ot0XATeu+tjEDF31wfzr5Ah6VHUTuiIIZ4t1Leq4OAMqH3yKjsvtiXEDHRCc?= =?us-ascii?Q?mE9ECJmw5zBGnmzC3p6egVxvphz6DjDzP/DeN8NvPJnqvvNx0q6Xfvd4w9Wn?= =?us-ascii?Q?qfhUUWFjEl1N7+7xRJ8FHu/hFjMTgBEIDR1K2MowzWCS0vrYPF4Q4IU1NnDN?= =?us-ascii?Q?aur7KIUaIfypaSwqowKUHDE4hlw/qa0iHV2VnoiMHwbICmbSGUhQCIDgBX9g?= =?us-ascii?Q?XJRTUINxPbGfKq0KBOI3awC6OOLsOR4qNyEvEjLAwtK20VpJE9mjt6C4bw9H?= =?us-ascii?Q?63e7xmeXPFDuiE7HDZKX3LpomSJkKD5a5b/j9RSDKuzAKJF+hAnBi16lUcBV?= =?us-ascii?Q?R1Y2HHjVoAzZlYXDO3HKBr8jZCSr+xgmHBFpvAt0ox3XwlmrfPQ+gsC0cIz3?= =?us-ascii?Q?l1cSgJLDXLIfpxaYEQhcRAAloGiB1IEtt3XCvFXtBLdY32bXKkHUMp0+5i7F?= =?us-ascii?Q?L2jlj1rQl2n25hZWkB+0TMGcZaPvCtNlzRt8h0WLuK+vdCQ+N9FY/qs/oM7s?= =?us-ascii?Q?Z4w9OZGrQCGABbGSxM3/ysOM4lGzlVwzMR5b6Wugav5XzecQVRmpA9kyLaAL?= =?us-ascii?Q?XRXumIbK1ZE3ggZ71LMj9HdFM3WpksxX6XDAVcANZwuhvGgUTbHibr4S8X9Z?= =?us-ascii?Q?fqHRFKct7xO4VfoTt6A38b/MW2cHPG4035d+EEwM8mjyUBen3qzsF8VveVw/?= =?us-ascii?Q?J6nKYcKXn7bOAolctJwJSgwCZthH9XLeCguFzy6SoS98mMdmMnUdJhGBgyma?= =?us-ascii?Q?ABqNtyH0oVw29OsELOaXgepXESfMH4jPoxjA3HWp+ETMxRw4RCTQv0B/1/xC?= =?us-ascii?Q?rnLkz5+JaOSbwpgrsR186RnUrzhcGrBsx9Vcys8JXoKCoQX09hsNtCZWPDIF?= =?us-ascii?Q?+kEVFqFZECB4F+ouVNSfEwSIW0G40NgMAQp0vwZZOMHnqaBZhFzGwcNGTVBg?= =?us-ascii?Q?JPQAwmPdD7o2khJv27QskG6shUJSUjKhLbmXGXb1uqYEWDGToYRpkrLa4/Gd?= =?us-ascii?Q?PjunXNPADmdeQxrxLx/KrbcUU9x78RSIey5S19xfcKMedV+RRkDnnINvz1Wy?= =?us-ascii?Q?hazmyArljsJ0XHIOIZPz+jhD2u3G4AKz8DTDKzwAQhSMAmHjixuLwHwqBnhz?= =?us-ascii?Q?SwgyCZWHy46hSKAQrSnF3MJYKQikLu/G?= X-Forefront-Antispam-Report: CIP:178.72.21.4; CTRY:DK; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:localhost.localdomain; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230038)(1800799022)(82310400024)(36860700011)(376012); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 1vK6noS2ANe5LC58z38UvwvAnxQRx3JQquNnAsxSs1yq9IlkSqzcIJ+jDrxXTIX8szrbWWQIlDxrBdVmUnusiPMT/+ZEjsLJBzbiGkUwOoDJu04Yuz7zwacxUpHJkabcjo6LQ+Yfw3BwSfI+2LXm2/8VUhD1jVvTLqV8cMsnPt9YMzGeYJRyhN4slvfMNrbazL6c3gaqZfMURF1H0JiDJZLdFuI7y/sxtNFZ1PRUqj7APyMKlW7aUxWXFGL2uWh6YFIZPA+DrcF4bmvEsN/teM4r0mvxIG3QpyhBNKQYL5jr/rIhtWRY8auGoUeEGfqpSr75gZ+AjmybOsCkEfdkTgTKbLg0nfkmOZ5r73zwhQ0Xz7mm1PbFOzr7PoDpeYas1s9ttY53HMa/KhDnX5RqoWuO+HzjFi+TnhlsXvVMcswtwt+0WkCR8lNBDqSQAFggm+vi4+/YVDfKxcM/vSGEGHmqup7EXKoGFJ8rZwlvaONMWnpG9mkeQG8C0Mzon7Lr7A/T3S3p0SbjwDBQ0MeU2DR3MSfCsFB7uNYiRgLSHPPjCeCWCvoCTMlgJ207+cetar8G4PTY4itmehteU8fq/81MBHlXn/zZw7es2RFliZ6fL754TIX+kJG3wsyBkmW6+xx+xNT4A7P5HRShpWn9Hg== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2024 19:56:00.5360 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2441b2de-04cf-4de7-2cad-08dc961a00d3 X-MS-Exchange-CrossTenant-Id: c4540d0b-728a-4233-9da5-9ea30c7ec3ed X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=c4540d0b-728a-4233-9da5-9ea30c7ec3ed; Ip=[178.72.21.4]; Helo=[localhost.localdomain] X-MS-Exchange-CrossTenant-AuthSource: AMS0EPF000001AC.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXP190MB2076 X-BESS-ID: 1719431764-310756-12112-19506-1 X-BESS-VER: 2019.1_20240620.2317 X-BESS-Apparent-Source-IP: 104.47.11.104 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVoaG5qbGQGYGUNQiNS0tzdDYwD Q1yTDJKCUtKTXJPDXZIs3cyMzI3DzVVKk2FgA+8zMaQgAAAA== X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.257218 [from cloudscan19-163.eu-central-1b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS113687 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add ntnic VFIO functionality. Signed-off-by: Serhii Iliushyk --- drivers/net/ntnic/meson.build | 1 + drivers/net/ntnic/ntnic_ethdev.c | 20 +- drivers/net/ntnic/ntnic_vfio.c | 235 +++++++++++++++++++++ drivers/net/ntnic/ntnic_vfio.h | 29 +++ drivers/net/ntnic/ntutil/include/nt_util.h | 15 ++ drivers/net/ntnic/ntutil/nt_util.c | 65 ++++++ 6 files changed, 362 insertions(+), 3 deletions(-) create mode 100644 drivers/net/ntnic/ntnic_vfio.c create mode 100644 drivers/net/ntnic/ntnic_vfio.h diff --git a/drivers/net/ntnic/meson.build b/drivers/net/ntnic/meson.build index d364cf8621..145f586a92 100644 --- a/drivers/net/ntnic/meson.build +++ b/drivers/net/ntnic/meson.build @@ -23,6 +23,7 @@ includes = [ sources = files( 'ntlog/ntlog.c', 'ntutil/nt_util.c', + 'ntnic_vfio.c', 'ntnic_ethdev.c', ) # END diff --git a/drivers/net/ntnic/ntnic_ethdev.c b/drivers/net/ntnic/ntnic_ethdev.c index c9726343a1..a4e32c30fa 100644 --- a/drivers/net/ntnic/ntnic_ethdev.c +++ b/drivers/net/ntnic/ntnic_ethdev.c @@ -8,25 +8,37 @@ #include #include #include -#include -#include -#include #include "ntlog.h" +#include "ntnic_vfio.h" #include "nt_util.h" +#define EXCEPTION_PATH_HID 0 + /* Global static variables: */ static int nthw_pci_dev_init(struct rte_pci_device *pci_dev) { + nt_vfio_init(); + uint32_t n_port_mask = -1; /* All ports enabled by default */ int n_phy_ports; NT_LOG_DBGX(DEBUG, NTNIC, "Dev %s PF #%i Init : %02x:%02x:%i\n", pci_dev->name, pci_dev->addr.function, pci_dev->addr.bus, pci_dev->addr.devid, pci_dev->addr.function); + + /* Setup VFIO context */ + int vfio = nt_vfio_setup(pci_dev); + + if (vfio < 0) { + NT_LOG_DBGX(ERR, TNIC, "%s: vfio_setup error %d\n", + (pci_dev->name[0] ? pci_dev->name : "NA"), -1); + return -1; + } + n_phy_ports = 0; for (int n_intf_no = 0; n_intf_no < n_phy_ports; n_intf_no++) { @@ -76,6 +88,7 @@ static int nthw_pci_dev_deinit(struct rte_eth_dev *eth_dev __rte_unused) { NT_LOG_DBGX(DEBUG, NTNIC, "PCI device deinitialization\n"); + nt_vfio_remove(EXCEPTION_PATH_HID); return 0; } @@ -157,3 +170,4 @@ static struct rte_pci_driver rte_nthw_pmd = { }; RTE_PMD_REGISTER_PCI(net_ntnic, rte_nthw_pmd); +RTE_PMD_REGISTER_KMOD_DEP(net_ntnic, "* vfio-pci"); diff --git a/drivers/net/ntnic/ntnic_vfio.c b/drivers/net/ntnic/ntnic_vfio.c new file mode 100644 index 0000000000..f4433152b7 --- /dev/null +++ b/drivers/net/ntnic/ntnic_vfio.c @@ -0,0 +1,235 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include "ntnic_vfio.h" + +#define ONE_G_SIZE 0x40000000 +#define ONE_G_MASK (ONE_G_SIZE - 1) +#define START_VF_IOVA 0x220000000000 + +int +nt_vfio_vf_num(const struct rte_pci_device *pdev) +{ + return ((pdev->addr.devid & 0x1f) << 3) + ((pdev->addr.function) & 0x7); +} + +/* Internal API */ +struct vfio_dev { + int container_fd; + int group_fd; + int dev_fd; + uint64_t iova_addr; +}; + +static struct vfio_dev vfio_list[256]; + +static struct vfio_dev * +vfio_get(int vf_num) +{ + if (vf_num < 0 || vf_num > 255) + return NULL; + + return &vfio_list[vf_num]; +} + +/* External API */ +int +nt_vfio_setup(struct rte_pci_device *dev) +{ + char devname[RTE_DEV_NAME_MAX_LEN] = { 0 }; + int iommu_group_num; + int vf_num; + struct vfio_dev *vfio; + + NT_LOG(INF, NTNIC, "NT VFIO device setup %s\n", dev->name); + + vf_num = nt_vfio_vf_num(dev); + + vfio = vfio_get(vf_num); + + if (vfio == NULL) { + NT_LOG(ERR, NTNIC, "VFIO device setup failed. Illegal device id\n"); + return -1; + } + + vfio->dev_fd = -1; + vfio->group_fd = -1; + vfio->container_fd = -1; + vfio->iova_addr = START_VF_IOVA; + + rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN); + rte_vfio_get_group_num(rte_pci_get_sysfs_path(), devname, &iommu_group_num); + + if (vf_num == 0) { + /* use default container for pf0 */ + vfio->container_fd = RTE_VFIO_DEFAULT_CONTAINER_FD; + + } else { + vfio->container_fd = rte_vfio_container_create(); + + if (vfio->container_fd < 0) { + NT_LOG(ERR, NTNIC, + "VFIO device setup failed. VFIO container creation failed.\n"); + return -1; + } + } + + vfio->group_fd = rte_vfio_container_group_bind(vfio->container_fd, iommu_group_num); + + if (vfio->group_fd < 0) { + NT_LOG(ERR, NTNIC, + "VFIO device setup failed. VFIO container group bind failed.\n"); + goto err; + } + + if (vf_num > 0) { + if (rte_pci_map_device(dev)) { + NT_LOG(ERR, NTNIC, + "Map VFIO device failed. is the vfio-pci driver loaded?\n"); + goto err; + } + } + + vfio->dev_fd = rte_intr_dev_fd_get(dev->intr_handle); + + NT_LOG(DBG, NTNIC, + "%s: VFIO id=%d, dev_fd=%d, container_fd=%d, group_fd=%d, iommu_group_num=%d\n", + dev->name, vf_num, vfio->dev_fd, vfio->container_fd, vfio->group_fd, + iommu_group_num); + + return vf_num; + +err: + + if (vfio->container_fd != RTE_VFIO_DEFAULT_CONTAINER_FD) + rte_vfio_container_destroy(vfio->container_fd); + + return -1; +} + +int +nt_vfio_remove(int vf_num) +{ + struct vfio_dev *vfio; + + NT_LOG(DBG, NTNIC, "NT VFIO device remove VF=%d\n", vf_num); + + vfio = vfio_get(vf_num); + + if (!vfio) { + NT_LOG(ERR, NTNIC, "VFIO device remove failed. Illegal device id\n"); + return -1; + } + + rte_vfio_container_destroy(vfio->container_fd); + return 0; +} + +int +nt_vfio_dma_map(int vf_num, void *virt_addr, uint64_t *iova_addr, uint64_t size) +{ + uint64_t gp_virt_base; + uint64_t gp_offset; + + if (size == ONE_G_SIZE) { + gp_virt_base = (uint64_t)virt_addr & ~ONE_G_MASK; + gp_offset = (uint64_t)virt_addr & ONE_G_MASK; + + } else { + gp_virt_base = (uint64_t)virt_addr; + gp_offset = 0; + } + + struct vfio_dev *vfio; + + vfio = vfio_get(vf_num); + + if (vfio == NULL) { + NT_LOG(ERR, NTNIC, "VFIO MAP: VF number %d invalid\n", vf_num); + return -1; + } + + NT_LOG(DBG, NTNIC, + "VFIO MMAP VF=%d VirtAddr=%p HPA=%" PRIX64 " VirtBase=%" PRIX64 + " IOVA Addr=%" PRIX64 " size=%" PRIX64 "\n", + vf_num, virt_addr, rte_malloc_virt2iova(virt_addr), gp_virt_base, vfio->iova_addr, + size); + + int res = rte_vfio_container_dma_map(vfio->container_fd, gp_virt_base, vfio->iova_addr, + size); + + NT_LOG(DBG, NTNIC, "VFIO MMAP res %i, container_fd %i, vf_num %i\n", res, + vfio->container_fd, vf_num); + + if (res) { + NT_LOG(ERR, NTNIC, "rte_vfio_container_dma_map failed: res %d\n", res); + return -1; + } + + *iova_addr = vfio->iova_addr + gp_offset; + + vfio->iova_addr += ONE_G_SIZE; + + return 0; +} + +int +nt_vfio_dma_unmap(int vf_num, void *virt_addr, uint64_t iova_addr, uint64_t size) +{ + uint64_t gp_virt_base; + struct vfio_dev *vfio; + + if (size == ONE_G_SIZE) { + uint64_t gp_offset; + gp_virt_base = (uint64_t)virt_addr & ~ONE_G_MASK; + gp_offset = (uint64_t)virt_addr & ONE_G_MASK; + iova_addr -= gp_offset; + + } else { + gp_virt_base = (uint64_t)virt_addr; + } + + vfio = vfio_get(vf_num); + + if (vfio == NULL) { + NT_LOG(ERR, NTNIC, "VFIO UNMAP: VF number %d invalid\n", vf_num); + return -1; + } + + if (vfio->container_fd == -1) + return 0; + + int res = rte_vfio_container_dma_unmap(vfio->container_fd, gp_virt_base, iova_addr, size); + + if (res != 0) { + NT_LOG(ERR, NTNIC, + "VFIO UNMMAP FAILED! res %i, container_fd %i, vf_num %i, virt_base=%" PRIX64 + ", IOVA=%" PRIX64 ", size=%" PRIX64 "\n", + res, vfio->container_fd, vf_num, gp_virt_base, iova_addr, size); + return -1; + } + + return 0; +} + +void +nt_vfio_init(void) +{ + struct nt_util_vfio_impl s = { .vfio_dma_map = nt_vfio_dma_map, + .vfio_dma_unmap = nt_vfio_dma_unmap + }; + nt_util_vfio_init(&s); +} diff --git a/drivers/net/ntnic/ntnic_vfio.h b/drivers/net/ntnic/ntnic_vfio.h new file mode 100644 index 0000000000..69fef7923d --- /dev/null +++ b/drivers/net/ntnic/ntnic_vfio.h @@ -0,0 +1,29 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#ifndef _NTNIC_VFIO_H_ +#define _NTNIC_VFIO_H_ + +#include +#include +#include + +void +nt_vfio_init(void); + +int +nt_vfio_setup(struct rte_pci_device *dev); +int +nt_vfio_remove(int vf_num); + +int +nt_vfio_dma_map(int vf_num, void *virt_addr, uint64_t *iova_addr, uint64_t size); +int +nt_vfio_dma_unmap(int vf_num, void *virt_addr, uint64_t iova_addr, uint64_t size); + +/* Find device (PF/VF) number from device address */ +int +nt_vfio_vf_num(const struct rte_pci_device *dev); +#endif /* _NTNIC_VFIO_H_ */ diff --git a/drivers/net/ntnic/ntutil/include/nt_util.h b/drivers/net/ntnic/ntutil/include/nt_util.h index 3c7b6cfce4..6dfd7428e1 100644 --- a/drivers/net/ntnic/ntutil/include/nt_util.h +++ b/drivers/net/ntnic/ntutil/include/nt_util.h @@ -24,5 +24,20 @@ void nt_os_wait_usec(int val); uint64_t nt_util_align_size(uint64_t size); +struct nt_dma_s { + uint64_t iova; + uint64_t addr; + uint64_t size; +}; + +struct nt_dma_s *nt_dma_alloc(uint64_t size, uint64_t align, int numa); +void nt_dma_free(struct nt_dma_s *vfio_addr); + +struct nt_util_vfio_impl { + int (*vfio_dma_map)(int vf_num, void *virt_addr, uint64_t *iova_addr, uint64_t size); + int (*vfio_dma_unmap)(int vf_num, void *virt_addr, uint64_t iova_addr, uint64_t size); +}; + +void nt_util_vfio_init(struct nt_util_vfio_impl *impl); #endif /* NTOSS_SYSTEM_NT_UTIL_H */ diff --git a/drivers/net/ntnic/ntutil/nt_util.c b/drivers/net/ntnic/ntutil/nt_util.c index 5395bf6993..53c39ef112 100644 --- a/drivers/net/ntnic/ntutil/nt_util.c +++ b/drivers/net/ntnic/ntutil/nt_util.c @@ -15,6 +15,8 @@ #include "ntlog.h" #include "nt_util.h" +static struct nt_util_vfio_impl vfio_cb; + /* uses usleep which schedules out the calling thread */ void nt_os_wait_usec(int val) { @@ -31,3 +33,66 @@ uint64_t nt_util_align_size(uint64_t size) { return 1 << rte_log2_u64(size); } + +void nt_util_vfio_init(struct nt_util_vfio_impl *impl) +{ + vfio_cb = *impl; +} + +struct nt_dma_s *nt_dma_alloc(uint64_t size, uint64_t align, int numa) +{ + int res; + struct nt_dma_s *vfio_addr; + + vfio_addr = rte_malloc(NULL, sizeof(struct nt_dma_s), 0); + + if (!vfio_addr) { + NT_LOG(ERR, GENERAL, "VFIO rte_malloc failed\n"); + return NULL; + } + + void *addr = rte_malloc_socket(NULL, size, align, numa); + + if (!addr) { + rte_free(vfio_addr); + NT_LOG(ERR, GENERAL, "VFIO rte_malloc_socket failed\n"); + return NULL; + } + + res = vfio_cb.vfio_dma_map(0, addr, &vfio_addr->iova, nt_util_align_size(size)); + + if (res != 0) { + rte_free(addr); + rte_free(vfio_addr); + NT_LOG(ERR, GENERAL, "VFIO nt_dma_map failed\n"); + return NULL; + } + + vfio_addr->addr = (uint64_t)addr; + vfio_addr->size = nt_util_align_size(size); + + NT_LOG(DBG, GENERAL, + "VFIO DMA alloc addr=%" PRIX64 ", iova=%" PRIX64 + ", size=%" PRIX64 "align=0x%" PRIX64 "\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size, align); + + return vfio_addr; +} + +void nt_dma_free(struct nt_dma_s *vfio_addr) +{ + NT_LOG(DBG, GENERAL, "VFIO DMA free addr=%" PRIX64 ", iova=%" PRIX64 ", size=%" PRIX64 "\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size); + + int res = vfio_cb.vfio_dma_unmap(0, (void *)vfio_addr->addr, vfio_addr->iova, + vfio_addr->size); + + if (res != 0) { + NT_LOG(WRN, GENERAL, + "VFIO DMA free FAILED addr=%" PRIX64 ", iova=%" PRIX64 ", size=%" PRIX64 "\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size); + } + + rte_free((void *)(vfio_addr->addr)); + rte_free(vfio_addr); +} -- 2.45.0