From patchwork Fri Oct 21 17:35:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 9389821 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F02DC607D0 for ; Fri, 21 Oct 2016 17:36:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E43122A180 for ; Fri, 21 Oct 2016 17:36:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D93F42A21D; Fri, 21 Oct 2016 17:36:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87D342A180 for ; Fri, 21 Oct 2016 17:36:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932787AbcJURgK (ORCPT ); Fri, 21 Oct 2016 13:36:10 -0400 Received: from mga03.intel.com ([134.134.136.65]:37028 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932730AbcJURgJ (ORCPT ); Fri, 21 Oct 2016 13:36:09 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP; 21 Oct 2016 10:36:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,377,1473145200"; d="scan'208";a="892553096" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 21 Oct 2016 10:36:08 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 625F9D1; Fri, 21 Oct 2016 20:35:36 +0300 (EEST) From: Andy Shevchenko To: dmaengine@vger.kernel.org, vinod.koul@intel.com Cc: Andy Shevchenko Subject: [PATCH v1 01/10] scatterlist: introduce sg_nents_for_dma() helper Date: Fri, 21 Oct 2016 20:35:26 +0300 Message-Id: <20161021173535.100245-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.9.3 Sender: dmaengine-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Andy Shevchenko Sometimes the user needs to split each entry on the mapped scatter list due to DMA length constrains. This helper returns a number of entities assuming that each of them is not bigger than supplied maximum length. Signed-off-by: Andy Shevchenko --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index cb3c8fe..d199667 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -243,6 +243,7 @@ static inline void *sg_virt(struct scatterlist *sg) int sg_nents(struct scatterlist *sg); int sg_nents_for_len(struct scatterlist *sg, u64 len); +int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len); struct scatterlist *sg_next(struct scatterlist *); struct scatterlist *sg_last(struct scatterlist *s, unsigned int); void sg_init_table(struct scatterlist *, unsigned int); diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 004fc70..5b75a21 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -90,6 +90,31 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len) EXPORT_SYMBOL(sg_nents_for_len); /** + * sg_nents_for_dma - return count of DMA-capable entries in scatterlist + * @sgl: The scatterlist + * @sglen: The current number of entries + * @len: The maximum length of DMA-capable block + * + * Description: + * Determines the number of entries in @sgl which would be permitted in + * DMA-capable transfer if list had been split accordingly, taking into + * account chaining as well. + * + * Returns: + * the number of sgl entries needed + * + **/ +int sg_nents_for_dma(struct scatterlist *sgl, unsigned int sglen, size_t len) +{ + struct scatterlist *sg; + int i, nents = 0; + + for_each_sg(sgl, sg, sglen, i) + nents += DIV_ROUND_UP(sg_dma_len(sg), len); + return nents; +} + +/** * sg_last - return the last scatterlist entry in a list * @sgl: First entry in the scatterlist * @nents: Number of entries in the scatterlist