From 2884d11a6307d239f72515309b565132c97f1172 Mon Sep 17 00:00:00 2001
From: Kai Ye <yekai13@huawei.com>
Date: Wed, 22 Jun 2022 10:26:32 +0800
Subject: [PATCH 142/183] uadk: add WD_DEV_ERR api

This interface supports device differentiation by the handle of.
context. Users can use the api if they have requested the ctx.
Otherwise, occurred an error.

Signed-off-by: Kai Ye <yekai13@huawei.com>
---
 drv/hisi_hpre.c        |  4 ----
 drv/hisi_qm_udrv.c     | 33 +++++++++++++++++----------------
 include/hisi_qm_udrv.h |  5 +++++
 include/wd.h           | 14 ++++++++++++++
 wd.c                   | 10 ++++++++++
 5 files changed, 46 insertions(+), 20 deletions(-)

diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c
index 7fd4189..d503d84 100644
--- a/drv/hisi_hpre.c
+++ b/drv/hisi_hpre.c
@@ -44,10 +44,6 @@
 #define GEN_PARAMS_SZ(key_size)		((key_size) << 1)
 #define CRT_PARAM_SZ(key_size)		((key_size) >> 1)
 
-#define container_of(ptr, type, member) ({ \
-		typeof(((type *)0)->member)(*__mptr) = (ptr); \
-		(type *)((char *)__mptr - offsetof(type, member)); })
-
 enum hpre_alg_type {
 	HPRE_ALG_NC_NCRT = 0x0,
 	HPRE_ALG_NC_CRT = 0x1,
diff --git a/drv/hisi_qm_udrv.c b/drv/hisi_qm_udrv.c
index 985aecc..9eaf60c 100644
--- a/drv/hisi_qm_udrv.c
+++ b/drv/hisi_qm_udrv.c
@@ -167,14 +167,14 @@ static int hisi_qm_setup_region(handle_t h_ctx,
 {
 	q_info->sq_base = wd_ctx_mmap_qfr(h_ctx, UACCE_QFRT_DUS);
 	if (!q_info->sq_base) {
-		WD_ERR("failed to mmap dus!\n");
+		WD_DEV_ERR(h_ctx, "failed to mmap dus!\n");
 		return -WD_ENOMEM;
 	}
 
 	q_info->mmio_base = wd_ctx_mmap_qfr(h_ctx, UACCE_QFRT_MMIO);
 	if (!q_info->mmio_base) {
 		wd_ctx_unmap_qfr(h_ctx, UACCE_QFRT_DUS);
-		WD_ERR("failed to mmap mmio!\n");
+		WD_DEV_ERR(h_ctx, "failed to mmap mmio!\n");
 		return -WD_ENOMEM;
 	}
 
@@ -197,14 +197,14 @@ static __u32 get_version_id(handle_t h_ctx)
 
 	api_name = wd_ctx_get_api(h_ctx);
 	if (!api_name || strlen(api_name) <= VERSION_ID_SHIFT) {
-		WD_ERR("invalid: api name is %s!\n", api_name);
+		WD_DEV_ERR(h_ctx, "invalid: api name is %s!\n", api_name);
 		return 0;
 	}
 
 	id = api_name + VERSION_ID_SHIFT;
 	ver = strtoul(id, NULL, 10);
 	if (!ver || ver == ULONG_MAX) {
-		WD_ERR("failed to strtoul, ver = %lu!\n", ver);
+		WD_DEV_ERR(h_ctx, "failed to strtoul, ver = %lu!\n", ver);
 		return 0;
 	}
 
@@ -250,7 +250,7 @@ static int his_qm_set_qp_ctx(handle_t h_ctx, struct hisi_qm_priv *config,
 	q_info->qc_type = qp_ctx.qc_type;
 	ret = wd_ctx_set_io_cmd(h_ctx, UACCE_CMD_QM_SET_QP_CTX, &qp_ctx);
 	if (ret < 0) {
-		WD_ERR("failed to set qc_type, use default value!\n");
+		WD_DEV_ERR(h_ctx, "failed to set qc_type, use default value!\n");
 		return ret;
 	}
 
@@ -267,14 +267,14 @@ static int hisi_qm_get_qfrs_offs(handle_t h_ctx,
 	type = UACCE_QFRT_DUS;
 	q_info->region_size[type] = wd_ctx_get_region_size(h_ctx, type);
 	if (!q_info->region_size[type]) {
-		WD_ERR("failed to get DUS qfrs offset!\n");
+		WD_DEV_ERR(h_ctx, "failed to get DUS qfrs offset!\n");
 		return -WD_EINVAL;
 	}
 
 	type = UACCE_QFRT_MMIO;
 	q_info->region_size[type] = wd_ctx_get_region_size(h_ctx, type);
 	if (!q_info->region_size[type]) {
-		WD_ERR("failed to get MMIO qfrs offset!\n");
+		WD_DEV_ERR(h_ctx, "failed to get MMIO qfrs offset!\n");
 		return -WD_EINVAL;
 	}
 
@@ -289,25 +289,25 @@ static int hisi_qm_setup_info(struct hisi_qp *qp, struct hisi_qm_priv *config)
 	q_info = &qp->q_info;
 	ret = hisi_qm_setup_region(qp->h_ctx, q_info);
 	if (ret) {
-		WD_ERR("failed to setup region!\n");
+		WD_DEV_ERR(qp->h_ctx, "failed to setup region!\n");
 		return ret;
 	}
 
 	ret = hisi_qm_get_qfrs_offs(qp->h_ctx, q_info);
 	if (ret) {
-		WD_ERR("failed to get dev qfrs offset!\n");
+		WD_DEV_ERR(qp->h_ctx, "failed to get dev qfrs offset!\n");
 		goto err_out;
 	}
 
 	ret = hisi_qm_setup_db(qp->h_ctx, q_info);
 	if (ret) {
-		WD_ERR("failed to setup db!\n");
+		WD_DEV_ERR(qp->h_ctx, "failed to setup db!\n");
 		goto err_out;
 	}
 
 	ret = his_qm_set_qp_ctx(qp->h_ctx, config, q_info);
 	if (ret) {
-		WD_ERR("failed to setup io cmd!\n");
+		WD_DEV_ERR(qp->h_ctx, "failed to setup io cmd!\n");
 		goto err_out;
 	}
 
@@ -324,7 +324,7 @@ static int hisi_qm_setup_info(struct hisi_qp *qp, struct hisi_qm_priv *config)
 
 	ret = pthread_spin_init(&q_info->lock, PTHREAD_PROCESS_SHARED);
 	if (ret) {
-		WD_ERR("failed to init qinfo lock!\n");
+		WD_DEV_ERR(qp->h_ctx, "failed to init qinfo lock!\n");
 		goto err_out;
 	}
 
@@ -455,6 +455,7 @@ int hisi_qm_send(handle_t h_qp, const void *req, __u16 expect, __u16 *count)
 
 static int hisi_qm_recv_single(struct hisi_qm_queue_info *q_info, void *resp)
 {
+	struct hisi_qp *qp = container_of(q_info, struct hisi_qp, q_info);
 	struct cqe *cqe;
 	__u16 i, j;
 
@@ -466,7 +467,7 @@ static int hisi_qm_recv_single(struct hisi_qm_queue_info *q_info, void *resp)
 		j = CQE_SQ_HEAD_INDEX(cqe);
 		if (j >= QM_Q_DEPTH) {
 			pthread_spin_unlock(&q_info->lock);
-			WD_ERR("CQE_SQ_HEAD_INDEX(%u) error!\n", j);
+			WD_DEV_ERR(qp->h_ctx, "CQE_SQ_HEAD_INDEX(%u) error!\n", j);
 			return -WD_EIO;
 		}
 		memcpy(resp, (void *)((uintptr_t)q_info->sq_base +
@@ -510,7 +511,7 @@ int hisi_qm_recv(handle_t h_qp, void *resp, __u16 expect, __u16 *count)
 
 	q_info = &qp->q_info;
 	if (wd_ioread32(q_info->ds_rx_base) == 1) {
-		WD_ERR("wd queue hw error happened before qm receive!\n");
+		WD_DEV_ERR(qp->h_ctx, "wd queue hw error happened before qm receive!\n");
 		return -WD_HW_EACCESS;
 	}
 
@@ -524,7 +525,7 @@ int hisi_qm_recv(handle_t h_qp, void *resp, __u16 expect, __u16 *count)
 
 	*count = recv_num++;
 	if (wd_ioread32(q_info->ds_rx_base) == 1) {
-		WD_ERR("wd queue hw error happened in qm receive!\n");
+		WD_DEV_ERR(qp->h_ctx, "wd queue hw error happened in qm receive!\n");
 		return -WD_HW_EACCESS;
 	}
 
@@ -537,7 +538,7 @@ int hisi_check_bd_id(handle_t h_qp, __u32 mid, __u32 bid)
 	__u8 mode = qp->q_info.qp_mode;
 
 	if (mode == CTX_MODE_SYNC && mid != bid) {
-		WD_ERR("failed to recv self bd, send id: %u, recv id: %u\n",
+		WD_DEV_ERR(qp->h_ctx, "failed to recv self bd, send id: %u, recv id: %u\n",
 			    mid, bid);
 		return -WD_EINVAL;
 	}
diff --git a/include/hisi_qm_udrv.h b/include/hisi_qm_udrv.h
index ea5807e..0ea17c6 100644
--- a/include/hisi_qm_udrv.h
+++ b/include/hisi_qm_udrv.h
@@ -29,6 +29,11 @@ extern "C" {
 #define BYTE_BITS			8
 #define BYTE_BITS_SHIFT		3
 
+#define __offsetof(t, m) ((size_t)(uintptr_t)&((t *)0)->m)
+#define container_of(ptr, type, member) ({ \
+		typeof(((type *)0)->member)(*__mptr) = (ptr); \
+		(type *)((char *)__mptr - __offsetof(type, member)); })
+
 enum hisi_qm_sgl_copy_dir {
 	COPY_SGL_TO_PBUFF,
 	COPY_PBUFF_TO_SGL
diff --git a/include/wd.h b/include/wd.h
index b8044a4..b0580ba 100644
--- a/include/wd.h
+++ b/include/wd.h
@@ -57,6 +57,13 @@ typedef void (*wd_log)(const char *format, ...);
 #define WD_ERR(fmt, args...)     fprintf(stderr, fmt, ##args)
 #endif
 
+/* @h_ctx: The handle of context. */
+#define WD_DEV_ERR(h_ctx, format, args...)\
+	do {							\
+		char *dev_name = wd_ctx_get_dev_name(h_ctx);	\
+		WD_ERR("%s: "format"\n", dev_name, ##args);	\
+	} while (0)
+
 #define WD_CONSOLE printf
 
 /* WD error code */
@@ -496,6 +503,13 @@ void wd_mempool_stats(handle_t mempool, struct wd_mempool_stats *stats);
  */
 void wd_blockpool_stats(handle_t blkpool, struct wd_blockpool_stats *stats);
 
+/**
+ * wd_ctx_get_dev_name() - Get the device name about task.
+ * @h_ctx: The handle of context.
+ * Return device name.
+ */
+char *wd_ctx_get_dev_name(handle_t h_ctx);
+
 /**
  * wd_get_version() - Get the libwd version number and released time.
  */
diff --git a/wd.c b/wd.c
index 9715e10..b0c3dec 100644
--- a/wd.c
+++ b/wd.c
@@ -826,3 +826,13 @@ bool wd_need_info(void)
 {
 	return uadk_log_level >= WD_LOG_INFO;
 }
+
+char *wd_ctx_get_dev_name(handle_t h_ctx)
+{
+	struct wd_ctx_h	*ctx = (struct wd_ctx_h *)h_ctx;
+
+	if (!ctx)
+		return NULL;
+
+	return ctx->dev_name;
+}
-- 
2.27.0

