blob: cc596bdd68746ec8160e2cb08b6dd97ea7de4b39 [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>
Herbert Xue98014a2015-05-11 17:47:48 +080049#include <crypto/internal/aead.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080050#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 Christophe922f9dc2015-04-17 16:32:07 +020058static void to_talitos_ptr(struct talitos_ptr *ptr, dma_addr_t dma_addr,
59 bool is_sec1)
Kim Phillips81eb0242009-08-13 11:51:51 +100060{
LEROY Christopheedc6bd62015-04-17 16:31:53 +020061 ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
LEROY Christophe922f9dc2015-04-17 16:32:07 +020062 if (!is_sec1)
63 ptr->eptr = upper_32_bits(dma_addr);
Kim Phillips81eb0242009-08-13 11:51:51 +100064}
65
Horia Geant?340ff602016-04-19 20:33:48 +030066static void copy_talitos_ptr(struct talitos_ptr *dst_ptr,
67 struct talitos_ptr *src_ptr, bool is_sec1)
68{
69 dst_ptr->ptr = src_ptr->ptr;
70 if (!is_sec1)
71 dst_ptr->eptr = src_ptr->eptr;
72}
73
Horia Geant?42e8b0d2015-05-11 20:04:56 +030074static void to_talitos_ptr_len(struct talitos_ptr *ptr, unsigned int len,
LEROY Christophe922f9dc2015-04-17 16:32:07 +020075 bool is_sec1)
LEROY Christophe538caf82015-04-17 16:31:59 +020076{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020077 if (is_sec1) {
78 ptr->res = 0;
79 ptr->len1 = cpu_to_be16(len);
80 } else {
81 ptr->len = cpu_to_be16(len);
82 }
LEROY Christophe538caf82015-04-17 16:31:59 +020083}
84
LEROY Christophe922f9dc2015-04-17 16:32:07 +020085static unsigned short from_talitos_ptr_len(struct talitos_ptr *ptr,
86 bool is_sec1)
LEROY Christophe538caf82015-04-17 16:31:59 +020087{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020088 if (is_sec1)
89 return be16_to_cpu(ptr->len1);
90 else
91 return be16_to_cpu(ptr->len);
LEROY Christophe538caf82015-04-17 16:31:59 +020092}
93
LEROY Christopheb096b542016-06-06 13:20:34 +020094static void to_talitos_ptr_ext_set(struct talitos_ptr *ptr, u8 val,
95 bool is_sec1)
LEROY Christophe185eb792015-04-17 16:31:55 +020096{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020097 if (!is_sec1)
LEROY Christopheb096b542016-06-06 13:20:34 +020098 ptr->j_extent = val;
99}
100
101static void to_talitos_ptr_ext_or(struct talitos_ptr *ptr, u8 val, bool is_sec1)
102{
103 if (!is_sec1)
104 ptr->j_extent |= val;
LEROY Christophe185eb792015-04-17 16:31:55 +0200105}
106
Kim Phillips9c4a7962008-06-23 19:50:15 +0800107/*
108 * map virtual single (contiguous) pointer to h/w descriptor pointer
109 */
110static void map_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200111 struct talitos_ptr *ptr,
Horia Geant?42e8b0d2015-05-11 20:04:56 +0300112 unsigned int len, void *data,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800113 enum dma_data_direction dir)
114{
Kim Phillips81eb0242009-08-13 11:51:51 +1000115 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200116 struct talitos_private *priv = dev_get_drvdata(dev);
117 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips81eb0242009-08-13 11:51:51 +1000118
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200119 to_talitos_ptr_len(ptr, len, is_sec1);
120 to_talitos_ptr(ptr, dma_addr, is_sec1);
LEROY Christopheb096b542016-06-06 13:20:34 +0200121 to_talitos_ptr_ext_set(ptr, 0, is_sec1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800122}
123
124/*
125 * unmap bus single (contiguous) h/w descriptor pointer
126 */
127static void unmap_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200128 struct talitos_ptr *ptr,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800129 enum dma_data_direction dir)
130{
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200131 struct talitos_private *priv = dev_get_drvdata(dev);
132 bool is_sec1 = has_ftr_sec1(priv);
133
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200134 dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200135 from_talitos_ptr_len(ptr, is_sec1), dir);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800136}
137
138static int reset_channel(struct device *dev, int ch)
139{
140 struct talitos_private *priv = dev_get_drvdata(dev);
141 unsigned int timeout = TALITOS_TIMEOUT;
LEROY Christophedd3c0982015-04-17 16:32:13 +0200142 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800143
LEROY Christophedd3c0982015-04-17 16:32:13 +0200144 if (is_sec1) {
145 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
146 TALITOS1_CCCR_LO_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800147
LEROY Christophedd3c0982015-04-17 16:32:13 +0200148 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR_LO) &
149 TALITOS1_CCCR_LO_RESET) && --timeout)
150 cpu_relax();
151 } else {
152 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
153 TALITOS2_CCCR_RESET);
154
155 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
156 TALITOS2_CCCR_RESET) && --timeout)
157 cpu_relax();
158 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800159
160 if (timeout == 0) {
161 dev_err(dev, "failed to reset channel %d\n", ch);
162 return -EIO;
163 }
164
Kim Phillips81eb0242009-08-13 11:51:51 +1000165 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800166 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000167 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800168
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800169 /* and ICCR writeback, if available */
170 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800171 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800172 TALITOS_CCCR_LO_IWSE);
173
Kim Phillips9c4a7962008-06-23 19:50:15 +0800174 return 0;
175}
176
177static int reset_device(struct device *dev)
178{
179 struct talitos_private *priv = dev_get_drvdata(dev);
180 unsigned int timeout = TALITOS_TIMEOUT;
LEROY Christophedd3c0982015-04-17 16:32:13 +0200181 bool is_sec1 = has_ftr_sec1(priv);
182 u32 mcr = is_sec1 ? TALITOS1_MCR_SWR : TALITOS2_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800183
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800184 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800185
LEROY Christophedd3c0982015-04-17 16:32:13 +0200186 while ((in_be32(priv->reg + TALITOS_MCR) & mcr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800187 && --timeout)
188 cpu_relax();
189
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600190 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800191 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
192 setbits32(priv->reg + TALITOS_MCR, mcr);
193 }
194
Kim Phillips9c4a7962008-06-23 19:50:15 +0800195 if (timeout == 0) {
196 dev_err(dev, "failed to reset device\n");
197 return -EIO;
198 }
199
200 return 0;
201}
202
203/*
204 * Reset and initialize the device
205 */
206static int init_device(struct device *dev)
207{
208 struct talitos_private *priv = dev_get_drvdata(dev);
209 int ch, err;
LEROY Christophedd3c0982015-04-17 16:32:13 +0200210 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800211
212 /*
213 * Master reset
214 * errata documentation: warning: certain SEC interrupts
215 * are not fully cleared by writing the MCR:SWR bit,
216 * set bit twice to completely reset
217 */
218 err = reset_device(dev);
219 if (err)
220 return err;
221
222 err = reset_device(dev);
223 if (err)
224 return err;
225
226 /* reset channels */
227 for (ch = 0; ch < priv->num_channels; ch++) {
228 err = reset_channel(dev, ch);
229 if (err)
230 return err;
231 }
232
233 /* enable channel done and error interrupts */
LEROY Christophedd3c0982015-04-17 16:32:13 +0200234 if (is_sec1) {
235 clrbits32(priv->reg + TALITOS_IMR, TALITOS1_IMR_INIT);
236 clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT);
237 /* disable parity error check in DEU (erroneous? test vect.) */
238 setbits32(priv->reg_deu + TALITOS_EUICR, TALITOS1_DEUICR_KPE);
239 } else {
240 setbits32(priv->reg + TALITOS_IMR, TALITOS2_IMR_INIT);
241 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT);
242 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800243
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800244 /* disable integrity check error interrupts (use writeback instead) */
245 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200246 setbits32(priv->reg_mdeu + TALITOS_EUICR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800247 TALITOS_MDEUICR_LO_ICE);
248
Kim Phillips9c4a7962008-06-23 19:50:15 +0800249 return 0;
250}
251
252/**
253 * talitos_submit - submits a descriptor to the device for processing
254 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800255 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800256 * @desc: the descriptor to be processed by the device
257 * @callback: whom to call when processing is complete
258 * @context: a handle for use by caller (optional)
259 *
260 * desc must contain valid dma-mapped (bus physical) address pointers.
261 * callback must check err and feedback in descriptor header
262 * for device processing status.
263 */
Horia Geanta865d5062012-07-03 19:16:52 +0300264int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
265 void (*callback)(struct device *dev,
266 struct talitos_desc *desc,
267 void *context, int error),
268 void *context)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800269{
270 struct talitos_private *priv = dev_get_drvdata(dev);
271 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800272 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800273 int head;
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200274 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800275
Kim Phillips4b9926282009-08-13 11:50:38 +1000276 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800277
Kim Phillips4b9926282009-08-13 11:50:38 +1000278 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800279 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000280 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800281 return -EAGAIN;
282 }
283
Kim Phillips4b9926282009-08-13 11:50:38 +1000284 head = priv->chan[ch].head;
285 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800286
Kim Phillips9c4a7962008-06-23 19:50:15 +0800287 /* map descriptor and save caller data */
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200288 if (is_sec1) {
289 desc->hdr1 = desc->hdr;
290 desc->next_desc = 0;
291 request->dma_desc = dma_map_single(dev, &desc->hdr1,
292 TALITOS_DESC_SIZE,
293 DMA_BIDIRECTIONAL);
294 } else {
295 request->dma_desc = dma_map_single(dev, desc,
296 TALITOS_DESC_SIZE,
297 DMA_BIDIRECTIONAL);
298 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800299 request->callback = callback;
300 request->context = context;
301
302 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000303 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800304
305 smp_wmb();
306 request->desc = desc;
307
308 /* GO! */
309 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800310 out_be32(priv->chan[ch].reg + TALITOS_FF,
311 upper_32_bits(request->dma_desc));
312 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800313 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800314
Kim Phillips4b9926282009-08-13 11:50:38 +1000315 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800316
317 return -EINPROGRESS;
318}
Horia Geanta865d5062012-07-03 19:16:52 +0300319EXPORT_SYMBOL(talitos_submit);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800320
321/*
322 * process what was done, notify callback of error if not
323 */
324static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
325{
326 struct talitos_private *priv = dev_get_drvdata(dev);
327 struct talitos_request *request, saved_req;
328 unsigned long flags;
329 int tail, status;
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200330 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800331
Kim Phillips4b9926282009-08-13 11:50:38 +1000332 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800333
Kim Phillips4b9926282009-08-13 11:50:38 +1000334 tail = priv->chan[ch].tail;
335 while (priv->chan[ch].fifo[tail].desc) {
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200336 __be32 hdr;
337
Kim Phillips4b9926282009-08-13 11:50:38 +1000338 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800339
340 /* descriptors with their done bits set don't get the error */
341 rmb();
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200342 hdr = is_sec1 ? request->desc->hdr1 : request->desc->hdr;
343
344 if ((hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800345 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100346 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800347 if (!error)
348 break;
349 else
350 status = error;
351
352 dma_unmap_single(dev, request->dma_desc,
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200353 TALITOS_DESC_SIZE,
Kim Phillipse938e462009-03-29 15:53:23 +0800354 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800355
356 /* copy entries so we can call callback outside lock */
357 saved_req.desc = request->desc;
358 saved_req.callback = request->callback;
359 saved_req.context = request->context;
360
361 /* release request entry in fifo */
362 smp_wmb();
363 request->desc = NULL;
364
365 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000366 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800367
Kim Phillips4b9926282009-08-13 11:50:38 +1000368 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800369
Kim Phillips4b9926282009-08-13 11:50:38 +1000370 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800371
Kim Phillips9c4a7962008-06-23 19:50:15 +0800372 saved_req.callback(dev, saved_req.desc, saved_req.context,
373 status);
374 /* channel may resume processing in single desc error case */
375 if (error && !reset_ch && status == error)
376 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000377 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
378 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800379 }
380
Kim Phillips4b9926282009-08-13 11:50:38 +1000381 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800382}
383
384/*
385 * process completed requests for channels that have done status
386 */
LEROY Christophedd3c0982015-04-17 16:32:13 +0200387#define DEF_TALITOS1_DONE(name, ch_done_mask) \
388static void talitos1_done_##name(unsigned long data) \
389{ \
390 struct device *dev = (struct device *)data; \
391 struct talitos_private *priv = dev_get_drvdata(dev); \
392 unsigned long flags; \
393 \
394 if (ch_done_mask & 0x10000000) \
395 flush_channel(dev, 0, 0, 0); \
396 if (priv->num_channels == 1) \
397 goto out; \
398 if (ch_done_mask & 0x40000000) \
399 flush_channel(dev, 1, 0, 0); \
400 if (ch_done_mask & 0x00010000) \
401 flush_channel(dev, 2, 0, 0); \
402 if (ch_done_mask & 0x00040000) \
403 flush_channel(dev, 3, 0, 0); \
404 \
405out: \
406 /* At this point, all completed channels have been processed */ \
407 /* Unmask done interrupts for channels completed later on. */ \
408 spin_lock_irqsave(&priv->reg_lock, flags); \
409 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
410 clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT); \
411 spin_unlock_irqrestore(&priv->reg_lock, flags); \
412}
413
414DEF_TALITOS1_DONE(4ch, TALITOS1_ISR_4CHDONE)
415
416#define DEF_TALITOS2_DONE(name, ch_done_mask) \
417static void talitos2_done_##name(unsigned long data) \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800418{ \
419 struct device *dev = (struct device *)data; \
420 struct talitos_private *priv = dev_get_drvdata(dev); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300421 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800422 \
423 if (ch_done_mask & 1) \
424 flush_channel(dev, 0, 0, 0); \
425 if (priv->num_channels == 1) \
426 goto out; \
427 if (ch_done_mask & (1 << 2)) \
428 flush_channel(dev, 1, 0, 0); \
429 if (ch_done_mask & (1 << 4)) \
430 flush_channel(dev, 2, 0, 0); \
431 if (ch_done_mask & (1 << 6)) \
432 flush_channel(dev, 3, 0, 0); \
433 \
434out: \
435 /* At this point, all completed channels have been processed */ \
436 /* Unmask done interrupts for channels completed later on. */ \
Horia Geanta511d63c2012-03-30 17:49:53 +0300437 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800438 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
LEROY Christophedd3c0982015-04-17 16:32:13 +0200439 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300440 spin_unlock_irqrestore(&priv->reg_lock, flags); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800441}
LEROY Christophedd3c0982015-04-17 16:32:13 +0200442
443DEF_TALITOS2_DONE(4ch, TALITOS2_ISR_4CHDONE)
444DEF_TALITOS2_DONE(ch0_2, TALITOS2_ISR_CH_0_2_DONE)
445DEF_TALITOS2_DONE(ch1_3, TALITOS2_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800446
447/*
448 * locate current (offending) descriptor
449 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200450static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800451{
452 struct talitos_private *priv = dev_get_drvdata(dev);
Horia Geantab62ffd82013-11-13 12:20:37 +0200453 int tail, iter;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800454 dma_addr_t cur_desc;
455
Horia Geantab62ffd82013-11-13 12:20:37 +0200456 cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
457 cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800458
Horia Geantab62ffd82013-11-13 12:20:37 +0200459 if (!cur_desc) {
460 dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
461 return 0;
462 }
463
464 tail = priv->chan[ch].tail;
465
466 iter = tail;
467 while (priv->chan[ch].fifo[iter].dma_desc != cur_desc) {
468 iter = (iter + 1) & (priv->fifo_len - 1);
469 if (iter == tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800470 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200471 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800472 }
473 }
474
Horia Geantab62ffd82013-11-13 12:20:37 +0200475 return priv->chan[ch].fifo[iter].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800476}
477
478/*
479 * user diagnostics; report root cause of error based on execution unit status
480 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200481static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800482{
483 struct talitos_private *priv = dev_get_drvdata(dev);
484 int i;
485
Kim Phillips3e721ae2011-10-21 15:20:28 +0200486 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800487 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200488
489 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800490 case DESC_HDR_SEL0_AFEU:
491 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200492 in_be32(priv->reg_afeu + TALITOS_EUISR),
493 in_be32(priv->reg_afeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800494 break;
495 case DESC_HDR_SEL0_DEU:
496 dev_err(dev, "DEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200497 in_be32(priv->reg_deu + TALITOS_EUISR),
498 in_be32(priv->reg_deu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800499 break;
500 case DESC_HDR_SEL0_MDEUA:
501 case DESC_HDR_SEL0_MDEUB:
502 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200503 in_be32(priv->reg_mdeu + TALITOS_EUISR),
504 in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800505 break;
506 case DESC_HDR_SEL0_RNG:
507 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200508 in_be32(priv->reg_rngu + TALITOS_ISR),
509 in_be32(priv->reg_rngu + TALITOS_ISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800510 break;
511 case DESC_HDR_SEL0_PKEU:
512 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200513 in_be32(priv->reg_pkeu + TALITOS_EUISR),
514 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800515 break;
516 case DESC_HDR_SEL0_AESU:
517 dev_err(dev, "AESUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200518 in_be32(priv->reg_aesu + TALITOS_EUISR),
519 in_be32(priv->reg_aesu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800520 break;
521 case DESC_HDR_SEL0_CRCU:
522 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200523 in_be32(priv->reg_crcu + TALITOS_EUISR),
524 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800525 break;
526 case DESC_HDR_SEL0_KEU:
527 dev_err(dev, "KEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200528 in_be32(priv->reg_pkeu + TALITOS_EUISR),
529 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800530 break;
531 }
532
Kim Phillips3e721ae2011-10-21 15:20:28 +0200533 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800534 case DESC_HDR_SEL1_MDEUA:
535 case DESC_HDR_SEL1_MDEUB:
536 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200537 in_be32(priv->reg_mdeu + TALITOS_EUISR),
538 in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800539 break;
540 case DESC_HDR_SEL1_CRCU:
541 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200542 in_be32(priv->reg_crcu + TALITOS_EUISR),
543 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800544 break;
545 }
546
547 for (i = 0; i < 8; i++)
548 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800549 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
550 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800551}
552
553/*
554 * recover from error interrupts
555 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600556static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800557{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800558 struct talitos_private *priv = dev_get_drvdata(dev);
559 unsigned int timeout = TALITOS_TIMEOUT;
LEROY Christophedd3c0982015-04-17 16:32:13 +0200560 int ch, error, reset_dev = 0;
Horia Geant?42e8b0d2015-05-11 20:04:56 +0300561 u32 v_lo;
LEROY Christophedd3c0982015-04-17 16:32:13 +0200562 bool is_sec1 = has_ftr_sec1(priv);
563 int reset_ch = is_sec1 ? 1 : 0; /* only SEC2 supports continuation */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800564
565 for (ch = 0; ch < priv->num_channels; ch++) {
566 /* skip channels without errors */
LEROY Christophedd3c0982015-04-17 16:32:13 +0200567 if (is_sec1) {
568 /* bits 29, 31, 17, 19 */
569 if (!(isr & (1 << (29 + (ch & 1) * 2 - (ch & 2) * 6))))
570 continue;
571 } else {
572 if (!(isr & (1 << (ch * 2 + 1))))
573 continue;
574 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800575
576 error = -EINVAL;
577
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800578 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800579
580 if (v_lo & TALITOS_CCPSR_LO_DOF) {
581 dev_err(dev, "double fetch fifo overflow error\n");
582 error = -EAGAIN;
583 reset_ch = 1;
584 }
585 if (v_lo & TALITOS_CCPSR_LO_SOF) {
586 /* h/w dropped descriptor */
587 dev_err(dev, "single fetch fifo overflow error\n");
588 error = -EAGAIN;
589 }
590 if (v_lo & TALITOS_CCPSR_LO_MDTE)
591 dev_err(dev, "master data transfer error\n");
592 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
LEROY Christophedd3c0982015-04-17 16:32:13 +0200593 dev_err(dev, is_sec1 ? "pointeur not complete error\n"
594 : "s/g data length zero error\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +0800595 if (v_lo & TALITOS_CCPSR_LO_FPZ)
LEROY Christophedd3c0982015-04-17 16:32:13 +0200596 dev_err(dev, is_sec1 ? "parity error\n"
597 : "fetch pointer zero error\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +0800598 if (v_lo & TALITOS_CCPSR_LO_IDH)
599 dev_err(dev, "illegal descriptor header error\n");
600 if (v_lo & TALITOS_CCPSR_LO_IEU)
LEROY Christophedd3c0982015-04-17 16:32:13 +0200601 dev_err(dev, is_sec1 ? "static assignment error\n"
602 : "invalid exec unit error\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +0800603 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200604 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
LEROY Christophedd3c0982015-04-17 16:32:13 +0200605 if (!is_sec1) {
606 if (v_lo & TALITOS_CCPSR_LO_GB)
607 dev_err(dev, "gather boundary error\n");
608 if (v_lo & TALITOS_CCPSR_LO_GRL)
609 dev_err(dev, "gather return/length error\n");
610 if (v_lo & TALITOS_CCPSR_LO_SB)
611 dev_err(dev, "scatter boundary error\n");
612 if (v_lo & TALITOS_CCPSR_LO_SRL)
613 dev_err(dev, "scatter return/length error\n");
614 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800615
616 flush_channel(dev, ch, error, reset_ch);
617
618 if (reset_ch) {
619 reset_channel(dev, ch);
620 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800621 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
LEROY Christophedd3c0982015-04-17 16:32:13 +0200622 TALITOS2_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800623 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
624 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
LEROY Christophedd3c0982015-04-17 16:32:13 +0200625 TALITOS2_CCCR_CONT) && --timeout)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800626 cpu_relax();
627 if (timeout == 0) {
628 dev_err(dev, "failed to restart channel %d\n",
629 ch);
630 reset_dev = 1;
631 }
632 }
633 }
LEROY Christophedd3c0982015-04-17 16:32:13 +0200634 if (reset_dev || (is_sec1 && isr & ~TALITOS1_ISR_4CHERR) ||
635 (!is_sec1 && isr & ~TALITOS2_ISR_4CHERR) || isr_lo) {
636 if (is_sec1 && (isr_lo & TALITOS1_ISR_TEA_ERR))
637 dev_err(dev, "TEA error: ISR 0x%08x_%08x\n",
638 isr, isr_lo);
639 else
640 dev_err(dev, "done overflow, internal time out, or "
641 "rngu error: ISR 0x%08x_%08x\n", isr, isr_lo);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800642
643 /* purge request queues */
644 for (ch = 0; ch < priv->num_channels; ch++)
645 flush_channel(dev, ch, -EIO, 1);
646
647 /* reset and reinitialize the device */
648 init_device(dev);
649 }
650}
651
LEROY Christophedd3c0982015-04-17 16:32:13 +0200652#define DEF_TALITOS1_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
653static irqreturn_t talitos1_interrupt_##name(int irq, void *data) \
654{ \
655 struct device *dev = data; \
656 struct talitos_private *priv = dev_get_drvdata(dev); \
657 u32 isr, isr_lo; \
658 unsigned long flags; \
659 \
660 spin_lock_irqsave(&priv->reg_lock, flags); \
661 isr = in_be32(priv->reg + TALITOS_ISR); \
662 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
663 /* Acknowledge interrupt */ \
664 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
665 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
666 \
667 if (unlikely(isr & ch_err_mask || isr_lo & TALITOS1_IMR_LO_INIT)) { \
668 spin_unlock_irqrestore(&priv->reg_lock, flags); \
669 talitos_error(dev, isr & ch_err_mask, isr_lo); \
670 } \
671 else { \
672 if (likely(isr & ch_done_mask)) { \
673 /* mask further done interrupts. */ \
674 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
675 /* done_task will unmask done interrupts at exit */ \
676 tasklet_schedule(&priv->done_task[tlet]); \
677 } \
678 spin_unlock_irqrestore(&priv->reg_lock, flags); \
679 } \
680 \
681 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
682 IRQ_NONE; \
683}
684
685DEF_TALITOS1_INTERRUPT(4ch, TALITOS1_ISR_4CHDONE, TALITOS1_ISR_4CHERR, 0)
686
687#define DEF_TALITOS2_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
688static irqreturn_t talitos2_interrupt_##name(int irq, void *data) \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800689{ \
690 struct device *dev = data; \
691 struct talitos_private *priv = dev_get_drvdata(dev); \
692 u32 isr, isr_lo; \
Horia Geanta511d63c2012-03-30 17:49:53 +0300693 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800694 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300695 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800696 isr = in_be32(priv->reg + TALITOS_ISR); \
697 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
698 /* Acknowledge interrupt */ \
699 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
700 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
701 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300702 if (unlikely(isr & ch_err_mask || isr_lo)) { \
703 spin_unlock_irqrestore(&priv->reg_lock, flags); \
704 talitos_error(dev, isr & ch_err_mask, isr_lo); \
705 } \
706 else { \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800707 if (likely(isr & ch_done_mask)) { \
708 /* mask further done interrupts. */ \
709 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
710 /* done_task will unmask done interrupts at exit */ \
711 tasklet_schedule(&priv->done_task[tlet]); \
712 } \
Horia Geanta511d63c2012-03-30 17:49:53 +0300713 spin_unlock_irqrestore(&priv->reg_lock, flags); \
714 } \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800715 \
716 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
717 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800718}
LEROY Christophedd3c0982015-04-17 16:32:13 +0200719
720DEF_TALITOS2_INTERRUPT(4ch, TALITOS2_ISR_4CHDONE, TALITOS2_ISR_4CHERR, 0)
721DEF_TALITOS2_INTERRUPT(ch0_2, TALITOS2_ISR_CH_0_2_DONE, TALITOS2_ISR_CH_0_2_ERR,
722 0)
723DEF_TALITOS2_INTERRUPT(ch1_3, TALITOS2_ISR_CH_1_3_DONE, TALITOS2_ISR_CH_1_3_ERR,
724 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800725
726/*
727 * hwrng
728 */
729static int talitos_rng_data_present(struct hwrng *rng, int wait)
730{
731 struct device *dev = (struct device *)rng->priv;
732 struct talitos_private *priv = dev_get_drvdata(dev);
733 u32 ofl;
734 int i;
735
736 for (i = 0; i < 20; i++) {
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200737 ofl = in_be32(priv->reg_rngu + TALITOS_EUSR_LO) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800738 TALITOS_RNGUSR_LO_OFL;
739 if (ofl || !wait)
740 break;
741 udelay(10);
742 }
743
744 return !!ofl;
745}
746
747static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
748{
749 struct device *dev = (struct device *)rng->priv;
750 struct talitos_private *priv = dev_get_drvdata(dev);
751
752 /* rng fifo requires 64-bit accesses */
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200753 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO);
754 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800755
756 return sizeof(u32);
757}
758
759static int talitos_rng_init(struct hwrng *rng)
760{
761 struct device *dev = (struct device *)rng->priv;
762 struct talitos_private *priv = dev_get_drvdata(dev);
763 unsigned int timeout = TALITOS_TIMEOUT;
764
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200765 setbits32(priv->reg_rngu + TALITOS_EURCR_LO, TALITOS_RNGURCR_LO_SR);
766 while (!(in_be32(priv->reg_rngu + TALITOS_EUSR_LO)
767 & TALITOS_RNGUSR_LO_RD)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800768 && --timeout)
769 cpu_relax();
770 if (timeout == 0) {
771 dev_err(dev, "failed to reset rng hw\n");
772 return -ENODEV;
773 }
774
775 /* start generating */
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200776 setbits32(priv->reg_rngu + TALITOS_EUDSR_LO, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800777
778 return 0;
779}
780
781static int talitos_register_rng(struct device *dev)
782{
783 struct talitos_private *priv = dev_get_drvdata(dev);
Aaron Sierra35a3bb32015-08-05 16:52:08 -0500784 int err;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800785
786 priv->rng.name = dev_driver_string(dev),
787 priv->rng.init = talitos_rng_init,
788 priv->rng.data_present = talitos_rng_data_present,
789 priv->rng.data_read = talitos_rng_data_read,
790 priv->rng.priv = (unsigned long)dev;
791
Aaron Sierra35a3bb32015-08-05 16:52:08 -0500792 err = hwrng_register(&priv->rng);
793 if (!err)
794 priv->rng_registered = true;
795
796 return err;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800797}
798
799static void talitos_unregister_rng(struct device *dev)
800{
801 struct talitos_private *priv = dev_get_drvdata(dev);
802
Aaron Sierra35a3bb32015-08-05 16:52:08 -0500803 if (!priv->rng_registered)
804 return;
805
Kim Phillips9c4a7962008-06-23 19:50:15 +0800806 hwrng_unregister(&priv->rng);
Aaron Sierra35a3bb32015-08-05 16:52:08 -0500807 priv->rng_registered = false;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800808}
809
810/*
811 * crypto alg
812 */
813#define TALITOS_CRA_PRIORITY 3000
LEROY Christophe7405c8d2016-06-06 13:20:46 +0200814/*
815 * Defines a priority for doing AEAD with descriptors type
816 * HMAC_SNOOP_NO_AFEA (HSNA) instead of type IPSEC_ESP
817 */
818#define TALITOS_CRA_PRIORITY_AEAD_HSNA (TALITOS_CRA_PRIORITY - 1)
Martin Hicks2ff2cc72017-05-02 09:38:35 -0400819#define TALITOS_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + SHA512_BLOCK_SIZE)
Lee Nipper3952f172008-07-10 18:29:18 +0800820#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800821
Kim Phillips9c4a7962008-06-23 19:50:15 +0800822struct talitos_ctx {
823 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800824 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800825 __be32 desc_hdr_template;
826 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800827 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800828 unsigned int keylen;
829 unsigned int enckeylen;
830 unsigned int authkeylen;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800831};
832
Lee Nipper497f2e62010-05-19 19:20:36 +1000833#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
834#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
835
836struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000837 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000838 unsigned int hw_context_size;
839 u8 buf[HASH_MAX_BLOCK_SIZE];
840 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000841 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000842 unsigned int first;
843 unsigned int last;
844 unsigned int to_hash_later;
Horia Geant?42e8b0d2015-05-11 20:04:56 +0300845 unsigned int nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000846 struct scatterlist bufsl[2];
847 struct scatterlist *psrc;
848};
849
Horia Geant?3639ca82016-04-21 19:24:55 +0300850struct talitos_export_state {
851 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
852 u8 buf[HASH_MAX_BLOCK_SIZE];
853 unsigned int swinit;
854 unsigned int first;
855 unsigned int last;
856 unsigned int to_hash_later;
857 unsigned int nbuf;
858};
859
Lee Nipper56af8cd2009-03-29 15:50:50 +0800860static int aead_setkey(struct crypto_aead *authenc,
861 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800862{
863 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Mathias Krausec306a982013-10-15 13:49:34 +0200864 struct crypto_authenc_keys keys;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800865
Mathias Krausec306a982013-10-15 13:49:34 +0200866 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800867 goto badkey;
868
Mathias Krausec306a982013-10-15 13:49:34 +0200869 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800870 goto badkey;
871
Mathias Krausec306a982013-10-15 13:49:34 +0200872 memcpy(ctx->key, keys.authkey, keys.authkeylen);
873 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800874
Mathias Krausec306a982013-10-15 13:49:34 +0200875 ctx->keylen = keys.authkeylen + keys.enckeylen;
876 ctx->enckeylen = keys.enckeylen;
877 ctx->authkeylen = keys.authkeylen;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800878
879 return 0;
880
881badkey:
882 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
883 return -EINVAL;
884}
885
886/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800887 * talitos_edesc - s/w-extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +0800888 * @src_nents: number of segments in input scatterlist
889 * @dst_nents: number of segments in output scatterlist
Herbert Xuaeb4c132015-07-30 17:53:22 +0800890 * @icv_ool: whether ICV is out-of-line
Horia Geanta79fd31d2012-08-02 17:16:40 +0300891 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips9c4a7962008-06-23 19:50:15 +0800892 * @dma_len: length of dma mapped link_tbl space
LEROY Christophe6f65f6a2015-04-17 16:32:15 +0200893 * @dma_link_tbl: bus physical address of link_tbl/buf
Kim Phillips9c4a7962008-06-23 19:50:15 +0800894 * @desc: h/w descriptor
LEROY Christophe6f65f6a2015-04-17 16:32:15 +0200895 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) (SEC2)
896 * @buf: input and output buffeur (if {src,dst}_nents > 1) (SEC1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800897 *
898 * if decrypting (with authcheck), or either one of src_nents or dst_nents
899 * is greater than 1, an integrity check value is concatenated to the end
900 * of link_tbl data
901 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800902struct talitos_edesc {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800903 int src_nents;
904 int dst_nents;
Herbert Xuaeb4c132015-07-30 17:53:22 +0800905 bool icv_ool;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300906 dma_addr_t iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800907 int dma_len;
908 dma_addr_t dma_link_tbl;
909 struct talitos_desc desc;
LEROY Christophe6f65f6a2015-04-17 16:32:15 +0200910 union {
911 struct talitos_ptr link_tbl[0];
912 u8 buf[0];
913 };
Kim Phillips9c4a7962008-06-23 19:50:15 +0800914};
915
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800916static void talitos_sg_unmap(struct device *dev,
917 struct talitos_edesc *edesc,
918 struct scatterlist *src,
LEROY Christophe6a1e8d12016-06-06 13:20:38 +0200919 struct scatterlist *dst,
920 unsigned int len, unsigned int offset)
LEROY Christophe246a87c2016-06-06 13:20:36 +0200921{
922 struct talitos_private *priv = dev_get_drvdata(dev);
923 bool is_sec1 = has_ftr_sec1(priv);
LEROY Christophe6a1e8d12016-06-06 13:20:38 +0200924 unsigned int src_nents = edesc->src_nents ? : 1;
925 unsigned int dst_nents = edesc->dst_nents ? : 1;
LEROY Christophe246a87c2016-06-06 13:20:36 +0200926
LEROY Christophe6a1e8d12016-06-06 13:20:38 +0200927 if (is_sec1 && dst && dst_nents > 1) {
928 dma_sync_single_for_device(dev, edesc->dma_link_tbl + offset,
929 len, DMA_FROM_DEVICE);
930 sg_pcopy_from_buffer(dst, dst_nents, edesc->buf + offset, len,
931 offset);
932 }
933 if (src != dst) {
934 if (src_nents == 1 || !is_sec1)
935 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
936
937 if (dst && (dst_nents == 1 || !is_sec1))
938 dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
939 } else if (src_nents == 1 || !is_sec1) {
940 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
LEROY Christophe246a87c2016-06-06 13:20:36 +0200941 }
942}
943
Kim Phillips9c4a7962008-06-23 19:50:15 +0800944static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800945 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800946 struct aead_request *areq)
947{
LEROY Christophe549bd8b2016-06-06 13:20:40 +0200948 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
949 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
950 unsigned int ivsize = crypto_aead_ivsize(aead);
951
952 if (edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP)
953 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6],
954 DMA_FROM_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800955 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
956 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
957 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
958
LEROY Christophe6a1e8d12016-06-06 13:20:38 +0200959 talitos_sg_unmap(dev, edesc, areq->src, areq->dst, areq->cryptlen,
960 areq->assoclen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800961
962 if (edesc->dma_len)
963 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
964 DMA_BIDIRECTIONAL);
LEROY Christophe549bd8b2016-06-06 13:20:40 +0200965
966 if (!(edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP)) {
967 unsigned int dst_nents = edesc->dst_nents ? : 1;
968
969 sg_pcopy_to_buffer(areq->dst, dst_nents, ctx->iv, ivsize,
970 areq->assoclen + areq->cryptlen - ivsize);
971 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800972}
973
974/*
975 * ipsec_esp descriptor callbacks
976 */
977static void ipsec_esp_encrypt_done(struct device *dev,
978 struct talitos_desc *desc, void *context,
979 int err)
980{
LEROY Christophe549bd8b2016-06-06 13:20:40 +0200981 struct talitos_private *priv = dev_get_drvdata(dev);
982 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800983 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800984 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
Herbert Xuaeb4c132015-07-30 17:53:22 +0800985 unsigned int authsize = crypto_aead_authsize(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800986 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800987 struct scatterlist *sg;
988 void *icvdata;
989
Kim Phillips19bbbc62009-03-29 15:53:59 +0800990 edesc = container_of(desc, struct talitos_edesc, desc);
991
Kim Phillips9c4a7962008-06-23 19:50:15 +0800992 ipsec_esp_unmap(dev, edesc, areq);
993
994 /* copy the generated ICV to dst */
Herbert Xuaeb4c132015-07-30 17:53:22 +0800995 if (edesc->icv_ool) {
LEROY Christophe549bd8b2016-06-06 13:20:40 +0200996 if (is_sec1)
997 icvdata = edesc->buf + areq->assoclen + areq->cryptlen;
998 else
999 icvdata = &edesc->link_tbl[edesc->src_nents +
1000 edesc->dst_nents + 2];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001001 sg = sg_last(areq->dst, edesc->dst_nents);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001002 memcpy((char *)sg_virt(sg) + sg->length - authsize,
1003 icvdata, authsize);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001004 }
1005
1006 kfree(edesc);
1007
1008 aead_request_complete(areq, err);
1009}
1010
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001011static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +08001012 struct talitos_desc *desc,
1013 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001014{
1015 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001016 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001017 unsigned int authsize = crypto_aead_authsize(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +08001018 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001019 struct scatterlist *sg;
Herbert Xuaeb4c132015-07-30 17:53:22 +08001020 char *oicv, *icv;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001021 struct talitos_private *priv = dev_get_drvdata(dev);
1022 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001023
Kim Phillips19bbbc62009-03-29 15:53:59 +08001024 edesc = container_of(desc, struct talitos_edesc, desc);
1025
Kim Phillips9c4a7962008-06-23 19:50:15 +08001026 ipsec_esp_unmap(dev, edesc, req);
1027
1028 if (!err) {
1029 /* auth check */
Kim Phillips9c4a7962008-06-23 19:50:15 +08001030 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001031 icv = (char *)sg_virt(sg) + sg->length - authsize;
1032
1033 if (edesc->dma_len) {
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001034 if (is_sec1)
1035 oicv = (char *)&edesc->dma_link_tbl +
1036 req->assoclen + req->cryptlen;
1037 else
1038 oicv = (char *)
1039 &edesc->link_tbl[edesc->src_nents +
Herbert Xuaeb4c132015-07-30 17:53:22 +08001040 edesc->dst_nents + 2];
1041 if (edesc->icv_ool)
1042 icv = oicv + authsize;
1043 } else
1044 oicv = (char *)&edesc->link_tbl[0];
1045
David Gstir79960942015-11-15 17:14:42 +01001046 err = crypto_memneq(oicv, icv, authsize) ? -EBADMSG : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001047 }
1048
1049 kfree(edesc);
1050
1051 aead_request_complete(req, err);
1052}
1053
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001054static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +08001055 struct talitos_desc *desc,
1056 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001057{
1058 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001059 struct talitos_edesc *edesc;
1060
1061 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001062
1063 ipsec_esp_unmap(dev, edesc, req);
1064
1065 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +08001066 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
1067 DESC_HDR_LO_ICCR1_PASS))
1068 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001069
1070 kfree(edesc);
1071
1072 aead_request_complete(req, err);
1073}
1074
Kim Phillips9c4a7962008-06-23 19:50:15 +08001075/*
1076 * convert scatterlist to SEC h/w link table format
1077 * stop at cryptlen bytes
1078 */
Herbert Xuaeb4c132015-07-30 17:53:22 +08001079static int sg_to_link_tbl_offset(struct scatterlist *sg, int sg_count,
1080 unsigned int offset, int cryptlen,
1081 struct talitos_ptr *link_tbl_ptr)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001082{
Lee Nipper70bcaca2008-07-03 19:08:46 +08001083 int n_sg = sg_count;
Herbert Xuaeb4c132015-07-30 17:53:22 +08001084 int count = 0;
Lee Nipper70bcaca2008-07-03 19:08:46 +08001085
Herbert Xuaeb4c132015-07-30 17:53:22 +08001086 while (cryptlen && sg && n_sg--) {
1087 unsigned int len = sg_dma_len(sg);
1088
1089 if (offset >= len) {
1090 offset -= len;
1091 goto next;
1092 }
1093
1094 len -= offset;
1095
1096 if (len > cryptlen)
1097 len = cryptlen;
1098
1099 to_talitos_ptr(link_tbl_ptr + count,
1100 sg_dma_address(sg) + offset, 0);
LEROY Christopheb096b542016-06-06 13:20:34 +02001101 to_talitos_ptr_len(link_tbl_ptr + count, len, 0);
1102 to_talitos_ptr_ext_set(link_tbl_ptr + count, 0, 0);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001103 count++;
1104 cryptlen -= len;
1105 offset = 0;
1106
1107next:
Cristian Stoica5be4d4c2015-01-20 10:06:16 +02001108 sg = sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001109 }
1110
Kim Phillips9c4a7962008-06-23 19:50:15 +08001111 /* tag end of link table */
Herbert Xuaeb4c132015-07-30 17:53:22 +08001112 if (count > 0)
LEROY Christopheb096b542016-06-06 13:20:34 +02001113 to_talitos_ptr_ext_set(link_tbl_ptr + count - 1,
1114 DESC_PTR_LNKTBL_RETURN, 0);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001115
Herbert Xuaeb4c132015-07-30 17:53:22 +08001116 return count;
1117}
1118
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001119int talitos_sg_map(struct device *dev, struct scatterlist *src,
1120 unsigned int len, struct talitos_edesc *edesc,
1121 struct talitos_ptr *ptr,
1122 int sg_count, unsigned int offset, int tbl_off)
LEROY Christophe246a87c2016-06-06 13:20:36 +02001123{
LEROY Christophe246a87c2016-06-06 13:20:36 +02001124 struct talitos_private *priv = dev_get_drvdata(dev);
1125 bool is_sec1 = has_ftr_sec1(priv);
1126
1127 to_talitos_ptr_len(ptr, len, is_sec1);
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001128 to_talitos_ptr_ext_set(ptr, 0, is_sec1);
LEROY Christophe246a87c2016-06-06 13:20:36 +02001129
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001130 if (sg_count == 1) {
1131 to_talitos_ptr(ptr, sg_dma_address(src) + offset, is_sec1);
1132 return sg_count;
LEROY Christophe246a87c2016-06-06 13:20:36 +02001133 }
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001134 if (is_sec1) {
1135 to_talitos_ptr(ptr, edesc->dma_link_tbl + offset, is_sec1);
1136 return sg_count;
1137 }
1138 sg_count = sg_to_link_tbl_offset(src, sg_count, offset, len,
1139 &edesc->link_tbl[tbl_off]);
1140 if (sg_count == 1) {
1141 /* Only one segment now, so no link tbl needed*/
1142 copy_talitos_ptr(ptr, &edesc->link_tbl[tbl_off], is_sec1);
1143 return sg_count;
1144 }
1145 to_talitos_ptr(ptr, edesc->dma_link_tbl +
1146 tbl_off * sizeof(struct talitos_ptr), is_sec1);
1147 to_talitos_ptr_ext_or(ptr, DESC_PTR_LNKTBL_JUMP, is_sec1);
1148
LEROY Christophe246a87c2016-06-06 13:20:36 +02001149 return sg_count;
1150}
1151
Kim Phillips9c4a7962008-06-23 19:50:15 +08001152/*
1153 * fill in and submit ipsec_esp descriptor
1154 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001155static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Herbert Xuaeb4c132015-07-30 17:53:22 +08001156 void (*callback)(struct device *dev,
1157 struct talitos_desc *desc,
1158 void *context, int error))
Kim Phillips9c4a7962008-06-23 19:50:15 +08001159{
1160 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001161 unsigned int authsize = crypto_aead_authsize(aead);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001162 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1163 struct device *dev = ctx->dev;
1164 struct talitos_desc *desc = &edesc->desc;
1165 unsigned int cryptlen = areq->cryptlen;
Kim Phillipse41256f2009-08-13 11:49:06 +10001166 unsigned int ivsize = crypto_aead_ivsize(aead);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001167 int tbl_off = 0;
Kim Phillipsfa86a262008-07-17 20:20:06 +08001168 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001169 int sg_link_tbl_len;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001170 bool sync_needed = false;
1171 struct talitos_private *priv = dev_get_drvdata(dev);
1172 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001173
1174 /* hmac key */
1175 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001176 DMA_TO_DEVICE);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001177
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001178 sg_count = edesc->src_nents ?: 1;
1179 if (is_sec1 && sg_count > 1)
1180 sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
1181 areq->assoclen + cryptlen);
1182 else
1183 sg_count = dma_map_sg(dev, areq->src, sg_count,
1184 (areq->src == areq->dst) ?
1185 DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1186
Kim Phillips9c4a7962008-06-23 19:50:15 +08001187 /* hmac data */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001188 ret = talitos_sg_map(dev, areq->src, areq->assoclen, edesc,
1189 &desc->ptr[1], sg_count, 0, tbl_off);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001190
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001191 if (ret > 1) {
Horia Geant?340ff602016-04-19 20:33:48 +03001192 tbl_off += ret;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001193 sync_needed = true;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001194 }
1195
Kim Phillips9c4a7962008-06-23 19:50:15 +08001196 /* cipher iv */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001197 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) {
1198 to_talitos_ptr(&desc->ptr[2], edesc->iv_dma, is_sec1);
1199 to_talitos_ptr_len(&desc->ptr[2], ivsize, is_sec1);
1200 to_talitos_ptr_ext_set(&desc->ptr[2], 0, is_sec1);
1201 } else {
1202 to_talitos_ptr(&desc->ptr[3], edesc->iv_dma, is_sec1);
1203 to_talitos_ptr_len(&desc->ptr[3], ivsize, is_sec1);
1204 to_talitos_ptr_ext_set(&desc->ptr[3], 0, is_sec1);
1205 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001206
1207 /* cipher key */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001208 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
1209 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1210 (char *)&ctx->key + ctx->authkeylen,
1211 DMA_TO_DEVICE);
1212 else
1213 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->enckeylen,
1214 (char *)&ctx->key + ctx->authkeylen,
1215 DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001216
1217 /*
1218 * cipher in
1219 * map and adjust cipher len to aead request cryptlen.
1220 * extent is bytes of HMAC postpended to ciphertext,
1221 * typically 12 for ipsec
1222 */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001223 to_talitos_ptr_len(&desc->ptr[4], cryptlen, is_sec1);
1224 to_talitos_ptr_ext_set(&desc->ptr[4], 0, is_sec1);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001225
Herbert Xuaeb4c132015-07-30 17:53:22 +08001226 sg_link_tbl_len = cryptlen;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001227
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001228 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) {
1229 to_talitos_ptr_ext_set(&desc->ptr[4], authsize, is_sec1);
1230
1231 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1232 sg_link_tbl_len += authsize;
1233 }
1234
LEROY Christophebde66672017-10-06 15:04:33 +02001235 ret = talitos_sg_map(dev, areq->src, cryptlen, edesc, &desc->ptr[4],
1236 sg_count, areq->assoclen, tbl_off);
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001237
LEROY Christophebde66672017-10-06 15:04:33 +02001238 if (ret > 1) {
1239 tbl_off += ret;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001240 sync_needed = true;
Horia Geant?340ff602016-04-19 20:33:48 +03001241 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001242
1243 /* cipher out */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001244 if (areq->src != areq->dst) {
1245 sg_count = edesc->dst_nents ? : 1;
1246 if (!is_sec1 || sg_count == 1)
1247 dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
1248 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249
LEROY Christophe5272b0e2017-10-06 15:04:35 +02001250 ret = talitos_sg_map(dev, areq->dst, cryptlen, edesc, &desc->ptr[5],
1251 sg_count, areq->assoclen, tbl_off);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001252
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001253 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
1254 to_talitos_ptr_ext_or(&desc->ptr[5], authsize, is_sec1);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001255
LEROY Christophe5272b0e2017-10-06 15:04:35 +02001256 /* ICV data */
1257 if (ret > 1) {
1258 tbl_off += ret;
Herbert Xuaeb4c132015-07-30 17:53:22 +08001259 edesc->icv_ool = true;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001260 sync_needed = true;
1261
1262 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) {
1263 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
1264 int offset = (edesc->src_nents + edesc->dst_nents + 2) *
1265 sizeof(struct talitos_ptr) + authsize;
1266
1267 /* Add an entry to the link table for ICV data */
LEROY Christophe5272b0e2017-10-06 15:04:35 +02001268 to_talitos_ptr_ext_set(tbl_ptr - 1, 0, is_sec1);
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001269 to_talitos_ptr_ext_set(tbl_ptr, DESC_PTR_LNKTBL_RETURN,
1270 is_sec1);
1271 to_talitos_ptr_len(tbl_ptr, authsize, is_sec1);
1272
1273 /* icv data follows link tables */
1274 to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl + offset,
1275 is_sec1);
LEROY Christophe5272b0e2017-10-06 15:04:35 +02001276 } else {
1277 dma_addr_t addr = edesc->dma_link_tbl;
1278
1279 if (is_sec1)
1280 addr += areq->assoclen + cryptlen;
1281 else
1282 addr += sizeof(struct talitos_ptr) * tbl_off;
1283
1284 to_talitos_ptr(&desc->ptr[6], addr, is_sec1);
1285 to_talitos_ptr_len(&desc->ptr[6], authsize, is_sec1);
1286 }
1287 } else if (!(desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)) {
1288 ret = talitos_sg_map(dev, areq->dst, authsize, edesc,
1289 &desc->ptr[6], sg_count, areq->assoclen +
1290 cryptlen,
1291 tbl_off);
1292 if (ret > 1) {
1293 tbl_off += ret;
1294 edesc->icv_ool = true;
1295 sync_needed = true;
1296 } else {
1297 edesc->icv_ool = false;
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001298 }
Horia Geant?340ff602016-04-19 20:33:48 +03001299 } else {
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001300 edesc->icv_ool = false;
1301 }
1302
Kim Phillips9c4a7962008-06-23 19:50:15 +08001303 /* iv out */
LEROY Christophe549bd8b2016-06-06 13:20:40 +02001304 if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
1305 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv,
1306 DMA_FROM_DEVICE);
1307
1308 if (sync_needed)
1309 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1310 edesc->dma_len,
1311 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001312
Kim Phillips5228f0f2011-07-15 11:21:38 +08001313 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001314 if (ret != -EINPROGRESS) {
1315 ipsec_esp_unmap(dev, edesc, areq);
1316 kfree(edesc);
1317 }
1318 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001319}
1320
Kim Phillips9c4a7962008-06-23 19:50:15 +08001321/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001322 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001323 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001324static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1325 struct scatterlist *src,
1326 struct scatterlist *dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001327 u8 *iv,
1328 unsigned int assoclen,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001329 unsigned int cryptlen,
1330 unsigned int authsize,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001331 unsigned int ivsize,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001332 int icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001333 u32 cryptoflags,
1334 bool encrypt)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001335{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001336 struct talitos_edesc *edesc;
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001337 int src_nents, dst_nents, alloc_len, dma_len, src_len, dst_len;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001338 dma_addr_t iv_dma = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001339 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001340 GFP_ATOMIC;
LEROY Christophe6f65f6a2015-04-17 16:32:15 +02001341 struct talitos_private *priv = dev_get_drvdata(dev);
1342 bool is_sec1 = has_ftr_sec1(priv);
1343 int max_len = is_sec1 ? TALITOS1_MAX_DATA_LEN : TALITOS2_MAX_DATA_LEN;
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001344 void *err;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001345
LEROY Christophe6f65f6a2015-04-17 16:32:15 +02001346 if (cryptlen + authsize > max_len) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001347 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001348 return ERR_PTR(-EINVAL);
1349 }
1350
Horia Geanta935e99a2013-11-19 14:57:49 +02001351 if (ivsize)
Horia Geanta79fd31d2012-08-02 17:16:40 +03001352 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1353
Horia Geanta62293a32013-11-28 15:11:17 +02001354 if (!dst || dst == src) {
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001355 src_len = assoclen + cryptlen + authsize;
1356 src_nents = sg_nents_for_len(src, src_len);
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001357 if (src_nents < 0) {
1358 dev_err(dev, "Invalid number of src SG.\n");
1359 err = ERR_PTR(-EINVAL);
1360 goto error_sg;
1361 }
Horia Geanta62293a32013-11-28 15:11:17 +02001362 src_nents = (src_nents == 1) ? 0 : src_nents;
1363 dst_nents = dst ? src_nents : 0;
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001364 dst_len = 0;
Horia Geanta62293a32013-11-28 15:11:17 +02001365 } else { /* dst && dst != src*/
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001366 src_len = assoclen + cryptlen + (encrypt ? 0 : authsize);
1367 src_nents = sg_nents_for_len(src, src_len);
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001368 if (src_nents < 0) {
1369 dev_err(dev, "Invalid number of src SG.\n");
1370 err = ERR_PTR(-EINVAL);
1371 goto error_sg;
1372 }
Horia Geanta62293a32013-11-28 15:11:17 +02001373 src_nents = (src_nents == 1) ? 0 : src_nents;
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001374 dst_len = assoclen + cryptlen + (encrypt ? authsize : 0);
1375 dst_nents = sg_nents_for_len(dst, dst_len);
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001376 if (dst_nents < 0) {
1377 dev_err(dev, "Invalid number of dst SG.\n");
1378 err = ERR_PTR(-EINVAL);
1379 goto error_sg;
1380 }
Horia Geanta62293a32013-11-28 15:11:17 +02001381 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001382 }
1383
1384 /*
1385 * allocate space for base edesc plus the link tables,
Herbert Xuaeb4c132015-07-30 17:53:22 +08001386 * allowing for two separate entries for AD and generated ICV (+ 2),
1387 * and space for two sets of ICVs (stashed and generated)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001388 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001389 alloc_len = sizeof(struct talitos_edesc);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001390 if (src_nents || dst_nents) {
LEROY Christophe6f65f6a2015-04-17 16:32:15 +02001391 if (is_sec1)
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001392 dma_len = (src_nents ? src_len : 0) +
1393 (dst_nents ? dst_len : 0);
LEROY Christophe6f65f6a2015-04-17 16:32:15 +02001394 else
Herbert Xuaeb4c132015-07-30 17:53:22 +08001395 dma_len = (src_nents + dst_nents + 2) *
1396 sizeof(struct talitos_ptr) + authsize * 2;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001397 alloc_len += dma_len;
1398 } else {
1399 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001400 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001401 }
1402
Kim Phillips586725f2008-07-17 20:19:18 +08001403 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001404 if (!edesc) {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001405 dev_err(dev, "could not allocate edescriptor\n");
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001406 err = ERR_PTR(-ENOMEM);
1407 goto error_sg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001408 }
1409
1410 edesc->src_nents = src_nents;
1411 edesc->dst_nents = dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001412 edesc->iv_dma = iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001413 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001414 if (dma_len)
1415 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1416 edesc->dma_len,
1417 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001418
1419 return edesc;
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001420error_sg:
1421 if (iv_dma)
1422 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
1423 return err;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001424}
1425
Horia Geanta79fd31d2012-08-02 17:16:40 +03001426static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
Horia Geanta62293a32013-11-28 15:11:17 +02001427 int icv_stashing, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001428{
1429 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001430 unsigned int authsize = crypto_aead_authsize(authenc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001431 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001432 unsigned int ivsize = crypto_aead_ivsize(authenc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001433
Herbert Xuaeb4c132015-07-30 17:53:22 +08001434 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001435 iv, areq->assoclen, areq->cryptlen,
Herbert Xuaeb4c132015-07-30 17:53:22 +08001436 authsize, ivsize, icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001437 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001438}
1439
Lee Nipper56af8cd2009-03-29 15:50:50 +08001440static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001441{
1442 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1443 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001444 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001445
1446 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001447 edesc = aead_edesc_alloc(req, req->iv, 0, true);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001448 if (IS_ERR(edesc))
1449 return PTR_ERR(edesc);
1450
1451 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001452 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001453
Herbert Xuaeb4c132015-07-30 17:53:22 +08001454 return ipsec_esp(edesc, req, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001455}
1456
Lee Nipper56af8cd2009-03-29 15:50:50 +08001457static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001458{
1459 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xuaeb4c132015-07-30 17:53:22 +08001460 unsigned int authsize = crypto_aead_authsize(authenc);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001461 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001462 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001463 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001464 struct scatterlist *sg;
1465 void *icvdata;
1466
1467 req->cryptlen -= authsize;
1468
1469 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001470 edesc = aead_edesc_alloc(req, req->iv, 1, false);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001471 if (IS_ERR(edesc))
1472 return PTR_ERR(edesc);
1473
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001474 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001475 ((!edesc->src_nents && !edesc->dst_nents) ||
1476 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001477
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001478 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001479 edesc->desc.hdr = ctx->desc_hdr_template |
1480 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001481 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001482
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001483 /* reset integrity check result bits */
1484 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001485
Herbert Xuaeb4c132015-07-30 17:53:22 +08001486 return ipsec_esp(edesc, req, ipsec_esp_decrypt_hwauth_done);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001487 }
Kim Phillipse938e462009-03-29 15:53:23 +08001488
1489 /* Have to check the ICV with software */
1490 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1491
1492 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1493 if (edesc->dma_len)
Herbert Xuaeb4c132015-07-30 17:53:22 +08001494 icvdata = (char *)&edesc->link_tbl[edesc->src_nents +
1495 edesc->dst_nents + 2];
Kim Phillipse938e462009-03-29 15:53:23 +08001496 else
1497 icvdata = &edesc->link_tbl[0];
1498
1499 sg = sg_last(req->src, edesc->src_nents ? : 1);
1500
Herbert Xuaeb4c132015-07-30 17:53:22 +08001501 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - authsize, authsize);
Kim Phillipse938e462009-03-29 15:53:23 +08001502
Herbert Xuaeb4c132015-07-30 17:53:22 +08001503 return ipsec_esp(edesc, req, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001504}
1505
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001506static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1507 const u8 *key, unsigned int keylen)
1508{
1509 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
LEROY Christophe552f74c2017-10-06 15:04:37 +02001510 u32 tmp[DES_EXPKEY_WORDS];
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001511
Martin Hicks2ff2cc72017-05-02 09:38:35 -04001512 if (keylen > TALITOS_MAX_KEY_SIZE) {
1513 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1514 return -EINVAL;
1515 }
1516
LEROY Christophe552f74c2017-10-06 15:04:37 +02001517 if (unlikely(crypto_ablkcipher_get_flags(cipher) &
1518 CRYPTO_TFM_REQ_WEAK_KEY) &&
1519 !des_ekey(tmp, key)) {
1520 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_WEAK_KEY);
1521 return -EINVAL;
1522 }
1523
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001524 memcpy(&ctx->key, key, keylen);
1525 ctx->keylen = keylen;
1526
1527 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001528}
1529
1530static void common_nonsnoop_unmap(struct device *dev,
1531 struct talitos_edesc *edesc,
1532 struct ablkcipher_request *areq)
1533{
1534 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
LEROY Christophe032d1972015-04-17 16:31:51 +02001535
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001536 talitos_sg_unmap(dev, edesc, areq->src, areq->dst, areq->nbytes, 0);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001537 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1538 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1539
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001540 if (edesc->dma_len)
1541 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1542 DMA_BIDIRECTIONAL);
1543}
1544
1545static void ablkcipher_done(struct device *dev,
1546 struct talitos_desc *desc, void *context,
1547 int err)
1548{
1549 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001550 struct talitos_edesc *edesc;
1551
1552 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001553
1554 common_nonsnoop_unmap(dev, edesc, areq);
1555
1556 kfree(edesc);
1557
1558 areq->base.complete(&areq->base, err);
1559}
1560
1561static int common_nonsnoop(struct talitos_edesc *edesc,
1562 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001563 void (*callback) (struct device *dev,
1564 struct talitos_desc *desc,
1565 void *context, int error))
1566{
1567 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1568 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1569 struct device *dev = ctx->dev;
1570 struct talitos_desc *desc = &edesc->desc;
1571 unsigned int cryptlen = areq->nbytes;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001572 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001573 int sg_count, ret;
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001574 bool sync_needed = false;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001575 struct talitos_private *priv = dev_get_drvdata(dev);
1576 bool is_sec1 = has_ftr_sec1(priv);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001577
1578 /* first DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001579 desc->ptr[0] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001580
1581 /* cipher iv */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001582 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma, is_sec1);
1583 to_talitos_ptr_len(&desc->ptr[1], ivsize, is_sec1);
LEROY Christopheb096b542016-06-06 13:20:34 +02001584 to_talitos_ptr_ext_set(&desc->ptr[1], 0, is_sec1);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001585
1586 /* cipher key */
1587 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001588 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001589
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001590 sg_count = edesc->src_nents ?: 1;
1591 if (is_sec1 && sg_count > 1)
1592 sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
1593 cryptlen);
1594 else
1595 sg_count = dma_map_sg(dev, areq->src, sg_count,
1596 (areq->src == areq->dst) ?
1597 DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001598 /*
1599 * cipher in
1600 */
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001601 sg_count = talitos_sg_map(dev, areq->src, cryptlen, edesc,
1602 &desc->ptr[3], sg_count, 0, 0);
1603 if (sg_count > 1)
1604 sync_needed = true;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001605
1606 /* cipher out */
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001607 if (areq->src != areq->dst) {
1608 sg_count = edesc->dst_nents ? : 1;
1609 if (!is_sec1 || sg_count == 1)
1610 dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
1611 }
1612
1613 ret = talitos_sg_map(dev, areq->dst, cryptlen, edesc, &desc->ptr[4],
1614 sg_count, 0, (edesc->src_nents + 1));
1615 if (ret > 1)
1616 sync_needed = true;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001617
1618 /* iv out */
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001619 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001620 DMA_FROM_DEVICE);
1621
1622 /* last DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001623 desc->ptr[6] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001624
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001625 if (sync_needed)
1626 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1627 edesc->dma_len, DMA_BIDIRECTIONAL);
1628
Kim Phillips5228f0f2011-07-15 11:21:38 +08001629 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001630 if (ret != -EINPROGRESS) {
1631 common_nonsnoop_unmap(dev, edesc, areq);
1632 kfree(edesc);
1633 }
1634 return ret;
1635}
1636
Kim Phillipse938e462009-03-29 15:53:23 +08001637static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
Horia Geanta62293a32013-11-28 15:11:17 +02001638 areq, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001639{
1640 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1641 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001642 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001643
Herbert Xuaeb4c132015-07-30 17:53:22 +08001644 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001645 areq->info, 0, areq->nbytes, 0, ivsize, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001646 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001647}
1648
1649static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1650{
1651 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1652 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1653 struct talitos_edesc *edesc;
1654
1655 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001656 edesc = ablkcipher_edesc_alloc(areq, true);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001657 if (IS_ERR(edesc))
1658 return PTR_ERR(edesc);
1659
1660 /* set encrypt */
1661 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1662
Kim Phillipsfebec542011-07-15 11:21:39 +08001663 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001664}
1665
1666static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1667{
1668 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1669 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1670 struct talitos_edesc *edesc;
1671
1672 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001673 edesc = ablkcipher_edesc_alloc(areq, false);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001674 if (IS_ERR(edesc))
1675 return PTR_ERR(edesc);
1676
1677 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1678
Kim Phillipsfebec542011-07-15 11:21:39 +08001679 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001680}
1681
Lee Nipper497f2e62010-05-19 19:20:36 +10001682static void common_nonsnoop_hash_unmap(struct device *dev,
1683 struct talitos_edesc *edesc,
1684 struct ahash_request *areq)
1685{
1686 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001687 struct talitos_private *priv = dev_get_drvdata(dev);
1688 bool is_sec1 = has_ftr_sec1(priv);
Lee Nipper497f2e62010-05-19 19:20:36 +10001689
1690 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1691
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001692 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL, 0, 0);
LEROY Christophe032d1972015-04-17 16:31:51 +02001693
Lee Nipper497f2e62010-05-19 19:20:36 +10001694 /* When using hashctx-in, must unmap it. */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001695 if (from_talitos_ptr_len(&edesc->desc.ptr[1], is_sec1))
Lee Nipper497f2e62010-05-19 19:20:36 +10001696 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1697 DMA_TO_DEVICE);
1698
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001699 if (from_talitos_ptr_len(&edesc->desc.ptr[2], is_sec1))
Lee Nipper497f2e62010-05-19 19:20:36 +10001700 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1701 DMA_TO_DEVICE);
1702
Lee Nipper497f2e62010-05-19 19:20:36 +10001703 if (edesc->dma_len)
1704 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1705 DMA_BIDIRECTIONAL);
1706
1707}
1708
1709static void ahash_done(struct device *dev,
1710 struct talitos_desc *desc, void *context,
1711 int err)
1712{
1713 struct ahash_request *areq = context;
1714 struct talitos_edesc *edesc =
1715 container_of(desc, struct talitos_edesc, desc);
1716 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1717
1718 if (!req_ctx->last && req_ctx->to_hash_later) {
1719 /* Position any partial block for next update/final/finup */
1720 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001721 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001722 }
1723 common_nonsnoop_hash_unmap(dev, edesc, areq);
1724
1725 kfree(edesc);
1726
1727 areq->base.complete(&areq->base, err);
1728}
1729
LEROY Christophe2d029052015-04-17 16:32:18 +02001730/*
1731 * SEC1 doesn't like hashing of 0 sized message, so we do the padding
1732 * ourself and submit a padded block
1733 */
1734void talitos_handle_buggy_hash(struct talitos_ctx *ctx,
1735 struct talitos_edesc *edesc,
1736 struct talitos_ptr *ptr)
1737{
1738 static u8 padded_hash[64] = {
1739 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1741 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1742 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1743 };
1744
1745 pr_err_once("Bug in SEC1, padding ourself\n");
1746 edesc->desc.hdr &= ~DESC_HDR_MODE0_MDEU_PAD;
1747 map_single_talitos_ptr(ctx->dev, ptr, sizeof(padded_hash),
1748 (char *)padded_hash, DMA_TO_DEVICE);
1749}
1750
Lee Nipper497f2e62010-05-19 19:20:36 +10001751static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1752 struct ahash_request *areq, unsigned int length,
1753 void (*callback) (struct device *dev,
1754 struct talitos_desc *desc,
1755 void *context, int error))
1756{
1757 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1758 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1759 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1760 struct device *dev = ctx->dev;
1761 struct talitos_desc *desc = &edesc->desc;
LEROY Christophe032d1972015-04-17 16:31:51 +02001762 int ret;
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001763 bool sync_needed = false;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001764 struct talitos_private *priv = dev_get_drvdata(dev);
1765 bool is_sec1 = has_ftr_sec1(priv);
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001766 int sg_count;
Lee Nipper497f2e62010-05-19 19:20:36 +10001767
1768 /* first DWORD empty */
1769 desc->ptr[0] = zero_entry;
1770
Kim Phillips60f208d2010-05-19 19:21:53 +10001771 /* hash context in */
1772 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001773 map_single_talitos_ptr(dev, &desc->ptr[1],
1774 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001775 (char *)req_ctx->hw_context,
Lee Nipper497f2e62010-05-19 19:20:36 +10001776 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001777 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001778 } else {
1779 desc->ptr[1] = zero_entry;
Lee Nipper497f2e62010-05-19 19:20:36 +10001780 }
LEROY Christophe14922592017-09-13 12:44:51 +02001781 /* Indicate next op is not the first. */
1782 req_ctx->first = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001783
1784 /* HMAC key */
1785 if (ctx->keylen)
1786 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001787 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001788 else
1789 desc->ptr[2] = zero_entry;
1790
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001791 sg_count = edesc->src_nents ?: 1;
1792 if (is_sec1 && sg_count > 1)
LEROY Christopheb60f7912017-09-13 12:44:57 +02001793 sg_copy_to_buffer(req_ctx->psrc, sg_count, edesc->buf, length);
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001794 else
1795 sg_count = dma_map_sg(dev, req_ctx->psrc, sg_count,
1796 DMA_TO_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001797 /*
1798 * data in
1799 */
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001800 sg_count = talitos_sg_map(dev, req_ctx->psrc, length, edesc,
1801 &desc->ptr[3], sg_count, 0, 0);
1802 if (sg_count > 1)
1803 sync_needed = true;
Lee Nipper497f2e62010-05-19 19:20:36 +10001804
1805 /* fifth DWORD empty */
1806 desc->ptr[4] = zero_entry;
1807
1808 /* hash/HMAC out -or- hash context out */
1809 if (req_ctx->last)
1810 map_single_talitos_ptr(dev, &desc->ptr[5],
1811 crypto_ahash_digestsize(tfm),
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001812 areq->result, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001813 else
1814 map_single_talitos_ptr(dev, &desc->ptr[5],
1815 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001816 req_ctx->hw_context, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001817
1818 /* last DWORD empty */
1819 desc->ptr[6] = zero_entry;
1820
LEROY Christophe2d029052015-04-17 16:32:18 +02001821 if (is_sec1 && from_talitos_ptr_len(&desc->ptr[3], true) == 0)
1822 talitos_handle_buggy_hash(ctx, edesc, &desc->ptr[3]);
1823
LEROY Christophe6a1e8d12016-06-06 13:20:38 +02001824 if (sync_needed)
1825 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1826 edesc->dma_len, DMA_BIDIRECTIONAL);
1827
Kim Phillips5228f0f2011-07-15 11:21:38 +08001828 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001829 if (ret != -EINPROGRESS) {
1830 common_nonsnoop_hash_unmap(dev, edesc, areq);
1831 kfree(edesc);
1832 }
1833 return ret;
1834}
1835
1836static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1837 unsigned int nbytes)
1838{
1839 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1840 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1841 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1842
Herbert Xuaeb4c132015-07-30 17:53:22 +08001843 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, NULL, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001844 nbytes, 0, 0, 0, areq->base.flags, false);
Lee Nipper497f2e62010-05-19 19:20:36 +10001845}
1846
1847static int ahash_init(struct ahash_request *areq)
1848{
1849 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1850 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1851
1852 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001853 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001854 req_ctx->first = 1; /* first indicates h/w must init its context */
1855 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001856 req_ctx->hw_context_size =
1857 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1858 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1859 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1860
1861 return 0;
1862}
1863
Kim Phillips60f208d2010-05-19 19:21:53 +10001864/*
1865 * on h/w without explicit sha224 support, we initialize h/w context
1866 * manually with sha224 constants, and tell it to run sha256.
1867 */
1868static int ahash_init_sha224_swinit(struct ahash_request *areq)
1869{
1870 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1871
1872 ahash_init(areq);
1873 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1874
Kim Phillipsa7524472010-09-23 15:56:38 +08001875 req_ctx->hw_context[0] = SHA224_H0;
1876 req_ctx->hw_context[1] = SHA224_H1;
1877 req_ctx->hw_context[2] = SHA224_H2;
1878 req_ctx->hw_context[3] = SHA224_H3;
1879 req_ctx->hw_context[4] = SHA224_H4;
1880 req_ctx->hw_context[5] = SHA224_H5;
1881 req_ctx->hw_context[6] = SHA224_H6;
1882 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001883
1884 /* init 64-bit count */
1885 req_ctx->hw_context[8] = 0;
1886 req_ctx->hw_context[9] = 0;
1887
1888 return 0;
1889}
1890
Lee Nipper497f2e62010-05-19 19:20:36 +10001891static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1892{
1893 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1894 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1895 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1896 struct talitos_edesc *edesc;
1897 unsigned int blocksize =
1898 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1899 unsigned int nbytes_to_hash;
1900 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001901 unsigned int nsg;
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001902 int nents;
Lee Nipper497f2e62010-05-19 19:20:36 +10001903
Lee Nipper5e833bc2010-06-16 15:29:15 +10001904 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1905 /* Buffer up to one whole block */
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001906 nents = sg_nents_for_len(areq->src, nbytes);
1907 if (nents < 0) {
1908 dev_err(ctx->dev, "Invalid number of src SG.\n");
1909 return nents;
1910 }
1911 sg_copy_to_buffer(areq->src, nents,
Lee Nipper5e833bc2010-06-16 15:29:15 +10001912 req_ctx->buf + req_ctx->nbuf, nbytes);
1913 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001914 return 0;
1915 }
1916
Lee Nipper5e833bc2010-06-16 15:29:15 +10001917 /* At least (blocksize + 1) bytes are available to hash */
1918 nbytes_to_hash = nbytes + req_ctx->nbuf;
1919 to_hash_later = nbytes_to_hash & (blocksize - 1);
1920
1921 if (req_ctx->last)
1922 to_hash_later = 0;
1923 else if (to_hash_later)
1924 /* There is a partial block. Hash the full block(s) now */
1925 nbytes_to_hash -= to_hash_later;
1926 else {
1927 /* Keep one block buffered */
1928 nbytes_to_hash -= blocksize;
1929 to_hash_later = blocksize;
1930 }
1931
1932 /* Chain in any previously buffered data */
1933 if (req_ctx->nbuf) {
1934 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1935 sg_init_table(req_ctx->bufsl, nsg);
1936 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1937 if (nsg > 1)
Dan Williamsc56f6d12015-08-07 18:15:13 +02001938 sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001939 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001940 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001941 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001942
Lee Nipper5e833bc2010-06-16 15:29:15 +10001943 if (to_hash_later) {
LABBE Corentin8e409fe2015-11-04 21:13:34 +01001944 nents = sg_nents_for_len(areq->src, nbytes);
1945 if (nents < 0) {
1946 dev_err(ctx->dev, "Invalid number of src SG.\n");
1947 return nents;
1948 }
Akinobu Mitad0525722013-07-08 16:01:55 -07001949 sg_pcopy_to_buffer(areq->src, nents,
Lee Nipper5e833bc2010-06-16 15:29:15 +10001950 req_ctx->bufnext,
1951 to_hash_later,
1952 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001953 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001954 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001955
Lee Nipper5e833bc2010-06-16 15:29:15 +10001956 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001957 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1958 if (IS_ERR(edesc))
1959 return PTR_ERR(edesc);
1960
1961 edesc->desc.hdr = ctx->desc_hdr_template;
1962
1963 /* On last one, request SEC to pad; otherwise continue */
1964 if (req_ctx->last)
1965 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1966 else
1967 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1968
Kim Phillips60f208d2010-05-19 19:21:53 +10001969 /* request SEC to INIT hash. */
1970 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001971 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1972
1973 /* When the tfm context has a keylen, it's an HMAC.
1974 * A first or last (ie. not middle) descriptor must request HMAC.
1975 */
1976 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1977 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1978
1979 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1980 ahash_done);
1981}
1982
1983static int ahash_update(struct ahash_request *areq)
1984{
1985 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1986
1987 req_ctx->last = 0;
1988
1989 return ahash_process_req(areq, areq->nbytes);
1990}
1991
1992static int ahash_final(struct ahash_request *areq)
1993{
1994 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1995
1996 req_ctx->last = 1;
1997
1998 return ahash_process_req(areq, 0);
1999}
2000
2001static int ahash_finup(struct ahash_request *areq)
2002{
2003 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2004
2005 req_ctx->last = 1;
2006
2007 return ahash_process_req(areq, areq->nbytes);
2008}
2009
2010static int ahash_digest(struct ahash_request *areq)
2011{
2012 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10002013 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10002014
Kim Phillips60f208d2010-05-19 19:21:53 +10002015 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10002016 req_ctx->last = 1;
2017
2018 return ahash_process_req(areq, areq->nbytes);
2019}
2020
Horia Geant?3639ca82016-04-21 19:24:55 +03002021static int ahash_export(struct ahash_request *areq, void *out)
2022{
2023 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2024 struct talitos_export_state *export = out;
2025
2026 memcpy(export->hw_context, req_ctx->hw_context,
2027 req_ctx->hw_context_size);
2028 memcpy(export->buf, req_ctx->buf, req_ctx->nbuf);
2029 export->swinit = req_ctx->swinit;
2030 export->first = req_ctx->first;
2031 export->last = req_ctx->last;
2032 export->to_hash_later = req_ctx->to_hash_later;
2033 export->nbuf = req_ctx->nbuf;
2034
2035 return 0;
2036}
2037
2038static int ahash_import(struct ahash_request *areq, const void *in)
2039{
2040 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2041 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2042 const struct talitos_export_state *export = in;
2043
2044 memset(req_ctx, 0, sizeof(*req_ctx));
2045 req_ctx->hw_context_size =
2046 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
2047 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
2048 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
2049 memcpy(req_ctx->hw_context, export->hw_context,
2050 req_ctx->hw_context_size);
2051 memcpy(req_ctx->buf, export->buf, export->nbuf);
2052 req_ctx->swinit = export->swinit;
2053 req_ctx->first = export->first;
2054 req_ctx->last = export->last;
2055 req_ctx->to_hash_later = export->to_hash_later;
2056 req_ctx->nbuf = export->nbuf;
2057
2058 return 0;
2059}
2060
Lee Nipper79b3a412011-11-21 16:13:25 +08002061struct keyhash_result {
2062 struct completion completion;
2063 int err;
2064};
2065
2066static void keyhash_complete(struct crypto_async_request *req, int err)
2067{
2068 struct keyhash_result *res = req->data;
2069
2070 if (err == -EINPROGRESS)
2071 return;
2072
2073 res->err = err;
2074 complete(&res->completion);
2075}
2076
2077static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
2078 u8 *hash)
2079{
2080 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
2081
2082 struct scatterlist sg[1];
2083 struct ahash_request *req;
2084 struct keyhash_result hresult;
2085 int ret;
2086
2087 init_completion(&hresult.completion);
2088
2089 req = ahash_request_alloc(tfm, GFP_KERNEL);
2090 if (!req)
2091 return -ENOMEM;
2092
2093 /* Keep tfm keylen == 0 during hash of the long key */
2094 ctx->keylen = 0;
2095 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2096 keyhash_complete, &hresult);
2097
2098 sg_init_one(&sg[0], key, keylen);
2099
2100 ahash_request_set_crypt(req, sg, hash, keylen);
2101 ret = crypto_ahash_digest(req);
2102 switch (ret) {
2103 case 0:
2104 break;
2105 case -EINPROGRESS:
2106 case -EBUSY:
2107 ret = wait_for_completion_interruptible(
2108 &hresult.completion);
2109 if (!ret)
2110 ret = hresult.err;
2111 break;
2112 default:
2113 break;
2114 }
2115 ahash_request_free(req);
2116
2117 return ret;
2118}
2119
2120static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2121 unsigned int keylen)
2122{
2123 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
2124 unsigned int blocksize =
2125 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2126 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2127 unsigned int keysize = keylen;
2128 u8 hash[SHA512_DIGEST_SIZE];
2129 int ret;
2130
2131 if (keylen <= blocksize)
2132 memcpy(ctx->key, key, keysize);
2133 else {
2134 /* Must get the hash of the long key */
2135 ret = keyhash(tfm, key, keylen, hash);
2136
2137 if (ret) {
2138 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
2139 return -EINVAL;
2140 }
2141
2142 keysize = digestsize;
2143 memcpy(ctx->key, hash, digestsize);
2144 }
2145
2146 ctx->keylen = keysize;
2147
2148 return 0;
2149}
2150
2151
Kim Phillips9c4a7962008-06-23 19:50:15 +08002152struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002153 u32 type;
LEROY Christopheb0057762016-06-06 13:20:44 +02002154 u32 priority;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002155 union {
2156 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002157 struct ahash_alg hash;
Herbert Xuaeb4c132015-07-30 17:53:22 +08002158 struct aead_alg aead;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002159 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002160 __be32 desc_hdr_template;
2161};
2162
2163static struct talitos_alg_template driver_algs[] = {
Horia Geanta991155b2013-03-20 16:31:38 +02002164 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002165 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002166 .alg.aead = {
2167 .base = {
2168 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2169 .cra_driver_name = "authenc-hmac-sha1-"
2170 "cbc-aes-talitos",
2171 .cra_blocksize = AES_BLOCK_SIZE,
2172 .cra_flags = CRYPTO_ALG_ASYNC,
2173 },
2174 .ivsize = AES_BLOCK_SIZE,
2175 .maxauthsize = SHA1_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002176 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002177 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2178 DESC_HDR_SEL0_AESU |
2179 DESC_HDR_MODE0_AESU_CBC |
2180 DESC_HDR_SEL1_MDEUA |
2181 DESC_HDR_MODE1_MDEU_INIT |
2182 DESC_HDR_MODE1_MDEU_PAD |
2183 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08002184 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002185 { .type = CRYPTO_ALG_TYPE_AEAD,
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002186 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2187 .alg.aead = {
2188 .base = {
2189 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2190 .cra_driver_name = "authenc-hmac-sha1-"
2191 "cbc-aes-talitos",
2192 .cra_blocksize = AES_BLOCK_SIZE,
2193 .cra_flags = CRYPTO_ALG_ASYNC,
2194 },
2195 .ivsize = AES_BLOCK_SIZE,
2196 .maxauthsize = SHA1_DIGEST_SIZE,
2197 },
2198 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2199 DESC_HDR_SEL0_AESU |
2200 DESC_HDR_MODE0_AESU_CBC |
2201 DESC_HDR_SEL1_MDEUA |
2202 DESC_HDR_MODE1_MDEU_INIT |
2203 DESC_HDR_MODE1_MDEU_PAD |
2204 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2205 },
2206 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002207 .alg.aead = {
2208 .base = {
2209 .cra_name = "authenc(hmac(sha1),"
2210 "cbc(des3_ede))",
2211 .cra_driver_name = "authenc-hmac-sha1-"
2212 "cbc-3des-talitos",
2213 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2214 .cra_flags = CRYPTO_ALG_ASYNC,
2215 },
2216 .ivsize = DES3_EDE_BLOCK_SIZE,
2217 .maxauthsize = SHA1_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002218 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08002219 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2220 DESC_HDR_SEL0_DEU |
2221 DESC_HDR_MODE0_DEU_CBC |
2222 DESC_HDR_MODE0_DEU_3DES |
2223 DESC_HDR_SEL1_MDEUA |
2224 DESC_HDR_MODE1_MDEU_INIT |
2225 DESC_HDR_MODE1_MDEU_PAD |
2226 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08002227 },
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002228 { .type = CRYPTO_ALG_TYPE_AEAD,
2229 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2230 .alg.aead = {
2231 .base = {
2232 .cra_name = "authenc(hmac(sha1),"
2233 "cbc(des3_ede))",
2234 .cra_driver_name = "authenc-hmac-sha1-"
2235 "cbc-3des-talitos",
2236 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2237 .cra_flags = CRYPTO_ALG_ASYNC,
2238 },
2239 .ivsize = DES3_EDE_BLOCK_SIZE,
2240 .maxauthsize = SHA1_DIGEST_SIZE,
2241 },
2242 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2243 DESC_HDR_SEL0_DEU |
2244 DESC_HDR_MODE0_DEU_CBC |
2245 DESC_HDR_MODE0_DEU_3DES |
2246 DESC_HDR_SEL1_MDEUA |
2247 DESC_HDR_MODE1_MDEU_INIT |
2248 DESC_HDR_MODE1_MDEU_PAD |
2249 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2250 },
Horia Geanta357fb602012-07-03 19:16:53 +03002251 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002252 .alg.aead = {
2253 .base = {
2254 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2255 .cra_driver_name = "authenc-hmac-sha224-"
2256 "cbc-aes-talitos",
2257 .cra_blocksize = AES_BLOCK_SIZE,
2258 .cra_flags = CRYPTO_ALG_ASYNC,
2259 },
2260 .ivsize = AES_BLOCK_SIZE,
2261 .maxauthsize = SHA224_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002262 },
2263 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2264 DESC_HDR_SEL0_AESU |
2265 DESC_HDR_MODE0_AESU_CBC |
2266 DESC_HDR_SEL1_MDEUA |
2267 DESC_HDR_MODE1_MDEU_INIT |
2268 DESC_HDR_MODE1_MDEU_PAD |
2269 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2270 },
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002271 { .type = CRYPTO_ALG_TYPE_AEAD,
2272 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2273 .alg.aead = {
2274 .base = {
2275 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2276 .cra_driver_name = "authenc-hmac-sha224-"
2277 "cbc-aes-talitos",
2278 .cra_blocksize = AES_BLOCK_SIZE,
2279 .cra_flags = CRYPTO_ALG_ASYNC,
2280 },
2281 .ivsize = AES_BLOCK_SIZE,
2282 .maxauthsize = SHA224_DIGEST_SIZE,
2283 },
2284 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2285 DESC_HDR_SEL0_AESU |
2286 DESC_HDR_MODE0_AESU_CBC |
2287 DESC_HDR_SEL1_MDEUA |
2288 DESC_HDR_MODE1_MDEU_INIT |
2289 DESC_HDR_MODE1_MDEU_PAD |
2290 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2291 },
Horia Geanta357fb602012-07-03 19:16:53 +03002292 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002293 .alg.aead = {
2294 .base = {
2295 .cra_name = "authenc(hmac(sha224),"
2296 "cbc(des3_ede))",
2297 .cra_driver_name = "authenc-hmac-sha224-"
2298 "cbc-3des-talitos",
2299 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2300 .cra_flags = CRYPTO_ALG_ASYNC,
2301 },
2302 .ivsize = DES3_EDE_BLOCK_SIZE,
2303 .maxauthsize = SHA224_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002304 },
2305 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2306 DESC_HDR_SEL0_DEU |
2307 DESC_HDR_MODE0_DEU_CBC |
2308 DESC_HDR_MODE0_DEU_3DES |
2309 DESC_HDR_SEL1_MDEUA |
2310 DESC_HDR_MODE1_MDEU_INIT |
2311 DESC_HDR_MODE1_MDEU_PAD |
2312 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2313 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002314 { .type = CRYPTO_ALG_TYPE_AEAD,
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002315 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2316 .alg.aead = {
2317 .base = {
2318 .cra_name = "authenc(hmac(sha224),"
2319 "cbc(des3_ede))",
2320 .cra_driver_name = "authenc-hmac-sha224-"
2321 "cbc-3des-talitos",
2322 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2323 .cra_flags = CRYPTO_ALG_ASYNC,
2324 },
2325 .ivsize = DES3_EDE_BLOCK_SIZE,
2326 .maxauthsize = SHA224_DIGEST_SIZE,
2327 },
2328 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2329 DESC_HDR_SEL0_DEU |
2330 DESC_HDR_MODE0_DEU_CBC |
2331 DESC_HDR_MODE0_DEU_3DES |
2332 DESC_HDR_SEL1_MDEUA |
2333 DESC_HDR_MODE1_MDEU_INIT |
2334 DESC_HDR_MODE1_MDEU_PAD |
2335 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2336 },
2337 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002338 .alg.aead = {
2339 .base = {
2340 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2341 .cra_driver_name = "authenc-hmac-sha256-"
2342 "cbc-aes-talitos",
2343 .cra_blocksize = AES_BLOCK_SIZE,
2344 .cra_flags = CRYPTO_ALG_ASYNC,
2345 },
2346 .ivsize = AES_BLOCK_SIZE,
2347 .maxauthsize = SHA256_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002348 },
Lee Nipper3952f172008-07-10 18:29:18 +08002349 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2350 DESC_HDR_SEL0_AESU |
2351 DESC_HDR_MODE0_AESU_CBC |
2352 DESC_HDR_SEL1_MDEUA |
2353 DESC_HDR_MODE1_MDEU_INIT |
2354 DESC_HDR_MODE1_MDEU_PAD |
2355 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2356 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002357 { .type = CRYPTO_ALG_TYPE_AEAD,
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002358 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2359 .alg.aead = {
2360 .base = {
2361 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2362 .cra_driver_name = "authenc-hmac-sha256-"
2363 "cbc-aes-talitos",
2364 .cra_blocksize = AES_BLOCK_SIZE,
2365 .cra_flags = CRYPTO_ALG_ASYNC,
2366 },
2367 .ivsize = AES_BLOCK_SIZE,
2368 .maxauthsize = SHA256_DIGEST_SIZE,
2369 },
2370 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2371 DESC_HDR_SEL0_AESU |
2372 DESC_HDR_MODE0_AESU_CBC |
2373 DESC_HDR_SEL1_MDEUA |
2374 DESC_HDR_MODE1_MDEU_INIT |
2375 DESC_HDR_MODE1_MDEU_PAD |
2376 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2377 },
2378 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002379 .alg.aead = {
2380 .base = {
2381 .cra_name = "authenc(hmac(sha256),"
2382 "cbc(des3_ede))",
2383 .cra_driver_name = "authenc-hmac-sha256-"
2384 "cbc-3des-talitos",
2385 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2386 .cra_flags = CRYPTO_ALG_ASYNC,
2387 },
2388 .ivsize = DES3_EDE_BLOCK_SIZE,
2389 .maxauthsize = SHA256_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002390 },
Lee Nipper3952f172008-07-10 18:29:18 +08002391 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2392 DESC_HDR_SEL0_DEU |
2393 DESC_HDR_MODE0_DEU_CBC |
2394 DESC_HDR_MODE0_DEU_3DES |
2395 DESC_HDR_SEL1_MDEUA |
2396 DESC_HDR_MODE1_MDEU_INIT |
2397 DESC_HDR_MODE1_MDEU_PAD |
2398 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2399 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002400 { .type = CRYPTO_ALG_TYPE_AEAD,
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002401 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2402 .alg.aead = {
2403 .base = {
2404 .cra_name = "authenc(hmac(sha256),"
2405 "cbc(des3_ede))",
2406 .cra_driver_name = "authenc-hmac-sha256-"
2407 "cbc-3des-talitos",
2408 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2409 .cra_flags = CRYPTO_ALG_ASYNC,
2410 },
2411 .ivsize = DES3_EDE_BLOCK_SIZE,
2412 .maxauthsize = SHA256_DIGEST_SIZE,
2413 },
2414 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2415 DESC_HDR_SEL0_DEU |
2416 DESC_HDR_MODE0_DEU_CBC |
2417 DESC_HDR_MODE0_DEU_3DES |
2418 DESC_HDR_SEL1_MDEUA |
2419 DESC_HDR_MODE1_MDEU_INIT |
2420 DESC_HDR_MODE1_MDEU_PAD |
2421 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2422 },
2423 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002424 .alg.aead = {
2425 .base = {
2426 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2427 .cra_driver_name = "authenc-hmac-sha384-"
2428 "cbc-aes-talitos",
2429 .cra_blocksize = AES_BLOCK_SIZE,
2430 .cra_flags = CRYPTO_ALG_ASYNC,
2431 },
2432 .ivsize = AES_BLOCK_SIZE,
2433 .maxauthsize = SHA384_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002434 },
2435 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2436 DESC_HDR_SEL0_AESU |
2437 DESC_HDR_MODE0_AESU_CBC |
2438 DESC_HDR_SEL1_MDEUB |
2439 DESC_HDR_MODE1_MDEU_INIT |
2440 DESC_HDR_MODE1_MDEU_PAD |
2441 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2442 },
2443 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002444 .alg.aead = {
2445 .base = {
2446 .cra_name = "authenc(hmac(sha384),"
2447 "cbc(des3_ede))",
2448 .cra_driver_name = "authenc-hmac-sha384-"
2449 "cbc-3des-talitos",
2450 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2451 .cra_flags = CRYPTO_ALG_ASYNC,
2452 },
2453 .ivsize = DES3_EDE_BLOCK_SIZE,
2454 .maxauthsize = SHA384_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002455 },
2456 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2457 DESC_HDR_SEL0_DEU |
2458 DESC_HDR_MODE0_DEU_CBC |
2459 DESC_HDR_MODE0_DEU_3DES |
2460 DESC_HDR_SEL1_MDEUB |
2461 DESC_HDR_MODE1_MDEU_INIT |
2462 DESC_HDR_MODE1_MDEU_PAD |
2463 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2464 },
2465 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002466 .alg.aead = {
2467 .base = {
2468 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2469 .cra_driver_name = "authenc-hmac-sha512-"
2470 "cbc-aes-talitos",
2471 .cra_blocksize = AES_BLOCK_SIZE,
2472 .cra_flags = CRYPTO_ALG_ASYNC,
2473 },
2474 .ivsize = AES_BLOCK_SIZE,
2475 .maxauthsize = SHA512_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002476 },
2477 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2478 DESC_HDR_SEL0_AESU |
2479 DESC_HDR_MODE0_AESU_CBC |
2480 DESC_HDR_SEL1_MDEUB |
2481 DESC_HDR_MODE1_MDEU_INIT |
2482 DESC_HDR_MODE1_MDEU_PAD |
2483 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2484 },
2485 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002486 .alg.aead = {
2487 .base = {
2488 .cra_name = "authenc(hmac(sha512),"
2489 "cbc(des3_ede))",
2490 .cra_driver_name = "authenc-hmac-sha512-"
2491 "cbc-3des-talitos",
2492 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2493 .cra_flags = CRYPTO_ALG_ASYNC,
2494 },
2495 .ivsize = DES3_EDE_BLOCK_SIZE,
2496 .maxauthsize = SHA512_DIGEST_SIZE,
Horia Geanta357fb602012-07-03 19:16:53 +03002497 },
2498 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2499 DESC_HDR_SEL0_DEU |
2500 DESC_HDR_MODE0_DEU_CBC |
2501 DESC_HDR_MODE0_DEU_3DES |
2502 DESC_HDR_SEL1_MDEUB |
2503 DESC_HDR_MODE1_MDEU_INIT |
2504 DESC_HDR_MODE1_MDEU_PAD |
2505 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2506 },
2507 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002508 .alg.aead = {
2509 .base = {
2510 .cra_name = "authenc(hmac(md5),cbc(aes))",
2511 .cra_driver_name = "authenc-hmac-md5-"
2512 "cbc-aes-talitos",
2513 .cra_blocksize = AES_BLOCK_SIZE,
2514 .cra_flags = CRYPTO_ALG_ASYNC,
2515 },
2516 .ivsize = AES_BLOCK_SIZE,
2517 .maxauthsize = MD5_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002518 },
Lee Nipper3952f172008-07-10 18:29:18 +08002519 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2520 DESC_HDR_SEL0_AESU |
2521 DESC_HDR_MODE0_AESU_CBC |
2522 DESC_HDR_SEL1_MDEUA |
2523 DESC_HDR_MODE1_MDEU_INIT |
2524 DESC_HDR_MODE1_MDEU_PAD |
2525 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2526 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002527 { .type = CRYPTO_ALG_TYPE_AEAD,
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002528 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2529 .alg.aead = {
2530 .base = {
2531 .cra_name = "authenc(hmac(md5),cbc(aes))",
2532 .cra_driver_name = "authenc-hmac-md5-"
2533 "cbc-aes-talitos",
2534 .cra_blocksize = AES_BLOCK_SIZE,
2535 .cra_flags = CRYPTO_ALG_ASYNC,
2536 },
2537 .ivsize = AES_BLOCK_SIZE,
2538 .maxauthsize = MD5_DIGEST_SIZE,
2539 },
2540 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2541 DESC_HDR_SEL0_AESU |
2542 DESC_HDR_MODE0_AESU_CBC |
2543 DESC_HDR_SEL1_MDEUA |
2544 DESC_HDR_MODE1_MDEU_INIT |
2545 DESC_HDR_MODE1_MDEU_PAD |
2546 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2547 },
2548 { .type = CRYPTO_ALG_TYPE_AEAD,
Herbert Xuaeb4c132015-07-30 17:53:22 +08002549 .alg.aead = {
2550 .base = {
2551 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2552 .cra_driver_name = "authenc-hmac-md5-"
2553 "cbc-3des-talitos",
2554 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2555 .cra_flags = CRYPTO_ALG_ASYNC,
2556 },
2557 .ivsize = DES3_EDE_BLOCK_SIZE,
2558 .maxauthsize = MD5_DIGEST_SIZE,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002559 },
Lee Nipper3952f172008-07-10 18:29:18 +08002560 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2561 DESC_HDR_SEL0_DEU |
2562 DESC_HDR_MODE0_DEU_CBC |
2563 DESC_HDR_MODE0_DEU_3DES |
2564 DESC_HDR_SEL1_MDEUA |
2565 DESC_HDR_MODE1_MDEU_INIT |
2566 DESC_HDR_MODE1_MDEU_PAD |
2567 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002568 },
LEROY Christophe7405c8d2016-06-06 13:20:46 +02002569 { .type = CRYPTO_ALG_TYPE_AEAD,
2570 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2571 .alg.aead = {
2572 .base = {
2573 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2574 .cra_driver_name = "authenc-hmac-md5-"
2575 "cbc-3des-talitos",
2576 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2577 .cra_flags = CRYPTO_ALG_ASYNC,
2578 },
2579 .ivsize = DES3_EDE_BLOCK_SIZE,
2580 .maxauthsize = MD5_DIGEST_SIZE,
2581 },
2582 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2583 DESC_HDR_SEL0_DEU |
2584 DESC_HDR_MODE0_DEU_CBC |
2585 DESC_HDR_MODE0_DEU_3DES |
2586 DESC_HDR_SEL1_MDEUA |
2587 DESC_HDR_MODE1_MDEU_INIT |
2588 DESC_HDR_MODE1_MDEU_PAD |
2589 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2590 },
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002591 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002592 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2593 .alg.crypto = {
LEROY Christophe5e75ae12015-12-01 12:44:15 +01002594 .cra_name = "ecb(aes)",
2595 .cra_driver_name = "ecb-aes-talitos",
2596 .cra_blocksize = AES_BLOCK_SIZE,
2597 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2598 CRYPTO_ALG_ASYNC,
2599 .cra_ablkcipher = {
2600 .min_keysize = AES_MIN_KEY_SIZE,
2601 .max_keysize = AES_MAX_KEY_SIZE,
2602 .ivsize = AES_BLOCK_SIZE,
2603 }
2604 },
2605 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2606 DESC_HDR_SEL0_AESU,
2607 },
2608 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2609 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002610 .cra_name = "cbc(aes)",
2611 .cra_driver_name = "cbc-aes-talitos",
2612 .cra_blocksize = AES_BLOCK_SIZE,
2613 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2614 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002615 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002616 .min_keysize = AES_MIN_KEY_SIZE,
2617 .max_keysize = AES_MAX_KEY_SIZE,
2618 .ivsize = AES_BLOCK_SIZE,
2619 }
2620 },
2621 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2622 DESC_HDR_SEL0_AESU |
2623 DESC_HDR_MODE0_AESU_CBC,
2624 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002625 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2626 .alg.crypto = {
LEROY Christophe5e75ae12015-12-01 12:44:15 +01002627 .cra_name = "ctr(aes)",
2628 .cra_driver_name = "ctr-aes-talitos",
2629 .cra_blocksize = AES_BLOCK_SIZE,
2630 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2631 CRYPTO_ALG_ASYNC,
2632 .cra_ablkcipher = {
2633 .min_keysize = AES_MIN_KEY_SIZE,
2634 .max_keysize = AES_MAX_KEY_SIZE,
2635 .ivsize = AES_BLOCK_SIZE,
2636 }
2637 },
2638 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2639 DESC_HDR_SEL0_AESU |
2640 DESC_HDR_MODE0_AESU_CTR,
2641 },
2642 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2643 .alg.crypto = {
2644 .cra_name = "ecb(des)",
2645 .cra_driver_name = "ecb-des-talitos",
2646 .cra_blocksize = DES_BLOCK_SIZE,
2647 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2648 CRYPTO_ALG_ASYNC,
2649 .cra_ablkcipher = {
2650 .min_keysize = DES_KEY_SIZE,
2651 .max_keysize = DES_KEY_SIZE,
2652 .ivsize = DES_BLOCK_SIZE,
2653 }
2654 },
2655 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2656 DESC_HDR_SEL0_DEU,
2657 },
2658 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2659 .alg.crypto = {
2660 .cra_name = "cbc(des)",
2661 .cra_driver_name = "cbc-des-talitos",
2662 .cra_blocksize = DES_BLOCK_SIZE,
2663 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2664 CRYPTO_ALG_ASYNC,
2665 .cra_ablkcipher = {
2666 .min_keysize = DES_KEY_SIZE,
2667 .max_keysize = DES_KEY_SIZE,
2668 .ivsize = DES_BLOCK_SIZE,
2669 }
2670 },
2671 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2672 DESC_HDR_SEL0_DEU |
2673 DESC_HDR_MODE0_DEU_CBC,
2674 },
2675 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2676 .alg.crypto = {
2677 .cra_name = "ecb(des3_ede)",
2678 .cra_driver_name = "ecb-3des-talitos",
2679 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2680 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2681 CRYPTO_ALG_ASYNC,
2682 .cra_ablkcipher = {
2683 .min_keysize = DES3_EDE_KEY_SIZE,
2684 .max_keysize = DES3_EDE_KEY_SIZE,
2685 .ivsize = DES3_EDE_BLOCK_SIZE,
2686 }
2687 },
2688 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2689 DESC_HDR_SEL0_DEU |
2690 DESC_HDR_MODE0_DEU_3DES,
2691 },
2692 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2693 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002694 .cra_name = "cbc(des3_ede)",
2695 .cra_driver_name = "cbc-3des-talitos",
2696 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2697 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2698 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002699 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002700 .min_keysize = DES3_EDE_KEY_SIZE,
2701 .max_keysize = DES3_EDE_KEY_SIZE,
2702 .ivsize = DES3_EDE_BLOCK_SIZE,
2703 }
2704 },
2705 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2706 DESC_HDR_SEL0_DEU |
2707 DESC_HDR_MODE0_DEU_CBC |
2708 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002709 },
2710 /* AHASH algorithms. */
2711 { .type = CRYPTO_ALG_TYPE_AHASH,
2712 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002713 .halg.digestsize = MD5_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002714 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper497f2e62010-05-19 19:20:36 +10002715 .halg.base = {
2716 .cra_name = "md5",
2717 .cra_driver_name = "md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002718 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper497f2e62010-05-19 19:20:36 +10002719 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2720 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002721 }
2722 },
2723 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2724 DESC_HDR_SEL0_MDEUA |
2725 DESC_HDR_MODE0_MDEU_MD5,
2726 },
2727 { .type = CRYPTO_ALG_TYPE_AHASH,
2728 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002729 .halg.digestsize = SHA1_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002730 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper497f2e62010-05-19 19:20:36 +10002731 .halg.base = {
2732 .cra_name = "sha1",
2733 .cra_driver_name = "sha1-talitos",
2734 .cra_blocksize = SHA1_BLOCK_SIZE,
2735 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2736 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002737 }
2738 },
2739 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2740 DESC_HDR_SEL0_MDEUA |
2741 DESC_HDR_MODE0_MDEU_SHA1,
2742 },
2743 { .type = CRYPTO_ALG_TYPE_AHASH,
2744 .alg.hash = {
Kim Phillips60f208d2010-05-19 19:21:53 +10002745 .halg.digestsize = SHA224_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002746 .halg.statesize = sizeof(struct talitos_export_state),
Kim Phillips60f208d2010-05-19 19:21:53 +10002747 .halg.base = {
2748 .cra_name = "sha224",
2749 .cra_driver_name = "sha224-talitos",
2750 .cra_blocksize = SHA224_BLOCK_SIZE,
2751 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2752 CRYPTO_ALG_ASYNC,
Kim Phillips60f208d2010-05-19 19:21:53 +10002753 }
2754 },
2755 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2756 DESC_HDR_SEL0_MDEUA |
2757 DESC_HDR_MODE0_MDEU_SHA224,
2758 },
2759 { .type = CRYPTO_ALG_TYPE_AHASH,
2760 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002761 .halg.digestsize = SHA256_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002762 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper497f2e62010-05-19 19:20:36 +10002763 .halg.base = {
2764 .cra_name = "sha256",
2765 .cra_driver_name = "sha256-talitos",
2766 .cra_blocksize = SHA256_BLOCK_SIZE,
2767 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2768 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002769 }
2770 },
2771 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2772 DESC_HDR_SEL0_MDEUA |
2773 DESC_HDR_MODE0_MDEU_SHA256,
2774 },
2775 { .type = CRYPTO_ALG_TYPE_AHASH,
2776 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002777 .halg.digestsize = SHA384_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002778 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper497f2e62010-05-19 19:20:36 +10002779 .halg.base = {
2780 .cra_name = "sha384",
2781 .cra_driver_name = "sha384-talitos",
2782 .cra_blocksize = SHA384_BLOCK_SIZE,
2783 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2784 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002785 }
2786 },
2787 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2788 DESC_HDR_SEL0_MDEUB |
2789 DESC_HDR_MODE0_MDEUB_SHA384,
2790 },
2791 { .type = CRYPTO_ALG_TYPE_AHASH,
2792 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002793 .halg.digestsize = SHA512_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002794 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper497f2e62010-05-19 19:20:36 +10002795 .halg.base = {
2796 .cra_name = "sha512",
2797 .cra_driver_name = "sha512-talitos",
2798 .cra_blocksize = SHA512_BLOCK_SIZE,
2799 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2800 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002801 }
2802 },
2803 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2804 DESC_HDR_SEL0_MDEUB |
2805 DESC_HDR_MODE0_MDEUB_SHA512,
2806 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002807 { .type = CRYPTO_ALG_TYPE_AHASH,
2808 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002809 .halg.digestsize = MD5_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002810 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002811 .halg.base = {
2812 .cra_name = "hmac(md5)",
2813 .cra_driver_name = "hmac-md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002814 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper79b3a412011-11-21 16:13:25 +08002815 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2816 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002817 }
2818 },
2819 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2820 DESC_HDR_SEL0_MDEUA |
2821 DESC_HDR_MODE0_MDEU_MD5,
2822 },
2823 { .type = CRYPTO_ALG_TYPE_AHASH,
2824 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002825 .halg.digestsize = SHA1_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002826 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002827 .halg.base = {
2828 .cra_name = "hmac(sha1)",
2829 .cra_driver_name = "hmac-sha1-talitos",
2830 .cra_blocksize = SHA1_BLOCK_SIZE,
2831 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2832 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002833 }
2834 },
2835 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2836 DESC_HDR_SEL0_MDEUA |
2837 DESC_HDR_MODE0_MDEU_SHA1,
2838 },
2839 { .type = CRYPTO_ALG_TYPE_AHASH,
2840 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002841 .halg.digestsize = SHA224_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002842 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002843 .halg.base = {
2844 .cra_name = "hmac(sha224)",
2845 .cra_driver_name = "hmac-sha224-talitos",
2846 .cra_blocksize = SHA224_BLOCK_SIZE,
2847 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2848 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002849 }
2850 },
2851 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2852 DESC_HDR_SEL0_MDEUA |
2853 DESC_HDR_MODE0_MDEU_SHA224,
2854 },
2855 { .type = CRYPTO_ALG_TYPE_AHASH,
2856 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002857 .halg.digestsize = SHA256_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002858 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002859 .halg.base = {
2860 .cra_name = "hmac(sha256)",
2861 .cra_driver_name = "hmac-sha256-talitos",
2862 .cra_blocksize = SHA256_BLOCK_SIZE,
2863 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2864 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002865 }
2866 },
2867 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2868 DESC_HDR_SEL0_MDEUA |
2869 DESC_HDR_MODE0_MDEU_SHA256,
2870 },
2871 { .type = CRYPTO_ALG_TYPE_AHASH,
2872 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002873 .halg.digestsize = SHA384_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002874 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002875 .halg.base = {
2876 .cra_name = "hmac(sha384)",
2877 .cra_driver_name = "hmac-sha384-talitos",
2878 .cra_blocksize = SHA384_BLOCK_SIZE,
2879 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2880 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002881 }
2882 },
2883 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2884 DESC_HDR_SEL0_MDEUB |
2885 DESC_HDR_MODE0_MDEUB_SHA384,
2886 },
2887 { .type = CRYPTO_ALG_TYPE_AHASH,
2888 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002889 .halg.digestsize = SHA512_DIGEST_SIZE,
Horia Geant?3639ca82016-04-21 19:24:55 +03002890 .halg.statesize = sizeof(struct talitos_export_state),
Lee Nipper79b3a412011-11-21 16:13:25 +08002891 .halg.base = {
2892 .cra_name = "hmac(sha512)",
2893 .cra_driver_name = "hmac-sha512-talitos",
2894 .cra_blocksize = SHA512_BLOCK_SIZE,
2895 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2896 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002897 }
2898 },
2899 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2900 DESC_HDR_SEL0_MDEUB |
2901 DESC_HDR_MODE0_MDEUB_SHA512,
2902 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002903};
2904
2905struct talitos_crypto_alg {
2906 struct list_head entry;
2907 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002908 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002909};
2910
Jonas Eymann89d124c2016-04-19 20:33:47 +03002911static int talitos_init_common(struct talitos_ctx *ctx,
2912 struct talitos_crypto_alg *talitos_alg)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002913{
Kim Phillips5228f0f2011-07-15 11:21:38 +08002914 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002915
2916 /* update context with ptr to dev */
2917 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002918
Kim Phillips5228f0f2011-07-15 11:21:38 +08002919 /* assign SEC channel to tfm in round-robin fashion */
2920 priv = dev_get_drvdata(ctx->dev);
2921 ctx->ch = atomic_inc_return(&priv->last_chan) &
2922 (priv->num_channels - 1);
2923
Kim Phillips9c4a7962008-06-23 19:50:15 +08002924 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002925 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002926
Kim Phillips602dba52011-07-15 11:21:39 +08002927 /* select done notification */
2928 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2929
Lee Nipper497f2e62010-05-19 19:20:36 +10002930 return 0;
2931}
2932
Jonas Eymann89d124c2016-04-19 20:33:47 +03002933static int talitos_cra_init(struct crypto_tfm *tfm)
2934{
2935 struct crypto_alg *alg = tfm->__crt_alg;
2936 struct talitos_crypto_alg *talitos_alg;
2937 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2938
2939 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2940 talitos_alg = container_of(__crypto_ahash_alg(alg),
2941 struct talitos_crypto_alg,
2942 algt.alg.hash);
2943 else
2944 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2945 algt.alg.crypto);
2946
2947 return talitos_init_common(ctx, talitos_alg);
2948}
2949
Herbert Xuaeb4c132015-07-30 17:53:22 +08002950static int talitos_cra_init_aead(struct crypto_aead *tfm)
Lee Nipper497f2e62010-05-19 19:20:36 +10002951{
Jonas Eymann89d124c2016-04-19 20:33:47 +03002952 struct aead_alg *alg = crypto_aead_alg(tfm);
2953 struct talitos_crypto_alg *talitos_alg;
2954 struct talitos_ctx *ctx = crypto_aead_ctx(tfm);
2955
2956 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2957 algt.alg.aead);
2958
2959 return talitos_init_common(ctx, talitos_alg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002960}
2961
Lee Nipper497f2e62010-05-19 19:20:36 +10002962static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2963{
2964 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2965
2966 talitos_cra_init(tfm);
2967
2968 ctx->keylen = 0;
2969 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2970 sizeof(struct talitos_ahash_req_ctx));
2971
2972 return 0;
2973}
2974
Kim Phillips9c4a7962008-06-23 19:50:15 +08002975/*
2976 * given the alg's descriptor header template, determine whether descriptor
2977 * type and primary/secondary execution units required match the hw
2978 * capabilities description provided in the device tree node.
2979 */
2980static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2981{
2982 struct talitos_private *priv = dev_get_drvdata(dev);
2983 int ret;
2984
2985 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2986 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2987
2988 if (SECONDARY_EU(desc_hdr_template))
2989 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2990 & priv->exec_units);
2991
2992 return ret;
2993}
2994
Grant Likely2dc11582010-08-06 09:25:50 -06002995static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002996{
2997 struct device *dev = &ofdev->dev;
2998 struct talitos_private *priv = dev_get_drvdata(dev);
2999 struct talitos_crypto_alg *t_alg, *n;
3000 int i;
3001
3002 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10003003 switch (t_alg->algt.type) {
3004 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipperacbf7c622010-05-19 19:19:33 +10003005 break;
Herbert Xuaeb4c132015-07-30 17:53:22 +08003006 case CRYPTO_ALG_TYPE_AEAD:
3007 crypto_unregister_aead(&t_alg->algt.alg.aead);
Lee Nipperacbf7c622010-05-19 19:19:33 +10003008 case CRYPTO_ALG_TYPE_AHASH:
3009 crypto_unregister_ahash(&t_alg->algt.alg.hash);
3010 break;
3011 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003012 list_del(&t_alg->entry);
3013 kfree(t_alg);
3014 }
3015
3016 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
3017 talitos_unregister_rng(dev);
3018
Aaron Sierra35a3bb32015-08-05 16:52:08 -05003019 for (i = 0; priv->chan && i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08003020 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003021
Kim Phillips4b9926282009-08-13 11:50:38 +10003022 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003023
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003024 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003025 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003026 free_irq(priv->irq[i], dev);
3027 irq_dispose_mapping(priv->irq[i]);
3028 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003029
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003030 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003031 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003032 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003033
3034 iounmap(priv->reg);
3035
Kim Phillips9c4a7962008-06-23 19:50:15 +08003036 kfree(priv);
3037
3038 return 0;
3039}
3040
3041static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
3042 struct talitos_alg_template
3043 *template)
3044{
Kim Phillips60f208d2010-05-19 19:21:53 +10003045 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003046 struct talitos_crypto_alg *t_alg;
3047 struct crypto_alg *alg;
3048
3049 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
3050 if (!t_alg)
3051 return ERR_PTR(-ENOMEM);
3052
Lee Nipperacbf7c622010-05-19 19:19:33 +10003053 t_alg->algt = *template;
3054
3055 switch (t_alg->algt.type) {
3056 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10003057 alg = &t_alg->algt.alg.crypto;
3058 alg->cra_init = talitos_cra_init;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05003059 alg->cra_type = &crypto_ablkcipher_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05003060 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
3061 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
3062 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
3063 alg->cra_ablkcipher.geniv = "eseqiv";
Lee Nipper497f2e62010-05-19 19:20:36 +10003064 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10003065 case CRYPTO_ALG_TYPE_AEAD:
Herbert Xuaeb4c132015-07-30 17:53:22 +08003066 alg = &t_alg->algt.alg.aead.base;
Herbert Xuaeb4c132015-07-30 17:53:22 +08003067 t_alg->algt.alg.aead.init = talitos_cra_init_aead;
3068 t_alg->algt.alg.aead.setkey = aead_setkey;
3069 t_alg->algt.alg.aead.encrypt = aead_encrypt;
3070 t_alg->algt.alg.aead.decrypt = aead_decrypt;
Lee Nipperacbf7c622010-05-19 19:19:33 +10003071 break;
3072 case CRYPTO_ALG_TYPE_AHASH:
3073 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10003074 alg->cra_init = talitos_cra_init_ahash;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05003075 alg->cra_type = &crypto_ahash_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05003076 t_alg->algt.alg.hash.init = ahash_init;
3077 t_alg->algt.alg.hash.update = ahash_update;
3078 t_alg->algt.alg.hash.final = ahash_final;
3079 t_alg->algt.alg.hash.finup = ahash_finup;
3080 t_alg->algt.alg.hash.digest = ahash_digest;
LEROY Christophe70117b72017-09-12 11:03:39 +02003081 if (!strncmp(alg->cra_name, "hmac", 4))
3082 t_alg->algt.alg.hash.setkey = ahash_setkey;
Horia Geant?3639ca82016-04-21 19:24:55 +03003083 t_alg->algt.alg.hash.import = ahash_import;
3084 t_alg->algt.alg.hash.export = ahash_export;
Kim Phillipsb286e002012-08-08 20:33:34 -05003085
Lee Nipper79b3a412011-11-21 16:13:25 +08003086 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06003087 !strncmp(alg->cra_name, "hmac", 4)) {
3088 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08003089 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06003090 }
Kim Phillips60f208d2010-05-19 19:21:53 +10003091 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08003092 (!strcmp(alg->cra_name, "sha224") ||
3093 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10003094 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
3095 t_alg->algt.desc_hdr_template =
3096 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
3097 DESC_HDR_SEL0_MDEUA |
3098 DESC_HDR_MODE0_MDEU_SHA256;
3099 }
Lee Nipper497f2e62010-05-19 19:20:36 +10003100 break;
Kim Phillips1d119112010-09-23 15:55:27 +08003101 default:
3102 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
Horia Geant?5fa7dad2015-05-11 20:03:24 +03003103 kfree(t_alg);
Kim Phillips1d119112010-09-23 15:55:27 +08003104 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10003105 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003106
Kim Phillips9c4a7962008-06-23 19:50:15 +08003107 alg->cra_module = THIS_MODULE;
LEROY Christopheb0057762016-06-06 13:20:44 +02003108 if (t_alg->algt.priority)
3109 alg->cra_priority = t_alg->algt.priority;
3110 else
3111 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003112 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003113 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01003114 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003115
Kim Phillips9c4a7962008-06-23 19:50:15 +08003116 t_alg->dev = dev;
3117
3118 return t_alg;
3119}
3120
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003121static int talitos_probe_irq(struct platform_device *ofdev)
3122{
3123 struct device *dev = &ofdev->dev;
3124 struct device_node *np = ofdev->dev.of_node;
3125 struct talitos_private *priv = dev_get_drvdata(dev);
3126 int err;
LEROY Christophedd3c0982015-04-17 16:32:13 +02003127 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003128
3129 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003130 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003131 dev_err(dev, "failed to map irq\n");
3132 return -EINVAL;
3133 }
LEROY Christophedd3c0982015-04-17 16:32:13 +02003134 if (is_sec1) {
3135 err = request_irq(priv->irq[0], talitos1_interrupt_4ch, 0,
3136 dev_driver_string(dev), dev);
3137 goto primary_out;
3138 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003139
3140 priv->irq[1] = irq_of_parse_and_map(np, 1);
3141
3142 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003143 if (!priv->irq[1]) {
LEROY Christophedd3c0982015-04-17 16:32:13 +02003144 err = request_irq(priv->irq[0], talitos2_interrupt_4ch, 0,
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003145 dev_driver_string(dev), dev);
3146 goto primary_out;
3147 }
3148
LEROY Christophedd3c0982015-04-17 16:32:13 +02003149 err = request_irq(priv->irq[0], talitos2_interrupt_ch0_2, 0,
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003150 dev_driver_string(dev), dev);
3151 if (err)
3152 goto primary_out;
3153
3154 /* get the secondary irq line */
LEROY Christophedd3c0982015-04-17 16:32:13 +02003155 err = request_irq(priv->irq[1], talitos2_interrupt_ch1_3, 0,
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003156 dev_driver_string(dev), dev);
3157 if (err) {
3158 dev_err(dev, "failed to request secondary irq\n");
3159 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003160 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003161 }
3162
3163 return err;
3164
3165primary_out:
3166 if (err) {
3167 dev_err(dev, "failed to request primary irq\n");
3168 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003169 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003170 }
3171
3172 return err;
3173}
3174
Grant Likely1c48a5c2011-02-17 02:43:24 -07003175static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08003176{
3177 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07003178 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003179 struct talitos_private *priv;
3180 const unsigned int *prop;
3181 int i, err;
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02003182 int stride;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003183
3184 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
3185 if (!priv)
3186 return -ENOMEM;
3187
Kevin Haof3de9cb2014-01-28 20:17:23 +08003188 INIT_LIST_HEAD(&priv->alg_list);
3189
Kim Phillips9c4a7962008-06-23 19:50:15 +08003190 dev_set_drvdata(dev, priv);
3191
3192 priv->ofdev = ofdev;
3193
Horia Geanta511d63c2012-03-30 17:49:53 +03003194 spin_lock_init(&priv->reg_lock);
3195
Kim Phillips9c4a7962008-06-23 19:50:15 +08003196 priv->reg = of_iomap(np, 0);
3197 if (!priv->reg) {
3198 dev_err(dev, "failed to of_iomap\n");
3199 err = -ENOMEM;
3200 goto err_out;
3201 }
3202
3203 /* get SEC version capabilities from device tree */
3204 prop = of_get_property(np, "fsl,num-channels", NULL);
3205 if (prop)
3206 priv->num_channels = *prop;
3207
3208 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
3209 if (prop)
3210 priv->chfifo_len = *prop;
3211
3212 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
3213 if (prop)
3214 priv->exec_units = *prop;
3215
3216 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
3217 if (prop)
3218 priv->desc_types = *prop;
3219
3220 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
3221 !priv->exec_units || !priv->desc_types) {
3222 dev_err(dev, "invalid property data in device tree node\n");
3223 err = -EINVAL;
3224 goto err_out;
3225 }
3226
Lee Nipperf3c85bc2008-07-30 16:26:57 +08003227 if (of_device_is_compatible(np, "fsl,sec3.0"))
3228 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
3229
Kim Phillipsfe5720e2008-10-12 20:33:14 +08003230 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10003231 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08003232 TALITOS_FTR_SHA224_HWINIT |
3233 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08003234
LEROY Christophe21590882015-04-17 16:32:05 +02003235 if (of_device_is_compatible(np, "fsl,sec1.0"))
3236 priv->features |= TALITOS_FTR_SEC1;
3237
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02003238 if (of_device_is_compatible(np, "fsl,sec1.2")) {
3239 priv->reg_deu = priv->reg + TALITOS12_DEU;
3240 priv->reg_aesu = priv->reg + TALITOS12_AESU;
3241 priv->reg_mdeu = priv->reg + TALITOS12_MDEU;
3242 stride = TALITOS1_CH_STRIDE;
3243 } else if (of_device_is_compatible(np, "fsl,sec1.0")) {
3244 priv->reg_deu = priv->reg + TALITOS10_DEU;
3245 priv->reg_aesu = priv->reg + TALITOS10_AESU;
3246 priv->reg_mdeu = priv->reg + TALITOS10_MDEU;
3247 priv->reg_afeu = priv->reg + TALITOS10_AFEU;
3248 priv->reg_rngu = priv->reg + TALITOS10_RNGU;
3249 priv->reg_pkeu = priv->reg + TALITOS10_PKEU;
3250 stride = TALITOS1_CH_STRIDE;
3251 } else {
3252 priv->reg_deu = priv->reg + TALITOS2_DEU;
3253 priv->reg_aesu = priv->reg + TALITOS2_AESU;
3254 priv->reg_mdeu = priv->reg + TALITOS2_MDEU;
3255 priv->reg_afeu = priv->reg + TALITOS2_AFEU;
3256 priv->reg_rngu = priv->reg + TALITOS2_RNGU;
3257 priv->reg_pkeu = priv->reg + TALITOS2_PKEU;
3258 priv->reg_keu = priv->reg + TALITOS2_KEU;
3259 priv->reg_crcu = priv->reg + TALITOS2_CRCU;
3260 stride = TALITOS2_CH_STRIDE;
3261 }
3262
LEROY Christophedd3c0982015-04-17 16:32:13 +02003263 err = talitos_probe_irq(ofdev);
3264 if (err)
3265 goto err_out;
3266
3267 if (of_device_is_compatible(np, "fsl,sec1.0")) {
3268 tasklet_init(&priv->done_task[0], talitos1_done_4ch,
3269 (unsigned long)dev);
3270 } else {
3271 if (!priv->irq[1]) {
3272 tasklet_init(&priv->done_task[0], talitos2_done_4ch,
3273 (unsigned long)dev);
3274 } else {
3275 tasklet_init(&priv->done_task[0], talitos2_done_ch0_2,
3276 (unsigned long)dev);
3277 tasklet_init(&priv->done_task[1], talitos2_done_ch1_3,
3278 (unsigned long)dev);
3279 }
3280 }
3281
Kim Phillips4b9926282009-08-13 11:50:38 +10003282 priv->chan = kzalloc(sizeof(struct talitos_channel) *
3283 priv->num_channels, GFP_KERNEL);
3284 if (!priv->chan) {
3285 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08003286 err = -ENOMEM;
3287 goto err_out;
3288 }
3289
Martin Hicksf641ddd2015-03-03 08:21:33 -05003290 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
3291
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003292 for (i = 0; i < priv->num_channels; i++) {
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02003293 priv->chan[i].reg = priv->reg + stride * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06003294 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08003295 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
Kim Phillipsad42d5f2011-11-21 16:13:27 +08003296
Kim Phillips4b9926282009-08-13 11:50:38 +10003297 spin_lock_init(&priv->chan[i].head_lock);
3298 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003299
Kim Phillips4b9926282009-08-13 11:50:38 +10003300 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
3301 priv->fifo_len, GFP_KERNEL);
3302 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08003303 dev_err(dev, "failed to allocate request fifo %d\n", i);
3304 err = -ENOMEM;
3305 goto err_out;
3306 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003307
Kim Phillips4b9926282009-08-13 11:50:38 +10003308 atomic_set(&priv->chan[i].submit_count,
3309 -(priv->chfifo_len - 1));
Martin Hicksf641ddd2015-03-03 08:21:33 -05003310 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003311
Kim Phillips81eb0242009-08-13 11:51:51 +10003312 dma_set_mask(dev, DMA_BIT_MASK(36));
3313
Kim Phillips9c4a7962008-06-23 19:50:15 +08003314 /* reset and initialize the h/w */
3315 err = init_device(dev);
3316 if (err) {
3317 dev_err(dev, "failed to initialize device\n");
3318 goto err_out;
3319 }
3320
3321 /* register the RNG, if available */
3322 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
3323 err = talitos_register_rng(dev);
3324 if (err) {
3325 dev_err(dev, "failed to register hwrng: %d\n", err);
3326 goto err_out;
3327 } else
3328 dev_info(dev, "hwrng\n");
3329 }
3330
3331 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08003332 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3333 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
3334 struct talitos_crypto_alg *t_alg;
Herbert Xuaeb4c132015-07-30 17:53:22 +08003335 struct crypto_alg *alg = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003336
3337 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
3338 if (IS_ERR(t_alg)) {
3339 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06003340 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08003341 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08003342 goto err_out;
3343 }
3344
Lee Nipperacbf7c622010-05-19 19:19:33 +10003345 switch (t_alg->algt.type) {
3346 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipperacbf7c622010-05-19 19:19:33 +10003347 err = crypto_register_alg(
3348 &t_alg->algt.alg.crypto);
Herbert Xuaeb4c132015-07-30 17:53:22 +08003349 alg = &t_alg->algt.alg.crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10003350 break;
Herbert Xuaeb4c132015-07-30 17:53:22 +08003351
3352 case CRYPTO_ALG_TYPE_AEAD:
3353 err = crypto_register_aead(
3354 &t_alg->algt.alg.aead);
3355 alg = &t_alg->algt.alg.aead.base;
3356 break;
3357
Lee Nipperacbf7c622010-05-19 19:19:33 +10003358 case CRYPTO_ALG_TYPE_AHASH:
3359 err = crypto_register_ahash(
3360 &t_alg->algt.alg.hash);
Herbert Xuaeb4c132015-07-30 17:53:22 +08003361 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipperacbf7c622010-05-19 19:19:33 +10003362 break;
3363 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08003364 if (err) {
3365 dev_err(dev, "%s alg registration failed\n",
Herbert Xuaeb4c132015-07-30 17:53:22 +08003366 alg->cra_driver_name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003367 kfree(t_alg);
Horia Geanta991155b2013-03-20 16:31:38 +02003368 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08003369 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003370 }
3371 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08003372 if (!list_empty(&priv->alg_list))
3373 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
3374 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08003375
3376 return 0;
3377
3378err_out:
3379 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003380
3381 return err;
3382}
3383
Márton Németh6c3f9752010-01-17 21:54:01 +11003384static const struct of_device_id talitos_match[] = {
LEROY Christophe0635b7d2015-04-17 16:32:20 +02003385#ifdef CONFIG_CRYPTO_DEV_TALITOS1
3386 {
3387 .compatible = "fsl,sec1.0",
3388 },
3389#endif
3390#ifdef CONFIG_CRYPTO_DEV_TALITOS2
Kim Phillips9c4a7962008-06-23 19:50:15 +08003391 {
3392 .compatible = "fsl,sec2.0",
3393 },
LEROY Christophe0635b7d2015-04-17 16:32:20 +02003394#endif
Kim Phillips9c4a7962008-06-23 19:50:15 +08003395 {},
3396};
3397MODULE_DEVICE_TABLE(of, talitos_match);
3398
Grant Likely1c48a5c2011-02-17 02:43:24 -07003399static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003400 .driver = {
3401 .name = "talitos",
Grant Likely40182942010-04-13 16:13:02 -07003402 .of_match_table = talitos_match,
3403 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08003404 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00003405 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08003406};
3407
Axel Lin741e8c22011-11-26 21:26:19 +08003408module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08003409
3410MODULE_LICENSE("GPL");
3411MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
3412MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");