From patchwork Fri Feb 2 03:04:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chaoyong He X-Patchwork-Id: 136300 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 11DA243A49; Fri, 2 Feb 2024 04:04:55 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5372E42D28; Fri, 2 Feb 2024 04:04:49 +0100 (CET) Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2094.outbound.protection.outlook.com [40.107.94.94]) by mails.dpdk.org (Postfix) with ESMTP id AC3E440268 for ; Fri, 2 Feb 2024 04:04:47 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TG2vQgftARknsy0L2DNuCscRHx68nNtqr+ZOzbNJdw1GRZvUb1Ly6E3uQ+AUQ+7BD3YcuThp9CjG4Vlmu0AK4MrTah22zQcCi40PpYpaPMbE1pVIbc8m5Qs3q7S9f8Sf1Fzg2VnlsbCOjnlOr+eo7v32ECU6osD56TVE9N2/5HkbYBJLnxP0OB9z6lCE6SzHr5K0xscizQeCGAO4y+Dpa3XN6zX13r8WDxlrAjErkPqKYK5UD4gSurdVs1i5mlUk5dTDHrFT8a3IzGplfO2EaGd6K06OJlx0/HW4d+EmSw8WYHWMnZitnFcT8wgqMG8QLrJUm7xvVT11OEoF5qE3YQ== 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=EDdidAtLEGr6J7F9yLfLTZ8UHNmJvBfxSXmNem/GJfk=; b=NufAKxM7lqbkp7HANFAEEym/WacMt/wqzGzD+1E6Era5vi3YpjXAL8OtxY/92TsI4Z8pan0DrW0dM8jAVCNeEzks0LNNR/ExpowU2v6n+MwtbylzIrrafAbdgw8Ga9GAMKM6rT7iXcmS1oQ29LIJ+4fwxZsxywdygRnhctwSzCVIUiBIYLpQ4GFL6pmxAySM3l9tYxA1pAhCpXd3qmi/yxVIABSuD/AxtmpLcvJ7N9RhJRoqNXP3DeD/d0PXez4/LHiEUUqREeST6BHhFkQbPei9mVZCaWQVhroRj9+HUJzQ+AJz/wJyXJFzHrTHK/ws2MpPHrIAKGeEAJgfSNU93w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=corigine.com; dmarc=pass action=none header.from=corigine.com; dkim=pass header.d=corigine.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=corigine.onmicrosoft.com; s=selector2-corigine-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EDdidAtLEGr6J7F9yLfLTZ8UHNmJvBfxSXmNem/GJfk=; b=GE4PQSpyLs4i3lL5SidpadiCH3Gc7//Mv24Xr9XtmXH9W1Eje0T3usQKMCcM5QDfA6Xqk+sKMCI1bJZkFmTWSjlpclxErhOuqZjxQMfqHPBKhs3mNfRHA41ZWtS8MnDQ4zaDFZq9AaQ5o7+oxOe77f0z6PH2e0J1To2wAtdykbU= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=corigine.com; Received: from SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) by SN7PR13MB6177.namprd13.prod.outlook.com (2603:10b6:806:2e3::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.30; Fri, 2 Feb 2024 03:04:45 +0000 Received: from SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::8e02:f738:570a:f8aa]) by SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::8e02:f738:570a:f8aa%7]) with mapi id 15.20.7249.024; Fri, 2 Feb 2024 03:04:45 +0000 From: Chaoyong He To: dev@dpdk.org Cc: oss-drivers@corigine.com, Long Wu , Chaoyong He , Peng Zhang Subject: [PATCH v2 1/3] net/nfp: add synchronize module Date: Fri, 2 Feb 2024 11:04:13 +0800 Message-Id: <20240202030415.739638-2-chaoyong.he@corigine.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240202030415.739638-1-chaoyong.he@corigine.com> References: <20240124092541.2011874-1-chaoyong.he@corigine.com> <20240202030415.739638-1-chaoyong.he@corigine.com> X-ClientProxiedBy: BYAPR05CA0004.namprd05.prod.outlook.com (2603:10b6:a03:c0::17) To SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR13MB5545:EE_|SN7PR13MB6177:EE_ X-MS-Office365-Filtering-Correlation-Id: 59109f1b-f34a-4c25-17b0-08dc239bb5af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AT/Y5NU1wTNcrAggBH9SbPP5uMKzUq1SYaDoKv8AL2g2d85wRofsNQkHkynxEPmIZ+KrQwe4hSs5eszAr5lPp7BZ0WjJfUkH0nIYDEcFHN0OeavhLg0K1a5pyZAi9PrGWkBHcFUUGj6ufEV3XyCf3G5jJR6t6aYXxzFpAqSCPCMKap7zmqGxPQzFQ1nx+bLLdydiINdCg+icnCiC0/gYppjFpbLt4/5kB6KyqONm8fmCYcbzgUfXMgIQXb4/d8SMtudAX9WDIRhhvp2xyz/lemgxkKD9TcaNGGpRJXtcR8UZsZQZvWPXZt3VyPbVimfJg6pTqFUMPtNN7ASL7AMRRsuJxLzUXtHADH/GuzGMLY7cm74zxnNBZ9YshfsJjFr4wFrsTv8So53Z97FPW2+frXQ2EO+q+mXp8nxxJcn/DbDNbeuTbWWPRiLMbzTB177787/szReXA99faMa1W0DhWUm+F6m+VJb4jDaHtLiXYoN/oTxMWO8Jker+ffSIDsgbBpdxeZgiHpOpzxFhn17poBIHyXrFwUnxjtUEntidHbTPp8ci/9LidAfkaDY9dmtKBn93yF/XYsl1RHNNVk01nKItLUE19XcBaW+/do/V4PxWv84UZNrwqm8raV1NkVpo X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SJ0PR13MB5545.namprd13.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(366004)(136003)(376002)(396003)(39840400004)(346002)(230922051799003)(451199024)(186009)(64100799003)(1800799012)(41300700001)(1076003)(107886003)(26005)(2616005)(6916009)(38350700005)(316002)(36756003)(478600001)(6512007)(6486002)(83380400001)(52116002)(6506007)(6666004)(38100700002)(86362001)(2906002)(5660300002)(30864003)(66946007)(54906003)(66476007)(66556008)(8936002)(44832011)(8676002)(4326008); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ssT5kUUZZOA7DfuvcYumF1KfX9nf9DnQ2T32FclX+FgdB9PvmhTo2karT1w5DzGknUGmOJCMA9yiLeWzcvN5NyIOxWM3vsL75i9jQgjb9aie4CiIEXXK9ctSAmz/GmDit8uGFRp94xwsx4AyPDhGq+TcFfNntMJC0RBZ/7NvjsmRZYcWgoki5sF+HXseveHRhCLoV6qmdTSwIqjJbr2/H4kH8KWfjbry9ynrQW+ly80Gdq08x6Xi9gGKNrwXg8rWS/A1vwPOFlRhQw/IrPibkzsn7ErvyHhbhZe0wI5G5zxwc7VSlxuQHce6iAFT7ka3FMSd08R3eVWhSzVjvDMngUUBsbniMP+PvMwEEjlyDRt2Zr2prmghs50+O2m1UYFCpig/npPAYYQSbkzbNemTPqoXvYUJGeNc1SlAKvAF10+OSGtoDSuorPAD+OS7/9wmnjkdzpjakKc3NCJyQQtElLIKeGnQYWcl8hpogdkLc61ZXwFH5KGb4nEudCU7X8uO6He6Sp7WlKkL0NZ02fa62jGYWZXIZVK/ioat2n2Q92Q9wc5l52nBL7rAVMUoF03+ChPDzFc1UV9Q8Ma+2aR8NumAM3wlNvLygDTT1NlHSlnCXpE5eL1A1VV18gMSFJaqrmiIvAFASd4A1EbsBn7Rix7lYZpNMgTHaA2tUx5HZQ5yYhhDurNav3wZ9AMKFYU7l0esBezAcXVc8dPC+ev5gFzzAXqgT2V+aeMHGmprW48f6yGyQJgSH2+5YdmrfYNKFg13HFdtpEtSujLmWkuRqGqjHz7oggPaiA/qX2g3PsvLEaXp9hKy/5fZrsceVO97d1AD0+yrrwziac8ZywZqTOfjR+MR7eySNlMF7lXhvxsX7NGoZE2G/FXlxmozcuaPs2uVISNkOHXVL4ptohfOEcTqf/KqnxW2EOMMD0AKID22Zh1PcOuHhAfPh5PTZLXn0W5SmiiD02CSUMHHRN9t6n4YPacvi2/G2kzhoT/ZiW170gOKRHqbWSjt5Tf6/y4VFYwmZYQG1LQA0JmA6c6r8cx0RQcXD+kFjsyQ5Ud5Ebbrcu+32RU3rKBBRB2UlFsGnghfqdkWhlDgqOMY8qQTkWyMQ8oobwkRas2+YTuVzTXreckJ1/Wab6Sv0eeugAFrCZkfqjAel2t4vAm6t0ZfxE0GlK0MMI0SY09yIrnWWVOShnPUke+5Zgn+nIoUouiM2WCd8CIDLRwC40U3FdUHpis1rCMBBoKYdaz/lfaZbk8cyzp9ndDKGBe99vAWDc9uBoDomAIwaKv+ZjYngH1LJuKK/7vc57/WVBZZ/FV0x0jvj7qKXNaLdE8/iwaeCcTGd7DfsGN6hvgc8Sz0doK87oKDL8wn+aHbJ4RkTkd9/o8cFktXjkn9cex61OGNxgigFJLdwXzCIqK7eOXQ56hiA7Bek5XB2K/U4FgowgYTjPFRijYW79llfgl4h/8XdgDgltuiHhV5s1pQZi1nRb5Jt0s/yfE+tOKT/QnlOe3gi9b2ZcEkO5ZnV8SwIX7ffwKOXtU30EVVJrbUw9tFTUU2rkKDACopJPAr1kr7sZ9S3Adeb3HonEDG+VWZT1eUrKMEjdUFHIb5VF4HzY4s1AUx0g== X-OriginatorOrg: corigine.com X-MS-Exchange-CrossTenant-Network-Message-Id: 59109f1b-f34a-4c25-17b0-08dc239bb5af X-MS-Exchange-CrossTenant-AuthSource: SJ0PR13MB5545.namprd13.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 03:04:45.7444 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: fe128f2c-073b-4c20-818e-7246a585940c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8bwwP5mK6wlldgVMh6yc9RWBwaudZCvObq7XULiJDXloh/Yysmm8uqkDm0eTPJld7wgjgjRXulJc6asM0ugbY4dzrFENhyY0LGEpeC54EeU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR13MB6177 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 From: Long Wu PMD has some information that needs synchronized: 1. Between devices in one process. 2. Between multiple processes for one device, before creating "rte_eth_device". 3. Between multiple processes for one device, after creating "rte_eth_device". The final one is already support by framework (eth_dev->data->dev_private), and this patch aims to support the first two cases by adding a synchronize module. Signed-off-by: Long Wu Reviewed-by: Chaoyong He Reviewed-by: Peng Zhang --- drivers/net/nfp/meson.build | 1 + drivers/net/nfp/nfp_ethdev.c | 31 ++- drivers/net/nfp/nfp_net_common.h | 4 + drivers/net/nfp/nfpcore/nfp_sync.c | 382 +++++++++++++++++++++++++++++ drivers/net/nfp/nfpcore/nfp_sync.h | 29 +++ 5 files changed, 444 insertions(+), 3 deletions(-) create mode 100644 drivers/net/nfp/nfpcore/nfp_sync.c create mode 100644 drivers/net/nfp/nfpcore/nfp_sync.h diff --git a/drivers/net/nfp/meson.build b/drivers/net/nfp/meson.build index 46be6f60cd..c625355d7a 100644 --- a/drivers/net/nfp/meson.build +++ b/drivers/net/nfp/meson.build @@ -26,6 +26,7 @@ sources = files( 'nfpcore/nfp_nsp_eth.c', 'nfpcore/nfp_resource.c', 'nfpcore/nfp_rtsym.c', + 'nfpcore/nfp_sync.c', 'nfpcore/nfp_target.c', 'nfpcore/nfp6000_pcie.c', 'nfp_cpp_bridge.c', diff --git a/drivers/net/nfp/nfp_ethdev.c b/drivers/net/nfp/nfp_ethdev.c index 07cd32881c..3ece806487 100644 --- a/drivers/net/nfp/nfp_ethdev.c +++ b/drivers/net/nfp/nfp_ethdev.c @@ -19,6 +19,7 @@ #include "nfpcore/nfp_nsp.h" #include "nfpcore/nfp6000_pcie.h" #include "nfpcore/nfp_resource.h" +#include "nfpcore/nfp_sync.h" #include "nfp_cpp_bridge.h" #include "nfp_ipsec.h" @@ -551,6 +552,7 @@ nfp_pf_uninit(struct nfp_pf_dev *pf_dev) free(pf_dev->nfp_eth_table); free(pf_dev->hwinfo); nfp_cpp_free(pf_dev->cpp); + nfp_sync_free(pf_dev->sync); rte_free(pf_dev); } @@ -559,6 +561,7 @@ nfp_pf_secondary_uninit(struct nfp_pf_dev *pf_dev) { free(pf_dev->sym_tbl); nfp_cpp_free(pf_dev->cpp); + nfp_sync_free(pf_dev->sync); rte_free(pf_dev); return 0; @@ -1612,6 +1615,7 @@ nfp_net_speed_capa_get(struct nfp_pf_dev *pf_dev, static int nfp_pf_init(struct rte_pci_device *pci_dev) { + void *sync; uint32_t i; uint32_t id; int ret = 0; @@ -1652,6 +1656,13 @@ nfp_pf_init(struct rte_pci_device *pci_dev) return -ENOMEM; } + sync = nfp_sync_alloc(); + if (sync == NULL) { + PMD_INIT_LOG(ERR, "Failed to alloc sync zone."); + ret = -ENOMEM; + goto pf_cleanup; + } + /* * When device bound to UIO, the device could be used, by mistake, * by two DPDK apps, and the UIO driver does not avoid it. This @@ -1667,7 +1678,7 @@ nfp_pf_init(struct rte_pci_device *pci_dev) if (cpp == NULL) { PMD_INIT_LOG(ERR, "A CPP handle can not be obtained"); ret = -EIO; - goto pf_cleanup; + goto sync_free; } hwinfo = nfp_hwinfo_read(cpp); @@ -1734,6 +1745,7 @@ nfp_pf_init(struct rte_pci_device *pci_dev) pf_dev->sym_tbl = sym_tbl; pf_dev->pci_dev = pci_dev; pf_dev->nfp_eth_table = nfp_eth_table; + pf_dev->sync = sync; /* Get the speed capability */ for (i = 0; i < nfp_eth_table->count; i++) { @@ -1815,6 +1827,8 @@ nfp_pf_init(struct rte_pci_device *pci_dev) free(hwinfo); cpp_cleanup: nfp_cpp_free(cpp); +sync_free: + nfp_sync_free(sync); pf_cleanup: rte_free(pf_dev); @@ -1878,6 +1892,7 @@ nfp_secondary_init_app_fw_nic(struct nfp_pf_dev *pf_dev) static int nfp_pf_secondary_init(struct rte_pci_device *pci_dev) { + void *sync; int ret = 0; struct nfp_cpp *cpp; uint8_t function_id; @@ -1910,6 +1925,13 @@ nfp_pf_secondary_init(struct rte_pci_device *pci_dev) return -ENOMEM; } + sync = nfp_sync_alloc(); + if (sync == NULL) { + PMD_INIT_LOG(ERR, "Failed to alloc sync zone."); + ret = -ENOMEM; + goto pf_cleanup; + } + /* * When device bound to UIO, the device could be used, by mistake, * by two DPDK apps, and the UIO driver does not avoid it. This @@ -1925,7 +1947,7 @@ nfp_pf_secondary_init(struct rte_pci_device *pci_dev) if (cpp == NULL) { PMD_INIT_LOG(ERR, "A CPP handle can not be obtained"); ret = -EIO; - goto pf_cleanup; + goto sync_free; } /* @@ -1936,7 +1958,7 @@ nfp_pf_secondary_init(struct rte_pci_device *pci_dev) if (sym_tbl == NULL) { PMD_INIT_LOG(ERR, "Something is wrong with the firmware symbol table"); ret = -EIO; - goto pf_cleanup; + goto sync_free; } /* Read the app ID of the firmware loaded */ @@ -1954,6 +1976,7 @@ nfp_pf_secondary_init(struct rte_pci_device *pci_dev) pf_dev->cpp = cpp; pf_dev->sym_tbl = sym_tbl; pf_dev->pci_dev = pci_dev; + pf_dev->sync = sync; /* Call app specific init code now */ switch (app_fw_id) { @@ -1983,6 +2006,8 @@ nfp_pf_secondary_init(struct rte_pci_device *pci_dev) sym_tbl_cleanup: free(sym_tbl); +sync_free: + nfp_sync_free(sync); pf_cleanup: rte_free(pf_dev); diff --git a/drivers/net/nfp/nfp_net_common.h b/drivers/net/nfp/nfp_net_common.h index 5648ad32d8..3a441f49f6 100644 --- a/drivers/net/nfp/nfp_net_common.h +++ b/drivers/net/nfp/nfp_net_common.h @@ -12,6 +12,7 @@ #include #include +#include "nfpcore/nfp_sync.h" #include "nfp_net_ctrl.h" /* Interrupt definitions */ @@ -109,6 +110,9 @@ struct nfp_pf_dev { /** Supported speeds bitmap */ uint32_t speed_capa; + + /** Synchronized info */ + struct nfp_sync *sync; }; #define NFP_NET_FLOW_LIMIT 1024 diff --git a/drivers/net/nfp/nfpcore/nfp_sync.c b/drivers/net/nfp/nfpcore/nfp_sync.c new file mode 100644 index 0000000000..8bc38d6585 --- /dev/null +++ b/drivers/net/nfp/nfpcore/nfp_sync.c @@ -0,0 +1,382 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2024 Corigine, Inc. + * All rights reserved. + */ + +#include "nfp_sync.h" + +#include +#include +#include +#include + +#include "nfp_logs.h" + +#define NFP_SYNC_ELEMENT_MAX 8 +#define NFP_SYNC_PCI_MAX 32 + +struct nfp_sync_element { + uint16_t count; + /** Element ID, use ASCII - SYN<> */ + uint32_t magic; + void *handle; +}; + +struct nfp_sync_common { + char pci_name[PCI_PRI_STR_SIZE + 1]; + uint16_t avail; + struct nfp_sync_element element[NFP_SYNC_ELEMENT_MAX]; +}; + +struct nfp_sync { + rte_spinlock_t spinlock; + + uint16_t alloc_count; + + struct nfp_sync_common process; + + struct nfp_sync_common pci[NFP_SYNC_PCI_MAX]; + + const struct rte_memzone *mz; +}; + +struct nfp_sync * +nfp_sync_alloc(void) +{ + uint16_t i; + struct nfp_sync *sync; + const struct rte_memzone *mz; + + mz = rte_memzone_lookup("nfp_sync"); + if (mz != NULL) { + sync = mz->addr; + sync->alloc_count++; + + return sync; + } + + mz = rte_memzone_reserve("nfp_sync", sizeof(*sync), SOCKET_ID_ANY, + RTE_MEMZONE_SIZE_HINT_ONLY); + if (mz == NULL) + return NULL; + + sync = mz->addr; + + memset(sync, 0, sizeof(*sync)); + + rte_spinlock_init(&sync->spinlock); + sync->alloc_count = 1; + sync->mz = mz; + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) + sync->pci[i].avail = NFP_SYNC_ELEMENT_MAX; + + sync->process.avail = NFP_SYNC_ELEMENT_MAX; + + return sync; +} + +void +nfp_sync_free(struct nfp_sync *sync) +{ + uint16_t i; + + rte_spinlock_lock(&sync->spinlock); + + sync->alloc_count--; + if (sync->alloc_count != 0) { + rte_spinlock_unlock(&sync->spinlock); + return; + } + + if (sync->process.avail != NFP_SYNC_ELEMENT_MAX) + PMD_DRV_LOG(ERR, "Sync process handle residue"); + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) { + if (sync->pci[i].avail != NFP_SYNC_ELEMENT_MAX) + PMD_DRV_LOG(ERR, "Sync %s pci handle residue", + sync->pci[i].pci_name); + } + + rte_spinlock_unlock(&sync->spinlock); + + rte_memzone_free(sync->mz); +} + +static void * +nfp_sync_element_alloc(struct nfp_sync_element *element, + uint32_t magic, + uint32_t size) +{ + void *handle; + + handle = rte_zmalloc(NULL, size, 0); + if (handle == NULL) + return NULL; + + element->handle = handle; + element->count = 1; + element->magic = magic; + + return handle; +} + +static void +nfp_sync_element_free(struct nfp_sync_element *element, + void *handle) +{ + element->count--; + if (element->count != 0) + return; + + rte_free(handle); + element->handle = NULL; + element->magic = 0; +} + +static void * +nfp_sync_common_handle_alloc(struct nfp_sync_common *common, + uint32_t magic, + uint32_t size) +{ + uint16_t i; + void *handle = NULL; + uint16_t avail_slot = NFP_SYNC_ELEMENT_MAX; + + for (i = 0; i < NFP_SYNC_ELEMENT_MAX; i++) { + if (common->element[i].magic != magic) + continue; + + common->element[i].count++; + + return common->element[i].handle; + } + + if (common->avail == 0) + return NULL; + + for (i = 0; i < NFP_SYNC_ELEMENT_MAX; i++) { + if (common->element[i].magic == 0) { + avail_slot = i; + break; + } + } + + handle = nfp_sync_element_alloc(&common->element[avail_slot], magic, size); + if (handle == NULL) + return NULL; + + common->avail--; + + return handle; +} + +static void +nfp_sync_common_handle_free(struct nfp_sync_common *common, + void *handle) +{ + uint16_t i; + + if (common->avail == NFP_SYNC_ELEMENT_MAX) + return; + + for (i = 0; i < NFP_SYNC_ELEMENT_MAX; i++) { + if (common->element[i].handle == handle) + break; + } + + if (i == NFP_SYNC_ELEMENT_MAX) + return; + + nfp_sync_element_free(&common->element[i], handle); + + if (common->element[i].count == 0) + common->avail++; +} + +static void * +nfp_sync_process_inner_handle_alloc(struct nfp_sync *sync, + uint32_t magic, + uint32_t size) +{ + void *handle = NULL; + + rte_spinlock_lock(&sync->spinlock); + + handle = nfp_sync_common_handle_alloc(&sync->process, magic, size); + if (handle == NULL) + PMD_DRV_LOG(ERR, "Process handle alloc failed"); + + rte_spinlock_unlock(&sync->spinlock); + + return handle; +} + +static void +nfp_sync_process_inner_handle_free(struct nfp_sync *sync, + void *handle) +{ + rte_spinlock_lock(&sync->spinlock); + + nfp_sync_common_handle_free(&sync->process, handle); + + rte_spinlock_unlock(&sync->spinlock); +} + +static uint16_t +nfp_sync_process_handle_count_get(struct nfp_sync *sync, + void *handle) +{ + uint16_t i; + uint16_t count = 0; + + rte_spinlock_lock(&sync->spinlock); + + for (i = 0; i < NFP_SYNC_ELEMENT_MAX; i++) { + if (sync->process.element[i].handle == handle) { + count = sync->process.element[i].count; + break; + } + } + + rte_spinlock_unlock(&sync->spinlock); + + return count; +} + +static void * +nfp_sync_pci_inner_handle_alloc(struct nfp_sync *sync, + const char *pci_name, + uint32_t magic, + uint32_t size) +{ + uint16_t i; + void *handle = NULL; + uint16_t pci_avail_id = NFP_SYNC_PCI_MAX; + + rte_spinlock_lock(&sync->spinlock); + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) { + if (strcmp(pci_name, sync->pci[i].pci_name) == 0) { + pci_avail_id = i; + goto common_alloc; + } + } + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) { + if (strlen(sync->pci[i].pci_name) == 0) { + pci_avail_id = i; + strcpy(sync->pci[pci_avail_id].pci_name, pci_name); + goto common_alloc; + } + } + + rte_spinlock_unlock(&sync->spinlock); + + return NULL; + +common_alloc: + handle = nfp_sync_common_handle_alloc(&sync->pci[pci_avail_id], + magic, size); + if (handle == NULL) + PMD_DRV_LOG(ERR, "PCI handle alloc failed"); + + rte_spinlock_unlock(&sync->spinlock); + + return handle; +} + +static void +nfp_sync_pci_inner_handle_free(struct nfp_sync *sync, + const char *pci_name, + void *handle) +{ + uint16_t i; + char *name_tmp; + + rte_spinlock_lock(&sync->spinlock); + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) { + name_tmp = sync->pci[i].pci_name; + if (strlen(name_tmp) != 0 && strcmp(pci_name, name_tmp) == 0) { + nfp_sync_common_handle_free(&sync->pci[i], handle); + if (sync->pci[i].avail == NFP_SYNC_ELEMENT_MAX) + name_tmp[0] = 0; + break; + } + } + + rte_spinlock_unlock(&sync->spinlock); +} + +static uint16_t +nfp_sync_pci_handle_count_get(struct nfp_sync *sync, + const char *pci_name, + void *handle) +{ + uint16_t i; + uint16_t count = 0; + struct nfp_sync_common *pci_common; + + rte_spinlock_lock(&sync->spinlock); + + for (i = 0; i < NFP_SYNC_PCI_MAX; i++) { + if (strcmp(sync->pci[i].pci_name, pci_name) == 0) + break; + } + + if (i == NFP_SYNC_PCI_MAX) { + rte_spinlock_unlock(&sync->spinlock); + return 0; + } + + pci_common = &sync->pci[i]; + + for (i = 0; i < NFP_SYNC_ELEMENT_MAX; i++) { + if (pci_common->element[i].handle == handle) { + count = pci_common->element[i].count; + break; + } + } + + rte_spinlock_unlock(&sync->spinlock); + + return count; +} + +void * +nfp_sync_handle_alloc(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + uint32_t magic, + uint32_t size) +{ + if (pci_dev == NULL) + return nfp_sync_process_inner_handle_alloc(sync, magic, size); + + return nfp_sync_pci_inner_handle_alloc(sync, pci_dev->device.name, + magic, size); +} + +void +nfp_sync_handle_free(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + void *handle) +{ + if (pci_dev == NULL) { + nfp_sync_process_inner_handle_free(sync, handle); + return; + } + + nfp_sync_pci_inner_handle_free(sync, pci_dev->device.name, handle); +} + +uint16_t +nfp_sync_handle_count_get(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + void *handle) +{ + if (pci_dev == NULL) + return nfp_sync_process_handle_count_get(sync, handle); + + return nfp_sync_pci_handle_count_get(sync, pci_dev->device.name, handle); +} diff --git a/drivers/net/nfp/nfpcore/nfp_sync.h b/drivers/net/nfp/nfpcore/nfp_sync.h new file mode 100644 index 0000000000..82f01e2652 --- /dev/null +++ b/drivers/net/nfp/nfpcore/nfp_sync.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2024 Corigine, Inc. + * All rights reserved. + */ + +#ifndef __NFP_SYNC_H__ +#define __NFP_SYNC_H__ + +#include + +#include + +struct nfp_sync; + +struct nfp_sync *nfp_sync_alloc(void); +void nfp_sync_free(struct nfp_sync *sync); + +void *nfp_sync_handle_alloc(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + uint32_t magic, + uint32_t size); +void nfp_sync_handle_free(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + void *handle); +uint16_t nfp_sync_handle_count_get(struct nfp_sync *sync, + struct rte_pci_device *pci_dev, + void *handle); + +#endif /* __NFP_SYNC_H__ */