From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753722AbbFAQau (ORCPT ); Mon, 1 Jun 2015 12:30:50 -0400 Received: from mail-bl2on0101.outbound.protection.outlook.com ([65.55.169.101]:9488 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751771AbbFAQam (ORCPT ); Mon, 1 Jun 2015 12:30:42 -0400 X-Greylist: delayed 889 seconds by postgrey-1.27 at vger.kernel.org; Mon, 01 Jun 2015 12:30:42 EDT Authentication-Results: spf=none (sender IP is 165.204.84.221) smtp.mailfrom=amd.com; davemloft.net; dkim=none (message not signed) header.d=none; X-WSS-ID: 0NP9X6C-07-QMT-02 X-M-MSG: Subject: [PATCH v1 1/2] scatterlist: introduce sg_nents_for_len From: Tom Lendacky To: CC: David Miller , , Herbert Xu Date: Mon, 1 Jun 2015 11:15:25 -0500 Message-ID: <20150601161525.729.58208.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20150601161519.729.75909.stgit@tlendack-t1.amdoffice.net> References: <20150601161519.729.75909.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1BFFO11FD008;1:XTVMiTgKhk1piaH/QxxGqKhOCXJE/NU1hoPl24A5wkQIE0VadHnelO/TuejXSVxaFbN35bWA3gosxVIg+2YlZ/isnop3ICp9HS04DG/+qo8r39U3OsVzYT/f5Fp4qOHLDlM5yjnhJnky7AL53KfvD74mi0jG396jzo0YHz3ZfwxlLjqGrXTkLiBZfIRvLA36nSEpxnfmv6AnFE/F3TRPqV0LGz3ALxrktG4IWjKNYYn/PeBqU1obJ8i6joPwugGjOxwgWQXsWNb70Fm8lofaxrir3DcUU5InDlhPdEupyR2WNVDR+j1Mrqlyotc6VlaH X-Forefront-Antispam-Report: CIP:165.204.84.221;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(428002)(50944005)(199003)(189002)(103116003)(110136002)(106466001)(19580405001)(229853001)(77156002)(62966003)(23676002)(92566002)(87936001)(575784001)(4001350100001)(4001540100001)(5001830100001)(5001860100001)(33646002)(189998001)(2950100001)(2351001)(68736005)(86362001)(97736004)(77096005)(76176999)(54356999)(105586002)(101416001)(53416004)(83506001)(46102003)(50466002)(97746001)(19580395003)(50986999)(64706001)(47776003)(71626006);DIR:OUT;SFP:1102;SCL:1;SRVR:BN1PR02MB072;H:atltwp01.amd.com;FPR:;SPF:None;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN1PR02MB072; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(520003)(5005006)(3002001);SRVR:BN1PR02MB072;BCL:0;PCL:0;RULEID:;SRVR:BN1PR02MB072; X-Forefront-PRVS: 05947791E4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jun 2015 16:15:50.0606 (UTC) X-MS-Exchange-CrossTenant-Id: fde4dada-be84-483f-92cc-e026cbee8e96 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fde4dada-be84-483f-92cc-e026cbee8e96;Ip=[165.204.84.221];Helo=[atltwp01.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN1PR02MB072 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When performing a dma_map_sg() call, the number of sg entries to map is required. Using sg_nents to retrieve the number of sg entries will return the total number of entries in the sg list up to the entry marked as the end. If there happen to be unused entries in the list, these will still be counted. Some dma_map_sg() implementations will not handle the unused entries correctly (lib/swiotlb.c) and execute a BUG_ON. The sg_nents_for_len() function will traverse the sg list and return the number of entries required to satisfy the supplied length argument. This can then be supplied to the dma_map_sg() call to successfully map the sg. Signed-off-by: Tom Lendacky --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index ed8f9e7..a0edb99 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -221,6 +221,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); 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 c9f2e8c..99fbc2f 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -56,6 +56,38 @@ int sg_nents(struct scatterlist *sg) } EXPORT_SYMBOL(sg_nents); +/** + * 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 + * + * 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_len(struct scatterlist *sg, u64 len) +{ + int nents; + u64 total; + + if (!len) + return 0; + + for (nents = 0, total = 0; sg; sg = sg_next(sg)) { + nents++; + total += sg->length; + if (total >= len) + return nents; + } + + return -EINVAL; +} +EXPORT_SYMBOL(sg_nents_for_len); /** * sg_last - return the last scatterlist entry in a list