-
Notifications
You must be signed in to change notification settings - Fork 4
/
ioqueue.cc
130 lines (115 loc) · 4.12 KB
/
ioqueue.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ioqueue.h"
void DevNvmeIoQueue::Init(DevNvme *nvme, DevNvmeAdminQueue *aq, uint16_t qid,
uint16_t sq_size, uint16_t cq_size) {
_nvme = nvme;
_queue = new DevNvmeQueue();
_queue->Init(_nvme, qid, sq_size, cq_size);
volatile CompletionQueueEntry *cqe;
cqe = aq->SubmitCmdCreateIoCompletionQueue(_queue->GetCompletionQueueMemory(),
cq_size, qid);
if (cqe->PrintIfError("CreateIoCompletionQueue")) {
exit(EXIT_FAILURE);
}
cqe = aq->SubmitCmdCreateIoSubmissionQueue(_queue->GetSubmissionQueueMemory(),
sq_size, qid, qid);
if (cqe->PrintIfError("CreateIoSubmissionQueue")) {
exit(EXIT_FAILURE);
}
printf("IO Queue %d created\n", qid);
}
void DevNvmeIoQueue::InterruptHandler() { _queue->InterruptHandler(); }
int DevNvmeIoQueue::Flush(DevNvmeNamespace *ns) {
SubmitCmdFlush(ns->GetId());
return 0;
}
volatile CompletionQueueEntry *DevNvmeIoQueue::ReadBlock(void *dst,
DevNvmeNamespace *ns,
uint64_t lba) {
assert(ns->GetBlockSize() <= 4096);
Memory prp(4096);
volatile CompletionQueueEntry *cqe;
cqe = SubmitCmdRead(&prp, ns->GetId(), lba, 1, false, false, 0);
uint8_t *buf = prp.GetVirtPtr<uint8_t>();
if (!cqe->isError()) {
memcpy(dst, buf, ns->GetBlockSize());
}
return cqe;
}
Memory _prp(4096);
volatile CompletionQueueEntry *DevNvmeIoQueue::WriteBlock(void *src,
DevNvmeNamespace *ns,
uint64_t lba) {
assert(ns->GetBlockSize() <= 4096);
uint8_t *buf = _prp.GetVirtPtr<uint8_t>();
memcpy(buf, src, ns->GetBlockSize());
return SubmitCmdWrite(&_prp, ns->GetId(), lba, 1, false, false, 0);
}
volatile CompletionQueueEntry *DevNvmeIoQueue::SubmitCmdFlush(uint32_t nsid) {
_queue->Lock();
int32_t slot = _queue->GetNextSubmissionSlot();
//
volatile CommandSet *cmd = _queue->GetCommandSet(slot);
bzero((void *)cmd, sizeof(CommandSet));
cmd->CDW0.OPC = kCmdFlush;
cmd->CDW0.CID = slot;
//
cmd->NSID = nsid;
//
_queue->SubmitCommand();
volatile CompletionQueueEntry *cqe = _queue->WaitUntilCompletion(slot);
_queue->Unlock();
cqe->PrintIfError("SubmitCmdFlush");
return cqe;
}
volatile CompletionQueueEntry *DevNvmeIoQueue::SubmitCmdRead(
Memory *prp1, uint32_t nsid, uint64_t lba, uint16_t number_of_blocks,
bool do_limited_retry, bool force_unit_access, uint8_t protection_info) {
_queue->Lock();
int32_t slot = _queue->GetNextSubmissionSlot();
//
volatile CommandSet *cmd = _queue->GetCommandSet(slot);
bzero((void *)cmd, sizeof(CommandSet));
cmd->CDW0.OPC = kCmdRead;
cmd->CDW0.CID = slot;
//
cmd->PRP1 = prp1->GetPhysPtr();
cmd->NSID = nsid;
cmd->CDW10 = lba & 0xffffffff;
cmd->CDW11 = (lba >> 32);
cmd->CDW12 |= do_limited_retry << 31;
cmd->CDW12 |= force_unit_access << 30;
cmd->CDW12 |= protection_info << 26;
cmd->CDW12 |= number_of_blocks;
//
_queue->SubmitCommand();
volatile CompletionQueueEntry *cqe = _queue->WaitUntilCompletion(slot);
_queue->Unlock();
cqe->PrintIfError("SubmitCmdRead");
return cqe;
}
volatile CompletionQueueEntry *DevNvmeIoQueue::SubmitCmdWrite(
Memory *prp1, uint32_t nsid, uint64_t lba, uint16_t number_of_blocks,
bool do_limited_retry, bool force_unit_access, uint8_t protection_info) {
_queue->Lock();
int32_t slot = _queue->GetNextSubmissionSlot();
//
volatile CommandSet *cmd = _queue->GetCommandSet(slot);
bzero((void *)cmd, sizeof(CommandSet));
cmd->CDW0.OPC = kCmdWrite;
cmd->CDW0.CID = slot;
//
cmd->PRP1 = prp1->GetPhysPtr();
cmd->NSID = nsid;
cmd->CDW10 = lba & 0xffffffff;
cmd->CDW11 = (lba >> 32);
cmd->CDW12 |= do_limited_retry << 31;
cmd->CDW12 |= force_unit_access << 30;
cmd->CDW12 |= protection_info << 26;
cmd->CDW12 |= number_of_blocks;
//
_queue->SubmitCommand();
volatile CompletionQueueEntry *cqe = _queue->WaitUntilCompletion(slot);
_queue->Unlock();
cqe->PrintIfError("SubmitCmdWrite");
return cqe;
}