DPDK patches and discussions
 help / color / mirror / Atom feed
From: <wanry@3snic.com>
To: <dev@dpdk.org>
Cc: <ferruh.yigit@amd.com>, Renyong Wan <wanry@3snic.com>,
	Steven Song <steven.song@3snic.com>
Subject: [PATCH v4 08/32] net/sssnic/base: add work queue
Date: Fri, 1 Sep 2023 17:34:50 +0800	[thread overview]
Message-ID: <20230901093514.224824-9-wanry@3snic.com> (raw)
In-Reply-To: <20230901093514.224824-1-wanry@3snic.com>

From: Renyong Wan <wanry@3snic.com>

Work queue is used to maintain hardware queue information by
driver, it is usually used in control queue, rx queue
and tx queue.

Signed-off-by: Steven Song <steven.song@3snic.com>
Signed-off-by: Renyong Wan <wanry@3snic.com>
---
v2:
* Removed error.h from including files.
---
 drivers/net/sssnic/base/meson.build    |   1 +
 drivers/net/sssnic/base/sssnic_workq.c | 141 +++++++++++++++++++++++++
 drivers/net/sssnic/base/sssnic_workq.h | 108 +++++++++++++++++++
 3 files changed, 250 insertions(+)
 create mode 100644 drivers/net/sssnic/base/sssnic_workq.c
 create mode 100644 drivers/net/sssnic/base/sssnic_workq.h

diff --git a/drivers/net/sssnic/base/meson.build b/drivers/net/sssnic/base/meson.build
index 4abd1a0daf..7c23a82ff3 100644
--- a/drivers/net/sssnic/base/meson.build
+++ b/drivers/net/sssnic/base/meson.build
@@ -6,6 +6,7 @@ sources = [
         'sssnic_eventq.c',
         'sssnic_msg.c',
         'sssnic_mbox.c',
+        'sssnic_workq.c',
 ]
 
 c_args = cflags
