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 C91DC425A3; Fri, 15 Sep 2023 11:18:59 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 08C5442D7D; Fri, 15 Sep 2023 11:17:03 +0200 (CEST) Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2117.outbound.protection.outlook.com [40.107.223.117]) by mails.dpdk.org (Postfix) with ESMTP id 8E48342D27 for ; Fri, 15 Sep 2023 11:17:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Oi/uT7MD6rRALpAWbvb6s775U9c7PTlYH9+WRFlF+He/BqG70LPlcnwj2dhFfK+cYki0agnWJVYnKhcqsQZY7whfmIyW4snMOp2+asfYPpwvJK848BhG4kWoMLXXOShJK0nZ+DMdFW+unTTjkDxwOBw8AvpTJpDoFI2/llF4bmqEfP615pHpssKBxbMClcxfTh58kkCRc8vo2OICmGjhc0gdMY+aNZ0ljpsW9zeUeMYljtg2l8yS5Vf6fiIKvaQq68ZxvR2ucIOBpMOafohBLzHOIO/Nget/WGQU/58rN/v3+YOZcPherqHfRnqtkEqQabp5Ax3hq6L+xqgmKrv01Q== 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=PW2xHWL6QNyk3Ff7FWSfXySy6+lIgvRhxzehARxsxgc=; b=mW4xJw9WCzh4yNHYQSirRrOagZ/heD5mY6lr7nKA4ZxZ8oclx4+QrRBQ/WcYF9BQbbVtC6JEL7keNNmAnrrSif5NpyOErQL/URJq2X5BHbPvfL1hiWS6tZMVt0V6j1Lxk6L+OcWSrw12kmFnRBNJKYuN3GgXm+5mb66xHH7AAUubyw4hg1lHK60tl6yJL05lx4D9MXr3HfCbEXKi2eaDookpf+VaxnhY9ZjJDg71ytGgd8k4AnL5vRrYHN3Wd7L8yKyGoY7SC85J/Xc5QNrholXEbw7pp5ngJJ2GLvjhcCb8vCfRe2wnosWoztBpD4tsHRzvx2B5DqnfBLpgd49YZg== 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=PW2xHWL6QNyk3Ff7FWSfXySy6+lIgvRhxzehARxsxgc=; b=dE+OrzTkc/s7QPrBuNPY1KbONN/4z4pdirgyv3tTOJ9fBdWrhAsmWvFU8OFwGqNibEGK2w8bxUnRKVZe94xPVCv0F6HG+n8cxrmwKBWz7btH0FIttLXYfG2wnHFOp1apEF709SjgYh5fGsi21S70qN4q6bjcKa2mY9f/BLgwHGQ= 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 CO1PR13MB4773.namprd13.prod.outlook.com (2603:10b6:303:fb::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.21; Fri, 15 Sep 2023 09:16:58 +0000 Received: from SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::c05c:6915:1628:70c4]) by SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::c05c:6915:1628:70c4%7]) with mapi id 15.20.6768.029; Fri, 15 Sep 2023 09:16:58 +0000 From: Chaoyong He To: dev@dpdk.org Cc: oss-drivers@corigine.com, Chaoyong He , =?UTF-8?q?Niklas=20S=C3=B6derlund?= Subject: [PATCH v3 20/27] net/nfp: refact the mutex module Date: Fri, 15 Sep 2023 17:15:44 +0800 Message-Id: <20230915091551.1459606-21-chaoyong.he@corigine.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230915091551.1459606-1-chaoyong.he@corigine.com> References: <20230830021457.2064750-1-chaoyong.he@corigine.com> <20230915091551.1459606-1-chaoyong.he@corigine.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: PH7PR10CA0017.namprd10.prod.outlook.com (2603:10b6:510:23d::10) To SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR13MB5545:EE_|CO1PR13MB4773:EE_ X-MS-Office365-Filtering-Correlation-Id: 857c46c6-3173-4b2f-163c-08dbb5cc833d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JQ/ZBYNRuoZn8rDDs7ufWb2LDFdIxAHfnq0Imgsx9M5BnLvzy9WqrsLsJPCbQx/4RR0TQiDkdbm9Bx4ePuojgIikxN6gX8/OS4HnaeYuo5UHzSjjEDcOJEsXf3JcP3wWhst2UdiXqV/ZoBX7OLv6+DvF9P98rSLSlA/Trkvo84ZCGxCoCz/8bJVnnpcINuEVPe9e0y5j9uviBg+VNnD88lEnJNfMvFz0BquA3/NaEZSjSwqYGKA/eJgTKbhClcR4tcut1cqZsBZdJUQcFrWylouExMPN59sh8IH/LS1A4PV96erXsXZViXW1KfUpACwECQhhlQuyIG7nVDCVj2jwGWxs0y0vdLEL4MCEm928bsZxQmqrta1QtshrfpjMcXqSVeuUXWyBfC9rhfOxdSn/vbznVDsxD7lMLBEqnYZyi86Gs6p75Gz9yzu5z68zTksA4t92II3I0QAdXzHRFplRmlrgzPVHRXqkmjmYho4E/OuPNG7iXn97hxXTzVVvpXgb+3bFlD/wmQw3mNUkMGzqkCf2nRW9tb9xWHrENMMUMyrwjmoki7pA+pgLcuMP1AkjzAgiOWuWOWXXl1RfUAUnQC26dkXubqi/3MVLlThUkOlnp84613AG570uKYhwDj0vjzLxm6932YIALLjfKE+0JjjPuRI2oug71faMvloaKpg= 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)(39840400004)(346002)(136003)(376002)(396003)(366004)(451199024)(186009)(1800799009)(6512007)(107886003)(1076003)(2616005)(6666004)(52116002)(6506007)(6486002)(66946007)(26005)(316002)(41300700001)(66476007)(66556008)(6916009)(54906003)(5660300002)(4326008)(8676002)(38350700002)(8936002)(38100700002)(478600001)(66574015)(83380400001)(36756003)(44832011)(30864003)(86362001)(2906002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?RTIxWk5XY2JyNjN4aWNXUDMwanhYSkkwdldtTWc3UnVsU0VXUllIaXFjZGFn?= =?utf-8?B?clZWbzFBMEd2YU11a3lieTZ0ait1ZVBJeUNDa1FVT1dSMFFSMGJHbnltaW5r?= =?utf-8?B?SXUzS1pMUlFKRVZqNy9zbUJ5Y3hKY2tVbGRGL0FrdERXRDd4R3ZGdnZQaDlO?= =?utf-8?B?Zld2eDVUWFFZdE1rcVo5OTZhQjg0VGJUcVVtL09pTlRMaVNqUnhpbUhLVGFz?= =?utf-8?B?WlJ2YUZ2eStXVmRxR0xRczVvekRWU3VMSGlnT2ZpRERWVm93Wk9ITU01N0FR?= =?utf-8?B?RTQ4eTNvZnNERU9tNzVES2R3MFlqc2xNK2Z0U1ZLaUV3bjlBMnBGZ2NzR081?= =?utf-8?B?MTNUNjNpR3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RWM0UnpqZklJdFVsS2t4em96?= =?utf-8?B?M0E9PQ==?= X-OriginatorOrg: corigine.com X-MS-Exchange-CrossTenant-Network-Message-Id: 857c46c6-3173-4b2f-163c-08dbb5cc833d X-MS-Exchange-CrossTenant-AuthSource: SJ0PR13MB5545.namprd13.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Sep 2023 09:16:58.5677 (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: H0R6FPGz6NT3QP947O0s8+LeaqnGrWTfkQtXjx3RkYM+SKksYY9OUVs4tSoT/WpAc7yjyBujmKQzr6dX8mnR+iw+GpUwQcQ0elbxT45JsbQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR13MB4773 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 a header file to holds the API declarations of this module. Also sync the logic from kernel driver and remove the unneeded header file include statements. Signed-off-by: Chaoyong He Reviewed-by: Niklas Söderlund --- drivers/net/nfp/nfpcore/nfp_cpp.h | 16 -- drivers/net/nfp/nfpcore/nfp_mutex.c | 205 ++++++++++++++----------- drivers/net/nfp/nfpcore/nfp_mutex.h | 25 +++ drivers/net/nfp/nfpcore/nfp_resource.c | 1 + 4 files changed, 138 insertions(+), 109 deletions(-) create mode 100644 drivers/net/nfp/nfpcore/nfp_mutex.h diff --git a/drivers/net/nfp/nfpcore/nfp_cpp.h b/drivers/net/nfp/nfpcore/nfp_cpp.h index be7ae1d919..42c4df5fdd 100644 --- a/drivers/net/nfp/nfpcore/nfp_cpp.h +++ b/drivers/net/nfp/nfpcore/nfp_cpp.h @@ -8,8 +8,6 @@ #include -struct nfp_cpp_mutex; - /* NFP CPP handle */ struct nfp_cpp { uint32_t model; @@ -426,20 +424,6 @@ int nfp_cpp_readq(struct nfp_cpp *cpp, uint32_t cpp_id, int nfp_cpp_writeq(struct nfp_cpp *cpp, uint32_t cpp_id, uint64_t address, uint64_t value); -int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target, - uint64_t address, uint32_t key_id); - -struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target, - uint64_t address, uint32_t key_id); - -void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex); - uint32_t nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp); #endif /* __NFP_CPP_H__ */ diff --git a/drivers/net/nfp/nfpcore/nfp_mutex.c b/drivers/net/nfp/nfpcore/nfp_mutex.c index 87a9351ff9..9363543422 100644 --- a/drivers/net/nfp/nfpcore/nfp_mutex.c +++ b/drivers/net/nfp/nfpcore/nfp_mutex.c @@ -3,21 +3,13 @@ * All rights reserved. */ -#include -#include +#include "nfp_mutex.h" + #include -#include "nfp_cpp.h" #include "nfp_logs.h" #include "nfp_target.h" -#define MUTEX_LOCKED(interface) ((((uint32_t)(interface)) << 16) | 0x000f) -#define MUTEX_UNLOCK(interface) (0 | 0x0000) - -#define MUTEX_IS_LOCKED(value) (((value) & 0xffff) == 0x000f) -#define MUTEX_IS_UNLOCKED(value) (((value) & 0xffff) == 0x0000) -#define MUTEX_INTERFACE(value) (((value) >> 16) & 0xffff) - /* * If you need more than 65536 recursive locks, please * rethink your code. @@ -34,21 +26,51 @@ struct nfp_cpp_mutex { struct nfp_cpp_mutex *prev, *next; }; +static inline uint32_t +nfp_mutex_locked(uint16_t interface) +{ + return (uint32_t)interface << 16 | 0x000f; +} + +static inline uint32_t +nfp_mutex_unlocked(uint16_t interface) +{ + return (uint32_t)interface << 16 | 0x0000; +} + +static inline uint16_t +nfp_mutex_owner(uint32_t val) +{ + return (val >> 16) & 0xffff; +} + +static inline bool +nfp_mutex_is_locked(uint32_t val) +{ + return (val & 0xffff) == 0x000f; +} + +static inline bool +nfp_mutex_is_unlocked(uint32_t val) +{ + return (val & 0xffff) == 0; +} + static int -nfp_cpp_mutex_validate(uint32_t model, +nfp_cpp_mutex_validate(uint16_t interface, int *target, uint64_t address) { + /* Not permitted on invalid interfaces */ + if (NFP_CPP_INTERFACE_TYPE_of(interface) == NFP_CPP_INTERFACE_TYPE_INVALID) + return -EINVAL; + /* Address must be 64-bit aligned */ if ((address & 7) != 0) return -EINVAL; - if (NFP_CPP_MODEL_IS_6000(model)) { - if (*target != NFP_CPP_TARGET_MU) - return -EINVAL; - } else { + if (*target != NFP_CPP_TARGET_MU) return -EINVAL; - } return 0; } @@ -84,10 +106,10 @@ nfp_cpp_mutex_init(struct nfp_cpp *cpp, uint32_t key) { int err; - uint32_t model = nfp_cpp_model(cpp); uint32_t muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */ + uint16_t interface = nfp_cpp_interface(cpp); - err = nfp_cpp_mutex_validate(model, &target, address); + err = nfp_cpp_mutex_validate(interface, &target, address); if (err < 0) return err; @@ -95,8 +117,7 @@ nfp_cpp_mutex_init(struct nfp_cpp *cpp, if (err < 0) return err; - err = nfp_cpp_writel(cpp, muw, address + 0, - MUTEX_LOCKED(nfp_cpp_interface(cpp))); + err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_locked(interface)); if (err < 0) return err; @@ -133,26 +154,10 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int err; uint32_t tmp; struct nfp_cpp_mutex *mutex; - uint32_t model = nfp_cpp_model(cpp); uint32_t mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */ + uint16_t interface = nfp_cpp_interface(cpp); - /* Look for cached mutex */ - for (mutex = cpp->mutex_cache; mutex; mutex = mutex->next) { - if (mutex->target == target && mutex->address == address) - break; - } - - if (mutex) { - if (mutex->key == key) { - mutex->usage++; - return mutex; - } - - /* If the key doesn't match... */ - return NULL; - } - - err = nfp_cpp_mutex_validate(model, &target, address); + err = nfp_cpp_mutex_validate(interface, &target, address); if (err < 0) return NULL; @@ -172,16 +177,6 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, mutex->address = address; mutex->key = key; mutex->depth = 0; - mutex->usage = 1; - - /* Add mutex to the cache */ - if (cpp->mutex_cache) { - cpp->mutex_cache->prev = mutex; - mutex->next = cpp->mutex_cache; - cpp->mutex_cache = mutex; - } else { - cpp->mutex_cache = mutex; - } return mutex; } @@ -195,20 +190,6 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex) { - mutex->usage--; - if (mutex->usage > 0) - return; - - /* Remove mutex from the cache */ - if (mutex->next) - mutex->next->prev = mutex->prev; - if (mutex->prev) - mutex->prev->next = mutex->next; - - /* If mutex->cpp == NULL, something broke */ - if (mutex->cpp && mutex == mutex->cpp->mutex_cache) - mutex->cpp->mutex_cache = mutex->next; - rte_free(mutex); } @@ -268,32 +249,28 @@ nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex) return 0; } - err = nfp_cpp_readl(mutex->cpp, mur, mutex->address, &value); - if (err < 0) - goto exit; - err = nfp_cpp_readl(mutex->cpp, mur, mutex->address + 4, &key); if (err < 0) - goto exit; + return err; - if (key != mutex->key) { - err = -EPERM; - goto exit; - } + if (key != mutex->key) + return -EPERM; - if (value != MUTEX_LOCKED(interface)) { - err = -EACCES; - goto exit; - } + err = nfp_cpp_readl(mutex->cpp, mur, mutex->address, &value); + if (err < 0) + return err; + + if (value != nfp_mutex_locked(interface)) + return -EACCES; - err = nfp_cpp_writel(cpp, muw, mutex->address, MUTEX_UNLOCK(interface)); + err = nfp_cpp_writel(cpp, muw, mutex->address, + nfp_mutex_unlocked(interface)); if (err < 0) - goto exit; + return err; mutex->depth = 0; -exit: - return err; + return 0; } /** @@ -332,19 +309,17 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) /* Verify that the lock marker is not damaged */ err = nfp_cpp_readl(cpp, mur, mutex->address + 4, &key); if (err < 0) - goto exit; + return err; - if (key != mutex->key) { - err = -EPERM; - goto exit; - } + if (key != mutex->key) + return -EPERM; /* * Compare against the unlocked state, and if true, * write the interface id into the top 16 bits, and * mark as locked. */ - value = MUTEX_LOCKED(nfp_cpp_interface(cpp)); + value = nfp_mutex_locked(nfp_cpp_interface(cpp)); /* * We use test_set_imm here, as it implies a read @@ -361,10 +336,10 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) */ err = nfp_cpp_readl(cpp, mus, mutex->address, &tmp); if (err < 0) - goto exit; + return err; /* Was it unlocked? */ - if (MUTEX_IS_UNLOCKED(tmp)) { + if (nfp_mutex_is_unlocked(tmp)) { /* * The read value can only be 0x....0000 in the unlocked state. * If there was another contending for this lock, then @@ -376,20 +351,64 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) */ err = nfp_cpp_writel(cpp, muw, mutex->address, value); if (err < 0) - goto exit; + return err; mutex->depth = 1; - goto exit; + return 0; } /* Already locked by us? Success! */ if (tmp == value) { mutex->depth = 1; - goto exit; + return 0; } - err = MUTEX_IS_LOCKED(tmp) ? -EBUSY : -EINVAL; + return nfp_mutex_is_locked(tmp) ? -EBUSY : -EINVAL; +} + +/** + * Release lock if held by local system. + * Extreme care is advised, call only when no local lock users can exist. + * + * @param cpp + * NFP CPP handle + * @param target + * NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU) + * @param address + * Offset into the address space of the NFP CPP target ID + * + * @return + * - (0) if the lock was OK + * - (1) if locked by us + * - (-errno) on invalid mutex + */ +int +nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, + int target, + uint64_t address) +{ + int err; + uint32_t tmp; + uint16_t interface = nfp_cpp_interface(cpp); + const uint32_t mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */ + const uint32_t muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */ + + err = nfp_cpp_mutex_validate(interface, &target, address); + if (err != 0) + return err; + + /* Check lock */ + err = nfp_cpp_readl(cpp, mur, address, &tmp); + if (err < 0) + return err; + + if (nfp_mutex_is_unlocked(tmp) || nfp_mutex_owner(tmp) != interface) + return 0; + + /* Bust the lock */ + err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_unlocked(interface)); + if (err < 0) + return err; -exit: - return err; + return 1; } diff --git a/drivers/net/nfp/nfpcore/nfp_mutex.h b/drivers/net/nfp/nfpcore/nfp_mutex.h new file mode 100644 index 0000000000..a79490b4d6 --- /dev/null +++ b/drivers/net/nfp/nfpcore/nfp_mutex.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Corigine, Inc. + * All rights reserved. + */ + +#ifndef __NFP_MUTEX_H__ +#define __NFP_MUTEX_H__ + +#include "nfp_cpp.h" + +struct nfp_cpp_mutex; + +int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target, + uint64_t address, uint32_t key_id); + +struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target, + uint64_t address, uint32_t key_id); + +void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target, uint64_t address); + +#endif /* __NFP_MUTEX_H__ */ diff --git a/drivers/net/nfp/nfpcore/nfp_resource.c b/drivers/net/nfp/nfpcore/nfp_resource.c index ee987c4438..c4cc7f42f6 100644 --- a/drivers/net/nfp/nfpcore/nfp_resource.c +++ b/drivers/net/nfp/nfpcore/nfp_resource.c @@ -7,6 +7,7 @@ #include "nfp_crc.h" #include "nfp_logs.h" +#include "nfp_mutex.h" #include "nfp_target.h" #define NFP_RESOURCE_TBL_TARGET NFP_CPP_TARGET_MU -- 2.39.1