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 42FB14411D; Fri, 31 May 2024 17:50:01 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D42A142E04; Fri, 31 May 2024 17:48:07 +0200 (CEST) Received: from egress-ip11a.ess.de.barracuda.com (egress-ip11a.ess.de.barracuda.com [18.184.203.234]) by mails.dpdk.org (Postfix) with ESMTP id 5D8F942DA7 for ; Fri, 31 May 2024 17:47:52 +0200 (CEST) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04lp2048.outbound.protection.outlook.com [104.47.13.48]) by mx-outbound13-64.eu-central-1a.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 31 May 2024 15:47:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WUSQ9/kC6RW6gw/ITGqfxMCm4m623u7bxfdvSSTGrNq8VRJY2MjDkrvY7aG51zHcpuDn3Clh/mc8gsRM7IvO5aywcojl3KG3ZkRfPtBb+ZZgYGUdGLbiiWmtkh6o9uoL/bDdPCd8Y9TfA64COMhmY3U2gyGaT2jMln3A+3MhTnHuDtRrsPl/2numVkwo/HYTMeK4eco7kWCLxDnfqAowWwqa2l5hm7YlVWYfsa9rwXbTfVY9JpgAOoL97EErNDUCMIyNXx4kZM5dN9UFUXlwMJ4quKws7HFqXfsC6y1N6RXgOt8V3LoQYcDjWCCuEhHF+rQ2+C+5HSCrn2PcO8Abwg== 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=iBRBybWzMF67X5ngiqWb3bYglNxLTMMUWCHB7KBJbkUWE2stKNVqw0yJ59dK4lupNX/3H2TvaSSiWIkEtAiCxc2CMXWnakHN4AwbtlH6vqvGoeC97zBVPIEdYxms66k9z8sol5KYPvSaauhRLkoQ0N4eesAUfWK0GxATyfGN6i4nLdidifY5IWUCwL8g3P5s6qzYjggD7eseZpCZ7iIbkx2vifCuYbSBV+g35+jme2AqXkmqu/BShm7MPs5pllagWCvIy2n1BdTJ8qiedGMNv96kkcIcaORf9XLTCpBTkbWYxIT4G5c2GaNh5Zt7m9W5jOliysvQ0JCvAp+sqSNKRw== 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=no9RITe9R6QzcOWQo3RuFd4sy0iePZXtLyIBZL16rG4HEkNlhzLyYrVEO2yAZEv5ywoiAIAZy6knK4HDaNFuxg/hvEoFvCra9vHlikqO1ZlhlcamCAll5jc52xsrgwf9h1XeLyfcKN9T4jW7XQkwfLX3aEmSHEJ6k4lJ0s/TUgE= Received: from AS4P251CA0028.EURP251.PROD.OUTLOOK.COM (2603:10a6:20b:5d3::17) by VE1P190MB1022.EURP190.PROD.OUTLOOK.COM (2603:10a6:800:1ac::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Fri, 31 May 2024 15:47:42 +0000 Received: from AMS1EPF00000047.eurprd04.prod.outlook.com (2603:10a6:20b:5d3:cafe::97) by AS4P251CA0028.outlook.office365.com (2603:10a6:20b:5d3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.27 via Frontend Transport; Fri, 31 May 2024 15:47:42 +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 AMS1EPF00000047.mail.protection.outlook.com (10.167.16.135) with Microsoft SMTP Server id 15.20.7633.15 via Frontend Transport; Fri, 31 May 2024 15:47:42 +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 v2 09/17] net/ntnic: add VFIO module Date: Fri, 31 May 2024 17:47:13 +0200 Message-ID: <20240531154731.1856874-9-sil-plv@napatech.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240531154731.1856874-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> <20240531154731.1856874-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS1EPF00000047:EE_|VE1P190MB1022:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 68da2d30-667b-4447-1c74-08dc81890223 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|36860700004|376005|82310400017|1800799015; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?ljhmEYm4f8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-ascii?Q?KLFjhwSdLi1AJhhr5TQn38AFzJ4Ju0R3N4iQdQEoR+t7Lh99Km7HLuhriyBZ?= =?us-ascii?Q?z14+wH5Q0wD0ol1gYsRLFfmAr46h6xhUky0fqIx4QHJ7g2lc/L3oq2bb0nSJ?= =?us-ascii?Q?Oix+Mt+aEyO07qOn5QOY2MhIlexRxtNOZZXiNE8be4wClg72dCG84UkSb2P5?= =?us-ascii?Q?LRVxcLRza/KFF4QXBxgSJLsgnjs5cztIng3SL2pNednvRhBMJjf9QS+Ugx6G?= =?us-ascii?Q?x4bmnEB+naMCusJl+HFgocpo5biCWmWP71dnBrIV+/7f4bxsquibvhNkrRHU?= =?us-ascii?Q?UBH39sTY/7543SiDkqBpejnUg3Wks0hQEHJcgdN6aa/QrNcfJgvaVK6EdeWr?= =?us-ascii?Q?y5pLsUot2w5fRPaS2QF6bpYcYMJTxc4ZwGY7zL2oMMW2ZlSA+2tyY7ZbE3gN?= =?us-ascii?Q?YiI0FO4BQr2iv1batp5Drn+VPLELU7oEVkmNhk/jY6NlpCI7KJko0RM04E+k?= =?us-ascii?Q?IEIoLxhgw27q0ErWBtqA8F9IBvDf?= 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)(376005)(82310400017)(1800799015); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 5jMjjp2OlmfQbcPH80MGRREHxj+eGlX3YK6mJ1ZU6RmUAoefYjFIdJv61wE/06c9O0X4+GQ6ehf3fqGUpMXlceEFhUweV/6G26URlJ5aW+P29PNW6CrYNo0PMCUj1H46AXkqwjMca65uc4JT87BZJxGyDew6XV18NtlA3vRNCiB7Dv8nueMMCap6F7vbKVMSlHpl/L3r8YFfklVZQw7LBea2myeuN5CYL7Aafk7JRH+ZJp9As3ysYRqFDMO9+ZLH6GSbgS6qJVTejKahSwWXtNnp2X9QW7+wAPFFizwyHvEMmlsaSgWWrdj/6AzZJPv15c3tH2TazwQr3KAhwlNv4H/7ZlTbjnjeblLuSFRDguQm6U8isfpDIOdFVTzYVQpWgmQ8ZjUETxYnhW688i3LIyD1Am2jdoE9SFWCAJ8YmOZnAYEX3ayAyj9PMGm7GL1UYr3o5LXkIptJtq13Y5mEzYW3Q1ucnJm+9LC4zMdkzb9tQ5zxK/2cRoZg8s1DWC/oESrrLB8rRLfk6HUMA019WQz15RcUSgTh6kIZb8xsjid8GXPsQeb5wmLM227Gu/RX5GkpHU22YIWHniiwgmjgTdiCatkN+kJ6sdv4ApfGBXPm3SBI0sQoSsWAXVBldeOr83PLFkFmmjIbIBcW4sO5Qg== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2024 15:47:42.5032 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 68da2d30-667b-4447-1c74-08dc81890223 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: AMS1EPF00000047.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1P190MB1022 X-BESS-ID: 1717170465-303392-12651-39856-2 X-BESS-VER: 2019.1_20240530.1612 X-BESS-Apparent-Source-IP: 104.47.13.48 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.256627 [from cloudscan22-138.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