/************************************************************
 * <bsn.cl fy=2014 v=onl>
 *
 *           Copyright 2014 Big Switch Networks, Inc.
 *           Copyright 2016 Accton Technology Corporation.
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *        http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *
 * </bsn.cl>
 ************************************************************
 *
 *
 *
 ***********************************************************/
#include <onlp/platformi/sfpi.h>
#include <fcntl.h>
#include <unistd.h>
#include "platform_lib.h"

#include "x86_64_accton_csp9250_log.h"

#define NUM_OF_SFP_PORT 54
#define MAX_SFP_PATH 	64
#define QSFP_START_PORT 48
static char sfp_node_path[MAX_SFP_PATH] = {0};
#define FRONT_PORT_BUS_INDEX(port) (port+17)
#define FRONT_QSFP_PORT_BUS_INDEX(port) (port-39)

static char*
sfp_get_port_path_addr(int port, int addr, char *node_name)
{
	  if(port < QSFP_START_PORT)
        sprintf(sfp_node_path, "/sys/bus/i2c/devices/%d-00%d/%s",
                           FRONT_PORT_BUS_INDEX(port), addr, node_name);
    else
    	  sprintf(sfp_node_path, "/sys/bus/i2c/devices/%d-00%d/%s",
                           FRONT_QSFP_PORT_BUS_INDEX(port), addr, node_name);
    return sfp_node_path;
}

static char*
sfp_get_port_path(int port, char *node_name)
{
    return sfp_get_port_path_addr(port, 50, node_name);
}

/************************************************************
 *
 * SFPI Entry Points
 *
 ***********************************************************/
int
onlp_sfpi_init(void)
{
    /* Called at initialization time */    
    return ONLP_STATUS_OK;
}

int
onlp_sfpi_bitmap_get(onlp_sfp_bitmap_t* bmap)
{
    /*
     * Ports {0, 54}
     */
    int p;    
    AIM_BITMAP_CLR_ALL(bmap);
    for(p = 0; p < NUM_OF_SFP_PORT; p++) {
        AIM_BITMAP_SET(bmap, p);
    }

    return ONLP_STATUS_OK;
}

int
onlp_sfpi_is_present(int port)
{
    /*
     * Return 1 if present.
     * Return 0 if not present.
     * Return < 0 if error.
     */
    int present;
    char *path = sfp_get_port_path(port, "sfp_is_present");

    if (onlp_file_read_int(&present, path) < 0) {
        AIM_LOG_ERROR("Unable to read present status from port(%d)\r\n", port);
        return ONLP_STATUS_E_INTERNAL;
    }
    
    return present;
}

int
onlp_sfpi_presence_bitmap_get(onlp_sfp_bitmap_t* dst)
{
    uint32_t bytes[7];
    char* path;
    FILE* fp;

    AIM_BITMAP_CLR_ALL(dst);

    path = sfp_get_port_path(0, "sfp_is_present_all");
    fp = fopen(path, "r");
    if(fp == NULL) {
        AIM_LOG_ERROR("Unable to open the sfp_is_present_all device file.");
        return ONLP_STATUS_E_INTERNAL;
    }
    int count = fscanf(fp, "%x %x %x %x %x %x %x",
                       bytes+0,
                       bytes+1,
                       bytes+2,
                       bytes+3,
                       bytes+4,
                       bytes+5,
                       bytes+6
                       );
    fclose(fp);
    if(count != AIM_ARRAYSIZE(bytes)) {
        /* Likely a CPLD read timeout. */
        AIM_LOG_ERROR("Unable to read all fields from the sfp_is_present_all device file.");
        return ONLP_STATUS_E_INTERNAL;
    }

    /* Mask out non-existant QSFP ports */
    bytes[6] &= 0x3F;

    /* Convert to 64 bit integer in port order */
    int i = 0;
    uint64_t presence_all = 0 ;
    for(i = AIM_ARRAYSIZE(bytes)-1; i >= 0; i--) {
        presence_all <<= 8;
        presence_all |= bytes[i];
    }

    /* Populate bitmap */
    for(i = 0; presence_all; i++) {
        AIM_BITMAP_MOD(dst, i, (presence_all & 1));
        presence_all >>= 1;
    }

    return ONLP_STATUS_OK;
}

int
onlp_sfpi_rx_los_bitmap_get(onlp_sfp_bitmap_t* dst)
{
    uint32_t bytes[6];
    char* path;
    FILE* fp;

    path = sfp_get_port_path(0, "sfp_rx_los_all");
    fp = fopen(path, "r");

    if(fp == NULL) {
        AIM_LOG_ERROR("Unable to open the sfp_rx_los_all device file.");
        return ONLP_STATUS_E_INTERNAL;
    }
    int count = fscanf(fp, "%x %x %x %x %x %x",
                       bytes+0,
                       bytes+1,
                       bytes+2,
                       bytes+3,
                       bytes+4,
                       bytes+5
                       );
    fclose(fp);
    if(count != 6) {
        AIM_LOG_ERROR("Unable to read all fields from the sfp_rx_los_all device file.");
        return ONLP_STATUS_E_INTERNAL;
    }

    /* Convert to 64 bit integer in port order */
    int i = 0;
    uint64_t rx_los_all = 0 ;
    for(i = 5; i >= 0; i--) {
        rx_los_all <<= 8;
        rx_los_all |= bytes[i];
    }

    /* Populate bitmap */
    for(i = 0; rx_los_all; i++) {
        AIM_BITMAP_MOD(dst, i, (rx_los_all & 1));
        rx_los_all >>= 1;
    }

    return ONLP_STATUS_OK;
}

