#!/usr/bin/env python
#
# We currently only need device drivers loaded for anything
# we do in here, so run this as early as possible.
#
# PROVIDE: isi_hw_init
# REQUIRE: isi_module_init isi_hal_init isi_psi_init

import sys
import os
import re
import shutil

import isi.app.lib.procs as procs
import isi.hw.bootstrap as bootstrap
import isi.hw.hal as hal
import isi.hw.psi as psi
import isi.hw.version as hwver
import isi.misc as misc
import isi.hw.hwmon.adaptivecooling as adaptivecooling

LSIEXPUTIL_CMDERR = 2
IPMIUTIL = '/usr/bin/ipmiutil'

def print_cmd(cmd):
    (error, output) = procs.get_cmd_output(cmd)
    for line in output:
        print line

def print_error(msg, out):
    print(msg)
    for l in out:
        print(l)

def sysctl(name, value):
    cmd = '%s=%s' % (name, value)
    misc.sysctlbyname(cmd)
    print '%s=%s' % (name, value)

def sysctl_known(name):
    cmd = 'sysctl %s' % name
    (error, output) = procs.get_cmd_output(cmd)
    return (error == 0)

def rescan():
    cmd = 'camcontrol rescan 0'
    print_cmd(cmd)

def enable_stall_timers():
    # Error frequency--relaxed since stalling is enabled
    sysctl('hw.disk_event.thresh.errfreq_max', 200)
    sysctl('hw.disk_event.thresh.errfreq_history', 2000)
    # Cause drv to place the drive in the stalled state on a single error
    sysctl('hw.disk_event.thresh.errfreq_single_act', 4)

    # Single slow I/O
    sysctl('hw.disk_event.thresh.slowacc_usec', 3500000)
    # Cause drv to place the drive in the stalled state
    sysctl('hw.disk_event.thresh.slowacc_single_act', 4)
    sysctl('hw.disk_event.thresh.slowacc_count', 0)
    sysctl('hw.disk_event.thresh.slowacc_act', 0)

    # Slow I/O frequency
    sysctl('hw.disk_event.thresh.slowfreq_usec', 150000)
    sysctl('hw.disk_event.thresh.slowfreq_max', 50)
    sysctl('hw.disk_event.thresh.slowfreq_history', 1300)
    # Cause drv to place the drive in the stalled state
    sysctl('hw.disk_event.thresh.slowfreq_act', 4)
    sysctl('hw.disk_event.thresh.slowfreq_single_act', 0)



def mpt_mfg_mode_check():
    """Check to see if booted with MPT Mfg Mode, based on kernel env.
       (See hw.check.lib.lsi.MptMfgModeCheck code)
       Note /boot/device.hints 'hint.mpt.0.mfg' line can set this kenv.
       Returns True if non-zero kenv hint.
       """
    hint = 'hint.mpt.0.mfg'
    numreg = re.compile('^-?\d+$')
    
    def check_num(line):
        return numreg.match(line) and int(line) != 0

    result = False
    cmd = ("kenv %s" % (hint))
    (error, output) = procs.get_cmd_output(cmd)
    if not error and len(output) == 1:
        result = check_num(output[0])
    return result

def process_ipmi_fw_entry(channel, lun, netfn, cmd):
    '''If the specified IPMI command is configurable then use the IPMI
       firewall to disable the command on the channel provided.'''

    # Grab the current configuration information for the specified
    # command.
    command = ("%s firewall info channel %d lun %d netfn 0x%x command 0x%x" %
               (IPMIUTIL, channel, lun, netfn, cmd))
    (error, output) = procs.get_cmd_output(command)
    if error != 0:
        print("failed to get info on channel %d lun %d netfn 0x%x command 0x%x" %
              (channel, lun, netfn, cmd))
        return

    # The ipmi firewall info command uses an 'X' to indicate if a
    # command is available (A), configurable (C) or enabled (E).  For
    # example from the ipmitool firewall command output:
    #
    # (A)vailable, (C)onfigurable, (E)nabled: | A | C | E |
    # -----------------------------------------------------
    # LUN 0, NetFn 0x30, Command 0x10:        | X | X |   |
    #
    # In the above output the command is available (A), configurable
    # (C) and _not_ enabled (E) since there is no X in the enabled
    # location.

    for line in output:
        # The output line of interest always starts with LUN.
        if line.startswith('LUN'):
            is_configurable = re.search(r"\| . \| X \| . \|", line.strip())
            # Only attempt to disable if the command is configurable.
            if not is_configurable:
                return

            is_disabled = re.search(r"\| . \| . \|   \|", line.strip())
            # No need to do anything if the command is already
            # disabled.
            if is_disabled:
                return

            # Disable the command on the specified channel.
            command = ("%s firewall disable channel %d lun %d netfn 0x%x command "
                       "0x%x" % (IPMIUTIL, channel, lun, netfn, cmd))
            (error, output) = procs.get_cmd_output(command)
            if error != 0:
                print("failed to disable command on channel %d lun %d netfn "
                      "0x%x command 0x%x" % (channel, lun, netfn, cmd));
                return
            break

