#!/bin/bash

# This script is called from slice_mgr to 
# detect the MS FPGA . this script will print 0 for success
# & error for other errors . this error will be passed to coherent
# process. Coherent FMEA should handle it.
#
# Copyright (c) 2016 by Cisco Systems, Inc.

. /etc/init.d/spirit_pd.sh

function set_plx_msfpga_port_capability(){
    if [ "$CARDINDEX" = 27012 ]; then
        platform_log "Taking care of Errata: changing the speed to $1"
        setpci -s 04:03.0 98.B=$1
        setpci -s 04:05.0 98.B=$1
        setpci -s 04:07.0 98.B=$1
    fi
}

function check_card_type () {
  init_iofpga_base_addr
  card_value=$(iofpga_reg_read 0x4)
  case "$card_value" in
   0x01505327)
     CARDINDEX="27013"
     ;;
   0x0011986C)
     CARDINDEX="27012"
     ;;
   esac
   platform_log "MSFPGA found $CARDINDEX"
}

function lxc_xr_lc_enable_msfpga()
{
    local num_msfpga=0
    local count=0
    local total_msfpga=2
    local fpga_version="10ee:8021"

    check_card_type

    # On 18 port MACSEC LC the power OFF and ON of DM2 during XR LXC reload 
    # causes the MSFPGA image loss we need to always check the status of 
    # MSFPGA using MIFPGA 0x40C status. If its not
    # 3ff then we need to reload MSFPGA using 0x408 writing 0x1 on it.
    if [ "$CARDINDEX" = "27013" ]; then
       fpga_version="1137:01c5";
       let total_msfpga=4
       local msfpga_conf=`/usr/sbin/pcimemread 0xd340040C 4 | grep ":" | grep 3ff | wc -l`
       if [ $msfpga_conf -le 0 ]; then
          # relaod MSFPGA and wait till it becomes stable
          /usr/sbin/pcimemwrite 0xd3400408  4  0x1 > /dev/null 2>&1
       fi 

       until [ $msfpga_conf -gt 0 ]; do
           msfpga_conf=`/usr/sbin/pcimemread 0xd340040C 4 | grep ":" | grep 3ff | wc -l`
           sleep 0.5
           let count=count+1
           if [ $count -gt 30 ]; then
             break
           fi
       done

       if [ $msfpga_conf -le 0 ]; then
          # MSFPGA NOT IN GOOD STATUS
          echo 2  
       fi 
    fi

    # We are reducing the PLX switch for all the MSFPGA port to 2.5 G
    # to take care of Erratta:
    # http://sjc5b-netapp-ns-web/local/snap_hw/c3/datasheets/plx/8619/PEX_8619_Errata_v2.0_4May12.pdf
    # Datasheet:
    # http://sjc5b-netapp-ns-web/local/snap_hw/c3/datasheets/plx/8619/PEX8619BA_Data_Book_v1.3_31Mar11.pdf
    # Here we are running into Erratta: 5 PHY Polling state machine can get stuck under noisy link
    set_plx_msfpga_port_capability 1

    let count=0
    num_msfpga=`lspci -nn | grep "$fpga_version" | cut -d " " -f 1 |wc -l`
    until [ $num_msfpga -gt $total_msfpga ]; do
        if [ "$CARDINDEX" = 27012 ]; then
            echo 1 > /sys/class/pci_bus/0000:07/rescan
            echo 1 > /sys/class/pci_bus/0000:08/rescan
            echo 1 > /sys/class/pci_bus/0000:09/rescan
        else
            echo 1 > /sys/class/pci_bus/0000:05/rescan
            echo 1 > /sys/class/pci_bus/0000:08/rescan 
            echo 1 > /sys/class/pci_bus/0000:09/rescan 
            echo 1 > /sys/class/pci_bus/0000:0a/rescan
            echo 1 > /sys/class/pci_bus/0000:0f/rescan
        fi

        num_msfpga=`lspci -nn | grep "$fpga_version" | cut -d " " -f 1 |wc -l`
        if [ $num_msfpga -le $total_msfpga ]; then
            sleep 0.5
        fi
        let count=count+1
        #Timeout increased to 3000 to support fallback. It covers cases
        #where image boots from primary when images in secondary and tertiary
        #are corrupted.
        if [ $count -gt 3000 ]; then
            break
        fi
    done

    if [ $num_msfpga -le 0 ]; then
       platform_log "NOT FOUND..MSFPGA"
       echo 1
       return 0
    fi
    platform_log "FOUND..MSFPGA"

    #After all the FPGAs are found switching back to 5 G speed
    set_plx_msfpga_port_capability 2

    ssh 10.0.2.16 '/etc/init.d/dev-allow default-sdr--1' > /dev/null 2>&1  
    echo 0
    return 0
}

lxc_xr_lc_enable_msfpga
