[][openwrt][mt7988][crytpo][add debugfs for crypto module]

[Description]
Add debugfs for crypto module to show SPI and CDRT idx of offloaed
xfrm states.

[Release-log]
N/A

Change-Id: I7ab7559eddd2ac92ad3ce2ca516655f581e85edf
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/9350757
diff --git a/feed/kernel/crypto-eip/src/Makefile b/feed/kernel/crypto-eip/src/Makefile
index 77f6750..94c9051 100644
--- a/feed/kernel/crypto-eip/src/Makefile
+++ b/feed/kernel/crypto-eip/src/Makefile
@@ -14,6 +14,7 @@
 crypto-eip-inline-y += lookaside.o
 crypto-eip-inline-y += lookaside-cipher.o
 crypto-eip-inline-y += lookaside-hash.o
+crypto-eip-inline-y += debugfs.o
 
 crypto-eip-inline-$(CONFIG_CRYPTO_XFRM_OFFLOAD_MTK_PCE) += xfrm-offload.o
 crypto-eip-inline-$(CONFIG_MTK_TOPS_CAPWAP_DTLS) += capwap-dtls-offload.o
diff --git a/feed/kernel/crypto-eip/src/debugfs.c b/feed/kernel/crypto-eip/src/debugfs.c
new file mode 100644
index 0000000..6db0afc
--- /dev/null
+++ b/feed/kernel/crypto-eip/src/debugfs.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 MediaTek Inc.
+ *
+ * Author: Frank-zj Lin <frank-zj.lin@mediatek.com>
+ */
+
+#include <linux/debugfs.h>
+#include <linux/spinlock.h>
+
+#include <pce/cdrt.h>
+
+#include "crypto-eip/crypto-eip.h"
+
+struct dentry *mtk_crypto_debugfs_root;
+
+static int mtk_crypto_debugfs_read(struct seq_file *s, void *private)
+{
+	struct xfrm_params_list *xfrm_params_list;
+	struct mtk_xfrm_params *xfrm_params;
+	unsigned long flags;
+
+	xfrm_params_list = mtk_xfrm_params_list_get();
+
+	spin_lock_irqsave(&xfrm_params_list->lock, flags);
+
+	list_for_each_entry(xfrm_params, &xfrm_params_list->list, node) {
+		seq_printf(s, "XFRM STATE: spi 0x%x, cdrt_idx %3d: ",
+			   htonl(xfrm_params->xs->id.spi),
+			   xfrm_params->cdrt->idx);
+
+		if (xfrm_params->cdrt->type == CDRT_DECRYPT)
+			seq_puts(s, "DECRYPT\n");
+		else if (xfrm_params->cdrt->type == CDRT_ENCRYPT)
+			seq_puts(s, "ENCRYPT\n");
+		else
+			seq_puts(s, "\n");
+	}
+
+	spin_unlock_irqrestore(&xfrm_params_list->lock, flags);
+
+	return 0;
+}
+
+static int mtk_crypto_debugfs_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, mtk_crypto_debugfs_read, file->private_data);
+}
+
+static ssize_t mtk_crypto_debugfs_write(struct file *file,
+					const char __user *ubuf,
+					size_t count, loff_t *ppos)
+{
+	return count;
+}
+
+static const struct file_operations mtk_crypto_debugfs_fops = {
+	.open = mtk_crypto_debugfs_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.write = mtk_crypto_debugfs_write,
+	.release = single_release,
+};
+
+int mtk_crypto_debugfs_init(void)
+{
+	mtk_crypto_debugfs_root = debugfs_create_dir("mtk_crypto", NULL);
+
+	debugfs_create_file("xfrm_params", 0644, mtk_crypto_debugfs_root, NULL,
+			    &mtk_crypto_debugfs_fops);
+
+	return 0;
+}
+
+void mtk_crypto_debugfs_exit(void)
+{
+	debugfs_remove_recursive(mtk_crypto_debugfs_root);
+}
diff --git a/feed/kernel/crypto-eip/src/inc/crypto-eip/crypto-eip.h b/feed/kernel/crypto-eip/src/inc/crypto-eip/crypto-eip.h
index 2fa7526..020822e 100644
--- a/feed/kernel/crypto-eip/src/inc/crypto-eip/crypto-eip.h
+++ b/feed/kernel/crypto-eip/src/inc/crypto-eip/crypto-eip.h
@@ -113,12 +113,26 @@
 	struct cdrt_entry *cdrt_outbound;
 };
 
+struct xfrm_params_list {
+	struct list_head list;
+	spinlock_t lock;
+};
+
 #if defined(CONFIG_MTK_TOPS_CAPWAP_DTLS)
 extern void (*mtk_submit_SAparam_to_eip_driver)(struct DTLS_param *DTLSParam_p, int TnlIdx);
 extern void (*mtk_remove_SAparam_to_eip_driver)(struct DTLS_param *DTLSParam_p, int TnlIdx);
 extern void (*mtk_update_cdrt_idx_from_eip_driver)(struct mtk_cdrt_idx_param *cdrt_idx_params_p);
 #endif
 
