blob: 6d77699a821b49f54d813346ac9ae97acbf47ed7 [file] [log] [blame]
Kim Phillips9c4a7962008-06-23 19:50:15 +08001/*
2 * talitos - Freescale Integrated Security Engine (SEC) device driver
3 *
Kim Phillips5228f0f2011-07-15 11:21:38 +08004 * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
Kim Phillips9c4a7962008-06-23 19:50:15 +08005 *
6 * Scatterlist Crypto API glue code copied from files with the following:
7 * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * Crypto algorithm registration code copied from hifn driver:
10 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/mod_devicetable.h>
31#include <linux/device.h>
32#include <linux/interrupt.h>
33#include <linux/crypto.h>
34#include <linux/hw_random.h>
Rob Herring5af50732013-09-17 14:28:33 -050035#include <linux/of_address.h>
36#include <linux/of_irq.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080037#include <linux/of_platform.h>
38#include <linux/dma-mapping.h>
39#include <linux/io.h>
40#include <linux/spinlock.h>
41#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080043
44#include <crypto/algapi.h>
45#include <crypto/aes.h>
Lee Nipper3952f172008-07-10 18:29:18 +080046#include <crypto/des.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080047#include <crypto/sha.h>
Lee Nipper497f2e62010-05-19 19:20:36 +100048#include <crypto/md5.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080049#include <crypto/aead.h>
50#include <crypto/authenc.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080051#include <crypto/skcipher.h>
Lee Nipperacbf7c622010-05-19 19:19:33 +100052#include <crypto/hash.h>
53#include <crypto/internal/hash.h>
Lee Nipper4de9d0b2009-03-29 15:52:32 +080054#include <crypto/scatterwalk.h>
Kim Phillips9c4a7962008-06-23 19:50:15 +080055
56#include "talitos.h"
57
LEROY 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
LEROY Christophe922f9dc2015-04-17 16:32:07 +020066static void to_talitos_ptr_len(struct talitos_ptr *ptr, unsigned short len,
67 bool is_sec1)
LEROY Christophe538caf82015-04-17 16:31:59 +020068{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020069 if (is_sec1) {
70 ptr->res = 0;
71 ptr->len1 = cpu_to_be16(len);
72 } else {
73 ptr->len = cpu_to_be16(len);
74 }
LEROY Christophe538caf82015-04-17 16:31:59 +020075}
76
LEROY Christophe922f9dc2015-04-17 16:32:07 +020077static unsigned short from_talitos_ptr_len(struct talitos_ptr *ptr,
78 bool is_sec1)
LEROY Christophe538caf82015-04-17 16:31:59 +020079{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020080 if (is_sec1)
81 return be16_to_cpu(ptr->len1);
82 else
83 return be16_to_cpu(ptr->len);
LEROY Christophe538caf82015-04-17 16:31:59 +020084}
85
LEROY Christophe922f9dc2015-04-17 16:32:07 +020086static void to_talitos_ptr_extent_clear(struct talitos_ptr *ptr, bool is_sec1)
LEROY Christophe185eb792015-04-17 16:31:55 +020087{
LEROY Christophe922f9dc2015-04-17 16:32:07 +020088 if (!is_sec1)
89 ptr->j_extent = 0;
LEROY Christophe185eb792015-04-17 16:31:55 +020090}
91
Kim Phillips9c4a7962008-06-23 19:50:15 +080092/*
93 * map virtual single (contiguous) pointer to h/w descriptor pointer
94 */
95static void map_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +020096 struct talitos_ptr *ptr,
Kim Phillips9c4a7962008-06-23 19:50:15 +080097 unsigned short len, void *data,
Kim Phillips9c4a7962008-06-23 19:50:15 +080098 enum dma_data_direction dir)
99{
Kim Phillips81eb0242009-08-13 11:51:51 +1000100 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200101 struct talitos_private *priv = dev_get_drvdata(dev);
102 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips81eb0242009-08-13 11:51:51 +1000103
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200104 to_talitos_ptr_len(ptr, len, is_sec1);
105 to_talitos_ptr(ptr, dma_addr, is_sec1);
106 to_talitos_ptr_extent_clear(ptr, is_sec1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800107}
108
109/*
110 * unmap bus single (contiguous) h/w descriptor pointer
111 */
112static void unmap_single_talitos_ptr(struct device *dev,
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200113 struct talitos_ptr *ptr,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800114 enum dma_data_direction dir)
115{
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200116 struct talitos_private *priv = dev_get_drvdata(dev);
117 bool is_sec1 = has_ftr_sec1(priv);
118
LEROY Christopheedc6bd62015-04-17 16:31:53 +0200119 dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200120 from_talitos_ptr_len(ptr, is_sec1), dir);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800121}
122
123static int reset_channel(struct device *dev, int ch)
124{
125 struct talitos_private *priv = dev_get_drvdata(dev);
126 unsigned int timeout = TALITOS_TIMEOUT;
127
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800128 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800129
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800130 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800131 && --timeout)
132 cpu_relax();
133
134 if (timeout == 0) {
135 dev_err(dev, "failed to reset channel %d\n", ch);
136 return -EIO;
137 }
138
Kim Phillips81eb0242009-08-13 11:51:51 +1000139 /* set 36-bit addressing, done writeback enable and done IRQ enable */
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800140 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
Kim Phillips81eb0242009-08-13 11:51:51 +1000141 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800142
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800143 /* and ICCR writeback, if available */
144 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800145 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800146 TALITOS_CCCR_LO_IWSE);
147
Kim Phillips9c4a7962008-06-23 19:50:15 +0800148 return 0;
149}
150
151static int reset_device(struct device *dev)
152{
153 struct talitos_private *priv = dev_get_drvdata(dev);
154 unsigned int timeout = TALITOS_TIMEOUT;
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800155 u32 mcr = TALITOS_MCR_SWR;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800156
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800157 setbits32(priv->reg + TALITOS_MCR, mcr);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800158
159 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
160 && --timeout)
161 cpu_relax();
162
Kim Phillips2cdba3c2011-12-12 14:59:11 -0600163 if (priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800164 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
165 setbits32(priv->reg + TALITOS_MCR, mcr);
166 }
167
Kim Phillips9c4a7962008-06-23 19:50:15 +0800168 if (timeout == 0) {
169 dev_err(dev, "failed to reset device\n");
170 return -EIO;
171 }
172
173 return 0;
174}
175
176/*
177 * Reset and initialize the device
178 */
179static int init_device(struct device *dev)
180{
181 struct talitos_private *priv = dev_get_drvdata(dev);
182 int ch, err;
183
184 /*
185 * Master reset
186 * errata documentation: warning: certain SEC interrupts
187 * are not fully cleared by writing the MCR:SWR bit,
188 * set bit twice to completely reset
189 */
190 err = reset_device(dev);
191 if (err)
192 return err;
193
194 err = reset_device(dev);
195 if (err)
196 return err;
197
198 /* reset channels */
199 for (ch = 0; ch < priv->num_channels; ch++) {
200 err = reset_channel(dev, ch);
201 if (err)
202 return err;
203 }
204
205 /* enable channel done and error interrupts */
206 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
207 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
208
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800209 /* disable integrity check error interrupts (use writeback instead) */
210 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200211 setbits32(priv->reg_mdeu + TALITOS_EUICR_LO,
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800212 TALITOS_MDEUICR_LO_ICE);
213
Kim Phillips9c4a7962008-06-23 19:50:15 +0800214 return 0;
215}
216
217/**
218 * talitos_submit - submits a descriptor to the device for processing
219 * @dev: the SEC device to be used
Kim Phillips5228f0f2011-07-15 11:21:38 +0800220 * @ch: the SEC device channel to be used
Kim Phillips9c4a7962008-06-23 19:50:15 +0800221 * @desc: the descriptor to be processed by the device
222 * @callback: whom to call when processing is complete
223 * @context: a handle for use by caller (optional)
224 *
225 * desc must contain valid dma-mapped (bus physical) address pointers.
226 * callback must check err and feedback in descriptor header
227 * for device processing status.
228 */
Horia Geanta865d5062012-07-03 19:16:52 +0300229int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
230 void (*callback)(struct device *dev,
231 struct talitos_desc *desc,
232 void *context, int error),
233 void *context)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800234{
235 struct talitos_private *priv = dev_get_drvdata(dev);
236 struct talitos_request *request;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800237 unsigned long flags;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800238 int head;
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200239 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800240
Kim Phillips4b9926282009-08-13 11:50:38 +1000241 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800242
Kim Phillips4b9926282009-08-13 11:50:38 +1000243 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
Kim Phillipsec6644d2008-07-17 20:16:40 +0800244 /* h/w fifo is full */
Kim Phillips4b9926282009-08-13 11:50:38 +1000245 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800246 return -EAGAIN;
247 }
248
Kim Phillips4b9926282009-08-13 11:50:38 +1000249 head = priv->chan[ch].head;
250 request = &priv->chan[ch].fifo[head];
Kim Phillipsec6644d2008-07-17 20:16:40 +0800251
Kim Phillips9c4a7962008-06-23 19:50:15 +0800252 /* map descriptor and save caller data */
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200253 if (is_sec1) {
254 desc->hdr1 = desc->hdr;
255 desc->next_desc = 0;
256 request->dma_desc = dma_map_single(dev, &desc->hdr1,
257 TALITOS_DESC_SIZE,
258 DMA_BIDIRECTIONAL);
259 } else {
260 request->dma_desc = dma_map_single(dev, desc,
261 TALITOS_DESC_SIZE,
262 DMA_BIDIRECTIONAL);
263 }
Kim Phillips9c4a7962008-06-23 19:50:15 +0800264 request->callback = callback;
265 request->context = context;
266
267 /* increment fifo head */
Kim Phillips4b9926282009-08-13 11:50:38 +1000268 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800269
270 smp_wmb();
271 request->desc = desc;
272
273 /* GO! */
274 wmb();
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800275 out_be32(priv->chan[ch].reg + TALITOS_FF,
276 upper_32_bits(request->dma_desc));
277 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
Kim Phillipsa7524472010-09-23 15:56:38 +0800278 lower_32_bits(request->dma_desc));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800279
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
282 return -EINPROGRESS;
283}
Horia Geanta865d5062012-07-03 19:16:52 +0300284EXPORT_SYMBOL(talitos_submit);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800285
286/*
287 * process what was done, notify callback of error if not
288 */
289static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
290{
291 struct talitos_private *priv = dev_get_drvdata(dev);
292 struct talitos_request *request, saved_req;
293 unsigned long flags;
294 int tail, status;
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200295 bool is_sec1 = has_ftr_sec1(priv);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800296
Kim Phillips4b9926282009-08-13 11:50:38 +1000297 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800298
Kim Phillips4b9926282009-08-13 11:50:38 +1000299 tail = priv->chan[ch].tail;
300 while (priv->chan[ch].fifo[tail].desc) {
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200301 __be32 hdr;
302
Kim Phillips4b9926282009-08-13 11:50:38 +1000303 request = &priv->chan[ch].fifo[tail];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800304
305 /* descriptors with their done bits set don't get the error */
306 rmb();
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200307 hdr = is_sec1 ? request->desc->hdr1 : request->desc->hdr;
308
309 if ((hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800310 status = 0;
Lee Nipperca38a812008-12-20 17:09:25 +1100311 else
Kim Phillips9c4a7962008-06-23 19:50:15 +0800312 if (!error)
313 break;
314 else
315 status = error;
316
317 dma_unmap_single(dev, request->dma_desc,
LEROY Christophe7d607c6a2015-04-17 16:32:09 +0200318 TALITOS_DESC_SIZE,
Kim Phillipse938e462009-03-29 15:53:23 +0800319 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800320
321 /* copy entries so we can call callback outside lock */
322 saved_req.desc = request->desc;
323 saved_req.callback = request->callback;
324 saved_req.context = request->context;
325
326 /* release request entry in fifo */
327 smp_wmb();
328 request->desc = NULL;
329
330 /* increment fifo tail */
Kim Phillips4b9926282009-08-13 11:50:38 +1000331 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800332
Kim Phillips4b9926282009-08-13 11:50:38 +1000333 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800334
Kim Phillips4b9926282009-08-13 11:50:38 +1000335 atomic_dec(&priv->chan[ch].submit_count);
Kim Phillipsec6644d2008-07-17 20:16:40 +0800336
Kim Phillips9c4a7962008-06-23 19:50:15 +0800337 saved_req.callback(dev, saved_req.desc, saved_req.context,
338 status);
339 /* channel may resume processing in single desc error case */
340 if (error && !reset_ch && status == error)
341 return;
Kim Phillips4b9926282009-08-13 11:50:38 +1000342 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
343 tail = priv->chan[ch].tail;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800344 }
345
Kim Phillips4b9926282009-08-13 11:50:38 +1000346 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800347}
348
349/*
350 * process completed requests for channels that have done status
351 */
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800352#define DEF_TALITOS_DONE(name, ch_done_mask) \
353static void talitos_done_##name(unsigned long data) \
354{ \
355 struct device *dev = (struct device *)data; \
356 struct talitos_private *priv = dev_get_drvdata(dev); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300357 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800358 \
359 if (ch_done_mask & 1) \
360 flush_channel(dev, 0, 0, 0); \
361 if (priv->num_channels == 1) \
362 goto out; \
363 if (ch_done_mask & (1 << 2)) \
364 flush_channel(dev, 1, 0, 0); \
365 if (ch_done_mask & (1 << 4)) \
366 flush_channel(dev, 2, 0, 0); \
367 if (ch_done_mask & (1 << 6)) \
368 flush_channel(dev, 3, 0, 0); \
369 \
370out: \
371 /* At this point, all completed channels have been processed */ \
372 /* Unmask done interrupts for channels completed later on. */ \
Horia Geanta511d63c2012-03-30 17:49:53 +0300373 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800374 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
375 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
Horia Geanta511d63c2012-03-30 17:49:53 +0300376 spin_unlock_irqrestore(&priv->reg_lock, flags); \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800377}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800378DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
379DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
380DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800381
382/*
383 * locate current (offending) descriptor
384 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200385static u32 current_desc_hdr(struct device *dev, int ch)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800386{
387 struct talitos_private *priv = dev_get_drvdata(dev);
Horia Geantab62ffd82013-11-13 12:20:37 +0200388 int tail, iter;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800389 dma_addr_t cur_desc;
390
Horia Geantab62ffd82013-11-13 12:20:37 +0200391 cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
392 cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800393
Horia Geantab62ffd82013-11-13 12:20:37 +0200394 if (!cur_desc) {
395 dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
396 return 0;
397 }
398
399 tail = priv->chan[ch].tail;
400
401 iter = tail;
402 while (priv->chan[ch].fifo[iter].dma_desc != cur_desc) {
403 iter = (iter + 1) & (priv->fifo_len - 1);
404 if (iter == tail) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800405 dev_err(dev, "couldn't locate current descriptor\n");
Kim Phillips3e721ae2011-10-21 15:20:28 +0200406 return 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800407 }
408 }
409
Horia Geantab62ffd82013-11-13 12:20:37 +0200410 return priv->chan[ch].fifo[iter].desc->hdr;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800411}
412
413/*
414 * user diagnostics; report root cause of error based on execution unit status
415 */
Kim Phillips3e721ae2011-10-21 15:20:28 +0200416static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800417{
418 struct talitos_private *priv = dev_get_drvdata(dev);
419 int i;
420
Kim Phillips3e721ae2011-10-21 15:20:28 +0200421 if (!desc_hdr)
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800422 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
Kim Phillips3e721ae2011-10-21 15:20:28 +0200423
424 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800425 case DESC_HDR_SEL0_AFEU:
426 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200427 in_be32(priv->reg_afeu + TALITOS_EUISR),
428 in_be32(priv->reg_afeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800429 break;
430 case DESC_HDR_SEL0_DEU:
431 dev_err(dev, "DEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200432 in_be32(priv->reg_deu + TALITOS_EUISR),
433 in_be32(priv->reg_deu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800434 break;
435 case DESC_HDR_SEL0_MDEUA:
436 case DESC_HDR_SEL0_MDEUB:
437 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200438 in_be32(priv->reg_mdeu + TALITOS_EUISR),
439 in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800440 break;
441 case DESC_HDR_SEL0_RNG:
442 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200443 in_be32(priv->reg_rngu + TALITOS_ISR),
444 in_be32(priv->reg_rngu + TALITOS_ISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800445 break;
446 case DESC_HDR_SEL0_PKEU:
447 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200448 in_be32(priv->reg_pkeu + TALITOS_EUISR),
449 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800450 break;
451 case DESC_HDR_SEL0_AESU:
452 dev_err(dev, "AESUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200453 in_be32(priv->reg_aesu + TALITOS_EUISR),
454 in_be32(priv->reg_aesu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800455 break;
456 case DESC_HDR_SEL0_CRCU:
457 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200458 in_be32(priv->reg_crcu + TALITOS_EUISR),
459 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800460 break;
461 case DESC_HDR_SEL0_KEU:
462 dev_err(dev, "KEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200463 in_be32(priv->reg_pkeu + TALITOS_EUISR),
464 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800465 break;
466 }
467
Kim Phillips3e721ae2011-10-21 15:20:28 +0200468 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800469 case DESC_HDR_SEL1_MDEUA:
470 case DESC_HDR_SEL1_MDEUB:
471 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200472 in_be32(priv->reg_mdeu + TALITOS_EUISR),
473 in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800474 break;
475 case DESC_HDR_SEL1_CRCU:
476 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200477 in_be32(priv->reg_crcu + TALITOS_EUISR),
478 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800479 break;
480 }
481
482 for (i = 0; i < 8; i++)
483 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800484 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
485 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800486}
487
488/*
489 * recover from error interrupts
490 */
Kim Phillips5e718a02011-12-12 14:59:12 -0600491static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800492{
Kim Phillips9c4a7962008-06-23 19:50:15 +0800493 struct talitos_private *priv = dev_get_drvdata(dev);
494 unsigned int timeout = TALITOS_TIMEOUT;
495 int ch, error, reset_dev = 0, reset_ch = 0;
Kim Phillips40405f12008-10-12 20:19:35 +0800496 u32 v, v_lo;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800497
498 for (ch = 0; ch < priv->num_channels; ch++) {
499 /* skip channels without errors */
500 if (!(isr & (1 << (ch * 2 + 1))))
501 continue;
502
503 error = -EINVAL;
504
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800505 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
506 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800507
508 if (v_lo & TALITOS_CCPSR_LO_DOF) {
509 dev_err(dev, "double fetch fifo overflow error\n");
510 error = -EAGAIN;
511 reset_ch = 1;
512 }
513 if (v_lo & TALITOS_CCPSR_LO_SOF) {
514 /* h/w dropped descriptor */
515 dev_err(dev, "single fetch fifo overflow error\n");
516 error = -EAGAIN;
517 }
518 if (v_lo & TALITOS_CCPSR_LO_MDTE)
519 dev_err(dev, "master data transfer error\n");
520 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
521 dev_err(dev, "s/g data length zero error\n");
522 if (v_lo & TALITOS_CCPSR_LO_FPZ)
523 dev_err(dev, "fetch pointer zero error\n");
524 if (v_lo & TALITOS_CCPSR_LO_IDH)
525 dev_err(dev, "illegal descriptor header error\n");
526 if (v_lo & TALITOS_CCPSR_LO_IEU)
527 dev_err(dev, "invalid execution unit error\n");
528 if (v_lo & TALITOS_CCPSR_LO_EU)
Kim Phillips3e721ae2011-10-21 15:20:28 +0200529 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
Kim Phillips9c4a7962008-06-23 19:50:15 +0800530 if (v_lo & TALITOS_CCPSR_LO_GB)
531 dev_err(dev, "gather boundary error\n");
532 if (v_lo & TALITOS_CCPSR_LO_GRL)
533 dev_err(dev, "gather return/length error\n");
534 if (v_lo & TALITOS_CCPSR_LO_SB)
535 dev_err(dev, "scatter boundary error\n");
536 if (v_lo & TALITOS_CCPSR_LO_SRL)
537 dev_err(dev, "scatter return/length error\n");
538
539 flush_channel(dev, ch, error, reset_ch);
540
541 if (reset_ch) {
542 reset_channel(dev, ch);
543 } else {
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800544 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800545 TALITOS_CCCR_CONT);
Kim Phillipsad42d5f2011-11-21 16:13:27 +0800546 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
547 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800548 TALITOS_CCCR_CONT) && --timeout)
549 cpu_relax();
550 if (timeout == 0) {
551 dev_err(dev, "failed to restart channel %d\n",
552 ch);
553 reset_dev = 1;
554 }
555 }
556 }
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800557 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800558 dev_err(dev, "done overflow, internal time out, or rngu error: "
559 "ISR 0x%08x_%08x\n", isr, isr_lo);
560
561 /* purge request queues */
562 for (ch = 0; ch < priv->num_channels; ch++)
563 flush_channel(dev, ch, -EIO, 1);
564
565 /* reset and reinitialize the device */
566 init_device(dev);
567 }
568}
569
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800570#define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
571static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
572{ \
573 struct device *dev = data; \
574 struct talitos_private *priv = dev_get_drvdata(dev); \
575 u32 isr, isr_lo; \
Horia Geanta511d63c2012-03-30 17:49:53 +0300576 unsigned long flags; \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800577 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300578 spin_lock_irqsave(&priv->reg_lock, flags); \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800579 isr = in_be32(priv->reg + TALITOS_ISR); \
580 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
581 /* Acknowledge interrupt */ \
582 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
583 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
584 \
Horia Geanta511d63c2012-03-30 17:49:53 +0300585 if (unlikely(isr & ch_err_mask || isr_lo)) { \
586 spin_unlock_irqrestore(&priv->reg_lock, flags); \
587 talitos_error(dev, isr & ch_err_mask, isr_lo); \
588 } \
589 else { \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800590 if (likely(isr & ch_done_mask)) { \
591 /* mask further done interrupts. */ \
592 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
593 /* done_task will unmask done interrupts at exit */ \
594 tasklet_schedule(&priv->done_task[tlet]); \
595 } \
Horia Geanta511d63c2012-03-30 17:49:53 +0300596 spin_unlock_irqrestore(&priv->reg_lock, flags); \
597 } \
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800598 \
599 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
600 IRQ_NONE; \
Kim Phillips9c4a7962008-06-23 19:50:15 +0800601}
Kim Phillipsc3e337f2011-11-21 16:13:27 +0800602DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
603DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
604DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800605
606/*
607 * hwrng
608 */
609static int talitos_rng_data_present(struct hwrng *rng, int wait)
610{
611 struct device *dev = (struct device *)rng->priv;
612 struct talitos_private *priv = dev_get_drvdata(dev);
613 u32 ofl;
614 int i;
615
616 for (i = 0; i < 20; i++) {
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200617 ofl = in_be32(priv->reg_rngu + TALITOS_EUSR_LO) &
Kim Phillips9c4a7962008-06-23 19:50:15 +0800618 TALITOS_RNGUSR_LO_OFL;
619 if (ofl || !wait)
620 break;
621 udelay(10);
622 }
623
624 return !!ofl;
625}
626
627static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
628{
629 struct device *dev = (struct device *)rng->priv;
630 struct talitos_private *priv = dev_get_drvdata(dev);
631
632 /* rng fifo requires 64-bit accesses */
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200633 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO);
634 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO_LO);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800635
636 return sizeof(u32);
637}
638
639static int talitos_rng_init(struct hwrng *rng)
640{
641 struct device *dev = (struct device *)rng->priv;
642 struct talitos_private *priv = dev_get_drvdata(dev);
643 unsigned int timeout = TALITOS_TIMEOUT;
644
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200645 setbits32(priv->reg_rngu + TALITOS_EURCR_LO, TALITOS_RNGURCR_LO_SR);
646 while (!(in_be32(priv->reg_rngu + TALITOS_EUSR_LO)
647 & TALITOS_RNGUSR_LO_RD)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800648 && --timeout)
649 cpu_relax();
650 if (timeout == 0) {
651 dev_err(dev, "failed to reset rng hw\n");
652 return -ENODEV;
653 }
654
655 /* start generating */
LEROY Christophe5fa7fa12015-04-17 16:32:11 +0200656 setbits32(priv->reg_rngu + TALITOS_EUDSR_LO, 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800657
658 return 0;
659}
660
661static int talitos_register_rng(struct device *dev)
662{
663 struct talitos_private *priv = dev_get_drvdata(dev);
664
665 priv->rng.name = dev_driver_string(dev),
666 priv->rng.init = talitos_rng_init,
667 priv->rng.data_present = talitos_rng_data_present,
668 priv->rng.data_read = talitos_rng_data_read,
669 priv->rng.priv = (unsigned long)dev;
670
671 return hwrng_register(&priv->rng);
672}
673
674static void talitos_unregister_rng(struct device *dev)
675{
676 struct talitos_private *priv = dev_get_drvdata(dev);
677
678 hwrng_unregister(&priv->rng);
679}
680
681/*
682 * crypto alg
683 */
684#define TALITOS_CRA_PRIORITY 3000
Horia Geanta357fb602012-07-03 19:16:53 +0300685#define TALITOS_MAX_KEY_SIZE 96
Lee Nipper3952f172008-07-10 18:29:18 +0800686#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800687
Kim Phillips9c4a7962008-06-23 19:50:15 +0800688struct talitos_ctx {
689 struct device *dev;
Kim Phillips5228f0f2011-07-15 11:21:38 +0800690 int ch;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800691 __be32 desc_hdr_template;
692 u8 key[TALITOS_MAX_KEY_SIZE];
Lee Nipper70bcaca2008-07-03 19:08:46 +0800693 u8 iv[TALITOS_MAX_IV_LENGTH];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800694 unsigned int keylen;
695 unsigned int enckeylen;
696 unsigned int authkeylen;
697 unsigned int authsize;
698};
699
Lee Nipper497f2e62010-05-19 19:20:36 +1000700#define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
701#define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
702
703struct talitos_ahash_req_ctx {
Kim Phillips60f208d2010-05-19 19:21:53 +1000704 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
Lee Nipper497f2e62010-05-19 19:20:36 +1000705 unsigned int hw_context_size;
706 u8 buf[HASH_MAX_BLOCK_SIZE];
707 u8 bufnext[HASH_MAX_BLOCK_SIZE];
Kim Phillips60f208d2010-05-19 19:21:53 +1000708 unsigned int swinit;
Lee Nipper497f2e62010-05-19 19:20:36 +1000709 unsigned int first;
710 unsigned int last;
711 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +1000712 u64 nbuf;
Lee Nipper497f2e62010-05-19 19:20:36 +1000713 struct scatterlist bufsl[2];
714 struct scatterlist *psrc;
715};
716
Lee Nipper56af8cd2009-03-29 15:50:50 +0800717static int aead_setauthsize(struct crypto_aead *authenc,
718 unsigned int authsize)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800719{
720 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
721
722 ctx->authsize = authsize;
723
724 return 0;
725}
726
Lee Nipper56af8cd2009-03-29 15:50:50 +0800727static int aead_setkey(struct crypto_aead *authenc,
728 const u8 *key, unsigned int keylen)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800729{
730 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Mathias Krausec306a982013-10-15 13:49:34 +0200731 struct crypto_authenc_keys keys;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800732
Mathias Krausec306a982013-10-15 13:49:34 +0200733 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800734 goto badkey;
735
Mathias Krausec306a982013-10-15 13:49:34 +0200736 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800737 goto badkey;
738
Mathias Krausec306a982013-10-15 13:49:34 +0200739 memcpy(ctx->key, keys.authkey, keys.authkeylen);
740 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800741
Mathias Krausec306a982013-10-15 13:49:34 +0200742 ctx->keylen = keys.authkeylen + keys.enckeylen;
743 ctx->enckeylen = keys.enckeylen;
744 ctx->authkeylen = keys.authkeylen;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800745
746 return 0;
747
748badkey:
749 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
750 return -EINVAL;
751}
752
753/*
Lee Nipper56af8cd2009-03-29 15:50:50 +0800754 * talitos_edesc - s/w-extended descriptor
Horia Geanta79fd31d2012-08-02 17:16:40 +0300755 * @assoc_nents: number of segments in associated data scatterlist
Kim Phillips9c4a7962008-06-23 19:50:15 +0800756 * @src_nents: number of segments in input scatterlist
757 * @dst_nents: number of segments in output scatterlist
Horia Geanta79fd31d2012-08-02 17:16:40 +0300758 * @assoc_chained: whether assoc is chained or not
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300759 * @src_chained: whether src is chained or not
760 * @dst_chained: whether dst is chained or not
Horia Geanta79fd31d2012-08-02 17:16:40 +0300761 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips9c4a7962008-06-23 19:50:15 +0800762 * @dma_len: length of dma mapped link_tbl space
763 * @dma_link_tbl: bus physical address of link_tbl
764 * @desc: h/w descriptor
765 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
766 *
767 * if decrypting (with authcheck), or either one of src_nents or dst_nents
768 * is greater than 1, an integrity check value is concatenated to the end
769 * of link_tbl data
770 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800771struct talitos_edesc {
Horia Geanta79fd31d2012-08-02 17:16:40 +0300772 int assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800773 int src_nents;
774 int dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300775 bool assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300776 bool src_chained;
777 bool dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +0300778 dma_addr_t iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800779 int dma_len;
780 dma_addr_t dma_link_tbl;
781 struct talitos_desc desc;
782 struct talitos_ptr link_tbl[0];
783};
784
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800785static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
786 unsigned int nents, enum dma_data_direction dir,
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300787 bool chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800788{
789 if (unlikely(chained))
790 while (sg) {
791 dma_map_sg(dev, sg, 1, dir);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200792 sg = sg_next(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800793 }
794 else
795 dma_map_sg(dev, sg, nents, dir);
796 return nents;
797}
798
799static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
800 enum dma_data_direction dir)
801{
802 while (sg) {
803 dma_unmap_sg(dev, sg, 1, dir);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200804 sg = sg_next(sg);
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800805 }
806}
807
808static void talitos_sg_unmap(struct device *dev,
809 struct talitos_edesc *edesc,
810 struct scatterlist *src,
811 struct scatterlist *dst)
812{
813 unsigned int src_nents = edesc->src_nents ? : 1;
814 unsigned int dst_nents = edesc->dst_nents ? : 1;
815
816 if (src != dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300817 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800818 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
819 else
820 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
821
Lee Nipper497f2e62010-05-19 19:20:36 +1000822 if (dst) {
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300823 if (edesc->dst_chained)
Lee Nipper497f2e62010-05-19 19:20:36 +1000824 talitos_unmap_sg_chain(dev, dst,
825 DMA_FROM_DEVICE);
826 else
827 dma_unmap_sg(dev, dst, dst_nents,
828 DMA_FROM_DEVICE);
829 }
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800830 } else
Horia Geanta2a1cfe42012-08-02 17:16:39 +0300831 if (edesc->src_chained)
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800832 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
833 else
834 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
835}
836
Kim Phillips9c4a7962008-06-23 19:50:15 +0800837static void ipsec_esp_unmap(struct device *dev,
Lee Nipper56af8cd2009-03-29 15:50:50 +0800838 struct talitos_edesc *edesc,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800839 struct aead_request *areq)
840{
841 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
842 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
843 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
844 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
845
Horia Geanta79fd31d2012-08-02 17:16:40 +0300846 if (edesc->assoc_chained)
847 talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
Horia Geanta935e99a2013-11-19 14:57:49 +0200848 else if (areq->assoclen)
Horia Geanta79fd31d2012-08-02 17:16:40 +0300849 /* assoc_nents counts also for IV in non-contiguous cases */
850 dma_unmap_sg(dev, areq->assoc,
851 edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
852 DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800853
Lee Nipper4de9d0b2009-03-29 15:52:32 +0800854 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800855
856 if (edesc->dma_len)
857 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
858 DMA_BIDIRECTIONAL);
859}
860
861/*
862 * ipsec_esp descriptor callbacks
863 */
864static void ipsec_esp_encrypt_done(struct device *dev,
865 struct talitos_desc *desc, void *context,
866 int err)
867{
868 struct aead_request *areq = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800869 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
870 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800871 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800872 struct scatterlist *sg;
873 void *icvdata;
874
Kim Phillips19bbbc62009-03-29 15:53:59 +0800875 edesc = container_of(desc, struct talitos_edesc, desc);
876
Kim Phillips9c4a7962008-06-23 19:50:15 +0800877 ipsec_esp_unmap(dev, edesc, areq);
878
879 /* copy the generated ICV to dst */
Horia Geanta60542502012-08-02 17:16:37 +0300880 if (edesc->dst_nents) {
Kim Phillips9c4a7962008-06-23 19:50:15 +0800881 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300882 edesc->dst_nents + 2 +
883 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800884 sg = sg_last(areq->dst, edesc->dst_nents);
885 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
886 icvdata, ctx->authsize);
887 }
888
889 kfree(edesc);
890
891 aead_request_complete(areq, err);
892}
893
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800894static void ipsec_esp_decrypt_swauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800895 struct talitos_desc *desc,
896 void *context, int err)
Kim Phillips9c4a7962008-06-23 19:50:15 +0800897{
898 struct aead_request *req = context;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800899 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
900 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Kim Phillips19bbbc62009-03-29 15:53:59 +0800901 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800902 struct scatterlist *sg;
903 void *icvdata;
904
Kim Phillips19bbbc62009-03-29 15:53:59 +0800905 edesc = container_of(desc, struct talitos_edesc, desc);
906
Kim Phillips9c4a7962008-06-23 19:50:15 +0800907 ipsec_esp_unmap(dev, edesc, req);
908
909 if (!err) {
910 /* auth check */
911 if (edesc->dma_len)
912 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +0300913 edesc->dst_nents + 2 +
914 edesc->assoc_nents];
Kim Phillips9c4a7962008-06-23 19:50:15 +0800915 else
916 icvdata = &edesc->link_tbl[0];
917
918 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
919 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
920 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
921 }
922
923 kfree(edesc);
924
925 aead_request_complete(req, err);
926}
927
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800928static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
Kim Phillipse938e462009-03-29 15:53:23 +0800929 struct talitos_desc *desc,
930 void *context, int err)
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800931{
932 struct aead_request *req = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +0800933 struct talitos_edesc *edesc;
934
935 edesc = container_of(desc, struct talitos_edesc, desc);
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800936
937 ipsec_esp_unmap(dev, edesc, req);
938
939 /* check ICV auth status */
Kim Phillipse938e462009-03-29 15:53:23 +0800940 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
941 DESC_HDR_LO_ICCR1_PASS))
942 err = -EBADMSG;
Kim Phillipsfe5720e2008-10-12 20:33:14 +0800943
944 kfree(edesc);
945
946 aead_request_complete(req, err);
947}
948
Kim Phillips9c4a7962008-06-23 19:50:15 +0800949/*
950 * convert scatterlist to SEC h/w link table format
951 * stop at cryptlen bytes
952 */
Lee Nipper70bcaca2008-07-03 19:08:46 +0800953static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
Kim Phillips9c4a7962008-06-23 19:50:15 +0800954 int cryptlen, struct talitos_ptr *link_tbl_ptr)
955{
Lee Nipper70bcaca2008-07-03 19:08:46 +0800956 int n_sg = sg_count;
957
958 while (n_sg--) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +0200959 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg), 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800960 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
961 link_tbl_ptr->j_extent = 0;
962 link_tbl_ptr++;
963 cryptlen -= sg_dma_len(sg);
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200964 sg = sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800965 }
966
Lee Nipper70bcaca2008-07-03 19:08:46 +0800967 /* adjust (decrease) last one (or two) entry's len to cryptlen */
Kim Phillips9c4a7962008-06-23 19:50:15 +0800968 link_tbl_ptr--;
Kim Phillipsc0e741d2008-07-17 20:20:59 +0800969 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
Lee Nipper70bcaca2008-07-03 19:08:46 +0800970 /* Empty this entry, and move to previous one */
971 cryptlen += be16_to_cpu(link_tbl_ptr->len);
972 link_tbl_ptr->len = 0;
973 sg_count--;
974 link_tbl_ptr--;
975 }
Wei Yongjun7291a932012-09-28 12:52:25 +0800976 be16_add_cpu(&link_tbl_ptr->len, cryptlen);
Kim Phillips9c4a7962008-06-23 19:50:15 +0800977
978 /* tag end of link table */
979 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
Lee Nipper70bcaca2008-07-03 19:08:46 +0800980
981 return sg_count;
Kim Phillips9c4a7962008-06-23 19:50:15 +0800982}
983
984/*
985 * fill in and submit ipsec_esp descriptor
986 */
Lee Nipper56af8cd2009-03-29 15:50:50 +0800987static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
Horia Geanta79fd31d2012-08-02 17:16:40 +0300988 u64 seq, void (*callback) (struct device *dev,
989 struct talitos_desc *desc,
990 void *context, int error))
Kim Phillips9c4a7962008-06-23 19:50:15 +0800991{
992 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
993 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
994 struct device *dev = ctx->dev;
995 struct talitos_desc *desc = &edesc->desc;
996 unsigned int cryptlen = areq->cryptlen;
997 unsigned int authsize = ctx->authsize;
Kim Phillipse41256f2009-08-13 11:49:06 +1000998 unsigned int ivsize = crypto_aead_ivsize(aead);
Kim Phillipsfa86a262008-07-17 20:20:06 +0800999 int sg_count, ret;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001000 int sg_link_tbl_len;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001001
1002 /* hmac key */
1003 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001004 DMA_TO_DEVICE);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001005
Kim Phillips9c4a7962008-06-23 19:50:15 +08001006 /* hmac data */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001007 desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
1008 if (edesc->assoc_nents) {
1009 int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
1010 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
1011
1012 to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001013 sizeof(struct talitos_ptr), 0);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001014 desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
1015
1016 /* assoc_nents - 1 entries for assoc, 1 for IV */
1017 sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
1018 areq->assoclen, tbl_ptr);
1019
1020 /* add IV to link table */
1021 tbl_ptr += sg_count - 1;
1022 tbl_ptr->j_extent = 0;
1023 tbl_ptr++;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001024 to_talitos_ptr(tbl_ptr, edesc->iv_dma, 0);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001025 tbl_ptr->len = cpu_to_be16(ivsize);
1026 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1027
1028 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1029 edesc->dma_len, DMA_BIDIRECTIONAL);
1030 } else {
Horia Geanta935e99a2013-11-19 14:57:49 +02001031 if (areq->assoclen)
1032 to_talitos_ptr(&desc->ptr[1],
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001033 sg_dma_address(areq->assoc), 0);
Horia Geanta935e99a2013-11-19 14:57:49 +02001034 else
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001035 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma, 0);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001036 desc->ptr[1].j_extent = 0;
1037 }
1038
Kim Phillips9c4a7962008-06-23 19:50:15 +08001039 /* cipher iv */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001040 to_talitos_ptr(&desc->ptr[2], edesc->iv_dma, 0);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001041 desc->ptr[2].len = cpu_to_be16(ivsize);
1042 desc->ptr[2].j_extent = 0;
1043 /* Sync needed for the aead_givencrypt case */
1044 dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001045
1046 /* cipher key */
1047 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001048 (char *)&ctx->key + ctx->authkeylen,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001049 DMA_TO_DEVICE);
1050
1051 /*
1052 * cipher in
1053 * map and adjust cipher len to aead request cryptlen.
1054 * extent is bytes of HMAC postpended to ciphertext,
1055 * typically 12 for ipsec
1056 */
1057 desc->ptr[4].len = cpu_to_be16(cryptlen);
1058 desc->ptr[4].j_extent = authsize;
1059
Kim Phillipse938e462009-03-29 15:53:23 +08001060 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1061 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1062 : DMA_TO_DEVICE,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001063 edesc->src_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001064
1065 if (sg_count == 1) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001066 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src), 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001067 } else {
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001068 sg_link_tbl_len = cryptlen;
1069
Kim Phillips962a9c92009-03-29 15:54:30 +08001070 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001071 sg_link_tbl_len = cryptlen + authsize;
Kim Phillipse938e462009-03-29 15:53:23 +08001072
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001073 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001074 &edesc->link_tbl[0]);
1075 if (sg_count > 1) {
1076 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001077 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl, 0);
Kim Phillipse938e462009-03-29 15:53:23 +08001078 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1079 edesc->dma_len,
1080 DMA_BIDIRECTIONAL);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001081 } else {
1082 /* Only one segment now, so no link tbl needed */
Kim Phillips81eb0242009-08-13 11:51:51 +10001083 to_talitos_ptr(&desc->ptr[4],
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001084 sg_dma_address(areq->src), 0);
Lee Nipper70bcaca2008-07-03 19:08:46 +08001085 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08001086 }
1087
1088 /* cipher out */
1089 desc->ptr[5].len = cpu_to_be16(cryptlen);
1090 desc->ptr[5].j_extent = authsize;
1091
Kim Phillipse938e462009-03-29 15:53:23 +08001092 if (areq->src != areq->dst)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001093 sg_count = talitos_map_sg(dev, areq->dst,
1094 edesc->dst_nents ? : 1,
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001095 DMA_FROM_DEVICE, edesc->dst_chained);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001096
1097 if (sg_count == 1) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001098 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst), 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001099 } else {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001100 int tbl_off = edesc->src_nents + 1;
1101 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
Kim Phillips9c4a7962008-06-23 19:50:15 +08001102
Kim Phillips81eb0242009-08-13 11:51:51 +10001103 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001104 tbl_off * sizeof(struct talitos_ptr), 0);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001105 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001106 tbl_ptr);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001107
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001108 /* Add an entry to the link table for ICV data */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001109 tbl_ptr += sg_count - 1;
1110 tbl_ptr->j_extent = 0;
1111 tbl_ptr++;
1112 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1113 tbl_ptr->len = cpu_to_be16(authsize);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001114
1115 /* icv data follows link tables */
Horia Geanta79fd31d2012-08-02 17:16:40 +03001116 to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1117 (tbl_off + edesc->dst_nents + 1 +
1118 edesc->assoc_nents) *
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001119 sizeof(struct talitos_ptr), 0);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001120 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1121 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1122 edesc->dma_len, DMA_BIDIRECTIONAL);
1123 }
1124
1125 /* iv out */
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001126 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv,
Kim Phillips9c4a7962008-06-23 19:50:15 +08001127 DMA_FROM_DEVICE);
1128
Kim Phillips5228f0f2011-07-15 11:21:38 +08001129 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Kim Phillipsfa86a262008-07-17 20:20:06 +08001130 if (ret != -EINPROGRESS) {
1131 ipsec_esp_unmap(dev, edesc, areq);
1132 kfree(edesc);
1133 }
1134 return ret;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001135}
1136
Kim Phillips9c4a7962008-06-23 19:50:15 +08001137/*
1138 * derive number of elements in scatterlist
1139 */
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001140static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001141{
1142 struct scatterlist *sg = sg_list;
1143 int sg_nents = 0;
1144
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001145 *chained = false;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001146 while (nbytes > 0) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001147 sg_nents++;
1148 nbytes -= sg->length;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001149 if (!sg_is_last(sg) && (sg + 1)->length == 0)
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001150 *chained = true;
Cristian Stoica5be4d4c2015-01-20 10:06:16 +02001151 sg = sg_next(sg);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001152 }
1153
1154 return sg_nents;
1155}
1156
1157/*
Lee Nipper56af8cd2009-03-29 15:50:50 +08001158 * allocate and map the extended descriptor
Kim Phillips9c4a7962008-06-23 19:50:15 +08001159 */
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001160static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001161 struct scatterlist *assoc,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001162 struct scatterlist *src,
1163 struct scatterlist *dst,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001164 u8 *iv,
1165 unsigned int assoclen,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001166 unsigned int cryptlen,
1167 unsigned int authsize,
Horia Geanta79fd31d2012-08-02 17:16:40 +03001168 unsigned int ivsize,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001169 int icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001170 u32 cryptoflags,
1171 bool encrypt)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001172{
Lee Nipper56af8cd2009-03-29 15:50:50 +08001173 struct talitos_edesc *edesc;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001174 int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1175 bool assoc_chained = false, src_chained = false, dst_chained = false;
1176 dma_addr_t iv_dma = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001177 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
Kim Phillips586725f2008-07-17 20:19:18 +08001178 GFP_ATOMIC;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001179
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001180 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1181 dev_err(dev, "length exceeds h/w max limit\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001182 return ERR_PTR(-EINVAL);
1183 }
1184
Horia Geanta935e99a2013-11-19 14:57:49 +02001185 if (ivsize)
Horia Geanta79fd31d2012-08-02 17:16:40 +03001186 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1187
Horia Geanta935e99a2013-11-19 14:57:49 +02001188 if (assoclen) {
Horia Geanta79fd31d2012-08-02 17:16:40 +03001189 /*
1190 * Currently it is assumed that iv is provided whenever assoc
1191 * is.
1192 */
1193 BUG_ON(!iv);
1194
1195 assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1196 talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1197 assoc_chained);
1198 assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1199
1200 if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1201 assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1202 }
1203
Horia Geanta62293a32013-11-28 15:11:17 +02001204 if (!dst || dst == src) {
1205 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
1206 src_nents = (src_nents == 1) ? 0 : src_nents;
1207 dst_nents = dst ? src_nents : 0;
1208 } else { /* dst && dst != src*/
1209 src_nents = sg_count(src, cryptlen + (encrypt ? 0 : authsize),
1210 &src_chained);
1211 src_nents = (src_nents == 1) ? 0 : src_nents;
1212 dst_nents = sg_count(dst, cryptlen + (encrypt ? authsize : 0),
1213 &dst_chained);
1214 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001215 }
1216
1217 /*
1218 * allocate space for base edesc plus the link tables,
Lee Nipperf3c85bc2008-07-30 16:26:57 +08001219 * allowing for two separate entries for ICV and generated ICV (+ 2),
Kim Phillips9c4a7962008-06-23 19:50:15 +08001220 * and the ICV data itself
1221 */
Lee Nipper56af8cd2009-03-29 15:50:50 +08001222 alloc_len = sizeof(struct talitos_edesc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001223 if (assoc_nents || src_nents || dst_nents) {
1224 dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
1225 sizeof(struct talitos_ptr) + authsize;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001226 alloc_len += dma_len;
1227 } else {
1228 dma_len = 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001229 alloc_len += icv_stashing ? authsize : 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001230 }
1231
Kim Phillips586725f2008-07-17 20:19:18 +08001232 edesc = kmalloc(alloc_len, GFP_DMA | flags);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001233 if (!edesc) {
Horia Geanta935e99a2013-11-19 14:57:49 +02001234 if (assoc_chained)
1235 talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1236 else if (assoclen)
1237 dma_unmap_sg(dev, assoc,
1238 assoc_nents ? assoc_nents - 1 : 1,
1239 DMA_TO_DEVICE);
1240
Horia Geanta79fd31d2012-08-02 17:16:40 +03001241 if (iv_dma)
1242 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Horia Geanta935e99a2013-11-19 14:57:49 +02001243
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001244 dev_err(dev, "could not allocate edescriptor\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08001245 return ERR_PTR(-ENOMEM);
1246 }
1247
Horia Geanta79fd31d2012-08-02 17:16:40 +03001248 edesc->assoc_nents = assoc_nents;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001249 edesc->src_nents = src_nents;
1250 edesc->dst_nents = dst_nents;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001251 edesc->assoc_chained = assoc_chained;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001252 edesc->src_chained = src_chained;
1253 edesc->dst_chained = dst_chained;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001254 edesc->iv_dma = iv_dma;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001255 edesc->dma_len = dma_len;
Lee Nipper497f2e62010-05-19 19:20:36 +10001256 if (dma_len)
1257 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1258 edesc->dma_len,
1259 DMA_BIDIRECTIONAL);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001260
1261 return edesc;
1262}
1263
Horia Geanta79fd31d2012-08-02 17:16:40 +03001264static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
Horia Geanta62293a32013-11-28 15:11:17 +02001265 int icv_stashing, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001266{
1267 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1268 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001269 unsigned int ivsize = crypto_aead_ivsize(authenc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001270
Horia Geanta79fd31d2012-08-02 17:16:40 +03001271 return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1272 iv, areq->assoclen, areq->cryptlen,
1273 ctx->authsize, ivsize, icv_stashing,
Horia Geanta62293a32013-11-28 15:11:17 +02001274 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001275}
1276
Lee Nipper56af8cd2009-03-29 15:50:50 +08001277static int aead_encrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001278{
1279 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1280 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001281 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001282
1283 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001284 edesc = aead_edesc_alloc(req, req->iv, 0, true);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001285 if (IS_ERR(edesc))
1286 return PTR_ERR(edesc);
1287
1288 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001289 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001290
Horia Geanta79fd31d2012-08-02 17:16:40 +03001291 return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001292}
1293
Lee Nipper56af8cd2009-03-29 15:50:50 +08001294static int aead_decrypt(struct aead_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001295{
1296 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1297 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1298 unsigned int authsize = ctx->authsize;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001299 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001300 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001301 struct scatterlist *sg;
1302 void *icvdata;
1303
1304 req->cryptlen -= authsize;
1305
1306 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001307 edesc = aead_edesc_alloc(req, req->iv, 1, false);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001308 if (IS_ERR(edesc))
1309 return PTR_ERR(edesc);
1310
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001311 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
Kim Phillipse938e462009-03-29 15:53:23 +08001312 ((!edesc->src_nents && !edesc->dst_nents) ||
1313 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08001314
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001315 /* decrypt and check the ICV */
Kim Phillipse938e462009-03-29 15:53:23 +08001316 edesc->desc.hdr = ctx->desc_hdr_template |
1317 DESC_HDR_DIR_INBOUND |
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001318 DESC_HDR_MODE1_MDEU_CICV;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001319
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001320 /* reset integrity check result bits */
1321 edesc->desc.hdr_lo = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001322
Horia Geanta79fd31d2012-08-02 17:16:40 +03001323 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
Kim Phillipsfe5720e2008-10-12 20:33:14 +08001324 }
Kim Phillipse938e462009-03-29 15:53:23 +08001325
1326 /* Have to check the ICV with software */
1327 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1328
1329 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1330 if (edesc->dma_len)
1331 icvdata = &edesc->link_tbl[edesc->src_nents +
Horia Geanta79fd31d2012-08-02 17:16:40 +03001332 edesc->dst_nents + 2 +
1333 edesc->assoc_nents];
Kim Phillipse938e462009-03-29 15:53:23 +08001334 else
1335 icvdata = &edesc->link_tbl[0];
1336
1337 sg = sg_last(req->src, edesc->src_nents ? : 1);
1338
1339 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1340 ctx->authsize);
1341
Horia Geanta79fd31d2012-08-02 17:16:40 +03001342 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001343}
1344
Lee Nipper56af8cd2009-03-29 15:50:50 +08001345static int aead_givencrypt(struct aead_givcrypt_request *req)
Kim Phillips9c4a7962008-06-23 19:50:15 +08001346{
1347 struct aead_request *areq = &req->areq;
1348 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1349 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
Lee Nipper56af8cd2009-03-29 15:50:50 +08001350 struct talitos_edesc *edesc;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001351
1352 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001353 edesc = aead_edesc_alloc(areq, req->giv, 0, true);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001354 if (IS_ERR(edesc))
1355 return PTR_ERR(edesc);
1356
1357 /* set encrypt */
Lee Nipper70bcaca2008-07-03 19:08:46 +08001358 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001359
1360 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
Kim Phillipsba954872008-09-14 13:41:19 -07001361 /* avoid consecutive packets going out with same IV */
1362 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001363
Horia Geanta79fd31d2012-08-02 17:16:40 +03001364 return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
Kim Phillips9c4a7962008-06-23 19:50:15 +08001365}
1366
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001367static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1368 const u8 *key, unsigned int keylen)
1369{
1370 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001371
1372 memcpy(&ctx->key, key, keylen);
1373 ctx->keylen = keylen;
1374
1375 return 0;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001376}
1377
LEROY Christophe032d1972015-04-17 16:31:51 +02001378static void unmap_sg_talitos_ptr(struct device *dev, struct scatterlist *src,
1379 struct scatterlist *dst, unsigned int len,
1380 struct talitos_edesc *edesc)
1381{
1382 talitos_sg_unmap(dev, edesc, src, dst);
1383}
1384
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001385static void common_nonsnoop_unmap(struct device *dev,
1386 struct talitos_edesc *edesc,
1387 struct ablkcipher_request *areq)
1388{
1389 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
LEROY Christophe032d1972015-04-17 16:31:51 +02001390
1391 unmap_sg_talitos_ptr(dev, areq->src, areq->dst, areq->nbytes, edesc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001392 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1393 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1394
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001395 if (edesc->dma_len)
1396 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1397 DMA_BIDIRECTIONAL);
1398}
1399
1400static void ablkcipher_done(struct device *dev,
1401 struct talitos_desc *desc, void *context,
1402 int err)
1403{
1404 struct ablkcipher_request *areq = context;
Kim Phillips19bbbc62009-03-29 15:53:59 +08001405 struct talitos_edesc *edesc;
1406
1407 edesc = container_of(desc, struct talitos_edesc, desc);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001408
1409 common_nonsnoop_unmap(dev, edesc, areq);
1410
1411 kfree(edesc);
1412
1413 areq->base.complete(&areq->base, err);
1414}
1415
LEROY Christophe032d1972015-04-17 16:31:51 +02001416int map_sg_in_talitos_ptr(struct device *dev, struct scatterlist *src,
1417 unsigned int len, struct talitos_edesc *edesc,
1418 enum dma_data_direction dir, struct talitos_ptr *ptr)
1419{
1420 int sg_count;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001421 struct talitos_private *priv = dev_get_drvdata(dev);
1422 bool is_sec1 = has_ftr_sec1(priv);
LEROY Christophe032d1972015-04-17 16:31:51 +02001423
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001424 to_talitos_ptr_len(ptr, len, is_sec1);
1425 to_talitos_ptr_extent_clear(ptr, is_sec1);
LEROY Christophe032d1972015-04-17 16:31:51 +02001426
1427 sg_count = talitos_map_sg(dev, src, edesc->src_nents ? : 1, dir,
1428 edesc->src_chained);
1429
1430 if (sg_count == 1) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001431 to_talitos_ptr(ptr, sg_dma_address(src), is_sec1);
LEROY Christophe032d1972015-04-17 16:31:51 +02001432 } else {
1433 sg_count = sg_to_link_tbl(src, sg_count, len,
1434 &edesc->link_tbl[0]);
1435 if (sg_count > 1) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001436 to_talitos_ptr(ptr, edesc->dma_link_tbl, 0);
LEROY Christophe032d1972015-04-17 16:31:51 +02001437 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1438 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1439 edesc->dma_len,
1440 DMA_BIDIRECTIONAL);
1441 } else {
1442 /* Only one segment now, so no link tbl needed */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001443 to_talitos_ptr(ptr, sg_dma_address(src), is_sec1);
LEROY Christophe032d1972015-04-17 16:31:51 +02001444 }
1445 }
1446 return sg_count;
1447}
1448
1449void map_sg_out_talitos_ptr(struct device *dev, struct scatterlist *dst,
1450 unsigned int len, struct talitos_edesc *edesc,
1451 enum dma_data_direction dir,
1452 struct talitos_ptr *ptr, int sg_count)
1453{
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001454 struct talitos_private *priv = dev_get_drvdata(dev);
1455 bool is_sec1 = has_ftr_sec1(priv);
1456
1457 to_talitos_ptr_len(ptr, len, is_sec1);
1458 to_talitos_ptr_extent_clear(ptr, is_sec1);
LEROY Christophe032d1972015-04-17 16:31:51 +02001459
1460 if (dir != DMA_NONE)
1461 sg_count = talitos_map_sg(dev, dst, edesc->dst_nents ? : 1,
1462 dir, edesc->dst_chained);
1463
1464 if (sg_count == 1) {
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001465 to_talitos_ptr(ptr, sg_dma_address(dst), is_sec1);
LEROY Christophe032d1972015-04-17 16:31:51 +02001466 } else {
1467 struct talitos_ptr *link_tbl_ptr =
1468 &edesc->link_tbl[edesc->src_nents + 1];
1469
1470 to_talitos_ptr(ptr, edesc->dma_link_tbl +
1471 (edesc->src_nents + 1) *
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001472 sizeof(struct talitos_ptr), 0);
LEROY Christophe032d1972015-04-17 16:31:51 +02001473 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1474 sg_count = sg_to_link_tbl(dst, sg_count, len, link_tbl_ptr);
1475 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1476 edesc->dma_len, DMA_BIDIRECTIONAL);
1477 }
1478}
1479
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001480static int common_nonsnoop(struct talitos_edesc *edesc,
1481 struct ablkcipher_request *areq,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001482 void (*callback) (struct device *dev,
1483 struct talitos_desc *desc,
1484 void *context, int error))
1485{
1486 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1487 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1488 struct device *dev = ctx->dev;
1489 struct talitos_desc *desc = &edesc->desc;
1490 unsigned int cryptlen = areq->nbytes;
Horia Geanta79fd31d2012-08-02 17:16:40 +03001491 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001492 int sg_count, ret;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001493 struct talitos_private *priv = dev_get_drvdata(dev);
1494 bool is_sec1 = has_ftr_sec1(priv);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001495
1496 /* first DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001497 desc->ptr[0] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001498
1499 /* cipher iv */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001500 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma, is_sec1);
1501 to_talitos_ptr_len(&desc->ptr[1], ivsize, is_sec1);
1502 to_talitos_ptr_extent_clear(&desc->ptr[1], is_sec1);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001503
1504 /* cipher key */
1505 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001506 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001507
1508 /*
1509 * cipher in
1510 */
LEROY Christophe032d1972015-04-17 16:31:51 +02001511 sg_count = map_sg_in_talitos_ptr(dev, areq->src, cryptlen, edesc,
1512 (areq->src == areq->dst) ?
1513 DMA_BIDIRECTIONAL : DMA_TO_DEVICE,
1514 &desc->ptr[3]);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001515
1516 /* cipher out */
LEROY Christophe032d1972015-04-17 16:31:51 +02001517 map_sg_out_talitos_ptr(dev, areq->dst, cryptlen, edesc,
1518 (areq->src == areq->dst) ? DMA_NONE
1519 : DMA_FROM_DEVICE,
1520 &desc->ptr[4], sg_count);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001521
1522 /* iv out */
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001523 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001524 DMA_FROM_DEVICE);
1525
1526 /* last DWORD empty */
LEROY Christophe2529bc32015-04-17 16:31:49 +02001527 desc->ptr[6] = zero_entry;
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001528
Kim Phillips5228f0f2011-07-15 11:21:38 +08001529 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001530 if (ret != -EINPROGRESS) {
1531 common_nonsnoop_unmap(dev, edesc, areq);
1532 kfree(edesc);
1533 }
1534 return ret;
1535}
1536
Kim Phillipse938e462009-03-29 15:53:23 +08001537static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
Horia Geanta62293a32013-11-28 15:11:17 +02001538 areq, bool encrypt)
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001539{
1540 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1541 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
Horia Geanta79fd31d2012-08-02 17:16:40 +03001542 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001543
Horia Geanta79fd31d2012-08-02 17:16:40 +03001544 return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1545 areq->info, 0, areq->nbytes, 0, ivsize, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001546 areq->base.flags, encrypt);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001547}
1548
1549static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1550{
1551 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1552 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1553 struct talitos_edesc *edesc;
1554
1555 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001556 edesc = ablkcipher_edesc_alloc(areq, true);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001557 if (IS_ERR(edesc))
1558 return PTR_ERR(edesc);
1559
1560 /* set encrypt */
1561 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1562
Kim Phillipsfebec542011-07-15 11:21:39 +08001563 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001564}
1565
1566static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1567{
1568 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1569 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1570 struct talitos_edesc *edesc;
1571
1572 /* allocate extended descriptor */
Horia Geanta62293a32013-11-28 15:11:17 +02001573 edesc = ablkcipher_edesc_alloc(areq, false);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001574 if (IS_ERR(edesc))
1575 return PTR_ERR(edesc);
1576
1577 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1578
Kim Phillipsfebec542011-07-15 11:21:39 +08001579 return common_nonsnoop(edesc, areq, ablkcipher_done);
Lee Nipper4de9d0b2009-03-29 15:52:32 +08001580}
1581
Lee Nipper497f2e62010-05-19 19:20:36 +10001582static void common_nonsnoop_hash_unmap(struct device *dev,
1583 struct talitos_edesc *edesc,
1584 struct ahash_request *areq)
1585{
1586 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001587 struct talitos_private *priv = dev_get_drvdata(dev);
1588 bool is_sec1 = has_ftr_sec1(priv);
Lee Nipper497f2e62010-05-19 19:20:36 +10001589
1590 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1591
LEROY Christophe032d1972015-04-17 16:31:51 +02001592 unmap_sg_talitos_ptr(dev, req_ctx->psrc, NULL, 0, edesc);
1593
Lee Nipper497f2e62010-05-19 19:20:36 +10001594 /* When using hashctx-in, must unmap it. */
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001595 if (from_talitos_ptr_len(&edesc->desc.ptr[1], is_sec1))
Lee Nipper497f2e62010-05-19 19:20:36 +10001596 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1597 DMA_TO_DEVICE);
1598
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001599 if (from_talitos_ptr_len(&edesc->desc.ptr[2], is_sec1))
Lee Nipper497f2e62010-05-19 19:20:36 +10001600 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1601 DMA_TO_DEVICE);
1602
Lee Nipper497f2e62010-05-19 19:20:36 +10001603 if (edesc->dma_len)
1604 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1605 DMA_BIDIRECTIONAL);
1606
1607}
1608
1609static void ahash_done(struct device *dev,
1610 struct talitos_desc *desc, void *context,
1611 int err)
1612{
1613 struct ahash_request *areq = context;
1614 struct talitos_edesc *edesc =
1615 container_of(desc, struct talitos_edesc, desc);
1616 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1617
1618 if (!req_ctx->last && req_ctx->to_hash_later) {
1619 /* Position any partial block for next update/final/finup */
1620 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
Lee Nipper5e833bc2010-06-16 15:29:15 +10001621 req_ctx->nbuf = req_ctx->to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001622 }
1623 common_nonsnoop_hash_unmap(dev, edesc, areq);
1624
1625 kfree(edesc);
1626
1627 areq->base.complete(&areq->base, err);
1628}
1629
1630static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1631 struct ahash_request *areq, unsigned int length,
1632 void (*callback) (struct device *dev,
1633 struct talitos_desc *desc,
1634 void *context, int error))
1635{
1636 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1637 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1638 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1639 struct device *dev = ctx->dev;
1640 struct talitos_desc *desc = &edesc->desc;
LEROY Christophe032d1972015-04-17 16:31:51 +02001641 int ret;
LEROY Christophe922f9dc2015-04-17 16:32:07 +02001642 struct talitos_private *priv = dev_get_drvdata(dev);
1643 bool is_sec1 = has_ftr_sec1(priv);
Lee Nipper497f2e62010-05-19 19:20:36 +10001644
1645 /* first DWORD empty */
1646 desc->ptr[0] = zero_entry;
1647
Kim Phillips60f208d2010-05-19 19:21:53 +10001648 /* hash context in */
1649 if (!req_ctx->first || req_ctx->swinit) {
Lee Nipper497f2e62010-05-19 19:20:36 +10001650 map_single_talitos_ptr(dev, &desc->ptr[1],
1651 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001652 (char *)req_ctx->hw_context,
Lee Nipper497f2e62010-05-19 19:20:36 +10001653 DMA_TO_DEVICE);
Kim Phillips60f208d2010-05-19 19:21:53 +10001654 req_ctx->swinit = 0;
Lee Nipper497f2e62010-05-19 19:20:36 +10001655 } else {
1656 desc->ptr[1] = zero_entry;
1657 /* Indicate next op is not the first. */
1658 req_ctx->first = 0;
1659 }
1660
1661 /* HMAC key */
1662 if (ctx->keylen)
1663 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001664 (char *)&ctx->key, DMA_TO_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001665 else
1666 desc->ptr[2] = zero_entry;
1667
1668 /*
1669 * data in
1670 */
LEROY Christophe032d1972015-04-17 16:31:51 +02001671 map_sg_in_talitos_ptr(dev, req_ctx->psrc, length, edesc,
1672 DMA_TO_DEVICE, &desc->ptr[3]);
Lee Nipper497f2e62010-05-19 19:20:36 +10001673
1674 /* fifth DWORD empty */
1675 desc->ptr[4] = zero_entry;
1676
1677 /* hash/HMAC out -or- hash context out */
1678 if (req_ctx->last)
1679 map_single_talitos_ptr(dev, &desc->ptr[5],
1680 crypto_ahash_digestsize(tfm),
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001681 areq->result, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001682 else
1683 map_single_talitos_ptr(dev, &desc->ptr[5],
1684 req_ctx->hw_context_size,
LEROY Christophea2b35aa2015-04-17 16:31:57 +02001685 req_ctx->hw_context, DMA_FROM_DEVICE);
Lee Nipper497f2e62010-05-19 19:20:36 +10001686
1687 /* last DWORD empty */
1688 desc->ptr[6] = zero_entry;
1689
Kim Phillips5228f0f2011-07-15 11:21:38 +08001690 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001691 if (ret != -EINPROGRESS) {
1692 common_nonsnoop_hash_unmap(dev, edesc, areq);
1693 kfree(edesc);
1694 }
1695 return ret;
1696}
1697
1698static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1699 unsigned int nbytes)
1700{
1701 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1702 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1703 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1704
Horia Geanta79fd31d2012-08-02 17:16:40 +03001705 return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
Horia Geanta62293a32013-11-28 15:11:17 +02001706 nbytes, 0, 0, 0, areq->base.flags, false);
Lee Nipper497f2e62010-05-19 19:20:36 +10001707}
1708
1709static int ahash_init(struct ahash_request *areq)
1710{
1711 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1712 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1713
1714 /* Initialize the context */
Lee Nipper5e833bc2010-06-16 15:29:15 +10001715 req_ctx->nbuf = 0;
Kim Phillips60f208d2010-05-19 19:21:53 +10001716 req_ctx->first = 1; /* first indicates h/w must init its context */
1717 req_ctx->swinit = 0; /* assume h/w init of context */
Lee Nipper497f2e62010-05-19 19:20:36 +10001718 req_ctx->hw_context_size =
1719 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1720 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1721 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1722
1723 return 0;
1724}
1725
Kim Phillips60f208d2010-05-19 19:21:53 +10001726/*
1727 * on h/w without explicit sha224 support, we initialize h/w context
1728 * manually with sha224 constants, and tell it to run sha256.
1729 */
1730static int ahash_init_sha224_swinit(struct ahash_request *areq)
1731{
1732 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1733
1734 ahash_init(areq);
1735 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1736
Kim Phillipsa7524472010-09-23 15:56:38 +08001737 req_ctx->hw_context[0] = SHA224_H0;
1738 req_ctx->hw_context[1] = SHA224_H1;
1739 req_ctx->hw_context[2] = SHA224_H2;
1740 req_ctx->hw_context[3] = SHA224_H3;
1741 req_ctx->hw_context[4] = SHA224_H4;
1742 req_ctx->hw_context[5] = SHA224_H5;
1743 req_ctx->hw_context[6] = SHA224_H6;
1744 req_ctx->hw_context[7] = SHA224_H7;
Kim Phillips60f208d2010-05-19 19:21:53 +10001745
1746 /* init 64-bit count */
1747 req_ctx->hw_context[8] = 0;
1748 req_ctx->hw_context[9] = 0;
1749
1750 return 0;
1751}
1752
Lee Nipper497f2e62010-05-19 19:20:36 +10001753static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1754{
1755 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1756 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1757 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1758 struct talitos_edesc *edesc;
1759 unsigned int blocksize =
1760 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1761 unsigned int nbytes_to_hash;
1762 unsigned int to_hash_later;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001763 unsigned int nsg;
Horia Geanta2a1cfe42012-08-02 17:16:39 +03001764 bool chained;
Lee Nipper497f2e62010-05-19 19:20:36 +10001765
Lee Nipper5e833bc2010-06-16 15:29:15 +10001766 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1767 /* Buffer up to one whole block */
Lee Nipper497f2e62010-05-19 19:20:36 +10001768 sg_copy_to_buffer(areq->src,
1769 sg_count(areq->src, nbytes, &chained),
Lee Nipper5e833bc2010-06-16 15:29:15 +10001770 req_ctx->buf + req_ctx->nbuf, nbytes);
1771 req_ctx->nbuf += nbytes;
Lee Nipper497f2e62010-05-19 19:20:36 +10001772 return 0;
1773 }
1774
Lee Nipper5e833bc2010-06-16 15:29:15 +10001775 /* At least (blocksize + 1) bytes are available to hash */
1776 nbytes_to_hash = nbytes + req_ctx->nbuf;
1777 to_hash_later = nbytes_to_hash & (blocksize - 1);
1778
1779 if (req_ctx->last)
1780 to_hash_later = 0;
1781 else if (to_hash_later)
1782 /* There is a partial block. Hash the full block(s) now */
1783 nbytes_to_hash -= to_hash_later;
1784 else {
1785 /* Keep one block buffered */
1786 nbytes_to_hash -= blocksize;
1787 to_hash_later = blocksize;
1788 }
1789
1790 /* Chain in any previously buffered data */
1791 if (req_ctx->nbuf) {
1792 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1793 sg_init_table(req_ctx->bufsl, nsg);
1794 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1795 if (nsg > 1)
1796 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
Lee Nipper497f2e62010-05-19 19:20:36 +10001797 req_ctx->psrc = req_ctx->bufsl;
Lee Nipper5e833bc2010-06-16 15:29:15 +10001798 } else
Lee Nipper497f2e62010-05-19 19:20:36 +10001799 req_ctx->psrc = areq->src;
Lee Nipper497f2e62010-05-19 19:20:36 +10001800
Lee Nipper5e833bc2010-06-16 15:29:15 +10001801 if (to_hash_later) {
1802 int nents = sg_count(areq->src, nbytes, &chained);
Akinobu Mitad0525722013-07-08 16:01:55 -07001803 sg_pcopy_to_buffer(areq->src, nents,
Lee Nipper5e833bc2010-06-16 15:29:15 +10001804 req_ctx->bufnext,
1805 to_hash_later,
1806 nbytes - to_hash_later);
Lee Nipper497f2e62010-05-19 19:20:36 +10001807 }
Lee Nipper5e833bc2010-06-16 15:29:15 +10001808 req_ctx->to_hash_later = to_hash_later;
Lee Nipper497f2e62010-05-19 19:20:36 +10001809
Lee Nipper5e833bc2010-06-16 15:29:15 +10001810 /* Allocate extended descriptor */
Lee Nipper497f2e62010-05-19 19:20:36 +10001811 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1812 if (IS_ERR(edesc))
1813 return PTR_ERR(edesc);
1814
1815 edesc->desc.hdr = ctx->desc_hdr_template;
1816
1817 /* On last one, request SEC to pad; otherwise continue */
1818 if (req_ctx->last)
1819 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1820 else
1821 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1822
Kim Phillips60f208d2010-05-19 19:21:53 +10001823 /* request SEC to INIT hash. */
1824 if (req_ctx->first && !req_ctx->swinit)
Lee Nipper497f2e62010-05-19 19:20:36 +10001825 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1826
1827 /* When the tfm context has a keylen, it's an HMAC.
1828 * A first or last (ie. not middle) descriptor must request HMAC.
1829 */
1830 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1831 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1832
1833 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1834 ahash_done);
1835}
1836
1837static int ahash_update(struct ahash_request *areq)
1838{
1839 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1840
1841 req_ctx->last = 0;
1842
1843 return ahash_process_req(areq, areq->nbytes);
1844}
1845
1846static int ahash_final(struct ahash_request *areq)
1847{
1848 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1849
1850 req_ctx->last = 1;
1851
1852 return ahash_process_req(areq, 0);
1853}
1854
1855static int ahash_finup(struct ahash_request *areq)
1856{
1857 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1858
1859 req_ctx->last = 1;
1860
1861 return ahash_process_req(areq, areq->nbytes);
1862}
1863
1864static int ahash_digest(struct ahash_request *areq)
1865{
1866 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
Kim Phillips60f208d2010-05-19 19:21:53 +10001867 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001868
Kim Phillips60f208d2010-05-19 19:21:53 +10001869 ahash->init(areq);
Lee Nipper497f2e62010-05-19 19:20:36 +10001870 req_ctx->last = 1;
1871
1872 return ahash_process_req(areq, areq->nbytes);
1873}
1874
Lee Nipper79b3a412011-11-21 16:13:25 +08001875struct keyhash_result {
1876 struct completion completion;
1877 int err;
1878};
1879
1880static void keyhash_complete(struct crypto_async_request *req, int err)
1881{
1882 struct keyhash_result *res = req->data;
1883
1884 if (err == -EINPROGRESS)
1885 return;
1886
1887 res->err = err;
1888 complete(&res->completion);
1889}
1890
1891static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1892 u8 *hash)
1893{
1894 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1895
1896 struct scatterlist sg[1];
1897 struct ahash_request *req;
1898 struct keyhash_result hresult;
1899 int ret;
1900
1901 init_completion(&hresult.completion);
1902
1903 req = ahash_request_alloc(tfm, GFP_KERNEL);
1904 if (!req)
1905 return -ENOMEM;
1906
1907 /* Keep tfm keylen == 0 during hash of the long key */
1908 ctx->keylen = 0;
1909 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1910 keyhash_complete, &hresult);
1911
1912 sg_init_one(&sg[0], key, keylen);
1913
1914 ahash_request_set_crypt(req, sg, hash, keylen);
1915 ret = crypto_ahash_digest(req);
1916 switch (ret) {
1917 case 0:
1918 break;
1919 case -EINPROGRESS:
1920 case -EBUSY:
1921 ret = wait_for_completion_interruptible(
1922 &hresult.completion);
1923 if (!ret)
1924 ret = hresult.err;
1925 break;
1926 default:
1927 break;
1928 }
1929 ahash_request_free(req);
1930
1931 return ret;
1932}
1933
1934static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1935 unsigned int keylen)
1936{
1937 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1938 unsigned int blocksize =
1939 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1940 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1941 unsigned int keysize = keylen;
1942 u8 hash[SHA512_DIGEST_SIZE];
1943 int ret;
1944
1945 if (keylen <= blocksize)
1946 memcpy(ctx->key, key, keysize);
1947 else {
1948 /* Must get the hash of the long key */
1949 ret = keyhash(tfm, key, keylen, hash);
1950
1951 if (ret) {
1952 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1953 return -EINVAL;
1954 }
1955
1956 keysize = digestsize;
1957 memcpy(ctx->key, hash, digestsize);
1958 }
1959
1960 ctx->keylen = keysize;
1961
1962 return 0;
1963}
1964
1965
Kim Phillips9c4a7962008-06-23 19:50:15 +08001966struct talitos_alg_template {
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001967 u32 type;
1968 union {
1969 struct crypto_alg crypto;
Lee Nipperacbf7c622010-05-19 19:19:33 +10001970 struct ahash_alg hash;
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001971 } alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08001972 __be32 desc_hdr_template;
1973};
1974
1975static struct talitos_alg_template driver_algs[] = {
Horia Geanta991155b2013-03-20 16:31:38 +02001976 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001977 { .type = CRYPTO_ALG_TYPE_AEAD,
1978 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001979 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1980 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1981 .cra_blocksize = AES_BLOCK_SIZE,
1982 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08001983 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001984 .ivsize = AES_BLOCK_SIZE,
1985 .maxauthsize = SHA1_DIGEST_SIZE,
1986 }
1987 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08001988 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1989 DESC_HDR_SEL0_AESU |
1990 DESC_HDR_MODE0_AESU_CBC |
1991 DESC_HDR_SEL1_MDEUA |
1992 DESC_HDR_MODE1_MDEU_INIT |
1993 DESC_HDR_MODE1_MDEU_PAD |
1994 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper70bcaca2008-07-03 19:08:46 +08001995 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10001996 { .type = CRYPTO_ALG_TYPE_AEAD,
1997 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08001998 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1999 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
2000 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2001 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002002 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002003 .ivsize = DES3_EDE_BLOCK_SIZE,
2004 .maxauthsize = SHA1_DIGEST_SIZE,
2005 }
2006 },
Lee Nipper70bcaca2008-07-03 19:08:46 +08002007 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2008 DESC_HDR_SEL0_DEU |
2009 DESC_HDR_MODE0_DEU_CBC |
2010 DESC_HDR_MODE0_DEU_3DES |
2011 DESC_HDR_SEL1_MDEUA |
2012 DESC_HDR_MODE1_MDEU_INIT |
2013 DESC_HDR_MODE1_MDEU_PAD |
2014 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
Lee Nipper3952f172008-07-10 18:29:18 +08002015 },
Horia Geanta357fb602012-07-03 19:16:53 +03002016 { .type = CRYPTO_ALG_TYPE_AEAD,
2017 .alg.crypto = {
2018 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2019 .cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
2020 .cra_blocksize = AES_BLOCK_SIZE,
2021 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002022 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002023 .ivsize = AES_BLOCK_SIZE,
2024 .maxauthsize = SHA224_DIGEST_SIZE,
2025 }
2026 },
2027 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2028 DESC_HDR_SEL0_AESU |
2029 DESC_HDR_MODE0_AESU_CBC |
2030 DESC_HDR_SEL1_MDEUA |
2031 DESC_HDR_MODE1_MDEU_INIT |
2032 DESC_HDR_MODE1_MDEU_PAD |
2033 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2034 },
2035 { .type = CRYPTO_ALG_TYPE_AEAD,
2036 .alg.crypto = {
2037 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
2038 .cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
2039 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2040 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002041 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002042 .ivsize = DES3_EDE_BLOCK_SIZE,
2043 .maxauthsize = SHA224_DIGEST_SIZE,
2044 }
2045 },
2046 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2047 DESC_HDR_SEL0_DEU |
2048 DESC_HDR_MODE0_DEU_CBC |
2049 DESC_HDR_MODE0_DEU_3DES |
2050 DESC_HDR_SEL1_MDEUA |
2051 DESC_HDR_MODE1_MDEU_INIT |
2052 DESC_HDR_MODE1_MDEU_PAD |
2053 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2054 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002055 { .type = CRYPTO_ALG_TYPE_AEAD,
2056 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002057 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2058 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
2059 .cra_blocksize = AES_BLOCK_SIZE,
2060 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002061 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002062 .ivsize = AES_BLOCK_SIZE,
2063 .maxauthsize = SHA256_DIGEST_SIZE,
2064 }
2065 },
Lee Nipper3952f172008-07-10 18:29:18 +08002066 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2067 DESC_HDR_SEL0_AESU |
2068 DESC_HDR_MODE0_AESU_CBC |
2069 DESC_HDR_SEL1_MDEUA |
2070 DESC_HDR_MODE1_MDEU_INIT |
2071 DESC_HDR_MODE1_MDEU_PAD |
2072 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2073 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002074 { .type = CRYPTO_ALG_TYPE_AEAD,
2075 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002076 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2077 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2078 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2079 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002080 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002081 .ivsize = DES3_EDE_BLOCK_SIZE,
2082 .maxauthsize = SHA256_DIGEST_SIZE,
2083 }
2084 },
Lee Nipper3952f172008-07-10 18:29:18 +08002085 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2086 DESC_HDR_SEL0_DEU |
2087 DESC_HDR_MODE0_DEU_CBC |
2088 DESC_HDR_MODE0_DEU_3DES |
2089 DESC_HDR_SEL1_MDEUA |
2090 DESC_HDR_MODE1_MDEU_INIT |
2091 DESC_HDR_MODE1_MDEU_PAD |
2092 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2093 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002094 { .type = CRYPTO_ALG_TYPE_AEAD,
2095 .alg.crypto = {
Horia Geanta357fb602012-07-03 19:16:53 +03002096 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2097 .cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2098 .cra_blocksize = AES_BLOCK_SIZE,
2099 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002100 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002101 .ivsize = AES_BLOCK_SIZE,
2102 .maxauthsize = SHA384_DIGEST_SIZE,
2103 }
2104 },
2105 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2106 DESC_HDR_SEL0_AESU |
2107 DESC_HDR_MODE0_AESU_CBC |
2108 DESC_HDR_SEL1_MDEUB |
2109 DESC_HDR_MODE1_MDEU_INIT |
2110 DESC_HDR_MODE1_MDEU_PAD |
2111 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2112 },
2113 { .type = CRYPTO_ALG_TYPE_AEAD,
2114 .alg.crypto = {
2115 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2116 .cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2117 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2118 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002119 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002120 .ivsize = DES3_EDE_BLOCK_SIZE,
2121 .maxauthsize = SHA384_DIGEST_SIZE,
2122 }
2123 },
2124 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2125 DESC_HDR_SEL0_DEU |
2126 DESC_HDR_MODE0_DEU_CBC |
2127 DESC_HDR_MODE0_DEU_3DES |
2128 DESC_HDR_SEL1_MDEUB |
2129 DESC_HDR_MODE1_MDEU_INIT |
2130 DESC_HDR_MODE1_MDEU_PAD |
2131 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2132 },
2133 { .type = CRYPTO_ALG_TYPE_AEAD,
2134 .alg.crypto = {
2135 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2136 .cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2137 .cra_blocksize = AES_BLOCK_SIZE,
2138 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002139 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002140 .ivsize = AES_BLOCK_SIZE,
2141 .maxauthsize = SHA512_DIGEST_SIZE,
2142 }
2143 },
2144 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2145 DESC_HDR_SEL0_AESU |
2146 DESC_HDR_MODE0_AESU_CBC |
2147 DESC_HDR_SEL1_MDEUB |
2148 DESC_HDR_MODE1_MDEU_INIT |
2149 DESC_HDR_MODE1_MDEU_PAD |
2150 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2151 },
2152 { .type = CRYPTO_ALG_TYPE_AEAD,
2153 .alg.crypto = {
2154 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2155 .cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2156 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2157 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Horia Geanta357fb602012-07-03 19:16:53 +03002158 .cra_aead = {
Horia Geanta357fb602012-07-03 19:16:53 +03002159 .ivsize = DES3_EDE_BLOCK_SIZE,
2160 .maxauthsize = SHA512_DIGEST_SIZE,
2161 }
2162 },
2163 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2164 DESC_HDR_SEL0_DEU |
2165 DESC_HDR_MODE0_DEU_CBC |
2166 DESC_HDR_MODE0_DEU_3DES |
2167 DESC_HDR_SEL1_MDEUB |
2168 DESC_HDR_MODE1_MDEU_INIT |
2169 DESC_HDR_MODE1_MDEU_PAD |
2170 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2171 },
2172 { .type = CRYPTO_ALG_TYPE_AEAD,
2173 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002174 .cra_name = "authenc(hmac(md5),cbc(aes))",
2175 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2176 .cra_blocksize = AES_BLOCK_SIZE,
2177 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002178 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002179 .ivsize = AES_BLOCK_SIZE,
2180 .maxauthsize = MD5_DIGEST_SIZE,
2181 }
2182 },
Lee Nipper3952f172008-07-10 18:29:18 +08002183 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2184 DESC_HDR_SEL0_AESU |
2185 DESC_HDR_MODE0_AESU_CBC |
2186 DESC_HDR_SEL1_MDEUA |
2187 DESC_HDR_MODE1_MDEU_INIT |
2188 DESC_HDR_MODE1_MDEU_PAD |
2189 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2190 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002191 { .type = CRYPTO_ALG_TYPE_AEAD,
2192 .alg.crypto = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002193 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2194 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2195 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2196 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
Lee Nipper56af8cd2009-03-29 15:50:50 +08002197 .cra_aead = {
Lee Nipper56af8cd2009-03-29 15:50:50 +08002198 .ivsize = DES3_EDE_BLOCK_SIZE,
2199 .maxauthsize = MD5_DIGEST_SIZE,
2200 }
2201 },
Lee Nipper3952f172008-07-10 18:29:18 +08002202 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2203 DESC_HDR_SEL0_DEU |
2204 DESC_HDR_MODE0_DEU_CBC |
2205 DESC_HDR_MODE0_DEU_3DES |
2206 DESC_HDR_SEL1_MDEUA |
2207 DESC_HDR_MODE1_MDEU_INIT |
2208 DESC_HDR_MODE1_MDEU_PAD |
2209 DESC_HDR_MODE1_MDEU_MD5_HMAC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002210 },
2211 /* ABLKCIPHER algorithms. */
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002212 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2213 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002214 .cra_name = "cbc(aes)",
2215 .cra_driver_name = "cbc-aes-talitos",
2216 .cra_blocksize = AES_BLOCK_SIZE,
2217 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2218 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002219 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002220 .min_keysize = AES_MIN_KEY_SIZE,
2221 .max_keysize = AES_MAX_KEY_SIZE,
2222 .ivsize = AES_BLOCK_SIZE,
2223 }
2224 },
2225 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2226 DESC_HDR_SEL0_AESU |
2227 DESC_HDR_MODE0_AESU_CBC,
2228 },
Lee Nipperd5e4aae2010-05-19 19:18:38 +10002229 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2230 .alg.crypto = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002231 .cra_name = "cbc(des3_ede)",
2232 .cra_driver_name = "cbc-3des-talitos",
2233 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2234 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2235 CRYPTO_ALG_ASYNC,
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002236 .cra_ablkcipher = {
Lee Nipper4de9d0b2009-03-29 15:52:32 +08002237 .min_keysize = DES3_EDE_KEY_SIZE,
2238 .max_keysize = DES3_EDE_KEY_SIZE,
2239 .ivsize = DES3_EDE_BLOCK_SIZE,
2240 }
2241 },
2242 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2243 DESC_HDR_SEL0_DEU |
2244 DESC_HDR_MODE0_DEU_CBC |
2245 DESC_HDR_MODE0_DEU_3DES,
Lee Nipper497f2e62010-05-19 19:20:36 +10002246 },
2247 /* AHASH algorithms. */
2248 { .type = CRYPTO_ALG_TYPE_AHASH,
2249 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002250 .halg.digestsize = MD5_DIGEST_SIZE,
2251 .halg.base = {
2252 .cra_name = "md5",
2253 .cra_driver_name = "md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002254 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper497f2e62010-05-19 19:20:36 +10002255 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2256 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002257 }
2258 },
2259 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2260 DESC_HDR_SEL0_MDEUA |
2261 DESC_HDR_MODE0_MDEU_MD5,
2262 },
2263 { .type = CRYPTO_ALG_TYPE_AHASH,
2264 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002265 .halg.digestsize = SHA1_DIGEST_SIZE,
2266 .halg.base = {
2267 .cra_name = "sha1",
2268 .cra_driver_name = "sha1-talitos",
2269 .cra_blocksize = SHA1_BLOCK_SIZE,
2270 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2271 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002272 }
2273 },
2274 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2275 DESC_HDR_SEL0_MDEUA |
2276 DESC_HDR_MODE0_MDEU_SHA1,
2277 },
2278 { .type = CRYPTO_ALG_TYPE_AHASH,
2279 .alg.hash = {
Kim Phillips60f208d2010-05-19 19:21:53 +10002280 .halg.digestsize = SHA224_DIGEST_SIZE,
2281 .halg.base = {
2282 .cra_name = "sha224",
2283 .cra_driver_name = "sha224-talitos",
2284 .cra_blocksize = SHA224_BLOCK_SIZE,
2285 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2286 CRYPTO_ALG_ASYNC,
Kim Phillips60f208d2010-05-19 19:21:53 +10002287 }
2288 },
2289 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2290 DESC_HDR_SEL0_MDEUA |
2291 DESC_HDR_MODE0_MDEU_SHA224,
2292 },
2293 { .type = CRYPTO_ALG_TYPE_AHASH,
2294 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002295 .halg.digestsize = SHA256_DIGEST_SIZE,
2296 .halg.base = {
2297 .cra_name = "sha256",
2298 .cra_driver_name = "sha256-talitos",
2299 .cra_blocksize = SHA256_BLOCK_SIZE,
2300 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2301 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002302 }
2303 },
2304 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2305 DESC_HDR_SEL0_MDEUA |
2306 DESC_HDR_MODE0_MDEU_SHA256,
2307 },
2308 { .type = CRYPTO_ALG_TYPE_AHASH,
2309 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002310 .halg.digestsize = SHA384_DIGEST_SIZE,
2311 .halg.base = {
2312 .cra_name = "sha384",
2313 .cra_driver_name = "sha384-talitos",
2314 .cra_blocksize = SHA384_BLOCK_SIZE,
2315 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2316 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002317 }
2318 },
2319 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2320 DESC_HDR_SEL0_MDEUB |
2321 DESC_HDR_MODE0_MDEUB_SHA384,
2322 },
2323 { .type = CRYPTO_ALG_TYPE_AHASH,
2324 .alg.hash = {
Lee Nipper497f2e62010-05-19 19:20:36 +10002325 .halg.digestsize = SHA512_DIGEST_SIZE,
2326 .halg.base = {
2327 .cra_name = "sha512",
2328 .cra_driver_name = "sha512-talitos",
2329 .cra_blocksize = SHA512_BLOCK_SIZE,
2330 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2331 CRYPTO_ALG_ASYNC,
Lee Nipper497f2e62010-05-19 19:20:36 +10002332 }
2333 },
2334 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2335 DESC_HDR_SEL0_MDEUB |
2336 DESC_HDR_MODE0_MDEUB_SHA512,
2337 },
Lee Nipper79b3a412011-11-21 16:13:25 +08002338 { .type = CRYPTO_ALG_TYPE_AHASH,
2339 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002340 .halg.digestsize = MD5_DIGEST_SIZE,
2341 .halg.base = {
2342 .cra_name = "hmac(md5)",
2343 .cra_driver_name = "hmac-md5-talitos",
Martin Hicksb3988612015-03-03 08:21:34 -05002344 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
Lee Nipper79b3a412011-11-21 16:13:25 +08002345 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2346 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002347 }
2348 },
2349 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2350 DESC_HDR_SEL0_MDEUA |
2351 DESC_HDR_MODE0_MDEU_MD5,
2352 },
2353 { .type = CRYPTO_ALG_TYPE_AHASH,
2354 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002355 .halg.digestsize = SHA1_DIGEST_SIZE,
2356 .halg.base = {
2357 .cra_name = "hmac(sha1)",
2358 .cra_driver_name = "hmac-sha1-talitos",
2359 .cra_blocksize = SHA1_BLOCK_SIZE,
2360 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2361 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002362 }
2363 },
2364 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2365 DESC_HDR_SEL0_MDEUA |
2366 DESC_HDR_MODE0_MDEU_SHA1,
2367 },
2368 { .type = CRYPTO_ALG_TYPE_AHASH,
2369 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002370 .halg.digestsize = SHA224_DIGEST_SIZE,
2371 .halg.base = {
2372 .cra_name = "hmac(sha224)",
2373 .cra_driver_name = "hmac-sha224-talitos",
2374 .cra_blocksize = SHA224_BLOCK_SIZE,
2375 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2376 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002377 }
2378 },
2379 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2380 DESC_HDR_SEL0_MDEUA |
2381 DESC_HDR_MODE0_MDEU_SHA224,
2382 },
2383 { .type = CRYPTO_ALG_TYPE_AHASH,
2384 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002385 .halg.digestsize = SHA256_DIGEST_SIZE,
2386 .halg.base = {
2387 .cra_name = "hmac(sha256)",
2388 .cra_driver_name = "hmac-sha256-talitos",
2389 .cra_blocksize = SHA256_BLOCK_SIZE,
2390 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2391 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002392 }
2393 },
2394 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2395 DESC_HDR_SEL0_MDEUA |
2396 DESC_HDR_MODE0_MDEU_SHA256,
2397 },
2398 { .type = CRYPTO_ALG_TYPE_AHASH,
2399 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002400 .halg.digestsize = SHA384_DIGEST_SIZE,
2401 .halg.base = {
2402 .cra_name = "hmac(sha384)",
2403 .cra_driver_name = "hmac-sha384-talitos",
2404 .cra_blocksize = SHA384_BLOCK_SIZE,
2405 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2406 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002407 }
2408 },
2409 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2410 DESC_HDR_SEL0_MDEUB |
2411 DESC_HDR_MODE0_MDEUB_SHA384,
2412 },
2413 { .type = CRYPTO_ALG_TYPE_AHASH,
2414 .alg.hash = {
Lee Nipper79b3a412011-11-21 16:13:25 +08002415 .halg.digestsize = SHA512_DIGEST_SIZE,
2416 .halg.base = {
2417 .cra_name = "hmac(sha512)",
2418 .cra_driver_name = "hmac-sha512-talitos",
2419 .cra_blocksize = SHA512_BLOCK_SIZE,
2420 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2421 CRYPTO_ALG_ASYNC,
Lee Nipper79b3a412011-11-21 16:13:25 +08002422 }
2423 },
2424 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2425 DESC_HDR_SEL0_MDEUB |
2426 DESC_HDR_MODE0_MDEUB_SHA512,
2427 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002428};
2429
2430struct talitos_crypto_alg {
2431 struct list_head entry;
2432 struct device *dev;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002433 struct talitos_alg_template algt;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002434};
2435
2436static int talitos_cra_init(struct crypto_tfm *tfm)
2437{
2438 struct crypto_alg *alg = tfm->__crt_alg;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002439 struct talitos_crypto_alg *talitos_alg;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002440 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips5228f0f2011-07-15 11:21:38 +08002441 struct talitos_private *priv;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002442
Lee Nipper497f2e62010-05-19 19:20:36 +10002443 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2444 talitos_alg = container_of(__crypto_ahash_alg(alg),
2445 struct talitos_crypto_alg,
2446 algt.alg.hash);
2447 else
2448 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2449 algt.alg.crypto);
Kim Phillips19bbbc62009-03-29 15:53:59 +08002450
Kim Phillips9c4a7962008-06-23 19:50:15 +08002451 /* update context with ptr to dev */
2452 ctx->dev = talitos_alg->dev;
Kim Phillips19bbbc62009-03-29 15:53:59 +08002453
Kim Phillips5228f0f2011-07-15 11:21:38 +08002454 /* assign SEC channel to tfm in round-robin fashion */
2455 priv = dev_get_drvdata(ctx->dev);
2456 ctx->ch = atomic_inc_return(&priv->last_chan) &
2457 (priv->num_channels - 1);
2458
Kim Phillips9c4a7962008-06-23 19:50:15 +08002459 /* copy descriptor header template value */
Lee Nipperacbf7c622010-05-19 19:19:33 +10002460 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002461
Kim Phillips602dba52011-07-15 11:21:39 +08002462 /* select done notification */
2463 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2464
Lee Nipper497f2e62010-05-19 19:20:36 +10002465 return 0;
2466}
2467
2468static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2469{
2470 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2471
2472 talitos_cra_init(tfm);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002473
2474 /* random first IV */
Lee Nipper70bcaca2008-07-03 19:08:46 +08002475 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002476
2477 return 0;
2478}
2479
Lee Nipper497f2e62010-05-19 19:20:36 +10002480static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2481{
2482 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2483
2484 talitos_cra_init(tfm);
2485
2486 ctx->keylen = 0;
2487 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2488 sizeof(struct talitos_ahash_req_ctx));
2489
2490 return 0;
2491}
2492
Kim Phillips9c4a7962008-06-23 19:50:15 +08002493/*
2494 * given the alg's descriptor header template, determine whether descriptor
2495 * type and primary/secondary execution units required match the hw
2496 * capabilities description provided in the device tree node.
2497 */
2498static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2499{
2500 struct talitos_private *priv = dev_get_drvdata(dev);
2501 int ret;
2502
2503 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2504 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2505
2506 if (SECONDARY_EU(desc_hdr_template))
2507 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2508 & priv->exec_units);
2509
2510 return ret;
2511}
2512
Grant Likely2dc11582010-08-06 09:25:50 -06002513static int talitos_remove(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002514{
2515 struct device *dev = &ofdev->dev;
2516 struct talitos_private *priv = dev_get_drvdata(dev);
2517 struct talitos_crypto_alg *t_alg, *n;
2518 int i;
2519
2520 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
Lee Nipperacbf7c622010-05-19 19:19:33 +10002521 switch (t_alg->algt.type) {
2522 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2523 case CRYPTO_ALG_TYPE_AEAD:
2524 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2525 break;
2526 case CRYPTO_ALG_TYPE_AHASH:
2527 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2528 break;
2529 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002530 list_del(&t_alg->entry);
2531 kfree(t_alg);
2532 }
2533
2534 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2535 talitos_unregister_rng(dev);
2536
Kim Phillips4b9926282009-08-13 11:50:38 +10002537 for (i = 0; i < priv->num_channels; i++)
Kim Phillips0b798242010-09-23 15:56:08 +08002538 kfree(priv->chan[i].fifo);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002539
Kim Phillips4b9926282009-08-13 11:50:38 +10002540 kfree(priv->chan);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002541
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002542 for (i = 0; i < 2; i++)
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002543 if (priv->irq[i]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002544 free_irq(priv->irq[i], dev);
2545 irq_dispose_mapping(priv->irq[i]);
2546 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002547
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002548 tasklet_kill(&priv->done_task[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002549 if (priv->irq[1])
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002550 tasklet_kill(&priv->done_task[1]);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002551
2552 iounmap(priv->reg);
2553
Kim Phillips9c4a7962008-06-23 19:50:15 +08002554 kfree(priv);
2555
2556 return 0;
2557}
2558
2559static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2560 struct talitos_alg_template
2561 *template)
2562{
Kim Phillips60f208d2010-05-19 19:21:53 +10002563 struct talitos_private *priv = dev_get_drvdata(dev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002564 struct talitos_crypto_alg *t_alg;
2565 struct crypto_alg *alg;
2566
2567 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2568 if (!t_alg)
2569 return ERR_PTR(-ENOMEM);
2570
Lee Nipperacbf7c622010-05-19 19:19:33 +10002571 t_alg->algt = *template;
2572
2573 switch (t_alg->algt.type) {
2574 case CRYPTO_ALG_TYPE_ABLKCIPHER:
Lee Nipper497f2e62010-05-19 19:20:36 +10002575 alg = &t_alg->algt.alg.crypto;
2576 alg->cra_init = talitos_cra_init;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002577 alg->cra_type = &crypto_ablkcipher_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002578 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2579 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2580 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2581 alg->cra_ablkcipher.geniv = "eseqiv";
Lee Nipper497f2e62010-05-19 19:20:36 +10002582 break;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002583 case CRYPTO_ALG_TYPE_AEAD:
2584 alg = &t_alg->algt.alg.crypto;
Lee Nipper497f2e62010-05-19 19:20:36 +10002585 alg->cra_init = talitos_cra_init_aead;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002586 alg->cra_type = &crypto_aead_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002587 alg->cra_aead.setkey = aead_setkey;
2588 alg->cra_aead.setauthsize = aead_setauthsize;
2589 alg->cra_aead.encrypt = aead_encrypt;
2590 alg->cra_aead.decrypt = aead_decrypt;
2591 alg->cra_aead.givencrypt = aead_givencrypt;
2592 alg->cra_aead.geniv = "<built-in>";
Lee Nipperacbf7c622010-05-19 19:19:33 +10002593 break;
2594 case CRYPTO_ALG_TYPE_AHASH:
2595 alg = &t_alg->algt.alg.hash.halg.base;
Lee Nipper497f2e62010-05-19 19:20:36 +10002596 alg->cra_init = talitos_cra_init_ahash;
Kim Phillipsd4cd3282012-08-08 20:32:00 -05002597 alg->cra_type = &crypto_ahash_type;
Kim Phillipsb286e002012-08-08 20:33:34 -05002598 t_alg->algt.alg.hash.init = ahash_init;
2599 t_alg->algt.alg.hash.update = ahash_update;
2600 t_alg->algt.alg.hash.final = ahash_final;
2601 t_alg->algt.alg.hash.finup = ahash_finup;
2602 t_alg->algt.alg.hash.digest = ahash_digest;
2603 t_alg->algt.alg.hash.setkey = ahash_setkey;
2604
Lee Nipper79b3a412011-11-21 16:13:25 +08002605 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
Kim Phillips0b2730d2011-12-12 14:59:10 -06002606 !strncmp(alg->cra_name, "hmac", 4)) {
2607 kfree(t_alg);
Lee Nipper79b3a412011-11-21 16:13:25 +08002608 return ERR_PTR(-ENOTSUPP);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002609 }
Kim Phillips60f208d2010-05-19 19:21:53 +10002610 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
Lee Nipper79b3a412011-11-21 16:13:25 +08002611 (!strcmp(alg->cra_name, "sha224") ||
2612 !strcmp(alg->cra_name, "hmac(sha224)"))) {
Kim Phillips60f208d2010-05-19 19:21:53 +10002613 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2614 t_alg->algt.desc_hdr_template =
2615 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2616 DESC_HDR_SEL0_MDEUA |
2617 DESC_HDR_MODE0_MDEU_SHA256;
2618 }
Lee Nipper497f2e62010-05-19 19:20:36 +10002619 break;
Kim Phillips1d119112010-09-23 15:55:27 +08002620 default:
2621 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2622 return ERR_PTR(-EINVAL);
Lee Nipperacbf7c622010-05-19 19:19:33 +10002623 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002624
Kim Phillips9c4a7962008-06-23 19:50:15 +08002625 alg->cra_module = THIS_MODULE;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002626 alg->cra_priority = TALITOS_CRA_PRIORITY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002627 alg->cra_alignmask = 0;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002628 alg->cra_ctxsize = sizeof(struct talitos_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002629 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002630
Kim Phillips9c4a7962008-06-23 19:50:15 +08002631 t_alg->dev = dev;
2632
2633 return t_alg;
2634}
2635
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002636static int talitos_probe_irq(struct platform_device *ofdev)
2637{
2638 struct device *dev = &ofdev->dev;
2639 struct device_node *np = ofdev->dev.of_node;
2640 struct talitos_private *priv = dev_get_drvdata(dev);
2641 int err;
2642
2643 priv->irq[0] = irq_of_parse_and_map(np, 0);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002644 if (!priv->irq[0]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002645 dev_err(dev, "failed to map irq\n");
2646 return -EINVAL;
2647 }
2648
2649 priv->irq[1] = irq_of_parse_and_map(np, 1);
2650
2651 /* get the primary irq line */
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002652 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002653 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2654 dev_driver_string(dev), dev);
2655 goto primary_out;
2656 }
2657
2658 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2659 dev_driver_string(dev), dev);
2660 if (err)
2661 goto primary_out;
2662
2663 /* get the secondary irq line */
2664 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2665 dev_driver_string(dev), dev);
2666 if (err) {
2667 dev_err(dev, "failed to request secondary irq\n");
2668 irq_dispose_mapping(priv->irq[1]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002669 priv->irq[1] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002670 }
2671
2672 return err;
2673
2674primary_out:
2675 if (err) {
2676 dev_err(dev, "failed to request primary irq\n");
2677 irq_dispose_mapping(priv->irq[0]);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002678 priv->irq[0] = 0;
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002679 }
2680
2681 return err;
2682}
2683
Grant Likely1c48a5c2011-02-17 02:43:24 -07002684static int talitos_probe(struct platform_device *ofdev)
Kim Phillips9c4a7962008-06-23 19:50:15 +08002685{
2686 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07002687 struct device_node *np = ofdev->dev.of_node;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002688 struct talitos_private *priv;
2689 const unsigned int *prop;
2690 int i, err;
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02002691 int stride;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002692
2693 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2694 if (!priv)
2695 return -ENOMEM;
2696
Kevin Haof3de9cb2014-01-28 20:17:23 +08002697 INIT_LIST_HEAD(&priv->alg_list);
2698
Kim Phillips9c4a7962008-06-23 19:50:15 +08002699 dev_set_drvdata(dev, priv);
2700
2701 priv->ofdev = ofdev;
2702
Horia Geanta511d63c2012-03-30 17:49:53 +03002703 spin_lock_init(&priv->reg_lock);
2704
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002705 err = talitos_probe_irq(ofdev);
2706 if (err)
2707 goto err_out;
2708
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002709 if (!priv->irq[1]) {
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002710 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2711 (unsigned long)dev);
2712 } else {
2713 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2714 (unsigned long)dev);
2715 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2716 (unsigned long)dev);
2717 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002718
Kim Phillips9c4a7962008-06-23 19:50:15 +08002719 priv->reg = of_iomap(np, 0);
2720 if (!priv->reg) {
2721 dev_err(dev, "failed to of_iomap\n");
2722 err = -ENOMEM;
2723 goto err_out;
2724 }
2725
2726 /* get SEC version capabilities from device tree */
2727 prop = of_get_property(np, "fsl,num-channels", NULL);
2728 if (prop)
2729 priv->num_channels = *prop;
2730
2731 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2732 if (prop)
2733 priv->chfifo_len = *prop;
2734
2735 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2736 if (prop)
2737 priv->exec_units = *prop;
2738
2739 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2740 if (prop)
2741 priv->desc_types = *prop;
2742
2743 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2744 !priv->exec_units || !priv->desc_types) {
2745 dev_err(dev, "invalid property data in device tree node\n");
2746 err = -EINVAL;
2747 goto err_out;
2748 }
2749
Lee Nipperf3c85bc2008-07-30 16:26:57 +08002750 if (of_device_is_compatible(np, "fsl,sec3.0"))
2751 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2752
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002753 if (of_device_is_compatible(np, "fsl,sec2.1"))
Kim Phillips60f208d2010-05-19 19:21:53 +10002754 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
Lee Nipper79b3a412011-11-21 16:13:25 +08002755 TALITOS_FTR_SHA224_HWINIT |
2756 TALITOS_FTR_HMAC_OK;
Kim Phillipsfe5720e2008-10-12 20:33:14 +08002757
LEROY Christophe21590882015-04-17 16:32:05 +02002758 if (of_device_is_compatible(np, "fsl,sec1.0"))
2759 priv->features |= TALITOS_FTR_SEC1;
2760
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02002761 if (of_device_is_compatible(np, "fsl,sec1.2")) {
2762 priv->reg_deu = priv->reg + TALITOS12_DEU;
2763 priv->reg_aesu = priv->reg + TALITOS12_AESU;
2764 priv->reg_mdeu = priv->reg + TALITOS12_MDEU;
2765 stride = TALITOS1_CH_STRIDE;
2766 } else if (of_device_is_compatible(np, "fsl,sec1.0")) {
2767 priv->reg_deu = priv->reg + TALITOS10_DEU;
2768 priv->reg_aesu = priv->reg + TALITOS10_AESU;
2769 priv->reg_mdeu = priv->reg + TALITOS10_MDEU;
2770 priv->reg_afeu = priv->reg + TALITOS10_AFEU;
2771 priv->reg_rngu = priv->reg + TALITOS10_RNGU;
2772 priv->reg_pkeu = priv->reg + TALITOS10_PKEU;
2773 stride = TALITOS1_CH_STRIDE;
2774 } else {
2775 priv->reg_deu = priv->reg + TALITOS2_DEU;
2776 priv->reg_aesu = priv->reg + TALITOS2_AESU;
2777 priv->reg_mdeu = priv->reg + TALITOS2_MDEU;
2778 priv->reg_afeu = priv->reg + TALITOS2_AFEU;
2779 priv->reg_rngu = priv->reg + TALITOS2_RNGU;
2780 priv->reg_pkeu = priv->reg + TALITOS2_PKEU;
2781 priv->reg_keu = priv->reg + TALITOS2_KEU;
2782 priv->reg_crcu = priv->reg + TALITOS2_CRCU;
2783 stride = TALITOS2_CH_STRIDE;
2784 }
2785
Kim Phillips4b9926282009-08-13 11:50:38 +10002786 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2787 priv->num_channels, GFP_KERNEL);
2788 if (!priv->chan) {
2789 dev_err(dev, "failed to allocate channel management space\n");
Kim Phillips9c4a7962008-06-23 19:50:15 +08002790 err = -ENOMEM;
2791 goto err_out;
2792 }
2793
Martin Hicksf641ddd2015-03-03 08:21:33 -05002794 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2795
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002796 for (i = 0; i < priv->num_channels; i++) {
LEROY Christophe5fa7fa12015-04-17 16:32:11 +02002797 priv->chan[i].reg = priv->reg + stride * (i + 1);
Kim Phillips2cdba3c2011-12-12 14:59:11 -06002798 if (!priv->irq[1] || !(i & 1))
Kim Phillipsc3e337f2011-11-21 16:13:27 +08002799 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
Kim Phillipsad42d5f2011-11-21 16:13:27 +08002800
Kim Phillips4b9926282009-08-13 11:50:38 +10002801 spin_lock_init(&priv->chan[i].head_lock);
2802 spin_lock_init(&priv->chan[i].tail_lock);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002803
Kim Phillips4b9926282009-08-13 11:50:38 +10002804 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2805 priv->fifo_len, GFP_KERNEL);
2806 if (!priv->chan[i].fifo) {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002807 dev_err(dev, "failed to allocate request fifo %d\n", i);
2808 err = -ENOMEM;
2809 goto err_out;
2810 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002811
Kim Phillips4b9926282009-08-13 11:50:38 +10002812 atomic_set(&priv->chan[i].submit_count,
2813 -(priv->chfifo_len - 1));
Martin Hicksf641ddd2015-03-03 08:21:33 -05002814 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002815
Kim Phillips81eb0242009-08-13 11:51:51 +10002816 dma_set_mask(dev, DMA_BIT_MASK(36));
2817
Kim Phillips9c4a7962008-06-23 19:50:15 +08002818 /* reset and initialize the h/w */
2819 err = init_device(dev);
2820 if (err) {
2821 dev_err(dev, "failed to initialize device\n");
2822 goto err_out;
2823 }
2824
2825 /* register the RNG, if available */
2826 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2827 err = talitos_register_rng(dev);
2828 if (err) {
2829 dev_err(dev, "failed to register hwrng: %d\n", err);
2830 goto err_out;
2831 } else
2832 dev_info(dev, "hwrng\n");
2833 }
2834
2835 /* register crypto algorithms the device supports */
Kim Phillips9c4a7962008-06-23 19:50:15 +08002836 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2837 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2838 struct talitos_crypto_alg *t_alg;
Lee Nipperacbf7c622010-05-19 19:19:33 +10002839 char *name = NULL;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002840
2841 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2842 if (IS_ERR(t_alg)) {
2843 err = PTR_ERR(t_alg);
Kim Phillips0b2730d2011-12-12 14:59:10 -06002844 if (err == -ENOTSUPP)
Lee Nipper79b3a412011-11-21 16:13:25 +08002845 continue;
Kim Phillips9c4a7962008-06-23 19:50:15 +08002846 goto err_out;
2847 }
2848
Lee Nipperacbf7c622010-05-19 19:19:33 +10002849 switch (t_alg->algt.type) {
2850 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2851 case CRYPTO_ALG_TYPE_AEAD:
2852 err = crypto_register_alg(
2853 &t_alg->algt.alg.crypto);
2854 name = t_alg->algt.alg.crypto.cra_driver_name;
2855 break;
2856 case CRYPTO_ALG_TYPE_AHASH:
2857 err = crypto_register_ahash(
2858 &t_alg->algt.alg.hash);
2859 name =
2860 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2861 break;
2862 }
Kim Phillips9c4a7962008-06-23 19:50:15 +08002863 if (err) {
2864 dev_err(dev, "%s alg registration failed\n",
Lee Nipperacbf7c622010-05-19 19:19:33 +10002865 name);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002866 kfree(t_alg);
Horia Geanta991155b2013-03-20 16:31:38 +02002867 } else
Kim Phillips9c4a7962008-06-23 19:50:15 +08002868 list_add_tail(&t_alg->entry, &priv->alg_list);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002869 }
2870 }
Kim Phillips5b859b6e2011-11-21 16:13:26 +08002871 if (!list_empty(&priv->alg_list))
2872 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2873 (char *)of_get_property(np, "compatible", NULL));
Kim Phillips9c4a7962008-06-23 19:50:15 +08002874
2875 return 0;
2876
2877err_out:
2878 talitos_remove(ofdev);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002879
2880 return err;
2881}
2882
Márton Németh6c3f9752010-01-17 21:54:01 +11002883static const struct of_device_id talitos_match[] = {
Kim Phillips9c4a7962008-06-23 19:50:15 +08002884 {
2885 .compatible = "fsl,sec2.0",
2886 },
2887 {},
2888};
2889MODULE_DEVICE_TABLE(of, talitos_match);
2890
Grant Likely1c48a5c2011-02-17 02:43:24 -07002891static struct platform_driver talitos_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002892 .driver = {
2893 .name = "talitos",
Grant Likely40182942010-04-13 16:13:02 -07002894 .of_match_table = talitos_match,
2895 },
Kim Phillips9c4a7962008-06-23 19:50:15 +08002896 .probe = talitos_probe,
Al Viro596f1032008-11-22 17:34:24 +00002897 .remove = talitos_remove,
Kim Phillips9c4a7962008-06-23 19:50:15 +08002898};
2899
Axel Lin741e8c22011-11-26 21:26:19 +08002900module_platform_driver(talitos_driver);
Kim Phillips9c4a7962008-06-23 19:50:15 +08002901
2902MODULE_LICENSE("GPL");
2903MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2904MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");