[qca-nss-drv] additional pbuf pool from host to NSS

Conflicts:
	exports/nss_n2h.h
	nss_n2h.c

Change-Id: Ic8f77c4b1378800ab856acc33ffa18dfdf939cef
Signed-off-by: Kalyan Tallapragada <ktallapr@codeaurora.org>
diff --git a/exports/nss_n2h.h b/exports/nss_n2h.h
index ddab0ca..7290c97 100644
--- a/exports/nss_n2h.h
+++ b/exports/nss_n2h.h
@@ -27,6 +27,8 @@
 #ifndef __NSS_N2H_H
 #define __NSS_N2H_H
 
+#define MAX_PAGES_PER_MSG 32
+
 /*
  * Private data structure for configure general configs
  */
@@ -47,6 +49,8 @@
 	NSS_TX_METADATA_TYPE_N2H_RPS_CFG,
 	NSS_TX_METADATA_TYPE_N2H_EMPTY_POOL_BUF_CFG,
 	NSS_TX_METADATA_TYPE_N2H_FLUSH_PAYLOADS,
+	NSS_TX_METADATA_TYPE_N2H_MITIGATION_CFG,
+	NSS_METADATA_TYPE_N2H_ADD_BUF_POOL,
 	NSS_TX_METADATA_TYPE_SET_WATER_MARK,
 	NSS_TX_METADATA_TYPE_GET_PAYLOAD_INFO,
 	NSS_METADATA_TYPE_N2H_MAX,
@@ -56,6 +60,17 @@
 	uint32_t enable; /* Enable NSS RPS */
 };
 
+struct nss_n2h_mitigation {
+	uint32_t enable; /* Enable NSS MITIGATION */
+};
+
+struct nss_n2h_buf_pool {
+	uint32_t nss_buf_page_size;
+	uint32_t nss_buf_num_pages;
+	void *nss_buf_pool_vaddr[MAX_PAGES_PER_MSG];
+	uint32_t nss_buf_pool_addr[MAX_PAGES_PER_MSG];
+};
+
 /*
  * Old way of setting number of empty pool buffers (payloads).
  * NSS FW then sets low water mark to 'n - ring_size' and
@@ -148,6 +163,9 @@
 							/* Message: empty pool buf configuration */
 		struct nss_n2h_flush_payloads flush_payloads;
 							/* Message: flush payloads present in NSS */
+		struct nss_n2h_mitigation mitigation_cfg;
+							/* Message: Mitigation configuration */
+		struct nss_n2h_buf_pool buf_pool;	/* Message: pbuf coniguration */
 		struct nss_n2h_water_mark wm;
 				/* Message: Sets low and high water marks */
 		struct nss_n2h_payload_info payload_info;
@@ -173,6 +191,18 @@
 extern nss_tx_status_t nss_n2h_rps_cfg(struct nss_ctx_instance *nss_ctx, int enable_rps);
 
 /*
+ * nss_n2h_mitigation_cfg()
+ * 	API to enable/disable Host MITIGATION support in NSS
+ */
+extern nss_tx_status_t nss_n2h_mitigation_cfg(struct nss_ctx_instance *nss_ctx, int enable_mitigation, nss_core_id_t nss_core);
+
+/*
+ * nss_n2h_buf_pool_cfg()
+ * 	API to increase the pbufs on NSS using Host memory
+ */
+extern nss_tx_status_t nss_n2h_buf_pool_cfg(struct nss_ctx_instance *nss_ctx, int nss_pbuf_pool_size, nss_core_id_t nss_core);
+
+/*
  * nss_n2h_empty_pool_buf_register_sysctl()
  *	API to register sysctl for empty pool buffer in n2h.
  */
diff --git a/nss_core.h b/nss_core.h
index 883e332..894aece 100755
--- a/nss_core.h
+++ b/nss_core.h
@@ -620,8 +620,10 @@
 					/* Host to NSS descriptor rings */
 	struct hlos_n2h_desc_ring n2h_desc_ring[15];
 					/* NSS to Host descriptor rings */
-	uint8_t n2h_rps_en;		/* N2H Enable Multiple queues for Data Packets */
+	uint16_t n2h_rps_en;		/* N2H Enable Multiple queues for Data Packets */
+	uint16_t n2h_mitigate_en;	/* N2H mitigation */
 	uint32_t max_buf_size;		/* Maximum buffer size */
+	uint32_t buf_sz_allocated;	/* size of bufs allocated from host */
 	nss_cmn_queue_decongestion_callback_t queue_decongestion_callback[NSS_MAX_CLIENTS];
 					/* Queue decongestion callbacks */
 	void *queue_decongestion_ctx[NSS_MAX_CLIENTS];
