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 59F0F430EF; Thu, 24 Aug 2023 13:13:38 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2C12D432D6; Thu, 24 Aug 2023 13:11:26 +0200 (CEST) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2109.outbound.protection.outlook.com [40.107.93.109]) by mails.dpdk.org (Postfix) with ESMTP id F0EEA4323A for ; Thu, 24 Aug 2023 13:11:23 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JlXulVQ4h72w6uIE+3Nxkw1kh6KKyfr+dJ2U2FqBGDJn3SwFyV1/rUxl9oWRWuE4chf3FWPKAEw55U9Zp9Mpjz49ZU3kgW/BdpENjX5Jh//SM6r46u5HM8g0mspj36uauRY7pALkWxOS9KJl/bjhh/Imqoo24l0duabgSRPv3gSsKLla6lVGRLXqTApgPpgRRIrfx7w7h2NwHE7zAEUUEfvNqFNSvXBEVW52e/xiZVs9aBcfcwDGTP/uW70YwJ6g8FOknqPx19+HsYP+BbM9yG9mpj4sja8iaak/jpp4iWBpPYxIjZXVHsfSEBDm4u5+rALIAgX1Hp+fY3F5k4N0PQ== 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=e+s6es9PD5Lq94fAMbmhKD8n/H2sMfRdbqGxcm6Zf94z/vSdLZk9nlpCWZGghTUC36nvdVVF2DVyqhg2HFe/kTdYMURKNRX/7knBPS3Yba1CcgM7jv9qDrckD6L91sSLvkidK/b2LvBUkEHCtzURbVC5ROWspm8NiWiQis7ehhf6p9kTb+oCijbH1LKtsqE9Z294J93DFirV6XGi8wrHrgLp+YrHEz81h6tKluDhYSL+7Qu6Ei0so1K6a3SsDZCucO6JR/ggDv3rxifLMkZD+7ttbOpvqrp7A17A4Ml7KmyBNadhRoFaQqCJbk/EY9Eki6Quxq8p2IaQN/u2IGL0fQ== 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=h7BF3m9CrPQdR7u+eZNmKj20zl6kHld6jVO22iLq4VfsbNWQvZ9XvuTEkFswDKf39u7oz/W1oqvlHS8BLog8NB8XkoSxQ0D9p6JyXVjDwNV6vxDO2A5hl0ymly2QTG1FKxXux0QbCmq3WLfgpJHWfDvYsTcEY7a0DCwR/Zq6YNk= 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 PH7PR13MB6116.namprd13.prod.outlook.com (2603:10b6:510:2b7::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.20; Thu, 24 Aug 2023 11:11:22 +0000 Received: from SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::51fe:5846:af8b:bace]) by SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::51fe:5846:af8b:bace%3]) with mapi id 15.20.6699.020; Thu, 24 Aug 2023 11:11:22 +0000 From: Chaoyong He To: dev@dpdk.org Cc: oss-drivers@corigine.com, niklas.soderlund@corigine.com, Chaoyong He Subject: [PATCH 20/27] net/nfp: refact the mutex module Date: Thu, 24 Aug 2023 19:09:49 +0800 Message-Id: <20230824110956.1943559-21-chaoyong.he@corigine.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230824110956.1943559-1-chaoyong.he@corigine.com> References: <20230824110956.1943559-1-chaoyong.he@corigine.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: SJ0PR05CA0005.namprd05.prod.outlook.com (2603:10b6:a03:33b::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_|PH7PR13MB6116:EE_ X-MS-Office365-Filtering-Correlation-Id: f3bf0499-baae-4d6a-a1e5-08dba492d939 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X54iKYf7O70J+S2o7g06wk/ADJcLlFJXwRryask2M50KL8w+WLjMblFSWsDsozydgJd1CUJJib0956MTyQLM9iFYtAkyAbbRe0DwsQK55su6R5NqjecNhwOfCnprwJuVUpirMnmv8yZwQJjKFpNZL/UMDeWcpNLsnEuFiHwYfcxm1UHiFTEAMYSFse7HJqrxXy3xR8KX8QjWxheYg9/M7vPj6qkyOS7iC8T7To1hC4TGJvMxiAHmIg8P5SK7hfEAIKsACtxf9pFy+RoUepVWYcV3xQ1HYZxyMcMumqBGQsXvQEDC2gqAJUE07e3n1KenHOClk7W/LwewK/1ZB20kD45VLxc0g7Z7n8+8IbWu6INdk30qt79Kjb9vF/1/6BJ0IMLlMk3LTT1T7JrYRtWKvunv9xxpp76a/uw/c7jJVFa/jMh6SaWiEA89C6UUHXB2qBpBUyUmrfNG/NIOpFyCOABXOKRzFhgYZDScVPr070uSEdttpndRB9jUdH9C6EiVIdyDmSt806J0o1Z4KIboQWsTU2/fIwitsrQ4JXSRLI3EQKjwrU4jfqVWxjTPt3hzsjfWhxO3onDTYnSR5eS9ZZ45vw0x0x8hrd3F62/WAwY5slYKhMo5ux7QPLmPbXXBNaiMAv53/8rlUfgh/O/ERyvCiFlACf3jVn6gcv6g9RPBLBEsHJMdprlhSHl2Ur63 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)(39830400003)(346002)(396003)(136003)(376002)(1800799009)(186009)(451199024)(6506007)(26005)(1076003)(2616005)(107886003)(52116002)(12101799020)(6512007)(66574015)(6486002)(83380400001)(38100700002)(8676002)(8936002)(36756003)(316002)(4326008)(41300700001)(30864003)(2906002)(86362001)(5660300002)(44832011)(38350700002)(6666004)(6916009)(478600001)(66476007)(66946007)(66556008); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?cTFoNVpQK21tWUw4cndSVFdKYXRRZnpzUDhuaE9HelBOZ2h5RDk2OVNmVUZs?= =?utf-8?B?TVRDeS90ai9ISUhjYWlSNjRBWHZTVWxKeXZTNldZdytrcjRjbzkyaGcydmZ3?= =?utf-8?B?YjJadG9ycmRUOE9KOFJhRWZmdUppakpRamVVcVZVbE5XQ2x3ZkZhTG5CeTBw?= =?utf-8?B?aHR6eXBrejduVUJJc1hQM1QrWUVTcGRHWll1L0JCWllkdjRLYXhBZk1JNHI2?= =?utf-8?B?VFd2ZjM0N05rRGZvTHBzdUlqUWlpQXlUc2pYaDdLZ2pKZEJLSCtrS2VxWEw3?= =?utf-8?B?VHNldmp4bTUrWkRuYk9GTEJxSHJpVkJMRnhOODNsUURRNk9UWEhtSEZzNUtV?= =?utf-8?B?YjJHV3I3NGpkVWIxbXVUSXM0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eXh4?= =?utf-8?B?aXc9PQ==?= X-OriginatorOrg: corigine.com X-MS-Exchange-CrossTenant-Network-Message-Id: f3bf0499-baae-4d6a-a1e5-08dba492d939 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR13MB5545.namprd13.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2023 11:11:22.3405 (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: 3mpHxIYaTDntL6dIkXMOfAXtwNQXDVohmsgL6Fw3EowOSVXoRGJeMpdx5efzenvrvOQjodEgit8G7BHBozY5Ooajk+OFHd5GP9d6BfPnrhI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR13MB6116 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