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 733A84414A; Mon, 3 Jun 2024 18:20:25 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 53B2142EC3; Mon, 3 Jun 2024 18:18:56 +0200 (CEST) Received: from egress-ip42b.ess.de.barracuda.com (egress-ip42b.ess.de.barracuda.com [18.185.115.246]) by mails.dpdk.org (Postfix) with ESMTP id 1485F42EBA for ; Mon, 3 Jun 2024 18:18:50 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2105.outbound.protection.outlook.com [104.47.17.105]) by mx-outbound23-246.eu-central-1b.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 03 Jun 2024 16:18:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dGaoT4c9T958jRZ0aYTFIi+bduJDtnQqLek6yDAMZ5LKiRrjwAnIkmB7YpH678Ys3Ezh93YHEWymYJlqV7sPxZHRHRFnBOvdkeQ6mHoE+c01pGArjeWt3971f2Jlyvsvf7qfYycbaLBEI+C7iE1F+OAdQMsy+IFDIWrf+JeO6iJ+18QD/Xx9YSHNdmK/KBTjoUrfsSVZ18i2gDdLr5UTu8goIY57F4uPHgzhxNgPLhtUPNIiYcoKt2FIZLbgpzKMVDF8i7cjBvVaQtsffKH236JPel3mAPN34g04GQz2+VYmsL5ZzOwCruJlj8xyh3mQdJ8J/fav1gBT0HA1gBFIGg== 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=+g6X5cMBns1bIuiKlJ1H+z3h6ZXqY4liWsTolkkgfTQ=; b=dhy1GgMwOx7Vbhz83Yo2UUq7kToLUKZoT1Ms6gR5O1I/Wd2jbemDM45rTWvXMinOa1yGgCEDy3yEx1eV16VtNcxr2nwlWG7EIvS6kSlp2JXyaQSZSaA6ia8dVjg4nETdxnpsux9cbVvhwWL0cAJDCm7ZGwMQlfNYrDpzft0+haA/XS5MLjt6WHian3faw/SIKmR+tORcz5H8DhlHMGc5fHIsvZzbm2gQLTrTCC8ht5Jx4QRPy5ydOYQG0Qhu3R4O/DT8mVARhwy113yBIuBjwEBxDOG4UUS3hefK3FqMAcdBAVPDiIGd0ujnzNNkQI/942IHGeVqa50XlKK4KMLhKw== 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=+g6X5cMBns1bIuiKlJ1H+z3h6ZXqY4liWsTolkkgfTQ=; b=L5VyZ3d11sYGrHv8jdkxZMEefWvk8u0jjmC3k+8QixbV7JDDQ/pluhqS3wq8L82IGpE18N6iM3io1P5N6PT/YTuCbqfLPGSjwWAP54y/ujZP8g/EX9G8/xPEdILwtzowmS3bwlqwSrVli+OUIbXFwhkrHYSuDsfBSzeEZi1NGCs= Received: from AM6P191CA0088.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8a::29) by GV1P190MB2171.EURP190.PROD.OUTLOOK.COM (2603:10a6:150:1f3::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7611.29; Mon, 3 Jun 2024 16:18:37 +0000 Received: from AMS0EPF00000196.eurprd05.prod.outlook.com (2603:10a6:209:8a:cafe::ae) by AM6P191CA0088.outlook.office365.com (2603:10a6:209:8a::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.29 via Frontend Transport; Mon, 3 Jun 2024 16:18:37 +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 AMS0EPF00000196.mail.protection.outlook.com (10.167.16.217) with Microsoft SMTP Server id 15.20.7633.15 via Frontend Transport; Mon, 3 Jun 2024 16:18:37 +0000 From: Serhii Iliushyk To: dev@dpdk.org Cc: mko-plv@napatech.com, ckm@napatech.com, andrew.rybchenko@oktetlabs.ru, ferruh.yigit@amd.com Subject: [PATCH v3 09/17] net/ntnic: add VFIO module Date: Mon, 3 Jun 2024 18:18:06 +0200 Message-ID: <20240603161822.59552-9-sil-plv@napatech.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240603161822.59552-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> <20240603161822.59552-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS0EPF00000196:EE_|GV1P190MB2171:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 29d4c423-0e1f-4edd-f5f4-08dc83e8d313 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|36860700004|82310400017|1800799015|376005; X-Microsoft-Antispam-Message-Info: =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Bw0a/FtHEtuP?= =?us-ascii?Q?89nnDDzhsT+RG3PBWYRh5x93PgFhIfb06GhQRqgQI6t/SD/+zCHh0tGzIC3d?= =?us-ascii?Q?9W/kWBYhIch2YmcrOEL3I5QL/B3lk8YeZbLL7Fpjt1YtnhJFYnaKuL6wE4Kn?= =?us-ascii?Q?tdf87sow/IwyBizi09tWhluA0/cxicdeWCf+HG9AZhyj4/L2dzpm6R8sGRIa?= =?us-ascii?Q?dtNidU0zMiEfO8anoojS/MUgCzbXN1b/Bceay7IRu7RBCCTsKpqIZRLQZ61r?= =?us-ascii?Q?VfkaOsV2GITVbNpy6rs6Vt/3xG825+fOxQl198qtR+gHXpxmWyeB1ZUawwjT?= =?us-ascii?Q?3nCwTC4siKr1Nr9hA/Uhl/7nnfkTh5CBgYuhkufjB5vc3l/Oa9Y11SQH/SXs?= =?us-ascii?Q?DphuMOgmUCZ0CGSb0a+B516hyVHe783MTWclycxXLVdY29DMUhu5xhbj4tJH?= =?us-ascii?Q?jMSd6TZUR6vee0d50XQnJdAMGnSYnRwyvJeTKF7Gn0h924TPL2UGM5tDwCME?= =?us-ascii?Q?eGVetUemAV6CIGB2yhekel7k+xit?= 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:(13230031)(36860700004)(82310400017)(1800799015)(376005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ZjEb5t/RRi6VBELiDkcAHhM45273RF723pRcmp3/PDcK/uBymTswIeYXl67nI82fZ0AXMGvGGZMna388E3txw94+EbdTlWH+6FLiwlIHJ4l2ivu8Nf62CAWOipDABAJLRrq3aioylEiucDdjIFo+O+LNDyXmCptryb7nCNSKtl2KXZwgLe1jidsULsFPZcTDMppL9+JQl6zqSj6p8rtodFAfN5UylrXIlDqCqUD4wGqoIDk2URR48zzIX3m019bO18Ug23nIJ4e3YYGOYlc5uX/ctvT4sTXwvMOMkYFBe/TQUb+3Zkk3ig9fFkQ5K/Rb8sZmWv4ilCuaUw87zMDPhlVlfqMGPMiG+LtFo26FdBGCDeAU2/28C18Zw7BMoUcGMAecE6AT0RWqqR0T7ht1VP6cTaN8/TBZorqrMDvUYSagkBCEpFY92GK6gbU+3K9HuCk9Cwh0x7SVXR4OKnQHePHxqOqmSm0jALuhQSIVATnFHzc52I5bZW45FRPpFKwVpOHekPCIpWFG3yoxczGZPtY5DLTHf9tF7CaA5LFHWL8S/t0uUA00yyOMDzmzsIrrKWTlIxKQEt/LEJy2f8101OZn1lzIPm/qKBcdOQuNyBBDElFOMXDuyqia7HJ62gtl+0t41lSY2V7LJycYuX9FxQ== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2024 16:18:37.5469 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 29d4c423-0e1f-4edd-f5f4-08dc83e8d313 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: AMS0EPF00000196.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1P190MB2171 X-BESS-ID: 1717431521-306134-12660-9921-2 X-BESS-VER: 2019.1_20240530.1612 X-BESS-Apparent-Source-IP: 104.47.17.105 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVmbmhqZAVgZQ0CjRxDg1McXIwC DVzNQw0cI4Kc00Oc0wNdHY3CDV0tBCqTYWABivoxBBAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.256698 [from cloudscan17-209.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/ntnic_vfio.c | 232 +++++++++++++++++++++++++++++++++ drivers/net/ntnic/ntnic_vfio.h | 21 +++ 2 files changed, 253 insertions(+) create mode 100644 drivers/net/ntnic/ntnic_vfio.c create mode 100644 drivers/net/ntnic/ntnic_vfio.h diff --git a/drivers/net/ntnic/ntnic_vfio.c b/drivers/net/ntnic/ntnic_vfio.c new file mode 100644 index 0000000000..116c238be6 --- /dev/null +++ b/drivers/net/ntnic/ntnic_vfio.c @@ -0,0 +1,232 @@ +/* + * 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, ETHDEV, "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, ETHDEV, "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, ETHDEV, + "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, ETHDEV, + "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, ETHDEV, + "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, ETHDEV, + "%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, ETHDEV, "NT VFIO device remove VF=%d\n", vf_num); + + vfio = vfio_get(vf_num); + + if (!vfio) { + NT_LOG(ERR, ETHDEV, "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, ETHDEV, "VFIO MAP: VF number %d invalid\n", vf_num); + return -1; + } + + NT_LOG(DBG, ETHDEV, + "VFIO MMAP VF=%d VirtAddr=%" PRIX64 " HPA=%" PRIX64 " VirtBase=%" PRIX64 + " IOVA Addr=%" PRIX64 " size=%d\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, ETHDEV, "VFIO MMAP res %i, container_fd %i, vf_num %i\n", res, + vfio->container_fd, vf_num); + + if (res) { + NT_LOG(ERR, ETHDEV, "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, ETHDEV, "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, ETHDEV, + "VFIO UNMMAP FAILED! res %i, container_fd %i, vf_num %i, virt_base=%" PRIX64 + ", IOVA=%" PRIX64 ", size=%llu\n", + res, vfio->container_fd, vf_num, gp_virt_base, iova_addr, size); + return -1; + } + + return 0; +} + +/* Internal init */ + +RTE_INIT(nt_vfio_init); + +static 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..0e3b12f847 --- /dev/null +++ b/drivers/net/ntnic/ntnic_vfio.h @@ -0,0 +1,21 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#ifndef _NTNIC_VFIO_H_ +#define _NTNIC_VFIO_H_ + +#include +#include +#include + +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_ */ -- 2.45.0