From patchwork Tue Mar 8 11:15:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris BREZILLON X-Patchwork-Id: 8532131 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1C955C0553 for ; Tue, 8 Mar 2016 11:28:42 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2E22B20165 for ; Tue, 8 Mar 2016 11:28:41 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 40028201D3 for ; Tue, 8 Mar 2016 11:28:40 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1adFnd-0006On-LN; Tue, 08 Mar 2016 11:27:37 +0000 Received: from down.free-electrons.com ([37.187.137.238] helo=mail.free-electrons.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1adFcX-00058n-8c; Tue, 08 Mar 2016 11:16:13 +0000 Received: by mail.free-electrons.com (Postfix, from userid 110) id F059D263; Tue, 8 Mar 2016 12:15:29 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from localhost.localdomain (AToulouse-657-1-1129-172.w92-156.abo.wanadoo.fr [92.156.51.172]) by mail.free-electrons.com (Postfix) with ESMTPSA id C44CB608; Tue, 8 Mar 2016 12:15:28 +0100 (CET) From: Boris Brezillon To: Andrew Morton , Dave Gordon , David Woodhouse , Brian Norris , linux-mtd@lists.infradead.org Subject: [PATCH 5/7] mtd: provide helper to prepare buffers for DMA operations Date: Tue, 8 Mar 2016 12:15:13 +0100 Message-Id: <1457435715-24740-6-git-send-email-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1457435715-24740-1-git-send-email-boris.brezillon@free-electrons.com> References: <1457435715-24740-1-git-send-email-boris.brezillon@free-electrons.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160308_031609_890653_85B8EA95 X-CRM114-Status: GOOD ( 16.86 ) X-Spam-Score: -1.9 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , devicetree@vger.kernel.org, Pawel Moll , Ian Campbell , Vinod Koul , Boris Brezillon , linux-sunxi@googlegroups.com, linux-kernel@vger.kernel.org, Rob Herring , linux-spi@vger.kernel.org, Chen-Yu Tsai , Mark Brown , Hans Verkuil , Laurent Pinchart , Kumar Gala , dmaengine@vger.kernel.org, Maxime Ripard , linux-media@vger.kernel.org, Dan Williams , linux-arm-kernel@lists.infradead.org, Mauro Carvalho Chehab MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Some NAND controller drivers are making use of DMA to transfer data from the controller to the buffer passed by the MTD user. Provide a generic mtd_map/unmap_buf() implementation to avoid open coded (and sometime erroneous) implementations. Signed-off-by: Boris Brezillon --- drivers/mtd/mtdcore.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/mtd.h | 25 +++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 3096251..3c368f0 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -1253,6 +1253,72 @@ void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) } EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); +#ifdef CONFIG_HAS_DMA +/** + * mtd_map_buf - create an SG table and prepare it for DMA operations + * + * @mtd: mtd device description object pointer + * @dev: device handling the DMA operation + * @buf: buf used to create the SG table + * @len: length of buf + * @constraints: optional constraints to take into account when creating + * the SG table. Can be NULL if no specific constraints + * are required. + * @dir: direction of the DMA operation + * + * This function should be used when an MTD driver wants to do DMA operations + * on a buffer passed by the MTD layer. This functions takes care of + * vmallocated buffer constraints, and return and sg_table that you can safely + * use. + */ +int mtd_map_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, const void *buf, size_t len, + const struct sg_constraints *constraints, + enum dma_data_direction dir) +{ + int ret; + + ret = sg_alloc_table_from_buf(sgt, buf, len, constraints, GFP_KERNEL); + if (ret) + return ret; + + ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); + if (!ret) + ret = -ENOMEM; + + if (ret < 0) { + sg_free_table(sgt); + return ret; + } + + sgt->nents = ret; + + return 0; +} +EXPORT_SYMBOL_GPL(mtd_map_buf); + +/** + * mtd_map_buf - unmap an SG table and release its resources + * + * @mtd: mtd device description object pointer + * @dev: device handling the DMA operation + * @sgt: SG table + * @dir: direction of the DMA operation + * + * This function unmaps a previously mapped SG table and release SG table + * resources. Should be called when your DMA operation is done. + */ +void mtd_unmap_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir) +{ + if (sgt->orig_nents) { + dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); + sg_free_table(sgt); + } +} +EXPORT_SYMBOL_GPL(mtd_unmap_buf); +#endif /* !CONFIG_HAS_DMA */ + #ifdef CONFIG_PROC_FS /*====================================================================*/ diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index cc84923..11c63f1 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -406,6 +407,30 @@ extern void register_mtd_user (struct mtd_notifier *new); extern int unregister_mtd_user (struct mtd_notifier *old); void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size); +#ifdef CONFIG_HAS_DMA +int mtd_map_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, const void *buf, size_t len, + const struct sg_constraints *constraints, + enum dma_data_direction dir); +void mtd_unmap_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir); +#else +static inline int mtd_map_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, const void *buf, + size_t len, + const struct sg_constraints *constraints + enum dma_data_direction dir) +{ + return -ENOTSUPP; +} + +static void mtd_unmap_buf(struct mtd_info *mtd, struct device *dev, + struct sg_table *sgt, enum dma_data_direction dir) +{ + return -ENOTSUPP; +} +#endif + void mtd_erase_callback(struct erase_info *instr); static inline int mtd_is_bitflip(int err) {