[qca-nss-clients] Add edma netlink module

Support Multicast event notifications.

Change-Id: I901a3d098de21f66b2ded9b76187b7915a2263b9
Signed-off-by: Wayne Tan <wtan@codeaurora.org>
diff --git a/netlink/Makefile b/netlink/Makefile
index c2ed18d..4b6c633 100644
--- a/netlink/Makefile
+++ b/netlink/Makefile
@@ -10,6 +10,7 @@
 ccflags-y += -DCONFIG_NSS_NLOAM=1
 ccflags-y += -DCONFIG_NSS_NLGRE_REDIR_FAMILY=1
 ccflags-y += -DCONFIG_NSS_NLETHRX=1
+ccflags-y += -DCONFIG_NSS_NLEDMA=1
 
 qca-nss-netlink-objs := nss_nl.o
 qca-nss-netlink-objs += nss_nlgre_redir_family.o
@@ -22,6 +23,7 @@
 qca-nss-netlink-objs += nss_nlipsec.o
 qca-nss-netlink-objs += nss_nloam.o
 qca-nss-netlink-objs += nss_nlethrx.o
+qca-nss-netlink-objs += nss_nledma.o
 
 CAPWAP_ENABLED:=CONFIG_PACKAGE_kmod-qca-nss-drv-capwapmgr=y
 CAPWAP_CONFIG:=$(shell  grep $(CAPWAP_ENABLED) $(TOPDIR)/.config)
diff --git a/netlink/include/nss_nlcmn_if.h b/netlink/include/nss_nlcmn_if.h
index 8ac0fdf..94eb903 100644
--- a/netlink/include/nss_nlcmn_if.h
+++ b/netlink/include/nss_nlcmn_if.h
@@ -41,6 +41,7 @@
  * @brief NSS subsystems in alphabetical order for nssinfo tool
  */
 enum nss_nlcmn_subsys {
+	NSS_NLCMN_SUBSYS_EDMA,
 	NSS_NLCMN_SUBSYS_ETHRX,
 	NSS_NLCMN_SUBSYS_IPV4,
 	NSS_NLCMN_SUBSYS_MAX
diff --git a/netlink/include/nss_nledma_if.h b/netlink/include/nss_nledma_if.h
new file mode 100644
index 0000000..604b5de
--- /dev/null
+++ b/netlink/include/nss_nledma_if.h
@@ -0,0 +1,42 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * @file nss_nledma_if.h
+ *      NSS Netlink Edma headers
+ */
+#ifndef __NSS_NLEDMA_IF_H
+#define __NSS_NLEDMA_IF_H
+#include "nss_edma.h"
+
+/**
+ * Edma forwarding Family
+ */
+#define NSS_NLEDMA_FAMILY "nss_nledma"
+#define NSS_NLEDMA_MCAST_GRP "nss_nledma_mc"
+
+/**
+ * @brief Edma stats
+ */
+struct nss_nledma_stats {
+	uint32_t core_id;
+	int port_id;
+	uint64_t cmn_node_stats[NSS_STATS_NODE_MAX];
+};
+
+#endif /* __NSS_NLEDMA_IF_H */
diff --git a/netlink/nss_nl.c b/netlink/nss_nl.c
index b425fce..41cacd6 100644
--- a/netlink/nss_nl.c
+++ b/netlink/nss_nl.c
@@ -50,6 +50,8 @@
 #include "nss_nloam_if.h"
 #include "nss_nlethrx.h"
 #include "nss_nlethrx_if.h"
+#include "nss_nledma.h"
+#include "nss_nledma_if.h"
 #if defined (CONFIG_NSS_NLCRYPTO)
 #include "nss_nlcrypto_if.h"
 #else
@@ -168,6 +170,15 @@
 		.exit = NSS_NLETHRX_EXIT,		/* exit */
 		.valid = CONFIG_NSS_NLETHRX		/* 1 or 0 */
 	},
+	{
+		/*
+		 * NSS_NLEDMA
+		 */
+		.name = NSS_NLEDMA_FAMILY,		/* edma */
+		.entry = NSS_NLEDMA_INIT,		/* init */
+		.exit = NSS_NLEDMA_EXIT,		/* exit */
+		.valid = CONFIG_NSS_NLEDMA		/* 1 or 0 */
+	},
 };
 
 #define NSS_NL_FAMILY_HANDLER_SZ ARRAY_SIZE(family_handlers)
