def unpack_us_cc(cls, pkt, check_parity=False): tlp = cls() tlp.fmt_type = TlpType.CPL tlp.lower_address = pkt.data[0] & 0x7f tlp.at = (pkt.data[0] >> 8) & 3 tlp.byte_count = (pkt.data[0] >> 16) & 0x1fff if pkt.data[0] & (1 << 29): tlp.fmt_type = TlpType.CPL_LOCKED tlp.length = pkt.data[1] & 0x7ff if tlp.length > 0: tlp.fmt = TlpFmt.THREE_DW_DATA tlp.status = CplStatus((pkt.data[1] >> 11) & 7) tlp.requester_id = PcieId.from_int(pkt.data[1] >> 16) tlp.completer_id = PcieId.from_int(pkt.data[2] >> 8) tlp.completer_id_enable = pkt.data[2] >> 24 & 1 != 0 tlp.tag = pkt.data[2] & 0xff tlp.completer_id_enable = bool(pkt.data[2] & (1 << 24)) tlp.tc = (pkt.data[2] >> 25) & 0x7 tlp.attr = (pkt.data[2] >> 28) & 0x7 tlp.discontinue = pkt.discontinue if tlp.length > 0: tlp.data = pkt.data[3:3+tlp.length] # check parity if check_parity: assert pkt.check_parity() return tlp
def unpack_us_rc(cls, pkt, check_parity=False): tlp = cls() tlp.fmt_type = TlpType.CPL tlp.lower_address = pkt.data[0] & 0xfff tlp.error_code = (pkt.data[0] >> 12) & 0xf tlp.byte_count = (pkt.data[0] >> 16) & 0x1fff if pkt.data[0] & (1 << 29): tlp.fmt_type = TlpType.CPL_LOCKED tlp.request_completed = pkt.data[0] & (1 << 30) tlp.length = pkt.data[1] & 0x7ff if tlp.length > 0: tlp.fmt = TlpFmt.THREE_DW_DATA tlp.status = CplStatus((pkt.data[1] >> 11) & 7) tlp.ep = bool(pkt.data[1] & 1 << 14) tlp.requester_id = PcieId.from_int(pkt.data[1] >> 16) tlp.completer_id = PcieId.from_int(pkt.data[2] >> 8) tlp.tag = pkt.data[2] & 0xff tlp.tc = TlpTc((pkt.data[2] >> 25) & 0x7) tlp.attr = TlpAttr((pkt.data[2] >> 28) & 0x7) tlp.discontinue = pkt.discontinue if tlp.length > 0: for dw in pkt.data[3:3+tlp.length]: tlp.data.extend(struct.pack('<L', dw)) # compute byte enables byte_en = [0]*3 first_be = (0xf << (tlp.lower_address & 3)) & 0xf if tlp.byte_count+(tlp.lower_address & 3) > tlp.length*4: last_be = 0xf else: last_be = 0xf >> ((4-tlp.byte_count-tlp.lower_address) & 3) if tlp.get_payload_size_dw() == 1: first_be = first_be & last_be last_be = 0 if tlp.get_payload_size_dw() >= 1: byte_en += [first_be] if tlp.get_payload_size_dw() > 2: byte_en += [0xf] * (tlp.get_payload_size_dw()-2) if tlp.get_payload_size_dw() > 1: byte_en += [last_be] # check byte enables assert byte_en == pkt.byte_en # check parity if check_parity: assert pkt.check_parity() return tlp
def unpack_us_rc(cls, pkt, check_parity=False): tlp = cls() tlp.fmt_type = TlpType.CPL tlp.lower_address = pkt.data[0] & 0xfff tlp.error_code = (pkt.data[0] >> 12) & 0xf tlp.byte_count = (pkt.data[0] >> 16) & 0x1fff if pkt.data[0] & (1 << 29): tlp.fmt_type = TlpType.CPL_LOCKED tlp.length = pkt.data[1] & 0x7ff if tlp.length > 0: tlp.fmt = TlpFmt.THREE_DW_DATA tlp.status = (pkt.data[1] >> 11) & 7 tlp.requester_id = PcieId.from_int(pkt.data[1] >> 16) tlp.completer_id = PcieId.from_int(pkt.data[2] >> 8) tlp.tag = pkt.data[2] & 0xff tlp.tc = (pkt.data[2] >> 25) & 0x7 tlp.attr = (pkt.data[2] >> 28) & 0x7 tlp.discontinue = pkt.discontinue if tlp.length > 0: tlp.data = pkt.data[3:3+tlp.length] # compute byte enables byte_en = [0]*3 first_be = (0xf << (tlp.lower_address & 3)) & 0xf if tlp.byte_count+(tlp.lower_address & 3) > tlp.length*4: last_be = 0xf else: last_be = 0xf >> ((4-tlp.byte_count-tlp.lower_address) & 3) if len(tlp.data) == 1: first_be = first_be & last_be last_be = 0 if len(tlp.data) >= 1: byte_en += [first_be] if len(tlp.data) > 2: byte_en += [0xf] * (len(tlp.data)-2) if len(tlp.data) > 1: byte_en += [last_be] # check byte enables assert byte_en == pkt.byte_en # check parity if check_parity: assert pkt.check_parity() return tlp
def unpack_us_rq(cls, pkt, check_parity=False): tlp = cls() req_type = (pkt.data[2] >> 11) & 0xf tlp.fmt_type = req_type_to_tlp_type[req_type] tlp.length = pkt.data[2] & 0x7ff tlp.ep = bool(pkt.data[2] & 1 << 15) tlp.requester_id = PcieId.from_int(pkt.data[2] >> 16) tlp.tag = pkt.data[3] & 0xff tlp.tc = TlpTc((pkt.data[3] >> 25) & 0x7) tlp.attr = TlpAttr((pkt.data[3] >> 28) & 0x7) if req_type < 12: if req_type < 8: # memory, IO, or atomic operation tlp.at = TlpAt(pkt.data[0] & 3) tlp.address = (pkt.data[1] << 32) | (pkt.data[0] & 0xfffffffc) if tlp.address > 0xffffffff: if tlp.fmt == TlpFmt.THREE_DW: tlp.fmt = TlpFmt.FOUR_DW elif tlp.fmt == TlpFmt.THREE_DW_DATA: tlp.fmt = TlpFmt.FOUR_DW_DATA else: # configuration tlp.register_number = (pkt.data[0] >> 2) & 0x3ff tlp.completer_id = PcieId.from_int(pkt.data[3] >> 8) tlp.requester_id_enable = bool(pkt.data[3] & (1 << 24)) tlp.first_be = pkt.first_be tlp.last_be = pkt.last_be tlp.discontinue = pkt.discontinue tlp.seq_num = pkt.seq_num for dw in pkt.data[4:]: tlp.data.extend(struct.pack('<L', dw)) # check parity if check_parity: assert pkt.check_parity() else: raise Exception("TODO") return tlp
def unpack_us_cq(cls, pkt, check_parity=False): tlp = cls() req_type = (pkt.data[2] >> 11) & 0xf tlp.fmt_type = req_type_to_tlp_type[req_type] tlp.length = pkt.data[2] & 0x7ff tlp.requester_id = PcieId.from_int(pkt.data[2] >> 16) tlp.tag = pkt.data[3] & 0xff tlp.tc = TlpTc((pkt.data[3] >> 25) & 0x7) tlp.attr = TlpAttr((pkt.data[3] >> 28) & 0x7) if req_type & 8 == 0: # memory, IO, or atomic operation tlp.at = TlpAt(pkt.data[0] & 3) tlp.address = (pkt.data[1] << 32) | (pkt.data[0] & 0xfffffffc) if tlp.address > 0xffffffff: if tlp.fmt == TlpFmt.THREE_DW: tlp.fmt = TlpFmt.FOUR_DW elif tlp.fmt == TlpFmt.THREE_DW_DATA: tlp.fmt = TlpFmt.FOUR_DW_DATA tlp.completer_id = PcieId(0, 0, (pkt.data[3] >> 8) & 0xff) tlp.bar_id = (pkt.data[3] >> 16) & 7 tlp.bar_aperture = (pkt.data[3] >> 19) & 0x3f tlp.first_be = pkt.first_be tlp.last_be = pkt.last_be tlp.discontinue = pkt.discontinue for dw in pkt.data[4:]: tlp.data.extend(struct.pack('<L', dw)) # compute byte enables byte_en = [0]*4 if tlp.get_payload_size_dw() >= 1: byte_en += [tlp.first_be] if tlp.get_payload_size_dw() > 2: byte_en += [0xf] * (tlp.get_payload_size_dw()-2) if tlp.get_payload_size_dw() > 1: byte_en += [tlp.last_be] # check byte enables assert byte_en == pkt.byte_en # check parity if check_parity: assert pkt.check_parity() return tlp
def unpack_us_rq(cls, pkt, check_parity=False): tlp = cls() req_type = (pkt.data[2] >> 11) & 0xf if req_type == ReqType.MEM_READ: tlp.fmt_type = TlpType.MEM_READ elif req_type == ReqType.MEM_WRITE: tlp.fmt_type = TlpType.MEM_WRITE elif req_type == ReqType.IO_READ: tlp.fmt_type = TlpType.IO_READ elif req_type == ReqType.IO_WRITE: tlp.fmt_type = TlpType.IO_WRITE elif req_type == ReqType.MEM_FETCH_ADD: tlp.fmt_type = TlpType.FETCH_ADD elif req_type == ReqType.MEM_SWAP: tlp.fmt_type = TlpType.SWAP elif req_type == ReqType.MEM_CAS: tlp.fmt_type = TlpType.CAS elif req_type == ReqType.MEM_READ_LOCKED: tlp.fmt_type = TlpType.MEM_READ_LOCKED elif req_type == ReqType.CFG_READ_0: tlp.fmt_type = TlpType.CFG_READ_0 elif req_type == ReqType.CFG_READ_1: tlp.fmt_type = TlpType.CFG_READ_1 elif req_type == ReqType.CFG_WRITE_0: tlp.fmt_type = TlpType.CFG_WRITE_0 elif req_type == ReqType.CFG_WRITE_1: tlp.fmt_type = TlpType.CFG_WRITE_1 else: raise Exception("Invalid packet type") tlp.length = pkt.data[2] & 0x7ff # TODO poisoned tlp.requester_id = PcieId.from_int(pkt.data[2] >> 16) tlp.tag = pkt.data[3] & 0xff tlp.tc = (pkt.data[3] >> 25) & 0x7 tlp.attr = (pkt.data[3] >> 28) & 0x7 if req_type < 12: if req_type < 8: # memory, IO, or atomic operation tlp.at = pkt.data[0] & 3 tlp.address = (pkt.data[1] << 32) | (pkt.data[0] & 0xfffffffc) if tlp.address > 0xffffffff: if tlp.fmt == TlpFmt.THREE_DW: tlp.fmt = TlpFmt.FOUR_DW elif tlp.fmt == TlpFmt.THREE_DW_DATA: tlp.fmt = TlpFmt.FOUR_DW_DATA else: tlp.register_number = (pkt.data[0] >> 2) & 0x3ff tlp.completer_id = PcieId.from_int(pkt.data[3] >> 8) tlp.requester_id_enable = bool(pkt.data[3] & (1 << 24)) tlp.first_be = pkt.first_be tlp.last_be = pkt.last_be tlp.discontinue = pkt.discontinue tlp.seq_num = pkt.seq_num tlp.data = pkt.data[4:] # check parity if check_parity: assert pkt.check_parity() else: raise Exception("TODO") return tlp
def unpack_us_cq(cls, pkt, check_parity=False): tlp = cls() req_type = (pkt.data[2] >> 11) & 0xf if req_type == ReqType.MEM_READ: tlp.fmt_type = TlpType.MEM_READ elif req_type == ReqType.MEM_WRITE: tlp.fmt_type = TlpType.MEM_WRITE elif req_type == ReqType.IO_READ: tlp.fmt_type = TlpType.IO_READ elif req_type == ReqType.IO_WRITE: tlp.fmt_type = TlpType.IO_WRITE elif req_type == ReqType.MEM_FETCH_ADD: tlp.fmt_type = TlpType.FETCH_ADD elif req_type == ReqType.MEM_SWAP: tlp.fmt_type = TlpType.SWAP elif req_type == ReqType.MEM_CAS: tlp.fmt_type = TlpType.CAS elif req_type == ReqType.MEM_READ_LOCKED: tlp.fmt_type = TlpType.MEM_READ_LOCKED else: raise Exception("Invalid packet type") tlp.length = pkt.data[2] & 0x7ff tlp.requester_id = PcieId.from_int(pkt.data[2] >> 16) tlp.tag = pkt.data[3] & 0xff tlp.tc = (pkt.data[3] >> 25) & 0x7 tlp.attr = (pkt.data[3] >> 28) & 0x7 if req_type & 8 == 0: # memory, IO, or atomic operation tlp.at = pkt.data[0] & 3 tlp.address = (pkt.data[1] << 32) | (pkt.data[0] & 0xfffffffc) if tlp.address > 0xffffffff: if tlp.fmt == TlpFmt.THREE_DW: tlp.fmt = TlpFmt.FOUR_DW elif tlp.fmt == TlpFmt.THREE_DW_DATA: tlp.fmt = TlpFmt.FOUR_DW_DATA tlp.completer_id = PcieId(0, 0, (pkt.data[3] >> 8) & 0xff) tlp.bar_id = (pkt.data[3] >> 16) & 7 tlp.bar_aperture = (pkt.data[3] >> 19) & 0x3f tlp.first_be = pkt.first_be tlp.last_be = pkt.last_be tlp.discontinue = pkt.discontinue tlp.data = pkt.data[4:] # compute byte enables byte_en = [0]*4 if len(tlp.data) >= 1: byte_en += [tlp.first_be] if len(tlp.data) > 2: byte_en += [0xf] * (len(tlp.data)-2) if len(tlp.data) > 1: byte_en += [tlp.last_be] # check byte enables assert byte_en == pkt.byte_en # check parity if check_parity: assert pkt.check_parity() return tlp