blob: 905de4427e7c4d1630604db45ffa2ba158983918 [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>
Rob Herring5af50732013-09-17 14:28:33 -050035#include <linux/of_address.h>
36#include <linux/of_irq.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080037#include <linux/of_platform.h>
38#include <linux/dma-mapping.h>
39#include <linux/io.h>
40#include <linux/spinlock.h>
41#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080043
44#include <crypto/algapi.h>
45#include <crypto/aes.h>
Lee Nipper3952f172008-07-10 18:29:18 +080046#include <crypto/des.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080047#include <crypto/sha.h>
Lee Nipper497f2e62010-05-19 19:20:36 +100048#include <crypto/md5.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080049#include <crypto/aead.h>
50#include <crypto/authenc.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080051#include <crypto/skcipher.h>
Lee Nipperacbf7c622010-05-19 19:19:33 +100052#include <crypto/hash.h>
53#include <crypto/internal/hash.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080054#include <crypto/scatterwalk.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080055
56#include "talitos.h"
57
Kim Phillips81eb0242009-08-13 11:51:51 +100058static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
59{
60 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
Kim Phillipsa7524472010-09-23 15:56:38 +080061 talitos_ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +100062}
63
Kim Phillips9c4a7962008-06-23 19:50:15 +080064/*
65 * map virtual single (contiguous) pointer to h/w descriptor pointer
66 */
67static void map_single_talitos_ptr(struct device *dev,
68 struct talitos_ptr *talitos_ptr,
69 unsigned short len, void *data,
70 unsigned char extent,
71 enum dma_data_direction dir)
72{
Kim Phillips81eb0242009-08-13 11:51:51 +100073 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
74
Kim Phillips9c4a7962008-06-23 19:50:15 +080075 talitos_ptr->len = cpu_to_be16(len);
Kim Phillips81eb0242009-08-13 11:51:51 +100076 to_talitos_ptr(talitos_ptr, dma_addr);
Kim Phillips9c4a7962008-06-23 19:50:15 +080077 talitos_ptr->j_extent = extent;
78}
79
80/*
81 * unmap bus single (contiguous) h/w descriptor pointer
82 */
83static void unmap_single_talitos_ptr(struct device *dev,
84 struct talitos_ptr *talitos_ptr,
85 enum dma_data_direction dir)
86{
87 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
88 be16_to_cpu(talitos_ptr->len), dir);
89}
90
91static int reset_channel(struct device *dev, int ch)
92{
93 struct talitos_private *priv = dev_get_drvdata(dev);
94 unsigned int timeout = TALITOS_TIMEOUT;
95
Kim Phillipsad42d5f2011-11-21 16:13:27 +080096 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +080097
Kim Phillipsad42d5f2011-11-21 16:13:27 +080098 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
Kim Phillips9c4a7962008-06-23 19:50:15 +080099 && --timeout)
100 cpu_relax();
101
102 if (timeout == 0) {
103 dev_err(dev, "failed to reset channel %d\n", ch);
104 return -EIO;
105 }
106
Kim Phillips81eb0242009-08-13 11:51:51 +1000107 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800108 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000109 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800110
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800111 /* and ICCR writeback, if available */
112 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800113 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800114 TALITOS_CCCR_LO_IWSE);
115
Kim Phillips9c4a7962008-06-23 19:50:15 +0800116 return 0;
117}
118
119static int reset_device(struct device *dev)
120{
121 struct talitos_private *priv = dev_get_drvdata(dev);
122 unsigned int timeout = TALITOS_TIMEOUT;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800123 u32 mcr = TALITOS_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800124
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800125 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800126
127 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
128 && --timeout)
129 cpu_relax();
130
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600131 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800132 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
133 setbits32(priv->reg + TALITOS_MCR, mcr);
134 }
135
Kim Phillips9c4a7962008-06-23 19:50:15 +0800136 if (timeout == 0) {
137 dev_err(dev, "failed to reset device\n");
138 return -EIO;
139 }
140
141 return 0;
142}
143
144/*
145 * Reset and initialize the device
146 */
147static int init_device(struct device *dev)
148{
149 struct talitos_private *priv = dev_get_drvdata(dev);
150 int ch, err;
151
152 /*
153 * Master reset
154 * errata documentation: warning: certain SEC interrupts
155 * are not fully cleared by writing the MCR:SWR bit,
156 * set bit twice to completely reset
157 */
158 err = reset_device(dev);
159 if (err)
160 return err;
161
162 err = reset_device(dev);
163 if (err)
164 return err;
165
166 /* reset channels */
167 for (ch = 0; ch < priv->num_channels; ch++) {
168 err = reset_channel(dev, ch);
169 if (err)
170 return err;
171 }
172
173 /* enable channel done and error interrupts */
174 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
175 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
176
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800177 /* disable integrity check error interrupts (use writeback instead) */
178 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
179 setbits32(priv->reg + TALITOS_MDEUICR_LO,
180 TALITOS_MDEUICR_LO_ICE);
181
Kim Phillips9c4a7962008-06-23 19:50:15 +0800182 return 0;
183}
184
185/**
186 * talitos_submit - submits a descriptor to the device for processing
187 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800188 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800189 * @desc: the descriptor to be processed by the device
190 * @callback: whom to call when processing is complete
191 * @context: a handle for use by caller (optional)
192 *
193 * desc must contain valid dma-mapped (bus physical) address pointers.
194 * callback must check err and feedback in descriptor header
195 * for device processing status.
196 */
Horia Geanta865d5062012-07-03 19:16:52 +0300197int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
198 void (*callback)(struct device *dev,
199 struct talitos_desc *desc,
200 void *context, int error),
201 void *context)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800202{
203 struct talitos_private *priv = dev_get_drvdata(dev);
204 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800205 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800206 int head;
207
Kim Phillips4b9926282009-08-13 11:50:38 +1000208 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800209
Kim Phillips4b9926282009-08-13 11:50:38 +1000210 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800211 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000212 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800213 return -EAGAIN;
214 }
215
Kim Phillips4b9926282009-08-13 11:50:38 +1000216 head = priv->chan[ch].head;
217 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800218
Kim Phillips9c4a7962008-06-23 19:50:15 +0800219 /* map descriptor and save caller data */
220 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
221 DMA_BIDIRECTIONAL);
222 request->callback = callback;
223 request->context = context;
224
225 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000226 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800227
228 smp_wmb();
229 request->desc = desc;
230
231 /* GO! */
232 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800233 out_be32(priv->chan[ch].reg + TALITOS_FF,
234 upper_32_bits(request->dma_desc));
235 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800236 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800237
Kim Phillips4b9926282009-08-13 11:50:38 +1000238 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800239
240 return -EINPROGRESS;
241}
Horia Geanta865d5062012-07-03 19:16:52 +0300242EXPORT_SYMBOL(talitos_submit);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800243
244/*
245 * process what was done, notify callback of error if not
246 */
247static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
248{
249 struct talitos_private *priv = dev_get_drvdata(dev);
250 struct talitos_request *request, saved_req;
251 unsigned long flags;
252 int tail, status;
253
Kim Phillips4b9926282009-08-13 11:50:38 +1000254 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800255
Kim Phillips4b9926282009-08-13 11:50:38 +1000256 tail = priv->chan[ch].tail;
257 while (priv->chan[ch].fifo[tail].desc) {
258 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800259
260 /* descriptors with their done bits set don't get the error */
261 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100262 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800263 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100264 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800265 if (!error)
266 break;
267 else
268 status = error;
269
270 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800271 sizeof(struct talitos_desc),
272 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800273
274 /* copy entries so we can call callback outside lock */
275 saved_req.desc = request->desc;
276 saved_req.callback = request->callback;
277 saved_req.context = request->context;
278
279 /* release request entry in fifo */
280 smp_wmb();
281 request->desc = NULL;
282
283 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000284 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800285
Kim Phillips4b9926282009-08-13 11:50:38 +1000286 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800287
Kim Phillips4b9926282009-08-13 11:50:38 +1000288 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800289
Kim Phillips9c4a7962008-06-23 19:50:15 +0800290 saved_req.callback(dev, saved_req.desc, saved_req.context,
291 status);
292 /* channel may resume processing in single desc error case */
293 if (error && !reset_ch && status == error)
294 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000295 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
296 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800297 }
298
Kim Phillips4b9926282009-08-13 11:50:38 +1000299 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800300}
301
302/*
303 * process completed requests for channels that have done status
304 */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800305#define DEF_TALITOS_DONE(name, ch_done_mask) \
306static void talitos_done_##name(unsigned long data) \
307{ \
308 struct device *dev = (struct device *)data; \
309 struct talitos_private *priv = dev_get_drvdata(dev); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300310 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800311 \
312 if (ch_done_mask & 1) \
313 flush_channel(dev, 0, 0, 0); \
314 if (priv->num_channels == 1) \
315 goto out; \
316 if (ch_done_mask & (1 << 2)) \
317 flush_channel(dev, 1, 0, 0); \
318 if (ch_done_mask & (1 << 4)) \
319 flush_channel(dev, 2, 0, 0); \
320 if (ch_done_mask & (1 << 6)) \
321 flush_channel(dev, 3, 0, 0); \
322 \
323out: \
324 /* At this point, all completed channels have been processed */ \
325 /* Unmask done interrupts for channels completed later on. */ \
Horia Geanta511d63c2012-03-30 17:49:53 +0300326 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800327 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
328 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300329 spin_unlock_irqrestore(&priv->reg_lock, flags); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800330}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800331DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
332DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
333DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800334
335/*
336 * locate current (offending) descriptor
337 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200338static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800339{
340 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips4b9926282009-08-13 11:50:38 +1000341 int tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800342 dma_addr_t cur_desc;
343
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800344 cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800345
Kim Phillips4b9926282009-08-13 11:50:38 +1000346 while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800347 tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips4b9926282009-08-13 11:50:38 +1000348 if (tail == priv->chan[ch].tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800349 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200350 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800351 }
352 }
353
Kim Phillips3e721ae2011-10-21 15:20:28 +0200354 return priv->chan[ch].fifo[tail].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800355}
356
357/*
358 * user diagnostics; report root cause of error based on execution unit status
359 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200360static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800361{
362 struct talitos_private *priv = dev_get_drvdata(dev);
363 int i;
364
Kim Phillips3e721ae2011-10-21 15:20:28 +0200365 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800366 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200367
368 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800369 case DESC_HDR_SEL0_AFEU:
370 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
371 in_be32(priv->reg + TALITOS_AFEUISR),
372 in_be32(priv->reg + TALITOS_AFEUISR_LO));
373 break;
374 case DESC_HDR_SEL0_DEU:
375 dev_err(dev, "DEUISR 0x%08x_%08x\n",
376 in_be32(priv->reg + TALITOS_DEUISR),
377 in_be32(priv->reg + TALITOS_DEUISR_LO));
378 break;
379 case DESC_HDR_SEL0_MDEUA:
380 case DESC_HDR_SEL0_MDEUB:
381 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
382 in_be32(priv->reg + TALITOS_MDEUISR),
383 in_be32(priv->reg + TALITOS_MDEUISR_LO));
384 break;
385 case DESC_HDR_SEL0_RNG:
386 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
387 in_be32(priv->reg + TALITOS_RNGUISR),
388 in_be32(priv->reg + TALITOS_RNGUISR_LO));
389 break;
390 case DESC_HDR_SEL0_PKEU:
391 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
392 in_be32(priv->reg + TALITOS_PKEUISR),
393 in_be32(priv->reg + TALITOS_PKEUISR_LO));
394 break;
395 case DESC_HDR_SEL0_AESU:
396 dev_err(dev, "AESUISR 0x%08x_%08x\n",
397 in_be32(priv->reg + TALITOS_AESUISR),
398 in_be32(priv->reg + TALITOS_AESUISR_LO));
399 break;
400 case DESC_HDR_SEL0_CRCU:
401 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
402 in_be32(priv->reg + TALITOS_CRCUISR),
403 in_be32(priv->reg + TALITOS_CRCUISR_LO));
404 break;
405 case DESC_HDR_SEL0_KEU:
406 dev_err(dev, "KEUISR 0x%08x_%08x\n",
407 in_be32(priv->reg + TALITOS_KEUISR),
408 in_be32(priv->reg + TALITOS_KEUISR_LO));
409 break;
410 }
411
Kim Phillips3e721ae2011-10-21 15:20:28 +0200412 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800413 case DESC_HDR_SEL1_MDEUA:
414 case DESC_HDR_SEL1_MDEUB:
415 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
416 in_be32(priv->reg + TALITOS_MDEUISR),
417 in_be32(priv->reg + TALITOS_MDEUISR_LO));
418 break;
419 case DESC_HDR_SEL1_CRCU:
420 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
421 in_be32(priv->reg + TALITOS_CRCUISR),
422 in_be32(priv->reg + TALITOS_CRCUISR_LO));
423 break;
424 }
425
426 for (i = 0; i < 8; i++)
427 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800428 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
429 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800430}
431
432/*
433 * recover from error interrupts
434 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600435static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800437 struct talitos_private *priv = dev_get_drvdata(dev);
438 unsigned int timeout = TALITOS_TIMEOUT;
439 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800440 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800441
442 for (ch = 0; ch < priv->num_channels; ch++) {
443 /* skip channels without errors */
444 if (!(isr & (1 << (ch * 2 + 1))))
445 continue;
446
447 error = -EINVAL;
448
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800449 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
450 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800451
452 if (v_lo & TALITOS_CCPSR_LO_DOF) {
453 dev_err(dev, "double fetch fifo overflow error\n");
454 error = -EAGAIN;
455 reset_ch = 1;
456 }
457 if (v_lo & TALITOS_CCPSR_LO_SOF) {
458 /* h/w dropped descriptor */
459 dev_err(dev, "single fetch fifo overflow error\n");
460 error = -EAGAIN;
461 }
462 if (v_lo & TALITOS_CCPSR_LO_MDTE)
463 dev_err(dev, "master data transfer error\n");
464 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
465 dev_err(dev, "s/g data length zero error\n");
466 if (v_lo & TALITOS_CCPSR_LO_FPZ)
467 dev_err(dev, "fetch pointer zero error\n");
468 if (v_lo & TALITOS_CCPSR_LO_IDH)
469 dev_err(dev, "illegal descriptor header error\n");
470 if (v_lo & TALITOS_CCPSR_LO_IEU)
471 dev_err(dev, "invalid execution unit error\n");
472 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200473 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800474 if (v_lo & TALITOS_CCPSR_LO_GB)
475 dev_err(dev, "gather boundary error\n");
476 if (v_lo & TALITOS_CCPSR_LO_GRL)
477 dev_err(dev, "gather return/length error\n");
478 if (v_lo & TALITOS_CCPSR_LO_SB)
479 dev_err(dev, "scatter boundary error\n");
480 if (v_lo & TALITOS_CCPSR_LO_SRL)
481 dev_err(dev, "scatter return/length error\n");
482
483 flush_channel(dev, ch, error, reset_ch);
484
485 if (reset_ch) {
486 reset_channel(dev, ch);
487 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800488 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800489 TALITOS_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800490 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
491 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800492 TALITOS_CCCR_CONT) && --timeout)
493 cpu_relax();
494 if (timeout == 0) {
495 dev_err(dev, "failed to restart channel %d\n",
496 ch);
497 reset_dev = 1;
498 }
499 }
500 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800501 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800502 dev_err(dev, "done overflow, internal time out, or rngu error: "
503 "ISR 0x%08x_%08x\n", isr, isr_lo);
504
505 /* purge request queues */
506 for (ch = 0; ch < priv->num_channels; ch++)
507 flush_channel(dev, ch, -EIO, 1);
508
509 /* reset and reinitialize the device */
510 init_device(dev);
511 }
512}
513
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800514#define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
515static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
516{ \
517 struct device *dev = data; \
518 struct talitos_private *priv = dev_get_drvdata(dev); \
519 u32 isr, isr_lo; \
Horia Geanta511d63c2012-03-30 17:49:53 +0300520 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800521 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300522 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800523 isr = in_be32(priv->reg + TALITOS_ISR); \
524 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
525 /* Acknowledge interrupt */ \
526 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
527 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
528 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300529 if (unlikely(isr & ch_err_mask || isr_lo)) { \
530 spin_unlock_irqrestore(&priv->reg_lock, flags); \
531 talitos_error(dev, isr & ch_err_mask, isr_lo); \
532 } \
533 else { \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800534 if (likely(isr & ch_done_mask)) { \
535 /* mask further done interrupts. */ \
536 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
537 /* done_task will unmask done interrupts at exit */ \
538 tasklet_schedule(&priv->done_task[tlet]); \
539 } \
Horia Geanta511d63c2012-03-30 17:49:53 +0300540 spin_unlock_irqrestore(&priv->reg_lock, flags); \
541 } \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800542 \
543 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
544 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800545}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800546DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
547DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
548DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800549
550/*
551 * hwrng
552 */
553static int talitos_rng_data_present(struct hwrng *rng, int wait)
554{
555 struct device *dev = (struct device *)rng->priv;
556 struct talitos_private *priv = dev_get_drvdata(dev);
557 u32 ofl;
558 int i;
559
560 for (i = 0; i < 20; i++) {
561 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
562 TALITOS_RNGUSR_LO_OFL;
563 if (ofl || !wait)
564 break;
565 udelay(10);
566 }
567
568 return !!ofl;
569}
570
571static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
572{
573 struct device *dev = (struct device *)rng->priv;
574 struct talitos_private *priv = dev_get_drvdata(dev);
575
576 /* rng fifo requires 64-bit accesses */
577 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
578 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
579
580 return sizeof(u32);
581}
582
583static int talitos_rng_init(struct hwrng *rng)
584{
585 struct device *dev = (struct device *)rng->priv;
586 struct talitos_private *priv = dev_get_drvdata(dev);
587 unsigned int timeout = TALITOS_TIMEOUT;
588
589 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
590 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
591 && --timeout)
592 cpu_relax();
593 if (timeout == 0) {
594 dev_err(dev, "failed to reset rng hw\n");
595 return -ENODEV;
596 }
597
598 /* start generating */
599 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
600
601 return 0;
602}
603
604static int talitos_register_rng(struct device *dev)
605{
606 struct talitos_private *priv = dev_get_drvdata(dev);
607
608 priv->rng.name = dev_driver_string(dev),
609 priv->rng.init = talitos_rng_init,
610 priv->rng.data_present = talitos_rng_data_present,
611 priv->rng.data_read = talitos_rng_data_read,
612 priv->rng.priv = (unsigned long)dev;
613
614 return hwrng_register(&priv->rng);
615}
616
617static void talitos_unregister_rng(struct device *dev)
618{
619 struct talitos_private *priv = dev_get_drvdata(dev);
620
621 hwrng_unregister(&priv->rng);
622}
623
624/*
625 * crypto alg
626 */
627#define TALITOS_CRA_PRIORITY 3000
Horia Geanta357fb602012-07-03 19:16:53 +0300628#define TALITOS_MAX_KEY_SIZE 96
Lee Nipper3952f172008-07-10 18:29:18 +0800629#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800630
Lee Nipper497f2e62010-05-19 19:20:36 +1000631#define MD5_BLOCK_SIZE 64
Kim Phillips9c4a7962008-06-23 19:50:15 +0800632
633struct talitos_ctx {
634 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800635 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800636 __be32 desc_hdr_template;
637 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800638 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800639 unsigned int keylen;
640 unsigned int enckeylen;
641 unsigned int authkeylen;
642 unsigned int authsize;
643};
644
Lee Nipper497f2e62010-05-19 19:20:36 +1000645#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
646#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
647
648struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000649 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000650 unsigned int hw_context_size;
651 u8 buf[HASH_MAX_BLOCK_SIZE];
652 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000653 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000654 unsigned int first;
655 unsigned int last;
656 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000657 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000658 struct scatterlist bufsl[2];
659 struct scatterlist *psrc;
660};
661
Lee Nipper56af8cd2009-03-29 15:50:50 +0800662static int aead_setauthsize(struct crypto_aead *authenc,
663 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800664{
665 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
666
667 ctx->authsize = authsize;
668
669 return 0;
670}
671
Lee Nipper56af8cd2009-03-29 15:50:50 +0800672static int aead_setkey(struct crypto_aead *authenc,
673 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800674{
675 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Mathias Krausec306a982013-10-15 13:49:34 +0200676 struct crypto_authenc_keys keys;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800677
Mathias Krausec306a982013-10-15 13:49:34 +0200678 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800679 goto badkey;
680
Mathias Krausec306a982013-10-15 13:49:34 +0200681 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800682 goto badkey;
683
Mathias Krausec306a982013-10-15 13:49:34 +0200684 memcpy(ctx->key, keys.authkey, keys.authkeylen);
685 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800686
Mathias Krausec306a982013-10-15 13:49:34 +0200687 ctx->keylen = keys.authkeylen + keys.enckeylen;
688 ctx->enckeylen = keys.enckeylen;
689 ctx->authkeylen = keys.authkeylen;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800690
691 return 0;
692
693badkey:
694 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
695 return -EINVAL;
696}
697
698/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800699 * talitos_edesc - s/w-extended descriptor
Horia Geanta79fd31d2012-08-02 17:16:40 +0300700 * @assoc_nents: number of segments in associated data scatterlist
Kim Phillips9c4a7962008-06-23 19:50:15 +0800701 * @src_nents: number of segments in input scatterlist
702 * @dst_nents: number of segments in output scatterlist
Horia Geanta79fd31d2012-08-02 17:16:40 +0300703 * @assoc_chained: whether assoc is chained or not
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300704 * @src_chained: whether src is chained or not
705 * @dst_chained: whether dst is chained or not
Horia Geanta79fd31d2012-08-02 17:16:40 +0300706 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips9c4a7962008-06-23 19:50:15 +0800707 * @dma_len: length of dma mapped link_tbl space
708 * @dma_link_tbl: bus physical address of link_tbl
709 * @desc: h/w descriptor
710 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
711 *
712 * if decrypting (with authcheck), or either one of src_nents or dst_nents
713 * is greater than 1, an integrity check value is concatenated to the end
714 * of link_tbl data
715 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800716struct talitos_edesc {
Horia Geanta79fd31d2012-08-02 17:16:40 +0300717 int assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800718 int src_nents;
719 int dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300720 bool assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300721 bool src_chained;
722 bool dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300723 dma_addr_t iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800724 int dma_len;
725 dma_addr_t dma_link_tbl;
726 struct talitos_desc desc;
727 struct talitos_ptr link_tbl[0];
728};
729
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800730static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
731 unsigned int nents, enum dma_data_direction dir,
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300732 bool chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800733{
734 if (unlikely(chained))
735 while (sg) {
736 dma_map_sg(dev, sg, 1, dir);
737 sg = scatterwalk_sg_next(sg);
738 }
739 else
740 dma_map_sg(dev, sg, nents, dir);
741 return nents;
742}
743
744static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
745 enum dma_data_direction dir)
746{
747 while (sg) {
748 dma_unmap_sg(dev, sg, 1, dir);
749 sg = scatterwalk_sg_next(sg);
750 }
751}
752
753static void talitos_sg_unmap(struct device *dev,
754 struct talitos_edesc *edesc,
755 struct scatterlist *src,
756 struct scatterlist *dst)
757{
758 unsigned int src_nents = edesc->src_nents ? : 1;
759 unsigned int dst_nents = edesc->dst_nents ? : 1;
760
761 if (src != dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300762 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800763 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
764 else
765 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
766
Lee Nipper497f2e62010-05-19 19:20:36 +1000767 if (dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300768 if (edesc->dst_chained)
Lee Nipper497f2e62010-05-19 19:20:36 +1000769 talitos_unmap_sg_chain(dev, dst,
770 DMA_FROM_DEVICE);
771 else
772 dma_unmap_sg(dev, dst, dst_nents,
773 DMA_FROM_DEVICE);
774 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800775 } else
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300776 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800777 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
778 else
779 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
780}
781
Kim Phillips9c4a7962008-06-23 19:50:15 +0800782static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800783 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800784 struct aead_request *areq)
785{
786 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
787 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
788 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
789 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
790
Horia Geanta79fd31d2012-08-02 17:16:40 +0300791 if (edesc->assoc_chained)
792 talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
793 else
794 /* assoc_nents counts also for IV in non-contiguous cases */
795 dma_unmap_sg(dev, areq->assoc,
796 edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
797 DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800798
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800799 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800800
801 if (edesc->dma_len)
802 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
803 DMA_BIDIRECTIONAL);
804}
805
806/*
807 * ipsec_esp descriptor callbacks
808 */
809static void ipsec_esp_encrypt_done(struct device *dev,
810 struct talitos_desc *desc, void *context,
811 int err)
812{
813 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800814 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
815 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800816 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800817 struct scatterlist *sg;
818 void *icvdata;
819
Kim Phillips19bbbc62009-03-29 15:53:59 +0800820 edesc = container_of(desc, struct talitos_edesc, desc);
821
Kim Phillips9c4a7962008-06-23 19:50:15 +0800822 ipsec_esp_unmap(dev, edesc, areq);
823
824 /* copy the generated ICV to dst */
Horia Geanta60542502012-08-02 17:16:37 +0300825 if (edesc->dst_nents) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800826 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300827 edesc->dst_nents + 2 +
828 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800829 sg = sg_last(areq->dst, edesc->dst_nents);
830 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
831 icvdata, ctx->authsize);
832 }
833
834 kfree(edesc);
835
836 aead_request_complete(areq, err);
837}
838
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800839static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800840 struct talitos_desc *desc,
841 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800842{
843 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800844 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
845 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800846 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800847 struct scatterlist *sg;
848 void *icvdata;
849
Kim Phillips19bbbc62009-03-29 15:53:59 +0800850 edesc = container_of(desc, struct talitos_edesc, desc);
851
Kim Phillips9c4a7962008-06-23 19:50:15 +0800852 ipsec_esp_unmap(dev, edesc, req);
853
854 if (!err) {
855 /* auth check */
856 if (edesc->dma_len)
857 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300858 edesc->dst_nents + 2 +
859 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800860 else
861 icvdata = &edesc->link_tbl[0];
862
863 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
864 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
865 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
866 }
867
868 kfree(edesc);
869
870 aead_request_complete(req, err);
871}
872
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800873static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800874 struct talitos_desc *desc,
875 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800876{
877 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800878 struct talitos_edesc *edesc;
879
880 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800881
882 ipsec_esp_unmap(dev, edesc, req);
883
884 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800885 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
886 DESC_HDR_LO_ICCR1_PASS))
887 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800888
889 kfree(edesc);
890
891 aead_request_complete(req, err);
892}
893
Kim Phillips9c4a7962008-06-23 19:50:15 +0800894/*
895 * convert scatterlist to SEC h/w link table format
896 * stop at cryptlen bytes
897 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800898static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800899 int cryptlen, struct talitos_ptr *link_tbl_ptr)
900{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800901 int n_sg = sg_count;
902
903 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000904 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800905 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
906 link_tbl_ptr->j_extent = 0;
907 link_tbl_ptr++;
908 cryptlen -= sg_dma_len(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800909 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800910 }
911
Lee Nipper70bcaca2008-07-03 19:08:46 +0800912 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800913 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800914 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800915 /* Empty this entry, and move to previous one */
916 cryptlen += be16_to_cpu(link_tbl_ptr->len);
917 link_tbl_ptr->len = 0;
918 sg_count--;
919 link_tbl_ptr--;
920 }
Wei Yongjun7291a932012-09-28 12:52:25 +0800921 be16_add_cpu(&link_tbl_ptr->len, cryptlen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800922
923 /* tag end of link table */
924 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +0800925
926 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800927}
928
929/*
930 * fill in and submit ipsec_esp descriptor
931 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800932static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Horia Geanta79fd31d2012-08-02 17:16:40 +0300933 u64 seq, void (*callback) (struct device *dev,
934 struct talitos_desc *desc,
935 void *context, int error))
Kim Phillips9c4a7962008-06-23 19:50:15 +0800936{
937 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
938 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
939 struct device *dev = ctx->dev;
940 struct talitos_desc *desc = &edesc->desc;
941 unsigned int cryptlen = areq->cryptlen;
942 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +1000943 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +0800944 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800945 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800946
947 /* hmac key */
948 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
949 0, DMA_TO_DEVICE);
Horia Geanta79fd31d2012-08-02 17:16:40 +0300950
Kim Phillips9c4a7962008-06-23 19:50:15 +0800951 /* hmac data */
Horia Geanta79fd31d2012-08-02 17:16:40 +0300952 desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
953 if (edesc->assoc_nents) {
954 int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
955 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
956
957 to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
958 sizeof(struct talitos_ptr));
959 desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
960
961 /* assoc_nents - 1 entries for assoc, 1 for IV */
962 sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
963 areq->assoclen, tbl_ptr);
964
965 /* add IV to link table */
966 tbl_ptr += sg_count - 1;
967 tbl_ptr->j_extent = 0;
968 tbl_ptr++;
969 to_talitos_ptr(tbl_ptr, edesc->iv_dma);
970 tbl_ptr->len = cpu_to_be16(ivsize);
971 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
972
973 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
974 edesc->dma_len, DMA_BIDIRECTIONAL);
975 } else {
976 to_talitos_ptr(&desc->ptr[1], sg_dma_address(areq->assoc));
977 desc->ptr[1].j_extent = 0;
978 }
979
Kim Phillips9c4a7962008-06-23 19:50:15 +0800980 /* cipher iv */
Horia Geanta79fd31d2012-08-02 17:16:40 +0300981 to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
982 desc->ptr[2].len = cpu_to_be16(ivsize);
983 desc->ptr[2].j_extent = 0;
984 /* Sync needed for the aead_givencrypt case */
985 dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800986
987 /* cipher key */
988 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
989 (char *)&ctx->key + ctx->authkeylen, 0,
990 DMA_TO_DEVICE);
991
992 /*
993 * cipher in
994 * map and adjust cipher len to aead request cryptlen.
995 * extent is bytes of HMAC postpended to ciphertext,
996 * typically 12 for ipsec
997 */
998 desc->ptr[4].len = cpu_to_be16(cryptlen);
999 desc->ptr[4].j_extent = authsize;
1000
Kim Phillipse938e462009-03-29 15:53:23 +08001001 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1002 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1003 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001004 edesc->src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001005
1006 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001007 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001008 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001009 sg_link_tbl_len = cryptlen;
1010
Kim Phillips962a9c92009-03-29 15:54:30 +08001011 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001012 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001013
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001014 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001015 &edesc->link_tbl[0]);
1016 if (sg_count > 1) {
1017 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001018 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001019 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1020 edesc->dma_len,
1021 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001022 } else {
1023 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001024 to_talitos_ptr(&desc->ptr[4],
1025 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001026 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001027 }
1028
1029 /* cipher out */
1030 desc->ptr[5].len = cpu_to_be16(cryptlen);
1031 desc->ptr[5].j_extent = authsize;
1032
Kim Phillipse938e462009-03-29 15:53:23 +08001033 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001034 sg_count = talitos_map_sg(dev, areq->dst,
1035 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001036 DMA_FROM_DEVICE, edesc->dst_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001037
1038 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001039 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001040 } else {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001041 int tbl_off = edesc->src_nents + 1;
1042 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001043
Kim Phillips81eb0242009-08-13 11:51:51 +10001044 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
Horia Geanta79fd31d2012-08-02 17:16:40 +03001045 tbl_off * sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001046 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001047 tbl_ptr);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001048
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001049 /* Add an entry to the link table for ICV data */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001050 tbl_ptr += sg_count - 1;
1051 tbl_ptr->j_extent = 0;
1052 tbl_ptr++;
1053 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1054 tbl_ptr->len = cpu_to_be16(authsize);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001055
1056 /* icv data follows link tables */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001057 to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1058 (tbl_off + edesc->dst_nents + 1 +
1059 edesc->assoc_nents) *
Kim Phillips81eb0242009-08-13 11:51:51 +10001060 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001061 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1062 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1063 edesc->dma_len, DMA_BIDIRECTIONAL);
1064 }
1065
1066 /* iv out */
1067 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1068 DMA_FROM_DEVICE);
1069
Kim Phillips5228f0f2011-07-15 11:21:38 +08001070 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001071 if (ret != -EINPROGRESS) {
1072 ipsec_esp_unmap(dev, edesc, areq);
1073 kfree(edesc);
1074 }
1075 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001076}
1077
Kim Phillips9c4a7962008-06-23 19:50:15 +08001078/*
1079 * derive number of elements in scatterlist
1080 */
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001081static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001082{
1083 struct scatterlist *sg = sg_list;
1084 int sg_nents = 0;
1085
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001086 *chained = false;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001087 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001088 sg_nents++;
1089 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001090 if (!sg_is_last(sg) && (sg + 1)->length == 0)
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001091 *chained = true;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001092 sg = scatterwalk_sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001093 }
1094
1095 return sg_nents;
1096}
1097
1098/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001099 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001100 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001101static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001102 struct scatterlist *assoc,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001103 struct scatterlist *src,
1104 struct scatterlist *dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001105 u8 *iv,
1106 unsigned int assoclen,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001107 unsigned int cryptlen,
1108 unsigned int authsize,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001109 unsigned int ivsize,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001110 int icv_stashing,
1111 u32 cryptoflags)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001112{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001113 struct talitos_edesc *edesc;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001114 int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1115 bool assoc_chained = false, src_chained = false, dst_chained = false;
1116 dma_addr_t iv_dma = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001117 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001118 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001119
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001120 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1121 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001122 return ERR_PTR(-EINVAL);
1123 }
1124
Horia Geanta79fd31d2012-08-02 17:16:40 +03001125 if (iv)
1126 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1127
1128 if (assoc) {
1129 /*
1130 * Currently it is assumed that iv is provided whenever assoc
1131 * is.
1132 */
1133 BUG_ON(!iv);
1134
1135 assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1136 talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1137 assoc_chained);
1138 assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1139
1140 if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1141 assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1142 }
1143
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001144 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001145 src_nents = (src_nents == 1) ? 0 : src_nents;
1146
Horia Geanta602499a2012-08-02 17:16:38 +03001147 if (!dst) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001148 dst_nents = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001149 } else {
Lee Nipper497f2e62010-05-19 19:20:36 +10001150 if (dst == src) {
1151 dst_nents = src_nents;
1152 } else {
1153 dst_nents = sg_count(dst, cryptlen + authsize,
1154 &dst_chained);
1155 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1156 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001157 }
1158
1159 /*
1160 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001161 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001162 * and the ICV data itself
1163 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001164 alloc_len = sizeof(struct talitos_edesc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001165 if (assoc_nents || src_nents || dst_nents) {
1166 dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
1167 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001168 alloc_len += dma_len;
1169 } else {
1170 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001171 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001172 }
1173
Kim Phillips586725f2008-07-17 20:19:18 +08001174 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001175 if (!edesc) {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001176 talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1177 if (iv_dma)
1178 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001179 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001180 return ERR_PTR(-ENOMEM);
1181 }
1182
Horia Geanta79fd31d2012-08-02 17:16:40 +03001183 edesc->assoc_nents = assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001184 edesc->src_nents = src_nents;
1185 edesc->dst_nents = dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001186 edesc->assoc_chained = assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001187 edesc->src_chained = src_chained;
1188 edesc->dst_chained = dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001189 edesc->iv_dma = iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001190 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001191 if (dma_len)
1192 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1193 edesc->dma_len,
1194 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001195
1196 return edesc;
1197}
1198
Horia Geanta79fd31d2012-08-02 17:16:40 +03001199static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001200 int icv_stashing)
1201{
1202 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1203 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001204 unsigned int ivsize = crypto_aead_ivsize(authenc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001205
Horia Geanta79fd31d2012-08-02 17:16:40 +03001206 return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1207 iv, areq->assoclen, areq->cryptlen,
1208 ctx->authsize, ivsize, icv_stashing,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001209 areq->base.flags);
1210}
1211
Lee Nipper56af8cd2009-03-29 15:50:50 +08001212static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001213{
1214 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1215 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001216 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001217
1218 /* allocate extended descriptor */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001219 edesc = aead_edesc_alloc(req, req->iv, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001220 if (IS_ERR(edesc))
1221 return PTR_ERR(edesc);
1222
1223 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001224 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001225
Horia Geanta79fd31d2012-08-02 17:16:40 +03001226 return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001227}
1228
Lee Nipper56af8cd2009-03-29 15:50:50 +08001229static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001230{
1231 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1232 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1233 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001234 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001235 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001236 struct scatterlist *sg;
1237 void *icvdata;
1238
1239 req->cryptlen -= authsize;
1240
1241 /* allocate extended descriptor */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001242 edesc = aead_edesc_alloc(req, req->iv, 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001243 if (IS_ERR(edesc))
1244 return PTR_ERR(edesc);
1245
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001246 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001247 ((!edesc->src_nents && !edesc->dst_nents) ||
1248 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001250 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001251 edesc->desc.hdr = ctx->desc_hdr_template |
1252 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001253 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001254
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001255 /* reset integrity check result bits */
1256 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001257
Horia Geanta79fd31d2012-08-02 17:16:40 +03001258 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001259 }
Kim Phillipse938e462009-03-29 15:53:23 +08001260
1261 /* Have to check the ICV with software */
1262 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1263
1264 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1265 if (edesc->dma_len)
1266 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +03001267 edesc->dst_nents + 2 +
1268 edesc->assoc_nents];
Kim Phillipse938e462009-03-29 15:53:23 +08001269 else
1270 icvdata = &edesc->link_tbl[0];
1271
1272 sg = sg_last(req->src, edesc->src_nents ? : 1);
1273
1274 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1275 ctx->authsize);
1276
Horia Geanta79fd31d2012-08-02 17:16:40 +03001277 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001278}
1279
Lee Nipper56af8cd2009-03-29 15:50:50 +08001280static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001281{
1282 struct aead_request *areq = &req->areq;
1283 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1284 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001285 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001286
1287 /* allocate extended descriptor */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001288 edesc = aead_edesc_alloc(areq, req->giv, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001289 if (IS_ERR(edesc))
1290 return PTR_ERR(edesc);
1291
1292 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001293 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001294
1295 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001296 /* avoid consecutive packets going out with same IV */
1297 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001298
Horia Geanta79fd31d2012-08-02 17:16:40 +03001299 return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001300}
1301
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001302static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1303 const u8 *key, unsigned int keylen)
1304{
1305 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001306
1307 memcpy(&ctx->key, key, keylen);
1308 ctx->keylen = keylen;
1309
1310 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001311}
1312
1313static void common_nonsnoop_unmap(struct device *dev,
1314 struct talitos_edesc *edesc,
1315 struct ablkcipher_request *areq)
1316{
1317 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1318 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1319 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1320
1321 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1322
1323 if (edesc->dma_len)
1324 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1325 DMA_BIDIRECTIONAL);
1326}
1327
1328static void ablkcipher_done(struct device *dev,
1329 struct talitos_desc *desc, void *context,
1330 int err)
1331{
1332 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001333 struct talitos_edesc *edesc;
1334
1335 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001336
1337 common_nonsnoop_unmap(dev, edesc, areq);
1338
1339 kfree(edesc);
1340
1341 areq->base.complete(&areq->base, err);
1342}
1343
1344static int common_nonsnoop(struct talitos_edesc *edesc,
1345 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001346 void (*callback) (struct device *dev,
1347 struct talitos_desc *desc,
1348 void *context, int error))
1349{
1350 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1351 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1352 struct device *dev = ctx->dev;
1353 struct talitos_desc *desc = &edesc->desc;
1354 unsigned int cryptlen = areq->nbytes;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001355 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001356 int sg_count, ret;
1357
1358 /* first DWORD empty */
1359 desc->ptr[0].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001360 to_talitos_ptr(&desc->ptr[0], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001361 desc->ptr[0].j_extent = 0;
1362
1363 /* cipher iv */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001364 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
1365 desc->ptr[1].len = cpu_to_be16(ivsize);
1366 desc->ptr[1].j_extent = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001367
1368 /* cipher key */
1369 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1370 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1371
1372 /*
1373 * cipher in
1374 */
1375 desc->ptr[3].len = cpu_to_be16(cryptlen);
1376 desc->ptr[3].j_extent = 0;
1377
1378 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1379 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1380 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001381 edesc->src_chained);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001382
1383 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001384 to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001385 } else {
1386 sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1387 &edesc->link_tbl[0]);
1388 if (sg_count > 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001389 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001390 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillipse938e462009-03-29 15:53:23 +08001391 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1392 edesc->dma_len,
1393 DMA_BIDIRECTIONAL);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001394 } else {
1395 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001396 to_talitos_ptr(&desc->ptr[3],
1397 sg_dma_address(areq->src));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001398 }
1399 }
1400
1401 /* cipher out */
1402 desc->ptr[4].len = cpu_to_be16(cryptlen);
1403 desc->ptr[4].j_extent = 0;
1404
1405 if (areq->src != areq->dst)
1406 sg_count = talitos_map_sg(dev, areq->dst,
1407 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001408 DMA_FROM_DEVICE, edesc->dst_chained);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001409
1410 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001411 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001412 } else {
1413 struct talitos_ptr *link_tbl_ptr =
1414 &edesc->link_tbl[edesc->src_nents + 1];
1415
Kim Phillips81eb0242009-08-13 11:51:51 +10001416 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
1417 (edesc->src_nents + 1) *
1418 sizeof(struct talitos_ptr));
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001419 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001420 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1421 link_tbl_ptr);
1422 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1423 edesc->dma_len, DMA_BIDIRECTIONAL);
1424 }
1425
1426 /* iv out */
1427 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1428 DMA_FROM_DEVICE);
1429
1430 /* last DWORD empty */
1431 desc->ptr[6].len = 0;
Kim Phillips81eb0242009-08-13 11:51:51 +10001432 to_talitos_ptr(&desc->ptr[6], 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001433 desc->ptr[6].j_extent = 0;
1434
Kim Phillips5228f0f2011-07-15 11:21:38 +08001435 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001436 if (ret != -EINPROGRESS) {
1437 common_nonsnoop_unmap(dev, edesc, areq);
1438 kfree(edesc);
1439 }
1440 return ret;
1441}
1442
Kim Phillipse938e462009-03-29 15:53:23 +08001443static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1444 areq)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001445{
1446 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1447 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001448 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001449
Horia Geanta79fd31d2012-08-02 17:16:40 +03001450 return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1451 areq->info, 0, areq->nbytes, 0, ivsize, 0,
1452 areq->base.flags);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001453}
1454
1455static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1456{
1457 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1458 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1459 struct talitos_edesc *edesc;
1460
1461 /* allocate extended descriptor */
1462 edesc = ablkcipher_edesc_alloc(areq);
1463 if (IS_ERR(edesc))
1464 return PTR_ERR(edesc);
1465
1466 /* set encrypt */
1467 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1468
Kim Phillipsfebec542011-07-15 11:21:39 +08001469 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001470}
1471
1472static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1473{
1474 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1475 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1476 struct talitos_edesc *edesc;
1477
1478 /* allocate extended descriptor */
1479 edesc = ablkcipher_edesc_alloc(areq);
1480 if (IS_ERR(edesc))
1481 return PTR_ERR(edesc);
1482
1483 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1484
Kim Phillipsfebec542011-07-15 11:21:39 +08001485 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001486}
1487
Lee Nipper497f2e62010-05-19 19:20:36 +10001488static void common_nonsnoop_hash_unmap(struct device *dev,
1489 struct talitos_edesc *edesc,
1490 struct ahash_request *areq)
1491{
1492 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1493
1494 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1495
1496 /* When using hashctx-in, must unmap it. */
1497 if (edesc->desc.ptr[1].len)
1498 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1499 DMA_TO_DEVICE);
1500
1501 if (edesc->desc.ptr[2].len)
1502 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1503 DMA_TO_DEVICE);
1504
1505 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1506
1507 if (edesc->dma_len)
1508 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1509 DMA_BIDIRECTIONAL);
1510
1511}
1512
1513static void ahash_done(struct device *dev,
1514 struct talitos_desc *desc, void *context,
1515 int err)
1516{
1517 struct ahash_request *areq = context;
1518 struct talitos_edesc *edesc =
1519 container_of(desc, struct talitos_edesc, desc);
1520 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1521
1522 if (!req_ctx->last && req_ctx->to_hash_later) {
1523 /* Position any partial block for next update/final/finup */
1524 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001525 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001526 }
1527 common_nonsnoop_hash_unmap(dev, edesc, areq);
1528
1529 kfree(edesc);
1530
1531 areq->base.complete(&areq->base, err);
1532}
1533
1534static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1535 struct ahash_request *areq, unsigned int length,
1536 void (*callback) (struct device *dev,
1537 struct talitos_desc *desc,
1538 void *context, int error))
1539{
1540 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1541 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1542 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1543 struct device *dev = ctx->dev;
1544 struct talitos_desc *desc = &edesc->desc;
1545 int sg_count, ret;
1546
1547 /* first DWORD empty */
1548 desc->ptr[0] = zero_entry;
1549
Kim Phillips60f208d2010-05-19 19:21:53 +10001550 /* hash context in */
1551 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001552 map_single_talitos_ptr(dev, &desc->ptr[1],
1553 req_ctx->hw_context_size,
1554 (char *)req_ctx->hw_context, 0,
1555 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001556 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001557 } else {
1558 desc->ptr[1] = zero_entry;
1559 /* Indicate next op is not the first. */
1560 req_ctx->first = 0;
1561 }
1562
1563 /* HMAC key */
1564 if (ctx->keylen)
1565 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1566 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1567 else
1568 desc->ptr[2] = zero_entry;
1569
1570 /*
1571 * data in
1572 */
1573 desc->ptr[3].len = cpu_to_be16(length);
1574 desc->ptr[3].j_extent = 0;
1575
1576 sg_count = talitos_map_sg(dev, req_ctx->psrc,
1577 edesc->src_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001578 DMA_TO_DEVICE, edesc->src_chained);
Lee Nipper497f2e62010-05-19 19:20:36 +10001579
1580 if (sg_count == 1) {
1581 to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1582 } else {
1583 sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1584 &edesc->link_tbl[0]);
1585 if (sg_count > 1) {
1586 desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1587 to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1588 dma_sync_single_for_device(ctx->dev,
1589 edesc->dma_link_tbl,
1590 edesc->dma_len,
1591 DMA_BIDIRECTIONAL);
1592 } else {
1593 /* Only one segment now, so no link tbl needed */
1594 to_talitos_ptr(&desc->ptr[3],
1595 sg_dma_address(req_ctx->psrc));
1596 }
1597 }
1598
1599 /* fifth DWORD empty */
1600 desc->ptr[4] = zero_entry;
1601
1602 /* hash/HMAC out -or- hash context out */
1603 if (req_ctx->last)
1604 map_single_talitos_ptr(dev, &desc->ptr[5],
1605 crypto_ahash_digestsize(tfm),
1606 areq->result, 0, DMA_FROM_DEVICE);
1607 else
1608 map_single_talitos_ptr(dev, &desc->ptr[5],
1609 req_ctx->hw_context_size,
1610 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1611
1612 /* last DWORD empty */
1613 desc->ptr[6] = zero_entry;
1614
Kim Phillips5228f0f2011-07-15 11:21:38 +08001615 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001616 if (ret != -EINPROGRESS) {
1617 common_nonsnoop_hash_unmap(dev, edesc, areq);
1618 kfree(edesc);
1619 }
1620 return ret;
1621}
1622
1623static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1624 unsigned int nbytes)
1625{
1626 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1627 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1628 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1629
Horia Geanta79fd31d2012-08-02 17:16:40 +03001630 return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
1631 nbytes, 0, 0, 0, areq->base.flags);
Lee Nipper497f2e62010-05-19 19:20:36 +10001632}
1633
1634static int ahash_init(struct ahash_request *areq)
1635{
1636 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1637 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1638
1639 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001640 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001641 req_ctx->first = 1; /* first indicates h/w must init its context */
1642 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001643 req_ctx->hw_context_size =
1644 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1645 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1646 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1647
1648 return 0;
1649}
1650
Kim Phillips60f208d2010-05-19 19:21:53 +10001651/*
1652 * on h/w without explicit sha224 support, we initialize h/w context
1653 * manually with sha224 constants, and tell it to run sha256.
1654 */
1655static int ahash_init_sha224_swinit(struct ahash_request *areq)
1656{
1657 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1658
1659 ahash_init(areq);
1660 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1661
Kim Phillipsa7524472010-09-23 15:56:38 +08001662 req_ctx->hw_context[0] = SHA224_H0;
1663 req_ctx->hw_context[1] = SHA224_H1;
1664 req_ctx->hw_context[2] = SHA224_H2;
1665 req_ctx->hw_context[3] = SHA224_H3;
1666 req_ctx->hw_context[4] = SHA224_H4;
1667 req_ctx->hw_context[5] = SHA224_H5;
1668 req_ctx->hw_context[6] = SHA224_H6;
1669 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001670
1671 /* init 64-bit count */
1672 req_ctx->hw_context[8] = 0;
1673 req_ctx->hw_context[9] = 0;
1674
1675 return 0;
1676}
1677
Lee Nipper497f2e62010-05-19 19:20:36 +10001678static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1679{
1680 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1681 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1682 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1683 struct talitos_edesc *edesc;
1684 unsigned int blocksize =
1685 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1686 unsigned int nbytes_to_hash;
1687 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001688 unsigned int nsg;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001689 bool chained;
Lee Nipper497f2e62010-05-19 19:20:36 +10001690
Lee Nipper5e833bc2010-06-16 15:29:15 +10001691 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1692 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001693 sg_copy_to_buffer(areq->src,
1694 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001695 req_ctx->buf + req_ctx->nbuf, nbytes);
1696 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001697 return 0;
1698 }
1699
Lee Nipper5e833bc2010-06-16 15:29:15 +10001700 /* At least (blocksize + 1) bytes are available to hash */
1701 nbytes_to_hash = nbytes + req_ctx->nbuf;
1702 to_hash_later = nbytes_to_hash & (blocksize - 1);
1703
1704 if (req_ctx->last)
1705 to_hash_later = 0;
1706 else if (to_hash_later)
1707 /* There is a partial block. Hash the full block(s) now */
1708 nbytes_to_hash -= to_hash_later;
1709 else {
1710 /* Keep one block buffered */
1711 nbytes_to_hash -= blocksize;
1712 to_hash_later = blocksize;
1713 }
1714
1715 /* Chain in any previously buffered data */
1716 if (req_ctx->nbuf) {
1717 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1718 sg_init_table(req_ctx->bufsl, nsg);
1719 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1720 if (nsg > 1)
1721 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001722 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001723 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001724 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001725
Lee Nipper5e833bc2010-06-16 15:29:15 +10001726 if (to_hash_later) {
1727 int nents = sg_count(areq->src, nbytes, &chained);
Akinobu Mitad0525722013-07-08 16:01:55 -07001728 sg_pcopy_to_buffer(areq->src, nents,
Lee Nipper5e833bc2010-06-16 15:29:15 +10001729 req_ctx->bufnext,
1730 to_hash_later,
1731 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001732 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001733 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001734
Lee Nipper5e833bc2010-06-16 15:29:15 +10001735 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001736 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1737 if (IS_ERR(edesc))
1738 return PTR_ERR(edesc);
1739
1740 edesc->desc.hdr = ctx->desc_hdr_template;
1741
1742 /* On last one, request SEC to pad; otherwise continue */
1743 if (req_ctx->last)
1744 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1745 else
1746 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1747
Kim Phillips60f208d2010-05-19 19:21:53 +10001748 /* request SEC to INIT hash. */
1749 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001750 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1751
1752 /* When the tfm context has a keylen, it's an HMAC.
1753 * A first or last (ie. not middle) descriptor must request HMAC.
1754 */
1755 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1756 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1757
1758 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1759 ahash_done);
1760}
1761
1762static int ahash_update(struct ahash_request *areq)
1763{
1764 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1765
1766 req_ctx->last = 0;
1767
1768 return ahash_process_req(areq, areq->nbytes);
1769}
1770
1771static int ahash_final(struct ahash_request *areq)
1772{
1773 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1774
1775 req_ctx->last = 1;
1776
1777 return ahash_process_req(areq, 0);
1778}
1779
1780static int ahash_finup(struct ahash_request *areq)
1781{
1782 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1783
1784 req_ctx->last = 1;
1785
1786 return ahash_process_req(areq, areq->nbytes);
1787}
1788
1789static int ahash_digest(struct ahash_request *areq)
1790{
1791 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001792 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001793
Kim Phillips60f208d2010-05-19 19:21:53 +10001794 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001795 req_ctx->last = 1;
1796
1797 return ahash_process_req(areq, areq->nbytes);
1798}
1799
Lee Nipper79b3a412011-11-21 16:13:25 +08001800struct keyhash_result {
1801 struct completion completion;
1802 int err;
1803};
1804
1805static void keyhash_complete(struct crypto_async_request *req, int err)
1806{
1807 struct keyhash_result *res = req->data;
1808
1809 if (err == -EINPROGRESS)
1810 return;
1811
1812 res->err = err;
1813 complete(&res->completion);
1814}
1815
1816static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1817 u8 *hash)
1818{
1819 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1820
1821 struct scatterlist sg[1];
1822 struct ahash_request *req;
1823 struct keyhash_result hresult;
1824 int ret;
1825
1826 init_completion(&hresult.completion);
1827
1828 req = ahash_request_alloc(tfm, GFP_KERNEL);
1829 if (!req)
1830 return -ENOMEM;
1831
1832 /* Keep tfm keylen == 0 during hash of the long key */
1833 ctx->keylen = 0;
1834 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1835 keyhash_complete, &hresult);
1836
1837 sg_init_one(&sg[0], key, keylen);
1838
1839 ahash_request_set_crypt(req, sg, hash, keylen);
1840 ret = crypto_ahash_digest(req);
1841 switch (ret) {
1842 case 0:
1843 break;
1844 case -EINPROGRESS:
1845 case -EBUSY:
1846 ret = wait_for_completion_interruptible(
1847 &hresult.completion);
1848 if (!ret)
1849 ret = hresult.err;
1850 break;
1851 default:
1852 break;
1853 }
1854 ahash_request_free(req);
1855
1856 return ret;
1857}
1858
1859static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1860 unsigned int keylen)
1861{
1862 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1863 unsigned int blocksize =
1864 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1865 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1866 unsigned int keysize = keylen;
1867 u8 hash[SHA512_DIGEST_SIZE];
1868 int ret;
1869
1870 if (keylen <= blocksize)
1871 memcpy(ctx->key, key, keysize);
1872 else {
1873 /* Must get the hash of the long key */
1874 ret = keyhash(tfm, key, keylen, hash);
1875
1876 if (ret) {
1877 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1878 return -EINVAL;
1879 }
1880
1881 keysize = digestsize;
1882 memcpy(ctx->key, hash, digestsize);
1883 }
1884
1885 ctx->keylen = keysize;
1886
1887 return 0;
1888}
1889
1890
Kim Phillips9c4a7962008-06-23 19:50:15 +08001891struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001892 u32 type;
1893 union {
1894 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001895 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001896 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001897 __be32 desc_hdr_template;
1898};
1899
1900static struct talitos_alg_template driver_algs[] = {
Horia Geanta991155b2013-03-20 16:31:38 +02001901 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001902 { .type = CRYPTO_ALG_TYPE_AEAD,
1903 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001904 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1905 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1906 .cra_blocksize = AES_BLOCK_SIZE,
1907 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001908 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001909 .ivsize = AES_BLOCK_SIZE,
1910 .maxauthsize = SHA1_DIGEST_SIZE,
1911 }
1912 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001913 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1914 DESC_HDR_SEL0_AESU |
1915 DESC_HDR_MODE0_AESU_CBC |
1916 DESC_HDR_SEL1_MDEUA |
1917 DESC_HDR_MODE1_MDEU_INIT |
1918 DESC_HDR_MODE1_MDEU_PAD |
1919 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001920 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001921 { .type = CRYPTO_ALG_TYPE_AEAD,
1922 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001923 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1924 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1925 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1926 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001927 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001928 .ivsize = DES3_EDE_BLOCK_SIZE,
1929 .maxauthsize = SHA1_DIGEST_SIZE,
1930 }
1931 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001932 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1933 DESC_HDR_SEL0_DEU |
1934 DESC_HDR_MODE0_DEU_CBC |
1935 DESC_HDR_MODE0_DEU_3DES |
1936 DESC_HDR_SEL1_MDEUA |
1937 DESC_HDR_MODE1_MDEU_INIT |
1938 DESC_HDR_MODE1_MDEU_PAD |
1939 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001940 },
Horia Geanta357fb602012-07-03 19:16:53 +03001941 { .type = CRYPTO_ALG_TYPE_AEAD,
1942 .alg.crypto = {
1943 .cra_name = "authenc(hmac(sha224),cbc(aes))",
1944 .cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1945 .cra_blocksize = AES_BLOCK_SIZE,
1946 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001947 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001948 .ivsize = AES_BLOCK_SIZE,
1949 .maxauthsize = SHA224_DIGEST_SIZE,
1950 }
1951 },
1952 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1953 DESC_HDR_SEL0_AESU |
1954 DESC_HDR_MODE0_AESU_CBC |
1955 DESC_HDR_SEL1_MDEUA |
1956 DESC_HDR_MODE1_MDEU_INIT |
1957 DESC_HDR_MODE1_MDEU_PAD |
1958 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1959 },
1960 { .type = CRYPTO_ALG_TYPE_AEAD,
1961 .alg.crypto = {
1962 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1963 .cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1964 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1965 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001966 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001967 .ivsize = DES3_EDE_BLOCK_SIZE,
1968 .maxauthsize = SHA224_DIGEST_SIZE,
1969 }
1970 },
1971 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1972 DESC_HDR_SEL0_DEU |
1973 DESC_HDR_MODE0_DEU_CBC |
1974 DESC_HDR_MODE0_DEU_3DES |
1975 DESC_HDR_SEL1_MDEUA |
1976 DESC_HDR_MODE1_MDEU_INIT |
1977 DESC_HDR_MODE1_MDEU_PAD |
1978 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1979 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001980 { .type = CRYPTO_ALG_TYPE_AEAD,
1981 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001982 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1983 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1984 .cra_blocksize = AES_BLOCK_SIZE,
1985 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001986 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001987 .ivsize = AES_BLOCK_SIZE,
1988 .maxauthsize = SHA256_DIGEST_SIZE,
1989 }
1990 },
Lee Nipper3952f172008-07-10 18:29:18 +08001991 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1992 DESC_HDR_SEL0_AESU |
1993 DESC_HDR_MODE0_AESU_CBC |
1994 DESC_HDR_SEL1_MDEUA |
1995 DESC_HDR_MODE1_MDEU_INIT |
1996 DESC_HDR_MODE1_MDEU_PAD |
1997 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
1998 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001999 { .type = CRYPTO_ALG_TYPE_AEAD,
2000 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002001 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2002 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2003 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2004 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002005 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002006 .ivsize = DES3_EDE_BLOCK_SIZE,
2007 .maxauthsize = SHA256_DIGEST_SIZE,
2008 }
2009 },
Lee Nipper3952f172008-07-10 18:29:18 +08002010 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2011 DESC_HDR_SEL0_DEU |
2012 DESC_HDR_MODE0_DEU_CBC |
2013 DESC_HDR_MODE0_DEU_3DES |
2014 DESC_HDR_SEL1_MDEUA |
2015 DESC_HDR_MODE1_MDEU_INIT |
2016 DESC_HDR_MODE1_MDEU_PAD |
2017 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2018 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002019 { .type = CRYPTO_ALG_TYPE_AEAD,
2020 .alg.crypto = {
Horia Geanta357fb602012-07-03 19:16:53 +03002021 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2022 .cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2023 .cra_blocksize = AES_BLOCK_SIZE,
2024 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002025 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002026 .ivsize = AES_BLOCK_SIZE,
2027 .maxauthsize = SHA384_DIGEST_SIZE,
2028 }
2029 },
2030 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2031 DESC_HDR_SEL0_AESU |
2032 DESC_HDR_MODE0_AESU_CBC |
2033 DESC_HDR_SEL1_MDEUB |
2034 DESC_HDR_MODE1_MDEU_INIT |
2035 DESC_HDR_MODE1_MDEU_PAD |
2036 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2037 },
2038 { .type = CRYPTO_ALG_TYPE_AEAD,
2039 .alg.crypto = {
2040 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2041 .cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2042 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2043 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002044 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002045 .ivsize = DES3_EDE_BLOCK_SIZE,
2046 .maxauthsize = SHA384_DIGEST_SIZE,
2047 }
2048 },
2049 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2050 DESC_HDR_SEL0_DEU |
2051 DESC_HDR_MODE0_DEU_CBC |
2052 DESC_HDR_MODE0_DEU_3DES |
2053 DESC_HDR_SEL1_MDEUB |
2054 DESC_HDR_MODE1_MDEU_INIT |
2055 DESC_HDR_MODE1_MDEU_PAD |
2056 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2057 },
2058 { .type = CRYPTO_ALG_TYPE_AEAD,
2059 .alg.crypto = {
2060 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2061 .cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2062 .cra_blocksize = AES_BLOCK_SIZE,
2063 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002064 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002065 .ivsize = AES_BLOCK_SIZE,
2066 .maxauthsize = SHA512_DIGEST_SIZE,
2067 }
2068 },
2069 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2070 DESC_HDR_SEL0_AESU |
2071 DESC_HDR_MODE0_AESU_CBC |
2072 DESC_HDR_SEL1_MDEUB |
2073 DESC_HDR_MODE1_MDEU_INIT |
2074 DESC_HDR_MODE1_MDEU_PAD |
2075 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2076 },
2077 { .type = CRYPTO_ALG_TYPE_AEAD,
2078 .alg.crypto = {
2079 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2080 .cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2081 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2082 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002083 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002084 .ivsize = DES3_EDE_BLOCK_SIZE,
2085 .maxauthsize = SHA512_DIGEST_SIZE,
2086 }
2087 },
2088 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2089 DESC_HDR_SEL0_DEU |
2090 DESC_HDR_MODE0_DEU_CBC |
2091 DESC_HDR_MODE0_DEU_3DES |
2092 DESC_HDR_SEL1_MDEUB |
2093 DESC_HDR_MODE1_MDEU_INIT |
2094 DESC_HDR_MODE1_MDEU_PAD |
2095 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2096 },
2097 { .type = CRYPTO_ALG_TYPE_AEAD,
2098 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002099 .cra_name = "authenc(hmac(md5),cbc(aes))",
2100 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2101 .cra_blocksize = AES_BLOCK_SIZE,
2102 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002103 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002104 .ivsize = AES_BLOCK_SIZE,
2105 .maxauthsize = MD5_DIGEST_SIZE,
2106 }
2107 },
Lee Nipper3952f172008-07-10 18:29:18 +08002108 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2109 DESC_HDR_SEL0_AESU |
2110 DESC_HDR_MODE0_AESU_CBC |
2111 DESC_HDR_SEL1_MDEUA |
2112 DESC_HDR_MODE1_MDEU_INIT |
2113 DESC_HDR_MODE1_MDEU_PAD |
2114 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2115 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002116 { .type = CRYPTO_ALG_TYPE_AEAD,
2117 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002118 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2119 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2120 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2121 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002122 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002123 .ivsize = DES3_EDE_BLOCK_SIZE,
2124 .maxauthsize = MD5_DIGEST_SIZE,
2125 }
2126 },
Lee Nipper3952f172008-07-10 18:29:18 +08002127 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2128 DESC_HDR_SEL0_DEU |
2129 DESC_HDR_MODE0_DEU_CBC |
2130 DESC_HDR_MODE0_DEU_3DES |
2131 DESC_HDR_SEL1_MDEUA |
2132 DESC_HDR_MODE1_MDEU_INIT |
2133 DESC_HDR_MODE1_MDEU_PAD |
2134 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002135 },
2136 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002137 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2138 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002139 .cra_name = "cbc(aes)",
2140 .cra_driver_name = "cbc-aes-talitos",
2141 .cra_blocksize = AES_BLOCK_SIZE,
2142 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2143 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002144 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002145 .min_keysize = AES_MIN_KEY_SIZE,
2146 .max_keysize = AES_MAX_KEY_SIZE,
2147 .ivsize = AES_BLOCK_SIZE,
2148 }
2149 },
2150 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2151 DESC_HDR_SEL0_AESU |
2152 DESC_HDR_MODE0_AESU_CBC,
2153 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002154 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2155 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002156 .cra_name = "cbc(des3_ede)",
2157 .cra_driver_name = "cbc-3des-talitos",
2158 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2159 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2160 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002161 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002162 .min_keysize = DES3_EDE_KEY_SIZE,
2163 .max_keysize = DES3_EDE_KEY_SIZE,
2164 .ivsize = DES3_EDE_BLOCK_SIZE,
2165 }
2166 },
2167 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2168 DESC_HDR_SEL0_DEU |
2169 DESC_HDR_MODE0_DEU_CBC |
2170 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002171 },
2172 /* AHASH algorithms. */
2173 { .type = CRYPTO_ALG_TYPE_AHASH,
2174 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002175 .halg.digestsize = MD5_DIGEST_SIZE,
2176 .halg.base = {
2177 .cra_name = "md5",
2178 .cra_driver_name = "md5-talitos",
2179 .cra_blocksize = MD5_BLOCK_SIZE,
2180 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2181 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002182 }
2183 },
2184 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2185 DESC_HDR_SEL0_MDEUA |
2186 DESC_HDR_MODE0_MDEU_MD5,
2187 },
2188 { .type = CRYPTO_ALG_TYPE_AHASH,
2189 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002190 .halg.digestsize = SHA1_DIGEST_SIZE,
2191 .halg.base = {
2192 .cra_name = "sha1",
2193 .cra_driver_name = "sha1-talitos",
2194 .cra_blocksize = SHA1_BLOCK_SIZE,
2195 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2196 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002197 }
2198 },
2199 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2200 DESC_HDR_SEL0_MDEUA |
2201 DESC_HDR_MODE0_MDEU_SHA1,
2202 },
2203 { .type = CRYPTO_ALG_TYPE_AHASH,
2204 .alg.hash = {
Kim Phillips60f208d2010-05-19 19:21:53 +10002205 .halg.digestsize = SHA224_DIGEST_SIZE,
2206 .halg.base = {
2207 .cra_name = "sha224",
2208 .cra_driver_name = "sha224-talitos",
2209 .cra_blocksize = SHA224_BLOCK_SIZE,
2210 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2211 CRYPTO_ALG_ASYNC,
Kim Phillips60f208d2010-05-19 19:21:53 +10002212 }
2213 },
2214 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2215 DESC_HDR_SEL0_MDEUA |
2216 DESC_HDR_MODE0_MDEU_SHA224,
2217 },
2218 { .type = CRYPTO_ALG_TYPE_AHASH,
2219 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002220 .halg.digestsize = SHA256_DIGEST_SIZE,
2221 .halg.base = {
2222 .cra_name = "sha256",
2223 .cra_driver_name = "sha256-talitos",
2224 .cra_blocksize = SHA256_BLOCK_SIZE,
2225 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2226 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002227 }
2228 },
2229 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2230 DESC_HDR_SEL0_MDEUA |
2231 DESC_HDR_MODE0_MDEU_SHA256,
2232 },
2233 { .type = CRYPTO_ALG_TYPE_AHASH,
2234 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002235 .halg.digestsize = SHA384_DIGEST_SIZE,
2236 .halg.base = {
2237 .cra_name = "sha384",
2238 .cra_driver_name = "sha384-talitos",
2239 .cra_blocksize = SHA384_BLOCK_SIZE,
2240 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2241 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002242 }
2243 },
2244 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2245 DESC_HDR_SEL0_MDEUB |
2246 DESC_HDR_MODE0_MDEUB_SHA384,
2247 },
2248 { .type = CRYPTO_ALG_TYPE_AHASH,
2249 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002250 .halg.digestsize = SHA512_DIGEST_SIZE,
2251 .halg.base = {
2252 .cra_name = "sha512",
2253 .cra_driver_name = "sha512-talitos",
2254 .cra_blocksize = SHA512_BLOCK_SIZE,
2255 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2256 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002257 }
2258 },
2259 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2260 DESC_HDR_SEL0_MDEUB |
2261 DESC_HDR_MODE0_MDEUB_SHA512,
2262 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002263 { .type = CRYPTO_ALG_TYPE_AHASH,
2264 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002265 .halg.digestsize = MD5_DIGEST_SIZE,
2266 .halg.base = {
2267 .cra_name = "hmac(md5)",
2268 .cra_driver_name = "hmac-md5-talitos",
2269 .cra_blocksize = MD5_BLOCK_SIZE,
2270 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2271 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002272 }
2273 },
2274 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2275 DESC_HDR_SEL0_MDEUA |
2276 DESC_HDR_MODE0_MDEU_MD5,
2277 },
2278 { .type = CRYPTO_ALG_TYPE_AHASH,
2279 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002280 .halg.digestsize = SHA1_DIGEST_SIZE,
2281 .halg.base = {
2282 .cra_name = "hmac(sha1)",
2283 .cra_driver_name = "hmac-sha1-talitos",
2284 .cra_blocksize = SHA1_BLOCK_SIZE,
2285 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2286 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002287 }
2288 },
2289 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2290 DESC_HDR_SEL0_MDEUA |
2291 DESC_HDR_MODE0_MDEU_SHA1,
2292 },
2293 { .type = CRYPTO_ALG_TYPE_AHASH,
2294 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002295 .halg.digestsize = SHA224_DIGEST_SIZE,
2296 .halg.base = {
2297 .cra_name = "hmac(sha224)",
2298 .cra_driver_name = "hmac-sha224-talitos",
2299 .cra_blocksize = SHA224_BLOCK_SIZE,
2300 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2301 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002302 }
2303 },
2304 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2305 DESC_HDR_SEL0_MDEUA |
2306 DESC_HDR_MODE0_MDEU_SHA224,
2307 },
2308 { .type = CRYPTO_ALG_TYPE_AHASH,
2309 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002310 .halg.digestsize = SHA256_DIGEST_SIZE,
2311 .halg.base = {
2312 .cra_name = "hmac(sha256)",
2313 .cra_driver_name = "hmac-sha256-talitos",
2314 .cra_blocksize = SHA256_BLOCK_SIZE,
2315 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2316 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002317 }
2318 },
2319 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2320 DESC_HDR_SEL0_MDEUA |
2321 DESC_HDR_MODE0_MDEU_SHA256,
2322 },
2323 { .type = CRYPTO_ALG_TYPE_AHASH,
2324 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002325 .halg.digestsize = SHA384_DIGEST_SIZE,
2326 .halg.base = {
2327 .cra_name = "hmac(sha384)",
2328 .cra_driver_name = "hmac-sha384-talitos",
2329 .cra_blocksize = SHA384_BLOCK_SIZE,
2330 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2331 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002332 }
2333 },
2334 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2335 DESC_HDR_SEL0_MDEUB |
2336 DESC_HDR_MODE0_MDEUB_SHA384,
2337 },
2338 { .type = CRYPTO_ALG_TYPE_AHASH,
2339 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002340 .halg.digestsize = SHA512_DIGEST_SIZE,
2341 .halg.base = {
2342 .cra_name = "hmac(sha512)",
2343 .cra_driver_name = "hmac-sha512-talitos",
2344 .cra_blocksize = SHA512_BLOCK_SIZE,
2345 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2346 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002347 }
2348 },
2349 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2350 DESC_HDR_SEL0_MDEUB |
2351 DESC_HDR_MODE0_MDEUB_SHA512,
2352 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002353};
2354
2355struct talitos_crypto_alg {
2356 struct list_head entry;
2357 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002358 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002359};
2360
2361static int talitos_cra_init(struct crypto_tfm *tfm)
2362{
2363 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002364 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002365 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002366 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002367
Lee Nipper497f2e62010-05-19 19:20:36 +10002368 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2369 talitos_alg = container_of(__crypto_ahash_alg(alg),
2370 struct talitos_crypto_alg,
2371 algt.alg.hash);
2372 else
2373 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2374 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002375
Kim Phillips9c4a7962008-06-23 19:50:15 +08002376 /* update context with ptr to dev */
2377 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002378
Kim Phillips5228f0f2011-07-15 11:21:38 +08002379 /* assign SEC channel to tfm in round-robin fashion */
2380 priv = dev_get_drvdata(ctx->dev);
2381 ctx->ch = atomic_inc_return(&priv->last_chan) &
2382 (priv->num_channels - 1);
2383
Kim Phillips9c4a7962008-06-23 19:50:15 +08002384 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002385 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002386
Kim Phillips602dba52011-07-15 11:21:39 +08002387 /* select done notification */
2388 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2389
Lee Nipper497f2e62010-05-19 19:20:36 +10002390 return 0;
2391}
2392
2393static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2394{
2395 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2396
2397 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002398
2399 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002400 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002401
2402 return 0;
2403}
2404
Lee Nipper497f2e62010-05-19 19:20:36 +10002405static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2406{
2407 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2408
2409 talitos_cra_init(tfm);
2410
2411 ctx->keylen = 0;
2412 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2413 sizeof(struct talitos_ahash_req_ctx));
2414
2415 return 0;
2416}
2417
Kim Phillips9c4a7962008-06-23 19:50:15 +08002418/*
2419 * given the alg's descriptor header template, determine whether descriptor
2420 * type and primary/secondary execution units required match the hw
2421 * capabilities description provided in the device tree node.
2422 */
2423static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2424{
2425 struct talitos_private *priv = dev_get_drvdata(dev);
2426 int ret;
2427
2428 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2429 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2430
2431 if (SECONDARY_EU(desc_hdr_template))
2432 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2433 & priv->exec_units);
2434
2435 return ret;
2436}
2437
Grant Likely2dc11582010-08-06 09:25:50 -06002438static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002439{
2440 struct device *dev = &ofdev->dev;
2441 struct talitos_private *priv = dev_get_drvdata(dev);
2442 struct talitos_crypto_alg *t_alg, *n;
2443 int i;
2444
2445 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002446 switch (t_alg->algt.type) {
2447 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2448 case CRYPTO_ALG_TYPE_AEAD:
2449 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2450 break;
2451 case CRYPTO_ALG_TYPE_AHASH:
2452 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2453 break;
2454 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002455 list_del(&t_alg->entry);
2456 kfree(t_alg);
2457 }
2458
2459 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2460 talitos_unregister_rng(dev);
2461
Kim Phillips4b9926282009-08-13 11:50:38 +10002462 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002463 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002464
Kim Phillips4b9926282009-08-13 11:50:38 +10002465 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002466
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002467 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002468 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002469 free_irq(priv->irq[i], dev);
2470 irq_dispose_mapping(priv->irq[i]);
2471 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002472
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002473 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002474 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002475 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002476
2477 iounmap(priv->reg);
2478
2479 dev_set_drvdata(dev, NULL);
2480
2481 kfree(priv);
2482
2483 return 0;
2484}
2485
2486static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2487 struct talitos_alg_template
2488 *template)
2489{
Kim Phillips60f208d2010-05-19 19:21:53 +10002490 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002491 struct talitos_crypto_alg *t_alg;
2492 struct crypto_alg *alg;
2493
2494 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2495 if (!t_alg)
2496 return ERR_PTR(-ENOMEM);
2497
Lee Nipperacbf7c622010-05-19 19:19:33 +10002498 t_alg->algt = *template;
2499
2500 switch (t_alg->algt.type) {
2501 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002502 alg = &t_alg->algt.alg.crypto;
2503 alg->cra_init = talitos_cra_init;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002504 alg->cra_type = &crypto_ablkcipher_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002505 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2506 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2507 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2508 alg->cra_ablkcipher.geniv = "eseqiv";
Lee Nipper497f2e62010-05-19 19:20:36 +10002509 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002510 case CRYPTO_ALG_TYPE_AEAD:
2511 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002512 alg->cra_init = talitos_cra_init_aead;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002513 alg->cra_type = &crypto_aead_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002514 alg->cra_aead.setkey = aead_setkey;
2515 alg->cra_aead.setauthsize = aead_setauthsize;
2516 alg->cra_aead.encrypt = aead_encrypt;
2517 alg->cra_aead.decrypt = aead_decrypt;
2518 alg->cra_aead.givencrypt = aead_givencrypt;
2519 alg->cra_aead.geniv = "<built-in>";
Lee Nipperacbf7c622010-05-19 19:19:33 +10002520 break;
2521 case CRYPTO_ALG_TYPE_AHASH:
2522 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002523 alg->cra_init = talitos_cra_init_ahash;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002524 alg->cra_type = &crypto_ahash_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002525 t_alg->algt.alg.hash.init = ahash_init;
2526 t_alg->algt.alg.hash.update = ahash_update;
2527 t_alg->algt.alg.hash.final = ahash_final;
2528 t_alg->algt.alg.hash.finup = ahash_finup;
2529 t_alg->algt.alg.hash.digest = ahash_digest;
2530 t_alg->algt.alg.hash.setkey = ahash_setkey;
2531
Lee Nipper79b3a412011-11-21 16:13:25 +08002532 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06002533 !strncmp(alg->cra_name, "hmac", 4)) {
2534 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002535 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002536 }
Kim Phillips60f208d2010-05-19 19:21:53 +10002537 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002538 (!strcmp(alg->cra_name, "sha224") ||
2539 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002540 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2541 t_alg->algt.desc_hdr_template =
2542 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2543 DESC_HDR_SEL0_MDEUA |
2544 DESC_HDR_MODE0_MDEU_SHA256;
2545 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002546 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002547 default:
2548 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2549 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002550 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002551
Kim Phillips9c4a7962008-06-23 19:50:15 +08002552 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002553 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002554 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002555 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002556 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002557
Kim Phillips9c4a7962008-06-23 19:50:15 +08002558 t_alg->dev = dev;
2559
2560 return t_alg;
2561}
2562
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002563static int talitos_probe_irq(struct platform_device *ofdev)
2564{
2565 struct device *dev = &ofdev->dev;
2566 struct device_node *np = ofdev->dev.of_node;
2567 struct talitos_private *priv = dev_get_drvdata(dev);
2568 int err;
2569
2570 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002571 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002572 dev_err(dev, "failed to map irq\n");
2573 return -EINVAL;
2574 }
2575
2576 priv->irq[1] = irq_of_parse_and_map(np, 1);
2577
2578 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002579 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002580 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2581 dev_driver_string(dev), dev);
2582 goto primary_out;
2583 }
2584
2585 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2586 dev_driver_string(dev), dev);
2587 if (err)
2588 goto primary_out;
2589
2590 /* get the secondary irq line */
2591 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2592 dev_driver_string(dev), dev);
2593 if (err) {
2594 dev_err(dev, "failed to request secondary irq\n");
2595 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002596 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002597 }
2598
2599 return err;
2600
2601primary_out:
2602 if (err) {
2603 dev_err(dev, "failed to request primary irq\n");
2604 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002605 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002606 }
2607
2608 return err;
2609}
2610
Grant Likely1c48a5c2011-02-17 02:43:24 -07002611static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002612{
2613 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002614 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002615 struct talitos_private *priv;
2616 const unsigned int *prop;
2617 int i, err;
2618
2619 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2620 if (!priv)
2621 return -ENOMEM;
2622
2623 dev_set_drvdata(dev, priv);
2624
2625 priv->ofdev = ofdev;
2626
Horia Geanta511d63c2012-03-30 17:49:53 +03002627 spin_lock_init(&priv->reg_lock);
2628
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002629 err = talitos_probe_irq(ofdev);
2630 if (err)
2631 goto err_out;
2632
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002633 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002634 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2635 (unsigned long)dev);
2636 } else {
2637 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2638 (unsigned long)dev);
2639 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2640 (unsigned long)dev);
2641 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002642
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002643 INIT_LIST_HEAD(&priv->alg_list);
2644
Kim Phillips9c4a7962008-06-23 19:50:15 +08002645 priv->reg = of_iomap(np, 0);
2646 if (!priv->reg) {
2647 dev_err(dev, "failed to of_iomap\n");
2648 err = -ENOMEM;
2649 goto err_out;
2650 }
2651
2652 /* get SEC version capabilities from device tree */
2653 prop = of_get_property(np, "fsl,num-channels", NULL);
2654 if (prop)
2655 priv->num_channels = *prop;
2656
2657 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2658 if (prop)
2659 priv->chfifo_len = *prop;
2660
2661 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2662 if (prop)
2663 priv->exec_units = *prop;
2664
2665 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2666 if (prop)
2667 priv->desc_types = *prop;
2668
2669 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2670 !priv->exec_units || !priv->desc_types) {
2671 dev_err(dev, "invalid property data in device tree node\n");
2672 err = -EINVAL;
2673 goto err_out;
2674 }
2675
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002676 if (of_device_is_compatible(np, "fsl,sec3.0"))
2677 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2678
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002679 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002680 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002681 TALITOS_FTR_SHA224_HWINIT |
2682 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002683
Kim Phillips4b9926282009-08-13 11:50:38 +10002684 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2685 priv->num_channels, GFP_KERNEL);
2686 if (!priv->chan) {
2687 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002688 err = -ENOMEM;
2689 goto err_out;
2690 }
2691
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002692 for (i = 0; i < priv->num_channels; i++) {
2693 priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002694 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002695 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2696 }
Kim Phillipsad42d5f2011-11-21 16:13:27 +08002697
Kim Phillips9c4a7962008-06-23 19:50:15 +08002698 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002699 spin_lock_init(&priv->chan[i].head_lock);
2700 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002701 }
2702
2703 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2704
2705 for (i = 0; i < priv->num_channels; i++) {
Kim Phillips4b9926282009-08-13 11:50:38 +10002706 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2707 priv->fifo_len, GFP_KERNEL);
2708 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002709 dev_err(dev, "failed to allocate request fifo %d\n", i);
2710 err = -ENOMEM;
2711 goto err_out;
2712 }
2713 }
2714
Kim Phillipsec6644d2008-07-17 20:16:40 +08002715 for (i = 0; i < priv->num_channels; i++)
Kim Phillips4b9926282009-08-13 11:50:38 +10002716 atomic_set(&priv->chan[i].submit_count,
2717 -(priv->chfifo_len - 1));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002718
Kim Phillips81eb0242009-08-13 11:51:51 +10002719 dma_set_mask(dev, DMA_BIT_MASK(36));
2720
Kim Phillips9c4a7962008-06-23 19:50:15 +08002721 /* reset and initialize the h/w */
2722 err = init_device(dev);
2723 if (err) {
2724 dev_err(dev, "failed to initialize device\n");
2725 goto err_out;
2726 }
2727
2728 /* register the RNG, if available */
2729 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2730 err = talitos_register_rng(dev);
2731 if (err) {
2732 dev_err(dev, "failed to register hwrng: %d\n", err);
2733 goto err_out;
2734 } else
2735 dev_info(dev, "hwrng\n");
2736 }
2737
2738 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002739 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2740 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2741 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002742 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002743
2744 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2745 if (IS_ERR(t_alg)) {
2746 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002747 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08002748 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002749 goto err_out;
2750 }
2751
Lee Nipperacbf7c622010-05-19 19:19:33 +10002752 switch (t_alg->algt.type) {
2753 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2754 case CRYPTO_ALG_TYPE_AEAD:
2755 err = crypto_register_alg(
2756 &t_alg->algt.alg.crypto);
2757 name = t_alg->algt.alg.crypto.cra_driver_name;
2758 break;
2759 case CRYPTO_ALG_TYPE_AHASH:
2760 err = crypto_register_ahash(
2761 &t_alg->algt.alg.hash);
2762 name =
2763 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2764 break;
2765 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002766 if (err) {
2767 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002768 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002769 kfree(t_alg);
Horia Geanta991155b2013-03-20 16:31:38 +02002770 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08002771 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002772 }
2773 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002774 if (!list_empty(&priv->alg_list))
2775 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2776 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002777
2778 return 0;
2779
2780err_out:
2781 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002782
2783 return err;
2784}
2785
Márton Németh6c3f9752010-01-17 21:54:01 +11002786static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002787 {
2788 .compatible = "fsl,sec2.0",
2789 },
2790 {},
2791};
2792MODULE_DEVICE_TABLE(of, talitos_match);
2793
Grant Likely1c48a5c2011-02-17 02:43:24 -07002794static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002795 .driver = {
2796 .name = "talitos",
2797 .owner = THIS_MODULE,
2798 .of_match_table = talitos_match,
2799 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002800 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002801 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002802};
2803
Axel Lin741e8c22011-11-26 21:26:19 +08002804module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002805
2806MODULE_LICENSE("GPL");
2807MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2808MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");