// File: 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:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movl %ecx, 0x6c(%rsp)
movq 0x80(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x60(%rsp)
addq $0x10, %rdi
movq %rdi, 0x28(%rsp)
callq 0x882b0
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x58(%rsp)
callq 0x882e0
movq 0x58(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x34(%rsp)
testl %eax, %eax
je 0x865e6
jmp 0x865d4
movl 0x34(%rsp), %eax
subl $0x1, %eax
je 0x867dc
jmp 0x86855
movl $0x80, %edi
callq 0x8450
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
callq 0xa4180
jmp 0x86602
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq 0x70(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq %rdx, 0x70(%rcx)
movq 0x58(%rsp), %rcx
movw 0x8(%rcx), %dx
movq 0x50(%rsp), %rcx
movw %dx, 0x68(%rcx)
movq 0x8(%rax), %rax
movq 0x50(%rax), %rdi
movq 0x50(%rsp), %rax
movzwl 0x68(%rax), %esi
callq 0x85bf0
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0x14(%rsp)
subl $0x1, %eax
je 0x86713
jmp 0x8668a
movl 0x14(%rsp), %eax
subl $0x2, %eax
je 0x866b5
jmp 0x8676e
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
callq 0x8410
jmp 0x86877
movq 0x50(%rsp), %rax
movl $0x0, 0x64(%rax)
movq 0x38(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %ecx
shlq $0x1f, %rcx
movq 0x38(%rsp), %rax
movl 0x30(%rax), %eax
orq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x38(%rsp), %rax
movl 0x38(%rax), %eax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x60(%rax)
movq 0x50(%rsp), %rax
movl 0x60(%rax), %ecx
shll $0x9, %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x5c(%rax)
jmp 0x867c6
movq 0x50(%rsp), %rax
movl $0x1, 0x64(%rax)
movq 0x38(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %ecx
shlq $0x1f, %rcx
movq 0x38(%rsp), %rax
movl 0x30(%rax), %eax
orq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x38(%rsp), %rax
movl 0x38(%rax), %eax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x60(%rax)
movq 0x50(%rsp), %rax
movl 0x60(%rax), %ecx
shll $0x9, %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x5c(%rax)
jmp 0x867c6
movl $0x10, %edi
callq 0x8200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
leaq 0x27ab4(%rip), %rsi     # 0xae23e
callq 0x87d0
jmp 0x86791
movq 0x8(%rsp), %rdi
movq 0x5382b(%rip), %rsi     # 0xd9fc8
movq 0x5380c(%rip), %rdx     # 0xd9fb0
callq 0x8710
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
callq 0x8330
jmp 0x86877
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x86857
callq 0x46500
movq %rax, %rdi
callq 0x46d20
testb $0x1, %al
jne 0x867ef
jmp 0x86829
movl 0x6c(%rsp), %eax
movl %eax, %edi
callq 0x8030
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x78(%rax)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x78(%rax), %rdi
movq 0x70(%rsp), %rsi
movl 0x6c(%rsp), %eax
movl %eax, %edx
callq 0x8390
jmp 0x8683b
movq 0x70(%rsp), %rcx
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq %rcx, 0x78(%rax)
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
jmp 0x86857
jmp 0x86857
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x8686f
movq (%rsp), %rdi
callq 0x8410
addq $0x88, %rsp
retq
movq 0x48(%rsp), %rdi
callq 0x8770
nopw %cs:(%rax,%rax)