|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms and conditions of the GNU General Public License, |
| 7 | + * version 2, as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | + * more details. |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef NVFS_DMA_H |
| 16 | +#define NVFS_DMA_H |
| 17 | + |
| 18 | +static blk_status_t nvme_pci_setup_prps(struct nvme_dev *dev, |
| 19 | + struct request *req, struct nvme_rw_command *cmnd); |
| 20 | + |
| 21 | +static blk_status_t nvme_pci_setup_sgls(struct nvme_dev *dev, |
| 22 | + struct request *req, struct nvme_rw_command *cmd, int entries); |
| 23 | + |
| 24 | +static bool nvme_nvfs_unmap_data(struct nvme_dev *dev, struct request *req) |
| 25 | +{ |
| 26 | + struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 27 | + enum dma_data_direction dma_dir = rq_dma_dir(req); |
| 28 | + |
| 29 | + if (!iod || !iod->nents) |
| 30 | + return false; |
| 31 | + if (iod->sg && !is_pci_p2pdma_page(sg_page(iod->sg)) && |
| 32 | + !blk_integrity_rq(req) && |
| 33 | + !iod->dma_len && |
| 34 | + nvfs_ops != NULL) { |
| 35 | + int count; |
| 36 | + |
| 37 | + count = nvfs_ops->nvfs_dma_unmap_sg(dev->dev, iod->sg, iod->nents, |
| 38 | + dma_dir); |
| 39 | + |
| 40 | + if (!count) |
| 41 | + return false; |
| 42 | + |
| 43 | + nvfs_put_ops(); |
| 44 | + return true; |
| 45 | + } |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +static blk_status_t nvme_nvfs_map_data(struct nvme_dev *dev, struct request *req, |
| 50 | + struct nvme_command *cmnd, bool *is_nvfs_io) |
| 51 | +{ |
| 52 | + struct nvme_iod *iod = blk_mq_rq_to_pdu(req); |
| 53 | + struct request_queue *q = req->q; |
| 54 | + enum dma_data_direction dma_dir = rq_dma_dir(req); |
| 55 | + blk_status_t ret = BLK_STS_RESOURCE; |
| 56 | + int nr_mapped; |
| 57 | + |
| 58 | + nr_mapped = 0; |
| 59 | + *is_nvfs_io = false; |
| 60 | + |
| 61 | + if (!blk_integrity_rq(req) && nvfs_get_ops()) { |
| 62 | + iod->dma_len = 0; |
| 63 | + iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC); |
| 64 | + if (!iod->sg) { |
| 65 | + nvfs_put_ops(); |
| 66 | + return BLK_STS_RESOURCE; |
| 67 | + } |
| 68 | + |
| 69 | + sg_init_table(iod->sg, blk_rq_nr_phys_segments(req)); |
| 70 | + // associates bio pages to scatterlist |
| 71 | + iod->nents = nvfs_ops->nvfs_blk_rq_map_sg(q, req, iod->sg); |
| 72 | + if (!iod->nents) { |
| 73 | + mempool_free(iod->sg, dev->iod_mempool); |
| 74 | + nvfs_put_ops(); |
| 75 | + return BLK_STS_IOERR; // reset to original ret |
| 76 | + } |
| 77 | + *is_nvfs_io = true; |
| 78 | + |
| 79 | + if (unlikely((iod->nents == NVFS_IO_ERR))) { |
| 80 | + pr_err("%s: failed to map sg_nents=:%d\n", __func__, iod->nents); |
| 81 | + mempool_free(iod->sg, dev->iod_mempool); |
| 82 | + nvfs_put_ops(); |
| 83 | + return BLK_STS_IOERR; |
| 84 | + } |
| 85 | + |
| 86 | + nr_mapped = nvfs_ops->nvfs_dma_map_sg_attrs(dev->dev, |
| 87 | + iod->sg, |
| 88 | + iod->nents, |
| 89 | + dma_dir, |
| 90 | + DMA_ATTR_NO_WARN); |
| 91 | + |
| 92 | + if (unlikely((nr_mapped == NVFS_IO_ERR))) { |
| 93 | + mempool_free(iod->sg, dev->iod_mempool); |
| 94 | + nvfs_put_ops(); |
| 95 | + pr_err("%s: failed to dma map sglist=:%d\n", __func__, iod->nents); |
| 96 | + return BLK_STS_IOERR; |
| 97 | + } |
| 98 | + |
| 99 | + if (unlikely(nr_mapped == NVFS_CPU_REQ)) { |
| 100 | + mempool_free(iod->sg, dev->iod_mempool); |
| 101 | + nvfs_put_ops(); |
| 102 | + WARN_ON(1); |
| 103 | + } |
| 104 | + |
| 105 | + iod->use_sgl = nvme_pci_use_sgls(dev, req); |
| 106 | + if (iod->use_sgl) { |
| 107 | + ret = nvme_pci_setup_sgls(dev, req, &cmnd->rw, nr_mapped); |
| 108 | + } else { |
| 109 | + // push dma address to hw registers |
| 110 | + ret = nvme_pci_setup_prps(dev, req, &cmnd->rw); |
| 111 | + } |
| 112 | + |
| 113 | + if (ret != BLK_STS_OK) { |
| 114 | + nvme_nvfs_unmap_data(dev, req); |
| 115 | + mempool_free(iod->sg, dev->iod_mempool); |
| 116 | + } |
| 117 | + return ret; |
| 118 | + } |
| 119 | + return ret; |
| 120 | +} |
| 121 | + |
| 122 | +#endif /* NVFS_DMA_H */ |
0 commit comments