blob: 8ce87317310bd05e4a9ec14b6bf583cf42e97f5a [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 Nipper79b3a412011-11-21 16:13:25 +0800160#define TALITOS_FTR_HMAC_OK 0x00000008
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800161
Kim Phillips81eb0242009-08-13 11:51:51 +1000162static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
163{
164 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +0800165 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +1000166}
167
Kim Phillips9c4a7962008-06-23 19:50:15 +0800168/*
169 * map virtual single (contiguous) pointer to h/w descriptor pointer
170 */
171static void map_single_talitos_ptr(struct device *dev,
172 struct talitos_ptr *talitos_ptr,
173 unsigned short len, void *data,
174 unsigned char extent,
175 enum dma_data_direction dir)
176{
Kim Phillips81eb0242009-08-13 11:51:51 +1000177 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
178
Kim Phillips9c4a7962008-06-23 19:50:15 +0800179 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +1000180 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800181 talitos_ptr->j_extent = extent;
182}
183
184/*
185 * unmap bus single (contiguous) h/w descriptor pointer
186 */
187static void unmap_single_talitos_ptr(struct device *dev,
188 struct talitos_ptr *talitos_ptr,
189 enum dma_data_direction dir)
190{
191 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
192 be16_to_cpu(talitos_ptr->len), dir);
193}
194
195static int reset_channel(struct device *dev, int ch)
196{
197 struct talitos_private *priv = dev_get_drvdata(dev);
198 unsigned int timeout = TALITOS_TIMEOUT;
199
200 setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
201
202 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
203 && --timeout)
204 cpu_relax();
205
206 if (timeout == 0) {
207 dev_err(dev, "failed to reset channel %d\n", ch);
208 return -EIO;
209 }
210
Kim Phillips81eb0242009-08-13 11:51:51 +1000211 /* set 36-bit addressing, done writeback enable and done IRQ enable */
212 setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE |
213 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800214
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800215 /* and ICCR writeback, if available */
216 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
217 setbits32(priv->reg + TALITOS_CCCR_LO(ch),
218 TALITOS_CCCR_LO_IWSE);
219
Kim Phillips9c4a7962008-06-23 19:50:15 +0800220 return 0;
221}
222
223static int reset_device(struct device *dev)
224{
225 struct talitos_private *priv = dev_get_drvdata(dev);
226 unsigned int timeout = TALITOS_TIMEOUT;
227
228 setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
229
230 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
231 && --timeout)
232 cpu_relax();
233
234 if (timeout == 0) {
235 dev_err(dev, "failed to reset device\n");
236 return -EIO;
237 }
238
239 return 0;
240}
241
242/*
243 * Reset and initialize the device
244 */
245static int init_device(struct device *dev)
246{
247 struct talitos_private *priv = dev_get_drvdata(dev);
248 int ch, err;
249
250 /*
251 * Master reset
252 * errata documentation: warning: certain SEC interrupts
253 * are not fully cleared by writing the MCR:SWR bit,
254 * set bit twice to completely reset
255 */
256 err = reset_device(dev);
257 if (err)
258 return err;
259
260 err = reset_device(dev);
261 if (err)
262 return err;
263
264 /* reset channels */
265 for (ch = 0; ch < priv->num_channels; ch++) {
266 err = reset_channel(dev, ch);
267 if (err)
268 return err;
269 }
270
271 /* enable channel done and error interrupts */
272 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
273 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
274
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800275 /* disable integrity check error interrupts (use writeback instead) */
276 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
277 setbits32(priv->reg + TALITOS_MDEUICR_LO,
278 TALITOS_MDEUICR_LO_ICE);
279
Kim Phillips9c4a7962008-06-23 19:50:15 +0800280 return 0;
281}
282
283/**
284 * talitos_submit - submits a descriptor to the device for processing
285 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800286 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800287 * @desc: the descriptor to be processed by the device
288 * @callback: whom to call when processing is complete
289 * @context: a handle for use by caller (optional)
290 *
291 * desc must contain valid dma-mapped (bus physical) address pointers.
292 * callback must check err and feedback in descriptor header
293 * for device processing status.
294 */
Kim Phillips5228f0f2011-07-15 11:21:38 +0800295static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800296 void (*callback)(struct device *dev,
297 struct talitos_desc *desc,
298 void *context, int error),
299 void *context)
300{
301 struct talitos_private *priv = dev_get_drvdata(dev);
302 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800303 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800304 int head;
305
Kim Phillips4b9926282009-08-13 11:50:38 +1000306 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800307
Kim Phillips4b9926282009-08-13 11:50:38 +1000308 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800309 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000310 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800311 return -EAGAIN;
312 }
313
Kim Phillips4b9926282009-08-13 11:50:38 +1000314 head = priv->chan[ch].head;
315 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800316
Kim Phillips9c4a7962008-06-23 19:50:15 +0800317 /* map descriptor and save caller data */
318 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
319 DMA_BIDIRECTIONAL);
320 request->callback = callback;
321 request->context = context;
322
323 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000324 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800325
326 smp_wmb();
327 request->desc = desc;
328
329 /* GO! */
330 wmb();
Kim Phillipsa7524472010-09-23 15:56:38 +0800331 out_be32(priv->reg + TALITOS_FF(ch), upper_32_bits(request->dma_desc));
Kim Phillips81eb0242009-08-13 11:51:51 +1000332 out_be32(priv->reg + TALITOS_FF_LO(ch),
Kim Phillipsa7524472010-09-23 15:56:38 +0800333 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800334
Kim Phillips4b9926282009-08-13 11:50:38 +1000335 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800336
337 return -EINPROGRESS;
338}
339
340/*
341 * process what was done, notify callback of error if not
342 */
343static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
344{
345 struct talitos_private *priv = dev_get_drvdata(dev);
346 struct talitos_request *request, saved_req;
347 unsigned long flags;
348 int tail, status;
349
Kim Phillips4b9926282009-08-13 11:50:38 +1000350 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800351
Kim Phillips4b9926282009-08-13 11:50:38 +1000352 tail = priv->chan[ch].tail;
353 while (priv->chan[ch].fifo[tail].desc) {
354 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800355
356 /* descriptors with their done bits set don't get the error */
357 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100358 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800359 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100360 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800361 if (!error)
362 break;
363 else
364 status = error;
365
366 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800367 sizeof(struct talitos_desc),
368 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800369
370 /* copy entries so we can call callback outside lock */
371 saved_req.desc = request->desc;
372 saved_req.callback = request->callback;
373 saved_req.context = request->context;
374
375 /* release request entry in fifo */
376 smp_wmb();
377 request->desc = NULL;
378
379 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000380 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800381
Kim Phillips4b9926282009-08-13 11:50:38 +1000382 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800383
Kim Phillips4b9926282009-08-13 11:50:38 +1000384 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800385
Kim Phillips9c4a7962008-06-23 19:50:15 +0800386 saved_req.callback(dev, saved_req.desc, saved_req.context,
387 status);
388 /* channel may resume processing in single desc error case */
389 if (error && !reset_ch && status == error)
390 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000391 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
392 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800393 }
394
Kim Phillips4b9926282009-08-13 11:50:38 +1000395 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800396}
397
398/*
399 * process completed requests for channels that have done status
400 */
401static void talitos_done(unsigned long data)
402{
403 struct device *dev = (struct device *)data;
404 struct talitos_private *priv = dev_get_drvdata(dev);
405 int ch;
406
407 for (ch = 0; ch < priv->num_channels; ch++)
408 flush_channel(dev, ch, 0, 0);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800409
410 /* At this point, all completed channels have been processed.
411 * Unmask done interrupts for channels completed later on.
412 */
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800413 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
414 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800415}
416
417/*
418 * locate current (offending) descriptor
419 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200420static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800421{
422 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000423 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800424 dma_addr_t cur_desc;
425
426 cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
427
Kim Phillips4b9926282009-08-13 11:50:38 +1000428 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800429 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000430 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800431 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200432 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800433 }
434 }
435
Kim Phillips3e721ae2011-10-21 15:20:28 +0200436 return priv->chan[ch].fifo[tail].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800437}
438
439/*
440 * user diagnostics; report root cause of error based on execution unit status
441 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200442static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800443{
444 struct talitos_private *priv = dev_get_drvdata(dev);
445 int i;
446
Kim Phillips3e721ae2011-10-21 15:20:28 +0200447 if (!desc_hdr)
448 desc_hdr = in_be32(priv->reg + TALITOS_DESCBUF(ch));
449
450 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800451 case DESC_HDR_SEL0_AFEU:
452 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
453 in_be32(priv->reg + TALITOS_AFEUISR),
454 in_be32(priv->reg + TALITOS_AFEUISR_LO));
455 break;
456 case DESC_HDR_SEL0_DEU:
457 dev_err(dev, "DEUISR 0x%08x_%08x\n",
458 in_be32(priv->reg + TALITOS_DEUISR),
459 in_be32(priv->reg + TALITOS_DEUISR_LO));
460 break;
461 case DESC_HDR_SEL0_MDEUA:
462 case DESC_HDR_SEL0_MDEUB:
463 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
464 in_be32(priv->reg + TALITOS_MDEUISR),
465 in_be32(priv->reg + TALITOS_MDEUISR_LO));
466 break;
467 case DESC_HDR_SEL0_RNG:
468 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
469 in_be32(priv->reg + TALITOS_RNGUISR),
470 in_be32(priv->reg + TALITOS_RNGUISR_LO));
471 break;
472 case DESC_HDR_SEL0_PKEU:
473 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
474 in_be32(priv->reg + TALITOS_PKEUISR),
475 in_be32(priv->reg + TALITOS_PKEUISR_LO));
476 break;
477 case DESC_HDR_SEL0_AESU:
478 dev_err(dev, "AESUISR 0x%08x_%08x\n",
479 in_be32(priv->reg + TALITOS_AESUISR),
480 in_be32(priv->reg + TALITOS_AESUISR_LO));
481 break;
482 case DESC_HDR_SEL0_CRCU:
483 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
484 in_be32(priv->reg + TALITOS_CRCUISR),
485 in_be32(priv->reg + TALITOS_CRCUISR_LO));
486 break;
487 case DESC_HDR_SEL0_KEU:
488 dev_err(dev, "KEUISR 0x%08x_%08x\n",
489 in_be32(priv->reg + TALITOS_KEUISR),
490 in_be32(priv->reg + TALITOS_KEUISR_LO));
491 break;
492 }
493
Kim Phillips3e721ae2011-10-21 15:20:28 +0200494 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800495 case DESC_HDR_SEL1_MDEUA:
496 case DESC_HDR_SEL1_MDEUB:
497 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
498 in_be32(priv->reg + TALITOS_MDEUISR),
499 in_be32(priv->reg + TALITOS_MDEUISR_LO));
500 break;
501 case DESC_HDR_SEL1_CRCU:
502 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
503 in_be32(priv->reg + TALITOS_CRCUISR),
504 in_be32(priv->reg + TALITOS_CRCUISR_LO));
505 break;
506 }
507
508 for (i = 0; i < 8; i++)
509 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
510 in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
511 in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
512}
513
514/*
515 * recover from error interrupts
516 */
Kim Phillips40405f12008-10-12 20:19:35 +0800517static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800518{
519 struct device *dev = (struct device *)data;
520 struct talitos_private *priv = dev_get_drvdata(dev);
521 unsigned int timeout = TALITOS_TIMEOUT;
522 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800523 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800524
525 for (ch = 0; ch < priv->num_channels; ch++) {
526 /* skip channels without errors */
527 if (!(isr & (1 << (ch * 2 + 1))))
528 continue;
529
530 error = -EINVAL;
531
532 v = in_be32(priv->reg + TALITOS_CCPSR(ch));
533 v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
534
535 if (v_lo & TALITOS_CCPSR_LO_DOF) {
536 dev_err(dev, "double fetch fifo overflow error\n");
537 error = -EAGAIN;
538 reset_ch = 1;
539 }
540 if (v_lo & TALITOS_CCPSR_LO_SOF) {
541 /* h/w dropped descriptor */
542 dev_err(dev, "single fetch fifo overflow error\n");
543 error = -EAGAIN;
544 }
545 if (v_lo & TALITOS_CCPSR_LO_MDTE)
546 dev_err(dev, "master data transfer error\n");
547 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
548 dev_err(dev, "s/g data length zero error\n");
549 if (v_lo & TALITOS_CCPSR_LO_FPZ)
550 dev_err(dev, "fetch pointer zero error\n");
551 if (v_lo & TALITOS_CCPSR_LO_IDH)
552 dev_err(dev, "illegal descriptor header error\n");
553 if (v_lo & TALITOS_CCPSR_LO_IEU)
554 dev_err(dev, "invalid execution unit error\n");
555 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200556 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800557 if (v_lo & TALITOS_CCPSR_LO_GB)
558 dev_err(dev, "gather boundary error\n");
559 if (v_lo & TALITOS_CCPSR_LO_GRL)
560 dev_err(dev, "gather return/length error\n");
561 if (v_lo & TALITOS_CCPSR_LO_SB)
562 dev_err(dev, "scatter boundary error\n");
563 if (v_lo & TALITOS_CCPSR_LO_SRL)
564 dev_err(dev, "scatter return/length error\n");
565
566 flush_channel(dev, ch, error, reset_ch);
567
568 if (reset_ch) {
569 reset_channel(dev, ch);
570 } else {
571 setbits32(priv->reg + TALITOS_CCCR(ch),
572 TALITOS_CCCR_CONT);
573 setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
574 while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
575 TALITOS_CCCR_CONT) && --timeout)
576 cpu_relax();
577 if (timeout == 0) {
578 dev_err(dev, "failed to restart channel %d\n",
579 ch);
580 reset_dev = 1;
581 }
582 }
583 }
584 if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
585 dev_err(dev, "done overflow, internal time out, or rngu error: "
586 "ISR 0x%08x_%08x\n", isr, isr_lo);
587
588 /* purge request queues */
589 for (ch = 0; ch < priv->num_channels; ch++)
590 flush_channel(dev, ch, -EIO, 1);
591
592 /* reset and reinitialize the device */
593 init_device(dev);
594 }
595}
596
597static irqreturn_t talitos_interrupt(int irq, void *data)
598{
599 struct device *dev = data;
600 struct talitos_private *priv = dev_get_drvdata(dev);
601 u32 isr, isr_lo;
602
603 isr = in_be32(priv->reg + TALITOS_ISR);
604 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
Lee Nipperca38a812008-12-20 17:09:25 +1100605 /* Acknowledge interrupt */
606 out_be32(priv->reg + TALITOS_ICR, isr);
607 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800608
Lee Nipperca38a812008-12-20 17:09:25 +1100609 if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
Kim Phillips40405f12008-10-12 20:19:35 +0800610 talitos_error((unsigned long)data, isr, isr_lo);
Lee Nipperca38a812008-12-20 17:09:25 +1100611 else
Lee Nipper1c2e8812008-10-12 20:29:34 +0800612 if (likely(isr & TALITOS_ISR_CHDONE)) {
613 /* mask further done interrupts. */
614 clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
615 /* done_task will unmask done interrupts at exit */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800616 tasklet_schedule(&priv->done_task);
Lee Nipper1c2e8812008-10-12 20:29:34 +0800617 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800618
619 return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
620}
621
622/*
623 * hwrng
624 */
625static int talitos_rng_data_present(struct hwrng *rng, int wait)
626{
627 struct device *dev = (struct device *)rng->priv;
628 struct talitos_private *priv = dev_get_drvdata(dev);
629 u32 ofl;
630 int i;
631
632 for (i = 0; i < 20; i++) {
633 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
634 TALITOS_RNGUSR_LO_OFL;
635 if (ofl || !wait)
636 break;
637 udelay(10);
638 }
639
640 return !!ofl;
641}
642
643static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
644{
645 struct device *dev = (struct device *)rng->priv;
646 struct talitos_private *priv = dev_get_drvdata(dev);
647
648 /* rng fifo requires 64-bit accesses */
649 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
650 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
651
652 return sizeof(u32);
653}
654
655static int talitos_rng_init(struct hwrng *rng)
656{
657 struct device *dev = (struct device *)rng->priv;
658 struct talitos_private *priv = dev_get_drvdata(dev);
659 unsigned int timeout = TALITOS_TIMEOUT;
660
661 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
662 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
663 && --timeout)
664 cpu_relax();
665 if (timeout == 0) {
666 dev_err(dev, "failed to reset rng hw\n");
667 return -ENODEV;
668 }
669
670 /* start generating */
671 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
672
673 return 0;
674}
675
676static int talitos_register_rng(struct device *dev)
677{
678 struct talitos_private *priv = dev_get_drvdata(dev);
679
680 priv->rng.name = dev_driver_string(dev),
681 priv->rng.init = talitos_rng_init,
682 priv->rng.data_present = talitos_rng_data_present,
683 priv->rng.data_read = talitos_rng_data_read,
684 priv->rng.priv = (unsigned long)dev;
685
686 return hwrng_register(&priv->rng);
687}
688
689static void talitos_unregister_rng(struct device *dev)
690{
691 struct talitos_private *priv = dev_get_drvdata(dev);
692
693 hwrng_unregister(&priv->rng);
694}
695
696/*
697 * crypto alg
698 */
699#define TALITOS_CRA_PRIORITY 3000
700#define TALITOS_MAX_KEY_SIZE 64
Lee Nipper3952f172008-07-10 18:29:18 +0800701#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800702
Lee Nipper497f2e62010-05-19 19:20:36 +1000703#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800704
705struct talitos_ctx {
706 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800707 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800708 __be32 desc_hdr_template;
709 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800710 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800711 unsigned int keylen;
712 unsigned int enckeylen;
713 unsigned int authkeylen;
714 unsigned int authsize;
715};
716
Lee Nipper497f2e62010-05-19 19:20:36 +1000717#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
718#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
719
720struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000721 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000722 unsigned int hw_context_size;
723 u8 buf[HASH_MAX_BLOCK_SIZE];
724 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000725 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000726 unsigned int first;
727 unsigned int last;
728 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000729 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000730 struct scatterlist bufsl[2];
731 struct scatterlist *psrc;
732};
733
Lee Nipper56af8cd2009-03-29 15:50:50 +0800734static int aead_setauthsize(struct crypto_aead *authenc,
735 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800736{
737 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
738
739 ctx->authsize = authsize;
740
741 return 0;
742}
743
Lee Nipper56af8cd2009-03-29 15:50:50 +0800744static int aead_setkey(struct crypto_aead *authenc,
745 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800746{
747 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
748 struct rtattr *rta = (void *)key;
749 struct crypto_authenc_key_param *param;
750 unsigned int authkeylen;
751 unsigned int enckeylen;
752
753 if (!RTA_OK(rta, keylen))
754 goto badkey;
755
756 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
757 goto badkey;
758
759 if (RTA_PAYLOAD(rta) < sizeof(*param))
760 goto badkey;
761
762 param = RTA_DATA(rta);
763 enckeylen = be32_to_cpu(param->enckeylen);
764
765 key += RTA_ALIGN(rta->rta_len);
766 keylen -= RTA_ALIGN(rta->rta_len);
767
768 if (keylen < enckeylen)
769 goto badkey;
770
771 authkeylen = keylen - enckeylen;
772
773 if (keylen > TALITOS_MAX_KEY_SIZE)
774 goto badkey;
775
776 memcpy(&ctx->key, key, keylen);
777
778 ctx->keylen = keylen;
779 ctx->enckeylen = enckeylen;
780 ctx->authkeylen = authkeylen;
781
782 return 0;
783
784badkey:
785 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
786 return -EINVAL;
787}
788
789/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800790 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800791 * @src_nents: number of segments in input scatterlist
792 * @dst_nents: number of segments in output scatterlist
793 * @dma_len: length of dma mapped link_tbl space
794 * @dma_link_tbl: bus physical address of link_tbl
795 * @desc: h/w descriptor
796 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
797 *
798 * if decrypting (with authcheck), or either one of src_nents or dst_nents
799 * is greater than 1, an integrity check value is concatenated to the end
800 * of link_tbl data
801 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800802struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800803 int src_nents;
804 int dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800805 int src_is_chained;
806 int dst_is_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800807 int dma_len;
808 dma_addr_t dma_link_tbl;
809 struct talitos_desc desc;
810 struct talitos_ptr link_tbl[0];
811};
812
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800813static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
814 unsigned int nents, enum dma_data_direction dir,
815 int chained)
816{
817 if (unlikely(chained))
818 while (sg) {
819 dma_map_sg(dev, sg, 1, dir);
820 sg = scatterwalk_sg_next(sg);
821 }
822 else
823 dma_map_sg(dev, sg, nents, dir);
824 return nents;
825}
826
827static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
828 enum dma_data_direction dir)
829{
830 while (sg) {
831 dma_unmap_sg(dev, sg, 1, dir);
832 sg = scatterwalk_sg_next(sg);
833 }
834}
835
836static void talitos_sg_unmap(struct device *dev,
837 struct talitos_edesc *edesc,
838 struct scatterlist *src,
839 struct scatterlist *dst)
840{
841 unsigned int src_nents = edesc->src_nents ? : 1;
842 unsigned int dst_nents = edesc->dst_nents ? : 1;
843
844 if (src != dst) {
845 if (edesc->src_is_chained)
846 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
847 else
848 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
849
Lee Nipper497f2e62010-05-19 19:20:36 +1000850 if (dst) {
851 if (edesc->dst_is_chained)
852 talitos_unmap_sg_chain(dev, dst,
853 DMA_FROM_DEVICE);
854 else
855 dma_unmap_sg(dev, dst, dst_nents,
856 DMA_FROM_DEVICE);
857 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800858 } else
859 if (edesc->src_is_chained)
860 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
861 else
862 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
863}
864
Kim Phillips9c4a7962008-06-23 19:50:15 +0800865static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800866 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800867 struct aead_request *areq)
868{
869 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
870 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
871 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
872 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
873
874 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
875
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800876 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800877
878 if (edesc->dma_len)
879 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
880 DMA_BIDIRECTIONAL);
881}
882
883/*
884 * ipsec_esp descriptor callbacks
885 */
886static void ipsec_esp_encrypt_done(struct device *dev,
887 struct talitos_desc *desc, void *context,
888 int err)
889{
890 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800891 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
892 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800893 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800894 struct scatterlist *sg;
895 void *icvdata;
896
Kim Phillips19bbbc62009-03-29 15:53:59 +0800897 edesc = container_of(desc, struct talitos_edesc, desc);
898
Kim Phillips9c4a7962008-06-23 19:50:15 +0800899 ipsec_esp_unmap(dev, edesc, areq);
900
901 /* copy the generated ICV to dst */
902 if (edesc->dma_len) {
903 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800904 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800905 sg = sg_last(areq->dst, edesc->dst_nents);
906 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
907 icvdata, ctx->authsize);
908 }
909
910 kfree(edesc);
911
912 aead_request_complete(areq, err);
913}
914
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800915static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800916 struct talitos_desc *desc,
917 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800918{
919 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800920 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
921 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800922 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800923 struct scatterlist *sg;
924 void *icvdata;
925
Kim Phillips19bbbc62009-03-29 15:53:59 +0800926 edesc = container_of(desc, struct talitos_edesc, desc);
927
Kim Phillips9c4a7962008-06-23 19:50:15 +0800928 ipsec_esp_unmap(dev, edesc, req);
929
930 if (!err) {
931 /* auth check */
932 if (edesc->dma_len)
933 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800934 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800935 else
936 icvdata = &edesc->link_tbl[0];
937
938 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
939 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
940 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
941 }
942
943 kfree(edesc);
944
945 aead_request_complete(req, err);
946}
947
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800948static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800949 struct talitos_desc *desc,
950 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800951{
952 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800953 struct talitos_edesc *edesc;
954
955 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800956
957 ipsec_esp_unmap(dev, edesc, req);
958
959 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800960 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
961 DESC_HDR_LO_ICCR1_PASS))
962 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800963
964 kfree(edesc);
965
966 aead_request_complete(req, err);
967}
968
Kim Phillips9c4a7962008-06-23 19:50:15 +0800969/*
970 * convert scatterlist to SEC h/w link table format
971 * stop at cryptlen bytes
972 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800973static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800974 int cryptlen, struct talitos_ptr *link_tbl_ptr)
975{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800976 int n_sg = sg_count;
977
978 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000979 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800980 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
981 link_tbl_ptr->j_extent = 0;
982 link_tbl_ptr++;
983 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800984 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800985 }
986
Lee Nipper70bcaca2008-07-03 19:08:46 +0800987 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800988 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800989 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800990 /* Empty this entry, and move to previous one */
991 cryptlen += be16_to_cpu(link_tbl_ptr->len);
992 link_tbl_ptr->len = 0;
993 sg_count--;
994 link_tbl_ptr--;
995 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800996 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
997 + cryptlen);
998
999 /* tag end of link table */
1000 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +08001001
1002 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001003}
1004
1005/*
1006 * fill in and submit ipsec_esp descriptor
1007 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001008static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001009 u8 *giv, u64 seq,
1010 void (*callback) (struct device *dev,
1011 struct talitos_desc *desc,
1012 void *context, int error))
1013{
1014 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1015 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1016 struct device *dev = ctx->dev;
1017 struct talitos_desc *desc = &edesc->desc;
1018 unsigned int cryptlen = areq->cryptlen;
1019 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +10001020 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001021 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001022 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001023
1024 /* hmac key */
1025 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
1026 0, DMA_TO_DEVICE);
1027 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +10001028 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1029 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001030 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001031 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
1032 DMA_TO_DEVICE);
1033
1034 /* cipher key */
1035 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1036 (char *)&ctx->key + ctx->authkeylen, 0,
1037 DMA_TO_DEVICE);
1038
1039 /*
1040 * cipher in
1041 * map and adjust cipher len to aead request cryptlen.
1042 * extent is bytes of HMAC postpended to ciphertext,
1043 * typically 12 for ipsec
1044 */
1045 desc->ptr[4].len = cpu_to_be16(cryptlen);
1046 desc->ptr[4].j_extent = authsize;
1047
Kim Phillipse938e462009-03-29 15:53:23 +08001048 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1049 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1050 : DMA_TO_DEVICE,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001051 edesc->src_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001052
1053 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001054 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001055 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001056 sg_link_tbl_len = cryptlen;
1057
Kim Phillips962a9c92009-03-29 15:54:30 +08001058 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001059 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001060
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001061 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001062 &edesc->link_tbl[0]);
1063 if (sg_count > 1) {
1064 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001065 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001066 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1067 edesc->dma_len,
1068 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001069 } else {
1070 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001071 to_talitos_ptr(&desc->ptr[4],
1072 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001073 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001074 }
1075
1076 /* cipher out */
1077 desc->ptr[5].len = cpu_to_be16(cryptlen);
1078 desc->ptr[5].j_extent = authsize;
1079
Kim Phillipse938e462009-03-29 15:53:23 +08001080 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001081 sg_count = talitos_map_sg(dev, areq->dst,
1082 edesc->dst_nents ? : 1,
1083 DMA_FROM_DEVICE,
1084 edesc->dst_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001085
1086 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001087 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001088 } else {
1089 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001090 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001091
Kim Phillips81eb0242009-08-13 11:51:51 +10001092 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1093 (edesc->src_nents + 1) *
1094 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001095 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1096 link_tbl_ptr);
1097
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001098 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001099 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001100 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001101 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001102 link_tbl_ptr++;
1103 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1104 link_tbl_ptr->len = cpu_to_be16(authsize);
1105
1106 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001107 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1108 (edesc->src_nents + edesc->dst_nents + 2) *
1109 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001110 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1111 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1112 edesc->dma_len, DMA_BIDIRECTIONAL);
1113 }
1114
1115 /* iv out */
1116 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1117 DMA_FROM_DEVICE);
1118
Kim Phillips5228f0f2011-07-15 11:21:38 +08001119 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001120 if (ret != -EINPROGRESS) {
1121 ipsec_esp_unmap(dev, edesc, areq);
1122 kfree(edesc);
1123 }
1124 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001125}
1126
Kim Phillips9c4a7962008-06-23 19:50:15 +08001127/*
1128 * derive number of elements in scatterlist
1129 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001130static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001131{
1132 struct scatterlist *sg = sg_list;
1133 int sg_nents = 0;
1134
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001135 *chained = 0;
1136 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001137 sg_nents++;
1138 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001139 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1140 *chained = 1;
1141 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001142 }
1143
1144 return sg_nents;
1145}
1146
Lee Nipper497f2e62010-05-19 19:20:36 +10001147/**
1148 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1149 * @sgl: The SG list
1150 * @nents: Number of SG entries
1151 * @buf: Where to copy to
1152 * @buflen: The number of bytes to copy
1153 * @skip: The number of bytes to skip before copying.
1154 * Note: skip + buflen should equal SG total size.
1155 *
1156 * Returns the number of copied bytes.
1157 *
1158 **/
1159static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1160 void *buf, size_t buflen, unsigned int skip)
1161{
1162 unsigned int offset = 0;
1163 unsigned int boffset = 0;
1164 struct sg_mapping_iter miter;
1165 unsigned long flags;
1166 unsigned int sg_flags = SG_MITER_ATOMIC;
1167 size_t total_buffer = buflen + skip;
1168
1169 sg_flags |= SG_MITER_FROM_SG;
1170
1171 sg_miter_start(&miter, sgl, nents, sg_flags);
1172
1173 local_irq_save(flags);
1174
1175 while (sg_miter_next(&miter) && offset < total_buffer) {
1176 unsigned int len;
1177 unsigned int ignore;
1178
1179 if ((offset + miter.length) > skip) {
1180 if (offset < skip) {
1181 /* Copy part of this segment */
1182 ignore = skip - offset;
1183 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001184 if (boffset + len > buflen)
1185 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001186 memcpy(buf + boffset, miter.addr + ignore, len);
1187 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001188 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001189 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001190 if (boffset + len > buflen)
1191 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001192 memcpy(buf + boffset, miter.addr, len);
1193 }
1194 boffset += len;
1195 }
1196 offset += miter.length;
1197 }
1198
1199 sg_miter_stop(&miter);
1200
1201 local_irq_restore(flags);
1202 return boffset;
1203}
1204
Kim Phillips9c4a7962008-06-23 19:50:15 +08001205/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001206 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001207 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001208static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1209 struct scatterlist *src,
1210 struct scatterlist *dst,
Lee Nipper497f2e62010-05-19 19:20:36 +10001211 int hash_result,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001212 unsigned int cryptlen,
1213 unsigned int authsize,
1214 int icv_stashing,
1215 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001216{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001217 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001218 int src_nents, dst_nents, alloc_len, dma_len;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001219 int src_chained, dst_chained = 0;
1220 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001221 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001222
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001223 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1224 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001225 return ERR_PTR(-EINVAL);
1226 }
1227
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001228 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001229 src_nents = (src_nents == 1) ? 0 : src_nents;
1230
Lee Nipper497f2e62010-05-19 19:20:36 +10001231 if (hash_result) {
1232 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001233 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001234 if (dst == src) {
1235 dst_nents = src_nents;
1236 } else {
1237 dst_nents = sg_count(dst, cryptlen + authsize,
1238 &dst_chained);
1239 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1240 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001241 }
1242
1243 /*
1244 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001245 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001246 * and the ICV data itself
1247 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001248 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001250 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001251 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001252 alloc_len += dma_len;
1253 } else {
1254 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001255 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001256 }
1257
Kim Phillips586725f2008-07-17 20:19:18 +08001258 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001259 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001260 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001261 return ERR_PTR(-ENOMEM);
1262 }
1263
1264 edesc->src_nents = src_nents;
1265 edesc->dst_nents = dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001266 edesc->src_is_chained = src_chained;
1267 edesc->dst_is_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001268 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001269 if (dma_len)
1270 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1271 edesc->dma_len,
1272 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001273
1274 return edesc;
1275}
1276
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001277static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1278 int icv_stashing)
1279{
1280 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1281 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1282
Lee Nipper497f2e62010-05-19 19:20:36 +10001283 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001284 areq->cryptlen, ctx->authsize, icv_stashing,
1285 areq->base.flags);
1286}
1287
Lee Nipper56af8cd2009-03-29 15:50:50 +08001288static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001289{
1290 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1291 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001292 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001293
1294 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001295 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001296 if (IS_ERR(edesc))
1297 return PTR_ERR(edesc);
1298
1299 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001300 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001301
1302 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1303}
1304
Lee Nipper56af8cd2009-03-29 15:50:50 +08001305static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001306{
1307 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1308 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1309 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001310 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001311 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001312 struct scatterlist *sg;
1313 void *icvdata;
1314
1315 req->cryptlen -= authsize;
1316
1317 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001318 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001319 if (IS_ERR(edesc))
1320 return PTR_ERR(edesc);
1321
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001322 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001323 ((!edesc->src_nents && !edesc->dst_nents) ||
1324 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001325
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001326 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001327 edesc->desc.hdr = ctx->desc_hdr_template |
1328 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001329 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001330
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001331 /* reset integrity check result bits */
1332 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001333
Kim Phillipse938e462009-03-29 15:53:23 +08001334 return ipsec_esp(edesc, req, NULL, 0,
1335 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001336
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001337 }
Kim Phillipse938e462009-03-29 15:53:23 +08001338
1339 /* Have to check the ICV with software */
1340 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1341
1342 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1343 if (edesc->dma_len)
1344 icvdata = &edesc->link_tbl[edesc->src_nents +
1345 edesc->dst_nents + 2];
1346 else
1347 icvdata = &edesc->link_tbl[0];
1348
1349 sg = sg_last(req->src, edesc->src_nents ? : 1);
1350
1351 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1352 ctx->authsize);
1353
1354 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001355}
1356
Lee Nipper56af8cd2009-03-29 15:50:50 +08001357static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001358{
1359 struct aead_request *areq = &req->areq;
1360 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1361 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001362 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001363
1364 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001365 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001366 if (IS_ERR(edesc))
1367 return PTR_ERR(edesc);
1368
1369 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001370 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001371
1372 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001373 /* avoid consecutive packets going out with same IV */
1374 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001375
1376 return ipsec_esp(edesc, areq, req->giv, req->seq,
1377 ipsec_esp_encrypt_done);
1378}
1379
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001380static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1381 const u8 *key, unsigned int keylen)
1382{
1383 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001384
1385 memcpy(&ctx->key, key, keylen);
1386 ctx->keylen = keylen;
1387
1388 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001389}
1390
1391static void common_nonsnoop_unmap(struct device *dev,
1392 struct talitos_edesc *edesc,
1393 struct ablkcipher_request *areq)
1394{
1395 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1396 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1397 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1398
1399 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1400
1401 if (edesc->dma_len)
1402 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1403 DMA_BIDIRECTIONAL);
1404}
1405
1406static void ablkcipher_done(struct device *dev,
1407 struct talitos_desc *desc, void *context,
1408 int err)
1409{
1410 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001411 struct talitos_edesc *edesc;
1412
1413 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001414
1415 common_nonsnoop_unmap(dev, edesc, areq);
1416
1417 kfree(edesc);
1418
1419 areq->base.complete(&areq->base, err);
1420}
1421
1422static int common_nonsnoop(struct talitos_edesc *edesc,
1423 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001424 void (*callback) (struct device *dev,
1425 struct talitos_desc *desc,
1426 void *context, int error))
1427{
1428 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1429 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1430 struct device *dev = ctx->dev;
1431 struct talitos_desc *desc = &edesc->desc;
1432 unsigned int cryptlen = areq->nbytes;
1433 unsigned int ivsize;
1434 int sg_count, ret;
1435
1436 /* first DWORD empty */
1437 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001438 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001439 desc->ptr[0].j_extent = 0;
1440
1441 /* cipher iv */
1442 ivsize = crypto_ablkcipher_ivsize(cipher);
Kim Phillipsfebec542011-07-15 11:21:39 +08001443 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001444 DMA_TO_DEVICE);
1445
1446 /* cipher key */
1447 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1448 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1449
1450 /*
1451 * cipher in
1452 */
1453 desc->ptr[3].len = cpu_to_be16(cryptlen);
1454 desc->ptr[3].j_extent = 0;
1455
1456 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1457 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1458 : DMA_TO_DEVICE,
1459 edesc->src_is_chained);
1460
1461 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001462 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001463 } else {
1464 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1465 &edesc->link_tbl[0]);
1466 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001467 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001468 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001469 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1470 edesc->dma_len,
1471 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001472 } else {
1473 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001474 to_talitos_ptr(&desc->ptr[3],
1475 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001476 }
1477 }
1478
1479 /* cipher out */
1480 desc->ptr[4].len = cpu_to_be16(cryptlen);
1481 desc->ptr[4].j_extent = 0;
1482
1483 if (areq->src != areq->dst)
1484 sg_count = talitos_map_sg(dev, areq->dst,
1485 edesc->dst_nents ? : 1,
1486 DMA_FROM_DEVICE,
1487 edesc->dst_is_chained);
1488
1489 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001490 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001491 } else {
1492 struct talitos_ptr *link_tbl_ptr =
1493 &edesc->link_tbl[edesc->src_nents + 1];
1494
Kim Phillips81eb0242009-08-13 11:51:51 +10001495 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1496 (edesc->src_nents + 1) *
1497 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001498 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001499 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1500 link_tbl_ptr);
1501 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1502 edesc->dma_len, DMA_BIDIRECTIONAL);
1503 }
1504
1505 /* iv out */
1506 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1507 DMA_FROM_DEVICE);
1508
1509 /* last DWORD empty */
1510 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001511 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001512 desc->ptr[6].j_extent = 0;
1513
Kim Phillips5228f0f2011-07-15 11:21:38 +08001514 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001515 if (ret != -EINPROGRESS) {
1516 common_nonsnoop_unmap(dev, edesc, areq);
1517 kfree(edesc);
1518 }
1519 return ret;
1520}
1521
Kim Phillipse938e462009-03-29 15:53:23 +08001522static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1523 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001524{
1525 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1526 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1527
Lee Nipper497f2e62010-05-19 19:20:36 +10001528 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1529 areq->nbytes, 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001530}
1531
1532static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1533{
1534 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1535 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1536 struct talitos_edesc *edesc;
1537
1538 /* allocate extended descriptor */
1539 edesc = ablkcipher_edesc_alloc(areq);
1540 if (IS_ERR(edesc))
1541 return PTR_ERR(edesc);
1542
1543 /* set encrypt */
1544 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1545
Kim Phillipsfebec542011-07-15 11:21:39 +08001546 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001547}
1548
1549static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1550{
1551 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1552 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1553 struct talitos_edesc *edesc;
1554
1555 /* allocate extended descriptor */
1556 edesc = ablkcipher_edesc_alloc(areq);
1557 if (IS_ERR(edesc))
1558 return PTR_ERR(edesc);
1559
1560 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1561
Kim Phillipsfebec542011-07-15 11:21:39 +08001562 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001563}
1564
Lee Nipper497f2e62010-05-19 19:20:36 +10001565static void common_nonsnoop_hash_unmap(struct device *dev,
1566 struct talitos_edesc *edesc,
1567 struct ahash_request *areq)
1568{
1569 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1570
1571 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1572
1573 /* When using hashctx-in, must unmap it. */
1574 if (edesc->desc.ptr[1].len)
1575 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1576 DMA_TO_DEVICE);
1577
1578 if (edesc->desc.ptr[2].len)
1579 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1580 DMA_TO_DEVICE);
1581
1582 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1583
1584 if (edesc->dma_len)
1585 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1586 DMA_BIDIRECTIONAL);
1587
1588}
1589
1590static void ahash_done(struct device *dev,
1591 struct talitos_desc *desc, void *context,
1592 int err)
1593{
1594 struct ahash_request *areq = context;
1595 struct talitos_edesc *edesc =
1596 container_of(desc, struct talitos_edesc, desc);
1597 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1598
1599 if (!req_ctx->last && req_ctx->to_hash_later) {
1600 /* Position any partial block for next update/final/finup */
1601 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001602 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001603 }
1604 common_nonsnoop_hash_unmap(dev, edesc, areq);
1605
1606 kfree(edesc);
1607
1608 areq->base.complete(&areq->base, err);
1609}
1610
1611static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1612 struct ahash_request *areq, unsigned int length,
1613 void (*callback) (struct device *dev,
1614 struct talitos_desc *desc,
1615 void *context, int error))
1616{
1617 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1618 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1619 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1620 struct device *dev = ctx->dev;
1621 struct talitos_desc *desc = &edesc->desc;
1622 int sg_count, ret;
1623
1624 /* first DWORD empty */
1625 desc->ptr[0] = zero_entry;
1626
Kim Phillips60f208d2010-05-19 19:21:53 +10001627 /* hash context in */
1628 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001629 map_single_talitos_ptr(dev, &desc->ptr[1],
1630 req_ctx->hw_context_size,
1631 (char *)req_ctx->hw_context, 0,
1632 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001633 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001634 } else {
1635 desc->ptr[1] = zero_entry;
1636 /* Indicate next op is not the first. */
1637 req_ctx->first = 0;
1638 }
1639
1640 /* HMAC key */
1641 if (ctx->keylen)
1642 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1643 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1644 else
1645 desc->ptr[2] = zero_entry;
1646
1647 /*
1648 * data in
1649 */
1650 desc->ptr[3].len = cpu_to_be16(length);
1651 desc->ptr[3].j_extent = 0;
1652
1653 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1654 edesc->src_nents ? : 1,
1655 DMA_TO_DEVICE,
1656 edesc->src_is_chained);
1657
1658 if (sg_count == 1) {
1659 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1660 } else {
1661 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1662 &edesc->link_tbl[0]);
1663 if (sg_count > 1) {
1664 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1665 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1666 dma_sync_single_for_device(ctx->dev,
1667 edesc->dma_link_tbl,
1668 edesc->dma_len,
1669 DMA_BIDIRECTIONAL);
1670 } else {
1671 /* Only one segment now, so no link tbl needed */
1672 to_talitos_ptr(&desc->ptr[3],
1673 sg_dma_address(req_ctx->psrc));
1674 }
1675 }
1676
1677 /* fifth DWORD empty */
1678 desc->ptr[4] = zero_entry;
1679
1680 /* hash/HMAC out -or- hash context out */
1681 if (req_ctx->last)
1682 map_single_talitos_ptr(dev, &desc->ptr[5],
1683 crypto_ahash_digestsize(tfm),
1684 areq->result, 0, DMA_FROM_DEVICE);
1685 else
1686 map_single_talitos_ptr(dev, &desc->ptr[5],
1687 req_ctx->hw_context_size,
1688 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1689
1690 /* last DWORD empty */
1691 desc->ptr[6] = zero_entry;
1692
Kim Phillips5228f0f2011-07-15 11:21:38 +08001693 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001694 if (ret != -EINPROGRESS) {
1695 common_nonsnoop_hash_unmap(dev, edesc, areq);
1696 kfree(edesc);
1697 }
1698 return ret;
1699}
1700
1701static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1702 unsigned int nbytes)
1703{
1704 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1705 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1706 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1707
1708 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1709 nbytes, 0, 0, areq->base.flags);
1710}
1711
1712static int ahash_init(struct ahash_request *areq)
1713{
1714 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1715 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1716
1717 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001718 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001719 req_ctx->first = 1; /* first indicates h/w must init its context */
1720 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001721 req_ctx->hw_context_size =
1722 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1723 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1724 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1725
1726 return 0;
1727}
1728
Kim Phillips60f208d2010-05-19 19:21:53 +10001729/*
1730 * on h/w without explicit sha224 support, we initialize h/w context
1731 * manually with sha224 constants, and tell it to run sha256.
1732 */
1733static int ahash_init_sha224_swinit(struct ahash_request *areq)
1734{
1735 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1736
1737 ahash_init(areq);
1738 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1739
Kim Phillipsa7524472010-09-23 15:56:38 +08001740 req_ctx->hw_context[0] = SHA224_H0;
1741 req_ctx->hw_context[1] = SHA224_H1;
1742 req_ctx->hw_context[2] = SHA224_H2;
1743 req_ctx->hw_context[3] = SHA224_H3;
1744 req_ctx->hw_context[4] = SHA224_H4;
1745 req_ctx->hw_context[5] = SHA224_H5;
1746 req_ctx->hw_context[6] = SHA224_H6;
1747 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001748
1749 /* init 64-bit count */
1750 req_ctx->hw_context[8] = 0;
1751 req_ctx->hw_context[9] = 0;
1752
1753 return 0;
1754}
1755
Lee Nipper497f2e62010-05-19 19:20:36 +10001756static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1757{
1758 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1759 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1760 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1761 struct talitos_edesc *edesc;
1762 unsigned int blocksize =
1763 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1764 unsigned int nbytes_to_hash;
1765 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001766 unsigned int nsg;
Lee Nipper497f2e62010-05-19 19:20:36 +10001767 int chained;
1768
Lee Nipper5e833bc2010-06-16 15:29:15 +10001769 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1770 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001771 sg_copy_to_buffer(areq->src,
1772 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001773 req_ctx->buf + req_ctx->nbuf, nbytes);
1774 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001775 return 0;
1776 }
1777
Lee Nipper5e833bc2010-06-16 15:29:15 +10001778 /* At least (blocksize + 1) bytes are available to hash */
1779 nbytes_to_hash = nbytes + req_ctx->nbuf;
1780 to_hash_later = nbytes_to_hash & (blocksize - 1);
1781
1782 if (req_ctx->last)
1783 to_hash_later = 0;
1784 else if (to_hash_later)
1785 /* There is a partial block. Hash the full block(s) now */
1786 nbytes_to_hash -= to_hash_later;
1787 else {
1788 /* Keep one block buffered */
1789 nbytes_to_hash -= blocksize;
1790 to_hash_later = blocksize;
1791 }
1792
1793 /* Chain in any previously buffered data */
1794 if (req_ctx->nbuf) {
1795 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1796 sg_init_table(req_ctx->bufsl, nsg);
1797 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1798 if (nsg > 1)
1799 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001800 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001801 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001802 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001803
Lee Nipper5e833bc2010-06-16 15:29:15 +10001804 if (to_hash_later) {
1805 int nents = sg_count(areq->src, nbytes, &chained);
1806 sg_copy_end_to_buffer(areq->src, nents,
1807 req_ctx->bufnext,
1808 to_hash_later,
1809 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001810 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001811 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001812
Lee Nipper5e833bc2010-06-16 15:29:15 +10001813 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001814 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1815 if (IS_ERR(edesc))
1816 return PTR_ERR(edesc);
1817
1818 edesc->desc.hdr = ctx->desc_hdr_template;
1819
1820 /* On last one, request SEC to pad; otherwise continue */
1821 if (req_ctx->last)
1822 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1823 else
1824 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1825
Kim Phillips60f208d2010-05-19 19:21:53 +10001826 /* request SEC to INIT hash. */
1827 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001828 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1829
1830 /* When the tfm context has a keylen, it's an HMAC.
1831 * A first or last (ie. not middle) descriptor must request HMAC.
1832 */
1833 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1834 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1835
1836 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1837 ahash_done);
1838}
1839
1840static int ahash_update(struct ahash_request *areq)
1841{
1842 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1843
1844 req_ctx->last = 0;
1845
1846 return ahash_process_req(areq, areq->nbytes);
1847}
1848
1849static int ahash_final(struct ahash_request *areq)
1850{
1851 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1852
1853 req_ctx->last = 1;
1854
1855 return ahash_process_req(areq, 0);
1856}
1857
1858static int ahash_finup(struct ahash_request *areq)
1859{
1860 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1861
1862 req_ctx->last = 1;
1863
1864 return ahash_process_req(areq, areq->nbytes);
1865}
1866
1867static int ahash_digest(struct ahash_request *areq)
1868{
1869 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001870 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001871
Kim Phillips60f208d2010-05-19 19:21:53 +10001872 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001873 req_ctx->last = 1;
1874
1875 return ahash_process_req(areq, areq->nbytes);
1876}
1877
Lee Nipper79b3a412011-11-21 16:13:25 +08001878struct keyhash_result {
1879 struct completion completion;
1880 int err;
1881};
1882
1883static void keyhash_complete(struct crypto_async_request *req, int err)
1884{
1885 struct keyhash_result *res = req->data;
1886
1887 if (err == -EINPROGRESS)
1888 return;
1889
1890 res->err = err;
1891 complete(&res->completion);
1892}
1893
1894static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1895 u8 *hash)
1896{
1897 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1898
1899 struct scatterlist sg[1];
1900 struct ahash_request *req;
1901 struct keyhash_result hresult;
1902 int ret;
1903
1904 init_completion(&hresult.completion);
1905
1906 req = ahash_request_alloc(tfm, GFP_KERNEL);
1907 if (!req)
1908 return -ENOMEM;
1909
1910 /* Keep tfm keylen == 0 during hash of the long key */
1911 ctx->keylen = 0;
1912 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1913 keyhash_complete, &hresult);
1914
1915 sg_init_one(&sg[0], key, keylen);
1916
1917 ahash_request_set_crypt(req, sg, hash, keylen);
1918 ret = crypto_ahash_digest(req);
1919 switch (ret) {
1920 case 0:
1921 break;
1922 case -EINPROGRESS:
1923 case -EBUSY:
1924 ret = wait_for_completion_interruptible(
1925 &hresult.completion);
1926 if (!ret)
1927 ret = hresult.err;
1928 break;
1929 default:
1930 break;
1931 }
1932 ahash_request_free(req);
1933
1934 return ret;
1935}
1936
1937static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1938 unsigned int keylen)
1939{
1940 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1941 unsigned int blocksize =
1942 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1943 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1944 unsigned int keysize = keylen;
1945 u8 hash[SHA512_DIGEST_SIZE];
1946 int ret;
1947
1948 if (keylen <= blocksize)
1949 memcpy(ctx->key, key, keysize);
1950 else {
1951 /* Must get the hash of the long key */
1952 ret = keyhash(tfm, key, keylen, hash);
1953
1954 if (ret) {
1955 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1956 return -EINVAL;
1957 }
1958
1959 keysize = digestsize;
1960 memcpy(ctx->key, hash, digestsize);
1961 }
1962
1963 ctx->keylen = keysize;
1964
1965 return 0;
1966}
1967
1968
Kim Phillips9c4a7962008-06-23 19:50:15 +08001969struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001970 u32 type;
1971 union {
1972 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001973 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001974 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001975 __be32 desc_hdr_template;
1976};
1977
1978static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001979 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001980 { .type = CRYPTO_ALG_TYPE_AEAD,
1981 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001982 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1983 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1984 .cra_blocksize = AES_BLOCK_SIZE,
1985 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1986 .cra_type = &crypto_aead_type,
1987 .cra_aead = {
1988 .setkey = aead_setkey,
1989 .setauthsize = aead_setauthsize,
1990 .encrypt = aead_encrypt,
1991 .decrypt = aead_decrypt,
1992 .givencrypt = aead_givencrypt,
1993 .geniv = "<built-in>",
1994 .ivsize = AES_BLOCK_SIZE,
1995 .maxauthsize = SHA1_DIGEST_SIZE,
1996 }
1997 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001998 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1999 DESC_HDR_SEL0_AESU |
2000 DESC_HDR_MODE0_AESU_CBC |
2001 DESC_HDR_SEL1_MDEUA |
2002 DESC_HDR_MODE1_MDEU_INIT |
2003 DESC_HDR_MODE1_MDEU_PAD |
2004 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08002005 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002006 { .type = CRYPTO_ALG_TYPE_AEAD,
2007 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002008 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
2009 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
2010 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2011 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2012 .cra_type = &crypto_aead_type,
2013 .cra_aead = {
2014 .setkey = aead_setkey,
2015 .setauthsize = aead_setauthsize,
2016 .encrypt = aead_encrypt,
2017 .decrypt = aead_decrypt,
2018 .givencrypt = aead_givencrypt,
2019 .geniv = "<built-in>",
2020 .ivsize = DES3_EDE_BLOCK_SIZE,
2021 .maxauthsize = SHA1_DIGEST_SIZE,
2022 }
2023 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08002024 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2025 DESC_HDR_SEL0_DEU |
2026 DESC_HDR_MODE0_DEU_CBC |
2027 DESC_HDR_MODE0_DEU_3DES |
2028 DESC_HDR_SEL1_MDEUA |
2029 DESC_HDR_MODE1_MDEU_INIT |
2030 DESC_HDR_MODE1_MDEU_PAD |
2031 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08002032 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002033 { .type = CRYPTO_ALG_TYPE_AEAD,
2034 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002035 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2036 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
2037 .cra_blocksize = AES_BLOCK_SIZE,
2038 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2039 .cra_type = &crypto_aead_type,
2040 .cra_aead = {
2041 .setkey = aead_setkey,
2042 .setauthsize = aead_setauthsize,
2043 .encrypt = aead_encrypt,
2044 .decrypt = aead_decrypt,
2045 .givencrypt = aead_givencrypt,
2046 .geniv = "<built-in>",
2047 .ivsize = AES_BLOCK_SIZE,
2048 .maxauthsize = SHA256_DIGEST_SIZE,
2049 }
2050 },
Lee Nipper3952f172008-07-10 18:29:18 +08002051 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2052 DESC_HDR_SEL0_AESU |
2053 DESC_HDR_MODE0_AESU_CBC |
2054 DESC_HDR_SEL1_MDEUA |
2055 DESC_HDR_MODE1_MDEU_INIT |
2056 DESC_HDR_MODE1_MDEU_PAD |
2057 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2058 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002059 { .type = CRYPTO_ALG_TYPE_AEAD,
2060 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002061 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2062 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2063 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2064 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2065 .cra_type = &crypto_aead_type,
2066 .cra_aead = {
2067 .setkey = aead_setkey,
2068 .setauthsize = aead_setauthsize,
2069 .encrypt = aead_encrypt,
2070 .decrypt = aead_decrypt,
2071 .givencrypt = aead_givencrypt,
2072 .geniv = "<built-in>",
2073 .ivsize = DES3_EDE_BLOCK_SIZE,
2074 .maxauthsize = SHA256_DIGEST_SIZE,
2075 }
2076 },
Lee Nipper3952f172008-07-10 18:29:18 +08002077 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2078 DESC_HDR_SEL0_DEU |
2079 DESC_HDR_MODE0_DEU_CBC |
2080 DESC_HDR_MODE0_DEU_3DES |
2081 DESC_HDR_SEL1_MDEUA |
2082 DESC_HDR_MODE1_MDEU_INIT |
2083 DESC_HDR_MODE1_MDEU_PAD |
2084 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2085 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002086 { .type = CRYPTO_ALG_TYPE_AEAD,
2087 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002088 .cra_name = "authenc(hmac(md5),cbc(aes))",
2089 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2090 .cra_blocksize = AES_BLOCK_SIZE,
2091 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2092 .cra_type = &crypto_aead_type,
2093 .cra_aead = {
2094 .setkey = aead_setkey,
2095 .setauthsize = aead_setauthsize,
2096 .encrypt = aead_encrypt,
2097 .decrypt = aead_decrypt,
2098 .givencrypt = aead_givencrypt,
2099 .geniv = "<built-in>",
2100 .ivsize = AES_BLOCK_SIZE,
2101 .maxauthsize = MD5_DIGEST_SIZE,
2102 }
2103 },
Lee Nipper3952f172008-07-10 18:29:18 +08002104 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2105 DESC_HDR_SEL0_AESU |
2106 DESC_HDR_MODE0_AESU_CBC |
2107 DESC_HDR_SEL1_MDEUA |
2108 DESC_HDR_MODE1_MDEU_INIT |
2109 DESC_HDR_MODE1_MDEU_PAD |
2110 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2111 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002112 { .type = CRYPTO_ALG_TYPE_AEAD,
2113 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002114 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2115 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2116 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2117 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2118 .cra_type = &crypto_aead_type,
2119 .cra_aead = {
2120 .setkey = aead_setkey,
2121 .setauthsize = aead_setauthsize,
2122 .encrypt = aead_encrypt,
2123 .decrypt = aead_decrypt,
2124 .givencrypt = aead_givencrypt,
2125 .geniv = "<built-in>",
2126 .ivsize = DES3_EDE_BLOCK_SIZE,
2127 .maxauthsize = MD5_DIGEST_SIZE,
2128 }
2129 },
Lee Nipper3952f172008-07-10 18:29:18 +08002130 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2131 DESC_HDR_SEL0_DEU |
2132 DESC_HDR_MODE0_DEU_CBC |
2133 DESC_HDR_MODE0_DEU_3DES |
2134 DESC_HDR_SEL1_MDEUA |
2135 DESC_HDR_MODE1_MDEU_INIT |
2136 DESC_HDR_MODE1_MDEU_PAD |
2137 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002138 },
2139 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002140 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2141 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002142 .cra_name = "cbc(aes)",
2143 .cra_driver_name = "cbc-aes-talitos",
2144 .cra_blocksize = AES_BLOCK_SIZE,
2145 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2146 CRYPTO_ALG_ASYNC,
2147 .cra_type = &crypto_ablkcipher_type,
2148 .cra_ablkcipher = {
2149 .setkey = ablkcipher_setkey,
2150 .encrypt = ablkcipher_encrypt,
2151 .decrypt = ablkcipher_decrypt,
2152 .geniv = "eseqiv",
2153 .min_keysize = AES_MIN_KEY_SIZE,
2154 .max_keysize = AES_MAX_KEY_SIZE,
2155 .ivsize = AES_BLOCK_SIZE,
2156 }
2157 },
2158 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2159 DESC_HDR_SEL0_AESU |
2160 DESC_HDR_MODE0_AESU_CBC,
2161 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002162 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2163 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002164 .cra_name = "cbc(des3_ede)",
2165 .cra_driver_name = "cbc-3des-talitos",
2166 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2167 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2168 CRYPTO_ALG_ASYNC,
2169 .cra_type = &crypto_ablkcipher_type,
2170 .cra_ablkcipher = {
2171 .setkey = ablkcipher_setkey,
2172 .encrypt = ablkcipher_encrypt,
2173 .decrypt = ablkcipher_decrypt,
2174 .geniv = "eseqiv",
2175 .min_keysize = DES3_EDE_KEY_SIZE,
2176 .max_keysize = DES3_EDE_KEY_SIZE,
2177 .ivsize = DES3_EDE_BLOCK_SIZE,
2178 }
2179 },
2180 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2181 DESC_HDR_SEL0_DEU |
2182 DESC_HDR_MODE0_DEU_CBC |
2183 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002184 },
2185 /* AHASH algorithms. */
2186 { .type = CRYPTO_ALG_TYPE_AHASH,
2187 .alg.hash = {
2188 .init = ahash_init,
2189 .update = ahash_update,
2190 .final = ahash_final,
2191 .finup = ahash_finup,
2192 .digest = ahash_digest,
2193 .halg.digestsize = MD5_DIGEST_SIZE,
2194 .halg.base = {
2195 .cra_name = "md5",
2196 .cra_driver_name = "md5-talitos",
2197 .cra_blocksize = MD5_BLOCK_SIZE,
2198 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2199 CRYPTO_ALG_ASYNC,
2200 .cra_type = &crypto_ahash_type
2201 }
2202 },
2203 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2204 DESC_HDR_SEL0_MDEUA |
2205 DESC_HDR_MODE0_MDEU_MD5,
2206 },
2207 { .type = CRYPTO_ALG_TYPE_AHASH,
2208 .alg.hash = {
2209 .init = ahash_init,
2210 .update = ahash_update,
2211 .final = ahash_final,
2212 .finup = ahash_finup,
2213 .digest = ahash_digest,
2214 .halg.digestsize = SHA1_DIGEST_SIZE,
2215 .halg.base = {
2216 .cra_name = "sha1",
2217 .cra_driver_name = "sha1-talitos",
2218 .cra_blocksize = SHA1_BLOCK_SIZE,
2219 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2220 CRYPTO_ALG_ASYNC,
2221 .cra_type = &crypto_ahash_type
2222 }
2223 },
2224 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2225 DESC_HDR_SEL0_MDEUA |
2226 DESC_HDR_MODE0_MDEU_SHA1,
2227 },
2228 { .type = CRYPTO_ALG_TYPE_AHASH,
2229 .alg.hash = {
2230 .init = ahash_init,
2231 .update = ahash_update,
2232 .final = ahash_final,
2233 .finup = ahash_finup,
2234 .digest = ahash_digest,
Kim Phillips60f208d2010-05-19 19:21:53 +10002235 .halg.digestsize = SHA224_DIGEST_SIZE,
2236 .halg.base = {
2237 .cra_name = "sha224",
2238 .cra_driver_name = "sha224-talitos",
2239 .cra_blocksize = SHA224_BLOCK_SIZE,
2240 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2241 CRYPTO_ALG_ASYNC,
2242 .cra_type = &crypto_ahash_type
2243 }
2244 },
2245 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2246 DESC_HDR_SEL0_MDEUA |
2247 DESC_HDR_MODE0_MDEU_SHA224,
2248 },
2249 { .type = CRYPTO_ALG_TYPE_AHASH,
2250 .alg.hash = {
2251 .init = ahash_init,
2252 .update = ahash_update,
2253 .final = ahash_final,
2254 .finup = ahash_finup,
2255 .digest = ahash_digest,
Lee Nipper497f2e62010-05-19 19:20:36 +10002256 .halg.digestsize = SHA256_DIGEST_SIZE,
2257 .halg.base = {
2258 .cra_name = "sha256",
2259 .cra_driver_name = "sha256-talitos",
2260 .cra_blocksize = SHA256_BLOCK_SIZE,
2261 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2262 CRYPTO_ALG_ASYNC,
2263 .cra_type = &crypto_ahash_type
2264 }
2265 },
2266 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2267 DESC_HDR_SEL0_MDEUA |
2268 DESC_HDR_MODE0_MDEU_SHA256,
2269 },
2270 { .type = CRYPTO_ALG_TYPE_AHASH,
2271 .alg.hash = {
2272 .init = ahash_init,
2273 .update = ahash_update,
2274 .final = ahash_final,
2275 .finup = ahash_finup,
2276 .digest = ahash_digest,
2277 .halg.digestsize = SHA384_DIGEST_SIZE,
2278 .halg.base = {
2279 .cra_name = "sha384",
2280 .cra_driver_name = "sha384-talitos",
2281 .cra_blocksize = SHA384_BLOCK_SIZE,
2282 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2283 CRYPTO_ALG_ASYNC,
2284 .cra_type = &crypto_ahash_type
2285 }
2286 },
2287 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2288 DESC_HDR_SEL0_MDEUB |
2289 DESC_HDR_MODE0_MDEUB_SHA384,
2290 },
2291 { .type = CRYPTO_ALG_TYPE_AHASH,
2292 .alg.hash = {
2293 .init = ahash_init,
2294 .update = ahash_update,
2295 .final = ahash_final,
2296 .finup = ahash_finup,
2297 .digest = ahash_digest,
2298 .halg.digestsize = SHA512_DIGEST_SIZE,
2299 .halg.base = {
2300 .cra_name = "sha512",
2301 .cra_driver_name = "sha512-talitos",
2302 .cra_blocksize = SHA512_BLOCK_SIZE,
2303 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2304 CRYPTO_ALG_ASYNC,
2305 .cra_type = &crypto_ahash_type
2306 }
2307 },
2308 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2309 DESC_HDR_SEL0_MDEUB |
2310 DESC_HDR_MODE0_MDEUB_SHA512,
2311 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002312 { .type = CRYPTO_ALG_TYPE_AHASH,
2313 .alg.hash = {
2314 .init = ahash_init,
2315 .update = ahash_update,
2316 .final = ahash_final,
2317 .finup = ahash_finup,
2318 .digest = ahash_digest,
2319 .setkey = ahash_setkey,
2320 .halg.digestsize = MD5_DIGEST_SIZE,
2321 .halg.base = {
2322 .cra_name = "hmac(md5)",
2323 .cra_driver_name = "hmac-md5-talitos",
2324 .cra_blocksize = MD5_BLOCK_SIZE,
2325 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2326 CRYPTO_ALG_ASYNC,
2327 .cra_type = &crypto_ahash_type
2328 }
2329 },
2330 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2331 DESC_HDR_SEL0_MDEUA |
2332 DESC_HDR_MODE0_MDEU_MD5,
2333 },
2334 { .type = CRYPTO_ALG_TYPE_AHASH,
2335 .alg.hash = {
2336 .init = ahash_init,
2337 .update = ahash_update,
2338 .final = ahash_final,
2339 .finup = ahash_finup,
2340 .digest = ahash_digest,
2341 .setkey = ahash_setkey,
2342 .halg.digestsize = SHA1_DIGEST_SIZE,
2343 .halg.base = {
2344 .cra_name = "hmac(sha1)",
2345 .cra_driver_name = "hmac-sha1-talitos",
2346 .cra_blocksize = SHA1_BLOCK_SIZE,
2347 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2348 CRYPTO_ALG_ASYNC,
2349 .cra_type = &crypto_ahash_type
2350 }
2351 },
2352 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2353 DESC_HDR_SEL0_MDEUA |
2354 DESC_HDR_MODE0_MDEU_SHA1,
2355 },
2356 { .type = CRYPTO_ALG_TYPE_AHASH,
2357 .alg.hash = {
2358 .init = ahash_init,
2359 .update = ahash_update,
2360 .final = ahash_final,
2361 .finup = ahash_finup,
2362 .digest = ahash_digest,
2363 .setkey = ahash_setkey,
2364 .halg.digestsize = SHA224_DIGEST_SIZE,
2365 .halg.base = {
2366 .cra_name = "hmac(sha224)",
2367 .cra_driver_name = "hmac-sha224-talitos",
2368 .cra_blocksize = SHA224_BLOCK_SIZE,
2369 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2370 CRYPTO_ALG_ASYNC,
2371 .cra_type = &crypto_ahash_type
2372 }
2373 },
2374 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2375 DESC_HDR_SEL0_MDEUA |
2376 DESC_HDR_MODE0_MDEU_SHA224,
2377 },
2378 { .type = CRYPTO_ALG_TYPE_AHASH,
2379 .alg.hash = {
2380 .init = ahash_init,
2381 .update = ahash_update,
2382 .final = ahash_final,
2383 .finup = ahash_finup,
2384 .digest = ahash_digest,
2385 .setkey = ahash_setkey,
2386 .halg.digestsize = SHA256_DIGEST_SIZE,
2387 .halg.base = {
2388 .cra_name = "hmac(sha256)",
2389 .cra_driver_name = "hmac-sha256-talitos",
2390 .cra_blocksize = SHA256_BLOCK_SIZE,
2391 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2392 CRYPTO_ALG_ASYNC,
2393 .cra_type = &crypto_ahash_type
2394 }
2395 },
2396 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2397 DESC_HDR_SEL0_MDEUA |
2398 DESC_HDR_MODE0_MDEU_SHA256,
2399 },
2400 { .type = CRYPTO_ALG_TYPE_AHASH,
2401 .alg.hash = {
2402 .init = ahash_init,
2403 .update = ahash_update,
2404 .final = ahash_final,
2405 .finup = ahash_finup,
2406 .digest = ahash_digest,
2407 .setkey = ahash_setkey,
2408 .halg.digestsize = SHA384_DIGEST_SIZE,
2409 .halg.base = {
2410 .cra_name = "hmac(sha384)",
2411 .cra_driver_name = "hmac-sha384-talitos",
2412 .cra_blocksize = SHA384_BLOCK_SIZE,
2413 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2414 CRYPTO_ALG_ASYNC,
2415 .cra_type = &crypto_ahash_type
2416 }
2417 },
2418 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2419 DESC_HDR_SEL0_MDEUB |
2420 DESC_HDR_MODE0_MDEUB_SHA384,
2421 },
2422 { .type = CRYPTO_ALG_TYPE_AHASH,
2423 .alg.hash = {
2424 .init = ahash_init,
2425 .update = ahash_update,
2426 .final = ahash_final,
2427 .finup = ahash_finup,
2428 .digest = ahash_digest,
2429 .setkey = ahash_setkey,
2430 .halg.digestsize = SHA512_DIGEST_SIZE,
2431 .halg.base = {
2432 .cra_name = "hmac(sha512)",
2433 .cra_driver_name = "hmac-sha512-talitos",
2434 .cra_blocksize = SHA512_BLOCK_SIZE,
2435 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2436 CRYPTO_ALG_ASYNC,
2437 .cra_type = &crypto_ahash_type
2438 }
2439 },
2440 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2441 DESC_HDR_SEL0_MDEUB |
2442 DESC_HDR_MODE0_MDEUB_SHA512,
2443 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002444};
2445
2446struct talitos_crypto_alg {
2447 struct list_head entry;
2448 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002449 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002450};
2451
2452static int talitos_cra_init(struct crypto_tfm *tfm)
2453{
2454 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002455 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002456 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002457 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002458
Lee Nipper497f2e62010-05-19 19:20:36 +10002459 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2460 talitos_alg = container_of(__crypto_ahash_alg(alg),
2461 struct talitos_crypto_alg,
2462 algt.alg.hash);
2463 else
2464 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2465 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002466
Kim Phillips9c4a7962008-06-23 19:50:15 +08002467 /* update context with ptr to dev */
2468 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002469
Kim Phillips5228f0f2011-07-15 11:21:38 +08002470 /* assign SEC channel to tfm in round-robin fashion */
2471 priv = dev_get_drvdata(ctx->dev);
2472 ctx->ch = atomic_inc_return(&priv->last_chan) &
2473 (priv->num_channels - 1);
2474
Kim Phillips9c4a7962008-06-23 19:50:15 +08002475 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002476 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002477
Kim Phillips602dba52011-07-15 11:21:39 +08002478 /* select done notification */
2479 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2480
Lee Nipper497f2e62010-05-19 19:20:36 +10002481 return 0;
2482}
2483
2484static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2485{
2486 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2487
2488 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002489
2490 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002491 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002492
2493 return 0;
2494}
2495
Lee Nipper497f2e62010-05-19 19:20:36 +10002496static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2497{
2498 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2499
2500 talitos_cra_init(tfm);
2501
2502 ctx->keylen = 0;
2503 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2504 sizeof(struct talitos_ahash_req_ctx));
2505
2506 return 0;
2507}
2508
Kim Phillips9c4a7962008-06-23 19:50:15 +08002509/*
2510 * given the alg's descriptor header template, determine whether descriptor
2511 * type and primary/secondary execution units required match the hw
2512 * capabilities description provided in the device tree node.
2513 */
2514static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2515{
2516 struct talitos_private *priv = dev_get_drvdata(dev);
2517 int ret;
2518
2519 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2520 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2521
2522 if (SECONDARY_EU(desc_hdr_template))
2523 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2524 & priv->exec_units);
2525
2526 return ret;
2527}
2528
Grant Likely2dc11582010-08-06 09:25:50 -06002529static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002530{
2531 struct device *dev = &ofdev->dev;
2532 struct talitos_private *priv = dev_get_drvdata(dev);
2533 struct talitos_crypto_alg *t_alg, *n;
2534 int i;
2535
2536 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002537 switch (t_alg->algt.type) {
2538 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2539 case CRYPTO_ALG_TYPE_AEAD:
2540 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2541 break;
2542 case CRYPTO_ALG_TYPE_AHASH:
2543 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2544 break;
2545 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002546 list_del(&t_alg->entry);
2547 kfree(t_alg);
2548 }
2549
2550 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2551 talitos_unregister_rng(dev);
2552
Kim Phillips4b9926282009-08-13 11:50:38 +10002553 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b7982472010-09-23 15:56:08 +08002554 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002555
Kim Phillips4b9926282009-08-13 11:50:38 +10002556 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002557
2558 if (priv->irq != NO_IRQ) {
2559 free_irq(priv->irq, dev);
2560 irq_dispose_mapping(priv->irq);
2561 }
2562
2563 tasklet_kill(&priv->done_task);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002564
2565 iounmap(priv->reg);
2566
2567 dev_set_drvdata(dev, NULL);
2568
2569 kfree(priv);
2570
2571 return 0;
2572}
2573
2574static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2575 struct talitos_alg_template
2576 *template)
2577{
Kim Phillips60f208d2010-05-19 19:21:53 +10002578 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002579 struct talitos_crypto_alg *t_alg;
2580 struct crypto_alg *alg;
2581
2582 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2583 if (!t_alg)
2584 return ERR_PTR(-ENOMEM);
2585
Lee Nipperacbf7c622010-05-19 19:19:33 +10002586 t_alg->algt = *template;
2587
2588 switch (t_alg->algt.type) {
2589 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002590 alg = &t_alg->algt.alg.crypto;
2591 alg->cra_init = talitos_cra_init;
2592 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002593 case CRYPTO_ALG_TYPE_AEAD:
2594 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002595 alg->cra_init = talitos_cra_init_aead;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002596 break;
2597 case CRYPTO_ALG_TYPE_AHASH:
2598 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002599 alg->cra_init = talitos_cra_init_ahash;
Lee Nipper79b3a412011-11-21 16:13:25 +08002600 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
2601 !strncmp(alg->cra_name, "hmac", 4))
2602 return ERR_PTR(-ENOTSUPP);
Kim Phillips60f208d2010-05-19 19:21:53 +10002603 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002604 (!strcmp(alg->cra_name, "sha224") ||
2605 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002606 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2607 t_alg->algt.desc_hdr_template =
2608 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2609 DESC_HDR_SEL0_MDEUA |
2610 DESC_HDR_MODE0_MDEU_SHA256;
2611 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002612 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002613 default:
2614 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2615 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002616 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002617
Kim Phillips9c4a7962008-06-23 19:50:15 +08002618 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002619 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002620 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002621 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002622
Kim Phillips9c4a7962008-06-23 19:50:15 +08002623 t_alg->dev = dev;
2624
2625 return t_alg;
2626}
2627
Grant Likely1c48a5c2011-02-17 02:43:24 -07002628static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002629{
2630 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002631 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002632 struct talitos_private *priv;
2633 const unsigned int *prop;
2634 int i, err;
2635
2636 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2637 if (!priv)
2638 return -ENOMEM;
2639
2640 dev_set_drvdata(dev, priv);
2641
2642 priv->ofdev = ofdev;
2643
2644 tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002645
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002646 INIT_LIST_HEAD(&priv->alg_list);
2647
Kim Phillips9c4a7962008-06-23 19:50:15 +08002648 priv->irq = irq_of_parse_and_map(np, 0);
2649
2650 if (priv->irq == NO_IRQ) {
2651 dev_err(dev, "failed to map irq\n");
2652 err = -EINVAL;
2653 goto err_out;
2654 }
2655
2656 /* get the irq line */
2657 err = request_irq(priv->irq, talitos_interrupt, 0,
2658 dev_driver_string(dev), dev);
2659 if (err) {
2660 dev_err(dev, "failed to request irq %d\n", priv->irq);
2661 irq_dispose_mapping(priv->irq);
2662 priv->irq = NO_IRQ;
2663 goto err_out;
2664 }
2665
2666 priv->reg = of_iomap(np, 0);
2667 if (!priv->reg) {
2668 dev_err(dev, "failed to of_iomap\n");
2669 err = -ENOMEM;
2670 goto err_out;
2671 }
2672
2673 /* get SEC version capabilities from device tree */
2674 prop = of_get_property(np, "fsl,num-channels", NULL);
2675 if (prop)
2676 priv->num_channels = *prop;
2677
2678 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2679 if (prop)
2680 priv->chfifo_len = *prop;
2681
2682 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2683 if (prop)
2684 priv->exec_units = *prop;
2685
2686 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2687 if (prop)
2688 priv->desc_types = *prop;
2689
2690 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2691 !priv->exec_units || !priv->desc_types) {
2692 dev_err(dev, "invalid property data in device tree node\n");
2693 err = -EINVAL;
2694 goto err_out;
2695 }
2696
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002697 if (of_device_is_compatible(np, "fsl,sec3.0"))
2698 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2699
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002700 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002701 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002702 TALITOS_FTR_SHA224_HWINIT |
2703 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002704
Kim Phillips4b9926282009-08-13 11:50:38 +10002705 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2706 priv->num_channels, GFP_KERNEL);
2707 if (!priv->chan) {
2708 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002709 err = -ENOMEM;
2710 goto err_out;
2711 }
2712
2713 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002714 spin_lock_init(&priv->chan[i].head_lock);
2715 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002716 }
2717
2718 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2719
2720 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002721 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2722 priv->fifo_len, GFP_KERNEL);
2723 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002724 dev_err(dev, "failed to allocate request fifo %d\n", i);
2725 err = -ENOMEM;
2726 goto err_out;
2727 }
2728 }
2729
Kim Phillipsec6644d2008-07-17 20:16:40 +08002730 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002731 atomic_set(&priv->chan[i].submit_count,
2732 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002733
Kim Phillips81eb0242009-08-13 11:51:51 +10002734 dma_set_mask(dev, DMA_BIT_MASK(36));
2735
Kim Phillips9c4a7962008-06-23 19:50:15 +08002736 /* reset and initialize the h/w */
2737 err = init_device(dev);
2738 if (err) {
2739 dev_err(dev, "failed to initialize device\n");
2740 goto err_out;
2741 }
2742
2743 /* register the RNG, if available */
2744 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2745 err = talitos_register_rng(dev);
2746 if (err) {
2747 dev_err(dev, "failed to register hwrng: %d\n", err);
2748 goto err_out;
2749 } else
2750 dev_info(dev, "hwrng\n");
2751 }
2752
2753 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002754 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2755 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2756 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002757 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002758
2759 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2760 if (IS_ERR(t_alg)) {
2761 err = PTR_ERR(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002762 if (err == -ENOTSUPP) {
2763 kfree(t_alg);
2764 continue;
2765 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002766 goto err_out;
2767 }
2768
Lee Nipperacbf7c622010-05-19 19:19:33 +10002769 switch (t_alg->algt.type) {
2770 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2771 case CRYPTO_ALG_TYPE_AEAD:
2772 err = crypto_register_alg(
2773 &t_alg->algt.alg.crypto);
2774 name = t_alg->algt.alg.crypto.cra_driver_name;
2775 break;
2776 case CRYPTO_ALG_TYPE_AHASH:
2777 err = crypto_register_ahash(
2778 &t_alg->algt.alg.hash);
2779 name =
2780 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2781 break;
2782 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002783 if (err) {
2784 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002785 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002786 kfree(t_alg);
2787 } else {
2788 list_add_tail(&t_alg->entry, &priv->alg_list);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002789 dev_info(dev, "%s\n", name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002790 }
2791 }
2792 }
2793
2794 return 0;
2795
2796err_out:
2797 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002798
2799 return err;
2800}
2801
Márton Németh6c3f9752010-01-17 21:54:01 +11002802static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002803 {
2804 .compatible = "fsl,sec2.0",
2805 },
2806 {},
2807};
2808MODULE_DEVICE_TABLE(of, talitos_match);
2809
Grant Likely1c48a5c2011-02-17 02:43:24 -07002810static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002811 .driver = {
2812 .name = "talitos",
2813 .owner = THIS_MODULE,
2814 .of_match_table = talitos_match,
2815 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002816 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002817 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002818};
2819
2820static int __init talitos_init(void)
2821{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002822 return platform_driver_register(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002823}
2824module_init(talitos_init);
2825
2826static void __exit talitos_exit(void)
2827{
Grant Likely1c48a5c2011-02-17 02:43:24 -07002828 platform_driver_unregister(&talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002829}
2830module_exit(talitos_exit);
2831
2832MODULE_LICENSE("GPL");
2833MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2834MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");