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 067BC45614; Fri, 12 Jul 2024 17:49:04 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4A89242FD9; Fri, 12 Jul 2024 17:48:01 +0200 (CEST) Received: from egress-ip11b.ess.de.barracuda.com (egress-ip11b.ess.de.barracuda.com [18.185.115.215]) by mails.dpdk.org (Postfix) with ESMTP id 3D64C4066F for ; Fri, 12 Jul 2024 17:47:56 +0200 (CEST) Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02lp2241.outbound.protection.outlook.com [104.47.11.241]) by mx-outbound23-2.eu-central-1b.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 12 Jul 2024 15:47:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gPU/a/OXFcJ7/a6LsHKyXbx04/Z58OvFrE10S0dZDZ2iPpwWoCPxu0u0NZxGbOuzUnSVP10egUrK2NPYp3sKu18LarlbVvtpbFjr5Gi02OXFbVL6++tsYjuHuZBiTRddM3uYa3/8i/2omc17l1/04A5yzVmOXkiNs3jLzz9hIXWfmSR0LAoaeGDy5IEU+iBILMII2UVybSsTDaKhF/IfOJGxrZJVEZdJVL3rmQLMSmsHStMyMNxArYr7Sm57mvKdhme2LH+vou+k70vNA9P0jpRQZVGVkS33JuAQVzfVO2vtRxKMKXdSdScnrnw6v51bxBsYjtP6saxi0qlrTDONJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=Q/GDHnPIgKGUE2u1gYyI6r6H0jG3S2SEkX5vaXGPaPg=; b=MHW5yIeNCs7pfETD4tOw9nPrXV6UuUyKzPTHMffiJ1B7AuYmIR78wipzqsTS93C94wXpPRbeplfaWsxQPSd/BfhOKIVNBBAp/yCmjbipikyqE2pnd4BNq3QD80TxLJD/7peBkalE08WXdQrRZCCcrkh8ZaOeRgYI+5qK6/rTjMnKe+PLUkren6nI8yg/U7TT/9TVBASs2IBoUisp+q/pNVyzIfrHu0Re7zRDn+liuxCzhigVciUHkgz9gf4s0qRzkKno6G+svJ7b9rJ3EWoSn2hKHD0bOpr0VfYrfK0VAwtJCl9Xklr1ER52vqgJxeDcyo65pba2960C9DLAxdq6yA== 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=Q/GDHnPIgKGUE2u1gYyI6r6H0jG3S2SEkX5vaXGPaPg=; b=Qbvspjeqiv0O0oDzx+MucSuuDAuVNdgnG8fz1yprmMeCnPDPAzRTwYaMahFKdR1qbQlbWqmxHF08bODULnEqUzV1pX44dItBY7vGkAirrzhd7q1B/de7LruByWKP4YPvWWiLcVgT4b91VwIkhNVJSPPr+HEwiGbk16vcgHoJXyc= Received: from DU7PR01CA0023.eurprd01.prod.exchangelabs.com (2603:10a6:10:50f::20) by GV1P190MB2225.EURP190.PROD.OUTLOOK.COM (2603:10a6:150:20a::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.20; Fri, 12 Jul 2024 15:47:45 +0000 Received: from DU2PEPF00028D0C.eurprd03.prod.outlook.com (2603:10a6:10:50f:cafe::41) by DU7PR01CA0023.outlook.office365.com (2603:10a6:10:50f::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.23 via Frontend Transport; Fri, 12 Jul 2024 15:47:44 +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 DU2PEPF00028D0C.mail.protection.outlook.com (10.167.242.20) with Microsoft SMTP Server id 15.20.7762.17 via Frontend Transport; Fri, 12 Jul 2024 15:47:44 +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 v8 05/21] net/ntnic: add VFIO module Date: Fri, 12 Jul 2024 17:47:15 +0200 Message-ID: <20240712154737.1339646-5-sil-plv@napatech.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240712154737.1339646-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> <20240712154737.1339646-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF00028D0C:EE_|GV1P190MB2225:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: a9295657-f42c-4d32-2175-08dca289f8b5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|1800799024|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?a2ObxVY5wOqVuJM8qAaHv3xtLYKBQjNEZuvF8ySVccJr4Ux7RnU9TQbvSs7S?= =?us-ascii?Q?XE/EtZ4ehWM4eXPkKQJ3ov8G5Ss/vhwc86ODVl2xZIeC6bLK0FyKEx2Jji6f?= =?us-ascii?Q?aLcG9UXshyhSoTnkOPTvzZbTULAba4D3I0Jpd3YrIiOqNki17FFljgoXGVrv?= =?us-ascii?Q?eukF0Xpx1DD9jY1n7C1Lv9hxoCC/c3mhYgJE7/Bdhbb8IfcPVWiA3CgNUbmX?= =?us-ascii?Q?9iZ06nilTURRqQTh2sMllgmAMjnV5uA4NswdclN4KdoBKshbMOAwGhhaA/9O?= =?us-ascii?Q?26ETmnLzxxqkc6mYHMcCeoiNs1s8g/Nm8tSv66SvB60PNisqHhaBxn2MBhrL?= =?us-ascii?Q?PFGJzpVgYP4mazagaylqSIP5amQRJ1XoOkrmchdWQ2UjHjYVR7Kq4OyON5eQ?= =?us-ascii?Q?wf85AI5QwQCGgy3WByglWumz9zwwIdJEkAYQSigs1as0HlUumdiUUzjR3RU9?= =?us-ascii?Q?YJkS3OnmHcMfrtN76kd6H605emLzVRrRWb2MYEimoRMUhprTiuH2q7bPZ5OJ?= =?us-ascii?Q?H5dGWMZZKZljUJImSX86D9DlvlRFEKCKxynFMbjqaRwPL5+ERlKHpngpiG+X?= =?us-ascii?Q?Wo6o3cfXMf8GybSNaW3KCDWqGKlDsztuoqjohiZq97To4L3QP2NeZT7mK/lB?= =?us-ascii?Q?QLaDuy+8Jk45IrXbMGT9VhsipHdfTiAORx4D1uT3AO9TO+3zvJo/ZfGWrOyx?= =?us-ascii?Q?picR8JjC1j7US2+P4HAFk9InFzJ7n+Z5CiWkdTKvEsNcBfKW4vJ2f3IIB0B2?= =?us-ascii?Q?1ZWhf+ZhAmOvSE/MNybbdDYtPc775s0V4jzIxXuVKS0+5Ec5wQAo/c/lQfiD?= =?us-ascii?Q?bYgpJzIs069cTBjAznt/Az7/7dh4pOnPG2kHtJE2obifvQE7WYbL30iVe9xX?= =?us-ascii?Q?anCN3wDRrr1T71vCr1xjFGRnMhHuXEq4Mv5DQUg9h0FnawkDgkMPRj+pk/7D?= =?us-ascii?Q?/VQUb47EvzZINRhGqLdONi1JXFO/BVbMeZoP0pBNoLL/Y0ENvZnYfQGt/w51?= =?us-ascii?Q?sJwhjxGUmPBR8mZzY350wAI90PqIyCSyjybbnTnnGvSDbrKw0xQrEAkAEtyC?= =?us-ascii?Q?ZbOg/Be/cC3yIujinjDppjFP3zziaCu62xEvZeldQQdOfaSS/UNUE/8w8dEd?= =?us-ascii?Q?EisOsfixl5ldRuJeYyD4ca+JELz69BYDC3zA0T2pgorGlSeuI4bly2qseV32?= =?us-ascii?Q?p1H6MYnBz6ECHbpwsQ6pqmHwpTqioUkMp4mRJYVR3mSvVx4sMb/Gsh9cl2dA?= =?us-ascii?Q?Qsv9Ko5lDU1b3t2ljuJxbpWWTxRgjidh2eFx+nL9yoVQVq1wMhb36aBUtCAB?= =?us-ascii?Q?b5e32kxISaXLETvzbpxLVbHUDbdzQy6YElVXupbaT7kTDwWVMALY4IHXb+qL?= =?us-ascii?Q?J887RP5UNl5HaT3rycO42t/dyP0pKvsAuvayoJ0XFvjzYv+SFmnNHLQdURlc?= =?us-ascii?Q?E0jv+bYlfk7PpCPgCVK/3vgvr/l5ZvHa?= 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:(13230040)(376014)(1800799024)(82310400026)(36860700013); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: r6zNdNSJ7WT+P3JmyMk9G74NoHBg5pSk/Z7AoGSE6J1Yh+hZVVOkOyde7M3fhZHLcBtEow5zRDr/4+1cxei/Lfmu71FMMSsN6eeaxeCyu9EQHlaL7OduYDTQF8bX0AjjjNq7WOpvYkjRUzYL+9za/bhg5X5NJVGdDVRW+1LWHKswZtkze2xKgtx2dE5VYmh7ZE5S+UGTBukCzj6kHcrj+olYgibvPmU3Ccbg7cNB1Rq+OpMwsA8EEVS8gDKJY4qJwY/45cVFFKp1rKvKrW/RwcWJpLTisVBWhyvJObieNZ7weuYMW3JXymcmdAY9YwmSzAbpimu/Df6U6IEJDGqLL1XJntp2JK0DmsHH5TwO3M4a3tvrfbN2Xx2CLFjgsBVirYKGi2WXyIiBC0f3MHw+wkBNBPMu0vuXq0ijdqoUbOBun9LK/F8F6Jot7WTwVC8RyPYHSKUDIkNYbGcuDXVMQ+ye8hOOZoxwYcYqRbj/tgRLuEZUTpFZ6QHI+iKiUbGarKtDagBtXwvXcDIVxzm3Y0b1Sd6kbCN1pba3uuQUpp462VCl1D314AsYKkcHq8kuETv6SE2RLA4NXSIzVy9l/HN0L+KQ2EnFvX2vqJRkcSW09wJgthtYHePcYX7fK2MylRplPoUpQi+rygfoGQUgew== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2024 15:47:44.3482 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a9295657-f42c-4d32-2175-08dca289f8b5 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: DU2PEPF00028D0C.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1P190MB2225 X-BESS-ID: 1720799273-305890-12640-32724-2 X-BESS-VER: 2019.1_20240702.1505 X-BESS-Apparent-Source-IP: 104.47.11.241 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVoaGBkbGQGYGUDQ5NS3RzDTJxN jI0sLIzMDQLCk1LcncMs3YwMLU2NjCVKk2FgDLrVYUQgAAAA== X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.257575 [from cloudscan22-202.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 Adds VFIO functionality and the DMA it requires. The VFIO context is initialized during ntnic ethdev startup. Signed-off-by: Serhii Iliushyk --- drivers/net/ntnic/meson.build | 1 + drivers/net/ntnic/ntnic_ethdev.c | 23 +++ drivers/net/ntnic/ntnic_vfio.c | 235 +++++++++++++++++++++++++++++ drivers/net/ntnic/ntnic_vfio.h | 29 ++++ drivers/net/ntnic/ntutil/nt_util.c | 65 ++++++++ 5 files changed, 353 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/meson.build b/drivers/net/ntnic/meson.build index c7f5a4efc7..bf86a0a098 100644 --- a/drivers/net/ntnic/meson.build +++ b/drivers/net/ntnic/meson.build @@ -18,5 +18,6 @@ includes = [ sources = files( 'ntlog/ntlog.c', 'ntutil/nt_util.c', + 'ntnic_vfio.c', 'ntnic_ethdev.c', ) diff --git a/drivers/net/ntnic/ntnic_ethdev.c b/drivers/net/ntnic/ntnic_ethdev.c index 26700eba6c..184bb43d92 100644 --- a/drivers/net/ntnic/ntnic_ethdev.c +++ b/drivers/net/ntnic/ntnic_ethdev.c @@ -8,10 +8,18 @@ #include #include +#include +#include +#include +#include + #include "ntlog.h" +#include "ntnic_vfio.h" #include "nt_util.h" +#define EXCEPTION_PATH_HID 0 + static const struct rte_pci_id nthw_pci_id_map[] = { { .vendor_id = 0, @@ -21,12 +29,24 @@ static const struct rte_pci_id nthw_pci_id_map[] = { 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++) { @@ -86,6 +106,8 @@ nthw_pci_dev_deinit(struct rte_eth_dev *eth_dev __rte_unused) continue; /* port already released */ rte_eth_dev_release_port(eth_dev); } + + nt_vfio_remove(EXCEPTION_PATH_HID); return 0; } @@ -150,3 +172,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/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