From patchwork Thu Sep 10 13:21:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corentin Labbe X-Patchwork-Id: 7154151 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D189A9F326 for ; Thu, 10 Sep 2015 13:22:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0D7AC20882 for ; Thu, 10 Sep 2015 13:22:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1DB9620881 for ; Thu, 10 Sep 2015 13:22:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753028AbbIJNVy (ORCPT ); Thu, 10 Sep 2015 09:21:54 -0400 Received: from mail-wi0-f179.google.com ([209.85.212.179]:38805 "EHLO mail-wi0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753439AbbIJNVu (ORCPT ); Thu, 10 Sep 2015 09:21:50 -0400 Received: by wiclk2 with SMTP id lk2so23003069wic.1; Thu, 10 Sep 2015 06:21:49 -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=CKMUfMawMc+S6GHLAOOmSXP5Q63GF1lbIEyGMy1Ag7g=; b=Wp5b5m8ntV3ba1TaqWiN+qkqm87ojWtkxyVaG/gtHzfY3OV001u5Fdo2AB2i4Wkz+c 5Ztyx6+6H29Ut6xBxd47Ue2XnfdM5LWsBhnCw/IYqD/iRunB1VR96DulRWKKI4k+msuW Q4FkQ0ijXijSn1b1jlNfb9cXHRwl/o6VqKshQ5jJpSbp+fW+6bbDwV1VUoa12Kgslmfy sagokF+gkccqP3eTGqmHbpzZXEI35pL2f0eRVPJKgsp8/gagfdE6DPlKSTx+zL8qZZ0J q6TzQ8C3OISL8AGrH7YOwrbsYA40hm78FVLJ9paiW1hjwh2lgGqX9BzJleY+FN+bvbgO ToRA== X-Received: by 10.194.23.36 with SMTP id j4mr70135580wjf.105.1441891309535; Thu, 10 Sep 2015 06:21:49 -0700 (PDT) Received: from Red.local (ANice-651-1-384-228.w86-205.abo.wanadoo.fr. [86.205.16.228]) by smtp.googlemail.com with ESMTPSA id h6sm9354122wiy.3.2015.09.10.06.21.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Sep 2015 06:21:49 -0700 (PDT) From: LABBE Corentin To: herbert@gondor.apana.org.au, davem@davemloft.net Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, LABBE Corentin Subject: [PATCH 4/7] lib: introduce sg_nents_for_len2 Date: Thu, 10 Sep 2015 15:21:39 +0200 Message-Id: <1441891302-12014-5-git-send-email-clabbe.montjoie@gmail.com> X-Mailer: git-send-email 2.4.6 In-Reply-To: <1441891302-12014-1-git-send-email-clabbe.montjoie@gmail.com> References: <1441891302-12014-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_for_len2 in lib/scatterlist.c Signed-off-by: LABBE Corentin --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 556ec1e..316c5c4 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_len2(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..0d1f746 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -90,6 +90,45 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len) EXPORT_SYMBOL(sg_nents_for_len); /** + * sg_nents_for_len - 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 chaind or not + * + * Description: + * Determines the number of entries in sg that are required to meet + * the supplied length, taking into acount chaining as well + * + * Returns: + * the number of sg entries needed, negative error on failure + * + **/ +int sg_nents_for_len2(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_for_len2); + +/** * sg_last - return the last scatterlist entry in a list * @sgl: First entry in the scatterlist * @nents: Number of entries in the scatterlist