1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 25b3d4d2eSTero Kristo /* 35b3d4d2eSTero Kristo * Cryptographic API. 45b3d4d2eSTero Kristo * 55b3d4d2eSTero Kristo * Support for OMAP AES HW ACCELERATOR defines 65b3d4d2eSTero Kristo * 75b3d4d2eSTero Kristo * Copyright (c) 2015 Texas Instruments Incorporated 85b3d4d2eSTero Kristo */ 95b3d4d2eSTero Kristo #ifndef __OMAP_AES_H__ 105b3d4d2eSTero Kristo #define __OMAP_AES_H__ 115b3d4d2eSTero Kristo 12c21c8b89SCorentin LABBE #include <crypto/engine.h> 13c21c8b89SCorentin LABBE 145b3d4d2eSTero Kristo #define DST_MAXBURST 4 155b3d4d2eSTero Kristo #define DMA_MIN (DST_MAXBURST * sizeof(u32)) 165b3d4d2eSTero Kristo 175b3d4d2eSTero Kristo #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset) 185b3d4d2eSTero Kristo 195b3d4d2eSTero Kristo /* 205b3d4d2eSTero Kristo * OMAP TRM gives bitfields as start:end, where start is the higher bit 215b3d4d2eSTero Kristo * number. For example 7:0 225b3d4d2eSTero Kristo */ 235b3d4d2eSTero Kristo #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end)) 245b3d4d2eSTero Kristo #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end)) 255b3d4d2eSTero Kristo 265b3d4d2eSTero Kristo #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \ 275b3d4d2eSTero Kristo (((x) ^ 0x01) * 0x04)) 285b3d4d2eSTero Kristo #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04)) 295b3d4d2eSTero Kristo 305b3d4d2eSTero Kristo #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs) 31ad18cc9dSTero Kristo #define AES_REG_CTRL_CONTEXT_READY BIT(31) 325b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7) 335b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_32 0 345b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7) 355b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8) 365b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7) 37ad18cc9dSTero Kristo #define AES_REG_CTRL_GCM GENMASK(17, 16) 385b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR BIT(6) 395b3d4d2eSTero Kristo #define AES_REG_CTRL_CBC BIT(5) 405b3d4d2eSTero Kristo #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3) 415b3d4d2eSTero Kristo #define AES_REG_CTRL_DIRECTION BIT(2) 425b3d4d2eSTero Kristo #define AES_REG_CTRL_INPUT_READY BIT(1) 435b3d4d2eSTero Kristo #define AES_REG_CTRL_OUTPUT_READY BIT(0) 445b3d4d2eSTero Kristo #define AES_REG_CTRL_MASK GENMASK(24, 2) 455b3d4d2eSTero Kristo 46ad18cc9dSTero Kristo #define AES_REG_C_LEN_0 0x54 47ad18cc9dSTero Kristo #define AES_REG_C_LEN_1 0x58 48ad18cc9dSTero Kristo #define AES_REG_A_LEN 0x5C 49ad18cc9dSTero Kristo 505b3d4d2eSTero Kristo #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04)) 51ad18cc9dSTero Kristo #define AES_REG_TAG_N(dd, x) (0x70 + ((x) * 0x04)) 525b3d4d2eSTero Kristo 535b3d4d2eSTero Kristo #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs) 545b3d4d2eSTero Kristo 555b3d4d2eSTero Kristo #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs) 565b3d4d2eSTero Kristo #define AES_REG_MASK_SIDLE BIT(6) 575b3d4d2eSTero Kristo #define AES_REG_MASK_START BIT(5) 585b3d4d2eSTero Kristo #define AES_REG_MASK_DMA_OUT_EN BIT(3) 595b3d4d2eSTero Kristo #define AES_REG_MASK_DMA_IN_EN BIT(2) 605b3d4d2eSTero Kristo #define AES_REG_MASK_SOFTRESET BIT(1) 615b3d4d2eSTero Kristo #define AES_REG_AUTOIDLE BIT(0) 625b3d4d2eSTero Kristo 635b3d4d2eSTero Kristo #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04)) 645b3d4d2eSTero Kristo 655b3d4d2eSTero Kristo #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs) 665b3d4d2eSTero Kristo #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs) 675b3d4d2eSTero Kristo #define AES_REG_IRQ_DATA_IN BIT(1) 685b3d4d2eSTero Kristo #define AES_REG_IRQ_DATA_OUT BIT(2) 695b3d4d2eSTero Kristo #define DEFAULT_TIMEOUT (5 * HZ) 705b3d4d2eSTero Kristo 715b3d4d2eSTero Kristo #define DEFAULT_AUTOSUSPEND_DELAY 1000 725b3d4d2eSTero Kristo 73ad18cc9dSTero Kristo #define FLAGS_MODE_MASK 0x001f 745b3d4d2eSTero Kristo #define FLAGS_ENCRYPT BIT(0) 755b3d4d2eSTero Kristo #define FLAGS_CBC BIT(1) 76ad18cc9dSTero Kristo #define FLAGS_CTR BIT(2) 77ad18cc9dSTero Kristo #define FLAGS_GCM BIT(3) 78ad18cc9dSTero Kristo #define FLAGS_RFC4106_GCM BIT(4) 795b3d4d2eSTero Kristo 80ad18cc9dSTero Kristo #define FLAGS_INIT BIT(5) 81ad18cc9dSTero Kristo #define FLAGS_FAST BIT(6) 82ad18cc9dSTero Kristo #define FLAGS_BUSY BIT(7) 835b3d4d2eSTero Kristo 845b3d4d2eSTero Kristo #define FLAGS_IN_DATA_ST_SHIFT 8 855b3d4d2eSTero Kristo #define FLAGS_OUT_DATA_ST_SHIFT 10 86ad18cc9dSTero Kristo #define FLAGS_ASSOC_DATA_ST_SHIFT 12 875b3d4d2eSTero Kristo 885b3d4d2eSTero Kristo #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2) 895b3d4d2eSTero Kristo 90ad18cc9dSTero Kristo struct omap_aes_gcm_result { 91ad18cc9dSTero Kristo struct completion completion; 92ad18cc9dSTero Kristo int err; 93ad18cc9dSTero Kristo }; 94ad18cc9dSTero Kristo 955b3d4d2eSTero Kristo struct omap_aes_ctx { 96c21c8b89SCorentin LABBE struct crypto_engine_ctx enginectx; 975b3d4d2eSTero Kristo int keylen; 985b3d4d2eSTero Kristo u32 key[AES_KEYSIZE_256 / sizeof(u32)]; 99ad18cc9dSTero Kristo u8 nonce[4]; 100e87f203cSKees Cook struct crypto_sync_skcipher *fallback; 101ad18cc9dSTero Kristo struct crypto_skcipher *ctr; 1025b3d4d2eSTero Kristo }; 1035b3d4d2eSTero Kristo 1045b3d4d2eSTero Kristo struct omap_aes_reqctx { 1055b3d4d2eSTero Kristo struct omap_aes_dev *dd; 1065b3d4d2eSTero Kristo unsigned long mode; 107ad18cc9dSTero Kristo u8 iv[AES_BLOCK_SIZE]; 108ad18cc9dSTero Kristo u32 auth_tag[AES_BLOCK_SIZE / sizeof(u32)]; 1095b3d4d2eSTero Kristo }; 1105b3d4d2eSTero Kristo 1115b3d4d2eSTero Kristo #define OMAP_AES_QUEUE_LENGTH 1 1125b3d4d2eSTero Kristo #define OMAP_AES_CACHE_SIZE 0 1135b3d4d2eSTero Kristo 1145b3d4d2eSTero Kristo struct omap_aes_algs_info { 115b3e3f0feSArd Biesheuvel struct skcipher_alg *algs_list; 1165b3d4d2eSTero Kristo unsigned int size; 1175b3d4d2eSTero Kristo unsigned int registered; 1185b3d4d2eSTero Kristo }; 1195b3d4d2eSTero Kristo 120ad18cc9dSTero Kristo struct omap_aes_aead_algs { 121ad18cc9dSTero Kristo struct aead_alg *algs_list; 122ad18cc9dSTero Kristo unsigned int size; 123ad18cc9dSTero Kristo unsigned int registered; 124ad18cc9dSTero Kristo }; 125ad18cc9dSTero Kristo 1265b3d4d2eSTero Kristo struct omap_aes_pdata { 1275b3d4d2eSTero Kristo struct omap_aes_algs_info *algs_info; 1285b3d4d2eSTero Kristo unsigned int algs_info_size; 129ad18cc9dSTero Kristo struct omap_aes_aead_algs *aead_algs_info; 1305b3d4d2eSTero Kristo 1315b3d4d2eSTero Kristo void (*trigger)(struct omap_aes_dev *dd, int length); 1325b3d4d2eSTero Kristo 1335b3d4d2eSTero Kristo u32 key_ofs; 1345b3d4d2eSTero Kristo u32 iv_ofs; 1355b3d4d2eSTero Kristo u32 ctrl_ofs; 1365b3d4d2eSTero Kristo u32 data_ofs; 1375b3d4d2eSTero Kristo u32 rev_ofs; 1385b3d4d2eSTero Kristo u32 mask_ofs; 1395b3d4d2eSTero Kristo u32 irq_enable_ofs; 1405b3d4d2eSTero Kristo u32 irq_status_ofs; 1415b3d4d2eSTero Kristo 1425b3d4d2eSTero Kristo u32 dma_enable_in; 1435b3d4d2eSTero Kristo u32 dma_enable_out; 1445b3d4d2eSTero Kristo u32 dma_start; 1455b3d4d2eSTero Kristo 1465b3d4d2eSTero Kristo u32 major_mask; 1475b3d4d2eSTero Kristo u32 major_shift; 1485b3d4d2eSTero Kristo u32 minor_mask; 1495b3d4d2eSTero Kristo u32 minor_shift; 1505b3d4d2eSTero Kristo }; 1515b3d4d2eSTero Kristo 1525b3d4d2eSTero Kristo struct omap_aes_dev { 1535b3d4d2eSTero Kristo struct list_head list; 1545b3d4d2eSTero Kristo unsigned long phys_base; 1555b3d4d2eSTero Kristo void __iomem *io_base; 1565b3d4d2eSTero Kristo struct omap_aes_ctx *ctx; 1575b3d4d2eSTero Kristo struct device *dev; 1585b3d4d2eSTero Kristo unsigned long flags; 1595b3d4d2eSTero Kristo int err; 1605b3d4d2eSTero Kristo 1615b3d4d2eSTero Kristo struct tasklet_struct done_task; 162ad18cc9dSTero Kristo struct aead_queue aead_queue; 163ad18cc9dSTero Kristo spinlock_t lock; 1645b3d4d2eSTero Kristo 165b3e3f0feSArd Biesheuvel struct skcipher_request *req; 166ad18cc9dSTero Kristo struct aead_request *aead_req; 1675b3d4d2eSTero Kristo struct crypto_engine *engine; 1685b3d4d2eSTero Kristo 1695b3d4d2eSTero Kristo /* 1705b3d4d2eSTero Kristo * total is used by PIO mode for book keeping so introduce 1715b3d4d2eSTero Kristo * variable total_save as need it to calc page_order 1725b3d4d2eSTero Kristo */ 1735b3d4d2eSTero Kristo size_t total; 1745b3d4d2eSTero Kristo size_t total_save; 175ad18cc9dSTero Kristo size_t assoc_len; 176ad18cc9dSTero Kristo size_t authsize; 1775b3d4d2eSTero Kristo 1785b3d4d2eSTero Kristo struct scatterlist *in_sg; 1795b3d4d2eSTero Kristo struct scatterlist *out_sg; 1805b3d4d2eSTero Kristo 1815b3d4d2eSTero Kristo /* Buffers for copying for unaligned cases */ 182ad18cc9dSTero Kristo struct scatterlist in_sgl[2]; 1835b3d4d2eSTero Kristo struct scatterlist out_sgl; 1845b3d4d2eSTero Kristo struct scatterlist *orig_out; 1855b3d4d2eSTero Kristo 1865b3d4d2eSTero Kristo struct scatter_walk in_walk; 1875b3d4d2eSTero Kristo struct scatter_walk out_walk; 1885b3d4d2eSTero Kristo struct dma_chan *dma_lch_in; 1895b3d4d2eSTero Kristo struct dma_chan *dma_lch_out; 1905b3d4d2eSTero Kristo int in_sg_len; 1915b3d4d2eSTero Kristo int out_sg_len; 1925b3d4d2eSTero Kristo int pio_only; 1935b3d4d2eSTero Kristo const struct omap_aes_pdata *pdata; 1945b3d4d2eSTero Kristo }; 1955b3d4d2eSTero Kristo 196d695bfd6STero Kristo u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset); 197d695bfd6STero Kristo void omap_aes_write(struct omap_aes_dev *dd, u32 offset, u32 value); 198d695bfd6STero Kristo struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx); 199ad18cc9dSTero Kristo int omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key, 200ad18cc9dSTero Kristo unsigned int keylen); 201ad18cc9dSTero Kristo int omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key, 202ad18cc9dSTero Kristo unsigned int keylen); 203ad18cc9dSTero Kristo int omap_aes_gcm_encrypt(struct aead_request *req); 204ad18cc9dSTero Kristo int omap_aes_gcm_decrypt(struct aead_request *req); 205ad18cc9dSTero Kristo int omap_aes_4106gcm_encrypt(struct aead_request *req); 206ad18cc9dSTero Kristo int omap_aes_4106gcm_decrypt(struct aead_request *req); 207d695bfd6STero Kristo int omap_aes_write_ctrl(struct omap_aes_dev *dd); 208d695bfd6STero Kristo int omap_aes_crypt_dma_start(struct omap_aes_dev *dd); 209d695bfd6STero Kristo int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd); 210ad18cc9dSTero Kristo void omap_aes_gcm_dma_out_callback(void *data); 211ad18cc9dSTero Kristo void omap_aes_clear_copy_flags(struct omap_aes_dev *dd); 212d695bfd6STero Kristo 2135b3d4d2eSTero Kristo #endif 214