// File: CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_SATA.cpp
// Function: SSD_Components::Request_Fetch_Unit_SATA::Process_pcie_read_message(unsigned long, void*, unsigned int)
void Request_Fetch_Unit_SATA::Process_pcie_read_message(uint64_t address, void * payload, unsigned int payload_size)
	{
		Host_Interface_SATA* hi = (Host_Interface_SATA*)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->STAT_InitiationTime = Simulator->Time();
				Submission_Queue_Entry* sqe = (Submission_Queue_Entry*)payload;
				switch (sqe->Opcode)
				{
					case SATA_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 SATA_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("SATA command is not supported!");
				}
				((Input_Stream_Manager_SATA*)(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_SATA*)(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 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 0x89ea1
jmp 0x89e8f
movl 0x34(%rsp), %eax
subl $0x1, %eax
je 0x8a073
jmp 0x8a0ec
movl $0x80, %edi
callq 0x8450
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
callq 0xa4180
jmp 0x89ebd
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x70(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x70(%rax)
movq 0x58(%rsp), %rax
movw 0x8(%rax), %cx
movq 0x50(%rsp), %rax
movw %cx, 0x68(%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, 0x1c(%rsp)
subl $0x1, %eax
je 0x89faa
jmp 0x89f21
movl 0x1c(%rsp), %eax
subl $0x2, %eax
je 0x89f4c
jmp 0x8a005
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
callq 0x8410
jmp 0x8a110
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 0x8a05d
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 0x8a05d
movl $0x10, %edi
callq 0x8200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
leaq 0x244bd(%rip), %rsi     # 0xae4de
callq 0x87d0
jmp 0x8a028
movq 0x10(%rsp), %rdi
movq 0x4ff94(%rip), %rsi     # 0xd9fc8
movq 0x4ff75(%rip), %rdx     # 0xd9fb0
callq 0x8710
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
callq 0x8330
jmp 0x8a110
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x8a0ee
callq 0x46500
movq %rax, %rdi
callq 0x46d20
testb $0x1, %al
jne 0x8a086
jmp 0x8a0c0
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 0x8a0d2
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 0x8a0ee
jmp 0x8a0ee
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x8a108
movq 0x8(%rsp), %rdi
callq 0x8410
addq $0x88, %rsp
retq
movq 0x48(%rsp), %rdi
callq 0x8770
nopw (%rax,%rax)