diff --git a/drivers/net/sssnic/base/sssnic_workq.c b/drivers/net/sssnic/base/sssnic_workq.c
new file mode 100644
index 0000000000..25b7585246
--- /dev/null
+++ b/drivers/net/sssnic/base/sssnic_workq.c
@@ -0,0 +1,141 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2018-2022 Shenzhen 3SNIC Information Technology Co., Ltd.
+ */
+
+#include <rte_common.h>
+#include <rte_malloc.h>
+#include <rte_memcpy.h>
+#include <rte_memzone.h>
+
+#include "../sssnic_log.h"
+#include "sssnic_workq.h"
+
+/* Consume num_entries and increase CI
+ * Return the first entry address of previous CI position
+ */
+void *
+sssnic_workq_consume(struct sssnic_workq *workq, uint16_t num_entries,
+	uint16_t *ci)
+{
+	void *e;
+	uint16_t current_ci;
+
+	if (workq->idle_entries + num_entries > workq->num_entries)
+		return NULL;
+
+	current_ci = sssnic_workq_ci_get(workq);
+	e = (void *)sssnic_workq_entry_get(workq, current_ci);
+	workq->idle_entries += num_entries;
+	workq->ci += num_entries;
+	if (ci != NULL)
+		*ci = current_ci;
+
+	return e;
+}
+
+/* Produce num_entries and increase pi.
+ * Return the first entry address of previous PI position
+ */
+void *
+sssnic_workq_produce(struct sssnic_workq *workq, uint16_t num_entries,
+	uint16_t *pi)
+{
+	void *e;
+	uint16_t current_pi;
+
+	if (workq->idle_entries < num_entries)
+		return NULL;
+
+	current_pi = sssnic_workq_pi_get(workq);
+	e = (void *)sssnic_workq_entry_get(workq, current_pi);
+	workq->idle_entries -= num_entries;
+	workq->pi += num_entries;
+	if (pi != NULL)
+		*pi = current_pi;
+
+	return e;
+}
+
+static int
+sssnic_workq_init(struct sssnic_workq *workq, const char *name, int socket_id,
+	uint32_t entry_size, uint32_t depth)
+{
+	char zname[RTE_MEMZONE_NAMESIZE];
+
+	if (!rte_is_power_of_2(entry_size)) {
+		PMD_DRV_LOG(ERR,
+			"The entry size(%u) of workq(%s) is not power of 2",
+			entry_size, name);
+		return -EINVAL;
+	}
+
+	if (!rte_is_power_of_2(depth)) {
+		PMD_DRV_LOG(ERR, "The depth(%u) of workq(%s) is not power of 2",
+			depth, name);
+		return -EINVAL;
+	}
+
+	workq->buf_size = entry_size * depth;
+	workq->entry_size = entry_size;
+	workq->entry_shift = rte_log2_u32(entry_size);
+	workq->num_entries = depth;
+	workq->idle_entries = depth;
+	workq->index_mask = depth - 1;
+
+	snprintf(zname, sizeof(zname), "%s_mz", name);
+	workq->buf_mz = rte_memzone_reserve_aligned(zname, workq->buf_size,
+		socket_id, RTE_MEMZONE_IOVA_CONTIG, RTE_PGSIZE_256K);
+	if (workq->buf_mz == NULL) {
+		PMD_DRV_LOG(ERR, "Failed to alloc DMA memory for %s", name);
+		return -ENOMEM;
+	}
+	workq->buf_addr = workq->buf_mz->addr;
+	workq->buf_phyaddr = workq->buf_mz->iova;
+
+	return 0;
+}
+
+static void
+sssnic_workq_cleanup(struct sssnic_workq *workq)
+{
+	if (workq != NULL && workq->buf_mz != NULL)
+		rte_memzone_free(workq->buf_mz);
+}
+
+/* Cleanup a work queue and free it*/
+void
+sssnic_workq_destroy(struct sssnic_workq *workq)
+{
+	if (workq != NULL) {
+		sssnic_workq_cleanup(workq);
+		rte_free(workq);
+	}
+}
+
+/*Create a work queue and initialize*/
+struct sssnic_workq *
+sssnic_workq_new(const char *name, int socket_id, uint32_t entry_size,
+	uint32_t depth)
+{
+	int ret;
+	struct sssnic_workq *workq;
+
+	if (name == NULL) {
+		PMD_DRV_LOG(ERR, "Bad parameter, workq name is NULL");
+		return NULL;
+	}
+
+	workq = rte_zmalloc(name, sizeof(struct sssnic_workq), 0);
+	if (workq == NULL) {
+		PMD_DRV_LOG(ERR, "Could not alloc memory for  %s", name);
+		return NULL;
+	}
+	ret = sssnic_workq_init(workq, name, socket_id, entry_size, depth);
+	if (ret) {
+		PMD_DRV_LOG(ERR, "Failed to initialize %s", name);
+		rte_free(workq);
+		return NULL;
+	}
+
+	return workq;
+}
diff --git a/drivers/net/sssnic/base/sssnic_workq.h b/drivers/net/sssnic/base/sssnic_workq.h
new file mode 100644
index 0000000000..470aef6409
--- /dev/null
+++ b/drivers/net/sssnic/base/sssnic_workq.h
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2018-2022 Shenzhen 3SNIC Information Technology Co., Ltd.
+ */
+
+#ifndef _SSSNIC_WORKQ_H_
+#define _SSSNIC_WORKQ_H_
+
+struct sssnic_workq {
+	/* DMA buffer of entries*/
+	const struct rte_memzone *buf_mz;
+	/* Virtual address of DMA buffer */
+	uint8_t *buf_addr;
+	/* Physic address of DMA buffer */
+	uint64_t buf_phyaddr;
+	/* DMA buffer size */
+	uint32_t buf_size;
+	/* element size */
+	uint32_t entry_size;
+	/* number of bits of entry size */
+	uint16_t entry_shift;
+	/* Max number of entries in buf */
+	uint16_t num_entries;
+	/* Number of entries not be used */
+	uint16_t idle_entries;
+	/* Consumer index */
+	uint16_t ci;
+	/* Producer index */
+	uint16_t pi;
+	/* CI and PI mask */
+	uint16_t index_mask;
+} __rte_cache_aligned;
+
+#define SSSNIC_WORKQ_ENTRY_CAST(workq, idx, type)                              \
+	(((type *)((workq)->buf_addr)) + (idx))
+#define SSSNIC_WORKQ_BUF_PHYADDR(workq) ((workq)->buf_phyaddr)
+
+static inline void *
+sssnic_workq_entry_get(struct sssnic_workq *workq, uint32_t index)
+{
+	return (void *)(workq->buf_addr + (index << workq->entry_shift));
+}
+
+/* Return the entry address of current CI position. */
+static inline void *
+sssnic_workq_peek(struct sssnic_workq *workq)
+{
+	if ((workq->idle_entries + 1) > workq->num_entries)
+		return NULL;
+
+	return sssnic_workq_entry_get(workq, workq->ci & workq->index_mask);
+}
+
+static inline uint16_t
+sssnic_workq_num_used_entries(struct sssnic_workq *workq)
+{
+	return workq->num_entries - workq->idle_entries;
+}
+
+static inline uint16_t
+sssnic_workq_num_idle_entries(struct sssnic_workq *workq)
+{
+	return workq->idle_entries;
+}
+
+static inline uint16_t
+sssnic_workq_ci_get(struct sssnic_workq *workq)
+{
+	return workq->ci & workq->index_mask;
+}
+
+static inline uint16_t
+sssnic_workq_pi_get(struct sssnic_workq *workq)
+{
+	return workq->pi & workq->index_mask;
+}
+
+static inline void
+sssnic_workq_consume_fast(struct sssnic_workq *workq, uint16_t num_entries)
+{
+	workq->idle_entries += num_entries;
+	workq->ci += num_entries;
+}
+
+static inline void
+sssnic_workq_produce_fast(struct sssnic_workq *workq, uint16_t num_entries)
+{
+	workq->idle_entries -= num_entries;
+	workq->pi += num_entries;
+}
+
+static inline void
+sssnic_workq_reset(struct sssnic_workq *workq)
+{
+	workq->ci = 0;
+	workq->pi = 0;
+	workq->idle_entries = workq->num_entries;
+}
+
+void *sssnic_workq_consume(struct sssnic_workq *workq, uint16_t num_entries,
+	uint16_t *ci);
+void *sssnic_workq_produce(struct sssnic_workq *workq, uint16_t num_entries,
+	uint16_t *pi);
+
+struct sssnic_workq *sssnic_workq_new(const char *name, int socket_id,
+	uint32_t entry_size, uint32_t depth);
+void sssnic_workq_destroy(struct sssnic_workq *workq);
+
+#endif /* _SSSNIC_WORKQ_H_ */
-- 
2.27.0


  parent reply	other threads:[~2023-09-01  9:36 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-01  9:34 [PATCH v4 00/32] Introduce sssnic PMD for 3SNIC's 9x0 serials Ethernet adapters wanry
2023-09-01  9:34 ` [PATCH v4 01/32] net/sssnic: add build and doc infrastructure wanry
2023-09-01 11:54   ` David Marchand
2023-09-04  3:26     ` Renyong Wan
2023-09-01  9:34 ` [PATCH v4 02/32] net/sssnic: add log type and log macros wanry
2023-09-01  9:34 ` [PATCH v4 03/32] net/sssnic: support probe and remove wanry
2023-09-01  9:34 ` [PATCH v4 04/32] net/sssnic: initialize hardware base wanry
2023-09-01  9:34 ` [PATCH v4 05/32] net/sssnic: add event queue wanry
2023-09-01  9:34 ` [PATCH v4 06/32] net/sssnic/base: add message definition and utility wanry
2023-09-01  9:34 ` [PATCH v4 07/32] net/sssnic/base: add mailbox support wanry
2023-09-01  9:34 ` wanry [this message]
2023-09-01  9:34 ` [PATCH v4 09/32] net/sssnic/base: add control queue wanry
2023-09-01  9:34 ` [PATCH v4 10/32] net/sssnic: add dev configure and infos get wanry
2023-09-01  9:34 ` [PATCH v4 11/32] net/sssnic: add dev MAC ops wanry
2023-09-01  9:34 ` [PATCH v4 12/32] net/sssnic: support dev link status wanry
2023-09-01  9:34 ` [PATCH v4 13/32] net/sssnic: support link status event wanry
2023-09-01  9:34 ` [PATCH v4 14/32] net/sssnic: support Rx queue setup and release wanry
2023-09-01  9:34 ` [PATCH v4 15/32] net/sssnic: support Tx " wanry
2023-09-01  9:34 ` [PATCH v4 16/32] net/sssnic: support Rx queue start and stop wanry
2023-09-01  9:34 ` [PATCH v4 17/32] net/sssnic: support Tx " wanry
2023-09-01  9:35 ` [PATCH v4 18/32] net/sssnic: add Rx interrupt support wanry
2023-09-01  9:35 ` [PATCH v4 19/32] net/sssnic: support dev start and stop wanry
2023-09-01  9:35 ` [PATCH v4 20/32] net/sssnic: support dev close and reset wanry
2023-09-01  9:35 ` [PATCH v4 21/32] net/sssnic: add allmulticast and promiscuous ops wanry
2023-09-01  9:35 ` [PATCH v4 22/32] net/sssnic: add basic and extended stats ops wanry
2023-09-01  9:35 ` [PATCH v4 23/32] net/sssnic: support Rx packet burst wanry
2023-09-01  9:35 ` [PATCH v4 24/32] net/sssnic: support Tx " wanry
2023-09-01  9:35 ` [PATCH v4 25/32] net/sssnic: add RSS support wanry
2023-09-01  9:35 ` [PATCH v4 26/32] net/sssnic: support dev MTU set wanry
2023-09-01  9:35 ` [PATCH v4 27/32] net/sssnic: support dev queue info get wanry
2023-09-01  9:35 ` [PATCH v4 28/32] net/sssnic: support dev firmware version get wanry
2023-09-01  9:35 ` [PATCH v4 29/32] net/sssnic: add dev flow control ops wanry
2023-09-01  9:35 ` [PATCH v4 30/32] net/sssnic: support VLAN offload and filter wanry
2023-09-01  9:35 ` [PATCH v4 31/32] net/sssnic: add generic flow ops wanry
2023-09-01  9:35 ` [PATCH v4 32/32] net/sssnic: add VF dev support wanry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230901093514.224824-9-wanry@3snic.com \
    --to=wanry@3snic.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=steven.song@3snic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).