From patchwork Thu Mar 31 12:29:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris BREZILLON X-Patchwork-Id: 33754 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.84_2) (envelope-from ) id 1alblH-0000Su-Es; Thu, 31 Mar 2016 12:31:43 +0000 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.76/mailfrontend-5) with esmtp id 1alblF-0004yV-7k; Thu, 31 Mar 2016 14:31:43 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756307AbcCaMbQ (ORCPT + 1 other); Thu, 31 Mar 2016 08:31:16 -0400 Received: from down.free-electrons.com ([37.187.137.238]:42009 "EHLO mail.free-electrons.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754429AbcCaM3w (ORCPT ); Thu, 31 Mar 2016 08:29:52 -0400 Received: by mail.free-electrons.com (Postfix, from userid 110) id 9CF8B1821; Thu, 31 Mar 2016 14:29:49 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.free-electrons.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost.localdomain (LMontsouris-657-1-184-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id EB569181E; Thu, 31 Mar 2016 14:29:48 +0200 (CEST) From: Boris Brezillon To: David Woodhouse , Brian Norris , linux-mtd@lists.infradead.org, Andrew Morton , Dave Gordon Cc: Mark Brown , linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Vinod Koul , Dan Williams , dmaengine@vger.kernel.org, Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , linux-media@vger.kernel.org, Boris Brezillon , Richard Weinberger , Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org, Vignesh R , linux-mm@kvack.org, Joerg Roedel , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/4] mtd: provide helper to prepare buffers for DMA operations Date: Thu, 31 Mar 2016 14:29:44 +0200 Message-Id: <1459427384-21374-5-git-send-email-boris.brezillon@free-electrons.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1459427384-21374-1-git-send-email-boris.brezillon@free-electrons.com> References: <1459427384-21374-1-git-send-email-boris.brezillon@free-electrons.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2016.3.31.122416 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODY_SIZE_4000_4999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, NO_URI_HTTPS 0, REFERENCES 0, SINGLE_URI_IN_BODY 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MULTIPLE_RCPTS_CC_X2 0, __MULTIPLE_RCPTS_TO_X5 0, __REFERENCES 0, __SANE_MSGID 0, __SINGLE_URI_TEXT 0, __SUBJ_ALPHA_END 0, __TO_MALFORMED_2 0, __URI_IN_BODY 0, __URI_NO_WWW 0, __URI_NS , __URI_WITH_PATH 0' 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..4c20f33 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_unmap_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 7712721..15cff85 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -410,6 +411,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) {