int
onlp_sfpi_eeprom_read(int port, uint8_t data[256])
{
    char* path = sfp_get_port_path(port, "sfp_eeprom");

    /*
     * Read the SFP eeprom into data[]
     *
     * Return MISSING if SFP is missing.
     * Return OK if eeprom is read
     */
    memset(data, 0, 256);
    
    if (onlp_file_read_binary(path, (char*)data, 256, 256) != 0) {
        AIM_LOG_ERROR("Unable to read eeprom from port(%d)\r\n", port);
        return ONLP_STATUS_E_INTERNAL;
    }

    return ONLP_STATUS_OK;
}

int
onlp_sfpi_dom_read(int port, uint8_t data[256])
{
    char* path = sfp_get_port_path_addr(port, 51, "sfp_eeprom");
    memset(data, 0, 256);

    if (onlp_file_read_binary(path, (char*)data, 256, 256) != 0) {
        AIM_LOG_INFO("Unable to read eeprom from port(%d)\r\n", port);
        return ONLP_STATUS_E_INTERNAL;
    }

    return ONLP_STATUS_OK;
}

int
onlp_sfpi_control_set(int port, onlp_sfp_control_t control, int value)
{
    int rv;

    switch(control)
        {
        case ONLP_SFP_CONTROL_TX_DISABLE:
            {
                /* QSFP tx_disable that need to set eeprom.
                 * So if present=0. So not need to set
                 */
                if(port >=QSFP_START_PORT && port < NUM_OF_SFP_PORT)
                {
                    rv = ONLP_STATUS_OK;
                    if(!onlp_sfpi_is_present(port))
                    {
                        break;                     
                    }                    
                    break;
                }

                char* path = sfp_get_port_path(port, "sfp_tx_disable");
                if (onlp_file_write_integer(path, value) != 0) {
                    AIM_LOG_ERROR("Unable to set tx_disable status to port(%d)\r\n", port);
                    rv = ONLP_STATUS_E_INTERNAL;
                }
                else {
                    rv = ONLP_STATUS_OK;
                }
                break;
            }

        default:
            rv = ONLP_STATUS_E_UNSUPPORTED;
            break;
        }

    return rv;
}

int
onlp_sfpi_control_get(int port, onlp_sfp_control_t control, int* value)
{
    int rv;
    char* path = NULL;

    switch(control)
        {
        case ONLP_SFP_CONTROL_RX_LOS:
            {
                path = sfp_get_port_path(port, "sfp_rx_los");

                if (onlp_file_read_int(value, path) < 0) {
                    AIM_LOG_ERROR("Unable to read rx_los status from port(%d)\r\n", port);
                    rv = ONLP_STATUS_E_INTERNAL;
                }
                else {
                    rv = ONLP_STATUS_OK;
                }
                break;
            }

        case ONLP_SFP_CONTROL_TX_FAULT:
            {
                path = sfp_get_port_path(port, "sfp_tx_fault");

                if (onlp_file_read_int(value, path) < 0) {
                    AIM_LOG_ERROR("Unable to read tx_fault status from port(%d)\r\n", port);
                    rv = ONLP_STATUS_E_INTERNAL;
                }
                else {
                    rv = ONLP_STATUS_OK;
                }
                break;
            }

        case ONLP_SFP_CONTROL_TX_DISABLE:
            {
		
	       /* QSFP tx_disable that need to set eeprom.
                * So if present=0. So not need to set
                */
                if(port >=QSFP_START_PORT && port <= NUM_OF_SFP_PORT)
                {
                    if(!onlp_sfpi_is_present(port))
                    {
                        rv = ONLP_STATUS_OK;
                        *value =0;  
                        break;                   
                    }
                }
                path = sfp_get_port_path(port, "sfp_tx_disable");

                if (onlp_file_read_int(value, path) < 0) {
                    AIM_LOG_ERROR("Unable to read tx_disabled status from port(%d)\r\n", port);
                    rv = ONLP_STATUS_E_INTERNAL;
                }
                else {
                    rv = ONLP_STATUS_OK;
                }
                break;
            }

        default:
            rv = ONLP_STATUS_E_UNSUPPORTED;
        }

    return rv;
}


int
onlp_sfpi_denit(void)
{
    return ONLP_STATUS_OK;
}


