Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2164043pxb; Fri, 17 Sep 2021 03:49:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxIgIsZimHo90pZCWQF1b1mbriUzUFsjGUJTU+RPffq0Do13ny6GQTdKo26iRYq5LZeJhkx X-Received: by 2002:aa7:d8c5:: with SMTP id k5mr11527781eds.194.1631875778857; Fri, 17 Sep 2021 03:49:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631875778; cv=none; d=google.com; s=arc-20160816; b=QHkhVwdkMH7tNOaD4mWWsdkhaMluVs+jdtw0tCVYk92h01RFeKgUO5fr/4U3vkr3sl orjbW5e8dd1psrxV8rhfwoRrVvZB1/ilLazMSOhcoMLuI3NhnEOs5k4n9dBuV1niDWLW M+4CJitnmdqDkvDGNCMHtLeSP9n2+pgMJPEpHBzUJsaXHFRlyBl4RDqSfbe6n1/9IC6F RLTdoWzhEUzLEBeGOXn4PgNUOWfnQAG+z6mMrNBUjvDzwWOoAejuja9QerE3S8hwqRh9 QCPtQvXXR97kxv5c2kMhuzJ1YwcQsI0BgzNXyf0jSOWGByf5cI113jkESh8ygp4Y0Ftu oyIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:cc:to:from:dkim-signature; bh=hGAN7PsX7nCUkaGWKgSS7ihkI9wmA4pPKB/9Vt5A1Dc=; b=dM2I7MGUNSR67w5UZIKxIeNe8xm//osKHu+OXG9TlS3bnKoxSW+IEyJQYw+3jWWepf RRkBj5vq7Fa54OMkXZEPThNGBzbF4+ceUL6OBwXC6cUMdDYhrRM9VFmM+0fmkqdieEyP nC2SawFnw2lfHigxWtw8v5nhMyABAFSSZJmS7sK3tD5U0g+h+xoIz8/LyqlhxTNh7TWX 2R7TuaaSK+mdOOnB6vO43uB23G6Mqb3A/HYYaY3U2ata3RuRQgyV3fQPFYgXlRMOoGcA 9SklRVwB/eDgmKgUmuHOsPWLsMtrg4VAreO2LnZFxtUFa8MaZZ1e1LCpsqBTArDvFmnE WOOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=Bk9cEz8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deltatee.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q12si3124452edi.328.2021.09.17.03.49.15; Fri, 17 Sep 2021 03:49:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@deltatee.com header.s=20200525 header.b=Bk9cEz8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deltatee.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241982AbhIPXna (ORCPT + 99 others); Thu, 16 Sep 2021 19:43:30 -0400 Received: from ale.deltatee.com ([204.191.154.188]:40774 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241734AbhIPXmj (ORCPT ); Thu, 16 Sep 2021 19:42:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=deltatee.com; s=20200525; h=Subject:MIME-Version:References:In-Reply-To: Message-Id:Date:Cc:To:From:content-disposition; bh=hGAN7PsX7nCUkaGWKgSS7ihkI9wmA4pPKB/9Vt5A1Dc=; b=Bk9cEz8oLRi1CuoYYkNeOzBA0z sTEMiMCRsKfzYBjLDSUI5SoM4HbeRKNbSv5sIhcSHqYDHb9KJEaNTHAHAstILIhKv5++sop8pJuT8 zTav0zFQrqJbZKq7nTEn4TE0obF/LT3hhVTmteixoxAGi1cegqAUdpnJqKMfu5+jLVJ57MnfGTG3l zUwJTc5NC6DbIRnvz/MmcTaCFPWKiBPfPNvvyhaZbDVTz/xw/NZDiz1E5pnGvndc3CmYlYT3L+Axt Bv3Y7wtem0+LTVRp7jl4JOMpLc38YTwlqp14GR+nNTAsW2hR8CQO75SqeDAYZURkrRBeLDN19OZ+Z gDVNQg+w==; Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mR10G-0008I0-Nl; Thu, 16 Sep 2021 17:41:17 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.94.2) (envelope-from ) id 1mR104-000Vr4-9z; Thu, 16 Sep 2021 17:41:04 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Stephen Bates , Christoph Hellwig , Dan Williams , Jason Gunthorpe , =?UTF-8?q?Christian=20K=C3=B6nig?= , John Hubbard , Don Dutile , Matthew Wilcox , Daniel Vetter , Jakowski Andrzej , Minturn Dave B , Jason Ekstrand , Dave Hansen , Xiong Jianxin , Bjorn Helgaas , Ira Weiny , Robin Murphy , Martin Oliveira , Chaitanya Kulkarni , Logan Gunthorpe Date: Thu, 16 Sep 2021 17:40:46 -0600 Message-Id: <20210916234100.122368-7-logang@deltatee.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210916234100.122368-1-logang@deltatee.com> References: <20210916234100.122368-1-logang@deltatee.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, sbates@raithlin.com, hch@lst.de, jgg@ziepe.ca, christian.koenig@amd.com, jhubbard@nvidia.com, ddutile@redhat.com, willy@infradead.org, daniel.vetter@ffwll.ch, jason@jlekstrand.net, dave.hansen@linux.intel.com, helgaas@kernel.org, dan.j.williams@intel.com, andrzej.jakowski@intel.com, dave.b.minturn@intel.com, jianxin.xiong@intel.com, ira.weiny@intel.com, robin.murphy@arm.com, martin.oliveira@eideticom.com, ckulkarnilinux@gmail.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-6.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, MYRULES_NO_TEXT autolearn=no autolearn_force=no version=3.4.2 Subject: [PATCH v3 06/20] dma-direct: support PCI P2PDMA pages in dma-direct map_sg X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add PCI P2PDMA support for dma_direct_map_sg() so that it can map PCI P2PDMA pages directly without a hack in the callers. This allows for heterogeneous SGLs that contain both P2PDMA and regular pages. A P2PDMA page may have three possible outcomes when being mapped: 1) If the data path between the two devices doesn't go through the root port, then it should be mapped with a PCI bus address 2) If the data path goes through the host bridge, it should be mapped normally, as though it were a CPU physical address 3) It is not possible for the two devices to communicate and thus the mapping operation should fail (and it will return -EREMOTEIO). SGL segments that contain PCI bus addresses are marked with sg_dma_mark_pci_p2pdma() and are ignored when unmapped. Signed-off-by: Logan Gunthorpe --- kernel/dma/direct.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 4c6c5e0635e3..fa8317e8ff44 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "direct.h" /* @@ -421,29 +422,60 @@ void dma_direct_sync_sg_for_cpu(struct device *dev, arch_sync_dma_for_cpu_all(); } +/* + * Unmaps segments, except for ones marked as pci_p2pdma which do not + * require any further action as they contain a bus address. + */ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) { struct scatterlist *sg; int i; - for_each_sg(sgl, sg, nents, i) - dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, - attrs); + for_each_sg(sgl, sg, nents, i) { + if (sg_is_dma_pci_p2pdma(sg)) { + sg_dma_unmark_pci_p2pdma(sg); + } else { + dma_direct_unmap_page(dev, sg->dma_address, + sg_dma_len(sg), dir, attrs); + } + } } #endif int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) { - int i; + struct pci_p2pdma_map_state p2pdma_state = {}; + enum pci_p2pdma_map_type map; struct scatterlist *sg; + int i, ret; for_each_sg(sgl, sg, nents, i) { + if (is_pci_p2pdma_page(sg_page(sg))) { + map = pci_p2pdma_map_segment(&p2pdma_state, dev, sg); + switch (map) { + case PCI_P2PDMA_MAP_BUS_ADDR: + continue; + case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: + /* + * Mapping through host bridge should be + * mapped normally, thus we do nothing + * and continue below. + */ + break; + default: + ret = -EREMOTEIO; + goto out_unmap; + } + } + sg->dma_address = dma_direct_map_page(dev, sg_page(sg), sg->offset, sg->length, dir, attrs); - if (sg->dma_address == DMA_MAPPING_ERROR) + if (sg->dma_address == DMA_MAPPING_ERROR) { + ret = -EIO; goto out_unmap; + } sg_dma_len(sg) = sg->length; } @@ -451,7 +483,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, out_unmap: dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); - return -EIO; + return ret; } dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, -- 2.30.2