From patchwork Fri Sep 18 12:57:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corentin Labbe X-Patchwork-Id: 7216081 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@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 82A25BEEC1 for ; Fri, 18 Sep 2015 12:59:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 98A96208B8 for ; Fri, 18 Sep 2015 12:59:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9DCD6208D2 for ; Fri, 18 Sep 2015 12:59:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753895AbbIRM6X (ORCPT ); Fri, 18 Sep 2015 08:58:23 -0400 Received: from mail-wi0-f174.google.com ([209.85.212.174]:37280 "EHLO mail-wi0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753827AbbIRM6V (ORCPT ); Fri, 18 Sep 2015 08:58:21 -0400 Received: by wicfx3 with SMTP id fx3so29944859wic.0; Fri, 18 Sep 2015 05:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=9REfYkIq27KhZKZ724TfwlTxjEk0T1sLmxYzpKAlP88=; b=XSpXT6bkHQW1sggJ6cZQ4HzmLbTSpoTcWZEUeoRUzUuSc6/QB6PsFBjHqLKOjubuTH 41Y6un/FH8tdF4oSjsQSrFwmz93wc6A9eiu50bFLY62mFF9vglLTv6LPKAJAnF1+EF6R M7Hx1wff2RtWcOHPaNaqOi6O1eLd8Qvl00LfZyArclGoI2tCwlfLZ9gpghloQ4JKkfHl jd37baYBL35+RuGYAz7QQfUu0We7dwfBGKN8UeQVu0CKvQc73dyF1mQAw8+OUNo3qzmc KPD/4C1687X77oJ8qDxzNRfO5F0eei7tfy6P1ArZbk2MOydNTVdrAP1LkqjC/+qDeda0 +vFw== X-Received: by 10.194.7.197 with SMTP id l5mr7831250wja.153.1442581100102; Fri, 18 Sep 2015 05:58:20 -0700 (PDT) Received: from Red.local (ANice-651-1-293-37.w83-201.abo.wanadoo.fr. [83.201.193.37]) by smtp.googlemail.com with ESMTPSA id kb5sm8789638wjc.17.2015.09.18.05.58.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 18 Sep 2015 05:58:19 -0700 (PDT) From: LABBE Corentin To: herbert@gondor.apana.org.au, davem@davemloft.net, akpm@linux-foundation.org, arnd@arndb.de, axboe@fb.com, clabbe.montjoie@gmail.com, david.s.gordon@intel.com, martin.petersen@oracle.com, robert.jarzmik@free.fr, thomas.lendacky@amd.com, tonyb@cybernetics.com Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Subject: [PATCH v2 5/8] lib: introduce sg_nents_len_chained Date: Fri, 18 Sep 2015 14:57:13 +0200 Message-Id: <1442581036-23789-6-git-send-email-clabbe.montjoie@gmail.com> X-Mailer: git-send-email 2.4.6 In-Reply-To: <1442581036-23789-1-git-send-email-clabbe.montjoie@gmail.com> References: <1442581036-23789-1-git-send-email-clabbe.montjoie@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some driver use a modified version of sg_nents_for_len with an additional parameter bool *chained for knowing if the scatterlist is chained or not. So, for removing duplicate code, add sg_nents_len_chained in lib/scatterlist.c Signed-off-by: LABBE Corentin --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 556ec1e..594cdb0 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_len_chained(struct scatterlist *sg, u64 len, bool *chained); 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 bafa993..070e396 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -90,6 +90,46 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len) EXPORT_SYMBOL(sg_nents_for_len); /** + * sg_nents_len_chained - return total count of entries in scatterlist + * needed to satisfy the supplied length + * @sg: The scatterlist + * @len: The total required length + * @chained A pointer where to store if SG is chained or not + * + * Description: + * Determines the number of entries in sg that are required to meet + * the supplied length, taking into account chaining as well + * If the scatterlist is chained, set *chained to true. + * + * Returns: + * the number of sg entries needed, negative error on failure + * + **/ +int sg_nents_len_chained(struct scatterlist *sg, u64 len, bool *chained) +{ + int nents; + u64 total; + + if (chained) + *chained = false; + + if (!len) + return 0; + + for (nents = 0, total = 0; sg; sg = sg_next(sg)) { + nents++; + total += sg->length; + if (!sg_is_last(sg) && (sg + 1)->length == 0 && chained) + *chained = true; + if (total >= len) + return nents; + } + + return -EINVAL; +} +EXPORT_SYMBOL(sg_nents_len_chained); + +/** * sg_last - return the last scatterlist entry in a list * @sgl: First entry in the scatterlist * @nents: Number of entries in the scatterlist