From patchwork Wed Sep 9 06:57:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Porosanu Alexandru X-Patchwork-Id: 7144701 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 9C08D9F314 for ; Wed, 9 Sep 2015 06:57:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DCEC0206D7 for ; Wed, 9 Sep 2015 06:57:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F40DA206B0 for ; Wed, 9 Sep 2015 06:57:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752791AbbIIG5T (ORCPT ); Wed, 9 Sep 2015 02:57:19 -0400 Received: from mail-bn1bbn0105.outbound.protection.outlook.com ([157.56.111.105]:4018 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751783AbbIIG5R (ORCPT ); Wed, 9 Sep 2015 02:57:17 -0400 Received: from BY2PR03MB572.namprd03.prod.outlook.com (10.141.143.152) by BY2PR03MB144.namprd03.prod.outlook.com (10.242.35.150) with Microsoft SMTP Server (TLS) id 15.1.262.15; Wed, 9 Sep 2015 06:57:13 +0000 Received: from BY2PR03CA007.namprd03.prod.outlook.com (10.255.93.24) by BY2PR03MB572.namprd03.prod.outlook.com (10.141.143.152) with Microsoft SMTP Server (TLS) id 15.1.262.15; Wed, 9 Sep 2015 06:57:11 +0000 Received: from BL2FFO11FD023.protection.gbl (10.255.93.4) by BY2PR03CA007.outlook.office365.com (10.255.93.24) with Microsoft SMTP Server (TLS) id 15.1.262.15 via Frontend Transport; Wed, 9 Sep 2015 06:57:11 +0000 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; freescale.mail.onmicrosoft.com; dmarc=none action=none header.from=freescale.com; Received-SPF: Fail (protection.outlook.com: domain of freescale.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Received: from az84smr01.freescale.net (192.88.158.2) by BL2FFO11FD023.mail.protection.outlook.com (10.173.161.102) with Microsoft SMTP Server (TLS) id 15.1.262.18 via Frontend Transport; Wed, 9 Sep 2015 06:57:10 +0000 Received: from [az84smr01.freescale.net (B06830-11.ea.freescale.net [10.171.90.79]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id t896v6xF009907; Tue, 8 Sep 2015 23:57:08 -0700 From: Alex Porosanu To: CC: , , Subject: [PATCH] crypto/caam: add backlogging support Date: Wed, 9 Sep 2015 09:57:04 +0300 Message-ID: <1441781824-57338-1-git-send-email-alexandru.porosanu@freescale.com> X-Mailer: git-send-email 1.9.1 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD023; 1:QcwmmTGU6BSk7Lc2CMlj7vRQSiZfhyFlVfazgKWj64PMAoV9S6ME0j4Qmix344b4MynTnmnTmaFEjwx2vSLneyMfk0qYt14RgT2wBhOY/dJVx9RWxw8ySQt3ctp42Gf8fjQxezvbDDvPqcVkhFDCFLctuKa8UjFGbdXDk7Wv1SWu494IVjDUDc3jXGaHRxcQdvjCkABP+UYzUZjxbFIfz/YEavakRDwUdDNhKoSW9SgOTxseLuJ7l5mzqMPPW3lZeic1jC1aK+4rLIQfPZEa1mJRsG4wnNXMJ/hMT0lg0fKtyZ0/8NEApqyK+be2OfWvTSyX0x8n0kSl6iJL/QidfV9vWhfgsNBFrM75KQRbTrO1JLdWkaiUJlxdug1OBeQK4aSAj2PjvlxK8xjoKUbrlQ== X-Forefront-Antispam-Report: CIP:192.88.158.2; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(50986999)(229853001)(48376002)(77096005)(2351001)(69596002)(62966003)(77156002)(33646002)(92566002)(64706001)(5003940100001)(5001830100001)(5001860100001)(50466002)(110136002)(68736005)(47776003)(11100500001)(104016003)(575784001)(106466001)(105606002)(86362001)(107886002)(50226001)(87936001)(85426001)(5001960100002)(19580395003)(36756003)(5007970100001)(81156007)(189998001)(46102003)(19580405001)(6806004)(4001540100001)(97736004)(4001430100001); DIR:OUT; SFP:1102; SCL:1; SRVR:BY2PR03MB572; H:az84smr01.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; MIME-Version: 1.0 X-Microsoft-Exchange-Diagnostics: 1; BY2PR03MB572; 2:jNfCYYYLajyPn8JsVC1gfptZQ9CBX30pS//Uiz64N1OvY4TRL5V0w53TocRT9SxP6+73CDAylAfQi3WbHBuZkKRpfBkFcOFgIiyCYhlYhebyt3tkGwCdoCkojUMI9xNO5boHXy3g8DlH9kVpmJGPKakzdHUgq05Keb7tTM2rzS0=; 3:MGS26sBGmodyzXllGzPynYthLouMTj/UKlDndSMXDS8e5wVtIabngiwcNjYjchjoKZciAt/5s4zJGoyiK9oF8pAUQTDCWB8FC3VWBpvxp0HcREJ9IcU6gAJWzDVUi7303pWJggofcGdP9S3QeRyEdCe16RH+ry71BQ80+2r2l29XJdA7QaXtreX7evsspwK2MIVNeHZjtWrBjTgibxTN5OSwb8I5DjiJngSMyHFwmPE=; 25:LlCs0Tk27Mm9WiXXXU3OCIxFmPz5VXcLHeS69el2V7ddtHA3+iNKPWLFCw/xYN1DiIippx09WVIJXHef74/9X40PJI45lkJMYFRDCCkLMLdUbup+PgHoTskowT/zvL8P60BDZBN68Qcv+3ORLZaP9q+bTiz6+hPlphu2GxI4wPP8564SLW8TMf03uD3THcp/k2HMYUj1YXbQqcR1WOc3qvQzjtzh+MK6etjyo0oZlrVh7K/nuiXYruBc1olHCljYiFQrlkPSJR+Mw0VBCSLxVQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BY2PR03MB572; X-Microsoft-Exchange-Diagnostics: 1; BY2PR03MB572; 20:ejskHh+2326PmhMiumsnfGWoG/nuJA403si1qP4tL2+oA+uw65aIjyTYql7gmX4pK5OfV7Wo4Uf4HMvvYaCLc4SU50yp9yBiYHoLOTi8hnv2lSXCO/aEVGTuS+n+cR3NOraNInBur0ukx5fCwlW/KASvVyoB9D/pK79S++u+6mUCdTteKPaDMxX3hg46V4T7lOzH23BV+Gv+ldrsuZeP3WglE3pXNvRvr1BKr/FLc6slU9f4ljHtfoTkB6XvWmhQdy8X6XEfx8UpT5ht/gyt66DqcH5hOi2SYlrkWODrJ4Alu9fIrkxepvydnFPamjTs3tVLKBB7eUyLn0hvCLU6Mtglj1wEnjQmj3HaYZ104pI=; 4:l9UqJonGwpe7wU51ssKEeHyne3z0WTZ6+NCLQkvr/EVy1rR1zmWPBCrFaKCsvn3JRWtK12X4BiIlY+KXWNSBPePoyXKdFmsUqKbTOlZXM4QPkZT4tv7rbuXHonSWCa33UU32aAlWG08lF5a6g2fIV+oKWerdi2gY1P2y/2VXCtW2az9a0Pe0WWQmyt7UOC9xfEbbD06T+RFJXnUGFXT+efCrs6QtFKekPDkthQuDgimIXgQt2oFts4a+msRf6mvC35QMf6ZM0T9M86v4I4/9oyyjt2E2rSOq4M5woGbFld6eelou8zELbp5PESR6BUet X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5005006)(8121501046)(3002001); SRVR:BY2PR03MB572; BCL:0; PCL:0; RULEID:; SRVR:BY2PR03MB572; X-Forefront-PRVS: 0694C54398 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR03MB572; 23:TgbtIJ4a2VIbQHPlJW7S/03YirCa2EnC8uTszMyKU3?= =?us-ascii?Q?FPu2pKkyy4h5M5PIOOsU8LuJgN0QFUtS3TQBRK+uqinSpJSOeVUw85bFdTSC?= =?us-ascii?Q?XF6UB5Mq0yA8OdtaLgjJlucFfO4eKBZqgNPJ1eOcYU6hBpW9bXx5nka3IfGJ?= =?us-ascii?Q?BM8KFMF1YoQ95SD+PIyrs9z6cMbTM4HydXWm3lL8BcMZlF5tJa1FO+7QGwjL?= =?us-ascii?Q?z67Pna445C3k0hs4z8/Vo/iMPD59Hoxg4tXn4kE3fsRgAhWAHGD73/lwLr64?= =?us-ascii?Q?45niRQO1ZDy3inOlq6f+ymTo2WrW8yusMMCBc+P+Kv7E6Lve4031kVJ/oVwS?= =?us-ascii?Q?WLiTRyo7bh03dK/KgzYkbtJf10jLPRkWoY+gLJGcBNP9NCE8J6pXWjD/h4pM?= =?us-ascii?Q?McoQyH+h2h43rLevIdWWcTMfyheYOW71JhIJFE8X6efKgttKKczN1CewQb9C?= =?us-ascii?Q?hse88YMbiKldWBQ3g/lyYFtFgDDTJcxqT8BCHmTEbadm1biYzPC7ZGJmiI7q?= =?us-ascii?Q?jnZhl/OH4j3sMc/a6yHJyZqBINGbY/oCM+2qSj4XXk076twemwKfSb6OFqEd?= =?us-ascii?Q?xGSbO95EjsUqJns3rWp5z5Pz9t7OEXaqzXqDS9Ss0uxPNylVoVs3pd9C45zb?= =?us-ascii?Q?Q7o1yIyASq/MdiMjPu/SkgyR7myehqt0ckDYG/F8lV2zmmDif2Kx2YSzy6WC?= =?us-ascii?Q?UXi+KUyiS8q1af2rAIXBjDTZQczJIinLYD4fm5arvPaaO6gfXVeS3NvWpg0X?= =?us-ascii?Q?pOErkTYyjFlZN/Gk5E2mH0YXUnKvYnxdJUczjANBDM2eklkPVvwyY0xlMUrn?= =?us-ascii?Q?HVvWaFIhp/mewmi6tKf0yYc1rNpIZX6OT+jUafbnUumwSADXvLdC3BFs3ft6?= =?us-ascii?Q?N3m30K5WAbfTwiRA7T72vlvyuqZwC9Y0l+V5gTRFV9EIgra6mYjAxdgg+q8y?= =?us-ascii?Q?e64spWJ3/FoHiwMHtKuQR1uJkFrTBa4Y9fZ/cP7meQxubVl+NJ9dQfnlWVlU?= =?us-ascii?Q?evkpe7JKkiN3Lz7vVnBwyGPVrXo/x0sH5FgA3RDYctJZaicHv/GOLJmM1T0g?= =?us-ascii?Q?Ty0prPjzBUQDsNOdtWrKc5fsjVKFU6360qMfyGu9EYJ3NG3BFP98KSB5rC3h?= =?us-ascii?Q?tRXGI95qimCFnFZGn9CihNe3QormNMvpYGRWQ9Rxja/z2j5AkKAkImEhnV8I?= =?us-ascii?Q?PMfEtyVRN8sphdip4iRLPJLbhdqWs7TUKt7N+9bF7prkzry4znSG7mlB1zwD?= =?us-ascii?Q?dC/98jayOF3nId5Y2sQ/30dcOb/xiaNpD5I+ip?= X-Microsoft-Exchange-Diagnostics: 1; BY2PR03MB572; 5:M6RuMH3pB2+k42HMB9nnRr+RIxSdfV71Aey/hA/gOZpkYJwp9aV0Y5U3Q468oDb/ZKypdgrUg45LXMbsavLgHs8YLEeEj1RgkLvlQGFPJRy0IK+AYTIVemyBxtgXgsDP2zMHC6OZhXLy0fwpg815rg==; 24:eLznMY3tADViFhwa1XBK8hOvrQFaV9hUHs5WBTG4q96jPfIm19XZFD+GIZVgDCx/pIiBOyc1f/tKV1BsTxqDy8in4jXlt1jYB/qAmTaDp0U=; 20:nb6nh5FBuiD09u2szKAVCfeIou1mJDRras2kHL/yynO0AThoKC2q0JAl3ggqD77Jf5JKd3fKY20hHlCMoyxZNA== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Sep 2015 06:57:10.8661 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR03MB572 X-Microsoft-Exchange-Diagnostics: 1; BY2PR03MB144; 2:tRrwr2oTKXUy/cr7DWUTaaYPQ2rPA0Jc+hqUlUJITZoGYlsrfpgJOIbzC+hIRydCc/2Neqz0QkDLUCuPSKFtVHviR436ppJurCqIHQmCuTwST5Gb2p52BMvcGmrtZraNl1+gc5s60OXZezGFNVaXsAXG36nur5y9pzM5Rz4KZ+I=; 23:Yjwbngv35UEPUiwrjBOxBLE+ijFevXcz0VtKQecX2602Ie0UDJQLWfQT41VvCzkbA4+RRFpY+INty+iVU+Ndk11LUV3mLExrpKywbH4Bbz6UbC3V5wE6GntWG4IJS5ZfuwSuWHKdC8Tiviovyd50bUY6nqp1wN+uYuZ027h8S4unfoPdwzikgIBqjsZubvYn X-OriginatorOrg: freescale.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.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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 caam_jr_enqueue() function returns -EBUSY once there are no more slots available in the JR, but it doesn't actually save the current request. This breaks the functionality of users that expect that even if there is no more space for the request, it is at least queued for later execution. In other words, all crypto transformations that request backlogging (i.e. have CRYPTO_TFM_REQ_MAY_BACKLOG set), will hang. Such an example is dm-crypt. The current patch solves this issue by setting a threshold after which caam_jr_enqueue() returns -EBUSY, but since the HW job ring isn't actually full, the job is enqueued. Caveat: if the users of the driver don't obey the API contract which states that once -EBUSY is received, no more requests are to be sent, eventually the driver will reject the enqueues. Signed-off-by: Alex Porosanu --- drivers/crypto/caam/caamalg.c | 233 ++++++++++++++++++++++++++++++++++-------- drivers/crypto/caam/intern.h | 7 ++ drivers/crypto/caam/jr.c | 190 +++++++++++++++++++++++++++------- drivers/crypto/caam/jr.h | 5 + 4 files changed, 352 insertions(+), 83 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index ba79d63..c281483 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -1815,9 +1815,14 @@ static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = container_of(desc, struct aead_edesc, hw_desc[0]); - if (err) + if (err && (err != -EINPROGRESS)) caam_jr_strstatus(jrdev, err); + if (err == -EINPROGRESS) { + aead_request_complete(req, err); + return; + } + aead_unmap(jrdev, edesc, req); kfree(edesc); @@ -1837,9 +1842,14 @@ static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = container_of(desc, struct aead_edesc, hw_desc[0]); - if (err) + if (err && (err != -EINPROGRESS)) caam_jr_strstatus(jrdev, err); + if (err == -EINPROGRESS) { + aead_request_complete(req, err); + return; + } + aead_unmap(jrdev, edesc, req); /* @@ -1864,13 +1874,17 @@ static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err, dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); #endif - edesc = (struct ablkcipher_edesc *)((char *)desc - offsetof(struct ablkcipher_edesc, hw_desc)); - if (err) + if (err && (err != -EINPROGRESS)) caam_jr_strstatus(jrdev, err); + if (err == -EINPROGRESS) { + ablkcipher_request_complete(req, err); + return; + } + #ifdef DEBUG print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, req->info, @@ -1900,9 +1914,14 @@ static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ablkcipher_edesc *)((char *)desc - offsetof(struct ablkcipher_edesc, hw_desc)); - if (err) + if (err && (err != -EINPROGRESS)) caam_jr_strstatus(jrdev, err); + if (err == -EINPROGRESS) { + ablkcipher_request_complete(req, err); + return; + } + #ifdef DEBUG print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, req->info, @@ -2294,12 +2313,30 @@ static int gcm_encrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_encrypt_done, + req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + aead_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - aead_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + if (!ret) { + ret = -EINPROGRESS; + } else { + aead_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; @@ -2338,12 +2375,30 @@ static int aead_encrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_encrypt_done, + req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + aead_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - aead_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + if (!ret) { + ret = -EINPROGRESS; + } else { + aead_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; @@ -2373,12 +2428,30 @@ static int gcm_decrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_decrypt_done, + req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + aead_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - aead_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + if (!ret) { + ret = -EINPROGRESS; + } else { + aead_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; @@ -2423,12 +2496,30 @@ static int aead_decrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_decrypt_done, + req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + aead_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - aead_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + if (!ret) { + ret = -EINPROGRESS; + } else { + aead_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; @@ -2575,13 +2666,31 @@ static int ablkcipher_encrypt(struct ablkcipher_request *req) desc_bytes(edesc->hw_desc), 1); #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req); - - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_encrypt_done, req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - ablkcipher_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, + req); + if (!ret) { + ret = -EINPROGRESS; + } else { + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; @@ -2612,15 +2721,32 @@ static int ablkcipher_decrypt(struct ablkcipher_request *req) DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, desc_bytes(edesc->hw_desc), 1); #endif - - ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req); - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_decrypt_done, req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - ablkcipher_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_decrypt_done, req); + if (!ret) { + ret = -EINPROGRESS; + } else { + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + } } - return ret; } @@ -2757,13 +2883,32 @@ static int ablkcipher_givencrypt(struct skcipher_givcrypt_request *creq) desc_bytes(edesc->hw_desc), 1); #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req); - - if (!ret) { - ret = -EINPROGRESS; + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_encrypt_done, req); + switch (ret) { + case 0: + ret = -EINPROGRESS; + break; + + case -EBUSY: + break; + + default: + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + break; + } } else { - ablkcipher_unmap(jrdev, edesc, req); - kfree(edesc); + ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, + req); + + if (!ret) { + ret = -EINPROGRESS; + } else { + ablkcipher_unmap(jrdev, edesc, req); + kfree(edesc); + } } return ret; diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index e2bcacc..13e63ef 100644 --- a/drivers/crypto/caam/intern.h +++ b/drivers/crypto/caam/intern.h @@ -11,6 +11,12 @@ /* Currently comes from Kconfig param as a ^2 (driver-required) */ #define JOBR_DEPTH (1 << CONFIG_CRYPTO_DEV_FSL_CAAM_RINGSIZE) +/* + * If the user tries to enqueue a job and the number of slots available + * is less than this value, then the job will be backlogged (if the user + * allows for it) or it will be dropped. + */ +#define JOBR_THRESH 16 /* Kconfig params for interrupt coalescing if selected (else zero) */ #ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_INTC @@ -33,6 +39,7 @@ struct caam_jrentry_info { u32 *desc_addr_virt; /* Stored virt addr for postprocessing */ dma_addr_t desc_addr_dma; /* Stored bus addr for done matching */ u32 desc_size; /* Stored size for postprocessing, header derived */ + bool is_backlogged; /* True if the request has been backlogged */ }; /* Private sub-storage for a single JobR */ diff --git a/drivers/crypto/caam/jr.c b/drivers/crypto/caam/jr.c index f7e0d8d..916288d 100644 --- a/drivers/crypto/caam/jr.c +++ b/drivers/crypto/caam/jr.c @@ -168,6 +168,7 @@ static void caam_jr_dequeue(unsigned long devarg) void (*usercall)(struct device *dev, u32 *desc, u32 status, void *arg); u32 *userdesc, userstatus; void *userarg; + bool is_backlogged; while (rd_reg32(&jrp->rregs->outring_used)) { @@ -201,6 +202,7 @@ static void caam_jr_dequeue(unsigned long devarg) userarg = jrp->entinfo[sw_idx].cbkarg; userdesc = jrp->entinfo[sw_idx].desc_addr_virt; userstatus = jrp->outring[hw_idx].jrstatus; + is_backlogged = jrp->entinfo[sw_idx].is_backlogged; /* * Make sure all information from the job has been obtained @@ -231,6 +233,20 @@ static void caam_jr_dequeue(unsigned long devarg) spin_unlock(&jrp->outlock); + if (is_backlogged) + /* + * For backlogged requests, the user callback needs to + * be called twice: once when starting to process it + * (with a status of -EINPROGRESS and once when it's + * done. Since SEC cheats by enqueuing the request in + * its HW ring but returning -EBUSY, the time when the + * request's processing has started is not known. + * Thus notify here the user. The second call is on the + * normal path (i.e. the one that is called even for + * non-backlogged requests. + */ + usercall(dev, userdesc, -EINPROGRESS, userarg); + /* Finally, execute user's callback */ usercall(dev, userdesc, userstatus, userarg); } @@ -292,6 +308,84 @@ void caam_jr_free(struct device *rdev) } EXPORT_SYMBOL(caam_jr_free); +static inline int __caam_jr_enqueue(struct caam_drv_private_jr *jrp, u32 *desc, + int desc_size, dma_addr_t desc_dma, + void (*cbk)(struct device *dev, u32 *desc, + u32 status, void *areq), + void *areq, + bool can_be_backlogged) +{ + int head, tail; + struct caam_jrentry_info *head_entry; + int ret = 0, hw_slots, sw_slots; + + spin_lock_bh(&jrp->inplock); + + head = jrp->head; + tail = ACCESS_ONCE(jrp->tail); + + head_entry = &jrp->entinfo[head]; + + /* Reset backlogging status here */ + head_entry->is_backlogged = false; + + hw_slots = rd_reg32(&jrp->rregs->inpring_avail); + sw_slots = CIRC_SPACE(head, tail, JOBR_DEPTH); + + if (hw_slots <= JOBR_THRESH || sw_slots <= JOBR_THRESH) { + /* + * The state below can be reached in three cases: + * 1) A badly behaved backlogging user doesn't back off when + * told so by the -EBUSY return code + * 2) More than JOBR_THRESH backlogging users requests + * 3) Due to the high system load, the entries reserved for the + * backlogging users are being filled (slowly) in between + * the successive calls to the user callback (the first one + * with -EINPROGRESS and the 2nd one with the real result. + * The code below is a last-resort measure which will DROP + * any request if there is physically no more space. This will + * lead to data-loss for disk-related users. + */ + if (!hw_slots || sw_slots <= 0) { + spin_unlock_bh(&jrp->inplock); + return -EIO; + } + + if (can_be_backlogged) { + head_entry->is_backlogged = true; + ret = -EBUSY; + } else { + spin_unlock_bh(&jrp->inplock); + return -EBUSY; + } + } + + head_entry->desc_addr_virt = desc; + head_entry->desc_size = desc_size; + head_entry->callbk = (void *)cbk; + head_entry->cbkarg = areq; + head_entry->desc_addr_dma = desc_dma; + + jrp->inpring[jrp->inp_ring_write_index] = desc_dma; + + /* + * Guarantee that the descriptor's DMA address has been written to + * the next slot in the ring before the write index is updated, since + * other cores may update this index independently. + */ + smp_wmb(); + + jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) & + (JOBR_DEPTH - 1); + jrp->head = (head + 1) & (JOBR_DEPTH - 1); + + wr_reg32(&jrp->rregs->inpring_jobadd, 1); + + spin_unlock_bh(&jrp->inplock); + + return ret; +} + /** * caam_jr_enqueue() - Enqueue a job descriptor head. Returns 0 if OK, * -EBUSY if the queue is full, -EIO if it cannot map the caller's @@ -326,8 +420,7 @@ int caam_jr_enqueue(struct device *dev, u32 *desc, void *areq) { struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); - struct caam_jrentry_info *head_entry; - int head, tail, desc_size; + int desc_size, ret; dma_addr_t desc_dma; desc_size = (*desc & HDR_JD_LENGTH_MASK) * sizeof(u32); @@ -337,51 +430,70 @@ int caam_jr_enqueue(struct device *dev, u32 *desc, return -EIO; } - spin_lock_bh(&jrp->inplock); - - head = jrp->head; - tail = ACCESS_ONCE(jrp->tail); - - if (!rd_reg32(&jrp->rregs->inpring_avail) || - CIRC_SPACE(head, tail, JOBR_DEPTH) <= 0) { - spin_unlock_bh(&jrp->inplock); + ret = __caam_jr_enqueue(jrp, desc, desc_size, desc_dma, cbk, areq, + false); + if (unlikely(ret)) dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE); - return -EBUSY; - } - head_entry = &jrp->entinfo[head]; - head_entry->desc_addr_virt = desc; - head_entry->desc_size = desc_size; - head_entry->callbk = (void *)cbk; - head_entry->cbkarg = areq; - head_entry->desc_addr_dma = desc_dma; - - jrp->inpring[jrp->inp_ring_write_index] = desc_dma; - - /* - * Guarantee that the descriptor's DMA address has been written to - * the next slot in the ring before the write index is updated, since - * other cores may update this index independently. - */ - smp_wmb(); + return ret; +} +EXPORT_SYMBOL(caam_jr_enqueue); - jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) & - (JOBR_DEPTH - 1); - jrp->head = (head + 1) & (JOBR_DEPTH - 1); +/** + * caam_jr_enqueue_bklog() - Enqueue a job descriptor head, returns 0 if OK, or + * -EINPROGRESS if the number of available entries in the Job Ring is less + * than the threshold configured through CONFIG_CRYPTO_DEV_FSL_CAAM_BKLOG_SIZE, + * and -EIO if it cannot map the caller's descriptor or if the threshold has + * been exceeded. + * @dev: device of the job ring to be used. This device should have + * been assigned prior by caam_jr_register(). + * @desc: points to a job descriptor that execute our request. All + * descriptors (and all referenced data) must be in a DMAable + * region, and all data references must be physical addresses + * accessible to CAAM (i.e. within a PAMU window granted + * to it). + * @cbk: pointer to a callback function to be invoked upon completion + * of this request. This has the form: + * callback(struct device *dev, u32 *desc, u32 stat, void *arg) + * where: + * @dev: contains the job ring device that processed this + * response. + * @desc: descriptor that initiated the request, same as + * "desc" being argued to caam_jr_enqueue(). + * @status: untranslated status received from CAAM. See the + * reference manual for a detailed description of + * error meaning, or see the JRSTA definitions in the + * register header file + * @areq: optional pointer to an argument passed with the + * original request + * @areq: optional pointer to a user argument for use at callback + * time. + **/ +int caam_jr_enqueue_bklog(struct device *dev, u32 *desc, + void (*cbk)(struct device *dev, u32 *desc, + u32 status, void *areq), + void *areq) +{ + struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); + int desc_size, ret; + dma_addr_t desc_dma; - /* - * Ensure that all job information has been written before - * notifying CAAM that a new job was added to the input ring. - */ - wmb(); + desc_size = (*desc & HDR_JD_LENGTH_MASK) * sizeof(u32); + desc_dma = dma_map_single(dev, desc, desc_size, DMA_TO_DEVICE); + if (dma_mapping_error(dev, desc_dma)) { + dev_err(dev, "caam_jr_enqueue(): can't map jobdesc\n"); + return -EIO; + } - wr_reg32(&jrp->rregs->inpring_jobadd, 1); + ret = __caam_jr_enqueue(jrp, desc, desc_size, desc_dma, cbk, areq, + true); + if (unlikely(ret && (ret != -EBUSY))) + dma_unmap_single(dev, desc_dma, desc_size, DMA_TO_DEVICE); - spin_unlock_bh(&jrp->inplock); + return ret; - return 0; } -EXPORT_SYMBOL(caam_jr_enqueue); +EXPORT_SYMBOL(caam_jr_enqueue_bklog); /* * Init JobR independent of platform property detection diff --git a/drivers/crypto/caam/jr.h b/drivers/crypto/caam/jr.h index 97113a6..21558df 100644 --- a/drivers/crypto/caam/jr.h +++ b/drivers/crypto/caam/jr.h @@ -15,4 +15,9 @@ int caam_jr_enqueue(struct device *dev, u32 *desc, void *areq), void *areq); +int caam_jr_enqueue_bklog(struct device *dev, u32 *desc, + void (*cbk)(struct device *dev, u32 *desc, u32 status, + void *areq), + void *areq); + #endif /* JR_H */