BAL and Maple Release 2.2

Signed-off-by: Shad Ansari <developer@Carbon.local>
diff --git a/bcm68620_release/release/host_driver/coop_dba/Makefile b/bcm68620_release/release/host_driver/coop_dba/Makefile
new file mode 100644
index 0000000..fb20a25
--- /dev/null
+++ b/bcm68620_release/release/host_driver/coop_dba/Makefile
@@ -0,0 +1,38 @@
+# Coop DBA
+MOD_NAME = coop_dba
+
+ifeq ("$(OS_KERNEL)", "linux")
+MOD_TYPE = linux_lib
+else
+MOD_TYPE = lib
+endif
+
+ifeq ("$(RELEASE_BUILD)", "y")
+	EXTRA_CFLAGS += -I$(TOP_DIR)/host_driver/api \
+-I$(TOP_DIR)/host_driver/utils \
+-I$(TOP_DIR)/host_reference/dev_log \
+-I$(TOP_DIR)/host_driver/common_gpon \
+-I$(TOP_DIR)/host_driver/transport/mux \
+-I$(TOP_DIR)/host_driver/transport/pcie_sw_queue \
+-I$(TOP_DIR)/host_driver/pcie \
+-I$(TOP_DIR)/host_driver/transport
+else
+	EXTRA_CFLAGS += -I$(TOP_DIR)/common/gpon \
+-I$(TOP_DIR)/common/model/maple \
+-I$(TOP_DIR)/common/transport/driver/maple/pcie_sw_queue \
+-I$(TOP_DIR)/common/drivers/maple/pcie \
+-I$(TOP_DIR)/common/api \
+-I$(TOP_DIR)/common/utils \
+-I$(TOP_DIR)/common/dev_log \
+-I$(TOP_DIR)/host/transport/driver/maple/mux
+endif
+				
+srcs = bcmolt_coop_dba.c
+
+MOD_INC_DIRS = $(SRC_DIR) $(MODEL_OUT_DIR)
+
+# If called by linux kernel builder - add include paths manually.
+# It is not elegant, but we'll not have many linux modules
+ifneq ("$(KBUILD_SRC)", "")
+	-include $(OUT_DIR_BASE)/Makefile.config.$(MOD_NAME)
+endif
diff --git a/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.c b/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.c
new file mode 100644
index 0000000..add7ae5
--- /dev/null
+++ b/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.c
@@ -0,0 +1,223 @@
+/*
+<:copyright-BRCM:2016:DUAL/GPL:standard
+
+   Broadcom Proprietary and Confidential.(c) 2016 Broadcom
+   All Rights Reserved
+
+Unless you and Broadcom execute a separate written software license
+agreement governing use of this software, this software is licensed
+to you under the terms of the GNU General Public License version 2
+(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
+with the following added to such license:
+
+   As a special exception, the copyright holders of this software give
+   you permission to link this software with independent modules, and
+   to copy and distribute the resulting executable under terms of your
+   choice, provided that you also meet, for each linked independent
+   module, the terms and conditions of the license of that module.
+   An independent module is a module which is not derived from this
+   software.  The special exception does not apply to any modifications
+   of the software.
+
+Not withstanding the above, under no circumstances may you combine
+this software in any way with any other Broadcom software provided
+under a license other than the GPL, without Broadcom's express prior
+written consent.
+
+:>
+ */
+
+#include "bcmolt_coop_dba.h"
+
+/* this is the host application callback that will be called to handle the indications sent from
+ * the embedded side to the host during DBA cycle
+ */
+static bcmolt_coop_dba_host_handler_cb host_handler_array[BCMTR_MAX_OLTS];
+
+/* An API to implement Partial-External DBA type */
+bcmos_errno bcmolt_fast_oper_set_allocs(bcmolt_devid device, uint8_t pon_ni, bcm_common_fast_set_allocs_msg *msg)
+{
+	bcmos_errno rc = BCM_ERR_OK;
+	bcmos_buf *buf;
+	unsigned char *buf_data;
+	bcm_common_fast_set_allocs_msg *msg_p;
+	uint32_t len = sizeof(bcm_common_fast_msg_type) + sizeof(pon_ni) +
+		sizeof(msg->hdr) + sizeof(bcm_common_fast_set_alloc) * msg->hdr.num_of_allocs;
+	uint32_t i;
+
+    buf = bcmos_buf_alloc(len);
+
+    if (!buf)
+    {
+        BCMOS_TRACE_ERR("Can't allocate packet buffer\n");
+        return BCM_ERR_NOMEM;
+    }
+    bcmos_buf_length_set(buf, len);
+    buf_data = bcmos_buf_data(buf);
+
+	/* change endianness from host to embedded */
+
+    /* fixing header endianness and packing */
+    *buf_data++ = (uint8_t)BCM_COMMON_FAST_MSG_TYPE_SET_ALLOCS; /* msg type */
+    *buf_data++ = pon_ni; /* pon_ni is U8 */
+
+    msg_p = (bcm_common_fast_set_allocs_msg *)buf_data;
+
+    msg_p->hdr.cycle_num = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, msg->hdr.cycle_num);
+
+    msg_p->hdr.num_of_allocs = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, msg->hdr.num_of_allocs);
+
+    for (i = 0; i < sizeof(msg->hdr.num_of_allocs); i++)
+    {
+    	/* going over the list parts and fixing their endianity and packing into buf */
+    	msg_p->set_alloc_list[i].alloc_id = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U16, msg->set_alloc_list[i].alloc_id);
+    	msg_p->set_alloc_list[i].allocation_blocks = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, msg->set_alloc_list[i].allocation_blocks);
+    }
+
+	/* send directly to PCIe driver on the dedicated channel for fast API */
+    rc = bcmtr_swq_send(device, URGENT_CHANNEL, buf);
+
+    if (rc)
+    {
+        BCMOS_TRACE_ERR("bcmtr_pcie_send failed. Error %s (%d)\n", bcmos_strerror(rc), rc);
+        bcmos_buf_free(buf);
+        return BCM_ERR_PARM;
+    }
+    /* buffer will be freed automatically once it is transmitted */
+    return BCM_ERR_OK;
+}
+
+bcmos_errno bcmolt_fast_oper_set_accesses(bcmolt_devid device, uint8_t pon_ni, bcm_common_fast_set_accesses_msg *msg)
+{
+	bcmos_errno rc = BCM_ERR_OK;
+	bcmos_buf* buf;
+	bcm_common_fast_set_accesses_msg *msg_p;
+	unsigned char* buf_data;
+	uint32_t len = sizeof(bcm_common_fast_msg_type) + sizeof(pon_ni) +
+		sizeof (msg->hdr) + sizeof(bcm_common_fast_set_access) * msg->hdr.num_of_allocs;
+	uint32_t i;
+
+    buf = bcmos_buf_alloc(len);
+
+    if (!buf)
+    {
+        BCMOS_TRACE_ERR("Can't allocate packet buffer\n");
+        return BCM_ERR_NOMEM;
+    }
+    bcmos_buf_length_set(buf, len);
+    buf_data = bcmos_buf_data(buf);
+
+	/* change endianess from host to embedded */
+    /* fixing header endianness and packing */
+    *buf_data++ = (uint8_t)BCM_COMMON_FAST_MSG_TYPE_SET_BW_ALLOCS;
+    *buf_data++ = pon_ni; /* pon_ni is U8 */
+
+    msg_p = (bcm_common_fast_set_accesses_msg *)buf_data;
+
+    msg_p->hdr.cycle_num = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, msg->hdr.cycle_num);
+    msg_p->hdr.num_of_allocs = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, msg->hdr.num_of_allocs);
+
+    for (i = 0; i < sizeof(msg->hdr.num_of_allocs); i++)
+    {
+    	/* going over the list parts and fixing their endianity and packing into buf */
+    	msg_p->set_access_list[i].alloc_id = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U16, msg->set_access_list[i].alloc_id);
+    	msg_p->set_access_list[i].allocation_size = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U16, msg->set_access_list[i].allocation_size);
+    	msg_p->set_access_list[i].start_time = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U16, msg->set_access_list[i].start_time);
+    	msg_p->set_access_list[i].burst_profile = msg->set_access_list[i].burst_profile; /* burst profile is U8 */
+    	msg_p->set_access_list[i].alloc_flags = msg->set_access_list[i].alloc_flags; /* ploam_flag, dbru_flag, end_of_frame, end_of_map and fwi are boolean and are represented as a bitmap (U8) */
+    }
+
+	/* send directly to PCIe driver on the dedicated channel for fast API */
+	rc = bcmtr_swq_send(device, URGENT_CHANNEL, buf);
+
+    if (rc)
+    {
+        BCMOS_TRACE_ERR("bcmtr_pcie_send failed. Error %s (%d)\n", bcmos_strerror(rc), rc);
+        bcmos_buf_free(buf);
+        return BCM_ERR_PARM;
+    }
+    /* buffer will be freed automatically once it is transmitted */
+    return BCM_ERR_OK;
+}
+
+/* RX callback to handle messages from embedded -> host */
+void bcmolt_coop_dba_rx(bcmolt_devid device, bcmos_buf *sysb, bcmtrmux_channel channel, void *data)
+{
+    bcmos_errno err;
+    bcm_common_fast_get_stats_ind *msg_p;
+
+    unsigned char *buf_data = bcmos_buf_data(sysb);
+    uint32_t i;
+
+    /* calling host application callback to handle the statistics message from the embedded */
+
+    /* the host is expecting a message of type:
+     * typedef struct
+		{
+			bcmolt_xgpon_ni_fast_get_stats_ind_hdr hdr;
+			bws_dba_get_alloc_stat alloc_stats_list[BCM_COMMON_GPON_FAST_API_LIST_SIZE];
+		}bcmolt_xgpon_ni_fast_get_stats_ind;
+     */
+
+    msg_p = (bcm_common_fast_get_stats_ind *)buf_data;
+    /* Fixing endianness and  creating a message. */
+    /* pon_ni us uint8 no need to fix endianness */
+    msg_p->hdr.cycle_num = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->hdr.cycle_num);
+    msg_p->hdr.available_bw = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->hdr.available_bw);
+    msg_p->hdr.num_of_allocs = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->hdr.num_of_allocs);
+    for (i = 0; i < msg_p->hdr.num_of_allocs; i++)
+    {
+    	msg_p->alloc_stats_list[i].alloc_id = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U16, msg_p->alloc_stats_list[i].alloc_id);
+    	msg_p->alloc_stats_list[i].allocated = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->alloc_stats_list[i].allocated);
+    	msg_p->alloc_stats_list[i].used = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->alloc_stats_list[i].used);
+    	msg_p->alloc_stats_list[i].status_report = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, msg_p->alloc_stats_list[i].status_report);
+    }
+    if (host_handler_array[device])
+    {
+    	err = host_handler_array[device](device, msg_p);
+    	BUG_ON(err);
+    }
+    else
+    {
+    	bcmos_free(sysb);
+    }
+}
+
+bcmos_errno bcmolt_coop_dba_init(bcmolt_devid devid, bcmtrmux_channel *channel, f_bcmtr_rx_handler rx_handler, void *data)
+{
+	bcmos_errno rc;
+
+	rc = bcmtrmux_channel_register(devid, channel, rx_handler, data);
+    if (rc != BCM_ERR_OK)
+    {
+        bcmos_printf("%s: can't register channel %d for device %u. rc=%d\n", __FUNCTION__, *channel, devid, (int)rc);
+        goto exit;
+    }
+	rc = bcmtr_swq_tx_queue_cfg(devid, BCMTR_PCIE_PRTY_NORMAL, BCMOLT_COOP_DBA_HARDQ_SIZE, BCMOLT_COOP_DBA_SOFTQ_SIZE);
+	if (rc != BCM_ERR_OK)
+	{
+		bcmos_printf("%s: can't configure tx queue channel %d for device %u. rc=%d\n", __FUNCTION__, devid, (int)rc);
+		goto exit;
+
+	}
+	return rc;
+
+exit:
+	bcmolt_coop_dba_exit(devid, *channel);
+	return rc;
+}
+
+void bcmolt_coop_dba_exit(bcmolt_devid devid, bcmtrmux_channel channel)
+{
+	bcmtrmux_channel_unregister(devid, channel);
+}
+
+void bcmolt_coop_dba_host_handler_register(bcmolt_devid devid, bcmolt_coop_dba_host_handler_cb host_handler)
+{
+	host_handler_array[devid] = host_handler;
+}
+
+void bcmolt_coop_dba_host_handler_unregister(bcmolt_devid devid)
+{
+	host_handler_array[devid] = NULL;
+}
diff --git a/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.h b/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.h
new file mode 100644
index 0000000..83ca4ec
--- /dev/null
+++ b/bcm68620_release/release/host_driver/coop_dba/bcmolt_coop_dba.h
@@ -0,0 +1,49 @@
+/*
+<:copyright-BRCM:2016:DUAL/GPL:standard
+
+   Broadcom Proprietary and Confidential.(c) 2016 Broadcom
+   All Rights Reserved
+
+Unless you and Broadcom execute a separate written software license
+agreement governing use of this software, this software is licensed
+to you under the terms of the GNU General Public License version 2
+(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
+with the following added to such license:
+
+   As a special exception, the copyright holders of this software give
+   you permission to link this software with independent modules, and
+   to copy and distribute the resulting executable under terms of your
+   choice, provided that you also meet, for each linked independent
+   module, the terms and conditions of the license of that module.
+   An independent module is a module which is not derived from this
+   software.  The special exception does not apply to any modifications
+   of the software.
+
+Not withstanding the above, under no circumstances may you combine
+this software in any way with any other Broadcom software provided
+under a license other than the GPL, without Broadcom's express prior
+written consent.
+
+:>
+ */
+
+#ifndef _BCMOLT_COOP_DBA_H_
+#define _BCMOLT_COOP_DBA_H_
+
+#include <bcmolt_msg.h>
+#include <bcm_common_gpon_fast.h>
+#include <bcmolt_tr_mux.h>
+
+#define BCMOLT_COOP_DBA_HARDQ_SIZE 4
+#define BCMOLT_COOP_DBA_SOFTQ_SIZE 0
+
+typedef bcmos_errno (*bcmolt_coop_dba_host_handler_cb)(bcmolt_devid device, bcm_common_fast_get_stats_ind *data);
+
+bcmos_errno bcmolt_fast_oper_set_allocs(bcmolt_devid device, uint8_t pon_ni, bcm_common_fast_set_allocs_msg *msg);
+bcmos_errno bcmolt_fast_oper_set_accesses(bcmolt_devid device, uint8_t pon_ni, bcm_common_fast_set_accesses_msg *msg);
+void bcmolt_coop_dba_exit(bcmolt_devid devid, bcmtrmux_channel channel);
+bcmos_errno bcmolt_coop_dba_init(bcmolt_devid devid, bcmtrmux_channel *channel, f_bcmtr_rx_handler rx_handler, void *data);
+void bcmolt_coop_dba_host_handler_register(bcmolt_devid devid, bcmolt_coop_dba_host_handler_cb host_handler);
+void bcmolt_coop_dba_host_handler_unregister(bcmolt_devid devid);
+void bcmolt_coop_dba_rx(bcmolt_devid device, bcmos_buf *sysb, bcmtrmux_channel channel, void *data);
+#endif