blob: 2d8c7890168664778be01834c8911bb2f52c97bc [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 {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800102 void __iomem *reg;
103
Kim Phillips4b9926282009-08-13 11:50:38 +1000104 /* request fifo */
105 struct talitos_request *fifo;
106
107 /* number of requests pending in channel h/w fifo */
108 atomic_t submit_count ____cacheline_aligned;
109
110 /* request submission (head) lock */
111 spinlock_t head_lock ____cacheline_aligned;
112 /* index to next free descriptor request */
113 int head;
114
115 /* request release (tail) lock */
116 spinlock_t tail_lock ____cacheline_aligned;
117 /* index to next in-progress/done descriptor request */
118 int tail;
119};
120
Kim Phillips9c4a7962008-06-23 19:50:15 +0800121struct talitos_private {
122 struct device *dev;
Grant Likely2dc11582010-08-06 09:25:50 -0600123 struct platform_device *ofdev;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800124 void __iomem *reg;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800125 int irq[2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800126
127 /* SEC version geometry (from device tree node) */
128 unsigned int num_channels;
129 unsigned int chfifo_len;
130 unsigned int exec_units;
131 unsigned int desc_types;
132
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800133 /* SEC Compatibility info */
134 unsigned long features;
135
Kim Phillips9c4a7962008-06-23 19:50:15 +0800136 /*
137 * length of the request fifo
138 * fifo_len is chfifo_len rounded up to next power of 2
139 * so we can use bitwise ops to wrap
140 */
141 unsigned int fifo_len;
142
Kim Phillips4b9926282009-08-13 11:50:38 +1000143 struct talitos_channel *chan;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800144
Kim Phillips4b9926282009-08-13 11:50:38 +1000145 /* next channel to be assigned next incoming descriptor */
146 atomic_t last_chan ____cacheline_aligned;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800147
148 /* request callback tasklet */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800149 struct tasklet_struct done_task[2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800150
151 /* list of registered algorithms */
152 struct list_head alg_list;
153
154 /* hwrng device */
155 struct hwrng rng;
156};
157
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800158/* .features flag */
159#define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800160#define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
Kim Phillips60f208d2010-05-19 19:21:53 +1000161#define TALITOS_FTR_SHA224_HWINIT 0x00000004
Lee Nipper79b3a412011-11-21 16:13:25 +0800162#define TALITOS_FTR_HMAC_OK 0x00000008
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800163
Kim Phillips81eb0242009-08-13 11:51:51 +1000164static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
165{
166 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +0800167 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +1000168}
169
Kim Phillips9c4a7962008-06-23 19:50:15 +0800170/*
171 * map virtual single (contiguous) pointer to h/w descriptor pointer
172 */
173static void map_single_talitos_ptr(struct device *dev,
174 struct talitos_ptr *talitos_ptr,
175 unsigned short len, void *data,
176 unsigned char extent,
177 enum dma_data_direction dir)
178{
Kim Phillips81eb0242009-08-13 11:51:51 +1000179 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
180
Kim Phillips9c4a7962008-06-23 19:50:15 +0800181 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +1000182 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800183 talitos_ptr->j_extent = extent;
184}
185
186/*
187 * unmap bus single (contiguous) h/w descriptor pointer
188 */
189static void unmap_single_talitos_ptr(struct device *dev,
190 struct talitos_ptr *talitos_ptr,
191 enum dma_data_direction dir)
192{
193 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
194 be16_to_cpu(talitos_ptr->len), dir);
195}
196
197static int reset_channel(struct device *dev, int ch)
198{
199 struct talitos_private *priv = dev_get_drvdata(dev);
200 unsigned int timeout = TALITOS_TIMEOUT;
201
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800202 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800203
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800204 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800205 && --timeout)
206 cpu_relax();
207
208 if (timeout == 0) {
209 dev_err(dev, "failed to reset channel %d\n", ch);
210 return -EIO;
211 }
212
Kim Phillips81eb0242009-08-13 11:51:51 +1000213 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800214 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000215 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800216
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800217 /* and ICCR writeback, if available */
218 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800219 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800220 TALITOS_CCCR_LO_IWSE);
221
Kim Phillips9c4a7962008-06-23 19:50:15 +0800222 return 0;
223}
224
225static int reset_device(struct device *dev)
226{
227 struct talitos_private *priv = dev_get_drvdata(dev);
228 unsigned int timeout = TALITOS_TIMEOUT;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800229 u32 mcr = TALITOS_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800230
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800231 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800232
233 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
234 && --timeout)
235 cpu_relax();
236
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600237 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800238 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
239 setbits32(priv->reg + TALITOS_MCR, mcr);
240 }
241
Kim Phillips9c4a7962008-06-23 19:50:15 +0800242 if (timeout == 0) {
243 dev_err(dev, "failed to reset device\n");
244 return -EIO;
245 }
246
247 return 0;
248}
249
250/*
251 * Reset and initialize the device
252 */
253static int init_device(struct device *dev)
254{
255 struct talitos_private *priv = dev_get_drvdata(dev);
256 int ch, err;
257
258 /*
259 * Master reset
260 * errata documentation: warning: certain SEC interrupts
261 * are not fully cleared by writing the MCR:SWR bit,
262 * set bit twice to completely reset
263 */
264 err = reset_device(dev);
265 if (err)
266 return err;
267
268 err = reset_device(dev);
269 if (err)
270 return err;
271
272 /* reset channels */
273 for (ch = 0; ch < priv->num_channels; ch++) {
274 err = reset_channel(dev, ch);
275 if (err)
276 return err;
277 }
278
279 /* enable channel done and error interrupts */
280 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
281 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
282
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800283 /* disable integrity check error interrupts (use writeback instead) */
284 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
285 setbits32(priv->reg + TALITOS_MDEUICR_LO,
286 TALITOS_MDEUICR_LO_ICE);
287
Kim Phillips9c4a7962008-06-23 19:50:15 +0800288 return 0;
289}
290
291/**
292 * talitos_submit - submits a descriptor to the device for processing
293 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800294 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800295 * @desc: the descriptor to be processed by the device
296 * @callback: whom to call when processing is complete
297 * @context: a handle for use by caller (optional)
298 *
299 * desc must contain valid dma-mapped (bus physical) address pointers.
300 * callback must check err and feedback in descriptor header
301 * for device processing status.
302 */
Kim Phillips5228f0f2011-07-15 11:21:38 +0800303static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800304 void (*callback)(struct device *dev,
305 struct talitos_desc *desc,
306 void *context, int error),
307 void *context)
308{
309 struct talitos_private *priv = dev_get_drvdata(dev);
310 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800311 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800312 int head;
313
Kim Phillips4b9926282009-08-13 11:50:38 +1000314 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800315
Kim Phillips4b9926282009-08-13 11:50:38 +1000316 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800317 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000318 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800319 return -EAGAIN;
320 }
321
Kim Phillips4b9926282009-08-13 11:50:38 +1000322 head = priv->chan[ch].head;
323 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800324
Kim Phillips9c4a7962008-06-23 19:50:15 +0800325 /* map descriptor and save caller data */
326 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
327 DMA_BIDIRECTIONAL);
328 request->callback = callback;
329 request->context = context;
330
331 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000332 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800333
334 smp_wmb();
335 request->desc = desc;
336
337 /* GO! */
338 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800339 out_be32(priv->chan[ch].reg + TALITOS_FF,
340 upper_32_bits(request->dma_desc));
341 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800342 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800343
Kim Phillips4b9926282009-08-13 11:50:38 +1000344 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800345
346 return -EINPROGRESS;
347}
348
349/*
350 * process what was done, notify callback of error if not
351 */
352static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
353{
354 struct talitos_private *priv = dev_get_drvdata(dev);
355 struct talitos_request *request, saved_req;
356 unsigned long flags;
357 int tail, status;
358
Kim Phillips4b9926282009-08-13 11:50:38 +1000359 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800360
Kim Phillips4b9926282009-08-13 11:50:38 +1000361 tail = priv->chan[ch].tail;
362 while (priv->chan[ch].fifo[tail].desc) {
363 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800364
365 /* descriptors with their done bits set don't get the error */
366 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100367 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800368 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100369 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800370 if (!error)
371 break;
372 else
373 status = error;
374
375 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800376 sizeof(struct talitos_desc),
377 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800378
379 /* copy entries so we can call callback outside lock */
380 saved_req.desc = request->desc;
381 saved_req.callback = request->callback;
382 saved_req.context = request->context;
383
384 /* release request entry in fifo */
385 smp_wmb();
386 request->desc = NULL;
387
388 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000389 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800390
Kim Phillips4b9926282009-08-13 11:50:38 +1000391 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800392
Kim Phillips4b9926282009-08-13 11:50:38 +1000393 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800394
Kim Phillips9c4a7962008-06-23 19:50:15 +0800395 saved_req.callback(dev, saved_req.desc, saved_req.context,
396 status);
397 /* channel may resume processing in single desc error case */
398 if (error && !reset_ch && status == error)
399 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000400 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
401 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800402 }
403
Kim Phillips4b9926282009-08-13 11:50:38 +1000404 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800405}
406
407/*
408 * process completed requests for channels that have done status
409 */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800410#define DEF_TALITOS_DONE(name, ch_done_mask) \
411static void talitos_done_##name(unsigned long data) \
412{ \
413 struct device *dev = (struct device *)data; \
414 struct talitos_private *priv = dev_get_drvdata(dev); \
415 \
416 if (ch_done_mask & 1) \
417 flush_channel(dev, 0, 0, 0); \
418 if (priv->num_channels == 1) \
419 goto out; \
420 if (ch_done_mask & (1 << 2)) \
421 flush_channel(dev, 1, 0, 0); \
422 if (ch_done_mask & (1 << 4)) \
423 flush_channel(dev, 2, 0, 0); \
424 if (ch_done_mask & (1 << 6)) \
425 flush_channel(dev, 3, 0, 0); \
426 \
427out: \
428 /* At this point, all completed channels have been processed */ \
429 /* Unmask done interrupts for channels completed later on. */ \
430 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
431 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800432}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800433DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
434DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
435DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436
437/*
438 * locate current (offending) descriptor
439 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200440static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800441{
442 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000443 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800444 dma_addr_t cur_desc;
445
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800446 cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800447
Kim Phillips4b9926282009-08-13 11:50:38 +1000448 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800449 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000450 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800451 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200452 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800453 }
454 }
455
Kim Phillips3e721ae2011-10-21 15:20:28 +0200456 return priv->chan[ch].fifo[tail].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800457}
458
459/*
460 * user diagnostics; report root cause of error based on execution unit status
461 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200462static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800463{
464 struct talitos_private *priv = dev_get_drvdata(dev);
465 int i;
466
Kim Phillips3e721ae2011-10-21 15:20:28 +0200467 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800468 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200469
470 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800471 case DESC_HDR_SEL0_AFEU:
472 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
473 in_be32(priv->reg + TALITOS_AFEUISR),
474 in_be32(priv->reg + TALITOS_AFEUISR_LO));
475 break;
476 case DESC_HDR_SEL0_DEU:
477 dev_err(dev, "DEUISR 0x%08x_%08x\n",
478 in_be32(priv->reg + TALITOS_DEUISR),
479 in_be32(priv->reg + TALITOS_DEUISR_LO));
480 break;
481 case DESC_HDR_SEL0_MDEUA:
482 case DESC_HDR_SEL0_MDEUB:
483 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
484 in_be32(priv->reg + TALITOS_MDEUISR),
485 in_be32(priv->reg + TALITOS_MDEUISR_LO));
486 break;
487 case DESC_HDR_SEL0_RNG:
488 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
489 in_be32(priv->reg + TALITOS_RNGUISR),
490 in_be32(priv->reg + TALITOS_RNGUISR_LO));
491 break;
492 case DESC_HDR_SEL0_PKEU:
493 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
494 in_be32(priv->reg + TALITOS_PKEUISR),
495 in_be32(priv->reg + TALITOS_PKEUISR_LO));
496 break;
497 case DESC_HDR_SEL0_AESU:
498 dev_err(dev, "AESUISR 0x%08x_%08x\n",
499 in_be32(priv->reg + TALITOS_AESUISR),
500 in_be32(priv->reg + TALITOS_AESUISR_LO));
501 break;
502 case DESC_HDR_SEL0_CRCU:
503 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
504 in_be32(priv->reg + TALITOS_CRCUISR),
505 in_be32(priv->reg + TALITOS_CRCUISR_LO));
506 break;
507 case DESC_HDR_SEL0_KEU:
508 dev_err(dev, "KEUISR 0x%08x_%08x\n",
509 in_be32(priv->reg + TALITOS_KEUISR),
510 in_be32(priv->reg + TALITOS_KEUISR_LO));
511 break;
512 }
513
Kim Phillips3e721ae2011-10-21 15:20:28 +0200514 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800515 case DESC_HDR_SEL1_MDEUA:
516 case DESC_HDR_SEL1_MDEUB:
517 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
518 in_be32(priv->reg + TALITOS_MDEUISR),
519 in_be32(priv->reg + TALITOS_MDEUISR_LO));
520 break;
521 case DESC_HDR_SEL1_CRCU:
522 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
523 in_be32(priv->reg + TALITOS_CRCUISR),
524 in_be32(priv->reg + TALITOS_CRCUISR_LO));
525 break;
526 }
527
528 for (i = 0; i < 8; i++)
529 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800530 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
531 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800532}
533
534/*
535 * recover from error interrupts
536 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600537static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800538{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800539 struct talitos_private *priv = dev_get_drvdata(dev);
540 unsigned int timeout = TALITOS_TIMEOUT;
541 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800542 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800543
544 for (ch = 0; ch < priv->num_channels; ch++) {
545 /* skip channels without errors */
546 if (!(isr & (1 << (ch * 2 + 1))))
547 continue;
548
549 error = -EINVAL;
550
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800551 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
552 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800553
554 if (v_lo & TALITOS_CCPSR_LO_DOF) {
555 dev_err(dev, "double fetch fifo overflow error\n");
556 error = -EAGAIN;
557 reset_ch = 1;
558 }
559 if (v_lo & TALITOS_CCPSR_LO_SOF) {
560 /* h/w dropped descriptor */
561 dev_err(dev, "single fetch fifo overflow error\n");
562 error = -EAGAIN;
563 }
564 if (v_lo & TALITOS_CCPSR_LO_MDTE)
565 dev_err(dev, "master data transfer error\n");
566 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
567 dev_err(dev, "s/g data length zero error\n");
568 if (v_lo & TALITOS_CCPSR_LO_FPZ)
569 dev_err(dev, "fetch pointer zero error\n");
570 if (v_lo & TALITOS_CCPSR_LO_IDH)
571 dev_err(dev, "illegal descriptor header error\n");
572 if (v_lo & TALITOS_CCPSR_LO_IEU)
573 dev_err(dev, "invalid execution unit error\n");
574 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200575 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800576 if (v_lo & TALITOS_CCPSR_LO_GB)
577 dev_err(dev, "gather boundary error\n");
578 if (v_lo & TALITOS_CCPSR_LO_GRL)
579 dev_err(dev, "gather return/length error\n");
580 if (v_lo & TALITOS_CCPSR_LO_SB)
581 dev_err(dev, "scatter boundary error\n");
582 if (v_lo & TALITOS_CCPSR_LO_SRL)
583 dev_err(dev, "scatter return/length error\n");
584
585 flush_channel(dev, ch, error, reset_ch);
586
587 if (reset_ch) {
588 reset_channel(dev, ch);
589 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800590 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800591 TALITOS_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800592 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
593 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800594 TALITOS_CCCR_CONT) && --timeout)
595 cpu_relax();
596 if (timeout == 0) {
597 dev_err(dev, "failed to restart channel %d\n",
598 ch);
599 reset_dev = 1;
600 }
601 }
602 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800603 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800604 dev_err(dev, "done overflow, internal time out, or rngu error: "
605 "ISR 0x%08x_%08x\n", isr, isr_lo);
606
607 /* purge request queues */
608 for (ch = 0; ch < priv->num_channels; ch++)
609 flush_channel(dev, ch, -EIO, 1);
610
611 /* reset and reinitialize the device */
612 init_device(dev);
613 }
614}
615
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800616#define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
617static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
618{ \
619 struct device *dev = data; \
620 struct talitos_private *priv = dev_get_drvdata(dev); \
621 u32 isr, isr_lo; \
622 \
623 isr = in_be32(priv->reg + TALITOS_ISR); \
624 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
625 /* Acknowledge interrupt */ \
626 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
627 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
628 \
629 if (unlikely((isr & ~TALITOS_ISR_4CHDONE) & ch_err_mask || isr_lo)) \
Kim Phillips5e718a02011-12-12 14:59:12 -0600630 talitos_error(dev, isr, isr_lo); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800631 else \
632 if (likely(isr & ch_done_mask)) { \
633 /* mask further done interrupts. */ \
634 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
635 /* done_task will unmask done interrupts at exit */ \
636 tasklet_schedule(&priv->done_task[tlet]); \
637 } \
638 \
639 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
640 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800641}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800642DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
643DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
644DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800645
646/*
647 * hwrng
648 */
649static int talitos_rng_data_present(struct hwrng *rng, int wait)
650{
651 struct device *dev = (struct device *)rng->priv;
652 struct talitos_private *priv = dev_get_drvdata(dev);
653 u32 ofl;
654 int i;
655
656 for (i = 0; i < 20; i++) {
657 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
658 TALITOS_RNGUSR_LO_OFL;
659 if (ofl || !wait)
660 break;
661 udelay(10);
662 }
663
664 return !!ofl;
665}
666
667static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
668{
669 struct device *dev = (struct device *)rng->priv;
670 struct talitos_private *priv = dev_get_drvdata(dev);
671
672 /* rng fifo requires 64-bit accesses */
673 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
674 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
675
676 return sizeof(u32);
677}
678
679static int talitos_rng_init(struct hwrng *rng)
680{
681 struct device *dev = (struct device *)rng->priv;
682 struct talitos_private *priv = dev_get_drvdata(dev);
683 unsigned int timeout = TALITOS_TIMEOUT;
684
685 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
686 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
687 && --timeout)
688 cpu_relax();
689 if (timeout == 0) {
690 dev_err(dev, "failed to reset rng hw\n");
691 return -ENODEV;
692 }
693
694 /* start generating */
695 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
696
697 return 0;
698}
699
700static int talitos_register_rng(struct device *dev)
701{
702 struct talitos_private *priv = dev_get_drvdata(dev);
703
704 priv->rng.name = dev_driver_string(dev),
705 priv->rng.init = talitos_rng_init,
706 priv->rng.data_present = talitos_rng_data_present,
707 priv->rng.data_read = talitos_rng_data_read,
708 priv->rng.priv = (unsigned long)dev;
709
710 return hwrng_register(&priv->rng);
711}
712
713static void talitos_unregister_rng(struct device *dev)
714{
715 struct talitos_private *priv = dev_get_drvdata(dev);
716
717 hwrng_unregister(&priv->rng);
718}
719
720/*
721 * crypto alg
722 */
723#define TALITOS_CRA_PRIORITY 3000
724#define TALITOS_MAX_KEY_SIZE 64
Lee Nipper3952f172008-07-10 18:29:18 +0800725#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800726
Lee Nipper497f2e62010-05-19 19:20:36 +1000727#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800728
729struct talitos_ctx {
730 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800731 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800732 __be32 desc_hdr_template;
733 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800734 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800735 unsigned int keylen;
736 unsigned int enckeylen;
737 unsigned int authkeylen;
738 unsigned int authsize;
739};
740
Lee Nipper497f2e62010-05-19 19:20:36 +1000741#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
742#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
743
744struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000745 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000746 unsigned int hw_context_size;
747 u8 buf[HASH_MAX_BLOCK_SIZE];
748 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000749 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000750 unsigned int first;
751 unsigned int last;
752 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000753 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000754 struct scatterlist bufsl[2];
755 struct scatterlist *psrc;
756};
757
Lee Nipper56af8cd2009-03-29 15:50:50 +0800758static int aead_setauthsize(struct crypto_aead *authenc,
759 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800760{
761 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
762
763 ctx->authsize = authsize;
764
765 return 0;
766}
767
Lee Nipper56af8cd2009-03-29 15:50:50 +0800768static int aead_setkey(struct crypto_aead *authenc,
769 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800770{
771 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
772 struct rtattr *rta = (void *)key;
773 struct crypto_authenc_key_param *param;
774 unsigned int authkeylen;
775 unsigned int enckeylen;
776
777 if (!RTA_OK(rta, keylen))
778 goto badkey;
779
780 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
781 goto badkey;
782
783 if (RTA_PAYLOAD(rta) < sizeof(*param))
784 goto badkey;
785
786 param = RTA_DATA(rta);
787 enckeylen = be32_to_cpu(param->enckeylen);
788
789 key += RTA_ALIGN(rta->rta_len);
790 keylen -= RTA_ALIGN(rta->rta_len);
791
792 if (keylen < enckeylen)
793 goto badkey;
794
795 authkeylen = keylen - enckeylen;
796
797 if (keylen > TALITOS_MAX_KEY_SIZE)
798 goto badkey;
799
800 memcpy(&ctx->key, key, keylen);
801
802 ctx->keylen = keylen;
803 ctx->enckeylen = enckeylen;
804 ctx->authkeylen = authkeylen;
805
806 return 0;
807
808badkey:
809 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
810 return -EINVAL;
811}
812
813/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800814 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800815 * @src_nents: number of segments in input scatterlist
816 * @dst_nents: number of segments in output scatterlist
817 * @dma_len: length of dma mapped link_tbl space
818 * @dma_link_tbl: bus physical address of link_tbl
819 * @desc: h/w descriptor
820 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
821 *
822 * if decrypting (with authcheck), or either one of src_nents or dst_nents
823 * is greater than 1, an integrity check value is concatenated to the end
824 * of link_tbl data
825 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800826struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800827 int src_nents;
828 int dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800829 int src_is_chained;
830 int dst_is_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800831 int dma_len;
832 dma_addr_t dma_link_tbl;
833 struct talitos_desc desc;
834 struct talitos_ptr link_tbl[0];
835};
836
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800837static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
838 unsigned int nents, enum dma_data_direction dir,
839 int chained)
840{
841 if (unlikely(chained))
842 while (sg) {
843 dma_map_sg(dev, sg, 1, dir);
844 sg = scatterwalk_sg_next(sg);
845 }
846 else
847 dma_map_sg(dev, sg, nents, dir);
848 return nents;
849}
850
851static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
852 enum dma_data_direction dir)
853{
854 while (sg) {
855 dma_unmap_sg(dev, sg, 1, dir);
856 sg = scatterwalk_sg_next(sg);
857 }
858}
859
860static void talitos_sg_unmap(struct device *dev,
861 struct talitos_edesc *edesc,
862 struct scatterlist *src,
863 struct scatterlist *dst)
864{
865 unsigned int src_nents = edesc->src_nents ? : 1;
866 unsigned int dst_nents = edesc->dst_nents ? : 1;
867
868 if (src != dst) {
869 if (edesc->src_is_chained)
870 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
871 else
872 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
873
Lee Nipper497f2e62010-05-19 19:20:36 +1000874 if (dst) {
875 if (edesc->dst_is_chained)
876 talitos_unmap_sg_chain(dev, dst,
877 DMA_FROM_DEVICE);
878 else
879 dma_unmap_sg(dev, dst, dst_nents,
880 DMA_FROM_DEVICE);
881 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800882 } else
883 if (edesc->src_is_chained)
884 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
885 else
886 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
887}
888
Kim Phillips9c4a7962008-06-23 19:50:15 +0800889static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800890 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800891 struct aead_request *areq)
892{
893 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
894 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
895 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
896 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
897
898 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
899
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800900 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800901
902 if (edesc->dma_len)
903 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
904 DMA_BIDIRECTIONAL);
905}
906
907/*
908 * ipsec_esp descriptor callbacks
909 */
910static void ipsec_esp_encrypt_done(struct device *dev,
911 struct talitos_desc *desc, void *context,
912 int err)
913{
914 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800915 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
916 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800917 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800918 struct scatterlist *sg;
919 void *icvdata;
920
Kim Phillips19bbbc62009-03-29 15:53:59 +0800921 edesc = container_of(desc, struct talitos_edesc, desc);
922
Kim Phillips9c4a7962008-06-23 19:50:15 +0800923 ipsec_esp_unmap(dev, edesc, areq);
924
925 /* copy the generated ICV to dst */
926 if (edesc->dma_len) {
927 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800928 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800929 sg = sg_last(areq->dst, edesc->dst_nents);
930 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
931 icvdata, ctx->authsize);
932 }
933
934 kfree(edesc);
935
936 aead_request_complete(areq, err);
937}
938
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800939static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800940 struct talitos_desc *desc,
941 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800942{
943 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800944 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
945 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800946 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800947 struct scatterlist *sg;
948 void *icvdata;
949
Kim Phillips19bbbc62009-03-29 15:53:59 +0800950 edesc = container_of(desc, struct talitos_edesc, desc);
951
Kim Phillips9c4a7962008-06-23 19:50:15 +0800952 ipsec_esp_unmap(dev, edesc, req);
953
954 if (!err) {
955 /* auth check */
956 if (edesc->dma_len)
957 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800958 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800959 else
960 icvdata = &edesc->link_tbl[0];
961
962 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
963 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
964 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
965 }
966
967 kfree(edesc);
968
969 aead_request_complete(req, err);
970}
971
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800972static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800973 struct talitos_desc *desc,
974 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800975{
976 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800977 struct talitos_edesc *edesc;
978
979 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800980
981 ipsec_esp_unmap(dev, edesc, req);
982
983 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800984 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
985 DESC_HDR_LO_ICCR1_PASS))
986 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800987
988 kfree(edesc);
989
990 aead_request_complete(req, err);
991}
992
Kim Phillips9c4a7962008-06-23 19:50:15 +0800993/*
994 * convert scatterlist to SEC h/w link table format
995 * stop at cryptlen bytes
996 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800997static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800998 int cryptlen, struct talitos_ptr *link_tbl_ptr)
999{
Lee Nipper70bcaca2008-07-03 19:08:46 +08001000 int n_sg = sg_count;
1001
1002 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001003 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001004 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
1005 link_tbl_ptr->j_extent = 0;
1006 link_tbl_ptr++;
1007 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001008 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001009 }
1010
Lee Nipper70bcaca2008-07-03 19:08:46 +08001011 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001012 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +08001013 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +08001014 /* Empty this entry, and move to previous one */
1015 cryptlen += be16_to_cpu(link_tbl_ptr->len);
1016 link_tbl_ptr->len = 0;
1017 sg_count--;
1018 link_tbl_ptr--;
1019 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001020 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
1021 + cryptlen);
1022
1023 /* tag end of link table */
1024 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +08001025
1026 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001027}
1028
1029/*
1030 * fill in and submit ipsec_esp descriptor
1031 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001032static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001033 u8 *giv, u64 seq,
1034 void (*callback) (struct device *dev,
1035 struct talitos_desc *desc,
1036 void *context, int error))
1037{
1038 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1039 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1040 struct device *dev = ctx->dev;
1041 struct talitos_desc *desc = &edesc->desc;
1042 unsigned int cryptlen = areq->cryptlen;
1043 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +10001044 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001045 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001046 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001047
1048 /* hmac key */
1049 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
1050 0, DMA_TO_DEVICE);
1051 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +10001052 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1053 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001054 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001055 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
1056 DMA_TO_DEVICE);
1057
1058 /* cipher key */
1059 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1060 (char *)&ctx->key + ctx->authkeylen, 0,
1061 DMA_TO_DEVICE);
1062
1063 /*
1064 * cipher in
1065 * map and adjust cipher len to aead request cryptlen.
1066 * extent is bytes of HMAC postpended to ciphertext,
1067 * typically 12 for ipsec
1068 */
1069 desc->ptr[4].len = cpu_to_be16(cryptlen);
1070 desc->ptr[4].j_extent = authsize;
1071
Kim Phillipse938e462009-03-29 15:53:23 +08001072 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1073 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1074 : DMA_TO_DEVICE,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001075 edesc->src_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001076
1077 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001078 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001079 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001080 sg_link_tbl_len = cryptlen;
1081
Kim Phillips962a9c92009-03-29 15:54:30 +08001082 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001083 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001084
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001085 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001086 &edesc->link_tbl[0]);
1087 if (sg_count > 1) {
1088 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001089 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001090 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1091 edesc->dma_len,
1092 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001093 } else {
1094 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001095 to_talitos_ptr(&desc->ptr[4],
1096 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001097 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001098 }
1099
1100 /* cipher out */
1101 desc->ptr[5].len = cpu_to_be16(cryptlen);
1102 desc->ptr[5].j_extent = authsize;
1103
Kim Phillipse938e462009-03-29 15:53:23 +08001104 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001105 sg_count = talitos_map_sg(dev, areq->dst,
1106 edesc->dst_nents ? : 1,
1107 DMA_FROM_DEVICE,
1108 edesc->dst_is_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001109
1110 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001111 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001112 } else {
1113 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001114 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001115
Kim Phillips81eb0242009-08-13 11:51:51 +10001116 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1117 (edesc->src_nents + 1) *
1118 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001119 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1120 link_tbl_ptr);
1121
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001122 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001123 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001124 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001125 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001126 link_tbl_ptr++;
1127 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1128 link_tbl_ptr->len = cpu_to_be16(authsize);
1129
1130 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001131 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1132 (edesc->src_nents + edesc->dst_nents + 2) *
1133 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001134 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1135 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1136 edesc->dma_len, DMA_BIDIRECTIONAL);
1137 }
1138
1139 /* iv out */
1140 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1141 DMA_FROM_DEVICE);
1142
Kim Phillips5228f0f2011-07-15 11:21:38 +08001143 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001144 if (ret != -EINPROGRESS) {
1145 ipsec_esp_unmap(dev, edesc, areq);
1146 kfree(edesc);
1147 }
1148 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001149}
1150
Kim Phillips9c4a7962008-06-23 19:50:15 +08001151/*
1152 * derive number of elements in scatterlist
1153 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001154static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001155{
1156 struct scatterlist *sg = sg_list;
1157 int sg_nents = 0;
1158
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001159 *chained = 0;
1160 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001161 sg_nents++;
1162 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001163 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1164 *chained = 1;
1165 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001166 }
1167
1168 return sg_nents;
1169}
1170
Lee Nipper497f2e62010-05-19 19:20:36 +10001171/**
1172 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1173 * @sgl: The SG list
1174 * @nents: Number of SG entries
1175 * @buf: Where to copy to
1176 * @buflen: The number of bytes to copy
1177 * @skip: The number of bytes to skip before copying.
1178 * Note: skip + buflen should equal SG total size.
1179 *
1180 * Returns the number of copied bytes.
1181 *
1182 **/
1183static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1184 void *buf, size_t buflen, unsigned int skip)
1185{
1186 unsigned int offset = 0;
1187 unsigned int boffset = 0;
1188 struct sg_mapping_iter miter;
1189 unsigned long flags;
1190 unsigned int sg_flags = SG_MITER_ATOMIC;
1191 size_t total_buffer = buflen + skip;
1192
1193 sg_flags |= SG_MITER_FROM_SG;
1194
1195 sg_miter_start(&miter, sgl, nents, sg_flags);
1196
1197 local_irq_save(flags);
1198
1199 while (sg_miter_next(&miter) && offset < total_buffer) {
1200 unsigned int len;
1201 unsigned int ignore;
1202
1203 if ((offset + miter.length) > skip) {
1204 if (offset < skip) {
1205 /* Copy part of this segment */
1206 ignore = skip - offset;
1207 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001208 if (boffset + len > buflen)
1209 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001210 memcpy(buf + boffset, miter.addr + ignore, len);
1211 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001212 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001213 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001214 if (boffset + len > buflen)
1215 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001216 memcpy(buf + boffset, miter.addr, len);
1217 }
1218 boffset += len;
1219 }
1220 offset += miter.length;
1221 }
1222
1223 sg_miter_stop(&miter);
1224
1225 local_irq_restore(flags);
1226 return boffset;
1227}
1228
Kim Phillips9c4a7962008-06-23 19:50:15 +08001229/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001230 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001231 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001232static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1233 struct scatterlist *src,
1234 struct scatterlist *dst,
Lee Nipper497f2e62010-05-19 19:20:36 +10001235 int hash_result,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001236 unsigned int cryptlen,
1237 unsigned int authsize,
1238 int icv_stashing,
1239 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001240{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001241 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001242 int src_nents, dst_nents, alloc_len, dma_len;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001243 int src_chained, dst_chained = 0;
1244 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001245 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001246
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001247 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1248 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249 return ERR_PTR(-EINVAL);
1250 }
1251
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001252 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001253 src_nents = (src_nents == 1) ? 0 : src_nents;
1254
Lee Nipper497f2e62010-05-19 19:20:36 +10001255 if (hash_result) {
1256 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001257 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001258 if (dst == src) {
1259 dst_nents = src_nents;
1260 } else {
1261 dst_nents = sg_count(dst, cryptlen + authsize,
1262 &dst_chained);
1263 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1264 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001265 }
1266
1267 /*
1268 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001269 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001270 * and the ICV data itself
1271 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001272 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001273 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001274 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001275 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001276 alloc_len += dma_len;
1277 } else {
1278 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001279 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001280 }
1281
Kim Phillips586725f2008-07-17 20:19:18 +08001282 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001283 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001284 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001285 return ERR_PTR(-ENOMEM);
1286 }
1287
1288 edesc->src_nents = src_nents;
1289 edesc->dst_nents = dst_nents;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001290 edesc->src_is_chained = src_chained;
1291 edesc->dst_is_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001292 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001293 if (dma_len)
1294 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1295 edesc->dma_len,
1296 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001297
1298 return edesc;
1299}
1300
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001301static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1302 int icv_stashing)
1303{
1304 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1305 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1306
Lee Nipper497f2e62010-05-19 19:20:36 +10001307 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001308 areq->cryptlen, ctx->authsize, icv_stashing,
1309 areq->base.flags);
1310}
1311
Lee Nipper56af8cd2009-03-29 15:50:50 +08001312static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001313{
1314 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1315 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001316 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001317
1318 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001319 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001320 if (IS_ERR(edesc))
1321 return PTR_ERR(edesc);
1322
1323 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001324 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001325
1326 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1327}
1328
Lee Nipper56af8cd2009-03-29 15:50:50 +08001329static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001330{
1331 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1332 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1333 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001334 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001335 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001336 struct scatterlist *sg;
1337 void *icvdata;
1338
1339 req->cryptlen -= authsize;
1340
1341 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001342 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001343 if (IS_ERR(edesc))
1344 return PTR_ERR(edesc);
1345
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001346 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001347 ((!edesc->src_nents && !edesc->dst_nents) ||
1348 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001349
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001350 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001351 edesc->desc.hdr = ctx->desc_hdr_template |
1352 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001353 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001354
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001355 /* reset integrity check result bits */
1356 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001357
Kim Phillipse938e462009-03-29 15:53:23 +08001358 return ipsec_esp(edesc, req, NULL, 0,
1359 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001360
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001361 }
Kim Phillipse938e462009-03-29 15:53:23 +08001362
1363 /* Have to check the ICV with software */
1364 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1365
1366 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1367 if (edesc->dma_len)
1368 icvdata = &edesc->link_tbl[edesc->src_nents +
1369 edesc->dst_nents + 2];
1370 else
1371 icvdata = &edesc->link_tbl[0];
1372
1373 sg = sg_last(req->src, edesc->src_nents ? : 1);
1374
1375 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1376 ctx->authsize);
1377
1378 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001379}
1380
Lee Nipper56af8cd2009-03-29 15:50:50 +08001381static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001382{
1383 struct aead_request *areq = &req->areq;
1384 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1385 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001386 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001387
1388 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001389 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001390 if (IS_ERR(edesc))
1391 return PTR_ERR(edesc);
1392
1393 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001394 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001395
1396 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001397 /* avoid consecutive packets going out with same IV */
1398 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001399
1400 return ipsec_esp(edesc, areq, req->giv, req->seq,
1401 ipsec_esp_encrypt_done);
1402}
1403
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001404static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1405 const u8 *key, unsigned int keylen)
1406{
1407 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001408
1409 memcpy(&ctx->key, key, keylen);
1410 ctx->keylen = keylen;
1411
1412 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001413}
1414
1415static void common_nonsnoop_unmap(struct device *dev,
1416 struct talitos_edesc *edesc,
1417 struct ablkcipher_request *areq)
1418{
1419 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1420 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1421 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1422
1423 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1424
1425 if (edesc->dma_len)
1426 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1427 DMA_BIDIRECTIONAL);
1428}
1429
1430static void ablkcipher_done(struct device *dev,
1431 struct talitos_desc *desc, void *context,
1432 int err)
1433{
1434 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001435 struct talitos_edesc *edesc;
1436
1437 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001438
1439 common_nonsnoop_unmap(dev, edesc, areq);
1440
1441 kfree(edesc);
1442
1443 areq->base.complete(&areq->base, err);
1444}
1445
1446static int common_nonsnoop(struct talitos_edesc *edesc,
1447 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001448 void (*callback) (struct device *dev,
1449 struct talitos_desc *desc,
1450 void *context, int error))
1451{
1452 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1453 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1454 struct device *dev = ctx->dev;
1455 struct talitos_desc *desc = &edesc->desc;
1456 unsigned int cryptlen = areq->nbytes;
1457 unsigned int ivsize;
1458 int sg_count, ret;
1459
1460 /* first DWORD empty */
1461 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001462 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001463 desc->ptr[0].j_extent = 0;
1464
1465 /* cipher iv */
1466 ivsize = crypto_ablkcipher_ivsize(cipher);
Kim Phillipsfebec542011-07-15 11:21:39 +08001467 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001468 DMA_TO_DEVICE);
1469
1470 /* cipher key */
1471 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1472 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1473
1474 /*
1475 * cipher in
1476 */
1477 desc->ptr[3].len = cpu_to_be16(cryptlen);
1478 desc->ptr[3].j_extent = 0;
1479
1480 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1481 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1482 : DMA_TO_DEVICE,
1483 edesc->src_is_chained);
1484
1485 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001486 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001487 } else {
1488 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1489 &edesc->link_tbl[0]);
1490 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001491 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001492 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001493 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1494 edesc->dma_len,
1495 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001496 } else {
1497 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001498 to_talitos_ptr(&desc->ptr[3],
1499 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001500 }
1501 }
1502
1503 /* cipher out */
1504 desc->ptr[4].len = cpu_to_be16(cryptlen);
1505 desc->ptr[4].j_extent = 0;
1506
1507 if (areq->src != areq->dst)
1508 sg_count = talitos_map_sg(dev, areq->dst,
1509 edesc->dst_nents ? : 1,
1510 DMA_FROM_DEVICE,
1511 edesc->dst_is_chained);
1512
1513 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001514 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001515 } else {
1516 struct talitos_ptr *link_tbl_ptr =
1517 &edesc->link_tbl[edesc->src_nents + 1];
1518
Kim Phillips81eb0242009-08-13 11:51:51 +10001519 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1520 (edesc->src_nents + 1) *
1521 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001522 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001523 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1524 link_tbl_ptr);
1525 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1526 edesc->dma_len, DMA_BIDIRECTIONAL);
1527 }
1528
1529 /* iv out */
1530 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1531 DMA_FROM_DEVICE);
1532
1533 /* last DWORD empty */
1534 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001535 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001536 desc->ptr[6].j_extent = 0;
1537
Kim Phillips5228f0f2011-07-15 11:21:38 +08001538 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001539 if (ret != -EINPROGRESS) {
1540 common_nonsnoop_unmap(dev, edesc, areq);
1541 kfree(edesc);
1542 }
1543 return ret;
1544}
1545
Kim Phillipse938e462009-03-29 15:53:23 +08001546static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1547 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001548{
1549 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1550 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1551
Lee Nipper497f2e62010-05-19 19:20:36 +10001552 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1553 areq->nbytes, 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001554}
1555
1556static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1557{
1558 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1559 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1560 struct talitos_edesc *edesc;
1561
1562 /* allocate extended descriptor */
1563 edesc = ablkcipher_edesc_alloc(areq);
1564 if (IS_ERR(edesc))
1565 return PTR_ERR(edesc);
1566
1567 /* set encrypt */
1568 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1569
Kim Phillipsfebec542011-07-15 11:21:39 +08001570 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001571}
1572
1573static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1574{
1575 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1576 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1577 struct talitos_edesc *edesc;
1578
1579 /* allocate extended descriptor */
1580 edesc = ablkcipher_edesc_alloc(areq);
1581 if (IS_ERR(edesc))
1582 return PTR_ERR(edesc);
1583
1584 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1585
Kim Phillipsfebec542011-07-15 11:21:39 +08001586 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001587}
1588
Lee Nipper497f2e62010-05-19 19:20:36 +10001589static void common_nonsnoop_hash_unmap(struct device *dev,
1590 struct talitos_edesc *edesc,
1591 struct ahash_request *areq)
1592{
1593 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1594
1595 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1596
1597 /* When using hashctx-in, must unmap it. */
1598 if (edesc->desc.ptr[1].len)
1599 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1600 DMA_TO_DEVICE);
1601
1602 if (edesc->desc.ptr[2].len)
1603 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1604 DMA_TO_DEVICE);
1605
1606 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1607
1608 if (edesc->dma_len)
1609 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1610 DMA_BIDIRECTIONAL);
1611
1612}
1613
1614static void ahash_done(struct device *dev,
1615 struct talitos_desc *desc, void *context,
1616 int err)
1617{
1618 struct ahash_request *areq = context;
1619 struct talitos_edesc *edesc =
1620 container_of(desc, struct talitos_edesc, desc);
1621 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1622
1623 if (!req_ctx->last && req_ctx->to_hash_later) {
1624 /* Position any partial block for next update/final/finup */
1625 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001626 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001627 }
1628 common_nonsnoop_hash_unmap(dev, edesc, areq);
1629
1630 kfree(edesc);
1631
1632 areq->base.complete(&areq->base, err);
1633}
1634
1635static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1636 struct ahash_request *areq, unsigned int length,
1637 void (*callback) (struct device *dev,
1638 struct talitos_desc *desc,
1639 void *context, int error))
1640{
1641 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1642 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1643 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1644 struct device *dev = ctx->dev;
1645 struct talitos_desc *desc = &edesc->desc;
1646 int sg_count, ret;
1647
1648 /* first DWORD empty */
1649 desc->ptr[0] = zero_entry;
1650
Kim Phillips60f208d2010-05-19 19:21:53 +10001651 /* hash context in */
1652 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001653 map_single_talitos_ptr(dev, &desc->ptr[1],
1654 req_ctx->hw_context_size,
1655 (char *)req_ctx->hw_context, 0,
1656 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001657 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001658 } else {
1659 desc->ptr[1] = zero_entry;
1660 /* Indicate next op is not the first. */
1661 req_ctx->first = 0;
1662 }
1663
1664 /* HMAC key */
1665 if (ctx->keylen)
1666 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1667 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1668 else
1669 desc->ptr[2] = zero_entry;
1670
1671 /*
1672 * data in
1673 */
1674 desc->ptr[3].len = cpu_to_be16(length);
1675 desc->ptr[3].j_extent = 0;
1676
1677 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1678 edesc->src_nents ? : 1,
1679 DMA_TO_DEVICE,
1680 edesc->src_is_chained);
1681
1682 if (sg_count == 1) {
1683 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1684 } else {
1685 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1686 &edesc->link_tbl[0]);
1687 if (sg_count > 1) {
1688 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1689 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1690 dma_sync_single_for_device(ctx->dev,
1691 edesc->dma_link_tbl,
1692 edesc->dma_len,
1693 DMA_BIDIRECTIONAL);
1694 } else {
1695 /* Only one segment now, so no link tbl needed */
1696 to_talitos_ptr(&desc->ptr[3],
1697 sg_dma_address(req_ctx->psrc));
1698 }
1699 }
1700
1701 /* fifth DWORD empty */
1702 desc->ptr[4] = zero_entry;
1703
1704 /* hash/HMAC out -or- hash context out */
1705 if (req_ctx->last)
1706 map_single_talitos_ptr(dev, &desc->ptr[5],
1707 crypto_ahash_digestsize(tfm),
1708 areq->result, 0, DMA_FROM_DEVICE);
1709 else
1710 map_single_talitos_ptr(dev, &desc->ptr[5],
1711 req_ctx->hw_context_size,
1712 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1713
1714 /* last DWORD empty */
1715 desc->ptr[6] = zero_entry;
1716
Kim Phillips5228f0f2011-07-15 11:21:38 +08001717 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001718 if (ret != -EINPROGRESS) {
1719 common_nonsnoop_hash_unmap(dev, edesc, areq);
1720 kfree(edesc);
1721 }
1722 return ret;
1723}
1724
1725static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1726 unsigned int nbytes)
1727{
1728 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1729 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1730 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1731
1732 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1733 nbytes, 0, 0, areq->base.flags);
1734}
1735
1736static int ahash_init(struct ahash_request *areq)
1737{
1738 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1739 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1740
1741 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001742 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001743 req_ctx->first = 1; /* first indicates h/w must init its context */
1744 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001745 req_ctx->hw_context_size =
1746 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1747 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1748 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1749
1750 return 0;
1751}
1752
Kim Phillips60f208d2010-05-19 19:21:53 +10001753/*
1754 * on h/w without explicit sha224 support, we initialize h/w context
1755 * manually with sha224 constants, and tell it to run sha256.
1756 */
1757static int ahash_init_sha224_swinit(struct ahash_request *areq)
1758{
1759 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1760
1761 ahash_init(areq);
1762 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1763
Kim Phillipsa7524472010-09-23 15:56:38 +08001764 req_ctx->hw_context[0] = SHA224_H0;
1765 req_ctx->hw_context[1] = SHA224_H1;
1766 req_ctx->hw_context[2] = SHA224_H2;
1767 req_ctx->hw_context[3] = SHA224_H3;
1768 req_ctx->hw_context[4] = SHA224_H4;
1769 req_ctx->hw_context[5] = SHA224_H5;
1770 req_ctx->hw_context[6] = SHA224_H6;
1771 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001772
1773 /* init 64-bit count */
1774 req_ctx->hw_context[8] = 0;
1775 req_ctx->hw_context[9] = 0;
1776
1777 return 0;
1778}
1779
Lee Nipper497f2e62010-05-19 19:20:36 +10001780static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1781{
1782 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1783 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1784 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1785 struct talitos_edesc *edesc;
1786 unsigned int blocksize =
1787 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1788 unsigned int nbytes_to_hash;
1789 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001790 unsigned int nsg;
Lee Nipper497f2e62010-05-19 19:20:36 +10001791 int chained;
1792
Lee Nipper5e833bc2010-06-16 15:29:15 +10001793 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1794 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001795 sg_copy_to_buffer(areq->src,
1796 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001797 req_ctx->buf + req_ctx->nbuf, nbytes);
1798 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001799 return 0;
1800 }
1801
Lee Nipper5e833bc2010-06-16 15:29:15 +10001802 /* At least (blocksize + 1) bytes are available to hash */
1803 nbytes_to_hash = nbytes + req_ctx->nbuf;
1804 to_hash_later = nbytes_to_hash & (blocksize - 1);
1805
1806 if (req_ctx->last)
1807 to_hash_later = 0;
1808 else if (to_hash_later)
1809 /* There is a partial block. Hash the full block(s) now */
1810 nbytes_to_hash -= to_hash_later;
1811 else {
1812 /* Keep one block buffered */
1813 nbytes_to_hash -= blocksize;
1814 to_hash_later = blocksize;
1815 }
1816
1817 /* Chain in any previously buffered data */
1818 if (req_ctx->nbuf) {
1819 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1820 sg_init_table(req_ctx->bufsl, nsg);
1821 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1822 if (nsg > 1)
1823 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001824 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001825 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001826 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001827
Lee Nipper5e833bc2010-06-16 15:29:15 +10001828 if (to_hash_later) {
1829 int nents = sg_count(areq->src, nbytes, &chained);
1830 sg_copy_end_to_buffer(areq->src, nents,
1831 req_ctx->bufnext,
1832 to_hash_later,
1833 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001834 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001835 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001836
Lee Nipper5e833bc2010-06-16 15:29:15 +10001837 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001838 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1839 if (IS_ERR(edesc))
1840 return PTR_ERR(edesc);
1841
1842 edesc->desc.hdr = ctx->desc_hdr_template;
1843
1844 /* On last one, request SEC to pad; otherwise continue */
1845 if (req_ctx->last)
1846 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1847 else
1848 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1849
Kim Phillips60f208d2010-05-19 19:21:53 +10001850 /* request SEC to INIT hash. */
1851 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001852 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1853
1854 /* When the tfm context has a keylen, it's an HMAC.
1855 * A first or last (ie. not middle) descriptor must request HMAC.
1856 */
1857 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1858 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1859
1860 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1861 ahash_done);
1862}
1863
1864static int ahash_update(struct ahash_request *areq)
1865{
1866 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1867
1868 req_ctx->last = 0;
1869
1870 return ahash_process_req(areq, areq->nbytes);
1871}
1872
1873static int ahash_final(struct ahash_request *areq)
1874{
1875 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1876
1877 req_ctx->last = 1;
1878
1879 return ahash_process_req(areq, 0);
1880}
1881
1882static int ahash_finup(struct ahash_request *areq)
1883{
1884 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1885
1886 req_ctx->last = 1;
1887
1888 return ahash_process_req(areq, areq->nbytes);
1889}
1890
1891static int ahash_digest(struct ahash_request *areq)
1892{
1893 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001894 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001895
Kim Phillips60f208d2010-05-19 19:21:53 +10001896 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001897 req_ctx->last = 1;
1898
1899 return ahash_process_req(areq, areq->nbytes);
1900}
1901
Lee Nipper79b3a412011-11-21 16:13:25 +08001902struct keyhash_result {
1903 struct completion completion;
1904 int err;
1905};
1906
1907static void keyhash_complete(struct crypto_async_request *req, int err)
1908{
1909 struct keyhash_result *res = req->data;
1910
1911 if (err == -EINPROGRESS)
1912 return;
1913
1914 res->err = err;
1915 complete(&res->completion);
1916}
1917
1918static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1919 u8 *hash)
1920{
1921 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1922
1923 struct scatterlist sg[1];
1924 struct ahash_request *req;
1925 struct keyhash_result hresult;
1926 int ret;
1927
1928 init_completion(&hresult.completion);
1929
1930 req = ahash_request_alloc(tfm, GFP_KERNEL);
1931 if (!req)
1932 return -ENOMEM;
1933
1934 /* Keep tfm keylen == 0 during hash of the long key */
1935 ctx->keylen = 0;
1936 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1937 keyhash_complete, &hresult);
1938
1939 sg_init_one(&sg[0], key, keylen);
1940
1941 ahash_request_set_crypt(req, sg, hash, keylen);
1942 ret = crypto_ahash_digest(req);
1943 switch (ret) {
1944 case 0:
1945 break;
1946 case -EINPROGRESS:
1947 case -EBUSY:
1948 ret = wait_for_completion_interruptible(
1949 &hresult.completion);
1950 if (!ret)
1951 ret = hresult.err;
1952 break;
1953 default:
1954 break;
1955 }
1956 ahash_request_free(req);
1957
1958 return ret;
1959}
1960
1961static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1962 unsigned int keylen)
1963{
1964 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1965 unsigned int blocksize =
1966 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1967 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1968 unsigned int keysize = keylen;
1969 u8 hash[SHA512_DIGEST_SIZE];
1970 int ret;
1971
1972 if (keylen <= blocksize)
1973 memcpy(ctx->key, key, keysize);
1974 else {
1975 /* Must get the hash of the long key */
1976 ret = keyhash(tfm, key, keylen, hash);
1977
1978 if (ret) {
1979 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1980 return -EINVAL;
1981 }
1982
1983 keysize = digestsize;
1984 memcpy(ctx->key, hash, digestsize);
1985 }
1986
1987 ctx->keylen = keysize;
1988
1989 return 0;
1990}
1991
1992
Kim Phillips9c4a7962008-06-23 19:50:15 +08001993struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001994 u32 type;
1995 union {
1996 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001997 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001998 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001999 __be32 desc_hdr_template;
2000};
2001
2002static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002003 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002004 { .type = CRYPTO_ALG_TYPE_AEAD,
2005 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002006 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2007 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
2008 .cra_blocksize = AES_BLOCK_SIZE,
2009 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2010 .cra_type = &crypto_aead_type,
2011 .cra_aead = {
2012 .setkey = aead_setkey,
2013 .setauthsize = aead_setauthsize,
2014 .encrypt = aead_encrypt,
2015 .decrypt = aead_decrypt,
2016 .givencrypt = aead_givencrypt,
2017 .geniv = "<built-in>",
2018 .ivsize = AES_BLOCK_SIZE,
2019 .maxauthsize = SHA1_DIGEST_SIZE,
2020 }
2021 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002022 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2023 DESC_HDR_SEL0_AESU |
2024 DESC_HDR_MODE0_AESU_CBC |
2025 DESC_HDR_SEL1_MDEUA |
2026 DESC_HDR_MODE1_MDEU_INIT |
2027 DESC_HDR_MODE1_MDEU_PAD |
2028 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08002029 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002030 { .type = CRYPTO_ALG_TYPE_AEAD,
2031 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002032 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
2033 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
2034 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2035 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2036 .cra_type = &crypto_aead_type,
2037 .cra_aead = {
2038 .setkey = aead_setkey,
2039 .setauthsize = aead_setauthsize,
2040 .encrypt = aead_encrypt,
2041 .decrypt = aead_decrypt,
2042 .givencrypt = aead_givencrypt,
2043 .geniv = "<built-in>",
2044 .ivsize = DES3_EDE_BLOCK_SIZE,
2045 .maxauthsize = SHA1_DIGEST_SIZE,
2046 }
2047 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08002048 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2049 DESC_HDR_SEL0_DEU |
2050 DESC_HDR_MODE0_DEU_CBC |
2051 DESC_HDR_MODE0_DEU_3DES |
2052 DESC_HDR_SEL1_MDEUA |
2053 DESC_HDR_MODE1_MDEU_INIT |
2054 DESC_HDR_MODE1_MDEU_PAD |
2055 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08002056 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002057 { .type = CRYPTO_ALG_TYPE_AEAD,
2058 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002059 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2060 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
2061 .cra_blocksize = AES_BLOCK_SIZE,
2062 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2063 .cra_type = &crypto_aead_type,
2064 .cra_aead = {
2065 .setkey = aead_setkey,
2066 .setauthsize = aead_setauthsize,
2067 .encrypt = aead_encrypt,
2068 .decrypt = aead_decrypt,
2069 .givencrypt = aead_givencrypt,
2070 .geniv = "<built-in>",
2071 .ivsize = AES_BLOCK_SIZE,
2072 .maxauthsize = SHA256_DIGEST_SIZE,
2073 }
2074 },
Lee Nipper3952f172008-07-10 18:29:18 +08002075 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2076 DESC_HDR_SEL0_AESU |
2077 DESC_HDR_MODE0_AESU_CBC |
2078 DESC_HDR_SEL1_MDEUA |
2079 DESC_HDR_MODE1_MDEU_INIT |
2080 DESC_HDR_MODE1_MDEU_PAD |
2081 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2082 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002083 { .type = CRYPTO_ALG_TYPE_AEAD,
2084 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002085 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2086 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2087 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2088 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2089 .cra_type = &crypto_aead_type,
2090 .cra_aead = {
2091 .setkey = aead_setkey,
2092 .setauthsize = aead_setauthsize,
2093 .encrypt = aead_encrypt,
2094 .decrypt = aead_decrypt,
2095 .givencrypt = aead_givencrypt,
2096 .geniv = "<built-in>",
2097 .ivsize = DES3_EDE_BLOCK_SIZE,
2098 .maxauthsize = SHA256_DIGEST_SIZE,
2099 }
2100 },
Lee Nipper3952f172008-07-10 18:29:18 +08002101 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2102 DESC_HDR_SEL0_DEU |
2103 DESC_HDR_MODE0_DEU_CBC |
2104 DESC_HDR_MODE0_DEU_3DES |
2105 DESC_HDR_SEL1_MDEUA |
2106 DESC_HDR_MODE1_MDEU_INIT |
2107 DESC_HDR_MODE1_MDEU_PAD |
2108 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2109 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002110 { .type = CRYPTO_ALG_TYPE_AEAD,
2111 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002112 .cra_name = "authenc(hmac(md5),cbc(aes))",
2113 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2114 .cra_blocksize = AES_BLOCK_SIZE,
2115 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2116 .cra_type = &crypto_aead_type,
2117 .cra_aead = {
2118 .setkey = aead_setkey,
2119 .setauthsize = aead_setauthsize,
2120 .encrypt = aead_encrypt,
2121 .decrypt = aead_decrypt,
2122 .givencrypt = aead_givencrypt,
2123 .geniv = "<built-in>",
2124 .ivsize = AES_BLOCK_SIZE,
2125 .maxauthsize = MD5_DIGEST_SIZE,
2126 }
2127 },
Lee Nipper3952f172008-07-10 18:29:18 +08002128 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2129 DESC_HDR_SEL0_AESU |
2130 DESC_HDR_MODE0_AESU_CBC |
2131 DESC_HDR_SEL1_MDEUA |
2132 DESC_HDR_MODE1_MDEU_INIT |
2133 DESC_HDR_MODE1_MDEU_PAD |
2134 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2135 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002136 { .type = CRYPTO_ALG_TYPE_AEAD,
2137 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002138 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2139 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2140 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2141 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2142 .cra_type = &crypto_aead_type,
2143 .cra_aead = {
2144 .setkey = aead_setkey,
2145 .setauthsize = aead_setauthsize,
2146 .encrypt = aead_encrypt,
2147 .decrypt = aead_decrypt,
2148 .givencrypt = aead_givencrypt,
2149 .geniv = "<built-in>",
2150 .ivsize = DES3_EDE_BLOCK_SIZE,
2151 .maxauthsize = MD5_DIGEST_SIZE,
2152 }
2153 },
Lee Nipper3952f172008-07-10 18:29:18 +08002154 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2155 DESC_HDR_SEL0_DEU |
2156 DESC_HDR_MODE0_DEU_CBC |
2157 DESC_HDR_MODE0_DEU_3DES |
2158 DESC_HDR_SEL1_MDEUA |
2159 DESC_HDR_MODE1_MDEU_INIT |
2160 DESC_HDR_MODE1_MDEU_PAD |
2161 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002162 },
2163 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002164 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2165 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002166 .cra_name = "cbc(aes)",
2167 .cra_driver_name = "cbc-aes-talitos",
2168 .cra_blocksize = AES_BLOCK_SIZE,
2169 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2170 CRYPTO_ALG_ASYNC,
2171 .cra_type = &crypto_ablkcipher_type,
2172 .cra_ablkcipher = {
2173 .setkey = ablkcipher_setkey,
2174 .encrypt = ablkcipher_encrypt,
2175 .decrypt = ablkcipher_decrypt,
2176 .geniv = "eseqiv",
2177 .min_keysize = AES_MIN_KEY_SIZE,
2178 .max_keysize = AES_MAX_KEY_SIZE,
2179 .ivsize = AES_BLOCK_SIZE,
2180 }
2181 },
2182 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2183 DESC_HDR_SEL0_AESU |
2184 DESC_HDR_MODE0_AESU_CBC,
2185 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002186 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2187 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002188 .cra_name = "cbc(des3_ede)",
2189 .cra_driver_name = "cbc-3des-talitos",
2190 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2191 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2192 CRYPTO_ALG_ASYNC,
2193 .cra_type = &crypto_ablkcipher_type,
2194 .cra_ablkcipher = {
2195 .setkey = ablkcipher_setkey,
2196 .encrypt = ablkcipher_encrypt,
2197 .decrypt = ablkcipher_decrypt,
2198 .geniv = "eseqiv",
2199 .min_keysize = DES3_EDE_KEY_SIZE,
2200 .max_keysize = DES3_EDE_KEY_SIZE,
2201 .ivsize = DES3_EDE_BLOCK_SIZE,
2202 }
2203 },
2204 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2205 DESC_HDR_SEL0_DEU |
2206 DESC_HDR_MODE0_DEU_CBC |
2207 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002208 },
2209 /* AHASH algorithms. */
2210 { .type = CRYPTO_ALG_TYPE_AHASH,
2211 .alg.hash = {
2212 .init = ahash_init,
2213 .update = ahash_update,
2214 .final = ahash_final,
2215 .finup = ahash_finup,
2216 .digest = ahash_digest,
2217 .halg.digestsize = MD5_DIGEST_SIZE,
2218 .halg.base = {
2219 .cra_name = "md5",
2220 .cra_driver_name = "md5-talitos",
2221 .cra_blocksize = MD5_BLOCK_SIZE,
2222 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2223 CRYPTO_ALG_ASYNC,
2224 .cra_type = &crypto_ahash_type
2225 }
2226 },
2227 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2228 DESC_HDR_SEL0_MDEUA |
2229 DESC_HDR_MODE0_MDEU_MD5,
2230 },
2231 { .type = CRYPTO_ALG_TYPE_AHASH,
2232 .alg.hash = {
2233 .init = ahash_init,
2234 .update = ahash_update,
2235 .final = ahash_final,
2236 .finup = ahash_finup,
2237 .digest = ahash_digest,
2238 .halg.digestsize = SHA1_DIGEST_SIZE,
2239 .halg.base = {
2240 .cra_name = "sha1",
2241 .cra_driver_name = "sha1-talitos",
2242 .cra_blocksize = SHA1_BLOCK_SIZE,
2243 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2244 CRYPTO_ALG_ASYNC,
2245 .cra_type = &crypto_ahash_type
2246 }
2247 },
2248 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2249 DESC_HDR_SEL0_MDEUA |
2250 DESC_HDR_MODE0_MDEU_SHA1,
2251 },
2252 { .type = CRYPTO_ALG_TYPE_AHASH,
2253 .alg.hash = {
2254 .init = ahash_init,
2255 .update = ahash_update,
2256 .final = ahash_final,
2257 .finup = ahash_finup,
2258 .digest = ahash_digest,
Kim Phillips60f208d2010-05-19 19:21:53 +10002259 .halg.digestsize = SHA224_DIGEST_SIZE,
2260 .halg.base = {
2261 .cra_name = "sha224",
2262 .cra_driver_name = "sha224-talitos",
2263 .cra_blocksize = SHA224_BLOCK_SIZE,
2264 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2265 CRYPTO_ALG_ASYNC,
2266 .cra_type = &crypto_ahash_type
2267 }
2268 },
2269 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2270 DESC_HDR_SEL0_MDEUA |
2271 DESC_HDR_MODE0_MDEU_SHA224,
2272 },
2273 { .type = CRYPTO_ALG_TYPE_AHASH,
2274 .alg.hash = {
2275 .init = ahash_init,
2276 .update = ahash_update,
2277 .final = ahash_final,
2278 .finup = ahash_finup,
2279 .digest = ahash_digest,
Lee Nipper497f2e62010-05-19 19:20:36 +10002280 .halg.digestsize = SHA256_DIGEST_SIZE,
2281 .halg.base = {
2282 .cra_name = "sha256",
2283 .cra_driver_name = "sha256-talitos",
2284 .cra_blocksize = SHA256_BLOCK_SIZE,
2285 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2286 CRYPTO_ALG_ASYNC,
2287 .cra_type = &crypto_ahash_type
2288 }
2289 },
2290 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2291 DESC_HDR_SEL0_MDEUA |
2292 DESC_HDR_MODE0_MDEU_SHA256,
2293 },
2294 { .type = CRYPTO_ALG_TYPE_AHASH,
2295 .alg.hash = {
2296 .init = ahash_init,
2297 .update = ahash_update,
2298 .final = ahash_final,
2299 .finup = ahash_finup,
2300 .digest = ahash_digest,
2301 .halg.digestsize = SHA384_DIGEST_SIZE,
2302 .halg.base = {
2303 .cra_name = "sha384",
2304 .cra_driver_name = "sha384-talitos",
2305 .cra_blocksize = SHA384_BLOCK_SIZE,
2306 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2307 CRYPTO_ALG_ASYNC,
2308 .cra_type = &crypto_ahash_type
2309 }
2310 },
2311 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2312 DESC_HDR_SEL0_MDEUB |
2313 DESC_HDR_MODE0_MDEUB_SHA384,
2314 },
2315 { .type = CRYPTO_ALG_TYPE_AHASH,
2316 .alg.hash = {
2317 .init = ahash_init,
2318 .update = ahash_update,
2319 .final = ahash_final,
2320 .finup = ahash_finup,
2321 .digest = ahash_digest,
2322 .halg.digestsize = SHA512_DIGEST_SIZE,
2323 .halg.base = {
2324 .cra_name = "sha512",
2325 .cra_driver_name = "sha512-talitos",
2326 .cra_blocksize = SHA512_BLOCK_SIZE,
2327 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2328 CRYPTO_ALG_ASYNC,
2329 .cra_type = &crypto_ahash_type
2330 }
2331 },
2332 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2333 DESC_HDR_SEL0_MDEUB |
2334 DESC_HDR_MODE0_MDEUB_SHA512,
2335 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002336 { .type = CRYPTO_ALG_TYPE_AHASH,
2337 .alg.hash = {
2338 .init = ahash_init,
2339 .update = ahash_update,
2340 .final = ahash_final,
2341 .finup = ahash_finup,
2342 .digest = ahash_digest,
2343 .setkey = ahash_setkey,
2344 .halg.digestsize = MD5_DIGEST_SIZE,
2345 .halg.base = {
2346 .cra_name = "hmac(md5)",
2347 .cra_driver_name = "hmac-md5-talitos",
2348 .cra_blocksize = MD5_BLOCK_SIZE,
2349 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2350 CRYPTO_ALG_ASYNC,
2351 .cra_type = &crypto_ahash_type
2352 }
2353 },
2354 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2355 DESC_HDR_SEL0_MDEUA |
2356 DESC_HDR_MODE0_MDEU_MD5,
2357 },
2358 { .type = CRYPTO_ALG_TYPE_AHASH,
2359 .alg.hash = {
2360 .init = ahash_init,
2361 .update = ahash_update,
2362 .final = ahash_final,
2363 .finup = ahash_finup,
2364 .digest = ahash_digest,
2365 .setkey = ahash_setkey,
2366 .halg.digestsize = SHA1_DIGEST_SIZE,
2367 .halg.base = {
2368 .cra_name = "hmac(sha1)",
2369 .cra_driver_name = "hmac-sha1-talitos",
2370 .cra_blocksize = SHA1_BLOCK_SIZE,
2371 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2372 CRYPTO_ALG_ASYNC,
2373 .cra_type = &crypto_ahash_type
2374 }
2375 },
2376 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2377 DESC_HDR_SEL0_MDEUA |
2378 DESC_HDR_MODE0_MDEU_SHA1,
2379 },
2380 { .type = CRYPTO_ALG_TYPE_AHASH,
2381 .alg.hash = {
2382 .init = ahash_init,
2383 .update = ahash_update,
2384 .final = ahash_final,
2385 .finup = ahash_finup,
2386 .digest = ahash_digest,
2387 .setkey = ahash_setkey,
2388 .halg.digestsize = SHA224_DIGEST_SIZE,
2389 .halg.base = {
2390 .cra_name = "hmac(sha224)",
2391 .cra_driver_name = "hmac-sha224-talitos",
2392 .cra_blocksize = SHA224_BLOCK_SIZE,
2393 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2394 CRYPTO_ALG_ASYNC,
2395 .cra_type = &crypto_ahash_type
2396 }
2397 },
2398 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2399 DESC_HDR_SEL0_MDEUA |
2400 DESC_HDR_MODE0_MDEU_SHA224,
2401 },
2402 { .type = CRYPTO_ALG_TYPE_AHASH,
2403 .alg.hash = {
2404 .init = ahash_init,
2405 .update = ahash_update,
2406 .final = ahash_final,
2407 .finup = ahash_finup,
2408 .digest = ahash_digest,
2409 .setkey = ahash_setkey,
2410 .halg.digestsize = SHA256_DIGEST_SIZE,
2411 .halg.base = {
2412 .cra_name = "hmac(sha256)",
2413 .cra_driver_name = "hmac-sha256-talitos",
2414 .cra_blocksize = SHA256_BLOCK_SIZE,
2415 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2416 CRYPTO_ALG_ASYNC,
2417 .cra_type = &crypto_ahash_type
2418 }
2419 },
2420 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2421 DESC_HDR_SEL0_MDEUA |
2422 DESC_HDR_MODE0_MDEU_SHA256,
2423 },
2424 { .type = CRYPTO_ALG_TYPE_AHASH,
2425 .alg.hash = {
2426 .init = ahash_init,
2427 .update = ahash_update,
2428 .final = ahash_final,
2429 .finup = ahash_finup,
2430 .digest = ahash_digest,
2431 .setkey = ahash_setkey,
2432 .halg.digestsize = SHA384_DIGEST_SIZE,
2433 .halg.base = {
2434 .cra_name = "hmac(sha384)",
2435 .cra_driver_name = "hmac-sha384-talitos",
2436 .cra_blocksize = SHA384_BLOCK_SIZE,
2437 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2438 CRYPTO_ALG_ASYNC,
2439 .cra_type = &crypto_ahash_type
2440 }
2441 },
2442 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2443 DESC_HDR_SEL0_MDEUB |
2444 DESC_HDR_MODE0_MDEUB_SHA384,
2445 },
2446 { .type = CRYPTO_ALG_TYPE_AHASH,
2447 .alg.hash = {
2448 .init = ahash_init,
2449 .update = ahash_update,
2450 .final = ahash_final,
2451 .finup = ahash_finup,
2452 .digest = ahash_digest,
2453 .setkey = ahash_setkey,
2454 .halg.digestsize = SHA512_DIGEST_SIZE,
2455 .halg.base = {
2456 .cra_name = "hmac(sha512)",
2457 .cra_driver_name = "hmac-sha512-talitos",
2458 .cra_blocksize = SHA512_BLOCK_SIZE,
2459 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2460 CRYPTO_ALG_ASYNC,
2461 .cra_type = &crypto_ahash_type
2462 }
2463 },
2464 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2465 DESC_HDR_SEL0_MDEUB |
2466 DESC_HDR_MODE0_MDEUB_SHA512,
2467 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002468};
2469
2470struct talitos_crypto_alg {
2471 struct list_head entry;
2472 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002473 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002474};
2475
2476static int talitos_cra_init(struct crypto_tfm *tfm)
2477{
2478 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002479 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002480 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002481 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002482
Lee Nipper497f2e62010-05-19 19:20:36 +10002483 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2484 talitos_alg = container_of(__crypto_ahash_alg(alg),
2485 struct talitos_crypto_alg,
2486 algt.alg.hash);
2487 else
2488 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2489 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002490
Kim Phillips9c4a7962008-06-23 19:50:15 +08002491 /* update context with ptr to dev */
2492 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002493
Kim Phillips5228f0f2011-07-15 11:21:38 +08002494 /* assign SEC channel to tfm in round-robin fashion */
2495 priv = dev_get_drvdata(ctx->dev);
2496 ctx->ch = atomic_inc_return(&priv->last_chan) &
2497 (priv->num_channels - 1);
2498
Kim Phillips9c4a7962008-06-23 19:50:15 +08002499 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002500 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002501
Kim Phillips602dba52011-07-15 11:21:39 +08002502 /* select done notification */
2503 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2504
Lee Nipper497f2e62010-05-19 19:20:36 +10002505 return 0;
2506}
2507
2508static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2509{
2510 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2511
2512 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002513
2514 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002515 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002516
2517 return 0;
2518}
2519
Lee Nipper497f2e62010-05-19 19:20:36 +10002520static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2521{
2522 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2523
2524 talitos_cra_init(tfm);
2525
2526 ctx->keylen = 0;
2527 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2528 sizeof(struct talitos_ahash_req_ctx));
2529
2530 return 0;
2531}
2532
Kim Phillips9c4a7962008-06-23 19:50:15 +08002533/*
2534 * given the alg's descriptor header template, determine whether descriptor
2535 * type and primary/secondary execution units required match the hw
2536 * capabilities description provided in the device tree node.
2537 */
2538static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2539{
2540 struct talitos_private *priv = dev_get_drvdata(dev);
2541 int ret;
2542
2543 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2544 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2545
2546 if (SECONDARY_EU(desc_hdr_template))
2547 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2548 & priv->exec_units);
2549
2550 return ret;
2551}
2552
Grant Likely2dc11582010-08-06 09:25:50 -06002553static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002554{
2555 struct device *dev = &ofdev->dev;
2556 struct talitos_private *priv = dev_get_drvdata(dev);
2557 struct talitos_crypto_alg *t_alg, *n;
2558 int i;
2559
2560 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002561 switch (t_alg->algt.type) {
2562 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2563 case CRYPTO_ALG_TYPE_AEAD:
2564 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2565 break;
2566 case CRYPTO_ALG_TYPE_AHASH:
2567 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2568 break;
2569 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002570 list_del(&t_alg->entry);
2571 kfree(t_alg);
2572 }
2573
2574 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2575 talitos_unregister_rng(dev);
2576
Kim Phillips4b9926282009-08-13 11:50:38 +10002577 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002578 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002579
Kim Phillips4b9926282009-08-13 11:50:38 +10002580 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002581
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002582 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002583 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002584 free_irq(priv->irq[i], dev);
2585 irq_dispose_mapping(priv->irq[i]);
2586 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002587
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002588 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002589 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002590 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002591
2592 iounmap(priv->reg);
2593
2594 dev_set_drvdata(dev, NULL);
2595
2596 kfree(priv);
2597
2598 return 0;
2599}
2600
2601static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2602 struct talitos_alg_template
2603 *template)
2604{
Kim Phillips60f208d2010-05-19 19:21:53 +10002605 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002606 struct talitos_crypto_alg *t_alg;
2607 struct crypto_alg *alg;
2608
2609 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2610 if (!t_alg)
2611 return ERR_PTR(-ENOMEM);
2612
Lee Nipperacbf7c622010-05-19 19:19:33 +10002613 t_alg->algt = *template;
2614
2615 switch (t_alg->algt.type) {
2616 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002617 alg = &t_alg->algt.alg.crypto;
2618 alg->cra_init = talitos_cra_init;
2619 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002620 case CRYPTO_ALG_TYPE_AEAD:
2621 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002622 alg->cra_init = talitos_cra_init_aead;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002623 break;
2624 case CRYPTO_ALG_TYPE_AHASH:
2625 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002626 alg->cra_init = talitos_cra_init_ahash;
Lee Nipper79b3a412011-11-21 16:13:25 +08002627 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06002628 !strncmp(alg->cra_name, "hmac", 4)) {
2629 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002630 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002631 }
Kim Phillips60f208d2010-05-19 19:21:53 +10002632 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002633 (!strcmp(alg->cra_name, "sha224") ||
2634 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002635 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2636 t_alg->algt.desc_hdr_template =
2637 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2638 DESC_HDR_SEL0_MDEUA |
2639 DESC_HDR_MODE0_MDEU_SHA256;
2640 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002641 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002642 default:
2643 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2644 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002645 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002646
Kim Phillips9c4a7962008-06-23 19:50:15 +08002647 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002648 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002649 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002650 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002651
Kim Phillips9c4a7962008-06-23 19:50:15 +08002652 t_alg->dev = dev;
2653
2654 return t_alg;
2655}
2656
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002657static int talitos_probe_irq(struct platform_device *ofdev)
2658{
2659 struct device *dev = &ofdev->dev;
2660 struct device_node *np = ofdev->dev.of_node;
2661 struct talitos_private *priv = dev_get_drvdata(dev);
2662 int err;
2663
2664 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002665 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002666 dev_err(dev, "failed to map irq\n");
2667 return -EINVAL;
2668 }
2669
2670 priv->irq[1] = irq_of_parse_and_map(np, 1);
2671
2672 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002673 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002674 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2675 dev_driver_string(dev), dev);
2676 goto primary_out;
2677 }
2678
2679 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2680 dev_driver_string(dev), dev);
2681 if (err)
2682 goto primary_out;
2683
2684 /* get the secondary irq line */
2685 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2686 dev_driver_string(dev), dev);
2687 if (err) {
2688 dev_err(dev, "failed to request secondary irq\n");
2689 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002690 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002691 }
2692
2693 return err;
2694
2695primary_out:
2696 if (err) {
2697 dev_err(dev, "failed to request primary irq\n");
2698 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002699 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002700 }
2701
2702 return err;
2703}
2704
Grant Likely1c48a5c2011-02-17 02:43:24 -07002705static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002706{
2707 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002708 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002709 struct talitos_private *priv;
2710 const unsigned int *prop;
2711 int i, err;
2712
2713 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2714 if (!priv)
2715 return -ENOMEM;
2716
2717 dev_set_drvdata(dev, priv);
2718
2719 priv->ofdev = ofdev;
2720
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002721 err = talitos_probe_irq(ofdev);
2722 if (err)
2723 goto err_out;
2724
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002725 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002726 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2727 (unsigned long)dev);
2728 } else {
2729 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2730 (unsigned long)dev);
2731 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2732 (unsigned long)dev);
2733 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002734
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002735 INIT_LIST_HEAD(&priv->alg_list);
2736
Kim Phillips9c4a7962008-06-23 19:50:15 +08002737 priv->reg = of_iomap(np, 0);
2738 if (!priv->reg) {
2739 dev_err(dev, "failed to of_iomap\n");
2740 err = -ENOMEM;
2741 goto err_out;
2742 }
2743
2744 /* get SEC version capabilities from device tree */
2745 prop = of_get_property(np, "fsl,num-channels", NULL);
2746 if (prop)
2747 priv->num_channels = *prop;
2748
2749 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2750 if (prop)
2751 priv->chfifo_len = *prop;
2752
2753 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2754 if (prop)
2755 priv->exec_units = *prop;
2756
2757 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2758 if (prop)
2759 priv->desc_types = *prop;
2760
2761 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2762 !priv->exec_units || !priv->desc_types) {
2763 dev_err(dev, "invalid property data in device tree node\n");
2764 err = -EINVAL;
2765 goto err_out;
2766 }
2767
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002768 if (of_device_is_compatible(np, "fsl,sec3.0"))
2769 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2770
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002771 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002772 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002773 TALITOS_FTR_SHA224_HWINIT |
2774 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002775
Kim Phillips4b9926282009-08-13 11:50:38 +10002776 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2777 priv->num_channels, GFP_KERNEL);
2778 if (!priv->chan) {
2779 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002780 err = -ENOMEM;
2781 goto err_out;
2782 }
2783
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002784 for (i = 0; i < priv->num_channels; i++) {
2785 priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002786 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002787 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2788 }
Kim Phillipsad42d5f2011-11-21 16:13:27 +08002789
Kim Phillips9c4a7962008-06-23 19:50:15 +08002790 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002791 spin_lock_init(&priv->chan[i].head_lock);
2792 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002793 }
2794
2795 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2796
2797 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002798 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2799 priv->fifo_len, GFP_KERNEL);
2800 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002801 dev_err(dev, "failed to allocate request fifo %d\n", i);
2802 err = -ENOMEM;
2803 goto err_out;
2804 }
2805 }
2806
Kim Phillipsec6644d2008-07-17 20:16:40 +08002807 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002808 atomic_set(&priv->chan[i].submit_count,
2809 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002810
Kim Phillips81eb0242009-08-13 11:51:51 +10002811 dma_set_mask(dev, DMA_BIT_MASK(36));
2812
Kim Phillips9c4a7962008-06-23 19:50:15 +08002813 /* reset and initialize the h/w */
2814 err = init_device(dev);
2815 if (err) {
2816 dev_err(dev, "failed to initialize device\n");
2817 goto err_out;
2818 }
2819
2820 /* register the RNG, if available */
2821 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2822 err = talitos_register_rng(dev);
2823 if (err) {
2824 dev_err(dev, "failed to register hwrng: %d\n", err);
2825 goto err_out;
2826 } else
2827 dev_info(dev, "hwrng\n");
2828 }
2829
2830 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002831 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2832 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2833 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002834 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002835
2836 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2837 if (IS_ERR(t_alg)) {
2838 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002839 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08002840 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002841 goto err_out;
2842 }
2843
Lee Nipperacbf7c622010-05-19 19:19:33 +10002844 switch (t_alg->algt.type) {
2845 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2846 case CRYPTO_ALG_TYPE_AEAD:
2847 err = crypto_register_alg(
2848 &t_alg->algt.alg.crypto);
2849 name = t_alg->algt.alg.crypto.cra_driver_name;
2850 break;
2851 case CRYPTO_ALG_TYPE_AHASH:
2852 err = crypto_register_ahash(
2853 &t_alg->algt.alg.hash);
2854 name =
2855 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2856 break;
2857 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002858 if (err) {
2859 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002860 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002861 kfree(t_alg);
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002862 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08002863 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002864 }
2865 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002866 if (!list_empty(&priv->alg_list))
2867 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2868 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002869
2870 return 0;
2871
2872err_out:
2873 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002874
2875 return err;
2876}
2877
Márton Németh6c3f9752010-01-17 21:54:01 +11002878static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002879 {
2880 .compatible = "fsl,sec2.0",
2881 },
2882 {},
2883};
2884MODULE_DEVICE_TABLE(of, talitos_match);
2885
Grant Likely1c48a5c2011-02-17 02:43:24 -07002886static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002887 .driver = {
2888 .name = "talitos",
2889 .owner = THIS_MODULE,
2890 .of_match_table = talitos_match,
2891 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002892 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002893 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002894};
2895
Axel Lin741e8c22011-11-26 21:26:19 +08002896module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002897
2898MODULE_LICENSE("GPL");
2899MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2900MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");