[qca-nss-drv] Break up statistics implementation

Break up the centralized statistics implementation to be subsystem based
statistics implementation.

Change-Id: Ibc14d88f391e816d3d6284a57ea6676f2842ac70
Signed-off-by: Yu Huang <yuhuang@codeaurora.org>
diff --git a/nss_gre.c b/nss_gre.c
index 902e9d4..4c426e8 100644
--- a/nss_gre.c
+++ b/nss_gre.c
@@ -15,6 +15,7 @@
  */
 
 #include "nss_tx_rx_common.h"
+#include "nss_gre_stats.h"
 
 #define NSS_GRE_TX_TIMEOUT 3000 /* 3 Seconds */
 
@@ -29,13 +30,6 @@
 	void *app_data;
 } nss_gre_pvt;
 
-/*
- * Data structures to store GRE nss debug stats
- */
-static DEFINE_SPINLOCK(nss_gre_stats_lock);
-static struct nss_stats_gre_session_debug session_debug_stats[NSS_GRE_MAX_DEBUG_SESSION_STATS];
-static struct nss_stats_gre_base_debug base_debug_stats;
-
 static atomic64_t pkt_cb_addr = ATOMIC64_INIT(0);
 
 /*
@@ -60,39 +54,6 @@
 }
 
 /*
- * nss_gre_session_debug_stats_sync()
- *	debug statistics sync for GRE session.
- */
-static void nss_gre_session_debug_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_gre_session_stats_msg *sstats, uint16_t if_num)
-{
-	int i, j;
-	spin_lock_bh(&nss_gre_stats_lock);
-	for (i = 0; i < NSS_GRE_MAX_DEBUG_SESSION_STATS; i++) {
-		if (session_debug_stats[i].if_num == if_num) {
-			for (j = 0; j < NSS_STATS_GRE_SESSION_DEBUG_MAX; j++) {
-				session_debug_stats[i].stats[j] += sstats->stats[j];
-			}
-			break;
-		}
-	}
-	spin_unlock_bh(&nss_gre_stats_lock);
-}
-
-/*
- * nss_gre_base_debug_stats_sync()
- *	Debug statistics sync for GRE base node.
- */
-static void nss_gre_base_debug_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_gre_base_stats_msg *bstats)
-{
-	int i;
-	spin_lock_bh(&nss_gre_stats_lock);
-	for (i = 0; i < NSS_STATS_GRE_BASE_DEBUG_MAX; i++) {
-		base_debug_stats.stats[i] += bstats->stats[i];
-	}
-	spin_unlock_bh(&nss_gre_stats_lock);
-}
-
-/*
  * nss_gre_msg_handler()
  *	Handle NSS -> HLOS messages for GRE
  */
@@ -124,11 +85,11 @@
 		/*
 		 * debug stats embedded in stats msg
 		 */
-		nss_gre_session_debug_stats_sync(nss_ctx, &ntm->msg.sstats, ncm->interface);
+		nss_gre_stats_session_debug_sync(nss_ctx, &ntm->msg.sstats, ncm->interface);
 		break;
 
 	case NSS_GRE_MSG_BASE_STATS:
-		nss_gre_base_debug_stats_sync(nss_ctx, &ntm->msg.bstats);
+		nss_gre_stats_base_debug_sync(nss_ctx, &ntm->msg.bstats);
 		break;
 
 	default:
@@ -195,53 +156,6 @@
 }
 
 /*
- * nss_gre_session_debug_stats_get()
- *	Get GRE session debug statistics.
- */
-void nss_gre_session_debug_stats_get(void *stats_mem, int size)
-{
-	struct nss_stats_gre_session_debug *stats = (struct nss_stats_gre_session_debug *)stats_mem;
-	int i;
-
-	if (!stats || (size < (sizeof(struct nss_stats_gre_session_debug) * NSS_STATS_GRE_SESSION_DEBUG_MAX)))  {
-		nss_warning("No memory to copy gre stats");
-		return;
-	}
-
-	spin_lock_bh(&nss_gre_stats_lock);
-	for (i = 0; i < NSS_GRE_MAX_DEBUG_SESSION_STATS; i++) {
-		if (session_debug_stats[i].valid) {
-			memcpy(stats, &session_debug_stats[i], sizeof(struct nss_stats_gre_session_debug));
-			stats++;
-		}
-	}
-	spin_unlock_bh(&nss_gre_stats_lock);
-}
-
-/*
- * nss_gre_base_debug_stats_get()
- *	Get GRE debug base statistics.
- */
-void nss_gre_base_debug_stats_get(void *stats_mem, int size)
-{
-	struct nss_stats_gre_base_debug *stats = (struct nss_stats_gre_base_debug *)stats_mem;
-
-	if (!stats) {
-		nss_warning("No memory to copy GRE base stats\n");
-		return;
-	}
-
-	if (size < sizeof(struct nss_stats_gre_base_debug)) {
-		nss_warning("Not enough memory to copy GRE base stats\n");
-		return;
-	}
-
-	spin_lock_bh(&nss_gre_stats_lock);
-	memcpy(stats, &base_debug_stats, sizeof(struct nss_stats_gre_base_debug));
-	spin_unlock_bh(&nss_gre_stats_lock);
-}
-
-/*
  * nss_gre_register_pkt_callback()
  *	Register for data callback.
  */
@@ -406,7 +320,6 @@
 			nss_gre_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
 {
 	struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id];
-	int i = 0;
 
 	nss_assert(nss_ctx);
 	nss_assert(nss_is_dynamic_interface(if_num));
@@ -418,16 +331,7 @@
 
 	nss_core_register_handler(nss_ctx, if_num, nss_gre_msg_handler, NULL);
 
-	spin_lock_bh(&nss_gre_stats_lock);
-	for (i = 0; i < NSS_GRE_MAX_DEBUG_SESSION_STATS; i++) {
-		if (!session_debug_stats[i].valid) {
-			session_debug_stats[i].valid = true;
-			session_debug_stats[i].if_num = if_num;
-			session_debug_stats[i].if_index = netdev->ifindex;
-			break;
-		}
-	}
-	spin_unlock_bh(&nss_gre_stats_lock);
+	nss_gre_stats_session_register(if_num, netdev);
 
 	return nss_ctx;
 }
@@ -440,7 +344,6 @@
 void nss_gre_unregister_if(uint32_t if_num)
 {
 	struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id];
-	int i;
 
 	nss_assert(nss_ctx);
 	nss_assert(nss_is_dynamic_interface(if_num));
@@ -451,14 +354,7 @@
 
 	nss_core_unregister_handler(nss_ctx, if_num);
 
-	spin_lock_bh(&nss_gre_stats_lock);
-	for (i = 0; i < NSS_GRE_MAX_DEBUG_SESSION_STATS; i++) {
-		if (session_debug_stats[i].if_num == if_num) {
-			memset(&session_debug_stats[i], 0, sizeof(struct nss_stats_gre_session_debug));
-			break;
-		}
-	}
-	spin_unlock_bh(&nss_gre_stats_lock);
+	nss_gre_stats_session_unregister(if_num);
 }
 EXPORT_SYMBOL(nss_gre_unregister_if);
 
@@ -493,4 +389,5 @@
 	sema_init(&nss_gre_pvt.sem, 1);
 	init_completion(&nss_gre_pvt.complete);
 	nss_core_register_handler(nss_ctx, NSS_GRE_INTERFACE, nss_gre_msg_handler, NULL);
+	nss_gre_stats_dentry_create();
 }