blob: dbe76b5df9cf2550b2e2621e2b051cf490396f76 [file] [log] [blame]
Kim Phillips9c4a7962008-06-23 19:50:15 +08001/*
2 * talitos - Freescale Integrated Security Engine (SEC) device driver
3 *
Kim Phillips5228f0f2011-07-15 11:21:38 +08004 * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
Kim Phillips9c4a7962008-06-23 19:50:15 +08005 *
6 * Scatterlist Crypto API glue code copied from files with the following:
7 * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * Crypto algorithm registration code copied from hifn driver:
10 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/mod_devicetable.h>
31#include <linux/device.h>
32#include <linux/interrupt.h>
33#include <linux/crypto.h>
34#include <linux/hw_random.h>
35#include <linux/of_platform.h>
36#include <linux/dma-mapping.h>
37#include <linux/io.h>
38#include <linux/spinlock.h>
39#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080041
42#include <crypto/algapi.h>
43#include <crypto/aes.h>
Lee Nipper3952f172008-07-10 18:29:18 +080044#include <crypto/des.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080045#include <crypto/sha.h>
Lee Nipper497f2e62010-05-19 19:20:36 +100046#include <crypto/md5.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080047#include <crypto/aead.h>
48#include <crypto/authenc.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080049#include <crypto/skcipher.h>
Lee Nipperacbf7c622010-05-19 19:19:33 +100050#include <crypto/hash.h>
51#include <crypto/internal/hash.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080052#include <crypto/scatterwalk.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080053
54#include "talitos.h"
55
56#define TALITOS_TIMEOUT 100000
57#define TALITOS_MAX_DATA_LEN 65535
58
59#define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
60#define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
61#define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
62
63/* descriptor pointer entry */
64struct talitos_ptr {
65 __be16 len; /* length */
66 u8 j_extent; /* jump to sg link table and/or extent */
67 u8 eptr; /* extended address */
68 __be32 ptr; /* address */
69};
70
Lee Nipper497f2e62010-05-19 19:20:36 +100071static const struct talitos_ptr zero_entry = {
72 .len = 0,
73 .j_extent = 0,
74 .eptr = 0,
75 .ptr = 0
76};
77
Kim Phillips9c4a7962008-06-23 19:50:15 +080078/* descriptor */
79struct talitos_desc {
80 __be32 hdr; /* header high bits */
81 __be32 hdr_lo; /* header low bits */
82 struct talitos_ptr ptr[7]; /* ptr/len pair array */
83};
84
85/**
86 * talitos_request - descriptor submission request
87 * @desc: descriptor pointer (kernel virtual)
88 * @dma_desc: descriptor's physical bus address
89 * @callback: whom to call when descriptor processing is done
90 * @context: caller context (optional)
91 */
92struct talitos_request {
93 struct talitos_desc *desc;
94 dma_addr_t dma_desc;
95 void (*callback) (struct device *dev, struct talitos_desc *desc,
96 void *context, int error);
97 void *context;
98};
99
Kim Phillips4b9926282009-08-13 11:50:38 +1000100/* per-channel fifo management */
101struct talitos_channel {
102 /* request fifo */
103 struct talitos_request *fifo;
104
105 /* number of requests pending in channel h/w fifo */
106 atomic_t submit_count ____cacheline_aligned;
107
108 /* request submission (head) lock */
109 spinlock_t head_lock ____cacheline_aligned;
110 /* index to next free descriptor request */
111 int head;
112
113 /* request release (tail) lock */
114 spinlock_t tail_lock ____cacheline_aligned;
115 /* index to next in-progress/done descriptor request */
116 int tail;
117};
118
Kim Phillips9c4a7962008-06-23 19:50:15 +0800119struct talitos_private {
120 struct device *dev;
Grant Likely2dc11582010-08-06 09:25:50 -0600121 struct platform_device *ofdev;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800122 void __iomem *reg;
123 int irq;
124
125 /* SEC version geometry (from device tree node) */
126 unsigned int num_channels;
127 unsigned int chfifo_len;
128 unsigned int exec_units;
129 unsigned int desc_types;
130
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800131 /* SEC Compatibility info */
132 unsigned long features;
133
Kim Phillips9c4a7962008-06-23 19:50:15 +0800134 /*
135 * length of the request fifo
136 * fifo_len is chfifo_len rounded up to next power of 2
137 * so we can use bitwise ops to wrap
138 */
139 unsigned int fifo_len;
140
Kim Phillips4b9926282009-08-13 11:50:38 +1000141 struct talitos_channel *chan;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800142
Kim Phillips4b9926282009-08-13 11:50:38 +1000143 /* next channel to be assigned next incoming descriptor */
144 atomic_t last_chan ____cacheline_aligned;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800145
146 /* request callback tasklet */
147 struct tasklet_struct done_task;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800148
149 /* list of registered algorithms */
150 struct list_head alg_list;
151
152 /* hwrng device */
153 struct hwrng rng;
154};
155
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800156/* .features flag */
157#define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800158#define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
Kim Phillips60f208d2010-05-19 19:21:53 +1000159#define TALITOS_FTR_SHA224_HWINIT 0x00000004
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800160
Kim Phillips81eb0242009-08-13 11:51:51 +1000161static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
162{
163 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +0800164 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +1000165}
166
Kim Phillips9c4a7962008-06-23 19:50:15 +0800167/*
168 * map virtual single (contiguous) pointer to h/w descriptor pointer
169 */
170static void map_single_talitos_ptr(struct device *dev,
171 struct talitos_ptr *talitos_ptr,
172 unsigned short len, void *data,
173 unsigned char extent,
174 enum dma_data_direction dir)
175{
Kim Phillips81eb0242009-08-13 11:51:51 +1000176 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
177
Kim Phillips9c4a7962008-06-23 19:50:15 +0800178 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +1000179 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800180 talitos_ptr->j_extent = extent;
181}
182
183/*
184 * unmap bus single (contiguous) h/w descriptor pointer
185 */
186static void unmap_single_talitos_ptr(struct device *dev,
187 struct talitos_ptr *talitos_ptr,
188 enum dma_data_direction dir)
189{
190 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
191 be16_to_cpu(talitos_ptr->len), dir);
192}
193
194static int reset_channel(struct device *dev, int ch)
195{
196 struct talitos_private *priv = dev_get_drvdata(dev);
197 unsigned int timeout = TALITOS_TIMEOUT;
198
199 setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
200
201 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
202 && --timeout)
203 cpu_relax();
204
205 if (timeout == 0) {
206 dev_err(dev, "failed to reset channel %d\n", ch);
207 return -EIO;
208 }
209
Kim Phillips81eb0242009-08-13 11:51:51 +1000210 /* set 36-bit addressing, done writeback enable and done IRQ enable */
211 setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE |
212 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800213
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800214 /* and ICCR writeback, if available */
215 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
216 setbits32(priv->reg + TALITOS_CCCR_LO(ch),
217 TALITOS_CCCR_LO_IWSE);
218
Kim Phillips9c4a7962008-06-23 19:50:15 +0800219 return 0;
220}
221
222static int reset_device(struct device *dev)
223{
224 struct talitos_private *priv = dev_get_drvdata(dev);
225 unsigned int timeout = TALITOS_TIMEOUT;
226
227 setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
228
229 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
230 && --timeout)
231 cpu_relax();
232
233 if (timeout == 0) {
234 dev_err(dev, "failed to reset device\n");
235 return -EIO;
236 }
237
238 return 0;
239}
240
241/*
242 * Reset and initialize the device
243 */
244static int init_device(struct device *dev)
245{
246 struct talitos_private *priv = dev_get_drvdata(dev);
247 int ch, err;
248
249 /*
250 * Master reset
251 * errata documentation: warning: certain SEC interrupts
252 * are not fully cleared by writing the MCR:SWR bit,
253 * set bit twice to completely reset
254 */
255 err = reset_device(dev);
256 if (err)
257 return err;
258
259 err = reset_device(dev);
260 if (err)
261 return err;
262
263 /* reset channels */
264 for (ch = 0; ch < priv->num_channels; ch++) {
265 err = reset_channel(dev, ch);
266 if (err)
267 return err;
268 }
269
270 /* enable channel done and error interrupts */
271 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
272 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
273
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800274 /* disable integrity check error interrupts (use writeback instead) */
275 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
276 setbits32(priv->reg + TALITOS_MDEUICR_LO,
277 TALITOS_MDEUICR_LO_ICE);
278
Kim Phillips9c4a7962008-06-23 19:50:15 +0800279 return 0;
280}
281
282/**
283 * talitos_submit - submits a descriptor to the device for processing
284 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800285 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800286 * @desc: the descriptor to be processed by the device
287 * @callback: whom to call when processing is complete
288 * @context: a handle for use by caller (optional)
289 *
290 * desc must contain valid dma-mapped (bus physical) address pointers.
291 * callback must check err and feedback in descriptor header
292 * for device processing status.
293 */
Kim Phillips5228f0f2011-07-15 11:21:38 +0800294static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800295 void (*callback)(struct device *dev,
296 struct talitos_desc *desc,
297 void *context, int error),
298 void *context)
299{
300 struct talitos_private *priv = dev_get_drvdata(dev);
301 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800302 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800303 int head;
304
Kim Phillips4b9926282009-08-13 11:50:38 +1000305 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800306
Kim Phillips4b9926282009-08-13 11:50:38 +1000307 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800308 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000309 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800310 return -EAGAIN;
311 }
312
Kim Phillips4b9926282009-08-13 11:50:38 +1000313 head = priv->chan[ch].head;
314 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800315
Kim Phillips9c4a7962008-06-23 19:50:15 +0800316 /* map descriptor and save caller data */
317 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
318 DMA_BIDIRECTIONAL);
319 request->callback = callback;
320 request->context = context;
321
322 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000323 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800324
325 smp_wmb();
326 request->desc = desc;
327
328 /* GO! */
329 wmb();
Kim Phillipsa7524472010-09-23 15:56:38 +0800330 out_be32(priv->reg + TALITOS_FF(ch), upper_32_bits(request->dma_desc));
Kim Phillips81eb0242009-08-13 11:51:51 +1000331 out_be32(priv->reg + TALITOS_FF_LO(ch),
Kim Phillipsa7524472010-09-23 15:56:38 +0800332 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800333
Kim Phillips4b9926282009-08-13 11:50:38 +1000334 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800335
336 return -EINPROGRESS;
337}
338
339/*
340 * process what was done, notify callback of error if not
341 */
342static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
343{
344 struct talitos_private *priv = dev_get_drvdata(dev);
345 struct talitos_request *request, saved_req;
346 unsigned long flags;
347 int tail, status;
348
Kim Phillips4b9926282009-08-13 11:50:38 +1000349 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800350
Kim Phillips4b9926282009-08-13 11:50:38 +1000351 tail = priv->chan[ch].tail;
352 while (priv->chan[ch].fifo[tail].desc) {
353 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800354
355 /* descriptors with their done bits set don't get the error */
356 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100357 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800358 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100359 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800360 if (!error)
361 break;
362 else
363 status = error;
364
365 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800366 sizeof(struct talitos_desc),
367 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800368
369 /* copy entries so we can call callback outside lock */
370 saved_req.desc = request->desc;
371 saved_req.callback = request->callback;
372 saved_req.context = request->context;
373
374 /* release request entry in fifo */
375 smp_wmb();
376 request->desc = NULL;
377
378 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000379 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800380
Kim Phillips4b9926282009-08-13 11:50:38 +1000381 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800382
Kim Phillips4b9926282009-08-13 11:50:38 +1000383 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800384
Kim Phillips9c4a7962008-06-23 19:50:15 +0800385 saved_req.callback(dev, saved_req.desc, saved_req.context,
386 status);
387 /* channel may resume processing in single desc error case */
388 if (error && !reset_ch && status == error)
389 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000390 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
391 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800392 }
393
Kim Phillips4b9926282009-08-13 11:50:38 +1000394 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800395}
396
397/*
398 * process completed requests for channels that have done status
399 */
400static void talitos_done(unsigned long data)
401{
402 struct device *dev = (struct device *)data;
403 struct talitos_private *priv = dev_get_drvdata(dev);
404 int ch;
405
406 for (ch = 0; ch < priv->num_channels; ch++)
407 flush_channel(dev, ch, 0, 0);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800408
409 /* At this point, all completed channels have been processed.
410 * Unmask done interrupts for channels completed later on.
411 */
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800412 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
413 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800414}
415
416/*
417 * locate current (offending) descriptor
418 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200419static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800420{
421 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000422 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800423 dma_addr_t cur_desc;
424
425 cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
426
Kim Phillips4b9926282009-08-13 11:50:38 +1000427 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800428 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000429 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800430 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200431 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800432 }
433 }
434
Kim Phillips3e721ae2011-10-21 15:20:28 +0200435 return priv->chan[ch].fifo[tail].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436}
437
438/*
439 * user diagnostics; report root cause of error based on execution unit status
440 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200441static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800442{
443 struct talitos_private *priv = dev_get_drvdata(dev);
444 int i;
445
Kim Phillips3e721ae2011-10-21 15:20:28 +0200446 if (!desc_hdr)
447 desc_hdr = in_be32(priv->reg + TALITOS_DESCBUF(ch));
448
449 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800450 case DESC_HDR_SEL0_AFEU:
451 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
452 in_be32(priv->reg + TALITOS_AFEUISR),
453 in_be32(priv->reg + TALITOS_AFEUISR_LO));
454 break;
455 case DESC_HDR_SEL0_DEU:
456 dev_err(dev, "DEUISR 0x%08x_%08x\n",
457 in_be32(priv->reg + TALITOS_DEUISR),
458 in_be32(priv->reg + TALITOS_DEUISR_LO));
459 break;
460 case DESC_HDR_SEL0_MDEUA:
461 case DESC_HDR_SEL0_MDEUB:
462 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
463 in_be32(priv->reg + TALITOS_MDEUISR),
464 in_be32(priv->reg + TALITOS_MDEUISR_LO));
465 break;
466 case DESC_HDR_SEL0_RNG:
467 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
468 in_be32(priv->reg + TALITOS_RNGUISR),
469 in_be32(priv->reg + TALITOS_RNGUISR_LO));
470 break;
471 case DESC_HDR_SEL0_PKEU:
472 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
473 in_be32(priv->reg + TALITOS_PKEUISR),
474 in_be32(priv->reg + TALITOS_PKEUISR_LO));
475 break;
476 case DESC_HDR_SEL0_AESU:
477 dev_err(dev, "AESUISR 0x%08x_%08x\n",
478 in_be32(priv->reg + TALITOS_AESUISR),
479 in_be32(priv->reg + TALITOS_AESUISR_LO));
480 break;
481 case DESC_HDR_SEL0_CRCU:
482 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
483 in_be32(priv->reg + TALITOS_CRCUISR),
484 in_be32(priv->reg + TALITOS_CRCUISR_LO));
485 break;
486 case DESC_HDR_SEL0_KEU:
487 dev_err(dev, "KEUISR 0x%08x_%08x\n",
488 in_be32(priv->reg + TALITOS_KEUISR),
489 in_be32(priv->reg + TALITOS_KEUISR_LO));
490 break;
491 }
492
Kim Phillips3e721ae2011-10-21 15:20:28 +0200493 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800494 case DESC_HDR_SEL1_MDEUA:
495 case DESC_HDR_SEL1_MDEUB:
496 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
497 in_be32(priv->reg + TALITOS_MDEUISR),
498 in_be32(priv->reg + TALITOS_MDEUISR_LO));
499 break;
500 case DESC_HDR_SEL1_CRCU:
501 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
502 in_be32(priv->reg + TALITOS_CRCUISR),
503 in_be32(priv->reg + TALITOS_CRCUISR_LO));
504 break;
505 }
506
507 for (i = 0; i < 8; i++)
508 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
509 in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
510 in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
511}
512
513/*
514 * recover from error interrupts
515 */
Kim Phillips40405f12008-10-12 20:19:35 +0800516static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800517{
518 struct device *dev = (struct device *)data;
519 struct talitos_private *priv = dev_get_drvdata(dev);
520 unsigned int timeout = TALITOS_TIMEOUT;
521 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800522 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800523
524 for (ch = 0; ch < priv->num_channels; ch++) {
525 /* skip channels without errors */
526 if (!(isr & (1 << (ch * 2 + 1))))
527 continue;
528
529 error = -EINVAL;
530
531 v = in_be32(priv->reg + TALITOS_CCPSR(ch));
532 v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
533
534 if (v_lo & TALITOS_CCPSR_LO_DOF) {
535 dev_err(dev, "double fetch fifo overflow error\n");
536 error = -EAGAIN;
537 reset_ch = 1;
538 }
539 if (v_lo & TALITOS_CCPSR_LO_SOF) {
540 /* h/w dropped descriptor */
541 dev_err(dev, "single fetch fifo overflow error\n");
542 error = -EAGAIN;
543 }
544 if (v_lo & TALITOS_CCPSR_LO_MDTE)
545 dev_err(dev, "master data transfer error\n");
546 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
547 dev_err(dev, "s/g data length zero error\n");
548 if (v_lo & TALITOS_CCPSR_LO_FPZ)
549 dev_err(dev, "fetch pointer zero error\n");
550 if (v_lo & TALITOS_CCPSR_LO_IDH)
551 dev_err(dev, "illegal descriptor header error\n");
552 if (v_lo & TALITOS_CCPSR_LO_IEU)
553 dev_err(dev, "invalid execution unit error\n");
554 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200555 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800556 if (v_lo & TALITOS_CCPSR_LO_GB)
557 dev_err(dev, "gather boundary error\n");
558 if (v_lo & TALITOS_CCPSR_LO_GRL)
559 dev_err(dev, "gather return/length error\n");
560 if (v_lo & TALITOS_CCPSR_LO_SB)
561 dev_err(dev, "scatter boundary error\n");
562 if (v_lo & TALITOS_CCPSR_LO_SRL)
563 dev_err(dev, "scatter return/length error\n");
564
565 flush_channel(dev, ch, error, reset_ch);
566
567 if (reset_ch) {
568 reset_channel(dev, ch);
569 } else {
570 setbits32(priv->reg + TALITOS_CCCR(ch),
571 TALITOS_CCCR_CONT);
572 setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
573 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
574 TALITOS_CCCR_CONT) && --timeout)
575 cpu_relax();
576 if (timeout == 0) {
577 dev_err(dev, "failed to restart channel %d\n",
578 ch);
579 reset_dev = 1;
580 }
581 }
582 }
583 if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
584 dev_err(dev, "done overflow, internal time out, or rngu error: "
585 "ISR 0x%08x_%08x\n", isr, isr_lo);
586
587 /* purge request queues */
588 for (ch = 0; ch < priv->num_channels; ch++)
589 flush_channel(dev, ch, -EIO, 1);
590
591 /* reset and reinitialize the device */
592 init_device(dev);
593 }
594}
595
596static irqreturn_t talitos_interrupt(int irq, void *data)
597{
598 struct device *dev = data;
599 struct talitos_private *priv = dev_get_drvdata(dev);
600 u32 isr, isr_lo;
601
602 isr = in_be32(priv->reg + TALITOS_ISR);
603 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
Lee Nipperca38a812008-12-20 17:09:25 +1100604 /* Acknowledge interrupt */
605 out_be32(priv->reg + TALITOS_ICR, isr);
606 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800607
Lee Nipperca38a812008-12-20 17:09:25 +1100608 if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
Kim Phillips40405f12008-10-12 20:19:35 +0800609 talitos_error((unsigned long)data, isr, isr_lo);
Lee Nipperca38a812008-12-20 17:09:25 +1100610 else
Lee Nipper1c2e8812008-10-12 20:29:34 +0800611 if (likely(isr & TALITOS_ISR_CHDONE)) {
612 /* mask further done interrupts. */
613 clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
614 /* done_task will unmask done interrupts at exit */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800615 tasklet_schedule(&priv->done_task);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800616 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800617
618 return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
619}
620
621/*
622 * hwrng
623 */
624static int talitos_rng_data_present(struct hwrng *rng, int wait)
625{
626 struct device *dev = (struct device *)rng->priv;
627 struct talitos_private *priv = dev_get_drvdata(dev);
628 u32 ofl;
629 int i;
630
631 for (i = 0; i < 20; i++) {
632 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
633 TALITOS_RNGUSR_LO_OFL;
634 if (ofl || !wait)
635 break;
636 udelay(10);
637 }
638
639 return !!ofl;
640}
641
642static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
643{
644 struct device *dev = (struct device *)rng->priv;
645 struct talitos_private *priv = dev_get_drvdata(dev);
646
647 /* rng fifo requires 64-bit accesses */
648 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
649 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
650
651 return sizeof(u32);
652}
653
654static int talitos_rng_init(struct hwrng *rng)
655{
656 struct device *dev = (struct device *)rng->priv;
657 struct talitos_private *priv = dev_get_drvdata(dev);
658 unsigned int timeout = TALITOS_TIMEOUT;
659
660 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
661 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
662 && --timeout)
663 cpu_relax();
664 if (timeout == 0) {
665 dev_err(dev, "failed to reset rng hw\n");
666 return -ENODEV;
667 }
668
669 /* start generating */
670 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
671
672 return 0;
673}
674
675static int talitos_register_rng(struct device *dev)
676{
677 struct talitos_private *priv = dev_get_drvdata(dev);
678
679 priv->rng.name = dev_driver_string(dev),
680 priv->rng.init = talitos_rng_init,
681 priv->rng.data_present = talitos_rng_data_present,
682 priv->rng.data_read = talitos_rng_data_read,
683 priv->rng.priv = (unsigned long)dev;
684
685 return hwrng_register(&priv->rng);
686}
687
688static void talitos_unregister_rng(struct device *dev)
689{
690 struct talitos_private *priv = dev_get_drvdata(dev);
691
692 hwrng_unregister(&priv->rng);
693}
694
695/*
696 * crypto alg
697 */
698#define TALITOS_CRA_PRIORITY 3000
699#define TALITOS_MAX_KEY_SIZE 64
Lee Nipper3952f172008-07-10 18:29:18 +0800700#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800701
Lee Nipper497f2e62010-05-19 19:20:36 +1000702#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800703
704struct talitos_ctx {
705 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800706 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800707 __be32 desc_hdr_template;
708 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800709 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800710 unsigned int keylen;
711 unsigned int enckeylen;
712 unsigned int authkeylen;
713 unsigned int authsize;
714};
715
Lee Nipper497f2e62010-05-19 19:20:36 +1000716#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
717#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
718
719struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000720 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000721 unsigned int hw_context_size;
722 u8 buf[HASH_MAX_BLOCK_SIZE];
723 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000724 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000725 unsigned int first;
726 unsigned int last;
727 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000728 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000729 struct scatterlist bufsl[2];
730 struct scatterlist *psrc;
731};
732
Lee Nipper56af8cd2009-03-29 15:50:50 +0800733static int aead_setauthsize(struct crypto_aead *authenc,
734 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800735{
736 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
737
738 ctx->authsize = authsize;
739
740 return 0;
741}
742
Lee Nipper56af8cd2009-03-29 15:50:50 +0800743static int aead_setkey(struct crypto_aead *authenc,
744 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800745{
746 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
747 struct rtattr *rta = (void *)key;
748 struct crypto_authenc_key_param *param;
749 unsigned int authkeylen;
750 unsigned int enckeylen;
751
752 if (!RTA_OK(rta, keylen))
753 goto badkey;
754
755 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
756 goto badkey;
757
758 if (RTA_PAYLOAD(rta) < sizeof(*param))
759 goto badkey;
760
761 param = RTA_DATA(rta);
762 enckeylen = be32_to_cpu(param->enckeylen);
763
764 key += RTA_ALIGN(rta->rta_len);
765 keylen -= RTA_ALIGN(rta->rta_len);
766
767 if (keylen < enckeylen)
768 goto badkey;
769
770 authkeylen = keylen - enckeylen;
771
772 if (keylen > TALITOS_MAX_KEY_SIZE)
773 goto badkey;
774
775 memcpy(&ctx->key, key, keylen);
776
777 ctx->keylen = keylen;
778 ctx->enckeylen = enckeylen;
779 ctx->authkeylen = authkeylen;
780
781 return 0;
782
783badkey:
784 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
785 return -EINVAL;
786}
787
788/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800789 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800790 * @src_nents: number of segments in input scatterlist
791 * @dst_nents: number of segments in output scatterlist
792 * @dma_len: length of dma mapped link_tbl space
793 * @dma_link_tbl: bus physical address of link_tbl
794 * @desc: h/w descriptor
795 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
796 *
797 * if decrypting (with authcheck), or either one of src_nents or dst_nents
798 * is greater than 1, an integrity check value is concatenated to the end
799 * of link_tbl data
800 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800801struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800802 int src_nents;
803 int dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800804 int src_is_chained;
805 int dst_is_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800806 int dma_len;
807 dma_addr_t dma_link_tbl;
808 struct talitos_desc desc;
809 struct talitos_ptr link_tbl[0];
810};
811
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800812static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
813 unsigned int nents, enum dma_data_direction dir,
814 int chained)
815{
816 if (unlikely(chained))
817 while (sg) {
818 dma_map_sg(dev, sg, 1, dir);
819 sg = scatterwalk_sg_next(sg);
820 }
821 else
822 dma_map_sg(dev, sg, nents, dir);
823 return nents;
824}
825
826static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
827 enum dma_data_direction dir)
828{
829 while (sg) {
830 dma_unmap_sg(dev, sg, 1, dir);
831 sg = scatterwalk_sg_next(sg);
832 }
833}
834
835static void talitos_sg_unmap(struct device *dev,
836 struct talitos_edesc *edesc,
837 struct scatterlist *src,
838 struct scatterlist *dst)
839{
840 unsigned int src_nents = edesc->src_nents ? : 1;
841 unsigned int dst_nents = edesc->dst_nents ? : 1;
842
843 if (src != dst) {
844 if (edesc->src_is_chained)
845 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
846 else
847 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
848
Lee Nipper497f2e62010-05-19 19:20:36 +1000849 if (dst) {
850 if (edesc->dst_is_chained)
851 talitos_unmap_sg_chain(dev, dst,
852 DMA_FROM_DEVICE);
853 else
854 dma_unmap_sg(dev, dst, dst_nents,
855 DMA_FROM_DEVICE);
856 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800857 } else
858 if (edesc->src_is_chained)
859 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
860 else
861 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
862}
863
Kim Phillips9c4a7962008-06-23 19:50:15 +0800864static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800865 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800866 struct aead_request *areq)
867{
868 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
869 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
870 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
871 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
872
873 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
874
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800875 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800876
877 if (edesc->dma_len)
878 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
879 DMA_BIDIRECTIONAL);
880}
881
882/*
883 * ipsec_esp descriptor callbacks
884 */
885static void ipsec_esp_encrypt_done(struct device *dev,
886 struct talitos_desc *desc, void *context,
887 int err)
888{
889 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800890 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
891 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800892 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800893 struct scatterlist *sg;
894 void *icvdata;
895
Kim Phillips19bbbc62009-03-29 15:53:59 +0800896 edesc = container_of(desc, struct talitos_edesc, desc);
897
Kim Phillips9c4a7962008-06-23 19:50:15 +0800898 ipsec_esp_unmap(dev, edesc, areq);
899
900 /* copy the generated ICV to dst */
901 if (edesc->dma_len) {
902 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800903 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800904 sg = sg_last(areq->dst, edesc->dst_nents);
905 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
906 icvdata, ctx->authsize);
907 }
908
909 kfree(edesc);
910
911 aead_request_complete(areq, err);
912}
913
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800914static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800915 struct talitos_desc *desc,
916 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800917{
918 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800919 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
920 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800921 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800922 struct scatterlist *sg;
923 void *icvdata;
924
Kim Phillips19bbbc62009-03-29 15:53:59 +0800925 edesc = container_of(desc, struct talitos_edesc, desc);
926
Kim Phillips9c4a7962008-06-23 19:50:15 +0800927 ipsec_esp_unmap(dev, edesc, req);
928
929 if (!err) {
930 /* auth check */
931 if (edesc->dma_len)
932 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800933 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800934 else
935 icvdata = &edesc->link_tbl[0];
936
937 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
938 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
939 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
940 }
941
942 kfree(edesc);
943
944 aead_request_complete(req, err);
945}
946
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800947static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800948 struct talitos_desc *desc,
949 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800950{
951 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800952 struct talitos_edesc *edesc;
953
954 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800955
956 ipsec_esp_unmap(dev, edesc, req);
957
958 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800959 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
960 DESC_HDR_LO_ICCR1_PASS))
961 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800962
963 kfree(edesc);
964
965 aead_request_complete(req, err);
966}
967
Kim Phillips9c4a7962008-06-23 19:50:15 +0800968/*
969 * convert scatterlist to SEC h/w link table format
970 * stop at cryptlen bytes
971 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800972static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800973 int cryptlen, struct talitos_ptr *link_tbl_ptr)
974{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800975 int n_sg = sg_count;
976
977 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000978 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800979 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
980 link_tbl_ptr->j_extent = 0;
981 link_tbl_ptr++;
982 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800983 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800984 }
985
Lee Nipper70bcaca2008-07-03 19:08:46 +0800986 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800987 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800988 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800989 /* Empty this entry, and move to previous one */
990 cryptlen += be16_to_cpu(link_tbl_ptr->len);
991 link_tbl_ptr->len = 0;
992 sg_count--;
993 link_tbl_ptr--;
994 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800995 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
996 + cryptlen);
997
998 /* tag end of link table */
999 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +08001000
1001 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001002}
1003
1004/*
1005 * fill in and submit ipsec_esp descriptor
1006 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001007static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001008 u8 *giv, u64 seq,
1009 void (*callback) (struct device *dev,
1010 struct talitos_desc *desc,
1011 void *context, int error))
1012{
1013 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1014 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1015 struct device *dev = ctx->dev;
1016 struct talitos_desc *desc = &edesc->desc;
1017 unsigned int cryptlen = areq->cryptlen;
1018 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +10001019 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001020 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001021 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001022
1023 /* hmac key */
1024 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
1025 0, DMA_TO_DEVICE);
1026 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +10001027 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1028 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001029 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001030 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
1031 DMA_TO_DEVICE);
1032
1033 /* cipher key */
1034 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1035 (char *)&ctx->key + ctx->authkeylen, 0,
1036 DMA_TO_DEVICE);
1037
1038 /*
1039 * cipher in
1040 * map and adjust cipher len to aead request cryptlen.
1041 * extent is bytes of HMAC postpended to ciphertext,
1042 * typically 12 for ipsec
1043 */
1044 desc->ptr[4].len = cpu_to_be16(cryptlen);
1045 desc->ptr[4].j_extent = authsize;
1046
Kim Phillipse938e462009-03-29 15:53:23 +08001047 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1048 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1049 : DMA_TO_DEVICE,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001050 edesc->src_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001051
1052 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001053 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001054 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001055 sg_link_tbl_len = cryptlen;
1056
Kim Phillips962a9c92009-03-29 15:54:30 +08001057 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001058 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001059
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001060 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001061 &edesc->link_tbl[0]);
1062 if (sg_count > 1) {
1063 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001064 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001065 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1066 edesc->dma_len,
1067 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001068 } else {
1069 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001070 to_talitos_ptr(&desc->ptr[4],
1071 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001072 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001073 }
1074
1075 /* cipher out */
1076 desc->ptr[5].len = cpu_to_be16(cryptlen);
1077 desc->ptr[5].j_extent = authsize;
1078
Kim Phillipse938e462009-03-29 15:53:23 +08001079 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001080 sg_count = talitos_map_sg(dev, areq->dst,
1081 edesc->dst_nents ? : 1,
1082 DMA_FROM_DEVICE,
1083 edesc->dst_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001084
1085 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001086 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001087 } else {
1088 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001089 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001090
Kim Phillips81eb0242009-08-13 11:51:51 +10001091 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1092 (edesc->src_nents + 1) *
1093 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001094 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1095 link_tbl_ptr);
1096
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001097 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001098 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001099 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001100 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001101 link_tbl_ptr++;
1102 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1103 link_tbl_ptr->len = cpu_to_be16(authsize);
1104
1105 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001106 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1107 (edesc->src_nents + edesc->dst_nents + 2) *
1108 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001109 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1110 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1111 edesc->dma_len, DMA_BIDIRECTIONAL);
1112 }
1113
1114 /* iv out */
1115 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1116 DMA_FROM_DEVICE);
1117
Kim Phillips5228f0f2011-07-15 11:21:38 +08001118 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001119 if (ret != -EINPROGRESS) {
1120 ipsec_esp_unmap(dev, edesc, areq);
1121 kfree(edesc);
1122 }
1123 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001124}
1125
Kim Phillips9c4a7962008-06-23 19:50:15 +08001126/*
1127 * derive number of elements in scatterlist
1128 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001129static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001130{
1131 struct scatterlist *sg = sg_list;
1132 int sg_nents = 0;
1133
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001134 *chained = 0;
1135 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001136 sg_nents++;
1137 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001138 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1139 *chained = 1;
1140 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001141 }
1142
1143 return sg_nents;
1144}
1145
Lee Nipper497f2e62010-05-19 19:20:36 +10001146/**
1147 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1148 * @sgl: The SG list
1149 * @nents: Number of SG entries
1150 * @buf: Where to copy to
1151 * @buflen: The number of bytes to copy
1152 * @skip: The number of bytes to skip before copying.
1153 * Note: skip + buflen should equal SG total size.
1154 *
1155 * Returns the number of copied bytes.
1156 *
1157 **/
1158static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1159 void *buf, size_t buflen, unsigned int skip)
1160{
1161 unsigned int offset = 0;
1162 unsigned int boffset = 0;
1163 struct sg_mapping_iter miter;
1164 unsigned long flags;
1165 unsigned int sg_flags = SG_MITER_ATOMIC;
1166 size_t total_buffer = buflen + skip;
1167
1168 sg_flags |= SG_MITER_FROM_SG;
1169
1170 sg_miter_start(&miter, sgl, nents, sg_flags);
1171
1172 local_irq_save(flags);
1173
1174 while (sg_miter_next(&miter) && offset < total_buffer) {
1175 unsigned int len;
1176 unsigned int ignore;
1177
1178 if ((offset + miter.length) > skip) {
1179 if (offset < skip) {
1180 /* Copy part of this segment */
1181 ignore = skip - offset;
1182 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001183 if (boffset + len > buflen)
1184 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001185 memcpy(buf + boffset, miter.addr + ignore, len);
1186 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001187 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001188 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001189 if (boffset + len > buflen)
1190 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001191 memcpy(buf + boffset, miter.addr, len);
1192 }
1193 boffset += len;
1194 }
1195 offset += miter.length;
1196 }
1197
1198 sg_miter_stop(&miter);
1199
1200 local_irq_restore(flags);
1201 return boffset;
1202}
1203
Kim Phillips9c4a7962008-06-23 19:50:15 +08001204/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001205 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001206 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001207static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1208 struct scatterlist *src,
1209 struct scatterlist *dst,
Lee Nipper497f2e62010-05-19 19:20:36 +10001210 int hash_result,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001211 unsigned int cryptlen,
1212 unsigned int authsize,
1213 int icv_stashing,
1214 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001215{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001216 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001217 int src_nents, dst_nents, alloc_len, dma_len;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001218 int src_chained, dst_chained = 0;
1219 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001220 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001221
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001222 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1223 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001224 return ERR_PTR(-EINVAL);
1225 }
1226
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001227 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001228 src_nents = (src_nents == 1) ? 0 : src_nents;
1229
Lee Nipper497f2e62010-05-19 19:20:36 +10001230 if (hash_result) {
1231 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001232 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001233 if (dst == src) {
1234 dst_nents = src_nents;
1235 } else {
1236 dst_nents = sg_count(dst, cryptlen + authsize,
1237 &dst_chained);
1238 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1239 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001240 }
1241
1242 /*
1243 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001244 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001245 * and the ICV data itself
1246 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001247 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001248 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001249 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001250 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001251 alloc_len += dma_len;
1252 } else {
1253 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001254 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001255 }
1256
Kim Phillips586725f2008-07-17 20:19:18 +08001257 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001258 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001259 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001260 return ERR_PTR(-ENOMEM);
1261 }
1262
1263 edesc->src_nents = src_nents;
1264 edesc->dst_nents = dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001265 edesc->src_is_chained = src_chained;
1266 edesc->dst_is_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001267 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001268 if (dma_len)
1269 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1270 edesc->dma_len,
1271 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001272
1273 return edesc;
1274}
1275
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001276static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1277 int icv_stashing)
1278{
1279 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1280 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1281
Lee Nipper497f2e62010-05-19 19:20:36 +10001282 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001283 areq->cryptlen, ctx->authsize, icv_stashing,
1284 areq->base.flags);
1285}
1286
Lee Nipper56af8cd2009-03-29 15:50:50 +08001287static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001288{
1289 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1290 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001291 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001292
1293 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001294 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001295 if (IS_ERR(edesc))
1296 return PTR_ERR(edesc);
1297
1298 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001299 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001300
1301 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1302}
1303
Lee Nipper56af8cd2009-03-29 15:50:50 +08001304static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001305{
1306 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1307 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1308 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001309 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001310 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001311 struct scatterlist *sg;
1312 void *icvdata;
1313
1314 req->cryptlen -= authsize;
1315
1316 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001317 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001318 if (IS_ERR(edesc))
1319 return PTR_ERR(edesc);
1320
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001321 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001322 ((!edesc->src_nents && !edesc->dst_nents) ||
1323 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001324
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001325 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001326 edesc->desc.hdr = ctx->desc_hdr_template |
1327 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001328 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001329
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001330 /* reset integrity check result bits */
1331 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001332
Kim Phillipse938e462009-03-29 15:53:23 +08001333 return ipsec_esp(edesc, req, NULL, 0,
1334 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001335
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001336 }
Kim Phillipse938e462009-03-29 15:53:23 +08001337
1338 /* Have to check the ICV with software */
1339 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1340
1341 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1342 if (edesc->dma_len)
1343 icvdata = &edesc->link_tbl[edesc->src_nents +
1344 edesc->dst_nents + 2];
1345 else
1346 icvdata = &edesc->link_tbl[0];
1347
1348 sg = sg_last(req->src, edesc->src_nents ? : 1);
1349
1350 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1351 ctx->authsize);
1352
1353 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001354}
1355
Lee Nipper56af8cd2009-03-29 15:50:50 +08001356static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001357{
1358 struct aead_request *areq = &req->areq;
1359 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1360 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001361 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001362
1363 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001364 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001365 if (IS_ERR(edesc))
1366 return PTR_ERR(edesc);
1367
1368 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001369 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001370
1371 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001372 /* avoid consecutive packets going out with same IV */
1373 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001374
1375 return ipsec_esp(edesc, areq, req->giv, req->seq,
1376 ipsec_esp_encrypt_done);
1377}
1378
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001379static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1380 const u8 *key, unsigned int keylen)
1381{
1382 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001383
1384 memcpy(&ctx->key, key, keylen);
1385 ctx->keylen = keylen;
1386
1387 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001388}
1389
1390static void common_nonsnoop_unmap(struct device *dev,
1391 struct talitos_edesc *edesc,
1392 struct ablkcipher_request *areq)
1393{
1394 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1395 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1396 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1397
1398 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1399
1400 if (edesc->dma_len)
1401 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1402 DMA_BIDIRECTIONAL);
1403}
1404
1405static void ablkcipher_done(struct device *dev,
1406 struct talitos_desc *desc, void *context,
1407 int err)
1408{
1409 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001410 struct talitos_edesc *edesc;
1411
1412 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001413
1414 common_nonsnoop_unmap(dev, edesc, areq);
1415
1416 kfree(edesc);
1417
1418 areq->base.complete(&areq->base, err);
1419}
1420
1421static int common_nonsnoop(struct talitos_edesc *edesc,
1422 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001423 void (*callback) (struct device *dev,
1424 struct talitos_desc *desc,
1425 void *context, int error))
1426{
1427 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1428 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1429 struct device *dev = ctx->dev;
1430 struct talitos_desc *desc = &edesc->desc;
1431 unsigned int cryptlen = areq->nbytes;
1432 unsigned int ivsize;
1433 int sg_count, ret;
1434
1435 /* first DWORD empty */
1436 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001437 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001438 desc->ptr[0].j_extent = 0;
1439
1440 /* cipher iv */
1441 ivsize = crypto_ablkcipher_ivsize(cipher);
Kim Phillipsfebec542011-07-15 11:21:39 +08001442 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001443 DMA_TO_DEVICE);
1444
1445 /* cipher key */
1446 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1447 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1448
1449 /*
1450 * cipher in
1451 */
1452 desc->ptr[3].len = cpu_to_be16(cryptlen);
1453 desc->ptr[3].j_extent = 0;
1454
1455 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1456 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1457 : DMA_TO_DEVICE,
1458 edesc->src_is_chained);
1459
1460 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001461 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001462 } else {
1463 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1464 &edesc->link_tbl[0]);
1465 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001466 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001467 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001468 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1469 edesc->dma_len,
1470 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001471 } else {
1472 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001473 to_talitos_ptr(&desc->ptr[3],
1474 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001475 }
1476 }
1477
1478 /* cipher out */
1479 desc->ptr[4].len = cpu_to_be16(cryptlen);
1480 desc->ptr[4].j_extent = 0;
1481
1482 if (areq->src != areq->dst)
1483 sg_count = talitos_map_sg(dev, areq->dst,
1484 edesc->dst_nents ? : 1,
1485 DMA_FROM_DEVICE,
1486 edesc->dst_is_chained);
1487
1488 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001489 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001490 } else {
1491 struct talitos_ptr *link_tbl_ptr =
1492 &edesc->link_tbl[edesc->src_nents + 1];
1493
Kim Phillips81eb0242009-08-13 11:51:51 +10001494 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1495 (edesc->src_nents + 1) *
1496 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001497 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001498 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1499 link_tbl_ptr);
1500 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1501 edesc->dma_len, DMA_BIDIRECTIONAL);
1502 }
1503
1504 /* iv out */
1505 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1506 DMA_FROM_DEVICE);
1507
1508 /* last DWORD empty */
1509 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001510 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001511 desc->ptr[6].j_extent = 0;
1512
Kim Phillips5228f0f2011-07-15 11:21:38 +08001513 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001514 if (ret != -EINPROGRESS) {
1515 common_nonsnoop_unmap(dev, edesc, areq);
1516 kfree(edesc);
1517 }
1518 return ret;
1519}
1520
Kim Phillipse938e462009-03-29 15:53:23 +08001521static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1522 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001523{
1524 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1525 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1526
Lee Nipper497f2e62010-05-19 19:20:36 +10001527 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1528 areq->nbytes, 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001529}
1530
1531static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1532{
1533 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1534 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1535 struct talitos_edesc *edesc;
1536
1537 /* allocate extended descriptor */
1538 edesc = ablkcipher_edesc_alloc(areq);
1539 if (IS_ERR(edesc))
1540 return PTR_ERR(edesc);
1541
1542 /* set encrypt */
1543 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1544
Kim Phillipsfebec542011-07-15 11:21:39 +08001545 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001546}
1547
1548static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1549{
1550 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1551 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1552 struct talitos_edesc *edesc;
1553
1554 /* allocate extended descriptor */
1555 edesc = ablkcipher_edesc_alloc(areq);
1556 if (IS_ERR(edesc))
1557 return PTR_ERR(edesc);
1558
1559 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1560
Kim Phillipsfebec542011-07-15 11:21:39 +08001561 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001562}
1563
Lee Nipper497f2e62010-05-19 19:20:36 +10001564static void common_nonsnoop_hash_unmap(struct device *dev,
1565 struct talitos_edesc *edesc,
1566 struct ahash_request *areq)
1567{
1568 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1569
1570 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1571
1572 /* When using hashctx-in, must unmap it. */
1573 if (edesc->desc.ptr[1].len)
1574 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1575 DMA_TO_DEVICE);
1576
1577 if (edesc->desc.ptr[2].len)
1578 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1579 DMA_TO_DEVICE);
1580
1581 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1582
1583 if (edesc->dma_len)
1584 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1585 DMA_BIDIRECTIONAL);
1586
1587}
1588
1589static void ahash_done(struct device *dev,
1590 struct talitos_desc *desc, void *context,
1591 int err)
1592{
1593 struct ahash_request *areq = context;
1594 struct talitos_edesc *edesc =
1595 container_of(desc, struct talitos_edesc, desc);
1596 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1597
1598 if (!req_ctx->last && req_ctx->to_hash_later) {
1599 /* Position any partial block for next update/final/finup */
1600 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001601 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001602 }
1603 common_nonsnoop_hash_unmap(dev, edesc, areq);
1604
1605 kfree(edesc);
1606
1607 areq->base.complete(&areq->base, err);
1608}
1609
1610static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1611 struct ahash_request *areq, unsigned int length,
1612 void (*callback) (struct device *dev,
1613 struct talitos_desc *desc,
1614 void *context, int error))
1615{
1616 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1617 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1618 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1619 struct device *dev = ctx->dev;
1620 struct talitos_desc *desc = &edesc->desc;
1621 int sg_count, ret;
1622
1623 /* first DWORD empty */
1624 desc->ptr[0] = zero_entry;
1625
Kim Phillips60f208d2010-05-19 19:21:53 +10001626 /* hash context in */
1627 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001628 map_single_talitos_ptr(dev, &desc->ptr[1],
1629 req_ctx->hw_context_size,
1630 (char *)req_ctx->hw_context, 0,
1631 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001632 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001633 } else {
1634 desc->ptr[1] = zero_entry;
1635 /* Indicate next op is not the first. */
1636 req_ctx->first = 0;
1637 }
1638
1639 /* HMAC key */
1640 if (ctx->keylen)
1641 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1642 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1643 else
1644 desc->ptr[2] = zero_entry;
1645
1646 /*
1647 * data in
1648 */
1649 desc->ptr[3].len = cpu_to_be16(length);
1650 desc->ptr[3].j_extent = 0;
1651
1652 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1653 edesc->src_nents ? : 1,
1654 DMA_TO_DEVICE,
1655 edesc->src_is_chained);
1656
1657 if (sg_count == 1) {
1658 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1659 } else {
1660 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1661 &edesc->link_tbl[0]);
1662 if (sg_count > 1) {
1663 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1664 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1665 dma_sync_single_for_device(ctx->dev,
1666 edesc->dma_link_tbl,
1667 edesc->dma_len,
1668 DMA_BIDIRECTIONAL);
1669 } else {
1670 /* Only one segment now, so no link tbl needed */
1671 to_talitos_ptr(&desc->ptr[3],
1672 sg_dma_address(req_ctx->psrc));
1673 }
1674 }
1675
1676 /* fifth DWORD empty */
1677 desc->ptr[4] = zero_entry;
1678
1679 /* hash/HMAC out -or- hash context out */
1680 if (req_ctx->last)
1681 map_single_talitos_ptr(dev, &desc->ptr[5],
1682 crypto_ahash_digestsize(tfm),
1683 areq->result, 0, DMA_FROM_DEVICE);
1684 else
1685 map_single_talitos_ptr(dev, &desc->ptr[5],
1686 req_ctx->hw_context_size,
1687 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1688
1689 /* last DWORD empty */
1690 desc->ptr[6] = zero_entry;
1691
Kim Phillips5228f0f2011-07-15 11:21:38 +08001692 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001693 if (ret != -EINPROGRESS) {
1694 common_nonsnoop_hash_unmap(dev, edesc, areq);
1695 kfree(edesc);
1696 }
1697 return ret;
1698}
1699
1700static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1701 unsigned int nbytes)
1702{
1703 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1704 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1705 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1706
1707 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1708 nbytes, 0, 0, areq->base.flags);
1709}
1710
1711static int ahash_init(struct ahash_request *areq)
1712{
1713 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1714 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1715
1716 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001717 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001718 req_ctx->first = 1; /* first indicates h/w must init its context */
1719 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001720 req_ctx->hw_context_size =
1721 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1722 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1723 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1724
1725 return 0;
1726}
1727
Kim Phillips60f208d2010-05-19 19:21:53 +10001728/*
1729 * on h/w without explicit sha224 support, we initialize h/w context
1730 * manually with sha224 constants, and tell it to run sha256.
1731 */
1732static int ahash_init_sha224_swinit(struct ahash_request *areq)
1733{
1734 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1735
1736 ahash_init(areq);
1737 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1738
Kim Phillipsa7524472010-09-23 15:56:38 +08001739 req_ctx->hw_context[0] = SHA224_H0;
1740 req_ctx->hw_context[1] = SHA224_H1;
1741 req_ctx->hw_context[2] = SHA224_H2;
1742 req_ctx->hw_context[3] = SHA224_H3;
1743 req_ctx->hw_context[4] = SHA224_H4;
1744 req_ctx->hw_context[5] = SHA224_H5;
1745 req_ctx->hw_context[6] = SHA224_H6;
1746 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001747
1748 /* init 64-bit count */
1749 req_ctx->hw_context[8] = 0;
1750 req_ctx->hw_context[9] = 0;
1751
1752 return 0;
1753}
1754
Lee Nipper497f2e62010-05-19 19:20:36 +10001755static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1756{
1757 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1758 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1759 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1760 struct talitos_edesc *edesc;
1761 unsigned int blocksize =
1762 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1763 unsigned int nbytes_to_hash;
1764 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001765 unsigned int nsg;
Lee Nipper497f2e62010-05-19 19:20:36 +10001766 int chained;
1767
Lee Nipper5e833bc2010-06-16 15:29:15 +10001768 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1769 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001770 sg_copy_to_buffer(areq->src,
1771 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001772 req_ctx->buf + req_ctx->nbuf, nbytes);
1773 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001774 return 0;
1775 }
1776
Lee Nipper5e833bc2010-06-16 15:29:15 +10001777 /* At least (blocksize + 1) bytes are available to hash */
1778 nbytes_to_hash = nbytes + req_ctx->nbuf;
1779 to_hash_later = nbytes_to_hash & (blocksize - 1);
1780
1781 if (req_ctx->last)
1782 to_hash_later = 0;
1783 else if (to_hash_later)
1784 /* There is a partial block. Hash the full block(s) now */
1785 nbytes_to_hash -= to_hash_later;
1786 else {
1787 /* Keep one block buffered */
1788 nbytes_to_hash -= blocksize;
1789 to_hash_later = blocksize;
1790 }
1791
1792 /* Chain in any previously buffered data */
1793 if (req_ctx->nbuf) {
1794 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1795 sg_init_table(req_ctx->bufsl, nsg);
1796 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1797 if (nsg > 1)
1798 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001799 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001800 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001801 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001802
Lee Nipper5e833bc2010-06-16 15:29:15 +10001803 if (to_hash_later) {
1804 int nents = sg_count(areq->src, nbytes, &chained);
1805 sg_copy_end_to_buffer(areq->src, nents,
1806 req_ctx->bufnext,
1807 to_hash_later,
1808 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001809 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001810 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001811
Lee Nipper5e833bc2010-06-16 15:29:15 +10001812 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001813 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1814 if (IS_ERR(edesc))
1815 return PTR_ERR(edesc);
1816
1817 edesc->desc.hdr = ctx->desc_hdr_template;
1818
1819 /* On last one, request SEC to pad; otherwise continue */
1820 if (req_ctx->last)
1821 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1822 else
1823 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1824
Kim Phillips60f208d2010-05-19 19:21:53 +10001825 /* request SEC to INIT hash. */
1826 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001827 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1828
1829 /* When the tfm context has a keylen, it's an HMAC.
1830 * A first or last (ie. not middle) descriptor must request HMAC.
1831 */
1832 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1833 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1834
1835 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1836 ahash_done);
1837}
1838
1839static int ahash_update(struct ahash_request *areq)
1840{
1841 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1842
1843 req_ctx->last = 0;
1844
1845 return ahash_process_req(areq, areq->nbytes);
1846}
1847
1848static int ahash_final(struct ahash_request *areq)
1849{
1850 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1851
1852 req_ctx->last = 1;
1853
1854 return ahash_process_req(areq, 0);
1855}
1856
1857static int ahash_finup(struct ahash_request *areq)
1858{
1859 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1860
1861 req_ctx->last = 1;
1862
1863 return ahash_process_req(areq, areq->nbytes);
1864}
1865
1866static int ahash_digest(struct ahash_request *areq)
1867{
1868 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001869 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001870
Kim Phillips60f208d2010-05-19 19:21:53 +10001871 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001872 req_ctx->last = 1;
1873
1874 return ahash_process_req(areq, areq->nbytes);
1875}
1876
Kim Phillips9c4a7962008-06-23 19:50:15 +08001877struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001878 u32 type;
1879 union {
1880 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001881 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001882 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001883 __be32 desc_hdr_template;
1884};
1885
1886static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001887 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001888 { .type = CRYPTO_ALG_TYPE_AEAD,
1889 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001890 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1891 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1892 .cra_blocksize = AES_BLOCK_SIZE,
1893 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1894 .cra_type = &crypto_aead_type,
1895 .cra_aead = {
1896 .setkey = aead_setkey,
1897 .setauthsize = aead_setauthsize,
1898 .encrypt = aead_encrypt,
1899 .decrypt = aead_decrypt,
1900 .givencrypt = aead_givencrypt,
1901 .geniv = "<built-in>",
1902 .ivsize = AES_BLOCK_SIZE,
1903 .maxauthsize = SHA1_DIGEST_SIZE,
1904 }
1905 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001906 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1907 DESC_HDR_SEL0_AESU |
1908 DESC_HDR_MODE0_AESU_CBC |
1909 DESC_HDR_SEL1_MDEUA |
1910 DESC_HDR_MODE1_MDEU_INIT |
1911 DESC_HDR_MODE1_MDEU_PAD |
1912 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001913 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001914 { .type = CRYPTO_ALG_TYPE_AEAD,
1915 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001916 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1917 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1918 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1919 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1920 .cra_type = &crypto_aead_type,
1921 .cra_aead = {
1922 .setkey = aead_setkey,
1923 .setauthsize = aead_setauthsize,
1924 .encrypt = aead_encrypt,
1925 .decrypt = aead_decrypt,
1926 .givencrypt = aead_givencrypt,
1927 .geniv = "<built-in>",
1928 .ivsize = DES3_EDE_BLOCK_SIZE,
1929 .maxauthsize = SHA1_DIGEST_SIZE,
1930 }
1931 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001932 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1933 DESC_HDR_SEL0_DEU |
1934 DESC_HDR_MODE0_DEU_CBC |
1935 DESC_HDR_MODE0_DEU_3DES |
1936 DESC_HDR_SEL1_MDEUA |
1937 DESC_HDR_MODE1_MDEU_INIT |
1938 DESC_HDR_MODE1_MDEU_PAD |
1939 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001940 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001941 { .type = CRYPTO_ALG_TYPE_AEAD,
1942 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001943 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1944 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1945 .cra_blocksize = AES_BLOCK_SIZE,
1946 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1947 .cra_type = &crypto_aead_type,
1948 .cra_aead = {
1949 .setkey = aead_setkey,
1950 .setauthsize = aead_setauthsize,
1951 .encrypt = aead_encrypt,
1952 .decrypt = aead_decrypt,
1953 .givencrypt = aead_givencrypt,
1954 .geniv = "<built-in>",
1955 .ivsize = AES_BLOCK_SIZE,
1956 .maxauthsize = SHA256_DIGEST_SIZE,
1957 }
1958 },
Lee Nipper3952f172008-07-10 18:29:18 +08001959 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1960 DESC_HDR_SEL0_AESU |
1961 DESC_HDR_MODE0_AESU_CBC |
1962 DESC_HDR_SEL1_MDEUA |
1963 DESC_HDR_MODE1_MDEU_INIT |
1964 DESC_HDR_MODE1_MDEU_PAD |
1965 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
1966 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001967 { .type = CRYPTO_ALG_TYPE_AEAD,
1968 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001969 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
1970 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
1971 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1972 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1973 .cra_type = &crypto_aead_type,
1974 .cra_aead = {
1975 .setkey = aead_setkey,
1976 .setauthsize = aead_setauthsize,
1977 .encrypt = aead_encrypt,
1978 .decrypt = aead_decrypt,
1979 .givencrypt = aead_givencrypt,
1980 .geniv = "<built-in>",
1981 .ivsize = DES3_EDE_BLOCK_SIZE,
1982 .maxauthsize = SHA256_DIGEST_SIZE,
1983 }
1984 },
Lee Nipper3952f172008-07-10 18:29:18 +08001985 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1986 DESC_HDR_SEL0_DEU |
1987 DESC_HDR_MODE0_DEU_CBC |
1988 DESC_HDR_MODE0_DEU_3DES |
1989 DESC_HDR_SEL1_MDEUA |
1990 DESC_HDR_MODE1_MDEU_INIT |
1991 DESC_HDR_MODE1_MDEU_PAD |
1992 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
1993 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001994 { .type = CRYPTO_ALG_TYPE_AEAD,
1995 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001996 .cra_name = "authenc(hmac(md5),cbc(aes))",
1997 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
1998 .cra_blocksize = AES_BLOCK_SIZE,
1999 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2000 .cra_type = &crypto_aead_type,
2001 .cra_aead = {
2002 .setkey = aead_setkey,
2003 .setauthsize = aead_setauthsize,
2004 .encrypt = aead_encrypt,
2005 .decrypt = aead_decrypt,
2006 .givencrypt = aead_givencrypt,
2007 .geniv = "<built-in>",
2008 .ivsize = AES_BLOCK_SIZE,
2009 .maxauthsize = MD5_DIGEST_SIZE,
2010 }
2011 },
Lee Nipper3952f172008-07-10 18:29:18 +08002012 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2013 DESC_HDR_SEL0_AESU |
2014 DESC_HDR_MODE0_AESU_CBC |
2015 DESC_HDR_SEL1_MDEUA |
2016 DESC_HDR_MODE1_MDEU_INIT |
2017 DESC_HDR_MODE1_MDEU_PAD |
2018 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2019 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002020 { .type = CRYPTO_ALG_TYPE_AEAD,
2021 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002022 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2023 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2024 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2025 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2026 .cra_type = &crypto_aead_type,
2027 .cra_aead = {
2028 .setkey = aead_setkey,
2029 .setauthsize = aead_setauthsize,
2030 .encrypt = aead_encrypt,
2031 .decrypt = aead_decrypt,
2032 .givencrypt = aead_givencrypt,
2033 .geniv = "<built-in>",
2034 .ivsize = DES3_EDE_BLOCK_SIZE,
2035 .maxauthsize = MD5_DIGEST_SIZE,
2036 }
2037 },
Lee Nipper3952f172008-07-10 18:29:18 +08002038 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2039 DESC_HDR_SEL0_DEU |
2040 DESC_HDR_MODE0_DEU_CBC |
2041 DESC_HDR_MODE0_DEU_3DES |
2042 DESC_HDR_SEL1_MDEUA |
2043 DESC_HDR_MODE1_MDEU_INIT |
2044 DESC_HDR_MODE1_MDEU_PAD |
2045 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002046 },
2047 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002048 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2049 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002050 .cra_name = "cbc(aes)",
2051 .cra_driver_name = "cbc-aes-talitos",
2052 .cra_blocksize = AES_BLOCK_SIZE,
2053 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2054 CRYPTO_ALG_ASYNC,
2055 .cra_type = &crypto_ablkcipher_type,
2056 .cra_ablkcipher = {
2057 .setkey = ablkcipher_setkey,
2058 .encrypt = ablkcipher_encrypt,
2059 .decrypt = ablkcipher_decrypt,
2060 .geniv = "eseqiv",
2061 .min_keysize = AES_MIN_KEY_SIZE,
2062 .max_keysize = AES_MAX_KEY_SIZE,
2063 .ivsize = AES_BLOCK_SIZE,
2064 }
2065 },
2066 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2067 DESC_HDR_SEL0_AESU |
2068 DESC_HDR_MODE0_AESU_CBC,
2069 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002070 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2071 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002072 .cra_name = "cbc(des3_ede)",
2073 .cra_driver_name = "cbc-3des-talitos",
2074 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2075 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2076 CRYPTO_ALG_ASYNC,
2077 .cra_type = &crypto_ablkcipher_type,
2078 .cra_ablkcipher = {
2079 .setkey = ablkcipher_setkey,
2080 .encrypt = ablkcipher_encrypt,
2081 .decrypt = ablkcipher_decrypt,
2082 .geniv = "eseqiv",
2083 .min_keysize = DES3_EDE_KEY_SIZE,
2084 .max_keysize = DES3_EDE_KEY_SIZE,
2085 .ivsize = DES3_EDE_BLOCK_SIZE,
2086 }
2087 },
2088 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2089 DESC_HDR_SEL0_DEU |
2090 DESC_HDR_MODE0_DEU_CBC |
2091 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002092 },
2093 /* AHASH algorithms. */
2094 { .type = CRYPTO_ALG_TYPE_AHASH,
2095 .alg.hash = {
2096 .init = ahash_init,
2097 .update = ahash_update,
2098 .final = ahash_final,
2099 .finup = ahash_finup,
2100 .digest = ahash_digest,
2101 .halg.digestsize = MD5_DIGEST_SIZE,
2102 .halg.base = {
2103 .cra_name = "md5",
2104 .cra_driver_name = "md5-talitos",
2105 .cra_blocksize = MD5_BLOCK_SIZE,
2106 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2107 CRYPTO_ALG_ASYNC,
2108 .cra_type = &crypto_ahash_type
2109 }
2110 },
2111 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2112 DESC_HDR_SEL0_MDEUA |
2113 DESC_HDR_MODE0_MDEU_MD5,
2114 },
2115 { .type = CRYPTO_ALG_TYPE_AHASH,
2116 .alg.hash = {
2117 .init = ahash_init,
2118 .update = ahash_update,
2119 .final = ahash_final,
2120 .finup = ahash_finup,
2121 .digest = ahash_digest,
2122 .halg.digestsize = SHA1_DIGEST_SIZE,
2123 .halg.base = {
2124 .cra_name = "sha1",
2125 .cra_driver_name = "sha1-talitos",
2126 .cra_blocksize = SHA1_BLOCK_SIZE,
2127 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2128 CRYPTO_ALG_ASYNC,
2129 .cra_type = &crypto_ahash_type
2130 }
2131 },
2132 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2133 DESC_HDR_SEL0_MDEUA |
2134 DESC_HDR_MODE0_MDEU_SHA1,
2135 },
2136 { .type = CRYPTO_ALG_TYPE_AHASH,
2137 .alg.hash = {
2138 .init = ahash_init,
2139 .update = ahash_update,
2140 .final = ahash_final,
2141 .finup = ahash_finup,
2142 .digest = ahash_digest,
Kim Phillips60f208d2010-05-19 19:21:53 +10002143 .halg.digestsize = SHA224_DIGEST_SIZE,
2144 .halg.base = {
2145 .cra_name = "sha224",
2146 .cra_driver_name = "sha224-talitos",
2147 .cra_blocksize = SHA224_BLOCK_SIZE,
2148 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2149 CRYPTO_ALG_ASYNC,
2150 .cra_type = &crypto_ahash_type
2151 }
2152 },
2153 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2154 DESC_HDR_SEL0_MDEUA |
2155 DESC_HDR_MODE0_MDEU_SHA224,
2156 },
2157 { .type = CRYPTO_ALG_TYPE_AHASH,
2158 .alg.hash = {
2159 .init = ahash_init,
2160 .update = ahash_update,
2161 .final = ahash_final,
2162 .finup = ahash_finup,
2163 .digest = ahash_digest,
Lee Nipper497f2e62010-05-19 19:20:36 +10002164 .halg.digestsize = SHA256_DIGEST_SIZE,
2165 .halg.base = {
2166 .cra_name = "sha256",
2167 .cra_driver_name = "sha256-talitos",
2168 .cra_blocksize = SHA256_BLOCK_SIZE,
2169 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2170 CRYPTO_ALG_ASYNC,
2171 .cra_type = &crypto_ahash_type
2172 }
2173 },
2174 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2175 DESC_HDR_SEL0_MDEUA |
2176 DESC_HDR_MODE0_MDEU_SHA256,
2177 },
2178 { .type = CRYPTO_ALG_TYPE_AHASH,
2179 .alg.hash = {
2180 .init = ahash_init,
2181 .update = ahash_update,
2182 .final = ahash_final,
2183 .finup = ahash_finup,
2184 .digest = ahash_digest,
2185 .halg.digestsize = SHA384_DIGEST_SIZE,
2186 .halg.base = {
2187 .cra_name = "sha384",
2188 .cra_driver_name = "sha384-talitos",
2189 .cra_blocksize = SHA384_BLOCK_SIZE,
2190 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2191 CRYPTO_ALG_ASYNC,
2192 .cra_type = &crypto_ahash_type
2193 }
2194 },
2195 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2196 DESC_HDR_SEL0_MDEUB |
2197 DESC_HDR_MODE0_MDEUB_SHA384,
2198 },
2199 { .type = CRYPTO_ALG_TYPE_AHASH,
2200 .alg.hash = {
2201 .init = ahash_init,
2202 .update = ahash_update,
2203 .final = ahash_final,
2204 .finup = ahash_finup,
2205 .digest = ahash_digest,
2206 .halg.digestsize = SHA512_DIGEST_SIZE,
2207 .halg.base = {
2208 .cra_name = "sha512",
2209 .cra_driver_name = "sha512-talitos",
2210 .cra_blocksize = SHA512_BLOCK_SIZE,
2211 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2212 CRYPTO_ALG_ASYNC,
2213 .cra_type = &crypto_ahash_type
2214 }
2215 },
2216 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2217 DESC_HDR_SEL0_MDEUB |
2218 DESC_HDR_MODE0_MDEUB_SHA512,
2219 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002220};
2221
2222struct talitos_crypto_alg {
2223 struct list_head entry;
2224 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002225 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002226};
2227
2228static int talitos_cra_init(struct crypto_tfm *tfm)
2229{
2230 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002231 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002232 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002233 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002234
Lee Nipper497f2e62010-05-19 19:20:36 +10002235 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2236 talitos_alg = container_of(__crypto_ahash_alg(alg),
2237 struct talitos_crypto_alg,
2238 algt.alg.hash);
2239 else
2240 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2241 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002242
Kim Phillips9c4a7962008-06-23 19:50:15 +08002243 /* update context with ptr to dev */
2244 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002245
Kim Phillips5228f0f2011-07-15 11:21:38 +08002246 /* assign SEC channel to tfm in round-robin fashion */
2247 priv = dev_get_drvdata(ctx->dev);
2248 ctx->ch = atomic_inc_return(&priv->last_chan) &
2249 (priv->num_channels - 1);
2250
Kim Phillips9c4a7962008-06-23 19:50:15 +08002251 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002252 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002253
Kim Phillips602dba52011-07-15 11:21:39 +08002254 /* select done notification */
2255 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2256
Lee Nipper497f2e62010-05-19 19:20:36 +10002257 return 0;
2258}
2259
2260static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2261{
2262 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2263
2264 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002265
2266 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002267 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002268
2269 return 0;
2270}
2271
Lee Nipper497f2e62010-05-19 19:20:36 +10002272static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2273{
2274 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2275
2276 talitos_cra_init(tfm);
2277
2278 ctx->keylen = 0;
2279 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2280 sizeof(struct talitos_ahash_req_ctx));
2281
2282 return 0;
2283}
2284
Kim Phillips9c4a7962008-06-23 19:50:15 +08002285/*
2286 * given the alg's descriptor header template, determine whether descriptor
2287 * type and primary/secondary execution units required match the hw
2288 * capabilities description provided in the device tree node.
2289 */
2290static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2291{
2292 struct talitos_private *priv = dev_get_drvdata(dev);
2293 int ret;
2294
2295 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2296 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2297
2298 if (SECONDARY_EU(desc_hdr_template))
2299 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2300 & priv->exec_units);
2301
2302 return ret;
2303}
2304
Grant Likely2dc11582010-08-06 09:25:50 -06002305static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002306{
2307 struct device *dev = &ofdev->dev;
2308 struct talitos_private *priv = dev_get_drvdata(dev);
2309 struct talitos_crypto_alg *t_alg, *n;
2310 int i;
2311
2312 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002313 switch (t_alg->algt.type) {
2314 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2315 case CRYPTO_ALG_TYPE_AEAD:
2316 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2317 break;
2318 case CRYPTO_ALG_TYPE_AHASH:
2319 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2320 break;
2321 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002322 list_del(&t_alg->entry);
2323 kfree(t_alg);
2324 }
2325
2326 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2327 talitos_unregister_rng(dev);
2328
Kim Phillips4b9926282009-08-13 11:50:38 +10002329 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002330 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002331
Kim Phillips4b9926282009-08-13 11:50:38 +10002332 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002333
2334 if (priv->irq != NO_IRQ) {
2335 free_irq(priv->irq, dev);
2336 irq_dispose_mapping(priv->irq);
2337 }
2338
2339 tasklet_kill(&priv->done_task);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002340
2341 iounmap(priv->reg);
2342
2343 dev_set_drvdata(dev, NULL);
2344
2345 kfree(priv);
2346
2347 return 0;
2348}
2349
2350static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2351 struct talitos_alg_template
2352 *template)
2353{
Kim Phillips60f208d2010-05-19 19:21:53 +10002354 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002355 struct talitos_crypto_alg *t_alg;
2356 struct crypto_alg *alg;
2357
2358 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2359 if (!t_alg)
2360 return ERR_PTR(-ENOMEM);
2361
Lee Nipperacbf7c622010-05-19 19:19:33 +10002362 t_alg->algt = *template;
2363
2364 switch (t_alg->algt.type) {
2365 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002366 alg = &t_alg->algt.alg.crypto;
2367 alg->cra_init = talitos_cra_init;
2368 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002369 case CRYPTO_ALG_TYPE_AEAD:
2370 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002371 alg->cra_init = talitos_cra_init_aead;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002372 break;
2373 case CRYPTO_ALG_TYPE_AHASH:
2374 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002375 alg->cra_init = talitos_cra_init_ahash;
Kim Phillips60f208d2010-05-19 19:21:53 +10002376 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
2377 !strcmp(alg->cra_name, "sha224")) {
2378 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2379 t_alg->algt.desc_hdr_template =
2380 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2381 DESC_HDR_SEL0_MDEUA |
2382 DESC_HDR_MODE0_MDEU_SHA256;
2383 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002384 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002385 default:
2386 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2387 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002388 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002389
Kim Phillips9c4a7962008-06-23 19:50:15 +08002390 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002391 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002392 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002393 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002394
Kim Phillips9c4a7962008-06-23 19:50:15 +08002395 t_alg->dev = dev;
2396
2397 return t_alg;
2398}
2399
Grant Likely1c48a5c2011-02-17 02:43:24 -07002400static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002401{
2402 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002403 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002404 struct talitos_private *priv;
2405 const unsigned int *prop;
2406 int i, err;
2407
2408 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2409 if (!priv)
2410 return -ENOMEM;
2411
2412 dev_set_drvdata(dev, priv);
2413
2414 priv->ofdev = ofdev;
2415
2416 tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002417
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002418 INIT_LIST_HEAD(&priv->alg_list);
2419
Kim Phillips9c4a7962008-06-23 19:50:15 +08002420 priv->irq = irq_of_parse_and_map(np, 0);
2421
2422 if (priv->irq == NO_IRQ) {
2423 dev_err(dev, "failed to map irq\n");
2424 err = -EINVAL;
2425 goto err_out;
2426 }
2427
2428 /* get the irq line */
2429 err = request_irq(priv->irq, talitos_interrupt, 0,
2430 dev_driver_string(dev), dev);
2431 if (err) {
2432 dev_err(dev, "failed to request irq %d\n", priv->irq);
2433 irq_dispose_mapping(priv->irq);
2434 priv->irq = NO_IRQ;
2435 goto err_out;
2436 }
2437
2438 priv->reg = of_iomap(np, 0);
2439 if (!priv->reg) {
2440 dev_err(dev, "failed to of_iomap\n");
2441 err = -ENOMEM;
2442 goto err_out;
2443 }
2444
2445 /* get SEC version capabilities from device tree */
2446 prop = of_get_property(np, "fsl,num-channels", NULL);
2447 if (prop)
2448 priv->num_channels = *prop;
2449
2450 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2451 if (prop)
2452 priv->chfifo_len = *prop;
2453
2454 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2455 if (prop)
2456 priv->exec_units = *prop;
2457
2458 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2459 if (prop)
2460 priv->desc_types = *prop;
2461
2462 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2463 !priv->exec_units || !priv->desc_types) {
2464 dev_err(dev, "invalid property data in device tree node\n");
2465 err = -EINVAL;
2466 goto err_out;
2467 }
2468
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002469 if (of_device_is_compatible(np, "fsl,sec3.0"))
2470 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2471
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002472 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002473 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
2474 TALITOS_FTR_SHA224_HWINIT;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002475
Kim Phillips4b9926282009-08-13 11:50:38 +10002476 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2477 priv->num_channels, GFP_KERNEL);
2478 if (!priv->chan) {
2479 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002480 err = -ENOMEM;
2481 goto err_out;
2482 }
2483
2484 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002485 spin_lock_init(&priv->chan[i].head_lock);
2486 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002487 }
2488
2489 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2490
2491 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002492 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2493 priv->fifo_len, GFP_KERNEL);
2494 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002495 dev_err(dev, "failed to allocate request fifo %d\n", i);
2496 err = -ENOMEM;
2497 goto err_out;
2498 }
2499 }
2500
Kim Phillipsec6644d2008-07-17 20:16:40 +08002501 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002502 atomic_set(&priv->chan[i].submit_count,
2503 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002504
Kim Phillips81eb0242009-08-13 11:51:51 +10002505 dma_set_mask(dev, DMA_BIT_MASK(36));
2506
Kim Phillips9c4a7962008-06-23 19:50:15 +08002507 /* reset and initialize the h/w */
2508 err = init_device(dev);
2509 if (err) {
2510 dev_err(dev, "failed to initialize device\n");
2511 goto err_out;
2512 }
2513
2514 /* register the RNG, if available */
2515 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2516 err = talitos_register_rng(dev);
2517 if (err) {
2518 dev_err(dev, "failed to register hwrng: %d\n", err);
2519 goto err_out;
2520 } else
2521 dev_info(dev, "hwrng\n");
2522 }
2523
2524 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002525 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2526 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2527 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002528 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002529
2530 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2531 if (IS_ERR(t_alg)) {
2532 err = PTR_ERR(t_alg);
2533 goto err_out;
2534 }
2535
Lee Nipperacbf7c622010-05-19 19:19:33 +10002536 switch (t_alg->algt.type) {
2537 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2538 case CRYPTO_ALG_TYPE_AEAD:
2539 err = crypto_register_alg(
2540 &t_alg->algt.alg.crypto);
2541 name = t_alg->algt.alg.crypto.cra_driver_name;
2542 break;
2543 case CRYPTO_ALG_TYPE_AHASH:
2544 err = crypto_register_ahash(
2545 &t_alg->algt.alg.hash);
2546 name =
2547 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2548 break;
2549 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002550 if (err) {
2551 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002552 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002553 kfree(t_alg);
2554 } else {
2555 list_add_tail(&t_alg->entry, &priv->alg_list);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002556 dev_info(dev, "%s\n", name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002557 }
2558 }
2559 }
2560
2561 return 0;
2562
2563err_out:
2564 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002565
2566 return err;
2567}
2568
Márton Németh6c3f9752010-01-17 21:54:01 +11002569static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002570 {
2571 .compatible = "fsl,sec2.0",
2572 },
2573 {},
2574};
2575MODULE_DEVICE_TABLE(of, talitos_match);
2576
Grant Likely1c48a5c2011-02-17 02:43:24 -07002577static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002578 .driver = {
2579 .name = "talitos",
2580 .owner = THIS_MODULE,
2581 .of_match_table = talitos_match,
2582 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002583 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002584 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002585};
2586
2587static int __init talitos_init(void)
2588{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002589 return platform_driver_register(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002590}
2591module_init(talitos_init);
2592
2593static void __exit talitos_exit(void)
2594{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002595 platform_driver_unregister(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002596}
2597module_exit(talitos_exit);
2598
2599MODULE_LICENSE("GPL");
2600MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2601MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");