diff --git a/nss_init.c b/nss_init.c
index dbc52dd..6bd2188 100755
--- a/nss_init.c
+++ b/nss_init.c
@@ -47,12 +47,18 @@
 #include <linux/regulator/consumer.h>
 #include <linux/clk.h>
 
+#define NSS_N2H_MAX_BUF_POOL_SIZE (1024 * 1024 * 4) /* 4MB */
+
 /*
  * Global declarations
  */
 int nss_ctl_redirect __read_mostly = 0;
 int nss_ctl_debug __read_mostly = 0;
 int nss_rps_cfg __read_mostly = 0;
+int nss_core0_mitigation_cfg __read_mostly = 1;
+int nss_core1_mitigation_cfg __read_mostly = 1;
+int nss_core0_add_buf_pool_size __read_mostly = 0;
+int nss_core1_add_buf_pool_size __read_mostly = 0;
 int nss_ctl_logbuf __read_mostly = 0;
 int nss_jumbo_mru  __read_mostly = 0;
 int nss_paged_mode __read_mostly = 0;
@@ -433,6 +439,146 @@
 }
 
 /*
+ * nss_mitigation_handler()
+ * Enable NSS MITIGATION
+ */
+static int nss_mitigationcfg_core0_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[NSS_CORE_0];
+	int ret;
+
+	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
+	if (ret) {
+		return ret;
+	}
+
+	/*
+	 * It's a read operation
+	 */
+	if (!write) {
+		return ret;
+	}
+
+	if (!nss_core0_mitigation_cfg ) {
+		printk("Disabling NSS MITIGATION\n");
+		nss_n2h_mitigation_cfg(nss_ctx, 0, NSS_CORE_0);
+		return 0;
+	}
+	printk("Invalid input value.Valid value is 0, Runtime re-enabling not supported\n");
+	return -EINVAL;
+}
+
+/*
+ * nss_mitigation_handler()
+ * Enable NSS MITIGATION
+ */
+static int nss_mitigationcfg_core1_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[NSS_CORE_1];
+	int ret;
+
+	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
+	if (ret) {
+		return ret;;
+	}
+
+	/*
+	 * It's a read operation
+	 */
+	if (!write) {
+		return ret;
+	}
+
+	if (!nss_core1_mitigation_cfg ) {
+		printk("Disabling NSS MITIGATION\n");
+		nss_n2h_mitigation_cfg(nss_ctx, 0, NSS_CORE_1);
+		return 0;
+	}
+	printk("Invalid input value.Valid value is 0, Runtime re-enabling not supported\n");
+	return -EINVAL;
+}
+
+/*
+ * nss_buf_handler()
+ *	Add extra NSS bufs from host memory
+ */
+static int nss_buf_cfg_core0_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[NSS_CORE_0];
+	int ret;
+
+	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
+	if (ret) {
+		return ret;
+	}
+
+	/*
+	 * It's a read operation
+	 */
+	if (!write) {
+		return ret;
+	}
+
+	if (nss_ctx->buf_sz_allocated) {
+		nss_core0_add_buf_pool_size = nss_ctx->buf_sz_allocated;
+		return -EPERM;
+	}
+
+	if ((nss_core0_add_buf_pool_size >= 1) && (nss_core0_add_buf_pool_size <= NSS_N2H_MAX_BUF_POOL_SIZE)) {
+		printk("configuring additional NSS pbufs\n");
+		ret = nss_n2h_buf_pool_cfg(nss_ctx, nss_core0_add_buf_pool_size, NSS_CORE_0);
+		nss_core0_add_buf_pool_size = nss_ctx->buf_sz_allocated;
+		printk("additional pbufs of size %d got added to NSS\n", nss_ctx->buf_sz_allocated);
+		return ret;;
+	}
+
+	printk("Invalid input value. should be greater than 1 and less than %d\n", NSS_N2H_MAX_BUF_POOL_SIZE);
+	return -EINVAL;
+}
+
+/*
+ * nss_buf_handler()
+ *	Add extra NSS bufs from host memory
+ */
+static int nss_buf_cfg_core1_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[NSS_CORE_1];
+	int ret;
+
+	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
+	if (ret) {
+		return ret;
+	}
+
+	/*
+	 * It's a read operation
+	 */
+	if (!write) {
+		return ret;
+	}
+
+	if (nss_ctx->buf_sz_allocated) {
+		nss_core1_add_buf_pool_size = nss_ctx->buf_sz_allocated;
+		return -EPERM;
+	}
+
+	if ((nss_core1_add_buf_pool_size >= 1) && (nss_core1_add_buf_pool_size <= NSS_N2H_MAX_BUF_POOL_SIZE)) {
+		printk("configuring additional NSS pbufs\n");
+		ret = nss_n2h_buf_pool_cfg(nss_ctx, nss_core1_add_buf_pool_size, NSS_CORE_1);
+		nss_core1_add_buf_pool_size = nss_ctx->buf_sz_allocated;
+		printk("additional pbufs of size %d got added to NSS\n", nss_ctx->buf_sz_allocated);
+		return ret;
+	}
+
+	printk("Invalid input value. should be greater than 1 and less than %d\n", NSS_N2H_MAX_BUF_POOL_SIZE);
+	return -EINVAL;
+}
+
+/*
  * nss_coredump_handler()
  *	Send Signal To Coredump NSS Cores
  */
