1515#include <linux/err.h>
1616#include <linux/delay.h>
1717#include <crypto/engine.h>
18+ #include <crypto/internal/hash.h>
1819#include "internal.h"
1920
2021#define CRYPTO_ENGINE_MAX_QLEN 10
2122
22- void crypto_finalize_request (struct crypto_engine * engine ,
23- struct ablkcipher_request * req , int err );
24-
2523/**
2624 * crypto_pump_requests - dequeue one request from engine queue to process
2725 * @engine: the hardware engine
@@ -35,10 +33,11 @@ static void crypto_pump_requests(struct crypto_engine *engine,
3533 bool in_kthread )
3634{
3735 struct crypto_async_request * async_req , * backlog ;
38- struct ablkcipher_request * req ;
36+ struct ahash_request * hreq ;
37+ struct ablkcipher_request * breq ;
3938 unsigned long flags ;
4039 bool was_busy = false;
41- int ret ;
40+ int ret , rtype ;
4241
4342 spin_lock_irqsave (& engine -> queue_lock , flags );
4443
@@ -83,9 +82,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
8382 if (!async_req )
8483 goto out ;
8584
86- req = ablkcipher_request_cast (async_req );
87-
88- engine -> cur_req = req ;
85+ engine -> cur_req = async_req ;
8986 if (backlog )
9087 backlog -> complete (backlog , - EINPROGRESS );
9188
@@ -96,6 +93,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
9693
9794 spin_unlock_irqrestore (& engine -> queue_lock , flags );
9895
96+ rtype = crypto_tfm_alg_type (engine -> cur_req -> tfm );
9997 /* Until here we get the request need to be encrypted successfully */
10098 if (!was_busy && engine -> prepare_crypt_hardware ) {
10199 ret = engine -> prepare_crypt_hardware (engine );
@@ -105,24 +103,55 @@ static void crypto_pump_requests(struct crypto_engine *engine,
105103 }
106104 }
107105
108- if (engine -> prepare_request ) {
109- ret = engine -> prepare_request (engine , engine -> cur_req );
106+ switch (rtype ) {
107+ case CRYPTO_ALG_TYPE_AHASH :
108+ hreq = ahash_request_cast (engine -> cur_req );
109+ if (engine -> prepare_hash_request ) {
110+ ret = engine -> prepare_hash_request (engine , hreq );
111+ if (ret ) {
112+ pr_err ("failed to prepare request: %d\n" , ret );
113+ goto req_err ;
114+ }
115+ engine -> cur_req_prepared = true;
116+ }
117+ ret = engine -> hash_one_request (engine , hreq );
110118 if (ret ) {
111- pr_err ("failed to prepare request: %d \n" , ret );
119+ pr_err ("failed to hash one request from queue \n" );
112120 goto req_err ;
113121 }
114- engine -> cur_req_prepared = true;
115- }
116-
117- ret = engine -> crypt_one_request (engine , engine -> cur_req );
118- if (ret ) {
119- pr_err ("failed to crypt one request from queue\n" );
120- goto req_err ;
122+ return ;
123+ case CRYPTO_ALG_TYPE_ABLKCIPHER :
124+ breq = ablkcipher_request_cast (engine -> cur_req );
125+ if (engine -> prepare_cipher_request ) {
126+ ret = engine -> prepare_cipher_request (engine , breq );
127+ if (ret ) {
128+ pr_err ("failed to prepare request: %d\n" , ret );
129+ goto req_err ;
130+ }
131+ engine -> cur_req_prepared = true;
132+ }
133+ ret = engine -> cipher_one_request (engine , breq );
134+ if (ret ) {
135+ pr_err ("failed to cipher one request from queue\n" );
136+ goto req_err ;
137+ }
138+ return ;
139+ default :
140+ pr_err ("failed to prepare request of unknown type\n" );
141+ return ;
121142 }
122- return ;
123143
124144req_err :
125- crypto_finalize_request (engine , engine -> cur_req , ret );
145+ switch (rtype ) {
146+ case CRYPTO_ALG_TYPE_AHASH :
147+ hreq = ahash_request_cast (engine -> cur_req );
148+ crypto_finalize_hash_request (engine , hreq , ret );
149+ break ;
150+ case CRYPTO_ALG_TYPE_ABLKCIPHER :
151+ breq = ablkcipher_request_cast (engine -> cur_req );
152+ crypto_finalize_cipher_request (engine , breq , ret );
153+ break ;
154+ }
126155 return ;
127156
128157out :
@@ -138,12 +167,14 @@ static void crypto_pump_work(struct kthread_work *work)
138167}
139168
140169/**
141- * crypto_transfer_request - transfer the new request into the engine queue
170+ * crypto_transfer_cipher_request - transfer the new request into the
171+ * enginequeue
142172 * @engine: the hardware engine
143173 * @req: the request need to be listed into the engine queue
144174 */
145- int crypto_transfer_request (struct crypto_engine * engine ,
146- struct ablkcipher_request * req , bool need_pump )
175+ int crypto_transfer_cipher_request (struct crypto_engine * engine ,
176+ struct ablkcipher_request * req ,
177+ bool need_pump )
147178{
148179 unsigned long flags ;
149180 int ret ;
@@ -163,46 +194,125 @@ int crypto_transfer_request(struct crypto_engine *engine,
163194 spin_unlock_irqrestore (& engine -> queue_lock , flags );
164195 return ret ;
165196}
166- EXPORT_SYMBOL_GPL (crypto_transfer_request );
197+ EXPORT_SYMBOL_GPL (crypto_transfer_cipher_request );
198+
199+ /**
200+ * crypto_transfer_cipher_request_to_engine - transfer one request to list
201+ * into the engine queue
202+ * @engine: the hardware engine
203+ * @req: the request need to be listed into the engine queue
204+ */
205+ int crypto_transfer_cipher_request_to_engine (struct crypto_engine * engine ,
206+ struct ablkcipher_request * req )
207+ {
208+ return crypto_transfer_cipher_request (engine , req , true);
209+ }
210+ EXPORT_SYMBOL_GPL (crypto_transfer_cipher_request_to_engine );
211+
212+ /**
213+ * crypto_transfer_hash_request - transfer the new request into the
214+ * enginequeue
215+ * @engine: the hardware engine
216+ * @req: the request need to be listed into the engine queue
217+ */
218+ int crypto_transfer_hash_request (struct crypto_engine * engine ,
219+ struct ahash_request * req , bool need_pump )
220+ {
221+ unsigned long flags ;
222+ int ret ;
223+
224+ spin_lock_irqsave (& engine -> queue_lock , flags );
225+
226+ if (!engine -> running ) {
227+ spin_unlock_irqrestore (& engine -> queue_lock , flags );
228+ return - ESHUTDOWN ;
229+ }
230+
231+ ret = ahash_enqueue_request (& engine -> queue , req );
232+
233+ if (!engine -> busy && need_pump )
234+ queue_kthread_work (& engine -> kworker , & engine -> pump_requests );
235+
236+ spin_unlock_irqrestore (& engine -> queue_lock , flags );
237+ return ret ;
238+ }
239+ EXPORT_SYMBOL_GPL (crypto_transfer_hash_request );
167240
168241/**
169- * crypto_transfer_request_to_engine - transfer one request to list into the
170- * engine queue
242+ * crypto_transfer_hash_request_to_engine - transfer one request to list
243+ * into the engine queue
171244 * @engine: the hardware engine
172245 * @req: the request need to be listed into the engine queue
173246 */
174- int crypto_transfer_request_to_engine (struct crypto_engine * engine ,
175- struct ablkcipher_request * req )
247+ int crypto_transfer_hash_request_to_engine (struct crypto_engine * engine ,
248+ struct ahash_request * req )
176249{
177- return crypto_transfer_request (engine , req , true);
250+ return crypto_transfer_hash_request (engine , req , true);
178251}
179- EXPORT_SYMBOL_GPL (crypto_transfer_request_to_engine );
252+ EXPORT_SYMBOL_GPL (crypto_transfer_hash_request_to_engine );
180253
181254/**
182- * crypto_finalize_request - finalize one request if the request is done
255+ * crypto_finalize_cipher_request - finalize one request if the request is done
183256 * @engine: the hardware engine
184257 * @req: the request need to be finalized
185258 * @err: error number
186259 */
187- void crypto_finalize_request (struct crypto_engine * engine ,
188- struct ablkcipher_request * req , int err )
260+ void crypto_finalize_cipher_request (struct crypto_engine * engine ,
261+ struct ablkcipher_request * req , int err )
189262{
190263 unsigned long flags ;
191264 bool finalize_cur_req = false;
192265 int ret ;
193266
194267 spin_lock_irqsave (& engine -> queue_lock , flags );
195- if (engine -> cur_req == req )
268+ if (engine -> cur_req == & req -> base )
196269 finalize_cur_req = true;
197270 spin_unlock_irqrestore (& engine -> queue_lock , flags );
198271
199272 if (finalize_cur_req ) {
200- if (engine -> cur_req_prepared && engine -> unprepare_request ) {
201- ret = engine -> unprepare_request (engine , req );
273+ if (engine -> cur_req_prepared &&
274+ engine -> unprepare_cipher_request ) {
275+ ret = engine -> unprepare_cipher_request (engine , req );
202276 if (ret )
203277 pr_err ("failed to unprepare request\n" );
204278 }
279+ spin_lock_irqsave (& engine -> queue_lock , flags );
280+ engine -> cur_req = NULL ;
281+ engine -> cur_req_prepared = false;
282+ spin_unlock_irqrestore (& engine -> queue_lock , flags );
283+ }
284+
285+ req -> base .complete (& req -> base , err );
205286
287+ queue_kthread_work (& engine -> kworker , & engine -> pump_requests );
288+ }
289+ EXPORT_SYMBOL_GPL (crypto_finalize_cipher_request );
290+
291+ /**
292+ * crypto_finalize_hash_request - finalize one request if the request is done
293+ * @engine: the hardware engine
294+ * @req: the request need to be finalized
295+ * @err: error number
296+ */
297+ void crypto_finalize_hash_request (struct crypto_engine * engine ,
298+ struct ahash_request * req , int err )
299+ {
300+ unsigned long flags ;
301+ bool finalize_cur_req = false;
302+ int ret ;
303+
304+ spin_lock_irqsave (& engine -> queue_lock , flags );
305+ if (engine -> cur_req == & req -> base )
306+ finalize_cur_req = true;
307+ spin_unlock_irqrestore (& engine -> queue_lock , flags );
308+
309+ if (finalize_cur_req ) {
310+ if (engine -> cur_req_prepared &&
311+ engine -> unprepare_hash_request ) {
312+ ret = engine -> unprepare_hash_request (engine , req );
313+ if (ret )
314+ pr_err ("failed to unprepare request\n" );
315+ }
206316 spin_lock_irqsave (& engine -> queue_lock , flags );
207317 engine -> cur_req = NULL ;
208318 engine -> cur_req_prepared = false;
@@ -213,7 +323,7 @@ void crypto_finalize_request(struct crypto_engine *engine,
213323
214324 queue_kthread_work (& engine -> kworker , & engine -> pump_requests );
215325}
216- EXPORT_SYMBOL_GPL (crypto_finalize_request );
326+ EXPORT_SYMBOL_GPL (crypto_finalize_hash_request );
217327
218328/**
219329 * crypto_engine_start - start the hardware engine
@@ -250,7 +360,7 @@ EXPORT_SYMBOL_GPL(crypto_engine_start);
250360int crypto_engine_stop (struct crypto_engine * engine )
251361{
252362 unsigned long flags ;
253- unsigned limit = 500 ;
363+ unsigned int limit = 500 ;
254364 int ret = 0 ;
255365
256366 spin_lock_irqsave (& engine -> queue_lock , flags );
0 commit comments