[PATCH v6 12/12] nvme-rdma: factor out the scatterlist DMA mapping helper
From: Mykola Marzhan
Date: Sun Jul 26 2026 - 07:37:46 EST
The data and the metadata mapping do the same thing: describe the
segments blk_rq_map_sg() produced in a local sg_table -- the embedded
one's orig_nents is the allocated size, not the valid-segment count --
map that, and hand back the DMA-mapped count. Fold both into a
helper.
No functional change.
Suggested-by: Logan Gunthorpe <logang@xxxxxxxxxxxx>
Assisted-by: Claude:claude-fable-5
Signed-off-by: Mykola Marzhan <mykola@xxxxxxxxxxx>
---
drivers/nvme/host/rdma.c | 42 +++++++++++++++++++++++++++-------------
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/drivers/nvme/host/rdma.c b/drivers/nvme/host/rdma.c
index 2f50509a7a61..525e5aea65a1 100644
--- a/drivers/nvme/host/rdma.c
+++ b/drivers/nvme/host/rdma.c
@@ -1465,11 +1465,33 @@ static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
return -EINVAL;
}
+/*
+ * Map the segments blk_rq_map_sg() produced. They are described by a local
+ * sg_table because the embedded one's orig_nents is the allocated size, not
+ * the number of valid segments; rdma_rw_ctx_init() does the same.
+ */
+static int nvme_rdma_dma_map_sgl(struct ib_device *ibdev,
+ struct scatterlist *sgl, int nents,
+ enum dma_data_direction dir, int *count)
+{
+ struct sg_table sgt = {
+ .sgl = sgl,
+ .orig_nents = nents,
+ };
+ int ret;
+
+ ret = ib_dma_map_sgtable_attrs(ibdev, &sgt, dir, 0);
+ if (unlikely(ret))
+ return ret;
+
+ *count = sgt.nents;
+ return 0;
+}
+
static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
int *count, int *pi_count)
{
struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
- struct sg_table sgt;
int ret;
req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
@@ -1481,14 +1503,10 @@ static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
req->data_sgl.nents = blk_rq_map_sg(rq, req->data_sgl.sg_table.sgl);
- sgt = (struct sg_table) {
- .sgl = req->data_sgl.sg_table.sgl,
- .orig_nents = req->data_sgl.nents,
- };
- ret = ib_dma_map_sgtable_attrs(ibdev, &sgt, rq_dma_dir(rq), 0);
+ ret = nvme_rdma_dma_map_sgl(ibdev, req->data_sgl.sg_table.sgl,
+ req->data_sgl.nents, rq_dma_dir(rq), count);
if (unlikely(ret))
goto out_free_table;
- *count = sgt.nents;
if (blk_integrity_rq(rq)) {
req->metadata_sgl->sg_table.sgl =
@@ -1504,14 +1522,12 @@ static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq,
req->metadata_sgl->sg_table.sgl);
- sgt = (struct sg_table) {
- .sgl = req->metadata_sgl->sg_table.sgl,
- .orig_nents = req->metadata_sgl->nents,
- };
- ret = ib_dma_map_sgtable_attrs(ibdev, &sgt, rq_dma_dir(rq), 0);
+ ret = nvme_rdma_dma_map_sgl(ibdev,
+ req->metadata_sgl->sg_table.sgl,
+ req->metadata_sgl->nents,
+ rq_dma_dir(rq), pi_count);
if (unlikely(ret))
goto out_free_pi_table;
- *pi_count = sgt.nents;
}
return 0;
--
2.52.0