blob: bd78acf3c365aa1910a467f3fce882fa26c84841 [file] [log] [blame]
Kim Phillips9c4a7962008-06-23 19:50:15 +08001/*
2 * talitos - Freescale Integrated Security Engine (SEC) device driver
3 *
Kim Phillips60f208d2010-05-19 19:21:53 +10004 * Copyright (c) 2008-2010 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;
121 struct of_device *ofdev;
122 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));
164 talitos_ptr->eptr = cpu_to_be32(upper_32_bits(dma_addr));
165}
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
285 * @desc: the descriptor to be processed by the device
286 * @callback: whom to call when processing is complete
287 * @context: a handle for use by caller (optional)
288 *
289 * desc must contain valid dma-mapped (bus physical) address pointers.
290 * callback must check err and feedback in descriptor header
291 * for device processing status.
292 */
293static int talitos_submit(struct device *dev, struct talitos_desc *desc,
294 void (*callback)(struct device *dev,
295 struct talitos_desc *desc,
296 void *context, int error),
297 void *context)
298{
299 struct talitos_private *priv = dev_get_drvdata(dev);
300 struct talitos_request *request;
301 unsigned long flags, ch;
302 int head;
303
304 /* select done notification */
305 desc->hdr |= DESC_HDR_DONE_NOTIFY;
306
307 /* emulate SEC's round-robin channel fifo polling scheme */
308 ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
309
Kim Phillips4b9926282009-08-13 11:50:38 +1000310 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800311
Kim Phillips4b9926282009-08-13 11:50:38 +1000312 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800313 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000314 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800315 return -EAGAIN;
316 }
317
Kim Phillips4b9926282009-08-13 11:50:38 +1000318 head = priv->chan[ch].head;
319 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800320
Kim Phillips9c4a7962008-06-23 19:50:15 +0800321 /* map descriptor and save caller data */
322 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
323 DMA_BIDIRECTIONAL);
324 request->callback = callback;
325 request->context = context;
326
327 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000328 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800329
330 smp_wmb();
331 request->desc = desc;
332
333 /* GO! */
334 wmb();
Kim Phillips81eb0242009-08-13 11:51:51 +1000335 out_be32(priv->reg + TALITOS_FF(ch),
336 cpu_to_be32(upper_32_bits(request->dma_desc)));
337 out_be32(priv->reg + TALITOS_FF_LO(ch),
338 cpu_to_be32(lower_32_bits(request->dma_desc)));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800339
Kim Phillips4b9926282009-08-13 11:50:38 +1000340 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800341
342 return -EINPROGRESS;
343}
344
345/*
346 * process what was done, notify callback of error if not
347 */
348static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
349{
350 struct talitos_private *priv = dev_get_drvdata(dev);
351 struct talitos_request *request, saved_req;
352 unsigned long flags;
353 int tail, status;
354
Kim Phillips4b9926282009-08-13 11:50:38 +1000355 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800356
Kim Phillips4b9926282009-08-13 11:50:38 +1000357 tail = priv->chan[ch].tail;
358 while (priv->chan[ch].fifo[tail].desc) {
359 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800360
361 /* descriptors with their done bits set don't get the error */
362 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100363 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800364 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100365 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800366 if (!error)
367 break;
368 else
369 status = error;
370
371 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800372 sizeof(struct talitos_desc),
373 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800374
375 /* copy entries so we can call callback outside lock */
376 saved_req.desc = request->desc;
377 saved_req.callback = request->callback;
378 saved_req.context = request->context;
379
380 /* release request entry in fifo */
381 smp_wmb();
382 request->desc = NULL;
383
384 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000385 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800386
Kim Phillips4b9926282009-08-13 11:50:38 +1000387 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800388
Kim Phillips4b9926282009-08-13 11:50:38 +1000389 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800390
Kim Phillips9c4a7962008-06-23 19:50:15 +0800391 saved_req.callback(dev, saved_req.desc, saved_req.context,
392 status);
393 /* channel may resume processing in single desc error case */
394 if (error && !reset_ch && status == error)
395 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000396 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
397 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800398 }
399
Kim Phillips4b9926282009-08-13 11:50:38 +1000400 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800401}
402
403/*
404 * process completed requests for channels that have done status
405 */
406static void talitos_done(unsigned long data)
407{
408 struct device *dev = (struct device *)data;
409 struct talitos_private *priv = dev_get_drvdata(dev);
410 int ch;
411
412 for (ch = 0; ch < priv->num_channels; ch++)
413 flush_channel(dev, ch, 0, 0);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800414
415 /* At this point, all completed channels have been processed.
416 * Unmask done interrupts for channels completed later on.
417 */
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800418 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
419 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800420}
421
422/*
423 * locate current (offending) descriptor
424 */
425static struct talitos_desc *current_desc(struct device *dev, int ch)
426{
427 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000428 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800429 dma_addr_t cur_desc;
430
431 cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
432
Kim Phillips4b9926282009-08-13 11:50:38 +1000433 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800434 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000435 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436 dev_err(dev, "couldn't locate current descriptor\n");
437 return NULL;
438 }
439 }
440
Kim Phillips4b9926282009-08-13 11:50:38 +1000441 return priv->chan[ch].fifo[tail].desc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800442}
443
444/*
445 * user diagnostics; report root cause of error based on execution unit status
446 */
Kim Phillipse938e462009-03-29 15:53:23 +0800447static void report_eu_error(struct device *dev, int ch,
448 struct talitos_desc *desc)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800449{
450 struct talitos_private *priv = dev_get_drvdata(dev);
451 int i;
452
453 switch (desc->hdr & DESC_HDR_SEL0_MASK) {
454 case DESC_HDR_SEL0_AFEU:
455 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
456 in_be32(priv->reg + TALITOS_AFEUISR),
457 in_be32(priv->reg + TALITOS_AFEUISR_LO));
458 break;
459 case DESC_HDR_SEL0_DEU:
460 dev_err(dev, "DEUISR 0x%08x_%08x\n",
461 in_be32(priv->reg + TALITOS_DEUISR),
462 in_be32(priv->reg + TALITOS_DEUISR_LO));
463 break;
464 case DESC_HDR_SEL0_MDEUA:
465 case DESC_HDR_SEL0_MDEUB:
466 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
467 in_be32(priv->reg + TALITOS_MDEUISR),
468 in_be32(priv->reg + TALITOS_MDEUISR_LO));
469 break;
470 case DESC_HDR_SEL0_RNG:
471 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
472 in_be32(priv->reg + TALITOS_RNGUISR),
473 in_be32(priv->reg + TALITOS_RNGUISR_LO));
474 break;
475 case DESC_HDR_SEL0_PKEU:
476 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
477 in_be32(priv->reg + TALITOS_PKEUISR),
478 in_be32(priv->reg + TALITOS_PKEUISR_LO));
479 break;
480 case DESC_HDR_SEL0_AESU:
481 dev_err(dev, "AESUISR 0x%08x_%08x\n",
482 in_be32(priv->reg + TALITOS_AESUISR),
483 in_be32(priv->reg + TALITOS_AESUISR_LO));
484 break;
485 case DESC_HDR_SEL0_CRCU:
486 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
487 in_be32(priv->reg + TALITOS_CRCUISR),
488 in_be32(priv->reg + TALITOS_CRCUISR_LO));
489 break;
490 case DESC_HDR_SEL0_KEU:
491 dev_err(dev, "KEUISR 0x%08x_%08x\n",
492 in_be32(priv->reg + TALITOS_KEUISR),
493 in_be32(priv->reg + TALITOS_KEUISR_LO));
494 break;
495 }
496
497 switch (desc->hdr & DESC_HDR_SEL1_MASK) {
498 case DESC_HDR_SEL1_MDEUA:
499 case DESC_HDR_SEL1_MDEUB:
500 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
501 in_be32(priv->reg + TALITOS_MDEUISR),
502 in_be32(priv->reg + TALITOS_MDEUISR_LO));
503 break;
504 case DESC_HDR_SEL1_CRCU:
505 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
506 in_be32(priv->reg + TALITOS_CRCUISR),
507 in_be32(priv->reg + TALITOS_CRCUISR_LO));
508 break;
509 }
510
511 for (i = 0; i < 8; i++)
512 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
513 in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
514 in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
515}
516
517/*
518 * recover from error interrupts
519 */
Kim Phillips40405f12008-10-12 20:19:35 +0800520static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800521{
522 struct device *dev = (struct device *)data;
523 struct talitos_private *priv = dev_get_drvdata(dev);
524 unsigned int timeout = TALITOS_TIMEOUT;
525 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800526 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800527
528 for (ch = 0; ch < priv->num_channels; ch++) {
529 /* skip channels without errors */
530 if (!(isr & (1 << (ch * 2 + 1))))
531 continue;
532
533 error = -EINVAL;
534
535 v = in_be32(priv->reg + TALITOS_CCPSR(ch));
536 v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
537
538 if (v_lo & TALITOS_CCPSR_LO_DOF) {
539 dev_err(dev, "double fetch fifo overflow error\n");
540 error = -EAGAIN;
541 reset_ch = 1;
542 }
543 if (v_lo & TALITOS_CCPSR_LO_SOF) {
544 /* h/w dropped descriptor */
545 dev_err(dev, "single fetch fifo overflow error\n");
546 error = -EAGAIN;
547 }
548 if (v_lo & TALITOS_CCPSR_LO_MDTE)
549 dev_err(dev, "master data transfer error\n");
550 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
551 dev_err(dev, "s/g data length zero error\n");
552 if (v_lo & TALITOS_CCPSR_LO_FPZ)
553 dev_err(dev, "fetch pointer zero error\n");
554 if (v_lo & TALITOS_CCPSR_LO_IDH)
555 dev_err(dev, "illegal descriptor header error\n");
556 if (v_lo & TALITOS_CCPSR_LO_IEU)
557 dev_err(dev, "invalid execution unit error\n");
558 if (v_lo & TALITOS_CCPSR_LO_EU)
559 report_eu_error(dev, ch, current_desc(dev, ch));
560 if (v_lo & TALITOS_CCPSR_LO_GB)
561 dev_err(dev, "gather boundary error\n");
562 if (v_lo & TALITOS_CCPSR_LO_GRL)
563 dev_err(dev, "gather return/length error\n");
564 if (v_lo & TALITOS_CCPSR_LO_SB)
565 dev_err(dev, "scatter boundary error\n");
566 if (v_lo & TALITOS_CCPSR_LO_SRL)
567 dev_err(dev, "scatter return/length error\n");
568
569 flush_channel(dev, ch, error, reset_ch);
570
571 if (reset_ch) {
572 reset_channel(dev, ch);
573 } else {
574 setbits32(priv->reg + TALITOS_CCCR(ch),
575 TALITOS_CCCR_CONT);
576 setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
577 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
578 TALITOS_CCCR_CONT) && --timeout)
579 cpu_relax();
580 if (timeout == 0) {
581 dev_err(dev, "failed to restart channel %d\n",
582 ch);
583 reset_dev = 1;
584 }
585 }
586 }
587 if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
588 dev_err(dev, "done overflow, internal time out, or rngu error: "
589 "ISR 0x%08x_%08x\n", isr, isr_lo);
590
591 /* purge request queues */
592 for (ch = 0; ch < priv->num_channels; ch++)
593 flush_channel(dev, ch, -EIO, 1);
594
595 /* reset and reinitialize the device */
596 init_device(dev);
597 }
598}
599
600static irqreturn_t talitos_interrupt(int irq, void *data)
601{
602 struct device *dev = data;
603 struct talitos_private *priv = dev_get_drvdata(dev);
604 u32 isr, isr_lo;
605
606 isr = in_be32(priv->reg + TALITOS_ISR);
607 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
Lee Nipperca38a812008-12-20 17:09:25 +1100608 /* Acknowledge interrupt */
609 out_be32(priv->reg + TALITOS_ICR, isr);
610 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800611
Lee Nipperca38a812008-12-20 17:09:25 +1100612 if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
Kim Phillips40405f12008-10-12 20:19:35 +0800613 talitos_error((unsigned long)data, isr, isr_lo);
Lee Nipperca38a812008-12-20 17:09:25 +1100614 else
Lee Nipper1c2e8812008-10-12 20:29:34 +0800615 if (likely(isr & TALITOS_ISR_CHDONE)) {
616 /* mask further done interrupts. */
617 clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
618 /* done_task will unmask done interrupts at exit */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800619 tasklet_schedule(&priv->done_task);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800620 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800621
622 return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
623}
624
625/*
626 * hwrng
627 */
628static int talitos_rng_data_present(struct hwrng *rng, int wait)
629{
630 struct device *dev = (struct device *)rng->priv;
631 struct talitos_private *priv = dev_get_drvdata(dev);
632 u32 ofl;
633 int i;
634
635 for (i = 0; i < 20; i++) {
636 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
637 TALITOS_RNGUSR_LO_OFL;
638 if (ofl || !wait)
639 break;
640 udelay(10);
641 }
642
643 return !!ofl;
644}
645
646static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
647{
648 struct device *dev = (struct device *)rng->priv;
649 struct talitos_private *priv = dev_get_drvdata(dev);
650
651 /* rng fifo requires 64-bit accesses */
652 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
653 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
654
655 return sizeof(u32);
656}
657
658static int talitos_rng_init(struct hwrng *rng)
659{
660 struct device *dev = (struct device *)rng->priv;
661 struct talitos_private *priv = dev_get_drvdata(dev);
662 unsigned int timeout = TALITOS_TIMEOUT;
663
664 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
665 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
666 && --timeout)
667 cpu_relax();
668 if (timeout == 0) {
669 dev_err(dev, "failed to reset rng hw\n");
670 return -ENODEV;
671 }
672
673 /* start generating */
674 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
675
676 return 0;
677}
678
679static int talitos_register_rng(struct device *dev)
680{
681 struct talitos_private *priv = dev_get_drvdata(dev);
682
683 priv->rng.name = dev_driver_string(dev),
684 priv->rng.init = talitos_rng_init,
685 priv->rng.data_present = talitos_rng_data_present,
686 priv->rng.data_read = talitos_rng_data_read,
687 priv->rng.priv = (unsigned long)dev;
688
689 return hwrng_register(&priv->rng);
690}
691
692static void talitos_unregister_rng(struct device *dev)
693{
694 struct talitos_private *priv = dev_get_drvdata(dev);
695
696 hwrng_unregister(&priv->rng);
697}
698
699/*
700 * crypto alg
701 */
702#define TALITOS_CRA_PRIORITY 3000
703#define TALITOS_MAX_KEY_SIZE 64
Lee Nipper3952f172008-07-10 18:29:18 +0800704#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800705
Lee Nipper497f2e62010-05-19 19:20:36 +1000706#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800707
708struct talitos_ctx {
709 struct device *dev;
710 __be32 desc_hdr_template;
711 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800712 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800713 unsigned int keylen;
714 unsigned int enckeylen;
715 unsigned int authkeylen;
716 unsigned int authsize;
717};
718
Lee Nipper497f2e62010-05-19 19:20:36 +1000719#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
720#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
721
722struct talitos_ahash_req_ctx {
723 u64 count;
Kim Phillips60f208d2010-05-19 19:21:53 +1000724 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000725 unsigned int hw_context_size;
726 u8 buf[HASH_MAX_BLOCK_SIZE];
727 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000728 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000729 unsigned int first;
730 unsigned int last;
731 unsigned int to_hash_later;
732 struct scatterlist bufsl[2];
733 struct scatterlist *psrc;
734};
735
Lee Nipper56af8cd2009-03-29 15:50:50 +0800736static int aead_setauthsize(struct crypto_aead *authenc,
737 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800738{
739 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
740
741 ctx->authsize = authsize;
742
743 return 0;
744}
745
Lee Nipper56af8cd2009-03-29 15:50:50 +0800746static int aead_setkey(struct crypto_aead *authenc,
747 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800748{
749 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
750 struct rtattr *rta = (void *)key;
751 struct crypto_authenc_key_param *param;
752 unsigned int authkeylen;
753 unsigned int enckeylen;
754
755 if (!RTA_OK(rta, keylen))
756 goto badkey;
757
758 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
759 goto badkey;
760
761 if (RTA_PAYLOAD(rta) < sizeof(*param))
762 goto badkey;
763
764 param = RTA_DATA(rta);
765 enckeylen = be32_to_cpu(param->enckeylen);
766
767 key += RTA_ALIGN(rta->rta_len);
768 keylen -= RTA_ALIGN(rta->rta_len);
769
770 if (keylen < enckeylen)
771 goto badkey;
772
773 authkeylen = keylen - enckeylen;
774
775 if (keylen > TALITOS_MAX_KEY_SIZE)
776 goto badkey;
777
778 memcpy(&ctx->key, key, keylen);
779
780 ctx->keylen = keylen;
781 ctx->enckeylen = enckeylen;
782 ctx->authkeylen = authkeylen;
783
784 return 0;
785
786badkey:
787 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
788 return -EINVAL;
789}
790
791/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800792 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800793 * @src_nents: number of segments in input scatterlist
794 * @dst_nents: number of segments in output scatterlist
795 * @dma_len: length of dma mapped link_tbl space
796 * @dma_link_tbl: bus physical address of link_tbl
797 * @desc: h/w descriptor
798 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
799 *
800 * if decrypting (with authcheck), or either one of src_nents or dst_nents
801 * is greater than 1, an integrity check value is concatenated to the end
802 * of link_tbl data
803 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800804struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800805 int src_nents;
806 int dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800807 int src_is_chained;
808 int dst_is_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800809 int dma_len;
810 dma_addr_t dma_link_tbl;
811 struct talitos_desc desc;
812 struct talitos_ptr link_tbl[0];
813};
814
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800815static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
816 unsigned int nents, enum dma_data_direction dir,
817 int chained)
818{
819 if (unlikely(chained))
820 while (sg) {
821 dma_map_sg(dev, sg, 1, dir);
822 sg = scatterwalk_sg_next(sg);
823 }
824 else
825 dma_map_sg(dev, sg, nents, dir);
826 return nents;
827}
828
829static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
830 enum dma_data_direction dir)
831{
832 while (sg) {
833 dma_unmap_sg(dev, sg, 1, dir);
834 sg = scatterwalk_sg_next(sg);
835 }
836}
837
838static void talitos_sg_unmap(struct device *dev,
839 struct talitos_edesc *edesc,
840 struct scatterlist *src,
841 struct scatterlist *dst)
842{
843 unsigned int src_nents = edesc->src_nents ? : 1;
844 unsigned int dst_nents = edesc->dst_nents ? : 1;
845
846 if (src != dst) {
847 if (edesc->src_is_chained)
848 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
849 else
850 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
851
Lee Nipper497f2e62010-05-19 19:20:36 +1000852 if (dst) {
853 if (edesc->dst_is_chained)
854 talitos_unmap_sg_chain(dev, dst,
855 DMA_FROM_DEVICE);
856 else
857 dma_unmap_sg(dev, dst, dst_nents,
858 DMA_FROM_DEVICE);
859 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800860 } else
861 if (edesc->src_is_chained)
862 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
863 else
864 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
865}
866
Kim Phillips9c4a7962008-06-23 19:50:15 +0800867static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800868 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800869 struct aead_request *areq)
870{
871 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
872 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
873 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
874 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
875
876 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
877
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800878 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800879
880 if (edesc->dma_len)
881 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
882 DMA_BIDIRECTIONAL);
883}
884
885/*
886 * ipsec_esp descriptor callbacks
887 */
888static void ipsec_esp_encrypt_done(struct device *dev,
889 struct talitos_desc *desc, void *context,
890 int err)
891{
892 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800893 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
894 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800895 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800896 struct scatterlist *sg;
897 void *icvdata;
898
Kim Phillips19bbbc62009-03-29 15:53:59 +0800899 edesc = container_of(desc, struct talitos_edesc, desc);
900
Kim Phillips9c4a7962008-06-23 19:50:15 +0800901 ipsec_esp_unmap(dev, edesc, areq);
902
903 /* copy the generated ICV to dst */
904 if (edesc->dma_len) {
905 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800906 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800907 sg = sg_last(areq->dst, edesc->dst_nents);
908 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
909 icvdata, ctx->authsize);
910 }
911
912 kfree(edesc);
913
914 aead_request_complete(areq, err);
915}
916
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800917static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800918 struct talitos_desc *desc,
919 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800920{
921 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800922 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
923 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800924 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800925 struct scatterlist *sg;
926 void *icvdata;
927
Kim Phillips19bbbc62009-03-29 15:53:59 +0800928 edesc = container_of(desc, struct talitos_edesc, desc);
929
Kim Phillips9c4a7962008-06-23 19:50:15 +0800930 ipsec_esp_unmap(dev, edesc, req);
931
932 if (!err) {
933 /* auth check */
934 if (edesc->dma_len)
935 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800936 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800937 else
938 icvdata = &edesc->link_tbl[0];
939
940 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
941 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
942 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
943 }
944
945 kfree(edesc);
946
947 aead_request_complete(req, err);
948}
949
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800950static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800951 struct talitos_desc *desc,
952 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800953{
954 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800955 struct talitos_edesc *edesc;
956
957 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800958
959 ipsec_esp_unmap(dev, edesc, req);
960
961 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800962 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
963 DESC_HDR_LO_ICCR1_PASS))
964 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800965
966 kfree(edesc);
967
968 aead_request_complete(req, err);
969}
970
Kim Phillips9c4a7962008-06-23 19:50:15 +0800971/*
972 * convert scatterlist to SEC h/w link table format
973 * stop at cryptlen bytes
974 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800975static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800976 int cryptlen, struct talitos_ptr *link_tbl_ptr)
977{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800978 int n_sg = sg_count;
979
980 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000981 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800982 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
983 link_tbl_ptr->j_extent = 0;
984 link_tbl_ptr++;
985 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800986 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800987 }
988
Lee Nipper70bcaca2008-07-03 19:08:46 +0800989 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800990 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800991 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800992 /* Empty this entry, and move to previous one */
993 cryptlen += be16_to_cpu(link_tbl_ptr->len);
994 link_tbl_ptr->len = 0;
995 sg_count--;
996 link_tbl_ptr--;
997 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800998 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
999 + cryptlen);
1000
1001 /* tag end of link table */
1002 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +08001003
1004 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001005}
1006
1007/*
1008 * fill in and submit ipsec_esp descriptor
1009 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001010static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001011 u8 *giv, u64 seq,
1012 void (*callback) (struct device *dev,
1013 struct talitos_desc *desc,
1014 void *context, int error))
1015{
1016 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1017 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1018 struct device *dev = ctx->dev;
1019 struct talitos_desc *desc = &edesc->desc;
1020 unsigned int cryptlen = areq->cryptlen;
1021 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +10001022 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001023 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001024 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001025
1026 /* hmac key */
1027 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
1028 0, DMA_TO_DEVICE);
1029 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +10001030 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1031 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001032 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001033 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
1034 DMA_TO_DEVICE);
1035
1036 /* cipher key */
1037 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1038 (char *)&ctx->key + ctx->authkeylen, 0,
1039 DMA_TO_DEVICE);
1040
1041 /*
1042 * cipher in
1043 * map and adjust cipher len to aead request cryptlen.
1044 * extent is bytes of HMAC postpended to ciphertext,
1045 * typically 12 for ipsec
1046 */
1047 desc->ptr[4].len = cpu_to_be16(cryptlen);
1048 desc->ptr[4].j_extent = authsize;
1049
Kim Phillipse938e462009-03-29 15:53:23 +08001050 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1051 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1052 : DMA_TO_DEVICE,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001053 edesc->src_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001054
1055 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001056 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001057 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001058 sg_link_tbl_len = cryptlen;
1059
Kim Phillips962a9c92009-03-29 15:54:30 +08001060 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001061 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001062
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001063 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001064 &edesc->link_tbl[0]);
1065 if (sg_count > 1) {
1066 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001067 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001068 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1069 edesc->dma_len,
1070 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001071 } else {
1072 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001073 to_talitos_ptr(&desc->ptr[4],
1074 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001075 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001076 }
1077
1078 /* cipher out */
1079 desc->ptr[5].len = cpu_to_be16(cryptlen);
1080 desc->ptr[5].j_extent = authsize;
1081
Kim Phillipse938e462009-03-29 15:53:23 +08001082 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001083 sg_count = talitos_map_sg(dev, areq->dst,
1084 edesc->dst_nents ? : 1,
1085 DMA_FROM_DEVICE,
1086 edesc->dst_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001087
1088 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001089 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001090 } else {
1091 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001092 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001093
Kim Phillips81eb0242009-08-13 11:51:51 +10001094 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1095 (edesc->src_nents + 1) *
1096 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001097 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1098 link_tbl_ptr);
1099
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001100 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001101 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001102 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001103 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001104 link_tbl_ptr++;
1105 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1106 link_tbl_ptr->len = cpu_to_be16(authsize);
1107
1108 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001109 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1110 (edesc->src_nents + edesc->dst_nents + 2) *
1111 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001112 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1113 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1114 edesc->dma_len, DMA_BIDIRECTIONAL);
1115 }
1116
1117 /* iv out */
1118 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1119 DMA_FROM_DEVICE);
1120
Kim Phillipsfa86a262008-07-17 20:20:06 +08001121 ret = talitos_submit(dev, desc, callback, areq);
1122 if (ret != -EINPROGRESS) {
1123 ipsec_esp_unmap(dev, edesc, areq);
1124 kfree(edesc);
1125 }
1126 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001127}
1128
Kim Phillips9c4a7962008-06-23 19:50:15 +08001129/*
1130 * derive number of elements in scatterlist
1131 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001132static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001133{
1134 struct scatterlist *sg = sg_list;
1135 int sg_nents = 0;
1136
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001137 *chained = 0;
1138 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001139 sg_nents++;
1140 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001141 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1142 *chained = 1;
1143 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001144 }
1145
1146 return sg_nents;
1147}
1148
Lee Nipper497f2e62010-05-19 19:20:36 +10001149/**
1150 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1151 * @sgl: The SG list
1152 * @nents: Number of SG entries
1153 * @buf: Where to copy to
1154 * @buflen: The number of bytes to copy
1155 * @skip: The number of bytes to skip before copying.
1156 * Note: skip + buflen should equal SG total size.
1157 *
1158 * Returns the number of copied bytes.
1159 *
1160 **/
1161static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1162 void *buf, size_t buflen, unsigned int skip)
1163{
1164 unsigned int offset = 0;
1165 unsigned int boffset = 0;
1166 struct sg_mapping_iter miter;
1167 unsigned long flags;
1168 unsigned int sg_flags = SG_MITER_ATOMIC;
1169 size_t total_buffer = buflen + skip;
1170
1171 sg_flags |= SG_MITER_FROM_SG;
1172
1173 sg_miter_start(&miter, sgl, nents, sg_flags);
1174
1175 local_irq_save(flags);
1176
1177 while (sg_miter_next(&miter) && offset < total_buffer) {
1178 unsigned int len;
1179 unsigned int ignore;
1180
1181 if ((offset + miter.length) > skip) {
1182 if (offset < skip) {
1183 /* Copy part of this segment */
1184 ignore = skip - offset;
1185 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001186 if (boffset + len > buflen)
1187 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001188 memcpy(buf + boffset, miter.addr + ignore, len);
1189 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001190 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001191 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001192 if (boffset + len > buflen)
1193 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001194 memcpy(buf + boffset, miter.addr, len);
1195 }
1196 boffset += len;
1197 }
1198 offset += miter.length;
1199 }
1200
1201 sg_miter_stop(&miter);
1202
1203 local_irq_restore(flags);
1204 return boffset;
1205}
1206
Kim Phillips9c4a7962008-06-23 19:50:15 +08001207/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001208 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001209 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001210static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1211 struct scatterlist *src,
1212 struct scatterlist *dst,
Lee Nipper497f2e62010-05-19 19:20:36 +10001213 int hash_result,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001214 unsigned int cryptlen,
1215 unsigned int authsize,
1216 int icv_stashing,
1217 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001218{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001219 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001220 int src_nents, dst_nents, alloc_len, dma_len;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001221 int src_chained, dst_chained = 0;
1222 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001223 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001224
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001225 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1226 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001227 return ERR_PTR(-EINVAL);
1228 }
1229
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001230 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001231 src_nents = (src_nents == 1) ? 0 : src_nents;
1232
Lee Nipper497f2e62010-05-19 19:20:36 +10001233 if (hash_result) {
1234 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001235 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001236 if (dst == src) {
1237 dst_nents = src_nents;
1238 } else {
1239 dst_nents = sg_count(dst, cryptlen + authsize,
1240 &dst_chained);
1241 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1242 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001243 }
1244
1245 /*
1246 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001247 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001248 * and the ICV data itself
1249 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001250 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001251 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001252 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001253 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001254 alloc_len += dma_len;
1255 } else {
1256 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001257 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001258 }
1259
Kim Phillips586725f2008-07-17 20:19:18 +08001260 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001261 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001262 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001263 return ERR_PTR(-ENOMEM);
1264 }
1265
1266 edesc->src_nents = src_nents;
1267 edesc->dst_nents = dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001268 edesc->src_is_chained = src_chained;
1269 edesc->dst_is_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001270 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001271 if (dma_len)
1272 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1273 edesc->dma_len,
1274 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001275
1276 return edesc;
1277}
1278
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001279static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1280 int icv_stashing)
1281{
1282 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1283 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1284
Lee Nipper497f2e62010-05-19 19:20:36 +10001285 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001286 areq->cryptlen, ctx->authsize, icv_stashing,
1287 areq->base.flags);
1288}
1289
Lee Nipper56af8cd2009-03-29 15:50:50 +08001290static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001291{
1292 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1293 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001294 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001295
1296 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001297 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001298 if (IS_ERR(edesc))
1299 return PTR_ERR(edesc);
1300
1301 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001302 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001303
1304 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1305}
1306
Lee Nipper56af8cd2009-03-29 15:50:50 +08001307static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001308{
1309 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1310 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1311 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001312 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001313 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001314 struct scatterlist *sg;
1315 void *icvdata;
1316
1317 req->cryptlen -= authsize;
1318
1319 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001320 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001321 if (IS_ERR(edesc))
1322 return PTR_ERR(edesc);
1323
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001324 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001325 ((!edesc->src_nents && !edesc->dst_nents) ||
1326 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001327
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001328 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001329 edesc->desc.hdr = ctx->desc_hdr_template |
1330 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001331 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001332
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001333 /* reset integrity check result bits */
1334 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001335
Kim Phillipse938e462009-03-29 15:53:23 +08001336 return ipsec_esp(edesc, req, NULL, 0,
1337 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001338
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001339 }
Kim Phillipse938e462009-03-29 15:53:23 +08001340
1341 /* Have to check the ICV with software */
1342 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1343
1344 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1345 if (edesc->dma_len)
1346 icvdata = &edesc->link_tbl[edesc->src_nents +
1347 edesc->dst_nents + 2];
1348 else
1349 icvdata = &edesc->link_tbl[0];
1350
1351 sg = sg_last(req->src, edesc->src_nents ? : 1);
1352
1353 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1354 ctx->authsize);
1355
1356 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001357}
1358
Lee Nipper56af8cd2009-03-29 15:50:50 +08001359static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001360{
1361 struct aead_request *areq = &req->areq;
1362 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1363 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001364 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001365
1366 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001367 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001368 if (IS_ERR(edesc))
1369 return PTR_ERR(edesc);
1370
1371 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001372 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001373
1374 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001375 /* avoid consecutive packets going out with same IV */
1376 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001377
1378 return ipsec_esp(edesc, areq, req->giv, req->seq,
1379 ipsec_esp_encrypt_done);
1380}
1381
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001382static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1383 const u8 *key, unsigned int keylen)
1384{
1385 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1386 struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher);
1387
1388 if (keylen > TALITOS_MAX_KEY_SIZE)
1389 goto badkey;
1390
1391 if (keylen < alg->min_keysize || keylen > alg->max_keysize)
1392 goto badkey;
1393
1394 memcpy(&ctx->key, key, keylen);
1395 ctx->keylen = keylen;
1396
1397 return 0;
1398
1399badkey:
1400 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1401 return -EINVAL;
1402}
1403
1404static void common_nonsnoop_unmap(struct device *dev,
1405 struct talitos_edesc *edesc,
1406 struct ablkcipher_request *areq)
1407{
1408 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1409 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1410 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1411
1412 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1413
1414 if (edesc->dma_len)
1415 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1416 DMA_BIDIRECTIONAL);
1417}
1418
1419static void ablkcipher_done(struct device *dev,
1420 struct talitos_desc *desc, void *context,
1421 int err)
1422{
1423 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001424 struct talitos_edesc *edesc;
1425
1426 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001427
1428 common_nonsnoop_unmap(dev, edesc, areq);
1429
1430 kfree(edesc);
1431
1432 areq->base.complete(&areq->base, err);
1433}
1434
1435static int common_nonsnoop(struct talitos_edesc *edesc,
1436 struct ablkcipher_request *areq,
1437 u8 *giv,
1438 void (*callback) (struct device *dev,
1439 struct talitos_desc *desc,
1440 void *context, int error))
1441{
1442 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1443 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1444 struct device *dev = ctx->dev;
1445 struct talitos_desc *desc = &edesc->desc;
1446 unsigned int cryptlen = areq->nbytes;
1447 unsigned int ivsize;
1448 int sg_count, ret;
1449
1450 /* first DWORD empty */
1451 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001452 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001453 desc->ptr[0].j_extent = 0;
1454
1455 /* cipher iv */
1456 ivsize = crypto_ablkcipher_ivsize(cipher);
1457 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0,
1458 DMA_TO_DEVICE);
1459
1460 /* cipher key */
1461 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1462 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1463
1464 /*
1465 * cipher in
1466 */
1467 desc->ptr[3].len = cpu_to_be16(cryptlen);
1468 desc->ptr[3].j_extent = 0;
1469
1470 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1471 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1472 : DMA_TO_DEVICE,
1473 edesc->src_is_chained);
1474
1475 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001476 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001477 } else {
1478 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1479 &edesc->link_tbl[0]);
1480 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001481 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001482 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001483 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1484 edesc->dma_len,
1485 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001486 } else {
1487 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001488 to_talitos_ptr(&desc->ptr[3],
1489 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001490 }
1491 }
1492
1493 /* cipher out */
1494 desc->ptr[4].len = cpu_to_be16(cryptlen);
1495 desc->ptr[4].j_extent = 0;
1496
1497 if (areq->src != areq->dst)
1498 sg_count = talitos_map_sg(dev, areq->dst,
1499 edesc->dst_nents ? : 1,
1500 DMA_FROM_DEVICE,
1501 edesc->dst_is_chained);
1502
1503 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001504 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001505 } else {
1506 struct talitos_ptr *link_tbl_ptr =
1507 &edesc->link_tbl[edesc->src_nents + 1];
1508
Kim Phillips81eb0242009-08-13 11:51:51 +10001509 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1510 (edesc->src_nents + 1) *
1511 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001512 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001513 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1514 link_tbl_ptr);
1515 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1516 edesc->dma_len, DMA_BIDIRECTIONAL);
1517 }
1518
1519 /* iv out */
1520 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1521 DMA_FROM_DEVICE);
1522
1523 /* last DWORD empty */
1524 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001525 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001526 desc->ptr[6].j_extent = 0;
1527
1528 ret = talitos_submit(dev, desc, callback, areq);
1529 if (ret != -EINPROGRESS) {
1530 common_nonsnoop_unmap(dev, edesc, areq);
1531 kfree(edesc);
1532 }
1533 return ret;
1534}
1535
Kim Phillipse938e462009-03-29 15:53:23 +08001536static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1537 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001538{
1539 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1540 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1541
Lee Nipper497f2e62010-05-19 19:20:36 +10001542 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1543 areq->nbytes, 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001544}
1545
1546static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1547{
1548 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1549 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1550 struct talitos_edesc *edesc;
1551
1552 /* allocate extended descriptor */
1553 edesc = ablkcipher_edesc_alloc(areq);
1554 if (IS_ERR(edesc))
1555 return PTR_ERR(edesc);
1556
1557 /* set encrypt */
1558 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1559
1560 return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
1561}
1562
1563static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1564{
1565 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1566 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1567 struct talitos_edesc *edesc;
1568
1569 /* allocate extended descriptor */
1570 edesc = ablkcipher_edesc_alloc(areq);
1571 if (IS_ERR(edesc))
1572 return PTR_ERR(edesc);
1573
1574 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1575
1576 return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
1577}
1578
Lee Nipper497f2e62010-05-19 19:20:36 +10001579static void common_nonsnoop_hash_unmap(struct device *dev,
1580 struct talitos_edesc *edesc,
1581 struct ahash_request *areq)
1582{
1583 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1584
1585 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1586
1587 /* When using hashctx-in, must unmap it. */
1588 if (edesc->desc.ptr[1].len)
1589 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1590 DMA_TO_DEVICE);
1591
1592 if (edesc->desc.ptr[2].len)
1593 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1594 DMA_TO_DEVICE);
1595
1596 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1597
1598 if (edesc->dma_len)
1599 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1600 DMA_BIDIRECTIONAL);
1601
1602}
1603
1604static void ahash_done(struct device *dev,
1605 struct talitos_desc *desc, void *context,
1606 int err)
1607{
1608 struct ahash_request *areq = context;
1609 struct talitos_edesc *edesc =
1610 container_of(desc, struct talitos_edesc, desc);
1611 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1612
1613 if (!req_ctx->last && req_ctx->to_hash_later) {
1614 /* Position any partial block for next update/final/finup */
1615 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
1616 }
1617 common_nonsnoop_hash_unmap(dev, edesc, areq);
1618
1619 kfree(edesc);
1620
1621 areq->base.complete(&areq->base, err);
1622}
1623
1624static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1625 struct ahash_request *areq, unsigned int length,
1626 void (*callback) (struct device *dev,
1627 struct talitos_desc *desc,
1628 void *context, int error))
1629{
1630 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1631 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1632 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1633 struct device *dev = ctx->dev;
1634 struct talitos_desc *desc = &edesc->desc;
1635 int sg_count, ret;
1636
1637 /* first DWORD empty */
1638 desc->ptr[0] = zero_entry;
1639
Kim Phillips60f208d2010-05-19 19:21:53 +10001640 /* hash context in */
1641 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001642 map_single_talitos_ptr(dev, &desc->ptr[1],
1643 req_ctx->hw_context_size,
1644 (char *)req_ctx->hw_context, 0,
1645 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001646 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001647 } else {
1648 desc->ptr[1] = zero_entry;
1649 /* Indicate next op is not the first. */
1650 req_ctx->first = 0;
1651 }
1652
1653 /* HMAC key */
1654 if (ctx->keylen)
1655 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1656 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1657 else
1658 desc->ptr[2] = zero_entry;
1659
1660 /*
1661 * data in
1662 */
1663 desc->ptr[3].len = cpu_to_be16(length);
1664 desc->ptr[3].j_extent = 0;
1665
1666 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1667 edesc->src_nents ? : 1,
1668 DMA_TO_DEVICE,
1669 edesc->src_is_chained);
1670
1671 if (sg_count == 1) {
1672 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1673 } else {
1674 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1675 &edesc->link_tbl[0]);
1676 if (sg_count > 1) {
1677 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1678 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1679 dma_sync_single_for_device(ctx->dev,
1680 edesc->dma_link_tbl,
1681 edesc->dma_len,
1682 DMA_BIDIRECTIONAL);
1683 } else {
1684 /* Only one segment now, so no link tbl needed */
1685 to_talitos_ptr(&desc->ptr[3],
1686 sg_dma_address(req_ctx->psrc));
1687 }
1688 }
1689
1690 /* fifth DWORD empty */
1691 desc->ptr[4] = zero_entry;
1692
1693 /* hash/HMAC out -or- hash context out */
1694 if (req_ctx->last)
1695 map_single_talitos_ptr(dev, &desc->ptr[5],
1696 crypto_ahash_digestsize(tfm),
1697 areq->result, 0, DMA_FROM_DEVICE);
1698 else
1699 map_single_talitos_ptr(dev, &desc->ptr[5],
1700 req_ctx->hw_context_size,
1701 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1702
1703 /* last DWORD empty */
1704 desc->ptr[6] = zero_entry;
1705
1706 ret = talitos_submit(dev, desc, callback, areq);
1707 if (ret != -EINPROGRESS) {
1708 common_nonsnoop_hash_unmap(dev, edesc, areq);
1709 kfree(edesc);
1710 }
1711 return ret;
1712}
1713
1714static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1715 unsigned int nbytes)
1716{
1717 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1718 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1719 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1720
1721 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1722 nbytes, 0, 0, areq->base.flags);
1723}
1724
1725static int ahash_init(struct ahash_request *areq)
1726{
1727 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1728 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1729
1730 /* Initialize the context */
1731 req_ctx->count = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001732 req_ctx->first = 1; /* first indicates h/w must init its context */
1733 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001734 req_ctx->hw_context_size =
1735 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1736 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1737 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1738
1739 return 0;
1740}
1741
Kim Phillips60f208d2010-05-19 19:21:53 +10001742/*
1743 * on h/w without explicit sha224 support, we initialize h/w context
1744 * manually with sha224 constants, and tell it to run sha256.
1745 */
1746static int ahash_init_sha224_swinit(struct ahash_request *areq)
1747{
1748 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1749
1750 ahash_init(areq);
1751 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1752
1753 req_ctx->hw_context[0] = cpu_to_be32(SHA224_H0);
1754 req_ctx->hw_context[1] = cpu_to_be32(SHA224_H1);
1755 req_ctx->hw_context[2] = cpu_to_be32(SHA224_H2);
1756 req_ctx->hw_context[3] = cpu_to_be32(SHA224_H3);
1757 req_ctx->hw_context[4] = cpu_to_be32(SHA224_H4);
1758 req_ctx->hw_context[5] = cpu_to_be32(SHA224_H5);
1759 req_ctx->hw_context[6] = cpu_to_be32(SHA224_H6);
1760 req_ctx->hw_context[7] = cpu_to_be32(SHA224_H7);
1761
1762 /* init 64-bit count */
1763 req_ctx->hw_context[8] = 0;
1764 req_ctx->hw_context[9] = 0;
1765
1766 return 0;
1767}
1768
Lee Nipper497f2e62010-05-19 19:20:36 +10001769static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1770{
1771 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1772 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1773 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1774 struct talitos_edesc *edesc;
1775 unsigned int blocksize =
1776 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1777 unsigned int nbytes_to_hash;
1778 unsigned int to_hash_later;
1779 unsigned int index;
1780 int chained;
1781
1782 index = req_ctx->count & (blocksize - 1);
1783 req_ctx->count += nbytes;
1784
1785 if (!req_ctx->last && (index + nbytes) < blocksize) {
1786 /* Buffer the partial block */
1787 sg_copy_to_buffer(areq->src,
1788 sg_count(areq->src, nbytes, &chained),
1789 req_ctx->buf + index, nbytes);
1790 return 0;
1791 }
1792
1793 if (index) {
1794 /* partial block from previous update; chain it in. */
1795 sg_init_table(req_ctx->bufsl, (nbytes) ? 2 : 1);
1796 sg_set_buf(req_ctx->bufsl, req_ctx->buf, index);
1797 if (nbytes)
1798 scatterwalk_sg_chain(req_ctx->bufsl, 2,
1799 areq->src);
1800 req_ctx->psrc = req_ctx->bufsl;
1801 } else {
1802 req_ctx->psrc = areq->src;
1803 }
1804 nbytes_to_hash = index + nbytes;
1805 if (!req_ctx->last) {
1806 to_hash_later = (nbytes_to_hash & (blocksize - 1));
1807 if (to_hash_later) {
1808 int nents;
1809 /* Must copy to_hash_later bytes from the end
1810 * to bufnext (a partial block) for later.
1811 */
1812 nents = sg_count(areq->src, nbytes, &chained);
1813 sg_copy_end_to_buffer(areq->src, nents,
1814 req_ctx->bufnext,
1815 to_hash_later,
1816 nbytes - to_hash_later);
1817
1818 /* Adjust count for what will be hashed now */
1819 nbytes_to_hash -= to_hash_later;
1820 }
1821 req_ctx->to_hash_later = to_hash_later;
1822 }
1823
1824 /* allocate extended descriptor */
1825 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1826 if (IS_ERR(edesc))
1827 return PTR_ERR(edesc);
1828
1829 edesc->desc.hdr = ctx->desc_hdr_template;
1830
1831 /* On last one, request SEC to pad; otherwise continue */
1832 if (req_ctx->last)
1833 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1834 else
1835 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1836
Kim Phillips60f208d2010-05-19 19:21:53 +10001837 /* request SEC to INIT hash. */
1838 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001839 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1840
1841 /* When the tfm context has a keylen, it's an HMAC.
1842 * A first or last (ie. not middle) descriptor must request HMAC.
1843 */
1844 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1845 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1846
1847 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1848 ahash_done);
1849}
1850
1851static int ahash_update(struct ahash_request *areq)
1852{
1853 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1854
1855 req_ctx->last = 0;
1856
1857 return ahash_process_req(areq, areq->nbytes);
1858}
1859
1860static int ahash_final(struct ahash_request *areq)
1861{
1862 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1863
1864 req_ctx->last = 1;
1865
1866 return ahash_process_req(areq, 0);
1867}
1868
1869static int ahash_finup(struct ahash_request *areq)
1870{
1871 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1872
1873 req_ctx->last = 1;
1874
1875 return ahash_process_req(areq, areq->nbytes);
1876}
1877
1878static int ahash_digest(struct ahash_request *areq)
1879{
1880 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001881 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001882
Kim Phillips60f208d2010-05-19 19:21:53 +10001883 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001884 req_ctx->last = 1;
1885
1886 return ahash_process_req(areq, areq->nbytes);
1887}
1888
Kim Phillips9c4a7962008-06-23 19:50:15 +08001889struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001890 u32 type;
1891 union {
1892 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001893 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001894 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001895 __be32 desc_hdr_template;
1896};
1897
1898static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001899 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001900 { .type = CRYPTO_ALG_TYPE_AEAD,
1901 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001902 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1903 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1904 .cra_blocksize = AES_BLOCK_SIZE,
1905 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1906 .cra_type = &crypto_aead_type,
1907 .cra_aead = {
1908 .setkey = aead_setkey,
1909 .setauthsize = aead_setauthsize,
1910 .encrypt = aead_encrypt,
1911 .decrypt = aead_decrypt,
1912 .givencrypt = aead_givencrypt,
1913 .geniv = "<built-in>",
1914 .ivsize = AES_BLOCK_SIZE,
1915 .maxauthsize = SHA1_DIGEST_SIZE,
1916 }
1917 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001918 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1919 DESC_HDR_SEL0_AESU |
1920 DESC_HDR_MODE0_AESU_CBC |
1921 DESC_HDR_SEL1_MDEUA |
1922 DESC_HDR_MODE1_MDEU_INIT |
1923 DESC_HDR_MODE1_MDEU_PAD |
1924 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001925 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001926 { .type = CRYPTO_ALG_TYPE_AEAD,
1927 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001928 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1929 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1930 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1931 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1932 .cra_type = &crypto_aead_type,
1933 .cra_aead = {
1934 .setkey = aead_setkey,
1935 .setauthsize = aead_setauthsize,
1936 .encrypt = aead_encrypt,
1937 .decrypt = aead_decrypt,
1938 .givencrypt = aead_givencrypt,
1939 .geniv = "<built-in>",
1940 .ivsize = DES3_EDE_BLOCK_SIZE,
1941 .maxauthsize = SHA1_DIGEST_SIZE,
1942 }
1943 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001944 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1945 DESC_HDR_SEL0_DEU |
1946 DESC_HDR_MODE0_DEU_CBC |
1947 DESC_HDR_MODE0_DEU_3DES |
1948 DESC_HDR_SEL1_MDEUA |
1949 DESC_HDR_MODE1_MDEU_INIT |
1950 DESC_HDR_MODE1_MDEU_PAD |
1951 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001952 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001953 { .type = CRYPTO_ALG_TYPE_AEAD,
1954 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001955 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1956 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1957 .cra_blocksize = AES_BLOCK_SIZE,
1958 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1959 .cra_type = &crypto_aead_type,
1960 .cra_aead = {
1961 .setkey = aead_setkey,
1962 .setauthsize = aead_setauthsize,
1963 .encrypt = aead_encrypt,
1964 .decrypt = aead_decrypt,
1965 .givencrypt = aead_givencrypt,
1966 .geniv = "<built-in>",
1967 .ivsize = AES_BLOCK_SIZE,
1968 .maxauthsize = SHA256_DIGEST_SIZE,
1969 }
1970 },
Lee Nipper3952f172008-07-10 18:29:18 +08001971 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1972 DESC_HDR_SEL0_AESU |
1973 DESC_HDR_MODE0_AESU_CBC |
1974 DESC_HDR_SEL1_MDEUA |
1975 DESC_HDR_MODE1_MDEU_INIT |
1976 DESC_HDR_MODE1_MDEU_PAD |
1977 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
1978 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001979 { .type = CRYPTO_ALG_TYPE_AEAD,
1980 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001981 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
1982 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
1983 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1984 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1985 .cra_type = &crypto_aead_type,
1986 .cra_aead = {
1987 .setkey = aead_setkey,
1988 .setauthsize = aead_setauthsize,
1989 .encrypt = aead_encrypt,
1990 .decrypt = aead_decrypt,
1991 .givencrypt = aead_givencrypt,
1992 .geniv = "<built-in>",
1993 .ivsize = DES3_EDE_BLOCK_SIZE,
1994 .maxauthsize = SHA256_DIGEST_SIZE,
1995 }
1996 },
Lee Nipper3952f172008-07-10 18:29:18 +08001997 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1998 DESC_HDR_SEL0_DEU |
1999 DESC_HDR_MODE0_DEU_CBC |
2000 DESC_HDR_MODE0_DEU_3DES |
2001 DESC_HDR_SEL1_MDEUA |
2002 DESC_HDR_MODE1_MDEU_INIT |
2003 DESC_HDR_MODE1_MDEU_PAD |
2004 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2005 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002006 { .type = CRYPTO_ALG_TYPE_AEAD,
2007 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002008 .cra_name = "authenc(hmac(md5),cbc(aes))",
2009 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2010 .cra_blocksize = AES_BLOCK_SIZE,
2011 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2012 .cra_type = &crypto_aead_type,
2013 .cra_aead = {
2014 .setkey = aead_setkey,
2015 .setauthsize = aead_setauthsize,
2016 .encrypt = aead_encrypt,
2017 .decrypt = aead_decrypt,
2018 .givencrypt = aead_givencrypt,
2019 .geniv = "<built-in>",
2020 .ivsize = AES_BLOCK_SIZE,
2021 .maxauthsize = MD5_DIGEST_SIZE,
2022 }
2023 },
Lee Nipper3952f172008-07-10 18:29:18 +08002024 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2025 DESC_HDR_SEL0_AESU |
2026 DESC_HDR_MODE0_AESU_CBC |
2027 DESC_HDR_SEL1_MDEUA |
2028 DESC_HDR_MODE1_MDEU_INIT |
2029 DESC_HDR_MODE1_MDEU_PAD |
2030 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2031 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002032 { .type = CRYPTO_ALG_TYPE_AEAD,
2033 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002034 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2035 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2036 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2037 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2038 .cra_type = &crypto_aead_type,
2039 .cra_aead = {
2040 .setkey = aead_setkey,
2041 .setauthsize = aead_setauthsize,
2042 .encrypt = aead_encrypt,
2043 .decrypt = aead_decrypt,
2044 .givencrypt = aead_givencrypt,
2045 .geniv = "<built-in>",
2046 .ivsize = DES3_EDE_BLOCK_SIZE,
2047 .maxauthsize = MD5_DIGEST_SIZE,
2048 }
2049 },
Lee Nipper3952f172008-07-10 18:29:18 +08002050 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2051 DESC_HDR_SEL0_DEU |
2052 DESC_HDR_MODE0_DEU_CBC |
2053 DESC_HDR_MODE0_DEU_3DES |
2054 DESC_HDR_SEL1_MDEUA |
2055 DESC_HDR_MODE1_MDEU_INIT |
2056 DESC_HDR_MODE1_MDEU_PAD |
2057 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002058 },
2059 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002060 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2061 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002062 .cra_name = "cbc(aes)",
2063 .cra_driver_name = "cbc-aes-talitos",
2064 .cra_blocksize = AES_BLOCK_SIZE,
2065 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2066 CRYPTO_ALG_ASYNC,
2067 .cra_type = &crypto_ablkcipher_type,
2068 .cra_ablkcipher = {
2069 .setkey = ablkcipher_setkey,
2070 .encrypt = ablkcipher_encrypt,
2071 .decrypt = ablkcipher_decrypt,
2072 .geniv = "eseqiv",
2073 .min_keysize = AES_MIN_KEY_SIZE,
2074 .max_keysize = AES_MAX_KEY_SIZE,
2075 .ivsize = AES_BLOCK_SIZE,
2076 }
2077 },
2078 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2079 DESC_HDR_SEL0_AESU |
2080 DESC_HDR_MODE0_AESU_CBC,
2081 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002082 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2083 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002084 .cra_name = "cbc(des3_ede)",
2085 .cra_driver_name = "cbc-3des-talitos",
2086 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2087 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2088 CRYPTO_ALG_ASYNC,
2089 .cra_type = &crypto_ablkcipher_type,
2090 .cra_ablkcipher = {
2091 .setkey = ablkcipher_setkey,
2092 .encrypt = ablkcipher_encrypt,
2093 .decrypt = ablkcipher_decrypt,
2094 .geniv = "eseqiv",
2095 .min_keysize = DES3_EDE_KEY_SIZE,
2096 .max_keysize = DES3_EDE_KEY_SIZE,
2097 .ivsize = DES3_EDE_BLOCK_SIZE,
2098 }
2099 },
2100 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2101 DESC_HDR_SEL0_DEU |
2102 DESC_HDR_MODE0_DEU_CBC |
2103 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002104 },
2105 /* AHASH algorithms. */
2106 { .type = CRYPTO_ALG_TYPE_AHASH,
2107 .alg.hash = {
2108 .init = ahash_init,
2109 .update = ahash_update,
2110 .final = ahash_final,
2111 .finup = ahash_finup,
2112 .digest = ahash_digest,
2113 .halg.digestsize = MD5_DIGEST_SIZE,
2114 .halg.base = {
2115 .cra_name = "md5",
2116 .cra_driver_name = "md5-talitos",
2117 .cra_blocksize = MD5_BLOCK_SIZE,
2118 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2119 CRYPTO_ALG_ASYNC,
2120 .cra_type = &crypto_ahash_type
2121 }
2122 },
2123 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2124 DESC_HDR_SEL0_MDEUA |
2125 DESC_HDR_MODE0_MDEU_MD5,
2126 },
2127 { .type = CRYPTO_ALG_TYPE_AHASH,
2128 .alg.hash = {
2129 .init = ahash_init,
2130 .update = ahash_update,
2131 .final = ahash_final,
2132 .finup = ahash_finup,
2133 .digest = ahash_digest,
2134 .halg.digestsize = SHA1_DIGEST_SIZE,
2135 .halg.base = {
2136 .cra_name = "sha1",
2137 .cra_driver_name = "sha1-talitos",
2138 .cra_blocksize = SHA1_BLOCK_SIZE,
2139 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2140 CRYPTO_ALG_ASYNC,
2141 .cra_type = &crypto_ahash_type
2142 }
2143 },
2144 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2145 DESC_HDR_SEL0_MDEUA |
2146 DESC_HDR_MODE0_MDEU_SHA1,
2147 },
2148 { .type = CRYPTO_ALG_TYPE_AHASH,
2149 .alg.hash = {
2150 .init = ahash_init,
2151 .update = ahash_update,
2152 .final = ahash_final,
2153 .finup = ahash_finup,
2154 .digest = ahash_digest,
Kim Phillips60f208d2010-05-19 19:21:53 +10002155 .halg.digestsize = SHA224_DIGEST_SIZE,
2156 .halg.base = {
2157 .cra_name = "sha224",
2158 .cra_driver_name = "sha224-talitos",
2159 .cra_blocksize = SHA224_BLOCK_SIZE,
2160 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2161 CRYPTO_ALG_ASYNC,
2162 .cra_type = &crypto_ahash_type
2163 }
2164 },
2165 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2166 DESC_HDR_SEL0_MDEUA |
2167 DESC_HDR_MODE0_MDEU_SHA224,
2168 },
2169 { .type = CRYPTO_ALG_TYPE_AHASH,
2170 .alg.hash = {
2171 .init = ahash_init,
2172 .update = ahash_update,
2173 .final = ahash_final,
2174 .finup = ahash_finup,
2175 .digest = ahash_digest,
Lee Nipper497f2e62010-05-19 19:20:36 +10002176 .halg.digestsize = SHA256_DIGEST_SIZE,
2177 .halg.base = {
2178 .cra_name = "sha256",
2179 .cra_driver_name = "sha256-talitos",
2180 .cra_blocksize = SHA256_BLOCK_SIZE,
2181 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2182 CRYPTO_ALG_ASYNC,
2183 .cra_type = &crypto_ahash_type
2184 }
2185 },
2186 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2187 DESC_HDR_SEL0_MDEUA |
2188 DESC_HDR_MODE0_MDEU_SHA256,
2189 },
2190 { .type = CRYPTO_ALG_TYPE_AHASH,
2191 .alg.hash = {
2192 .init = ahash_init,
2193 .update = ahash_update,
2194 .final = ahash_final,
2195 .finup = ahash_finup,
2196 .digest = ahash_digest,
2197 .halg.digestsize = SHA384_DIGEST_SIZE,
2198 .halg.base = {
2199 .cra_name = "sha384",
2200 .cra_driver_name = "sha384-talitos",
2201 .cra_blocksize = SHA384_BLOCK_SIZE,
2202 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2203 CRYPTO_ALG_ASYNC,
2204 .cra_type = &crypto_ahash_type
2205 }
2206 },
2207 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2208 DESC_HDR_SEL0_MDEUB |
2209 DESC_HDR_MODE0_MDEUB_SHA384,
2210 },
2211 { .type = CRYPTO_ALG_TYPE_AHASH,
2212 .alg.hash = {
2213 .init = ahash_init,
2214 .update = ahash_update,
2215 .final = ahash_final,
2216 .finup = ahash_finup,
2217 .digest = ahash_digest,
2218 .halg.digestsize = SHA512_DIGEST_SIZE,
2219 .halg.base = {
2220 .cra_name = "sha512",
2221 .cra_driver_name = "sha512-talitos",
2222 .cra_blocksize = SHA512_BLOCK_SIZE,
2223 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2224 CRYPTO_ALG_ASYNC,
2225 .cra_type = &crypto_ahash_type
2226 }
2227 },
2228 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2229 DESC_HDR_SEL0_MDEUB |
2230 DESC_HDR_MODE0_MDEUB_SHA512,
2231 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002232};
2233
2234struct talitos_crypto_alg {
2235 struct list_head entry;
2236 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002237 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002238};
2239
2240static int talitos_cra_init(struct crypto_tfm *tfm)
2241{
2242 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002243 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002244 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2245
Lee Nipper497f2e62010-05-19 19:20:36 +10002246 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2247 talitos_alg = container_of(__crypto_ahash_alg(alg),
2248 struct talitos_crypto_alg,
2249 algt.alg.hash);
2250 else
2251 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2252 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002253
Kim Phillips9c4a7962008-06-23 19:50:15 +08002254 /* update context with ptr to dev */
2255 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002256
Kim Phillips9c4a7962008-06-23 19:50:15 +08002257 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002258 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002259
Lee Nipper497f2e62010-05-19 19:20:36 +10002260 return 0;
2261}
2262
2263static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2264{
2265 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2266
2267 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002268
2269 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002270 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002271
2272 return 0;
2273}
2274
Lee Nipper497f2e62010-05-19 19:20:36 +10002275static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2276{
2277 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2278
2279 talitos_cra_init(tfm);
2280
2281 ctx->keylen = 0;
2282 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2283 sizeof(struct talitos_ahash_req_ctx));
2284
2285 return 0;
2286}
2287
Kim Phillips9c4a7962008-06-23 19:50:15 +08002288/*
2289 * given the alg's descriptor header template, determine whether descriptor
2290 * type and primary/secondary execution units required match the hw
2291 * capabilities description provided in the device tree node.
2292 */
2293static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2294{
2295 struct talitos_private *priv = dev_get_drvdata(dev);
2296 int ret;
2297
2298 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2299 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2300
2301 if (SECONDARY_EU(desc_hdr_template))
2302 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2303 & priv->exec_units);
2304
2305 return ret;
2306}
2307
Al Viro596f1032008-11-22 17:34:24 +00002308static int talitos_remove(struct of_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002309{
2310 struct device *dev = &ofdev->dev;
2311 struct talitos_private *priv = dev_get_drvdata(dev);
2312 struct talitos_crypto_alg *t_alg, *n;
2313 int i;
2314
2315 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002316 switch (t_alg->algt.type) {
2317 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2318 case CRYPTO_ALG_TYPE_AEAD:
2319 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2320 break;
2321 case CRYPTO_ALG_TYPE_AHASH:
2322 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2323 break;
2324 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002325 list_del(&t_alg->entry);
2326 kfree(t_alg);
2327 }
2328
2329 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2330 talitos_unregister_rng(dev);
2331
Kim Phillips4b9926282009-08-13 11:50:38 +10002332 for (i = 0; i < priv->num_channels; i++)
2333 if (priv->chan[i].fifo)
2334 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002335
Kim Phillips4b9926282009-08-13 11:50:38 +10002336 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002337
2338 if (priv->irq != NO_IRQ) {
2339 free_irq(priv->irq, dev);
2340 irq_dispose_mapping(priv->irq);
2341 }
2342
2343 tasklet_kill(&priv->done_task);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002344
2345 iounmap(priv->reg);
2346
2347 dev_set_drvdata(dev, NULL);
2348
2349 kfree(priv);
2350
2351 return 0;
2352}
2353
2354static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2355 struct talitos_alg_template
2356 *template)
2357{
Kim Phillips60f208d2010-05-19 19:21:53 +10002358 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002359 struct talitos_crypto_alg *t_alg;
2360 struct crypto_alg *alg;
2361
2362 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2363 if (!t_alg)
2364 return ERR_PTR(-ENOMEM);
2365
Lee Nipperacbf7c622010-05-19 19:19:33 +10002366 t_alg->algt = *template;
2367
2368 switch (t_alg->algt.type) {
2369 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002370 alg = &t_alg->algt.alg.crypto;
2371 alg->cra_init = talitos_cra_init;
2372 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002373 case CRYPTO_ALG_TYPE_AEAD:
2374 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002375 alg->cra_init = talitos_cra_init_aead;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002376 break;
2377 case CRYPTO_ALG_TYPE_AHASH:
2378 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002379 alg->cra_init = talitos_cra_init_ahash;
Kim Phillips60f208d2010-05-19 19:21:53 +10002380 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
2381 !strcmp(alg->cra_name, "sha224")) {
2382 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2383 t_alg->algt.desc_hdr_template =
2384 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2385 DESC_HDR_SEL0_MDEUA |
2386 DESC_HDR_MODE0_MDEU_SHA256;
2387 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002388 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002389 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002390
Kim Phillips9c4a7962008-06-23 19:50:15 +08002391 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002392 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002393 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002394 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002395
Kim Phillips9c4a7962008-06-23 19:50:15 +08002396 t_alg->dev = dev;
2397
2398 return t_alg;
2399}
2400
2401static int talitos_probe(struct of_device *ofdev,
2402 const struct of_device_id *match)
2403{
2404 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002405 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002406 struct talitos_private *priv;
2407 const unsigned int *prop;
2408 int i, err;
2409
2410 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2411 if (!priv)
2412 return -ENOMEM;
2413
2414 dev_set_drvdata(dev, priv);
2415
2416 priv->ofdev = ofdev;
2417
2418 tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002419
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002420 INIT_LIST_HEAD(&priv->alg_list);
2421
Kim Phillips9c4a7962008-06-23 19:50:15 +08002422 priv->irq = irq_of_parse_and_map(np, 0);
2423
2424 if (priv->irq == NO_IRQ) {
2425 dev_err(dev, "failed to map irq\n");
2426 err = -EINVAL;
2427 goto err_out;
2428 }
2429
2430 /* get the irq line */
2431 err = request_irq(priv->irq, talitos_interrupt, 0,
2432 dev_driver_string(dev), dev);
2433 if (err) {
2434 dev_err(dev, "failed to request irq %d\n", priv->irq);
2435 irq_dispose_mapping(priv->irq);
2436 priv->irq = NO_IRQ;
2437 goto err_out;
2438 }
2439
2440 priv->reg = of_iomap(np, 0);
2441 if (!priv->reg) {
2442 dev_err(dev, "failed to of_iomap\n");
2443 err = -ENOMEM;
2444 goto err_out;
2445 }
2446
2447 /* get SEC version capabilities from device tree */
2448 prop = of_get_property(np, "fsl,num-channels", NULL);
2449 if (prop)
2450 priv->num_channels = *prop;
2451
2452 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2453 if (prop)
2454 priv->chfifo_len = *prop;
2455
2456 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2457 if (prop)
2458 priv->exec_units = *prop;
2459
2460 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2461 if (prop)
2462 priv->desc_types = *prop;
2463
2464 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2465 !priv->exec_units || !priv->desc_types) {
2466 dev_err(dev, "invalid property data in device tree node\n");
2467 err = -EINVAL;
2468 goto err_out;
2469 }
2470
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002471 if (of_device_is_compatible(np, "fsl,sec3.0"))
2472 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2473
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002474 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002475 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
2476 TALITOS_FTR_SHA224_HWINIT;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002477
Kim Phillips4b9926282009-08-13 11:50:38 +10002478 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2479 priv->num_channels, GFP_KERNEL);
2480 if (!priv->chan) {
2481 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002482 err = -ENOMEM;
2483 goto err_out;
2484 }
2485
2486 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002487 spin_lock_init(&priv->chan[i].head_lock);
2488 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002489 }
2490
2491 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2492
2493 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002494 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2495 priv->fifo_len, GFP_KERNEL);
2496 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002497 dev_err(dev, "failed to allocate request fifo %d\n", i);
2498 err = -ENOMEM;
2499 goto err_out;
2500 }
2501 }
2502
Kim Phillipsec6644d2008-07-17 20:16:40 +08002503 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002504 atomic_set(&priv->chan[i].submit_count,
2505 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002506
Kim Phillips81eb0242009-08-13 11:51:51 +10002507 dma_set_mask(dev, DMA_BIT_MASK(36));
2508
Kim Phillips9c4a7962008-06-23 19:50:15 +08002509 /* reset and initialize the h/w */
2510 err = init_device(dev);
2511 if (err) {
2512 dev_err(dev, "failed to initialize device\n");
2513 goto err_out;
2514 }
2515
2516 /* register the RNG, if available */
2517 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2518 err = talitos_register_rng(dev);
2519 if (err) {
2520 dev_err(dev, "failed to register hwrng: %d\n", err);
2521 goto err_out;
2522 } else
2523 dev_info(dev, "hwrng\n");
2524 }
2525
2526 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002527 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2528 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2529 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002530 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002531
2532 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2533 if (IS_ERR(t_alg)) {
2534 err = PTR_ERR(t_alg);
2535 goto err_out;
2536 }
2537
Lee Nipperacbf7c622010-05-19 19:19:33 +10002538 switch (t_alg->algt.type) {
2539 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2540 case CRYPTO_ALG_TYPE_AEAD:
2541 err = crypto_register_alg(
2542 &t_alg->algt.alg.crypto);
2543 name = t_alg->algt.alg.crypto.cra_driver_name;
2544 break;
2545 case CRYPTO_ALG_TYPE_AHASH:
2546 err = crypto_register_ahash(
2547 &t_alg->algt.alg.hash);
2548 name =
2549 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2550 break;
2551 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002552 if (err) {
2553 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002554 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002555 kfree(t_alg);
2556 } else {
2557 list_add_tail(&t_alg->entry, &priv->alg_list);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002558 dev_info(dev, "%s\n", name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002559 }
2560 }
2561 }
2562
2563 return 0;
2564
2565err_out:
2566 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002567
2568 return err;
2569}
2570
Márton Németh6c3f9752010-01-17 21:54:01 +11002571static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002572 {
2573 .compatible = "fsl,sec2.0",
2574 },
2575 {},
2576};
2577MODULE_DEVICE_TABLE(of, talitos_match);
2578
2579static struct of_platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002580 .driver = {
2581 .name = "talitos",
2582 .owner = THIS_MODULE,
2583 .of_match_table = talitos_match,
2584 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002585 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002586 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002587};
2588
2589static int __init talitos_init(void)
2590{
2591 return of_register_platform_driver(&talitos_driver);
2592}
2593module_init(talitos_init);
2594
2595static void __exit talitos_exit(void)
2596{
2597 of_unregister_platform_driver(&talitos_driver);
2598}
2599module_exit(talitos_exit);
2600
2601MODULE_LICENSE("GPL");
2602MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2603MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");