2018-08-21 13:36:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2012-07-01 17:19:45 +00:00
|
|
|
/*
|
|
|
|
* Cryptographic API.
|
|
|
|
*
|
|
|
|
* Support for ATMEL DES/TDES HW acceleration.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Eukréa Electromatique - ATMEL
|
|
|
|
* Author: Nicolas Royer <nicolas@eukrea.com>
|
|
|
|
*
|
|
|
|
* Some ideas are from omap-aes.c drivers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/hw_random.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
2020-01-15 12:53:53 +00:00
|
|
|
#include <linux/dmaengine.h>
|
2012-07-01 17:19:45 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2023-07-14 17:44:18 +00:00
|
|
|
#include <linux/mod_devicetable.h>
|
2012-07-01 17:19:45 +00:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <crypto/scatterwalk.h>
|
|
|
|
#include <crypto/algapi.h>
|
2019-08-15 09:00:46 +00:00
|
|
|
#include <crypto/internal/des.h>
|
2019-11-09 17:09:34 +00:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2012-07-01 17:19:45 +00:00
|
|
|
#include "atmel-tdes-regs.h"
|
|
|
|
|
2019-12-05 09:53:46 +00:00
|
|
|
#define ATMEL_TDES_PRIORITY 300
|
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
/* TDES flags */
|
2019-12-05 09:53:56 +00:00
|
|
|
/* Reserve bits [17:16], [13:12], [2:0] for AES Mode Register */
|
|
|
|
#define TDES_FLAGS_ENCRYPT TDES_MR_CYPHER_ENC
|
|
|
|
#define TDES_FLAGS_OPMODE_MASK (TDES_MR_OPMOD_MASK | TDES_MR_CFBS_MASK)
|
|
|
|
#define TDES_FLAGS_ECB TDES_MR_OPMOD_ECB
|
|
|
|
#define TDES_FLAGS_CBC TDES_MR_OPMOD_CBC
|
|
|
|
|
|
|
|
#define TDES_FLAGS_MODE_MASK (TDES_FLAGS_OPMODE_MASK | TDES_FLAGS_ENCRYPT)
|
|
|
|
|
|
|
|
#define TDES_FLAGS_INIT BIT(3)
|
|
|
|
#define TDES_FLAGS_FAST BIT(4)
|
|
|
|
#define TDES_FLAGS_BUSY BIT(5)
|
|
|
|
#define TDES_FLAGS_DMA BIT(6)
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
#define ATMEL_TDES_QUEUE_LENGTH 50
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
struct atmel_tdes_caps {
|
|
|
|
bool has_dma;
|
|
|
|
};
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
struct atmel_tdes_dev;
|
|
|
|
|
|
|
|
struct atmel_tdes_ctx {
|
|
|
|
struct atmel_tdes_dev *dd;
|
|
|
|
|
|
|
|
int keylen;
|
2019-11-09 17:09:34 +00:00
|
|
|
u32 key[DES3_EDE_KEY_SIZE / sizeof(u32)];
|
2012-07-01 17:19:45 +00:00
|
|
|
unsigned long flags;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
u16 block_size;
|
2012-07-01 17:19:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct atmel_tdes_reqctx {
|
|
|
|
unsigned long mode;
|
2019-11-15 13:49:06 +00:00
|
|
|
u8 lastc[DES_BLOCK_SIZE];
|
2012-07-01 17:19:45 +00:00
|
|
|
};
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
struct atmel_tdes_dma {
|
|
|
|
struct dma_chan *chan;
|
|
|
|
struct dma_slave_config dma_conf;
|
|
|
|
};
|
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
struct atmel_tdes_dev {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned long phys_base;
|
|
|
|
void __iomem *io_base;
|
|
|
|
|
|
|
|
struct atmel_tdes_ctx *ctx;
|
|
|
|
struct device *dev;
|
|
|
|
struct clk *iclk;
|
|
|
|
int irq;
|
|
|
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spinlock_t lock;
|
|
|
|
struct crypto_queue queue;
|
|
|
|
|
|
|
|
struct tasklet_struct done_task;
|
|
|
|
struct tasklet_struct queue_task;
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
struct skcipher_request *req;
|
2012-07-01 17:19:45 +00:00
|
|
|
size_t total;
|
|
|
|
|
|
|
|
struct scatterlist *in_sg;
|
2013-02-20 16:10:25 +00:00
|
|
|
unsigned int nb_in_sg;
|
2012-07-01 17:19:45 +00:00
|
|
|
size_t in_offset;
|
|
|
|
struct scatterlist *out_sg;
|
2013-02-20 16:10:25 +00:00
|
|
|
unsigned int nb_out_sg;
|
2012-07-01 17:19:45 +00:00
|
|
|
size_t out_offset;
|
|
|
|
|
|
|
|
size_t buflen;
|
|
|
|
size_t dma_size;
|
|
|
|
|
|
|
|
void *buf_in;
|
|
|
|
int dma_in;
|
|
|
|
dma_addr_t dma_addr_in;
|
2013-02-20 16:10:25 +00:00
|
|
|
struct atmel_tdes_dma dma_lch_in;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
void *buf_out;
|
|
|
|
int dma_out;
|
|
|
|
dma_addr_t dma_addr_out;
|
2013-02-20 16:10:25 +00:00
|
|
|
struct atmel_tdes_dma dma_lch_out;
|
|
|
|
|
|
|
|
struct atmel_tdes_caps caps;
|
|
|
|
|
|
|
|
u32 hw_version;
|
2012-07-01 17:19:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct atmel_tdes_drv {
|
|
|
|
struct list_head dev_list;
|
|
|
|
spinlock_t lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct atmel_tdes_drv atmel_tdes = {
|
|
|
|
.dev_list = LIST_HEAD_INIT(atmel_tdes.dev_list),
|
|
|
|
.lock = __SPIN_LOCK_UNLOCKED(atmel_tdes.lock),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int atmel_tdes_sg_copy(struct scatterlist **sg, size_t *offset,
|
|
|
|
void *buf, size_t buflen, size_t total, int out)
|
|
|
|
{
|
2017-02-06 12:32:16 +00:00
|
|
|
size_t count, off = 0;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
while (buflen && total) {
|
|
|
|
count = min((*sg)->length - *offset, total);
|
|
|
|
count = min(count, buflen);
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return off;
|
|
|
|
|
|
|
|
scatterwalk_map_and_copy(buf + off, *sg, *offset, count, out);
|
|
|
|
|
|
|
|
off += count;
|
|
|
|
buflen -= count;
|
|
|
|
*offset += count;
|
|
|
|
total -= count;
|
|
|
|
|
|
|
|
if (*offset == (*sg)->length) {
|
|
|
|
*sg = sg_next(*sg);
|
|
|
|
if (*sg)
|
|
|
|
*offset = 0;
|
|
|
|
else
|
|
|
|
total = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 atmel_tdes_read(struct atmel_tdes_dev *dd, u32 offset)
|
|
|
|
{
|
|
|
|
return readl_relaxed(dd->io_base + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atmel_tdes_write(struct atmel_tdes_dev *dd,
|
|
|
|
u32 offset, u32 value)
|
|
|
|
{
|
|
|
|
writel_relaxed(value, dd->io_base + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void atmel_tdes_write_n(struct atmel_tdes_dev *dd, u32 offset,
|
2019-12-05 09:53:45 +00:00
|
|
|
const u32 *value, int count)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
for (; count--; value++, offset += 4)
|
|
|
|
atmel_tdes_write(dd, offset, *value);
|
|
|
|
}
|
|
|
|
|
2021-07-20 08:55:27 +00:00
|
|
|
static struct atmel_tdes_dev *atmel_tdes_dev_alloc(void)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2021-07-20 08:55:27 +00:00
|
|
|
struct atmel_tdes_dev *tdes_dd;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
spin_lock_bh(&atmel_tdes.lock);
|
2021-07-20 08:55:27 +00:00
|
|
|
/* One TDES IP per SoC. */
|
|
|
|
tdes_dd = list_first_entry_or_null(&atmel_tdes.dev_list,
|
|
|
|
struct atmel_tdes_dev, list);
|
2012-07-01 17:19:45 +00:00
|
|
|
spin_unlock_bh(&atmel_tdes.lock);
|
|
|
|
return tdes_dd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_tdes_hw_init(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
2015-10-02 12:12:58 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = clk_prepare_enable(dd->iclk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
if (!(dd->flags & TDES_FLAGS_INIT)) {
|
|
|
|
atmel_tdes_write(dd, TDES_CR, TDES_CR_SWRST);
|
|
|
|
dd->flags |= TDES_FLAGS_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static inline unsigned int atmel_tdes_get_version(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
return atmel_tdes_read(dd, TDES_HW_VERSION) & 0x00000fff;
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:53:50 +00:00
|
|
|
static int atmel_tdes_hw_version_init(struct atmel_tdes_dev *dd)
|
2013-02-20 16:10:25 +00:00
|
|
|
{
|
2019-12-05 09:53:50 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = atmel_tdes_hw_init(dd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
dd->hw_version = atmel_tdes_get_version(dd);
|
|
|
|
|
|
|
|
dev_info(dd->dev,
|
|
|
|
"version: 0x%x\n", dd->hw_version);
|
|
|
|
|
|
|
|
clk_disable_unprepare(dd->iclk);
|
2019-12-05 09:53:50 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-02-20 16:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void atmel_tdes_dma_callback(void *data)
|
|
|
|
{
|
|
|
|
struct atmel_tdes_dev *dd = data;
|
|
|
|
|
|
|
|
/* dma_lch_out - completed */
|
|
|
|
tasklet_schedule(&dd->done_task);
|
|
|
|
}
|
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
static int atmel_tdes_write_ctrl(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
int err;
|
2019-12-05 09:53:55 +00:00
|
|
|
u32 valmr = TDES_MR_SMOD_PDC;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
err = atmel_tdes_hw_init(dd);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
if (!dd->caps.has_dma)
|
|
|
|
atmel_tdes_write(dd, TDES_PTCR,
|
|
|
|
TDES_PTCR_TXTDIS | TDES_PTCR_RXTDIS);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
/* MR register must be set before IV registers */
|
|
|
|
if (dd->ctx->keylen > (DES_KEY_SIZE << 1)) {
|
|
|
|
valmr |= TDES_MR_KEYMOD_3KEY;
|
|
|
|
valmr |= TDES_MR_TDESMOD_TDES;
|
|
|
|
} else if (dd->ctx->keylen > DES_KEY_SIZE) {
|
|
|
|
valmr |= TDES_MR_KEYMOD_2KEY;
|
|
|
|
valmr |= TDES_MR_TDESMOD_TDES;
|
|
|
|
} else {
|
|
|
|
valmr |= TDES_MR_TDESMOD_DES;
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:53:56 +00:00
|
|
|
valmr |= dd->flags & TDES_FLAGS_MODE_MASK;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
atmel_tdes_write(dd, TDES_MR, valmr);
|
|
|
|
|
|
|
|
atmel_tdes_write_n(dd, TDES_KEY1W1R, dd->ctx->key,
|
|
|
|
dd->ctx->keylen >> 2);
|
|
|
|
|
2019-12-05 09:53:56 +00:00
|
|
|
if (dd->req->iv && (valmr & TDES_MR_OPMOD_MASK) != TDES_MR_OPMOD_ECB)
|
2019-11-09 17:09:34 +00:00
|
|
|
atmel_tdes_write_n(dd, TDES_IV1R, (void *)dd->req->iv, 2);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static int atmel_tdes_crypt_pdc_stop(struct atmel_tdes_dev *dd)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
|
|
|
|
|
|
|
|
if (dd->flags & TDES_FLAGS_FAST) {
|
|
|
|
dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
|
|
|
|
dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
|
|
|
|
} else {
|
|
|
|
dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
|
|
|
|
dd->dma_size, DMA_FROM_DEVICE);
|
|
|
|
|
|
|
|
/* copy data */
|
|
|
|
count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
|
|
|
|
dd->buf_out, dd->buflen, dd->dma_size, 1);
|
|
|
|
if (count != dd->dma_size) {
|
|
|
|
err = -EINVAL;
|
2021-07-20 08:55:28 +00:00
|
|
|
dev_dbg(dd->dev, "not all data converted: %zu\n", count);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static int atmel_tdes_buff_init(struct atmel_tdes_dev *dd)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, 0);
|
|
|
|
dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, 0);
|
|
|
|
dd->buflen = PAGE_SIZE;
|
|
|
|
dd->buflen &= ~(DES_BLOCK_SIZE - 1);
|
|
|
|
|
|
|
|
if (!dd->buf_in || !dd->buf_out) {
|
2021-07-20 08:55:28 +00:00
|
|
|
dev_dbg(dd->dev, "unable to alloc pages.\n");
|
2012-07-01 17:19:45 +00:00
|
|
|
goto err_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MAP here */
|
|
|
|
dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in,
|
|
|
|
dd->buflen, DMA_TO_DEVICE);
|
2021-07-20 08:55:28 +00:00
|
|
|
err = dma_mapping_error(dd->dev, dd->dma_addr_in);
|
|
|
|
if (err) {
|
|
|
|
dev_dbg(dd->dev, "dma %zd bytes error\n", dd->buflen);
|
2012-07-01 17:19:45 +00:00
|
|
|
goto err_map_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out,
|
|
|
|
dd->buflen, DMA_FROM_DEVICE);
|
2021-07-20 08:55:28 +00:00
|
|
|
err = dma_mapping_error(dd->dev, dd->dma_addr_out);
|
|
|
|
if (err) {
|
|
|
|
dev_dbg(dd->dev, "dma %zd bytes error\n", dd->buflen);
|
2012-07-01 17:19:45 +00:00
|
|
|
goto err_map_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_map_out:
|
|
|
|
dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
err_map_in:
|
2015-01-20 07:15:52 +00:00
|
|
|
err_alloc:
|
2012-07-01 17:19:45 +00:00
|
|
|
free_page((unsigned long)dd->buf_out);
|
|
|
|
free_page((unsigned long)dd->buf_in);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static void atmel_tdes_buff_cleanup(struct atmel_tdes_dev *dd)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
|
|
|
|
DMA_FROM_DEVICE);
|
|
|
|
dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
free_page((unsigned long)dd->buf_out);
|
|
|
|
free_page((unsigned long)dd->buf_in);
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:53:58 +00:00
|
|
|
static int atmel_tdes_crypt_pdc(struct atmel_tdes_dev *dd,
|
|
|
|
dma_addr_t dma_addr_in,
|
|
|
|
dma_addr_t dma_addr_out, int length)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
int len32;
|
|
|
|
|
|
|
|
dd->dma_size = length;
|
|
|
|
|
|
|
|
if (!(dd->flags & TDES_FLAGS_FAST)) {
|
|
|
|
dma_sync_single_for_device(dd->dev, dma_addr_in, length,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:33:56 +00:00
|
|
|
len32 = DIV_ROUND_UP(length, sizeof(u32));
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTDIS|TDES_PTCR_RXTDIS);
|
|
|
|
atmel_tdes_write(dd, TDES_TPR, dma_addr_in);
|
|
|
|
atmel_tdes_write(dd, TDES_TCR, len32);
|
|
|
|
atmel_tdes_write(dd, TDES_RPR, dma_addr_out);
|
|
|
|
atmel_tdes_write(dd, TDES_RCR, len32);
|
|
|
|
|
|
|
|
/* Enable Interrupt */
|
|
|
|
atmel_tdes_write(dd, TDES_IER, TDES_INT_ENDRX);
|
|
|
|
|
|
|
|
/* Start DMA transfer */
|
|
|
|
atmel_tdes_write(dd, TDES_PTCR, TDES_PTCR_TXTEN | TDES_PTCR_RXTEN);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-05 09:53:58 +00:00
|
|
|
static int atmel_tdes_crypt_dma(struct atmel_tdes_dev *dd,
|
|
|
|
dma_addr_t dma_addr_in,
|
|
|
|
dma_addr_t dma_addr_out, int length)
|
2013-02-20 16:10:25 +00:00
|
|
|
{
|
|
|
|
struct scatterlist sg[2];
|
|
|
|
struct dma_async_tx_descriptor *in_desc, *out_desc;
|
2019-12-05 09:53:56 +00:00
|
|
|
enum dma_slave_buswidth addr_width;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
dd->dma_size = length;
|
|
|
|
|
|
|
|
if (!(dd->flags & TDES_FLAGS_FAST)) {
|
|
|
|
dma_sync_single_for_device(dd->dev, dma_addr_in, length,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
}
|
|
|
|
|
2023-09-16 09:33:56 +00:00
|
|
|
addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
2019-12-05 09:53:56 +00:00
|
|
|
dd->dma_lch_in.dma_conf.dst_addr_width = addr_width;
|
|
|
|
dd->dma_lch_out.dma_conf.src_addr_width = addr_width;
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
|
|
|
|
dmaengine_slave_config(dd->dma_lch_out.chan, &dd->dma_lch_out.dma_conf);
|
|
|
|
|
|
|
|
dd->flags |= TDES_FLAGS_DMA;
|
|
|
|
|
|
|
|
sg_init_table(&sg[0], 1);
|
|
|
|
sg_dma_address(&sg[0]) = dma_addr_in;
|
|
|
|
sg_dma_len(&sg[0]) = length;
|
|
|
|
|
|
|
|
sg_init_table(&sg[1], 1);
|
|
|
|
sg_dma_address(&sg[1]) = dma_addr_out;
|
|
|
|
sg_dma_len(&sg[1]) = length;
|
|
|
|
|
|
|
|
in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, &sg[0],
|
|
|
|
1, DMA_MEM_TO_DEV,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!in_desc)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
out_desc = dmaengine_prep_slave_sg(dd->dma_lch_out.chan, &sg[1],
|
|
|
|
1, DMA_DEV_TO_MEM,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!out_desc)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
out_desc->callback = atmel_tdes_dma_callback;
|
|
|
|
out_desc->callback_param = dd;
|
|
|
|
|
|
|
|
dmaengine_submit(out_desc);
|
|
|
|
dma_async_issue_pending(dd->dma_lch_out.chan);
|
|
|
|
|
|
|
|
dmaengine_submit(in_desc);
|
|
|
|
dma_async_issue_pending(dd->dma_lch_in.chan);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
int err, fast = 0, in, out;
|
|
|
|
size_t count;
|
|
|
|
dma_addr_t addr_in, addr_out;
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
if ((!dd->in_offset) && (!dd->out_offset)) {
|
2012-07-01 17:19:45 +00:00
|
|
|
/* check for alignment */
|
2013-02-20 16:10:25 +00:00
|
|
|
in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)) &&
|
|
|
|
IS_ALIGNED(dd->in_sg->length, dd->ctx->block_size);
|
|
|
|
out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)) &&
|
|
|
|
IS_ALIGNED(dd->out_sg->length, dd->ctx->block_size);
|
2012-07-01 17:19:45 +00:00
|
|
|
fast = in && out;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
if (sg_dma_len(dd->in_sg) != sg_dma_len(dd->out_sg))
|
|
|
|
fast = 0;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
if (fast) {
|
2017-02-06 12:32:16 +00:00
|
|
|
count = min_t(size_t, dd->total, sg_dma_len(dd->in_sg));
|
|
|
|
count = min_t(size_t, count, sg_dma_len(dd->out_sg));
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
|
|
|
|
if (!err) {
|
2021-07-20 08:55:28 +00:00
|
|
|
dev_dbg(dd->dev, "dma_map_sg() error\n");
|
2012-07-01 17:19:45 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dma_map_sg(dd->dev, dd->out_sg, 1,
|
|
|
|
DMA_FROM_DEVICE);
|
|
|
|
if (!err) {
|
2021-07-20 08:55:28 +00:00
|
|
|
dev_dbg(dd->dev, "dma_map_sg() error\n");
|
2012-07-01 17:19:45 +00:00
|
|
|
dma_unmap_sg(dd->dev, dd->in_sg, 1,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_in = sg_dma_address(dd->in_sg);
|
|
|
|
addr_out = sg_dma_address(dd->out_sg);
|
|
|
|
|
|
|
|
dd->flags |= TDES_FLAGS_FAST;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* use cache buffers */
|
|
|
|
count = atmel_tdes_sg_copy(&dd->in_sg, &dd->in_offset,
|
|
|
|
dd->buf_in, dd->buflen, dd->total, 0);
|
|
|
|
|
|
|
|
addr_in = dd->dma_addr_in;
|
|
|
|
addr_out = dd->dma_addr_out;
|
|
|
|
|
|
|
|
dd->flags &= ~TDES_FLAGS_FAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
dd->total -= count;
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
if (dd->caps.has_dma)
|
2019-12-05 09:53:58 +00:00
|
|
|
err = atmel_tdes_crypt_dma(dd, addr_in, addr_out, count);
|
2013-02-20 16:10:25 +00:00
|
|
|
else
|
2019-12-05 09:53:58 +00:00
|
|
|
err = atmel_tdes_crypt_pdc(dd, addr_in, addr_out, count);
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
if (err && (dd->flags & TDES_FLAGS_FAST)) {
|
2012-07-01 17:19:45 +00:00
|
|
|
dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
|
|
|
|
dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:49:06 +00:00
|
|
|
static void
|
|
|
|
atmel_tdes_set_iv_as_last_ciphertext_block(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
struct skcipher_request *req = dd->req;
|
|
|
|
struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
|
|
|
|
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
|
|
|
|
unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
|
|
|
|
|
|
|
|
if (req->cryptlen < ivsize)
|
|
|
|
return;
|
|
|
|
|
2023-03-28 19:56:27 +00:00
|
|
|
if (rctx->mode & TDES_FLAGS_ENCRYPT)
|
2019-11-15 13:49:06 +00:00
|
|
|
scatterwalk_map_and_copy(req->iv, req->dst,
|
|
|
|
req->cryptlen - ivsize, ivsize, 0);
|
2023-03-28 19:56:27 +00:00
|
|
|
else
|
|
|
|
memcpy(req->iv, rctx->lastc, ivsize);
|
|
|
|
|
2019-11-15 13:49:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
struct skcipher_request *req = dd->req;
|
2019-12-05 09:54:00 +00:00
|
|
|
struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
clk_disable_unprepare(dd->iclk);
|
|
|
|
|
|
|
|
dd->flags &= ~TDES_FLAGS_BUSY;
|
|
|
|
|
2019-12-13 09:54:56 +00:00
|
|
|
if (!err && (rctx->mode & TDES_FLAGS_OPMODE_MASK) != TDES_FLAGS_ECB)
|
2019-12-05 09:54:00 +00:00
|
|
|
atmel_tdes_set_iv_as_last_ciphertext_block(dd);
|
2019-11-15 13:49:06 +00:00
|
|
|
|
2023-01-31 08:02:08 +00:00
|
|
|
skcipher_request_complete(req, err);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_tdes_handle_queue(struct atmel_tdes_dev *dd,
|
2019-11-09 17:09:34 +00:00
|
|
|
struct skcipher_request *req)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
struct crypto_async_request *async_req, *backlog;
|
|
|
|
struct atmel_tdes_ctx *ctx;
|
|
|
|
struct atmel_tdes_reqctx *rctx;
|
|
|
|
unsigned long flags;
|
|
|
|
int err, ret = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dd->lock, flags);
|
|
|
|
if (req)
|
2019-11-09 17:09:34 +00:00
|
|
|
ret = crypto_enqueue_request(&dd->queue, &req->base);
|
2012-07-01 17:19:45 +00:00
|
|
|
if (dd->flags & TDES_FLAGS_BUSY) {
|
|
|
|
spin_unlock_irqrestore(&dd->lock, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
backlog = crypto_get_backlog(&dd->queue);
|
|
|
|
async_req = crypto_dequeue_request(&dd->queue);
|
|
|
|
if (async_req)
|
|
|
|
dd->flags |= TDES_FLAGS_BUSY;
|
|
|
|
spin_unlock_irqrestore(&dd->lock, flags);
|
|
|
|
|
|
|
|
if (!async_req)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (backlog)
|
2023-01-31 08:02:08 +00:00
|
|
|
crypto_request_complete(backlog, -EINPROGRESS);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
req = skcipher_request_cast(async_req);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
/* assign new request to device */
|
|
|
|
dd->req = req;
|
2019-11-09 17:09:34 +00:00
|
|
|
dd->total = req->cryptlen;
|
2012-07-01 17:19:45 +00:00
|
|
|
dd->in_offset = 0;
|
|
|
|
dd->in_sg = req->src;
|
|
|
|
dd->out_offset = 0;
|
|
|
|
dd->out_sg = req->dst;
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
rctx = skcipher_request_ctx(req);
|
|
|
|
ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
|
2012-07-01 17:19:45 +00:00
|
|
|
rctx->mode &= TDES_FLAGS_MODE_MASK;
|
|
|
|
dd->flags = (dd->flags & ~TDES_FLAGS_MODE_MASK) | rctx->mode;
|
|
|
|
dd->ctx = ctx;
|
|
|
|
|
|
|
|
err = atmel_tdes_write_ctrl(dd);
|
|
|
|
if (!err)
|
2013-02-20 16:10:25 +00:00
|
|
|
err = atmel_tdes_crypt_start(dd);
|
2012-07-01 17:19:45 +00:00
|
|
|
if (err) {
|
|
|
|
/* des_task will not finish it, so do it here */
|
|
|
|
atmel_tdes_finish_req(dd, err);
|
|
|
|
tasklet_schedule(&dd->queue_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
int err = -EINVAL;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
if (dd->flags & TDES_FLAGS_DMA) {
|
|
|
|
err = 0;
|
|
|
|
if (dd->flags & TDES_FLAGS_FAST) {
|
|
|
|
dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
|
|
|
|
dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
|
|
|
|
} else {
|
|
|
|
dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
|
|
|
|
dd->dma_size, DMA_FROM_DEVICE);
|
|
|
|
|
|
|
|
/* copy data */
|
|
|
|
count = atmel_tdes_sg_copy(&dd->out_sg, &dd->out_offset,
|
|
|
|
dd->buf_out, dd->buflen, dd->dma_size, 1);
|
|
|
|
if (count != dd->dma_size) {
|
|
|
|
err = -EINVAL;
|
2021-07-20 08:55:28 +00:00
|
|
|
dev_dbg(dd->dev, "not all data converted: %zu\n", count);
|
2013-02-20 16:10:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-11-15 13:49:06 +00:00
|
|
|
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
|
|
|
|
struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(skcipher);
|
2019-11-09 17:09:34 +00:00
|
|
|
struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
|
2021-07-20 08:55:28 +00:00
|
|
|
struct device *dev = ctx->dd->dev;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2021-07-20 08:55:32 +00:00
|
|
|
if (!req->cryptlen)
|
|
|
|
return 0;
|
|
|
|
|
2023-09-16 09:33:56 +00:00
|
|
|
if (!IS_ALIGNED(req->cryptlen, DES_BLOCK_SIZE)) {
|
|
|
|
dev_dbg(dev, "request size is not exact amount of DES blocks\n");
|
|
|
|
return -EINVAL;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
2023-09-16 09:33:56 +00:00
|
|
|
ctx->block_size = DES_BLOCK_SIZE;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
rctx->mode = mode;
|
|
|
|
|
2019-12-05 09:54:00 +00:00
|
|
|
if ((mode & TDES_FLAGS_OPMODE_MASK) != TDES_FLAGS_ECB &&
|
2023-03-28 19:56:27 +00:00
|
|
|
!(mode & TDES_FLAGS_ENCRYPT)) {
|
2019-11-15 13:49:06 +00:00
|
|
|
unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
|
|
|
|
|
|
|
|
if (req->cryptlen >= ivsize)
|
|
|
|
scatterwalk_map_and_copy(rctx->lastc, req->src,
|
|
|
|
req->cryptlen - ivsize,
|
|
|
|
ivsize, 0);
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
return atmel_tdes_handle_queue(ctx->dd, req);
|
|
|
|
}
|
|
|
|
|
2019-12-13 09:54:49 +00:00
|
|
|
static int atmel_tdes_dma_init(struct atmel_tdes_dev *dd)
|
2013-02-20 16:10:25 +00:00
|
|
|
{
|
2019-11-21 10:16:02 +00:00
|
|
|
int ret;
|
2013-10-15 10:14:58 +00:00
|
|
|
|
|
|
|
/* Try to grab 2 DMA channels */
|
2019-11-21 10:16:02 +00:00
|
|
|
dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
|
|
|
|
if (IS_ERR(dd->dma_lch_in.chan)) {
|
|
|
|
ret = PTR_ERR(dd->dma_lch_in.chan);
|
2013-10-15 10:14:58 +00:00
|
|
|
goto err_dma_in;
|
2019-11-21 10:16:02 +00:00
|
|
|
}
|
2013-10-15 10:14:58 +00:00
|
|
|
|
|
|
|
dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
|
|
|
|
TDES_IDATA1R;
|
|
|
|
dd->dma_lch_in.dma_conf.src_maxburst = 1;
|
|
|
|
dd->dma_lch_in.dma_conf.src_addr_width =
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
dd->dma_lch_in.dma_conf.dst_maxburst = 1;
|
|
|
|
dd->dma_lch_in.dma_conf.dst_addr_width =
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
dd->dma_lch_in.dma_conf.device_fc = false;
|
|
|
|
|
2019-11-21 10:16:02 +00:00
|
|
|
dd->dma_lch_out.chan = dma_request_chan(dd->dev, "rx");
|
|
|
|
if (IS_ERR(dd->dma_lch_out.chan)) {
|
|
|
|
ret = PTR_ERR(dd->dma_lch_out.chan);
|
2013-10-15 10:14:58 +00:00
|
|
|
goto err_dma_out;
|
2019-11-21 10:16:02 +00:00
|
|
|
}
|
2013-10-15 10:14:58 +00:00
|
|
|
|
|
|
|
dd->dma_lch_out.dma_conf.src_addr = dd->phys_base +
|
|
|
|
TDES_ODATA1R;
|
|
|
|
dd->dma_lch_out.dma_conf.src_maxburst = 1;
|
|
|
|
dd->dma_lch_out.dma_conf.src_addr_width =
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
dd->dma_lch_out.dma_conf.dst_maxburst = 1;
|
|
|
|
dd->dma_lch_out.dma_conf.dst_addr_width =
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
dd->dma_lch_out.dma_conf.device_fc = false;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
2013-10-15 10:14:58 +00:00
|
|
|
return 0;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
err_dma_out:
|
|
|
|
dma_release_channel(dd->dma_lch_in.chan);
|
|
|
|
err_dma_in:
|
2019-12-13 09:54:54 +00:00
|
|
|
dev_err(dd->dev, "no DMA channel available\n");
|
2019-11-21 10:16:02 +00:00
|
|
|
return ret;
|
2013-02-20 16:10:25 +00:00
|
|
|
}
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static void atmel_tdes_dma_cleanup(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
dma_release_channel(dd->dma_lch_in.chan);
|
|
|
|
dma_release_channel(dd->dma_lch_out.chan);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2012-07-01 17:19:45 +00:00
|
|
|
unsigned int keylen)
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2019-08-15 09:00:46 +00:00
|
|
|
int err;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
err = verify_skcipher_des_key(tfm, key);
|
2019-08-15 09:00:46 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
memcpy(ctx->key, key, keylen);
|
|
|
|
ctx->keylen = keylen;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2012-07-01 17:19:45 +00:00
|
|
|
unsigned int keylen)
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2019-04-11 08:51:00 +00:00
|
|
|
int err;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
err = verify_skcipher_des3_key(tfm, key);
|
2019-08-15 09:00:46 +00:00
|
|
|
if (err)
|
2019-04-11 08:51:00 +00:00
|
|
|
return err;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
memcpy(ctx->key, key, keylen);
|
|
|
|
ctx->keylen = keylen;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_ecb_encrypt(struct skcipher_request *req)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-12-05 09:53:56 +00:00
|
|
|
return atmel_tdes_crypt(req, TDES_FLAGS_ECB | TDES_FLAGS_ENCRYPT);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_ecb_decrypt(struct skcipher_request *req)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-12-05 09:53:56 +00:00
|
|
|
return atmel_tdes_crypt(req, TDES_FLAGS_ECB);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_cbc_encrypt(struct skcipher_request *req)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-12-05 09:53:56 +00:00
|
|
|
return atmel_tdes_crypt(req, TDES_FLAGS_CBC | TDES_FLAGS_ENCRYPT);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_cbc_decrypt(struct skcipher_request *req)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
return atmel_tdes_crypt(req, TDES_FLAGS_CBC);
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static int atmel_tdes_init_tfm(struct crypto_skcipher *tfm)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2013-02-20 16:10:25 +00:00
|
|
|
|
2021-07-20 08:55:27 +00:00
|
|
|
ctx->dd = atmel_tdes_dev_alloc();
|
|
|
|
if (!ctx->dd)
|
2013-02-20 16:10:25 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2021-07-20 08:55:27 +00:00
|
|
|
crypto_skcipher_set_reqsize(tfm, sizeof(struct atmel_tdes_reqctx));
|
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-05 13:48:39 +00:00
|
|
|
static void atmel_tdes_skcipher_alg_init(struct skcipher_alg *alg)
|
|
|
|
{
|
|
|
|
alg->base.cra_priority = ATMEL_TDES_PRIORITY;
|
|
|
|
alg->base.cra_flags = CRYPTO_ALG_ASYNC;
|
2020-09-27 19:12:27 +00:00
|
|
|
alg->base.cra_ctxsize = sizeof(struct atmel_tdes_ctx);
|
2019-12-05 13:48:39 +00:00
|
|
|
alg->base.cra_module = THIS_MODULE;
|
|
|
|
|
|
|
|
alg->init = atmel_tdes_init_tfm;
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
static struct skcipher_alg tdes_algs[] = {
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
.base.cra_name = "ecb(des)",
|
|
|
|
.base.cra_driver_name = "atmel-ecb-des",
|
|
|
|
.base.cra_blocksize = DES_BLOCK_SIZE,
|
|
|
|
.base.cra_alignmask = 0x7,
|
|
|
|
|
|
|
|
.min_keysize = DES_KEY_SIZE,
|
|
|
|
.max_keysize = DES_KEY_SIZE,
|
|
|
|
.setkey = atmel_des_setkey,
|
|
|
|
.encrypt = atmel_tdes_ecb_encrypt,
|
|
|
|
.decrypt = atmel_tdes_ecb_decrypt,
|
2012-07-01 17:19:45 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
.base.cra_name = "cbc(des)",
|
|
|
|
.base.cra_driver_name = "atmel-cbc-des",
|
|
|
|
.base.cra_blocksize = DES_BLOCK_SIZE,
|
|
|
|
.base.cra_alignmask = 0x7,
|
|
|
|
|
|
|
|
.min_keysize = DES_KEY_SIZE,
|
|
|
|
.max_keysize = DES_KEY_SIZE,
|
|
|
|
.ivsize = DES_BLOCK_SIZE,
|
|
|
|
.setkey = atmel_des_setkey,
|
|
|
|
.encrypt = atmel_tdes_cbc_encrypt,
|
|
|
|
.decrypt = atmel_tdes_cbc_decrypt,
|
2012-07-01 17:19:45 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
.base.cra_name = "ecb(des3_ede)",
|
|
|
|
.base.cra_driver_name = "atmel-ecb-tdes",
|
|
|
|
.base.cra_blocksize = DES_BLOCK_SIZE,
|
|
|
|
.base.cra_alignmask = 0x7,
|
|
|
|
|
|
|
|
.min_keysize = DES3_EDE_KEY_SIZE,
|
|
|
|
.max_keysize = DES3_EDE_KEY_SIZE,
|
|
|
|
.setkey = atmel_tdes_setkey,
|
|
|
|
.encrypt = atmel_tdes_ecb_encrypt,
|
|
|
|
.decrypt = atmel_tdes_ecb_decrypt,
|
2012-07-01 17:19:45 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-09 17:09:34 +00:00
|
|
|
.base.cra_name = "cbc(des3_ede)",
|
|
|
|
.base.cra_driver_name = "atmel-cbc-tdes",
|
|
|
|
.base.cra_blocksize = DES_BLOCK_SIZE,
|
|
|
|
.base.cra_alignmask = 0x7,
|
|
|
|
|
|
|
|
.min_keysize = DES3_EDE_KEY_SIZE,
|
|
|
|
.max_keysize = DES3_EDE_KEY_SIZE,
|
|
|
|
.setkey = atmel_tdes_setkey,
|
|
|
|
.encrypt = atmel_tdes_cbc_encrypt,
|
|
|
|
.decrypt = atmel_tdes_cbc_decrypt,
|
|
|
|
.ivsize = DES_BLOCK_SIZE,
|
2012-07-01 17:19:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void atmel_tdes_queue_task(unsigned long data)
|
|
|
|
{
|
|
|
|
struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *)data;
|
|
|
|
|
|
|
|
atmel_tdes_handle_queue(dd, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void atmel_tdes_done_task(unsigned long data)
|
|
|
|
{
|
|
|
|
struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *) data;
|
|
|
|
int err;
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
if (!(dd->flags & TDES_FLAGS_DMA))
|
|
|
|
err = atmel_tdes_crypt_pdc_stop(dd);
|
|
|
|
else
|
|
|
|
err = atmel_tdes_crypt_dma_stop(dd);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
if (dd->total && !err) {
|
2013-02-20 16:10:25 +00:00
|
|
|
if (dd->flags & TDES_FLAGS_FAST) {
|
|
|
|
dd->in_sg = sg_next(dd->in_sg);
|
|
|
|
dd->out_sg = sg_next(dd->out_sg);
|
|
|
|
if (!dd->in_sg || !dd->out_sg)
|
|
|
|
err = -EINVAL;
|
|
|
|
}
|
2012-07-01 17:19:45 +00:00
|
|
|
if (!err)
|
2013-02-20 16:10:25 +00:00
|
|
|
err = atmel_tdes_crypt_start(dd);
|
|
|
|
if (!err)
|
2024-09-20 06:11:41 +00:00
|
|
|
return; /* DMA started. Not finishing. */
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
atmel_tdes_finish_req(dd, err);
|
|
|
|
atmel_tdes_handle_queue(dd, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t atmel_tdes_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct atmel_tdes_dev *tdes_dd = dev_id;
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
reg = atmel_tdes_read(tdes_dd, TDES_ISR);
|
|
|
|
if (reg & atmel_tdes_read(tdes_dd, TDES_IMR)) {
|
|
|
|
atmel_tdes_write(tdes_dd, TDES_IDR, reg);
|
|
|
|
if (TDES_FLAGS_BUSY & tdes_dd->flags)
|
|
|
|
tasklet_schedule(&tdes_dd->done_task);
|
|
|
|
else
|
|
|
|
dev_warn(tdes_dd->dev, "TDES interrupt when no active requests.\n");
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void atmel_tdes_unregister_algs(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(tdes_algs); i++)
|
2019-11-09 17:09:34 +00:00
|
|
|
crypto_unregister_skcipher(&tdes_algs[i]);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_tdes_register_algs(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
int err, i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(tdes_algs); i++) {
|
2019-12-05 13:48:39 +00:00
|
|
|
atmel_tdes_skcipher_alg_init(&tdes_algs[i]);
|
|
|
|
|
2019-11-09 17:09:34 +00:00
|
|
|
err = crypto_register_skcipher(&tdes_algs[i]);
|
2012-07-01 17:19:45 +00:00
|
|
|
if (err)
|
|
|
|
goto err_tdes_algs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_tdes_algs:
|
|
|
|
for (j = 0; j < i; j++)
|
2019-11-09 17:09:34 +00:00
|
|
|
crypto_unregister_skcipher(&tdes_algs[j]);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
static void atmel_tdes_get_cap(struct atmel_tdes_dev *dd)
|
|
|
|
{
|
|
|
|
|
|
|
|
dd->caps.has_dma = 0;
|
|
|
|
|
|
|
|
/* keep only major version number */
|
|
|
|
switch (dd->hw_version & 0xf00) {
|
2022-02-04 13:35:22 +00:00
|
|
|
case 0x800:
|
2013-02-20 16:10:25 +00:00
|
|
|
case 0x700:
|
|
|
|
dd->caps.has_dma = 1;
|
|
|
|
break;
|
|
|
|
case 0x600:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_warn(dd->dev,
|
|
|
|
"Unmanaged tdes version, set minimum capabilities\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 10:14:58 +00:00
|
|
|
static const struct of_device_id atmel_tdes_dt_ids[] = {
|
|
|
|
{ .compatible = "atmel,at91sam9g46-tdes" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, atmel_tdes_dt_ids);
|
|
|
|
|
2012-12-21 21:14:09 +00:00
|
|
|
static int atmel_tdes_probe(struct platform_device *pdev)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
|
|
|
struct atmel_tdes_dev *tdes_dd;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct resource *tdes_res;
|
|
|
|
int err;
|
|
|
|
|
2014-07-28 12:16:33 +00:00
|
|
|
tdes_dd = devm_kmalloc(&pdev->dev, sizeof(*tdes_dd), GFP_KERNEL);
|
2019-12-05 09:53:51 +00:00
|
|
|
if (!tdes_dd)
|
|
|
|
return -ENOMEM;
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
tdes_dd->dev = dev;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, tdes_dd);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&tdes_dd->list);
|
2015-04-07 09:45:08 +00:00
|
|
|
spin_lock_init(&tdes_dd->lock);
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
tasklet_init(&tdes_dd->done_task, atmel_tdes_done_task,
|
|
|
|
(unsigned long)tdes_dd);
|
|
|
|
tasklet_init(&tdes_dd->queue_task, atmel_tdes_queue_task,
|
|
|
|
(unsigned long)tdes_dd);
|
|
|
|
|
|
|
|
crypto_init_queue(&tdes_dd->queue, ATMEL_TDES_QUEUE_LENGTH);
|
|
|
|
|
2023-07-05 07:21:58 +00:00
|
|
|
tdes_dd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &tdes_res);
|
|
|
|
if (IS_ERR(tdes_dd->io_base)) {
|
|
|
|
err = PTR_ERR(tdes_dd->io_base);
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
tdes_dd->phys_base = tdes_res->start;
|
|
|
|
|
|
|
|
/* Get the IRQ */
|
|
|
|
tdes_dd->irq = platform_get_irq(pdev, 0);
|
|
|
|
if (tdes_dd->irq < 0) {
|
|
|
|
err = tdes_dd->irq;
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 17:47:03 +00:00
|
|
|
err = devm_request_irq(&pdev->dev, tdes_dd->irq, atmel_tdes_irq,
|
|
|
|
IRQF_SHARED, "atmel-tdes", tdes_dd);
|
2012-07-01 17:19:45 +00:00
|
|
|
if (err) {
|
|
|
|
dev_err(dev, "unable to request tdes irq.\n");
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initializing the clock */
|
2015-10-12 17:47:03 +00:00
|
|
|
tdes_dd->iclk = devm_clk_get(&pdev->dev, "tdes_clk");
|
2012-07-01 17:19:45 +00:00
|
|
|
if (IS_ERR(tdes_dd->iclk)) {
|
2015-02-28 20:40:10 +00:00
|
|
|
dev_err(dev, "clock initialization failed.\n");
|
2012-07-01 17:19:45 +00:00
|
|
|
err = PTR_ERR(tdes_dd->iclk);
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 09:53:50 +00:00
|
|
|
err = atmel_tdes_hw_version_init(tdes_dd);
|
|
|
|
if (err)
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
atmel_tdes_get_cap(tdes_dd);
|
|
|
|
|
|
|
|
err = atmel_tdes_buff_init(tdes_dd);
|
2012-07-01 17:19:45 +00:00
|
|
|
if (err)
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_tasklet_kill;
|
2013-02-20 16:10:25 +00:00
|
|
|
|
|
|
|
if (tdes_dd->caps.has_dma) {
|
2019-12-13 09:54:49 +00:00
|
|
|
err = atmel_tdes_dma_init(tdes_dd);
|
2013-02-20 16:10:25 +00:00
|
|
|
if (err)
|
2019-12-05 09:53:53 +00:00
|
|
|
goto err_buff_cleanup;
|
2013-10-15 10:14:58 +00:00
|
|
|
|
|
|
|
dev_info(dev, "using %s, %s for DMA transfers\n",
|
|
|
|
dma_chan_name(tdes_dd->dma_lch_in.chan),
|
|
|
|
dma_chan_name(tdes_dd->dma_lch_out.chan));
|
2013-02-20 16:10:25 +00:00
|
|
|
}
|
2012-07-01 17:19:45 +00:00
|
|
|
|
|
|
|
spin_lock(&atmel_tdes.lock);
|
|
|
|
list_add_tail(&tdes_dd->list, &atmel_tdes.dev_list);
|
|
|
|
spin_unlock(&atmel_tdes.lock);
|
|
|
|
|
|
|
|
err = atmel_tdes_register_algs(tdes_dd);
|
|
|
|
if (err)
|
|
|
|
goto err_algs;
|
|
|
|
|
|
|
|
dev_info(dev, "Atmel DES/TDES\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_algs:
|
|
|
|
spin_lock(&atmel_tdes.lock);
|
|
|
|
list_del(&tdes_dd->list);
|
|
|
|
spin_unlock(&atmel_tdes.lock);
|
2013-02-20 16:10:25 +00:00
|
|
|
if (tdes_dd->caps.has_dma)
|
|
|
|
atmel_tdes_dma_cleanup(tdes_dd);
|
2019-12-05 09:53:53 +00:00
|
|
|
err_buff_cleanup:
|
2013-02-20 16:10:25 +00:00
|
|
|
atmel_tdes_buff_cleanup(tdes_dd);
|
2019-12-05 09:53:53 +00:00
|
|
|
err_tasklet_kill:
|
2012-07-01 17:19:45 +00:00
|
|
|
tasklet_kill(&tdes_dd->done_task);
|
|
|
|
tasklet_kill(&tdes_dd->queue_task);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-10-20 07:55:32 +00:00
|
|
|
static void atmel_tdes_remove(struct platform_device *pdev)
|
2012-07-01 17:19:45 +00:00
|
|
|
{
|
2022-07-06 09:17:27 +00:00
|
|
|
struct atmel_tdes_dev *tdes_dd = platform_get_drvdata(pdev);
|
2022-07-05 20:51:40 +00:00
|
|
|
|
2012-07-01 17:19:45 +00:00
|
|
|
spin_lock(&atmel_tdes.lock);
|
|
|
|
list_del(&tdes_dd->list);
|
|
|
|
spin_unlock(&atmel_tdes.lock);
|
|
|
|
|
|
|
|
atmel_tdes_unregister_algs(tdes_dd);
|
|
|
|
|
|
|
|
tasklet_kill(&tdes_dd->done_task);
|
|
|
|
tasklet_kill(&tdes_dd->queue_task);
|
|
|
|
|
2013-02-20 16:10:25 +00:00
|
|
|
if (tdes_dd->caps.has_dma)
|
|
|
|
atmel_tdes_dma_cleanup(tdes_dd);
|
|
|
|
|
|
|
|
atmel_tdes_buff_cleanup(tdes_dd);
|
2012-07-01 17:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver atmel_tdes_driver = {
|
|
|
|
.probe = atmel_tdes_probe,
|
2024-10-07 20:58:06 +00:00
|
|
|
.remove = atmel_tdes_remove,
|
2012-07-01 17:19:45 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "atmel_tdes",
|
2023-07-14 17:44:18 +00:00
|
|
|
.of_match_table = atmel_tdes_dt_ids,
|
2012-07-01 17:19:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(atmel_tdes_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");
|