15b3d4d2eSTero Kristo /* 25b3d4d2eSTero Kristo * Cryptographic API. 35b3d4d2eSTero Kristo * 45b3d4d2eSTero Kristo * Support for OMAP AES HW ACCELERATOR defines 55b3d4d2eSTero Kristo * 65b3d4d2eSTero Kristo * Copyright (c) 2015 Texas Instruments Incorporated 75b3d4d2eSTero Kristo * 85b3d4d2eSTero Kristo * This program is free software; you can redistribute it and/or modify 95b3d4d2eSTero Kristo * it under the terms of the GNU General Public License version 2 as published 105b3d4d2eSTero Kristo * by the Free Software Foundation. 115b3d4d2eSTero Kristo * 125b3d4d2eSTero Kristo */ 135b3d4d2eSTero Kristo #ifndef __OMAP_AES_H__ 145b3d4d2eSTero Kristo #define __OMAP_AES_H__ 155b3d4d2eSTero Kristo 16c21c8b89SCorentin LABBE #include <crypto/engine.h> 17c21c8b89SCorentin LABBE 185b3d4d2eSTero Kristo #define DST_MAXBURST 4 195b3d4d2eSTero Kristo #define DMA_MIN (DST_MAXBURST * sizeof(u32)) 205b3d4d2eSTero Kristo 215b3d4d2eSTero Kristo #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset) 225b3d4d2eSTero Kristo 235b3d4d2eSTero Kristo /* 245b3d4d2eSTero Kristo * OMAP TRM gives bitfields as start:end, where start is the higher bit 255b3d4d2eSTero Kristo * number. For example 7:0 265b3d4d2eSTero Kristo */ 275b3d4d2eSTero Kristo #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end)) 285b3d4d2eSTero Kristo #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end)) 295b3d4d2eSTero Kristo 305b3d4d2eSTero Kristo #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \ 315b3d4d2eSTero Kristo (((x) ^ 0x01) * 0x04)) 325b3d4d2eSTero Kristo #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04)) 335b3d4d2eSTero Kristo 345b3d4d2eSTero Kristo #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs) 35ad18cc9dSTero Kristo #define AES_REG_CTRL_CONTEXT_READY BIT(31) 365b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7) 375b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_32 0 385b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7) 395b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8) 405b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7) 41ad18cc9dSTero Kristo #define AES_REG_CTRL_GCM GENMASK(17, 16) 425b3d4d2eSTero Kristo #define AES_REG_CTRL_CTR BIT(6) 435b3d4d2eSTero Kristo #define AES_REG_CTRL_CBC BIT(5) 445b3d4d2eSTero Kristo #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3) 455b3d4d2eSTero Kristo #define AES_REG_CTRL_DIRECTION BIT(2) 465b3d4d2eSTero Kristo #define AES_REG_CTRL_INPUT_READY BIT(1) 475b3d4d2eSTero Kristo #define AES_REG_CTRL_OUTPUT_READY BIT(0) 485b3d4d2eSTero Kristo #define AES_REG_CTRL_MASK GENMASK(24, 2) 495b3d4d2eSTero Kristo 50ad18cc9dSTero Kristo #define AES_REG_C_LEN_0 0x54 51ad18cc9dSTero Kristo #define AES_REG_C_LEN_1 0x58 52ad18cc9dSTero Kristo #define AES_REG_A_LEN 0x5C 53ad18cc9dSTero Kristo 545b3d4d2eSTero Kristo #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04)) 55ad18cc9dSTero Kristo #define AES_REG_TAG_N(dd, x) (0x70 + ((x) * 0x04)) 565b3d4d2eSTero Kristo 575b3d4d2eSTero Kristo #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs) 585b3d4d2eSTero Kristo 595b3d4d2eSTero Kristo #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs) 605b3d4d2eSTero Kristo #define AES_REG_MASK_SIDLE BIT(6) 615b3d4d2eSTero Kristo #define AES_REG_MASK_START BIT(5) 625b3d4d2eSTero Kristo #define AES_REG_MASK_DMA_OUT_EN BIT(3) 635b3d4d2eSTero Kristo #define AES_REG_MASK_DMA_IN_EN BIT(2) 645b3d4d2eSTero Kristo #define AES_REG_MASK_SOFTRESET BIT(1) 655b3d4d2eSTero Kristo #define AES_REG_AUTOIDLE BIT(0) 665b3d4d2eSTero Kristo 675b3d4d2eSTero Kristo #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04)) 685b3d4d2eSTero Kristo 695b3d4d2eSTero Kristo #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs) 705b3d4d2eSTero Kristo #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs) 715b3d4d2eSTero Kristo #define AES_REG_IRQ_DATA_IN BIT(1) 725b3d4d2eSTero Kristo #define AES_REG_IRQ_DATA_OUT BIT(2) 735b3d4d2eSTero Kristo #define DEFAULT_TIMEOUT (5 * HZ) 745b3d4d2eSTero Kristo 755b3d4d2eSTero Kristo #define DEFAULT_AUTOSUSPEND_DELAY 1000 765b3d4d2eSTero Kristo 77ad18cc9dSTero Kristo #define FLAGS_MODE_MASK 0x001f 785b3d4d2eSTero Kristo #define FLAGS_ENCRYPT BIT(0) 795b3d4d2eSTero Kristo #define FLAGS_CBC BIT(1) 80ad18cc9dSTero Kristo #define FLAGS_CTR BIT(2) 81ad18cc9dSTero Kristo #define FLAGS_GCM BIT(3) 82ad18cc9dSTero Kristo #define FLAGS_RFC4106_GCM BIT(4) 835b3d4d2eSTero Kristo 84ad18cc9dSTero Kristo #define FLAGS_INIT BIT(5) 85ad18cc9dSTero Kristo #define FLAGS_FAST BIT(6) 86ad18cc9dSTero Kristo #define FLAGS_BUSY BIT(7) 875b3d4d2eSTero Kristo 885b3d4d2eSTero Kristo #define FLAGS_IN_DATA_ST_SHIFT 8 895b3d4d2eSTero Kristo #define FLAGS_OUT_DATA_ST_SHIFT 10 90ad18cc9dSTero Kristo #define FLAGS_ASSOC_DATA_ST_SHIFT 12 915b3d4d2eSTero Kristo 925b3d4d2eSTero Kristo #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2) 935b3d4d2eSTero Kristo 94ad18cc9dSTero Kristo struct omap_aes_gcm_result { 95ad18cc9dSTero Kristo struct completion completion; 96ad18cc9dSTero Kristo int err; 97ad18cc9dSTero Kristo }; 98ad18cc9dSTero Kristo 995b3d4d2eSTero Kristo struct omap_aes_ctx { 100c21c8b89SCorentin LABBE struct crypto_engine_ctx enginectx; 1015b3d4d2eSTero Kristo int keylen; 1025b3d4d2eSTero Kristo u32 key[AES_KEYSIZE_256 / sizeof(u32)]; 103ad18cc9dSTero Kristo u8 nonce[4]; 104e87f203cSKees Cook struct crypto_sync_skcipher *fallback; 105ad18cc9dSTero Kristo struct crypto_skcipher *ctr; 1065b3d4d2eSTero Kristo }; 1075b3d4d2eSTero Kristo 1085b3d4d2eSTero Kristo struct omap_aes_reqctx { 1095b3d4d2eSTero Kristo struct omap_aes_dev *dd; 1105b3d4d2eSTero Kristo unsigned long mode; 111ad18cc9dSTero Kristo u8 iv[AES_BLOCK_SIZE]; 112ad18cc9dSTero Kristo u32 auth_tag[AES_BLOCK_SIZE / sizeof(u32)]; 1135b3d4d2eSTero Kristo }; 1145b3d4d2eSTero Kristo 1155b3d4d2eSTero Kristo #define OMAP_AES_QUEUE_LENGTH 1 1165b3d4d2eSTero Kristo #define OMAP_AES_CACHE_SIZE 0 1175b3d4d2eSTero Kristo 1185b3d4d2eSTero Kristo struct omap_aes_algs_info { 1195b3d4d2eSTero Kristo struct crypto_alg *algs_list; 1205b3d4d2eSTero Kristo unsigned int size; 1215b3d4d2eSTero Kristo unsigned int registered; 1225b3d4d2eSTero Kristo }; 1235b3d4d2eSTero Kristo 124ad18cc9dSTero Kristo struct omap_aes_aead_algs { 125ad18cc9dSTero Kristo struct aead_alg *algs_list; 126ad18cc9dSTero Kristo unsigned int size; 127ad18cc9dSTero Kristo unsigned int registered; 128ad18cc9dSTero Kristo }; 129ad18cc9dSTero Kristo 1305b3d4d2eSTero Kristo struct omap_aes_pdata { 1315b3d4d2eSTero Kristo struct omap_aes_algs_info *algs_info; 1325b3d4d2eSTero Kristo unsigned int algs_info_size; 133ad18cc9dSTero Kristo struct omap_aes_aead_algs *aead_algs_info; 1345b3d4d2eSTero Kristo 1355b3d4d2eSTero Kristo void (*trigger)(struct omap_aes_dev *dd, int length); 1365b3d4d2eSTero Kristo 1375b3d4d2eSTero Kristo u32 key_ofs; 1385b3d4d2eSTero Kristo u32 iv_ofs; 1395b3d4d2eSTero Kristo u32 ctrl_ofs; 1405b3d4d2eSTero Kristo u32 data_ofs; 1415b3d4d2eSTero Kristo u32 rev_ofs; 1425b3d4d2eSTero Kristo u32 mask_ofs; 1435b3d4d2eSTero Kristo u32 irq_enable_ofs; 1445b3d4d2eSTero Kristo u32 irq_status_ofs; 1455b3d4d2eSTero Kristo 1465b3d4d2eSTero Kristo u32 dma_enable_in; 1475b3d4d2eSTero Kristo u32 dma_enable_out; 1485b3d4d2eSTero Kristo u32 dma_start; 1495b3d4d2eSTero Kristo 1505b3d4d2eSTero Kristo u32 major_mask; 1515b3d4d2eSTero Kristo u32 major_shift; 1525b3d4d2eSTero Kristo u32 minor_mask; 1535b3d4d2eSTero Kristo u32 minor_shift; 1545b3d4d2eSTero Kristo }; 1555b3d4d2eSTero Kristo 1565b3d4d2eSTero Kristo struct omap_aes_dev { 1575b3d4d2eSTero Kristo struct list_head list; 1585b3d4d2eSTero Kristo unsigned long phys_base; 1595b3d4d2eSTero Kristo void __iomem *io_base; 1605b3d4d2eSTero Kristo struct omap_aes_ctx *ctx; 1615b3d4d2eSTero Kristo struct device *dev; 1625b3d4d2eSTero Kristo unsigned long flags; 1635b3d4d2eSTero Kristo int err; 1645b3d4d2eSTero Kristo 1655b3d4d2eSTero Kristo struct tasklet_struct done_task; 166ad18cc9dSTero Kristo struct aead_queue aead_queue; 167ad18cc9dSTero Kristo spinlock_t lock; 1685b3d4d2eSTero Kristo 1695b3d4d2eSTero Kristo struct ablkcipher_request *req; 170ad18cc9dSTero Kristo struct aead_request *aead_req; 1715b3d4d2eSTero Kristo struct crypto_engine *engine; 1725b3d4d2eSTero Kristo 1735b3d4d2eSTero Kristo /* 1745b3d4d2eSTero Kristo * total is used by PIO mode for book keeping so introduce 1755b3d4d2eSTero Kristo * variable total_save as need it to calc page_order 1765b3d4d2eSTero Kristo */ 1775b3d4d2eSTero Kristo size_t total; 1785b3d4d2eSTero Kristo size_t total_save; 179ad18cc9dSTero Kristo size_t assoc_len; 180ad18cc9dSTero Kristo size_t authsize; 1815b3d4d2eSTero Kristo 1825b3d4d2eSTero Kristo struct scatterlist *in_sg; 1835b3d4d2eSTero Kristo struct scatterlist *out_sg; 1845b3d4d2eSTero Kristo 1855b3d4d2eSTero Kristo /* Buffers for copying for unaligned cases */ 186ad18cc9dSTero Kristo struct scatterlist in_sgl[2]; 1875b3d4d2eSTero Kristo struct scatterlist out_sgl; 1885b3d4d2eSTero Kristo struct scatterlist *orig_out; 1895b3d4d2eSTero Kristo 1905b3d4d2eSTero Kristo struct scatter_walk in_walk; 1915b3d4d2eSTero Kristo struct scatter_walk out_walk; 1925b3d4d2eSTero Kristo struct dma_chan *dma_lch_in; 1935b3d4d2eSTero Kristo struct dma_chan *dma_lch_out; 1945b3d4d2eSTero Kristo int in_sg_len; 1955b3d4d2eSTero Kristo int out_sg_len; 1965b3d4d2eSTero Kristo int pio_only; 1975b3d4d2eSTero Kristo const struct omap_aes_pdata *pdata; 1985b3d4d2eSTero Kristo }; 1995b3d4d2eSTero Kristo 200d695bfd6STero Kristo u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset); 201d695bfd6STero Kristo void omap_aes_write(struct omap_aes_dev *dd, u32 offset, u32 value); 202d695bfd6STero Kristo struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx); 203ad18cc9dSTero Kristo int omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key, 204ad18cc9dSTero Kristo unsigned int keylen); 205ad18cc9dSTero Kristo int omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key, 206ad18cc9dSTero Kristo unsigned int keylen); 207ad18cc9dSTero Kristo int omap_aes_gcm_encrypt(struct aead_request *req); 208ad18cc9dSTero Kristo int omap_aes_gcm_decrypt(struct aead_request *req); 209ad18cc9dSTero Kristo int omap_aes_4106gcm_encrypt(struct aead_request *req); 210ad18cc9dSTero Kristo int omap_aes_4106gcm_decrypt(struct aead_request *req); 211d695bfd6STero Kristo int omap_aes_write_ctrl(struct omap_aes_dev *dd); 212d695bfd6STero Kristo int omap_aes_crypt_dma_start(struct omap_aes_dev *dd); 213d695bfd6STero Kristo int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd); 214ad18cc9dSTero Kristo void omap_aes_gcm_dma_out_callback(void *data); 215ad18cc9dSTero Kristo void omap_aes_clear_copy_flags(struct omap_aes_dev *dd); 216d695bfd6STero Kristo 2175b3d4d2eSTero Kristo #endif 218