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 120EAA0C4E; Tue, 2 Nov 2021 09:54:40 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 7E7D74113A; Tue, 2 Nov 2021 09:54:14 +0100 (CET) Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by mails.dpdk.org (Postfix) with ESMTP id C8F874113F for ; Tue, 2 Nov 2021 09:54:12 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=llaK3WUp6OfS7rcfIJH1pO8g6tXKzkr6VVIn2btDRbLCKlCRcBljfJezPn8jGQVtz5FbOp2FpS+oiORcwxKCC/aTqm6+LnQL3opBBnArsF+NJaSqINUG3UE7RLMnGXUCCIStpbjtf/XCFsJmKjHWCze/bTLpXBixmVTp4WH46R8ZyWP94IDefwShx+CuPhfDZWI4jFCCf22YjyyODJvOhwuANErT912/gBYuI0MALbfLaC2oGTKjgokFvuWAtiSvslVxZByqgsNjXH+//EGNFb93AMAm3W39hAgHbnPfHIjiW0Xvu9PlA4TcwixDLGd15GnCncQ6McmttB1nX3xK+w== 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=pYdc13FhKdRluoEQoX3Y1rEJyXWNAUeH/zDE95xJcEg=; b=PzLzL5EzQ1djpiyKWwA3eIshM9Y3jCsBBv6VhB/5vaXLMgl2eoJwyPG82cAc0EnoL4jl5NVKkKWrTK8vVAv5FzU3uvsfJvBdKTklilNqc2DAHDXqkhbH/iB+IuUh6qolq2FMZdxRmkRt70JTdcV1cF/l8LZzV621qZeO7hBdXX/K55xc1hHXbkY7Qz2mMLeIud4aiW8urDFog8+COK0aKtKe6nJSJGhAk79CQtcyr9oa/YclXF3f8CyPleM5WMs9RmvLtXIWlhi7qwVNwETuYu2Wd27g0t+Z4oUepWUJwuJFNuPavAjBdbWGuisAkQTED8tslGdCEv3g1v0ceuGC3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.34) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=quarantine sp=quarantine 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=pYdc13FhKdRluoEQoX3Y1rEJyXWNAUeH/zDE95xJcEg=; b=Clzt0V8JboWVQTFJs4TKfSY9UWqc/Fazv3+89TqdBjzC55jp9xM5GKzVB4aYs3xcT5hJHJHhezE6pdJq1mLhpI/EIwj0DtYyWyz/JIltROLbYNHG2ltDUKxbvfokVWvccNFpe2/vSfdEcurLfnacsiAdg+lzoM6omwDYgWTUHr0Y0TarVsZWxNTu1JXkccuf/CkjA4n+sAetRGKIaRSn7GQSxX0R6Ac1+bmonQmhVyNbQNqDHKDKVf7tue768GD0lhskf3CGRN7hrJcUxP3g4xOaREY4ECE4/aAePYkQWU0PGybJtmvuy4HKGM8VYyRCVTFZXIdlf5VQrxqf/WTIaQ== Received: from DM6PR07CA0080.namprd07.prod.outlook.com (2603:10b6:5:337::13) by DM6PR12MB3436.namprd12.prod.outlook.com (2603:10b6:5:11b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15; Tue, 2 Nov 2021 08:54:10 +0000 Received: from DM6NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:5:337:cafe::bb) by DM6PR07CA0080.outlook.office365.com (2603:10b6:5:337::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4649.15 via Frontend Transport; Tue, 2 Nov 2021 08:54:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34) smtp.mailfrom=nvidia.com; intel.com; dkim=none (message not signed) header.d=none;intel.com; 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 DM6NAM11FT003.mail.protection.outlook.com (10.13.173.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4649.14 via Frontend Transport; Tue, 2 Nov 2021 08:54:10 +0000 Received: from nvidia.com (172.20.187.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Tue, 2 Nov 2021 08:54:08 +0000 From: Gregory Etelson To: , , CC: , Anatoly Burakov Date: Tue, 2 Nov 2021 10:53:42 +0200 Message-ID: <20211102085347.20568-6-getelson@nvidia.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211102085347.20568-1-getelson@nvidia.com> References: <20211101091514.3891-1-getelson@nvidia.com> <20211102085347.20568-1-getelson@nvidia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.20.187.5] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 075fbc69-f9ec-4cab-7864-08d99dde5644 X-MS-TrafficTypeDiagnostic: DM6PR12MB3436: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:326; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Tq0ZxdFJ7e/Jp9/XmWFBYIec9QQgu4IkkPtKGHCWOAcOdq9AN1TX91tqLWXGRvbQ2Zae1ERxSd8xF6lkICtkBpdioJeH3i5pBXuWx0igPf9x2ZatoZtdUMwlDjsYic0nW/14SLcjuT1BsWW874i5Uhf+kfc0GP8AmDn2S4l1JZ8BGDEx8Nfe7rhywI9TnisTEj2jQVjsIl64jCO8lL5dJqQd51YofNkoGt/5arDkU8pe+8gpTiXpwWq7vQ3vR71ZbBTfKvmLqwna4lyPfjB3+436QaRJKiNlwlDH6YaBLRDfd3IlCVoTTV3ESwyXecsa3sijkG99JzPGSi/6+VyCTRWLW8flU50X550h0gq0KcW0oME08UO2Ppl7P8xY0lQ5OAX4JNZbR2g5MB0wofC1tQzQ7DMTEvtVFA6roP2YO7Yt45Vp2dB32kFPS/ckvdNV1M7lsurIBpt1WuH/BaWY85lOAIVuhLNScVxYNsGFvxmgjLU7E2QSxVYg45JgdXhLHJOy5TGTd70Y9aQbXIWBmLSgMocYc6gH+d5teEYka+xnV9x4nScZ0YU0Hh06aGLWPrkH0BW7+MEIkBCHnGx3hNvZTENeqZFE/xnSD1XzEDAy8xHyv7qgRdAN7KuYF6XY8TH6esLtmNYEnmDbnmhPN0MjULZbCLsrPax6Kqcxr7HAtgrC9xIGdPxBx2y9XV5OffeCZQTXj9ODrRWicY7lxLBg930eUSs3lSE/3YXVTxU= 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)(36840700001)(46966006)(86362001)(336012)(7696005)(5660300002)(4326008)(70206006)(2616005)(426003)(47076005)(55016002)(6666004)(1076003)(82310400003)(508600001)(70586007)(54906003)(36860700001)(316002)(36906005)(16526019)(8676002)(186003)(36756003)(6636002)(356005)(2906002)(30864003)(110136005)(26005)(83380400001)(7636003)(6286002)(8936002)(21314003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Nov 2021 08:54:10.2865 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 075fbc69-f9ec-4cab-7864-08d99dde5644 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: DM6NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3436 Subject: [dpdk-dev] [PATCH v2 5/9] 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" From: Viacheslav Ovsiienko 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 underlying 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: Gregory Etelson Signed-off-by: Viacheslav Ovsiienko --- drivers/net/mlx5/linux/mlx5_os.c | 4 + 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(+), 4 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 f31f1e96c6..3f7c34b687 100644 --- a/drivers/net/mlx5/linux/mlx5_os.c +++ b/drivers/net/mlx5/linux/mlx5_os.c @@ -1687,6 +1687,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 (sh->devx && config->dv_flow_en && config->dest_tir) { priv->obj_ops = devx_obj_ops; @@ -1823,6 +1825,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 636a1be890..2f6d8cbb3d 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_rss.c', 'mlx5_rx.c', diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c index cd7bb3f27c..a4a0e258a9 100644 --- a/drivers/net/mlx5/mlx5.c +++ b/drivers/net/mlx5/mlx5.c @@ -378,7 +378,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 @@ -1683,6 +1682,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 912c4a183b..f0c1775f8c 100644 --- a/drivers/net/mlx5/mlx5.h +++ b/drivers/net/mlx5/mlx5.h @@ -50,6 +50,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. */ @@ -1096,6 +1099,12 @@ struct mlx5_lag { uint8_t affinity_mode; /* TIS or hash based affinity */ }; +/* 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. @@ -1425,6 +1434,10 @@ struct mlx5_priv { struct mlx5_devx_obj *q_counters; /* DevX queue counter object. */ uint32_t counter_set_id; /* Queue counter ID to set in DevX objects. */ uint32_t lag_affinity_idx; /* LAG mode queue 0 affinity starting. */ + 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) @@ -1804,4 +1817,15 @@ mlx5_get_supported_sw_parsing_offloads(const struct mlx5_hca_attr *attr); uint32_t mlx5_get_supported_tunneling_offloads(const struct mlx5_hca_attr *attr); +/* 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 2385a0b550..71550de808 100644 --- a/drivers/net/mlx5/mlx5_flow.c +++ b/drivers/net/mlx5/mlx5_flow.c @@ -748,6 +748,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, @@ -767,6 +775,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. */ @@ -9640,6 +9650,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 5509c28f01..43399471ec 100644 --- a/drivers/net/mlx5/mlx5_flow.h +++ b/drivers/net/mlx5/mlx5_flow.h @@ -1232,6 +1232,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; @@ -1266,6 +1279,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 */ @@ -1722,6 +1738,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 8e2714233c..9d60c9154f 100644 --- a/drivers/net/mlx5/mlx5_flow_dv.c +++ b/drivers/net/mlx5/mlx5_flow_dv.c @@ -17965,7 +17965,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.33.1