@@ -563,6 +709,34 @@
 		.proc_handler   	= &nss_rpscfg_handler,
 	},
 	{
+		.procname               = "mitigation_core0",
+		.data                   = &nss_core0_mitigation_cfg,
+		.maxlen                 = sizeof(int),
+		.mode                   = 0644,
+		.proc_handler           = &nss_mitigationcfg_core0_handler,
+	},
+	{
+		.procname               = "mitigation_core1",
+		.data                   = &nss_core1_mitigation_cfg,
+		.maxlen                 = sizeof(int),
+		.mode                   = 0644,
+		.proc_handler           = &nss_mitigationcfg_core1_handler,
+	},
+	{
+		.procname               = "extra_pbuf_core0",
+		.data                   = &nss_core0_add_buf_pool_size,
+		.maxlen                 = sizeof(int),
+		.mode                   = 0644,
+		.proc_handler   	= &nss_buf_cfg_core0_handler,
+	},
+	{
+		.procname               = "extra_pbuf_core1",
+		.data                   = &nss_core1_add_buf_pool_size,
+		.maxlen                 = sizeof(int),
+		.mode                   = 0644,
+		.proc_handler   	= &nss_buf_cfg_core1_handler,
+	},
+	{
 		.procname               = "logbuf",
 		.data                   = &nss_ctl_logbuf,
 		.maxlen                 = sizeof(int),
diff --git a/nss_n2h.c b/nss_n2h.c
index b12509f..486a41b 100755
--- a/nss_n2h.c
+++ b/nss_n2h.c
@@ -20,9 +20,8 @@
  */
 
 #include "nss_tx_rx_common.h"
+#include <asm/cacheflush.h>
 
-#define NSS_CORE_0			0
-#define NSS_CORE_1			1
 
 #define NSS_N2H_MIN_EMPTY_POOL_BUF_SZ		32
 #define NSS_N2H_DEFAULT_EMPTY_POOL_BUF_SZ	8192
@@ -38,6 +37,8 @@
 static struct nss_n2h_cfg_pvt nss_n2h_nepbcfgp[NSS_MAX_CORES];
 static struct nss_n2h_registered_data nss_n2h_rd[NSS_MAX_CORES];
 static struct nss_n2h_cfg_pvt nss_n2h_rcp;
+static struct nss_n2h_cfg_pvt nss_n2h_mitigationcp[NSS_CORE_MAX];
+static struct nss_n2h_cfg_pvt nss_n2h_bufcp[NSS_CORE_MAX];
 
 /*
  * nss_n2h_stats_sync()
@@ -133,6 +134,10 @@
 		nss_info("NSS N2H rps_en %d \n",nnm->msg.rps_cfg.enable);
 		break;
 
+	case NSS_TX_METADATA_TYPE_N2H_MITIGATION_CFG:
+		nss_info("NSS N2H mitigation_dis %d \n",nnm->msg.mitigation_cfg.enable);
+		break;
+
 	case NSS_TX_METADATA_TYPE_N2H_EMPTY_POOL_BUF_CFG:
 		nss_info("%p: empty pool buf cfg response from FW", nss_ctx);
 		break;
@@ -210,6 +215,63 @@
 }
 
 /*
+ * nss_n2h_mitigation_cfg_callback()
+ *	call back function for mitigation configuration
+ */
+static void nss_n2h_mitigation_cfg_callback(void *app_data, struct nss_n2h_msg *nnm)
+{
+	int core_num = (int)app_data;
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[core_num];
+
+	if (nnm->cm.response != NSS_CMN_RESPONSE_ACK) {
+
+		/*
+		 * Error, hence we are not updating the nss_n2h_mitigate_en
+		 */
+		nss_n2h_mitigationcp[core_num].response = NSS_FAILURE;
+		complete(&nss_n2h_mitigationcp[core_num].complete);
+		nss_warning("core%d: MITIGATION configuration failed : %d\n", core_num, nnm->cm.error);
+		return;
+	}
+
+	nss_info("core%d: MITIGATION configuration succeeded: %d\n", core_num, nnm->cm.error);
+
+	nss_ctx->n2h_mitigate_en = nnm->msg.mitigation_cfg.enable;
+	nss_n2h_mitigationcp[core_num].response = NSS_SUCCESS;
+	complete(&nss_n2h_mitigationcp[core_num].complete);
+}
+
+/*
+ * nss_n2h_buf_cfg_callback()
+ *	call back function for pbuf configuration
+ */
+static void nss_n2h_bufs_cfg_callback(void *app_data, struct nss_n2h_msg *nnm)
+{
+	int core_num = (int)app_data;
+	unsigned int allocated_sz;
+
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[core_num];
+
+	if (nnm->cm.response != NSS_CMN_RESPONSE_ACK) {
+		nss_n2h_bufcp[core_num].response = NSS_FAILURE;
+		nss_warning("core%d: buf configuration failed : %d\n", core_num, nnm->cm.error);
+		goto done;
+	}
+
+	nss_info("core%d: buf configuration succeeded: %d\n", core_num, nnm->cm.error);
+
+	allocated_sz = nnm->msg.buf_pool.nss_buf_page_size * nnm->msg.buf_pool.nss_buf_num_pages;
+	nss_ctx->buf_sz_allocated += allocated_sz;
+
+	nss_n2h_bufcp[core_num].response = NSS_SUCCESS;
+
+done:
+	complete(&nss_n2h_bufcp[core_num].complete);
+}
+
+/*
  * nss_n2h_payload_stats_callback()
  *	It gets called response to payload accounting.
  */
@@ -663,6 +725,146 @@
 	return NSS_SUCCESS;
 }
 