def check_shutdown_timer():
    # Phobos Functional Specification: 6.5 Adaptive Cooling Commands
    shutdown_timer_get = '/usr/bin/isi_hwtools/isi_ipmicmc -a bmc -VR 3031'
    shutdown_timer_set = '/usr/bin/isi_hwtools/isi_ipmicmc -a bmc -VR 30302C01'
    err, out = procs.get_cmd_output(shutdown_timer_get)
    if not err and out and len(out) > 0:
        rsp = out[0].split(' ')
        if len(rsp) == 4 and rsp[1] == '00': # cmd + cc + rsp1 + rsp2
            try:
                if int(rsp[3] + rsp[2], 16) == 0: # check if disabled
                    err, out = procs.get_cmd_output(shutdown_timer_set)
                    if err:
                        print_error("Failed to set shutdown timer", out)
                    else:
                        print("Shutdown timer was disabled. Resetting back "
                            "to default value of 300s")
            except:
                print('Failed to parse shutdown timer value: %s' %
                    (rsp[3] + rsp[2]))
        else:
            print_error("Unexpected shutdown timer response", out)
    else:
        print_error("Failed to get shutdown timer value", out)

def ipmi_fw_config():
    '''Process IPMI firewall configuration entries.'''

    def valid_range(value, min_value, max_value):
        '''Return True if the value provided is within the range of min_value
           to max_value inclusive, otherwise return False.'''

        if min_value <= value <= max_value:
            return True

        return False

    # XXX Pipeline, consider having the IPMI Firewall configuration in
    # a separate configuration file.  The following IPMI commands will
    # be disabled on the channel specified on Infinity:
    entries = [{'channel':0x1, 'lun':0x0, 'netfn':0x06, 'command':0x60},
               {'channel':0x1, 'lun':0x0, 'netfn':0x30, 'command':0x10},
               {'channel':0x1, 'lun':0x0, 'netfn':0x30, 'command':0x11},
               {'channel':0x1, 'lun':0x0, 'netfn':0x30, 'command':0x12}]

    for ipmi_cmd in entries:
        # Basic sanity checking.  The min/max Channel values come from
        # the V2 IPMI Specification, see Table 6-1 Channel Number
        # Assignments.
        if not valid_range(ipmi_cmd['channel'], 0, 0xf):
            print("IPMI Firewall entry has invalid channel: 0x%x" %
                  ipmi_cmd['channel'])
            continue
        # The LUN is 2-bits in size and the min/max values come from
        # the V2 IPMI Specification, see section 5.1 Network Function
        # Codes, section 19.3 Logical Unit Numbers (LUNs) for Commands
        # and section 7.2 BMC IPMB LUNs.
        if not valid_range(ipmi_cmd['lun'], 0, 0x3):
            print("IPMI Firewall entry has invalid lun: 0x%x" %
                  ipmi_cmd['lun'])
            continue
        # The min/max NetFn values come from the V2 IPMI
        # Specification, see Table 5-1 Network Function Codes.
        if not valid_range(ipmi_cmd['netfn'], 0, 0x3f):
            print("IPMI Firewall entry has invalid netfn: 0x%x" %
                  ipmi_cmd['netfn'])
            continue
        # The min/max Command values come from the V2 IPMI
        # Specification, see Table 44-11 Command Number Assignments
        # and Privilege Levels.
        if not valid_range(ipmi_cmd['command'], 0, 0xff):
            print("IPMI Firewall entry has invalid command: 0x%x" %
                  ipmi_cmd['command'])
            continue
        process_ipmi_fw_entry(ipmi_cmd['channel'], ipmi_cmd['lun'],
                              ipmi_cmd['netfn'], ipmi_cmd['command'])

