From patchwork Fri May 6 13:18:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Vasile X-Patchwork-Id: 9033121 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 685689F39D for ; Fri, 6 May 2016 13:19:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 70B3B2038F for ; Fri, 6 May 2016 13:19:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 483EF20373 for ; Fri, 6 May 2016 13:19:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757870AbcEFNTE (ORCPT ); Fri, 6 May 2016 09:19:04 -0400 Received: from mail-bn1on0088.outbound.protection.outlook.com ([157.56.110.88]:55552 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757596AbcEFNTD (ORCPT ); Fri, 6 May 2016 09:19:03 -0400 Received: from BY2PR03CA076.namprd03.prod.outlook.com (10.141.249.49) by CY1PR0301MB1228.namprd03.prod.outlook.com (10.161.212.150) with Microsoft SMTP Server (TLS) id 15.1.485.9; Fri, 6 May 2016 13:19:00 +0000 Received: from BL2FFO11FD030.protection.gbl (2a01:111:f400:7c09::198) by BY2PR03CA076.outlook.office365.com (2a01:111:e400:2c5d::49) with Microsoft SMTP Server (TLS) id 15.1.485.9 via Frontend Transport; Fri, 6 May 2016 13:18:59 +0000 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=none action=none header.from=nxp.com; Received-SPF: Fail (protection.outlook.com: domain of nxp.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 BL2FFO11FD030.mail.protection.outlook.com (10.173.161.40) with Microsoft SMTP Server (TLS) id 15.1.485.4 via Frontend Transport; Fri, 6 May 2016 13:18:59 +0000 Received: from enigma.ea.freescale.net (enigma.ea.freescale.net [10.171.77.120]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id u46DIujm029807; Fri, 6 May 2016 06:18:56 -0700 From: Catalin Vasile To: CC: , , , , Catalin Vasile Subject: [PATCH] crypto: caam: add backlogging support Date: Fri, 6 May 2016 16:18:52 +0300 Message-ID: <1462540733-2170-1-git-send-email-cata.vasile@nxp.com> X-Mailer: git-send-email 1.8.3.1 X-EOPAttributedMessage: 0 X-Matching-Connectors: 131070143395365589; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.158.2; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1109001)(1110001)(339900001)(189002)(199003)(106466001)(110136002)(11100500001)(85426001)(229853001)(87936001)(19580395003)(105606002)(5008740100001)(92566002)(50226002)(189998001)(6806005)(47776003)(36756003)(104016004)(2906002)(48376002)(1220700001)(19580405001)(81166005)(4326007)(50986999)(86362001)(8936002)(33646002)(50466002)(77096005)(5003940100001)(586003)(2351001)(7059030)(4720700001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR0301MB1228; H:az84smr01.freescale.net; FPR:; SPF:Fail; MLV:sfv; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD030; 1:1KTWHaeIY1rfPM1dw7xgNvz7OfbhYmFD18KVuuWNpw/qfguTvXI/2MAqofkMMcHF8JtrUvyqkEj4Fe1BhNgcKuMusTno3rlip/Vfr0sh+EQl7U1Z8UeXcAWnfsWr+YWye/eB0uO/L7o5R/B5iXok4WJFYfmTer/8fn5htq9Us25Qv5Qxh7yPGpb5/Pb6zw+KJ5yPeAKFyu0kpwlUO/QBTuGYzwATznzReGhZ41g9hy+kEkVbB/Zyjih9FswqYQbmpv1za2jnRiRyT475AF/mhkT7thxyrjxrJuX7O2rhGnv9rgN8m8ZDlePl7TxUgmi9VFzsf2cntLndXsI5UvBGG2i6HiVmOhrQ3VA1CQv85dgp85rfOuBpujo7wwdKp7CvlbneZKLgH46Io2mp7/MCKZQXNPEIBhP7a8k9wbDgZX//7p1QtNJycE2ZtUwrRz+o5v/2dr5UvlA4DX4qku2MJVICebNig4J1r5QhF6jAbq8Rycd6g7KcawYWAeoI7NwRiN66xxpsdm9zAs1cBHk8dO5ZoUjebKPI0vpAim31jkJws5yGRxM8AelpoTb3OM0d17lmrTcxQeCl/wFyHvjbEnL2uC1AcdJjdE/rcOqJnc10hKImlOSu3foquFAMCRrM4Ei+/sZoak2VAPz9WF0ttggTxJ+WZDNgjMiT840uSjs9q78TgN3BUZ+eFJ3TdLM6yywk8v+xcDQxJyd9H5F6Cg== MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: f9930d43-bace-4d92-82d5-08d375b0fc34 X-Microsoft-Exchange-Diagnostics: 1; CY1PR0301MB1228; 2:kh6jiCleBkAPIS3dkisAXgOey4iQxXEnpmmAwqzi8VpJPX9aCXYtrvJ5zfMXYnaz6+VbQsOSPVSzEeidCTTpEfwIrVXD3vD1erKvBvpsk8l7BIfoinwrr7cOZaj8d5UxuyrG+AsJlq2Nwcg9+Lh52svp/7IkxdaP4j9d02pwHsCDxAD0reBFJgwyc8nSco4m; 3:aqzKiXnksPCefT8H31fUplJx0piQb5X27hHf/mporG/NMGOmx/jZHhNHK2tU+L09d6vr5B8QG6BYPJ9rRlMBgnInPBs8uL2mq7Xxfon3x/bhwoDrQrbVkCNRrjmvmedMcixh2gLNnF6CBE8ypMdtdDutEh4pOFU1MImB+vnbCeG+g7DsUvKzrxBEmLCzPBv1Qmt5OfYD5PhZTO6oNWsorBaThogf2dSD6zQekW3WJLM= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1PR0301MB1228; X-Microsoft-Exchange-Diagnostics: 1; CY1PR0301MB1228; 25:40q8dT0OVuoNcTgCB1Z0WgmcruNxVWNfB2QsRWzWQhxjaLj4z0CqxnQwxs1mSsnbDM1esd4E/wYnqHg4JveF8dCbEYd+EHXOy4rucQd64Hndkda6jZedwtc5kxQ4boTN9PagLroILwsCiO5U7JSpKltqcUXCKAvBAkZIUWXmQGpEydI6z5eRBhW+weSWGTKJia+bnpHAK5JZ6oDSDIcShIBQaOVOQNU9ffPn+5HwA+ba/+VHbUd62woK/ZYfF8k0v/bViDRUmjV9JYjFYdug/t7RFUB7UWwsW26624OTOOB13tzBKj3hbWyM5nvzaOOcXg5x0oORIsOp1wIWSbWxfcxxU1utRkPFP5syJiM3uzX/8Pq0fW0vt/bUVu3fTCIgN3EKM4Sf7aA//7sLojMnkjQtqce5r1vAwemoFEnY2vHJmKkdw5/Dq/zwd40pbsm5SVq9VphDZ0vuCnL1P3IaaHnV/jhOyhlBWbZrwacpufMN+IJ9biD2yr/5PwB9Cmdp5UpxbV5FwsFKJiasvR6ZoNeGimGlOY9pcNHkXHu0ZV3/Jsqa+JB+TtJJwLjD64le84wgu8PpX+YDdjv3Z/+/ZioQd7I2Kaq4eCZzaFbsPJIJQ8UBuDJGtEjZmGveyvDSN3biExBQSVkBj0UAOWCz1rCtgAuUCCEzQpcWOL2wT/el7O4Sue73RPYECu0NLXCnPODIBRtdQr6TDN3gLAytJKsXYEtBQ56F9XMjlxAxv+pIwlLJRAmKUKLADm5AFChfayy22wM5oB2nf2y7HGVDdg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(8121501046)(5005006)(13017025)(13024025)(13015025)(13018025)(10201501046)(3002001)(6055026); SRVR:CY1PR0301MB1228; BCL:0; PCL:0; RULEID:(400006); SRVR:CY1PR0301MB1228; X-Microsoft-Exchange-Diagnostics: 1; CY1PR0301MB1228; 4:yIm2/uKf58es64Il9S3enlESo/Zin8n8GE12WqlUHV8zsPQaVjNsbUDVbBkAASD/ePRuiUyXg84jYHY7UJQWvpCsrM5ts8lN9qVSwUqSm2sqXSIH4L9HJuvwY1CtBUWj9+rEIXAaqzXFHUvj3hi/jE5iyCLV8UX1tigBFQyWO47IMc2Z+bQXqY+wtGPJpgDo+HcxdBpoZKMv/ENin+KXreUJ5mFp7LnsdFwoJCvzW5C3oZJxUxEjP9lCLCGEb1VZ0KIwW+3e60ujKMPlhu97BLAZ9FPwrSOB9wMQAx7BH7Ke+aKyjO2G8w5ISaZZaaxAJs+sX6+DaNcgpVtenlk4fjsXS7wg/ltYo+fr6omPMsXeXvj5+NAa8vbaTOTg0/u1emMEAX9jnTkTagj1CdSiDoV4Ym4PWgiPHoSZ7nROIsNiYg5KLjwl2frbz7imHmUWQfb+NOPCOEW5ndDYxsOG7QYC2hytcuCeoK97Ec7M+04= X-Forefront-PRVS: 09347618C4 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY1PR0301MB1228; 23:hzKDivIUvtTqvni07bMZ6aVORNu8QWAkTM2gp2g?= =?us-ascii?Q?OaaZ0VL3bmQrDpvCtQc5jazPi9nVLzumrAuTqiLSf0rxwsa0stpqXpOCIyAg?= =?us-ascii?Q?IOZ/mLLFdyeHkcw0vE/6mk2cqayN3FkYqx5P6KN5tSbK4cGCFtZZasnmZ7E6?= =?us-ascii?Q?8Buwbx4n8kUAiaNgDmMY+2ISbtqlC5ZSSbCujBlAzvAdakQfb71gnibPOzci?= =?us-ascii?Q?K0Gg199lK98t4mdEkLNAW0w/aIy+aC3iA/ThdTv6RJrymXb1YBM1heSetoyH?= =?us-ascii?Q?M0/NW6pOaZ41jCgBgR+9Y7Y0CbDDmNKniPUyQMZUPV/6AIBy1ccLQSrl8qRS?= =?us-ascii?Q?ijU89iKV6NXSNGpbe26Naxx/VN17fmF1Skbve/EoZPgW4JYSH+Aadh4GiCL1?= =?us-ascii?Q?EnXfMMdaDCNRgy8gJM7L1HbSASO5kunq2SnByTRf+BWcvcuSEMBqmgzm2PGN?= =?us-ascii?Q?AYmQM1R7Qc7DzldzZDotjb6KS8OWnuK6CQ5K3I8St1jKGeP3vqY3jGWgtYpg?= =?us-ascii?Q?+XWLcy/6XnL4z0tJmSHfMdj1Dw9782Oun34sEvOBIE/CYlQFaODiFJlrqEfp?= =?us-ascii?Q?vlCSeZVXMJdNONzO9ZbDL5kZqmP/JZVT6vfYPOIemIfVyRzuxDfUTQ1Eolbk?= =?us-ascii?Q?b7nHvcTlHzbPgJUq1haQT+ky9CfhdowoQb+zoOqNdHLM0w1PY998MnWlGJSm?= =?us-ascii?Q?GaKH6J0kkWNrINfIxA7dAdi9h8Z6/KmAaELydmIVbkaG/ER/SEBbR8eWBIW+?= =?us-ascii?Q?H93F9SRDfWDvPRrP3kJnVb6WRYj1pRvpzR+C5KRCqWGlNRXLAIFOjUjrpcy7?= =?us-ascii?Q?d76oEsmlKdjflFVQGR7+hfwMqcfh8/SRwU8huv/14cK6+YPYoXKscD1b2DGJ?= =?us-ascii?Q?88zt9zwsuOtXkivHifcL2F4iSMhfLWyYRUSvsx/c1Yzu7FvxhHBeuCV3gQqx?= =?us-ascii?Q?Hen6aN+hka2xDj0Z3WF/I2Mh/vJ2KF4AbqD1NQEXiuK6YyRf30FD/ar+u+Sa?= =?us-ascii?Q?0H9E3sYM5/KoHzL/nABCpno6hviDwQKQXc7h4yZbBLhP6fYHK0LYXa9lFtBo?= =?us-ascii?Q?tb4sfZ5o=3D?= X-Microsoft-Exchange-Diagnostics: 1; CY1PR0301MB1228; 5:eIMIMkVRSuYh3C9BjOxjI7GGYm0ZTebA8IPLXCZJ0/Z1Heskw96zfrM6mPH7DGsG2WmBGFNmemFxdsFVb4hLGcLNy9cMPvWG3daREEtBovzRTl3sUebYXtBGFzT0ND1wsOf5UaLt0X8+HmscZZzHGT6nxApb1S0ePuGJAplV+AI=; 24:cZMMzvLG7sYO+82/O5oF/e6K0br7TjzBUx39EqqTU6eJ26GPhZBwwC+0MQxDxmHuz5JMTIqst30rWNDGMb0eUTaBz71Cchvvoq7nRM0HGwo=; 7:bWidWfVKrJMz1BOEg7QwdL0p6yXvNv/C3aRc6uxcJ1YZa9zPHLLZLZt+8GFr8xCEsGg6tQ/oPAzq1JkIWBj09JWUYb7dR4bkN4o3nqLxnKhqB3Ce+gdyNHSMBoU//gpDx984BKBYI1Ps/owSo7XZhfrn+/UwrcQX/LmST1Nh+5Ee5GiErzjzyVc33lDRyN3n SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 May 2016 13:18:59.3181 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0301MB1228 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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. Signed-off-by: Alexandru Porosanu Tested-by: Catalin Vasile --- drivers/crypto/caam/caamalg.c | 88 ++++++++++++++++-- drivers/crypto/caam/caamhash.c | 101 +++++++++++++++++++-- drivers/crypto/caam/intern.h | 7 ++ drivers/crypto/caam/jr.c | 196 +++++++++++++++++++++++++++++++++-------- drivers/crypto/caam/jr.h | 5 ++ 5 files changed, 343 insertions(+), 54 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index ea8189f..62bce17 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -1921,6 +1921,9 @@ static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = container_of(desc, struct aead_edesc, hw_desc[0]); + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -1928,6 +1931,7 @@ static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err, kfree(edesc); +out_bklogged: aead_request_complete(req, err); } @@ -1943,6 +1947,9 @@ static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = container_of(desc, struct aead_edesc, hw_desc[0]); + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -1956,6 +1963,7 @@ static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err, kfree(edesc); +out_bklogged: aead_request_complete(req, err); } @@ -1974,6 +1982,9 @@ static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ablkcipher_edesc *)((char *)desc - offsetof(struct ablkcipher_edesc, hw_desc)); + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -1989,6 +2000,7 @@ static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err, ablkcipher_unmap(jrdev, edesc, req); kfree(edesc); +out_bklogged: ablkcipher_request_complete(req, err); } @@ -2006,6 +2018,10 @@ 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 == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -2021,6 +2037,7 @@ static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err, ablkcipher_unmap(jrdev, edesc, req); kfree(edesc); +out_bklogged: ablkcipher_request_complete(req, err); } @@ -2394,7 +2411,15 @@ static int gcm_encrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_encrypt_done, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -2438,7 +2463,15 @@ static int aead_encrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_encrypt_done, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -2473,7 +2506,15 @@ static int gcm_decrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_decrypt_done, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -2523,7 +2564,15 @@ static int aead_decrypt(struct aead_request *req) #endif desc = edesc->hw_desc; - ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, aead_decrypt_done, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -2672,7 +2721,15 @@ 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 (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_encrypt_done, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, + req); + } if (!ret) { ret = -EINPROGRESS; @@ -2710,7 +2767,16 @@ static int ablkcipher_decrypt(struct ablkcipher_request *req) desc_bytes(edesc->hw_desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_decrypt_done, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, + req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -2851,7 +2917,15 @@ 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 (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ablkcipher_encrypt_done, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, + req); + } if (!ret) { ret = -EINPROGRESS; diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c index 5845d4a..910a350 100644 --- a/drivers/crypto/caam/caamhash.c +++ b/drivers/crypto/caam/caamhash.c @@ -650,6 +650,10 @@ static void ahash_done(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ahash_edesc *)((char *)desc - offsetof(struct ahash_edesc, hw_desc)); + + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -666,6 +670,7 @@ static void ahash_done(struct device *jrdev, u32 *desc, u32 err, digestsize, 1); #endif +out_bklogged: req->base.complete(&req->base, err); } @@ -685,6 +690,10 @@ static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ahash_edesc *)((char *)desc - offsetof(struct ahash_edesc, hw_desc)); + + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -701,6 +710,7 @@ static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err, digestsize, 1); #endif +out_bklogged: req->base.complete(&req->base, err); } @@ -720,6 +730,10 @@ static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ahash_edesc *)((char *)desc - offsetof(struct ahash_edesc, hw_desc)); + + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -736,6 +750,7 @@ static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err, digestsize, 1); #endif +out_bklogged: req->base.complete(&req->base, err); } @@ -755,6 +770,10 @@ static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err, edesc = (struct ahash_edesc *)((char *)desc - offsetof(struct ahash_edesc, hw_desc)); + + if (err == -EINPROGRESS) + goto out_bklogged; + if (err) caam_jr_strstatus(jrdev, err); @@ -771,6 +790,7 @@ static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err, digestsize, 1); #endif +out_bklogged: req->base.complete(&req->base, err); } @@ -876,7 +896,15 @@ static int ahash_update_ctx(struct ahash_request *req) desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done_bi, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -973,7 +1001,15 @@ static int ahash_final_ctx(struct ahash_request *req) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done_ctx_src, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -1065,7 +1101,15 @@ static int ahash_finup_ctx(struct ahash_request *req) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done_ctx_src, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -1145,7 +1189,14 @@ static int ahash_digest(struct ahash_request *req) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -1207,7 +1258,14 @@ static int ahash_final_no_ctx(struct ahash_request *req) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -1308,7 +1366,16 @@ static int ahash_update_no_ctx(struct ahash_request *req) desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ahash_done_ctx_dst, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, + req); + } + if (!ret) { ret = -EINPROGRESS; state->update = ahash_update_ctx; @@ -1411,7 +1478,15 @@ static int ahash_finup_no_ctx(struct ahash_request *req) DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, ahash_done, + req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done, req); + } + if (!ret) { ret = -EINPROGRESS; } else { @@ -1514,8 +1589,16 @@ static int ahash_update_first(struct ahash_request *req) desc_bytes(desc), 1); #endif - ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, - req); + if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) { + ret = caam_jr_enqueue_bklog(jrdev, desc, + ahash_done_ctx_dst, req); + if (ret == -EBUSY) + return ret; + } else { + ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, + req); + } + if (!ret) { ret = -EINPROGRESS; state->update = ahash_update_ctx; diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index e2bcacc..aa4e257 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 (JOBR_DEPTH / 2 - 1) /* 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 5ef4be2..49790e1 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,9 @@ 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; + /* Reset backlogging status here */ + jrp->entinfo[sw_idx].is_backlogged = false; /* * Make sure all information from the job has been obtained @@ -231,6 +235,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); } @@ -261,6 +279,15 @@ struct device *caam_jr_alloc(void) list_for_each_entry(jrpriv, &driver_data.jr_list, list_node) { tfm_cnt = atomic_read(&jrpriv->tfm_count); + + /* + * Don't allow more than JOBR_THRES jobs on this JR. If more + * are allowed, then backlogging API contract won't work (each + * "backloggable" tfm must allow for at least 1 enqueue. + */ + if (tfm_cnt == JOBR_THRESH) + continue; + if (tfm_cnt < min_tfm_cnt) { min_tfm_cnt = tfm_cnt; min_jrpriv = jrpriv; @@ -292,6 +319,80 @@ 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]; + + 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) { + ret = -EIO; + goto out_unlock; + } + + ret = -EBUSY; + if (!can_be_backlogged) + goto out_unlock; + + head_entry->is_backlogged = true; + } + + 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); + +out_unlock: + 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 +427,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 +437,71 @@ 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; + return 0; +} +EXPORT_SYMBOL(caam_jr_enqueue); - /* - * 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(); +/** + * caam_jr_enqueue_bklog() - Enqueue a job descriptor head, returns 0 if OK, or + * -EBUSY if the number of available entries in the Job Ring is less + * than the threshold configured through JOBR_THRESH, and -EIO if it cannot map + * the caller's descriptor or if there is really no more space in the hardware + * job ring. + * @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; - jrp->inp_ring_write_index = (jrp->inp_ring_write_index + 1) & - (JOBR_DEPTH - 1); - jrp->head = (head + 1) & (JOBR_DEPTH - 1); - /* - * 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 */