2014-11-12 12:20:34 -05:00
|
|
|
import subprocess
|
2014-12-14 14:30:01 -05:00
|
|
|
import math
|
2014-11-12 12:20:34 -05:00
|
|
|
|
2014-12-14 04:45:26 -05:00
|
|
|
from lib.sata.common import *
|
2014-12-05 11:48:01 -05:00
|
|
|
from lib.sata.test.common import *
|
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
def print_with_prefix(s, prefix=""):
|
|
|
|
if not isinstance(s, str):
|
|
|
|
s = s.__repr__()
|
|
|
|
s = s.split("\n")
|
|
|
|
for l in s:
|
|
|
|
print(prefix + l)
|
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
# PHY Layer model
|
2014-12-04 17:43:21 -05:00
|
|
|
class PHYDword:
|
2014-11-11 12:47:34 -05:00
|
|
|
def __init__(self, dat=0):
|
|
|
|
self.dat = dat
|
|
|
|
self.start = 1
|
|
|
|
self.done = 0
|
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class PHYSource(Module):
|
|
|
|
def __init__(self):
|
2014-12-14 04:52:56 -05:00
|
|
|
self.source = Source(phy_description(32))
|
2014-11-11 12:47:34 -05:00
|
|
|
###
|
2014-12-04 17:43:21 -05:00
|
|
|
self.dword = PHYDword()
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def send(self, dword):
|
|
|
|
self.dword = dword
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
2014-12-02 15:34:16 -05:00
|
|
|
selfp.source.stb = 1
|
|
|
|
selfp.source.charisk = 0b0000
|
|
|
|
for k, v in primitives.items():
|
|
|
|
if v == self.dword.dat:
|
|
|
|
selfp.source.charisk = 0b0001
|
|
|
|
selfp.source.data = self.dword.dat
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class PHYSink(Module):
|
|
|
|
def __init__(self):
|
2014-12-14 04:52:56 -05:00
|
|
|
self.sink = Sink(phy_description(32))
|
2014-11-11 12:47:34 -05:00
|
|
|
###
|
2014-12-04 17:43:21 -05:00
|
|
|
self.dword = PHYDword()
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
self.dword.done = 0
|
|
|
|
while self.dword.done == 0:
|
|
|
|
yield
|
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
|
|
|
self.dword.done = 0
|
|
|
|
selfp.sink.ack = 1
|
|
|
|
if selfp.sink.stb == 1:
|
|
|
|
self.dword.done = 1
|
|
|
|
self.dword.dat = selfp.sink.data
|
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class PHYLayer(Module):
|
2014-12-15 07:26:53 -05:00
|
|
|
def __init__(self):
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-18 19:35:18 -05:00
|
|
|
self.rx = PHYSink()
|
|
|
|
self.tx = PHYSource()
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
self.source = self.tx.source
|
|
|
|
self.sink = self.rx.sink
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def send(self, dword):
|
2014-12-04 17:43:21 -05:00
|
|
|
packet = PHYDword(dword)
|
|
|
|
self.tx.send(packet)
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def receive(self):
|
2014-12-05 19:23:03 -05:00
|
|
|
yield from self.rx.receive()
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def __repr__(self):
|
2014-12-04 17:43:21 -05:00
|
|
|
receiving = "%08x " %self.rx.dword.dat
|
|
|
|
receiving += decode_primitive(self.rx.dword.dat)
|
2014-12-03 03:17:51 -05:00
|
|
|
receiving += " "*(16-len(receiving))
|
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
sending = "%08x " %self.tx.dword.dat
|
|
|
|
sending += decode_primitive(self.tx.dword.dat)
|
2014-12-03 03:17:51 -05:00
|
|
|
sending += " "*(16-len(sending))
|
|
|
|
|
|
|
|
return receiving + sending
|
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
# Link Layer model
|
2014-12-15 07:26:53 -05:00
|
|
|
def print_link(s):
|
|
|
|
print_with_prefix(s, "[LNK]: ")
|
|
|
|
|
2014-12-05 19:23:03 -05:00
|
|
|
def import_scrambler_datas():
|
|
|
|
with subprocess.Popen(["./scrambler"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write("0x10000".encode("ASCII"))
|
|
|
|
out, err = process.communicate()
|
|
|
|
return [int(e, 16) for e in out.decode("utf-8").split("\n")[:-1]]
|
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class LinkPacket(list):
|
2014-12-05 12:00:02 -05:00
|
|
|
def __init__(self, init=[]):
|
2014-12-04 17:43:21 -05:00
|
|
|
self.ongoing = False
|
2014-12-05 12:00:02 -05:00
|
|
|
self.done = False
|
2014-12-05 19:23:03 -05:00
|
|
|
self.scrambled_datas = import_scrambler_datas()
|
2014-12-05 12:00:02 -05:00
|
|
|
for dword in init:
|
|
|
|
self.append(dword)
|
2014-12-03 03:17:51 -05:00
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class LinkRXPacket(LinkPacket):
|
|
|
|
def descramble(self):
|
|
|
|
for i in range(len(self)):
|
|
|
|
self[i] = self[i] ^ self.scrambled_datas[i]
|
2014-12-03 03:17:51 -05:00
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
def check_crc(self):
|
|
|
|
stdin = ""
|
|
|
|
for v in self[:-1]:
|
|
|
|
stdin += "0x%08x " %v
|
|
|
|
stdin += "exit"
|
|
|
|
with subprocess.Popen("./crc", stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write(stdin.encode("ASCII"))
|
2014-12-02 13:53:13 -05:00
|
|
|
out, err = process.communicate()
|
2014-12-04 17:43:21 -05:00
|
|
|
crc = int(out.decode("ASCII"), 16)
|
|
|
|
r = (self[-1] == crc)
|
|
|
|
self.pop()
|
|
|
|
return r
|
2014-11-12 12:20:34 -05:00
|
|
|
|
2014-12-14 10:49:35 -05:00
|
|
|
def decode(self):
|
|
|
|
self.descramble()
|
|
|
|
return self.check_crc()
|
2014-12-04 17:43:21 -05:00
|
|
|
|
2014-12-14 10:49:35 -05:00
|
|
|
class LinkTXPacket(LinkPacket):
|
2014-12-04 17:43:21 -05:00
|
|
|
def insert_crc(self):
|
2014-12-02 14:02:43 -05:00
|
|
|
stdin = ""
|
2014-12-05 14:26:09 -05:00
|
|
|
for v in self:
|
2014-12-02 14:02:43 -05:00
|
|
|
stdin += "0x%08x " %v
|
|
|
|
stdin += "exit"
|
|
|
|
with subprocess.Popen("./crc", stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write(stdin.encode("ASCII"))
|
|
|
|
out, err = process.communicate()
|
|
|
|
crc = int(out.decode("ASCII"), 16)
|
2014-12-04 17:43:21 -05:00
|
|
|
self.append(crc)
|
|
|
|
|
2014-12-14 10:49:35 -05:00
|
|
|
def scramble(self):
|
|
|
|
for i in range(len(self)):
|
|
|
|
self[i] = self[i] ^ self.scrambled_datas[i]
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
self.insert_crc()
|
|
|
|
self.scramble()
|
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
class LinkLayer(Module):
|
2014-12-05 14:26:09 -05:00
|
|
|
def __init__(self, phy, debug=False, random_level=0):
|
2014-12-04 17:43:21 -05:00
|
|
|
self.phy = phy
|
|
|
|
self.debug = debug
|
2014-12-05 14:26:09 -05:00
|
|
|
self.random_level = random_level
|
|
|
|
self.tx_packets = []
|
2014-12-04 17:43:21 -05:00
|
|
|
self.tx_packet = LinkTXPacket()
|
|
|
|
self.rx_packet = LinkRXPacket()
|
2014-12-05 19:23:03 -05:00
|
|
|
|
2014-12-04 17:43:21 -05:00
|
|
|
self.rx_cont = False
|
2014-12-05 19:23:03 -05:00
|
|
|
self.rx_last = 0
|
2014-12-05 15:27:26 -05:00
|
|
|
self.tx_cont = False
|
2014-12-05 19:23:03 -05:00
|
|
|
self.tx_cont_nb = -1
|
|
|
|
self.tx_lasts = [0, 0, 0]
|
|
|
|
|
|
|
|
self.scrambled_datas = import_scrambler_datas()
|
2014-12-04 17:43:21 -05:00
|
|
|
|
2014-12-04 19:13:55 -05:00
|
|
|
self.transport_callback = None
|
|
|
|
|
2014-12-05 14:26:09 -05:00
|
|
|
self.send_state = ""
|
|
|
|
self.send_states = ["RDY", "SOF", "DATA", "EOF", "WTRM"]
|
|
|
|
|
2014-12-04 19:13:55 -05:00
|
|
|
def set_transport_callback(self, callback):
|
|
|
|
self.transport_callback = callback
|
|
|
|
|
2014-12-05 14:26:09 -05:00
|
|
|
def send(self, dword):
|
|
|
|
if self.send_state == "RDY":
|
|
|
|
self.phy.send(primitives["X_RDY"])
|
|
|
|
if dword == primitives["R_RDY"]:
|
|
|
|
self.send_state = "SOF"
|
|
|
|
elif self.send_state == "SOF":
|
|
|
|
self.phy.send(primitives["SOF"])
|
|
|
|
self.send_state = "DATA"
|
|
|
|
elif self.send_state == "DATA":
|
2014-12-05 15:27:26 -05:00
|
|
|
if dword == primitives["HOLD"]:
|
|
|
|
self.phy.send(primitives["HOLDA"])
|
|
|
|
else:
|
|
|
|
self.phy.send(self.tx_packet.pop(0))
|
|
|
|
if len(self.tx_packet) == 0:
|
|
|
|
self.send_state = "EOF"
|
2014-12-05 14:26:09 -05:00
|
|
|
elif self.send_state == "EOF":
|
|
|
|
self.phy.send(primitives["EOF"])
|
|
|
|
self.send_state = "WTRM"
|
|
|
|
elif self.send_state == "WTRM":
|
|
|
|
self.phy.send(primitives["WTRM"])
|
|
|
|
if dword == primitives["R_OK"]:
|
|
|
|
self.tx_packet.done = True
|
|
|
|
elif dword == primitives["R_ERR"]:
|
|
|
|
self.tx_packet.done = True
|
2014-12-14 09:32:00 -05:00
|
|
|
self.phy.send(primitives["SYNC"])
|
2014-11-11 12:47:34 -05:00
|
|
|
|
2014-12-05 19:23:03 -05:00
|
|
|
def insert_cont(self):
|
|
|
|
self.tx_lasts.pop(0)
|
|
|
|
self.tx_lasts.append(self.phy.tx.dword.dat)
|
|
|
|
self.tx_cont = True
|
|
|
|
for i in range(3):
|
|
|
|
if not is_primitive(self.tx_lasts[i]):
|
|
|
|
self.tx_cont = False
|
|
|
|
if self.tx_lasts[i] != self.tx_lasts[0]:
|
|
|
|
self.tx_cont = False
|
|
|
|
if self.tx_cont:
|
|
|
|
if self.tx_cont_nb == 0:
|
|
|
|
self.phy.send(primitives["CONT"])
|
|
|
|
else:
|
|
|
|
self.phy.send(self.scrambled_datas[self.tx_cont_nb])
|
|
|
|
self.tx_cont_nb += 1
|
|
|
|
else:
|
|
|
|
self.tx_cont_nb = 0
|
|
|
|
|
2014-12-14 10:49:35 -05:00
|
|
|
def remove_cont(self, dword):
|
|
|
|
if dword == primitives["HOLD"]:
|
|
|
|
if self.rx_cont:
|
|
|
|
self.tx_lasts = [0, 0, 0]
|
|
|
|
if dword == primitives["CONT"]:
|
|
|
|
self.rx_cont = True
|
|
|
|
elif is_primitive(dword):
|
|
|
|
self.rx_last = dword
|
|
|
|
self.rx_cont = False
|
|
|
|
if self.rx_cont:
|
|
|
|
dword = self.rx_last
|
|
|
|
return dword
|
|
|
|
|
|
|
|
def callback(self, dword):
|
|
|
|
if dword == primitives["X_RDY"]:
|
|
|
|
self.phy.send(primitives["R_RDY"])
|
|
|
|
elif dword == primitives["WTRM"]:
|
|
|
|
self.phy.send(primitives["R_OK"])
|
|
|
|
if self.rx_packet.ongoing:
|
|
|
|
self.rx_packet.decode()
|
|
|
|
if self.transport_callback is not None:
|
|
|
|
self.transport_callback(self.rx_packet)
|
|
|
|
self.rx_packet.ongoing = False
|
|
|
|
elif dword == primitives["HOLD"]:
|
|
|
|
self.phy.send(primitives["HOLDA"])
|
|
|
|
elif dword == primitives["EOF"]:
|
|
|
|
pass
|
|
|
|
elif self.rx_packet.ongoing:
|
|
|
|
if dword != primitives["HOLD"]:
|
|
|
|
n = randn(100)
|
|
|
|
if n < self.random_level:
|
|
|
|
self.phy.send(primitives["HOLD"])
|
|
|
|
else:
|
|
|
|
self.phy.send(primitives["R_IP"])
|
|
|
|
if not is_primitive(dword):
|
|
|
|
self.rx_packet.append(dword)
|
|
|
|
elif dword == primitives["SOF"]:
|
|
|
|
self.rx_packet = LinkRXPacket()
|
|
|
|
self.rx_packet.ongoing = True
|
|
|
|
|
2014-11-11 12:47:34 -05:00
|
|
|
def gen_simulation(self, selfp):
|
2014-12-05 14:26:09 -05:00
|
|
|
self.tx_packet.done = True
|
2014-12-05 15:27:26 -05:00
|
|
|
self.phy.send(primitives["SYNC"])
|
2014-11-11 12:47:34 -05:00
|
|
|
while True:
|
|
|
|
yield from self.phy.receive()
|
2014-12-15 07:26:53 -05:00
|
|
|
if self.debug:
|
|
|
|
print_link(self.phy)
|
2014-12-05 14:26:09 -05:00
|
|
|
self.phy.send(primitives["SYNC"])
|
|
|
|
rx_dword = self.phy.rx.dword.dat
|
2014-12-05 19:23:03 -05:00
|
|
|
rx_dword = self.remove_cont(rx_dword)
|
2014-12-05 14:26:09 -05:00
|
|
|
if len(self.tx_packets) != 0:
|
|
|
|
if self.tx_packet.done:
|
|
|
|
self.tx_packet = self.tx_packets.pop(0)
|
|
|
|
self.tx_packet.encode()
|
|
|
|
self.send_state = "RDY"
|
|
|
|
if not self.tx_packet.done:
|
|
|
|
self.send(rx_dword)
|
|
|
|
else:
|
2014-12-05 19:23:03 -05:00
|
|
|
self.callback(rx_dword)
|
|
|
|
self.insert_cont()
|
2014-12-04 17:43:21 -05:00
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
# Transport Layer model
|
2014-12-15 07:26:53 -05:00
|
|
|
def print_transport(s):
|
|
|
|
print_with_prefix(s, "[TRN]: ")
|
|
|
|
|
2014-12-04 19:13:55 -05:00
|
|
|
def get_field_data(field, packet):
|
|
|
|
return (packet[field.dword] >> field.offset) & (2**field.width-1)
|
|
|
|
|
|
|
|
class FIS:
|
2014-12-14 10:20:22 -05:00
|
|
|
def __init__(self, packet, description, direction="H2D"):
|
2014-12-04 19:13:55 -05:00
|
|
|
self.packet = packet
|
2014-12-14 04:52:56 -05:00
|
|
|
self.description = description
|
2014-12-14 10:20:22 -05:00
|
|
|
self.direction = direction
|
2014-12-04 19:13:55 -05:00
|
|
|
self.decode()
|
|
|
|
|
|
|
|
def decode(self):
|
2014-12-14 04:52:56 -05:00
|
|
|
for k, v in self.description.items():
|
2014-12-04 19:13:55 -05:00
|
|
|
setattr(self, k, get_field_data(v, self.packet))
|
|
|
|
|
|
|
|
def encode(self):
|
2014-12-14 04:52:56 -05:00
|
|
|
for k, v in self.description.items():
|
2014-12-04 19:13:55 -05:00
|
|
|
self.packet[v.dword] |= (getattr(self, k) << v.offset)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2014-12-14 10:20:22 -05:00
|
|
|
if self.direction == "H2D":
|
|
|
|
r = ">>>>>>>>\n"
|
|
|
|
else:
|
|
|
|
r = "<<<<<<<<\n"
|
2014-12-14 04:52:56 -05:00
|
|
|
for k in sorted(self.description.keys()):
|
2014-12-04 19:13:55 -05:00
|
|
|
r += k + " : 0x%x" %getattr(self,k) + "\n"
|
|
|
|
return r
|
|
|
|
|
|
|
|
class FIS_REG_H2D(FIS):
|
2014-12-11 18:56:29 -05:00
|
|
|
def __init__(self, packet=[0]*fis_reg_h2d_cmd_len):
|
2014-12-14 07:14:32 -05:00
|
|
|
FIS.__init__(self, packet, fis_reg_h2d_layout)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.type = fis_types["REG_H2D"]
|
2014-12-14 10:20:22 -05:00
|
|
|
self.direction = "H2D"
|
2014-12-04 19:13:55 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "FIS_REG_H2D\n"
|
|
|
|
r += FIS.__repr__(self)
|
|
|
|
return r
|
|
|
|
|
|
|
|
class FIS_REG_D2H(FIS):
|
2014-12-11 18:56:29 -05:00
|
|
|
def __init__(self, packet=[0]*fis_reg_d2h_cmd_len):
|
2014-12-14 07:14:32 -05:00
|
|
|
FIS.__init__(self, packet, fis_reg_d2h_layout)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.type = fis_types["REG_D2H"]
|
2014-12-14 10:20:22 -05:00
|
|
|
self.direction = "D2H"
|
2014-12-04 19:13:55 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "FIS_REG_D2H\n"
|
|
|
|
r += FIS.__repr__(self)
|
|
|
|
return r
|
|
|
|
|
|
|
|
class FIS_DMA_ACTIVATE_D2H(FIS):
|
2014-12-11 18:56:29 -05:00
|
|
|
def __init__(self, packet=[0]*fis_dma_activate_d2h_cmd_len):
|
2014-12-14 07:14:32 -05:00
|
|
|
FIS.__init__(self, packet, fis_dma_activate_d2h_layout)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.type = fis_types["DMA_ACTIVATE_D2H"]
|
2014-12-14 10:20:22 -05:00
|
|
|
self.direction = "D2H"
|
2014-12-04 19:13:55 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "FIS_DMA_ACTIVATE_D2H\n"
|
|
|
|
r += FIS.__repr__(self)
|
|
|
|
return r
|
|
|
|
|
|
|
|
class FIS_DATA(FIS):
|
2014-12-14 10:20:22 -05:00
|
|
|
def __init__(self, packet=[0], direction="H2D"):
|
|
|
|
FIS.__init__(self, packet, fis_data_layout, direction)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.type = fis_types["DATA"]
|
2014-12-04 19:13:55 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "FIS_DATA\n"
|
|
|
|
r += FIS.__repr__(self)
|
2014-12-12 05:22:21 -05:00
|
|
|
for data in self.packet[1:]:
|
|
|
|
r += "%08x\n" %data
|
2014-12-04 19:13:55 -05:00
|
|
|
return r
|
|
|
|
|
|
|
|
class FIS_UNKNOWN(FIS):
|
2014-12-14 10:20:22 -05:00
|
|
|
def __init__(self, packet=[0], direction="H2D"):
|
|
|
|
FIS.__init__(self, packet, {}, direction)
|
2014-12-04 19:13:55 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "UNKNOWN\n"
|
2014-12-14 10:20:22 -05:00
|
|
|
if self.direction == "H2D":
|
|
|
|
r += ">>>>>>>>\\n"
|
|
|
|
else:
|
|
|
|
r += "<<<<<<<<\n"
|
2014-12-04 19:13:55 -05:00
|
|
|
for dword in self.packet:
|
|
|
|
r += "%08x\n" %dword
|
|
|
|
return r
|
|
|
|
|
|
|
|
class TransportLayer(Module):
|
2014-12-05 14:26:09 -05:00
|
|
|
def __init__(self, link, debug=False, loopback=False):
|
|
|
|
self.link = link
|
|
|
|
self.debug = debug
|
|
|
|
self.loopback = loopback
|
|
|
|
self.link.set_transport_callback(self.callback)
|
2014-12-04 19:13:55 -05:00
|
|
|
|
2014-12-12 16:26:04 -05:00
|
|
|
def set_command_callback(self, callback):
|
|
|
|
self.command_callback = callback
|
|
|
|
|
|
|
|
def send(self, fis):
|
|
|
|
fis.encode()
|
|
|
|
packet = LinkTXPacket(fis.packet)
|
|
|
|
self.link.tx_packets.append(packet)
|
|
|
|
if self.debug and not self.loopback:
|
2014-12-15 07:26:53 -05:00
|
|
|
print_transport(fis)
|
2014-12-12 16:26:04 -05:00
|
|
|
|
2014-12-04 19:13:55 -05:00
|
|
|
def callback(self, packet):
|
2014-12-12 16:26:04 -05:00
|
|
|
fis_type = packet[0] & 0xff
|
2014-12-04 19:13:55 -05:00
|
|
|
if fis_type == fis_types["REG_H2D"]:
|
|
|
|
fis = FIS_REG_H2D(packet)
|
|
|
|
elif fis_type == fis_types["REG_D2H"]:
|
|
|
|
fis = FIS_REG_D2H(packet)
|
|
|
|
elif fis_type == fis_types["DMA_ACTIVATE_D2H"]:
|
|
|
|
fis = FIS_DMA_ACTIVATE_D2H(packet)
|
|
|
|
elif fis_type == fis_types["DATA"]:
|
2014-12-14 10:20:22 -05:00
|
|
|
fis = FIS_DATA(packet, direction="H2D")
|
2014-12-04 19:13:55 -05:00
|
|
|
else:
|
2014-12-14 10:20:22 -05:00
|
|
|
fis = FIS_UNKNOWN(packet, direction="H2D")
|
2014-12-05 14:26:09 -05:00
|
|
|
if self.debug:
|
2014-12-15 07:26:53 -05:00
|
|
|
print_transport(fis)
|
2014-12-05 14:26:09 -05:00
|
|
|
if self.loopback:
|
2014-12-12 16:26:04 -05:00
|
|
|
self.send(fis)
|
|
|
|
else:
|
|
|
|
self.command_callback(fis)
|
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
# Command Layer model
|
2014-12-12 16:26:04 -05:00
|
|
|
class CommandLayer(Module):
|
2014-12-15 07:26:53 -05:00
|
|
|
def __init__(self, transport):
|
2014-12-12 16:26:04 -05:00
|
|
|
self.transport = transport
|
|
|
|
self.transport.set_command_callback(self.callback)
|
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
self.hdd = None
|
2014-12-12 19:18:08 -05:00
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
def set_hdd(self, hdd):
|
|
|
|
self.hdd = hdd
|
2014-12-12 19:18:08 -05:00
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
def callback(self, fis):
|
2014-12-14 09:32:00 -05:00
|
|
|
resp = None
|
2014-12-14 05:44:12 -05:00
|
|
|
if isinstance(fis, FIS_REG_H2D):
|
|
|
|
if fis.command == regs["WRITE_DMA_EXT"]:
|
2014-12-15 07:26:53 -05:00
|
|
|
resp = self.hdd.write_dma_callback(fis)
|
2014-12-14 05:44:12 -05:00
|
|
|
elif fis.command == regs["READ_DMA_EXT"]:
|
2014-12-15 07:26:53 -05:00
|
|
|
resp = self.hdd.read_dma_callback(fis)
|
2014-12-14 05:44:12 -05:00
|
|
|
elif fis.command == regs["IDENTIFY_DEVICE_DMA"]:
|
2014-12-15 07:26:53 -05:00
|
|
|
resp = self.hdd.identify_device_dma_callback(fis)
|
2014-12-14 05:44:12 -05:00
|
|
|
elif isinstance(fis, FIS_DATA):
|
2014-12-15 07:26:53 -05:00
|
|
|
resp = self.hdd.data_callback(fis)
|
2014-12-14 09:32:00 -05:00
|
|
|
|
|
|
|
if resp is not None:
|
|
|
|
for packet in resp:
|
|
|
|
self.transport.send(packet)
|
2014-12-14 05:44:12 -05:00
|
|
|
|
|
|
|
# HDD model
|
2014-12-15 07:26:53 -05:00
|
|
|
def print_hdd(s):
|
|
|
|
print_with_prefix(s, "[HDD]: ")
|
|
|
|
|
2014-12-14 05:44:12 -05:00
|
|
|
class HDDMemRegion:
|
2014-12-15 07:26:53 -05:00
|
|
|
def __init__(self, base, count, sector_size):
|
2014-12-14 05:44:12 -05:00
|
|
|
self.base = base
|
2014-12-15 07:26:53 -05:00
|
|
|
self.count = count
|
|
|
|
self.data = [0]*(count*sector_size//4)
|
2014-12-14 05:44:12 -05:00
|
|
|
|
|
|
|
class HDD(Module):
|
2014-12-14 10:20:22 -05:00
|
|
|
def __init__(self,
|
|
|
|
link_debug=False, link_random_level=0,
|
|
|
|
transport_debug=False, transport_loopback=False,
|
2014-12-14 14:30:01 -05:00
|
|
|
hdd_debug=False, hdd_sector_size=512,
|
2014-12-14 10:20:22 -05:00
|
|
|
):
|
|
|
|
###
|
2014-12-18 19:35:18 -05:00
|
|
|
self.phy = PHYLayer()
|
|
|
|
self.link = LinkLayer(self.phy, link_debug, link_random_level)
|
|
|
|
self.transport = TransportLayer(self.link, transport_debug, transport_loopback)
|
|
|
|
self.command = CommandLayer(self.transport)
|
2014-12-14 05:44:12 -05:00
|
|
|
|
2014-12-14 10:20:22 -05:00
|
|
|
self.command.set_hdd(self)
|
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
self.debug = hdd_debug
|
|
|
|
self.sector_size = hdd_sector_size
|
2014-12-14 05:44:12 -05:00
|
|
|
self.mem = None
|
2014-12-15 07:26:53 -05:00
|
|
|
self.wr_sector = 0
|
|
|
|
self.wr_end_sector = 0
|
|
|
|
|
|
|
|
def dwords2sectors(self, n):
|
|
|
|
return math.ceil(n*4/self.sector_size)
|
|
|
|
|
|
|
|
def sectors2dwords(self, n):
|
|
|
|
return n*self.sector_size//4
|
|
|
|
|
|
|
|
def malloc(self, sector, count):
|
|
|
|
if self.debug:
|
|
|
|
s = "Allocating {n} sectors: {s} to {e}".format(n=count, s=sector, e=sector+count)
|
|
|
|
s += " ({} KB)".format(count*self.sector_size//1024)
|
|
|
|
print_hdd(s)
|
|
|
|
self.mem = HDDMemRegion(sector, count, self.sector_size)
|
|
|
|
|
|
|
|
def write(self, sector, data):
|
|
|
|
n = math.ceil(self.dwords2sectors(len(data)))
|
|
|
|
if self.debug:
|
2014-12-15 13:04:07 -05:00
|
|
|
if n == 1:
|
|
|
|
s = "{}".format(sector)
|
|
|
|
else:
|
|
|
|
s = "{s} to {e}".format(s=sector, e=sector+n-1)
|
|
|
|
print_hdd("Writing sector " + s)
|
2014-12-14 10:49:35 -05:00
|
|
|
for i in range(len(data)):
|
2014-12-15 07:26:53 -05:00
|
|
|
offset = self.sectors2dwords(sector)
|
|
|
|
self.mem.data[offset+i] = data[i]
|
2014-12-14 10:49:35 -05:00
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
def read(self, sector, count):
|
|
|
|
if self.debug:
|
2014-12-15 13:04:07 -05:00
|
|
|
if count == 1:
|
|
|
|
s = "{}".format(sector)
|
|
|
|
else:
|
|
|
|
s = "{s} to {e}".format(s=sector, e=sector+count-1)
|
|
|
|
print_hdd("Reading sector " + s)
|
2014-12-14 10:49:35 -05:00
|
|
|
data = []
|
2014-12-15 07:26:53 -05:00
|
|
|
for i in range(self.sectors2dwords(count)):
|
|
|
|
data.append(self.mem.data[self.sectors2dwords(sector)+i])
|
2014-12-14 10:49:35 -05:00
|
|
|
return data
|
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
def write_dma_callback(self, fis):
|
|
|
|
self.wr_sector = fis.lba_lsb + (fis.lba_msb << 32)
|
|
|
|
self.wr_end_sector = self.wr_sector + fis.count
|
2014-12-14 09:32:00 -05:00
|
|
|
return [FIS_DMA_ACTIVATE_D2H()]
|
2014-12-12 19:18:08 -05:00
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
def read_dma_callback(self, fis):
|
|
|
|
sector = fis.lba_lsb + (fis.lba_msb << 32)
|
|
|
|
packet = self.read(sector, fis.count)
|
|
|
|
packet.insert(0, 0)
|
|
|
|
return [FIS_DATA(packet, direction="D2H"), FIS_REG_D2H()]
|
|
|
|
|
|
|
|
def identify_dma_callback(self, fis):
|
2014-12-14 07:14:32 -05:00
|
|
|
packet = [i for i in range(256)]
|
|
|
|
packet.insert(0, 0)
|
2014-12-14 14:30:01 -05:00
|
|
|
return [FIS_DATA(packet, direction="D2H"), FIS_REG_D2H()]
|
2014-12-14 05:44:12 -05:00
|
|
|
|
2014-12-15 07:26:53 -05:00
|
|
|
def data_callback(self, fis):
|
|
|
|
self.write(self.wr_sector, fis.packet[1:])
|
|
|
|
self.wr_sector += self.dwords2sectors(len(fis.packet[1:]))
|
|
|
|
if self.wr_sector == self.wr_end_sector:
|
2014-12-14 09:32:00 -05:00
|
|
|
return [FIS_REG_D2H()]
|
|
|
|
else:
|
2014-12-14 14:30:01 -05:00
|
|
|
return [FIS_DMA_ACTIVATE_D2H()]
|