[qca-nss-drv] Module param for maximum IPv4/IPv6 connections.

Add module parameters to configure the maximum number of IPv4/IPv6
connections supported by the accelerator. Configure these values as
soon as NSS is initialized.

Change-Id: Ifc337dcf0fb4d609871911365bae40630c244904
Signed-off-by: Tushar Mathur <tushar@codeaurora.org>
diff --git a/nss_ipv4.c b/nss_ipv4.c
index 6c73c7c..90917a9 100644
--- a/nss_ipv4.c
+++ b/nss_ipv4.c
@@ -323,6 +323,58 @@
 }
 
 /*
+ * nss_ipv4_conn_cfg_process()
+ *	Process request to configure number of ipv4 connections
+ */
+static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn,
+				     void (*cfg_cb)(void *app_data, struct nss_ipv4_msg *nim))
+{
+	struct nss_ipv4_msg nim;
+	struct nss_ipv4_rule_conn_cfg_msg *nirccm;
+	nss_tx_status_t nss_tx_status;
+	uint32_t sum_of_conn;
+
+	/*
+	 * The input should be multiple of 1024.
+	 * Input for ipv4 and ipv6 sum together should not exceed 8k
+	 * Min. value should be at least 256 connections. This is the
+	 * minimum connections we will support for each of them.
+	 */
+	sum_of_conn = conn + nss_ipv6_conn_cfg;
+	if ((conn & NSS_NUM_CONN_QUANTA_MASK) ||
+		(sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
+		(conn < NSS_MIN_NUM_CONN)) {
+		nss_warning("%p: input supported connections (%d) does not adhere\
+				specifications\n1) not multiple of 1024,\n2) is less than \
+				min val: %d, OR\n 	IPv4/6 total exceeds %d\n",
+				nss_ctx,
+				conn,
+				NSS_MIN_NUM_CONN,
+				NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
+		return -EINVAL;
+	}
+
+	nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
+
+	memset(&nim, 0, sizeof(struct nss_ipv4_msg));
+	nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
+		sizeof(struct nss_ipv4_rule_conn_cfg_msg), cfg_cb, NULL);
+
+	nirccm = &nim.msg.rule_conn_cfg;
+	nirccm->num_conn = htonl(conn);
+	nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
+
+	if (nss_tx_status != NSS_TX_SUCCESS) {
+		nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
+							nss_ctx,
+							conn);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+/*
  * nss_ipv4_conn_cfg_callback()
  *	call back function for the ipv4 connection configuration handler
  */
@@ -357,11 +409,7 @@
 {
 	struct nss_top_instance *nss_top = &nss_top_main;
 	struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
-	struct nss_ipv4_msg nim;
-	struct nss_ipv4_rule_conn_cfg_msg *nirccm;
-	nss_tx_status_t nss_tx_status;
 	int ret = NSS_FAILURE;
-	uint32_t sum_of_conn;
 
 	/*
 	 * Acquiring semaphore
@@ -383,38 +431,10 @@
 	}
 
 	/*
-	 * The input should be multiple of 1024.
-	 * Input for ipv4 and ipv6 sum together should not exceed 8k
-	 * Min. value should be at least 256 connections. This is the
-	 * minimum connections we will support for each of them.
+	 * Process request to change number of IPv4 connections
 	 */
-	sum_of_conn = nss_ipv4_conn_cfg + nss_ipv6_conn_cfg;
-	if ((nss_ipv4_conn_cfg & NSS_NUM_CONN_QUANTA_MASK) ||
-		(sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
-		(nss_ipv4_conn_cfg < NSS_MIN_NUM_CONN)) {
-		nss_warning("%p: input supported connections (%d) does not adhere\
-				specifications\n1) not multiple of 1024,\n2) is less than \
-				min val: %d, OR\n 	IPv4/6 total exceeds %d\n",
-				nss_ctx,
-				nss_ipv4_conn_cfg,
-				NSS_MIN_NUM_CONN,
-				NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
-		goto failure;
-	}
-
-	nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, nss_ipv4_conn_cfg);
-
-	nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
-		sizeof(struct nss_ipv4_rule_conn_cfg_msg), nss_ipv4_conn_cfg_callback, NULL);
-
-	nirccm = &nim.msg.rule_conn_cfg;
-	nirccm->num_conn = htonl(nss_ipv4_conn_cfg);
-	nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
-
-	if (nss_tx_status != NSS_TX_SUCCESS) {
-		nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
-							nss_ctx,
-							nss_ipv4_conn_cfg);
+	ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg, nss_ipv4_conn_cfg_callback);
+	if (ret != 0) {
 		goto failure;
 	}
 
@@ -448,6 +468,46 @@
 	return -EINVAL;
 }
 
+/*
+ * nss_ipv4_update_conn_count_cb()
+ *	call back function for the ipv4 connection count update handler
+ */
+static void nss_ipv4_update_conn_count_cb(void *app_data, struct nss_ipv4_msg *nim)
+{
+	if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
+		nss_warning("IPv4 connection count update failed with error: %d\n", nim->cm.error);
+		return;
+	}
+
+	nss_warning("IPv4 connection count update success: %d\n", nim->cm.error);
+}
+
+/*
+ * nss_ipv4_update_conn_count()
+ *	Sets the maximum number of connections for IPv4
+ */
+int nss_ipv4_update_conn_count(int ipv4_num_conn)
+{
+	struct nss_top_instance *nss_top = &nss_top_main;
+	struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
+	int saved_nss_ipv4_conn_cfg = nss_ipv4_conn_cfg;
+	int ret = 0;
+
+	nss_ipv4_conn_cfg = ipv4_num_conn;
+
+	/*
+	 * Process request to change number of IPv4 connections
+	 */
+	ret = nss_ipv4_conn_cfg_process(nss_ctx, nss_ipv4_conn_cfg,
+					nss_ipv4_update_conn_count_cb);
+	if (ret != 0) {
+		nss_ipv4_conn_cfg = saved_nss_ipv4_conn_cfg;
+		return ret;
+	}
+
+	return 0;
+}
+
 static ctl_table nss_ipv4_table[] = {
 	{
 		.procname		= "ipv4_conn",
@@ -536,3 +596,4 @@
 EXPORT_SYMBOL(nss_ipv4_register_sysctl);
 EXPORT_SYMBOL(nss_ipv4_unregister_sysctl);
 EXPORT_SYMBOL(nss_ipv4_msg_init);
+EXPORT_SYMBOL(nss_ipv4_update_conn_count);