Skip to content

Commit ee309ec

Browse files
committed
examples/io: Calculate and forward size in number of LBAs
Qemu uses the number of LBAs in order to traverse the "correct" number of prps for the transfer. If this value is not forwarded it acts like if there were just one PRP. Signed-off-by: Joel Granados <[email protected]>
1 parent b2bbb46 commit ee309ec

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/io.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ static struct opt_table opts[] = {
4545
OPT_ENDTABLE,
4646
};
4747

48+
unsigned int get_lb_bytes(struct nvme_ctrl *ctrl, unsigned long nsid)
49+
{
50+
void *vaddr;
51+
ssize_t len;
52+
union nvme_cmd cmd;
53+
struct nvme_id_ns *id_ns;
54+
struct nvme_lbaf *lbaf;
55+
unsigned int lb_bytes;
56+
57+
len = pgmap(&vaddr, NVME_IDENTIFY_DATA_SIZE);
58+
if (len < 0)
59+
err(1, "could not allocate aligned memory");
60+
61+
cmd.identify = (struct nvme_cmd_identify) {
62+
.opcode = nvme_admin_identify,
63+
.cns = NVME_IDENTIFY_CNS_NS,
64+
.nsid = cpu_to_le32(nsid),
65+
};
66+
67+
if (nvme_admin(ctrl, &cmd, vaddr, len, NULL))
68+
err(1, "nvme_admin");
69+
70+
id_ns = vaddr;
71+
lbaf = &id_ns->lbaf[id_ns->flbas];
72+
lb_bytes = 1ULL << lbaf->ds;
73+
74+
pgunmap(vaddr, len);
75+
return lb_bytes;
76+
}
77+
4878
int main(int argc, char **argv)
4979
{
5080
void *vaddr;
@@ -77,6 +107,8 @@ int main(int argc, char **argv)
77107
if (nvme_init(&ctrl, bdf, &ctrl_opts))
78108
err(1, "failed to init nvme controller");
79109

110+
unsigned int lb_nbytes = get_lb_bytes(&ctrl, nsid);
111+
80112
if (nvme_create_ioqpair(&ctrl, 1, 64, -1, 0x0))
81113
err(1, "could not create io queue pair");
82114

@@ -100,6 +132,7 @@ int main(int argc, char **argv)
100132
cmd.rw = (struct nvme_cmd_rw) {
101133
.opcode = op_write ? nvme_cmd_write : nvme_cmd_read,
102134
.nsid = cpu_to_le32(nsid),
135+
.nlb = (ROUND_UP(op_len, lb_nbytes)/lb_nbytes) - 1,
103136
};
104137

105138
ret = nvme_rq_map_prp(&ctrl, rq, &cmd, iova, op_len);

0 commit comments

Comments
 (0)