blob: 2cba392f16826f7e59a87dd2b0ddc6def73af978 [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
Kim Phillips81eb0242009-08-13 11:51:51 +100056static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
57{
58 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +080059 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +100060}
61
Kim Phillips9c4a7962008-06-23 19:50:15 +080062/*
63 * map virtual single (contiguous) pointer to h/w descriptor pointer
64 */
65static void map_single_talitos_ptr(struct device *dev,
66 struct talitos_ptr *talitos_ptr,
67 unsigned short len, void *data,
68 unsigned char extent,
69 enum dma_data_direction dir)
70{
Kim Phillips81eb0242009-08-13 11:51:51 +100071 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
72
Kim Phillips9c4a7962008-06-23 19:50:15 +080073 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +100074 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +080075 talitos_ptr->j_extent = extent;
76}
77
78/*
79 * unmap bus single (contiguous) h/w descriptor pointer
80 */
81static void unmap_single_talitos_ptr(struct device *dev,
82 struct talitos_ptr *talitos_ptr,
83 enum dma_data_direction dir)
84{
85 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
86 be16_to_cpu(talitos_ptr->len), dir);
87}
88
89static int reset_channel(struct device *dev, int ch)
90{
91 struct talitos_private *priv = dev_get_drvdata(dev);
92 unsigned int timeout = TALITOS_TIMEOUT;
93
Kim Phillipsad42d5f2011-11-21 16:13:27 +080094 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +080095
Kim Phillipsad42d5f2011-11-21 16:13:27 +080096 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
Kim Phillips9c4a7962008-06-23 19:50:15 +080097 && --timeout)
98 cpu_relax();
99
100 if (timeout == 0) {
101 dev_err(dev, "failed to reset channel %d\n", ch);
102 return -EIO;
103 }
104
Kim Phillips81eb0242009-08-13 11:51:51 +1000105 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800106 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000107 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800108
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800109 /* and ICCR writeback, if available */
110 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800111 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800112 TALITOS_CCCR_LO_IWSE);
113
Kim Phillips9c4a7962008-06-23 19:50:15 +0800114 return 0;
115}
116
117static int reset_device(struct device *dev)
118{
119 struct talitos_private *priv = dev_get_drvdata(dev);
120 unsigned int timeout = TALITOS_TIMEOUT;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800121 u32 mcr = TALITOS_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800122
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800123 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800124
125 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
126 && --timeout)
127 cpu_relax();
128
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600129 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800130 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
131 setbits32(priv->reg + TALITOS_MCR, mcr);
132 }
133
Kim Phillips9c4a7962008-06-23 19:50:15 +0800134 if (timeout == 0) {
135 dev_err(dev, "failed to reset device\n");
136 return -EIO;
137 }
138
139 return 0;
140}
141
142/*
143 * Reset and initialize the device
144 */
145static int init_device(struct device *dev)
146{
147 struct talitos_private *priv = dev_get_drvdata(dev);
148 int ch, err;
149
150 /*
151 * Master reset
152 * errata documentation: warning: certain SEC interrupts
153 * are not fully cleared by writing the MCR:SWR bit,
154 * set bit twice to completely reset
155 */
156 err = reset_device(dev);
157 if (err)
158 return err;
159
160 err = reset_device(dev);
161 if (err)
162 return err;
163
164 /* reset channels */
165 for (ch = 0; ch < priv->num_channels; ch++) {
166 err = reset_channel(dev, ch);
167 if (err)
168 return err;
169 }
170
171 /* enable channel done and error interrupts */
172 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
173 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
174
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800175 /* disable integrity check error interrupts (use writeback instead) */
176 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
177 setbits32(priv->reg + TALITOS_MDEUICR_LO,
178 TALITOS_MDEUICR_LO_ICE);
179
Kim Phillips9c4a7962008-06-23 19:50:15 +0800180 return 0;
181}
182
183/**
184 * talitos_submit - submits a descriptor to the device for processing
185 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800186 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800187 * @desc: the descriptor to be processed by the device
188 * @callback: whom to call when processing is complete
189 * @context: a handle for use by caller (optional)
190 *
191 * desc must contain valid dma-mapped (bus physical) address pointers.
192 * callback must check err and feedback in descriptor header
193 * for device processing status.
194 */
Horia Geanta865d5062012-07-03 19:16:52 +0300195int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
196 void (*callback)(struct device *dev,
197 struct talitos_desc *desc,
198 void *context, int error),
199 void *context)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800200{
201 struct talitos_private *priv = dev_get_drvdata(dev);
202 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800203 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800204 int head;
205
Kim Phillips4b9926282009-08-13 11:50:38 +1000206 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800207
Kim Phillips4b9926282009-08-13 11:50:38 +1000208 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800209 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000210 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800211 return -EAGAIN;
212 }
213
Kim Phillips4b9926282009-08-13 11:50:38 +1000214 head = priv->chan[ch].head;
215 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800216
Kim Phillips9c4a7962008-06-23 19:50:15 +0800217 /* map descriptor and save caller data */
218 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
219 DMA_BIDIRECTIONAL);
220 request->callback = callback;
221 request->context = context;
222
223 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000224 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800225
226 smp_wmb();
227 request->desc = desc;
228
229 /* GO! */
230 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800231 out_be32(priv->chan[ch].reg + TALITOS_FF,
232 upper_32_bits(request->dma_desc));
233 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800234 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800235
Kim Phillips4b9926282009-08-13 11:50:38 +1000236 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800237
238 return -EINPROGRESS;
239}
Horia Geanta865d5062012-07-03 19:16:52 +0300240EXPORT_SYMBOL(talitos_submit);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800241
242/*
243 * process what was done, notify callback of error if not
244 */
245static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
246{
247 struct talitos_private *priv = dev_get_drvdata(dev);
248 struct talitos_request *request, saved_req;
249 unsigned long flags;
250 int tail, status;
251
Kim Phillips4b9926282009-08-13 11:50:38 +1000252 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800253
Kim Phillips4b9926282009-08-13 11:50:38 +1000254 tail = priv->chan[ch].tail;
255 while (priv->chan[ch].fifo[tail].desc) {
256 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800257
258 /* descriptors with their done bits set don't get the error */
259 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100260 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800261 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100262 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800263 if (!error)
264 break;
265 else
266 status = error;
267
268 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800269 sizeof(struct talitos_desc),
270 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800271
272 /* copy entries so we can call callback outside lock */
273 saved_req.desc = request->desc;
274 saved_req.callback = request->callback;
275 saved_req.context = request->context;
276
277 /* release request entry in fifo */
278 smp_wmb();
279 request->desc = NULL;
280
281 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000282 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800283
Kim Phillips4b9926282009-08-13 11:50:38 +1000284 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800285
Kim Phillips4b9926282009-08-13 11:50:38 +1000286 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800287
Kim Phillips9c4a7962008-06-23 19:50:15 +0800288 saved_req.callback(dev, saved_req.desc, saved_req.context,
289 status);
290 /* channel may resume processing in single desc error case */
291 if (error && !reset_ch && status == error)
292 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000293 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
294 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800295 }
296
Kim Phillips4b9926282009-08-13 11:50:38 +1000297 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800298}
299
300/*
301 * process completed requests for channels that have done status
302 */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800303#define DEF_TALITOS_DONE(name, ch_done_mask) \
304static void talitos_done_##name(unsigned long data) \
305{ \
306 struct device *dev = (struct device *)data; \
307 struct talitos_private *priv = dev_get_drvdata(dev); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300308 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800309 \
310 if (ch_done_mask & 1) \
311 flush_channel(dev, 0, 0, 0); \
312 if (priv->num_channels == 1) \
313 goto out; \
314 if (ch_done_mask & (1 << 2)) \
315 flush_channel(dev, 1, 0, 0); \
316 if (ch_done_mask & (1 << 4)) \
317 flush_channel(dev, 2, 0, 0); \
318 if (ch_done_mask & (1 << 6)) \
319 flush_channel(dev, 3, 0, 0); \
320 \
321out: \
322 /* At this point, all completed channels have been processed */ \
323 /* Unmask done interrupts for channels completed later on. */ \
Horia Geanta511d63c2012-03-30 17:49:53 +0300324 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800325 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
326 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300327 spin_unlock_irqrestore(&priv->reg_lock, flags); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800328}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800329DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
330DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
331DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800332
333/*
334 * locate current (offending) descriptor
335 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200336static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800337{
338 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000339 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800340 dma_addr_t cur_desc;
341
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800342 cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800343
Kim Phillips4b9926282009-08-13 11:50:38 +1000344 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800345 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000346 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800347 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200348 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800349 }
350 }
351
Kim Phillips3e721ae2011-10-21 15:20:28 +0200352 return priv->chan[ch].fifo[tail].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800353}
354
355/*
356 * user diagnostics; report root cause of error based on execution unit status
357 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200358static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800359{
360 struct talitos_private *priv = dev_get_drvdata(dev);
361 int i;
362
Kim Phillips3e721ae2011-10-21 15:20:28 +0200363 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800364 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200365
366 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800367 case DESC_HDR_SEL0_AFEU:
368 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
369 in_be32(priv->reg + TALITOS_AFEUISR),
370 in_be32(priv->reg + TALITOS_AFEUISR_LO));
371 break;
372 case DESC_HDR_SEL0_DEU:
373 dev_err(dev, "DEUISR 0x%08x_%08x\n",
374 in_be32(priv->reg + TALITOS_DEUISR),
375 in_be32(priv->reg + TALITOS_DEUISR_LO));
376 break;
377 case DESC_HDR_SEL0_MDEUA:
378 case DESC_HDR_SEL0_MDEUB:
379 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
380 in_be32(priv->reg + TALITOS_MDEUISR),
381 in_be32(priv->reg + TALITOS_MDEUISR_LO));
382 break;
383 case DESC_HDR_SEL0_RNG:
384 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
385 in_be32(priv->reg + TALITOS_RNGUISR),
386 in_be32(priv->reg + TALITOS_RNGUISR_LO));
387 break;
388 case DESC_HDR_SEL0_PKEU:
389 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
390 in_be32(priv->reg + TALITOS_PKEUISR),
391 in_be32(priv->reg + TALITOS_PKEUISR_LO));
392 break;
393 case DESC_HDR_SEL0_AESU:
394 dev_err(dev, "AESUISR 0x%08x_%08x\n",
395 in_be32(priv->reg + TALITOS_AESUISR),
396 in_be32(priv->reg + TALITOS_AESUISR_LO));
397 break;
398 case DESC_HDR_SEL0_CRCU:
399 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
400 in_be32(priv->reg + TALITOS_CRCUISR),
401 in_be32(priv->reg + TALITOS_CRCUISR_LO));
402 break;
403 case DESC_HDR_SEL0_KEU:
404 dev_err(dev, "KEUISR 0x%08x_%08x\n",
405 in_be32(priv->reg + TALITOS_KEUISR),
406 in_be32(priv->reg + TALITOS_KEUISR_LO));
407 break;
408 }
409
Kim Phillips3e721ae2011-10-21 15:20:28 +0200410 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800411 case DESC_HDR_SEL1_MDEUA:
412 case DESC_HDR_SEL1_MDEUB:
413 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
414 in_be32(priv->reg + TALITOS_MDEUISR),
415 in_be32(priv->reg + TALITOS_MDEUISR_LO));
416 break;
417 case DESC_HDR_SEL1_CRCU:
418 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
419 in_be32(priv->reg + TALITOS_CRCUISR),
420 in_be32(priv->reg + TALITOS_CRCUISR_LO));
421 break;
422 }
423
424 for (i = 0; i < 8; i++)
425 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800426 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
427 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800428}
429
430/*
431 * recover from error interrupts
432 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600433static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800434{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800435 struct talitos_private *priv = dev_get_drvdata(dev);
436 unsigned int timeout = TALITOS_TIMEOUT;
437 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800438 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800439
440 for (ch = 0; ch < priv->num_channels; ch++) {
441 /* skip channels without errors */
442 if (!(isr & (1 << (ch * 2 + 1))))
443 continue;
444
445 error = -EINVAL;
446
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800447 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
448 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800449
450 if (v_lo & TALITOS_CCPSR_LO_DOF) {
451 dev_err(dev, "double fetch fifo overflow error\n");
452 error = -EAGAIN;
453 reset_ch = 1;
454 }
455 if (v_lo & TALITOS_CCPSR_LO_SOF) {
456 /* h/w dropped descriptor */
457 dev_err(dev, "single fetch fifo overflow error\n");
458 error = -EAGAIN;
459 }
460 if (v_lo & TALITOS_CCPSR_LO_MDTE)
461 dev_err(dev, "master data transfer error\n");
462 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
463 dev_err(dev, "s/g data length zero error\n");
464 if (v_lo & TALITOS_CCPSR_LO_FPZ)
465 dev_err(dev, "fetch pointer zero error\n");
466 if (v_lo & TALITOS_CCPSR_LO_IDH)
467 dev_err(dev, "illegal descriptor header error\n");
468 if (v_lo & TALITOS_CCPSR_LO_IEU)
469 dev_err(dev, "invalid execution unit error\n");
470 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200471 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800472 if (v_lo & TALITOS_CCPSR_LO_GB)
473 dev_err(dev, "gather boundary error\n");
474 if (v_lo & TALITOS_CCPSR_LO_GRL)
475 dev_err(dev, "gather return/length error\n");
476 if (v_lo & TALITOS_CCPSR_LO_SB)
477 dev_err(dev, "scatter boundary error\n");
478 if (v_lo & TALITOS_CCPSR_LO_SRL)
479 dev_err(dev, "scatter return/length error\n");
480
481 flush_channel(dev, ch, error, reset_ch);
482
483 if (reset_ch) {
484 reset_channel(dev, ch);
485 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800486 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800487 TALITOS_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800488 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
489 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800490 TALITOS_CCCR_CONT) && --timeout)
491 cpu_relax();
492 if (timeout == 0) {
493 dev_err(dev, "failed to restart channel %d\n",
494 ch);
495 reset_dev = 1;
496 }
497 }
498 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800499 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800500 dev_err(dev, "done overflow, internal time out, or rngu error: "
501 "ISR 0x%08x_%08x\n", isr, isr_lo);
502
503 /* purge request queues */
504 for (ch = 0; ch < priv->num_channels; ch++)
505 flush_channel(dev, ch, -EIO, 1);
506
507 /* reset and reinitialize the device */
508 init_device(dev);
509 }
510}
511
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800512#define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
513static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
514{ \
515 struct device *dev = data; \
516 struct talitos_private *priv = dev_get_drvdata(dev); \
517 u32 isr, isr_lo; \
Horia Geanta511d63c2012-03-30 17:49:53 +0300518 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800519 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300520 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800521 isr = in_be32(priv->reg + TALITOS_ISR); \
522 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
523 /* Acknowledge interrupt */ \
524 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
525 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
526 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300527 if (unlikely(isr & ch_err_mask || isr_lo)) { \
528 spin_unlock_irqrestore(&priv->reg_lock, flags); \
529 talitos_error(dev, isr & ch_err_mask, isr_lo); \
530 } \
531 else { \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800532 if (likely(isr & ch_done_mask)) { \
533 /* mask further done interrupts. */ \
534 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
535 /* done_task will unmask done interrupts at exit */ \
536 tasklet_schedule(&priv->done_task[tlet]); \
537 } \
Horia Geanta511d63c2012-03-30 17:49:53 +0300538 spin_unlock_irqrestore(&priv->reg_lock, flags); \
539 } \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800540 \
541 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
542 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800543}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800544DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
545DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
546DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800547
548/*
549 * hwrng
550 */
551static int talitos_rng_data_present(struct hwrng *rng, int wait)
552{
553 struct device *dev = (struct device *)rng->priv;
554 struct talitos_private *priv = dev_get_drvdata(dev);
555 u32 ofl;
556 int i;
557
558 for (i = 0; i < 20; i++) {
559 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
560 TALITOS_RNGUSR_LO_OFL;
561 if (ofl || !wait)
562 break;
563 udelay(10);
564 }
565
566 return !!ofl;
567}
568
569static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
570{
571 struct device *dev = (struct device *)rng->priv;
572 struct talitos_private *priv = dev_get_drvdata(dev);
573
574 /* rng fifo requires 64-bit accesses */
575 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
576 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
577
578 return sizeof(u32);
579}
580
581static int talitos_rng_init(struct hwrng *rng)
582{
583 struct device *dev = (struct device *)rng->priv;
584 struct talitos_private *priv = dev_get_drvdata(dev);
585 unsigned int timeout = TALITOS_TIMEOUT;
586
587 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
588 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
589 && --timeout)
590 cpu_relax();
591 if (timeout == 0) {
592 dev_err(dev, "failed to reset rng hw\n");
593 return -ENODEV;
594 }
595
596 /* start generating */
597 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
598
599 return 0;
600}
601
602static int talitos_register_rng(struct device *dev)
603{
604 struct talitos_private *priv = dev_get_drvdata(dev);
605
606 priv->rng.name = dev_driver_string(dev),
607 priv->rng.init = talitos_rng_init,
608 priv->rng.data_present = talitos_rng_data_present,
609 priv->rng.data_read = talitos_rng_data_read,
610 priv->rng.priv = (unsigned long)dev;
611
612 return hwrng_register(&priv->rng);
613}
614
615static void talitos_unregister_rng(struct device *dev)
616{
617 struct talitos_private *priv = dev_get_drvdata(dev);
618
619 hwrng_unregister(&priv->rng);
620}
621
622/*
623 * crypto alg
624 */
625#define TALITOS_CRA_PRIORITY 3000
Horia Geanta357fb602012-07-03 19:16:53 +0300626#define TALITOS_MAX_KEY_SIZE 96
Lee Nipper3952f172008-07-10 18:29:18 +0800627#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800628
Lee Nipper497f2e62010-05-19 19:20:36 +1000629#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800630
631struct talitos_ctx {
632 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800633 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800634 __be32 desc_hdr_template;
635 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800636 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800637 unsigned int keylen;
638 unsigned int enckeylen;
639 unsigned int authkeylen;
640 unsigned int authsize;
641};
642
Lee Nipper497f2e62010-05-19 19:20:36 +1000643#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
644#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
645
646struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000647 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000648 unsigned int hw_context_size;
649 u8 buf[HASH_MAX_BLOCK_SIZE];
650 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000651 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000652 unsigned int first;
653 unsigned int last;
654 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000655 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000656 struct scatterlist bufsl[2];
657 struct scatterlist *psrc;
658};
659
Lee Nipper56af8cd2009-03-29 15:50:50 +0800660static int aead_setauthsize(struct crypto_aead *authenc,
661 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800662{
663 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
664
665 ctx->authsize = authsize;
666
667 return 0;
668}
669
Lee Nipper56af8cd2009-03-29 15:50:50 +0800670static int aead_setkey(struct crypto_aead *authenc,
671 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800672{
673 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
674 struct rtattr *rta = (void *)key;
675 struct crypto_authenc_key_param *param;
676 unsigned int authkeylen;
677 unsigned int enckeylen;
678
679 if (!RTA_OK(rta, keylen))
680 goto badkey;
681
682 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
683 goto badkey;
684
685 if (RTA_PAYLOAD(rta) < sizeof(*param))
686 goto badkey;
687
688 param = RTA_DATA(rta);
689 enckeylen = be32_to_cpu(param->enckeylen);
690
691 key += RTA_ALIGN(rta->rta_len);
692 keylen -= RTA_ALIGN(rta->rta_len);
693
694 if (keylen < enckeylen)
695 goto badkey;
696
697 authkeylen = keylen - enckeylen;
698
699 if (keylen > TALITOS_MAX_KEY_SIZE)
700 goto badkey;
701
702 memcpy(&ctx->key, key, keylen);
703
704 ctx->keylen = keylen;
705 ctx->enckeylen = enckeylen;
706 ctx->authkeylen = authkeylen;
707
708 return 0;
709
710badkey:
711 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
712 return -EINVAL;
713}
714
715/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800716 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800717 * @src_nents: number of segments in input scatterlist
718 * @dst_nents: number of segments in output scatterlist
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300719 * @src_chained: whether src is chained or not
720 * @dst_chained: whether dst is chained or not
Kim Phillips9c4a7962008-06-23 19:50:15 +0800721 * @dma_len: length of dma mapped link_tbl space
722 * @dma_link_tbl: bus physical address of link_tbl
723 * @desc: h/w descriptor
724 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
725 *
726 * if decrypting (with authcheck), or either one of src_nents or dst_nents
727 * is greater than 1, an integrity check value is concatenated to the end
728 * of link_tbl data
729 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800730struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800731 int src_nents;
732 int dst_nents;
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300733 bool src_chained;
734 bool dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800735 int dma_len;
736 dma_addr_t dma_link_tbl;
737 struct talitos_desc desc;
738 struct talitos_ptr link_tbl[0];
739};
740
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800741static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
742 unsigned int nents, enum dma_data_direction dir,
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300743 bool chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800744{
745 if (unlikely(chained))
746 while (sg) {
747 dma_map_sg(dev, sg, 1, dir);
748 sg = scatterwalk_sg_next(sg);
749 }
750 else
751 dma_map_sg(dev, sg, nents, dir);
752 return nents;
753}
754
755static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
756 enum dma_data_direction dir)
757{
758 while (sg) {
759 dma_unmap_sg(dev, sg, 1, dir);
760 sg = scatterwalk_sg_next(sg);
761 }
762}
763
764static void talitos_sg_unmap(struct device *dev,
765 struct talitos_edesc *edesc,
766 struct scatterlist *src,
767 struct scatterlist *dst)
768{
769 unsigned int src_nents = edesc->src_nents ? : 1;
770 unsigned int dst_nents = edesc->dst_nents ? : 1;
771
772 if (src != dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300773 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800774 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
775 else
776 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
777
Lee Nipper497f2e62010-05-19 19:20:36 +1000778 if (dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300779 if (edesc->dst_chained)
Lee Nipper497f2e62010-05-19 19:20:36 +1000780 talitos_unmap_sg_chain(dev, dst,
781 DMA_FROM_DEVICE);
782 else
783 dma_unmap_sg(dev, dst, dst_nents,
784 DMA_FROM_DEVICE);
785 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800786 } else
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300787 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800788 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
789 else
790 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
791}
792
Kim Phillips9c4a7962008-06-23 19:50:15 +0800793static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800794 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800795 struct aead_request *areq)
796{
797 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
798 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
799 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
800 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
801
802 dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
803
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800804 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800805
806 if (edesc->dma_len)
807 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
808 DMA_BIDIRECTIONAL);
809}
810
811/*
812 * ipsec_esp descriptor callbacks
813 */
814static void ipsec_esp_encrypt_done(struct device *dev,
815 struct talitos_desc *desc, void *context,
816 int err)
817{
818 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800819 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
820 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800821 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800822 struct scatterlist *sg;
823 void *icvdata;
824
Kim Phillips19bbbc62009-03-29 15:53:59 +0800825 edesc = container_of(desc, struct talitos_edesc, desc);
826
Kim Phillips9c4a7962008-06-23 19:50:15 +0800827 ipsec_esp_unmap(dev, edesc, areq);
828
829 /* copy the generated ICV to dst */
Horia Geanta60542502012-08-02 17:16:37 +0300830 if (edesc->dst_nents) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800831 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800832 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800833 sg = sg_last(areq->dst, edesc->dst_nents);
834 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
835 icvdata, ctx->authsize);
836 }
837
838 kfree(edesc);
839
840 aead_request_complete(areq, err);
841}
842
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800843static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800844 struct talitos_desc *desc,
845 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800846{
847 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800848 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
849 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800850 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800851 struct scatterlist *sg;
852 void *icvdata;
853
Kim Phillips19bbbc62009-03-29 15:53:59 +0800854 edesc = container_of(desc, struct talitos_edesc, desc);
855
Kim Phillips9c4a7962008-06-23 19:50:15 +0800856 ipsec_esp_unmap(dev, edesc, req);
857
858 if (!err) {
859 /* auth check */
860 if (edesc->dma_len)
861 icvdata = &edesc->link_tbl[edesc->src_nents +
Lee Nipperf3c85bc2008-07-30 16:26:57 +0800862 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800863 else
864 icvdata = &edesc->link_tbl[0];
865
866 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
867 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
868 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
869 }
870
871 kfree(edesc);
872
873 aead_request_complete(req, err);
874}
875
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800876static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800877 struct talitos_desc *desc,
878 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800879{
880 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800881 struct talitos_edesc *edesc;
882
883 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800884
885 ipsec_esp_unmap(dev, edesc, req);
886
887 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800888 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
889 DESC_HDR_LO_ICCR1_PASS))
890 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800891
892 kfree(edesc);
893
894 aead_request_complete(req, err);
895}
896
Kim Phillips9c4a7962008-06-23 19:50:15 +0800897/*
898 * convert scatterlist to SEC h/w link table format
899 * stop at cryptlen bytes
900 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800901static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800902 int cryptlen, struct talitos_ptr *link_tbl_ptr)
903{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800904 int n_sg = sg_count;
905
906 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000907 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800908 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
909 link_tbl_ptr->j_extent = 0;
910 link_tbl_ptr++;
911 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800912 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800913 }
914
Lee Nipper70bcaca2008-07-03 19:08:46 +0800915 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800916 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800917 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800918 /* Empty this entry, and move to previous one */
919 cryptlen += be16_to_cpu(link_tbl_ptr->len);
920 link_tbl_ptr->len = 0;
921 sg_count--;
922 link_tbl_ptr--;
923 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800924 link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
925 + cryptlen);
926
927 /* tag end of link table */
928 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +0800929
930 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800931}
932
933/*
934 * fill in and submit ipsec_esp descriptor
935 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800936static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800937 u8 *giv, u64 seq,
938 void (*callback) (struct device *dev,
939 struct talitos_desc *desc,
940 void *context, int error))
941{
942 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
943 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
944 struct device *dev = ctx->dev;
945 struct talitos_desc *desc = &edesc->desc;
946 unsigned int cryptlen = areq->cryptlen;
947 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +1000948 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +0800949 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800950 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800951
952 /* hmac key */
953 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
954 0, DMA_TO_DEVICE);
955 /* hmac data */
Kim Phillipse41256f2009-08-13 11:49:06 +1000956 map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
957 sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800958 /* cipher iv */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800959 map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
960 DMA_TO_DEVICE);
961
962 /* cipher key */
963 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
964 (char *)&ctx->key + ctx->authkeylen, 0,
965 DMA_TO_DEVICE);
966
967 /*
968 * cipher in
969 * map and adjust cipher len to aead request cryptlen.
970 * extent is bytes of HMAC postpended to ciphertext,
971 * typically 12 for ipsec
972 */
973 desc->ptr[4].len = cpu_to_be16(cryptlen);
974 desc->ptr[4].j_extent = authsize;
975
Kim Phillipse938e462009-03-29 15:53:23 +0800976 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
977 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
978 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300979 edesc->src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800980
981 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000982 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800983 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800984 sg_link_tbl_len = cryptlen;
985
Kim Phillips962a9c92009-03-29 15:54:30 +0800986 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800987 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +0800988
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800989 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +0800990 &edesc->link_tbl[0]);
991 if (sg_count > 1) {
992 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +1000993 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +0800994 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
995 edesc->dma_len,
996 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +0800997 } else {
998 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +1000999 to_talitos_ptr(&desc->ptr[4],
1000 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001001 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001002 }
1003
1004 /* cipher out */
1005 desc->ptr[5].len = cpu_to_be16(cryptlen);
1006 desc->ptr[5].j_extent = authsize;
1007
Kim Phillipse938e462009-03-29 15:53:23 +08001008 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001009 sg_count = talitos_map_sg(dev, areq->dst,
1010 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001011 DMA_FROM_DEVICE, edesc->dst_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001012
1013 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001014 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001015 } else {
1016 struct talitos_ptr *link_tbl_ptr =
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001017 &edesc->link_tbl[edesc->src_nents + 1];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001018
Kim Phillips81eb0242009-08-13 11:51:51 +10001019 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1020 (edesc->src_nents + 1) *
1021 sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001022 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1023 link_tbl_ptr);
1024
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001025 /* Add an entry to the link table for ICV data */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001026 link_tbl_ptr += sg_count - 1;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001027 link_tbl_ptr->j_extent = 0;
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001028 sg_count++;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001029 link_tbl_ptr++;
1030 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1031 link_tbl_ptr->len = cpu_to_be16(authsize);
1032
1033 /* icv data follows link tables */
Kim Phillips81eb0242009-08-13 11:51:51 +10001034 to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1035 (edesc->src_nents + edesc->dst_nents + 2) *
1036 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001037 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1038 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1039 edesc->dma_len, DMA_BIDIRECTIONAL);
1040 }
1041
1042 /* iv out */
1043 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1044 DMA_FROM_DEVICE);
1045
Kim Phillips5228f0f2011-07-15 11:21:38 +08001046 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001047 if (ret != -EINPROGRESS) {
1048 ipsec_esp_unmap(dev, edesc, areq);
1049 kfree(edesc);
1050 }
1051 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001052}
1053
Kim Phillips9c4a7962008-06-23 19:50:15 +08001054/*
1055 * derive number of elements in scatterlist
1056 */
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001057static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001058{
1059 struct scatterlist *sg = sg_list;
1060 int sg_nents = 0;
1061
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001062 *chained = false;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001063 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001064 sg_nents++;
1065 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001066 if (!sg_is_last(sg) && (sg + 1)->length == 0)
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001067 *chained = true;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001068 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001069 }
1070
1071 return sg_nents;
1072}
1073
Lee Nipper497f2e62010-05-19 19:20:36 +10001074/**
1075 * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1076 * @sgl: The SG list
1077 * @nents: Number of SG entries
1078 * @buf: Where to copy to
1079 * @buflen: The number of bytes to copy
1080 * @skip: The number of bytes to skip before copying.
1081 * Note: skip + buflen should equal SG total size.
1082 *
1083 * Returns the number of copied bytes.
1084 *
1085 **/
1086static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1087 void *buf, size_t buflen, unsigned int skip)
1088{
1089 unsigned int offset = 0;
1090 unsigned int boffset = 0;
1091 struct sg_mapping_iter miter;
1092 unsigned long flags;
1093 unsigned int sg_flags = SG_MITER_ATOMIC;
1094 size_t total_buffer = buflen + skip;
1095
1096 sg_flags |= SG_MITER_FROM_SG;
1097
1098 sg_miter_start(&miter, sgl, nents, sg_flags);
1099
1100 local_irq_save(flags);
1101
1102 while (sg_miter_next(&miter) && offset < total_buffer) {
1103 unsigned int len;
1104 unsigned int ignore;
1105
1106 if ((offset + miter.length) > skip) {
1107 if (offset < skip) {
1108 /* Copy part of this segment */
1109 ignore = skip - offset;
1110 len = miter.length - ignore;
Lee Nipper72600422010-07-19 14:11:24 +08001111 if (boffset + len > buflen)
1112 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001113 memcpy(buf + boffset, miter.addr + ignore, len);
1114 } else {
Lee Nipper72600422010-07-19 14:11:24 +08001115 /* Copy all of this segment (up to buflen) */
Lee Nipper497f2e62010-05-19 19:20:36 +10001116 len = miter.length;
Lee Nipper72600422010-07-19 14:11:24 +08001117 if (boffset + len > buflen)
1118 len = buflen - boffset;
Lee Nipper497f2e62010-05-19 19:20:36 +10001119 memcpy(buf + boffset, miter.addr, len);
1120 }
1121 boffset += len;
1122 }
1123 offset += miter.length;
1124 }
1125
1126 sg_miter_stop(&miter);
1127
1128 local_irq_restore(flags);
1129 return boffset;
1130}
1131
Kim Phillips9c4a7962008-06-23 19:50:15 +08001132/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001133 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001134 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001135static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1136 struct scatterlist *src,
1137 struct scatterlist *dst,
1138 unsigned int cryptlen,
1139 unsigned int authsize,
1140 int icv_stashing,
1141 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001142{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001143 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001144 int src_nents, dst_nents, alloc_len, dma_len;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001145 bool src_chained, dst_chained = false;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001146 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001147 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001148
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001149 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1150 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001151 return ERR_PTR(-EINVAL);
1152 }
1153
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001154 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001155 src_nents = (src_nents == 1) ? 0 : src_nents;
1156
Horia Geanta602499a2012-08-02 17:16:38 +03001157 if (!dst) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001158 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001159 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001160 if (dst == src) {
1161 dst_nents = src_nents;
1162 } else {
1163 dst_nents = sg_count(dst, cryptlen + authsize,
1164 &dst_chained);
1165 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1166 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001167 }
1168
1169 /*
1170 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001171 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001172 * and the ICV data itself
1173 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001174 alloc_len = sizeof(struct talitos_edesc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001175 if (src_nents || dst_nents) {
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001176 dma_len = (src_nents + dst_nents + 2) *
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001177 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001178 alloc_len += dma_len;
1179 } else {
1180 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001181 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001182 }
1183
Kim Phillips586725f2008-07-17 20:19:18 +08001184 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001185 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001186 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001187 return ERR_PTR(-ENOMEM);
1188 }
1189
1190 edesc->src_nents = src_nents;
1191 edesc->dst_nents = dst_nents;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001192 edesc->src_chained = src_chained;
1193 edesc->dst_chained = dst_chained;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001194 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001195 if (dma_len)
1196 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1197 edesc->dma_len,
1198 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001199
1200 return edesc;
1201}
1202
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001203static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1204 int icv_stashing)
1205{
1206 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1207 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1208
Horia Geanta602499a2012-08-02 17:16:38 +03001209 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001210 areq->cryptlen, ctx->authsize, icv_stashing,
1211 areq->base.flags);
1212}
1213
Lee Nipper56af8cd2009-03-29 15:50:50 +08001214static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001215{
1216 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1217 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001218 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001219
1220 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001221 edesc = aead_edesc_alloc(req, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001222 if (IS_ERR(edesc))
1223 return PTR_ERR(edesc);
1224
1225 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001226 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001227
1228 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1229}
1230
Lee Nipper56af8cd2009-03-29 15:50:50 +08001231static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001232{
1233 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1234 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1235 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001236 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001237 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001238 struct scatterlist *sg;
1239 void *icvdata;
1240
1241 req->cryptlen -= authsize;
1242
1243 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001244 edesc = aead_edesc_alloc(req, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001245 if (IS_ERR(edesc))
1246 return PTR_ERR(edesc);
1247
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001248 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001249 ((!edesc->src_nents && !edesc->dst_nents) ||
1250 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001251
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001252 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001253 edesc->desc.hdr = ctx->desc_hdr_template |
1254 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001255 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001256
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001257 /* reset integrity check result bits */
1258 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001259
Kim Phillipse938e462009-03-29 15:53:23 +08001260 return ipsec_esp(edesc, req, NULL, 0,
1261 ipsec_esp_decrypt_hwauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001262
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001263 }
Kim Phillipse938e462009-03-29 15:53:23 +08001264
1265 /* Have to check the ICV with software */
1266 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1267
1268 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1269 if (edesc->dma_len)
1270 icvdata = &edesc->link_tbl[edesc->src_nents +
1271 edesc->dst_nents + 2];
1272 else
1273 icvdata = &edesc->link_tbl[0];
1274
1275 sg = sg_last(req->src, edesc->src_nents ? : 1);
1276
1277 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1278 ctx->authsize);
1279
1280 return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001281}
1282
Lee Nipper56af8cd2009-03-29 15:50:50 +08001283static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001284{
1285 struct aead_request *areq = &req->areq;
1286 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1287 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001288 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001289
1290 /* allocate extended descriptor */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001291 edesc = aead_edesc_alloc(areq, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001292 if (IS_ERR(edesc))
1293 return PTR_ERR(edesc);
1294
1295 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001296 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001297
1298 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001299 /* avoid consecutive packets going out with same IV */
1300 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001301
1302 return ipsec_esp(edesc, areq, req->giv, req->seq,
1303 ipsec_esp_encrypt_done);
1304}
1305
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001306static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1307 const u8 *key, unsigned int keylen)
1308{
1309 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001310
1311 memcpy(&ctx->key, key, keylen);
1312 ctx->keylen = keylen;
1313
1314 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001315}
1316
1317static void common_nonsnoop_unmap(struct device *dev,
1318 struct talitos_edesc *edesc,
1319 struct ablkcipher_request *areq)
1320{
1321 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1322 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1323 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1324
1325 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1326
1327 if (edesc->dma_len)
1328 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1329 DMA_BIDIRECTIONAL);
1330}
1331
1332static void ablkcipher_done(struct device *dev,
1333 struct talitos_desc *desc, void *context,
1334 int err)
1335{
1336 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001337 struct talitos_edesc *edesc;
1338
1339 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001340
1341 common_nonsnoop_unmap(dev, edesc, areq);
1342
1343 kfree(edesc);
1344
1345 areq->base.complete(&areq->base, err);
1346}
1347
1348static int common_nonsnoop(struct talitos_edesc *edesc,
1349 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001350 void (*callback) (struct device *dev,
1351 struct talitos_desc *desc,
1352 void *context, int error))
1353{
1354 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1355 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1356 struct device *dev = ctx->dev;
1357 struct talitos_desc *desc = &edesc->desc;
1358 unsigned int cryptlen = areq->nbytes;
1359 unsigned int ivsize;
1360 int sg_count, ret;
1361
1362 /* first DWORD empty */
1363 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001364 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001365 desc->ptr[0].j_extent = 0;
1366
1367 /* cipher iv */
1368 ivsize = crypto_ablkcipher_ivsize(cipher);
Kim Phillipsfebec542011-07-15 11:21:39 +08001369 map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001370 DMA_TO_DEVICE);
1371
1372 /* cipher key */
1373 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1374 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1375
1376 /*
1377 * cipher in
1378 */
1379 desc->ptr[3].len = cpu_to_be16(cryptlen);
1380 desc->ptr[3].j_extent = 0;
1381
1382 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1383 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1384 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001385 edesc->src_chained);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001386
1387 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001388 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001389 } else {
1390 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1391 &edesc->link_tbl[0]);
1392 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001393 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001394 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001395 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1396 edesc->dma_len,
1397 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001398 } else {
1399 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001400 to_talitos_ptr(&desc->ptr[3],
1401 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001402 }
1403 }
1404
1405 /* cipher out */
1406 desc->ptr[4].len = cpu_to_be16(cryptlen);
1407 desc->ptr[4].j_extent = 0;
1408
1409 if (areq->src != areq->dst)
1410 sg_count = talitos_map_sg(dev, areq->dst,
1411 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001412 DMA_FROM_DEVICE, edesc->dst_chained);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001413
1414 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001415 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001416 } else {
1417 struct talitos_ptr *link_tbl_ptr =
1418 &edesc->link_tbl[edesc->src_nents + 1];
1419
Kim Phillips81eb0242009-08-13 11:51:51 +10001420 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1421 (edesc->src_nents + 1) *
1422 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001423 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001424 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1425 link_tbl_ptr);
1426 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1427 edesc->dma_len, DMA_BIDIRECTIONAL);
1428 }
1429
1430 /* iv out */
1431 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1432 DMA_FROM_DEVICE);
1433
1434 /* last DWORD empty */
1435 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001436 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001437 desc->ptr[6].j_extent = 0;
1438
Kim Phillips5228f0f2011-07-15 11:21:38 +08001439 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001440 if (ret != -EINPROGRESS) {
1441 common_nonsnoop_unmap(dev, edesc, areq);
1442 kfree(edesc);
1443 }
1444 return ret;
1445}
1446
Kim Phillipse938e462009-03-29 15:53:23 +08001447static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1448 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001449{
1450 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1451 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1452
Horia Geanta602499a2012-08-02 17:16:38 +03001453 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, areq->nbytes,
1454 0, 0, areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001455}
1456
1457static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1458{
1459 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1460 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1461 struct talitos_edesc *edesc;
1462
1463 /* allocate extended descriptor */
1464 edesc = ablkcipher_edesc_alloc(areq);
1465 if (IS_ERR(edesc))
1466 return PTR_ERR(edesc);
1467
1468 /* set encrypt */
1469 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1470
Kim Phillipsfebec542011-07-15 11:21:39 +08001471 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001472}
1473
1474static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1475{
1476 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1477 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1478 struct talitos_edesc *edesc;
1479
1480 /* allocate extended descriptor */
1481 edesc = ablkcipher_edesc_alloc(areq);
1482 if (IS_ERR(edesc))
1483 return PTR_ERR(edesc);
1484
1485 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1486
Kim Phillipsfebec542011-07-15 11:21:39 +08001487 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001488}
1489
Lee Nipper497f2e62010-05-19 19:20:36 +10001490static void common_nonsnoop_hash_unmap(struct device *dev,
1491 struct talitos_edesc *edesc,
1492 struct ahash_request *areq)
1493{
1494 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1495
1496 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1497
1498 /* When using hashctx-in, must unmap it. */
1499 if (edesc->desc.ptr[1].len)
1500 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1501 DMA_TO_DEVICE);
1502
1503 if (edesc->desc.ptr[2].len)
1504 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1505 DMA_TO_DEVICE);
1506
1507 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1508
1509 if (edesc->dma_len)
1510 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1511 DMA_BIDIRECTIONAL);
1512
1513}
1514
1515static void ahash_done(struct device *dev,
1516 struct talitos_desc *desc, void *context,
1517 int err)
1518{
1519 struct ahash_request *areq = context;
1520 struct talitos_edesc *edesc =
1521 container_of(desc, struct talitos_edesc, desc);
1522 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1523
1524 if (!req_ctx->last && req_ctx->to_hash_later) {
1525 /* Position any partial block for next update/final/finup */
1526 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001527 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001528 }
1529 common_nonsnoop_hash_unmap(dev, edesc, areq);
1530
1531 kfree(edesc);
1532
1533 areq->base.complete(&areq->base, err);
1534}
1535
1536static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1537 struct ahash_request *areq, unsigned int length,
1538 void (*callback) (struct device *dev,
1539 struct talitos_desc *desc,
1540 void *context, int error))
1541{
1542 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1543 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1544 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1545 struct device *dev = ctx->dev;
1546 struct talitos_desc *desc = &edesc->desc;
1547 int sg_count, ret;
1548
1549 /* first DWORD empty */
1550 desc->ptr[0] = zero_entry;
1551
Kim Phillips60f208d2010-05-19 19:21:53 +10001552 /* hash context in */
1553 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001554 map_single_talitos_ptr(dev, &desc->ptr[1],
1555 req_ctx->hw_context_size,
1556 (char *)req_ctx->hw_context, 0,
1557 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001558 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001559 } else {
1560 desc->ptr[1] = zero_entry;
1561 /* Indicate next op is not the first. */
1562 req_ctx->first = 0;
1563 }
1564
1565 /* HMAC key */
1566 if (ctx->keylen)
1567 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1568 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1569 else
1570 desc->ptr[2] = zero_entry;
1571
1572 /*
1573 * data in
1574 */
1575 desc->ptr[3].len = cpu_to_be16(length);
1576 desc->ptr[3].j_extent = 0;
1577
1578 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1579 edesc->src_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001580 DMA_TO_DEVICE, edesc->src_chained);
Lee Nipper497f2e62010-05-19 19:20:36 +10001581
1582 if (sg_count == 1) {
1583 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1584 } else {
1585 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1586 &edesc->link_tbl[0]);
1587 if (sg_count > 1) {
1588 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1589 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1590 dma_sync_single_for_device(ctx->dev,
1591 edesc->dma_link_tbl,
1592 edesc->dma_len,
1593 DMA_BIDIRECTIONAL);
1594 } else {
1595 /* Only one segment now, so no link tbl needed */
1596 to_talitos_ptr(&desc->ptr[3],
1597 sg_dma_address(req_ctx->psrc));
1598 }
1599 }
1600
1601 /* fifth DWORD empty */
1602 desc->ptr[4] = zero_entry;
1603
1604 /* hash/HMAC out -or- hash context out */
1605 if (req_ctx->last)
1606 map_single_talitos_ptr(dev, &desc->ptr[5],
1607 crypto_ahash_digestsize(tfm),
1608 areq->result, 0, DMA_FROM_DEVICE);
1609 else
1610 map_single_talitos_ptr(dev, &desc->ptr[5],
1611 req_ctx->hw_context_size,
1612 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1613
1614 /* last DWORD empty */
1615 desc->ptr[6] = zero_entry;
1616
Kim Phillips5228f0f2011-07-15 11:21:38 +08001617 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001618 if (ret != -EINPROGRESS) {
1619 common_nonsnoop_hash_unmap(dev, edesc, areq);
1620 kfree(edesc);
1621 }
1622 return ret;
1623}
1624
1625static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1626 unsigned int nbytes)
1627{
1628 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1629 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1630 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1631
Horia Geanta602499a2012-08-02 17:16:38 +03001632 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, nbytes, 0, 0,
1633 areq->base.flags);
Lee Nipper497f2e62010-05-19 19:20:36 +10001634}
1635
1636static int ahash_init(struct ahash_request *areq)
1637{
1638 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1639 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1640
1641 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001642 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001643 req_ctx->first = 1; /* first indicates h/w must init its context */
1644 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001645 req_ctx->hw_context_size =
1646 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1647 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1648 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1649
1650 return 0;
1651}
1652
Kim Phillips60f208d2010-05-19 19:21:53 +10001653/*
1654 * on h/w without explicit sha224 support, we initialize h/w context
1655 * manually with sha224 constants, and tell it to run sha256.
1656 */
1657static int ahash_init_sha224_swinit(struct ahash_request *areq)
1658{
1659 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1660
1661 ahash_init(areq);
1662 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1663
Kim Phillipsa7524472010-09-23 15:56:38 +08001664 req_ctx->hw_context[0] = SHA224_H0;
1665 req_ctx->hw_context[1] = SHA224_H1;
1666 req_ctx->hw_context[2] = SHA224_H2;
1667 req_ctx->hw_context[3] = SHA224_H3;
1668 req_ctx->hw_context[4] = SHA224_H4;
1669 req_ctx->hw_context[5] = SHA224_H5;
1670 req_ctx->hw_context[6] = SHA224_H6;
1671 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001672
1673 /* init 64-bit count */
1674 req_ctx->hw_context[8] = 0;
1675 req_ctx->hw_context[9] = 0;
1676
1677 return 0;
1678}
1679
Lee Nipper497f2e62010-05-19 19:20:36 +10001680static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1681{
1682 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1683 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1684 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1685 struct talitos_edesc *edesc;
1686 unsigned int blocksize =
1687 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1688 unsigned int nbytes_to_hash;
1689 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001690 unsigned int nsg;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001691 bool chained;
Lee Nipper497f2e62010-05-19 19:20:36 +10001692
Lee Nipper5e833bc2010-06-16 15:29:15 +10001693 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1694 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001695 sg_copy_to_buffer(areq->src,
1696 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001697 req_ctx->buf + req_ctx->nbuf, nbytes);
1698 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001699 return 0;
1700 }
1701
Lee Nipper5e833bc2010-06-16 15:29:15 +10001702 /* At least (blocksize + 1) bytes are available to hash */
1703 nbytes_to_hash = nbytes + req_ctx->nbuf;
1704 to_hash_later = nbytes_to_hash & (blocksize - 1);
1705
1706 if (req_ctx->last)
1707 to_hash_later = 0;
1708 else if (to_hash_later)
1709 /* There is a partial block. Hash the full block(s) now */
1710 nbytes_to_hash -= to_hash_later;
1711 else {
1712 /* Keep one block buffered */
1713 nbytes_to_hash -= blocksize;
1714 to_hash_later = blocksize;
1715 }
1716
1717 /* Chain in any previously buffered data */
1718 if (req_ctx->nbuf) {
1719 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1720 sg_init_table(req_ctx->bufsl, nsg);
1721 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1722 if (nsg > 1)
1723 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001724 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001725 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001726 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001727
Lee Nipper5e833bc2010-06-16 15:29:15 +10001728 if (to_hash_later) {
1729 int nents = sg_count(areq->src, nbytes, &chained);
1730 sg_copy_end_to_buffer(areq->src, nents,
1731 req_ctx->bufnext,
1732 to_hash_later,
1733 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001734 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001735 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001736
Lee Nipper5e833bc2010-06-16 15:29:15 +10001737 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001738 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1739 if (IS_ERR(edesc))
1740 return PTR_ERR(edesc);
1741
1742 edesc->desc.hdr = ctx->desc_hdr_template;
1743
1744 /* On last one, request SEC to pad; otherwise continue */
1745 if (req_ctx->last)
1746 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1747 else
1748 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1749
Kim Phillips60f208d2010-05-19 19:21:53 +10001750 /* request SEC to INIT hash. */
1751 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001752 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1753
1754 /* When the tfm context has a keylen, it's an HMAC.
1755 * A first or last (ie. not middle) descriptor must request HMAC.
1756 */
1757 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1758 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1759
1760 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1761 ahash_done);
1762}
1763
1764static int ahash_update(struct ahash_request *areq)
1765{
1766 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1767
1768 req_ctx->last = 0;
1769
1770 return ahash_process_req(areq, areq->nbytes);
1771}
1772
1773static int ahash_final(struct ahash_request *areq)
1774{
1775 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1776
1777 req_ctx->last = 1;
1778
1779 return ahash_process_req(areq, 0);
1780}
1781
1782static int ahash_finup(struct ahash_request *areq)
1783{
1784 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1785
1786 req_ctx->last = 1;
1787
1788 return ahash_process_req(areq, areq->nbytes);
1789}
1790
1791static int ahash_digest(struct ahash_request *areq)
1792{
1793 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001794 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001795
Kim Phillips60f208d2010-05-19 19:21:53 +10001796 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001797 req_ctx->last = 1;
1798
1799 return ahash_process_req(areq, areq->nbytes);
1800}
1801
Lee Nipper79b3a412011-11-21 16:13:25 +08001802struct keyhash_result {
1803 struct completion completion;
1804 int err;
1805};
1806
1807static void keyhash_complete(struct crypto_async_request *req, int err)
1808{
1809 struct keyhash_result *res = req->data;
1810
1811 if (err == -EINPROGRESS)
1812 return;
1813
1814 res->err = err;
1815 complete(&res->completion);
1816}
1817
1818static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1819 u8 *hash)
1820{
1821 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1822
1823 struct scatterlist sg[1];
1824 struct ahash_request *req;
1825 struct keyhash_result hresult;
1826 int ret;
1827
1828 init_completion(&hresult.completion);
1829
1830 req = ahash_request_alloc(tfm, GFP_KERNEL);
1831 if (!req)
1832 return -ENOMEM;
1833
1834 /* Keep tfm keylen == 0 during hash of the long key */
1835 ctx->keylen = 0;
1836 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1837 keyhash_complete, &hresult);
1838
1839 sg_init_one(&sg[0], key, keylen);
1840
1841 ahash_request_set_crypt(req, sg, hash, keylen);
1842 ret = crypto_ahash_digest(req);
1843 switch (ret) {
1844 case 0:
1845 break;
1846 case -EINPROGRESS:
1847 case -EBUSY:
1848 ret = wait_for_completion_interruptible(
1849 &hresult.completion);
1850 if (!ret)
1851 ret = hresult.err;
1852 break;
1853 default:
1854 break;
1855 }
1856 ahash_request_free(req);
1857
1858 return ret;
1859}
1860
1861static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1862 unsigned int keylen)
1863{
1864 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1865 unsigned int blocksize =
1866 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1867 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1868 unsigned int keysize = keylen;
1869 u8 hash[SHA512_DIGEST_SIZE];
1870 int ret;
1871
1872 if (keylen <= blocksize)
1873 memcpy(ctx->key, key, keysize);
1874 else {
1875 /* Must get the hash of the long key */
1876 ret = keyhash(tfm, key, keylen, hash);
1877
1878 if (ret) {
1879 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1880 return -EINVAL;
1881 }
1882
1883 keysize = digestsize;
1884 memcpy(ctx->key, hash, digestsize);
1885 }
1886
1887 ctx->keylen = keysize;
1888
1889 return 0;
1890}
1891
1892
Kim Phillips9c4a7962008-06-23 19:50:15 +08001893struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001894 u32 type;
1895 union {
1896 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001897 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001898 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001899 __be32 desc_hdr_template;
1900};
1901
1902static struct talitos_alg_template driver_algs[] = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001903 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001904 { .type = CRYPTO_ALG_TYPE_AEAD,
1905 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001906 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1907 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1908 .cra_blocksize = AES_BLOCK_SIZE,
1909 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001910 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001911 .ivsize = AES_BLOCK_SIZE,
1912 .maxauthsize = SHA1_DIGEST_SIZE,
1913 }
1914 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001915 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1916 DESC_HDR_SEL0_AESU |
1917 DESC_HDR_MODE0_AESU_CBC |
1918 DESC_HDR_SEL1_MDEUA |
1919 DESC_HDR_MODE1_MDEU_INIT |
1920 DESC_HDR_MODE1_MDEU_PAD |
1921 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001922 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001923 { .type = CRYPTO_ALG_TYPE_AEAD,
1924 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001925 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1926 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1927 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1928 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001929 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001930 .ivsize = DES3_EDE_BLOCK_SIZE,
1931 .maxauthsize = SHA1_DIGEST_SIZE,
1932 }
1933 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001934 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1935 DESC_HDR_SEL0_DEU |
1936 DESC_HDR_MODE0_DEU_CBC |
1937 DESC_HDR_MODE0_DEU_3DES |
1938 DESC_HDR_SEL1_MDEUA |
1939 DESC_HDR_MODE1_MDEU_INIT |
1940 DESC_HDR_MODE1_MDEU_PAD |
1941 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001942 },
Horia Geanta357fb602012-07-03 19:16:53 +03001943 { .type = CRYPTO_ALG_TYPE_AEAD,
1944 .alg.crypto = {
1945 .cra_name = "authenc(hmac(sha224),cbc(aes))",
1946 .cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1947 .cra_blocksize = AES_BLOCK_SIZE,
1948 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001949 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001950 .ivsize = AES_BLOCK_SIZE,
1951 .maxauthsize = SHA224_DIGEST_SIZE,
1952 }
1953 },
1954 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1955 DESC_HDR_SEL0_AESU |
1956 DESC_HDR_MODE0_AESU_CBC |
1957 DESC_HDR_SEL1_MDEUA |
1958 DESC_HDR_MODE1_MDEU_INIT |
1959 DESC_HDR_MODE1_MDEU_PAD |
1960 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1961 },
1962 { .type = CRYPTO_ALG_TYPE_AEAD,
1963 .alg.crypto = {
1964 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1965 .cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1966 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1967 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001968 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001969 .ivsize = DES3_EDE_BLOCK_SIZE,
1970 .maxauthsize = SHA224_DIGEST_SIZE,
1971 }
1972 },
1973 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1974 DESC_HDR_SEL0_DEU |
1975 DESC_HDR_MODE0_DEU_CBC |
1976 DESC_HDR_MODE0_DEU_3DES |
1977 DESC_HDR_SEL1_MDEUA |
1978 DESC_HDR_MODE1_MDEU_INIT |
1979 DESC_HDR_MODE1_MDEU_PAD |
1980 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1981 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001982 { .type = CRYPTO_ALG_TYPE_AEAD,
1983 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001984 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1985 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1986 .cra_blocksize = AES_BLOCK_SIZE,
1987 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001988 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001989 .ivsize = AES_BLOCK_SIZE,
1990 .maxauthsize = SHA256_DIGEST_SIZE,
1991 }
1992 },
Lee Nipper3952f172008-07-10 18:29:18 +08001993 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1994 DESC_HDR_SEL0_AESU |
1995 DESC_HDR_MODE0_AESU_CBC |
1996 DESC_HDR_SEL1_MDEUA |
1997 DESC_HDR_MODE1_MDEU_INIT |
1998 DESC_HDR_MODE1_MDEU_PAD |
1999 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2000 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002001 { .type = CRYPTO_ALG_TYPE_AEAD,
2002 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002003 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2004 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2005 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2006 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002007 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002008 .ivsize = DES3_EDE_BLOCK_SIZE,
2009 .maxauthsize = SHA256_DIGEST_SIZE,
2010 }
2011 },
Lee Nipper3952f172008-07-10 18:29:18 +08002012 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2013 DESC_HDR_SEL0_DEU |
2014 DESC_HDR_MODE0_DEU_CBC |
2015 DESC_HDR_MODE0_DEU_3DES |
2016 DESC_HDR_SEL1_MDEUA |
2017 DESC_HDR_MODE1_MDEU_INIT |
2018 DESC_HDR_MODE1_MDEU_PAD |
2019 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2020 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002021 { .type = CRYPTO_ALG_TYPE_AEAD,
2022 .alg.crypto = {
Horia Geanta357fb602012-07-03 19:16:53 +03002023 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2024 .cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2025 .cra_blocksize = AES_BLOCK_SIZE,
2026 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002027 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002028 .ivsize = AES_BLOCK_SIZE,
2029 .maxauthsize = SHA384_DIGEST_SIZE,
2030 }
2031 },
2032 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2033 DESC_HDR_SEL0_AESU |
2034 DESC_HDR_MODE0_AESU_CBC |
2035 DESC_HDR_SEL1_MDEUB |
2036 DESC_HDR_MODE1_MDEU_INIT |
2037 DESC_HDR_MODE1_MDEU_PAD |
2038 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2039 },
2040 { .type = CRYPTO_ALG_TYPE_AEAD,
2041 .alg.crypto = {
2042 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2043 .cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2044 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2045 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002046 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002047 .ivsize = DES3_EDE_BLOCK_SIZE,
2048 .maxauthsize = SHA384_DIGEST_SIZE,
2049 }
2050 },
2051 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2052 DESC_HDR_SEL0_DEU |
2053 DESC_HDR_MODE0_DEU_CBC |
2054 DESC_HDR_MODE0_DEU_3DES |
2055 DESC_HDR_SEL1_MDEUB |
2056 DESC_HDR_MODE1_MDEU_INIT |
2057 DESC_HDR_MODE1_MDEU_PAD |
2058 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2059 },
2060 { .type = CRYPTO_ALG_TYPE_AEAD,
2061 .alg.crypto = {
2062 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2063 .cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2064 .cra_blocksize = AES_BLOCK_SIZE,
2065 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002066 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002067 .ivsize = AES_BLOCK_SIZE,
2068 .maxauthsize = SHA512_DIGEST_SIZE,
2069 }
2070 },
2071 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2072 DESC_HDR_SEL0_AESU |
2073 DESC_HDR_MODE0_AESU_CBC |
2074 DESC_HDR_SEL1_MDEUB |
2075 DESC_HDR_MODE1_MDEU_INIT |
2076 DESC_HDR_MODE1_MDEU_PAD |
2077 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2078 },
2079 { .type = CRYPTO_ALG_TYPE_AEAD,
2080 .alg.crypto = {
2081 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2082 .cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2083 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2084 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002085 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002086 .ivsize = DES3_EDE_BLOCK_SIZE,
2087 .maxauthsize = SHA512_DIGEST_SIZE,
2088 }
2089 },
2090 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2091 DESC_HDR_SEL0_DEU |
2092 DESC_HDR_MODE0_DEU_CBC |
2093 DESC_HDR_MODE0_DEU_3DES |
2094 DESC_HDR_SEL1_MDEUB |
2095 DESC_HDR_MODE1_MDEU_INIT |
2096 DESC_HDR_MODE1_MDEU_PAD |
2097 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2098 },
2099 { .type = CRYPTO_ALG_TYPE_AEAD,
2100 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002101 .cra_name = "authenc(hmac(md5),cbc(aes))",
2102 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2103 .cra_blocksize = AES_BLOCK_SIZE,
2104 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002105 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002106 .ivsize = AES_BLOCK_SIZE,
2107 .maxauthsize = MD5_DIGEST_SIZE,
2108 }
2109 },
Lee Nipper3952f172008-07-10 18:29:18 +08002110 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2111 DESC_HDR_SEL0_AESU |
2112 DESC_HDR_MODE0_AESU_CBC |
2113 DESC_HDR_SEL1_MDEUA |
2114 DESC_HDR_MODE1_MDEU_INIT |
2115 DESC_HDR_MODE1_MDEU_PAD |
2116 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2117 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002118 { .type = CRYPTO_ALG_TYPE_AEAD,
2119 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002120 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2121 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2122 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2123 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002124 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002125 .ivsize = DES3_EDE_BLOCK_SIZE,
2126 .maxauthsize = MD5_DIGEST_SIZE,
2127 }
2128 },
Lee Nipper3952f172008-07-10 18:29:18 +08002129 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2130 DESC_HDR_SEL0_DEU |
2131 DESC_HDR_MODE0_DEU_CBC |
2132 DESC_HDR_MODE0_DEU_3DES |
2133 DESC_HDR_SEL1_MDEUA |
2134 DESC_HDR_MODE1_MDEU_INIT |
2135 DESC_HDR_MODE1_MDEU_PAD |
2136 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002137 },
2138 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002139 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2140 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002141 .cra_name = "cbc(aes)",
2142 .cra_driver_name = "cbc-aes-talitos",
2143 .cra_blocksize = AES_BLOCK_SIZE,
2144 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2145 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002146 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002147 .min_keysize = AES_MIN_KEY_SIZE,
2148 .max_keysize = AES_MAX_KEY_SIZE,
2149 .ivsize = AES_BLOCK_SIZE,
2150 }
2151 },
2152 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2153 DESC_HDR_SEL0_AESU |
2154 DESC_HDR_MODE0_AESU_CBC,
2155 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002156 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2157 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002158 .cra_name = "cbc(des3_ede)",
2159 .cra_driver_name = "cbc-3des-talitos",
2160 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2161 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2162 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002163 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002164 .min_keysize = DES3_EDE_KEY_SIZE,
2165 .max_keysize = DES3_EDE_KEY_SIZE,
2166 .ivsize = DES3_EDE_BLOCK_SIZE,
2167 }
2168 },
2169 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2170 DESC_HDR_SEL0_DEU |
2171 DESC_HDR_MODE0_DEU_CBC |
2172 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002173 },
2174 /* AHASH algorithms. */
2175 { .type = CRYPTO_ALG_TYPE_AHASH,
2176 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002177 .halg.digestsize = MD5_DIGEST_SIZE,
2178 .halg.base = {
2179 .cra_name = "md5",
2180 .cra_driver_name = "md5-talitos",
2181 .cra_blocksize = MD5_BLOCK_SIZE,
2182 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2183 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002184 }
2185 },
2186 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2187 DESC_HDR_SEL0_MDEUA |
2188 DESC_HDR_MODE0_MDEU_MD5,
2189 },
2190 { .type = CRYPTO_ALG_TYPE_AHASH,
2191 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002192 .halg.digestsize = SHA1_DIGEST_SIZE,
2193 .halg.base = {
2194 .cra_name = "sha1",
2195 .cra_driver_name = "sha1-talitos",
2196 .cra_blocksize = SHA1_BLOCK_SIZE,
2197 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2198 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002199 }
2200 },
2201 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2202 DESC_HDR_SEL0_MDEUA |
2203 DESC_HDR_MODE0_MDEU_SHA1,
2204 },
2205 { .type = CRYPTO_ALG_TYPE_AHASH,
2206 .alg.hash = {
Kim Phillips60f208d2010-05-19 19:21:53 +10002207 .halg.digestsize = SHA224_DIGEST_SIZE,
2208 .halg.base = {
2209 .cra_name = "sha224",
2210 .cra_driver_name = "sha224-talitos",
2211 .cra_blocksize = SHA224_BLOCK_SIZE,
2212 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2213 CRYPTO_ALG_ASYNC,
Kim Phillips60f208d2010-05-19 19:21:53 +10002214 }
2215 },
2216 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2217 DESC_HDR_SEL0_MDEUA |
2218 DESC_HDR_MODE0_MDEU_SHA224,
2219 },
2220 { .type = CRYPTO_ALG_TYPE_AHASH,
2221 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002222 .halg.digestsize = SHA256_DIGEST_SIZE,
2223 .halg.base = {
2224 .cra_name = "sha256",
2225 .cra_driver_name = "sha256-talitos",
2226 .cra_blocksize = SHA256_BLOCK_SIZE,
2227 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2228 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002229 }
2230 },
2231 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2232 DESC_HDR_SEL0_MDEUA |
2233 DESC_HDR_MODE0_MDEU_SHA256,
2234 },
2235 { .type = CRYPTO_ALG_TYPE_AHASH,
2236 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002237 .halg.digestsize = SHA384_DIGEST_SIZE,
2238 .halg.base = {
2239 .cra_name = "sha384",
2240 .cra_driver_name = "sha384-talitos",
2241 .cra_blocksize = SHA384_BLOCK_SIZE,
2242 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2243 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002244 }
2245 },
2246 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2247 DESC_HDR_SEL0_MDEUB |
2248 DESC_HDR_MODE0_MDEUB_SHA384,
2249 },
2250 { .type = CRYPTO_ALG_TYPE_AHASH,
2251 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002252 .halg.digestsize = SHA512_DIGEST_SIZE,
2253 .halg.base = {
2254 .cra_name = "sha512",
2255 .cra_driver_name = "sha512-talitos",
2256 .cra_blocksize = SHA512_BLOCK_SIZE,
2257 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2258 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002259 }
2260 },
2261 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2262 DESC_HDR_SEL0_MDEUB |
2263 DESC_HDR_MODE0_MDEUB_SHA512,
2264 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002265 { .type = CRYPTO_ALG_TYPE_AHASH,
2266 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002267 .halg.digestsize = MD5_DIGEST_SIZE,
2268 .halg.base = {
2269 .cra_name = "hmac(md5)",
2270 .cra_driver_name = "hmac-md5-talitos",
2271 .cra_blocksize = MD5_BLOCK_SIZE,
2272 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2273 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002274 }
2275 },
2276 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2277 DESC_HDR_SEL0_MDEUA |
2278 DESC_HDR_MODE0_MDEU_MD5,
2279 },
2280 { .type = CRYPTO_ALG_TYPE_AHASH,
2281 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002282 .halg.digestsize = SHA1_DIGEST_SIZE,
2283 .halg.base = {
2284 .cra_name = "hmac(sha1)",
2285 .cra_driver_name = "hmac-sha1-talitos",
2286 .cra_blocksize = SHA1_BLOCK_SIZE,
2287 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2288 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002289 }
2290 },
2291 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2292 DESC_HDR_SEL0_MDEUA |
2293 DESC_HDR_MODE0_MDEU_SHA1,
2294 },
2295 { .type = CRYPTO_ALG_TYPE_AHASH,
2296 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002297 .halg.digestsize = SHA224_DIGEST_SIZE,
2298 .halg.base = {
2299 .cra_name = "hmac(sha224)",
2300 .cra_driver_name = "hmac-sha224-talitos",
2301 .cra_blocksize = SHA224_BLOCK_SIZE,
2302 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2303 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002304 }
2305 },
2306 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2307 DESC_HDR_SEL0_MDEUA |
2308 DESC_HDR_MODE0_MDEU_SHA224,
2309 },
2310 { .type = CRYPTO_ALG_TYPE_AHASH,
2311 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002312 .halg.digestsize = SHA256_DIGEST_SIZE,
2313 .halg.base = {
2314 .cra_name = "hmac(sha256)",
2315 .cra_driver_name = "hmac-sha256-talitos",
2316 .cra_blocksize = SHA256_BLOCK_SIZE,
2317 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2318 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002319 }
2320 },
2321 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2322 DESC_HDR_SEL0_MDEUA |
2323 DESC_HDR_MODE0_MDEU_SHA256,
2324 },
2325 { .type = CRYPTO_ALG_TYPE_AHASH,
2326 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002327 .halg.digestsize = SHA384_DIGEST_SIZE,
2328 .halg.base = {
2329 .cra_name = "hmac(sha384)",
2330 .cra_driver_name = "hmac-sha384-talitos",
2331 .cra_blocksize = SHA384_BLOCK_SIZE,
2332 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2333 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002334 }
2335 },
2336 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2337 DESC_HDR_SEL0_MDEUB |
2338 DESC_HDR_MODE0_MDEUB_SHA384,
2339 },
2340 { .type = CRYPTO_ALG_TYPE_AHASH,
2341 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002342 .halg.digestsize = SHA512_DIGEST_SIZE,
2343 .halg.base = {
2344 .cra_name = "hmac(sha512)",
2345 .cra_driver_name = "hmac-sha512-talitos",
2346 .cra_blocksize = SHA512_BLOCK_SIZE,
2347 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2348 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002349 }
2350 },
2351 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2352 DESC_HDR_SEL0_MDEUB |
2353 DESC_HDR_MODE0_MDEUB_SHA512,
2354 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002355};
2356
2357struct talitos_crypto_alg {
2358 struct list_head entry;
2359 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002360 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002361};
2362
2363static int talitos_cra_init(struct crypto_tfm *tfm)
2364{
2365 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002366 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002367 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002368 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002369
Lee Nipper497f2e62010-05-19 19:20:36 +10002370 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2371 talitos_alg = container_of(__crypto_ahash_alg(alg),
2372 struct talitos_crypto_alg,
2373 algt.alg.hash);
2374 else
2375 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2376 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002377
Kim Phillips9c4a7962008-06-23 19:50:15 +08002378 /* update context with ptr to dev */
2379 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002380
Kim Phillips5228f0f2011-07-15 11:21:38 +08002381 /* assign SEC channel to tfm in round-robin fashion */
2382 priv = dev_get_drvdata(ctx->dev);
2383 ctx->ch = atomic_inc_return(&priv->last_chan) &
2384 (priv->num_channels - 1);
2385
Kim Phillips9c4a7962008-06-23 19:50:15 +08002386 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002387 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002388
Kim Phillips602dba52011-07-15 11:21:39 +08002389 /* select done notification */
2390 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2391
Lee Nipper497f2e62010-05-19 19:20:36 +10002392 return 0;
2393}
2394
2395static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2396{
2397 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2398
2399 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002400
2401 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002402 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002403
2404 return 0;
2405}
2406
Lee Nipper497f2e62010-05-19 19:20:36 +10002407static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2408{
2409 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2410
2411 talitos_cra_init(tfm);
2412
2413 ctx->keylen = 0;
2414 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2415 sizeof(struct talitos_ahash_req_ctx));
2416
2417 return 0;
2418}
2419
Kim Phillips9c4a7962008-06-23 19:50:15 +08002420/*
2421 * given the alg's descriptor header template, determine whether descriptor
2422 * type and primary/secondary execution units required match the hw
2423 * capabilities description provided in the device tree node.
2424 */
2425static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2426{
2427 struct talitos_private *priv = dev_get_drvdata(dev);
2428 int ret;
2429
2430 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2431 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2432
2433 if (SECONDARY_EU(desc_hdr_template))
2434 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2435 & priv->exec_units);
2436
2437 return ret;
2438}
2439
Grant Likely2dc11582010-08-06 09:25:50 -06002440static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002441{
2442 struct device *dev = &ofdev->dev;
2443 struct talitos_private *priv = dev_get_drvdata(dev);
2444 struct talitos_crypto_alg *t_alg, *n;
2445 int i;
2446
2447 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002448 switch (t_alg->algt.type) {
2449 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2450 case CRYPTO_ALG_TYPE_AEAD:
2451 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2452 break;
2453 case CRYPTO_ALG_TYPE_AHASH:
2454 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2455 break;
2456 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002457 list_del(&t_alg->entry);
2458 kfree(t_alg);
2459 }
2460
2461 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2462 talitos_unregister_rng(dev);
2463
Kim Phillips4b9926282009-08-13 11:50:38 +10002464 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002465 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002466
Kim Phillips4b9926282009-08-13 11:50:38 +10002467 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002468
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002469 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002470 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002471 free_irq(priv->irq[i], dev);
2472 irq_dispose_mapping(priv->irq[i]);
2473 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002474
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002475 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002476 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002477 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002478
2479 iounmap(priv->reg);
2480
2481 dev_set_drvdata(dev, NULL);
2482
2483 kfree(priv);
2484
2485 return 0;
2486}
2487
2488static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2489 struct talitos_alg_template
2490 *template)
2491{
Kim Phillips60f208d2010-05-19 19:21:53 +10002492 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002493 struct talitos_crypto_alg *t_alg;
2494 struct crypto_alg *alg;
2495
2496 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2497 if (!t_alg)
2498 return ERR_PTR(-ENOMEM);
2499
Lee Nipperacbf7c622010-05-19 19:19:33 +10002500 t_alg->algt = *template;
2501
2502 switch (t_alg->algt.type) {
2503 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002504 alg = &t_alg->algt.alg.crypto;
2505 alg->cra_init = talitos_cra_init;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002506 alg->cra_type = &crypto_ablkcipher_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002507 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2508 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2509 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2510 alg->cra_ablkcipher.geniv = "eseqiv";
Lee Nipper497f2e62010-05-19 19:20:36 +10002511 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002512 case CRYPTO_ALG_TYPE_AEAD:
2513 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002514 alg->cra_init = talitos_cra_init_aead;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002515 alg->cra_type = &crypto_aead_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002516 alg->cra_aead.setkey = aead_setkey;
2517 alg->cra_aead.setauthsize = aead_setauthsize;
2518 alg->cra_aead.encrypt = aead_encrypt;
2519 alg->cra_aead.decrypt = aead_decrypt;
2520 alg->cra_aead.givencrypt = aead_givencrypt;
2521 alg->cra_aead.geniv = "<built-in>";
Lee Nipperacbf7c622010-05-19 19:19:33 +10002522 break;
2523 case CRYPTO_ALG_TYPE_AHASH:
2524 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002525 alg->cra_init = talitos_cra_init_ahash;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002526 alg->cra_type = &crypto_ahash_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002527 t_alg->algt.alg.hash.init = ahash_init;
2528 t_alg->algt.alg.hash.update = ahash_update;
2529 t_alg->algt.alg.hash.final = ahash_final;
2530 t_alg->algt.alg.hash.finup = ahash_finup;
2531 t_alg->algt.alg.hash.digest = ahash_digest;
2532 t_alg->algt.alg.hash.setkey = ahash_setkey;
2533
Lee Nipper79b3a412011-11-21 16:13:25 +08002534 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06002535 !strncmp(alg->cra_name, "hmac", 4)) {
2536 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002537 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002538 }
Kim Phillips60f208d2010-05-19 19:21:53 +10002539 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002540 (!strcmp(alg->cra_name, "sha224") ||
2541 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002542 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2543 t_alg->algt.desc_hdr_template =
2544 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2545 DESC_HDR_SEL0_MDEUA |
2546 DESC_HDR_MODE0_MDEU_SHA256;
2547 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002548 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002549 default:
2550 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2551 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002552 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002553
Kim Phillips9c4a7962008-06-23 19:50:15 +08002554 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002555 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002556 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002557 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002558 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002559
Kim Phillips9c4a7962008-06-23 19:50:15 +08002560 t_alg->dev = dev;
2561
2562 return t_alg;
2563}
2564
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002565static int talitos_probe_irq(struct platform_device *ofdev)
2566{
2567 struct device *dev = &ofdev->dev;
2568 struct device_node *np = ofdev->dev.of_node;
2569 struct talitos_private *priv = dev_get_drvdata(dev);
2570 int err;
2571
2572 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002573 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002574 dev_err(dev, "failed to map irq\n");
2575 return -EINVAL;
2576 }
2577
2578 priv->irq[1] = irq_of_parse_and_map(np, 1);
2579
2580 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002581 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002582 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2583 dev_driver_string(dev), dev);
2584 goto primary_out;
2585 }
2586
2587 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2588 dev_driver_string(dev), dev);
2589 if (err)
2590 goto primary_out;
2591
2592 /* get the secondary irq line */
2593 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2594 dev_driver_string(dev), dev);
2595 if (err) {
2596 dev_err(dev, "failed to request secondary irq\n");
2597 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002598 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002599 }
2600
2601 return err;
2602
2603primary_out:
2604 if (err) {
2605 dev_err(dev, "failed to request primary irq\n");
2606 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002607 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002608 }
2609
2610 return err;
2611}
2612
Grant Likely1c48a5c2011-02-17 02:43:24 -07002613static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002614{
2615 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002616 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002617 struct talitos_private *priv;
2618 const unsigned int *prop;
2619 int i, err;
2620
2621 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2622 if (!priv)
2623 return -ENOMEM;
2624
2625 dev_set_drvdata(dev, priv);
2626
2627 priv->ofdev = ofdev;
2628
Horia Geanta511d63c2012-03-30 17:49:53 +03002629 spin_lock_init(&priv->reg_lock);
2630
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002631 err = talitos_probe_irq(ofdev);
2632 if (err)
2633 goto err_out;
2634
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002635 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002636 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2637 (unsigned long)dev);
2638 } else {
2639 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2640 (unsigned long)dev);
2641 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2642 (unsigned long)dev);
2643 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002644
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002645 INIT_LIST_HEAD(&priv->alg_list);
2646
Kim Phillips9c4a7962008-06-23 19:50:15 +08002647 priv->reg = of_iomap(np, 0);
2648 if (!priv->reg) {
2649 dev_err(dev, "failed to of_iomap\n");
2650 err = -ENOMEM;
2651 goto err_out;
2652 }
2653
2654 /* get SEC version capabilities from device tree */
2655 prop = of_get_property(np, "fsl,num-channels", NULL);
2656 if (prop)
2657 priv->num_channels = *prop;
2658
2659 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2660 if (prop)
2661 priv->chfifo_len = *prop;
2662
2663 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2664 if (prop)
2665 priv->exec_units = *prop;
2666
2667 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2668 if (prop)
2669 priv->desc_types = *prop;
2670
2671 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2672 !priv->exec_units || !priv->desc_types) {
2673 dev_err(dev, "invalid property data in device tree node\n");
2674 err = -EINVAL;
2675 goto err_out;
2676 }
2677
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002678 if (of_device_is_compatible(np, "fsl,sec3.0"))
2679 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2680
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002681 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002682 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002683 TALITOS_FTR_SHA224_HWINIT |
2684 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002685
Kim Phillips4b9926282009-08-13 11:50:38 +10002686 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2687 priv->num_channels, GFP_KERNEL);
2688 if (!priv->chan) {
2689 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002690 err = -ENOMEM;
2691 goto err_out;
2692 }
2693
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002694 for (i = 0; i < priv->num_channels; i++) {
2695 priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002696 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002697 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2698 }
Kim Phillipsad42d5f2011-11-21 16:13:27 +08002699
Kim Phillips9c4a7962008-06-23 19:50:15 +08002700 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002701 spin_lock_init(&priv->chan[i].head_lock);
2702 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002703 }
2704
2705 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2706
2707 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002708 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2709 priv->fifo_len, GFP_KERNEL);
2710 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002711 dev_err(dev, "failed to allocate request fifo %d\n", i);
2712 err = -ENOMEM;
2713 goto err_out;
2714 }
2715 }
2716
Kim Phillipsec6644d2008-07-17 20:16:40 +08002717 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002718 atomic_set(&priv->chan[i].submit_count,
2719 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002720
Kim Phillips81eb0242009-08-13 11:51:51 +10002721 dma_set_mask(dev, DMA_BIT_MASK(36));
2722
Kim Phillips9c4a7962008-06-23 19:50:15 +08002723 /* reset and initialize the h/w */
2724 err = init_device(dev);
2725 if (err) {
2726 dev_err(dev, "failed to initialize device\n");
2727 goto err_out;
2728 }
2729
2730 /* register the RNG, if available */
2731 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2732 err = talitos_register_rng(dev);
2733 if (err) {
2734 dev_err(dev, "failed to register hwrng: %d\n", err);
2735 goto err_out;
2736 } else
2737 dev_info(dev, "hwrng\n");
2738 }
2739
2740 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002741 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2742 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2743 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002744 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002745
2746 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2747 if (IS_ERR(t_alg)) {
2748 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002749 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08002750 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002751 goto err_out;
2752 }
2753
Lee Nipperacbf7c622010-05-19 19:19:33 +10002754 switch (t_alg->algt.type) {
2755 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2756 case CRYPTO_ALG_TYPE_AEAD:
2757 err = crypto_register_alg(
2758 &t_alg->algt.alg.crypto);
2759 name = t_alg->algt.alg.crypto.cra_driver_name;
2760 break;
2761 case CRYPTO_ALG_TYPE_AHASH:
2762 err = crypto_register_ahash(
2763 &t_alg->algt.alg.hash);
2764 name =
2765 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2766 break;
2767 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002768 if (err) {
2769 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002770 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002771 kfree(t_alg);
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002772 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08002773 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002774 }
2775 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002776 if (!list_empty(&priv->alg_list))
2777 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2778 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002779
2780 return 0;
2781
2782err_out:
2783 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002784
2785 return err;
2786}
2787
Márton Németh6c3f9752010-01-17 21:54:01 +11002788static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002789 {
2790 .compatible = "fsl,sec2.0",
2791 },
2792 {},
2793};
2794MODULE_DEVICE_TABLE(of, talitos_match);
2795
Grant Likely1c48a5c2011-02-17 02:43:24 -07002796static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002797 .driver = {
2798 .name = "talitos",
2799 .owner = THIS_MODULE,
2800 .of_match_table = talitos_match,
2801 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002802 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002803 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002804};
2805
Axel Lin741e8c22011-11-26 21:26:19 +08002806module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002807
2808MODULE_LICENSE("GPL");
2809MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2810MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");