diff --git a/netlink/nss_nledma.c b/netlink/nss_nledma.c
new file mode 100644
index 0000000..220958a
--- /dev/null
+++ b/netlink/nss_nledma.c
@@ -0,0 +1,177 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * nss_nledma.c
+ *	NSS Netlink Edma Handler
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/netlink.h>
+#include <linux/vmalloc.h>
+
+#include <net/genetlink.h>
+#include <net/sock.h>
+
+#include <nss_api_if.h>
+#include <nss_cmn.h>
+#include <nss_nl_if.h>
+#include "nss_nl.h"
+#include "nss_nlcmn_if.h"
+#include "nss_nledma_if.h"
+#include "nss_edma.h"
+
+/*
+ * prototypes
+ */
+static int nss_nledma_ops_get_stats(struct sk_buff *skb, struct genl_info *info);
+static int nss_nledma_process_notify(struct notifier_block *nb, unsigned long val, void *data);
+
+/*
+ * Edma family definition
+ */
+static struct genl_family nss_nledma_family = {
+	.id = GENL_ID_GENERATE,				/* Auto generate ID */
+	.name = NSS_NLEDMA_FAMILY,			/* family name string */
+	.hdrsize = sizeof(struct nss_nledma_stats),	/* NSS NETLINK Edma stats */
+	.version = NSS_NL_VER,				/* Set it to NSS_NLEDMA version */
+	.maxattr = NSS_STATS_EVENT_MAX,			/* maximum commands supported */
+	.netnsok = true,
+	.pre_doit = NULL,
+	.post_doit = NULL,
+};
+
+/*
+ * multicast group for sending message status & events
+ */
+static const struct genl_multicast_group nss_nledma_mcgrp[] = {
+	{.name = NSS_NLEDMA_MCAST_GRP},
+};
+
+/*
+ * operation table called by the generic netlink layer based on the command
+ */
+static struct genl_ops nss_nledma_ops[] = {
+	{.cmd = NSS_STATS_EVENT_NOTIFY, .doit = nss_nledma_ops_get_stats,},
+};
+
+/*
+ * statistics call back handler for edma from NSS
+ */
+static struct notifier_block nss_edma_stats_notifier_nb = {
+	.notifier_call = nss_nledma_process_notify,
+};
+
+#define NSS_NLEDMA_OPS_SZ ARRAY_SIZE(nss_nledma_ops)
+
+/*
+ * nss_nledma_ops_get_stats()
+ *	get stats handler
+ */
+static int nss_nledma_ops_get_stats(struct sk_buff *skb, struct genl_info *info)
+{
+	return 0;
+}
+
+/*
+ * nss_nledma_process_notify()
+ *	process notification messages from NSS
+ */
+static int nss_nledma_process_notify(struct notifier_block *nb, unsigned long val, void *data)
+{
+	struct nss_nledma_stats *stats;
+	struct sk_buff *skb;
+	int id;
+
+	for (id = 0 ; id < NSS_EDMA_NUM_PORTS_MAX; id++) {
+		skb = nss_nl_new_msg(&nss_nledma_family, NSS_NLCMN_SUBSYS_EDMA);
+		if (!skb) {
+			nss_nl_error("unable to allocate NSS_NLEDMA event\n");
+			return NOTIFY_DONE;
+		}
+
+		stats = nss_nl_get_data(skb);
+		nss_edma_get_stats(stats->cmn_node_stats,id);
+		stats->core_id = *(uint32_t *)data;
+		stats->port_id = id;
+		nss_nl_mcast_event(&nss_nledma_family, skb);
+	}
+
+	return NOTIFY_DONE;
+}
+
+/*
+ * nss_nledma_init()
+ *	handler init
+ */
+bool nss_nledma_init(void)
+{
+	int error, ret;
+
+	nss_nl_info_always("Init NSS netlink Edma handler\n");
+
+	/*
+	 * register NETLINK ops with the family
+	 */
+	error = genl_register_family_with_ops_groups(&nss_nledma_family, nss_nledma_ops, nss_nledma_mcgrp);
+	if (error) {
+		nss_nl_info_always("Error: unable to register Edma family\n");
+		return false;
+	}
+
+	/*
+	 * register device call back handler for edma from NSS
+	 */
+	ret = nss_edma_stats_register_notifier(&nss_edma_stats_notifier_nb);
+	if (ret) {
+		nss_nl_info_always("Error: retreiving the NSS Context\n");
+		genl_unregister_family(&nss_nledma_family);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * nss_nledma_exit()
+ *	handler exit
+ */
+bool nss_nledma_exit(void)
+{
+	int error;
+
+	nss_nl_info_always("Exit NSS netlink Edma handler\n");
+
+	/*
+	 * Unregister the device callback handler for edma
+	 */
+	nss_edma_stats_unregister_notifier(&nss_edma_stats_notifier_nb);
+
+	/*
+	 * unregister the ops family
+	 */
+	error = genl_unregister_family(&nss_nledma_family);
+	if (error) {
+		nss_nl_info_always("unable to unregister Edma NETLINK family\n");
+		return false;
+	}
+
+	return true;
+}
diff --git a/netlink/nss_nledma.h b/netlink/nss_nledma.h
new file mode 100644
index 0000000..0d96617
--- /dev/null
+++ b/netlink/nss_nledma.h
@@ -0,0 +1,37 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * nss_nledma.h
+ *      NSS Netlink Edma API definitions
+ */
+#ifndef __NSS_NLEDMA_H
+#define __NSS_NLEDMA_H
+
+bool nss_nledma_init(void);
+bool nss_nledma_exit(void);
+
+#if defined(CONFIG_NSS_NLEDMA)
+#define NSS_NLEDMA_INIT nss_nledma_init
+#define NSS_NLEDMA_EXIT nss_nledma_exit
+#else
+#define NSS_NLEDMA_INIT 0
+#define NSS_NLEDMA_EXIT 0
+#endif /* !CONFIG_NSS_NLEDMA */
+
+#endif /* __NSS_NLEDMA_H */