// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_NVMe.cpp
// Function: SSD_Components::Request_Fetch_Unit_NVMe::Process_pcie_read_message(unsigned long, void*, unsigned int)
void Request_Fetch_Unit_NVMe::Process_pcie_read_message(uint64_t address, void *payload, unsigned int payload_size)
{
	Host_Interface_NVMe *hi = (Host_Interface_NVMe *)host_interface;
	DMA_Req_Item *dma_req_item = dma_list.front();
	dma_list.pop_front();

	switch (dma_req_item->Type)
	{
	case DMA_Req_Type::REQUEST_INFO:
	{
		User_Request *new_request = new User_Request;
		new_request->IO_command_info = payload;
		new_request->Stream_id = (stream_id_type)((uint64_t)(dma_req_item->object));
		new_request->Priority_class = ((Input_Stream_Manager_NVMe *)host_interface->input_stream_manager)->Get_priority_class(new_request->Stream_id);
		new_request->STAT_InitiationTime = Simulator->Time();
		Submission_Queue_Entry *sqe = (Submission_Queue_Entry *)payload;
		switch (sqe->Opcode)
		{
		case NVME_READ_OPCODE:
			new_request->Type = UserRequestType::READ;
			new_request->Start_LBA = ((LHA_type)sqe->Command_specific[1]) << 31 | (LHA_type)sqe->Command_specific[0]; //Command Dword 10 and Command Dword 11
			new_request->SizeInSectors = sqe->Command_specific[2] & (LHA_type)(0x0000ffff);
			new_request->Size_in_byte = new_request->SizeInSectors * SECTOR_SIZE_IN_BYTE;
			break;
		case NVME_WRITE_OPCODE:
			new_request->Type = UserRequestType::WRITE;
			new_request->Start_LBA = ((LHA_type)sqe->Command_specific[1]) << 31 | (LHA_type)sqe->Command_specific[0]; //Command Dword 10 and Command Dword 11
			new_request->SizeInSectors = sqe->Command_specific[2] & (LHA_type)(0x0000ffff);
			new_request->Size_in_byte = new_request->SizeInSectors * SECTOR_SIZE_IN_BYTE;
			break;
		default:
			throw std::invalid_argument("NVMe command is not supported!");
		}
		((Input_Stream_Manager_NVMe *)(hi->input_stream_manager))->Handle_new_arrived_request(new_request);
		break;
	}
	case DMA_Req_Type::WRITE_DATA:
		COPYDATA(((User_Request *)dma_req_item->object)->Data, payload, payload_size);
		((Input_Stream_Manager_NVMe *)(hi->input_stream_manager))->Handle_arrived_write_data((User_Request *)dma_req_item->object);
		break;
	default:
		break;
	}
	delete dma_req_item;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %r12
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r15
movq 0x10(%r15), %rbx
decq 0x20(%rdi)
movq %r15, %rdi
callq 0x7580
movq %r15, %rdi
callq 0x7370
movl (%rbx), %eax
cmpl $0x1, %eax
je 0x417b3
testl %eax, %eax
jne 0x41834
movl $0x80, %edi
callq 0x73a0
movq %rax, %r15
movq %rax, %rdi
callq 0x5181c
movq %r14, 0x70(%r15)
movq 0x8(%rbx), %rax
movw %ax, 0x68(%r15)
movq 0x8(%r12), %rcx
movq 0x50(%rcx), %rcx
movzwl %ax, %eax
movq 0x10(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x48(%rax), %eax
movl %eax, (%r15)
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq %rax, 0x30(%r15)
movzbl (%r14), %ecx
cmpl $0x1, %ecx
je 0x417f6
xorl %eax, %eax
cmpl $0x2, %ecx
je 0x417fb
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x187e5(%rip), %rsi     # 0x59f7a
movq %rax, %rdi
callq 0x7680
movq 0x2d824(%rip), %rsi     # 0x6efc8
movq 0x2d7fd(%rip), %rdx     # 0x6efa8
movq %rbx, %rdi
callq 0x75e0
callq 0x28018
movq %rax, %rdi
callq 0x28484
testb %al, %al
je 0x417e7
movl %ebp, %r12d
movq %r12, %rdi
callq 0x7030
movq 0x8(%rbx), %r15
movq %rax, 0x78(%r15)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x7310
jmp 0x417ef
movq 0x8(%rbx), %r15
movq %r14, 0x78(%r15)
movl $0x18, %eax
jmp 0x41827
movl $0x1, %eax
movl %eax, 0x64(%r15)
movl 0x30(%r14), %eax
movl 0x34(%r14), %ecx
shlq $0x1f, %rcx
orq %rcx, %rax
movq %rax, 0x28(%r15)
movzwl 0x38(%r14), %eax
movl %eax, 0x60(%r15)
shll $0x9, %eax
movl %eax, 0x5c(%r15)
movl $0x10, %eax
movq 0x50(%r13), %rdi
movq (%rdi), %rcx
movq %r15, %rsi
callq *(%rcx,%rax)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7370
movq %rax, %r14
movq %rbx, %rdi
callq 0x72c0
jmp 0x41862
movq %rax, %r14
movq %r15, %rdi
callq 0x7370
movq %r14, %rdi
callq 0x7620