Received: by 10.213.65.68 with SMTP id h4csp251798imn; Mon, 12 Mar 2018 12:38:59 -0700 (PDT) X-Google-Smtp-Source: AG47ELtVhgRfVJpGpIJ6+XV3tLJSxMfP+38fJompO1RBetzUeBNMgIIfcA6AGZrtSum3jQhcFY2S X-Received: by 10.101.82.139 with SMTP id y11mr7594147pgp.68.1520883539164; Mon, 12 Mar 2018 12:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520883539; cv=none; d=google.com; s=arc-20160816; b=swYuKHhRK4x45SYbyutCBpF1wZAaiOnyROwUPOQJSQtfFCBUZNm9MChBob4ejk/MYW 0J2Jmz+dok+A0x82QgJPjFIjumx01j8TPrtP0VDDh4wDsLm+TNJtJLtyX4iU44ByU5BT nHVPZw0o65SI3Zb4R5eK3Rp8lCDG6JShnhCXdX1wSVsVJj8NLreN6kDfeROOdLP+e5AJ 07yseLfV7KqtqB24RwHMn+FYLeQ1QB1fskcYmAApnCxEVM18a3PUEWcagTcy6SAqzyrp khYOVYQQnAEjjpI52GNjiWxBUf3/AKDpuRCes3G2ZriMd/8etSKCT+oa7WOPOxPM+8AA tQmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:references:in-reply-to:message-id :date:cc:to:from:arc-authentication-results; bh=YgtlLA11ld8VnsQ3LpF+Km9b8mI2IaFRJ1vcrmpANrA=; b=hMhRpPI7uYv3+PMPES20f19LO6yzU7+HzCDKfTlOMlIJbdpqWl4L0JASu/2BsW3nBu iJi8JKmDWpIl1yBCe8ihXnv1xXIlZls9vD5jLADhiyYNn5CoA8w6xrm5IWvFqNXnX10g yUuH+/lLqhygh4fZwYoV5+h+d+hPr3UVgDoOQveJYxRsQL5B6BFykbdlVrGyGSyrOXs2 DZl92JCgBMk2rcohoxsjHoFz1fU5Oax3njCiFR/7Y76IvzCivq62JLdqCwCTQDW1q8dX PjG8kdX5ci9/wXF59eJ/TnH5gbXpRoPQN3mzGna5aFyYppFK03pzP5vE+x2W78PyLEMZ G+VQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 23si6343942pfy.85.2018.03.12.12.38.44; Mon, 12 Mar 2018 12:38:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932549AbeCLThQ (ORCPT + 99 others); Mon, 12 Mar 2018 15:37:16 -0400 Received: from ale.deltatee.com ([207.54.116.67]:54642 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932303AbeCLTfj (ORCPT ); Mon, 12 Mar 2018 15:35:39 -0400 Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1evTEJ-000665-QD; Mon, 12 Mar 2018 13:35:32 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.89) (envelope-from ) id 1evTEG-0000l3-K9; Mon, 12 Mar 2018 13:35:28 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-nvme@lists.infradead.org, linux-rdma@vger.kernel.org, linux-nvdimm@lists.01.org, linux-block@vger.kernel.org Cc: Stephen Bates , Christoph Hellwig , Jens Axboe , Keith Busch , Sagi Grimberg , Bjorn Helgaas , Jason Gunthorpe , Max Gurtovoy , Dan Williams , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Benjamin Herrenschmidt , Alex Williamson , Logan Gunthorpe Date: Mon, 12 Mar 2018 13:35:17 -0600 Message-Id: <20180312193525.2855-4-logang@deltatee.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180312193525.2855-1-logang@deltatee.com> References: <20180312193525.2855-1-logang@deltatee.com> X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-nvdimm@lists.01.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-rdma@vger.kernel.org, linux-block@vger.kernel.org, sbates@raithlin.com, hch@lst.de, axboe@kernel.dk, sagi@grimberg.me, bhelgaas@google.com, jgg@mellanox.com, maxg@mellanox.com, keith.busch@intel.com, dan.j.williams@intel.com, benh@kernel.crashing.org, jglisse@redhat.com, alex.williamson@redhat.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-6.5 required=5.0 tests=ALL_TRUSTED,BAYES_00, MYRULES_FREE,MYRULES_NO_TEXT,T_RP_MATCHES_RCVD autolearn=no autolearn_force=no version=3.4.1 Subject: [PATCH v3 03/11] PCI/P2PDMA: Add PCI p2pmem dma mappings to adjust the bus offset X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The DMA address used when mapping PCI P2P memory must be the PCI bus address. Thus, introduce pci_p2pmem_[un]map_sg() to map the correct addresses when using P2P memory. For this, we assume that an SGL passed to these functions contain all P2P memory or no P2P memory. Signed-off-by: Logan Gunthorpe --- drivers/pci/p2pdma.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/memremap.h | 1 + include/linux/pci-p2pdma.h | 13 ++++++++++++ 3 files changed, 65 insertions(+) diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index fd4789566a56..ab810c3a93eb 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -190,6 +190,8 @@ int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, pgmap->res.flags = pci_resource_flags(pdev, bar); pgmap->ref = &pdev->p2pdma->devmap_ref; pgmap->type = MEMORY_DEVICE_PCI_P2PDMA; + pgmap->pci_p2pdma_bus_offset = pci_bus_address(pdev, bar) - + pci_resource_start(pdev, bar); addr = devm_memremap_pages(&pdev->dev, pgmap); if (IS_ERR(addr)) { @@ -731,3 +733,52 @@ void pci_p2pmem_publish(struct pci_dev *pdev, bool publish) pdev->p2pdma->p2pmem_published = publish; } EXPORT_SYMBOL_GPL(pci_p2pmem_publish); + +/** + * pci_p2pdma_map_sg - map a PCI peer-to-peer sg for DMA + * @dev: device doing the DMA request + * @sg: scatter list to map + * @nents: elements in the scatterlist + * @dir: DMA direction + * + * Returns the number of SG entries mapped + */ +int pci_p2pdma_map_sg(struct device *dev, struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ + struct dev_pagemap *pgmap; + struct scatterlist *s; + phys_addr_t paddr; + int i; + + /* + * p2pdma mappings are not compatible with devices that use + * dma_virt_ops. + */ + if (IS_ENABLED(CONFIG_DMA_VIRT_OPS) && dev->dma_ops == &dma_virt_ops) + return 0; + + for_each_sg(sg, s, nents, i) { + pgmap = sg_page(s)->pgmap; + paddr = sg_phys(s); + + s->dma_address = paddr - pgmap->pci_p2pdma_bus_offset; + sg_dma_len(s) = s->length; + } + + return nents; +} +EXPORT_SYMBOL_GPL(pci_p2pdma_map_sg); + +/** + * pci_p2pdma_unmap_sg - unmap a PCI peer-to-peer sg for DMA + * @dev: device doing the DMA request + * @sg: scatter list to map + * @nents: elements in the scatterlist + * @dir: DMA direction + */ +void pci_p2pdma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, + enum dma_data_direction dir) +{ +} +EXPORT_SYMBOL_GPL(pci_p2pdma_unmap_sg); diff --git a/include/linux/memremap.h b/include/linux/memremap.h index 9e907c338a44..1660f64ce96f 100644 --- a/include/linux/memremap.h +++ b/include/linux/memremap.h @@ -125,6 +125,7 @@ struct dev_pagemap { struct device *dev; void *data; enum memory_type type; + u64 pci_p2pdma_bus_offset; }; #ifdef CONFIG_ZONE_DEVICE diff --git a/include/linux/pci-p2pdma.h b/include/linux/pci-p2pdma.h index 1f7856ff098b..59eb218bdb25 100644 --- a/include/linux/pci-p2pdma.h +++ b/include/linux/pci-p2pdma.h @@ -36,6 +36,10 @@ int pci_p2pmem_alloc_sgl(struct pci_dev *pdev, struct scatterlist **sgl, void pci_p2pmem_free_sgl(struct pci_dev *pdev, struct scatterlist *sgl, unsigned int nents); void pci_p2pmem_publish(struct pci_dev *pdev, bool publish); +int pci_p2pdma_map_sg(struct device *dev, struct scatterlist *sg, int nents, + enum dma_data_direction dir); +void pci_p2pdma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, + enum dma_data_direction dir); #else /* CONFIG_PCI_P2PDMA */ static inline int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, u64 offset) @@ -97,5 +101,14 @@ static inline void pci_p2pmem_free_sgl(struct pci_dev *pdev, static inline void pci_p2pmem_publish(struct pci_dev *pdev, bool publish) { } +static inline int pci_p2pdma_map_sg(struct device *dev, + struct scatterlist *sg, int nents, enum dma_data_direction dir) +{ + return 0; +} +static inline void pci_p2pdma_unmap_sg(struct device *dev, + struct scatterlist *sg, int nents, enum dma_data_direction dir) +{ +} #endif /* CONFIG_PCI_P2PDMA */ #endif /* _LINUX_PCI_P2P_H */ -- 2.11.0