From patchwork Wed Jan 24 09:25:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chaoyong He X-Patchwork-Id: 136095 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 3191D439B4; Wed, 24 Jan 2024 10:26:09 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6F48C402E6; Wed, 24 Jan 2024 10:26:05 +0100 (CET) Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2102.outbound.protection.outlook.com [40.107.101.102]) by mails.dpdk.org (Postfix) with ESMTP id C04A5400D5 for ; Wed, 24 Jan 2024 10:26:03 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TLN1PUS9yte6/gNvrAyq89F4baNHApRLMK4Uospm0mAPqSNIhTL5tWHkzKQoXRV/xfSlmHyHu9IdpzKSILCUzhASfVT7PmRCMlODaUa0Z3YCEHurkP0E9zG61/Y4onrm6jGQvTIqd4xBLFIycDZD6ag9rX9ARhoQOBjUoJRG+B4k/dgn2OgypqXt6nCNSNVgv5xRZwDpy0GcLFbeGy5JxHmT0XqsKi6lW6Rw04ghSfIePV6rzYae0bghDCBsxewxPqyFd0rL6BrTr/b5YzHvmDcotB1Pr/cZ3z5ItgKrlOUdvwUSy2oK9MLaiiqC+Bx6noWIUvY7JCwzxSQ0tjzKLQ== 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=ayhiMDeAVyDskGSfgU7JG/x9H7S7OjJyk0qqkdSOVMQ=; b=WXfjWuphnQWU3XiOKS8aXtk9jIa7kWoVxDH1Cmi8ImAcBq62HKaomYhQzLTMzaotcd+kcvSm++sqxvfe6viqqm8tZTzYN9sx38UMKkWb33UATG9O+ElxZI892t+OOlqGe3FqCd1gdIL7ZigxuyZBn0a6AlkIN/6INppqjYBAAfMVmrXLEwC1aAe5JU2QNwTnbtN819GbrS8lLW3Dg/sT2JnnMlungpuyon+9AyVxGMm8r4SnQNlSZRctUmBy5bfrAf5n+qDaKuNcrCEwiihETmeT7PWp5TuGwpks0QwCEjvy7h2T7LFYVuDXh1sS6SpgQdOCOkG6gEGZBrJwHu2t3Q== 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=ayhiMDeAVyDskGSfgU7JG/x9H7S7OjJyk0qqkdSOVMQ=; b=bNjn+UHNTrNjGkyYNfoPUaJPqR4tzzqdsBudRJImo6DBJubdNo5GTYWmSjy+CtneEiJ7EPFlsutxnto0+AWLr96m6vyT9wKxwdKNo8pkj6d1N64HePeO6wjJMdDw0+BYSDW06YINu49LnUcTzfmO75WQAZRr/mvjIfOoddMOPVA= 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 DM8PR13MB5093.namprd13.prod.outlook.com (2603:10b6:8:20::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 09:26:01 +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.7228.022; Wed, 24 Jan 2024 09:26:01 +0000 From: Chaoyong He To: dev@dpdk.org Cc: oss-drivers@corigine.com, Long Wu , Chaoyong He , Peng Zhang Subject: [PATCH 1/4] net/nfp: add synchronize module Date: Wed, 24 Jan 2024 17:25:38 +0800 Message-Id: <20240124092541.2011874-2-chaoyong.he@corigine.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240124092541.2011874-1-chaoyong.he@corigine.com> References: <20240124092541.2011874-1-chaoyong.he@corigine.com> X-ClientProxiedBy: PH8PR05CA0019.namprd05.prod.outlook.com (2603:10b6:510:2cc::25) To SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR13MB5545:EE_|DM8PR13MB5093:EE_ X-MS-Office365-Filtering-Correlation-Id: b03ea80b-d68f-4c2e-c911-08dc1cbe7b2b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vuy9R50Mk36I9kElJeepPFc4DjAeAAHii3igKJJxQXsGmtLB5MUl792iDa0P2WdaD1e5eUfBNlRe5/QEmf5RkYvxoBOPZr2DEYrO6zrlNBz65PTZfb0/J9LA5dFqkkZOvj2u9b+DetVvI0GLwakNFib6+ZKRRGRHdCcGcBnKCQPCX+B+ulSnnGJCIWe4G94UgIBUTfcCoFj/rSWxik79RN3rqYdly0QXJiogmSCmZGCdPwV+eRY11zjmzbwhwCxFjn5W5xNP6XCMNnC6vkIOWv43qR3wrs16Gl3zg+0F48mHuIRlhjZw/WucPMqE7m9yN0eUmpJVGWPS+4LFZl98AFAyfLZjnvLLBv5FefBVrApzbj4wRihRfQWU5jewpN9lZ1jWkIa1vhknhseuYfmyNN639jljXeDmBV1tNYpfsvEvaQkRsVkxgSQtx+DiFHWGQlE8nLxaj+mrje5qARBIeLrtfF3Xm+ga2gbqIdVP4t5M2D6M23VeWTwCwkPEFqhQqZKwgk3g0a6cew8UYZ81PaaVjqq0+vx5Al1HuziywYZG1PHo3QXpK4LQQZwJfqkdLMfct7A75+DjxQrFhV1/LNnX9LOW5n2rZ3NKlBsSkrFoqrULgol/6LcnkdSXuTd8 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)(346002)(136003)(396003)(366004)(376002)(39830400003)(230922051799003)(186009)(64100799003)(451199024)(1800799012)(83380400001)(1076003)(26005)(66946007)(44832011)(8936002)(6916009)(5660300002)(30864003)(478600001)(54906003)(66556008)(316002)(4326008)(66476007)(86362001)(2906002)(38100700002)(6486002)(2616005)(6512007)(107886003)(41300700001)(6506007)(36756003)(38350700005)(8676002)(6666004)(52116002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pgYUGtQdphKDRKip8JjACngvyvYVjDAvUH+/xjmWEgdi6p6kzidU+QgpLk7LlttHx54FCdJKGv4Gq9p52xJU6QTl7d1C3zhCvkph+icEz/Tk4Vw9MHGznj1Ss//hYRX5b7pRb5upz8B/j3R/zEdipc2gCxQGFPr5i35S3/aUKSAMdDQ1TO33RIFqP3ErkWtJLboKRFTt28v/DkbcVCUfPC9q+T4oWbbmbqYDf6GVVPO2JnwhqYhBhTT1ALPAzn/KFPxrgMm66VeiPPziFbeXUucOeg0genICl/tlBFSFzWsF5J4fJLgafAdONvpLIrlpub9MlBYF6jxfHEvTvLaqQYhImmhrTyftWqeu+XR0cs0Ipt07VRg1CrZy8/eyKV1F0l8KkXXfr5Gg3ZVdT/IOIdROVQHo2eQMYdsj93U54nTz7mrZcOevFkESflZR05cbH2/JzpP2fNbCv0s2/yD51//BLWH95D/JZFdcuf2hTHvRNr7SlLkEE+U8X0bt5DIR9MFq1XcnyvS/H6iKQeNJ31dUzSPIZGYp05EFeiOWLzMk7t9sfbiqqCJLpHP/kmeavHw1W91JYxsfBB4SlJiewrFle9Jf19pGMowLR/681iNSMPz2ZfqROdHUoLdWRj+cU8x9vcrvuNxFTVWA46zHBsrSOXVYsg+GhtYc3NxoJip9UlcZQV97LiJIzFBqpQBLrJliaVZKK2w2yMiS92aPdvmnYbXWsnUhWMOsLt9HZ/zNNy+0KnEQDIdOht19GqAyfbC2j0+7/SF88RfPdkdGF8lq8k4KjxzufzwsUjN6eXGnIucvqQSW/TtCzy11If9GQMGVUPHiieUW70uqxnXv2BVwOKcpOGlOxe6l9AnKjmubHZxlmn1QTdbKCVFy8+E4Lq3a4o2VpQ7fTszJXIRNMdeeI8uFH6YhB9GqjInxrm8HQK68nXMWfh1OqPDFEs9RaQN5D1oUFrMdx9XT38o8eG7naXbN62XRpNYh8H9l9ij6lxEPYfRHOg4C+kCtgMW3sJ+W7vgclfAsO5JNczSvjTclbIhj54LaSI7TnMiVFHC8ffzWjexerEZ9CGDp8L5GtP0J6KBa6u/63N+kD+nKFwagFo67DTj465extXbEFE5ntRIwnGyRXjlNiAe42ii3IJeSXEGkRtHIXMUrFB4nFtjPwV8LrIxjQYzDpPO8+CkfmhglD4xI3jWQhWACrg1r8X5D1+Jv5oEVSu/dOSr3lSqiIkaflu/9BR4PrKsDnEw3nj/Nh1jozFx2JrTlLpcyW+1BBxEvweKzMfZWkPfooBXXZ++dBSR0nn5eZzXNl+gwpXf//Bsok+jdPc7GmHAtP/mXb3VFroj084JIy1T18h1kGisRedoDIJWF634jolKH4aC2ARCsxHjHTm2k1WR4PzxRQUHHN4bg2O0oBLa/5tjtiyQO7sViQmx1PN1b5gnGKBqWy//sl/JA+hECwHzh2hCQcUXeUEkuhrGP9gkrPteO3c6ENmtvQ9iOILIAa787S9n5Eo5Fk8gt8oeNrWqPXBlgyYFv/0kfxj/xWj36n00DCI7so3UswBBA0wssMcI6ebNQZOEoOHTpSiVVgEQT2OGPi16w3KY30YSLHcURxw== X-OriginatorOrg: corigine.com X-MS-Exchange-CrossTenant-Network-Message-Id: b03ea80b-d68f-4c2e-c911-08dc1cbe7b2b X-MS-Exchange-CrossTenant-AuthSource: SJ0PR13MB5545.namprd13.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 09:26:01.8549 (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: HqLQ6uOULHZxa5ymmLN2BQViOi7BTEIUL+hqRz2+zWcon6fi1ORhlBWOX/N9jU76FDbKDd95tMB16/imUkHEgxyawF7WdbthoGuT5XPf3b4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR13MB5093 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 66c900e3b8..110ca015c0 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__ */