+#if defined(CONFIG_CRYPTO_XFRM_OFFLOAD_MTK_PCE)
+struct xfrm_params_list *mtk_xfrm_params_list_get(void);
+#else /* !defined(CONFIG_CRYPTO_XFRM_OFFLOAD_MTK_PCE) */
+static inline struct xfrm_params_list *mtk_xfrm_params_list_get(void)
+{
+	return NULL;
+}
+#endif /* defined(CONFIG_CRYPTO_XFRM_OFFLOAD_MTK_PCE) */
+
 void mtk_update_dtls_param(struct DTLS_param *DTLSParam_p, int TnlIdx);
 void mtk_remove_dtls_param(struct DTLS_param *DTLSParam_p, int TnlIdx);
 
diff --git a/feed/kernel/crypto-eip/src/inc/crypto-eip/debugfs.h b/feed/kernel/crypto-eip/src/inc/crypto-eip/debugfs.h
new file mode 100644
index 0000000..75ef742
--- /dev/null
+++ b/feed/kernel/crypto-eip/src/inc/crypto-eip/debugfs.h
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 MediaTek Inc.
+ *
+ * Author: Frank-zj Lin <frank-zj.lin@mediatek.com>
+ */
+
+#ifndef _CRYPTO_EIP_DEBUGFS_H_
+#define _CRYPTO_EIP_DEBUGFS_H_
+
+int mtk_crypto_debugfs_init(void);
+void mtk_crypto_debugfs_eixt(void);
+#endif /* _CRYPTO_EIP_DEBUGFS_H_ */
diff --git a/feed/kernel/crypto-eip/src/init.c b/feed/kernel/crypto-eip/src/init.c
index 42976ee..757f2ec 100644
--- a/feed/kernel/crypto-eip/src/init.c
+++ b/feed/kernel/crypto-eip/src/init.c
@@ -26,6 +26,7 @@
 #include "crypto-eip/ddk-wrapper.h"
 #include "crypto-eip/lookaside.h"
 #include "crypto-eip/internal.h"
+#include "crypto-eip/debugfs.h"
 
 #define DRIVER_AUTHOR	"Ren-Ting Wang <ren-ting.wang@mediatek.com, " \
 			"Chris.Chou <chris.chou@mediatek.com"
@@ -410,6 +411,7 @@
 	}
 
 	mtk_crypto_xfrm_offload_init(mcrypto.eth);
+	mtk_crypto_debugfs_init();
 	mtk_crypto_register_algorithms(priv);
 #if defined(CONFIG_MTK_TOPS_CAPWAP_DTLS)
 	mtk_dtls_capwap_init();
diff --git a/feed/kernel/crypto-eip/src/xfrm-offload.c b/feed/kernel/crypto-eip/src/xfrm-offload.c
index 2d8702d..1075b77 100644
--- a/feed/kernel/crypto-eip/src/xfrm-offload.c
+++ b/feed/kernel/crypto-eip/src/xfrm-offload.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/spinlock.h>
 
 #include <mtk_eth_soc.h>
 
@@ -27,7 +28,10 @@
 #include "crypto-eip/ddk-wrapper.h"
 #include "crypto-eip/internal.h"
 
-static LIST_HEAD(xfrm_params_head);
+static struct xfrm_params_list xfrm_params_list = {
+	.list = LIST_HEAD_INIT(xfrm_params_list.list),
+	.lock = __SPIN_LOCK_UNLOCKED(xfrm_params_list.lock),
+};
 
 #if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
 extern int (*ra_sw_nat_hook_tx)(struct sk_buff *skb, int gmac_no);
@@ -50,6 +54,11 @@
 }
 #endif // HNAT
 
+struct xfrm_params_list *mtk_xfrm_params_list_get(void)
+{
+	return &xfrm_params_list;
+}
+
 static void mtk_xfrm_offload_cdrt_tear_down(struct mtk_xfrm_params *xfrm_params)
 {
 	memset(&xfrm_params->cdrt->desc, 0, sizeof(struct cdrt_desc));
@@ -220,6 +229,7 @@
 int mtk_xfrm_offload_state_add(struct xfrm_state *xs)
 {
 	struct mtk_xfrm_params *xfrm_params;
+	unsigned long flags;
 	int ret = 0;
 
 	/* TODO: maybe support IPv6 in the future? */
@@ -261,7 +271,12 @@
 	}
 
 	xs->xso.offload_handle = (unsigned long)xfrm_params;
-	list_add_tail(&xfrm_params->node, &xfrm_params_head);
+
+	spin_lock_irqsave(&xfrm_params_list.lock, flags);
+
+	list_add_tail(&xfrm_params->node, &xfrm_params_list.list);
+
+	spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
 out:
 	return ret;
 }
@@ -294,9 +309,14 @@
 void mtk_xfrm_offload_state_tear_down(void)
 {
 	struct mtk_xfrm_params *xfrm_params, *tmp;
+	unsigned long flags;
 
-	list_for_each_entry_safe(xfrm_params, tmp, &xfrm_params_head, node)
+	spin_lock_irqsave(&xfrm_params_list.lock, flags);
+
+	list_for_each_entry_safe(xfrm_params, tmp, &xfrm_params_list.list, node)
 		mtk_xfrm_offload_state_free(xfrm_params->xs);
+
+	spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
 }
 
 int mtk_xfrm_offload_policy_add(struct xfrm_policy *xp)