def node_marker_led_off():
    """Turn off the Infinity node marker LED."""

    # The Infinity node marker LED is controlled via an IPMI OEM extension.
    # See the Phobos Functional Specification section 6.15 "LED and
    # Expander Commands" for details.
    #
    # As for the raw IPMI command required to turn off the node marker
    # LED:
    #   NetFn and Command: 0x30 0xd0
    #   Command Params: FRU ID 0x0, LED type 0x0, color on 0x0, color off 0x0,
    #                   LED off 0x0
    #
    # Now bend the raw command into shape for ipmiutil, the bus is 00,
    # rsSa is 20, NetFn/LUN becomes (0x30 << 2 | 0) = c0 followed by
    # the command (d0) and its parameters.
    command = "%s cmd 00 20 c0 d0 00 00 00 00 00" % IPMIUTIL
    (error, output) = procs.get_cmd_output(command)
    if error != 0:
        print "Failed to turn off the node marker LED."

def main(args):

    # Prior to clearing any error logs, check for adaptive cooling events
    # and block if appropriate
    if adaptivecooling.is_adaptive_cooling_boot():
        # For initial work, run in verbose mode. Turn off later.
        adaptivecooling.boot_handler(verbose=True)

    # Bug 200371:	Make sure to select the maximum available frequency
    #			from official levels when cpufreq is probed.
    cmd = 'sysctl -n dev.cpu.0.freq_levels'
    (lerror, levels) = procs.get_cmd_output(cmd)
    cmd = 'sysctl -n dev.cpu.0.freq'
    (aerror, afreq) = procs.get_cmd_output(cmd)
    if (lerror == 0 and aerror == 0):
        levels = levels[0].split('/', 1)
        maxfreq = levels[0]
        freq_enabled = (int(levels[1].split(' ', 1)[0]) > 0)
        if (freq_enabled and int(maxfreq) > int(afreq[0])):
            sysctl('dev.cpu.0.freq', int(maxfreq))

    ## For LSI/PMC expanders, we need to clear out the phy error log at boot
    ## time, as a reboot can cause the SAS HBA to reset, logging errors
    ## on the expander phys.
    ##
    ## Eventually this should probably just be a call to something like
    ## isi_exputil that can generically handle whatever expander we may
    ## have.  For now, add a cdesexputil call for Colossus nodes.
    if hal.supportsSasPhyMonitoring():
        if hal.supportsLsiExpander():
            cmd = '/usr/bin/isi_hwtools/isi_lsiexputil -pC all'
            (error, output) = procs.get_cmd_output(cmd)
            if (error != 0):
                for line in output:
                    print line
            cmd = '/usr/bin/isi_hwtools/isi_lsiexputil -pC all e1'
            (error, output) = procs.get_cmd_output(cmd)
            # XXX: exit status 2 indicates we have no second LSI expander
            # (bad argument error from the lsiexputil), if we have
            # encountered an error besides that, print it out to console.
            # This code should probably not be hardware-specific to this
            # type of expander, HAL should abstract this
            exit_status = os.WEXITSTATUS(error)
            if (exit_status != LSIEXPUTIL_CMDERR and exit_status != 0):
                for line in output:
                    print line
        if hal.supportsCDES():
            cmd = '/usr/bin/isi_hwtools/isi_cdesexputil -pC all'
            (error, output) = procs.get_cmd_output(cmd)
            if (error != 0):
                for line in output:
                    print line

    #------------------------------
    # Note:  hw.disk_event sysctls and the schemes they represent are discussed in
    # https://svn/view/*checkout*/tech-doc/head/DDD/drive_timeout_improvements/drive_thresh_design.pdf
    #------------------------------

    #------------------------------
    # Card-specific actions
    #------------------------------
    dskctl_family = hal.getDskctlFamily()
    if ((dskctl_family == hwver.DISKCTL_FAMILY_LSI) and
          (hal.getDskctlType() != hwver.DISKCTL_LSI_VMWARE) and
          not bootstrap.isInfinityMotherboard()):
        # Turn on rebooting if the LSI device crashes
        if sysctl_known('hw.mpt0.reboot_on_fault'):
            sysctl('hw.mpt0.reboot_on_fault', 1)

        # Infinity case handled in below block
        enable_stall_timers()

        # Rescan the bus to flush out any possible boot-time error conditions,
        # enable reading the SMART data during probes, and then rescan once
        # more to read it.
        # NOTE for Mfg FUT CFs (and any other boot that includes the line
        # "hint.mpt.0.mfg=1" in boot/device.hints, or has kernel env var set),
        # we shouldn't be doing any rescan()'s, since driver in MPT Mfg mode
        # won't do PortEnable's and we won't see the drives anyway, and we
        # will get boot errors:
        # camcontrol: CAMIOCOMMAND ioctl failed: Invalid argument
        mfg_mode = mpt_mfg_mode_check()
        if not mfg_mode:
            rescan()
        sysctl('kern.cam.probe_smart_data', 1)
        if not mfg_mode:
            rescan()
        sysctl('kern.cam.lost_cache_downs_drive', 1)
    else:
        sysctl('hw.disk_event.thresh.slowacc_usec', 0)
        sysctl('hw.disk_event.thresh.slowacc_single_act', 0)

    #------------------------------
    # Platform-specific actions
    #------------------------------

    # L3 does not cache data on the HD and high cap NL platforms
    if (hal.supportsL3MetaOnlyCaching()):
        sysctl('efs.l3.evict.IDI_CLASS_UNPROTECTED', 0)
    else:
        sysctl('efs.l3.evict.IDI_CLASS_UNPROTECTED', 1)

    # In order to communicate with sattelite controllers on Infinity
    # we have to send the Enable Message Channel Receive command to the BMC
    # Details regarding this command is in section 22.5 of the IPMI 2.0 spec.
    # Also, we need to configure the multifunction button to pulse the PCH
    # power button input on short press, and power off on long press.
    # Details for this can be found in section 6.10.3 of the Phobos Functional
    # Spec.
    if bootstrap.isInfinityMotherboard():
        procs.run_cmd('/usr/bin/isi_hwtools/isi_ipmicmc -a bmc -R 06320001')
        procs.run_cmd('/usr/bin/isi_hwtools/isi_ipmicmc -a bmc -R 308206060404')
        check_shutdown_timer()
        # Skip WCE check until GDE releases SATA drives that are WCD by default.
        key = "hw.drives.sata.skip_wce_check"
        val = None if (psi.setInt(key, 1) == -1) else psi.getInt(key)
        print "Infinity psi set: %s = %d" % (key, 0 if val is None else val)
        # shutdown.py sets this as well, but set it here in case something
        # decides to call shutdown_real directly
        sysctl('hw.ipmi.poweroff', 1)
        isi_first_boot_file = '/etc/ifs/isi_first_boot_already_ran'
        if os.path.exists(isi_first_boot_file):
            # defer setting the sysctls until 'isi_radish -e' has run by newfs
            # enable isi_chkcache_done to clear wce on drive resets
            sysctl('kern.cam.handle_resets', 1)
            sysctl('kern.cam.clear_wce_on_resets', 1)
            sysctl('kern.cam.lost_cache_downs_drive', 1)
        sysctl('kern.cam.force_tur_probe', 1)
        # Ensure that infinity nodes of whichever family have stall timers set
        enable_stall_timers()

        # RFD 202500 - set our resets-before-removal back from 0
        if (dskctl_family == hwver.DISKCTL_FAMILY_PMC):
            sysctl('dev.pmspcv.0.resets_to_remove', 5);

        # Enforce queue depth of 4 for Catamaran nodes which don't support
        # zoning (bugs 188574). PMC SIM driver sets the depth on its own,
        # which is internally constrained to a max openings of 3 by CAM.
        # XXX PSI PIPELINE: 'ses0' device id should come from the PSI config
        (error, output) = procs.get_cmd_output('camcontrol tags ses0 -N 3')
        if error:
            print ('ERROR: unable to set ses0\'s dev_openings to 3')
        else:
            print ('Set ses0\'s dev_openings to 3')
        # Configure the IPMI Firewall.
        ipmi_fw_config()
        # We are up far enough to turn off the Infinity node marker LED.
        node_marker_led_off()

    # Fix "Correctable Error Threshold" BIOS setting on Gen5 platforms
    mobo = hal.getMotherboardType()
    if mobo in [hwver.MOBO_INTELS1400FP, hwver.MOBO_INTELS2600CP]:
        cmd = ('/usr/bin/isi_hwtools/syscfg /d biossettings "Correctable '
               'Error Threshold"')
        (error, output) = procs.get_cmd_output(cmd)
        if 'Current Value : ALL' not in output:
            cmd = ('/usr/bin/isi_hwtools/syscfg /bcs "" "Correctable Error '
                   'Threshold" 1')
            error = procs.run_cmd(cmd)
            if error:
                print ('ERROR: Unable to set "Correctable Error Threshold" '
                       'BIOS setting')
            else:
                print ('Rebooting to apply change to "Correctable Error '
                       'Threshold" BIOS setting')
                cmd = '/sbin/shutdown -r now'
                error = procs.run_cmd(cmd)
                if error:
                    print ('ERROR: Unable to reboot to apply change to '
                           '"Correctable Error Threshold" BIOS setting')
        else:
            # Save a copy of all settings for later analysis
            bios_log = '/var/log/bios_settings.ini'
            if os.path.exists(bios_log):
                os.unlink(bios_log)
            cmd = '/usr/bin/isi_hwtools/syscfg /s %s /b /f' % bios_log
            error = procs.run_cmd(cmd)
            if error:
                print 'ERROR: Unable to save BIOS settings to %s' % bios_log
        # syscfg litters in /var/log, clean that up
        if os.path.exists('/var/log/CacheRiver'):
            shutil.rmtree('/var/log/CacheRiver')

    #------------------------------
    # Card-agnostic actions
    #------------------------------

    # Set disk event thresholds
    sysctl('hw.disk_event.thresh.ecclist_max', 30)
    sysctl('hw.disk_event.thresh.ignore_idle_usec', 1000000)
    sysctl('hw.disk_event.thresh.access_avg_history', 1000)
    sysctl('hw.disk_event.thresh.access_hi_history', 1000)

    # Set disk event actions
    # 2 - "softfail"
    # 4 - set the abort_bios sysctl for the drive (put the drive into the 'stalled' state in ifs/drv)
    sysctl('hw.disk_event.thresh.smart_thresh_act', 2)
    sysctl('hw.disk_event.thresh.udma_downgrade_act', 2)
    sysctl('hw.disk_event.thresh.udma_upgrade_act', 2)
    sysctl('hw.disk_event.thresh.ecclist_act', 2)
    sysctl('hw.disk_event.thresh.errfreq_act', 2)

    # Bug 101620: Accelerator-III nodes are built at FoxConn Technology
    #             and do not go through the Isilon manufacturing process
    #             and the same is true for the spare drives for ACC-III.
    #             Therefore, in order to make sure that the drives have
    #             the optimal settings turned on, we must force them to
    #             be set now. We don't specifically do this on just
    #             ACC-III since setting the optimal settings on the older
    #             Accelerator-X nodes is essentially a NOOP since those
    #             nodes and their spare drives do go through Isilon
    #             manufacturing and do get their optimal settings turned on.
    # Bug 107938: Similar to the Accelerator-III, vOneFS nodes will not
    #             go through Isilon manufacturing so we need to ensure WCE
    #             is set for each of the drives.
    is_accelerator = (
        hal.isClass(hal.PERFORMANCE_ACCELERATOR_CLASS) or
        hal.isClass(hal.BACKUP_ACCELERATOR_CLASS))
    if is_accelerator or hal.isVOneFsNode():
        procs.run_cmd('/usr/bin/isi_radish -e')

if __name__=='__main__':
    main(sys.argv)