+/*
+ * nss_n2h_mitigation_cfg()
+ *	Send Message to NSS to disable MITIGATION.
+ */
+nss_tx_status_t nss_n2h_mitigation_cfg(struct nss_ctx_instance *nss_ctx, int enable_mitigation, nss_core_id_t core_num)
+{
+	struct nss_n2h_msg nnm;
+	struct nss_n2h_mitigation *mitigation_cfg;
+	nss_tx_status_t nss_tx_status;
+	int ret;
+
+	nss_assert(core_num < NSS_CORE_MAX);
+
+	down(&nss_n2h_mitigationcp[core_num].sem);
+	nss_n2h_msg_init(&nnm, NSS_N2H_INTERFACE, NSS_TX_METADATA_TYPE_N2H_MITIGATION_CFG,
+			sizeof(struct nss_n2h_mitigation),
+			nss_n2h_mitigation_cfg_callback,
+			(void *)core_num);
+
+	mitigation_cfg = &nnm.msg.mitigation_cfg;
+	mitigation_cfg->enable = enable_mitigation;
+
+	nss_tx_status = nss_n2h_tx_msg(nss_ctx, &nnm);
+
+	if (nss_tx_status != NSS_TX_SUCCESS) {
+		nss_warning("%p: nss_tx error setting mitigation\n", nss_ctx);
+		goto failure;
+	}
+
+	/*
+	 * Blocking call, wait till we get ACK for this msg.
+	 */
+	ret = wait_for_completion_timeout(&nss_n2h_mitigationcp[core_num].complete, msecs_to_jiffies(NSS_CONN_CFG_TIMEOUT));
+	if (ret == 0) {
+		nss_warning("%p: Waiting for ack timed out\n", nss_ctx);
+		goto failure;
+	}
+
+	/*
+	 * ACK/NACK received from NSS FW
+	 */
+	if (NSS_FAILURE == nss_n2h_mitigationcp[core_num].response) {
+		goto failure;
+	}
+
+	up(&nss_n2h_mitigationcp[core_num].sem);
+	return NSS_SUCCESS;
+
+failure:
+	up(&nss_n2h_mitigationcp[core_num].sem);
+	return NSS_FAILURE;
+}
+
+static inline void nss_n2h_buf_pool_free(struct nss_n2h_buf_pool *buf_pool)
+{
+	int page_count;
+	for (page_count = 0; page_count < buf_pool->nss_buf_num_pages; page_count++) {
+		kfree(buf_pool->nss_buf_pool_vaddr[page_count]);
+	}
+}
+
+/*
+ * nss_n2h_buf_cfg()
+ *	Send Message to NSS to enable pbufs.
+ */
+nss_tx_status_t nss_n2h_buf_pool_cfg(struct nss_ctx_instance *nss_ctx,
+	       				int buf_pool_size, nss_core_id_t core_num)
+{
+	static struct nss_n2h_msg nnm;
+	struct nss_n2h_buf_pool *buf_pool;
+	nss_tx_status_t nss_tx_status;
+	int ret;
+	int page_count;
+	int num_pages = ALIGN(buf_pool_size, PAGE_SIZE)/PAGE_SIZE;
+
+	nss_assert(core_num < NSS_CORE_MAX);
+
+	nss_n2h_msg_init(&nnm, NSS_N2H_INTERFACE, NSS_METADATA_TYPE_N2H_ADD_BUF_POOL,
+			sizeof(struct nss_n2h_buf_pool),
+			nss_n2h_bufs_cfg_callback,
+			(void *)core_num);
+
+	do {
+
+		down(&nss_n2h_bufcp[core_num].sem);
+
+		buf_pool = &nnm.msg.buf_pool;
+		buf_pool->nss_buf_page_size = PAGE_SIZE;
+
+		for (page_count = 0; page_count < MAX_PAGES_PER_MSG && num_pages; page_count++, num_pages--) {
+
+			void *kern_addr = kzalloc(PAGE_SIZE, GFP_ATOMIC);
+			if (!kern_addr) {
+				BUG_ON(!page_count);
+				break;
+			}
+			BUG_ON((long unsigned int)kern_addr % PAGE_SIZE);
+
+			buf_pool->nss_buf_pool_vaddr[page_count] = kern_addr;
+			buf_pool->nss_buf_pool_addr[page_count] = dma_map_single(NULL, kern_addr, PAGE_SIZE, DMA_TO_DEVICE);
+		}
+
+		buf_pool->nss_buf_num_pages = page_count;
+		nss_tx_status = nss_n2h_tx_msg(nss_ctx, &nnm);
+		if (nss_tx_status != NSS_TX_SUCCESS) {
+
+			nss_n2h_buf_pool_free(buf_pool);
+			nss_warning("%p: nss_tx error setting pbuf\n", nss_ctx);
+			goto failure;
+		}
+
+		/*
+	 	 * Blocking call, wait till we get ACK for this msg.
+	 	 */
+		ret = wait_for_completion_timeout(&nss_n2h_bufcp[core_num].complete, msecs_to_jiffies(NSS_CONN_CFG_TIMEOUT));
+		if (ret == 0) {
+			nss_warning("%p: Waiting for ack timed out\n", nss_ctx);
+			goto failure;
+		}
+
+		/*
+		 * ACK/NACK received from NSS FW
+		 */
+		if (NSS_FAILURE == nss_n2h_bufcp[core_num].response) {
+
+			nss_n2h_buf_pool_free(buf_pool);
+			goto failure;
+		}
+
+		up(&nss_n2h_bufcp[core_num].sem);
+	} while(num_pages);
+
+	return NSS_SUCCESS;
+failure:
+	up(&nss_n2h_bufcp[core_num].sem);
+	return NSS_FAILURE;
+}
+
+
+
 static ctl_table nss_n2h_table[] = {
 	{
 		.procname	= "n2h_empty_pool_buf_core0",
@@ -894,6 +1096,29 @@
 	sema_init(&nss_n2h_rcp.sem, 1);
 	init_completion(&nss_n2h_rcp.complete);
 
+	/*
+	 * MITIGATION sema init for core0
+	 */
+	sema_init(&nss_n2h_mitigationcp[NSS_CORE_0].sem, 1);
+	init_completion(&nss_n2h_mitigationcp[NSS_CORE_0].complete);
+
+	/*
+	 * MITIGATION sema init for core1
+	 */
+	sema_init(&nss_n2h_mitigationcp[NSS_CORE_1].sem, 1);
+	init_completion(&nss_n2h_mitigationcp[NSS_CORE_1].complete);
+
+	/*
+	 * PBUF addition sema init for core0
+	 */
+	sema_init(&nss_n2h_bufcp[NSS_CORE_0].sem, 1);
+	init_completion(&nss_n2h_bufcp[NSS_CORE_0].complete);
+
+	/*
+	 * PBUF addition sema init for core1
+	 */
+	sema_init(&nss_n2h_bufcp[NSS_CORE_1].sem, 1);
+	init_completion(&nss_n2h_bufcp[NSS_CORE_1].complete);
 
 	nss_n2h_notify_register(NSS_CORE_0, NULL, NULL);
 	nss_n2h_notify_register(NSS_CORE_1, NULL, NULL);