blob: 521244e08fb11d45381b389e610b503a11a8284b [file] [log] [blame]
Kim Phillips9c4a7962008-06-23 19:50:15 +08001/*
2 * talitos - Freescale Integrated Security Engine (SEC) device driver
3 *
Kim Phillips5228f0f2011-07-15 11:21:38 +08004 * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
Kim Phillips9c4a7962008-06-23 19:50:15 +08005 *
6 * Scatterlist Crypto API glue code copied from files with the following:
7 * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * Crypto algorithm registration code copied from hifn driver:
10 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/mod_devicetable.h>
31#include <linux/device.h>
32#include <linux/interrupt.h>
33#include <linux/crypto.h>
34#include <linux/hw_random.h>
35#include <linux/of_platform.h>
36#include <linux/dma-mapping.h>
37#include <linux/io.h>
38#include <linux/spinlock.h>
39#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080041
42#include <crypto/algapi.h>
43#include <crypto/aes.h>
Lee Nipper3952f172008-07-10 18:29:18 +080044#include <crypto/des.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080045#include <crypto/sha.h>
Lee Nipper497f2e62010-05-19 19:20:36 +100046#include <crypto/md5.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080047#include <crypto/aead.h>
48#include <crypto/authenc.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080049#include <crypto/skcipher.h>
Lee Nipperacbf7c622010-05-19 19:19:33 +100050#include <crypto/hash.h>
51#include <crypto/internal/hash.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080052#include <crypto/scatterwalk.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080053
54#include "talitos.h"
55
56#define TALITOS_TIMEOUT 100000
57#define TALITOS_MAX_DATA_LEN 65535
58
59#define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
60#define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
61#define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
62
63/* descriptor pointer entry */
64struct talitos_ptr {
65 __be16 len; /* length */
66 u8 j_extent; /* jump to sg link table and/or extent */
67 u8 eptr; /* extended address */
68 __be32 ptr; /* address */
69};
70
Lee Nipper497f2e62010-05-19 19:20:36 +100071static const struct talitos_ptr zero_entry = {
72 .len = 0,
73 .j_extent = 0,
74 .eptr = 0,
75 .ptr = 0
76};
77
Kim Phillips9c4a7962008-06-23 19:50:15 +080078/* descriptor */
79struct talitos_desc {
80 __be32 hdr; /* header high bits */
81 __be32 hdr_lo; /* header low bits */
82 struct talitos_ptr ptr[7]; /* ptr/len pair array */
83};
84
85/**
86 * talitos_request - descriptor submission request
87 * @desc: descriptor pointer (kernel virtual)
88 * @dma_desc: descriptor's physical bus address
89 * @callback: whom to call when descriptor processing is done
90 * @context: caller context (optional)
91 */
92struct talitos_request {
93 struct talitos_desc *desc;
94 dma_addr_t dma_desc;
95 void (*callback) (struct device *dev, struct talitos_desc *desc,
96 void *context, int error);
97 void *context;
98};
99
Kim Phillips4b9926282009-08-13 11:50:38 +1000100/* per-channel fifo management */
101struct talitos_channel {
102 /* request fifo */
103 struct talitos_request *fifo;
104
105 /* number of requests pending in channel h/w fifo */
106 atomic_t submit_count ____cacheline_aligned;
107
108 /* request submission (head) lock */
109 spinlock_t head_lock ____cacheline_aligned;
110 /* index to next free descriptor request */
111 int head;
112
113 /* request release (tail) lock */
114 spinlock_t tail_lock ____cacheline_aligned;
115 /* index to next in-progress/done descriptor request */
116 int tail;
117};
118
Kim Phillips9c4a7962008-06-23 19:50:15 +0800119struct talitos_private {
120 struct device *dev;
Grant Likely2dc11582010-08-06 09:25:50 -0600121 struct platform_device *ofdev;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800122 void __iomem *reg;
123 int irq;
124
125 /* SEC version geometry (from device tree node) */
126 unsigned int num_channels;
127 unsigned int chfifo_len;
128 unsigned int exec_units;
129 unsigned int desc_types;
130
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800131 /* SEC Compatibility info */
132 unsigned long features;
133
Kim Phillips9c4a7962008-06-23 19:50:15 +0800134 /*
135 * length of the request fifo
136 * fifo_len is chfifo_len rounded up to next power of 2
137 * so we can use bitwise ops to wrap
138 */
139 unsigned int fifo_len;
140
Kim Phillips4b9926282009-08-13 11:50:38 +1000141 struct talitos_channel *chan;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800142
Kim Phillips4b9926282009-08-13 11:50:38 +1000143 /* next channel to be assigned next incoming descriptor */
144 atomic_t last_chan ____cacheline_aligned;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800145
146 /* request callback tasklet */
147 struct tasklet_struct done_task;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800148
149 /* list of registered algorithms */
150 struct list_head alg_list;
151
152 /* hwrng device */
153 struct hwrng rng;
154};
155
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800156/* .features flag */
157#define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800158#define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
Kim Phillips60f208d2010-05-19 19:21:53 +1000159#define TALITOS_FTR_SHA224_HWINIT 0x00000004
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800160
Kim Phillips81eb0242009-08-13 11:51:51 +1000161static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
162{
163 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +0800164 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +1000165}
166
Kim Phillips9c4a7962008-06-23 19:50:15 +0800167/*
168 * map virtual single (contiguous) pointer to h/w descriptor pointer
169 */
170static void map_single_talitos_ptr(struct device *dev,
171 struct talitos_ptr *talitos_ptr,
172 unsigned short len, void *data,
173 unsigned char extent,
174 enum dma_data_direction dir)
175{
Kim Phillips81eb0242009-08-13 11:51:51 +1000176 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
177
Kim Phillips9c4a7962008-06-23 19:50:15 +0800178 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +1000179 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800180 talitos_ptr->j_extent = extent;
181}
182
183/*
184 * unmap bus single (contiguous) h/w descriptor pointer
185 */
186static void unmap_single_talitos_ptr(struct device *dev,
187 struct talitos_ptr *talitos_ptr,
188 enum dma_data_direction dir)
189{
190 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
191 be16_to_cpu(talitos_ptr->len), dir);
192}
193
194static int reset_channel(struct device *dev, int ch)
195{
196 struct talitos_private *priv = dev_get_drvdata(dev);
197 unsigned int timeout = TALITOS_TIMEOUT;
198
199 setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
200
201 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
202 && --timeout)
203 cpu_relax();
204
205 if (timeout == 0) {
206 dev_err(dev, "failed to reset channel %d\n", ch);
207 return -EIO;
208 }
209
Kim Phillips81eb0242009-08-13 11:51:51 +1000210 /* set 36-bit addressing, done writeback enable and done IRQ enable */
211 setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE |
212 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800213
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800214 /* and ICCR writeback, if available */
215 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
216 setbits32(priv->reg + TALITOS_CCCR_LO(ch),
217 TALITOS_CCCR_LO_IWSE);
218
Kim Phillips9c4a7962008-06-23 19:50:15 +0800219 return 0;
220}
221
222static int reset_device(struct device *dev)
223{
224 struct talitos_private *priv = dev_get_drvdata(dev);
225 unsigned int timeout = TALITOS_TIMEOUT;
226
227 setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
228
229 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
230 && --timeout)
231 cpu_relax();
232
233 if (timeout == 0) {
234 dev_err(dev, "failed to reset device\n");
235 return -EIO;
236 }
237
238 return 0;
239}
240
241/*
242 * Reset and initialize the device
243 */
244static int init_device(struct device *dev)
245{
246 struct talitos_private *priv = dev_get_drvdata(dev);
247 int ch, err;
248
249 /*
250 * Master reset
251 * errata documentation: warning: certain SEC interrupts
252 * are not fully cleared by writing the MCR:SWR bit,
253 * set bit twice to completely reset
254 */
255 err = reset_device(dev);
256 if (err)
257 return err;
258
259 err = reset_device(dev);
260 if (err)
261 return err;
262
263 /* reset channels */
264 for (ch = 0; ch < priv->num_channels; ch++) {
265 err = reset_channel(dev, ch);
266 if (err)
267 return err;
268 }
269
270 /* enable channel done and error interrupts */
271 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
272 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
273
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800274 /* disable integrity check error interrupts (use writeback instead) */
275 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
276 setbits32(priv->reg + TALITOS_MDEUICR_LO,
277 TALITOS_MDEUICR_LO_ICE);
278
Kim Phillips9c4a7962008-06-23 19:50:15 +0800279 return 0;
280}
281
282/**
283 * talitos_submit - submits a descriptor to the device for processing
284 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800285 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800286 * @desc: the descriptor to be processed by the device
287 * @callback: whom to call when processing is complete
288 * @context: a handle for use by caller (optional)
289 *
290 * desc must contain valid dma-mapped (bus physical) address pointers.
291 * callback must check err and feedback in descriptor header
292 * for device processing status.
293 */
Kim Phillips5228f0f2011-07-15 11:21:38 +0800294static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800295 void (*callback)(struct device *dev,
296 struct talitos_desc *desc,
297 void *context, int error),
298 void *context)
299{
300 struct talitos_private *priv = dev_get_drvdata(dev);
301 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800302 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800303 int head;
304
Kim Phillips4b9926282009-08-13 11:50:38 +1000305 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800306
Kim Phillips4b9926282009-08-13 11:50:38 +1000307 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800308 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000309 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800310 return -EAGAIN;
311 }
312
Kim Phillips4b9926282009-08-13 11:50:38 +1000313 head = priv->chan[ch].head;
314 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800315
Kim Phillips9c4a7962008-06-23 19:50:15 +0800316 /* map descriptor and save caller data */
317 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
318 DMA_BIDIRECTIONAL);
319 request->callback = callback;
320 request->context = context;
321
322 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000323 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800324
325 smp_wmb();
326 request->desc = desc;
327
328 /* GO! */
329 wmb();
Kim Phillipsa7524472010-09-23 15:56:38 +0800330 out_be32(priv->reg + TALITOS_FF(ch), upper_32_bits(request->dma_desc));
Kim Phillips81eb0242009-08-13 11:51:51 +1000331 out_be32(priv->reg + TALITOS_FF_LO(ch),
Kim Phillipsa7524472010-09-23 15:56:38 +0800332 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800333
Kim Phillips4b9926282009-08-13 11:50:38 +1000334 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800335
336 return -EINPROGRESS;
337}
338
339/*
340 * process what was done, notify callback of error if not
341 */
342static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
343{
344 struct talitos_private *priv = dev_get_drvdata(dev);
345 struct talitos_request *request, saved_req;
346 unsigned long flags;
347 int tail, status;
348
Kim Phillips4b9926282009-08-13 11:50:38 +1000349 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800350
Kim Phillips4b9926282009-08-13 11:50:38 +1000351 tail = priv->chan[ch].tail;
352 while (priv->chan[ch].fifo[tail].desc) {
353 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800354
355 /* descriptors with their done bits set don't get the error */
356 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100357 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800358 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100359 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800360 if (!error)
361 break;
362 else
363 status = error;
364
365 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800366 sizeof(struct talitos_desc),
367 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800368
369 /* copy entries so we can call callback outside lock */
370 saved_req.desc = request->desc;
371 saved_req.callback = request->callback;
372 saved_req.context = request->context;
373
374 /* release request entry in fifo */
375 smp_wmb();
376 request->desc = NULL;
377
378 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000379 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800380
Kim Phillips4b9926282009-08-13 11:50:38 +1000381 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800382
Kim Phillips4b9926282009-08-13 11:50:38 +1000383 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800384
Kim Phillips9c4a7962008-06-23 19:50:15 +0800385 saved_req.callback(dev, saved_req.desc, saved_req.context,
386 status);
387 /* channel may resume processing in single desc error case */
388 if (error && !reset_ch && status == error)
389 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000390 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
391 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800392 }
393
Kim Phillips4b9926282009-08-13 11:50:38 +1000394 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800395}
396
397/*
398 * process completed requests for channels that have done status
399 */
400static void talitos_done(unsigned long data)
401{
402 struct device *dev = (struct device *)data;
403 struct talitos_private *priv = dev_get_drvdata(dev);
404 int ch;
405
406 for (ch = 0; ch < priv->num_channels; ch++)
407 flush_channel(dev, ch, 0, 0);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800408
409 /* At this point, all completed channels have been processed.
410 * Unmask done interrupts for channels completed later on.
411 */
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800412 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
413 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800414}
415
416/*
417 * locate current (offending) descriptor
418 */
419static struct talitos_desc *current_desc(struct device *dev, int ch)
420{
421 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000422 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800423 dma_addr_t cur_desc;
424
425 cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
426
Kim Phillips4b9926282009-08-13 11:50:38 +1000427 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800428 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000429 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800430 dev_err(dev, "couldn't locate current descriptor\n");
431 return NULL;
432 }
433 }
434
Kim Phillips4b9926282009-08-13 11:50:38 +1000435 return priv->chan[ch].fifo[tail].desc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436}
437
438/*
439 * user diagnostics; report root cause of error based on execution unit status
440 */
Kim Phillipse938e462009-03-29 15:53:23 +0800441static void report_eu_error(struct device *dev, int ch,
442 struct talitos_desc *desc)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800443{
444 struct talitos_private *priv = dev_get_drvdata(dev);
445 int i;
446
447 switch (desc->hdr & DESC_HDR_SEL0_MASK) {
448 case DESC_HDR_SEL0_AFEU:
449 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
450 in_be32(priv->reg + TALITOS_AFEUISR),
451 in_be32(priv->reg + TALITOS_AFEUISR_LO));
452 break;
453 case DESC_HDR_SEL0_DEU:
454 dev_err(dev, "DEUISR 0x%08x_%08x\n",
455 in_be32(priv->reg + TALITOS_DEUISR),
456 in_be32(priv->reg + TALITOS_DEUISR_LO));
457 break;
458 case DESC_HDR_SEL0_MDEUA:
459 case DESC_HDR_SEL0_MDEUB:
460 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
461 in_be32(priv->reg + TALITOS_MDEUISR),
462 in_be32(priv->reg + TALITOS_MDEUISR_LO));
463 break;
464 case DESC_HDR_SEL0_RNG:
465 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
466 in_be32(priv->reg + TALITOS_RNGUISR),
467 in_be32(priv->reg + TALITOS_RNGUISR_LO));
468 break;
469 case DESC_HDR_SEL0_PKEU:
470 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
471 in_be32(priv->reg + TALITOS_PKEUISR),
472 in_be32(priv->reg + TALITOS_PKEUISR_LO));
473 break;
474 case DESC_HDR_SEL0_AESU:
475 dev_err(dev, "AESUISR 0x%08x_%08x\n",
476 in_be32(priv->reg + TALITOS_AESUISR),
477 in_be32(priv->reg + TALITOS_AESUISR_LO));
478 break;
479 case DESC_HDR_SEL0_CRCU:
480 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
481 in_be32(priv->reg + TALITOS_CRCUISR),
482 in_be32(priv->reg + TALITOS_CRCUISR_LO));
483 break;
484 case DESC_HDR_SEL0_KEU:
485 dev_err(dev, "KEUISR 0x%08x_%08x\n",
486 in_be32(priv->reg + TALITOS_KEUISR),
487 in_be32(priv->reg + TALITOS_KEUISR_LO));
488 break;
489 }
490
491 switch (desc->hdr & DESC_HDR_SEL1_MASK) {
492 case DESC_HDR_SEL1_MDEUA:
493 case DESC_HDR_SEL1_MDEUB:
494 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
495 in_be32(priv->reg + TALITOS_MDEUISR),
496 in_be32(priv->reg + TALITOS_MDEUISR_LO));
497 break;
498 case DESC_HDR_SEL1_CRCU:
499 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
500 in_be32(priv->reg + TALITOS_CRCUISR),
501 in_be32(priv->reg + TALITOS_CRCUISR_LO));
502 break;
503 }
504
505 for (i = 0; i < 8; i++)
506 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
507 in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
508 in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
509}
510
511/*
512 * recover from error interrupts
513 */
Kim Phillips40405f12008-10-12 20:19:35 +0800514static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800515{
516 struct device *dev = (struct device *)data;
517 struct talitos_private *priv = dev_get_drvdata(dev);
518 unsigned int timeout = TALITOS_TIMEOUT;
519 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800520 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800521
522 for (ch = 0; ch < priv->num_channels; ch++) {
523 /* skip channels without errors */
524 if (!(isr & (1 << (ch * 2 + 1))))
525 continue;
526
527 error = -EINVAL;
528
529 v = in_be32(priv->reg + TALITOS_CCPSR(ch));
530 v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
531
532 if (v_lo & TALITOS_CCPSR_LO_DOF) {
533 dev_err(dev, "double fetch fifo overflow error\n");
534 error = -EAGAIN;
535 reset_ch = 1;
536 }
537 if (v_lo & TALITOS_CCPSR_LO_SOF) {
538 /* h/w dropped descriptor */
539 dev_err(dev, "single fetch fifo overflow error\n");
540 error = -EAGAIN;
541 }
542 if (v_lo & TALITOS_CCPSR_LO_MDTE)
543 dev_err(dev, "master data transfer error\n");
544 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
545 dev_err(dev, "s/g data length zero error\n");
546 if (v_lo & TALITOS_CCPSR_LO_FPZ)
547 dev_err(dev, "fetch pointer zero error\n");
548 if (v_lo & TALITOS_CCPSR_LO_IDH)
549 dev_err(dev, "illegal descriptor header error\n");
550 if (v_lo & TALITOS_CCPSR_LO_IEU)
551 dev_err(dev, "invalid execution unit error\n");
552 if (v_lo & TALITOS_CCPSR_LO_EU)
553 report_eu_error(dev, ch, current_desc(dev, ch));
554 if (v_lo & TALITOS_CCPSR_LO_GB)
555 dev_err(dev, "gather boundary error\n");
556 if (v_lo & TALITOS_CCPSR_LO_GRL)
557 dev_err(dev, "gather return/length error\n");
558 if (v_lo & TALITOS_CCPSR_LO_SB)
559 dev_err(dev, "scatter boundary error\n");
560 if (v_lo & TALITOS_CCPSR_LO_SRL)
561 dev_err(dev, "scatter return/length error\n");
562
563 flush_channel(dev, ch, error, reset_ch);
564
565 if (reset_ch) {
566 reset_channel(dev, ch);
567 } else {
568 setbits32(priv->reg + TALITOS_CCCR(ch),
569 TALITOS_CCCR_CONT);
570 setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
571 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
572 TALITOS_CCCR_CONT) && --timeout)
573 cpu_relax();
574 if (timeout == 0) {
575 dev_err(dev, "failed to restart channel %d\n",
576 ch);
577 reset_dev = 1;
578 }
579 }
580 }
581 if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
582 dev_err(dev, "done overflow, internal time out, or rngu error: "
583 "ISR 0x%08x_%08x\n", isr, isr_lo);
584
585 /* purge request queues */
586 for (ch = 0; ch < priv->num_channels; ch++)
587 flush_channel(dev, ch, -EIO, 1);
588
589 /* reset and reinitialize the device */
590 init_device(dev);
591 }
592}
593
594static irqreturn_t talitos_interrupt(int irq, void *data)
595{
596 struct device *dev = data;
597 struct talitos_private *priv = dev_get_drvdata(dev);
598 u32 isr, isr_lo;
599
600 isr = in_be32(priv->reg + TALITOS_ISR);
601 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
Lee Nipperca38a812008-12-20 17:09:25 +1100602 /* Acknowledge interrupt */
603 out_be32(priv->reg + TALITOS_ICR, isr);
604 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800605
Lee Nipperca38a812008-12-20 17:09:25 +1100606 if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
Kim Phillips40405f12008-10-12 20:19:35 +0800607 talitos_error((unsigned long)data, isr, isr_lo);
Lee Nipperca38a812008-12-20 17:09:25 +1100608 else
Lee Nipper1c2e8812008-10-12 20:29:34 +0800609 if (likely(isr & TALITOS_ISR_CHDONE)) {
610 /* mask further done interrupts. */
611 clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
612 /* done_task will unmask done interrupts at exit */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800613 tasklet_schedule(&priv->done_task);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800614 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800615
616 return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
617}
618
619/*
620 * hwrng
621 */
622static int talitos_rng_data_present(struct hwrng *rng, int wait)
623{
624 struct device *dev = (struct device *)rng->priv;
625 struct talitos_private *priv = dev_get_drvdata(dev);
626 u32 ofl;
627 int i;
628
629 for (i = 0; i < 20; i++) {
630 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
631 TALITOS_RNGUSR_LO_OFL;
632 if (ofl || !wait)
633 break;
634 udelay(10);
635 }
636
637 return !!ofl;
638}
639
640static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
641{
642 struct device *dev = (struct device *)rng->priv;
643 struct talitos_private *priv = dev_get_drvdata(dev);
644
645 /* rng fifo requires 64-bit accesses */
646 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
647 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
648
649 return sizeof(u32);
650}
651
652static int talitos_rng_init(struct hwrng *rng)
653{
654 struct device *dev = (struct device *)rng->priv;
655 struct talitos_private *priv = dev_get_drvdata(dev);
656 unsigned int timeout = TALITOS_TIMEOUT;
657
658 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
659 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
660 && --timeout)
661 cpu_relax();
662 if (timeout == 0) {
663 dev_err(dev, "failed to reset rng hw\n");
664 return -ENODEV;
665 }
666
667 /* start generating */
668 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
669
670 return 0;
671}
672
673static int talitos_register_rng(struct device *dev)
674{
675 struct talitos_private *priv = dev_get_drvdata(dev);
676
677 priv->rng.name = dev_driver_string(dev),
678 priv->rng.init = talitos_rng_init,
679 priv->rng.data_present = talitos_rng_data_present,
680 priv->rng.data_read = talitos_rng_data_read,
681 priv->rng.priv = (unsigned long)dev;
682
683 return hwrng_register(&priv->rng);
684}
685
686static void talitos_unregister_rng(struct device *dev)
687{
688 struct talitos_private *priv = dev_get_drvdata(dev);
689
690 hwrng_unregister(&priv->rng);
691}
692
693/*
694 * crypto alg
695 */
696#define TALITOS_CRA_PRIORITY 3000
697#define TALITOS_MAX_KEY_SIZE 64
Lee Nipper3952f172008-07-10 18:29:18 +0800698#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800699
Lee Nipper497f2e62010-05-19 19:20:36 +1000700#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800701
702struct talitos_ctx {
703 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800704 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800705 __be32 desc_hdr_template;
706 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800707 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800708 unsigned int keylen;
709 unsigned int enckeylen;
710 unsigned int authkeylen;
711 unsigned int authsize;
712};
713
Lee Nipper497f2e62010-05-19 19:20:36 +1000714#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
715#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
716
717struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000718 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000719 unsigned int hw_context_size;
720 u8 buf[HASH_MAX_BLOCK_SIZE];
721 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000722 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000723 unsigned int first;
724 unsigned int last;
725 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000726 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000727 struct scatterlist bufsl[2];
728 struct scatterlist *psrc;
729};
730
Lee Nipper56af8cd2009-03-29 15:50:50 +0800731static int aead_setauthsize(struct crypto_aead *authenc,
732 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800733{
734 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
735
736 ctx->authsize = authsize;
737
738 return 0;
739}
740
Lee Nipper56af8cd2009-03-29 15:50:50 +0800741static int aead_setkey(struct crypto_aead *authenc,
742 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800743{
744 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
745 struct rtattr *rta = (void *)key;
746 struct crypto_authenc_key_param *param;
747 unsigned int authkeylen;
748 unsigned int enckeylen;
749
750 if (!RTA_OK(rta, keylen))
751 goto badkey;
752
753 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
754 goto badkey;
755
756 if (RTA_PAYLOAD(rta) < sizeof(*param))
757 goto badkey;
758
759 param = RTA_DATA(rta);
760 enckeylen = be32_to_cpu(param->enckeylen);
761
762 key += RTA_ALIGN(rta->rta_len);
763 keylen -= RTA_ALIGN(rta->rta_len);
764
765 if (keylen < enckeylen)
766 goto badkey;
767
768 authkeylen = keylen - enckeylen;
769
770 if (keylen > TALITOS_MAX_KEY_SIZE)
771 goto badkey;
772
773 memcpy(&ctx->key, key, keylen);
774
775 ctx->keylen = keylen;
776 ctx->enckeylen = enckeylen;
777 ctx->authkeylen = authkeylen;
778
779 return 0;
780
781badkey:
782 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
783 return -EINVAL;
784}
785
786/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800787 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800788 * @src_nents: number of segments in input scatterlist
789 * @dst_nents: number of segments in output scatterlist
790 * @dma_len: length of dma mapped link_tbl space
791 * @dma_link_tbl: bus physical address of link_tbl
792 * @desc: h/w descriptor
793 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
794 *
795 * if decrypting (with authcheck), or either one of src_nents or dst_nents
796 * is greater than 1, an integrity check value is concatenated to the end
797 * of link_tbl data
798 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800799struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800800 int src_nents;
801 int dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800802 int src_is_chained;
803 int dst_is_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800804 int dma_len;
805 dma_addr_t dma_link_tbl;
806 struct talitos_desc desc;
807 struct talitos_ptr link_tbl[0];
808};
809
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800810static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
811 unsigned int nents, enum dma_data_direction dir,
812 int chained)
813{
814 if (unlikely(chained))
815 while (sg) {
816 dma_map_sg(dev, sg, 1, dir);
817 sg = scatterwalk_sg_next(sg);
818 }
819 else
820 dma_map_sg(dev, sg, nents, dir);
821 return nents;
822}
823
824static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
825 enum dma_data_direction dir)
826{
827 while (sg) {
828 dma_unmap_sg(dev, sg, 1, dir);
829 sg = scatterwalk_sg_next(sg);
830 }
831}
832
833static void talitos_sg_unmap(struct device *dev,
834 struct talitos_edesc *edesc,
835 struct scatterlist *src,
836 struct scatterlist *dst)
837{
838 unsigned int src_nents = edesc->src_nents ? : 1;
839 unsigned int dst_nents = edesc->dst_nents ? : 1;
840
841 if (src != dst) {
842 if (edesc->src_is_chained)
843 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
844 else
845 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
846
Lee Nipper497f2e62010-05-19 19:20:36 +1000847 if (dst) {
848 if (edesc->dst_is_chained)
849 talitos_unmap_sg_chain(dev, dst,
850 DMA_FROM_DEVICE);
851 else
852 dma_unmap_sg(dev, dst, dst_nents,
853 DMA_FROM_DEVICE);
854 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800855 } else
856 if (edesc->src_is_chained)
857 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
858 else
859 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
860}
861
Kim Phillips9c4a7962008-06-23 19:50:15 +0800862static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800863 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800864 struct aead_request *areq)
865{
866 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
867 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
868 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
869 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
870
871 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
872
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800873 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800874
875 if (edesc->dma_len)
876 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
877 DMA_BIDIRECTIONAL);
878}
879
880/*
881 * ipsec_esp descriptor callbacks
882 */
883static void ipsec_esp_encrypt_done(struct device *dev,
884 struct talitos_desc *desc, void *context,
885 int err)
886{
887 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800888 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
889 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800890 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800891 struct scatterlist *sg;
892 void *icvdata;
893
Kim Phillips19bbbc62009-03-29 15:53:59 +0800894 edesc = container_of(desc, struct talitos_edesc, desc);
895
Kim Phillips9c4a7962008-06-23 19:50:15 +0800896 ipsec_esp_unmap(dev, edesc, areq);
897
898 /* copy the generated ICV to dst */
899 if (edesc->dma_len) {
900 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800901 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800902 sg = sg_last(areq->dst, edesc->dst_nents);
903 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
904 icvdata, ctx->authsize);
905 }
906
907 kfree(edesc);
908
909 aead_request_complete(areq, err);
910}
911
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800912static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800913 struct talitos_desc *desc,
914 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800915{
916 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800917 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
918 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800919 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800920 struct scatterlist *sg;
921 void *icvdata;
922
Kim Phillips19bbbc62009-03-29 15:53:59 +0800923 edesc = container_of(desc, struct talitos_edesc, desc);
924
Kim Phillips9c4a7962008-06-23 19:50:15 +0800925 ipsec_esp_unmap(dev, edesc, req);
926
927 if (!err) {
928 /* auth check */
929 if (edesc->dma_len)
930 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800931 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800932 else
933 icvdata = &edesc->link_tbl[0];
934
935 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
936 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
937 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
938 }
939
940 kfree(edesc);
941
942 aead_request_complete(req, err);
943}
944
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800945static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800946 struct talitos_desc *desc,
947 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800948{
949 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800950 struct talitos_edesc *edesc;
951
952 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800953
954 ipsec_esp_unmap(dev, edesc, req);
955
956 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800957 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
958 DESC_HDR_LO_ICCR1_PASS))
959 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800960
961 kfree(edesc);
962
963 aead_request_complete(req, err);
964}
965
Kim Phillips9c4a7962008-06-23 19:50:15 +0800966/*
967 * convert scatterlist to SEC h/w link table format
968 * stop at cryptlen bytes
969 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800970static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800971 int cryptlen, struct talitos_ptr *link_tbl_ptr)
972{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800973 int n_sg = sg_count;
974
975 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000976 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800977 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
978 link_tbl_ptr->j_extent = 0;
979 link_tbl_ptr++;
980 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800981 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800982 }
983
Lee Nipper70bcaca2008-07-03 19:08:46 +0800984 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800985 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800986 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800987 /* Empty this entry, and move to previous one */
988 cryptlen += be16_to_cpu(link_tbl_ptr->len);
989 link_tbl_ptr->len = 0;
990 sg_count--;
991 link_tbl_ptr--;
992 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800993 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
994 + cryptlen);
995
996 /* tag end of link table */
997 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +0800998
999 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001000}
1001
1002/*
1003 * fill in and submit ipsec_esp descriptor
1004 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001005static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001006 u8 *giv, u64 seq,
1007 void (*callback) (struct device *dev,
1008 struct talitos_desc *desc,
1009 void *context, int error))
1010{
1011 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1012 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1013 struct device *dev = ctx->dev;
1014 struct talitos_desc *desc = &edesc->desc;
1015 unsigned int cryptlen = areq->cryptlen;
1016 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +10001017 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001018 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001019 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001020
1021 /* hmac key */
1022 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
1023 0, DMA_TO_DEVICE);
1024 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +10001025 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1026 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001027 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001028 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
1029 DMA_TO_DEVICE);
1030
1031 /* cipher key */
1032 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1033 (char *)&ctx->key + ctx->authkeylen, 0,
1034 DMA_TO_DEVICE);
1035
1036 /*
1037 * cipher in
1038 * map and adjust cipher len to aead request cryptlen.
1039 * extent is bytes of HMAC postpended to ciphertext,
1040 * typically 12 for ipsec
1041 */
1042 desc->ptr[4].len = cpu_to_be16(cryptlen);
1043 desc->ptr[4].j_extent = authsize;
1044
Kim Phillipse938e462009-03-29 15:53:23 +08001045 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1046 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1047 : DMA_TO_DEVICE,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001048 edesc->src_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001049
1050 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001051 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001052 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001053 sg_link_tbl_len = cryptlen;
1054
Kim Phillips962a9c92009-03-29 15:54:30 +08001055 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001056 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001057
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001058 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001059 &edesc->link_tbl[0]);
1060 if (sg_count > 1) {
1061 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001062 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001063 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1064 edesc->dma_len,
1065 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001066 } else {
1067 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001068 to_talitos_ptr(&desc->ptr[4],
1069 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001070 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001071 }
1072
1073 /* cipher out */
1074 desc->ptr[5].len = cpu_to_be16(cryptlen);
1075 desc->ptr[5].j_extent = authsize;
1076
Kim Phillipse938e462009-03-29 15:53:23 +08001077 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001078 sg_count = talitos_map_sg(dev, areq->dst,
1079 edesc->dst_nents ? : 1,
1080 DMA_FROM_DEVICE,
1081 edesc->dst_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001082
1083 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001084 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001085 } else {
1086 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001087 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001088
Kim Phillips81eb0242009-08-13 11:51:51 +10001089 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1090 (edesc->src_nents + 1) *
1091 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001092 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1093 link_tbl_ptr);
1094
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001095 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001096 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001097 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001098 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001099 link_tbl_ptr++;
1100 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1101 link_tbl_ptr->len = cpu_to_be16(authsize);
1102
1103 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001104 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1105 (edesc->src_nents + edesc->dst_nents + 2) *
1106 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001107 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1108 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1109 edesc->dma_len, DMA_BIDIRECTIONAL);
1110 }
1111
1112 /* iv out */
1113 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1114 DMA_FROM_DEVICE);
1115
Kim Phillips5228f0f2011-07-15 11:21:38 +08001116 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001117 if (ret != -EINPROGRESS) {
1118 ipsec_esp_unmap(dev, edesc, areq);
1119 kfree(edesc);
1120 }
1121 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001122}
1123
Kim Phillips9c4a7962008-06-23 19:50:15 +08001124/*
1125 * derive number of elements in scatterlist
1126 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001127static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001128{
1129 struct scatterlist *sg = sg_list;
1130 int sg_nents = 0;
1131
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001132 *chained = 0;
1133 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001134 sg_nents++;
1135 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001136 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1137 *chained = 1;
1138 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001139 }
1140
1141 return sg_nents;
1142}
1143
Lee Nipper497f2e62010-05-19 19:20:36 +10001144/**
1145 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1146 * @sgl: The SG list
1147 * @nents: Number of SG entries
1148 * @buf: Where to copy to
1149 * @buflen: The number of bytes to copy
1150 * @skip: The number of bytes to skip before copying.
1151 * Note: skip + buflen should equal SG total size.
1152 *
1153 * Returns the number of copied bytes.
1154 *
1155 **/
1156static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1157 void *buf, size_t buflen, unsigned int skip)
1158{
1159 unsigned int offset = 0;
1160 unsigned int boffset = 0;
1161 struct sg_mapping_iter miter;
1162 unsigned long flags;
1163 unsigned int sg_flags = SG_MITER_ATOMIC;
1164 size_t total_buffer = buflen + skip;
1165
1166 sg_flags |= SG_MITER_FROM_SG;
1167
1168 sg_miter_start(&miter, sgl, nents, sg_flags);
1169
1170 local_irq_save(flags);
1171
1172 while (sg_miter_next(&miter) && offset < total_buffer) {
1173 unsigned int len;
1174 unsigned int ignore;
1175
1176 if ((offset + miter.length) > skip) {
1177 if (offset < skip) {
1178 /* Copy part of this segment */
1179 ignore = skip - offset;
1180 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001181 if (boffset + len > buflen)
1182 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001183 memcpy(buf + boffset, miter.addr + ignore, len);
1184 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001185 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001186 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001187 if (boffset + len > buflen)
1188 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001189 memcpy(buf + boffset, miter.addr, len);
1190 }
1191 boffset += len;
1192 }
1193 offset += miter.length;
1194 }
1195
1196 sg_miter_stop(&miter);
1197
1198 local_irq_restore(flags);
1199 return boffset;
1200}
1201
Kim Phillips9c4a7962008-06-23 19:50:15 +08001202/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001203 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001204 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001205static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1206 struct scatterlist *src,
1207 struct scatterlist *dst,
Lee Nipper497f2e62010-05-19 19:20:36 +10001208 int hash_result,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001209 unsigned int cryptlen,
1210 unsigned int authsize,
1211 int icv_stashing,
1212 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001213{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001214 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001215 int src_nents, dst_nents, alloc_len, dma_len;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001216 int src_chained, dst_chained = 0;
1217 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001218 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001219
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001220 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1221 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001222 return ERR_PTR(-EINVAL);
1223 }
1224
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001225 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001226 src_nents = (src_nents == 1) ? 0 : src_nents;
1227
Lee Nipper497f2e62010-05-19 19:20:36 +10001228 if (hash_result) {
1229 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001230 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001231 if (dst == src) {
1232 dst_nents = src_nents;
1233 } else {
1234 dst_nents = sg_count(dst, cryptlen + authsize,
1235 &dst_chained);
1236 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1237 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001238 }
1239
1240 /*
1241 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001242 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001243 * and the ICV data itself
1244 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001245 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001246 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001247 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001248 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249 alloc_len += dma_len;
1250 } else {
1251 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001252 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001253 }
1254
Kim Phillips586725f2008-07-17 20:19:18 +08001255 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001256 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001257 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001258 return ERR_PTR(-ENOMEM);
1259 }
1260
1261 edesc->src_nents = src_nents;
1262 edesc->dst_nents = dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001263 edesc->src_is_chained = src_chained;
1264 edesc->dst_is_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001265 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001266 if (dma_len)
1267 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1268 edesc->dma_len,
1269 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001270
1271 return edesc;
1272}
1273
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001274static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1275 int icv_stashing)
1276{
1277 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1278 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1279
Lee Nipper497f2e62010-05-19 19:20:36 +10001280 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001281 areq->cryptlen, ctx->authsize, icv_stashing,
1282 areq->base.flags);
1283}
1284
Lee Nipper56af8cd2009-03-29 15:50:50 +08001285static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001286{
1287 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1288 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001289 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001290
1291 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001292 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001293 if (IS_ERR(edesc))
1294 return PTR_ERR(edesc);
1295
1296 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001297 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001298
1299 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1300}
1301
Lee Nipper56af8cd2009-03-29 15:50:50 +08001302static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001303{
1304 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1305 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1306 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001307 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001308 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001309 struct scatterlist *sg;
1310 void *icvdata;
1311
1312 req->cryptlen -= authsize;
1313
1314 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001315 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001316 if (IS_ERR(edesc))
1317 return PTR_ERR(edesc);
1318
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001319 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001320 ((!edesc->src_nents && !edesc->dst_nents) ||
1321 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001322
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001323 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001324 edesc->desc.hdr = ctx->desc_hdr_template |
1325 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001326 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001327
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001328 /* reset integrity check result bits */
1329 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001330
Kim Phillipse938e462009-03-29 15:53:23 +08001331 return ipsec_esp(edesc, req, NULL, 0,
1332 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001333
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001334 }
Kim Phillipse938e462009-03-29 15:53:23 +08001335
1336 /* Have to check the ICV with software */
1337 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1338
1339 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1340 if (edesc->dma_len)
1341 icvdata = &edesc->link_tbl[edesc->src_nents +
1342 edesc->dst_nents + 2];
1343 else
1344 icvdata = &edesc->link_tbl[0];
1345
1346 sg = sg_last(req->src, edesc->src_nents ? : 1);
1347
1348 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1349 ctx->authsize);
1350
1351 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001352}
1353
Lee Nipper56af8cd2009-03-29 15:50:50 +08001354static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001355{
1356 struct aead_request *areq = &req->areq;
1357 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1358 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001359 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001360
1361 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001362 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001363 if (IS_ERR(edesc))
1364 return PTR_ERR(edesc);
1365
1366 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001367 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001368
1369 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001370 /* avoid consecutive packets going out with same IV */
1371 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001372
1373 return ipsec_esp(edesc, areq, req->giv, req->seq,
1374 ipsec_esp_encrypt_done);
1375}
1376
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001377static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1378 const u8 *key, unsigned int keylen)
1379{
1380 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1381 struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher);
1382
1383 if (keylen > TALITOS_MAX_KEY_SIZE)
1384 goto badkey;
1385
1386 if (keylen < alg->min_keysize || keylen > alg->max_keysize)
1387 goto badkey;
1388
1389 memcpy(&ctx->key, key, keylen);
1390 ctx->keylen = keylen;
1391
1392 return 0;
1393
1394badkey:
1395 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1396 return -EINVAL;
1397}
1398
1399static void common_nonsnoop_unmap(struct device *dev,
1400 struct talitos_edesc *edesc,
1401 struct ablkcipher_request *areq)
1402{
1403 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1404 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1405 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1406
1407 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1408
1409 if (edesc->dma_len)
1410 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1411 DMA_BIDIRECTIONAL);
1412}
1413
1414static void ablkcipher_done(struct device *dev,
1415 struct talitos_desc *desc, void *context,
1416 int err)
1417{
1418 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001419 struct talitos_edesc *edesc;
1420
1421 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001422
1423 common_nonsnoop_unmap(dev, edesc, areq);
1424
1425 kfree(edesc);
1426
1427 areq->base.complete(&areq->base, err);
1428}
1429
1430static int common_nonsnoop(struct talitos_edesc *edesc,
1431 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001432 void (*callback) (struct device *dev,
1433 struct talitos_desc *desc,
1434 void *context, int error))
1435{
1436 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1437 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1438 struct device *dev = ctx->dev;
1439 struct talitos_desc *desc = &edesc->desc;
1440 unsigned int cryptlen = areq->nbytes;
1441 unsigned int ivsize;
1442 int sg_count, ret;
1443
1444 /* first DWORD empty */
1445 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001446 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001447 desc->ptr[0].j_extent = 0;
1448
1449 /* cipher iv */
1450 ivsize = crypto_ablkcipher_ivsize(cipher);
Kim Phillipsfebec542011-07-15 11:21:39 +08001451 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001452 DMA_TO_DEVICE);
1453
1454 /* cipher key */
1455 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1456 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1457
1458 /*
1459 * cipher in
1460 */
1461 desc->ptr[3].len = cpu_to_be16(cryptlen);
1462 desc->ptr[3].j_extent = 0;
1463
1464 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1465 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1466 : DMA_TO_DEVICE,
1467 edesc->src_is_chained);
1468
1469 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001470 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001471 } else {
1472 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1473 &edesc->link_tbl[0]);
1474 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001475 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001476 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001477 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1478 edesc->dma_len,
1479 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001480 } else {
1481 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001482 to_talitos_ptr(&desc->ptr[3],
1483 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001484 }
1485 }
1486
1487 /* cipher out */
1488 desc->ptr[4].len = cpu_to_be16(cryptlen);
1489 desc->ptr[4].j_extent = 0;
1490
1491 if (areq->src != areq->dst)
1492 sg_count = talitos_map_sg(dev, areq->dst,
1493 edesc->dst_nents ? : 1,
1494 DMA_FROM_DEVICE,
1495 edesc->dst_is_chained);
1496
1497 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001498 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001499 } else {
1500 struct talitos_ptr *link_tbl_ptr =
1501 &edesc->link_tbl[edesc->src_nents + 1];
1502
Kim Phillips81eb0242009-08-13 11:51:51 +10001503 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1504 (edesc->src_nents + 1) *
1505 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001506 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001507 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1508 link_tbl_ptr);
1509 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1510 edesc->dma_len, DMA_BIDIRECTIONAL);
1511 }
1512
1513 /* iv out */
1514 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1515 DMA_FROM_DEVICE);
1516
1517 /* last DWORD empty */
1518 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001519 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001520 desc->ptr[6].j_extent = 0;
1521
Kim Phillips5228f0f2011-07-15 11:21:38 +08001522 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001523 if (ret != -EINPROGRESS) {
1524 common_nonsnoop_unmap(dev, edesc, areq);
1525 kfree(edesc);
1526 }
1527 return ret;
1528}
1529
Kim Phillipse938e462009-03-29 15:53:23 +08001530static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1531 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001532{
1533 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1534 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1535
Lee Nipper497f2e62010-05-19 19:20:36 +10001536 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1537 areq->nbytes, 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001538}
1539
1540static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1541{
1542 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1543 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1544 struct talitos_edesc *edesc;
1545
1546 /* allocate extended descriptor */
1547 edesc = ablkcipher_edesc_alloc(areq);
1548 if (IS_ERR(edesc))
1549 return PTR_ERR(edesc);
1550
1551 /* set encrypt */
1552 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1553
Kim Phillipsfebec542011-07-15 11:21:39 +08001554 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001555}
1556
1557static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1558{
1559 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1560 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1561 struct talitos_edesc *edesc;
1562
1563 /* allocate extended descriptor */
1564 edesc = ablkcipher_edesc_alloc(areq);
1565 if (IS_ERR(edesc))
1566 return PTR_ERR(edesc);
1567
1568 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1569
Kim Phillipsfebec542011-07-15 11:21:39 +08001570 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001571}
1572
Lee Nipper497f2e62010-05-19 19:20:36 +10001573static void common_nonsnoop_hash_unmap(struct device *dev,
1574 struct talitos_edesc *edesc,
1575 struct ahash_request *areq)
1576{
1577 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1578
1579 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1580
1581 /* When using hashctx-in, must unmap it. */
1582 if (edesc->desc.ptr[1].len)
1583 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1584 DMA_TO_DEVICE);
1585
1586 if (edesc->desc.ptr[2].len)
1587 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1588 DMA_TO_DEVICE);
1589
1590 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1591
1592 if (edesc->dma_len)
1593 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1594 DMA_BIDIRECTIONAL);
1595
1596}
1597
1598static void ahash_done(struct device *dev,
1599 struct talitos_desc *desc, void *context,
1600 int err)
1601{
1602 struct ahash_request *areq = context;
1603 struct talitos_edesc *edesc =
1604 container_of(desc, struct talitos_edesc, desc);
1605 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1606
1607 if (!req_ctx->last && req_ctx->to_hash_later) {
1608 /* Position any partial block for next update/final/finup */
1609 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001610 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001611 }
1612 common_nonsnoop_hash_unmap(dev, edesc, areq);
1613
1614 kfree(edesc);
1615
1616 areq->base.complete(&areq->base, err);
1617}
1618
1619static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1620 struct ahash_request *areq, unsigned int length,
1621 void (*callback) (struct device *dev,
1622 struct talitos_desc *desc,
1623 void *context, int error))
1624{
1625 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1626 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1627 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1628 struct device *dev = ctx->dev;
1629 struct talitos_desc *desc = &edesc->desc;
1630 int sg_count, ret;
1631
1632 /* first DWORD empty */
1633 desc->ptr[0] = zero_entry;
1634
Kim Phillips60f208d2010-05-19 19:21:53 +10001635 /* hash context in */
1636 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001637 map_single_talitos_ptr(dev, &desc->ptr[1],
1638 req_ctx->hw_context_size,
1639 (char *)req_ctx->hw_context, 0,
1640 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001641 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001642 } else {
1643 desc->ptr[1] = zero_entry;
1644 /* Indicate next op is not the first. */
1645 req_ctx->first = 0;
1646 }
1647
1648 /* HMAC key */
1649 if (ctx->keylen)
1650 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1651 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1652 else
1653 desc->ptr[2] = zero_entry;
1654
1655 /*
1656 * data in
1657 */
1658 desc->ptr[3].len = cpu_to_be16(length);
1659 desc->ptr[3].j_extent = 0;
1660
1661 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1662 edesc->src_nents ? : 1,
1663 DMA_TO_DEVICE,
1664 edesc->src_is_chained);
1665
1666 if (sg_count == 1) {
1667 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1668 } else {
1669 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1670 &edesc->link_tbl[0]);
1671 if (sg_count > 1) {
1672 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1673 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1674 dma_sync_single_for_device(ctx->dev,
1675 edesc->dma_link_tbl,
1676 edesc->dma_len,
1677 DMA_BIDIRECTIONAL);
1678 } else {
1679 /* Only one segment now, so no link tbl needed */
1680 to_talitos_ptr(&desc->ptr[3],
1681 sg_dma_address(req_ctx->psrc));
1682 }
1683 }
1684
1685 /* fifth DWORD empty */
1686 desc->ptr[4] = zero_entry;
1687
1688 /* hash/HMAC out -or- hash context out */
1689 if (req_ctx->last)
1690 map_single_talitos_ptr(dev, &desc->ptr[5],
1691 crypto_ahash_digestsize(tfm),
1692 areq->result, 0, DMA_FROM_DEVICE);
1693 else
1694 map_single_talitos_ptr(dev, &desc->ptr[5],
1695 req_ctx->hw_context_size,
1696 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1697
1698 /* last DWORD empty */
1699 desc->ptr[6] = zero_entry;
1700
Kim Phillips5228f0f2011-07-15 11:21:38 +08001701 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001702 if (ret != -EINPROGRESS) {
1703 common_nonsnoop_hash_unmap(dev, edesc, areq);
1704 kfree(edesc);
1705 }
1706 return ret;
1707}
1708
1709static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1710 unsigned int nbytes)
1711{
1712 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1713 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1714 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1715
1716 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1717 nbytes, 0, 0, areq->base.flags);
1718}
1719
1720static int ahash_init(struct ahash_request *areq)
1721{
1722 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1723 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1724
1725 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001726 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001727 req_ctx->first = 1; /* first indicates h/w must init its context */
1728 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001729 req_ctx->hw_context_size =
1730 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1731 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1732 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1733
1734 return 0;
1735}
1736
Kim Phillips60f208d2010-05-19 19:21:53 +10001737/*
1738 * on h/w without explicit sha224 support, we initialize h/w context
1739 * manually with sha224 constants, and tell it to run sha256.
1740 */
1741static int ahash_init_sha224_swinit(struct ahash_request *areq)
1742{
1743 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1744
1745 ahash_init(areq);
1746 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1747
Kim Phillipsa7524472010-09-23 15:56:38 +08001748 req_ctx->hw_context[0] = SHA224_H0;
1749 req_ctx->hw_context[1] = SHA224_H1;
1750 req_ctx->hw_context[2] = SHA224_H2;
1751 req_ctx->hw_context[3] = SHA224_H3;
1752 req_ctx->hw_context[4] = SHA224_H4;
1753 req_ctx->hw_context[5] = SHA224_H5;
1754 req_ctx->hw_context[6] = SHA224_H6;
1755 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001756
1757 /* init 64-bit count */
1758 req_ctx->hw_context[8] = 0;
1759 req_ctx->hw_context[9] = 0;
1760
1761 return 0;
1762}
1763
Lee Nipper497f2e62010-05-19 19:20:36 +10001764static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1765{
1766 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1767 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1768 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1769 struct talitos_edesc *edesc;
1770 unsigned int blocksize =
1771 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1772 unsigned int nbytes_to_hash;
1773 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001774 unsigned int nsg;
Lee Nipper497f2e62010-05-19 19:20:36 +10001775 int chained;
1776
Lee Nipper5e833bc2010-06-16 15:29:15 +10001777 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1778 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001779 sg_copy_to_buffer(areq->src,
1780 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001781 req_ctx->buf + req_ctx->nbuf, nbytes);
1782 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001783 return 0;
1784 }
1785
Lee Nipper5e833bc2010-06-16 15:29:15 +10001786 /* At least (blocksize + 1) bytes are available to hash */
1787 nbytes_to_hash = nbytes + req_ctx->nbuf;
1788 to_hash_later = nbytes_to_hash & (blocksize - 1);
1789
1790 if (req_ctx->last)
1791 to_hash_later = 0;
1792 else if (to_hash_later)
1793 /* There is a partial block. Hash the full block(s) now */
1794 nbytes_to_hash -= to_hash_later;
1795 else {
1796 /* Keep one block buffered */
1797 nbytes_to_hash -= blocksize;
1798 to_hash_later = blocksize;
1799 }
1800
1801 /* Chain in any previously buffered data */
1802 if (req_ctx->nbuf) {
1803 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1804 sg_init_table(req_ctx->bufsl, nsg);
1805 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1806 if (nsg > 1)
1807 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001808 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001809 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001810 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001811
Lee Nipper5e833bc2010-06-16 15:29:15 +10001812 if (to_hash_later) {
1813 int nents = sg_count(areq->src, nbytes, &chained);
1814 sg_copy_end_to_buffer(areq->src, nents,
1815 req_ctx->bufnext,
1816 to_hash_later,
1817 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001818 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001819 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001820
Lee Nipper5e833bc2010-06-16 15:29:15 +10001821 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001822 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1823 if (IS_ERR(edesc))
1824 return PTR_ERR(edesc);
1825
1826 edesc->desc.hdr = ctx->desc_hdr_template;
1827
1828 /* On last one, request SEC to pad; otherwise continue */
1829 if (req_ctx->last)
1830 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1831 else
1832 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1833
Kim Phillips60f208d2010-05-19 19:21:53 +10001834 /* request SEC to INIT hash. */
1835 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001836 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1837
1838 /* When the tfm context has a keylen, it's an HMAC.
1839 * A first or last (ie. not middle) descriptor must request HMAC.
1840 */
1841 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1842 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1843
1844 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1845 ahash_done);
1846}
1847
1848static int ahash_update(struct ahash_request *areq)
1849{
1850 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1851
1852 req_ctx->last = 0;
1853
1854 return ahash_process_req(areq, areq->nbytes);
1855}
1856
1857static int ahash_final(struct ahash_request *areq)
1858{
1859 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1860
1861 req_ctx->last = 1;
1862
1863 return ahash_process_req(areq, 0);
1864}
1865
1866static int ahash_finup(struct ahash_request *areq)
1867{
1868 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1869
1870 req_ctx->last = 1;
1871
1872 return ahash_process_req(areq, areq->nbytes);
1873}
1874
1875static int ahash_digest(struct ahash_request *areq)
1876{
1877 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001878 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001879
Kim Phillips60f208d2010-05-19 19:21:53 +10001880 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001881 req_ctx->last = 1;
1882
1883 return ahash_process_req(areq, areq->nbytes);
1884}
1885
Kim Phillips9c4a7962008-06-23 19:50:15 +08001886struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001887 u32 type;
1888 union {
1889 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001890 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001891 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001892 __be32 desc_hdr_template;
1893};
1894
1895static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001896 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001897 { .type = CRYPTO_ALG_TYPE_AEAD,
1898 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001899 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1900 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1901 .cra_blocksize = AES_BLOCK_SIZE,
1902 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1903 .cra_type = &crypto_aead_type,
1904 .cra_aead = {
1905 .setkey = aead_setkey,
1906 .setauthsize = aead_setauthsize,
1907 .encrypt = aead_encrypt,
1908 .decrypt = aead_decrypt,
1909 .givencrypt = aead_givencrypt,
1910 .geniv = "<built-in>",
1911 .ivsize = AES_BLOCK_SIZE,
1912 .maxauthsize = SHA1_DIGEST_SIZE,
1913 }
1914 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001915 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1916 DESC_HDR_SEL0_AESU |
1917 DESC_HDR_MODE0_AESU_CBC |
1918 DESC_HDR_SEL1_MDEUA |
1919 DESC_HDR_MODE1_MDEU_INIT |
1920 DESC_HDR_MODE1_MDEU_PAD |
1921 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001922 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001923 { .type = CRYPTO_ALG_TYPE_AEAD,
1924 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001925 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1926 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1927 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1928 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1929 .cra_type = &crypto_aead_type,
1930 .cra_aead = {
1931 .setkey = aead_setkey,
1932 .setauthsize = aead_setauthsize,
1933 .encrypt = aead_encrypt,
1934 .decrypt = aead_decrypt,
1935 .givencrypt = aead_givencrypt,
1936 .geniv = "<built-in>",
1937 .ivsize = DES3_EDE_BLOCK_SIZE,
1938 .maxauthsize = SHA1_DIGEST_SIZE,
1939 }
1940 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001941 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1942 DESC_HDR_SEL0_DEU |
1943 DESC_HDR_MODE0_DEU_CBC |
1944 DESC_HDR_MODE0_DEU_3DES |
1945 DESC_HDR_SEL1_MDEUA |
1946 DESC_HDR_MODE1_MDEU_INIT |
1947 DESC_HDR_MODE1_MDEU_PAD |
1948 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001949 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001950 { .type = CRYPTO_ALG_TYPE_AEAD,
1951 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001952 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1953 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1954 .cra_blocksize = AES_BLOCK_SIZE,
1955 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1956 .cra_type = &crypto_aead_type,
1957 .cra_aead = {
1958 .setkey = aead_setkey,
1959 .setauthsize = aead_setauthsize,
1960 .encrypt = aead_encrypt,
1961 .decrypt = aead_decrypt,
1962 .givencrypt = aead_givencrypt,
1963 .geniv = "<built-in>",
1964 .ivsize = AES_BLOCK_SIZE,
1965 .maxauthsize = SHA256_DIGEST_SIZE,
1966 }
1967 },
Lee Nipper3952f172008-07-10 18:29:18 +08001968 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1969 DESC_HDR_SEL0_AESU |
1970 DESC_HDR_MODE0_AESU_CBC |
1971 DESC_HDR_SEL1_MDEUA |
1972 DESC_HDR_MODE1_MDEU_INIT |
1973 DESC_HDR_MODE1_MDEU_PAD |
1974 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
1975 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001976 { .type = CRYPTO_ALG_TYPE_AEAD,
1977 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001978 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
1979 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
1980 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1981 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1982 .cra_type = &crypto_aead_type,
1983 .cra_aead = {
1984 .setkey = aead_setkey,
1985 .setauthsize = aead_setauthsize,
1986 .encrypt = aead_encrypt,
1987 .decrypt = aead_decrypt,
1988 .givencrypt = aead_givencrypt,
1989 .geniv = "<built-in>",
1990 .ivsize = DES3_EDE_BLOCK_SIZE,
1991 .maxauthsize = SHA256_DIGEST_SIZE,
1992 }
1993 },
Lee Nipper3952f172008-07-10 18:29:18 +08001994 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1995 DESC_HDR_SEL0_DEU |
1996 DESC_HDR_MODE0_DEU_CBC |
1997 DESC_HDR_MODE0_DEU_3DES |
1998 DESC_HDR_SEL1_MDEUA |
1999 DESC_HDR_MODE1_MDEU_INIT |
2000 DESC_HDR_MODE1_MDEU_PAD |
2001 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2002 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002003 { .type = CRYPTO_ALG_TYPE_AEAD,
2004 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002005 .cra_name = "authenc(hmac(md5),cbc(aes))",
2006 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2007 .cra_blocksize = AES_BLOCK_SIZE,
2008 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2009 .cra_type = &crypto_aead_type,
2010 .cra_aead = {
2011 .setkey = aead_setkey,
2012 .setauthsize = aead_setauthsize,
2013 .encrypt = aead_encrypt,
2014 .decrypt = aead_decrypt,
2015 .givencrypt = aead_givencrypt,
2016 .geniv = "<built-in>",
2017 .ivsize = AES_BLOCK_SIZE,
2018 .maxauthsize = MD5_DIGEST_SIZE,
2019 }
2020 },
Lee Nipper3952f172008-07-10 18:29:18 +08002021 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2022 DESC_HDR_SEL0_AESU |
2023 DESC_HDR_MODE0_AESU_CBC |
2024 DESC_HDR_SEL1_MDEUA |
2025 DESC_HDR_MODE1_MDEU_INIT |
2026 DESC_HDR_MODE1_MDEU_PAD |
2027 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2028 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002029 { .type = CRYPTO_ALG_TYPE_AEAD,
2030 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002031 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2032 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2033 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2034 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2035 .cra_type = &crypto_aead_type,
2036 .cra_aead = {
2037 .setkey = aead_setkey,
2038 .setauthsize = aead_setauthsize,
2039 .encrypt = aead_encrypt,
2040 .decrypt = aead_decrypt,
2041 .givencrypt = aead_givencrypt,
2042 .geniv = "<built-in>",
2043 .ivsize = DES3_EDE_BLOCK_SIZE,
2044 .maxauthsize = MD5_DIGEST_SIZE,
2045 }
2046 },
Lee Nipper3952f172008-07-10 18:29:18 +08002047 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2048 DESC_HDR_SEL0_DEU |
2049 DESC_HDR_MODE0_DEU_CBC |
2050 DESC_HDR_MODE0_DEU_3DES |
2051 DESC_HDR_SEL1_MDEUA |
2052 DESC_HDR_MODE1_MDEU_INIT |
2053 DESC_HDR_MODE1_MDEU_PAD |
2054 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002055 },
2056 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002057 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2058 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002059 .cra_name = "cbc(aes)",
2060 .cra_driver_name = "cbc-aes-talitos",
2061 .cra_blocksize = AES_BLOCK_SIZE,
2062 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2063 CRYPTO_ALG_ASYNC,
2064 .cra_type = &crypto_ablkcipher_type,
2065 .cra_ablkcipher = {
2066 .setkey = ablkcipher_setkey,
2067 .encrypt = ablkcipher_encrypt,
2068 .decrypt = ablkcipher_decrypt,
2069 .geniv = "eseqiv",
2070 .min_keysize = AES_MIN_KEY_SIZE,
2071 .max_keysize = AES_MAX_KEY_SIZE,
2072 .ivsize = AES_BLOCK_SIZE,
2073 }
2074 },
2075 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2076 DESC_HDR_SEL0_AESU |
2077 DESC_HDR_MODE0_AESU_CBC,
2078 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002079 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2080 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002081 .cra_name = "cbc(des3_ede)",
2082 .cra_driver_name = "cbc-3des-talitos",
2083 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2084 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2085 CRYPTO_ALG_ASYNC,
2086 .cra_type = &crypto_ablkcipher_type,
2087 .cra_ablkcipher = {
2088 .setkey = ablkcipher_setkey,
2089 .encrypt = ablkcipher_encrypt,
2090 .decrypt = ablkcipher_decrypt,
2091 .geniv = "eseqiv",
2092 .min_keysize = DES3_EDE_KEY_SIZE,
2093 .max_keysize = DES3_EDE_KEY_SIZE,
2094 .ivsize = DES3_EDE_BLOCK_SIZE,
2095 }
2096 },
2097 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2098 DESC_HDR_SEL0_DEU |
2099 DESC_HDR_MODE0_DEU_CBC |
2100 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002101 },
2102 /* AHASH algorithms. */
2103 { .type = CRYPTO_ALG_TYPE_AHASH,
2104 .alg.hash = {
2105 .init = ahash_init,
2106 .update = ahash_update,
2107 .final = ahash_final,
2108 .finup = ahash_finup,
2109 .digest = ahash_digest,
2110 .halg.digestsize = MD5_DIGEST_SIZE,
2111 .halg.base = {
2112 .cra_name = "md5",
2113 .cra_driver_name = "md5-talitos",
2114 .cra_blocksize = MD5_BLOCK_SIZE,
2115 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2116 CRYPTO_ALG_ASYNC,
2117 .cra_type = &crypto_ahash_type
2118 }
2119 },
2120 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2121 DESC_HDR_SEL0_MDEUA |
2122 DESC_HDR_MODE0_MDEU_MD5,
2123 },
2124 { .type = CRYPTO_ALG_TYPE_AHASH,
2125 .alg.hash = {
2126 .init = ahash_init,
2127 .update = ahash_update,
2128 .final = ahash_final,
2129 .finup = ahash_finup,
2130 .digest = ahash_digest,
2131 .halg.digestsize = SHA1_DIGEST_SIZE,
2132 .halg.base = {
2133 .cra_name = "sha1",
2134 .cra_driver_name = "sha1-talitos",
2135 .cra_blocksize = SHA1_BLOCK_SIZE,
2136 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2137 CRYPTO_ALG_ASYNC,
2138 .cra_type = &crypto_ahash_type
2139 }
2140 },
2141 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2142 DESC_HDR_SEL0_MDEUA |
2143 DESC_HDR_MODE0_MDEU_SHA1,
2144 },
2145 { .type = CRYPTO_ALG_TYPE_AHASH,
2146 .alg.hash = {
2147 .init = ahash_init,
2148 .update = ahash_update,
2149 .final = ahash_final,
2150 .finup = ahash_finup,
2151 .digest = ahash_digest,
Kim Phillips60f208d2010-05-19 19:21:53 +10002152 .halg.digestsize = SHA224_DIGEST_SIZE,
2153 .halg.base = {
2154 .cra_name = "sha224",
2155 .cra_driver_name = "sha224-talitos",
2156 .cra_blocksize = SHA224_BLOCK_SIZE,
2157 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2158 CRYPTO_ALG_ASYNC,
2159 .cra_type = &crypto_ahash_type
2160 }
2161 },
2162 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2163 DESC_HDR_SEL0_MDEUA |
2164 DESC_HDR_MODE0_MDEU_SHA224,
2165 },
2166 { .type = CRYPTO_ALG_TYPE_AHASH,
2167 .alg.hash = {
2168 .init = ahash_init,
2169 .update = ahash_update,
2170 .final = ahash_final,
2171 .finup = ahash_finup,
2172 .digest = ahash_digest,
Lee Nipper497f2e62010-05-19 19:20:36 +10002173 .halg.digestsize = SHA256_DIGEST_SIZE,
2174 .halg.base = {
2175 .cra_name = "sha256",
2176 .cra_driver_name = "sha256-talitos",
2177 .cra_blocksize = SHA256_BLOCK_SIZE,
2178 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2179 CRYPTO_ALG_ASYNC,
2180 .cra_type = &crypto_ahash_type
2181 }
2182 },
2183 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2184 DESC_HDR_SEL0_MDEUA |
2185 DESC_HDR_MODE0_MDEU_SHA256,
2186 },
2187 { .type = CRYPTO_ALG_TYPE_AHASH,
2188 .alg.hash = {
2189 .init = ahash_init,
2190 .update = ahash_update,
2191 .final = ahash_final,
2192 .finup = ahash_finup,
2193 .digest = ahash_digest,
2194 .halg.digestsize = SHA384_DIGEST_SIZE,
2195 .halg.base = {
2196 .cra_name = "sha384",
2197 .cra_driver_name = "sha384-talitos",
2198 .cra_blocksize = SHA384_BLOCK_SIZE,
2199 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2200 CRYPTO_ALG_ASYNC,
2201 .cra_type = &crypto_ahash_type
2202 }
2203 },
2204 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2205 DESC_HDR_SEL0_MDEUB |
2206 DESC_HDR_MODE0_MDEUB_SHA384,
2207 },
2208 { .type = CRYPTO_ALG_TYPE_AHASH,
2209 .alg.hash = {
2210 .init = ahash_init,
2211 .update = ahash_update,
2212 .final = ahash_final,
2213 .finup = ahash_finup,
2214 .digest = ahash_digest,
2215 .halg.digestsize = SHA512_DIGEST_SIZE,
2216 .halg.base = {
2217 .cra_name = "sha512",
2218 .cra_driver_name = "sha512-talitos",
2219 .cra_blocksize = SHA512_BLOCK_SIZE,
2220 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2221 CRYPTO_ALG_ASYNC,
2222 .cra_type = &crypto_ahash_type
2223 }
2224 },
2225 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2226 DESC_HDR_SEL0_MDEUB |
2227 DESC_HDR_MODE0_MDEUB_SHA512,
2228 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002229};
2230
2231struct talitos_crypto_alg {
2232 struct list_head entry;
2233 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002234 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002235};
2236
2237static int talitos_cra_init(struct crypto_tfm *tfm)
2238{
2239 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002240 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002241 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002242 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002243
Lee Nipper497f2e62010-05-19 19:20:36 +10002244 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2245 talitos_alg = container_of(__crypto_ahash_alg(alg),
2246 struct talitos_crypto_alg,
2247 algt.alg.hash);
2248 else
2249 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2250 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002251
Kim Phillips9c4a7962008-06-23 19:50:15 +08002252 /* update context with ptr to dev */
2253 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002254
Kim Phillips5228f0f2011-07-15 11:21:38 +08002255 /* assign SEC channel to tfm in round-robin fashion */
2256 priv = dev_get_drvdata(ctx->dev);
2257 ctx->ch = atomic_inc_return(&priv->last_chan) &
2258 (priv->num_channels - 1);
2259
Kim Phillips9c4a7962008-06-23 19:50:15 +08002260 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002261 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002262
Kim Phillips602dba52011-07-15 11:21:39 +08002263 /* select done notification */
2264 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2265
Lee Nipper497f2e62010-05-19 19:20:36 +10002266 return 0;
2267}
2268
2269static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2270{
2271 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2272
2273 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002274
2275 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002276 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002277
2278 return 0;
2279}
2280
Lee Nipper497f2e62010-05-19 19:20:36 +10002281static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2282{
2283 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2284
2285 talitos_cra_init(tfm);
2286
2287 ctx->keylen = 0;
2288 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2289 sizeof(struct talitos_ahash_req_ctx));
2290
2291 return 0;
2292}
2293
Kim Phillips9c4a7962008-06-23 19:50:15 +08002294/*
2295 * given the alg's descriptor header template, determine whether descriptor
2296 * type and primary/secondary execution units required match the hw
2297 * capabilities description provided in the device tree node.
2298 */
2299static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2300{
2301 struct talitos_private *priv = dev_get_drvdata(dev);
2302 int ret;
2303
2304 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2305 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2306
2307 if (SECONDARY_EU(desc_hdr_template))
2308 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2309 & priv->exec_units);
2310
2311 return ret;
2312}
2313
Grant Likely2dc11582010-08-06 09:25:50 -06002314static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002315{
2316 struct device *dev = &ofdev->dev;
2317 struct talitos_private *priv = dev_get_drvdata(dev);
2318 struct talitos_crypto_alg *t_alg, *n;
2319 int i;
2320
2321 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002322 switch (t_alg->algt.type) {
2323 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2324 case CRYPTO_ALG_TYPE_AEAD:
2325 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2326 break;
2327 case CRYPTO_ALG_TYPE_AHASH:
2328 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2329 break;
2330 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002331 list_del(&t_alg->entry);
2332 kfree(t_alg);
2333 }
2334
2335 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2336 talitos_unregister_rng(dev);
2337
Kim Phillips4b9926282009-08-13 11:50:38 +10002338 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002339 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002340
Kim Phillips4b9926282009-08-13 11:50:38 +10002341 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002342
2343 if (priv->irq != NO_IRQ) {
2344 free_irq(priv->irq, dev);
2345 irq_dispose_mapping(priv->irq);
2346 }
2347
2348 tasklet_kill(&priv->done_task);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002349
2350 iounmap(priv->reg);
2351
2352 dev_set_drvdata(dev, NULL);
2353
2354 kfree(priv);
2355
2356 return 0;
2357}
2358
2359static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2360 struct talitos_alg_template
2361 *template)
2362{
Kim Phillips60f208d2010-05-19 19:21:53 +10002363 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002364 struct talitos_crypto_alg *t_alg;
2365 struct crypto_alg *alg;
2366
2367 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2368 if (!t_alg)
2369 return ERR_PTR(-ENOMEM);
2370
Lee Nipperacbf7c622010-05-19 19:19:33 +10002371 t_alg->algt = *template;
2372
2373 switch (t_alg->algt.type) {
2374 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002375 alg = &t_alg->algt.alg.crypto;
2376 alg->cra_init = talitos_cra_init;
2377 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002378 case CRYPTO_ALG_TYPE_AEAD:
2379 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002380 alg->cra_init = talitos_cra_init_aead;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002381 break;
2382 case CRYPTO_ALG_TYPE_AHASH:
2383 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002384 alg->cra_init = talitos_cra_init_ahash;
Kim Phillips60f208d2010-05-19 19:21:53 +10002385 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
2386 !strcmp(alg->cra_name, "sha224")) {
2387 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2388 t_alg->algt.desc_hdr_template =
2389 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2390 DESC_HDR_SEL0_MDEUA |
2391 DESC_HDR_MODE0_MDEU_SHA256;
2392 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002393 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002394 default:
2395 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2396 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002397 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002398
Kim Phillips9c4a7962008-06-23 19:50:15 +08002399 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002400 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002401 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002402 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002403
Kim Phillips9c4a7962008-06-23 19:50:15 +08002404 t_alg->dev = dev;
2405
2406 return t_alg;
2407}
2408
Grant Likely1c48a5c2011-02-17 02:43:24 -07002409static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002410{
2411 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002412 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002413 struct talitos_private *priv;
2414 const unsigned int *prop;
2415 int i, err;
2416
2417 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2418 if (!priv)
2419 return -ENOMEM;
2420
2421 dev_set_drvdata(dev, priv);
2422
2423 priv->ofdev = ofdev;
2424
2425 tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002426
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002427 INIT_LIST_HEAD(&priv->alg_list);
2428
Kim Phillips9c4a7962008-06-23 19:50:15 +08002429 priv->irq = irq_of_parse_and_map(np, 0);
2430
2431 if (priv->irq == NO_IRQ) {
2432 dev_err(dev, "failed to map irq\n");
2433 err = -EINVAL;
2434 goto err_out;
2435 }
2436
2437 /* get the irq line */
2438 err = request_irq(priv->irq, talitos_interrupt, 0,
2439 dev_driver_string(dev), dev);
2440 if (err) {
2441 dev_err(dev, "failed to request irq %d\n", priv->irq);
2442 irq_dispose_mapping(priv->irq);
2443 priv->irq = NO_IRQ;
2444 goto err_out;
2445 }
2446
2447 priv->reg = of_iomap(np, 0);
2448 if (!priv->reg) {
2449 dev_err(dev, "failed to of_iomap\n");
2450 err = -ENOMEM;
2451 goto err_out;
2452 }
2453
2454 /* get SEC version capabilities from device tree */
2455 prop = of_get_property(np, "fsl,num-channels", NULL);
2456 if (prop)
2457 priv->num_channels = *prop;
2458
2459 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2460 if (prop)
2461 priv->chfifo_len = *prop;
2462
2463 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2464 if (prop)
2465 priv->exec_units = *prop;
2466
2467 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2468 if (prop)
2469 priv->desc_types = *prop;
2470
2471 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2472 !priv->exec_units || !priv->desc_types) {
2473 dev_err(dev, "invalid property data in device tree node\n");
2474 err = -EINVAL;
2475 goto err_out;
2476 }
2477
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002478 if (of_device_is_compatible(np, "fsl,sec3.0"))
2479 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2480
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002481 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002482 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
2483 TALITOS_FTR_SHA224_HWINIT;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002484
Kim Phillips4b9926282009-08-13 11:50:38 +10002485 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2486 priv->num_channels, GFP_KERNEL);
2487 if (!priv->chan) {
2488 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002489 err = -ENOMEM;
2490 goto err_out;
2491 }
2492
2493 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002494 spin_lock_init(&priv->chan[i].head_lock);
2495 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002496 }
2497
2498 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2499
2500 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002501 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2502 priv->fifo_len, GFP_KERNEL);
2503 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002504 dev_err(dev, "failed to allocate request fifo %d\n", i);
2505 err = -ENOMEM;
2506 goto err_out;
2507 }
2508 }
2509
Kim Phillipsec6644d2008-07-17 20:16:40 +08002510 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002511 atomic_set(&priv->chan[i].submit_count,
2512 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002513
Kim Phillips81eb0242009-08-13 11:51:51 +10002514 dma_set_mask(dev, DMA_BIT_MASK(36));
2515
Kim Phillips9c4a7962008-06-23 19:50:15 +08002516 /* reset and initialize the h/w */
2517 err = init_device(dev);
2518 if (err) {
2519 dev_err(dev, "failed to initialize device\n");
2520 goto err_out;
2521 }
2522
2523 /* register the RNG, if available */
2524 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2525 err = talitos_register_rng(dev);
2526 if (err) {
2527 dev_err(dev, "failed to register hwrng: %d\n", err);
2528 goto err_out;
2529 } else
2530 dev_info(dev, "hwrng\n");
2531 }
2532
2533 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002534 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2535 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2536 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002537 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002538
2539 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2540 if (IS_ERR(t_alg)) {
2541 err = PTR_ERR(t_alg);
2542 goto err_out;
2543 }
2544
Lee Nipperacbf7c622010-05-19 19:19:33 +10002545 switch (t_alg->algt.type) {
2546 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2547 case CRYPTO_ALG_TYPE_AEAD:
2548 err = crypto_register_alg(
2549 &t_alg->algt.alg.crypto);
2550 name = t_alg->algt.alg.crypto.cra_driver_name;
2551 break;
2552 case CRYPTO_ALG_TYPE_AHASH:
2553 err = crypto_register_ahash(
2554 &t_alg->algt.alg.hash);
2555 name =
2556 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2557 break;
2558 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002559 if (err) {
2560 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002561 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002562 kfree(t_alg);
2563 } else {
2564 list_add_tail(&t_alg->entry, &priv->alg_list);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002565 dev_info(dev, "%s\n", name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002566 }
2567 }
2568 }
2569
2570 return 0;
2571
2572err_out:
2573 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002574
2575 return err;
2576}
2577
Márton Németh6c3f9752010-01-17 21:54:01 +11002578static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002579 {
2580 .compatible = "fsl,sec2.0",
2581 },
2582 {},
2583};
2584MODULE_DEVICE_TABLE(of, talitos_match);
2585
Grant Likely1c48a5c2011-02-17 02:43:24 -07002586static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002587 .driver = {
2588 .name = "talitos",
2589 .owner = THIS_MODULE,
2590 .of_match_table = talitos_match,
2591 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002592 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002593 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002594};
2595
2596static int __init talitos_init(void)
2597{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002598 return platform_driver_register(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002599}
2600module_init(talitos_init);
2601
2602static void __exit talitos_exit(void)
2603{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002604 platform_driver_unregister(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002605}
2606module_exit(talitos_exit);
2607
2608MODULE_LICENSE("GPL");
2609MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2610MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");