#!/usr/bin/python

# Copyright C 2002-2013 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

usage = ("isi_lox_info - dump LOX card information\nOptions:\n"
    "-d\tDebug mode - print PCI config space information\n"
    "-h\tHelp - print this message")

import sys
import os
import getopt
import isi.app.lib.procs as procs

# Be sure to capture stdout and stderr of commands, and then re-print.
# Isi_gather_info will cause interleaving if some output if child processes
# do their own printing to stdout
def print_cmd(command):
    err, out = procs.get_cmd_output(command)
    if err:
        print "Error: %s"% err
    for line in out:
        print line

def gather_tribble_logs():
# Fetch LOG page base address
    reg_addr = None
    err, out = procs.get_cmd_output("/sbin/nvmecontrol readdnet -T 0x1030 -n 0x1 lnv0")
    if err:
        print "Error accessing Log page base address: %s" % err
    else:
        for line in out:
            if "DNET reg 0x1030 =" in line:
                reg_addr = (line.split("DNET reg 0x1030 = "))[1]
                break
    # Gather all logs at base address
    if reg_addr:
        print_cmd("/sbin/nvmecontrol readdnet -T %s -n 0x8400 lnv0" % reg_addr)


debug = False

opts, args = getopt.getopt(sys.argv[1:], "dh")

for opt in opts:
    if opt[0] == '-d':
        debug = True
    if opt[0] == '-h':
        print usage
        sys.exit(0)

# Print basic information on all calls
print "OneFS Version and node name:"
print_cmd("uname -a")

print "\nlnv devices:"
print_cmd("/bin/ls /dev/lnv*")
print "\nmn devices:"
print_cmd("/bin/ls /dev/mn*")
print "\nnv devices:"
print_cmd("/bin/ls /dev/nv*")

if debug:
    # XXX Bug 119488 - dump extra debug register reads.  The set of register reads
    # and their arguments below are specific to current firmware, and could change.
    # We'll want to either remove these once they are no longer relevant, or come up
    # with a way to programmatically determine exactly which portions of config space
    # to read.
    print "\nCheck LOx NVME is enabled and ready:"
    print_cmd("/sbin/nvmecontrol readnvme -t 0x14 lnv0")
    print_cmd("/sbin/nvmecontrol readnvme -t 0x1c lnv0")

    print "\nPrinceton errors reported :"
    # Princeton errors reported
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60800 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60804 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60808 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x6080C -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60880 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60884 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60888 -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x6088C -c 3 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60144 -c 0x54 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60480 -c 0x54 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60488 -c 0x54 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60490 -c 0x54 lnv0")

    # MI2C health
    print "\nMI2C health:"
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60004 -c 0x23 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60400 -c 0x23 lnv0")
    print_cmd("/sbin/nvmecontrol readcsr -t 0x60400 -c 0x23 lnv0")

    # Superblock
    print "\nSuperblock:"
    print_cmd("/sbin/nvmecontrol readdnet -T 0x0 -n 0x200 lnv0")

    print "\nTribble logs:"
    gather_tribble_logs()

