Commit 503100cc authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

improved switching procedures and error detection

parent 07ce71d3
......@@ -5,10 +5,13 @@ from __future__ import division
import x393sata
import x393_mem
import sys
import time
from time import sleep
LOGFILE = "/var/log/x393sata_control.log"
# constants
RESET_LIMIT = 10
def colorize(string, color, bold):
color=color.upper()
attr = []
......@@ -54,6 +57,7 @@ def log_msg(msg, mode=0):
print (colorize("[%8.2f] %s: "%(t, sys.argv[0].split('/')[-1].split('.')[0]),'CYAN',0)+msg)
def connection_errors():
result = True
skip0 = True
MAXI1_ADDR = 0x80000000
......@@ -77,29 +81,30 @@ def connection_errors():
if fld_value or not skip0:
log_msg("%8x : %s (%s)"%(fld_value, fld['name'], fld['description'] ))
# the device is there but fails to establish a correct link
if fld['name']=="DIAG.B" or fld['name']=="DIAG.S":
if fld['name']=="DIAG.B" or fld['name']=="DIAG.S" or fld['name']=="ERR.E":
result = False
return result
def reset_device():
result = False
#sata.reset_device()
sata.reset_ie()
for i in range(reset_limit):
if not connection_errors():
log_msg("connection error ("+str(i)+"), resetting device",4)
sata.reset_device()
sata.reset_ie()
sata.reset_device()
sleep(0.5)
else:
if i!=0:
log_msg("resetting device: success")
result = True
break
time.sleep(1)
return result
mem = x393_mem.X393Mem(0,0,1)
sata = x393sata.x393sata() # 1,0,"10389B")
......@@ -108,7 +113,6 @@ if len(sys.argv) > 1:
else:
cmd = "donothing"
reset_limit = 10
if cmd == "set_zynq_ssd":
sata.vsc3304.disconnect_all()
......
......@@ -9,6 +9,7 @@ import subprocess
import sys
import time
import os
import re
from time import sleep
......@@ -18,6 +19,7 @@ LOGFILE = "/var/log/x393sata_control_test.log"
RESET_LIMIT = 10
DRIVER_RELOAD_LIMIT = 5
DRIVER_WAIT_TIME = 10
DRIVER_UNLOAD_TRIES = 30
def colorize(string, color, bold):
color=color.upper()
......@@ -125,6 +127,65 @@ def reset_device():
return result
def load_ahci_elphel_driver():
shout("modprobe ahci_elphel &")
shout("sleep 2")
shout("echo 1 > /sys/devices/soc0/amba@0/80000000.elphel-ahci/load_module")
log_msg("AHCI driver loaded")
def unload_ahci_elphel_driver():
for i in range(DRIVER_UNLOAD_TRIES):
unmount_partitions()
try:
output = subprocess.check_output(["rmmod","ahci_elphel"],stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
output = [x.strip() for x in e.output.split(":")]
if output[-1]=="Resource temporarily unavailable":
log_msg("Tried to unload driver "+str(i)+": "+output[-1])
if i==(DRIVER_UNLOAD_TRIES-1):
log_msg("AHCI driver unloading timeout")
sleep(2)
else:
log_msg("AHCI driver unloaded")
break
else:
log_msg("AHCI driver unloaded")
break
def unmount_partitions():
with open("/proc/mounts") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split(" ")[0] for x in content]
for mounted_device in content:
m = re.search(r"\/dev\/sd[a-z][0-9]",mounted_device)
if m:
log_msg("Unmounting "+m.group(0))
shout("umount "+m.group(0))
def check_device():
with open("/proc/partitions") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split(" ")[-1] for x in content]
result = False
for device in content:
m = re.search(r"sd[a-z]",device)
if m:
result = True
break
return result
def test1():
for i in range(100):
log_msg("TEST1 ("+str(i)+")",2)
......@@ -160,7 +221,7 @@ def test2():
def test3():
connection_error()
for i in range(100):
log_msg("TEST3 ("+str(i)+")",2)
log_msg("TEST3 ("+str(i)+")",3)
sata.vsc3304.disconnect_all()
sata.reset_ie()
......@@ -180,34 +241,37 @@ def test3():
connection_error()
def run_test4():
if os.path.ismount("/mnt/sda1"):
shout("umount /mnt/sda1")
shout("rmmod ahci_elphel")
unload_ahci_elphel_driver()
sata.reset_ie()
#sata.reset_device()
sleep(0.1)
connection_error()
shout("modprobe ahci_elphel &")
shout("sleep 2")
shout("echo 1 > /sys/devices/soc0/amba@0/80000000.elphel-ahci/load_module")
load_ahci_elphel_driver()
sleep(DRIVER_WAIT_TIME)
shout("sleep "+str(DRIVER_WAIT_TIME))
result = check_device()
if os.path.ismount("/mnt/sda1"):
# one more try
if not result:
log_msg(colorize("SSD was not detected: waiting for another "+str(DRIVER_WAIT_TIME)+" seconds",'YELLOW',True))
sleep(DRIVER_WAIT_TIME)
result = check_device()
if result:
log_msg(colorize("PASS",'GREEN',True))
result = True
else:
log_msg(colorize("FAIL",'RED',True))
result = False
return result
def test4():
errcounter = 0
for i in range(100):
log_msg("TEST "+str(i)+", failed: "+str(errcounter),2)
log_msg("TEST "+str(i)+", failed: "+str(errcounter),3)
if not run_test4():
errcounter = errcounter + 1
......
......@@ -9,6 +9,7 @@ import subprocess
import sys
import time
import os
import re
from time import sleep
......@@ -19,6 +20,7 @@ STATEFILE = "/var/state/ssd"
RESET_LIMIT = 10
DRIVER_RELOAD_LIMIT = 5
DRIVER_WAIT_TIME = 10
DRIVER_UNLOAD_TRIES = 30
def colorize(string, color, bold):
color=color.upper()
......@@ -63,7 +65,7 @@ def log_msg(msg, mode=0):
print("[%8.2f] %s"%(t,msg),file=msg_file)
if bold or color:
msg = colorize(msg,color,bold)
print (colorize("[%8.2f] %s: "%(t, sys.argv[0].split('/')[-1].split('.')[0]),'CYAN',0)+msg)
print (colorize("[%8.2f] %s: "%(t, sys.argv[0].split('/')[-1].split('.')[0]),'GREEN',0)+msg)
def shout(cmd):
......@@ -119,6 +121,47 @@ def reset_device():
return result
def load_ahci_elphel_driver():
shout("modprobe ahci_elphel &")
shout("sleep 2")
shout("echo 1 > /sys/devices/soc0/amba@0/80000000.elphel-ahci/load_module")
log_msg("AHCI driver loaded")
def unload_ahci_elphel_driver():
for i in range(DRIVER_UNLOAD_TRIES):
unmount_partitions()
try:
output = subprocess.check_output(["rmmod","ahci_elphel"],stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
output = [x.strip() for x in e.output.split(":")]
if output[-1]=="Resource temporarily unavailable":
log_msg("Tried to unload driver "+str(i)+": "+output[-1])
if i==(DRIVER_UNLOAD_TRIES-1):
log_msg("AHCI driver unloading timeout")
sleep(2)
else:
log_msg("AHCI driver unloaded")
break
else:
log_msg("AHCI driver unloaded")
break
def unmount_partitions():
with open("/proc/mounts") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split(" ")[0] for x in content]
for mounted_device in content:
m = re.search(r"\/dev\/sd[a-z][0-9]",mounted_device)
if m:
log_msg("Unmounting "+m.group(0))
shout("umount "+m.group(0))
def load_driver():
......@@ -132,35 +175,39 @@ def load_driver():
log_msg("SATA failed, SSD was not mounted: reconnect SSD",2)
shout("echo 0 > "+STATEFILE)
else:
log_msg("SATA ok, SSD mounted after "+str(i)+" tries")
log_msg("SATA ok, SSD detected after "+str(i)+" tries")
shout("echo 1 > "+STATEFILE)
def check_device():
with open("/proc/partitions") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = [x.split(" ")[-1] for x in content]
result = False
for device in content:
m = re.search(r"sd[a-z]",device)
if m:
result = True
break
return result
def reload_driver(i):
if i!=0:
if os.path.ismount("/mnt/sda1"):
# driver was loaded?!
shout("umount /mnt/sda1")
shout("rmmod ahci_elphel")
unload_ahci_elphel_driver()
# check once
sata.reset_ie()
#sata.reset_device()
sleep(0.1)
connection_errors()
shout("modprobe ahci_elphel &")
sleep(2)
shout("echo 1 > /sys/devices/soc0/amba@0/80000000.elphel-ahci/load_module")
load_ahci_elphel_driver()
sleep(DRIVER_WAIT_TIME)
if os.path.ismount("/mnt/sda1"):
log_msg(colorize("OK",'GREEN',True))
result = True
else:
log_msg(colorize("FAIL",'RED',True))
result = False
result = check_device()
return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment