GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / crypto / bcm / cipher.c
1 /*
2  * Copyright 2016 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2, as
6  * published by the Free Software Foundation (the "GPL").
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License version 2 (GPLv2) for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * version 2 (GPLv2) along with this source code.
15  */
16
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 #include <linux/crypto.h>
26 #include <linux/kthread.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched.h>
29 #include <linux/of_address.h>
30 #include <linux/of_device.h>
31 #include <linux/io.h>
32 #include <linux/bitops.h>
33
34 #include <crypto/algapi.h>
35 #include <crypto/aead.h>
36 #include <crypto/internal/aead.h>
37 #include <crypto/aes.h>
38 #include <crypto/des.h>
39 #include <crypto/hmac.h>
40 #include <crypto/sha.h>
41 #include <crypto/md5.h>
42 #include <crypto/authenc.h>
43 #include <crypto/skcipher.h>
44 #include <crypto/hash.h>
45 #include <crypto/aes.h>
46 #include <crypto/sha3.h>
47
48 #include "util.h"
49 #include "cipher.h"
50 #include "spu.h"
51 #include "spum.h"
52 #include "spu2.h"
53
54 /* ================= Device Structure ================== */
55
56 struct bcm_device_private iproc_priv;
57
58 /* ==================== Parameters ===================== */
59
60 int flow_debug_logging;
61 module_param(flow_debug_logging, int, 0644);
62 MODULE_PARM_DESC(flow_debug_logging, "Enable Flow Debug Logging");
63
64 int packet_debug_logging;
65 module_param(packet_debug_logging, int, 0644);
66 MODULE_PARM_DESC(packet_debug_logging, "Enable Packet Debug Logging");
67
68 int debug_logging_sleep;
69 module_param(debug_logging_sleep, int, 0644);
70 MODULE_PARM_DESC(debug_logging_sleep, "Packet Debug Logging Sleep");
71
72 /*
73  * The value of these module parameters is used to set the priority for each
74  * algo type when this driver registers algos with the kernel crypto API.
75  * To use a priority other than the default, set the priority in the insmod or
76  * modprobe. Changing the module priority after init time has no effect.
77  *
78  * The default priorities are chosen to be lower (less preferred) than ARMv8 CE
79  * algos, but more preferred than generic software algos.
80  */
81 static int cipher_pri = 150;
82 module_param(cipher_pri, int, 0644);
83 MODULE_PARM_DESC(cipher_pri, "Priority for cipher algos");
84
85 static int hash_pri = 100;
86 module_param(hash_pri, int, 0644);
87 MODULE_PARM_DESC(hash_pri, "Priority for hash algos");
88
89 static int aead_pri = 150;
90 module_param(aead_pri, int, 0644);
91 MODULE_PARM_DESC(aead_pri, "Priority for AEAD algos");
92
93 /* A type 3 BCM header, expected to precede the SPU header for SPU-M.
94  * Bits 3 and 4 in the first byte encode the channel number (the dma ringset).
95  * 0x60 - ring 0
96  * 0x68 - ring 1
97  * 0x70 - ring 2
98  * 0x78 - ring 3
99  */
100 char BCMHEADER[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 };
101 /*
102  * Some SPU hw does not use BCM header on SPU messages. So BCM_HDR_LEN
103  * is set dynamically after reading SPU type from device tree.
104  */
105 #define BCM_HDR_LEN  iproc_priv.bcm_hdr_len
106
107 /* min and max time to sleep before retrying when mbox queue is full. usec */
108 #define MBOX_SLEEP_MIN  800
109 #define MBOX_SLEEP_MAX 1000
110
111 /**
112  * select_channel() - Select a SPU channel to handle a crypto request. Selects
113  * channel in round robin order.
114  *
115  * Return:  channel index
116  */
117 static u8 select_channel(void)
118 {
119         u8 chan_idx = atomic_inc_return(&iproc_priv.next_chan);
120
121         return chan_idx % iproc_priv.spu.num_chan;
122 }
123
124 /**
125  * spu_ablkcipher_rx_sg_create() - Build up the scatterlist of buffers used to
126  * receive a SPU response message for an ablkcipher request. Includes buffers to
127  * catch SPU message headers and the response data.
128  * @mssg:       mailbox message containing the receive sg
129  * @rctx:       crypto request context
130  * @rx_frag_num: number of scatterlist elements required to hold the
131  *              SPU response message
132  * @chunksize:  Number of bytes of response data expected
133  * @stat_pad_len: Number of bytes required to pad the STAT field to
134  *              a 4-byte boundary
135  *
136  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
137  * when the request completes, whether the request is handled successfully or
138  * there is an error.
139  *
140  * Returns:
141  *   0 if successful
142  *   < 0 if an error
143  */
144 static int
145 spu_ablkcipher_rx_sg_create(struct brcm_message *mssg,
146                             struct iproc_reqctx_s *rctx,
147                             u8 rx_frag_num,
148                             unsigned int chunksize, u32 stat_pad_len)
149 {
150         struct spu_hw *spu = &iproc_priv.spu;
151         struct scatterlist *sg; /* used to build sgs in mbox message */
152         struct iproc_ctx_s *ctx = rctx->ctx;
153         u32 datalen;            /* Number of bytes of response data expected */
154
155         mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
156                                 rctx->gfp);
157         if (!mssg->spu.dst)
158                 return -ENOMEM;
159
160         sg = mssg->spu.dst;
161         sg_init_table(sg, rx_frag_num);
162         /* Space for SPU message header */
163         sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
164
165         /* If XTS tweak in payload, add buffer to receive encrypted tweak */
166         if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
167             spu->spu_xts_tweak_in_payload())
168                 sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak,
169                            SPU_XTS_TWEAK_SIZE);
170
171         /* Copy in each dst sg entry from request, up to chunksize */
172         datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
173                                  rctx->dst_nents, chunksize);
174         if (datalen < chunksize) {
175                 pr_err("%s(): failed to copy dst sg to mbox msg. chunksize %u, datalen %u",
176                        __func__, chunksize, datalen);
177                 return -EFAULT;
178         }
179
180         if (ctx->cipher.alg == CIPHER_ALG_RC4)
181                 /* Add buffer to catch 260-byte SUPDT field for RC4 */
182                 sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak, SPU_SUPDT_LEN);
183
184         if (stat_pad_len)
185                 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
186
187         memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
188         sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
189
190         return 0;
191 }
192
193 /**
194  * spu_ablkcipher_tx_sg_create() - Build up the scatterlist of buffers used to
195  * send a SPU request message for an ablkcipher request. Includes SPU message
196  * headers and the request data.
197  * @mssg:       mailbox message containing the transmit sg
198  * @rctx:       crypto request context
199  * @tx_frag_num: number of scatterlist elements required to construct the
200  *              SPU request message
201  * @chunksize:  Number of bytes of request data
202  * @pad_len:    Number of pad bytes
203  *
204  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
205  * when the request completes, whether the request is handled successfully or
206  * there is an error.
207  *
208  * Returns:
209  *   0 if successful
210  *   < 0 if an error
211  */
212 static int
213 spu_ablkcipher_tx_sg_create(struct brcm_message *mssg,
214                             struct iproc_reqctx_s *rctx,
215                             u8 tx_frag_num, unsigned int chunksize, u32 pad_len)
216 {
217         struct spu_hw *spu = &iproc_priv.spu;
218         struct scatterlist *sg; /* used to build sgs in mbox message */
219         struct iproc_ctx_s *ctx = rctx->ctx;
220         u32 datalen;            /* Number of bytes of response data expected */
221         u32 stat_len;
222
223         mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
224                                 rctx->gfp);
225         if (unlikely(!mssg->spu.src))
226                 return -ENOMEM;
227
228         sg = mssg->spu.src;
229         sg_init_table(sg, tx_frag_num);
230
231         sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
232                    BCM_HDR_LEN + ctx->spu_req_hdr_len);
233
234         /* if XTS tweak in payload, copy from IV (where crypto API puts it) */
235         if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
236             spu->spu_xts_tweak_in_payload())
237                 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, SPU_XTS_TWEAK_SIZE);
238
239         /* Copy in each src sg entry from request, up to chunksize */
240         datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
241                                  rctx->src_nents, chunksize);
242         if (unlikely(datalen < chunksize)) {
243                 pr_err("%s(): failed to copy src sg to mbox msg",
244                        __func__);
245                 return -EFAULT;
246         }
247
248         if (pad_len)
249                 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
250
251         stat_len = spu->spu_tx_status_len();
252         if (stat_len) {
253                 memset(rctx->msg_buf.tx_stat, 0, stat_len);
254                 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
255         }
256         return 0;
257 }
258
259 static int mailbox_send_message(struct brcm_message *mssg, u32 flags,
260                                 u8 chan_idx)
261 {
262         int err;
263         int retry_cnt = 0;
264         struct device *dev = &(iproc_priv.pdev->dev);
265
266         err = mbox_send_message(iproc_priv.mbox[chan_idx], mssg);
267         if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) {
268                 while ((err == -ENOBUFS) && (retry_cnt < SPU_MB_RETRY_MAX)) {
269                         /*
270                          * Mailbox queue is full. Since MAY_SLEEP is set, assume
271                          * not in atomic context and we can wait and try again.
272                          */
273                         retry_cnt++;
274                         usleep_range(MBOX_SLEEP_MIN, MBOX_SLEEP_MAX);
275                         err = mbox_send_message(iproc_priv.mbox[chan_idx],
276                                                 mssg);
277                         atomic_inc(&iproc_priv.mb_no_spc);
278                 }
279         }
280         if (err < 0) {
281                 atomic_inc(&iproc_priv.mb_send_fail);
282                 return err;
283         }
284
285         /* Check error returned by mailbox controller */
286         err = mssg->error;
287         if (unlikely(err < 0)) {
288                 dev_err(dev, "message error %d", err);
289                 /* Signal txdone for mailbox channel */
290         }
291
292         /* Signal txdone for mailbox channel */
293         mbox_client_txdone(iproc_priv.mbox[chan_idx], err);
294         return err;
295 }
296
297 /**
298  * handle_ablkcipher_req() - Submit as much of a block cipher request as fits in
299  * a single SPU request message, starting at the current position in the request
300  * data.
301  * @rctx:       Crypto request context
302  *
303  * This may be called on the crypto API thread, or, when a request is so large
304  * it must be broken into multiple SPU messages, on the thread used to invoke
305  * the response callback. When requests are broken into multiple SPU
306  * messages, we assume subsequent messages depend on previous results, and
307  * thus always wait for previous results before submitting the next message.
308  * Because requests are submitted in lock step like this, there is no need
309  * to synchronize access to request data structures.
310  *
311  * Return: -EINPROGRESS: request has been accepted and result will be returned
312  *                       asynchronously
313  *         Any other value indicates an error
314  */
315 static int handle_ablkcipher_req(struct iproc_reqctx_s *rctx)
316 {
317         struct spu_hw *spu = &iproc_priv.spu;
318         struct crypto_async_request *areq = rctx->parent;
319         struct ablkcipher_request *req =
320             container_of(areq, struct ablkcipher_request, base);
321         struct iproc_ctx_s *ctx = rctx->ctx;
322         struct spu_cipher_parms cipher_parms;
323         int err = 0;
324         unsigned int chunksize = 0;     /* Num bytes of request to submit */
325         int remaining = 0;      /* Bytes of request still to process */
326         int chunk_start;        /* Beginning of data for current SPU msg */
327
328         /* IV or ctr value to use in this SPU msg */
329         u8 local_iv_ctr[MAX_IV_SIZE];
330         u32 stat_pad_len;       /* num bytes to align status field */
331         u32 pad_len;            /* total length of all padding */
332         bool update_key = false;
333         struct brcm_message *mssg;      /* mailbox message */
334
335         /* number of entries in src and dst sg in mailbox message. */
336         u8 rx_frag_num = 2;     /* response header and STATUS */
337         u8 tx_frag_num = 1;     /* request header */
338
339         flow_log("%s\n", __func__);
340
341         cipher_parms.alg = ctx->cipher.alg;
342         cipher_parms.mode = ctx->cipher.mode;
343         cipher_parms.type = ctx->cipher_type;
344         cipher_parms.key_len = ctx->enckeylen;
345         cipher_parms.key_buf = ctx->enckey;
346         cipher_parms.iv_buf = local_iv_ctr;
347         cipher_parms.iv_len = rctx->iv_ctr_len;
348
349         mssg = &rctx->mb_mssg;
350         chunk_start = rctx->src_sent;
351         remaining = rctx->total_todo - chunk_start;
352
353         /* determine the chunk we are breaking off and update the indexes */
354         if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
355             (remaining > ctx->max_payload))
356                 chunksize = ctx->max_payload;
357         else
358                 chunksize = remaining;
359
360         rctx->src_sent += chunksize;
361         rctx->total_sent = rctx->src_sent;
362
363         /* Count number of sg entries to be included in this request */
364         rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
365         rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
366
367         if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
368             rctx->is_encrypt && chunk_start)
369                 /*
370                  * Encrypting non-first first chunk. Copy last block of
371                  * previous result to IV for this chunk.
372                  */
373                 sg_copy_part_to_buf(req->dst, rctx->msg_buf.iv_ctr,
374                                     rctx->iv_ctr_len,
375                                     chunk_start - rctx->iv_ctr_len);
376
377         if (rctx->iv_ctr_len) {
378                 /* get our local copy of the iv */
379                 __builtin_memcpy(local_iv_ctr, rctx->msg_buf.iv_ctr,
380                                  rctx->iv_ctr_len);
381
382                 /* generate the next IV if possible */
383                 if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
384                     !rctx->is_encrypt) {
385                         /*
386                          * CBC Decrypt: next IV is the last ciphertext block in
387                          * this chunk
388                          */
389                         sg_copy_part_to_buf(req->src, rctx->msg_buf.iv_ctr,
390                                             rctx->iv_ctr_len,
391                                             rctx->src_sent - rctx->iv_ctr_len);
392                 } else if (ctx->cipher.mode == CIPHER_MODE_CTR) {
393                         /*
394                          * The SPU hardware increments the counter once for
395                          * each AES block of 16 bytes. So update the counter
396                          * for the next chunk, if there is one. Note that for
397                          * this chunk, the counter has already been copied to
398                          * local_iv_ctr. We can assume a block size of 16,
399                          * because we only support CTR mode for AES, not for
400                          * any other cipher alg.
401                          */
402                         add_to_ctr(rctx->msg_buf.iv_ctr, chunksize >> 4);
403                 }
404         }
405
406         if (ctx->cipher.alg == CIPHER_ALG_RC4) {
407                 rx_frag_num++;
408                 if (chunk_start) {
409                         /*
410                          * for non-first RC4 chunks, use SUPDT from previous
411                          * response as key for this chunk.
412                          */
413                         cipher_parms.key_buf = rctx->msg_buf.c.supdt_tweak;
414                         update_key = true;
415                         cipher_parms.type = CIPHER_TYPE_UPDT;
416                 } else if (!rctx->is_encrypt) {
417                         /*
418                          * First RC4 chunk. For decrypt, key in pre-built msg
419                          * header may have been changed if encrypt required
420                          * multiple chunks. So revert the key to the
421                          * ctx->enckey value.
422                          */
423                         update_key = true;
424                         cipher_parms.type = CIPHER_TYPE_INIT;
425                 }
426         }
427
428         if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
429                 flow_log("max_payload infinite\n");
430         else
431                 flow_log("max_payload %u\n", ctx->max_payload);
432
433         flow_log("sent:%u start:%u remains:%u size:%u\n",
434                  rctx->src_sent, chunk_start, remaining, chunksize);
435
436         /* Copy SPU header template created at setkey time */
437         memcpy(rctx->msg_buf.bcm_spu_req_hdr, ctx->bcm_spu_req_hdr,
438                sizeof(rctx->msg_buf.bcm_spu_req_hdr));
439
440         /*
441          * Pass SUPDT field as key. Key field in finish() call is only used
442          * when update_key has been set above for RC4. Will be ignored in
443          * all other cases.
444          */
445         spu->spu_cipher_req_finish(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
446                                    ctx->spu_req_hdr_len, !(rctx->is_encrypt),
447                                    &cipher_parms, update_key, chunksize);
448
449         atomic64_add(chunksize, &iproc_priv.bytes_out);
450
451         stat_pad_len = spu->spu_wordalign_padlen(chunksize);
452         if (stat_pad_len)
453                 rx_frag_num++;
454         pad_len = stat_pad_len;
455         if (pad_len) {
456                 tx_frag_num++;
457                 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, 0,
458                                      0, ctx->auth.alg, ctx->auth.mode,
459                                      rctx->total_sent, stat_pad_len);
460         }
461
462         spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
463                               ctx->spu_req_hdr_len);
464         packet_log("payload:\n");
465         dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
466         packet_dump("   pad: ", rctx->msg_buf.spu_req_pad, pad_len);
467
468         /*
469          * Build mailbox message containing SPU request msg and rx buffers
470          * to catch response message
471          */
472         memset(mssg, 0, sizeof(*mssg));
473         mssg->type = BRCM_MESSAGE_SPU;
474         mssg->ctx = rctx;       /* Will be returned in response */
475
476         /* Create rx scatterlist to catch result */
477         rx_frag_num += rctx->dst_nents;
478
479         if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
480             spu->spu_xts_tweak_in_payload())
481                 rx_frag_num++;  /* extra sg to insert tweak */
482
483         err = spu_ablkcipher_rx_sg_create(mssg, rctx, rx_frag_num, chunksize,
484                                           stat_pad_len);
485         if (err)
486                 return err;
487
488         /* Create tx scatterlist containing SPU request message */
489         tx_frag_num += rctx->src_nents;
490         if (spu->spu_tx_status_len())
491                 tx_frag_num++;
492
493         if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
494             spu->spu_xts_tweak_in_payload())
495                 tx_frag_num++;  /* extra sg to insert tweak */
496
497         err = spu_ablkcipher_tx_sg_create(mssg, rctx, tx_frag_num, chunksize,
498                                           pad_len);
499         if (err)
500                 return err;
501
502         err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
503         if (unlikely(err < 0))
504                 return err;
505
506         return -EINPROGRESS;
507 }
508
509 /**
510  * handle_ablkcipher_resp() - Process a block cipher SPU response. Updates the
511  * total received count for the request and updates global stats.
512  * @rctx:       Crypto request context
513  */
514 static void handle_ablkcipher_resp(struct iproc_reqctx_s *rctx)
515 {
516         struct spu_hw *spu = &iproc_priv.spu;
517 #ifdef DEBUG
518         struct crypto_async_request *areq = rctx->parent;
519         struct ablkcipher_request *req = ablkcipher_request_cast(areq);
520 #endif
521         struct iproc_ctx_s *ctx = rctx->ctx;
522         u32 payload_len;
523
524         /* See how much data was returned */
525         payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
526
527         /*
528          * In XTS mode, the first SPU_XTS_TWEAK_SIZE bytes may be the
529          * encrypted tweak ("i") value; we don't count those.
530          */
531         if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
532             spu->spu_xts_tweak_in_payload() &&
533             (payload_len >= SPU_XTS_TWEAK_SIZE))
534                 payload_len -= SPU_XTS_TWEAK_SIZE;
535
536         atomic64_add(payload_len, &iproc_priv.bytes_in);
537
538         flow_log("%s() offset: %u, bd_len: %u BD:\n",
539                  __func__, rctx->total_received, payload_len);
540
541         dump_sg(req->dst, rctx->total_received, payload_len);
542         if (ctx->cipher.alg == CIPHER_ALG_RC4)
543                 packet_dump("  supdt ", rctx->msg_buf.c.supdt_tweak,
544                             SPU_SUPDT_LEN);
545
546         rctx->total_received += payload_len;
547         if (rctx->total_received == rctx->total_todo) {
548                 atomic_inc(&iproc_priv.op_counts[SPU_OP_CIPHER]);
549                 atomic_inc(
550                    &iproc_priv.cipher_cnt[ctx->cipher.alg][ctx->cipher.mode]);
551         }
552 }
553
554 /**
555  * spu_ahash_rx_sg_create() - Build up the scatterlist of buffers used to
556  * receive a SPU response message for an ahash request.
557  * @mssg:       mailbox message containing the receive sg
558  * @rctx:       crypto request context
559  * @rx_frag_num: number of scatterlist elements required to hold the
560  *              SPU response message
561  * @digestsize: length of hash digest, in bytes
562  * @stat_pad_len: Number of bytes required to pad the STAT field to
563  *              a 4-byte boundary
564  *
565  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
566  * when the request completes, whether the request is handled successfully or
567  * there is an error.
568  *
569  * Return:
570  *   0 if successful
571  *   < 0 if an error
572  */
573 static int
574 spu_ahash_rx_sg_create(struct brcm_message *mssg,
575                        struct iproc_reqctx_s *rctx,
576                        u8 rx_frag_num, unsigned int digestsize,
577                        u32 stat_pad_len)
578 {
579         struct spu_hw *spu = &iproc_priv.spu;
580         struct scatterlist *sg; /* used to build sgs in mbox message */
581         struct iproc_ctx_s *ctx = rctx->ctx;
582
583         mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
584                                 rctx->gfp);
585         if (!mssg->spu.dst)
586                 return -ENOMEM;
587
588         sg = mssg->spu.dst;
589         sg_init_table(sg, rx_frag_num);
590         /* Space for SPU message header */
591         sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
592
593         /* Space for digest */
594         sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
595
596         if (stat_pad_len)
597                 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
598
599         memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
600         sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
601         return 0;
602 }
603
604 /**
605  * spu_ahash_tx_sg_create() -  Build up the scatterlist of buffers used to send
606  * a SPU request message for an ahash request. Includes SPU message headers and
607  * the request data.
608  * @mssg:       mailbox message containing the transmit sg
609  * @rctx:       crypto request context
610  * @tx_frag_num: number of scatterlist elements required to construct the
611  *              SPU request message
612  * @spu_hdr_len: length in bytes of SPU message header
613  * @hash_carry_len: Number of bytes of data carried over from previous req
614  * @new_data_len: Number of bytes of new request data
615  * @pad_len:    Number of pad bytes
616  *
617  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
618  * when the request completes, whether the request is handled successfully or
619  * there is an error.
620  *
621  * Return:
622  *   0 if successful
623  *   < 0 if an error
624  */
625 static int
626 spu_ahash_tx_sg_create(struct brcm_message *mssg,
627                        struct iproc_reqctx_s *rctx,
628                        u8 tx_frag_num,
629                        u32 spu_hdr_len,
630                        unsigned int hash_carry_len,
631                        unsigned int new_data_len, u32 pad_len)
632 {
633         struct spu_hw *spu = &iproc_priv.spu;
634         struct scatterlist *sg; /* used to build sgs in mbox message */
635         u32 datalen;            /* Number of bytes of response data expected */
636         u32 stat_len;
637
638         mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
639                                 rctx->gfp);
640         if (!mssg->spu.src)
641                 return -ENOMEM;
642
643         sg = mssg->spu.src;
644         sg_init_table(sg, tx_frag_num);
645
646         sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
647                    BCM_HDR_LEN + spu_hdr_len);
648
649         if (hash_carry_len)
650                 sg_set_buf(sg++, rctx->hash_carry, hash_carry_len);
651
652         if (new_data_len) {
653                 /* Copy in each src sg entry from request, up to chunksize */
654                 datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
655                                          rctx->src_nents, new_data_len);
656                 if (datalen < new_data_len) {
657                         pr_err("%s(): failed to copy src sg to mbox msg",
658                                __func__);
659                         return -EFAULT;
660                 }
661         }
662
663         if (pad_len)
664                 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
665
666         stat_len = spu->spu_tx_status_len();
667         if (stat_len) {
668                 memset(rctx->msg_buf.tx_stat, 0, stat_len);
669                 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
670         }
671
672         return 0;
673 }
674
675 /**
676  * handle_ahash_req() - Process an asynchronous hash request from the crypto
677  * API.
678  * @rctx:  Crypto request context
679  *
680  * Builds a SPU request message embedded in a mailbox message and submits the
681  * mailbox message on a selected mailbox channel. The SPU request message is
682  * constructed as a scatterlist, including entries from the crypto API's
683  * src scatterlist to avoid copying the data to be hashed. This function is
684  * called either on the thread from the crypto API, or, in the case that the
685  * crypto API request is too large to fit in a single SPU request message,
686  * on the thread that invokes the receive callback with a response message.
687  * Because some operations require the response from one chunk before the next
688  * chunk can be submitted, we always wait for the response for the previous
689  * chunk before submitting the next chunk. Because requests are submitted in
690  * lock step like this, there is no need to synchronize access to request data
691  * structures.
692  *
693  * Return:
694  *   -EINPROGRESS: request has been submitted to SPU and response will be
695  *                 returned asynchronously
696  *   -EAGAIN:      non-final request included a small amount of data, which for
697  *                 efficiency we did not submit to the SPU, but instead stored
698  *                 to be submitted to the SPU with the next part of the request
699  *   other:        an error code
700  */
701 static int handle_ahash_req(struct iproc_reqctx_s *rctx)
702 {
703         struct spu_hw *spu = &iproc_priv.spu;
704         struct crypto_async_request *areq = rctx->parent;
705         struct ahash_request *req = ahash_request_cast(areq);
706         struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
707         struct crypto_tfm *tfm = crypto_ahash_tfm(ahash);
708         unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
709         struct iproc_ctx_s *ctx = rctx->ctx;
710
711         /* number of bytes still to be hashed in this req */
712         unsigned int nbytes_to_hash = 0;
713         int err = 0;
714         unsigned int chunksize = 0;     /* length of hash carry + new data */
715         /*
716          * length of new data, not from hash carry, to be submitted in
717          * this hw request
718          */
719         unsigned int new_data_len;
720
721         unsigned int __maybe_unused chunk_start = 0;
722         u32 db_size;     /* Length of data field, incl gcm and hash padding */
723         int pad_len = 0; /* total pad len, including gcm, hash, stat padding */
724         u32 data_pad_len = 0;   /* length of GCM/CCM padding */
725         u32 stat_pad_len = 0;   /* length of padding to align STATUS word */
726         struct brcm_message *mssg;      /* mailbox message */
727         struct spu_request_opts req_opts;
728         struct spu_cipher_parms cipher_parms;
729         struct spu_hash_parms hash_parms;
730         struct spu_aead_parms aead_parms;
731         unsigned int local_nbuf;
732         u32 spu_hdr_len;
733         unsigned int digestsize;
734         u16 rem = 0;
735
736         /*
737          * number of entries in src and dst sg. Always includes SPU msg header.
738          * rx always includes a buffer to catch digest and STATUS.
739          */
740         u8 rx_frag_num = 3;
741         u8 tx_frag_num = 1;
742
743         flow_log("total_todo %u, total_sent %u\n",
744                  rctx->total_todo, rctx->total_sent);
745
746         memset(&req_opts, 0, sizeof(req_opts));
747         memset(&cipher_parms, 0, sizeof(cipher_parms));
748         memset(&hash_parms, 0, sizeof(hash_parms));
749         memset(&aead_parms, 0, sizeof(aead_parms));
750
751         req_opts.bd_suppress = true;
752         hash_parms.alg = ctx->auth.alg;
753         hash_parms.mode = ctx->auth.mode;
754         hash_parms.type = HASH_TYPE_NONE;
755         hash_parms.key_buf = (u8 *)ctx->authkey;
756         hash_parms.key_len = ctx->authkeylen;
757
758         /*
759          * For hash algorithms below assignment looks bit odd but
760          * it's needed for AES-XCBC and AES-CMAC hash algorithms
761          * to differentiate between 128, 192, 256 bit key values.
762          * Based on the key values, hash algorithm is selected.
763          * For example for 128 bit key, hash algorithm is AES-128.
764          */
765         cipher_parms.type = ctx->cipher_type;
766
767         mssg = &rctx->mb_mssg;
768         chunk_start = rctx->src_sent;
769
770         /*
771          * Compute the amount remaining to hash. This may include data
772          * carried over from previous requests.
773          */
774         nbytes_to_hash = rctx->total_todo - rctx->total_sent;
775         chunksize = nbytes_to_hash;
776         if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
777             (chunksize > ctx->max_payload))
778                 chunksize = ctx->max_payload;
779
780         /*
781          * If this is not a final request and the request data is not a multiple
782          * of a full block, then simply park the extra data and prefix it to the
783          * data for the next request.
784          */
785         if (!rctx->is_final) {
786                 u8 *dest = rctx->hash_carry + rctx->hash_carry_len;
787                 u16 new_len;  /* len of data to add to hash carry */
788
789                 rem = chunksize % blocksize;   /* remainder */
790                 if (rem) {
791                         /* chunksize not a multiple of blocksize */
792                         chunksize -= rem;
793                         if (chunksize == 0) {
794                                 /* Don't have a full block to submit to hw */
795                                 new_len = rem - rctx->hash_carry_len;
796                                 sg_copy_part_to_buf(req->src, dest, new_len,
797                                                     rctx->src_sent);
798                                 rctx->hash_carry_len = rem;
799                                 flow_log("Exiting with hash carry len: %u\n",
800                                          rctx->hash_carry_len);
801                                 packet_dump("  buf: ",
802                                             rctx->hash_carry,
803                                             rctx->hash_carry_len);
804                                 return -EAGAIN;
805                         }
806                 }
807         }
808
809         /* if we have hash carry, then prefix it to the data in this request */
810         local_nbuf = rctx->hash_carry_len;
811         rctx->hash_carry_len = 0;
812         if (local_nbuf)
813                 tx_frag_num++;
814         new_data_len = chunksize - local_nbuf;
815
816         /* Count number of sg entries to be used in this request */
817         rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip,
818                                        new_data_len);
819
820         /* AES hashing keeps key size in type field, so need to copy it here */
821         if (hash_parms.alg == HASH_ALG_AES)
822                 hash_parms.type = cipher_parms.type;
823         else
824                 hash_parms.type = spu->spu_hash_type(rctx->total_sent);
825
826         digestsize = spu->spu_digest_size(ctx->digestsize, ctx->auth.alg,
827                                           hash_parms.type);
828         hash_parms.digestsize = digestsize;
829
830         /* update the indexes */
831         rctx->total_sent += chunksize;
832         /* if you sent a prebuf then that wasn't from this req->src */
833         rctx->src_sent += new_data_len;
834
835         if ((rctx->total_sent == rctx->total_todo) && rctx->is_final)
836                 hash_parms.pad_len = spu->spu_hash_pad_len(hash_parms.alg,
837                                                            hash_parms.mode,
838                                                            chunksize,
839                                                            blocksize);
840
841         /*
842          * If a non-first chunk, then include the digest returned from the
843          * previous chunk so that hw can add to it (except for AES types).
844          */
845         if ((hash_parms.type == HASH_TYPE_UPDT) &&
846             (hash_parms.alg != HASH_ALG_AES)) {
847                 hash_parms.key_buf = rctx->incr_hash;
848                 hash_parms.key_len = digestsize;
849         }
850
851         atomic64_add(chunksize, &iproc_priv.bytes_out);
852
853         flow_log("%s() final: %u nbuf: %u ",
854                  __func__, rctx->is_final, local_nbuf);
855
856         if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
857                 flow_log("max_payload infinite\n");
858         else
859                 flow_log("max_payload %u\n", ctx->max_payload);
860
861         flow_log("chunk_start: %u chunk_size: %u\n", chunk_start, chunksize);
862
863         /* Prepend SPU header with type 3 BCM header */
864         memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
865
866         hash_parms.prebuf_len = local_nbuf;
867         spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
868                                               BCM_HDR_LEN,
869                                               &req_opts, &cipher_parms,
870                                               &hash_parms, &aead_parms,
871                                               new_data_len);
872
873         if (spu_hdr_len == 0) {
874                 pr_err("Failed to create SPU request header\n");
875                 return -EFAULT;
876         }
877
878         /*
879          * Determine total length of padding required. Put all padding in one
880          * buffer.
881          */
882         data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode, chunksize);
883         db_size = spu_real_db_size(0, 0, local_nbuf, new_data_len,
884                                    0, 0, hash_parms.pad_len);
885         if (spu->spu_tx_status_len())
886                 stat_pad_len = spu->spu_wordalign_padlen(db_size);
887         if (stat_pad_len)
888                 rx_frag_num++;
889         pad_len = hash_parms.pad_len + data_pad_len + stat_pad_len;
890         if (pad_len) {
891                 tx_frag_num++;
892                 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, data_pad_len,
893                                      hash_parms.pad_len, ctx->auth.alg,
894                                      ctx->auth.mode, rctx->total_sent,
895                                      stat_pad_len);
896         }
897
898         spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
899                               spu_hdr_len);
900         packet_dump("    prebuf: ", rctx->hash_carry, local_nbuf);
901         flow_log("Data:\n");
902         dump_sg(rctx->src_sg, rctx->src_skip, new_data_len);
903         packet_dump("   pad: ", rctx->msg_buf.spu_req_pad, pad_len);
904
905         /*
906          * Build mailbox message containing SPU request msg and rx buffers
907          * to catch response message
908          */
909         memset(mssg, 0, sizeof(*mssg));
910         mssg->type = BRCM_MESSAGE_SPU;
911         mssg->ctx = rctx;       /* Will be returned in response */
912
913         /* Create rx scatterlist to catch result */
914         err = spu_ahash_rx_sg_create(mssg, rctx, rx_frag_num, digestsize,
915                                      stat_pad_len);
916         if (err)
917                 return err;
918
919         /* Create tx scatterlist containing SPU request message */
920         tx_frag_num += rctx->src_nents;
921         if (spu->spu_tx_status_len())
922                 tx_frag_num++;
923         err = spu_ahash_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
924                                      local_nbuf, new_data_len, pad_len);
925         if (err)
926                 return err;
927
928         err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
929         if (unlikely(err < 0))
930                 return err;
931
932         return -EINPROGRESS;
933 }
934
935 /**
936  * spu_hmac_outer_hash() - Request synchonous software compute of the outer hash
937  * for an HMAC request.
938  * @req:  The HMAC request from the crypto API
939  * @ctx:  The session context
940  *
941  * Return: 0 if synchronous hash operation successful
942  *         -EINVAL if the hash algo is unrecognized
943  *         any other value indicates an error
944  */
945 static int spu_hmac_outer_hash(struct ahash_request *req,
946                                struct iproc_ctx_s *ctx)
947 {
948         struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
949         unsigned int blocksize =
950                 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
951         int rc;
952
953         switch (ctx->auth.alg) {
954         case HASH_ALG_MD5:
955                 rc = do_shash("md5", req->result, ctx->opad, blocksize,
956                               req->result, ctx->digestsize, NULL, 0);
957                 break;
958         case HASH_ALG_SHA1:
959                 rc = do_shash("sha1", req->result, ctx->opad, blocksize,
960                               req->result, ctx->digestsize, NULL, 0);
961                 break;
962         case HASH_ALG_SHA224:
963                 rc = do_shash("sha224", req->result, ctx->opad, blocksize,
964                               req->result, ctx->digestsize, NULL, 0);
965                 break;
966         case HASH_ALG_SHA256:
967                 rc = do_shash("sha256", req->result, ctx->opad, blocksize,
968                               req->result, ctx->digestsize, NULL, 0);
969                 break;
970         case HASH_ALG_SHA384:
971                 rc = do_shash("sha384", req->result, ctx->opad, blocksize,
972                               req->result, ctx->digestsize, NULL, 0);
973                 break;
974         case HASH_ALG_SHA512:
975                 rc = do_shash("sha512", req->result, ctx->opad, blocksize,
976                               req->result, ctx->digestsize, NULL, 0);
977                 break;
978         default:
979                 pr_err("%s() Error : unknown hmac type\n", __func__);
980                 rc = -EINVAL;
981         }
982         return rc;
983 }
984
985 /**
986  * ahash_req_done() - Process a hash result from the SPU hardware.
987  * @rctx: Crypto request context
988  *
989  * Return: 0 if successful
990  *         < 0 if an error
991  */
992 static int ahash_req_done(struct iproc_reqctx_s *rctx)
993 {
994         struct spu_hw *spu = &iproc_priv.spu;
995         struct crypto_async_request *areq = rctx->parent;
996         struct ahash_request *req = ahash_request_cast(areq);
997         struct iproc_ctx_s *ctx = rctx->ctx;
998         int err;
999
1000         memcpy(req->result, rctx->msg_buf.digest, ctx->digestsize);
1001
1002         if (spu->spu_type == SPU_TYPE_SPUM) {
1003                 /* byte swap the output from the UPDT function to network byte
1004                  * order
1005                  */
1006                 if (ctx->auth.alg == HASH_ALG_MD5) {
1007                         __swab32s((u32 *)req->result);
1008                         __swab32s(((u32 *)req->result) + 1);
1009                         __swab32s(((u32 *)req->result) + 2);
1010                         __swab32s(((u32 *)req->result) + 3);
1011                         __swab32s(((u32 *)req->result) + 4);
1012                 }
1013         }
1014
1015         flow_dump("  digest ", req->result, ctx->digestsize);
1016
1017         /* if this an HMAC then do the outer hash */
1018         if (rctx->is_sw_hmac) {
1019                 err = spu_hmac_outer_hash(req, ctx);
1020                 if (err < 0)
1021                         return err;
1022                 flow_dump("  hmac: ", req->result, ctx->digestsize);
1023         }
1024
1025         if (rctx->is_sw_hmac || ctx->auth.mode == HASH_MODE_HMAC) {
1026                 atomic_inc(&iproc_priv.op_counts[SPU_OP_HMAC]);
1027                 atomic_inc(&iproc_priv.hmac_cnt[ctx->auth.alg]);
1028         } else {
1029                 atomic_inc(&iproc_priv.op_counts[SPU_OP_HASH]);
1030                 atomic_inc(&iproc_priv.hash_cnt[ctx->auth.alg]);
1031         }
1032
1033         return 0;
1034 }
1035
1036 /**
1037  * handle_ahash_resp() - Process a SPU response message for a hash request.
1038  * Checks if the entire crypto API request has been processed, and if so,
1039  * invokes post processing on the result.
1040  * @rctx: Crypto request context
1041  */
1042 static void handle_ahash_resp(struct iproc_reqctx_s *rctx)
1043 {
1044         struct iproc_ctx_s *ctx = rctx->ctx;
1045 #ifdef DEBUG
1046         struct crypto_async_request *areq = rctx->parent;
1047         struct ahash_request *req = ahash_request_cast(areq);
1048         struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1049         unsigned int blocksize =
1050                 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
1051 #endif
1052         /*
1053          * Save hash to use as input to next op if incremental. Might be copying
1054          * too much, but that's easier than figuring out actual digest size here
1055          */
1056         memcpy(rctx->incr_hash, rctx->msg_buf.digest, MAX_DIGEST_SIZE);
1057
1058         flow_log("%s() blocksize:%u digestsize:%u\n",
1059                  __func__, blocksize, ctx->digestsize);
1060
1061         atomic64_add(ctx->digestsize, &iproc_priv.bytes_in);
1062
1063         if (rctx->is_final && (rctx->total_sent == rctx->total_todo))
1064                 ahash_req_done(rctx);
1065 }
1066
1067 /**
1068  * spu_aead_rx_sg_create() - Build up the scatterlist of buffers used to receive
1069  * a SPU response message for an AEAD request. Includes buffers to catch SPU
1070  * message headers and the response data.
1071  * @mssg:       mailbox message containing the receive sg
1072  * @rctx:       crypto request context
1073  * @rx_frag_num: number of scatterlist elements required to hold the
1074  *              SPU response message
1075  * @assoc_len:  Length of associated data included in the crypto request
1076  * @ret_iv_len: Length of IV returned in response
1077  * @resp_len:   Number of bytes of response data expected to be written to
1078  *              dst buffer from crypto API
1079  * @digestsize: Length of hash digest, in bytes
1080  * @stat_pad_len: Number of bytes required to pad the STAT field to
1081  *              a 4-byte boundary
1082  *
1083  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1084  * when the request completes, whether the request is handled successfully or
1085  * there is an error.
1086  *
1087  * Returns:
1088  *   0 if successful
1089  *   < 0 if an error
1090  */
1091 static int spu_aead_rx_sg_create(struct brcm_message *mssg,
1092                                  struct aead_request *req,
1093                                  struct iproc_reqctx_s *rctx,
1094                                  u8 rx_frag_num,
1095                                  unsigned int assoc_len,
1096                                  u32 ret_iv_len, unsigned int resp_len,
1097                                  unsigned int digestsize, u32 stat_pad_len)
1098 {
1099         struct spu_hw *spu = &iproc_priv.spu;
1100         struct scatterlist *sg; /* used to build sgs in mbox message */
1101         struct iproc_ctx_s *ctx = rctx->ctx;
1102         u32 datalen;            /* Number of bytes of response data expected */
1103         u32 assoc_buf_len;
1104         u8 data_padlen = 0;
1105
1106         if (ctx->is_rfc4543) {
1107                 /* RFC4543: only pad after data, not after AAD */
1108                 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1109                                                           assoc_len + resp_len);
1110                 assoc_buf_len = assoc_len;
1111         } else {
1112                 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1113                                                           resp_len);
1114                 assoc_buf_len = spu->spu_assoc_resp_len(ctx->cipher.mode,
1115                                                 assoc_len, ret_iv_len,
1116                                                 rctx->is_encrypt);
1117         }
1118
1119         if (ctx->cipher.mode == CIPHER_MODE_CCM)
1120                 /* ICV (after data) must be in the next 32-bit word for CCM */
1121                 data_padlen += spu->spu_wordalign_padlen(assoc_buf_len +
1122                                                          resp_len +
1123                                                          data_padlen);
1124
1125         if (data_padlen)
1126                 /* have to catch gcm pad in separate buffer */
1127                 rx_frag_num++;
1128
1129         mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
1130                                 rctx->gfp);
1131         if (!mssg->spu.dst)
1132                 return -ENOMEM;
1133
1134         sg = mssg->spu.dst;
1135         sg_init_table(sg, rx_frag_num);
1136
1137         /* Space for SPU message header */
1138         sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
1139
1140         if (assoc_buf_len) {
1141                 /*
1142                  * Don't write directly to req->dst, because SPU may pad the
1143                  * assoc data in the response
1144                  */
1145                 memset(rctx->msg_buf.a.resp_aad, 0, assoc_buf_len);
1146                 sg_set_buf(sg++, rctx->msg_buf.a.resp_aad, assoc_buf_len);
1147         }
1148
1149         if (resp_len) {
1150                 /*
1151                  * Copy in each dst sg entry from request, up to chunksize.
1152                  * dst sg catches just the data. digest caught in separate buf.
1153                  */
1154                 datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
1155                                          rctx->dst_nents, resp_len);
1156                 if (datalen < (resp_len)) {
1157                         pr_err("%s(): failed to copy dst sg to mbox msg. expected len %u, datalen %u",
1158                                __func__, resp_len, datalen);
1159                         return -EFAULT;
1160                 }
1161         }
1162
1163         /* If GCM/CCM data is padded, catch padding in separate buffer */
1164         if (data_padlen) {
1165                 memset(rctx->msg_buf.a.gcmpad, 0, data_padlen);
1166                 sg_set_buf(sg++, rctx->msg_buf.a.gcmpad, data_padlen);
1167         }
1168
1169         /* Always catch ICV in separate buffer */
1170         sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
1171
1172         flow_log("stat_pad_len %u\n", stat_pad_len);
1173         if (stat_pad_len) {
1174                 memset(rctx->msg_buf.rx_stat_pad, 0, stat_pad_len);
1175                 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
1176         }
1177
1178         memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
1179         sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
1180
1181         return 0;
1182 }
1183
1184 /**
1185  * spu_aead_tx_sg_create() - Build up the scatterlist of buffers used to send a
1186  * SPU request message for an AEAD request. Includes SPU message headers and the
1187  * request data.
1188  * @mssg:       mailbox message containing the transmit sg
1189  * @rctx:       crypto request context
1190  * @tx_frag_num: number of scatterlist elements required to construct the
1191  *              SPU request message
1192  * @spu_hdr_len: length of SPU message header in bytes
1193  * @assoc:      crypto API associated data scatterlist
1194  * @assoc_len:  length of associated data
1195  * @assoc_nents: number of scatterlist entries containing assoc data
1196  * @aead_iv_len: length of AEAD IV, if included
1197  * @chunksize:  Number of bytes of request data
1198  * @aad_pad_len: Number of bytes of padding at end of AAD. For GCM/CCM.
1199  * @pad_len:    Number of pad bytes
1200  * @incl_icv:   If true, write separate ICV buffer after data and
1201  *              any padding
1202  *
1203  * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1204  * when the request completes, whether the request is handled successfully or
1205  * there is an error.
1206  *
1207  * Return:
1208  *   0 if successful
1209  *   < 0 if an error
1210  */
1211 static int spu_aead_tx_sg_create(struct brcm_message *mssg,
1212                                  struct iproc_reqctx_s *rctx,
1213                                  u8 tx_frag_num,
1214                                  u32 spu_hdr_len,
1215                                  struct scatterlist *assoc,
1216                                  unsigned int assoc_len,
1217                                  int assoc_nents,
1218                                  unsigned int aead_iv_len,
1219                                  unsigned int chunksize,
1220                                  u32 aad_pad_len, u32 pad_len, bool incl_icv)
1221 {
1222         struct spu_hw *spu = &iproc_priv.spu;
1223         struct scatterlist *sg; /* used to build sgs in mbox message */
1224         struct scatterlist *assoc_sg = assoc;
1225         struct iproc_ctx_s *ctx = rctx->ctx;
1226         u32 datalen;            /* Number of bytes of data to write */
1227         u32 written;            /* Number of bytes of data written */
1228         u32 assoc_offset = 0;
1229         u32 stat_len;
1230
1231         mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
1232                                 rctx->gfp);
1233         if (!mssg->spu.src)
1234                 return -ENOMEM;
1235
1236         sg = mssg->spu.src;
1237         sg_init_table(sg, tx_frag_num);
1238
1239         sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
1240                    BCM_HDR_LEN + spu_hdr_len);
1241
1242         if (assoc_len) {
1243                 /* Copy in each associated data sg entry from request */
1244                 written = spu_msg_sg_add(&sg, &assoc_sg, &assoc_offset,
1245                                          assoc_nents, assoc_len);
1246                 if (written < assoc_len) {
1247                         pr_err("%s(): failed to copy assoc sg to mbox msg",
1248                                __func__);
1249                         return -EFAULT;
1250                 }
1251         }
1252
1253         if (aead_iv_len)
1254                 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, aead_iv_len);
1255
1256         if (aad_pad_len) {
1257                 memset(rctx->msg_buf.a.req_aad_pad, 0, aad_pad_len);
1258                 sg_set_buf(sg++, rctx->msg_buf.a.req_aad_pad, aad_pad_len);
1259         }
1260
1261         datalen = chunksize;
1262         if ((chunksize > ctx->digestsize) && incl_icv)
1263                 datalen -= ctx->digestsize;
1264         if (datalen) {
1265                 /* For aead, a single msg should consume the entire src sg */
1266                 written = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
1267                                          rctx->src_nents, datalen);
1268                 if (written < datalen) {
1269                         pr_err("%s(): failed to copy src sg to mbox msg",
1270                                __func__);
1271                         return -EFAULT;
1272                 }
1273         }
1274
1275         if (pad_len) {
1276                 memset(rctx->msg_buf.spu_req_pad, 0, pad_len);
1277                 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
1278         }
1279
1280         if (incl_icv)
1281                 sg_set_buf(sg++, rctx->msg_buf.digest, ctx->digestsize);
1282
1283         stat_len = spu->spu_tx_status_len();
1284         if (stat_len) {
1285                 memset(rctx->msg_buf.tx_stat, 0, stat_len);
1286                 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
1287         }
1288         return 0;
1289 }
1290
1291 /**
1292  * handle_aead_req() - Submit a SPU request message for the next chunk of the
1293  * current AEAD request.
1294  * @rctx:  Crypto request context
1295  *
1296  * Unlike other operation types, we assume the length of the request fits in
1297  * a single SPU request message. aead_enqueue() makes sure this is true.
1298  * Comments for other op types regarding threads applies here as well.
1299  *
1300  * Unlike incremental hash ops, where the spu returns the entire hash for
1301  * truncated algs like sha-224, the SPU returns just the truncated hash in
1302  * response to aead requests. So digestsize is always ctx->digestsize here.
1303  *
1304  * Return: -EINPROGRESS: crypto request has been accepted and result will be
1305  *                       returned asynchronously
1306  *         Any other value indicates an error
1307  */
1308 static int handle_aead_req(struct iproc_reqctx_s *rctx)
1309 {
1310         struct spu_hw *spu = &iproc_priv.spu;
1311         struct crypto_async_request *areq = rctx->parent;
1312         struct aead_request *req = container_of(areq,
1313                                                 struct aead_request, base);
1314         struct iproc_ctx_s *ctx = rctx->ctx;
1315         int err;
1316         unsigned int chunksize;
1317         unsigned int resp_len;
1318         u32 spu_hdr_len;
1319         u32 db_size;
1320         u32 stat_pad_len;
1321         u32 pad_len;
1322         struct brcm_message *mssg;      /* mailbox message */
1323         struct spu_request_opts req_opts;
1324         struct spu_cipher_parms cipher_parms;
1325         struct spu_hash_parms hash_parms;
1326         struct spu_aead_parms aead_parms;
1327         int assoc_nents = 0;
1328         bool incl_icv = false;
1329         unsigned int digestsize = ctx->digestsize;
1330
1331         /* number of entries in src and dst sg. Always includes SPU msg header.
1332          */
1333         u8 rx_frag_num = 2;     /* and STATUS */
1334         u8 tx_frag_num = 1;
1335
1336         /* doing the whole thing at once */
1337         chunksize = rctx->total_todo;
1338
1339         flow_log("%s: chunksize %u\n", __func__, chunksize);
1340
1341         memset(&req_opts, 0, sizeof(req_opts));
1342         memset(&hash_parms, 0, sizeof(hash_parms));
1343         memset(&aead_parms, 0, sizeof(aead_parms));
1344
1345         req_opts.is_inbound = !(rctx->is_encrypt);
1346         req_opts.auth_first = ctx->auth_first;
1347         req_opts.is_aead = true;
1348         req_opts.is_esp = ctx->is_esp;
1349
1350         cipher_parms.alg = ctx->cipher.alg;
1351         cipher_parms.mode = ctx->cipher.mode;
1352         cipher_parms.type = ctx->cipher_type;
1353         cipher_parms.key_buf = ctx->enckey;
1354         cipher_parms.key_len = ctx->enckeylen;
1355         cipher_parms.iv_buf = rctx->msg_buf.iv_ctr;
1356         cipher_parms.iv_len = rctx->iv_ctr_len;
1357
1358         hash_parms.alg = ctx->auth.alg;
1359         hash_parms.mode = ctx->auth.mode;
1360         hash_parms.type = HASH_TYPE_NONE;
1361         hash_parms.key_buf = (u8 *)ctx->authkey;
1362         hash_parms.key_len = ctx->authkeylen;
1363         hash_parms.digestsize = digestsize;
1364
1365         if ((ctx->auth.alg == HASH_ALG_SHA224) &&
1366             (ctx->authkeylen < SHA224_DIGEST_SIZE))
1367                 hash_parms.key_len = SHA224_DIGEST_SIZE;
1368
1369         aead_parms.assoc_size = req->assoclen;
1370         if (ctx->is_esp && !ctx->is_rfc4543) {
1371                 /*
1372                  * 8-byte IV is included assoc data in request. SPU2
1373                  * expects AAD to include just SPI and seqno. So
1374                  * subtract off the IV len.
1375                  */
1376                 aead_parms.assoc_size -= GCM_ESP_IV_SIZE;
1377
1378                 if (rctx->is_encrypt) {
1379                         aead_parms.return_iv = true;
1380                         aead_parms.ret_iv_len = GCM_ESP_IV_SIZE;
1381                         aead_parms.ret_iv_off = GCM_ESP_SALT_SIZE;
1382                 }
1383         } else {
1384                 aead_parms.ret_iv_len = 0;
1385         }
1386
1387         /*
1388          * Count number of sg entries from the crypto API request that are to
1389          * be included in this mailbox message. For dst sg, don't count space
1390          * for digest. Digest gets caught in a separate buffer and copied back
1391          * to dst sg when processing response.
1392          */
1393         rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
1394         rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
1395         if (aead_parms.assoc_size)
1396                 assoc_nents = spu_sg_count(rctx->assoc, 0,
1397                                            aead_parms.assoc_size);
1398
1399         mssg = &rctx->mb_mssg;
1400
1401         rctx->total_sent = chunksize;
1402         rctx->src_sent = chunksize;
1403         if (spu->spu_assoc_resp_len(ctx->cipher.mode,
1404                                     aead_parms.assoc_size,
1405                                     aead_parms.ret_iv_len,
1406                                     rctx->is_encrypt))
1407                 rx_frag_num++;
1408
1409         aead_parms.iv_len = spu->spu_aead_ivlen(ctx->cipher.mode,
1410                                                 rctx->iv_ctr_len);
1411
1412         if (ctx->auth.alg == HASH_ALG_AES)
1413                 hash_parms.type = ctx->cipher_type;
1414
1415         /* General case AAD padding (CCM and RFC4543 special cases below) */
1416         aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1417                                                  aead_parms.assoc_size);
1418
1419         /* General case data padding (CCM decrypt special case below) */
1420         aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1421                                                            chunksize);
1422
1423         if (ctx->cipher.mode == CIPHER_MODE_CCM) {
1424                 /*
1425                  * for CCM, AAD len + 2 (rather than AAD len) needs to be
1426                  * 128-bit aligned
1427                  */
1428                 aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(
1429                                          ctx->cipher.mode,
1430                                          aead_parms.assoc_size + 2);
1431
1432                 /*
1433                  * And when decrypting CCM, need to pad without including
1434                  * size of ICV which is tacked on to end of chunk
1435                  */
1436                 if (!rctx->is_encrypt)
1437                         aead_parms.data_pad_len =
1438                                 spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1439                                                         chunksize - digestsize);
1440
1441                 /* CCM also requires software to rewrite portions of IV: */
1442                 spu->spu_ccm_update_iv(digestsize, &cipher_parms, req->assoclen,
1443                                        chunksize, rctx->is_encrypt,
1444                                        ctx->is_esp);
1445         }
1446
1447         if (ctx->is_rfc4543) {
1448                 /*
1449                  * RFC4543: data is included in AAD, so don't pad after AAD
1450                  * and pad data based on both AAD + data size
1451                  */
1452                 aead_parms.aad_pad_len = 0;
1453                 if (!rctx->is_encrypt)
1454                         aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1455                                         ctx->cipher.mode,
1456                                         aead_parms.assoc_size + chunksize -
1457                                         digestsize);
1458                 else
1459                         aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1460                                         ctx->cipher.mode,
1461                                         aead_parms.assoc_size + chunksize);
1462
1463                 req_opts.is_rfc4543 = true;
1464         }
1465
1466         if (spu_req_incl_icv(ctx->cipher.mode, rctx->is_encrypt)) {
1467                 incl_icv = true;
1468                 tx_frag_num++;
1469                 /* Copy ICV from end of src scatterlist to digest buf */
1470                 sg_copy_part_to_buf(req->src, rctx->msg_buf.digest, digestsize,
1471                                     req->assoclen + rctx->total_sent -
1472                                     digestsize);
1473         }
1474
1475         atomic64_add(chunksize, &iproc_priv.bytes_out);
1476
1477         flow_log("%s()-sent chunksize:%u\n", __func__, chunksize);
1478
1479         /* Prepend SPU header with type 3 BCM header */
1480         memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1481
1482         spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
1483                                               BCM_HDR_LEN, &req_opts,
1484                                               &cipher_parms, &hash_parms,
1485                                               &aead_parms, chunksize);
1486
1487         /* Determine total length of padding. Put all padding in one buffer. */
1488         db_size = spu_real_db_size(aead_parms.assoc_size, aead_parms.iv_len, 0,
1489                                    chunksize, aead_parms.aad_pad_len,
1490                                    aead_parms.data_pad_len, 0);
1491
1492         stat_pad_len = spu->spu_wordalign_padlen(db_size);
1493
1494         if (stat_pad_len)
1495                 rx_frag_num++;
1496         pad_len = aead_parms.data_pad_len + stat_pad_len;
1497         if (pad_len) {
1498                 tx_frag_num++;
1499                 spu->spu_request_pad(rctx->msg_buf.spu_req_pad,
1500                                      aead_parms.data_pad_len, 0,
1501                                      ctx->auth.alg, ctx->auth.mode,
1502                                      rctx->total_sent, stat_pad_len);
1503         }
1504
1505         spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
1506                               spu_hdr_len);
1507         dump_sg(rctx->assoc, 0, aead_parms.assoc_size);
1508         packet_dump("    aead iv: ", rctx->msg_buf.iv_ctr, aead_parms.iv_len);
1509         packet_log("BD:\n");
1510         dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
1511         packet_dump("   pad: ", rctx->msg_buf.spu_req_pad, pad_len);
1512
1513         /*
1514          * Build mailbox message containing SPU request msg and rx buffers
1515          * to catch response message
1516          */
1517         memset(mssg, 0, sizeof(*mssg));
1518         mssg->type = BRCM_MESSAGE_SPU;
1519         mssg->ctx = rctx;       /* Will be returned in response */
1520
1521         /* Create rx scatterlist to catch result */
1522         rx_frag_num += rctx->dst_nents;
1523         resp_len = chunksize;
1524
1525         /*
1526          * Always catch ICV in separate buffer. Have to for GCM/CCM because of
1527          * padding. Have to for SHA-224 and other truncated SHAs because SPU
1528          * sends entire digest back.
1529          */
1530         rx_frag_num++;
1531
1532         if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
1533              (ctx->cipher.mode == CIPHER_MODE_CCM)) && !rctx->is_encrypt) {
1534                 /*
1535                  * Input is ciphertxt plus ICV, but ICV not incl
1536                  * in output.
1537                  */
1538                 resp_len -= ctx->digestsize;
1539                 if (resp_len == 0)
1540                         /* no rx frags to catch output data */
1541                         rx_frag_num -= rctx->dst_nents;
1542         }
1543
1544         err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
1545                                     aead_parms.assoc_size,
1546                                     aead_parms.ret_iv_len, resp_len, digestsize,
1547                                     stat_pad_len);
1548         if (err)
1549                 return err;
1550
1551         /* Create tx scatterlist containing SPU request message */
1552         tx_frag_num += rctx->src_nents;
1553         tx_frag_num += assoc_nents;
1554         if (aead_parms.aad_pad_len)
1555                 tx_frag_num++;
1556         if (aead_parms.iv_len)
1557                 tx_frag_num++;
1558         if (spu->spu_tx_status_len())
1559                 tx_frag_num++;
1560         err = spu_aead_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
1561                                     rctx->assoc, aead_parms.assoc_size,
1562                                     assoc_nents, aead_parms.iv_len, chunksize,
1563                                     aead_parms.aad_pad_len, pad_len, incl_icv);
1564         if (err)
1565                 return err;
1566
1567         err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
1568         if (unlikely(err < 0))
1569                 return err;
1570
1571         return -EINPROGRESS;
1572 }
1573
1574 /**
1575  * handle_aead_resp() - Process a SPU response message for an AEAD request.
1576  * @rctx:  Crypto request context
1577  */
1578 static void handle_aead_resp(struct iproc_reqctx_s *rctx)
1579 {
1580         struct spu_hw *spu = &iproc_priv.spu;
1581         struct crypto_async_request *areq = rctx->parent;
1582         struct aead_request *req = container_of(areq,
1583                                                 struct aead_request, base);
1584         struct iproc_ctx_s *ctx = rctx->ctx;
1585         u32 payload_len;
1586         unsigned int icv_offset;
1587         u32 result_len;
1588
1589         /* See how much data was returned */
1590         payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
1591         flow_log("payload_len %u\n", payload_len);
1592
1593         /* only count payload */
1594         atomic64_add(payload_len, &iproc_priv.bytes_in);
1595
1596         if (req->assoclen)
1597                 packet_dump("  assoc_data ", rctx->msg_buf.a.resp_aad,
1598                             req->assoclen);
1599
1600         /*
1601          * Copy the ICV back to the destination
1602          * buffer. In decrypt case, SPU gives us back the digest, but crypto
1603          * API doesn't expect ICV in dst buffer.
1604          */
1605         result_len = req->cryptlen;
1606         if (rctx->is_encrypt) {
1607                 icv_offset = req->assoclen + rctx->total_sent;
1608                 packet_dump("  ICV: ", rctx->msg_buf.digest, ctx->digestsize);
1609                 flow_log("copying ICV to dst sg at offset %u\n", icv_offset);
1610                 sg_copy_part_from_buf(req->dst, rctx->msg_buf.digest,
1611                                       ctx->digestsize, icv_offset);
1612                 result_len += ctx->digestsize;
1613         }
1614
1615         packet_log("response data:  ");
1616         dump_sg(req->dst, req->assoclen, result_len);
1617
1618         atomic_inc(&iproc_priv.op_counts[SPU_OP_AEAD]);
1619         if (ctx->cipher.alg == CIPHER_ALG_AES) {
1620                 if (ctx->cipher.mode == CIPHER_MODE_CCM)
1621                         atomic_inc(&iproc_priv.aead_cnt[AES_CCM]);
1622                 else if (ctx->cipher.mode == CIPHER_MODE_GCM)
1623                         atomic_inc(&iproc_priv.aead_cnt[AES_GCM]);
1624                 else
1625                         atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1626         } else {
1627                 atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1628         }
1629 }
1630
1631 /**
1632  * spu_chunk_cleanup() - Do cleanup after processing one chunk of a request
1633  * @rctx:  request context
1634  *
1635  * Mailbox scatterlists are allocated for each chunk. So free them after
1636  * processing each chunk.
1637  */
1638 static void spu_chunk_cleanup(struct iproc_reqctx_s *rctx)
1639 {
1640         /* mailbox message used to tx request */
1641         struct brcm_message *mssg = &rctx->mb_mssg;
1642
1643         kfree(mssg->spu.src);
1644         kfree(mssg->spu.dst);
1645         memset(mssg, 0, sizeof(struct brcm_message));
1646 }
1647
1648 /**
1649  * finish_req() - Used to invoke the complete callback from the requester when
1650  * a request has been handled asynchronously.
1651  * @rctx:  Request context
1652  * @err:   Indicates whether the request was successful or not
1653  *
1654  * Ensures that cleanup has been done for request
1655  */
1656 static void finish_req(struct iproc_reqctx_s *rctx, int err)
1657 {
1658         struct crypto_async_request *areq = rctx->parent;
1659
1660         flow_log("%s() err:%d\n\n", __func__, err);
1661
1662         /* No harm done if already called */
1663         spu_chunk_cleanup(rctx);
1664
1665         if (areq)
1666                 areq->complete(areq, err);
1667 }
1668
1669 /**
1670  * spu_rx_callback() - Callback from mailbox framework with a SPU response.
1671  * @cl:         mailbox client structure for SPU driver
1672  * @msg:        mailbox message containing SPU response
1673  */
1674 static void spu_rx_callback(struct mbox_client *cl, void *msg)
1675 {
1676         struct spu_hw *spu = &iproc_priv.spu;
1677         struct brcm_message *mssg = msg;
1678         struct iproc_reqctx_s *rctx;
1679         int err = 0;
1680
1681         rctx = mssg->ctx;
1682         if (unlikely(!rctx)) {
1683                 /* This is fatal */
1684                 pr_err("%s(): no request context", __func__);
1685                 err = -EFAULT;
1686                 goto cb_finish;
1687         }
1688
1689         /* process the SPU status */
1690         err = spu->spu_status_process(rctx->msg_buf.rx_stat);
1691         if (err != 0) {
1692                 if (err == SPU_INVALID_ICV)
1693                         atomic_inc(&iproc_priv.bad_icv);
1694                 err = -EBADMSG;
1695                 goto cb_finish;
1696         }
1697
1698         /* Process the SPU response message */
1699         switch (rctx->ctx->alg->type) {
1700         case CRYPTO_ALG_TYPE_ABLKCIPHER:
1701                 handle_ablkcipher_resp(rctx);
1702                 break;
1703         case CRYPTO_ALG_TYPE_AHASH:
1704                 handle_ahash_resp(rctx);
1705                 break;
1706         case CRYPTO_ALG_TYPE_AEAD:
1707                 handle_aead_resp(rctx);
1708                 break;
1709         default:
1710                 err = -EINVAL;
1711                 goto cb_finish;
1712         }
1713
1714         /*
1715          * If this response does not complete the request, then send the next
1716          * request chunk.
1717          */
1718         if (rctx->total_sent < rctx->total_todo) {
1719                 /* Deallocate anything specific to previous chunk */
1720                 spu_chunk_cleanup(rctx);
1721
1722                 switch (rctx->ctx->alg->type) {
1723                 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1724                         err = handle_ablkcipher_req(rctx);
1725                         break;
1726                 case CRYPTO_ALG_TYPE_AHASH:
1727                         err = handle_ahash_req(rctx);
1728                         if (err == -EAGAIN)
1729                                 /*
1730                                  * we saved data in hash carry, but tell crypto
1731                                  * API we successfully completed request.
1732                                  */
1733                                 err = 0;
1734                         break;
1735                 case CRYPTO_ALG_TYPE_AEAD:
1736                         err = handle_aead_req(rctx);
1737                         break;
1738                 default:
1739                         err = -EINVAL;
1740                 }
1741
1742                 if (err == -EINPROGRESS)
1743                         /* Successfully submitted request for next chunk */
1744                         return;
1745         }
1746
1747 cb_finish:
1748         finish_req(rctx, err);
1749 }
1750
1751 /* ==================== Kernel Cryptographic API ==================== */
1752
1753 /**
1754  * ablkcipher_enqueue() - Handle ablkcipher encrypt or decrypt request.
1755  * @req:        Crypto API request
1756  * @encrypt:    true if encrypting; false if decrypting
1757  *
1758  * Return: -EINPROGRESS if request accepted and result will be returned
1759  *                      asynchronously
1760  *         < 0 if an error
1761  */
1762 static int ablkcipher_enqueue(struct ablkcipher_request *req, bool encrypt)
1763 {
1764         struct iproc_reqctx_s *rctx = ablkcipher_request_ctx(req);
1765         struct iproc_ctx_s *ctx =
1766             crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
1767         int err;
1768
1769         flow_log("%s() enc:%u\n", __func__, encrypt);
1770
1771         rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1772                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1773         rctx->parent = &req->base;
1774         rctx->is_encrypt = encrypt;
1775         rctx->bd_suppress = false;
1776         rctx->total_todo = req->nbytes;
1777         rctx->src_sent = 0;
1778         rctx->total_sent = 0;
1779         rctx->total_received = 0;
1780         rctx->ctx = ctx;
1781
1782         /* Initialize current position in src and dst scatterlists */
1783         rctx->src_sg = req->src;
1784         rctx->src_nents = 0;
1785         rctx->src_skip = 0;
1786         rctx->dst_sg = req->dst;
1787         rctx->dst_nents = 0;
1788         rctx->dst_skip = 0;
1789
1790         if (ctx->cipher.mode == CIPHER_MODE_CBC ||
1791             ctx->cipher.mode == CIPHER_MODE_CTR ||
1792             ctx->cipher.mode == CIPHER_MODE_OFB ||
1793             ctx->cipher.mode == CIPHER_MODE_XTS ||
1794             ctx->cipher.mode == CIPHER_MODE_GCM ||
1795             ctx->cipher.mode == CIPHER_MODE_CCM) {
1796                 rctx->iv_ctr_len =
1797                     crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
1798                 memcpy(rctx->msg_buf.iv_ctr, req->info, rctx->iv_ctr_len);
1799         } else {
1800                 rctx->iv_ctr_len = 0;
1801         }
1802
1803         /* Choose a SPU to process this request */
1804         rctx->chan_idx = select_channel();
1805         err = handle_ablkcipher_req(rctx);
1806         if (err != -EINPROGRESS)
1807                 /* synchronous result */
1808                 spu_chunk_cleanup(rctx);
1809
1810         return err;
1811 }
1812
1813 static int des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1814                       unsigned int keylen)
1815 {
1816         struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1817         u32 tmp[DES_EXPKEY_WORDS];
1818
1819         if (keylen == DES_KEY_SIZE) {
1820                 if (des_ekey(tmp, key) == 0) {
1821                         if (crypto_ablkcipher_get_flags(cipher) &
1822                             CRYPTO_TFM_REQ_WEAK_KEY) {
1823                                 u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
1824
1825                                 crypto_ablkcipher_set_flags(cipher, flags);
1826                                 return -EINVAL;
1827                         }
1828                 }
1829
1830                 ctx->cipher_type = CIPHER_TYPE_DES;
1831         } else {
1832                 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1833                 return -EINVAL;
1834         }
1835         return 0;
1836 }
1837
1838 static int threedes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1839                            unsigned int keylen)
1840 {
1841         struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1842
1843         if (keylen == (DES_KEY_SIZE * 3)) {
1844                 const u32 *K = (const u32 *)key;
1845                 u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
1846
1847                 if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
1848                     !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
1849                         crypto_ablkcipher_set_flags(cipher, flags);
1850                         return -EINVAL;
1851                 }
1852
1853                 ctx->cipher_type = CIPHER_TYPE_3DES;
1854         } else {
1855                 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1856                 return -EINVAL;
1857         }
1858         return 0;
1859 }
1860
1861 static int aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1862                       unsigned int keylen)
1863 {
1864         struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1865
1866         if (ctx->cipher.mode == CIPHER_MODE_XTS)
1867                 /* XTS includes two keys of equal length */
1868                 keylen = keylen / 2;
1869
1870         switch (keylen) {
1871         case AES_KEYSIZE_128:
1872                 ctx->cipher_type = CIPHER_TYPE_AES128;
1873                 break;
1874         case AES_KEYSIZE_192:
1875                 ctx->cipher_type = CIPHER_TYPE_AES192;
1876                 break;
1877         case AES_KEYSIZE_256:
1878                 ctx->cipher_type = CIPHER_TYPE_AES256;
1879                 break;
1880         default:
1881                 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1882                 return -EINVAL;
1883         }
1884         WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
1885                 ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
1886         return 0;
1887 }
1888
1889 static int rc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1890                       unsigned int keylen)
1891 {
1892         struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1893         int i;
1894
1895         ctx->enckeylen = ARC4_MAX_KEY_SIZE + ARC4_STATE_SIZE;
1896
1897         ctx->enckey[0] = 0x00;  /* 0x00 */
1898         ctx->enckey[1] = 0x00;  /* i    */
1899         ctx->enckey[2] = 0x00;  /* 0x00 */
1900         ctx->enckey[3] = 0x00;  /* j    */
1901         for (i = 0; i < ARC4_MAX_KEY_SIZE; i++)
1902                 ctx->enckey[i + ARC4_STATE_SIZE] = key[i % keylen];
1903
1904         ctx->cipher_type = CIPHER_TYPE_INIT;
1905
1906         return 0;
1907 }
1908
1909 static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1910                              unsigned int keylen)
1911 {
1912         struct spu_hw *spu = &iproc_priv.spu;
1913         struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1914         struct spu_cipher_parms cipher_parms;
1915         u32 alloc_len = 0;
1916         int err;
1917
1918         flow_log("ablkcipher_setkey() keylen: %d\n", keylen);
1919         flow_dump("  key: ", key, keylen);
1920
1921         switch (ctx->cipher.alg) {
1922         case CIPHER_ALG_DES:
1923                 err = des_setkey(cipher, key, keylen);
1924                 break;
1925         case CIPHER_ALG_3DES:
1926                 err = threedes_setkey(cipher, key, keylen);
1927                 break;
1928         case CIPHER_ALG_AES:
1929                 err = aes_setkey(cipher, key, keylen);
1930                 break;
1931         case CIPHER_ALG_RC4:
1932                 err = rc4_setkey(cipher, key, keylen);
1933                 break;
1934         default:
1935                 pr_err("%s() Error: unknown cipher alg\n", __func__);
1936                 err = -EINVAL;
1937         }
1938         if (err)
1939                 return err;
1940
1941         /* RC4 already populated ctx->enkey */
1942         if (ctx->cipher.alg != CIPHER_ALG_RC4) {
1943                 memcpy(ctx->enckey, key, keylen);
1944                 ctx->enckeylen = keylen;
1945         }
1946         /* SPU needs XTS keys in the reverse order the crypto API presents */
1947         if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
1948             (ctx->cipher.mode == CIPHER_MODE_XTS)) {
1949                 unsigned int xts_keylen = keylen / 2;
1950
1951                 memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
1952                 memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
1953         }
1954
1955         if (spu->spu_type == SPU_TYPE_SPUM)
1956                 alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
1957         else if (spu->spu_type == SPU_TYPE_SPU2)
1958                 alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
1959         memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
1960         cipher_parms.iv_buf = NULL;
1961         cipher_parms.iv_len = crypto_ablkcipher_ivsize(cipher);
1962         flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
1963
1964         cipher_parms.alg = ctx->cipher.alg;
1965         cipher_parms.mode = ctx->cipher.mode;
1966         cipher_parms.type = ctx->cipher_type;
1967         cipher_parms.key_buf = ctx->enckey;
1968         cipher_parms.key_len = ctx->enckeylen;
1969
1970         /* Prepend SPU request message with BCM header */
1971         memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1972         ctx->spu_req_hdr_len =
1973             spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
1974                                      &cipher_parms);
1975
1976         ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
1977                                                           ctx->enckeylen,
1978                                                           false);
1979
1980         atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
1981
1982         return 0;
1983 }
1984
1985 static int ablkcipher_encrypt(struct ablkcipher_request *req)
1986 {
1987         flow_log("ablkcipher_encrypt() nbytes:%u\n", req->nbytes);
1988
1989         return ablkcipher_enqueue(req, true);
1990 }
1991
1992 static int ablkcipher_decrypt(struct ablkcipher_request *req)
1993 {
1994         flow_log("ablkcipher_decrypt() nbytes:%u\n", req->nbytes);
1995         return ablkcipher_enqueue(req, false);
1996 }
1997
1998 static int ahash_enqueue(struct ahash_request *req)
1999 {
2000         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2001         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2002         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2003         int err = 0;
2004         const char *alg_name;
2005
2006         flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
2007
2008         rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2009                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2010         rctx->parent = &req->base;
2011         rctx->ctx = ctx;
2012         rctx->bd_suppress = true;
2013         memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2014
2015         /* Initialize position in src scatterlist */
2016         rctx->src_sg = req->src;
2017         rctx->src_skip = 0;
2018         rctx->src_nents = 0;
2019         rctx->dst_sg = NULL;
2020         rctx->dst_skip = 0;
2021         rctx->dst_nents = 0;
2022
2023         /* SPU2 hardware does not compute hash of zero length data */
2024         if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
2025             (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
2026                 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2027                 flow_log("Doing %sfinal %s zero-len hash request in software\n",
2028                          rctx->is_final ? "" : "non-", alg_name);
2029                 err = do_shash((unsigned char *)alg_name, req->result,
2030                                NULL, 0, NULL, 0, ctx->authkey,
2031                                ctx->authkeylen);
2032                 if (err < 0)
2033                         flow_log("Hash request failed with error %d\n", err);
2034                 return err;
2035         }
2036         /* Choose a SPU to process this request */
2037         rctx->chan_idx = select_channel();
2038
2039         err = handle_ahash_req(rctx);
2040         if (err != -EINPROGRESS)
2041                 /* synchronous result */
2042                 spu_chunk_cleanup(rctx);
2043
2044         if (err == -EAGAIN)
2045                 /*
2046                  * we saved data in hash carry, but tell crypto API
2047                  * we successfully completed request.
2048                  */
2049                 err = 0;
2050
2051         return err;
2052 }
2053
2054 static int __ahash_init(struct ahash_request *req)
2055 {
2056         struct spu_hw *spu = &iproc_priv.spu;
2057         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2058         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2059         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2060
2061         flow_log("%s()\n", __func__);
2062
2063         /* Initialize the context */
2064         rctx->hash_carry_len = 0;
2065         rctx->is_final = 0;
2066
2067         rctx->total_todo = 0;
2068         rctx->src_sent = 0;
2069         rctx->total_sent = 0;
2070         rctx->total_received = 0;
2071
2072         ctx->digestsize = crypto_ahash_digestsize(tfm);
2073         /* If we add a hash whose digest is larger, catch it here. */
2074         WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
2075
2076         rctx->is_sw_hmac = false;
2077
2078         ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
2079                                                           true);
2080
2081         return 0;
2082 }
2083
2084 /**
2085  * spu_no_incr_hash() - Determine whether incremental hashing is supported.
2086  * @ctx:  Crypto session context
2087  *
2088  * SPU-2 does not support incremental hashing (we'll have to revisit and
2089  * condition based on chip revision or device tree entry if future versions do
2090  * support incremental hash)
2091  *
2092  * SPU-M also doesn't support incremental hashing of AES-XCBC
2093  *
2094  * Return: true if incremental hashing is not supported
2095  *         false otherwise
2096  */
2097 bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
2098 {
2099         struct spu_hw *spu = &iproc_priv.spu;
2100
2101         if (spu->spu_type == SPU_TYPE_SPU2)
2102                 return true;
2103
2104         if ((ctx->auth.alg == HASH_ALG_AES) &&
2105             (ctx->auth.mode == HASH_MODE_XCBC))
2106                 return true;
2107
2108         /* Otherwise, incremental hashing is supported */
2109         return false;
2110 }
2111
2112 static int ahash_init(struct ahash_request *req)
2113 {
2114         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2115         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2116         const char *alg_name;
2117         struct crypto_shash *hash;
2118         int ret;
2119         gfp_t gfp;
2120
2121         if (spu_no_incr_hash(ctx)) {
2122                 /*
2123                  * If we get an incremental hashing request and it's not
2124                  * supported by the hardware, we need to handle it in software
2125                  * by calling synchronous hash functions.
2126                  */
2127                 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2128                 hash = crypto_alloc_shash(alg_name, 0, 0);
2129                 if (IS_ERR(hash)) {
2130                         ret = PTR_ERR(hash);
2131                         goto err;
2132                 }
2133
2134                 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2135                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2136                 ctx->shash = kmalloc(sizeof(*ctx->shash) +
2137                                      crypto_shash_descsize(hash), gfp);
2138                 if (!ctx->shash) {
2139                         ret = -ENOMEM;
2140                         goto err_hash;
2141                 }
2142                 ctx->shash->tfm = hash;
2143                 ctx->shash->flags = 0;
2144
2145                 /* Set the key using data we already have from setkey */
2146                 if (ctx->authkeylen > 0) {
2147                         ret = crypto_shash_setkey(hash, ctx->authkey,
2148                                                   ctx->authkeylen);
2149                         if (ret)
2150                                 goto err_shash;
2151                 }
2152
2153                 /* Initialize hash w/ this key and other params */
2154                 ret = crypto_shash_init(ctx->shash);
2155                 if (ret)
2156                         goto err_shash;
2157         } else {
2158                 /* Otherwise call the internal function which uses SPU hw */
2159                 ret = __ahash_init(req);
2160         }
2161
2162         return ret;
2163
2164 err_shash:
2165         kfree(ctx->shash);
2166 err_hash:
2167         crypto_free_shash(hash);
2168 err:
2169         return ret;
2170 }
2171
2172 static int __ahash_update(struct ahash_request *req)
2173 {
2174         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2175
2176         flow_log("ahash_update() nbytes:%u\n", req->nbytes);
2177
2178         if (!req->nbytes)
2179                 return 0;
2180         rctx->total_todo += req->nbytes;
2181         rctx->src_sent = 0;
2182
2183         return ahash_enqueue(req);
2184 }
2185
2186 static int ahash_update(struct ahash_request *req)
2187 {
2188         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2189         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2190         u8 *tmpbuf;
2191         int ret;
2192         int nents;
2193         gfp_t gfp;
2194
2195         if (spu_no_incr_hash(ctx)) {
2196                 /*
2197                  * If we get an incremental hashing request and it's not
2198                  * supported by the hardware, we need to handle it in software
2199                  * by calling synchronous hash functions.
2200                  */
2201                 if (req->src)
2202                         nents = sg_nents(req->src);
2203                 else
2204                         return -EINVAL;
2205
2206                 /* Copy data from req scatterlist to tmp buffer */
2207                 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2208                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2209                 tmpbuf = kmalloc(req->nbytes, gfp);
2210                 if (!tmpbuf)
2211                         return -ENOMEM;
2212
2213                 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2214                                 req->nbytes) {
2215                         kfree(tmpbuf);
2216                         return -EINVAL;
2217                 }
2218
2219                 /* Call synchronous update */
2220                 ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
2221                 kfree(tmpbuf);
2222         } else {
2223                 /* Otherwise call the internal function which uses SPU hw */
2224                 ret = __ahash_update(req);
2225         }
2226
2227         return ret;
2228 }
2229
2230 static int __ahash_final(struct ahash_request *req)
2231 {
2232         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2233
2234         flow_log("ahash_final() nbytes:%u\n", req->nbytes);
2235
2236         rctx->is_final = 1;
2237
2238         return ahash_enqueue(req);
2239 }
2240
2241 static int ahash_final(struct ahash_request *req)
2242 {
2243         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2244         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2245         int ret;
2246
2247         if (spu_no_incr_hash(ctx)) {
2248                 /*
2249                  * If we get an incremental hashing request and it's not
2250                  * supported by the hardware, we need to handle it in software
2251                  * by calling synchronous hash functions.
2252                  */
2253                 ret = crypto_shash_final(ctx->shash, req->result);
2254
2255                 /* Done with hash, can deallocate it now */
2256                 crypto_free_shash(ctx->shash->tfm);
2257                 kfree(ctx->shash);
2258
2259         } else {
2260                 /* Otherwise call the internal function which uses SPU hw */
2261                 ret = __ahash_final(req);
2262         }
2263
2264         return ret;
2265 }
2266
2267 static int __ahash_finup(struct ahash_request *req)
2268 {
2269         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2270
2271         flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
2272
2273         rctx->total_todo += req->nbytes;
2274         rctx->src_sent = 0;
2275         rctx->is_final = 1;
2276
2277         return ahash_enqueue(req);
2278 }
2279
2280 static int ahash_finup(struct ahash_request *req)
2281 {
2282         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2283         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2284         u8 *tmpbuf;
2285         int ret;
2286         int nents;
2287         gfp_t gfp;
2288
2289         if (spu_no_incr_hash(ctx)) {
2290                 /*
2291                  * If we get an incremental hashing request and it's not
2292                  * supported by the hardware, we need to handle it in software
2293                  * by calling synchronous hash functions.
2294                  */
2295                 if (req->src) {
2296                         nents = sg_nents(req->src);
2297                 } else {
2298                         ret = -EINVAL;
2299                         goto ahash_finup_exit;
2300                 }
2301
2302                 /* Copy data from req scatterlist to tmp buffer */
2303                 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2304                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2305                 tmpbuf = kmalloc(req->nbytes, gfp);
2306                 if (!tmpbuf) {
2307                         ret = -ENOMEM;
2308                         goto ahash_finup_exit;
2309                 }
2310
2311                 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2312                                 req->nbytes) {
2313                         ret = -EINVAL;
2314                         goto ahash_finup_free;
2315                 }
2316
2317                 /* Call synchronous update */
2318                 ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
2319                                          req->result);
2320         } else {
2321                 /* Otherwise call the internal function which uses SPU hw */
2322                 return __ahash_finup(req);
2323         }
2324 ahash_finup_free:
2325         kfree(tmpbuf);
2326
2327 ahash_finup_exit:
2328         /* Done with hash, can deallocate it now */
2329         crypto_free_shash(ctx->shash->tfm);
2330         kfree(ctx->shash);
2331         return ret;
2332 }
2333
2334 static int ahash_digest(struct ahash_request *req)
2335 {
2336         int err = 0;
2337
2338         flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
2339
2340         /* whole thing at once */
2341         err = __ahash_init(req);
2342         if (!err)
2343                 err = __ahash_finup(req);
2344
2345         return err;
2346 }
2347
2348 static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
2349                         unsigned int keylen)
2350 {
2351         struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2352
2353         flow_log("%s() ahash:%p key:%p keylen:%u\n",
2354                  __func__, ahash, key, keylen);
2355         flow_dump("  key: ", key, keylen);
2356
2357         if (ctx->auth.alg == HASH_ALG_AES) {
2358                 switch (keylen) {
2359                 case AES_KEYSIZE_128:
2360                         ctx->cipher_type = CIPHER_TYPE_AES128;
2361                         break;
2362                 case AES_KEYSIZE_192:
2363                         ctx->cipher_type = CIPHER_TYPE_AES192;
2364                         break;
2365                 case AES_KEYSIZE_256:
2366                         ctx->cipher_type = CIPHER_TYPE_AES256;
2367                         break;
2368                 default:
2369                         pr_err("%s() Error: Invalid key length\n", __func__);
2370                         return -EINVAL;
2371                 }
2372         } else {
2373                 pr_err("%s() Error: unknown hash alg\n", __func__);
2374                 return -EINVAL;
2375         }
2376         memcpy(ctx->authkey, key, keylen);
2377         ctx->authkeylen = keylen;
2378
2379         return 0;
2380 }
2381
2382 static int ahash_export(struct ahash_request *req, void *out)
2383 {
2384         const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2385         struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
2386
2387         spu_exp->total_todo = rctx->total_todo;
2388         spu_exp->total_sent = rctx->total_sent;
2389         spu_exp->is_sw_hmac = rctx->is_sw_hmac;
2390         memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
2391         spu_exp->hash_carry_len = rctx->hash_carry_len;
2392         memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
2393
2394         return 0;
2395 }
2396
2397 static int ahash_import(struct ahash_request *req, const void *in)
2398 {
2399         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2400         struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
2401
2402         rctx->total_todo = spu_exp->total_todo;
2403         rctx->total_sent = spu_exp->total_sent;
2404         rctx->is_sw_hmac = spu_exp->is_sw_hmac;
2405         memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
2406         rctx->hash_carry_len = spu_exp->hash_carry_len;
2407         memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
2408
2409         return 0;
2410 }
2411
2412 static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
2413                              unsigned int keylen)
2414 {
2415         struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2416         unsigned int blocksize =
2417                 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
2418         unsigned int digestsize = crypto_ahash_digestsize(ahash);
2419         unsigned int index;
2420         int rc;
2421
2422         flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
2423                  __func__, ahash, key, keylen, blocksize, digestsize);
2424         flow_dump("  key: ", key, keylen);
2425
2426         if (keylen > blocksize) {
2427                 switch (ctx->auth.alg) {
2428                 case HASH_ALG_MD5:
2429                         rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
2430                                       0, NULL, 0);
2431                         break;
2432                 case HASH_ALG_SHA1:
2433                         rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
2434                                       0, NULL, 0);
2435                         break;
2436                 case HASH_ALG_SHA224:
2437                         rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
2438                                       0, NULL, 0);
2439                         break;
2440                 case HASH_ALG_SHA256:
2441                         rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
2442                                       0, NULL, 0);
2443                         break;
2444                 case HASH_ALG_SHA384:
2445                         rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
2446                                       0, NULL, 0);
2447                         break;
2448                 case HASH_ALG_SHA512:
2449                         rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
2450                                       0, NULL, 0);
2451                         break;
2452                 case HASH_ALG_SHA3_224:
2453                         rc = do_shash("sha3-224", ctx->authkey, key, keylen,
2454                                       NULL, 0, NULL, 0);
2455                         break;
2456                 case HASH_ALG_SHA3_256:
2457                         rc = do_shash("sha3-256", ctx->authkey, key, keylen,
2458                                       NULL, 0, NULL, 0);
2459                         break;
2460                 case HASH_ALG_SHA3_384:
2461                         rc = do_shash("sha3-384", ctx->authkey, key, keylen,
2462                                       NULL, 0, NULL, 0);
2463                         break;
2464                 case HASH_ALG_SHA3_512:
2465                         rc = do_shash("sha3-512", ctx->authkey, key, keylen,
2466                                       NULL, 0, NULL, 0);
2467                         break;
2468                 default:
2469                         pr_err("%s() Error: unknown hash alg\n", __func__);
2470                         return -EINVAL;
2471                 }
2472                 if (rc < 0) {
2473                         pr_err("%s() Error %d computing shash for %s\n",
2474                                __func__, rc, hash_alg_name[ctx->auth.alg]);
2475                         return rc;
2476                 }
2477                 ctx->authkeylen = digestsize;
2478
2479                 flow_log("  keylen > digestsize... hashed\n");
2480                 flow_dump("  newkey: ", ctx->authkey, ctx->authkeylen);
2481         } else {
2482                 memcpy(ctx->authkey, key, keylen);
2483                 ctx->authkeylen = keylen;
2484         }
2485
2486         /*
2487          * Full HMAC operation in SPUM is not verified,
2488          * So keeping the generation of IPAD, OPAD and
2489          * outer hashing in software.
2490          */
2491         if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
2492                 memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
2493                 memset(ctx->ipad + ctx->authkeylen, 0,
2494                        blocksize - ctx->authkeylen);
2495                 ctx->authkeylen = 0;
2496                 memcpy(ctx->opad, ctx->ipad, blocksize);
2497
2498                 for (index = 0; index < blocksize; index++) {
2499                         ctx->ipad[index] ^= HMAC_IPAD_VALUE;
2500                         ctx->opad[index] ^= HMAC_OPAD_VALUE;
2501                 }
2502
2503                 flow_dump("  ipad: ", ctx->ipad, blocksize);
2504                 flow_dump("  opad: ", ctx->opad, blocksize);
2505         }
2506         ctx->digestsize = digestsize;
2507         atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
2508
2509         return 0;
2510 }
2511
2512 static int ahash_hmac_init(struct ahash_request *req)
2513 {
2514         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2515         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2516         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2517         unsigned int blocksize =
2518                         crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2519
2520         flow_log("ahash_hmac_init()\n");
2521
2522         /* init the context as a hash */
2523         ahash_init(req);
2524
2525         if (!spu_no_incr_hash(ctx)) {
2526                 /* SPU-M can do incr hashing but needs sw for outer HMAC */
2527                 rctx->is_sw_hmac = true;
2528                 ctx->auth.mode = HASH_MODE_HASH;
2529                 /* start with a prepended ipad */
2530                 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2531                 rctx->hash_carry_len = blocksize;
2532                 rctx->total_todo += blocksize;
2533         }
2534
2535         return 0;
2536 }
2537
2538 static int ahash_hmac_update(struct ahash_request *req)
2539 {
2540         flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
2541
2542         if (!req->nbytes)
2543                 return 0;
2544
2545         return ahash_update(req);
2546 }
2547
2548 static int ahash_hmac_final(struct ahash_request *req)
2549 {
2550         flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
2551
2552         return ahash_final(req);
2553 }
2554
2555 static int ahash_hmac_finup(struct ahash_request *req)
2556 {
2557         flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
2558
2559         return ahash_finup(req);
2560 }
2561
2562 static int ahash_hmac_digest(struct ahash_request *req)
2563 {
2564         struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2565         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2566         struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2567         unsigned int blocksize =
2568                         crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2569
2570         flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
2571
2572         /* Perform initialization and then call finup */
2573         __ahash_init(req);
2574
2575         if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
2576                 /*
2577                  * SPU2 supports full HMAC implementation in the
2578                  * hardware, need not to generate IPAD, OPAD and
2579                  * outer hash in software.
2580                  * Only for hash key len > hash block size, SPU2
2581                  * expects to perform hashing on the key, shorten
2582                  * it to digest size and feed it as hash key.
2583                  */
2584                 rctx->is_sw_hmac = false;
2585                 ctx->auth.mode = HASH_MODE_HMAC;
2586         } else {
2587                 rctx->is_sw_hmac = true;
2588                 ctx->auth.mode = HASH_MODE_HASH;
2589                 /* start with a prepended ipad */
2590                 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2591                 rctx->hash_carry_len = blocksize;
2592                 rctx->total_todo += blocksize;
2593         }
2594
2595         return __ahash_finup(req);
2596 }
2597
2598 /* aead helpers */
2599
2600 static int aead_need_fallback(struct aead_request *req)
2601 {
2602         struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2603         struct spu_hw *spu = &iproc_priv.spu;
2604         struct crypto_aead *aead = crypto_aead_reqtfm(req);
2605         struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2606         u32 payload_len;
2607
2608         /*
2609          * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
2610          * and AAD are both 0 bytes long. So use fallback in this case.
2611          */
2612         if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
2613              (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
2614             (req->assoclen == 0)) {
2615                 if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
2616                     (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
2617                         flow_log("AES GCM/CCM needs fallback for 0 len req\n");
2618                         return 1;
2619                 }
2620         }
2621
2622         /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
2623         if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2624             (spu->spu_type == SPU_TYPE_SPUM) &&
2625             (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
2626             (ctx->digestsize != 16)) {
2627                 flow_log("%s() AES CCM needs fallback for digest size %d\n",
2628                          __func__, ctx->digestsize);
2629                 return 1;
2630         }
2631
2632         /*
2633          * SPU-M on NSP has an issue where AES-CCM hash is not correct
2634          * when AAD size is 0
2635          */
2636         if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2637             (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
2638             (req->assoclen == 0)) {
2639                 flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
2640                          __func__);
2641                 return 1;
2642         }
2643
2644         payload_len = req->cryptlen;
2645         if (spu->spu_type == SPU_TYPE_SPUM)
2646                 payload_len += req->assoclen;
2647
2648         flow_log("%s() payload len: %u\n", __func__, payload_len);
2649
2650         if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2651                 return 0;
2652         else
2653                 return payload_len > ctx->max_payload;
2654 }
2655
2656 static void aead_complete(struct crypto_async_request *areq, int err)
2657 {
2658         struct aead_request *req =
2659             container_of(areq, struct aead_request, base);
2660         struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2661         struct crypto_aead *aead = crypto_aead_reqtfm(req);
2662
2663         flow_log("%s() err:%d\n", __func__, err);
2664
2665         areq->tfm = crypto_aead_tfm(aead);
2666
2667         areq->complete = rctx->old_complete;
2668         areq->data = rctx->old_data;
2669
2670         areq->complete(areq, err);
2671 }
2672
2673 static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
2674 {
2675         struct crypto_aead *aead = crypto_aead_reqtfm(req);
2676         struct crypto_tfm *tfm = crypto_aead_tfm(aead);
2677         struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2678         struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
2679         int err;
2680         u32 req_flags;
2681
2682         flow_log("%s() enc:%u\n", __func__, is_encrypt);
2683
2684         if (ctx->fallback_cipher) {
2685                 /* Store the cipher tfm and then use the fallback tfm */
2686                 rctx->old_tfm = tfm;
2687                 aead_request_set_tfm(req, ctx->fallback_cipher);
2688                 /*
2689                  * Save the callback and chain ourselves in, so we can restore
2690                  * the tfm
2691                  */
2692                 rctx->old_complete = req->base.complete;
2693                 rctx->old_data = req->base.data;
2694                 req_flags = aead_request_flags(req);
2695                 aead_request_set_callback(req, req_flags, aead_complete, req);
2696                 err = is_encrypt ? crypto_aead_encrypt(req) :
2697                     crypto_aead_decrypt(req);
2698
2699                 if (err == 0) {
2700                         /*
2701                          * fallback was synchronous (did not return
2702                          * -EINPROGRESS). So restore request state here.
2703                          */
2704                         aead_request_set_callback(req, req_flags,
2705                                                   rctx->old_complete, req);
2706                         req->base.data = rctx->old_data;
2707                         aead_request_set_tfm(req, aead);
2708                         flow_log("%s() fallback completed successfully\n\n",
2709                                  __func__);
2710                 }
2711         } else {
2712                 err = -EINVAL;
2713         }
2714
2715         return err;
2716 }
2717
2718 static int aead_enqueue(struct aead_request *req, bool is_encrypt)
2719 {
2720         struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2721         struct crypto_aead *aead = crypto_aead_reqtfm(req);
2722         struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2723         int err;
2724
2725         flow_log("%s() enc:%u\n", __func__, is_encrypt);
2726
2727         if (req->assoclen > MAX_ASSOC_SIZE) {
2728                 pr_err
2729                     ("%s() Error: associated data too long. (%u > %u bytes)\n",
2730                      __func__, req->assoclen, MAX_ASSOC_SIZE);
2731                 return -EINVAL;
2732         }
2733
2734         rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2735                        CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2736         rctx->parent = &req->base;
2737         rctx->is_encrypt = is_encrypt;
2738         rctx->bd_suppress = false;
2739         rctx->total_todo = req->cryptlen;
2740         rctx->src_sent = 0;
2741         rctx->total_sent = 0;
2742         rctx->total_received = 0;
2743         rctx->is_sw_hmac = false;
2744         rctx->ctx = ctx;
2745         memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2746
2747         /* assoc data is at start of src sg */
2748         rctx->assoc = req->src;
2749
2750         /*
2751          * Init current position in src scatterlist to be after assoc data.
2752          * src_skip set to buffer offset where data begins. (Assoc data could
2753          * end in the middle of a buffer.)
2754          */
2755         if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
2756                              &rctx->src_skip) < 0) {
2757                 pr_err("%s() Error: Unable to find start of src data\n",
2758                        __func__);
2759                 return -EINVAL;
2760         }
2761
2762         rctx->src_nents = 0;
2763         rctx->dst_nents = 0;
2764         if (req->dst == req->src) {
2765                 rctx->dst_sg = rctx->src_sg;
2766                 rctx->dst_skip = rctx->src_skip;
2767         } else {
2768                 /*
2769                  * Expect req->dst to have room for assoc data followed by
2770                  * output data and ICV, if encrypt. So initialize dst_sg
2771                  * to point beyond assoc len offset.
2772                  */
2773                 if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
2774                                      &rctx->dst_skip) < 0) {
2775                         pr_err("%s() Error: Unable to find start of dst data\n",
2776                                __func__);
2777                         return -EINVAL;
2778                 }
2779         }
2780
2781         if (ctx->cipher.mode == CIPHER_MODE_CBC ||
2782             ctx->cipher.mode == CIPHER_MODE_CTR ||
2783             ctx->cipher.mode == CIPHER_MODE_OFB ||
2784             ctx->cipher.mode == CIPHER_MODE_XTS ||
2785             ctx->cipher.mode == CIPHER_MODE_GCM) {
2786                 rctx->iv_ctr_len =
2787                         ctx->salt_len +
2788                         crypto_aead_ivsize(crypto_aead_reqtfm(req));
2789         } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
2790                 rctx->iv_ctr_len = CCM_AES_IV_SIZE;
2791         } else {
2792                 rctx->iv_ctr_len = 0;
2793         }
2794
2795         rctx->hash_carry_len = 0;
2796
2797         flow_log("  src sg: %p\n", req->src);
2798         flow_log("  rctx->src_sg: %p, src_skip %u\n",
2799                  rctx->src_sg, rctx->src_skip);
2800         flow_log("  assoc:  %p, assoclen %u\n", rctx->assoc, req->assoclen);
2801         flow_log("  dst sg: %p\n", req->dst);
2802         flow_log("  rctx->dst_sg: %p, dst_skip %u\n",
2803                  rctx->dst_sg, rctx->dst_skip);
2804         flow_log("  iv_ctr_len:%u\n", rctx->iv_ctr_len);
2805         flow_dump("  iv: ", req->iv, rctx->iv_ctr_len);
2806         flow_log("  authkeylen:%u\n", ctx->authkeylen);
2807         flow_log("  is_esp: %s\n", ctx->is_esp ? "yes" : "no");
2808
2809         if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2810                 flow_log("  max_payload infinite");
2811         else
2812                 flow_log("  max_payload: %u\n", ctx->max_payload);
2813
2814         if (unlikely(aead_need_fallback(req)))
2815                 return aead_do_fallback(req, is_encrypt);
2816
2817         /*
2818          * Do memory allocations for request after fallback check, because if we
2819          * do fallback, we won't call finish_req() to dealloc.
2820          */
2821         if (rctx->iv_ctr_len) {
2822                 if (ctx->salt_len)
2823                         memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
2824                                ctx->salt, ctx->salt_len);
2825                 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
2826                        req->iv,
2827                        rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
2828         }
2829
2830         rctx->chan_idx = select_channel();
2831         err = handle_aead_req(rctx);
2832         if (err != -EINPROGRESS)
2833                 /* synchronous result */
2834                 spu_chunk_cleanup(rctx);
2835
2836         return err;
2837 }
2838
2839 static int aead_authenc_setkey(struct crypto_aead *cipher,
2840                                const u8 *key, unsigned int keylen)
2841 {
2842         struct spu_hw *spu = &iproc_priv.spu;
2843         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2844         struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2845         struct crypto_authenc_keys keys;
2846         int ret;
2847
2848         flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
2849                  keylen);
2850         flow_dump("  key: ", key, keylen);
2851
2852         ret = crypto_authenc_extractkeys(&keys, key, keylen);
2853         if (ret)
2854                 goto badkey;
2855
2856         if (keys.enckeylen > MAX_KEY_SIZE ||
2857             keys.authkeylen > MAX_KEY_SIZE)
2858                 goto badkey;
2859
2860         ctx->enckeylen = keys.enckeylen;
2861         ctx->authkeylen = keys.authkeylen;
2862
2863         memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
2864         /* May end up padding auth key. So make sure it's zeroed. */
2865         memset(ctx->authkey, 0, sizeof(ctx->authkey));
2866         memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
2867
2868         switch (ctx->alg->cipher_info.alg) {
2869         case CIPHER_ALG_DES:
2870                 if (ctx->enckeylen == DES_KEY_SIZE) {
2871                         u32 tmp[DES_EXPKEY_WORDS];
2872                         u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
2873
2874                         if (des_ekey(tmp, keys.enckey) == 0) {
2875                                 if (crypto_aead_get_flags(cipher) &
2876                                     CRYPTO_TFM_REQ_WEAK_KEY) {
2877                                         crypto_aead_set_flags(cipher, flags);
2878                                         return -EINVAL;
2879                                 }
2880                         }
2881
2882                         ctx->cipher_type = CIPHER_TYPE_DES;
2883                 } else {
2884                         goto badkey;
2885                 }
2886                 break;
2887         case CIPHER_ALG_3DES:
2888                 if (ctx->enckeylen == (DES_KEY_SIZE * 3)) {
2889                         const u32 *K = (const u32 *)keys.enckey;
2890                         u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
2891
2892                         if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
2893                             !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
2894                                 crypto_aead_set_flags(cipher, flags);
2895                                 return -EINVAL;
2896                         }
2897
2898                         ctx->cipher_type = CIPHER_TYPE_3DES;
2899                 } else {
2900                         crypto_aead_set_flags(cipher,
2901                                               CRYPTO_TFM_RES_BAD_KEY_LEN);
2902                         return -EINVAL;
2903                 }
2904                 break;
2905         case CIPHER_ALG_AES:
2906                 switch (ctx->enckeylen) {
2907                 case AES_KEYSIZE_128:
2908                         ctx->cipher_type = CIPHER_TYPE_AES128;
2909                         break;
2910                 case AES_KEYSIZE_192:
2911                         ctx->cipher_type = CIPHER_TYPE_AES192;
2912                         break;
2913                 case AES_KEYSIZE_256:
2914                         ctx->cipher_type = CIPHER_TYPE_AES256;
2915                         break;
2916                 default:
2917                         goto badkey;
2918                 }
2919                 break;
2920         case CIPHER_ALG_RC4:
2921                 ctx->cipher_type = CIPHER_TYPE_INIT;
2922                 break;
2923         default:
2924                 pr_err("%s() Error: Unknown cipher alg\n", __func__);
2925                 return -EINVAL;
2926         }
2927
2928         flow_log("  enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2929                  ctx->authkeylen);
2930         flow_dump("  enc: ", ctx->enckey, ctx->enckeylen);
2931         flow_dump("  auth: ", ctx->authkey, ctx->authkeylen);
2932
2933         /* setkey the fallback just in case we needto use it */
2934         if (ctx->fallback_cipher) {
2935                 flow_log("  running fallback setkey()\n");
2936
2937                 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
2938                 ctx->fallback_cipher->base.crt_flags |=
2939                     tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
2940                 ret = crypto_aead_setkey(ctx->fallback_cipher, key, keylen);
2941                 if (ret) {
2942                         flow_log("  fallback setkey() returned:%d\n", ret);
2943                         tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
2944                         tfm->crt_flags |=
2945                             (ctx->fallback_cipher->base.crt_flags &
2946                              CRYPTO_TFM_RES_MASK);
2947                 }
2948         }
2949
2950         ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
2951                                                           ctx->enckeylen,
2952                                                           false);
2953
2954         atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
2955
2956         return ret;
2957
2958 badkey:
2959         ctx->enckeylen = 0;
2960         ctx->authkeylen = 0;
2961         ctx->digestsize = 0;
2962
2963         crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2964         return -EINVAL;
2965 }
2966
2967 static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
2968                                const u8 *key, unsigned int keylen)
2969 {
2970         struct spu_hw *spu = &iproc_priv.spu;
2971         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2972         struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2973
2974         int ret = 0;
2975
2976         flow_log("%s() keylen:%u\n", __func__, keylen);
2977         flow_dump("  key: ", key, keylen);
2978
2979         if (!ctx->is_esp)
2980                 ctx->digestsize = keylen;
2981
2982         ctx->enckeylen = keylen;
2983         ctx->authkeylen = 0;
2984
2985         switch (ctx->enckeylen) {
2986         case AES_KEYSIZE_128:
2987                 ctx->cipher_type = CIPHER_TYPE_AES128;
2988                 break;
2989         case AES_KEYSIZE_192:
2990                 ctx->cipher_type = CIPHER_TYPE_AES192;
2991                 break;
2992         case AES_KEYSIZE_256:
2993                 ctx->cipher_type = CIPHER_TYPE_AES256;
2994                 break;
2995         default:
2996                 goto badkey;
2997         }
2998
2999         memcpy(ctx->enckey, key, ctx->enckeylen);
3000
3001         flow_log("  enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3002                  ctx->authkeylen);
3003         flow_dump("  enc: ", ctx->enckey, ctx->enckeylen);
3004         flow_dump("  auth: ", ctx->authkey, ctx->authkeylen);
3005
3006         /* setkey the fallback just in case we need to use it */
3007         if (ctx->fallback_cipher) {
3008                 flow_log("  running fallback setkey()\n");
3009
3010                 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
3011                 ctx->fallback_cipher->base.crt_flags |=
3012                     tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
3013                 ret = crypto_aead_setkey(ctx->fallback_cipher, key,
3014                                          keylen + ctx->salt_len);
3015                 if (ret) {
3016                         flow_log("  fallback setkey() returned:%d\n", ret);
3017                         tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
3018                         tfm->crt_flags |=
3019                             (ctx->fallback_cipher->base.crt_flags &
3020                              CRYPTO_TFM_RES_MASK);
3021                 }
3022         }
3023
3024         ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
3025                                                           ctx->enckeylen,
3026                                                           false);
3027
3028         atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
3029
3030         flow_log("  enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3031                  ctx->authkeylen);
3032
3033         return ret;
3034
3035 badkey:
3036         ctx->enckeylen = 0;
3037         ctx->authkeylen = 0;
3038         ctx->digestsize = 0;
3039
3040         crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
3041         return -EINVAL;
3042 }
3043
3044 /**
3045  * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
3046  * @cipher: AEAD structure
3047  * @key:    Key followed by 4 bytes of salt
3048  * @keylen: Length of key plus salt, in bytes
3049  *
3050  * Extracts salt from key and stores it to be prepended to IV on each request.
3051  * Digest is always 16 bytes
3052  *
3053  * Return: Value from generic gcm setkey.
3054  */
3055 static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
3056                                const u8 *key, unsigned int keylen)
3057 {
3058         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3059
3060         flow_log("%s\n", __func__);
3061
3062         if (keylen < GCM_ESP_SALT_SIZE)
3063                 return -EINVAL;
3064
3065         ctx->salt_len = GCM_ESP_SALT_SIZE;
3066         ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3067         memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3068         keylen -= GCM_ESP_SALT_SIZE;
3069         ctx->digestsize = GCM_ESP_DIGESTSIZE;
3070         ctx->is_esp = true;
3071         flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3072
3073         return aead_gcm_ccm_setkey(cipher, key, keylen);
3074 }
3075
3076 /**
3077  * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
3078  * cipher: AEAD structure
3079  * key:    Key followed by 4 bytes of salt
3080  * keylen: Length of key plus salt, in bytes
3081  *
3082  * Extracts salt from key and stores it to be prepended to IV on each request.
3083  * Digest is always 16 bytes
3084  *
3085  * Return: Value from generic gcm setkey.
3086  */
3087 static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
3088                                   const u8 *key, unsigned int keylen)
3089 {
3090         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3091
3092         flow_log("%s\n", __func__);
3093
3094         if (keylen < GCM_ESP_SALT_SIZE)
3095                 return -EINVAL;
3096
3097         ctx->salt_len = GCM_ESP_SALT_SIZE;
3098         ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3099         memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3100         keylen -= GCM_ESP_SALT_SIZE;
3101         ctx->digestsize = GCM_ESP_DIGESTSIZE;
3102         ctx->is_esp = true;
3103         ctx->is_rfc4543 = true;
3104         flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3105
3106         return aead_gcm_ccm_setkey(cipher, key, keylen);
3107 }
3108
3109 /**
3110  * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
3111  * @cipher: AEAD structure
3112  * @key:    Key followed by 4 bytes of salt
3113  * @keylen: Length of key plus salt, in bytes
3114  *
3115  * Extracts salt from key and stores it to be prepended to IV on each request.
3116  * Digest is always 16 bytes
3117  *
3118  * Return: Value from generic ccm setkey.
3119  */
3120 static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
3121                                const u8 *key, unsigned int keylen)
3122 {
3123         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3124
3125         flow_log("%s\n", __func__);
3126
3127         if (keylen < CCM_ESP_SALT_SIZE)
3128                 return -EINVAL;
3129
3130         ctx->salt_len = CCM_ESP_SALT_SIZE;
3131         ctx->salt_offset = CCM_ESP_SALT_OFFSET;
3132         memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
3133         keylen -= CCM_ESP_SALT_SIZE;
3134         ctx->is_esp = true;
3135         flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
3136
3137         return aead_gcm_ccm_setkey(cipher, key, keylen);
3138 }
3139
3140 static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
3141 {
3142         struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3143         int ret = 0;
3144
3145         flow_log("%s() authkeylen:%u authsize:%u\n",
3146                  __func__, ctx->authkeylen, authsize);
3147
3148         ctx->digestsize = authsize;
3149
3150         /* setkey the fallback just in case we needto use it */
3151         if (ctx->fallback_cipher) {
3152                 flow_log("  running fallback setauth()\n");
3153
3154                 ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
3155                 if (ret)
3156                         flow_log("  fallback setauth() returned:%d\n", ret);
3157         }
3158
3159         return ret;
3160 }
3161
3162 static int aead_encrypt(struct aead_request *req)
3163 {
3164         flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
3165                  req->cryptlen);
3166         dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3167         flow_log("  assoc_len:%u\n", req->assoclen);
3168
3169         return aead_enqueue(req, true);
3170 }
3171
3172 static int aead_decrypt(struct aead_request *req)
3173 {
3174         flow_log("%s() cryptlen:%u\n", __func__, req->cryptlen);
3175         dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3176         flow_log("  assoc_len:%u\n", req->assoclen);
3177
3178         return aead_enqueue(req, false);
3179 }
3180
3181 /* ==================== Supported Cipher Algorithms ==================== */
3182
3183 static struct iproc_alg_s driver_algs[] = {
3184         {
3185          .type = CRYPTO_ALG_TYPE_AEAD,
3186          .alg.aead = {
3187                  .base = {
3188                         .cra_name = "gcm(aes)",
3189                         .cra_driver_name = "gcm-aes-iproc",
3190                         .cra_blocksize = AES_BLOCK_SIZE,
3191                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3192                  },
3193                  .setkey = aead_gcm_ccm_setkey,
3194                  .ivsize = GCM_AES_IV_SIZE,
3195                 .maxauthsize = AES_BLOCK_SIZE,
3196          },
3197          .cipher_info = {
3198                          .alg = CIPHER_ALG_AES,
3199                          .mode = CIPHER_MODE_GCM,
3200                          },
3201          .auth_info = {
3202                        .alg = HASH_ALG_AES,
3203                        .mode = HASH_MODE_GCM,
3204                        },
3205          .auth_first = 0,
3206          },
3207         {
3208          .type = CRYPTO_ALG_TYPE_AEAD,
3209          .alg.aead = {
3210                  .base = {
3211                         .cra_name = "ccm(aes)",
3212                         .cra_driver_name = "ccm-aes-iproc",
3213                         .cra_blocksize = AES_BLOCK_SIZE,
3214                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3215                  },
3216                  .setkey = aead_gcm_ccm_setkey,
3217                  .ivsize = CCM_AES_IV_SIZE,
3218                 .maxauthsize = AES_BLOCK_SIZE,
3219          },
3220          .cipher_info = {
3221                          .alg = CIPHER_ALG_AES,
3222                          .mode = CIPHER_MODE_CCM,
3223                          },
3224          .auth_info = {
3225                        .alg = HASH_ALG_AES,
3226                        .mode = HASH_MODE_CCM,
3227                        },
3228          .auth_first = 0,
3229          },
3230         {
3231          .type = CRYPTO_ALG_TYPE_AEAD,
3232          .alg.aead = {
3233                  .base = {
3234                         .cra_name = "rfc4106(gcm(aes))",
3235                         .cra_driver_name = "gcm-aes-esp-iproc",
3236                         .cra_blocksize = AES_BLOCK_SIZE,
3237                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3238                  },
3239                  .setkey = aead_gcm_esp_setkey,
3240                  .ivsize = GCM_ESP_IV_SIZE,
3241                  .maxauthsize = AES_BLOCK_SIZE,
3242          },
3243          .cipher_info = {
3244                          .alg = CIPHER_ALG_AES,
3245                          .mode = CIPHER_MODE_GCM,
3246                          },
3247          .auth_info = {
3248                        .alg = HASH_ALG_AES,
3249                        .mode = HASH_MODE_GCM,
3250                        },
3251          .auth_first = 0,
3252          },
3253         {
3254          .type = CRYPTO_ALG_TYPE_AEAD,
3255          .alg.aead = {
3256                  .base = {
3257                         .cra_name = "rfc4309(ccm(aes))",
3258                         .cra_driver_name = "ccm-aes-esp-iproc",
3259                         .cra_blocksize = AES_BLOCK_SIZE,
3260                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3261                  },
3262                  .setkey = aead_ccm_esp_setkey,
3263                  .ivsize = CCM_AES_IV_SIZE,
3264                  .maxauthsize = AES_BLOCK_SIZE,
3265          },
3266          .cipher_info = {
3267                          .alg = CIPHER_ALG_AES,
3268                          .mode = CIPHER_MODE_CCM,
3269                          },
3270          .auth_info = {
3271                        .alg = HASH_ALG_AES,
3272                        .mode = HASH_MODE_CCM,
3273                        },
3274          .auth_first = 0,
3275          },
3276         {
3277          .type = CRYPTO_ALG_TYPE_AEAD,
3278          .alg.aead = {
3279                  .base = {
3280                         .cra_name = "rfc4543(gcm(aes))",
3281                         .cra_driver_name = "gmac-aes-esp-iproc",
3282                         .cra_blocksize = AES_BLOCK_SIZE,
3283                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3284                  },
3285                  .setkey = rfc4543_gcm_esp_setkey,
3286                  .ivsize = GCM_ESP_IV_SIZE,
3287                  .maxauthsize = AES_BLOCK_SIZE,
3288          },
3289          .cipher_info = {
3290                          .alg = CIPHER_ALG_AES,
3291                          .mode = CIPHER_MODE_GCM,
3292                          },
3293          .auth_info = {
3294                        .alg = HASH_ALG_AES,
3295                        .mode = HASH_MODE_GCM,
3296                        },
3297          .auth_first = 0,
3298          },
3299         {
3300          .type = CRYPTO_ALG_TYPE_AEAD,
3301          .alg.aead = {
3302                  .base = {
3303                         .cra_name = "authenc(hmac(md5),cbc(aes))",
3304                         .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
3305                         .cra_blocksize = AES_BLOCK_SIZE,
3306                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3307                  },
3308                  .setkey = aead_authenc_setkey,
3309                 .ivsize = AES_BLOCK_SIZE,
3310                 .maxauthsize = MD5_DIGEST_SIZE,
3311          },
3312          .cipher_info = {
3313                          .alg = CIPHER_ALG_AES,
3314                          .mode = CIPHER_MODE_CBC,
3315                          },
3316          .auth_info = {
3317                        .alg = HASH_ALG_MD5,
3318                        .mode = HASH_MODE_HMAC,
3319                        },
3320          .auth_first = 0,
3321          },
3322         {
3323          .type = CRYPTO_ALG_TYPE_AEAD,
3324          .alg.aead = {
3325                  .base = {
3326                         .cra_name = "authenc(hmac(sha1),cbc(aes))",
3327                         .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
3328                         .cra_blocksize = AES_BLOCK_SIZE,
3329                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3330                  },
3331                  .setkey = aead_authenc_setkey,
3332                  .ivsize = AES_BLOCK_SIZE,
3333                  .maxauthsize = SHA1_DIGEST_SIZE,
3334          },
3335          .cipher_info = {
3336                          .alg = CIPHER_ALG_AES,
3337                          .mode = CIPHER_MODE_CBC,
3338                          },
3339          .auth_info = {
3340                        .alg = HASH_ALG_SHA1,
3341                        .mode = HASH_MODE_HMAC,
3342                        },
3343          .auth_first = 0,
3344          },
3345         {
3346          .type = CRYPTO_ALG_TYPE_AEAD,
3347          .alg.aead = {
3348                  .base = {
3349                         .cra_name = "authenc(hmac(sha256),cbc(aes))",
3350                         .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
3351                         .cra_blocksize = AES_BLOCK_SIZE,
3352                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3353                  },
3354                  .setkey = aead_authenc_setkey,
3355                  .ivsize = AES_BLOCK_SIZE,
3356                  .maxauthsize = SHA256_DIGEST_SIZE,
3357          },
3358          .cipher_info = {
3359                          .alg = CIPHER_ALG_AES,
3360                          .mode = CIPHER_MODE_CBC,
3361                          },
3362          .auth_info = {
3363                        .alg = HASH_ALG_SHA256,
3364                        .mode = HASH_MODE_HMAC,
3365                        },
3366          .auth_first = 0,
3367          },
3368         {
3369          .type = CRYPTO_ALG_TYPE_AEAD,
3370          .alg.aead = {
3371                  .base = {
3372                         .cra_name = "authenc(hmac(md5),cbc(des))",
3373                         .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
3374                         .cra_blocksize = DES_BLOCK_SIZE,
3375                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3376                  },
3377                  .setkey = aead_authenc_setkey,
3378                  .ivsize = DES_BLOCK_SIZE,
3379                  .maxauthsize = MD5_DIGEST_SIZE,
3380          },
3381          .cipher_info = {
3382                          .alg = CIPHER_ALG_DES,
3383                          .mode = CIPHER_MODE_CBC,
3384                          },
3385          .auth_info = {
3386                        .alg = HASH_ALG_MD5,
3387                        .mode = HASH_MODE_HMAC,
3388                        },
3389          .auth_first = 0,
3390          },
3391         {
3392          .type = CRYPTO_ALG_TYPE_AEAD,
3393          .alg.aead = {
3394                  .base = {
3395                         .cra_name = "authenc(hmac(sha1),cbc(des))",
3396                         .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
3397                         .cra_blocksize = DES_BLOCK_SIZE,
3398                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3399                  },
3400                  .setkey = aead_authenc_setkey,
3401                  .ivsize = DES_BLOCK_SIZE,
3402                  .maxauthsize = SHA1_DIGEST_SIZE,
3403          },
3404          .cipher_info = {
3405                          .alg = CIPHER_ALG_DES,
3406                          .mode = CIPHER_MODE_CBC,
3407                          },
3408          .auth_info = {
3409                        .alg = HASH_ALG_SHA1,
3410                        .mode = HASH_MODE_HMAC,
3411                        },
3412          .auth_first = 0,
3413          },
3414         {
3415          .type = CRYPTO_ALG_TYPE_AEAD,
3416          .alg.aead = {
3417                  .base = {
3418                         .cra_name = "authenc(hmac(sha224),cbc(des))",
3419                         .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
3420                         .cra_blocksize = DES_BLOCK_SIZE,
3421                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3422                  },
3423                  .setkey = aead_authenc_setkey,
3424                  .ivsize = DES_BLOCK_SIZE,
3425                  .maxauthsize = SHA224_DIGEST_SIZE,
3426          },
3427          .cipher_info = {
3428                          .alg = CIPHER_ALG_DES,
3429                          .mode = CIPHER_MODE_CBC,
3430                          },
3431          .auth_info = {
3432                        .alg = HASH_ALG_SHA224,
3433                        .mode = HASH_MODE_HMAC,
3434                        },
3435          .auth_first = 0,
3436          },
3437         {
3438          .type = CRYPTO_ALG_TYPE_AEAD,
3439          .alg.aead = {
3440                  .base = {
3441                         .cra_name = "authenc(hmac(sha256),cbc(des))",
3442                         .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
3443                         .cra_blocksize = DES_BLOCK_SIZE,
3444                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3445                  },
3446                  .setkey = aead_authenc_setkey,
3447                  .ivsize = DES_BLOCK_SIZE,
3448                  .maxauthsize = SHA256_DIGEST_SIZE,
3449          },
3450          .cipher_info = {
3451                          .alg = CIPHER_ALG_DES,
3452                          .mode = CIPHER_MODE_CBC,
3453                          },
3454          .auth_info = {
3455                        .alg = HASH_ALG_SHA256,
3456                        .mode = HASH_MODE_HMAC,
3457                        },
3458          .auth_first = 0,
3459          },
3460         {
3461          .type = CRYPTO_ALG_TYPE_AEAD,
3462          .alg.aead = {
3463                  .base = {
3464                         .cra_name = "authenc(hmac(sha384),cbc(des))",
3465                         .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
3466                         .cra_blocksize = DES_BLOCK_SIZE,
3467                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3468                  },
3469                  .setkey = aead_authenc_setkey,
3470                  .ivsize = DES_BLOCK_SIZE,
3471                  .maxauthsize = SHA384_DIGEST_SIZE,
3472          },
3473          .cipher_info = {
3474                          .alg = CIPHER_ALG_DES,
3475                          .mode = CIPHER_MODE_CBC,
3476                          },
3477          .auth_info = {
3478                        .alg = HASH_ALG_SHA384,
3479                        .mode = HASH_MODE_HMAC,
3480                        },
3481          .auth_first = 0,
3482          },
3483         {
3484          .type = CRYPTO_ALG_TYPE_AEAD,
3485          .alg.aead = {
3486                  .base = {
3487                         .cra_name = "authenc(hmac(sha512),cbc(des))",
3488                         .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
3489                         .cra_blocksize = DES_BLOCK_SIZE,
3490                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3491                  },
3492                  .setkey = aead_authenc_setkey,
3493                  .ivsize = DES_BLOCK_SIZE,
3494                  .maxauthsize = SHA512_DIGEST_SIZE,
3495          },
3496          .cipher_info = {
3497                          .alg = CIPHER_ALG_DES,
3498                          .mode = CIPHER_MODE_CBC,
3499                          },
3500          .auth_info = {
3501                        .alg = HASH_ALG_SHA512,
3502                        .mode = HASH_MODE_HMAC,
3503                        },
3504          .auth_first = 0,
3505          },
3506         {
3507          .type = CRYPTO_ALG_TYPE_AEAD,
3508          .alg.aead = {
3509                  .base = {
3510                         .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
3511                         .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
3512                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3513                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3514                  },
3515                  .setkey = aead_authenc_setkey,
3516                  .ivsize = DES3_EDE_BLOCK_SIZE,
3517                  .maxauthsize = MD5_DIGEST_SIZE,
3518          },
3519          .cipher_info = {
3520                          .alg = CIPHER_ALG_3DES,
3521                          .mode = CIPHER_MODE_CBC,
3522                          },
3523          .auth_info = {
3524                        .alg = HASH_ALG_MD5,
3525                        .mode = HASH_MODE_HMAC,
3526                        },
3527          .auth_first = 0,
3528          },
3529         {
3530          .type = CRYPTO_ALG_TYPE_AEAD,
3531          .alg.aead = {
3532                  .base = {
3533                         .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
3534                         .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
3535                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3536                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3537                  },
3538                  .setkey = aead_authenc_setkey,
3539                  .ivsize = DES3_EDE_BLOCK_SIZE,
3540                  .maxauthsize = SHA1_DIGEST_SIZE,
3541          },
3542          .cipher_info = {
3543                          .alg = CIPHER_ALG_3DES,
3544                          .mode = CIPHER_MODE_CBC,
3545                          },
3546          .auth_info = {
3547                        .alg = HASH_ALG_SHA1,
3548                        .mode = HASH_MODE_HMAC,
3549                        },
3550          .auth_first = 0,
3551          },
3552         {
3553          .type = CRYPTO_ALG_TYPE_AEAD,
3554          .alg.aead = {
3555                  .base = {
3556                         .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
3557                         .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
3558                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3559                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3560                  },
3561                  .setkey = aead_authenc_setkey,
3562                  .ivsize = DES3_EDE_BLOCK_SIZE,
3563                  .maxauthsize = SHA224_DIGEST_SIZE,
3564          },
3565          .cipher_info = {
3566                          .alg = CIPHER_ALG_3DES,
3567                          .mode = CIPHER_MODE_CBC,
3568                          },
3569          .auth_info = {
3570                        .alg = HASH_ALG_SHA224,
3571                        .mode = HASH_MODE_HMAC,
3572                        },
3573          .auth_first = 0,
3574          },
3575         {
3576          .type = CRYPTO_ALG_TYPE_AEAD,
3577          .alg.aead = {
3578                  .base = {
3579                         .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
3580                         .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
3581                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3582                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3583                  },
3584                  .setkey = aead_authenc_setkey,
3585                  .ivsize = DES3_EDE_BLOCK_SIZE,
3586                  .maxauthsize = SHA256_DIGEST_SIZE,
3587          },
3588          .cipher_info = {
3589                          .alg = CIPHER_ALG_3DES,
3590                          .mode = CIPHER_MODE_CBC,
3591                          },
3592          .auth_info = {
3593                        .alg = HASH_ALG_SHA256,
3594                        .mode = HASH_MODE_HMAC,
3595                        },
3596          .auth_first = 0,
3597          },
3598         {
3599          .type = CRYPTO_ALG_TYPE_AEAD,
3600          .alg.aead = {
3601                  .base = {
3602                         .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
3603                         .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
3604                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3605                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3606                  },
3607                  .setkey = aead_authenc_setkey,
3608                  .ivsize = DES3_EDE_BLOCK_SIZE,
3609                  .maxauthsize = SHA384_DIGEST_SIZE,
3610          },
3611          .cipher_info = {
3612                          .alg = CIPHER_ALG_3DES,
3613                          .mode = CIPHER_MODE_CBC,
3614                          },
3615          .auth_info = {
3616                        .alg = HASH_ALG_SHA384,
3617                        .mode = HASH_MODE_HMAC,
3618                        },
3619          .auth_first = 0,
3620          },
3621         {
3622          .type = CRYPTO_ALG_TYPE_AEAD,
3623          .alg.aead = {
3624                  .base = {
3625                         .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
3626                         .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
3627                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3628                         .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3629                  },
3630                  .setkey = aead_authenc_setkey,
3631                  .ivsize = DES3_EDE_BLOCK_SIZE,
3632                  .maxauthsize = SHA512_DIGEST_SIZE,
3633          },
3634          .cipher_info = {
3635                          .alg = CIPHER_ALG_3DES,
3636                          .mode = CIPHER_MODE_CBC,
3637                          },
3638          .auth_info = {
3639                        .alg = HASH_ALG_SHA512,
3640                        .mode = HASH_MODE_HMAC,
3641                        },
3642          .auth_first = 0,
3643          },
3644
3645 /* ABLKCIPHER algorithms. */
3646         {
3647          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3648          .alg.crypto = {
3649                         .cra_name = "ecb(arc4)",
3650                         .cra_driver_name = "ecb-arc4-iproc",
3651                         .cra_blocksize = ARC4_BLOCK_SIZE,
3652                         .cra_ablkcipher = {
3653                                            .min_keysize = ARC4_MIN_KEY_SIZE,
3654                                            .max_keysize = ARC4_MAX_KEY_SIZE,
3655                                            .ivsize = 0,
3656                                         }
3657                         },
3658          .cipher_info = {
3659                          .alg = CIPHER_ALG_RC4,
3660                          .mode = CIPHER_MODE_NONE,
3661                          },
3662          .auth_info = {
3663                        .alg = HASH_ALG_NONE,
3664                        .mode = HASH_MODE_NONE,
3665                        },
3666          },
3667         {
3668          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3669          .alg.crypto = {
3670                         .cra_name = "ofb(des)",
3671                         .cra_driver_name = "ofb-des-iproc",
3672                         .cra_blocksize = DES_BLOCK_SIZE,
3673                         .cra_ablkcipher = {
3674                                            .min_keysize = DES_KEY_SIZE,
3675                                            .max_keysize = DES_KEY_SIZE,
3676                                            .ivsize = DES_BLOCK_SIZE,
3677                                         }
3678                         },
3679          .cipher_info = {
3680                          .alg = CIPHER_ALG_DES,
3681                          .mode = CIPHER_MODE_OFB,
3682                          },
3683          .auth_info = {
3684                        .alg = HASH_ALG_NONE,
3685                        .mode = HASH_MODE_NONE,
3686                        },
3687          },
3688         {
3689          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3690          .alg.crypto = {
3691                         .cra_name = "cbc(des)",
3692                         .cra_driver_name = "cbc-des-iproc",
3693                         .cra_blocksize = DES_BLOCK_SIZE,
3694                         .cra_ablkcipher = {
3695                                            .min_keysize = DES_KEY_SIZE,
3696                                            .max_keysize = DES_KEY_SIZE,
3697                                            .ivsize = DES_BLOCK_SIZE,
3698                                         }
3699                         },
3700          .cipher_info = {
3701                          .alg = CIPHER_ALG_DES,
3702                          .mode = CIPHER_MODE_CBC,
3703                          },
3704          .auth_info = {
3705                        .alg = HASH_ALG_NONE,
3706                        .mode = HASH_MODE_NONE,
3707                        },
3708          },
3709         {
3710          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3711          .alg.crypto = {
3712                         .cra_name = "ecb(des)",
3713                         .cra_driver_name = "ecb-des-iproc",
3714                         .cra_blocksize = DES_BLOCK_SIZE,
3715                         .cra_ablkcipher = {
3716                                            .min_keysize = DES_KEY_SIZE,
3717                                            .max_keysize = DES_KEY_SIZE,
3718                                            .ivsize = 0,
3719                                         }
3720                         },
3721          .cipher_info = {
3722                          .alg = CIPHER_ALG_DES,
3723                          .mode = CIPHER_MODE_ECB,
3724                          },
3725          .auth_info = {
3726                        .alg = HASH_ALG_NONE,
3727                        .mode = HASH_MODE_NONE,
3728                        },
3729          },
3730         {
3731          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3732          .alg.crypto = {
3733                         .cra_name = "ofb(des3_ede)",
3734                         .cra_driver_name = "ofb-des3-iproc",
3735                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3736                         .cra_ablkcipher = {
3737                                            .min_keysize = DES3_EDE_KEY_SIZE,
3738                                            .max_keysize = DES3_EDE_KEY_SIZE,
3739                                            .ivsize = DES3_EDE_BLOCK_SIZE,
3740                                         }
3741                         },
3742          .cipher_info = {
3743                          .alg = CIPHER_ALG_3DES,
3744                          .mode = CIPHER_MODE_OFB,
3745                          },
3746          .auth_info = {
3747                        .alg = HASH_ALG_NONE,
3748                        .mode = HASH_MODE_NONE,
3749                        },
3750          },
3751         {
3752          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3753          .alg.crypto = {
3754                         .cra_name = "cbc(des3_ede)",
3755                         .cra_driver_name = "cbc-des3-iproc",
3756                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3757                         .cra_ablkcipher = {
3758                                            .min_keysize = DES3_EDE_KEY_SIZE,
3759                                            .max_keysize = DES3_EDE_KEY_SIZE,
3760                                            .ivsize = DES3_EDE_BLOCK_SIZE,
3761                                         }
3762                         },
3763          .cipher_info = {
3764                          .alg = CIPHER_ALG_3DES,
3765                          .mode = CIPHER_MODE_CBC,
3766                          },
3767          .auth_info = {
3768                        .alg = HASH_ALG_NONE,
3769                        .mode = HASH_MODE_NONE,
3770                        },
3771          },
3772         {
3773          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3774          .alg.crypto = {
3775                         .cra_name = "ecb(des3_ede)",
3776                         .cra_driver_name = "ecb-des3-iproc",
3777                         .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3778                         .cra_ablkcipher = {
3779                                            .min_keysize = DES3_EDE_KEY_SIZE,
3780                                            .max_keysize = DES3_EDE_KEY_SIZE,
3781                                            .ivsize = 0,
3782                                         }
3783                         },
3784          .cipher_info = {
3785                          .alg = CIPHER_ALG_3DES,
3786                          .mode = CIPHER_MODE_ECB,
3787                          },
3788          .auth_info = {
3789                        .alg = HASH_ALG_NONE,
3790                        .mode = HASH_MODE_NONE,
3791                        },
3792          },
3793         {
3794          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3795          .alg.crypto = {
3796                         .cra_name = "ofb(aes)",
3797                         .cra_driver_name = "ofb-aes-iproc",
3798                         .cra_blocksize = AES_BLOCK_SIZE,
3799                         .cra_ablkcipher = {
3800                                            .min_keysize = AES_MIN_KEY_SIZE,
3801                                            .max_keysize = AES_MAX_KEY_SIZE,
3802                                            .ivsize = AES_BLOCK_SIZE,
3803                                         }
3804                         },
3805          .cipher_info = {
3806                          .alg = CIPHER_ALG_AES,
3807                          .mode = CIPHER_MODE_OFB,
3808                          },
3809          .auth_info = {
3810                        .alg = HASH_ALG_NONE,
3811                        .mode = HASH_MODE_NONE,
3812                        },
3813          },
3814         {
3815          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3816          .alg.crypto = {
3817                         .cra_name = "cbc(aes)",
3818                         .cra_driver_name = "cbc-aes-iproc",
3819                         .cra_blocksize = AES_BLOCK_SIZE,
3820                         .cra_ablkcipher = {
3821                                            .min_keysize = AES_MIN_KEY_SIZE,
3822                                            .max_keysize = AES_MAX_KEY_SIZE,
3823                                            .ivsize = AES_BLOCK_SIZE,
3824                                         }
3825                         },
3826          .cipher_info = {
3827                          .alg = CIPHER_ALG_AES,
3828                          .mode = CIPHER_MODE_CBC,
3829                          },
3830          .auth_info = {
3831                        .alg = HASH_ALG_NONE,
3832                        .mode = HASH_MODE_NONE,
3833                        },
3834          },
3835         {
3836          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3837          .alg.crypto = {
3838                         .cra_name = "ecb(aes)",
3839                         .cra_driver_name = "ecb-aes-iproc",
3840                         .cra_blocksize = AES_BLOCK_SIZE,
3841                         .cra_ablkcipher = {
3842                                            .min_keysize = AES_MIN_KEY_SIZE,
3843                                            .max_keysize = AES_MAX_KEY_SIZE,
3844                                            .ivsize = 0,
3845                                         }
3846                         },
3847          .cipher_info = {
3848                          .alg = CIPHER_ALG_AES,
3849                          .mode = CIPHER_MODE_ECB,
3850                          },
3851          .auth_info = {
3852                        .alg = HASH_ALG_NONE,
3853                        .mode = HASH_MODE_NONE,
3854                        },
3855          },
3856         {
3857          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3858          .alg.crypto = {
3859                         .cra_name = "ctr(aes)",
3860                         .cra_driver_name = "ctr-aes-iproc",
3861                         .cra_blocksize = AES_BLOCK_SIZE,
3862                         .cra_ablkcipher = {
3863                                            /* .geniv = "chainiv", */
3864                                            .min_keysize = AES_MIN_KEY_SIZE,
3865                                            .max_keysize = AES_MAX_KEY_SIZE,
3866                                            .ivsize = AES_BLOCK_SIZE,
3867                                         }
3868                         },
3869          .cipher_info = {
3870                          .alg = CIPHER_ALG_AES,
3871                          .mode = CIPHER_MODE_CTR,
3872                          },
3873          .auth_info = {
3874                        .alg = HASH_ALG_NONE,
3875                        .mode = HASH_MODE_NONE,
3876                        },
3877          },
3878 {
3879          .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3880          .alg.crypto = {
3881                         .cra_name = "xts(aes)",
3882                         .cra_driver_name = "xts-aes-iproc",
3883                         .cra_blocksize = AES_BLOCK_SIZE,
3884                         .cra_ablkcipher = {
3885                                 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3886                                 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3887                                 .ivsize = AES_BLOCK_SIZE,
3888                                 }
3889                         },
3890          .cipher_info = {
3891                          .alg = CIPHER_ALG_AES,
3892                          .mode = CIPHER_MODE_XTS,
3893                          },
3894          .auth_info = {
3895                        .alg = HASH_ALG_NONE,
3896                        .mode = HASH_MODE_NONE,
3897                        },
3898          },
3899
3900 /* AHASH algorithms. */
3901         {
3902          .type = CRYPTO_ALG_TYPE_AHASH,
3903          .alg.hash = {
3904                       .halg.digestsize = MD5_DIGEST_SIZE,
3905                       .halg.base = {
3906                                     .cra_name = "md5",
3907                                     .cra_driver_name = "md5-iproc",
3908                                     .cra_blocksize = MD5_BLOCK_WORDS * 4,
3909                                     .cra_flags = CRYPTO_ALG_TYPE_AHASH |
3910                                              CRYPTO_ALG_ASYNC,
3911                                 }
3912                       },
3913          .cipher_info = {
3914                          .alg = CIPHER_ALG_NONE,
3915                          .mode = CIPHER_MODE_NONE,
3916                          },
3917          .auth_info = {
3918                        .alg = HASH_ALG_MD5,
3919                        .mode = HASH_MODE_HASH,
3920                        },
3921          },
3922         {
3923          .type = CRYPTO_ALG_TYPE_AHASH,
3924          .alg.hash = {
3925                       .halg.digestsize = MD5_DIGEST_SIZE,
3926                       .halg.base = {
3927                                     .cra_name = "hmac(md5)",
3928                                     .cra_driver_name = "hmac-md5-iproc",
3929                                     .cra_blocksize = MD5_BLOCK_WORDS * 4,
3930                                 }
3931                       },
3932          .cipher_info = {
3933                          .alg = CIPHER_ALG_NONE,
3934                          .mode = CIPHER_MODE_NONE,
3935                          },
3936          .auth_info = {
3937                        .alg = HASH_ALG_MD5,
3938                        .mode = HASH_MODE_HMAC,
3939                        },
3940          },
3941         {.type = CRYPTO_ALG_TYPE_AHASH,
3942          .alg.hash = {
3943                       .halg.digestsize = SHA1_DIGEST_SIZE,
3944                       .halg.base = {
3945                                     .cra_name = "sha1",
3946                                     .cra_driver_name = "sha1-iproc",
3947                                     .cra_blocksize = SHA1_BLOCK_SIZE,
3948                                 }
3949                       },
3950          .cipher_info = {
3951                          .alg = CIPHER_ALG_NONE,
3952                          .mode = CIPHER_MODE_NONE,
3953                          },
3954          .auth_info = {
3955                        .alg = HASH_ALG_SHA1,
3956                        .mode = HASH_MODE_HASH,
3957                        },
3958          },
3959         {.type = CRYPTO_ALG_TYPE_AHASH,
3960          .alg.hash = {
3961                       .halg.digestsize = SHA1_DIGEST_SIZE,
3962                       .halg.base = {
3963                                     .cra_name = "hmac(sha1)",
3964                                     .cra_driver_name = "hmac-sha1-iproc",
3965                                     .cra_blocksize = SHA1_BLOCK_SIZE,
3966                                 }
3967                       },
3968          .cipher_info = {
3969                          .alg = CIPHER_ALG_NONE,
3970                          .mode = CIPHER_MODE_NONE,
3971                          },
3972          .auth_info = {
3973                        .alg = HASH_ALG_SHA1,
3974                        .mode = HASH_MODE_HMAC,
3975                        },
3976          },
3977         {.type = CRYPTO_ALG_TYPE_AHASH,
3978          .alg.hash = {
3979                         .halg.digestsize = SHA224_DIGEST_SIZE,
3980                         .halg.base = {
3981                                     .cra_name = "sha224",
3982                                     .cra_driver_name = "sha224-iproc",
3983                                     .cra_blocksize = SHA224_BLOCK_SIZE,
3984                         }
3985                       },
3986          .cipher_info = {
3987                          .alg = CIPHER_ALG_NONE,
3988                          .mode = CIPHER_MODE_NONE,
3989                          },
3990          .auth_info = {
3991                        .alg = HASH_ALG_SHA224,
3992                        .mode = HASH_MODE_HASH,
3993                        },
3994          },
3995         {.type = CRYPTO_ALG_TYPE_AHASH,
3996          .alg.hash = {
3997                       .halg.digestsize = SHA224_DIGEST_SIZE,
3998                       .halg.base = {
3999                                     .cra_name = "hmac(sha224)",
4000                                     .cra_driver_name = "hmac-sha224-iproc",
4001                                     .cra_blocksize = SHA224_BLOCK_SIZE,
4002                                 }
4003                       },
4004          .cipher_info = {
4005                          .alg = CIPHER_ALG_NONE,
4006                          .mode = CIPHER_MODE_NONE,
4007                          },
4008          .auth_info = {
4009                        .alg = HASH_ALG_SHA224,
4010                        .mode = HASH_MODE_HMAC,
4011                        },
4012          },
4013         {.type = CRYPTO_ALG_TYPE_AHASH,
4014          .alg.hash = {
4015                       .halg.digestsize = SHA256_DIGEST_SIZE,
4016                       .halg.base = {
4017                                     .cra_name = "sha256",
4018                                     .cra_driver_name = "sha256-iproc",
4019                                     .cra_blocksize = SHA256_BLOCK_SIZE,
4020                                 }
4021                       },
4022          .cipher_info = {
4023                          .alg = CIPHER_ALG_NONE,
4024                          .mode = CIPHER_MODE_NONE,
4025                          },
4026          .auth_info = {
4027                        .alg = HASH_ALG_SHA256,
4028                        .mode = HASH_MODE_HASH,
4029                        },
4030          },
4031         {.type = CRYPTO_ALG_TYPE_AHASH,
4032          .alg.hash = {
4033                       .halg.digestsize = SHA256_DIGEST_SIZE,
4034                       .halg.base = {
4035                                     .cra_name = "hmac(sha256)",
4036                                     .cra_driver_name = "hmac-sha256-iproc",
4037                                     .cra_blocksize = SHA256_BLOCK_SIZE,
4038                                 }
4039                       },
4040          .cipher_info = {
4041                          .alg = CIPHER_ALG_NONE,
4042                          .mode = CIPHER_MODE_NONE,
4043                          },
4044          .auth_info = {
4045                        .alg = HASH_ALG_SHA256,
4046                        .mode = HASH_MODE_HMAC,
4047                        },
4048          },
4049         {
4050         .type = CRYPTO_ALG_TYPE_AHASH,
4051          .alg.hash = {
4052                       .halg.digestsize = SHA384_DIGEST_SIZE,
4053                       .halg.base = {
4054                                     .cra_name = "sha384",
4055                                     .cra_driver_name = "sha384-iproc",
4056                                     .cra_blocksize = SHA384_BLOCK_SIZE,
4057                                 }
4058                       },
4059          .cipher_info = {
4060                          .alg = CIPHER_ALG_NONE,
4061                          .mode = CIPHER_MODE_NONE,
4062                          },
4063          .auth_info = {
4064                        .alg = HASH_ALG_SHA384,
4065                        .mode = HASH_MODE_HASH,
4066                        },
4067          },
4068         {
4069          .type = CRYPTO_ALG_TYPE_AHASH,
4070          .alg.hash = {
4071                       .halg.digestsize = SHA384_DIGEST_SIZE,
4072                       .halg.base = {
4073                                     .cra_name = "hmac(sha384)",
4074                                     .cra_driver_name = "hmac-sha384-iproc",
4075                                     .cra_blocksize = SHA384_BLOCK_SIZE,
4076                                 }
4077                       },
4078          .cipher_info = {
4079                          .alg = CIPHER_ALG_NONE,
4080                          .mode = CIPHER_MODE_NONE,
4081                          },
4082          .auth_info = {
4083                        .alg = HASH_ALG_SHA384,
4084                        .mode = HASH_MODE_HMAC,
4085                        },
4086          },
4087         {
4088          .type = CRYPTO_ALG_TYPE_AHASH,
4089          .alg.hash = {
4090                       .halg.digestsize = SHA512_DIGEST_SIZE,
4091                       .halg.base = {
4092                                     .cra_name = "sha512",
4093                                     .cra_driver_name = "sha512-iproc",
4094                                     .cra_blocksize = SHA512_BLOCK_SIZE,
4095                                 }
4096                       },
4097          .cipher_info = {
4098                          .alg = CIPHER_ALG_NONE,
4099                          .mode = CIPHER_MODE_NONE,
4100                          },
4101          .auth_info = {
4102                        .alg = HASH_ALG_SHA512,
4103                        .mode = HASH_MODE_HASH,
4104                        },
4105          },
4106         {
4107          .type = CRYPTO_ALG_TYPE_AHASH,
4108          .alg.hash = {
4109                       .halg.digestsize = SHA512_DIGEST_SIZE,
4110                       .halg.base = {
4111                                     .cra_name = "hmac(sha512)",
4112                                     .cra_driver_name = "hmac-sha512-iproc",
4113                                     .cra_blocksize = SHA512_BLOCK_SIZE,
4114                                 }
4115                       },
4116          .cipher_info = {
4117                          .alg = CIPHER_ALG_NONE,
4118                          .mode = CIPHER_MODE_NONE,
4119                          },
4120          .auth_info = {
4121                        .alg = HASH_ALG_SHA512,
4122                        .mode = HASH_MODE_HMAC,
4123                        },
4124          },
4125         {
4126          .type = CRYPTO_ALG_TYPE_AHASH,
4127          .alg.hash = {
4128                       .halg.digestsize = SHA3_224_DIGEST_SIZE,
4129                       .halg.base = {
4130                                     .cra_name = "sha3-224",
4131                                     .cra_driver_name = "sha3-224-iproc",
4132                                     .cra_blocksize = SHA3_224_BLOCK_SIZE,
4133                                 }
4134                       },
4135          .cipher_info = {
4136                          .alg = CIPHER_ALG_NONE,
4137                          .mode = CIPHER_MODE_NONE,
4138                          },
4139          .auth_info = {
4140                        .alg = HASH_ALG_SHA3_224,
4141                        .mode = HASH_MODE_HASH,
4142                        },
4143          },
4144         {
4145          .type = CRYPTO_ALG_TYPE_AHASH,
4146          .alg.hash = {
4147                       .halg.digestsize = SHA3_224_DIGEST_SIZE,
4148                       .halg.base = {
4149                                     .cra_name = "hmac(sha3-224)",
4150                                     .cra_driver_name = "hmac-sha3-224-iproc",
4151                                     .cra_blocksize = SHA3_224_BLOCK_SIZE,
4152                                 }
4153                       },
4154          .cipher_info = {
4155                          .alg = CIPHER_ALG_NONE,
4156                          .mode = CIPHER_MODE_NONE,
4157                          },
4158          .auth_info = {
4159                        .alg = HASH_ALG_SHA3_224,
4160                        .mode = HASH_MODE_HMAC
4161                        },
4162          },
4163         {
4164          .type = CRYPTO_ALG_TYPE_AHASH,
4165          .alg.hash = {
4166                       .halg.digestsize = SHA3_256_DIGEST_SIZE,
4167                       .halg.base = {
4168                                     .cra_name = "sha3-256",
4169                                     .cra_driver_name = "sha3-256-iproc",
4170                                     .cra_blocksize = SHA3_256_BLOCK_SIZE,
4171                                 }
4172                       },
4173          .cipher_info = {
4174                          .alg = CIPHER_ALG_NONE,
4175                          .mode = CIPHER_MODE_NONE,
4176                          },
4177          .auth_info = {
4178                        .alg = HASH_ALG_SHA3_256,
4179                        .mode = HASH_MODE_HASH,
4180                        },
4181          },
4182         {
4183          .type = CRYPTO_ALG_TYPE_AHASH,
4184          .alg.hash = {
4185                       .halg.digestsize = SHA3_256_DIGEST_SIZE,
4186                       .halg.base = {
4187                                     .cra_name = "hmac(sha3-256)",
4188                                     .cra_driver_name = "hmac-sha3-256-iproc",
4189                                     .cra_blocksize = SHA3_256_BLOCK_SIZE,
4190                                 }
4191                       },
4192          .cipher_info = {
4193                          .alg = CIPHER_ALG_NONE,
4194                          .mode = CIPHER_MODE_NONE,
4195                          },
4196          .auth_info = {
4197                        .alg = HASH_ALG_SHA3_256,
4198                        .mode = HASH_MODE_HMAC,
4199                        },
4200          },
4201         {
4202          .type = CRYPTO_ALG_TYPE_AHASH,
4203          .alg.hash = {
4204                       .halg.digestsize = SHA3_384_DIGEST_SIZE,
4205                       .halg.base = {
4206                                     .cra_name = "sha3-384",
4207                                     .cra_driver_name = "sha3-384-iproc",
4208                                     .cra_blocksize = SHA3_224_BLOCK_SIZE,
4209                                 }
4210                       },
4211          .cipher_info = {
4212                          .alg = CIPHER_ALG_NONE,
4213                          .mode = CIPHER_MODE_NONE,
4214                          },
4215          .auth_info = {
4216                        .alg = HASH_ALG_SHA3_384,
4217                        .mode = HASH_MODE_HASH,
4218                        },
4219          },
4220         {
4221          .type = CRYPTO_ALG_TYPE_AHASH,
4222          .alg.hash = {
4223                       .halg.digestsize = SHA3_384_DIGEST_SIZE,
4224                       .halg.base = {
4225                                     .cra_name = "hmac(sha3-384)",
4226                                     .cra_driver_name = "hmac-sha3-384-iproc",
4227                                     .cra_blocksize = SHA3_384_BLOCK_SIZE,
4228                                 }
4229                       },
4230          .cipher_info = {
4231                          .alg = CIPHER_ALG_NONE,
4232                          .mode = CIPHER_MODE_NONE,
4233                          },
4234          .auth_info = {
4235                        .alg = HASH_ALG_SHA3_384,
4236                        .mode = HASH_MODE_HMAC,
4237                        },
4238          },
4239         {
4240          .type = CRYPTO_ALG_TYPE_AHASH,
4241          .alg.hash = {
4242                       .halg.digestsize = SHA3_512_DIGEST_SIZE,
4243                       .halg.base = {
4244                                     .cra_name = "sha3-512",
4245                                     .cra_driver_name = "sha3-512-iproc",
4246                                     .cra_blocksize = SHA3_512_BLOCK_SIZE,
4247                                 }
4248                       },
4249          .cipher_info = {
4250                          .alg = CIPHER_ALG_NONE,
4251                          .mode = CIPHER_MODE_NONE,
4252                          },
4253          .auth_info = {
4254                        .alg = HASH_ALG_SHA3_512,
4255                        .mode = HASH_MODE_HASH,
4256                        },
4257          },
4258         {
4259          .type = CRYPTO_ALG_TYPE_AHASH,
4260          .alg.hash = {
4261                       .halg.digestsize = SHA3_512_DIGEST_SIZE,
4262                       .halg.base = {
4263                                     .cra_name = "hmac(sha3-512)",
4264                                     .cra_driver_name = "hmac-sha3-512-iproc",
4265                                     .cra_blocksize = SHA3_512_BLOCK_SIZE,
4266                                 }
4267                       },
4268          .cipher_info = {
4269                          .alg = CIPHER_ALG_NONE,
4270                          .mode = CIPHER_MODE_NONE,
4271                          },
4272          .auth_info = {
4273                        .alg = HASH_ALG_SHA3_512,
4274                        .mode = HASH_MODE_HMAC,
4275                        },
4276          },
4277         {
4278          .type = CRYPTO_ALG_TYPE_AHASH,
4279          .alg.hash = {
4280                       .halg.digestsize = AES_BLOCK_SIZE,
4281                       .halg.base = {
4282                                     .cra_name = "xcbc(aes)",
4283                                     .cra_driver_name = "xcbc-aes-iproc",
4284                                     .cra_blocksize = AES_BLOCK_SIZE,
4285                                 }
4286                       },
4287          .cipher_info = {
4288                          .alg = CIPHER_ALG_NONE,
4289                          .mode = CIPHER_MODE_NONE,
4290                          },
4291          .auth_info = {
4292                        .alg = HASH_ALG_AES,
4293                        .mode = HASH_MODE_XCBC,
4294                        },
4295          },
4296         {
4297          .type = CRYPTO_ALG_TYPE_AHASH,
4298          .alg.hash = {
4299                       .halg.digestsize = AES_BLOCK_SIZE,
4300                       .halg.base = {
4301                                     .cra_name = "cmac(aes)",
4302                                     .cra_driver_name = "cmac-aes-iproc",
4303                                     .cra_blocksize = AES_BLOCK_SIZE,
4304                                 }
4305                       },
4306          .cipher_info = {
4307                          .alg = CIPHER_ALG_NONE,
4308                          .mode = CIPHER_MODE_NONE,
4309                          },
4310          .auth_info = {
4311                        .alg = HASH_ALG_AES,
4312                        .mode = HASH_MODE_CMAC,
4313                        },
4314          },
4315 };
4316
4317 static int generic_cra_init(struct crypto_tfm *tfm,
4318                             struct iproc_alg_s *cipher_alg)
4319 {
4320         struct spu_hw *spu = &iproc_priv.spu;
4321         struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4322         unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
4323
4324         flow_log("%s()\n", __func__);
4325
4326         ctx->alg = cipher_alg;
4327         ctx->cipher = cipher_alg->cipher_info;
4328         ctx->auth = cipher_alg->auth_info;
4329         ctx->auth_first = cipher_alg->auth_first;
4330         ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
4331                                                     ctx->cipher.mode,
4332                                                     blocksize);
4333         ctx->fallback_cipher = NULL;
4334
4335         ctx->enckeylen = 0;
4336         ctx->authkeylen = 0;
4337
4338         atomic_inc(&iproc_priv.stream_count);
4339         atomic_inc(&iproc_priv.session_count);
4340
4341         return 0;
4342 }
4343
4344 static int ablkcipher_cra_init(struct crypto_tfm *tfm)
4345 {
4346         struct crypto_alg *alg = tfm->__crt_alg;
4347         struct iproc_alg_s *cipher_alg;
4348
4349         flow_log("%s()\n", __func__);
4350
4351         tfm->crt_ablkcipher.reqsize = sizeof(struct iproc_reqctx_s);
4352
4353         cipher_alg = container_of(alg, struct iproc_alg_s, alg.crypto);
4354         return generic_cra_init(tfm, cipher_alg);
4355 }
4356
4357 static int ahash_cra_init(struct crypto_tfm *tfm)
4358 {
4359         int err;
4360         struct crypto_alg *alg = tfm->__crt_alg;
4361         struct iproc_alg_s *cipher_alg;
4362
4363         cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
4364                                   alg.hash);
4365
4366         err = generic_cra_init(tfm, cipher_alg);
4367         flow_log("%s()\n", __func__);
4368
4369         /*
4370          * export state size has to be < 512 bytes. So don't include msg bufs
4371          * in state size.
4372          */
4373         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
4374                                  sizeof(struct iproc_reqctx_s));
4375
4376         return err;
4377 }
4378
4379 static int aead_cra_init(struct crypto_aead *aead)
4380 {
4381         struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4382         struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4383         struct crypto_alg *alg = tfm->__crt_alg;
4384         struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
4385         struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
4386                                                       alg.aead);
4387
4388         int err = generic_cra_init(tfm, cipher_alg);
4389
4390         flow_log("%s()\n", __func__);
4391
4392         crypto_aead_set_reqsize(aead, sizeof(struct iproc_reqctx_s));
4393         ctx->is_esp = false;
4394         ctx->salt_len = 0;
4395         ctx->salt_offset = 0;
4396
4397         /* random first IV */
4398         get_random_bytes(ctx->iv, MAX_IV_SIZE);
4399         flow_dump("  iv: ", ctx->iv, MAX_IV_SIZE);
4400
4401         if (!err) {
4402                 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
4403                         flow_log("%s() creating fallback cipher\n", __func__);
4404
4405                         ctx->fallback_cipher =
4406                             crypto_alloc_aead(alg->cra_name, 0,
4407                                               CRYPTO_ALG_ASYNC |
4408                                               CRYPTO_ALG_NEED_FALLBACK);
4409                         if (IS_ERR(ctx->fallback_cipher)) {
4410                                 pr_err("%s() Error: failed to allocate fallback for %s\n",
4411                                        __func__, alg->cra_name);
4412                                 return PTR_ERR(ctx->fallback_cipher);
4413                         }
4414                 }
4415         }
4416
4417         return err;
4418 }
4419
4420 static void generic_cra_exit(struct crypto_tfm *tfm)
4421 {
4422         atomic_dec(&iproc_priv.session_count);
4423 }
4424
4425 static void aead_cra_exit(struct crypto_aead *aead)
4426 {
4427         struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4428         struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4429
4430         generic_cra_exit(tfm);
4431
4432         if (ctx->fallback_cipher) {
4433                 crypto_free_aead(ctx->fallback_cipher);
4434                 ctx->fallback_cipher = NULL;
4435         }
4436 }
4437
4438 /**
4439  * spu_functions_register() - Specify hardware-specific SPU functions based on
4440  * SPU type read from device tree.
4441  * @dev:        device structure
4442  * @spu_type:   SPU hardware generation
4443  * @spu_subtype: SPU hardware version
4444  */
4445 static void spu_functions_register(struct device *dev,
4446                                    enum spu_spu_type spu_type,
4447                                    enum spu_spu_subtype spu_subtype)
4448 {
4449         struct spu_hw *spu = &iproc_priv.spu;
4450
4451         if (spu_type == SPU_TYPE_SPUM) {
4452                 dev_dbg(dev, "Registering SPUM functions");
4453                 spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
4454                 spu->spu_payload_length = spum_payload_length;
4455                 spu->spu_response_hdr_len = spum_response_hdr_len;
4456                 spu->spu_hash_pad_len = spum_hash_pad_len;
4457                 spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
4458                 spu->spu_assoc_resp_len = spum_assoc_resp_len;
4459                 spu->spu_aead_ivlen = spum_aead_ivlen;
4460                 spu->spu_hash_type = spum_hash_type;
4461                 spu->spu_digest_size = spum_digest_size;
4462                 spu->spu_create_request = spum_create_request;
4463                 spu->spu_cipher_req_init = spum_cipher_req_init;
4464                 spu->spu_cipher_req_finish = spum_cipher_req_finish;
4465                 spu->spu_request_pad = spum_request_pad;
4466                 spu->spu_tx_status_len = spum_tx_status_len;
4467                 spu->spu_rx_status_len = spum_rx_status_len;
4468                 spu->spu_status_process = spum_status_process;
4469                 spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
4470                 spu->spu_ccm_update_iv = spum_ccm_update_iv;
4471                 spu->spu_wordalign_padlen = spum_wordalign_padlen;
4472                 if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
4473                         spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
4474                 else
4475                         spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
4476         } else {
4477                 dev_dbg(dev, "Registering SPU2 functions");
4478                 spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
4479                 spu->spu_ctx_max_payload = spu2_ctx_max_payload;
4480                 spu->spu_payload_length = spu2_payload_length;
4481                 spu->spu_response_hdr_len = spu2_response_hdr_len;
4482                 spu->spu_hash_pad_len = spu2_hash_pad_len;
4483                 spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
4484                 spu->spu_assoc_resp_len = spu2_assoc_resp_len;
4485                 spu->spu_aead_ivlen = spu2_aead_ivlen;
4486                 spu->spu_hash_type = spu2_hash_type;
4487                 spu->spu_digest_size = spu2_digest_size;
4488                 spu->spu_create_request = spu2_create_request;
4489                 spu->spu_cipher_req_init = spu2_cipher_req_init;
4490                 spu->spu_cipher_req_finish = spu2_cipher_req_finish;
4491                 spu->spu_request_pad = spu2_request_pad;
4492                 spu->spu_tx_status_len = spu2_tx_status_len;
4493                 spu->spu_rx_status_len = spu2_rx_status_len;
4494                 spu->spu_status_process = spu2_status_process;
4495                 spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
4496                 spu->spu_ccm_update_iv = spu2_ccm_update_iv;
4497                 spu->spu_wordalign_padlen = spu2_wordalign_padlen;
4498         }
4499 }
4500
4501 /**
4502  * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
4503  * channel for the SPU being probed.
4504  * @dev:  SPU driver device structure
4505  *
4506  * Return: 0 if successful
4507  *         < 0 otherwise
4508  */
4509 static int spu_mb_init(struct device *dev)
4510 {
4511         struct mbox_client *mcl = &iproc_priv.mcl;
4512         int err, i;
4513
4514         iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
4515                                   sizeof(struct mbox_chan *), GFP_KERNEL);
4516         if (!iproc_priv.mbox)
4517                 return -ENOMEM;
4518
4519         mcl->dev = dev;
4520         mcl->tx_block = false;
4521         mcl->tx_tout = 0;
4522         mcl->knows_txdone = true;
4523         mcl->rx_callback = spu_rx_callback;
4524         mcl->tx_done = NULL;
4525
4526         for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4527                 iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
4528                 if (IS_ERR(iproc_priv.mbox[i])) {
4529                         err = (int)PTR_ERR(iproc_priv.mbox[i]);
4530                         dev_err(dev,
4531                                 "Mbox channel %d request failed with err %d",
4532                                 i, err);
4533                         iproc_priv.mbox[i] = NULL;
4534                         goto free_channels;
4535                 }
4536         }
4537
4538         return 0;
4539 free_channels:
4540         for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4541                 if (iproc_priv.mbox[i])
4542                         mbox_free_channel(iproc_priv.mbox[i]);
4543         }
4544
4545         return err;
4546 }
4547
4548 static void spu_mb_release(struct platform_device *pdev)
4549 {
4550         int i;
4551
4552         for (i = 0; i < iproc_priv.spu.num_chan; i++)
4553                 mbox_free_channel(iproc_priv.mbox[i]);
4554 }
4555
4556 static void spu_counters_init(void)
4557 {
4558         int i;
4559         int j;
4560
4561         atomic_set(&iproc_priv.session_count, 0);
4562         atomic_set(&iproc_priv.stream_count, 0);
4563         atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
4564         atomic64_set(&iproc_priv.bytes_in, 0);
4565         atomic64_set(&iproc_priv.bytes_out, 0);
4566         for (i = 0; i < SPU_OP_NUM; i++) {
4567                 atomic_set(&iproc_priv.op_counts[i], 0);
4568                 atomic_set(&iproc_priv.setkey_cnt[i], 0);
4569         }
4570         for (i = 0; i < CIPHER_ALG_LAST; i++)
4571                 for (j = 0; j < CIPHER_MODE_LAST; j++)
4572                         atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
4573
4574         for (i = 0; i < HASH_ALG_LAST; i++) {
4575                 atomic_set(&iproc_priv.hash_cnt[i], 0);
4576                 atomic_set(&iproc_priv.hmac_cnt[i], 0);
4577         }
4578         for (i = 0; i < AEAD_TYPE_LAST; i++)
4579                 atomic_set(&iproc_priv.aead_cnt[i], 0);
4580
4581         atomic_set(&iproc_priv.mb_no_spc, 0);
4582         atomic_set(&iproc_priv.mb_send_fail, 0);
4583         atomic_set(&iproc_priv.bad_icv, 0);
4584 }
4585
4586 static int spu_register_ablkcipher(struct iproc_alg_s *driver_alg)
4587 {
4588         struct spu_hw *spu = &iproc_priv.spu;
4589         struct crypto_alg *crypto = &driver_alg->alg.crypto;
4590         int err;
4591
4592         /* SPU2 does not support RC4 */
4593         if ((driver_alg->cipher_info.alg == CIPHER_ALG_RC4) &&
4594             (spu->spu_type == SPU_TYPE_SPU2))
4595                 return 0;
4596
4597         crypto->cra_module = THIS_MODULE;
4598         crypto->cra_priority = cipher_pri;
4599         crypto->cra_alignmask = 0;
4600         crypto->cra_ctxsize = sizeof(struct iproc_ctx_s);
4601         INIT_LIST_HEAD(&crypto->cra_list);
4602
4603         crypto->cra_init = ablkcipher_cra_init;
4604         crypto->cra_exit = generic_cra_exit;
4605         crypto->cra_type = &crypto_ablkcipher_type;
4606         crypto->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
4607                                 CRYPTO_ALG_KERN_DRIVER_ONLY;
4608
4609         crypto->cra_ablkcipher.setkey = ablkcipher_setkey;
4610         crypto->cra_ablkcipher.encrypt = ablkcipher_encrypt;
4611         crypto->cra_ablkcipher.decrypt = ablkcipher_decrypt;
4612
4613         err = crypto_register_alg(crypto);
4614         /* Mark alg as having been registered, if successful */
4615         if (err == 0)
4616                 driver_alg->registered = true;
4617         pr_debug("  registered ablkcipher %s\n", crypto->cra_driver_name);
4618         return err;
4619 }
4620
4621 static int spu_register_ahash(struct iproc_alg_s *driver_alg)
4622 {
4623         struct spu_hw *spu = &iproc_priv.spu;
4624         struct ahash_alg *hash = &driver_alg->alg.hash;
4625         int err;
4626
4627         /* AES-XCBC is the only AES hash type currently supported on SPU-M */
4628         if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4629             (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
4630             (spu->spu_type == SPU_TYPE_SPUM))
4631                 return 0;
4632
4633         /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
4634         if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
4635             (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
4636                 return 0;
4637
4638         hash->halg.base.cra_module = THIS_MODULE;
4639         hash->halg.base.cra_priority = hash_pri;
4640         hash->halg.base.cra_alignmask = 0;
4641         hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4642         hash->halg.base.cra_init = ahash_cra_init;
4643         hash->halg.base.cra_exit = generic_cra_exit;
4644         hash->halg.base.cra_type = &crypto_ahash_type;
4645         hash->halg.base.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
4646         hash->halg.statesize = sizeof(struct spu_hash_export_s);
4647
4648         if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
4649                 hash->init = ahash_init;
4650                 hash->update = ahash_update;
4651                 hash->final = ahash_final;
4652                 hash->finup = ahash_finup;
4653                 hash->digest = ahash_digest;
4654                 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4655                     ((driver_alg->auth_info.mode == HASH_MODE_XCBC) ||
4656                     (driver_alg->auth_info.mode == HASH_MODE_CMAC))) {
4657                         hash->setkey = ahash_setkey;
4658                 }
4659         } else {
4660                 hash->setkey = ahash_hmac_setkey;
4661                 hash->init = ahash_hmac_init;
4662                 hash->update = ahash_hmac_update;
4663                 hash->final = ahash_hmac_final;
4664                 hash->finup = ahash_hmac_finup;
4665                 hash->digest = ahash_hmac_digest;
4666         }
4667         hash->export = ahash_export;
4668         hash->import = ahash_import;
4669
4670         err = crypto_register_ahash(hash);
4671         /* Mark alg as having been registered, if successful */
4672         if (err == 0)
4673                 driver_alg->registered = true;
4674         pr_debug("  registered ahash %s\n",
4675                  hash->halg.base.cra_driver_name);
4676         return err;
4677 }
4678
4679 static int spu_register_aead(struct iproc_alg_s *driver_alg)
4680 {
4681         struct aead_alg *aead = &driver_alg->alg.aead;
4682         int err;
4683
4684         aead->base.cra_module = THIS_MODULE;
4685         aead->base.cra_priority = aead_pri;
4686         aead->base.cra_alignmask = 0;
4687         aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4688         INIT_LIST_HEAD(&aead->base.cra_list);
4689
4690         aead->base.cra_flags |= CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
4691         /* setkey set in alg initialization */
4692         aead->setauthsize = aead_setauthsize;
4693         aead->encrypt = aead_encrypt;
4694         aead->decrypt = aead_decrypt;
4695         aead->init = aead_cra_init;
4696         aead->exit = aead_cra_exit;
4697
4698         err = crypto_register_aead(aead);
4699         /* Mark alg as having been registered, if successful */
4700         if (err == 0)
4701                 driver_alg->registered = true;
4702         pr_debug("  registered aead %s\n", aead->base.cra_driver_name);
4703         return err;
4704 }
4705
4706 /* register crypto algorithms the device supports */
4707 static int spu_algs_register(struct device *dev)
4708 {
4709         int i, j;
4710         int err;
4711
4712         for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4713                 switch (driver_algs[i].type) {
4714                 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4715                         err = spu_register_ablkcipher(&driver_algs[i]);
4716                         break;
4717                 case CRYPTO_ALG_TYPE_AHASH:
4718                         err = spu_register_ahash(&driver_algs[i]);
4719                         break;
4720                 case CRYPTO_ALG_TYPE_AEAD:
4721                         err = spu_register_aead(&driver_algs[i]);
4722                         break;
4723                 default:
4724                         dev_err(dev,
4725                                 "iproc-crypto: unknown alg type: %d",
4726                                 driver_algs[i].type);
4727                         err = -EINVAL;
4728                 }
4729
4730                 if (err) {
4731                         dev_err(dev, "alg registration failed with error %d\n",
4732                                 err);
4733                         goto err_algs;
4734                 }
4735         }
4736
4737         return 0;
4738
4739 err_algs:
4740         for (j = 0; j < i; j++) {
4741                 /* Skip any algorithm not registered */
4742                 if (!driver_algs[j].registered)
4743                         continue;
4744                 switch (driver_algs[j].type) {
4745                 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4746                         crypto_unregister_alg(&driver_algs[j].alg.crypto);
4747                         driver_algs[j].registered = false;
4748                         break;
4749                 case CRYPTO_ALG_TYPE_AHASH:
4750                         crypto_unregister_ahash(&driver_algs[j].alg.hash);
4751                         driver_algs[j].registered = false;
4752                         break;
4753                 case CRYPTO_ALG_TYPE_AEAD:
4754                         crypto_unregister_aead(&driver_algs[j].alg.aead);
4755                         driver_algs[j].registered = false;
4756                         break;
4757                 }
4758         }
4759         return err;
4760 }
4761
4762 /* ==================== Kernel Platform API ==================== */
4763
4764 static struct spu_type_subtype spum_ns2_types = {
4765         SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
4766 };
4767
4768 static struct spu_type_subtype spum_nsp_types = {
4769         SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
4770 };
4771
4772 static struct spu_type_subtype spu2_types = {
4773         SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
4774 };
4775
4776 static struct spu_type_subtype spu2_v2_types = {
4777         SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
4778 };
4779
4780 static const struct of_device_id bcm_spu_dt_ids[] = {
4781         {
4782                 .compatible = "brcm,spum-crypto",
4783                 .data = &spum_ns2_types,
4784         },
4785         {
4786                 .compatible = "brcm,spum-nsp-crypto",
4787                 .data = &spum_nsp_types,
4788         },
4789         {
4790                 .compatible = "brcm,spu2-crypto",
4791                 .data = &spu2_types,
4792         },
4793         {
4794                 .compatible = "brcm,spu2-v2-crypto",
4795                 .data = &spu2_v2_types,
4796         },
4797         { /* sentinel */ }
4798 };
4799
4800 MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
4801
4802 static int spu_dt_read(struct platform_device *pdev)
4803 {
4804         struct device *dev = &pdev->dev;
4805         struct spu_hw *spu = &iproc_priv.spu;
4806         struct resource *spu_ctrl_regs;
4807         const struct of_device_id *match;
4808         const struct spu_type_subtype *matched_spu_type;
4809         struct device_node *dn = pdev->dev.of_node;
4810         int err, i;
4811
4812         /* Count number of mailbox channels */
4813         spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
4814
4815         match = of_match_device(of_match_ptr(bcm_spu_dt_ids), dev);
4816         if (!match) {
4817                 dev_err(&pdev->dev, "Failed to match device\n");
4818                 return -ENODEV;
4819         }
4820
4821         matched_spu_type = match->data;
4822
4823         spu->spu_type = matched_spu_type->type;
4824         spu->spu_subtype = matched_spu_type->subtype;
4825
4826         i = 0;
4827         for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
4828                 platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
4829
4830                 spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
4831                 if (IS_ERR(spu->reg_vbase[i])) {
4832                         err = PTR_ERR(spu->reg_vbase[i]);
4833                         dev_err(&pdev->dev, "Failed to map registers: %d\n",
4834                                 err);
4835                         spu->reg_vbase[i] = NULL;
4836                         return err;
4837                 }
4838         }
4839         spu->num_spu = i;
4840         dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
4841
4842         return 0;
4843 }
4844
4845 int bcm_spu_probe(struct platform_device *pdev)
4846 {
4847         struct device *dev = &pdev->dev;
4848         struct spu_hw *spu = &iproc_priv.spu;
4849         int err = 0;
4850
4851         iproc_priv.pdev  = pdev;
4852         platform_set_drvdata(iproc_priv.pdev,
4853                              &iproc_priv);
4854
4855         err = spu_dt_read(pdev);
4856         if (err < 0)
4857                 goto failure;
4858
4859         err = spu_mb_init(&pdev->dev);
4860         if (err < 0)
4861                 goto failure;
4862
4863         if (spu->spu_type == SPU_TYPE_SPUM)
4864                 iproc_priv.bcm_hdr_len = 8;
4865         else if (spu->spu_type == SPU_TYPE_SPU2)
4866                 iproc_priv.bcm_hdr_len = 0;
4867
4868         spu_functions_register(&pdev->dev, spu->spu_type, spu->spu_subtype);
4869
4870         spu_counters_init();
4871
4872         spu_setup_debugfs();
4873
4874         err = spu_algs_register(dev);
4875         if (err < 0)
4876                 goto fail_reg;
4877
4878         return 0;
4879
4880 fail_reg:
4881         spu_free_debugfs();
4882 failure:
4883         spu_mb_release(pdev);
4884         dev_err(dev, "%s failed with error %d.\n", __func__, err);
4885
4886         return err;
4887 }
4888
4889 int bcm_spu_remove(struct platform_device *pdev)
4890 {
4891         int i;
4892         struct device *dev = &pdev->dev;
4893         char *cdn;
4894
4895         for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4896                 /*
4897                  * Not all algorithms were registered, depending on whether
4898                  * hardware is SPU or SPU2.  So here we make sure to skip
4899                  * those algorithms that were not previously registered.
4900                  */
4901                 if (!driver_algs[i].registered)
4902                         continue;
4903
4904                 switch (driver_algs[i].type) {
4905                 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4906                         crypto_unregister_alg(&driver_algs[i].alg.crypto);
4907                         dev_dbg(dev, "  unregistered cipher %s\n",
4908                                 driver_algs[i].alg.crypto.cra_driver_name);
4909                         driver_algs[i].registered = false;
4910                         break;
4911                 case CRYPTO_ALG_TYPE_AHASH:
4912                         crypto_unregister_ahash(&driver_algs[i].alg.hash);
4913                         cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
4914                         dev_dbg(dev, "  unregistered hash %s\n", cdn);
4915                         driver_algs[i].registered = false;
4916                         break;
4917                 case CRYPTO_ALG_TYPE_AEAD:
4918                         crypto_unregister_aead(&driver_algs[i].alg.aead);
4919                         dev_dbg(dev, "  unregistered aead %s\n",
4920                                 driver_algs[i].alg.aead.base.cra_driver_name);
4921                         driver_algs[i].registered = false;
4922                         break;
4923                 }
4924         }
4925         spu_free_debugfs();
4926         spu_mb_release(pdev);
4927         return 0;
4928 }
4929
4930 /* ===== Kernel Module API ===== */
4931
4932 static struct platform_driver bcm_spu_pdriver = {
4933         .driver = {
4934                    .name = "brcm-spu-crypto",
4935                    .of_match_table = of_match_ptr(bcm_spu_dt_ids),
4936                    },
4937         .probe = bcm_spu_probe,
4938         .remove = bcm_spu_remove,
4939 };
4940 module_platform_driver(bcm_spu_pdriver);
4941
4942 MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
4943 MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
4944 MODULE_LICENSE("GPL v2");