blob: bca6dedb357d385146a4dbe31efb28fca337b773 [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
LEROY Christopheedc6bd62015-04-17 16:31:53 +020058static void to_talitos_ptr(struct talitos_ptr *ptr, dma_addr_t dma_addr)
Kim Phillips81eb0242009-08-13 11:51:51 +100059{
LEROY Christopheedc6bd62015-04-17 16:31:53 +020060 ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
61 ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +100062}
63
LEROY Christophe538caf82015-04-17 16:31:59 +020064static void to_talitos_ptr_len(struct talitos_ptr *ptr, unsigned short len)
65{
66 ptr->len = cpu_to_be16(len);
67}
68
69static unsigned short from_talitos_ptr_len(struct talitos_ptr *ptr)
70{
71 return be16_to_cpu(ptr->len);
72}
73
LEROY Christophe185eb792015-04-17 16:31:55 +020074static void to_talitos_ptr_extent_clear(struct talitos_ptr *ptr)
75{
76 ptr->j_extent = 0;
77}
78
Kim Phillips9c4a7962008-06-23 19:50:15 +080079/*
80 * map virtual single (contiguous) pointer to h/w descriptor pointer
81 */
82static void map_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +020083 struct talitos_ptr *ptr,
Kim Phillips9c4a7962008-06-23 19:50:15 +080084 unsigned short len, void *data,
Kim Phillips9c4a7962008-06-23 19:50:15 +080085 enum dma_data_direction dir)
86{
Kim Phillips81eb0242009-08-13 11:51:51 +100087 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
88
LEROY Christophe538caf82015-04-17 16:31:59 +020089 to_talitos_ptr_len(ptr, len);
LEROY Christopheedc6bd62015-04-17 16:31:53 +020090 to_talitos_ptr(ptr, dma_addr);
LEROY Christophea2b35aa2015-04-17 16:31:57 +020091 to_talitos_ptr_extent_clear(ptr);
Kim Phillips9c4a7962008-06-23 19:50:15 +080092}
93
94/*
95 * unmap bus single (contiguous) h/w descriptor pointer
96 */
97static void unmap_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +020098 struct talitos_ptr *ptr,
Kim Phillips9c4a7962008-06-23 19:50:15 +080099 enum dma_data_direction dir)
100{
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200101 dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
LEROY Christophe538caf82015-04-17 16:31:59 +0200102 from_talitos_ptr_len(ptr), dir);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800103}
104
105static int reset_channel(struct device *dev, int ch)
106{
107 struct talitos_private *priv = dev_get_drvdata(dev);
108 unsigned int timeout = TALITOS_TIMEOUT;
109
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800110 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800111
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800112 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800113 && --timeout)
114 cpu_relax();
115
116 if (timeout == 0) {
117 dev_err(dev, "failed to reset channel %d\n", ch);
118 return -EIO;
119 }
120
Kim Phillips81eb0242009-08-13 11:51:51 +1000121 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800122 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000123 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800124
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800125 /* and ICCR writeback, if available */
126 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800127 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800128 TALITOS_CCCR_LO_IWSE);
129
Kim Phillips9c4a7962008-06-23 19:50:15 +0800130 return 0;
131}
132
133static int reset_device(struct device *dev)
134{
135 struct talitos_private *priv = dev_get_drvdata(dev);
136 unsigned int timeout = TALITOS_TIMEOUT;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800137 u32 mcr = TALITOS_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800138
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800139 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800140
141 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
142 && --timeout)
143 cpu_relax();
144
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600145 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800146 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
147 setbits32(priv->reg + TALITOS_MCR, mcr);
148 }
149
Kim Phillips9c4a7962008-06-23 19:50:15 +0800150 if (timeout == 0) {
151 dev_err(dev, "failed to reset device\n");
152 return -EIO;
153 }
154
155 return 0;
156}
157
158/*
159 * Reset and initialize the device
160 */
161static int init_device(struct device *dev)
162{
163 struct talitos_private *priv = dev_get_drvdata(dev);
164 int ch, err;
165
166 /*
167 * Master reset
168 * errata documentation: warning: certain SEC interrupts
169 * are not fully cleared by writing the MCR:SWR bit,
170 * set bit twice to completely reset
171 */
172 err = reset_device(dev);
173 if (err)
174 return err;
175
176 err = reset_device(dev);
177 if (err)
178 return err;
179
180 /* reset channels */
181 for (ch = 0; ch < priv->num_channels; ch++) {
182 err = reset_channel(dev, ch);
183 if (err)
184 return err;
185 }
186
187 /* enable channel done and error interrupts */
188 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
189 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
190
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800191 /* disable integrity check error interrupts (use writeback instead) */
192 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
193 setbits32(priv->reg + TALITOS_MDEUICR_LO,
194 TALITOS_MDEUICR_LO_ICE);
195
Kim Phillips9c4a7962008-06-23 19:50:15 +0800196 return 0;
197}
198
199/**
200 * talitos_submit - submits a descriptor to the device for processing
201 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800202 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800203 * @desc: the descriptor to be processed by the device
204 * @callback: whom to call when processing is complete
205 * @context: a handle for use by caller (optional)
206 *
207 * desc must contain valid dma-mapped (bus physical) address pointers.
208 * callback must check err and feedback in descriptor header
209 * for device processing status.
210 */
Horia Geanta865d5062012-07-03 19:16:52 +0300211int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
212 void (*callback)(struct device *dev,
213 struct talitos_desc *desc,
214 void *context, int error),
215 void *context)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800216{
217 struct talitos_private *priv = dev_get_drvdata(dev);
218 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800219 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800220 int head;
221
Kim Phillips4b9926282009-08-13 11:50:38 +1000222 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800223
Kim Phillips4b9926282009-08-13 11:50:38 +1000224 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800225 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000226 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800227 return -EAGAIN;
228 }
229
Kim Phillips4b9926282009-08-13 11:50:38 +1000230 head = priv->chan[ch].head;
231 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800232
Kim Phillips9c4a7962008-06-23 19:50:15 +0800233 /* map descriptor and save caller data */
234 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
235 DMA_BIDIRECTIONAL);
236 request->callback = callback;
237 request->context = context;
238
239 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000240 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800241
242 smp_wmb();
243 request->desc = desc;
244
245 /* GO! */
246 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800247 out_be32(priv->chan[ch].reg + TALITOS_FF,
248 upper_32_bits(request->dma_desc));
249 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800250 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800251
Kim Phillips4b9926282009-08-13 11:50:38 +1000252 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800253
254 return -EINPROGRESS;
255}
Horia Geanta865d5062012-07-03 19:16:52 +0300256EXPORT_SYMBOL(talitos_submit);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800257
258/*
259 * process what was done, notify callback of error if not
260 */
261static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
262{
263 struct talitos_private *priv = dev_get_drvdata(dev);
264 struct talitos_request *request, saved_req;
265 unsigned long flags;
266 int tail, status;
267
Kim Phillips4b9926282009-08-13 11:50:38 +1000268 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800269
Kim Phillips4b9926282009-08-13 11:50:38 +1000270 tail = priv->chan[ch].tail;
271 while (priv->chan[ch].fifo[tail].desc) {
272 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800273
274 /* descriptors with their done bits set don't get the error */
275 rmb();
Lee Nipperca38a812008-12-20 17:09:25 +1100276 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800277 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100278 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800279 if (!error)
280 break;
281 else
282 status = error;
283
284 dma_unmap_single(dev, request->dma_desc,
Kim Phillipse938e462009-03-29 15:53:23 +0800285 sizeof(struct talitos_desc),
286 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800287
288 /* copy entries so we can call callback outside lock */
289 saved_req.desc = request->desc;
290 saved_req.callback = request->callback;
291 saved_req.context = request->context;
292
293 /* release request entry in fifo */
294 smp_wmb();
295 request->desc = NULL;
296
297 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000298 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800299
Kim Phillips4b9926282009-08-13 11:50:38 +1000300 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800301
Kim Phillips4b9926282009-08-13 11:50:38 +1000302 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800303
Kim Phillips9c4a7962008-06-23 19:50:15 +0800304 saved_req.callback(dev, saved_req.desc, saved_req.context,
305 status);
306 /* channel may resume processing in single desc error case */
307 if (error && !reset_ch && status == error)
308 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000309 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
310 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800311 }
312
Kim Phillips4b9926282009-08-13 11:50:38 +1000313 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800314}
315
316/*
317 * process completed requests for channels that have done status
318 */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800319#define DEF_TALITOS_DONE(name, ch_done_mask) \
320static void talitos_done_##name(unsigned long data) \
321{ \
322 struct device *dev = (struct device *)data; \
323 struct talitos_private *priv = dev_get_drvdata(dev); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300324 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800325 \
326 if (ch_done_mask & 1) \
327 flush_channel(dev, 0, 0, 0); \
328 if (priv->num_channels == 1) \
329 goto out; \
330 if (ch_done_mask & (1 << 2)) \
331 flush_channel(dev, 1, 0, 0); \
332 if (ch_done_mask & (1 << 4)) \
333 flush_channel(dev, 2, 0, 0); \
334 if (ch_done_mask & (1 << 6)) \
335 flush_channel(dev, 3, 0, 0); \
336 \
337out: \
338 /* At this point, all completed channels have been processed */ \
339 /* Unmask done interrupts for channels completed later on. */ \
Horia Geanta511d63c2012-03-30 17:49:53 +0300340 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800341 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
342 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300343 spin_unlock_irqrestore(&priv->reg_lock, flags); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800344}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800345DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
346DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
347DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800348
349/*
350 * locate current (offending) descriptor
351 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200352static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800353{
354 struct talitos_private *priv = dev_get_drvdata(dev);
Horia Geantab62ffd82013-11-13 12:20:37 +0200355 int tail, iter;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800356 dma_addr_t cur_desc;
357
Horia Geantab62ffd82013-11-13 12:20:37 +0200358 cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
359 cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800360
Horia Geantab62ffd82013-11-13 12:20:37 +0200361 if (!cur_desc) {
362 dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
363 return 0;
364 }
365
366 tail = priv->chan[ch].tail;
367
368 iter = tail;
369 while (priv->chan[ch].fifo[iter].dma_desc != cur_desc) {
370 iter = (iter + 1) & (priv->fifo_len - 1);
371 if (iter == tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800372 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200373 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800374 }
375 }
376
Horia Geantab62ffd82013-11-13 12:20:37 +0200377 return priv->chan[ch].fifo[iter].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800378}
379
380/*
381 * user diagnostics; report root cause of error based on execution unit status
382 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200383static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800384{
385 struct talitos_private *priv = dev_get_drvdata(dev);
386 int i;
387
Kim Phillips3e721ae2011-10-21 15:20:28 +0200388 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800389 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200390
391 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800392 case DESC_HDR_SEL0_AFEU:
393 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
394 in_be32(priv->reg + TALITOS_AFEUISR),
395 in_be32(priv->reg + TALITOS_AFEUISR_LO));
396 break;
397 case DESC_HDR_SEL0_DEU:
398 dev_err(dev, "DEUISR 0x%08x_%08x\n",
399 in_be32(priv->reg + TALITOS_DEUISR),
400 in_be32(priv->reg + TALITOS_DEUISR_LO));
401 break;
402 case DESC_HDR_SEL0_MDEUA:
403 case DESC_HDR_SEL0_MDEUB:
404 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
405 in_be32(priv->reg + TALITOS_MDEUISR),
406 in_be32(priv->reg + TALITOS_MDEUISR_LO));
407 break;
408 case DESC_HDR_SEL0_RNG:
409 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
410 in_be32(priv->reg + TALITOS_RNGUISR),
411 in_be32(priv->reg + TALITOS_RNGUISR_LO));
412 break;
413 case DESC_HDR_SEL0_PKEU:
414 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
415 in_be32(priv->reg + TALITOS_PKEUISR),
416 in_be32(priv->reg + TALITOS_PKEUISR_LO));
417 break;
418 case DESC_HDR_SEL0_AESU:
419 dev_err(dev, "AESUISR 0x%08x_%08x\n",
420 in_be32(priv->reg + TALITOS_AESUISR),
421 in_be32(priv->reg + TALITOS_AESUISR_LO));
422 break;
423 case DESC_HDR_SEL0_CRCU:
424 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
425 in_be32(priv->reg + TALITOS_CRCUISR),
426 in_be32(priv->reg + TALITOS_CRCUISR_LO));
427 break;
428 case DESC_HDR_SEL0_KEU:
429 dev_err(dev, "KEUISR 0x%08x_%08x\n",
430 in_be32(priv->reg + TALITOS_KEUISR),
431 in_be32(priv->reg + TALITOS_KEUISR_LO));
432 break;
433 }
434
Kim Phillips3e721ae2011-10-21 15:20:28 +0200435 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800436 case DESC_HDR_SEL1_MDEUA:
437 case DESC_HDR_SEL1_MDEUB:
438 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
439 in_be32(priv->reg + TALITOS_MDEUISR),
440 in_be32(priv->reg + TALITOS_MDEUISR_LO));
441 break;
442 case DESC_HDR_SEL1_CRCU:
443 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
444 in_be32(priv->reg + TALITOS_CRCUISR),
445 in_be32(priv->reg + TALITOS_CRCUISR_LO));
446 break;
447 }
448
449 for (i = 0; i < 8; i++)
450 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800451 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
452 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800453}
454
455/*
456 * recover from error interrupts
457 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600458static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800459{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800460 struct talitos_private *priv = dev_get_drvdata(dev);
461 unsigned int timeout = TALITOS_TIMEOUT;
462 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800463 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800464
465 for (ch = 0; ch < priv->num_channels; ch++) {
466 /* skip channels without errors */
467 if (!(isr & (1 << (ch * 2 + 1))))
468 continue;
469
470 error = -EINVAL;
471
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800472 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
473 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800474
475 if (v_lo & TALITOS_CCPSR_LO_DOF) {
476 dev_err(dev, "double fetch fifo overflow error\n");
477 error = -EAGAIN;
478 reset_ch = 1;
479 }
480 if (v_lo & TALITOS_CCPSR_LO_SOF) {
481 /* h/w dropped descriptor */
482 dev_err(dev, "single fetch fifo overflow error\n");
483 error = -EAGAIN;
484 }
485 if (v_lo & TALITOS_CCPSR_LO_MDTE)
486 dev_err(dev, "master data transfer error\n");
487 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
488 dev_err(dev, "s/g data length zero error\n");
489 if (v_lo & TALITOS_CCPSR_LO_FPZ)
490 dev_err(dev, "fetch pointer zero error\n");
491 if (v_lo & TALITOS_CCPSR_LO_IDH)
492 dev_err(dev, "illegal descriptor header error\n");
493 if (v_lo & TALITOS_CCPSR_LO_IEU)
494 dev_err(dev, "invalid execution unit error\n");
495 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200496 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800497 if (v_lo & TALITOS_CCPSR_LO_GB)
498 dev_err(dev, "gather boundary error\n");
499 if (v_lo & TALITOS_CCPSR_LO_GRL)
500 dev_err(dev, "gather return/length error\n");
501 if (v_lo & TALITOS_CCPSR_LO_SB)
502 dev_err(dev, "scatter boundary error\n");
503 if (v_lo & TALITOS_CCPSR_LO_SRL)
504 dev_err(dev, "scatter return/length error\n");
505
506 flush_channel(dev, ch, error, reset_ch);
507
508 if (reset_ch) {
509 reset_channel(dev, ch);
510 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800511 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800512 TALITOS_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800513 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
514 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800515 TALITOS_CCCR_CONT) && --timeout)
516 cpu_relax();
517 if (timeout == 0) {
518 dev_err(dev, "failed to restart channel %d\n",
519 ch);
520 reset_dev = 1;
521 }
522 }
523 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800524 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800525 dev_err(dev, "done overflow, internal time out, or rngu error: "
526 "ISR 0x%08x_%08x\n", isr, isr_lo);
527
528 /* purge request queues */
529 for (ch = 0; ch < priv->num_channels; ch++)
530 flush_channel(dev, ch, -EIO, 1);
531
532 /* reset and reinitialize the device */
533 init_device(dev);
534 }
535}
536
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800537#define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
538static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
539{ \
540 struct device *dev = data; \
541 struct talitos_private *priv = dev_get_drvdata(dev); \
542 u32 isr, isr_lo; \
Horia Geanta511d63c2012-03-30 17:49:53 +0300543 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800544 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300545 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800546 isr = in_be32(priv->reg + TALITOS_ISR); \
547 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
548 /* Acknowledge interrupt */ \
549 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
550 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
551 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300552 if (unlikely(isr & ch_err_mask || isr_lo)) { \
553 spin_unlock_irqrestore(&priv->reg_lock, flags); \
554 talitos_error(dev, isr & ch_err_mask, isr_lo); \
555 } \
556 else { \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800557 if (likely(isr & ch_done_mask)) { \
558 /* mask further done interrupts. */ \
559 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
560 /* done_task will unmask done interrupts at exit */ \
561 tasklet_schedule(&priv->done_task[tlet]); \
562 } \
Horia Geanta511d63c2012-03-30 17:49:53 +0300563 spin_unlock_irqrestore(&priv->reg_lock, flags); \
564 } \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800565 \
566 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
567 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800568}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800569DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
570DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
571DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800572
573/*
574 * hwrng
575 */
576static int talitos_rng_data_present(struct hwrng *rng, int wait)
577{
578 struct device *dev = (struct device *)rng->priv;
579 struct talitos_private *priv = dev_get_drvdata(dev);
580 u32 ofl;
581 int i;
582
583 for (i = 0; i < 20; i++) {
584 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
585 TALITOS_RNGUSR_LO_OFL;
586 if (ofl || !wait)
587 break;
588 udelay(10);
589 }
590
591 return !!ofl;
592}
593
594static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
595{
596 struct device *dev = (struct device *)rng->priv;
597 struct talitos_private *priv = dev_get_drvdata(dev);
598
599 /* rng fifo requires 64-bit accesses */
600 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
601 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
602
603 return sizeof(u32);
604}
605
606static int talitos_rng_init(struct hwrng *rng)
607{
608 struct device *dev = (struct device *)rng->priv;
609 struct talitos_private *priv = dev_get_drvdata(dev);
610 unsigned int timeout = TALITOS_TIMEOUT;
611
612 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
613 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
614 && --timeout)
615 cpu_relax();
616 if (timeout == 0) {
617 dev_err(dev, "failed to reset rng hw\n");
618 return -ENODEV;
619 }
620
621 /* start generating */
622 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
623
624 return 0;
625}
626
627static int talitos_register_rng(struct device *dev)
628{
629 struct talitos_private *priv = dev_get_drvdata(dev);
630
631 priv->rng.name = dev_driver_string(dev),
632 priv->rng.init = talitos_rng_init,
633 priv->rng.data_present = talitos_rng_data_present,
634 priv->rng.data_read = talitos_rng_data_read,
635 priv->rng.priv = (unsigned long)dev;
636
637 return hwrng_register(&priv->rng);
638}
639
640static void talitos_unregister_rng(struct device *dev)
641{
642 struct talitos_private *priv = dev_get_drvdata(dev);
643
644 hwrng_unregister(&priv->rng);
645}
646
647/*
648 * crypto alg
649 */
650#define TALITOS_CRA_PRIORITY 3000
Horia Geanta357fb602012-07-03 19:16:53 +0300651#define TALITOS_MAX_KEY_SIZE 96
Lee Nipper3952f172008-07-10 18:29:18 +0800652#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800653
Kim Phillips9c4a7962008-06-23 19:50:15 +0800654struct talitos_ctx {
655 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800656 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800657 __be32 desc_hdr_template;
658 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800659 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800660 unsigned int keylen;
661 unsigned int enckeylen;
662 unsigned int authkeylen;
663 unsigned int authsize;
664};
665
Lee Nipper497f2e62010-05-19 19:20:36 +1000666#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
667#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
668
669struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000670 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000671 unsigned int hw_context_size;
672 u8 buf[HASH_MAX_BLOCK_SIZE];
673 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000674 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000675 unsigned int first;
676 unsigned int last;
677 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000678 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000679 struct scatterlist bufsl[2];
680 struct scatterlist *psrc;
681};
682
Lee Nipper56af8cd2009-03-29 15:50:50 +0800683static int aead_setauthsize(struct crypto_aead *authenc,
684 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800685{
686 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
687
688 ctx->authsize = authsize;
689
690 return 0;
691}
692
Lee Nipper56af8cd2009-03-29 15:50:50 +0800693static int aead_setkey(struct crypto_aead *authenc,
694 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800695{
696 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Mathias Krausec306a982013-10-15 13:49:34 +0200697 struct crypto_authenc_keys keys;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800698
Mathias Krausec306a982013-10-15 13:49:34 +0200699 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800700 goto badkey;
701
Mathias Krausec306a982013-10-15 13:49:34 +0200702 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800703 goto badkey;
704
Mathias Krausec306a982013-10-15 13:49:34 +0200705 memcpy(ctx->key, keys.authkey, keys.authkeylen);
706 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800707
Mathias Krausec306a982013-10-15 13:49:34 +0200708 ctx->keylen = keys.authkeylen + keys.enckeylen;
709 ctx->enckeylen = keys.enckeylen;
710 ctx->authkeylen = keys.authkeylen;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800711
712 return 0;
713
714badkey:
715 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
716 return -EINVAL;
717}
718
719/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800720 * talitos_edesc - s/w-extended descriptor
Horia Geanta79fd31d2012-08-02 17:16:40 +0300721 * @assoc_nents: number of segments in associated data scatterlist
Kim Phillips9c4a7962008-06-23 19:50:15 +0800722 * @src_nents: number of segments in input scatterlist
723 * @dst_nents: number of segments in output scatterlist
Horia Geanta79fd31d2012-08-02 17:16:40 +0300724 * @assoc_chained: whether assoc is chained or not
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300725 * @src_chained: whether src is chained or not
726 * @dst_chained: whether dst is chained or not
Horia Geanta79fd31d2012-08-02 17:16:40 +0300727 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips9c4a7962008-06-23 19:50:15 +0800728 * @dma_len: length of dma mapped link_tbl space
729 * @dma_link_tbl: bus physical address of link_tbl
730 * @desc: h/w descriptor
731 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
732 *
733 * if decrypting (with authcheck), or either one of src_nents or dst_nents
734 * is greater than 1, an integrity check value is concatenated to the end
735 * of link_tbl data
736 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800737struct talitos_edesc {
Horia Geanta79fd31d2012-08-02 17:16:40 +0300738 int assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800739 int src_nents;
740 int dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300741 bool assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300742 bool src_chained;
743 bool dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300744 dma_addr_t iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800745 int dma_len;
746 dma_addr_t dma_link_tbl;
747 struct talitos_desc desc;
748 struct talitos_ptr link_tbl[0];
749};
750
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800751static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
752 unsigned int nents, enum dma_data_direction dir,
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300753 bool chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800754{
755 if (unlikely(chained))
756 while (sg) {
757 dma_map_sg(dev, sg, 1, dir);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200758 sg = sg_next(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800759 }
760 else
761 dma_map_sg(dev, sg, nents, dir);
762 return nents;
763}
764
765static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
766 enum dma_data_direction dir)
767{
768 while (sg) {
769 dma_unmap_sg(dev, sg, 1, dir);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200770 sg = sg_next(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800771 }
772}
773
774static void talitos_sg_unmap(struct device *dev,
775 struct talitos_edesc *edesc,
776 struct scatterlist *src,
777 struct scatterlist *dst)
778{
779 unsigned int src_nents = edesc->src_nents ? : 1;
780 unsigned int dst_nents = edesc->dst_nents ? : 1;
781
782 if (src != dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300783 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800784 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
785 else
786 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
787
Lee Nipper497f2e62010-05-19 19:20:36 +1000788 if (dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300789 if (edesc->dst_chained)
Lee Nipper497f2e62010-05-19 19:20:36 +1000790 talitos_unmap_sg_chain(dev, dst,
791 DMA_FROM_DEVICE);
792 else
793 dma_unmap_sg(dev, dst, dst_nents,
794 DMA_FROM_DEVICE);
795 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800796 } else
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300797 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800798 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
799 else
800 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
801}
802
Kim Phillips9c4a7962008-06-23 19:50:15 +0800803static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800804 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800805 struct aead_request *areq)
806{
807 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
808 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
809 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
810 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
811
Horia Geanta79fd31d2012-08-02 17:16:40 +0300812 if (edesc->assoc_chained)
813 talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
Horia Geanta935e99a2013-11-19 14:57:49 +0200814 else if (areq->assoclen)
Horia Geanta79fd31d2012-08-02 17:16:40 +0300815 /* assoc_nents counts also for IV in non-contiguous cases */
816 dma_unmap_sg(dev, areq->assoc,
817 edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
818 DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800819
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800820 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800821
822 if (edesc->dma_len)
823 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
824 DMA_BIDIRECTIONAL);
825}
826
827/*
828 * ipsec_esp descriptor callbacks
829 */
830static void ipsec_esp_encrypt_done(struct device *dev,
831 struct talitos_desc *desc, void *context,
832 int err)
833{
834 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800835 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
836 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800837 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800838 struct scatterlist *sg;
839 void *icvdata;
840
Kim Phillips19bbbc62009-03-29 15:53:59 +0800841 edesc = container_of(desc, struct talitos_edesc, desc);
842
Kim Phillips9c4a7962008-06-23 19:50:15 +0800843 ipsec_esp_unmap(dev, edesc, areq);
844
845 /* copy the generated ICV to dst */
Horia Geanta60542502012-08-02 17:16:37 +0300846 if (edesc->dst_nents) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800847 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300848 edesc->dst_nents + 2 +
849 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800850 sg = sg_last(areq->dst, edesc->dst_nents);
851 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
852 icvdata, ctx->authsize);
853 }
854
855 kfree(edesc);
856
857 aead_request_complete(areq, err);
858}
859
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800860static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800861 struct talitos_desc *desc,
862 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800863{
864 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800865 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
866 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800867 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800868 struct scatterlist *sg;
869 void *icvdata;
870
Kim Phillips19bbbc62009-03-29 15:53:59 +0800871 edesc = container_of(desc, struct talitos_edesc, desc);
872
Kim Phillips9c4a7962008-06-23 19:50:15 +0800873 ipsec_esp_unmap(dev, edesc, req);
874
875 if (!err) {
876 /* auth check */
877 if (edesc->dma_len)
878 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300879 edesc->dst_nents + 2 +
880 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800881 else
882 icvdata = &edesc->link_tbl[0];
883
884 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
885 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
886 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
887 }
888
889 kfree(edesc);
890
891 aead_request_complete(req, err);
892}
893
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800894static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800895 struct talitos_desc *desc,
896 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800897{
898 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800899 struct talitos_edesc *edesc;
900
901 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800902
903 ipsec_esp_unmap(dev, edesc, req);
904
905 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800906 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
907 DESC_HDR_LO_ICCR1_PASS))
908 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800909
910 kfree(edesc);
911
912 aead_request_complete(req, err);
913}
914
Kim Phillips9c4a7962008-06-23 19:50:15 +0800915/*
916 * convert scatterlist to SEC h/w link table format
917 * stop at cryptlen bytes
918 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800919static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800920 int cryptlen, struct talitos_ptr *link_tbl_ptr)
921{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800922 int n_sg = sg_count;
923
924 while (n_sg--) {
Kim Phillips81eb0242009-08-13 11:51:51 +1000925 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800926 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
927 link_tbl_ptr->j_extent = 0;
928 link_tbl_ptr++;
929 cryptlen -= sg_dma_len(sg);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200930 sg = sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800931 }
932
Lee Nipper70bcaca2008-07-03 19:08:46 +0800933 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800934 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800935 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800936 /* Empty this entry, and move to previous one */
937 cryptlen += be16_to_cpu(link_tbl_ptr->len);
938 link_tbl_ptr->len = 0;
939 sg_count--;
940 link_tbl_ptr--;
941 }
Wei Yongjun7291a932012-09-28 12:52:25 +0800942 be16_add_cpu(&link_tbl_ptr->len, cryptlen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800943
944 /* tag end of link table */
945 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +0800946
947 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800948}
949
950/*
951 * fill in and submit ipsec_esp descriptor
952 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800953static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Horia Geanta79fd31d2012-08-02 17:16:40 +0300954 u64 seq, void (*callback) (struct device *dev,
955 struct talitos_desc *desc,
956 void *context, int error))
Kim Phillips9c4a7962008-06-23 19:50:15 +0800957{
958 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
959 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
960 struct device *dev = ctx->dev;
961 struct talitos_desc *desc = &edesc->desc;
962 unsigned int cryptlen = areq->cryptlen;
963 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +1000964 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +0800965 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800966 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800967
968 /* hmac key */
969 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
LEROY Christophea2b35aa2015-04-17 16:31:57 +0200970 DMA_TO_DEVICE);
Horia Geanta79fd31d2012-08-02 17:16:40 +0300971
Kim Phillips9c4a7962008-06-23 19:50:15 +0800972 /* hmac data */
Horia Geanta79fd31d2012-08-02 17:16:40 +0300973 desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
974 if (edesc->assoc_nents) {
975 int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
976 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
977
978 to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
979 sizeof(struct talitos_ptr));
980 desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
981
982 /* assoc_nents - 1 entries for assoc, 1 for IV */
983 sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
984 areq->assoclen, tbl_ptr);
985
986 /* add IV to link table */
987 tbl_ptr += sg_count - 1;
988 tbl_ptr->j_extent = 0;
989 tbl_ptr++;
990 to_talitos_ptr(tbl_ptr, edesc->iv_dma);
991 tbl_ptr->len = cpu_to_be16(ivsize);
992 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
993
994 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
995 edesc->dma_len, DMA_BIDIRECTIONAL);
996 } else {
Horia Geanta935e99a2013-11-19 14:57:49 +0200997 if (areq->assoclen)
998 to_talitos_ptr(&desc->ptr[1],
999 sg_dma_address(areq->assoc));
1000 else
1001 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001002 desc->ptr[1].j_extent = 0;
1003 }
1004
Kim Phillips9c4a7962008-06-23 19:50:15 +08001005 /* cipher iv */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001006 to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
1007 desc->ptr[2].len = cpu_to_be16(ivsize);
1008 desc->ptr[2].j_extent = 0;
1009 /* Sync needed for the aead_givencrypt case */
1010 dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001011
1012 /* cipher key */
1013 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001014 (char *)&ctx->key + ctx->authkeylen,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001015 DMA_TO_DEVICE);
1016
1017 /*
1018 * cipher in
1019 * map and adjust cipher len to aead request cryptlen.
1020 * extent is bytes of HMAC postpended to ciphertext,
1021 * typically 12 for ipsec
1022 */
1023 desc->ptr[4].len = cpu_to_be16(cryptlen);
1024 desc->ptr[4].j_extent = authsize;
1025
Kim Phillipse938e462009-03-29 15:53:23 +08001026 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1027 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1028 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001029 edesc->src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001030
1031 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001032 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001033 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001034 sg_link_tbl_len = cryptlen;
1035
Kim Phillips962a9c92009-03-29 15:54:30 +08001036 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001037 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001038
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001039 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001040 &edesc->link_tbl[0]);
1041 if (sg_count > 1) {
1042 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
Kim Phillips81eb0242009-08-13 11:51:51 +10001043 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
Kim Phillipse938e462009-03-29 15:53:23 +08001044 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1045 edesc->dma_len,
1046 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001047 } else {
1048 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001049 to_talitos_ptr(&desc->ptr[4],
1050 sg_dma_address(areq->src));
Lee Nipper70bcaca2008-07-03 19:08:46 +08001051 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001052 }
1053
1054 /* cipher out */
1055 desc->ptr[5].len = cpu_to_be16(cryptlen);
1056 desc->ptr[5].j_extent = authsize;
1057
Kim Phillipse938e462009-03-29 15:53:23 +08001058 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001059 sg_count = talitos_map_sg(dev, areq->dst,
1060 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001061 DMA_FROM_DEVICE, edesc->dst_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001062
1063 if (sg_count == 1) {
Kim Phillips81eb0242009-08-13 11:51:51 +10001064 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001065 } else {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001066 int tbl_off = edesc->src_nents + 1;
1067 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001068
Kim Phillips81eb0242009-08-13 11:51:51 +10001069 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
Horia Geanta79fd31d2012-08-02 17:16:40 +03001070 tbl_off * sizeof(struct talitos_ptr));
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001071 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001072 tbl_ptr);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001073
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001074 /* Add an entry to the link table for ICV data */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001075 tbl_ptr += sg_count - 1;
1076 tbl_ptr->j_extent = 0;
1077 tbl_ptr++;
1078 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1079 tbl_ptr->len = cpu_to_be16(authsize);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001080
1081 /* icv data follows link tables */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001082 to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1083 (tbl_off + edesc->dst_nents + 1 +
1084 edesc->assoc_nents) *
Kim Phillips81eb0242009-08-13 11:51:51 +10001085 sizeof(struct talitos_ptr));
Kim Phillips9c4a7962008-06-23 19:50:15 +08001086 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1087 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1088 edesc->dma_len, DMA_BIDIRECTIONAL);
1089 }
1090
1091 /* iv out */
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001092 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001093 DMA_FROM_DEVICE);
1094
Kim Phillips5228f0f2011-07-15 11:21:38 +08001095 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001096 if (ret != -EINPROGRESS) {
1097 ipsec_esp_unmap(dev, edesc, areq);
1098 kfree(edesc);
1099 }
1100 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001101}
1102
Kim Phillips9c4a7962008-06-23 19:50:15 +08001103/*
1104 * derive number of elements in scatterlist
1105 */
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001106static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001107{
1108 struct scatterlist *sg = sg_list;
1109 int sg_nents = 0;
1110
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001111 *chained = false;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001112 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001113 sg_nents++;
1114 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001115 if (!sg_is_last(sg) && (sg + 1)->length == 0)
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001116 *chained = true;
Cristian Stoica5be4d4c2015-01-20 10:06:16 +02001117 sg = sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001118 }
1119
1120 return sg_nents;
1121}
1122
1123/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001124 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001125 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001126static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001127 struct scatterlist *assoc,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001128 struct scatterlist *src,
1129 struct scatterlist *dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001130 u8 *iv,
1131 unsigned int assoclen,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001132 unsigned int cryptlen,
1133 unsigned int authsize,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001134 unsigned int ivsize,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001135 int icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001136 u32 cryptoflags,
1137 bool encrypt)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001138{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001139 struct talitos_edesc *edesc;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001140 int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1141 bool assoc_chained = false, src_chained = false, dst_chained = false;
1142 dma_addr_t iv_dma = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001143 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001144 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001145
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001146 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1147 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001148 return ERR_PTR(-EINVAL);
1149 }
1150
Horia Geanta935e99a2013-11-19 14:57:49 +02001151 if (ivsize)
Horia Geanta79fd31d2012-08-02 17:16:40 +03001152 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1153
Horia Geanta935e99a2013-11-19 14:57:49 +02001154 if (assoclen) {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001155 /*
1156 * Currently it is assumed that iv is provided whenever assoc
1157 * is.
1158 */
1159 BUG_ON(!iv);
1160
1161 assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1162 talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1163 assoc_chained);
1164 assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1165
1166 if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1167 assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1168 }
1169
Horia Geanta62293a32013-11-28 15:11:17 +02001170 if (!dst || dst == src) {
1171 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
1172 src_nents = (src_nents == 1) ? 0 : src_nents;
1173 dst_nents = dst ? src_nents : 0;
1174 } else { /* dst && dst != src*/
1175 src_nents = sg_count(src, cryptlen + (encrypt ? 0 : authsize),
1176 &src_chained);
1177 src_nents = (src_nents == 1) ? 0 : src_nents;
1178 dst_nents = sg_count(dst, cryptlen + (encrypt ? authsize : 0),
1179 &dst_chained);
1180 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001181 }
1182
1183 /*
1184 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001185 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001186 * and the ICV data itself
1187 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001188 alloc_len = sizeof(struct talitos_edesc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001189 if (assoc_nents || src_nents || dst_nents) {
1190 dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
1191 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001192 alloc_len += dma_len;
1193 } else {
1194 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001195 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001196 }
1197
Kim Phillips586725f2008-07-17 20:19:18 +08001198 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001199 if (!edesc) {
Horia Geanta935e99a2013-11-19 14:57:49 +02001200 if (assoc_chained)
1201 talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1202 else if (assoclen)
1203 dma_unmap_sg(dev, assoc,
1204 assoc_nents ? assoc_nents - 1 : 1,
1205 DMA_TO_DEVICE);
1206
Horia Geanta79fd31d2012-08-02 17:16:40 +03001207 if (iv_dma)
1208 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Horia Geanta935e99a2013-11-19 14:57:49 +02001209
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001210 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001211 return ERR_PTR(-ENOMEM);
1212 }
1213
Horia Geanta79fd31d2012-08-02 17:16:40 +03001214 edesc->assoc_nents = assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001215 edesc->src_nents = src_nents;
1216 edesc->dst_nents = dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001217 edesc->assoc_chained = assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001218 edesc->src_chained = src_chained;
1219 edesc->dst_chained = dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001220 edesc->iv_dma = iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001221 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001222 if (dma_len)
1223 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1224 edesc->dma_len,
1225 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001226
1227 return edesc;
1228}
1229
Horia Geanta79fd31d2012-08-02 17:16:40 +03001230static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
Horia Geanta62293a32013-11-28 15:11:17 +02001231 int icv_stashing, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001232{
1233 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1234 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001235 unsigned int ivsize = crypto_aead_ivsize(authenc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001236
Horia Geanta79fd31d2012-08-02 17:16:40 +03001237 return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1238 iv, areq->assoclen, areq->cryptlen,
1239 ctx->authsize, ivsize, icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001240 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001241}
1242
Lee Nipper56af8cd2009-03-29 15:50:50 +08001243static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001244{
1245 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1246 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001247 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001248
1249 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001250 edesc = aead_edesc_alloc(req, req->iv, 0, true);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001251 if (IS_ERR(edesc))
1252 return PTR_ERR(edesc);
1253
1254 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001255 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001256
Horia Geanta79fd31d2012-08-02 17:16:40 +03001257 return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001258}
1259
Lee Nipper56af8cd2009-03-29 15:50:50 +08001260static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001261{
1262 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1263 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1264 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001265 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001266 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001267 struct scatterlist *sg;
1268 void *icvdata;
1269
1270 req->cryptlen -= authsize;
1271
1272 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001273 edesc = aead_edesc_alloc(req, req->iv, 1, false);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001274 if (IS_ERR(edesc))
1275 return PTR_ERR(edesc);
1276
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001277 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001278 ((!edesc->src_nents && !edesc->dst_nents) ||
1279 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001280
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001281 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001282 edesc->desc.hdr = ctx->desc_hdr_template |
1283 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001284 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001285
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001286 /* reset integrity check result bits */
1287 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001288
Horia Geanta79fd31d2012-08-02 17:16:40 +03001289 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001290 }
Kim Phillipse938e462009-03-29 15:53:23 +08001291
1292 /* Have to check the ICV with software */
1293 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1294
1295 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1296 if (edesc->dma_len)
1297 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +03001298 edesc->dst_nents + 2 +
1299 edesc->assoc_nents];
Kim Phillipse938e462009-03-29 15:53:23 +08001300 else
1301 icvdata = &edesc->link_tbl[0];
1302
1303 sg = sg_last(req->src, edesc->src_nents ? : 1);
1304
1305 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1306 ctx->authsize);
1307
Horia Geanta79fd31d2012-08-02 17:16:40 +03001308 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001309}
1310
Lee Nipper56af8cd2009-03-29 15:50:50 +08001311static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001312{
1313 struct aead_request *areq = &req->areq;
1314 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1315 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001316 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001317
1318 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001319 edesc = aead_edesc_alloc(areq, req->giv, 0, true);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001320 if (IS_ERR(edesc))
1321 return PTR_ERR(edesc);
1322
1323 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001324 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001325
1326 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001327 /* avoid consecutive packets going out with same IV */
1328 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001329
Horia Geanta79fd31d2012-08-02 17:16:40 +03001330 return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001331}
1332
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001333static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1334 const u8 *key, unsigned int keylen)
1335{
1336 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001337
1338 memcpy(&ctx->key, key, keylen);
1339 ctx->keylen = keylen;
1340
1341 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001342}
1343
LEROY Christophe032d1972015-04-17 16:31:51 +02001344static void unmap_sg_talitos_ptr(struct device *dev, struct scatterlist *src,
1345 struct scatterlist *dst, unsigned int len,
1346 struct talitos_edesc *edesc)
1347{
1348 talitos_sg_unmap(dev, edesc, src, dst);
1349}
1350
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001351static void common_nonsnoop_unmap(struct device *dev,
1352 struct talitos_edesc *edesc,
1353 struct ablkcipher_request *areq)
1354{
1355 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
LEROY Christophe032d1972015-04-17 16:31:51 +02001356
1357 unmap_sg_talitos_ptr(dev, areq->src, areq->dst, areq->nbytes, edesc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001358 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1359 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1360
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001361 if (edesc->dma_len)
1362 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1363 DMA_BIDIRECTIONAL);
1364}
1365
1366static void ablkcipher_done(struct device *dev,
1367 struct talitos_desc *desc, void *context,
1368 int err)
1369{
1370 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001371 struct talitos_edesc *edesc;
1372
1373 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001374
1375 common_nonsnoop_unmap(dev, edesc, areq);
1376
1377 kfree(edesc);
1378
1379 areq->base.complete(&areq->base, err);
1380}
1381
LEROY Christophe032d1972015-04-17 16:31:51 +02001382int map_sg_in_talitos_ptr(struct device *dev, struct scatterlist *src,
1383 unsigned int len, struct talitos_edesc *edesc,
1384 enum dma_data_direction dir, struct talitos_ptr *ptr)
1385{
1386 int sg_count;
1387
LEROY Christophe538caf82015-04-17 16:31:59 +02001388 to_talitos_ptr_len(ptr, len);
LEROY Christophe185eb792015-04-17 16:31:55 +02001389 to_talitos_ptr_extent_clear(ptr);
LEROY Christophe032d1972015-04-17 16:31:51 +02001390
1391 sg_count = talitos_map_sg(dev, src, edesc->src_nents ? : 1, dir,
1392 edesc->src_chained);
1393
1394 if (sg_count == 1) {
1395 to_talitos_ptr(ptr, sg_dma_address(src));
1396 } else {
1397 sg_count = sg_to_link_tbl(src, sg_count, len,
1398 &edesc->link_tbl[0]);
1399 if (sg_count > 1) {
1400 to_talitos_ptr(ptr, edesc->dma_link_tbl);
1401 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1402 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1403 edesc->dma_len,
1404 DMA_BIDIRECTIONAL);
1405 } else {
1406 /* Only one segment now, so no link tbl needed */
1407 to_talitos_ptr(ptr, sg_dma_address(src));
1408 }
1409 }
1410 return sg_count;
1411}
1412
1413void map_sg_out_talitos_ptr(struct device *dev, struct scatterlist *dst,
1414 unsigned int len, struct talitos_edesc *edesc,
1415 enum dma_data_direction dir,
1416 struct talitos_ptr *ptr, int sg_count)
1417{
LEROY Christophe538caf82015-04-17 16:31:59 +02001418 to_talitos_ptr_len(ptr, len);
LEROY Christophe185eb792015-04-17 16:31:55 +02001419 to_talitos_ptr_extent_clear(ptr);
LEROY Christophe032d1972015-04-17 16:31:51 +02001420
1421 if (dir != DMA_NONE)
1422 sg_count = talitos_map_sg(dev, dst, edesc->dst_nents ? : 1,
1423 dir, edesc->dst_chained);
1424
1425 if (sg_count == 1) {
1426 to_talitos_ptr(ptr, sg_dma_address(dst));
1427 } else {
1428 struct talitos_ptr *link_tbl_ptr =
1429 &edesc->link_tbl[edesc->src_nents + 1];
1430
1431 to_talitos_ptr(ptr, edesc->dma_link_tbl +
1432 (edesc->src_nents + 1) *
1433 sizeof(struct talitos_ptr));
1434 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1435 sg_count = sg_to_link_tbl(dst, sg_count, len, link_tbl_ptr);
1436 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1437 edesc->dma_len, DMA_BIDIRECTIONAL);
1438 }
1439}
1440
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001441static int common_nonsnoop(struct talitos_edesc *edesc,
1442 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001443 void (*callback) (struct device *dev,
1444 struct talitos_desc *desc,
1445 void *context, int error))
1446{
1447 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1448 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1449 struct device *dev = ctx->dev;
1450 struct talitos_desc *desc = &edesc->desc;
1451 unsigned int cryptlen = areq->nbytes;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001452 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001453 int sg_count, ret;
1454
1455 /* first DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001456 desc->ptr[0] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001457
1458 /* cipher iv */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001459 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
LEROY Christophe538caf82015-04-17 16:31:59 +02001460 to_talitos_ptr_len(&desc->ptr[1], ivsize);
LEROY Christophe185eb792015-04-17 16:31:55 +02001461 to_talitos_ptr_extent_clear(&desc->ptr[1]);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001462
1463 /* cipher key */
1464 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001465 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001466
1467 /*
1468 * cipher in
1469 */
LEROY Christophe032d1972015-04-17 16:31:51 +02001470 sg_count = map_sg_in_talitos_ptr(dev, areq->src, cryptlen, edesc,
1471 (areq->src == areq->dst) ?
1472 DMA_BIDIRECTIONAL : DMA_TO_DEVICE,
1473 &desc->ptr[3]);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001474
1475 /* cipher out */
LEROY Christophe032d1972015-04-17 16:31:51 +02001476 map_sg_out_talitos_ptr(dev, areq->dst, cryptlen, edesc,
1477 (areq->src == areq->dst) ? DMA_NONE
1478 : DMA_FROM_DEVICE,
1479 &desc->ptr[4], sg_count);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001480
1481 /* iv out */
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001482 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001483 DMA_FROM_DEVICE);
1484
1485 /* last DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001486 desc->ptr[6] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001487
Kim Phillips5228f0f2011-07-15 11:21:38 +08001488 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001489 if (ret != -EINPROGRESS) {
1490 common_nonsnoop_unmap(dev, edesc, areq);
1491 kfree(edesc);
1492 }
1493 return ret;
1494}
1495
Kim Phillipse938e462009-03-29 15:53:23 +08001496static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
Horia Geanta62293a32013-11-28 15:11:17 +02001497 areq, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001498{
1499 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1500 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001501 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001502
Horia Geanta79fd31d2012-08-02 17:16:40 +03001503 return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1504 areq->info, 0, areq->nbytes, 0, ivsize, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001505 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001506}
1507
1508static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1509{
1510 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1511 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1512 struct talitos_edesc *edesc;
1513
1514 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001515 edesc = ablkcipher_edesc_alloc(areq, true);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001516 if (IS_ERR(edesc))
1517 return PTR_ERR(edesc);
1518
1519 /* set encrypt */
1520 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1521
Kim Phillipsfebec542011-07-15 11:21:39 +08001522 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001523}
1524
1525static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1526{
1527 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1528 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1529 struct talitos_edesc *edesc;
1530
1531 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001532 edesc = ablkcipher_edesc_alloc(areq, false);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001533 if (IS_ERR(edesc))
1534 return PTR_ERR(edesc);
1535
1536 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1537
Kim Phillipsfebec542011-07-15 11:21:39 +08001538 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001539}
1540
Lee Nipper497f2e62010-05-19 19:20:36 +10001541static void common_nonsnoop_hash_unmap(struct device *dev,
1542 struct talitos_edesc *edesc,
1543 struct ahash_request *areq)
1544{
1545 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1546
1547 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1548
LEROY Christophe032d1972015-04-17 16:31:51 +02001549 unmap_sg_talitos_ptr(dev, req_ctx->psrc, NULL, 0, edesc);
1550
Lee Nipper497f2e62010-05-19 19:20:36 +10001551 /* When using hashctx-in, must unmap it. */
LEROY Christophe538caf82015-04-17 16:31:59 +02001552 if (from_talitos_ptr_len(&edesc->desc.ptr[1]))
Lee Nipper497f2e62010-05-19 19:20:36 +10001553 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1554 DMA_TO_DEVICE);
1555
LEROY Christophe538caf82015-04-17 16:31:59 +02001556 if (from_talitos_ptr_len(&edesc->desc.ptr[2]))
Lee Nipper497f2e62010-05-19 19:20:36 +10001557 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1558 DMA_TO_DEVICE);
1559
Lee Nipper497f2e62010-05-19 19:20:36 +10001560 if (edesc->dma_len)
1561 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1562 DMA_BIDIRECTIONAL);
1563
1564}
1565
1566static void ahash_done(struct device *dev,
1567 struct talitos_desc *desc, void *context,
1568 int err)
1569{
1570 struct ahash_request *areq = context;
1571 struct talitos_edesc *edesc =
1572 container_of(desc, struct talitos_edesc, desc);
1573 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1574
1575 if (!req_ctx->last && req_ctx->to_hash_later) {
1576 /* Position any partial block for next update/final/finup */
1577 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001578 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001579 }
1580 common_nonsnoop_hash_unmap(dev, edesc, areq);
1581
1582 kfree(edesc);
1583
1584 areq->base.complete(&areq->base, err);
1585}
1586
1587static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1588 struct ahash_request *areq, unsigned int length,
1589 void (*callback) (struct device *dev,
1590 struct talitos_desc *desc,
1591 void *context, int error))
1592{
1593 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1594 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1595 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1596 struct device *dev = ctx->dev;
1597 struct talitos_desc *desc = &edesc->desc;
LEROY Christophe032d1972015-04-17 16:31:51 +02001598 int ret;
Lee Nipper497f2e62010-05-19 19:20:36 +10001599
1600 /* first DWORD empty */
1601 desc->ptr[0] = zero_entry;
1602
Kim Phillips60f208d2010-05-19 19:21:53 +10001603 /* hash context in */
1604 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001605 map_single_talitos_ptr(dev, &desc->ptr[1],
1606 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001607 (char *)req_ctx->hw_context,
Lee Nipper497f2e62010-05-19 19:20:36 +10001608 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001609 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001610 } else {
1611 desc->ptr[1] = zero_entry;
1612 /* Indicate next op is not the first. */
1613 req_ctx->first = 0;
1614 }
1615
1616 /* HMAC key */
1617 if (ctx->keylen)
1618 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001619 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001620 else
1621 desc->ptr[2] = zero_entry;
1622
1623 /*
1624 * data in
1625 */
LEROY Christophe032d1972015-04-17 16:31:51 +02001626 map_sg_in_talitos_ptr(dev, req_ctx->psrc, length, edesc,
1627 DMA_TO_DEVICE, &desc->ptr[3]);
Lee Nipper497f2e62010-05-19 19:20:36 +10001628
1629 /* fifth DWORD empty */
1630 desc->ptr[4] = zero_entry;
1631
1632 /* hash/HMAC out -or- hash context out */
1633 if (req_ctx->last)
1634 map_single_talitos_ptr(dev, &desc->ptr[5],
1635 crypto_ahash_digestsize(tfm),
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001636 areq->result, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001637 else
1638 map_single_talitos_ptr(dev, &desc->ptr[5],
1639 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001640 req_ctx->hw_context, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001641
1642 /* last DWORD empty */
1643 desc->ptr[6] = zero_entry;
1644
Kim Phillips5228f0f2011-07-15 11:21:38 +08001645 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001646 if (ret != -EINPROGRESS) {
1647 common_nonsnoop_hash_unmap(dev, edesc, areq);
1648 kfree(edesc);
1649 }
1650 return ret;
1651}
1652
1653static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1654 unsigned int nbytes)
1655{
1656 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1657 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1658 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1659
Horia Geanta79fd31d2012-08-02 17:16:40 +03001660 return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001661 nbytes, 0, 0, 0, areq->base.flags, false);
Lee Nipper497f2e62010-05-19 19:20:36 +10001662}
1663
1664static int ahash_init(struct ahash_request *areq)
1665{
1666 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1667 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1668
1669 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001670 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001671 req_ctx->first = 1; /* first indicates h/w must init its context */
1672 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001673 req_ctx->hw_context_size =
1674 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1675 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1676 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1677
1678 return 0;
1679}
1680
Kim Phillips60f208d2010-05-19 19:21:53 +10001681/*
1682 * on h/w without explicit sha224 support, we initialize h/w context
1683 * manually with sha224 constants, and tell it to run sha256.
1684 */
1685static int ahash_init_sha224_swinit(struct ahash_request *areq)
1686{
1687 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1688
1689 ahash_init(areq);
1690 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1691
Kim Phillipsa7524472010-09-23 15:56:38 +08001692 req_ctx->hw_context[0] = SHA224_H0;
1693 req_ctx->hw_context[1] = SHA224_H1;
1694 req_ctx->hw_context[2] = SHA224_H2;
1695 req_ctx->hw_context[3] = SHA224_H3;
1696 req_ctx->hw_context[4] = SHA224_H4;
1697 req_ctx->hw_context[5] = SHA224_H5;
1698 req_ctx->hw_context[6] = SHA224_H6;
1699 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001700
1701 /* init 64-bit count */
1702 req_ctx->hw_context[8] = 0;
1703 req_ctx->hw_context[9] = 0;
1704
1705 return 0;
1706}
1707
Lee Nipper497f2e62010-05-19 19:20:36 +10001708static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1709{
1710 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1711 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1712 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1713 struct talitos_edesc *edesc;
1714 unsigned int blocksize =
1715 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1716 unsigned int nbytes_to_hash;
1717 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001718 unsigned int nsg;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001719 bool chained;
Lee Nipper497f2e62010-05-19 19:20:36 +10001720
Lee Nipper5e833bc2010-06-16 15:29:15 +10001721 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1722 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001723 sg_copy_to_buffer(areq->src,
1724 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001725 req_ctx->buf + req_ctx->nbuf, nbytes);
1726 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001727 return 0;
1728 }
1729
Lee Nipper5e833bc2010-06-16 15:29:15 +10001730 /* At least (blocksize + 1) bytes are available to hash */
1731 nbytes_to_hash = nbytes + req_ctx->nbuf;
1732 to_hash_later = nbytes_to_hash & (blocksize - 1);
1733
1734 if (req_ctx->last)
1735 to_hash_later = 0;
1736 else if (to_hash_later)
1737 /* There is a partial block. Hash the full block(s) now */
1738 nbytes_to_hash -= to_hash_later;
1739 else {
1740 /* Keep one block buffered */
1741 nbytes_to_hash -= blocksize;
1742 to_hash_later = blocksize;
1743 }
1744
1745 /* Chain in any previously buffered data */
1746 if (req_ctx->nbuf) {
1747 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1748 sg_init_table(req_ctx->bufsl, nsg);
1749 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1750 if (nsg > 1)
1751 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001752 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001753 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001754 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001755
Lee Nipper5e833bc2010-06-16 15:29:15 +10001756 if (to_hash_later) {
1757 int nents = sg_count(areq->src, nbytes, &chained);
Akinobu Mitad0525722013-07-08 16:01:55 -07001758 sg_pcopy_to_buffer(areq->src, nents,
Lee Nipper5e833bc2010-06-16 15:29:15 +10001759 req_ctx->bufnext,
1760 to_hash_later,
1761 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001762 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001763 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001764
Lee Nipper5e833bc2010-06-16 15:29:15 +10001765 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001766 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1767 if (IS_ERR(edesc))
1768 return PTR_ERR(edesc);
1769
1770 edesc->desc.hdr = ctx->desc_hdr_template;
1771
1772 /* On last one, request SEC to pad; otherwise continue */
1773 if (req_ctx->last)
1774 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1775 else
1776 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1777
Kim Phillips60f208d2010-05-19 19:21:53 +10001778 /* request SEC to INIT hash. */
1779 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001780 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1781
1782 /* When the tfm context has a keylen, it's an HMAC.
1783 * A first or last (ie. not middle) descriptor must request HMAC.
1784 */
1785 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1786 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1787
1788 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1789 ahash_done);
1790}
1791
1792static int ahash_update(struct ahash_request *areq)
1793{
1794 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1795
1796 req_ctx->last = 0;
1797
1798 return ahash_process_req(areq, areq->nbytes);
1799}
1800
1801static int ahash_final(struct ahash_request *areq)
1802{
1803 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1804
1805 req_ctx->last = 1;
1806
1807 return ahash_process_req(areq, 0);
1808}
1809
1810static int ahash_finup(struct ahash_request *areq)
1811{
1812 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1813
1814 req_ctx->last = 1;
1815
1816 return ahash_process_req(areq, areq->nbytes);
1817}
1818
1819static int ahash_digest(struct ahash_request *areq)
1820{
1821 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001822 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001823
Kim Phillips60f208d2010-05-19 19:21:53 +10001824 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001825 req_ctx->last = 1;
1826
1827 return ahash_process_req(areq, areq->nbytes);
1828}
1829
Lee Nipper79b3a412011-11-21 16:13:25 +08001830struct keyhash_result {
1831 struct completion completion;
1832 int err;
1833};
1834
1835static void keyhash_complete(struct crypto_async_request *req, int err)
1836{
1837 struct keyhash_result *res = req->data;
1838
1839 if (err == -EINPROGRESS)
1840 return;
1841
1842 res->err = err;
1843 complete(&res->completion);
1844}
1845
1846static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1847 u8 *hash)
1848{
1849 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1850
1851 struct scatterlist sg[1];
1852 struct ahash_request *req;
1853 struct keyhash_result hresult;
1854 int ret;
1855
1856 init_completion(&hresult.completion);
1857
1858 req = ahash_request_alloc(tfm, GFP_KERNEL);
1859 if (!req)
1860 return -ENOMEM;
1861
1862 /* Keep tfm keylen == 0 during hash of the long key */
1863 ctx->keylen = 0;
1864 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1865 keyhash_complete, &hresult);
1866
1867 sg_init_one(&sg[0], key, keylen);
1868
1869 ahash_request_set_crypt(req, sg, hash, keylen);
1870 ret = crypto_ahash_digest(req);
1871 switch (ret) {
1872 case 0:
1873 break;
1874 case -EINPROGRESS:
1875 case -EBUSY:
1876 ret = wait_for_completion_interruptible(
1877 &hresult.completion);
1878 if (!ret)
1879 ret = hresult.err;
1880 break;
1881 default:
1882 break;
1883 }
1884 ahash_request_free(req);
1885
1886 return ret;
1887}
1888
1889static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1890 unsigned int keylen)
1891{
1892 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1893 unsigned int blocksize =
1894 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1895 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1896 unsigned int keysize = keylen;
1897 u8 hash[SHA512_DIGEST_SIZE];
1898 int ret;
1899
1900 if (keylen <= blocksize)
1901 memcpy(ctx->key, key, keysize);
1902 else {
1903 /* Must get the hash of the long key */
1904 ret = keyhash(tfm, key, keylen, hash);
1905
1906 if (ret) {
1907 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1908 return -EINVAL;
1909 }
1910
1911 keysize = digestsize;
1912 memcpy(ctx->key, hash, digestsize);
1913 }
1914
1915 ctx->keylen = keysize;
1916
1917 return 0;
1918}
1919
1920
Kim Phillips9c4a7962008-06-23 19:50:15 +08001921struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001922 u32 type;
1923 union {
1924 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001925 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001926 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001927 __be32 desc_hdr_template;
1928};
1929
1930static struct talitos_alg_template driver_algs[] = {
Horia Geanta991155b2013-03-20 16:31:38 +02001931 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001932 { .type = CRYPTO_ALG_TYPE_AEAD,
1933 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001934 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1935 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1936 .cra_blocksize = AES_BLOCK_SIZE,
1937 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001938 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001939 .ivsize = AES_BLOCK_SIZE,
1940 .maxauthsize = SHA1_DIGEST_SIZE,
1941 }
1942 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001943 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1944 DESC_HDR_SEL0_AESU |
1945 DESC_HDR_MODE0_AESU_CBC |
1946 DESC_HDR_SEL1_MDEUA |
1947 DESC_HDR_MODE1_MDEU_INIT |
1948 DESC_HDR_MODE1_MDEU_PAD |
1949 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001950 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001951 { .type = CRYPTO_ALG_TYPE_AEAD,
1952 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001953 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1954 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1955 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1956 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001957 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001958 .ivsize = DES3_EDE_BLOCK_SIZE,
1959 .maxauthsize = SHA1_DIGEST_SIZE,
1960 }
1961 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08001962 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1963 DESC_HDR_SEL0_DEU |
1964 DESC_HDR_MODE0_DEU_CBC |
1965 DESC_HDR_MODE0_DEU_3DES |
1966 DESC_HDR_SEL1_MDEUA |
1967 DESC_HDR_MODE1_MDEU_INIT |
1968 DESC_HDR_MODE1_MDEU_PAD |
1969 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08001970 },
Horia Geanta357fb602012-07-03 19:16:53 +03001971 { .type = CRYPTO_ALG_TYPE_AEAD,
1972 .alg.crypto = {
1973 .cra_name = "authenc(hmac(sha224),cbc(aes))",
1974 .cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1975 .cra_blocksize = AES_BLOCK_SIZE,
1976 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001977 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001978 .ivsize = AES_BLOCK_SIZE,
1979 .maxauthsize = SHA224_DIGEST_SIZE,
1980 }
1981 },
1982 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1983 DESC_HDR_SEL0_AESU |
1984 DESC_HDR_MODE0_AESU_CBC |
1985 DESC_HDR_SEL1_MDEUA |
1986 DESC_HDR_MODE1_MDEU_INIT |
1987 DESC_HDR_MODE1_MDEU_PAD |
1988 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1989 },
1990 { .type = CRYPTO_ALG_TYPE_AEAD,
1991 .alg.crypto = {
1992 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1993 .cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1994 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1995 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03001996 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03001997 .ivsize = DES3_EDE_BLOCK_SIZE,
1998 .maxauthsize = SHA224_DIGEST_SIZE,
1999 }
2000 },
2001 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2002 DESC_HDR_SEL0_DEU |
2003 DESC_HDR_MODE0_DEU_CBC |
2004 DESC_HDR_MODE0_DEU_3DES |
2005 DESC_HDR_SEL1_MDEUA |
2006 DESC_HDR_MODE1_MDEU_INIT |
2007 DESC_HDR_MODE1_MDEU_PAD |
2008 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2009 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002010 { .type = CRYPTO_ALG_TYPE_AEAD,
2011 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002012 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2013 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
2014 .cra_blocksize = AES_BLOCK_SIZE,
2015 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002016 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002017 .ivsize = AES_BLOCK_SIZE,
2018 .maxauthsize = SHA256_DIGEST_SIZE,
2019 }
2020 },
Lee Nipper3952f172008-07-10 18:29:18 +08002021 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2022 DESC_HDR_SEL0_AESU |
2023 DESC_HDR_MODE0_AESU_CBC |
2024 DESC_HDR_SEL1_MDEUA |
2025 DESC_HDR_MODE1_MDEU_INIT |
2026 DESC_HDR_MODE1_MDEU_PAD |
2027 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2028 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002029 { .type = CRYPTO_ALG_TYPE_AEAD,
2030 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002031 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2032 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2033 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2034 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002035 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002036 .ivsize = DES3_EDE_BLOCK_SIZE,
2037 .maxauthsize = SHA256_DIGEST_SIZE,
2038 }
2039 },
Lee Nipper3952f172008-07-10 18:29:18 +08002040 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2041 DESC_HDR_SEL0_DEU |
2042 DESC_HDR_MODE0_DEU_CBC |
2043 DESC_HDR_MODE0_DEU_3DES |
2044 DESC_HDR_SEL1_MDEUA |
2045 DESC_HDR_MODE1_MDEU_INIT |
2046 DESC_HDR_MODE1_MDEU_PAD |
2047 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2048 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002049 { .type = CRYPTO_ALG_TYPE_AEAD,
2050 .alg.crypto = {
Horia Geanta357fb602012-07-03 19:16:53 +03002051 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2052 .cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2053 .cra_blocksize = AES_BLOCK_SIZE,
2054 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002055 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002056 .ivsize = AES_BLOCK_SIZE,
2057 .maxauthsize = SHA384_DIGEST_SIZE,
2058 }
2059 },
2060 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2061 DESC_HDR_SEL0_AESU |
2062 DESC_HDR_MODE0_AESU_CBC |
2063 DESC_HDR_SEL1_MDEUB |
2064 DESC_HDR_MODE1_MDEU_INIT |
2065 DESC_HDR_MODE1_MDEU_PAD |
2066 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2067 },
2068 { .type = CRYPTO_ALG_TYPE_AEAD,
2069 .alg.crypto = {
2070 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2071 .cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2072 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2073 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002074 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002075 .ivsize = DES3_EDE_BLOCK_SIZE,
2076 .maxauthsize = SHA384_DIGEST_SIZE,
2077 }
2078 },
2079 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2080 DESC_HDR_SEL0_DEU |
2081 DESC_HDR_MODE0_DEU_CBC |
2082 DESC_HDR_MODE0_DEU_3DES |
2083 DESC_HDR_SEL1_MDEUB |
2084 DESC_HDR_MODE1_MDEU_INIT |
2085 DESC_HDR_MODE1_MDEU_PAD |
2086 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2087 },
2088 { .type = CRYPTO_ALG_TYPE_AEAD,
2089 .alg.crypto = {
2090 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2091 .cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2092 .cra_blocksize = AES_BLOCK_SIZE,
2093 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002094 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002095 .ivsize = AES_BLOCK_SIZE,
2096 .maxauthsize = SHA512_DIGEST_SIZE,
2097 }
2098 },
2099 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2100 DESC_HDR_SEL0_AESU |
2101 DESC_HDR_MODE0_AESU_CBC |
2102 DESC_HDR_SEL1_MDEUB |
2103 DESC_HDR_MODE1_MDEU_INIT |
2104 DESC_HDR_MODE1_MDEU_PAD |
2105 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2106 },
2107 { .type = CRYPTO_ALG_TYPE_AEAD,
2108 .alg.crypto = {
2109 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2110 .cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2111 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2112 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002113 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002114 .ivsize = DES3_EDE_BLOCK_SIZE,
2115 .maxauthsize = SHA512_DIGEST_SIZE,
2116 }
2117 },
2118 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2119 DESC_HDR_SEL0_DEU |
2120 DESC_HDR_MODE0_DEU_CBC |
2121 DESC_HDR_MODE0_DEU_3DES |
2122 DESC_HDR_SEL1_MDEUB |
2123 DESC_HDR_MODE1_MDEU_INIT |
2124 DESC_HDR_MODE1_MDEU_PAD |
2125 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2126 },
2127 { .type = CRYPTO_ALG_TYPE_AEAD,
2128 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002129 .cra_name = "authenc(hmac(md5),cbc(aes))",
2130 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2131 .cra_blocksize = AES_BLOCK_SIZE,
2132 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002133 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002134 .ivsize = AES_BLOCK_SIZE,
2135 .maxauthsize = MD5_DIGEST_SIZE,
2136 }
2137 },
Lee Nipper3952f172008-07-10 18:29:18 +08002138 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2139 DESC_HDR_SEL0_AESU |
2140 DESC_HDR_MODE0_AESU_CBC |
2141 DESC_HDR_SEL1_MDEUA |
2142 DESC_HDR_MODE1_MDEU_INIT |
2143 DESC_HDR_MODE1_MDEU_PAD |
2144 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2145 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002146 { .type = CRYPTO_ALG_TYPE_AEAD,
2147 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002148 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2149 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2150 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2151 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002152 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002153 .ivsize = DES3_EDE_BLOCK_SIZE,
2154 .maxauthsize = MD5_DIGEST_SIZE,
2155 }
2156 },
Lee Nipper3952f172008-07-10 18:29:18 +08002157 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2158 DESC_HDR_SEL0_DEU |
2159 DESC_HDR_MODE0_DEU_CBC |
2160 DESC_HDR_MODE0_DEU_3DES |
2161 DESC_HDR_SEL1_MDEUA |
2162 DESC_HDR_MODE1_MDEU_INIT |
2163 DESC_HDR_MODE1_MDEU_PAD |
2164 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002165 },
2166 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002167 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2168 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002169 .cra_name = "cbc(aes)",
2170 .cra_driver_name = "cbc-aes-talitos",
2171 .cra_blocksize = AES_BLOCK_SIZE,
2172 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2173 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002174 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002175 .min_keysize = AES_MIN_KEY_SIZE,
2176 .max_keysize = AES_MAX_KEY_SIZE,
2177 .ivsize = AES_BLOCK_SIZE,
2178 }
2179 },
2180 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2181 DESC_HDR_SEL0_AESU |
2182 DESC_HDR_MODE0_AESU_CBC,
2183 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002184 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2185 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002186 .cra_name = "cbc(des3_ede)",
2187 .cra_driver_name = "cbc-3des-talitos",
2188 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2189 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2190 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002191 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002192 .min_keysize = DES3_EDE_KEY_SIZE,
2193 .max_keysize = DES3_EDE_KEY_SIZE,
2194 .ivsize = DES3_EDE_BLOCK_SIZE,
2195 }
2196 },
2197 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2198 DESC_HDR_SEL0_DEU |
2199 DESC_HDR_MODE0_DEU_CBC |
2200 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002201 },
2202 /* AHASH algorithms. */
2203 { .type = CRYPTO_ALG_TYPE_AHASH,
2204 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002205 .halg.digestsize = MD5_DIGEST_SIZE,
2206 .halg.base = {
2207 .cra_name = "md5",
2208 .cra_driver_name = "md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002209 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper497f2e62010-05-19 19:20:36 +10002210 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2211 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002212 }
2213 },
2214 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2215 DESC_HDR_SEL0_MDEUA |
2216 DESC_HDR_MODE0_MDEU_MD5,
2217 },
2218 { .type = CRYPTO_ALG_TYPE_AHASH,
2219 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002220 .halg.digestsize = SHA1_DIGEST_SIZE,
2221 .halg.base = {
2222 .cra_name = "sha1",
2223 .cra_driver_name = "sha1-talitos",
2224 .cra_blocksize = SHA1_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_SHA1,
2232 },
2233 { .type = CRYPTO_ALG_TYPE_AHASH,
2234 .alg.hash = {
Kim Phillips60f208d2010-05-19 19:21:53 +10002235 .halg.digestsize = SHA224_DIGEST_SIZE,
2236 .halg.base = {
2237 .cra_name = "sha224",
2238 .cra_driver_name = "sha224-talitos",
2239 .cra_blocksize = SHA224_BLOCK_SIZE,
2240 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2241 CRYPTO_ALG_ASYNC,
Kim Phillips60f208d2010-05-19 19:21:53 +10002242 }
2243 },
2244 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2245 DESC_HDR_SEL0_MDEUA |
2246 DESC_HDR_MODE0_MDEU_SHA224,
2247 },
2248 { .type = CRYPTO_ALG_TYPE_AHASH,
2249 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002250 .halg.digestsize = SHA256_DIGEST_SIZE,
2251 .halg.base = {
2252 .cra_name = "sha256",
2253 .cra_driver_name = "sha256-talitos",
2254 .cra_blocksize = SHA256_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_MDEUA |
2261 DESC_HDR_MODE0_MDEU_SHA256,
2262 },
2263 { .type = CRYPTO_ALG_TYPE_AHASH,
2264 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002265 .halg.digestsize = SHA384_DIGEST_SIZE,
2266 .halg.base = {
2267 .cra_name = "sha384",
2268 .cra_driver_name = "sha384-talitos",
2269 .cra_blocksize = SHA384_BLOCK_SIZE,
2270 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2271 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002272 }
2273 },
2274 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2275 DESC_HDR_SEL0_MDEUB |
2276 DESC_HDR_MODE0_MDEUB_SHA384,
2277 },
2278 { .type = CRYPTO_ALG_TYPE_AHASH,
2279 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002280 .halg.digestsize = SHA512_DIGEST_SIZE,
2281 .halg.base = {
2282 .cra_name = "sha512",
2283 .cra_driver_name = "sha512-talitos",
2284 .cra_blocksize = SHA512_BLOCK_SIZE,
2285 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2286 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002287 }
2288 },
2289 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2290 DESC_HDR_SEL0_MDEUB |
2291 DESC_HDR_MODE0_MDEUB_SHA512,
2292 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002293 { .type = CRYPTO_ALG_TYPE_AHASH,
2294 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002295 .halg.digestsize = MD5_DIGEST_SIZE,
2296 .halg.base = {
2297 .cra_name = "hmac(md5)",
2298 .cra_driver_name = "hmac-md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002299 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper79b3a412011-11-21 16:13:25 +08002300 .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_MD5,
2307 },
2308 { .type = CRYPTO_ALG_TYPE_AHASH,
2309 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002310 .halg.digestsize = SHA1_DIGEST_SIZE,
2311 .halg.base = {
2312 .cra_name = "hmac(sha1)",
2313 .cra_driver_name = "hmac-sha1-talitos",
2314 .cra_blocksize = SHA1_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_SHA1,
2322 },
2323 { .type = CRYPTO_ALG_TYPE_AHASH,
2324 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002325 .halg.digestsize = SHA224_DIGEST_SIZE,
2326 .halg.base = {
2327 .cra_name = "hmac(sha224)",
2328 .cra_driver_name = "hmac-sha224-talitos",
2329 .cra_blocksize = SHA224_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_MDEUA |
2336 DESC_HDR_MODE0_MDEU_SHA224,
2337 },
2338 { .type = CRYPTO_ALG_TYPE_AHASH,
2339 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002340 .halg.digestsize = SHA256_DIGEST_SIZE,
2341 .halg.base = {
2342 .cra_name = "hmac(sha256)",
2343 .cra_driver_name = "hmac-sha256-talitos",
2344 .cra_blocksize = SHA256_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_MDEUA |
2351 DESC_HDR_MODE0_MDEU_SHA256,
2352 },
2353 { .type = CRYPTO_ALG_TYPE_AHASH,
2354 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002355 .halg.digestsize = SHA384_DIGEST_SIZE,
2356 .halg.base = {
2357 .cra_name = "hmac(sha384)",
2358 .cra_driver_name = "hmac-sha384-talitos",
2359 .cra_blocksize = SHA384_BLOCK_SIZE,
2360 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2361 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002362 }
2363 },
2364 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2365 DESC_HDR_SEL0_MDEUB |
2366 DESC_HDR_MODE0_MDEUB_SHA384,
2367 },
2368 { .type = CRYPTO_ALG_TYPE_AHASH,
2369 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002370 .halg.digestsize = SHA512_DIGEST_SIZE,
2371 .halg.base = {
2372 .cra_name = "hmac(sha512)",
2373 .cra_driver_name = "hmac-sha512-talitos",
2374 .cra_blocksize = SHA512_BLOCK_SIZE,
2375 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2376 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002377 }
2378 },
2379 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2380 DESC_HDR_SEL0_MDEUB |
2381 DESC_HDR_MODE0_MDEUB_SHA512,
2382 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002383};
2384
2385struct talitos_crypto_alg {
2386 struct list_head entry;
2387 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002388 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002389};
2390
2391static int talitos_cra_init(struct crypto_tfm *tfm)
2392{
2393 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002394 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002395 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002396 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002397
Lee Nipper497f2e62010-05-19 19:20:36 +10002398 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2399 talitos_alg = container_of(__crypto_ahash_alg(alg),
2400 struct talitos_crypto_alg,
2401 algt.alg.hash);
2402 else
2403 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2404 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002405
Kim Phillips9c4a7962008-06-23 19:50:15 +08002406 /* update context with ptr to dev */
2407 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002408
Kim Phillips5228f0f2011-07-15 11:21:38 +08002409 /* assign SEC channel to tfm in round-robin fashion */
2410 priv = dev_get_drvdata(ctx->dev);
2411 ctx->ch = atomic_inc_return(&priv->last_chan) &
2412 (priv->num_channels - 1);
2413
Kim Phillips9c4a7962008-06-23 19:50:15 +08002414 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002415 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002416
Kim Phillips602dba52011-07-15 11:21:39 +08002417 /* select done notification */
2418 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2419
Lee Nipper497f2e62010-05-19 19:20:36 +10002420 return 0;
2421}
2422
2423static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2424{
2425 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2426
2427 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002428
2429 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002430 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002431
2432 return 0;
2433}
2434
Lee Nipper497f2e62010-05-19 19:20:36 +10002435static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2436{
2437 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2438
2439 talitos_cra_init(tfm);
2440
2441 ctx->keylen = 0;
2442 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2443 sizeof(struct talitos_ahash_req_ctx));
2444
2445 return 0;
2446}
2447
Kim Phillips9c4a7962008-06-23 19:50:15 +08002448/*
2449 * given the alg's descriptor header template, determine whether descriptor
2450 * type and primary/secondary execution units required match the hw
2451 * capabilities description provided in the device tree node.
2452 */
2453static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2454{
2455 struct talitos_private *priv = dev_get_drvdata(dev);
2456 int ret;
2457
2458 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2459 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2460
2461 if (SECONDARY_EU(desc_hdr_template))
2462 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2463 & priv->exec_units);
2464
2465 return ret;
2466}
2467
Grant Likely2dc11582010-08-06 09:25:50 -06002468static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002469{
2470 struct device *dev = &ofdev->dev;
2471 struct talitos_private *priv = dev_get_drvdata(dev);
2472 struct talitos_crypto_alg *t_alg, *n;
2473 int i;
2474
2475 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002476 switch (t_alg->algt.type) {
2477 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2478 case CRYPTO_ALG_TYPE_AEAD:
2479 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2480 break;
2481 case CRYPTO_ALG_TYPE_AHASH:
2482 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2483 break;
2484 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002485 list_del(&t_alg->entry);
2486 kfree(t_alg);
2487 }
2488
2489 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2490 talitos_unregister_rng(dev);
2491
Kim Phillips4b9926282009-08-13 11:50:38 +10002492 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002493 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002494
Kim Phillips4b9926282009-08-13 11:50:38 +10002495 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002496
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002497 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002498 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002499 free_irq(priv->irq[i], dev);
2500 irq_dispose_mapping(priv->irq[i]);
2501 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002502
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002503 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002504 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002505 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002506
2507 iounmap(priv->reg);
2508
Kim Phillips9c4a7962008-06-23 19:50:15 +08002509 kfree(priv);
2510
2511 return 0;
2512}
2513
2514static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2515 struct talitos_alg_template
2516 *template)
2517{
Kim Phillips60f208d2010-05-19 19:21:53 +10002518 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002519 struct talitos_crypto_alg *t_alg;
2520 struct crypto_alg *alg;
2521
2522 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2523 if (!t_alg)
2524 return ERR_PTR(-ENOMEM);
2525
Lee Nipperacbf7c622010-05-19 19:19:33 +10002526 t_alg->algt = *template;
2527
2528 switch (t_alg->algt.type) {
2529 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002530 alg = &t_alg->algt.alg.crypto;
2531 alg->cra_init = talitos_cra_init;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002532 alg->cra_type = &crypto_ablkcipher_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002533 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2534 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2535 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2536 alg->cra_ablkcipher.geniv = "eseqiv";
Lee Nipper497f2e62010-05-19 19:20:36 +10002537 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002538 case CRYPTO_ALG_TYPE_AEAD:
2539 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002540 alg->cra_init = talitos_cra_init_aead;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002541 alg->cra_type = &crypto_aead_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002542 alg->cra_aead.setkey = aead_setkey;
2543 alg->cra_aead.setauthsize = aead_setauthsize;
2544 alg->cra_aead.encrypt = aead_encrypt;
2545 alg->cra_aead.decrypt = aead_decrypt;
2546 alg->cra_aead.givencrypt = aead_givencrypt;
2547 alg->cra_aead.geniv = "<built-in>";
Lee Nipperacbf7c622010-05-19 19:19:33 +10002548 break;
2549 case CRYPTO_ALG_TYPE_AHASH:
2550 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002551 alg->cra_init = talitos_cra_init_ahash;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002552 alg->cra_type = &crypto_ahash_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002553 t_alg->algt.alg.hash.init = ahash_init;
2554 t_alg->algt.alg.hash.update = ahash_update;
2555 t_alg->algt.alg.hash.final = ahash_final;
2556 t_alg->algt.alg.hash.finup = ahash_finup;
2557 t_alg->algt.alg.hash.digest = ahash_digest;
2558 t_alg->algt.alg.hash.setkey = ahash_setkey;
2559
Lee Nipper79b3a412011-11-21 16:13:25 +08002560 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06002561 !strncmp(alg->cra_name, "hmac", 4)) {
2562 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002563 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002564 }
Kim Phillips60f208d2010-05-19 19:21:53 +10002565 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002566 (!strcmp(alg->cra_name, "sha224") ||
2567 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002568 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2569 t_alg->algt.desc_hdr_template =
2570 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2571 DESC_HDR_SEL0_MDEUA |
2572 DESC_HDR_MODE0_MDEU_SHA256;
2573 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002574 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002575 default:
2576 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2577 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002578 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002579
Kim Phillips9c4a7962008-06-23 19:50:15 +08002580 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002581 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002582 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002583 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002584 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002585
Kim Phillips9c4a7962008-06-23 19:50:15 +08002586 t_alg->dev = dev;
2587
2588 return t_alg;
2589}
2590
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002591static int talitos_probe_irq(struct platform_device *ofdev)
2592{
2593 struct device *dev = &ofdev->dev;
2594 struct device_node *np = ofdev->dev.of_node;
2595 struct talitos_private *priv = dev_get_drvdata(dev);
2596 int err;
2597
2598 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002599 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002600 dev_err(dev, "failed to map irq\n");
2601 return -EINVAL;
2602 }
2603
2604 priv->irq[1] = irq_of_parse_and_map(np, 1);
2605
2606 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002607 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002608 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2609 dev_driver_string(dev), dev);
2610 goto primary_out;
2611 }
2612
2613 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2614 dev_driver_string(dev), dev);
2615 if (err)
2616 goto primary_out;
2617
2618 /* get the secondary irq line */
2619 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2620 dev_driver_string(dev), dev);
2621 if (err) {
2622 dev_err(dev, "failed to request secondary irq\n");
2623 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002624 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002625 }
2626
2627 return err;
2628
2629primary_out:
2630 if (err) {
2631 dev_err(dev, "failed to request primary irq\n");
2632 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002633 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002634 }
2635
2636 return err;
2637}
2638
Grant Likely1c48a5c2011-02-17 02:43:24 -07002639static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002640{
2641 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002642 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002643 struct talitos_private *priv;
2644 const unsigned int *prop;
2645 int i, err;
2646
2647 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2648 if (!priv)
2649 return -ENOMEM;
2650
Kevin Haof3de9cb2014-01-28 20:17:23 +08002651 INIT_LIST_HEAD(&priv->alg_list);
2652
Kim Phillips9c4a7962008-06-23 19:50:15 +08002653 dev_set_drvdata(dev, priv);
2654
2655 priv->ofdev = ofdev;
2656
Horia Geanta511d63c2012-03-30 17:49:53 +03002657 spin_lock_init(&priv->reg_lock);
2658
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002659 err = talitos_probe_irq(ofdev);
2660 if (err)
2661 goto err_out;
2662
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002663 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002664 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2665 (unsigned long)dev);
2666 } else {
2667 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2668 (unsigned long)dev);
2669 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2670 (unsigned long)dev);
2671 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002672
Kim Phillips9c4a7962008-06-23 19:50:15 +08002673 priv->reg = of_iomap(np, 0);
2674 if (!priv->reg) {
2675 dev_err(dev, "failed to of_iomap\n");
2676 err = -ENOMEM;
2677 goto err_out;
2678 }
2679
2680 /* get SEC version capabilities from device tree */
2681 prop = of_get_property(np, "fsl,num-channels", NULL);
2682 if (prop)
2683 priv->num_channels = *prop;
2684
2685 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2686 if (prop)
2687 priv->chfifo_len = *prop;
2688
2689 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2690 if (prop)
2691 priv->exec_units = *prop;
2692
2693 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2694 if (prop)
2695 priv->desc_types = *prop;
2696
2697 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2698 !priv->exec_units || !priv->desc_types) {
2699 dev_err(dev, "invalid property data in device tree node\n");
2700 err = -EINVAL;
2701 goto err_out;
2702 }
2703
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002704 if (of_device_is_compatible(np, "fsl,sec3.0"))
2705 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2706
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002707 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002708 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002709 TALITOS_FTR_SHA224_HWINIT |
2710 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002711
Kim Phillips4b9926282009-08-13 11:50:38 +10002712 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2713 priv->num_channels, GFP_KERNEL);
2714 if (!priv->chan) {
2715 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002716 err = -ENOMEM;
2717 goto err_out;
2718 }
2719
Martin Hicksf641ddd2015-03-03 08:21:33 -05002720 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2721
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002722 for (i = 0; i < priv->num_channels; i++) {
2723 priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002724 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002725 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
Kim Phillipsad42d5f2011-11-21 16:13:27 +08002726
Kim Phillips4b9926282009-08-13 11:50:38 +10002727 spin_lock_init(&priv->chan[i].head_lock);
2728 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002729
Kim Phillips4b9926282009-08-13 11:50:38 +10002730 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2731 priv->fifo_len, GFP_KERNEL);
2732 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002733 dev_err(dev, "failed to allocate request fifo %d\n", i);
2734 err = -ENOMEM;
2735 goto err_out;
2736 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002737
Kim Phillips4b9926282009-08-13 11:50:38 +10002738 atomic_set(&priv->chan[i].submit_count,
2739 -(priv->chfifo_len - 1));
Martin Hicksf641ddd2015-03-03 08:21:33 -05002740 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002741
Kim Phillips81eb0242009-08-13 11:51:51 +10002742 dma_set_mask(dev, DMA_BIT_MASK(36));
2743
Kim Phillips9c4a7962008-06-23 19:50:15 +08002744 /* reset and initialize the h/w */
2745 err = init_device(dev);
2746 if (err) {
2747 dev_err(dev, "failed to initialize device\n");
2748 goto err_out;
2749 }
2750
2751 /* register the RNG, if available */
2752 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2753 err = talitos_register_rng(dev);
2754 if (err) {
2755 dev_err(dev, "failed to register hwrng: %d\n", err);
2756 goto err_out;
2757 } else
2758 dev_info(dev, "hwrng\n");
2759 }
2760
2761 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002762 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2763 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2764 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002765 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002766
2767 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2768 if (IS_ERR(t_alg)) {
2769 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002770 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08002771 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002772 goto err_out;
2773 }
2774
Lee Nipperacbf7c622010-05-19 19:19:33 +10002775 switch (t_alg->algt.type) {
2776 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2777 case CRYPTO_ALG_TYPE_AEAD:
2778 err = crypto_register_alg(
2779 &t_alg->algt.alg.crypto);
2780 name = t_alg->algt.alg.crypto.cra_driver_name;
2781 break;
2782 case CRYPTO_ALG_TYPE_AHASH:
2783 err = crypto_register_ahash(
2784 &t_alg->algt.alg.hash);
2785 name =
2786 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2787 break;
2788 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002789 if (err) {
2790 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002791 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002792 kfree(t_alg);
Horia Geanta991155b2013-03-20 16:31:38 +02002793 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08002794 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002795 }
2796 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002797 if (!list_empty(&priv->alg_list))
2798 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2799 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002800
2801 return 0;
2802
2803err_out:
2804 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002805
2806 return err;
2807}
2808
Márton Németh6c3f9752010-01-17 21:54:01 +11002809static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002810 {
2811 .compatible = "fsl,sec2.0",
2812 },
2813 {},
2814};
2815MODULE_DEVICE_TABLE(of, talitos_match);
2816
Grant Likely1c48a5c2011-02-17 02:43:24 -07002817static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002818 .driver = {
2819 .name = "talitos",
Grant Likely40182942010-04-13 16:13:02 -07002820 .of_match_table = talitos_match,
2821 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002822 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002823 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002824};
2825
Axel Lin741e8c22011-11-26 21:26:19 +08002826module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002827
2828MODULE_LICENSE("GPL");
2829MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2830MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");