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 2A5F9A0032; Fri, 1 Oct 2021 21:36:00 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5C0A641259; Fri, 1 Oct 2021 21:35:08 +0200 (CEST) Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam07on2066.outbound.protection.outlook.com [40.107.95.66]) by mails.dpdk.org (Postfix) with ESMTP id E2B5241259 for ; Fri, 1 Oct 2021 21:35:06 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MsDUka/a3ssRo2KvAj3cSJZHxr9aits8jdROR9/cuRJLqP8Wg9BN5zpgq5rDXzG/mu/aXfbSI07EYHyZyOhHkSgAq3R8sCBPG2ZkQ9X3yulL0RkMPvKXnQBX5nlfHipIy8et0BmguB+TcAFk5su8O7jkZkD10xI4WtWvX0zzkAZRNMVy4dYRIKKKt6netajx6pHCAba0VXU8n5uocI8Ui67Vc5SPL+6/pRlak3uKvu9qveRCBkfR2jE6r5OvDEw6SOlQMxgq2WELrxm91OB0+LYbvLs64Nh5VKO7q3haQmhuUgfURK9zOW34HEZKtoqgnIUNRV1iqzY7P5OkNsLw3Q== 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=EXMf2UcVK7hi796CshcNaJb4Rka94qvhNykNjAHhOJ0=; b=ASCEFTymvBEI4s1Uzhw2CrlYiHCdZ9tVeCk5q6SECoRTrRKX+1nImSTN2Y5v8bgObA+WujiwmpYu+wI1Q2Bw2xEE6nzEUwMaKGcX9kNxu52wiJRmX5XAYW6CaozJPq4zdHx0638ipXfHk4E8IVkHUAntC81klkxFG5KR+9+N+OMefet4n6n3oRKk2sfI3Of7v8/OZrclNQoVOkNHjZV99+3/HCB/98riXcWKU55jpoKzvPOmqnacaNHpuhfaEONrL6eXpsFV/oINj72j2p58YS8/yxTt8FBZOgJ6A5P3zmseX8jB6aTq8GW8GtOmY0XBcCuhv1bN4KGaH0EC9q4rLw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.34) smtp.rcpttodomain=monjalon.net smtp.mailfrom=nvidia.com; dmarc=pass (p=quarantine sp=none pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EXMf2UcVK7hi796CshcNaJb4Rka94qvhNykNjAHhOJ0=; b=n36R0wUYziDZw0QwbduALFhT01hZuTHxsa43ontKzi59SnSJWweLTSoPfICzDpaK4SFL+QorbqZa1SxbxO8V90IwjX1haofaxuDvNRYtt7c13mvcaFN+/rpCBabt5S1HigfcsVUcodhWHn329OhrILLl9MTR2AbS8+kbt+qfKGUqSWQNKF36dmFzUxlNqvC75g4s0ivLcbiOu7zaX/FwG1U4tPdnAcptmDL4ORmhOnK7RMiO6n8RsIyhbuUNdZNB4jR96Y/Qmy4CJJr0uWd7rhN551AQZaLZumozsi/cT45d4f3pb6vh3jtvM9cLD4u+ATcRCfHceYgR0tJ4A+aETg== Received: from BN9PR03CA0845.namprd03.prod.outlook.com (2603:10b6:408:13d::10) by BY5PR12MB4163.namprd12.prod.outlook.com (2603:10b6:a03:202::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.14; Fri, 1 Oct 2021 19:35:04 +0000 Received: from BN8NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:408:13d:cafe::72) by BN9PR03CA0845.outlook.office365.com (2603:10b6:408:13d::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.19 via Frontend Transport; Fri, 1 Oct 2021 19:35:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34) smtp.mailfrom=nvidia.com; monjalon.net; dkim=none (message not signed) header.d=none;monjalon.net; dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.112.34 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.112.34; helo=mail.nvidia.com; Received: from mail.nvidia.com (216.228.112.34) by BN8NAM11FT034.mail.protection.outlook.com (10.13.176.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4566.14 via Frontend Transport; Fri, 1 Oct 2021 19:35:04 +0000 Received: from nvidia.com (172.20.187.6) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Fri, 1 Oct 2021 19:35:01 +0000 From: Viacheslav Ovsiienko To: CC: , , , , , Date: Fri, 1 Oct 2021 22:34:11 +0300 Message-ID: <20211001193415.23288-11-viacheslavo@nvidia.com> X-Mailer: git-send-email 2.18.1 In-Reply-To: <20211001193415.23288-1-viacheslavo@nvidia.com> References: <20210922180418.20663-1-viacheslavo@nvidia.com> <20211001193415.23288-1-viacheslavo@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.20.187.6] X-ClientProxiedBy: HQMAIL107.nvidia.com (172.20.187.13) To HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fb20dfb5-1715-48a2-847c-08d985129150 X-MS-TrafficTypeDiagnostic: BY5PR12MB4163: X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:302; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KmSc51WziPjrIrwSv/+4ewWcJ1vULdCy3ayIrKIuMfiCHMpuRt7y3pVMUaYvajg/2CxzuNOA4bKmg7ZWSMRXvcQ9XWYxnpZM+eilRk/830v8HxbiQzZjfIO+uoQbs77ewFB5riEBLlrySMAkFoQmJbBUJsTYYk7wgnt84RdTgIxRKmGaac54ZC2jYXAV/raDwy8qXC5IdRuDTRPZn45/ls+sQLfbKhVfBY1M1f+ol35847Blygsn042OO9qh6hr7CfDCpbxidlT0JpozpyNCrd1odIouc49jAC0sscviLsIDcnFZDyssdRZWXmoICxtknh8GxDr1+BTT07P0qdtzUfHbX1gYB/AxFJ3pkGTPX7fBYS2nqhkPNHF++emiuGlf3NPYf8zV8VddAqMYSwuO/p0HC7siL8XyD4dzCAuLPwwe3PsBSrhbWrGxFenJ9JeydFRvshfLLFZyH6n0w904t+745aQxHt6IpAuRcUGlV1ZFV82V0zSQfXwGyWUux3Q5HDOX4tcNdMFI94TfpxJbf7JsVCNLPFJNUiMgno8oMaeyIF7pNt0A1JLM1+iDMgvVPtPxrrVcB7k9Ehb5LaHhR+ljuLMHieSllOb2a4UHmtj4Iqiq9CQt4/QH4fGOJBCl2q+qbWoWfPhEaZIGNZSb22C4QQVSGHlPik9Qa+QEmSDDZx75dp4QKbQeM9FgOjAZu89VU3EP5uKZKDNkLqeEovOnojYl9XcwPu0AnWo/SBY= X-Forefront-Antispam-Report: CIP:216.228.112.34; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid03.nvidia.com; CAT:NONE; SFS:(4636009)(46966006)(36840700001)(83380400001)(508600001)(7696005)(36756003)(336012)(426003)(82310400003)(6286002)(55016002)(47076005)(70586007)(70206006)(1076003)(7636003)(6666004)(6916009)(316002)(54906003)(36860700001)(356005)(86362001)(186003)(5660300002)(16526019)(8676002)(26005)(2616005)(30864003)(8936002)(2906002)(4326008)(21314003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2021 19:35:04.0906 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fb20dfb5-1715-48a2-847c-08d985129150 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.112.34]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4163 Subject: [dpdk-dev] [PATCH v2 10/14] net/mlx5: add flex item API 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 Sender: "dev" This patch is a preparation step of implementing flex item feature in driver and it provides: - external entry point routines for flex item creation/deletion - flex item objects management over the ports. The flex item object keeps information about the item created over the port - reference counter to track whether item is in use by some active flows and the pointer to underlaying shared DevX object, providing all the data needed to translate the flow flex pattern into matcher fields according hardware configuration. There is not too many flex items supposed to be created on the port, the design is optimized rather for flow insertion rate than memory savings. Signed-off-by: Viacheslav Ovsiienko --- drivers/net/mlx5/linux/mlx5_os.c | 5 +- drivers/net/mlx5/meson.build | 1 + drivers/net/mlx5/mlx5.c | 2 +- drivers/net/mlx5/mlx5.h | 24 ++++ drivers/net/mlx5/mlx5_flow.c | 49 ++++++++ drivers/net/mlx5/mlx5_flow.h | 18 ++- drivers/net/mlx5/mlx5_flow_dv.c | 3 +- drivers/net/mlx5/mlx5_flow_flex.c | 189 ++++++++++++++++++++++++++++++ 8 files changed, 286 insertions(+), 5 deletions(-) create mode 100644 drivers/net/mlx5/mlx5_flow_flex.c diff --git a/drivers/net/mlx5/linux/mlx5_os.c b/drivers/net/mlx5/linux/mlx5_os.c index 3746057673..cbbc152782 100644 --- a/drivers/net/mlx5/linux/mlx5_os.c +++ b/drivers/net/mlx5/linux/mlx5_os.c @@ -928,7 +928,6 @@ mlx5_representor_match(struct mlx5_dev_spawn_data *spawn, return false; } - /** * Spawn an Ethernet device from Verbs information. * @@ -1787,6 +1786,8 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev, err = mlx5_alloc_shared_dr(priv); if (err) goto error; + if (mlx5_flex_item_port_init(eth_dev) < 0) + goto error; } if (config->devx && config->dv_flow_en && config->dest_tir) { priv->obj_ops = devx_obj_ops; @@ -1922,6 +1923,8 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev, claim_zero(rte_eth_switch_domain_free(priv->domain_id)); if (priv->hrxqs) mlx5_list_destroy(priv->hrxqs); + if (eth_dev && priv->flex_item_map) + mlx5_flex_item_port_cleanup(eth_dev); mlx5_free(priv); if (eth_dev != NULL) eth_dev->data->dev_private = NULL; diff --git a/drivers/net/mlx5/meson.build b/drivers/net/mlx5/meson.build index dac7f1fabf..f9b21c35d9 100644 --- a/drivers/net/mlx5/meson.build +++ b/drivers/net/mlx5/meson.build @@ -17,6 +17,7 @@ sources = files( 'mlx5_flow_meter.c', 'mlx5_flow_dv.c', 'mlx5_flow_aso.c', + 'mlx5_flow_flex.c', 'mlx5_mac.c', 'mlx5_mr.c', 'mlx5_rss.c', diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c index aa49542b9d..d902e00ea3 100644 --- a/drivers/net/mlx5/mlx5.c +++ b/drivers/net/mlx5/mlx5.c @@ -376,7 +376,6 @@ static const struct mlx5_indexed_pool_config mlx5_ipool_cfg[] = { }, }; - #define MLX5_FLOW_MIN_ID_POOL_SIZE 512 #define MLX5_ID_GENERATION_ARRAY_FACTOR 16 @@ -1575,6 +1574,7 @@ mlx5_dev_close(struct rte_eth_dev *dev) mlx5_mp_os_req_stop_rxtx(dev); /* Free the eCPRI flex parser resource. */ mlx5_flex_parser_ecpri_release(dev); + mlx5_flex_item_port_cleanup(dev); if (priv->rxqs != NULL) { /* XXX race condition if mlx5_rx_burst() is still running. */ rte_delay_us_sleep(1000); diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h index 5000d2d4c5..89b4d66374 100644 --- a/drivers/net/mlx5/mlx5.h +++ b/drivers/net/mlx5/mlx5.h @@ -49,6 +49,9 @@ #define MLX5_MAX_MODIFY_NUM 32 #define MLX5_ROOT_TBL_MODIFY_NUM 16 +/* Maximal number of flex items created on the port.*/ +#define MLX5_PORT_FLEX_ITEM_NUM 4 + enum mlx5_ipool_index { #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H) MLX5_IPOOL_DECAP_ENCAP = 0, /* Pool for encap/decap resource. */ @@ -1112,6 +1115,12 @@ struct mlx5_aso_ct_pools_mng { struct mlx5_aso_sq aso_sq; /* ASO queue objects. */ }; +/* Port flex item context. */ +struct mlx5_flex_item { + struct mlx5_flex_parser_devx *devx_fp; /* DevX flex parser object. */ + uint32_t refcnt; /**< Atomically accessed refcnt by flows. */ +}; + /* * Shared Infiniband device context for Master/Representors * which belong to same IB device with multiple IB ports. @@ -1448,6 +1457,10 @@ struct mlx5_priv { uint32_t rss_shared_actions; /* RSS shared actions. */ struct mlx5_devx_obj *q_counters; /* DevX queue counter object. */ uint32_t counter_set_id; /* Queue counter ID to set in DevX objects. */ + rte_spinlock_t flex_item_sl; /* Flex item list spinlock. */ + struct mlx5_flex_item flex_item[MLX5_PORT_FLEX_ITEM_NUM]; + /* Flex items have been created on the port. */ + uint32_t flex_item_map; /* Map of allocated flex item elements. */ }; #define PORT_ID(priv) ((priv)->dev_data->port_id) @@ -1823,4 +1836,15 @@ int mlx5_aso_ct_query_by_wqe(struct mlx5_dev_ctx_shared *sh, int mlx5_aso_ct_available(struct mlx5_dev_ctx_shared *sh, struct mlx5_aso_ct_action *ct); +/* mlx5_flow_flex.c */ + +struct rte_flow_item_flex_handle * +flow_dv_item_create(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error); +int flow_dv_item_release(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *flex_handle, + struct rte_flow_error *error); +int mlx5_flex_item_port_init(struct rte_eth_dev *dev); +void mlx5_flex_item_port_cleanup(struct rte_eth_dev *dev); #endif /* RTE_PMD_MLX5_H_ */ diff --git a/drivers/net/mlx5/mlx5_flow.c b/drivers/net/mlx5/mlx5_flow.c index c914a7120c..5224daed6c 100644 --- a/drivers/net/mlx5/mlx5_flow.c +++ b/drivers/net/mlx5/mlx5_flow.c @@ -718,6 +718,14 @@ mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev, struct rte_mbuf *m, struct rte_flow_restore_info *info, struct rte_flow_error *err); +static struct rte_flow_item_flex_handle * +mlx5_flow_flex_item_create(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error); +static int +mlx5_flow_flex_item_release(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *handle, + struct rte_flow_error *error); static const struct rte_flow_ops mlx5_flow_ops = { .validate = mlx5_flow_validate, @@ -737,6 +745,8 @@ static const struct rte_flow_ops mlx5_flow_ops = { .tunnel_action_decap_release = mlx5_flow_tunnel_action_release, .tunnel_item_release = mlx5_flow_tunnel_item_release, .get_restore_info = mlx5_flow_tunnel_get_restore_info, + .flex_item_create = mlx5_flow_flex_item_create, + .flex_item_release = mlx5_flow_flex_item_release, }; /* Tunnel information. */ @@ -9398,6 +9408,45 @@ mlx5_release_tunnel_hub(__rte_unused struct mlx5_dev_ctx_shared *sh, } #endif /* HAVE_IBV_FLOW_DV_SUPPORT */ +/* Flex flow item API */ +static struct rte_flow_item_flex_handle * +mlx5_flow_flex_item_create(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error) +{ + static const char err_msg[] = "flex item creation unsupported"; + struct rte_flow_attr attr = { .transfer = 0 }; + const struct mlx5_flow_driver_ops *fops = + flow_get_drv_ops(flow_get_drv_type(dev, &attr)); + + if (!fops->item_create) { + DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg); + rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION, + NULL, err_msg); + return NULL; + } + return fops->item_create(dev, conf, error); +} + +static int +mlx5_flow_flex_item_release(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *handle, + struct rte_flow_error *error) +{ + static const char err_msg[] = "flex item release unsupported"; + struct rte_flow_attr attr = { .transfer = 0 }; + const struct mlx5_flow_driver_ops *fops = + flow_get_drv_ops(flow_get_drv_type(dev, &attr)); + + if (!fops->item_release) { + DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg); + rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION, + NULL, err_msg); + return -rte_errno; + } + return fops->item_release(dev, handle, error); +} + static void mlx5_dbg__print_pattern(const struct rte_flow_item *item) { diff --git a/drivers/net/mlx5/mlx5_flow.h b/drivers/net/mlx5/mlx5_flow.h index 5c68d4f7d7..a8f8c49dd2 100644 --- a/drivers/net/mlx5/mlx5_flow.h +++ b/drivers/net/mlx5/mlx5_flow.h @@ -1226,6 +1226,19 @@ typedef int (*mlx5_flow_create_def_policy_t) (struct rte_eth_dev *dev); typedef void (*mlx5_flow_destroy_def_policy_t) (struct rte_eth_dev *dev); +typedef struct rte_flow_item_flex_handle *(*mlx5_flow_item_create_t) + (struct rte_eth_dev *dev, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error); +typedef int (*mlx5_flow_item_release_t) + (struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *handle, + struct rte_flow_error *error); +typedef int (*mlx5_flow_item_update_t) + (struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *handle, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error); struct mlx5_flow_driver_ops { mlx5_flow_validate_t validate; @@ -1260,6 +1273,9 @@ struct mlx5_flow_driver_ops { mlx5_flow_action_update_t action_update; mlx5_flow_action_query_t action_query; mlx5_flow_sync_domain_t sync_domain; + mlx5_flow_item_create_t item_create; + mlx5_flow_item_release_t item_release; + mlx5_flow_item_update_t item_update; }; /* mlx5_flow.c */ @@ -1709,6 +1725,4 @@ const struct mlx5_flow_tunnel * mlx5_get_tof(const struct rte_flow_item *items, const struct rte_flow_action *actions, enum mlx5_tof_rule_type *rule_type); - - #endif /* RTE_PMD_MLX5_FLOW_H_ */ diff --git a/drivers/net/mlx5/mlx5_flow_dv.c b/drivers/net/mlx5/mlx5_flow_dv.c index fc676d3ee4..a3c35a5edf 100644 --- a/drivers/net/mlx5/mlx5_flow_dv.c +++ b/drivers/net/mlx5/mlx5_flow_dv.c @@ -18011,7 +18011,8 @@ const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = { .action_update = flow_dv_action_update, .action_query = flow_dv_action_query, .sync_domain = flow_dv_sync_domain, + .item_create = flow_dv_item_create, + .item_release = flow_dv_item_release, }; - #endif /* HAVE_IBV_FLOW_DV_SUPPORT */ diff --git a/drivers/net/mlx5/mlx5_flow_flex.c b/drivers/net/mlx5/mlx5_flow_flex.c new file mode 100644 index 0000000000..b7bc4af6fb --- /dev/null +++ b/drivers/net/mlx5/mlx5_flow_flex.c @@ -0,0 +1,189 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 NVIDIA Corporation & Affiliates + */ +#include +#include +#include +#include "mlx5.h" +#include "mlx5_flow.h" + +static_assert(sizeof(uint32_t) * CHAR_BIT >= MLX5_PORT_FLEX_ITEM_NUM, + "Flex item maximal number exceeds uint32_t bit width"); + +/** + * Routine called once on port initialization to init flex item + * related infrastructure initialization + * + * @param dev + * Ethernet device to perform flex item initialization + * + * @return + * 0 on success, a negative errno value otherwise and rte_errno is set. + */ +int +mlx5_flex_item_port_init(struct rte_eth_dev *dev) +{ + struct mlx5_priv *priv = dev->data->dev_private; + + rte_spinlock_init(&priv->flex_item_sl); + MLX5_ASSERT(!priv->flex_item_map); + return 0; +} + +/** + * Routine called once on port close to perform flex item + * related infrastructure cleanup. + * + * @param dev + * Ethernet device to perform cleanup + */ +void +mlx5_flex_item_port_cleanup(struct rte_eth_dev *dev) +{ + struct mlx5_priv *priv = dev->data->dev_private; + uint32_t i; + + for (i = 0; i < MLX5_PORT_FLEX_ITEM_NUM && priv->flex_item_map ; i++) { + if (priv->flex_item_map & (1 << i)) { + /* DevX object dereferencing should be provided here. */ + priv->flex_item_map &= ~(1 << i); + } + } +} + +static int +mlx5_flex_index(struct mlx5_priv *priv, struct mlx5_flex_item *item) +{ + uintptr_t start = (uintptr_t)&priv->flex_item[0]; + uintptr_t entry = (uintptr_t)item; + uintptr_t idx = (entry - start) / sizeof(struct mlx5_flex_item); + + if (entry < start || + idx >= MLX5_PORT_FLEX_ITEM_NUM || + (entry - start) % sizeof(struct mlx5_flex_item) || + !(priv->flex_item_map & (1u << idx))) + return -1; + return (int)idx; +} + +static struct mlx5_flex_item * +mlx5_flex_alloc(struct mlx5_priv *priv) +{ + struct mlx5_flex_item *item = NULL; + + rte_spinlock_lock(&priv->flex_item_sl); + if (~priv->flex_item_map) { + uint32_t idx = rte_bsf32(~priv->flex_item_map); + + if (idx < MLX5_PORT_FLEX_ITEM_NUM) { + item = &priv->flex_item[idx]; + MLX5_ASSERT(!item->refcnt); + MLX5_ASSERT(!item->devx_fp); + item->devx_fp = NULL; + __atomic_store_n(&item->refcnt, 0, __ATOMIC_RELEASE); + priv->flex_item_map |= 1u << idx; + } + } + rte_spinlock_unlock(&priv->flex_item_sl); + return item; +} + +static void +mlx5_flex_free(struct mlx5_priv *priv, struct mlx5_flex_item *item) +{ + int idx = mlx5_flex_index(priv, item); + + MLX5_ASSERT(idx >= 0 && + idx < MLX5_PORT_FLEX_ITEM_NUM && + (priv->flex_item_map & (1u << idx))); + if (idx >= 0) { + rte_spinlock_lock(&priv->flex_item_sl); + MLX5_ASSERT(!item->refcnt); + MLX5_ASSERT(!item->devx_fp); + item->devx_fp = NULL; + __atomic_store_n(&item->refcnt, 0, __ATOMIC_RELEASE); + priv->flex_item_map &= ~(1u << idx); + rte_spinlock_unlock(&priv->flex_item_sl); + } +} + +/** + * Create the flex item with specified configuration over the Ethernet device. + * + * @param dev + * Ethernet device to create flex item on. + * @param[in] conf + * Flex item configuration. + * @param[out] error + * Perform verbose error reporting if not NULL. PMDs initialize this + * structure in case of error only. + * + * @return + * Non-NULL opaque pointer on success, NULL otherwise and rte_errno is set. + */ +struct rte_flow_item_flex_handle * +flow_dv_item_create(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_conf *conf, + struct rte_flow_error *error) +{ + struct mlx5_priv *priv = dev->data->dev_private; + struct mlx5_flex_item *flex; + + MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); + flex = mlx5_flex_alloc(priv); + if (!flex) { + rte_flow_error_set(error, ENOMEM, + RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, + "too many flex items created on the port"); + return NULL; + } + RTE_SET_USED(conf); + /* Mark initialized flex item valid. */ + __atomic_add_fetch(&flex->refcnt, 1, __ATOMIC_RELEASE); + return (struct rte_flow_item_flex_handle *)flex; +} + +/** + * Release the flex item on the specified Ethernet device. + * + * @param dev + * Ethernet device to destroy flex item on. + * @param[in] handle + * Handle of the item existing on the specified device. + * @param[out] error + * Perform verbose error reporting if not NULL. PMDs initialize this + * structure in case of error only. + * + * @return + * 0 on success, a negative errno value otherwise and rte_errno is set. + */ +int +flow_dv_item_release(struct rte_eth_dev *dev, + const struct rte_flow_item_flex_handle *handle, + struct rte_flow_error *error) +{ + struct mlx5_priv *priv = dev->data->dev_private; + struct mlx5_flex_item *flex = + (struct mlx5_flex_item *)(uintptr_t)handle; + uint32_t old_refcnt = 1; + + MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); + rte_spinlock_lock(&priv->flex_item_sl); + if (mlx5_flex_index(priv, flex) < 0) { + rte_spinlock_unlock(&priv->flex_item_sl); + return rte_flow_error_set(error, EINVAL, + RTE_FLOW_ERROR_TYPE_ITEM, NULL, + "invalid flex item handle value"); + } + if (!__atomic_compare_exchange_n(&flex->refcnt, &old_refcnt, 0, 0, + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { + rte_spinlock_unlock(&priv->flex_item_sl); + return rte_flow_error_set(error, EBUSY, + RTE_FLOW_ERROR_TYPE_ITEM, NULL, + "flex item has flow references"); + } + /* Flex item is marked as invalid, we can leave locked section. */ + rte_spinlock_unlock(&priv->flex_item_sl); + mlx5_flex_free(priv, flex); + return 0; +} -- 2.18.1