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 1739044111; Thu, 30 May 2024 16:50:35 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 7A47C411F3; Thu, 30 May 2024 16:50:04 +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 836F440A7F for ; Thu, 30 May 2024 16:50:00 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04lp2050.outbound.protection.outlook.com [104.47.12.50]) by mx-outbound15-68.eu-central-1a.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 30 May 2024 14:49:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y7JTMf2iYmrMmEjJlOr2ltIJs6d5gL7/enUf0vqCeoR5rJUnw8vvbB0GdYLpmXegrKuZGM9XJl//aQ05He5XscX4+jZ7LG3k4CjPaXLxWk/zRDD5UOYsZaeT/GovtgvRBf2GB7Mhu7ZU348NJKPTK3wW1Eniw6I/jl87rlyRACjAMkH4Xy7tjjfXTzfp8LMwV/7dXCJy7nzRxlY1Cxf6wgOH8jGeeoG2qlWstDwWA1iJPk0vBYC41J6v2EMBNOIdAWcFGInamMDqjQq8DXrLxSMb/2J5kUK+pS7XLW0OCv1oX9XEo4sLWTHQrFFa6ZZrBWDu2zIhAcYg6Tl5N5PPuw== 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=ypYkeDPil8+cXub4fDDXFOIIjcMxIsI+JVPN6IRDw9s=; b=d7FLULE2pRNm7U9aJ0sXfBJq5Q9JDHUkmOIGculOEGqNePWfFfRiOqptGolcM7VV+ZfzJCzFaZTasPoMmr/uAvZEuABAJgCU2l4MtHIbRdbvu18C9VQWDO7C5gwusDLT5z0tDYZP0lzshlTq40YH8yZtZjTYxejFIkc2b5u8NadNYJbNaxY2dEFS3sy2/IwMHKqcPdPb6R5HNTj0KcGXo7e8/fR9RHz/wDpQ/II89dmIdIwrjbrf97rjfPT1jjGaN43QYBdVjgZqLLc9/GnM6EZW2peQGOCIRjqUQ7tQLBkSCD9cAC93w0qvmgL+nqbOTCl0aTAsneHOzfqN9ie4hQ== 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=ypYkeDPil8+cXub4fDDXFOIIjcMxIsI+JVPN6IRDw9s=; b=Odo1A26M6PZ0ZcxDkz+hOhtIKkYXjzlN0gRiReleNes0UtnxfYTG5T6B4JyseRWhQ+KMOjWd3qP3qiqnWDCU2XwiA7vmdnsakS8teM9Az6nHdmOR5/YXMMl114ztoJHLVb6GJmIUX56bgJFEFLtuLK6Qg9o7G3Gvbtzr24dGXc0= Received: from DU6P191CA0038.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:53f::22) by AM8P190MB0804.EURP190.PROD.OUTLOOK.COM (2603:10a6:20b:1d2::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Thu, 30 May 2024 14:49:57 +0000 Received: from DB1PEPF000509EB.eurprd03.prod.outlook.com (2603:10a6:10:53f:cafe::cb) by DU6P191CA0038.outlook.office365.com (2603:10a6:10:53f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21 via Frontend Transport; Thu, 30 May 2024 14:49:57 +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 DB1PEPF000509EB.mail.protection.outlook.com (10.167.242.69) with Microsoft SMTP Server id 15.20.7633.15 via Frontend Transport; Thu, 30 May 2024 14:49:57 +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 v1 09/17] net/ntnic: add VFIO module Date: Thu, 30 May 2024 16:49:06 +0200 Message-ID: <20240530144929.4127931-9-sil-plv@napatech.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240530144929.4127931-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB1PEPF000509EB:EE_|AM8P190MB0804:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 6cf5a4d7-5b2f-4933-d840-08dc80b7c684 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400017|376005|1800799015|36860700004; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?NFcZk21uJMZQlUoi8HDjX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Q?wtj/ZVyJJpu488yjETzlPLVq2af3lumI+wGDvv+tiL03xuW7KQGuoN5dkP4p?= =?us-ascii?Q?nSpB25AJ10j3/u0gLXJY421L+aSPwciYUUZLBVDc7UQ4OyhN4jKgTxPPn2oW?= =?us-ascii?Q?gLOE3BeZ1Y/NUpcN6JdNJjZZMf9z/VdTWKF1CYhDkmuGjWOC2yPcZmNuX8Uq?= =?us-ascii?Q?NPvSg3C2UOTgYxAZBkPe1s+mWk/GioLJtOgSD7s6UX9joe5M/vKZED1qp84T?= =?us-ascii?Q?4euOMY/xwtBks5KlIBi3pRWZwruL7xTewN1ZsEWxIC2xPaD09DWtwNRK0VFY?= =?us-ascii?Q?HLxU4JfcvwhQFo0UTM2i2ZXNB1kdxw8YoWdFa2L6r/TrOqDHTy93QUdXyM1y?= =?us-ascii?Q?aOM6orch6XZli9uFDu8qKBixbOq4LIETMFGtJ70axbmTrgUQNxIKtlTCepBH?= =?us-ascii?Q?MKB7emtosGcm5Jc8JTO4y5iHdLBWgJu32HBimcXGld/BgxGYrZ3/PgFc6iSc?= =?us-ascii?Q?RW/IXdzWOS1GEd+eb7X/qUqMyrq6?= 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)(82310400017)(376005)(1800799015)(36860700004); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: L5sRZQZ7cLqtmnpFzC8tYDm/VaeSdo5t3d+mozT2UX3ihFX3v4jprU7rD1QevwRpLx+gsDS8eI/D+iKffPjlq+L89zRDlb4urf4pjDGboAD5kdYIe3w6UbeM9k5lc7wFyPDyPBtEZXSTQE+vLdQwM6oWcT9Ck5Qz5QHAT1+3fnVLyYxhjcEdm0SJRa3jT0ofNrcDbHY+iiHQKBZUQOZFoIzcuCseubI/wHv6EzLz6u1AOa6kL+xfmt5To+0sRTxUghWx+0jNOOwBdYGiDeK3MmiJjF66YyAexinmTxTQRKIDzOJSBGacfj0uh4Rhi3EbS/TPpXpvG85Y4Qkp1Zy0c7mVm0nfh4/i1648o7eQhL9eVzg6gFA8Qw0wlYtwc1oHMdgMdvRU5OJU5iKTICmuD1MoTwlz8h+TmSV2X3Ej8T47F5FzZJtLag7j9QzS4dN7eU3n3lCamZTLDe5Un7w9ZCgr/zBJOyDJlf0KK52BiWKmWklpZfKX25/JjHD5XwsZqCpLwq6f9pyyR8yR+pfnC/TSxB+gjNjm/m8CkRPOCXR98edVKfNNP+yxu1+ZIaz0wfgF+Q6h1CsFytelyLtdySczWUe5G5srtcDvSxfLBJghB08Rg+Ug5ajQEC9AEZcoOnf6/8GTiqQSBfQ3sTE6nA== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 14:49:57.5233 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6cf5a4d7-5b2f-4933-d840-08dc80b7c684 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: DB1PEPF000509EB.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P190MB0804 X-BESS-ID: 1717080599-303908-12700-38262-1 X-BESS-VER: 2019.1_20240429.2309 X-BESS-Apparent-Source-IP: 104.47.12.50 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVmbmhqZAVgZQ0CzFKM3YyNTAxM I40cAw1dzEwMTExNLS2NzQMtHcLNFcqTYWABkRKMxBAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.256603 [from cloudscan22-0.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.44.0